$ npm install libbase64
Encode and decode base64 strings. Written in TypeScript, published with both ES module and CommonJS builds and type declarations. Requires Node.js 20 or newer.
Install with npm
npm install libbase64
Require in your script
const libbase64 = require('libbase64');
or import it as an ES module
import libbase64 from 'libbase64';
// or
import { encode, decode, wrap, Encoder, Decoder } from 'libbase64';
Encode Buffer objects or unicode strings with
libbase64.encode(val) → String
Where
Example
libbase64.encode('jõgeva');
// asO1Z2V2YQ==
Decode base64 strings to a Buffer with
libbase64.decode(str) → Buffer
Where
Example
libbase64.decode('asO1Z2V2YQ==').toString();
// jõgeva
To enforce soft line breaks on lines longer than selected amount of characters, use wrap
libbase64.wrap(str[, lineLength]) → String
Where
Example
libbase64.wrap('asO1Z2V2asO1Z2V2asO1Z2V2YQ==', 10);
// asO1Z2V2as\r\n
// O1Z2V2asO1\r\n
// Z2V2YQ==
libbase64 makes it possible to encode and decode streams with libbase64.Encoder and libbase64.Decoder constructors.
Create new Encoder Stream with
const encoder = new libbase64.Encoder([options])
Where
false to turn the soft wrapping off completely)limitOutbutBytes that earlier versions documented is still acceptedThe output is the same as libbase64.wrap(libbase64.encode(input), lineLength), no matter how the input is split into chunks.
Example
The following example script reads in a file, encodes it to base64 and saves the output to a file.
const libbase64 = require('libbase64');
const fs = require('fs');
const source = fs.createReadStream('source.txt');
const encoded = fs.createWriteStream('encoded.txt');
const encoder = new libbase64.Encoder();
source.pipe(encoder).pipe(encoded);
Create new Decoder Stream with
const decoder = new libbase64.Decoder([options])
Where
Example
The following example script reads in a file in base64 encoding, decodes it and saves the output to a file.
const libbase64 = require('libbase64');
const fs = require('fs');
const encoded = fs.createReadStream('encoded.txt');
const dest = fs.createWriteStream('dest.txt');
const decoder = new libbase64.Decoder();
encoded.pipe(decoder).pipe(dest);
MIT
© 2010 - cnpmjs.org x YWFE | Home | YWFE