bufferbase 1.3.0 → 3.0.0

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 (144) hide show
  1. package/README.md +186 -17
  2. package/dist/cjs/bases.d.ts +189 -0
  3. package/dist/cjs/bases.d.ts.map +1 -0
  4. package/dist/cjs/bases.js +134 -0
  5. package/dist/cjs/bases.js.map +1 -0
  6. package/dist/cjs/bufferbase.d.ts +22 -45
  7. package/dist/cjs/bufferbase.d.ts.map +1 -1
  8. package/dist/cjs/bufferbase.js +26 -120
  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 +38 -0
  27. package/dist/cjs/codec.d.ts.map +1 -0
  28. package/dist/cjs/codec.js +69 -0
  29. package/dist/cjs/codec.js.map +1 -0
  30. package/dist/cjs/codecs.d.ts +20 -0
  31. package/dist/cjs/codecs.d.ts.map +1 -0
  32. package/dist/cjs/codecs.js +19 -0
  33. package/dist/cjs/codecs.js.map +1 -0
  34. package/dist/cjs/errors.d.ts +93 -0
  35. package/dist/cjs/errors.d.ts.map +1 -0
  36. package/dist/cjs/errors.js +130 -0
  37. package/dist/cjs/errors.js.map +1 -0
  38. package/dist/cjs/example.js +49 -33
  39. package/dist/cjs/example.js.map +1 -1
  40. package/dist/cjs/functions.d.ts +58 -0
  41. package/dist/cjs/functions.d.ts.map +1 -0
  42. package/dist/cjs/functions.js +71 -0
  43. package/dist/cjs/functions.js.map +1 -0
  44. package/dist/cjs/index.d.ts +10 -1
  45. package/dist/cjs/index.d.ts.map +1 -1
  46. package/dist/cjs/index.js +35 -4
  47. package/dist/cjs/index.js.map +1 -1
  48. package/dist/cjs/package.json +17 -12
  49. package/dist/cjs/types.d.ts +144 -0
  50. package/dist/cjs/types.d.ts.map +1 -0
  51. package/dist/cjs/types.js +3 -0
  52. package/dist/cjs/types.js.map +1 -0
  53. package/dist/esm/bases.d.ts +189 -0
  54. package/dist/esm/bases.d.ts.map +1 -0
  55. package/dist/esm/bases.js +129 -0
  56. package/dist/esm/bases.js.map +1 -0
  57. package/dist/esm/bufferbase.d.ts +22 -45
  58. package/dist/esm/bufferbase.d.ts.map +1 -1
  59. package/dist/esm/bufferbase.js +21 -115
  60. package/dist/esm/bufferbase.js.map +1 -1
  61. package/dist/esm/codec-base.d.ts +56 -0
  62. package/dist/esm/codec-base.d.ts.map +1 -0
  63. package/dist/esm/codec-base.js +127 -0
  64. package/dist/esm/codec-base.js.map +1 -0
  65. package/dist/esm/codec-block.d.ts +47 -0
  66. package/dist/esm/codec-block.d.ts.map +1 -0
  67. package/dist/esm/codec-block.js +209 -0
  68. package/dist/esm/codec-block.js.map +1 -0
  69. package/dist/esm/codec-block85.d.ts +56 -0
  70. package/dist/esm/codec-block85.d.ts.map +1 -0
  71. package/dist/esm/codec-block85.js +267 -0
  72. package/dist/esm/codec-block85.js.map +1 -0
  73. package/dist/esm/codec-radix.d.ts +46 -0
  74. package/dist/esm/codec-radix.d.ts.map +1 -0
  75. package/dist/esm/codec-radix.js +177 -0
  76. package/dist/esm/codec-radix.js.map +1 -0
  77. package/dist/esm/codec.d.ts +38 -0
  78. package/dist/esm/codec.d.ts.map +1 -0
  79. package/dist/esm/codec.js +60 -0
  80. package/dist/esm/codec.js.map +1 -0
  81. package/dist/esm/codecs.d.ts +20 -0
  82. package/dist/esm/codecs.d.ts.map +1 -0
  83. package/dist/esm/codecs.js +16 -0
  84. package/dist/esm/codecs.js.map +1 -0
  85. package/dist/esm/errors.d.ts +93 -0
  86. package/dist/esm/errors.d.ts.map +1 -0
  87. package/dist/esm/errors.js +119 -0
  88. package/dist/esm/errors.js.map +1 -0
  89. package/dist/esm/example.js +48 -32
  90. package/dist/esm/example.js.map +1 -1
  91. package/dist/esm/functions.d.ts +58 -0
  92. package/dist/esm/functions.d.ts.map +1 -0
  93. package/dist/esm/functions.js +65 -0
  94. package/dist/esm/functions.js.map +1 -0
  95. package/dist/esm/index.d.ts +10 -1
  96. package/dist/esm/index.d.ts.map +1 -1
  97. package/dist/esm/index.js +12 -1
  98. package/dist/esm/index.js.map +1 -1
  99. package/dist/esm/package.json +17 -12
  100. package/dist/esm/types.d.ts +144 -0
  101. package/dist/esm/types.d.ts.map +1 -0
  102. package/dist/esm/types.js +2 -0
  103. package/dist/esm/types.js.map +1 -0
  104. package/package.json +19 -14
  105. package/src/algorithms.test.ts +442 -0
  106. package/src/bases.ts +142 -0
  107. package/src/bufferbase.test.ts +44 -27
  108. package/src/bufferbase.ts +32 -117
  109. package/src/codec-base.ts +181 -0
  110. package/src/codec-block.ts +258 -0
  111. package/src/codec-block85.ts +315 -0
  112. package/src/codec-radix.ts +206 -0
  113. package/src/codec.test.ts +328 -0
  114. package/src/codec.ts +67 -0
  115. package/src/codecs.ts +24 -0
  116. package/src/errors.ts +127 -0
  117. package/src/example.ts +49 -34
  118. package/src/functions.ts +73 -0
  119. package/src/index.ts +39 -6
  120. package/src/types.ts +156 -0
  121. package/dist/cjs/bufferbase.d.mts +0 -60
  122. package/dist/cjs/bufferbase.d.mts.map +0 -1
  123. package/dist/cjs/bufferbase.mjs +0 -150
  124. package/dist/cjs/bufferbase.mjs.map +0 -1
  125. package/dist/cjs/example.d.mts +0 -2
  126. package/dist/cjs/example.d.mts.map +0 -1
  127. package/dist/cjs/example.mjs +0 -39
  128. package/dist/cjs/example.mjs.map +0 -1
  129. package/dist/cjs/index.d.mts +0 -2
  130. package/dist/cjs/index.d.mts.map +0 -1
  131. package/dist/cjs/index.mjs +0 -11
  132. package/dist/cjs/index.mjs.map +0 -1
  133. package/dist/esm/bufferbase.d.mts +0 -60
  134. package/dist/esm/bufferbase.d.mts.map +0 -1
  135. package/dist/esm/bufferbase.mjs +0 -141
  136. package/dist/esm/bufferbase.mjs.map +0 -1
  137. package/dist/esm/example.d.mts +0 -2
  138. package/dist/esm/example.d.mts.map +0 -1
  139. package/dist/esm/example.mjs +0 -37
  140. package/dist/esm/example.mjs.map +0 -1
  141. package/dist/esm/index.d.mts +0 -2
  142. package/dist/esm/index.d.mts.map +0 -1
  143. package/dist/esm/index.mjs +0 -2
  144. package/dist/esm/index.mjs.map +0 -1
package/src/errors.ts ADDED
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Error thrown when an invalid character is encountered during decoding.
3
+ */
4
+ export class InvalidCharacterError extends Error {
5
+ /**
6
+ * @param char - The invalid character that was encountered (optional)
7
+ */
8
+ constructor(char?: string) {
9
+ const message = char ? `Invalid character found: '${char}'` : 'Invalid character found';
10
+ super(message);
11
+ this.name = 'InvalidCharacterError';
12
+ }
13
+ }
14
+
15
+ /**
16
+ * Error thrown when the decoded buffer does not have the size requested
17
+ * through `DecodeOptions.size`.
18
+ */
19
+ export class BufferSizeError extends Error {
20
+ /**
21
+ * @param actual - The actual decoded buffer size
22
+ * @param expected - The expected (specified) buffer size
23
+ */
24
+ constructor(actual: number, expected: number) {
25
+ super(`Buffer size ${actual} does not match specified size ${expected}`);
26
+ this.name = 'BufferSizeError';
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Error thrown when an unknown base name is provided.
32
+ */
33
+ export class UnknownBaseError extends Error {
34
+ /**
35
+ * @param name - The unknown base name that was provided
36
+ */
37
+ constructor(name: string) {
38
+ super(
39
+ `Unknown base encoding: '${name}'. ` +
40
+ 'Pass a CodecSpec such as { alphabet, algorithm: \'radix\' } to use a custom alphabet.',
41
+ );
42
+ this.name = 'UnknownBaseError';
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Error thrown when a string cannot be the encoding of any byte sequence
48
+ * because of its length.
49
+ *
50
+ * A Base64 string of 5 characters, for example, describes 30 bits: too many
51
+ * for 3 bytes and too few for 4.
52
+ */
53
+ export class InvalidLengthError extends Error {
54
+ /**
55
+ * @param length - The length that cannot occur
56
+ * @param detail - What the length should have been
57
+ */
58
+ constructor(length: number, detail: string) {
59
+ super(`Invalid encoded length ${length}: ${detail}`);
60
+ this.name = 'InvalidLengthError';
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Error thrown when input is well-formed but is not the canonical encoding of
66
+ * the bytes it decodes to (RFC 4648 §3.5).
67
+ *
68
+ * Accepting such input makes decoding non-injective: `'Zm8='` and `'Zm9='`
69
+ * would both decode to `'fo'`. Pass `strict: false` in the codec spec to allow
70
+ * it.
71
+ */
72
+ export class NonCanonicalError extends Error {
73
+ /**
74
+ * @param detail - What made the input non-canonical
75
+ */
76
+ constructor(detail: string) {
77
+ super(`Non-canonical encoding: ${detail}`);
78
+ this.name = 'NonCanonicalError';
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Error thrown when a codec spec cannot be realised, such as a `'block'`
84
+ * algorithm with an alphabet whose length is not a power of two.
85
+ */
86
+ export class InvalidCodecError extends Error {
87
+ /**
88
+ * @param detail - Why the spec is unusable
89
+ */
90
+ constructor(detail: string) {
91
+ super(`Invalid codec: ${detail}`);
92
+ this.name = 'InvalidCodecError';
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Error thrown when a group of characters denotes a number too large for the
98
+ * bytes it is supposed to encode.
99
+ *
100
+ * In an Ascii85 group, five characters must denote a value below 2^32; `'s8W-!'`
101
+ * does not, so it encodes nothing. Unlike `NonCanonicalError` this is never
102
+ * allowed, because there is no byte sequence to decode to.
103
+ */
104
+ export class ValueRangeError extends Error {
105
+ /**
106
+ * @param group - The group of characters that is out of range
107
+ */
108
+ constructor(group: string) {
109
+ super(`Encoded group '${group}' exceeds the maximum value for its block`);
110
+ this.name = 'ValueRangeError';
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Error thrown when a trailing check symbol does not match the value it
116
+ * follows, which means the string was mistyped or corrupted.
117
+ */
118
+ export class CheckSymbolError extends Error {
119
+ /**
120
+ * @param found - The check symbol present in the input
121
+ * @param expected - The check symbol the value calls for
122
+ */
123
+ constructor(found: string, expected: string) {
124
+ super(`Check symbol '${found}' does not match the expected '${expected}'`);
125
+ this.name = 'CheckSymbolError';
126
+ }
127
+ }
package/src/example.ts CHANGED
@@ -1,43 +1,58 @@
1
- import { Chars, BufferEncoder, Converter, Validator } from './bufferbase.js';
1
+ import { Buffer } from 'node:buffer';
2
+ import { encode, decode, convert, validate, createCodec, Chars, Codecs } from './index.js';
2
3
 
3
- // Example buffer
4
4
  const bytes = Buffer.from('Hello, World!', 'utf8');
5
5
 
6
- // Encoding buffer to Base32
7
- const encoder = new BufferEncoder(Chars.Base32Crockford);
8
- const base32encoded = encoder.encode(bytes);
9
-
10
- // Converting Base32 to Base58
11
- const converter_32to58 = new Converter(Chars.Base32Crockford, Chars.Base58);
12
- const base58encoded = converter_32to58.convert(base32encoded);
6
+ // The same bytes under each algorithm.
7
+ console.log('=== Algorithms ===');
8
+ console.table({
9
+ 'block base64': encode(bytes, 'base64'),
10
+ 'block base32': encode(bytes, 'base32'),
11
+ 'block85 ascii85': encode(bytes, 'ascii85'),
12
+ 'radix base58': encode(bytes, 'base58'),
13
+ 'radix base32crockford': encode(bytes, 'base32crockford'),
14
+ });
13
15
 
14
- // Converting Base32 to Base58
15
- const converter_58to64 = new Converter(Chars.Base58, Chars.Base64_URL_SAFE);
16
- const base64encoded = converter_58to64.convert(base58encoded);
16
+ // Converting goes through the bytes, so any pair works.
17
+ console.log('\n=== Function API ===');
18
+ const base58 = encode(bytes, 'base58');
19
+ const base64url = convert(base58, 'base58', 'base64url');
20
+ console.table({
21
+ bytes: bytes.toString('utf8'),
22
+ base58,
23
+ base64url,
24
+ valid: validate(base64url, 'base64url'),
25
+ decoded: decode(base64url, 'base64url').toString('utf8'),
26
+ });
17
27
 
18
- // Validating Base64
19
- const validatorB64 = new Validator(Chars.Base64_URL_SAFE);
20
- const isValidAsBase64 = validatorB64.validate(base64encoded);
28
+ // validate() agrees with decode(): it checks length, padding and canonicality.
29
+ console.log('\n=== Rejected input ===');
30
+ console.table({
31
+ "'Zm8=' (canonical)": validate('Zm8=', 'base64'),
32
+ "'Zm9=' (spare bits set)": validate('Zm9=', 'base64'),
33
+ "'Zm9vd' (5 characters)": validate('Zm9vd', 'base64'),
34
+ });
21
35
 
22
- // Decoding Base64
23
- const decoder = new BufferEncoder(Chars.Base64_URL_SAFE);
24
- const decoded = decoder.decode(base64encoded);
36
+ // Crockford's Base32 reads what a person is likely to have written down.
37
+ console.log('\n=== Crockford ===');
38
+ const crockford = encode(bytes, 'base32crockfordcheck');
39
+ console.table({
40
+ encoded: crockford,
41
+ 'as typed': decode(`${crockford.slice(0, 4)}-${crockford.slice(4)}`.toLowerCase(), 'base32crockfordcheck').toString('utf8'),
42
+ 'one character changed': validate(`${crockford.slice(0, -2)}Z${crockford.slice(-1)}`, 'base32crockfordcheck'),
43
+ });
25
44
 
45
+ // Codecs, and a custom alphabet.
46
+ console.log('\n=== Codecs and custom alphabets ===');
47
+ const binary = createCodec('01');
48
+ const adobe = createCodec({
49
+ alphabet: Chars.Ascii85,
50
+ algorithm: 'block85',
51
+ zeroShortcut: true,
52
+ delimiters: true,
53
+ });
26
54
  console.table({
27
- bytes: bytes.toString('utf8'),
28
- base32encoded,
29
- base58encoded,
30
- base64encoded,
31
- isValidAsBase64,
32
- decoded: decoded.toString('utf8'),
55
+ 'Codecs.base58': Codecs.base58.encode(bytes),
56
+ 'binary of [5]': binary.encode(Uint8Array.of(5)),
57
+ 'adobe ascii85': adobe.encode(bytes),
33
58
  });
34
- // ┌─────────────────┬─────────────────────────┐
35
- // │ (index) │ Values │
36
- // ├─────────────────┼─────────────────────────┤
37
- // │ bytes │ 'Hello, World!' │
38
- // │ base32encoded │ '4GSBCDHQJR82QDXS6RS11' │
39
- // │ base58encoded │ '72k1xXWG59fYdzSNoA' │
40
- // │ base64encoded │ 'BIZWxsbywgV29ybGQh' │
41
- // │ isValidAsBase64 │ true │
42
- // │ decoded │ 'Hello, World!' │
43
- // └─────────────────┴─────────────────────────┘
@@ -0,0 +1,73 @@
1
+ import type { BufferLike, CodecSpec, DecodeOptions } from './types.js';
2
+ import type { Buffer } from 'node:buffer';
3
+ import { resolveSpec, type BaseName } from './bases.js';
4
+ import { getCodec } from './codec.js';
5
+
6
+ /** A named encoding, or a spec describing one directly. */
7
+ export type Base = BaseName | CodecSpec;
8
+
9
+ /**
10
+ * Encodes a buffer to a base-encoded string.
11
+ *
12
+ * @param buffer - Buffer or Uint8Array to encode
13
+ * @param base - Base name (e.g. `'base58'`) or a codec spec
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * encode(Buffer.from('fo'), 'base64'); // 'Zm8='
18
+ * encode(Uint8Array.of(5), { alphabet: '01', algorithm: 'radix' }); // '101'
19
+ * ```
20
+ */
21
+ export function encode(buffer: BufferLike, base: Base): string {
22
+ return getCodec(resolveSpec(base)).encode(buffer);
23
+ }
24
+
25
+ /**
26
+ * Decodes a base-encoded string to a buffer.
27
+ *
28
+ * @param encoded - String to decode
29
+ * @param base - Base name (e.g. `'base58'`) or a codec spec
30
+ * @param options - Decode options, such as a required `size`
31
+ * @throws {InvalidCharacterError} If a character is not in the alphabet
32
+ * @throws {InvalidLengthError} If the length cannot encode a whole number of bytes
33
+ * @throws {NonCanonicalError} If spare bits are set and the codec is strict
34
+ * @throws {BufferSizeError} If the result does not have the requested `size`
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * decode('Zm8=', 'base64'); // Buffer('fo')
39
+ * decode('9Ajdvzr', 'base58', { size: 32 });
40
+ * ```
41
+ */
42
+ export function decode(encoded: string, base: Base, options?: DecodeOptions): Buffer {
43
+ return getCodec(resolveSpec(base)).decode(encoded, options);
44
+ }
45
+
46
+ /**
47
+ * Converts a string from one base to another, through the bytes they describe.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * convert('9Ajdvzr', 'base58', 'base64url');
52
+ * convert(legacy, 'radix64', 'base64'); // migrate data written by bufferbase 2
53
+ * ```
54
+ */
55
+ export function convert(input: string, from: Base, to: Base): string {
56
+ const source = getCodec(resolveSpec(from));
57
+ const target = getCodec(resolveSpec(to));
58
+ return source.convertTo(target, input);
59
+ }
60
+
61
+ /**
62
+ * Reports whether `decode` would succeed for this input.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * validate('Zm8=', 'base64'); // true
67
+ * validate('Zm9=', 'base64'); // false — spare bits are not zero
68
+ * validate('Zm9vd', 'base64'); // false — 5 characters cannot be whole bytes
69
+ * ```
70
+ */
71
+ export function validate(input: string, base: Base): boolean {
72
+ return getCodec(resolveSpec(base)).validate(input);
73
+ }
package/src/index.ts CHANGED
@@ -1,8 +1,41 @@
1
+ // Types
2
+ export type {
3
+ Algorithm,
4
+ Block85Spec,
5
+ BlockSpec,
6
+ BufferLike,
7
+ CodecSpec,
8
+ DecodeOptions,
9
+ ICodec,
10
+ RadixSpec,
11
+ } from './types.js';
12
+ export type { BaseName } from './bases.js';
13
+ export type { Base } from './functions.js';
14
+ export type { CodecName } from './codecs.js';
15
+
16
+ // Errors
1
17
  export {
2
- Chars,
3
- BufferEncoder,
4
- Converter,
18
+ BufferSizeError,
19
+ CheckSymbolError,
5
20
  InvalidCharacterError,
6
- Validator,
7
- validate,
8
- } from './bufferbase.js';
21
+ InvalidCodecError,
22
+ InvalidLengthError,
23
+ NonCanonicalError,
24
+ UnknownBaseError,
25
+ ValueRangeError,
26
+ } from './errors.js';
27
+
28
+ // Codec implementations and factory
29
+ export { Block85Codec, BlockCodec, Codec, RadixCodec, createCodec, getCodec } from './codec.js';
30
+
31
+ // Character sets, encoding definitions, and lookup helpers
32
+ export { Bases, Chars, isBaseName, resolveSpec } from './bases.js';
33
+
34
+ // Simple function API
35
+ export { convert, decode, encode, validate } from './functions.js';
36
+
37
+ // Pre-defined codecs (namespaced)
38
+ export { Codecs } from './codecs.js';
39
+
40
+ // Deprecated exports for backward compatibility
41
+ export { BufferEncoder, Converter, Validator } from './bufferbase.js';
package/src/types.ts ADDED
@@ -0,0 +1,156 @@
1
+ import { Buffer } from 'node:buffer';
2
+
3
+ /**
4
+ * Input type accepted by encode functions.
5
+ *
6
+ * Supports both Node.js `Buffer` and standard `Uint8Array` for browser compatibility.
7
+ */
8
+ export type BufferLike = Buffer | Uint8Array;
9
+
10
+ /**
11
+ * How bytes are mapped to characters.
12
+ *
13
+ * An alphabet alone does not determine an encoding: the same 64 characters can
14
+ * be used by a radix conversion or by a bit-block encoding, producing different
15
+ * output. Every codec therefore names its algorithm explicitly.
16
+ *
17
+ * - `'radix'` — treats the whole buffer as one big integer and rewrites it in
18
+ * base `alphabet.length`. Works with any alphabet and any input length.
19
+ * Leading zero bytes are preserved as leading `alphabet[0]` characters.
20
+ * Used by Base58, Base36, and custom alphabets. Not RFC 4648.
21
+ * - `'block'` — regroups the bit stream into `log2(alphabet.length)`-bit chunks,
22
+ * as specified by RFC 4648. Requires an alphabet of 2, 4, 8, 16, 32, or 64
23
+ * characters. Used by Base16, Base32, and Base64.
24
+ * - `'block85'` — converts each 4-byte block to 5 characters (85^5 > 2^32).
25
+ * Requires an alphabet of exactly 85 characters. Used by Ascii85 and Z85.
26
+ */
27
+ export type Algorithm = 'radix' | 'block' | 'block85';
28
+
29
+ /** Options shared by every algorithm. */
30
+ interface CommonSpec {
31
+ /** Characters used for encoding, ordered by value. */
32
+ readonly alphabet: string;
33
+ /**
34
+ * Reject input that is well-formed but not the canonical encoding of any
35
+ * byte sequence (RFC 4648 §3.5). Defaults to `true`.
36
+ *
37
+ * With `strict: false`, `'Zm9='` and `'Zm8='` both decode to `'fo'`, so
38
+ * decoding is no longer injective. Only disable it to read data produced by
39
+ * a lenient encoder.
40
+ */
41
+ readonly strict?: boolean;
42
+ /**
43
+ * Accept either letter case when decoding, and when validating.
44
+ * Requires an alphabet that does not contain both cases of the same letter.
45
+ * Defaults to `false`.
46
+ */
47
+ readonly caseInsensitive?: boolean;
48
+ /**
49
+ * Extra characters accepted when decoding, each mapped to the alphabet
50
+ * character it stands for.
51
+ *
52
+ * Crockford's Base32 uses `{ I: '1', L: '1', O: '0' }` so that a symbol
53
+ * written down by hand is read back as the digit it resembles. Encoding
54
+ * never emits them.
55
+ */
56
+ readonly aliases?: Readonly<Record<string, string>>;
57
+ /**
58
+ * Characters skipped when decoding, such as the hyphens Crockford's Base32
59
+ * allows for readability. Encoding never emits them.
60
+ */
61
+ readonly ignore?: string;
62
+ }
63
+
64
+ /** A codec that rewrites the buffer as an integer in base `alphabet.length`. */
65
+ export interface RadixSpec extends CommonSpec {
66
+ readonly algorithm: 'radix';
67
+ /**
68
+ * Symbols that extend the alphabet for a trailing check symbol, which
69
+ * encodes the value modulo `alphabet.length + checkSymbols.length`.
70
+ *
71
+ * Crockford's Base32 uses `'*~$=U'`, giving a modulus of 37. When set, every
72
+ * encoding carries the symbol and every decoding verifies it.
73
+ */
74
+ readonly checkSymbols?: string;
75
+ }
76
+
77
+ /** A codec that regroups the bit stream, as specified by RFC 4648. */
78
+ export interface BlockSpec extends CommonSpec {
79
+ readonly algorithm: 'block';
80
+ /**
81
+ * Character used to pad the final block to a whole number of characters,
82
+ * or `false` to emit no padding. Defaults to `'='`.
83
+ */
84
+ readonly pad?: string | false;
85
+ }
86
+
87
+ /** A codec that converts each 4-byte block to 5 characters. */
88
+ export interface Block85Spec extends CommonSpec {
89
+ readonly algorithm: 'block85';
90
+ /** Write a block of four zero bytes as `'z'`. Defaults to `false`. */
91
+ readonly zeroShortcut?: boolean;
92
+ /** Write a block of four spaces as `'y'`. Defaults to `false`. */
93
+ readonly spaceShortcut?: boolean;
94
+ /** Wrap the output in `'<~'` and `'~>'`. Defaults to `false`. */
95
+ readonly delimiters?: boolean;
96
+ /**
97
+ * Require the input length to be a multiple of 4 bytes, as Z85 does.
98
+ * Defaults to `false`, which encodes a trailing partial block as
99
+ * `length + 1` characters.
100
+ */
101
+ readonly requireFullBlocks?: boolean;
102
+ }
103
+
104
+ /**
105
+ * A complete codec definition: an alphabet plus the algorithm that uses it.
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * import { encode, type CodecSpec } from 'bufferbase';
110
+ *
111
+ * const binary: CodecSpec = { alphabet: '01', algorithm: 'radix' };
112
+ * encode(Uint8Array.of(5), binary); // '101'
113
+ * ```
114
+ */
115
+ export type CodecSpec = RadixSpec | BlockSpec | Block85Spec;
116
+
117
+ /**
118
+ * Options for decode operations.
119
+ */
120
+ export interface DecodeOptions {
121
+ /**
122
+ * Required byte length of the result.
123
+ *
124
+ * The decoded buffer must be exactly this long, otherwise `BufferSizeError`
125
+ * is thrown. Because leading zeros carry no value in a radix encoding, a
126
+ * `'radix'` codec pads a shorter result with leading zero bytes to reach it;
127
+ * `'block'` and `'block85'` codecs derive the length from the input and can
128
+ * only check it.
129
+ */
130
+ size?: number;
131
+ }
132
+
133
+ /**
134
+ * Codec interface for encoding/decoding operations.
135
+ *
136
+ * `validate(input)` returns `true` exactly when `decode(input)` succeeds.
137
+ */
138
+ export interface ICodec {
139
+ /** Character set used for encoding. */
140
+ readonly chars: string;
141
+
142
+ /** The algorithm this codec applies to `chars`. */
143
+ readonly algorithm: Algorithm;
144
+
145
+ /** Encodes a buffer to a base-encoded string. */
146
+ encode(buffer: BufferLike): string;
147
+
148
+ /** Decodes a base-encoded string to a buffer. */
149
+ decode(encoded: string, options?: DecodeOptions): Buffer;
150
+
151
+ /** Reports whether `decode` would succeed for this input. */
152
+ validate(input: string): boolean;
153
+
154
+ /** Decodes with this codec, then re-encodes with `target`. */
155
+ convertTo(target: ICodec, input: string): string;
156
+ }
@@ -1,60 +0,0 @@
1
- /// <reference types="node" />
2
- /**
3
- * A collection of common bases.
4
- */
5
- export declare const Chars: {
6
- Decimal: string;
7
- Base16: string;
8
- Base32: string;
9
- Base32Crockford: string;
10
- Base36: string;
11
- Base52: string;
12
- Base58: string;
13
- Base64_STD: string;
14
- Base64_URL_SAFE: string;
15
- Base64_XML_NMTOKEN: string;
16
- Base64_XML_NAME: string;
17
- Ascii85: string;
18
- Base85: string;
19
- Z85: string;
20
- };
21
- export declare class InvalidCharacterError extends Error {
22
- message: string;
23
- }
24
- export declare class BufferSizeError extends Error {
25
- message: string;
26
- }
27
- /**
28
- * Encodes and decodes buffers to and from a base.
29
- */
30
- export declare class BufferEncoder {
31
- private baseChars;
32
- constructor(baseChars: string);
33
- /**
34
- * Encodes a buffer to a string.
35
- */
36
- encode(buffer: Buffer): string;
37
- /**
38
- * Decodes a string to a buffer.
39
- */
40
- decode(encoded: string, bufferSize?: number): Buffer;
41
- }
42
- /**
43
- * Creates a converter function that can convert between two bases.
44
- */
45
- export declare class Converter {
46
- decoder: BufferEncoder;
47
- encoder: BufferEncoder;
48
- constructor(inputBase: string, outputBase: string);
49
- /**
50
- * Converts a string from the input base to the output base.
51
- */
52
- convert(input: string): string;
53
- }
54
- export declare class Validator {
55
- decorder: BufferEncoder;
56
- constructor(inputBase: string);
57
- validate(input: string): boolean;
58
- }
59
- export declare const validate: (input: string, base: string) => boolean;
60
- //# sourceMappingURL=bufferbase.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bufferbase.d.mts","sourceRoot":"","sources":["../../src/bufferbase.mts"],"names":[],"mappings":";AAAA;;GAEG;AACH,eAAO,MAAM,KAAK;;;;;;;;;;;;;;;CAqBjB,CAAC;AAEF,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,OAAO,SAA6B;CACrC;AAED,qBAAa,eAAgB,SAAQ,KAAK;IACxC,OAAO,SAAwC;CAChD;AAED;;GAEG;AACH,qBAAa,aAAa;IACZ,OAAO,CAAC,SAAS;gBAAT,SAAS,EAAE,MAAM;IAErC;;OAEG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IA2B9B;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM;CAgCrD;AAED;;GAEG;AACH,qBAAa,SAAS;IACpB,OAAO,EAAE,aAAa,CAAC;IACvB,OAAO,EAAE,aAAa,CAAC;gBAEX,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;IAKjD;;OAEG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAG/B;AAED,qBAAa,SAAS;IACpB,QAAQ,EAAE,aAAa,CAAC;gBAEZ,SAAS,EAAE,MAAM;IAI7B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;CAWjC;AAED,eAAO,MAAM,QAAQ,UAAW,MAAM,QAAQ,MAAM,KAAG,OAEtD,CAAC"}