The TransformStreamDefaultController
interface of the Streams API provides methods to manipulate the associated ReadableStream
and WritableStream
.
When constructing a TransformStream
, the TransformStreamDefaultController
is created. It therefore has no constructor. The way to get an instance of TransformStreamDefaultController
is via the callback methods of TransformStream()
.
Instance properties
TransformStreamDefaultController.desiredSize
Read only
-
Returns the desired size to fill the readable side of the stream's internal queue.
Instance methods
-
TransformStreamDefaultController.enqueue()
-
Enqueues a chunk (single piece of data) in the readable side of the stream.
-
TransformStreamDefaultController.error()
-
Errors both the readable and writable side of the transform stream.
-
TransformStreamDefaultController.terminate()
-
Closes the readable side and errors the writable side of the stream.
Examples
In the following example, a transform stream passes through all chunks it receives as Uint8Array
values, using the error()
and enqueue()
methods.
const transformContent = {
start() {},
async transform(chunk, controller) {
chunk = await chunk;
switch (typeof chunk) {
case "object":
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);
} 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() {
},
};
class AnyToU8Stream extends TransformStream {
constructor() {
super({ ...transformContent, textencoder: new TextEncoder() });
}
}
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
TransformStreamDefaultController |
67 |
79 |
102 |
No |
54 |
14.1 |
67 |
67 |
102 |
48 |
14.5 |
9.0 |
desiredSize |
67 |
79 |
102 |
No |
54 |
14.1 |
67 |
67 |
102 |
48 |
14.5 |
9.0 |
enqueue |
67 |
79 |
102 |
No |
54 |
14.1 |
67 |
67 |
102 |
48 |
14.5 |
9.0 |
error |
67 |
79 |
102 |
No |
54 |
14.1 |
67 |
67 |
102 |
48 |
14.5 |
9.0 |
terminate |
67 |
79 |
102 |
No |
54 |
14.1 |
67 |
67 |
102 |
48 |
14.5 |
9.0 |