TransformStream

Last update:2024-08-14 19:50:29

The TransformStream object consists of a pair of streams: a readable stream, known as its readable side, and a writable stream, known as its writable side. Data written to the writable side will be made available for reading from the readable side as new data. It is based on the Web APIs standard TransformStream interface and can be used in Edge Cloud Apps Functions to process streaming data, such as transforming data formats, compressing data, encrypting data, and more.

Constructor

new TransformStream(transformer?: Transformer, writableStrategy?: WritableStrategy);

Parameters:

Parameter Name Type Required Description
transformer Transformer No The transformer object used to customize data transformation logic. Currently not supported, passing a value has no effect and the parameter is automatically ignored.
writableStrategy WritableStrategy No Writable side strategy configuration.

WritableStrategy

Property Name Type Required Description
highWaterMark number Yes The size of the writable buffer in bytes. The default value is 32KB, the maximum value is 256KB, and if it exceeds the maximum value, it will be automatically adjusted to 256KB.

Properties

readable

  • Type: ReadableStream (read-only)
  • Description: The readable side. See ReadableStream for details.

writable

  • Type: WritableStream (read-only)
  • Description: The writable side. See WritableStream for details.

Code Example

async function handleEvent(event) {
  // Create readable and writable sides
  const { readable, writable } = new TransformStream();
  // Fetch remote resource
  const response = await fetch('https://www.tencentcloud.com/');
  // Stream the response to the client
  response.body.pipeTo(writable);

  return new Response(readable, response);
}

addEventListener('fetch', (event) => {
  event.respondWith(handleEvent(event));
});

IdentityTransformStream

The IdentityTransformStream class implements the same behavior as the current TransformStream class. This type of stream forwards all chunks of byte data (in the form of TypedArrays) written to its writable side to its readable side, without any changes.

The IdentityTransformStream readable side supports bring your own buffer (BYOB) reads.

Constructor

new IdentityTransformStream();

Return Value: Returns a new identity transform stream.

Properties

readable

  • Type: ReadableStream
  • Description: An instance of a ReadableStream.

writable

  • Type: WritableStream
  • Description: An instance of a WritableStream.

FixedLengthStream

The FixedLengthStream is a specialization of IdentityTransformStream that limits the total number of bytes that the stream will pass through. It is useful primarily because, when using FixedLengthStream to produce either a Response or Request, the fixed length of the stream will be used as the Content-Length header value as opposed to using chunked encoding when using any other type of stream. An error will occur if too many or too few bytes are written through the stream.

Constructor

new FixedLengthStream(length: number | bigint);

Parameters:

  • length: A number or bigint, representing the total number of bytes that the stream will pass through. The maximum value is 2^53 - 1.

Return Value: Returns a new identity transform stream.

Properties

readable

  • Type: ReadableStream
  • Description: An instance of a ReadableStream.

writable

  • Type: WritableStream
  • Description: An instance of a WritableStream.

References

Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!