TransformStreamDefaultController
TransformStreamDefaultController` はストリーム API のインターフェイスで、関連する ReadableStream
と WritableStream
を操作するメソッドを提供します。
TransformStream
を作成すると、 TransformStreamDefaultController
が作成されます。そのため、コンストラクターはありません。 TransformStreamDefaultController
のインスタンスを取得するには、 TransformStream()
のコールバックメソッドを使用します。
インスタンスプロパティ
TransformStreamDefaultController.desiredSize
読取専用-
ストリームの内部キューの読み取り可能な側を埋めるために必要なサイズを返します。
インスタンスメソッド
TransformStreamDefaultController.enqueue()
-
ストリームの読み取り可能な側にチャンク(単一のデータ)をキューに入れます。
TransformStreamDefaultController.error()
-
変換ストリームの読み取り可能な側と書き込み可能な側の両方をエラーを発生させます。
TransformStreamDefaultController.terminate()
-
ストリームの読み取り可能な側を閉じ、書き込み可能な側にエラーを発生させます。
例
次の例では、変換ストリームは、 error()
メソッドと enqueue()
メソッドを使用して、受信したチャンクをすべて Uint8Array
値として渡します。
js
const transformContent = {
start() {}, // required.
async transform(chunk, controller) {
chunk = await chunk;
switch (typeof chunk) {
case "object":
// just say the stream is done I guess
if (chunk === null) {
controller.terminate();
} else if (ArrayBuffer.isView(chunk)) {
controller.enqueue(
new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength),
);
} else if (
Array.isArray(chunk) &&
chunk.every((value) => typeof value === "number")
) {
controller.enqueue(new Uint8Array(chunk));
} else if (
typeof chunk.valueOf === "function" &&
chunk.valueOf() !== chunk
) {
this.transform(chunk.valueOf(), controller); // hack
} else if ("toJSON" in chunk) {
this.transform(JSON.stringify(chunk), controller);
}
break;
case "symbol":
controller.error("Cannot send a symbol as a chunk part");
break;
case "undefined":
controller.error("Cannot send undefined as a chunk part");
break;
default:
controller.enqueue(this.textencoder.encode(String(chunk)));
break;
}
},
flush() {
/* do any destructor work here */
},
};
class AnyToU8Stream extends TransformStream {
constructor() {
super({ ...transformContent, textencoder: new TextEncoder() });
}
}
仕様書
Specification |
---|
Streams # ts-default-controller-class |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TransformStreamDefaultController | ||||||||||||||
desiredSize | ||||||||||||||
enqueue | ||||||||||||||
error | ||||||||||||||
terminate |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Partial support
- Partial support
- Has more compatibility info.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.