Created
March 14, 2023 04:57
-
-
Save 4rmx/6b8bb5c7399032fe3145a12eebaee4ce to your computer and use it in GitHub Desktop.
convert stream to buffer in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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