bufferbase 2.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +162 -63
  2. package/dist/cjs/bases.d.ts +152 -45
  3. package/dist/cjs/bases.d.ts.map +1 -1
  4. package/dist/cjs/bases.js +90 -77
  5. package/dist/cjs/bases.js.map +1 -1
  6. package/dist/cjs/bufferbase.d.ts +21 -27
  7. package/dist/cjs/bufferbase.d.ts.map +1 -1
  8. package/dist/cjs/bufferbase.js +21 -31
  9. package/dist/cjs/bufferbase.js.map +1 -1
  10. package/dist/cjs/codec-base.d.ts +56 -0
  11. package/dist/cjs/codec-base.d.ts.map +1 -0
  12. package/dist/cjs/codec-base.js +132 -0
  13. package/dist/cjs/codec-base.js.map +1 -0
  14. package/dist/cjs/codec-block.d.ts +47 -0
  15. package/dist/cjs/codec-block.d.ts.map +1 -0
  16. package/dist/cjs/codec-block.js +213 -0
  17. package/dist/cjs/codec-block.js.map +1 -0
  18. package/dist/cjs/codec-block85.d.ts +56 -0
  19. package/dist/cjs/codec-block85.d.ts.map +1 -0
  20. package/dist/cjs/codec-block85.js +271 -0
  21. package/dist/cjs/codec-block85.js.map +1 -0
  22. package/dist/cjs/codec-radix.d.ts +46 -0
  23. package/dist/cjs/codec-radix.d.ts.map +1 -0
  24. package/dist/cjs/codec-radix.js +181 -0
  25. package/dist/cjs/codec-radix.js.map +1 -0
  26. package/dist/cjs/codec.d.ts +25 -111
  27. package/dist/cjs/codec.d.ts.map +1 -1
  28. package/dist/cjs/codec.js +50 -228
  29. package/dist/cjs/codec.js.map +1 -1
  30. package/dist/cjs/codecs.d.ts +8 -59
  31. package/dist/cjs/codecs.d.ts.map +1 -1
  32. package/dist/cjs/codecs.js +6 -79
  33. package/dist/cjs/codecs.js.map +1 -1
  34. package/dist/cjs/errors.d.ts +65 -48
  35. package/dist/cjs/errors.d.ts.map +1 -1
  36. package/dist/cjs/errors.js +89 -51
  37. package/dist/cjs/errors.js.map +1 -1
  38. package/dist/cjs/example.js +44 -21
  39. package/dist/cjs/example.js.map +1 -1
  40. package/dist/cjs/functions.d.ts +27 -31
  41. package/dist/cjs/functions.d.ts.map +1 -1
  42. package/dist/cjs/functions.js +25 -47
  43. package/dist/cjs/functions.js.map +1 -1
  44. package/dist/cjs/index.d.ts +7 -5
  45. package/dist/cjs/index.d.ts.map +1 -1
  46. package/dist/cjs/index.js +17 -7
  47. package/dist/cjs/index.js.map +1 -1
  48. package/dist/cjs/package.json +12 -7
  49. package/dist/cjs/types.d.ts +108 -71
  50. package/dist/cjs/types.d.ts.map +1 -1
  51. package/dist/esm/bases.d.ts +152 -45
  52. package/dist/esm/bases.d.ts.map +1 -1
  53. package/dist/esm/bases.js +88 -75
  54. package/dist/esm/bases.js.map +1 -1
  55. package/dist/esm/bufferbase.d.ts +21 -27
  56. package/dist/esm/bufferbase.d.ts.map +1 -1
  57. package/dist/esm/bufferbase.js +18 -30
  58. package/dist/esm/bufferbase.js.map +1 -1
  59. package/dist/esm/codec-base.d.ts +56 -0
  60. package/dist/esm/codec-base.d.ts.map +1 -0
  61. package/dist/esm/codec-base.js +127 -0
  62. package/dist/esm/codec-base.js.map +1 -0
  63. package/dist/esm/codec-block.d.ts +47 -0
  64. package/dist/esm/codec-block.d.ts.map +1 -0
  65. package/dist/esm/codec-block.js +209 -0
  66. package/dist/esm/codec-block.js.map +1 -0
  67. package/dist/esm/codec-block85.d.ts +56 -0
  68. package/dist/esm/codec-block85.d.ts.map +1 -0
  69. package/dist/esm/codec-block85.js +267 -0
  70. package/dist/esm/codec-block85.js.map +1 -0
  71. package/dist/esm/codec-radix.d.ts +46 -0
  72. package/dist/esm/codec-radix.d.ts.map +1 -0
  73. package/dist/esm/codec-radix.js +177 -0
  74. package/dist/esm/codec-radix.js.map +1 -0
  75. package/dist/esm/codec.d.ts +25 -111
  76. package/dist/esm/codec.d.ts.map +1 -1
  77. package/dist/esm/codec.js +44 -226
  78. package/dist/esm/codec.js.map +1 -1
  79. package/dist/esm/codecs.d.ts +8 -59
  80. package/dist/esm/codecs.d.ts.map +1 -1
  81. package/dist/esm/codecs.js +7 -80
  82. package/dist/esm/codecs.js.map +1 -1
  83. package/dist/esm/errors.d.ts +65 -48
  84. package/dist/esm/errors.d.ts.map +1 -1
  85. package/dist/esm/errors.js +83 -50
  86. package/dist/esm/errors.js.map +1 -1
  87. package/dist/esm/example.js +45 -22
  88. package/dist/esm/example.js.map +1 -1
  89. package/dist/esm/functions.d.ts +27 -31
  90. package/dist/esm/functions.d.ts.map +1 -1
  91. package/dist/esm/functions.js +26 -48
  92. package/dist/esm/functions.js.map +1 -1
  93. package/dist/esm/index.d.ts +7 -5
  94. package/dist/esm/index.d.ts.map +1 -1
  95. package/dist/esm/index.js +6 -6
  96. package/dist/esm/index.js.map +1 -1
  97. package/dist/esm/package.json +12 -7
  98. package/dist/esm/types.d.ts +108 -71
  99. package/dist/esm/types.d.ts.map +1 -1
  100. package/package.json +12 -7
  101. package/src/algorithms.test.ts +442 -0
  102. package/src/bases.ts +98 -76
  103. package/src/bufferbase.ts +26 -36
  104. package/src/codec-base.ts +181 -0
  105. package/src/codec-block.ts +258 -0
  106. package/src/codec-block85.ts +315 -0
  107. package/src/codec-radix.ts +206 -0
  108. package/src/codec.test.ts +17 -9
  109. package/src/codec.ts +48 -247
  110. package/src/codecs.ts +11 -105
  111. package/src/errors.ts +90 -50
  112. package/src/example.ts +47 -22
  113. package/src/functions.ts +34 -54
  114. package/src/index.ts +27 -7
  115. package/src/types.ts +113 -86
@@ -3,105 +3,142 @@ import { Buffer } from 'node:buffer';
3
3
  * Input type accepted by encode functions.
4
4
  *
5
5
  * Supports both Node.js `Buffer` and standard `Uint8Array` for browser compatibility.
6
- *
7
- * @example
8
- * ```typescript
9
- * // Node.js Buffer
10
- * encode(Buffer.from('Hello'), 'base58');
11
- *
12
- * // Browser Uint8Array
13
- * encode(new Uint8Array([72, 101, 108, 108, 111]), 'base58');
14
- * ```
15
6
  */
16
7
  export type BufferLike = Buffer | Uint8Array;
17
8
  /**
18
- * Supported base encoding names.
9
+ * How bytes are mapped to characters.
19
10
  *
20
- * These names can be used with the function API:
21
- * - `'decimal'` - Base 10 (0-9)
22
- * - `'base16'` / `'hex'` - Hexadecimal (0-9A-F)
23
- * - `'base32'` - RFC 4648 Base32 (A-Z2-7)
24
- * - `'base32crockford'` - Crockford's Base32 (0-9A-HJKMNP-TV-Z)
25
- * - `'base36'` - Alphanumeric (0-9A-Z)
26
- * - `'base52'` - Letters only (A-Za-z)
27
- * - `'base58'` - Bitcoin alphabet (no 0OIl)
28
- * - `'base64'` - Standard Base64 (A-Za-z0-9+/)
29
- * - `'base64url'` - URL-safe Base64 (A-Za-z0-9-_)
30
- * - `'base64xml'` - XML token Base64 (A-Za-z0-9._)
31
- * - `'base64xmlname'` - XML name Base64 (A-Za-z0-9_:)
32
- * - `'ascii85'` - ASCII85 encoding
33
- * - `'base85'` - Base85 encoding
34
- * - `'z85'` - ZeroMQ Z85 encoding
11
+ * An alphabet alone does not determine an encoding: the same 64 characters can
12
+ * be used by a radix conversion or by a bit-block encoding, producing different
13
+ * output. Every codec therefore names its algorithm explicitly.
14
+ *
15
+ * - `'radix'` treats the whole buffer as one big integer and rewrites it in
16
+ * base `alphabet.length`. Works with any alphabet and any input length.
17
+ * Leading zero bytes are preserved as leading `alphabet[0]` characters.
18
+ * Used by Base58, Base36, and custom alphabets. Not RFC 4648.
19
+ * - `'block'` regroups the bit stream into `log2(alphabet.length)`-bit chunks,
20
+ * as specified by RFC 4648. Requires an alphabet of 2, 4, 8, 16, 32, or 64
21
+ * characters. Used by Base16, Base32, and Base64.
22
+ * - `'block85'` — converts each 4-byte block to 5 characters (85^5 > 2^32).
23
+ * Requires an alphabet of exactly 85 characters. Used by Ascii85 and Z85.
24
+ */
25
+ export type Algorithm = 'radix' | 'block' | 'block85';
26
+ /** Options shared by every algorithm. */
27
+ interface CommonSpec {
28
+ /** Characters used for encoding, ordered by value. */
29
+ readonly alphabet: string;
30
+ /**
31
+ * Reject input that is well-formed but not the canonical encoding of any
32
+ * byte sequence (RFC 4648 §3.5). Defaults to `true`.
33
+ *
34
+ * With `strict: false`, `'Zm9='` and `'Zm8='` both decode to `'fo'`, so
35
+ * decoding is no longer injective. Only disable it to read data produced by
36
+ * a lenient encoder.
37
+ */
38
+ readonly strict?: boolean;
39
+ /**
40
+ * Accept either letter case when decoding, and when validating.
41
+ * Requires an alphabet that does not contain both cases of the same letter.
42
+ * Defaults to `false`.
43
+ */
44
+ readonly caseInsensitive?: boolean;
45
+ /**
46
+ * Extra characters accepted when decoding, each mapped to the alphabet
47
+ * character it stands for.
48
+ *
49
+ * Crockford's Base32 uses `{ I: '1', L: '1', O: '0' }` so that a symbol
50
+ * written down by hand is read back as the digit it resembles. Encoding
51
+ * never emits them.
52
+ */
53
+ readonly aliases?: Readonly<Record<string, string>>;
54
+ /**
55
+ * Characters skipped when decoding, such as the hyphens Crockford's Base32
56
+ * allows for readability. Encoding never emits them.
57
+ */
58
+ readonly ignore?: string;
59
+ }
60
+ /** A codec that rewrites the buffer as an integer in base `alphabet.length`. */
61
+ export interface RadixSpec extends CommonSpec {
62
+ readonly algorithm: 'radix';
63
+ /**
64
+ * Symbols that extend the alphabet for a trailing check symbol, which
65
+ * encodes the value modulo `alphabet.length + checkSymbols.length`.
66
+ *
67
+ * Crockford's Base32 uses `'*~$=U'`, giving a modulus of 37. When set, every
68
+ * encoding carries the symbol and every decoding verifies it.
69
+ */
70
+ readonly checkSymbols?: string;
71
+ }
72
+ /** A codec that regroups the bit stream, as specified by RFC 4648. */
73
+ export interface BlockSpec extends CommonSpec {
74
+ readonly algorithm: 'block';
75
+ /**
76
+ * Character used to pad the final block to a whole number of characters,
77
+ * or `false` to emit no padding. Defaults to `'='`.
78
+ */
79
+ readonly pad?: string | false;
80
+ }
81
+ /** A codec that converts each 4-byte block to 5 characters. */
82
+ export interface Block85Spec extends CommonSpec {
83
+ readonly algorithm: 'block85';
84
+ /** Write a block of four zero bytes as `'z'`. Defaults to `false`. */
85
+ readonly zeroShortcut?: boolean;
86
+ /** Write a block of four spaces as `'y'`. Defaults to `false`. */
87
+ readonly spaceShortcut?: boolean;
88
+ /** Wrap the output in `'<~'` and `'~>'`. Defaults to `false`. */
89
+ readonly delimiters?: boolean;
90
+ /**
91
+ * Require the input length to be a multiple of 4 bytes, as Z85 does.
92
+ * Defaults to `false`, which encodes a trailing partial block as
93
+ * `length + 1` characters.
94
+ */
95
+ readonly requireFullBlocks?: boolean;
96
+ }
97
+ /**
98
+ * A complete codec definition: an alphabet plus the algorithm that uses it.
35
99
  *
36
100
  * @example
37
101
  * ```typescript
38
- * import { encode, decode } from 'bufferbase';
102
+ * import { encode, type CodecSpec } from 'bufferbase';
39
103
  *
40
- * const encoded = encode(Buffer.from('Hello'), 'base58');
41
- * const decoded = decode(encoded, 'base58');
104
+ * const binary: CodecSpec = { alphabet: '01', algorithm: 'radix' };
105
+ * encode(Uint8Array.of(5), binary); // '101'
42
106
  * ```
43
107
  */
44
- export type BaseName = 'decimal' | 'base16' | 'hex' | 'base32' | 'base32crockford' | 'base36' | 'base52' | 'base58' | 'base64' | 'base64url' | 'base64xml' | 'base64xmlname' | 'ascii85' | 'base85' | 'z85';
108
+ export type CodecSpec = RadixSpec | BlockSpec | Block85Spec;
45
109
  /**
46
110
  * Options for decode operations.
47
- *
48
- * @example
49
- * ```typescript
50
- * // Decode with fixed buffer size (pads with leading zeros)
51
- * const buffer = decode(encoded, 'base58', { size: 32 });
52
- * ```
53
111
  */
54
112
  export interface DecodeOptions {
55
113
  /**
56
- * Expected buffer size in bytes.
57
- * - If decoded result is smaller, pads with leading zeros.
58
- * - If decoded result is larger, throws `BufferSizeError`.
114
+ * Required byte length of the result.
115
+ *
116
+ * The decoded buffer must be exactly this long, otherwise `BufferSizeError`
117
+ * is thrown. Because leading zeros carry no value in a radix encoding, a
118
+ * `'radix'` codec pads a shorter result with leading zero bytes to reach it;
119
+ * `'block'` and `'block85'` codecs derive the length from the input and can
120
+ * only check it.
59
121
  */
60
122
  size?: number;
61
123
  }
62
124
  /**
63
125
  * Codec interface for encoding/decoding operations.
64
126
  *
65
- * Implemented by the `Codec` class and used by pre-defined codecs in `Codecs`.
66
- *
67
- * @example
68
- * ```typescript
69
- * import { Codecs } from 'bufferbase';
70
- *
71
- * const encoded = Codecs.base58.encode(Buffer.from('Hello'));
72
- * const decoded = Codecs.base58.decode(encoded);
73
- * ```
127
+ * `validate(input)` returns `true` exactly when `decode(input)` succeeds.
74
128
  */
75
129
  export interface ICodec {
76
130
  /** Character set used for encoding. */
77
131
  readonly chars: string;
78
- /**
79
- * Encodes a buffer to a base-encoded string.
80
- * @param buffer - The buffer or Uint8Array to encode
81
- * @returns Base-encoded string
82
- */
132
+ /** The algorithm this codec applies to `chars`. */
133
+ readonly algorithm: Algorithm;
134
+ /** Encodes a buffer to a base-encoded string. */
83
135
  encode(buffer: BufferLike): string;
84
- /**
85
- * Decodes a base-encoded string to a buffer.
86
- * @param encoded - The string to decode
87
- * @param options - Decode options (e.g., fixed size)
88
- * @returns Decoded buffer
89
- * @throws {InvalidCharacterError} If the string contains invalid characters
90
- * @throws {BufferSizeError} If the result exceeds the specified size
91
- */
136
+ /** Decodes a base-encoded string to a buffer. */
92
137
  decode(encoded: string, options?: DecodeOptions): Buffer;
93
- /**
94
- * Validates if a string contains only valid characters for this base.
95
- * @param input - The string to validate
96
- * @returns `true` if valid, `false` otherwise
97
- */
138
+ /** Reports whether `decode` would succeed for this input. */
98
139
  validate(input: string): boolean;
99
- /**
100
- * Converts a string from this base to another base.
101
- * @param target - Target codec to convert to
102
- * @param input - The string to convert
103
- * @returns String in the target base encoding
104
- */
140
+ /** Decodes with this codec, then re-encodes with `target`. */
105
141
  convertTo(target: ICodec, input: string): string;
106
142
  }
143
+ export {};
107
144
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types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AAW,aAAa;IAC5B;;;;;;;;OAQG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;GAIG;AACH,MAAM,WAAW,MAAM;IACrB,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,mDAAmD;IACnD,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAE9B,iDAAiD;IACjD,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC;IAEnC,iDAAiD;IACjD,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IAEzD,6DAA6D;IAC7D,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAEjC,8DAA8D;IAC9D,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;CAClD"}
@@ -1,25 +1,19 @@
1
- import type { BaseName } from './types.js';
1
+ import type { CodecSpec } from './types.js';
2
2
  /**
3
- * Character sets for common base encodings.
3
+ * Character sets, ordered by value.
4
4
  *
5
- * Use these with `createCodec()` or the deprecated `BufferEncoder` class
6
- * when you need direct access to character sets.
7
- *
8
- * @example
9
- * ```typescript
10
- * import { Chars, createCodec } from 'bufferbase';
11
- *
12
- * const codec = createCodec(Chars.Base58);
13
- * const encoded = codec.encode(Buffer.from('Hello'));
14
- * ```
5
+ * A character set does not by itself describe an encoding — see `Bases` for
6
+ * the alphabet/algorithm pairs that do.
15
7
  */
16
8
  export declare const Chars: {
17
9
  /** Base 10: 0-9 */
18
10
  readonly Decimal: "0123456789";
19
- /** Hexadecimal: 0-9A-F */
11
+ /** RFC 4648 Table 5 — Base16: 0-9A-F */
20
12
  readonly Base16: "0123456789ABCDEF";
21
- /** RFC 4648 Base32: A-Z2-7 */
13
+ /** RFC 4648 Table 3 — Base32: A-Z2-7 */
22
14
  readonly Base32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
15
+ /** RFC 4648 Table 4 — Base32 with extended hex alphabet: 0-9A-V */
16
+ readonly Base32Hex: "0123456789ABCDEFGHIJKLMNOPQRSTUV";
23
17
  /** Crockford's Base32: 0-9A-HJKMNP-TV-Z (no I, L, O, U) */
24
18
  readonly Base32Crockford: "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
25
19
  /** Alphanumeric: 0-9A-Z */
@@ -28,55 +22,168 @@ export declare const Chars: {
28
22
  readonly Base52: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
29
23
  /** Bitcoin alphabet: 1-9A-HJ-NP-Za-km-z (no 0, O, I, l) */
30
24
  readonly Base58: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
31
- /** Standard Base64: A-Za-z0-9+/ */
25
+ /** RFC 4648 Table 1 — Base64: A-Za-z0-9+/ */
32
26
  readonly Base64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33
- /** URL-safe Base64: A-Za-z0-9-_ */
27
+ /** RFC 4648 Table 2 — URL-safe Base64: A-Za-z0-9-_ */
34
28
  readonly Base64Url: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
35
29
  /** XML token Base64: A-Za-z0-9._ */
36
30
  readonly Base64Xml: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
37
31
  /** XML name Base64: A-Za-z0-9_: */
38
32
  readonly Base64XmlName: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:";
39
- /** ASCII85 encoding */
40
- readonly Ascii85: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstu";
41
- /** Base85 encoding */
42
- readonly Base85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~";
43
- /** ZeroMQ Z85 encoding */
33
+ /** Ascii85: the 85 printable characters from `!` (0x21) to `u` (0x75) */
34
+ readonly Ascii85: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu";
35
+ /** ZeroMQ Z85 (RFC 32): avoids the quote and backslash characters */
44
36
  readonly Z85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
37
+ /** The five symbols Crockford's Base32 adds for its check symbol, giving a modulus of 37 */
38
+ readonly CrockfordCheck: "*~$=U";
45
39
  };
46
40
  /**
47
- * Resolves a base name or character set string to a character set.
48
- *
49
- * This function is used internally by the function API to convert
50
- * base names (like `'base58'`) to their character sets.
41
+ * The encodings this package knows by name.
51
42
  *
52
- * @param base - Base name (e.g., `'base58'`) or custom character set string
53
- * @returns The character set string
54
- * @throws {UnknownBaseError} If base name is not recognized and string length <= 15
43
+ * Each entry pairs an alphabet with the algorithm that reads it, because the
44
+ * alphabet alone is ambiguous: `base64` and `radix64` share Table 1 but
45
+ * produce different strings for the same bytes.
55
46
  *
56
47
  * @example
57
48
  * ```typescript
58
- * import { resolveChars } from 'bufferbase';
49
+ * import { encode, Bases } from 'bufferbase';
59
50
  *
60
- * resolveChars('base58'); // Returns Bitcoin alphabet
61
- * resolveChars('0123456789ABCDEF'); // Returns the string as-is (custom charset)
62
- * resolveChars('invalid'); // Throws UnknownBaseError
51
+ * encode(Buffer.from('fo'), 'base64'); // 'Zm8=' — RFC 4648
52
+ * encode(Buffer.from('fo'), 'radix64'); // 'GZv' — radix conversion
63
53
  * ```
64
54
  */
65
- export declare function resolveChars(base: BaseName | string): string;
55
+ export declare const Bases: {
56
+ readonly decimal: {
57
+ readonly alphabet: "0123456789";
58
+ readonly algorithm: "radix";
59
+ };
60
+ readonly base36: {
61
+ readonly alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
62
+ readonly algorithm: "radix";
63
+ readonly caseInsensitive: true;
64
+ };
65
+ readonly base52: {
66
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
67
+ readonly algorithm: "radix";
68
+ };
69
+ readonly base58: {
70
+ readonly alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
71
+ readonly algorithm: "radix";
72
+ };
73
+ /**
74
+ * Crockford's Base32, which specifies a notation for numbers rather than for
75
+ * byte streams, so a radix conversion is the right reading of it.
76
+ *
77
+ * Decoding accepts either case, reads `I` and `L` as `1` and `O` as `0`, and
78
+ * ignores hyphens.
79
+ */
80
+ readonly base32crockford: {
81
+ readonly alphabet: "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
82
+ readonly algorithm: "radix";
83
+ readonly caseInsensitive: true;
84
+ readonly aliases: {
85
+ readonly I: "1";
86
+ readonly L: "1";
87
+ readonly O: "0";
88
+ };
89
+ readonly ignore: "-";
90
+ };
91
+ /** Crockford's Base32 carrying its trailing check symbol. */
92
+ readonly base32crockfordcheck: {
93
+ readonly alphabet: "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
94
+ readonly algorithm: "radix";
95
+ readonly caseInsensitive: true;
96
+ readonly aliases: {
97
+ readonly I: "1";
98
+ readonly L: "1";
99
+ readonly O: "0";
100
+ };
101
+ readonly ignore: "-";
102
+ readonly checkSymbols: "*~$=U";
103
+ };
104
+ readonly base16: {
105
+ readonly alphabet: "0123456789ABCDEF";
106
+ readonly algorithm: "block";
107
+ readonly pad: false;
108
+ readonly caseInsensitive: true;
109
+ };
110
+ readonly hex: {
111
+ readonly alphabet: "0123456789ABCDEF";
112
+ readonly algorithm: "block";
113
+ readonly pad: false;
114
+ readonly caseInsensitive: true;
115
+ };
116
+ readonly base32: {
117
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
118
+ readonly algorithm: "block";
119
+ readonly caseInsensitive: true;
120
+ };
121
+ readonly base32hex: {
122
+ readonly alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV";
123
+ readonly algorithm: "block";
124
+ readonly caseInsensitive: true;
125
+ };
126
+ readonly base64: {
127
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
128
+ readonly algorithm: "block";
129
+ };
130
+ /** Unpadded, as used by JWT and other URL-facing formats. */
131
+ readonly base64url: {
132
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
133
+ readonly algorithm: "block";
134
+ readonly pad: false;
135
+ };
136
+ readonly base64xml: {
137
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
138
+ readonly algorithm: "block";
139
+ readonly pad: false;
140
+ };
141
+ readonly base64xmlname: {
142
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:";
143
+ readonly algorithm: "block";
144
+ readonly pad: false;
145
+ };
146
+ /** Ascii85 with the zero-block shortcut, matching Python's `a85encode`. */
147
+ readonly ascii85: {
148
+ readonly alphabet: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu";
149
+ readonly algorithm: "block85";
150
+ readonly zeroShortcut: true;
151
+ };
152
+ /** ZeroMQ Z85, which has no padding and so encodes whole blocks only. */
153
+ readonly z85: {
154
+ readonly alphabet: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
155
+ readonly algorithm: "block85";
156
+ readonly requireFullBlocks: true;
157
+ };
158
+ readonly radix16: {
159
+ readonly alphabet: "0123456789ABCDEF";
160
+ readonly algorithm: "radix";
161
+ };
162
+ readonly radix32: {
163
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
164
+ readonly algorithm: "radix";
165
+ };
166
+ readonly radix64: {
167
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
168
+ readonly algorithm: "radix";
169
+ };
170
+ readonly radix64url: {
171
+ readonly alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
172
+ readonly algorithm: "radix";
173
+ };
174
+ };
66
175
  /**
67
- * Checks if a string is a valid base name.
68
- *
69
- * @param value - The string to check
70
- * @returns `true` if the value is a valid `BaseName`, `false` otherwise
71
- *
72
- * @example
73
- * ```typescript
74
- * import { isBaseName } from 'bufferbase';
176
+ * The names accepted wherever a base is expected.
177
+ */
178
+ export type BaseName = keyof typeof Bases;
179
+ /**
180
+ * Resolves a base name to its codec spec, and passes a spec through unchanged.
75
181
  *
76
- * isBaseName('base58'); // true
77
- * isBaseName('BASE58'); // true (case-insensitive)
78
- * isBaseName('unknown'); // false
79
- * ```
182
+ * @throws {UnknownBaseError} If the name is not one of `Bases`
183
+ */
184
+ export declare function resolveSpec(base: BaseName | CodecSpec): CodecSpec;
185
+ /**
186
+ * Checks whether a string names one of the encodings in `Bases`.
80
187
  */
81
188
  export declare function isBaseName(value: string): value is BaseName;
82
189
  //# sourceMappingURL=bases.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bases.d.ts","sourceRoot":"","sources":["../../src/bases.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,KAAK;IAChB,mBAAmB;;IAEnB,0BAA0B;;IAE1B,8BAA8B;;IAE9B,2DAA2D;;IAE3D,2BAA2B;;IAE3B,2BAA2B;;IAE3B,2DAA2D;;IAE3D,mCAAmC;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