aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/@exodus/bytes/base64.d.ts
blob: 064b18feec9edcb41b462aebee616d596890b67e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
 * (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
 *
 * ```js
 * import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
 * import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
 * import { fromBase64any } from '@exodus/bytes/base64.js'
 * ```
 *
 * @module @exodus/bytes/base64.js
 */

/// <reference types="node" />

import type { OutputFormat, Uint8ArrayBuffer } from './array.js';

/**
 * Options for base64 encoding
 */
export interface ToBase64Options {
  /** Whether to include padding characters (default: true for base64, false for base64url) */
  padding?: boolean;
}

/**
 * Padding mode for base64 decoding
 * - `true`: padding is required
 * - `false`: padding is not allowed (default for base64url)
 * - `'both'`: padding is optional (default for base64)
 */
export type PaddingMode = boolean | 'both';

/**
 * Options for base64 decoding
 */
export interface FromBase64Options {
  /** Output format (default: 'uint8') */
  format?: OutputFormat;
  /** Padding mode */
  padding?: PaddingMode;
}

/**
 * Encode a `Uint8Array` to a base64 string (RFC 4648)
 *
 * @param arr - The input bytes
 * @param options - Encoding options
 * @returns The base64 encoded string
 */
export function toBase64(arr: Uint8Array, options?: ToBase64Options): string;

/**
 * Encode a `Uint8Array` to a base64url string (RFC 4648)
 *
 * @param arr - The input bytes
 * @param options - Encoding options (padding defaults to false)
 * @returns The base64url encoded string
 */
export function toBase64url(arr: Uint8Array, options?: ToBase64Options): string;

/**
 * Decode a base64 string to bytes
 *
 * Operates in strict mode for last chunk, does not allow whitespace
 *
 * @param string - The base64 encoded string
 * @param options - Decoding options
 * @returns The decoded bytes
 */
export function fromBase64(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
export function fromBase64(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;

/**
 * Decode a base64url string to bytes
 *
 * Operates in strict mode for last chunk, does not allow whitespace
 *
 * @param string - The base64url encoded string
 * @param options - Decoding options (padding defaults to false)
 * @returns The decoded bytes
 */
export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
export function fromBase64url(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;

/**
 * Decode either base64 or base64url string to bytes
 *
 * Automatically detects the variant based on characters present
 *
 * @param string - The base64 or base64url encoded string
 * @param options - Decoding options
 * @returns The decoded bytes
 */
export function fromBase64any(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
export function fromBase64any(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;