Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. TransformStreamDefaultController

TransformStreamDefaultController

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨June 2022⁩.

Note: This feature is available inWeb Workers.

TheTransformStreamDefaultController interface of theStreams API provides methods to manipulate the associatedReadableStream andWritableStream.

When constructing aTransformStream, theTransformStreamDefaultController is created. It therefore has no constructor. The way to get an instance ofTransformStreamDefaultController is via the callback methods ofTransformStream().

Instance properties

TransformStreamDefaultController.desiredSizeRead 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 asUint8Array values, using theerror() andenqueue() methods.

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() });  }}

Specifications

Specification
Streams
# ts-default-controller-class

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp