Created
July 7, 2022 07:38
-
-
Save monjer/acec1919d7408ea2c563ded425dd4a91 to your computer and use it in GitHub Desktop.
Convert readable stream to buffer
This file contains hidden or 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
const streamToBuffer = (readableStream) => { | |
if (!readableStream) { | |
throw new Error('Error: readableStream can not be null') | |
} | |
return new Promise((resovle, reject) => { | |
const buffers = []; | |
readableStream.on('data', (chunks) => { | |
buffers.push(chunks); | |
}); | |
readableStream.once('end', () => { | |
resovle(Buffer.concat(buffers)); | |
}); | |
readableStream.once('error', (error) => { | |
reject(error) | |
}); | |
}) | |
} | |
export default streamToBuffer; |
Author
monjer
commented
Jul 7, 2022
•
- Convert stream into buffer?
- Node.js: How to read a stream into a buffer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment