Skip to content

Instantly share code, notes, and snippets.

@4rmx
Created March 14, 2023 04:57
Show Gist options
  • Save 4rmx/6b8bb5c7399032fe3145a12eebaee4ce to your computer and use it in GitHub Desktop.
Save 4rmx/6b8bb5c7399032fe3145a12eebaee4ce to your computer and use it in GitHub Desktop.
convert stream to buffer in nodejs
/**
* @param {import('stream').Stream} stream
*/
async function streamToBuffer(stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(chunks)));
});
}
module.exports = stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment