-
-
Save chinhvo/69882842bec4a84953c8ed42e94710b6 to your computer and use it in GitHub Desktop.
Convert a readable stream to base64 string
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
/** | |
* Convert a Readable Stream to base64 string | |
* @param {ReadableStream} stream - a readable stream to convert in base64 string | |
* @returns {Promise} - Promise that resolve in a string containing the base64 | |
*/ | |
const streamToBase64 = (stream) => { | |
const concat = require('concat-stream') | |
const { Base64Encode } = require('base64-stream') | |
return new Promise((resolve, reject) => { | |
const base64 = new Base64Encode() | |
const cbConcat = (base64) => { | |
resolve(base64) | |
} | |
stream | |
.pipe(base64) | |
.pipe(concat(cbConcat)) | |
.on('error', (error) => { | |
reject(error) | |
}) | |
}) | |
} | |
// Example: https://repl.it/@luan0ap/streamToBase64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment