toBase64
Encodes a string (as UTF-8) or raw bytes to Base64, without relying on btoa or Buffer, so it
behaves the same in a browser, Node.js and GJS.
Available since v3.1.8
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input | string | Uint8Array<ArrayBufferLike> | The text to encode as UTF-8, or the bytes to encode as they are |
options | ToBase64Options | The URL-safe alphabet and the padding — see ToBase64Options |
Returns
Section titled “Returns”string — The Base64 text
Examples
Section titled “Examples”Encode text as UTF-8 Base64
Section titled “Encode text as UTF-8 Base64”Non-ASCII text is encoded as UTF-8 first, so it round-trips with fromBase64.
URL-safe variant for tokens and URLs
Section titled “URL-safe variant for tokens and URLs”Uses - and _ and drops the padding, ready for a query string or a JWT-like token.
Related Types
Section titled “Related Types”ToBase64Options
Section titled “ToBase64Options”Options for toBase64.
