npm install node-stream-test
creates test Readable stream, simulating sync/async behaviors
(options: MakeReadableOptions) => (readableOptions: ReadableOptions) => (iterable: Iterable<any>) => Readable
typeMakeReadableOptions={eager: boolean// lazy or eager stream behaviorlog?: typeofconsole.log// provide debug logger or noopdelayMs?: number// simulate async stream behaviorerrorAtStep?: number// emit 'error' event at certain stepcontinueOnError?: boolean// whether should stream continue on error or break}Lazy stream pushes one
chunkof data on everyread.
Eager stream pushes allchunksin a synchronous loop onread.
delayMsis a delay betweenreadcall and actualchunkpush.
This simulates asynchronous stream behavior.
If the stream iseager, it will push allchunksin a loop after first delay
import{readable}from'node-stream-test'// create test-readable streamconsttestReadable=readable({log: console.log,// output debug info to consoledelayMs: 10,// delay 10mseager: false// eager or lazy stream })({objectMode: true// provide Node Readable configuration})([1,2,3,4,5]// provide data to stream)// subscribe to test-readabletestReadable.on('data',()=>{}).on('end',()=>{})creates test Writable stream, simulating sync/async behaviors
(options: MakeWritableOptions) => (writableOptions: WritableOptions) => (sink: (chunk: any) => void) => Writable
typeMakeWritableOptions={log: typeofconsole.log,// provide debug logger or noopdelayMs?: number// simulate asyncerrorAtStep?: number// emit 'error' event at certain step}
delayMsis a delay betweenwritecall and passingchunkto a sink.
This simulates long async writes.
import{writable}from'node-stream-test'// We have the following streamdeclarevarstream: ReadableStreamconsttestWritable=writable({log: console.log,// output debug info to consoledelayMs: 10// delay 10ms})({objectMode: true// provide Node Writable configuration})// pipe the stream into test-writablestream.pipe(stream,testWritable).on('data',()=>{}).on('end',()=>{})writes chunks to a stream
(options: ProducerOptions) => (iterable: Iterable<any>) => (stream: WritableStream) => () => void
typeProducerOptions={log: typeofconsole.log,// provide debug logger or noopeager: boolean// eager or lazy producer}
eagerproducer writeschunksin a synchronous loop untilhighWatermarkreached.lazyproducer writes onechunkondrainevent.
import{producer}from'node-stream-test'// We have the following writable streamdeclarevarstream: WritableStream// create a producerconstbeginProduce=producer({log: console.log,// output debug info to consoleeager: true// eager producer})([1,2,3,4,5],// data to write0// write all data)(stream// write to this stream)simple on('data') consumer with logging
(options: DataConsumerOptions) => (sink: (chunk: any) => void) => (stream: ReadableStream) => () => void
typePushConsumerOptions={log: typeofconsole.log// provide debug logger or noop}import{pushConsumer}from'node-stream-test'// We have the following streamdeclarevarstream: ReadableStreampushConsumer({log: console.log// output debug info to console})((chunk: string)=>{}// your callback on every `data` event)(stream,// stream to consume)simple on('readable') consumer with sync/async behavior and logging
(options: ReadableConsumerOptions) => (sink: (chunk: any) => void) => (stream: ReadableStream) => () => void
typePullConsumerOptions={log: typeofconsole.log,// provide debug logger or noopdelayMs?: number,// simulate asynceager?: boolean,// eager or lazy behaviorreadSize?: number// how much data to read on each 'readable' event}
delayMsis a time betweenreadableevent and actualreadcall on stream.
eagerconsumer callsreadin synchronous loop untilnullreturned.
Then waits for the nextreadable.lazyconsumer reads onechunk, then waits.
import{pullConsumer}from'node-stream-test'// We have the following streamdeclarevarstream: ReadableStreampullConsumer({log: console.log,// print debug info to consoledelayMs: 10,// delay 10mseager: false,// lazy behaviorreadSize: undefined// read all available data})((chunk: string)=>{}// your callback on `read` call, after `readable` event)(stream,// stream to consume)