UNPKG

noop-stream

Version:

Create a readable Node.js stream that produces no data (or optionally blank data) or a writable stream that discards data

44 lines (36 loc) 791 B
import { Readable as ReadableStream, Writable as WritableStream, } from 'node:stream'; import {Buffer} from 'node:buffer'; export function readableNoopStream({size = 0, ...options} = {}) { let producedSize = 0; return new ReadableStream({ ...options, read(readSize) { let shouldEnd = false; if ((producedSize + readSize) >= size) { readSize = size - producedSize; shouldEnd = true; } setImmediate(() => { if (size === 0) { this.push(null); } producedSize += readSize; this.push(Buffer.alloc(readSize)); if (shouldEnd) { this.push(null); } }); }, }); } export function writableNoopStream(options) { return new WritableStream({ ...options, write(chunk, encding, callback) { setImmediate(callback); }, }); }