Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created October 10, 2019 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpscholar/270005d42b860b1c33cf5ab25b37928a to your computer and use it in GitHub Desktop.
Save wpscholar/270005d42b860b1c33cf5ab25b37928a to your computer and use it in GitHub Desktop.
Convert a buffer to a stream in Node.js
/**
* Convert a buffer to a stream
*
* @param binary Buffer
* @returns Readable
*/
function bufferToStream(binary) {
return new Readable({
read() {
this.push(binary);
this.push(null);
}
});
}
@wpscholar
Copy link
Author

Use https://github.com/stream-utils/stream-to-array combined with Buffer.concat(array) to turn a stream into a buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment