DEV Community

bsorrentino
bsorrentino

Posted on

Answer: Node.js: How to read a stream into a buffer?

Javascript snippet

function stream2buffer(stream) {
    return new Promise((resolve, reject) => {
        
        const _buf = [];

        stream.on("data", (chunk) => _buf.push(chunk));
        stream.on("end", () => resolve(Buffer.concat(_buf)));
        stream.on("error", (err) => reject(err));

    });
} 

Typescript snippet

async function stream2buffer(stream: Stream): Promise<Buffer> {

    return new Promise < Buffer > ((resolve, reject) => {
        
        const _buf =

Top comments (0)