Skip to content

Instantly share code, notes, and snippets.

@chinhvo
Forked from luan0ap/streamToBase64.js
Created October 8, 2024 06:52
Show Gist options
  • Save chinhvo/69882842bec4a84953c8ed42e94710b6 to your computer and use it in GitHub Desktop.
Save chinhvo/69882842bec4a84953c8ed42e94710b6 to your computer and use it in GitHub Desktop.
Convert a readable stream to base64 string
/**
* 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