Skip to main content
Version: 3.19.0

TransformStream()

The TransformStream() constructor creates a new TransformStream object which represents a pair of streams: a WritableStream representing the writable side, and a ReadableStream representing the readable side.

Syntax

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

Parameters

  • transformer optional

    • : An object representing the transformer. If not supplied the resulting stream will be an identity 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 method controller is an instance of TransformStreamDefaultController.

      • start(controller)
        • : Called when the TransformStream is constructed. It is typically used to enqueue chunks using TransformStreamDefaultController.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. If no transform() 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.
  • writableStrategy optional

    • : An object that optionally defines a queuing strategy for the stream. This takes two parameters:

      • highWaterMark
        • : A non-negative integer. This defines the total number of chunks that can be contained in the internal queue before backpressure is applied.
      • size(chunk)
        • : A method containing a parameter chunk. This indicates the size to use for each chunk, in bytes.
  • readableStrategy optional

    • : An object that optionally defines a queuing strategy for the stream. This takes two parameters:

      • highWaterMark
        • : A non-negative integer. This defines the total number of chunks that can be contained in the internal queue before backpressure is applied.
      • size(chunk)
        • : A method containing a parameter chunk. This indicates the size to use for each chunk, in bytes.

Note: You could define your own custom readableStrategy or writableStrategy, or use an instance of ByteLengthQueuingStrategy or CountQueuingStrategy for the object values.