This is a lightweight, fast library for encoding and decoding strings in base64 format.
It's portable between the browser and Node.js. In order to keep backwards compatibility with old browsers, this library can only encode and decode strings and buffers (if it's running in Node.js). This serves as a lightweight, portable replacement for the btoa
and atob
functions, and provides clearer names.
Wanna contribute? File an issue or pull request! Make sure you follow the contribution Code of Conduct.
- Via NPM:
npm install @santi100/base64-lib
- Via Yarn:
yarn add @santi100/base64-lib
- Via PNPM:
pnpm install @santi100/base64-lib
function encode(input: string): string;
Encodesinput
to base64 format. It takes a string as an argument, and returns a base64 representation ofinput
.function decode(input: string): string;
Decodesinput
back to its original form. Must be a valid base64 string. It takes a valid base64 string, and returns the decoded value ofinput
.
The above functions must:
- Be able to handle strings that contain non-printable ASCII (the result of stringifying a buffer, for example).
- Be fully ES3 compliant (no ArrayBuffer, Uint8Array or anything like that).
- Use padding
=
signs. - Be fully portable between the browser and Node.js (no reliance in any browser- or Node-exclusive API).
- Not modify the argument vector.
If any of the above is not being satisfied, open a pull request or issue. I have no idea how to satisfy idea #1 reliably (see Contribute (for instructions) and disclaimers😀).
- Keep in mind this library can only reliably handle plaintext for now.
- *Hasn't been tested in an actual ES3 environment. Feel free to open an issue or pull request if you find any non-ES3 thing.
- ^The source code is about 3 kilobytes.