node:stream
The node:stream module in Azion Runtime: the abstract interface for streaming data, examples of each stream type, and the supported APIs.
The node:stream module provides the Node.js abstract interface for streaming data: the Readable, Writable, Duplex, Transform, and PassThrough classes. A stream reads or writes data as a continuous flow of chunks, so a function processes a large volume of data without holding all of it in memory at once.
Examples
Each example is a complete function that imports from node:stream. The response below each example is the one a deployed function returns.
Basic readable and writable streams
This function creates a readable stream and a writable stream, connects them with pipe(), and responds when the pipe is set up:
The function responds with:
Transform stream for data processing
A Transform stream changes data as it passes through. This function connects two transforms and a collector with pipeline(): the first uppercases each line, the second numbers it:
The function responds with the numbered, uppercased lines:
PassThrough stream for data forwarding
A PassThrough stream forwards data without changing it. This function writes three chunks to one and collects each chunk as it passes:
The function responds with the chunks it collected:
Duplex stream for bidirectional communication
A Duplex stream is both readable and writable. This function writes two chunks to a duplex stream and reads three chunks from it:
The function logs Written: Data to write and Written: More data, and responds with the chunks it read:
Request body processing with streams
This function reads the request body chunk by chunk, passes each chunk through two transforms, and counts the bytes:
For a POST request whose body is streamed request body for the stream sample, the function responds with the byte count, the number of chunks, and the start of the body:
Supported APIs
The table lists the status of each node:stream API in Azion Runtime:
| API | Status |
|---|---|
stream.Readable | 🟢 Supported |
stream.Writable | 🟢 Supported |
stream.Duplex | 🟢 Supported |
stream.Transform | 🟢 Supported |
stream.PassThrough | 🟢 Supported |
stream.pipeline() | 🟢 Supported |
stream.compose() | 🟡 Partially supported |
stream.finished() | 🟡 Partially supported |
readable.pipe() | 🟢 Supported |
readable.on('data') | 🟢 Supported |
readable.on('end') | 🟢 Supported |
readable.on('error') | 🟢 Supported |
writable.write() | 🟢 Supported |
writable.end() | 🟢 Supported |
transform.push() | 🟢 Supported |
stream.pipeline() takes a callback, as the transform example shows. The promise form, pipeline() from node:stream/promises, throws Error: [unenv] stream.promises.pipeline is not implemented yet! in a deployed function and under azion dev.