import * as Bytes from './Bytes.js' import type * as Errors from './Errors.js' import * as Hex from './Hex.js' import * as internal from './internal/codec/base64.js' /** * Encodes a {@link ox#Bytes.Bytes} to a Base64-encoded string (with optional padding and/or URL-safe characters). * * @example * ```ts twoslash * import { Base64, Bytes } from 'ox' * * const value = Base64.fromBytes( * Bytes.fromString('hello world') * ) * // @log: 'aGVsbG8gd29ybGQ=' * ``` * * @example * ### No Padding * * Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option: * * ```ts twoslash * import { Base64, Bytes } from 'ox' * * const value = Base64.fromBytes( * Bytes.fromString('hello world'), * { pad: false } * ) * // @log: 'aGVsbG8gd29ybGQ' * ``` * * ### URL-safe Encoding * * Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option: * * ```ts twoslash * import { Base64, Bytes } from 'ox' * * const value = Base64.fromBytes( * Bytes.fromString('hello wod'), * { url: true } * ) * // @log: 'aGVsbG8gd29_77-9ZA==' * ``` * * @param value - The byte array to encode. * @param options - Encoding options. * @returns The Base64 encoded string. */ export function fromBytes(value: Bytes.Bytes, options: fromBytes.Options = {}) { return internal.fromBytes(value, options) } export declare namespace fromBytes { type Options = { /** * Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string. * * @default true */ pad?: boolean | undefined /** * Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5). * * @default false */ url?: boolean | undefined } type ErrorType = Errors.GlobalErrorType } /** * Encodes a {@link ox#Hex.Hex} to a Base64-encoded string (with optional padding and/or URL-safe characters). * * @example * ```ts twoslash * import { Base64, Hex } from 'ox' * * const value = Base64.fromHex(Hex.fromString('hello world')) * // @log: 'aGVsbG8gd29ybGQ=' * ``` * * @example * ### No Padding * * Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option: * * ```ts twoslash * import { Base64, Hex } from 'ox' * * const value = Base64.fromHex( * Hex.fromString('hello world'), * { pad: false } * ) * // @log: 'aGVsbG8gd29ybGQ' * ``` * * ### URL-safe Encoding * * Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option: * * ```ts twoslash * import { Base64, Hex } from 'ox' * * const value = Base64.fromHex(Hex.fromString('hello wod'), { * url: true * }) * // @log: 'aGVsbG8gd29_77-9ZA==' * ``` * * @param value - The hex value to encode. * @param options - Encoding options. * @returns The Base64 encoded string. */ export function fromHex(value: Hex.Hex, options: fromHex.Options = {}) { return fromBytes(Bytes.fromHex(value), options) } export declare namespace fromHex { type Options = { /** * Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string. * * @default true */ pad?: boolean | undefined /** * Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5). * * @default false */ url?: boolean | undefined } type ErrorType = fromBytes.ErrorType | Errors.GlobalErrorType } /** * Encodes a string to a Base64-encoded string (with optional padding and/or URL-safe characters). * * @example * ```ts twoslash * import { Base64 } from 'ox' * * const value = Base64.fromString('hello world') * // @log: 'aGVsbG8gd29ybGQ=' * ``` * * @example * ### No Padding * * Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option: * * ```ts twoslash * import { Base64 } from 'ox' * * const value = Base64.fromString('hello world', { * pad: false * }) * // @log: 'aGVsbG8gd29ybGQ' * ``` * * ### URL-safe Encoding * * Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option: * * ```ts twoslash * import { Base64 } from 'ox' * * const value = Base64.fromString('hello wod', { url: true }) * // @log: 'aGVsbG8gd29_77-9ZA==' * ``` * * @param value - The string to encode. * @param options - Encoding options. * @returns The Base64 encoded string. */ export function fromString(value: string, options: fromString.Options = {}) { return fromBytes(Bytes.fromString(value), options) } export declare namespace fromString { type Options = { /** * Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string. * * @default true */ pad?: boolean | undefined /** * Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5). * * @default false */ url?: boolean | undefined } type ErrorType = fromBytes.ErrorType | Errors.GlobalErrorType } /** * Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to {@link ox#Bytes.Bytes}. * * @example * ```ts twoslash * import { Base64, Bytes } from 'ox' * * const value = Base64.toBytes('aGVsbG8gd29ybGQ=') * // @log: Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) * ``` * * @param value - The string, hex value, or byte array to encode. * @returns The Base64 decoded {@link ox#Bytes.Bytes}. */ export function toBytes(value: string): Bytes.Bytes { return internal.toBytes(value) } export declare namespace toBytes { type ErrorType = | InvalidCharacterError | InvalidLengthError | InvalidPaddingError | Errors.GlobalErrorType } /** * Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to {@link ox#Hex.Hex}. * * @example * ```ts twoslash * import { Base64, Hex } from 'ox' * * const value = Base64.toHex('aGVsbG8gd29ybGQ=') * // @log: 0x68656c6c6f20776f726c64 * ``` * * @param value - The string, hex value, or byte array to encode. * @returns The Base64 decoded {@link ox#Hex.Hex}. */ export function toHex(value: string): Hex.Hex { return Hex.fromBytes(toBytes(value)) } export declare namespace toHex { type ErrorType = toBytes.ErrorType | Errors.GlobalErrorType } /** * Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to a string. * * @example * ```ts twoslash * import { Base64 } from 'ox' * * const value = Base64.toString('aGVsbG8gd29ybGQ=') * // @log: 'hello world' * ``` * * @param value - The string, hex value, or byte array to encode. * @returns The Base64 decoded string. */ export function toString(value: string): string { return Bytes.toString(toBytes(value)) } export declare namespace toString { type ErrorType = toBytes.ErrorType | Errors.GlobalErrorType } /** Thrown when a Base64 string contains a character outside its alphabet. */ export const InvalidCharacterError = internal.InvalidCharacterError export type InvalidCharacterError = InstanceType< typeof internal.InvalidCharacterError > /** Thrown when a Base64 string has an impossible length. */ export const InvalidLengthError = internal.InvalidLengthError export type InvalidLengthError = InstanceType< typeof internal.InvalidLengthError > /** Thrown when a Base64 string carries more than two padding characters. */ export const InvalidPaddingError = internal.InvalidPaddingError export type InvalidPaddingError = InstanceType< typeof internal.InvalidPaddingError >