Created
December 10, 2021 11:47
-
-
Save TrejGun/dc4d40fb6b54209d1ceba84c7e40d551 to your computer and use it in GitHub Desktop.
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
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