TransformStream
The TransformStream interface of Azion Runtime: its constructor, its readable and writable properties, and a transform stream that converts chunks to bytes.
The TransformStream interface is part of the Streams API of Azion Runtime. It is a concrete implementation of the pipe chain transform stream concept: you pass it to the pipeThrough() method of a ReadableStream to convert a stream of data from one format into another. Use it to decode or encode video frames, decompress data, or convert a stream from XML to JSON. TransformStream is a transferable object. For more information, refer to TransformStream on MDN Web Docs.
Constructor
The TransformStream() constructor creates and returns a transform stream object:
The constructor takes an optional transformation object and optional queuing strategies for its streams. Without a transformation object, data passes through the stream unchanged.
The transformation object carries the transformation algorithm. Its transform(chunk, controller) method receives each chunk written to the stream, and controller.enqueue() passes a chunk to the readable side. A transformation object can also define start() and flush() methods, as the Example section shows.
For a queuing strategy, Azion Runtime defines CountQueuingStrategy but not ByteLengthQueuingStrategy: a reference to ByteLengthQueuingStrategy throws ReferenceError: ByteLengthQueuingStrategy is not defined.
Properties
A transform stream has two ends. Together they form the pair that pipeThrough() accepts:
| Property | Type | Description |
|---|---|---|
readable | ReadableStream | The readable end of the transform stream. It returns the transformed chunks. |
writable | WritableStream | The writable end of the transform stream. It receives the chunks to transform. |
Example
This handler defines AnyToU8Stream, a transform stream that passes every chunk it receives through as a Uint8Array. It pipes a string, an array of numbers, and a Uint16Array through the stream, then returns the bytes of each output chunk:
The function returns these bytes for the three output chunks. The string 'hi' becomes its encoded bytes, the number array becomes a Uint8Array with the same values, and the Uint16Array comes out as the two bytes of its buffer: