BufferListStream extends BufferList with Node.js Duplex Stream capabilities, allowing it to be used as both a readable and writable stream. It provides all BufferList functionality plus streaming interface for collecting data from streams and emitting data to streams.
Creates a BufferListStream instance with optional callback for stream completion.
/**
* Creates a BufferListStream instance with duplex stream capabilities
* @param callback - Optional callback invoked when stream ends with (err, buffer)
*/
class BufferListStream extends BufferList {
constructor(callback?: (err: Error | null, buffer: Buffer) => void);
}
// Factory function (new not required)
function BufferListStream(callback?: (err: Error | null, buffer: Buffer) => void): BufferListStream;Usage Examples:
const { BufferListStream } = require('bl');
// Stream collector with callback
const bl1 = new BufferListStream((err, data) => {
if (err) throw err;
console.log('Collected:', data.toString());
});
// Factory function usage
const bl2 = BufferListStream((err, data) => {
console.log('Stream complete:', data.length, 'bytes');
});
// Without callback
const bl3 = new BufferListStream();Use as a writable stream to collect data from readable streams.
/**
* Collect data from a readable stream
* The callback receives the complete collected buffer when the stream ends
*/
pipe(source: ReadableStream): BufferListStream;Usage Examples:
const fs = require('fs');
const { BufferListStream } = require('bl');
// Collect file contents
fs.createReadStream('data.txt')
.pipe(BufferListStream((err, data) => {
if (err) throw err;
console.log('File contents:', data.toString());
}));
// Collect HTTP response
const https = require('https');
https.get('https://api.example.com/data', (res) => {
res.pipe(BufferListStream((err, data) => {
if (err) throw err;
const json = JSON.parse(data.toString());
console.log(json);
}));
});Use as a readable stream to emit collected data to writable streams.
/**
* Emit collected data to a writable stream
*/
pipe(destination: WritableStream): WritableStream;Usage Examples:
const fs = require('fs');
const { BufferListStream } = require('bl');
// Create and populate a buffer list
const bl = new BufferListStream();
bl.append('Hello ');
bl.append('world!');
// Pipe to file
bl.pipe(fs.createWriteStream('output.txt'));
// Pipe to stdout
bl.pipe(process.stdout);BufferListStream can function as a transform stream, collecting input and emitting output.
/**
* Use as a duplex stream for both collection and emission
*/
// Inherits all stream methods from Node.js Duplex classUsage Examples:
const { BufferListStream } = require('bl');
// Transform stream usage
const transform = new BufferListStream();
// Set up pipeline
process.stdin
.pipe(transform)
.pipe(process.stdout);
// Manually write data
transform.write('Processing: ');
setTimeout(() => {
transform.write('data received\n');
transform.end();
}, 1000);BufferListStream inherits all BufferList capabilities and can use them during streaming operations.
// All BufferList methods are available:
append(buffer: BufferListAcceptedTypes): this;
prepend(buffer: BufferListAcceptedTypes): this;
get(index: number): number | undefined;
slice(start?: number, end?: number): Buffer;
shallowSlice(start?: number, end?: number): BufferListStream;
copy(dest: Buffer, destStart?: number, srcStart?: number, srcEnd?: number): Buffer;
duplicate(): BufferListStream;
consume(bytes?: number): this;
toString(encoding?: BufferEncoding, start?: number, end?: number): string;
indexOf(value: string | number | Uint8Array | BufferList | Buffer, byteOffset?: number, encoding?: BufferEncoding): number;
getBuffers(): Buffer[];
// All binary read methods available
readDoubleBE(offset?: number): number;
readDoubleLE(offset?: number): number;
// ... (all other read methods from BufferList)
// Property
length: number;Type detection that recognizes both BufferList and BufferListStream instances.
/**
* Determines if the passed object is a BufferList or BufferListStream instance
* @param obj - Object to test
* @returns true if obj is BufferList or BufferListStream instance
*/
static BufferListStream.isBufferList(obj: unknown): boolean;As a Node.js Duplex stream, BufferListStream emits standard stream events.
// Standard Node.js stream events
on(event: 'end', listener: () => void): this;
on(event: 'error', listener: (err: Error) => void): this;
on(event: 'data', listener: (chunk: Buffer) => void): this;
on(event: 'finish', listener: () => void): this;
on(event: 'pipe', listener: (src: ReadableStream) => void): this;
on(event: 'unpipe', listener: (src: ReadableStream) => void): this;Usage Examples:
const { BufferListStream } = require('bl');
const bl = new BufferListStream();
bl.on('pipe', (src) => {
console.log('Something is piping into this stream');
});
bl.on('data', (chunk) => {
console.log('Received chunk:', chunk.length, 'bytes');
});
bl.on('end', () => {
console.log('Stream ended, total length:', bl.length);
});
bl.on('error', (err) => {
console.error('Stream error:', err);
});const { BufferListStream } = require('bl');
const fs = require('fs');
// Create processing pipeline
fs.createReadStream('input.txt')
.pipe(new BufferListStream((err, data) => {
if (err) throw err;
// Process the collected data
const processed = data.toString()
.toUpperCase()
.replace(/\n/g, ' ');
// Write processed data
fs.writeFileSync('output.txt', processed);
}));const { BufferListStream } = require('bl');
const collector = new BufferListStream();
// Collect data in chunks
collector.write('chunk 1 ');
collector.write('chunk 2 ');
collector.write('chunk 3');
// Process when ready
collector.end();
console.log(collector.toString()); // "chunk 1 chunk 2 chunk 3"const http = require('http');
const { BufferListStream } = require('bl');
const server = http.createServer((req, res) => {
// Collect request body
req.pipe(BufferListStream((err, data) => {
if (err) {
res.statusCode = 400;
res.end('Bad Request');
return;
}
try {
const json = JSON.parse(data.toString());
// Process JSON data
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ received: json }));
} catch (e) {
res.statusCode = 400;
res.end('Invalid JSON');
}
}));
});type BufferListStreamCallback = (err: Error | null, buffer: Buffer) => void;
interface ReadableStream {
pipe<T extends WritableStream>(destination: T): T;
}
interface WritableStream {
write(chunk: any): boolean;
end(chunk?: any): void;
}