Exports
[<Class>]
type Exports =
static member finished: Finished
static member pipeline: Pipeline
static member promises: Promises
static member prototype: Stream
static member Duplex (?opts: DuplexOptions<Duplex>) : Duplex
static member PassThrough (?opts: TransformOptions<Transform>) : PassThrough
static member Readable (?opts: ReadableOptions<Readable>) : Readable
static member Stream (?options: EventEmitterOptions) : Stream
static member Transform (?opts: TransformOptions<Transform>) : Transform
static member Writable (?opts: WritableOptions<Writable>) : Writable
static member addAbortSignal<'T when 'T :> Stream> (signal: AbortSignal, stream: 'T) : 'T
static member duplexPair (?options: DuplexOptions<Duplex>) : Duplex * Duplex
static member getDefaultHighWaterMark (objectMode: bool) : float
static member isErrored (stream: U4<Readable,ReadableStream,Writable,WritableStream>) : bool
static member isReadable (stream: U2<Readable,ReadableStream>) : bool option
static member isWritable (stream: U2<Writable,WritableStream>) : bool option
static member setDefaultHighWaterMark (objectMode: bool, value: float) : unit| Assembly | Xantham.Fable.Node |
| Attributes | [<Erase>] |
The package's value exports, each bound to its import.
Properties
static member finished: Finished
static member finished: FinishedA readable and/or writable stream/webstream.
A function to get notified when a stream is no longer readable, writable or has experienced an error or a premature close event.
import { finished } from 'node:stream';
import fs from 'node:fs';
const rs = fs.createReadStream('archive.tar');
finished(rs, (err) => {
if (err) {
console.error('Stream failed.', err);
} else {
console.log('Stream is done reading.');
}
});
rs.resume(); // Drain the stream.
Especially useful in error handling scenarios where a stream is destroyed prematurely (like an aborted HTTP request), and will not emit 'end' or 'finish'.
The finished API provides promise version.
stream.finished() leaves dangling event listeners (in particular 'error', 'end', 'finish' and 'close') after callback has been invoked. The reason for this is so that unexpected 'error' events (due to incorrect stream implementations) do not cause unexpected crashes. If this is unwanted behavior then the returned cleanup function needs to be invoked in the callback:
const cleanup = finished(rs, (err) => {
cleanup();
// ...
});
@since v10.0.0
| Parameter | |
|---|---|
stream | A readable and/or writable stream. |
callback | A callback function that takes an optional error argument. |
Returns. A cleanup function which removes all registered listeners.
static member pipeline: Pipeline
static member pipeline: PipelineA module method to pipe between streams and generators forwarding errors and properly cleaning up and provide a callback when the pipeline is complete.
import { pipeline } from 'node:stream';
import fs from 'node:fs';
import zlib from 'node:zlib';
// Use the pipeline API to easily pipe a series of streams
// together and get notified when the pipeline is fully done.
// A pipeline to gzip a potentially huge tar file efficiently:
pipeline(
fs.createReadStream('archive.tar'),
zlib.createGzip(),
fs.createWriteStream('archive.tar.gz'),
(err) => {
if (err) {
console.error('Pipeline failed.', err);
} else {
console.log('Pipeline succeeded.');
}
},
);
The pipeline API provides a promise version.
stream.pipeline() will call stream.destroy(err) on all streams except:
Readablestreams which have emitted'end'or'close'.Writablestreams which have emitted'finish'or'close'.
stream.pipeline() leaves dangling event listeners on the streams after the callback has been invoked. In the case of reuse of streams after failure, this can cause event listener leaks and swallowed errors. If the last stream is readable, dangling event listeners will be removed so that the last stream can be consumed later.
stream.pipeline() closes all the streams when an error is raised. The IncomingRequest usage with pipeline could lead to an unexpected behavior once it would destroy the socket without sending the expected response. See the example below:
import fs from 'node:fs';
import http from 'node:http';
import { pipeline } from 'node:stream';
const server = http.createServer((req, res) => {
const fileStream = fs.createReadStream('./fileNotExist.txt');
pipeline(fileStream, res, (err) => {
if (err) {
console.log(err); // No such file
// this message can't be sent once `pipeline` already destroyed the socket
return res.end('error!!!');
}
});
});
@since v10.0.0
| Parameter | |
|---|---|
callback | Called when the pipeline is fully done. |
Methods
static member Duplex (?opts: DuplexOptions<Duplex>) : Duplex
static member Duplex (?opts: DuplexOptions<Duplex>) : DuplexDuplex streams are streams that implement both the Readable and Writable interfaces.
Examples of Duplex streams include:
TCP socketszlib streamscrypto streams
@since v0.9.4
static member PassThrough (?opts: TransformOptions<Transform>) : PassThrough
static member PassThrough (?opts: TransformOptions<Transform>) : PassThroughThe stream.PassThrough class is a trivial implementation of a Transform stream that simply passes the input bytes across to the output. Its purpose is primarily for examples and testing, but there are some use cases where stream.PassThrough is useful as a building block for novel sorts of streams.
static member Stream (?options: EventEmitterOptions) : Streamstatic member Transform (?opts: TransformOptions<Transform>) : Transform
static member Transform (?opts: TransformOptions<Transform>) : TransformTransform streams are Duplex streams where the output is in some way related to the input. Like all Duplex streams, Transform streams implement both the Readable and Writable interfaces.
Examples of Transform streams include:
zlib streamscrypto streams
@since v0.9.4
static member addAbortSignal<'T when 'T :> Stream> (signal: AbortSignal, stream: 'T) : 'T
static member addAbortSignal<'T when 'T :> Stream> (signal: AbortSignal, stream: 'T) : 'TA stream to attach a signal to.
Attaches an AbortSignal to a readable or writeable stream. This lets code control stream destruction using an AbortController.
Calling abort on the AbortController corresponding to the passed AbortSignal will behave the same way as calling .destroy(new AbortError()) on the stream, and controller.error(new AbortError()) for webstreams.
import fs from 'node:fs';
const controller = new AbortController();
const read = addAbortSignal(
controller.signal,
fs.createReadStream(('object.json')),
);
// Later, abort the operation closing the stream
controller.abort();
Or using an AbortSignal with a readable stream as an async iterable:
const controller = new AbortController();
setTimeout(() => controller.abort(), 10_000); // set a timeout
const stream = addAbortSignal(
controller.signal,
fs.createReadStream(('object.json')),
);
(async () => {
try {
for await (const chunk of stream) {
await process(chunk);
}
} catch (e) {
if (e.name === 'AbortError') {
// The operation was cancelled
} else {
throw e;
}
}
})();
Or using an AbortSignal with a ReadableStream:
const controller = new AbortController();
const rs = new ReadableStream({
start(controller) {
controller.enqueue('hello');
controller.enqueue('world');
controller.close();
},
});
addAbortSignal(controller.signal, rs);
finished(rs, (err) => {
if (err) {
if (err.name === 'AbortError') {
// The operation was cancelled
}
}
});
const reader = rs.getReader();
reader.read().then(({ value, done }) => {
console.log(value); // hello
console.log(done); // false
controller.abort();
});
@since v15.4.0
| Parameter | |
|---|---|
signal | A signal representing possible cancellation |
stream | A stream to attach a signal to. |
static member duplexPair (?options: DuplexOptions<Duplex>) : Duplex * Duplex
static member duplexPair (?options: DuplexOptions<Duplex>) : Duplex * DuplexThe utility function duplexPair returns an Array with two items, each being a Duplex stream connected to the other side:
const [ sideA, sideB ] = duplexPair();
Whatever is written to one stream is made readable on the other. It provides behavior analogous to a network connection, where the data written by the client becomes readable by the server, and vice-versa.
The Duplex streams are symmetrical; one or the other may be used without any difference in behavior.
@since v22.6.0
| Parameter | |
|---|---|
options | A value to pass to both {@link Duplex} constructors, to set options such as buffering. |
static member getDefaultHighWaterMark (objectMode: bool) : float
static member getDefaultHighWaterMark (objectMode: bool) : floatReturns the default highWaterMark used by streams. Defaults to 65536 (64 KiB), or 16 for objectMode.
@since v19.9.0
static member isErrored (stream: U4<Readable,ReadableStream,Writable,WritableStream>) : bool
static member isErrored (stream: U4<Readable,ReadableStream,Writable,WritableStream>) : boolReturns whether the stream has encountered an error.
@since v17.3.0, v16.14.0
static member isReadable (stream: U2<Readable,ReadableStream>) : bool option
static member isReadable (stream: U2<Readable,ReadableStream>) : bool optionReturns whether the stream is readable.
@since v17.4.0, v16.14.0
Returns. Only returns null if stream is not a valid Readable, Duplex or ReadableStream.
static member isWritable (stream: U2<Writable,WritableStream>) : bool option
static member isWritable (stream: U2<Writable,WritableStream>) : bool optionReturns whether the stream is writable.
@since v20.0.0
Returns. Only returns null if stream is not a valid Writable, Duplex or WritableStream.
static member setDefaultHighWaterMark (objectMode: bool, value: float) : unit
static member setDefaultHighWaterMark (objectMode: bool, value: float) : unitSets the default highWaterMark used by streams.
@since v19.9.0
| Parameter | |
|---|---|
value | highWaterMark value |