Skip to content

Instantly share code, notes, and snippets.

@TrejGun
Created December 10, 2021 11:47
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 TrejGun/dc4d40fb6b54209d1ceba84c7e40d551 to your computer and use it in GitHub Desktop.
Save TrejGun/dc4d40fb6b54209d1ceba84c7e40d551 to your computer and use it in GitHub Desktop.
Stream to buffer
import { Writable } from "stream";
export const streamAsPromise = (readable: Readable): Promise<Buffer> => {
const result: Array<Buffer> = [];
const w = new Writable({
write(chunk, encoding, callback) {
result.push(chunk);
callback();
},
});
readable.pipe(w);
return new Promise((resolve, reject) => {
w.on("finish", resolve);
w.on("error", reject);
}).then(() => Buffer.concat(result));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment