Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created October 10, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpscholar/d03a94a6e9699025c4c469632a8590f1 to your computer and use it in GitHub Desktop.
Save wpscholar/d03a94a6e9699025c4c469632a8590f1 to your computer and use it in GitHub Desktop.
Utility functions for base64 encoding/decoding for buffers in Node.js
/**
* Take a file and convert to a base64 encoded string.
*
* @param buffer A Buffer instance.
* @returns {string} A base64 encoded string.
*/
function base64Encode(buffer) {
return new Buffer.from(buffer).toString('base64');
}
/**
* Take a base64 encoded string and convert to a buffer.
*
* @param base64
* @returns {Buffer}
*/
function base64Decode(base64) {
return new Buffer.from(base64, 'base64');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment