base64-pro
does base64 encoding and decoding.
- Pure JS
- Zero dependencies
- Supports modern browsers & Node.js
- Support conversion between binary data and "base64 string"
- Support conversion between "unicode string" and "base64 string" (atob and btoa only support characters which code point is between 0 and 255)
- Support converting binary data or "unicode string" to DataURL
- Support custom base64 character set
- Support TypeScript
- Support ESM, CommonJS, browser direct loading
npm i base64-pro
import Base64 from "base64-pro"; // ESM
Or
const Base64 = require("base64-pro"); // CommonJS
Or
Loading the JS file directly in the browser. You will get the global variable window.Base64
.
1. Conversion between binary data and "base64 string". (Binary data means an instance of ArrayBuffer or TypedArray or DataView or Buffer in Node.js)
const b64 = new Base64();
// encoding
b64.bufferToBase64(new Uint8Array([1, 2, 3, 4])); // AQIDBA==
// decoding
b64.base64ToBuffer("AQIDBA=="); // ArrayBuffer { [Uint8Contents]: <01 02 03 04>, byteLength: 4 }
// encoding
b64.strToBase64("👻base64-pro🤗"); // 8J+Ru2Jhc2U2NC1wcm/wn6SX
// decoding
b64.base64ToStr("8J+Ru2Jhc2U2NC1wcm/wn6SX"); // 👻base64-pro🤗
b64.bufferToDataURL(require("fs").readFileSync("icon.png"), "image/png"); // data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...
b64.strToDataURL("👻base64-pro🤗"); // data:text/plain;charset=utf-8;base64,8J%2BRu2Jhc2U2NC1wcm%2Fwn6SX
new Base64(initOpts)
initOpts
- required: no
- type: object
- props:
alphabet
- required: no
- type: string
- default:
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
- description: base64 character set
encodeChunkSize
- required: no
- type: number
- default:
16383
- description: Performance optimization related, not recommended to be changed. Long strings are slow to stitch together, so when generating base64 strings, they are generated in chunks and finally merged into a complete string. This value represents a temporary string fragment generated for every how many bytes of raw binary data.
- description: Converts binary data to a base64 string.
- arguments:
value
- required: yes
- type: buffer
- description: buffer means an ArrayBuffer instance or a TypedArray instance or a DataView instance or a Buffer instance in Node.js.
padding
- required: no
- type: boolean
- default:
true
- description: When the length of a base64 string is not an integer multiple of 4, is it automatically completed with
=
at the end.
- return: A base64 string.
- description: Converts a base64 string to binary data.
- arguments:
base64Str
- required: yes
- type: string
- description: a valid base64 string
- return: An ArrayBuffer instance. You are free to create any kind of view to read this part of the binary data.
- description: Converts a unicode string to a base64 string.
- arguments:
str
- required: yes
- type: string
- description: A unicode string
encoding
- required: no
- type: string
- default:
utf-8
- optional values:
utf-8
,utf8
,utf-16
,utf-16le
,utf-16be
- description: Unicode has multiple character encoding methods, and even if the characters are the same, the underlying binary will be different for different encoding methods. Currently, we only supported convert to base64 strings in utf-8 and utf-16 encoding. utf8 is an alias for utf-8. utf-16 is an alias for utf-16le. utf-16le is a little-endian byte order encoding, and utf-16be is a big-endian byte order encoding.
padding
- required: no
- type: boolean
- default:
true
- description: ibid.
- return: A base64 string.
- description: Converts a base64 string to a unicode string.
- arguments:
base64Str
- required: yes
- type: string
- description: A valid base64 string
encoding
- required: no
- type: string
- default:
utf-8
- optional values:
utf-8
,utf8
,utf-16
,utf-16le
,utf-16be
- description: ibid.
- return: A unicode string
- description: Converts a unicode string into a DataURL string. The converted result can be opened directly in the browser address bar.
- arguments:
str
- required: yes
- type: string
- description: a unicode string
encoding
- required: no
- type: string
- default:
utf-8
- optional values:
utf-8
,utf8
,utf-16
,utf-16le
,utf-16be
- description: ibid.
- return: A DataURL string.
- description: Converts binary data into a DataURL string. Using DataURL reduces network requests, and it is recommended that smaller files be converted to DataURL.
- arguments:
value
- required: yes
- type: buffer
- description: ibid.
mimeType
- required: no
- type: string
- default:
application/octet-stream
- description: a valid MIME Type, for example,
image/png
,application/pdf
, etc.
- return: A DataURL string.