Created
January 9, 2020 17:42
-
-
Save luan0ap/521325c619413a02a798482e2c06af90 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
This is a Node API. In the browser, you would do something like that (Typescript):
To only get the base64 string, you need to split and get the latter part.