tencent cloud

Tencent Cloud EdgeOne

Encoding

ダウンロード
フォーカスモード
フォントサイズ
最終更新日: 2026-10-15 18:20:38
AI翻訳
Web APIs標準TextEncoder、TextDecoderに基づいて設計され、エンコーダーとデコーダーを実装しています。

TextEncoder

エンコーダーは、コードポイントストリームを入力として受け取り、UTF-8バイトストリームを出力します。MDN公式ドキュメントTextEncoderを参照してください。

コンストラクタ

// TextEncoderコンストラクタは、いかなる引数も受け取りません。
const encoder = new TextEncoder();

属性

// encoder.encoding
readonly encoding: string;
エンコーダーのエンコードタイプは、現在の値はUTF-8のみです。

方法

encode

encoder.encode(input?: string | undefined): Uint8Array
コードポイントストリームを入力として受け取り、UTF-8バイトストリームを出力します。
注意:
inputの最大長は300Mです。長さを超えると例外がスローされます。
encoder.encodeパラメータ
パラメータ名
タイプ
必須
説明
input
string | undefined
いいえ
エンコーダー入力値。

encodeInto

encoder.encodeInto(input: string, destination: Uint8Array): EncodeIntoResult;
コードポイントストリームを入力として受け取り、UTF-8バイトストリームを出力し、パラメータdestinationのバイト配列に書き込みます。
パラメータ
パラメータ名
タイプ
必須
説明
input
string
はい
エンコーダー入力値。
destination
はい
エンコーダー出力値。
戻り値EncodeIntoResult
属性名
タイプ
説明
read
number
UTF-8に変換されたUTF-16ユニット数。
written
number
対象Uint8Array内で変更されたバイト数。

TextDecoder

デコーダー。バイトストリームを入力として受け取り、コードポイントストリームを出力します。MDN公式ドキュメントTextDecoderを参照してください。

コンストラクタ

const decoder = new TextDecoder(label?: string | undefined, options?: DecoderOptions | undefined): TextEncoder;

パラメータ

注意:
パラメータlabelでは、以下の値は現在サポートされていません:
iso-8859-16。
hz-gb-2312。
csiso2022kr、iso-2022-kr。
パラメータ名
タイプ
必須
説明
label
string | undefined
いいえ
デコーダータイプ。デフォルト値はUTF-8です。オプションのlabel値についてはMDN公式ドキュメントを参照してください。
options
DecoderOptions | undefined
いいえ
デコーダー設定項目

DecoderOptions

デコーダー設定項目は以下の通りです。
属性名
タイプ
デフォルト値
説明
fatal
boolean
false
デコード失敗時に例外をスローするかどうかを示します。
ignoreBOM
boolean
false
byte-order markerを無視するかどうかを示します。

属性

encoding

// decoder.encoding
readonly encoding: string;
デコーダータイプ。

fatal

// decoder.fatal
readonly fatal: boolean;
デコード失敗時に、例外をスローするかどうかを示します。

ignoreBOM

// decoder.ignoreBOM
readonly ignoreBOM: boolean;
byte-order markerを無視するかどうかを示します。

方法

decode

const result = decoder.decode(buffer?: ArrayBuffer | ArrayBufferView | undefined, options?: DecodeOptions | undefined): string;
注意:
パラメータ buffer の最大長は100Mです。長さを超えると例外がスローされます。
パラメータ名
タイプ
必須
説明
buffer
いいえ
デコード対象のバイトストリーム。
bufferの最大長は100Mです。長さを超えると例外がスローされます。
options
いいえ
デコード設定項目を実行します。

DecodeOptions

デコード実行設定項目は以下の通りです。
属性名
タイプ
デフォルト値
説明
stream
boolean
false
ストリーミングデコードを設定します。デフォルトは false で、値の説明は以下の通りです。
true
データをチャンク単位で処理することを表します。つまり、ストリーミングデコードです。
false
チャンクが終了したか、またはチャンクを使用してデータを処理していないこと、つまり非ストリーミングデコードを表します。

サンプルコード

function handleEvent(event) {
// エンコーダー
const encoder = new TextEncoder();
const encodeText = encoder.encode('hello world');

// デコーダー
const decoder = new TextDecoder();
const decodeText = decoder.decode(encodeText);

// クライアント応答内容
const response = new Response(JSON.stringify({
encodeText: encodeText.toString(),
decodeText,
}));

return response;
}

addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event));
});

関連参考

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック