Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TransformStream: TransformStream() constructor

BaselineWidely available *

Note: This feature is available inWeb Workers.

TheTransformStream() constructor creates a newTransformStream object which represents a pair of streams: aWritableStream representing the writable side, and aReadableStream representing the readable side.

Syntax

js
new TransformStream()new TransformStream(transformer)new TransformStream(transformer, writableStrategy)new TransformStream(transformer, writableStrategy, readableStrategy)

Parameters

transformerOptional

An object representing thetransformer. If not supplied the resulting stream will be anidentity transform stream which forwards all chunks written to its writable side to its readable side, without any changes.

The transformer object can contain any of the following methods. In each methodcontroller is an instance ofTransformStreamDefaultController.

start(controller)

Called when theTransformStream is constructed. It is typically used to enqueue chunks usingTransformStreamDefaultController.enqueue().

transform(chunk, controller)

Called when a chunk written to the writable side is ready to be transformed, and performs the work of the transformation stream. It can return a promise to signal success or failure of the write operation. If notransform() method is supplied, the identity transform is used, and the chunk will be enqueued with no changes.

flush(controller)

Called after all chunks written to the writable side have been successfully transformed, and the writable side is about to be closed.

writableStrategyOptional

An object that optionally defines a queuing strategy for the stream. This takes twoparameters:

highWaterMark

A non-negative integer. This defines the total number of chunks that can becontained in the internal queue before backpressure is applied.

size(chunk)

A method containing a parameterchunk. This indicates the size touse for each chunk, in bytes.

readableStrategyOptional

An object that optionally defines a queuing strategy for the stream. This takes twoparameters:

highWaterMark

A non-negative integer. This defines the total number of chunks that can becontained in the internal queue before backpressure is applied.

size(chunk)

A method containing a parameterchunk. This indicates the size touse for each chunk, in bytes.

Note:You could define your own customreadableStrategy orwritableStrategy, or use an instance ofByteLengthQueuingStrategy orCountQueuingStrategyfor the object values.

Examples

Transforming text to uppercase

The following example transforms text to uppercase chunk by chunk. This example is fromStreams—The Definitive Guide, which has a number of examples of different types of streams.

js
function upperCaseStream() {  return new TransformStream({    transform(chunk, controller) {      controller.enqueue(chunk.toUpperCase());    },  });}function appendToDOMStream(el) {  return new WritableStream({    write(chunk) {      el.append(chunk);    },  });}fetch("./lorem-ipsum.txt").then((response) =>  response.body    .pipeThrough(new TextDecoderStream())    .pipeThrough(upperCaseStream())    .pipeTo(appendToDOMStream(document.body)),);

Creating an identity transform stream

If notransformer argument is supplied then the result will be an identity transform stream which forwards all chunks written to the writable side to the readable side with no changes. In the following example an identity transform stream is used to add buffering to a pipe.

js
const writableStrategy = new ByteLengthQueuingStrategy({  highWaterMark: 1024 * 1024,});readableStream  .pipeThrough(new TransformStream(undefined, writableStrategy))  .pipeTo(writableStream);

Specifications

Specification
Streams
# ref-for-ts-constructor④

Browser compatibility

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp