UNPKG

random-bytes-readable-stream

Version:

Creates a readable stream producing cryptographically strong pseudo-random data using `crypto.randomBytes()`

32 lines (25 loc) 634 B
import {Readable as ReadableStream} from 'node:stream'; import {randomBytes} from 'node:crypto'; export default function randomBytesReadableStream({size = Number.POSITIVE_INFINITY} = {}) { let producedSize = 0; return new ReadableStream({ read(readSize) { let shouldEnd = false; if ((producedSize + readSize) >= size) { readSize = size - producedSize; shouldEnd = true; } randomBytes(readSize, (error, buffer) => { if (error) { this.emit('error', error); return; } producedSize += readSize; this.push(buffer); if (shouldEnd) { this.push(null); } }); }, }); }