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
@@ -0,0 +1,177 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { BaseCodec, toBuffer } from './codec-base.js';
3
+ import { BufferSizeError, CheckSymbolError, InvalidCharacterError, InvalidCodecError, InvalidLengthError, } from './errors.js';
4
+ /**
5
+ * Reads the buffer as one big integer and rewrites it in base
6
+ * `alphabet.length`.
7
+ *
8
+ * This is the Base58 family of encodings. It accepts any alphabet and any
9
+ * input length, but it is not RFC 4648: the output length is not a fixed
10
+ * function of the input length, so it cannot be streamed, and encoding is
11
+ * O(n²) in the input size.
12
+ *
13
+ * Leading zero bytes carry no value in an integer, so they are recorded
14
+ * separately as leading `alphabet[0]` characters.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { RadixCodec } from 'bufferbase';
19
+ *
20
+ * const binary = new RadixCodec({ alphabet: '01', algorithm: 'radix' });
21
+ * binary.encode(Uint8Array.of(5)); // '101'
22
+ * ```
23
+ */
24
+ export class RadixCodec extends BaseCodec {
25
+ constructor(spec) {
26
+ super(spec);
27
+ this.algorithm = 'radix';
28
+ const extra = spec.checkSymbols;
29
+ if (extra === undefined || extra === '') {
30
+ this.checkChars = null;
31
+ this.checkValues = new Map();
32
+ return;
33
+ }
34
+ const combined = spec.alphabet + extra;
35
+ if (new Set(combined).size !== combined.length) {
36
+ throw new InvalidCodecError('checkSymbols repeat a character of the alphabet');
37
+ }
38
+ // The symbol is matched by value, so that a case-insensitive codec accepts
39
+ // it in either case just as it does the rest of the string.
40
+ const values = new Map();
41
+ for (let i = 0; i < combined.length; i++) {
42
+ const char = combined[i];
43
+ values.set(char, i);
44
+ if (spec.caseInsensitive === true) {
45
+ values.set(char.toLowerCase(), i);
46
+ values.set(char.toUpperCase(), i);
47
+ }
48
+ }
49
+ this.checkChars = combined;
50
+ this.checkValues = values;
51
+ }
52
+ encode(input) {
53
+ const buffer = toBuffer(input);
54
+ const base = this.chars.length;
55
+ const digits = [];
56
+ for (const byte of buffer) {
57
+ let carry = byte;
58
+ for (let i = 0; i < digits.length; i++) {
59
+ carry += digits[i] * 256;
60
+ digits[i] = carry % base;
61
+ carry = Math.floor(carry / base);
62
+ }
63
+ while (carry > 0) {
64
+ digits.push(carry % base);
65
+ carry = Math.floor(carry / base);
66
+ }
67
+ }
68
+ for (let i = 0; i < buffer.length && buffer[i] === 0; i++) {
69
+ digits.push(0);
70
+ }
71
+ let out = '';
72
+ for (let i = digits.length - 1; i >= 0; i--) {
73
+ out += this.chars[digits[i]];
74
+ }
75
+ if (this.checkChars !== null) {
76
+ out += this.checkChars[this.remainderOf(buffer, this.checkChars.length)];
77
+ }
78
+ return out;
79
+ }
80
+ decode(encoded, options) {
81
+ const bytes = this.read(this.strip(encoded));
82
+ const size = options?.size;
83
+ if (size === undefined) {
84
+ return Buffer.from(bytes);
85
+ }
86
+ if (bytes.length > size) {
87
+ throw new BufferSizeError(bytes.length, size);
88
+ }
89
+ const padded = Buffer.alloc(size);
90
+ Buffer.from(bytes).copy(padded, size - bytes.length);
91
+ return padded;
92
+ }
93
+ validate(input) {
94
+ const stripped = this.strip(input);
95
+ if (this.checkChars === null) {
96
+ // Every string over the alphabet decodes, so the characters settle it.
97
+ for (let i = 0; i < stripped.length; i++) {
98
+ if (this.valueAt(stripped, i) < 0) {
99
+ return false;
100
+ }
101
+ }
102
+ return true;
103
+ }
104
+ try {
105
+ this.read(stripped);
106
+ return true;
107
+ }
108
+ catch {
109
+ return false;
110
+ }
111
+ }
112
+ /**
113
+ * Decodes the body, verifying the check symbol when the codec carries one.
114
+ *
115
+ * Everything `decode` can reject other than `DecodeOptions.size` is checked
116
+ * here, so `validate` and `decode` always agree.
117
+ */
118
+ read(input) {
119
+ let body = input;
120
+ if (this.checkChars !== null) {
121
+ if (body.length === 0) {
122
+ throw new InvalidLengthError(0, 'a checked encoding always carries a check symbol');
123
+ }
124
+ const found = body.slice(-1);
125
+ body = body.slice(0, -1);
126
+ const bytes = this.toBytes(body);
127
+ const expected = this.remainderOf(bytes, this.checkChars.length);
128
+ if (this.checkValues.get(found) !== expected) {
129
+ throw new CheckSymbolError(found, this.checkChars[expected]);
130
+ }
131
+ return bytes;
132
+ }
133
+ return this.toBytes(input);
134
+ }
135
+ /** Rewrites the digits of `body` as bytes, most significant first. */
136
+ toBytes(body) {
137
+ const base = this.chars.length;
138
+ const bytes = [];
139
+ let leadingZeros = 0;
140
+ let stillLeading = true;
141
+ for (let at = 0; at < body.length; at++) {
142
+ const value = this.valueAt(body, at);
143
+ if (value < 0) {
144
+ throw new InvalidCharacterError(body[at]);
145
+ }
146
+ if (stillLeading && value === 0) {
147
+ leadingZeros++;
148
+ continue;
149
+ }
150
+ stillLeading = false;
151
+ let carry = value;
152
+ for (let i = 0; i < bytes.length; i++) {
153
+ carry += bytes[i] * base;
154
+ bytes[i] = carry % 256;
155
+ carry = Math.floor(carry / 256);
156
+ }
157
+ while (carry > 0) {
158
+ bytes.push(carry % 256);
159
+ carry = Math.floor(carry / 256);
160
+ }
161
+ }
162
+ for (let i = 0; i < leadingZeros; i++) {
163
+ bytes.push(0);
164
+ }
165
+ bytes.reverse();
166
+ return bytes;
167
+ }
168
+ /** The value the bytes denote, modulo `divisor`. */
169
+ remainderOf(bytes, divisor) {
170
+ let remainder = 0;
171
+ for (const byte of bytes) {
172
+ remainder = (remainder * 256 + byte) % divisor;
173
+ }
174
+ return remainder;
175
+ }
176
+ }
177
+ //# sourceMappingURL=codec-radix.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec-radix.js","sourceRoot":"","sources":["../../src/codec-radix.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,UAAW,SAAQ,SAAS;IASvC,YAAY,IAAe;QACzB,KAAK,CAAC,IAAI,CAAC,CAAC;QATL,cAAS,GAAG,OAAgB,CAAC;QAWpC,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC;QAChC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;YACxC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YACvB,IAAI,C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"}
@@ -1,124 +1,38 @@
1
- import { Buffer } from 'node:buffer';
2
- import type { ICodec, DecodeOptions, BufferLike } from './types.js';
1
+ import type { CodecSpec, ICodec } from './types.js';
2
+ import { RadixCodec } from './codec-radix.js';
3
3
  /**
4
- * Codec for encoding and decoding buffers to/from a specific base.
4
+ * Builds the codec described by a spec.
5
5
  *
6
- * Use pre-defined codecs from `Codecs` for common encodings,
7
- * or create custom codecs with `createCodec()`.
6
+ * A bare string is read as a custom alphabet for a radix conversion, which is
7
+ * the only algorithm that works with an alphabet of any length.
8
8
  *
9
9
  * @example
10
10
  * ```typescript
11
- * import { Codec, Codecs, createCodec } from 'bufferbase';
12
- *
13
- * // Use pre-defined codec
14
- * const encoded = Codecs.base58.encode(Buffer.from('Hello'));
11
+ * import { createCodec } from 'bufferbase';
15
12
  *
16
- * // Create custom codec
17
13
  * const binary = createCodec('01');
18
- * const bits = binary.encode(Buffer.from([5])); // '101'
14
+ * binary.encode(Uint8Array.of(5)); // '101'
15
+ *
16
+ * const paddedHex = createCodec({ alphabet: '0123456789abcdef', algorithm: 'block' });
17
+ * paddedHex.encode(Uint8Array.of(0, 1)); // '0001'
19
18
  * ```
20
19
  */
21
- export declare class Codec implements ICodec {
22
- /** Character set used for encoding. */
23
- readonly chars: string;
24
- /** @internal */
25
- private readonly charToIndex;
26
- /**
27
- * Creates a new codec with the specified character set.
28
- * @param chars - The character set to use for encoding/decoding
29
- */
30
- constructor(chars: string);
31
- /**
32
- * Encodes a buffer to a base-encoded string.
33
- *
34
- * Leading zero bytes in the buffer are preserved as the first character
35
- * of the encoding alphabet (e.g., '1' for Base58).
36
- *
37
- * @param input - The buffer or Uint8Array to encode
38
- * @returns Base-encoded string
39
- *
40
- * @example
41
- * ```typescript
42
- * const codec = Codecs.base58;
43
- * codec.encode(Buffer.from('Hello')); // '9Ajdvzr'
44
- * codec.encode(new Uint8Array([0, 0, 1])); // '112' (preserves leading zeros)
45
- * ```
46
- */
47
- encode(input: BufferLike): string;
48
- /**
49
- * Decodes a base-encoded string to a buffer.
50
- *
51
- * Leading characters that represent zero (first char of alphabet)
52
- * are converted to leading zero bytes in the result.
53
- *
54
- * @param encoded - The string to decode
55
- * @param options - Decode options
56
- * @returns Decoded buffer
57
- * @throws {InvalidCharacterError} If the string contains invalid characters
58
- * @throws {BufferSizeError} If the result exceeds the specified size
59
- *
60
- * @example
61
- * ```typescript
62
- * const codec = Codecs.base58;
63
- * codec.decode('9Ajdvzr'); // Buffer('Hello')
64
- * codec.decode('9Ajdvzr', { size: 32 }); // Pads to 32 bytes
65
- * ```
66
- */
67
- decode(encoded: string, options?: DecodeOptions): Buffer;
68
- /**
69
- * Validates if a string contains only valid characters for this base.
70
- *
71
- * This is a fast check that doesn't perform full decoding.
72
- *
73
- * @param input - The string to validate
74
- * @returns `true` if all characters are valid, `false` otherwise
75
- *
76
- * @example
77
- * ```typescript
78
- * Codecs.base58.validate('9Ajdvzr'); // true
79
- * Codecs.base58.validate('0invalid'); // false ('0' not in Base58)
80
- * ```
81
- */
82
- validate(input: string): boolean;
83
- /**
84
- * Converts a string from this base to another base.
85
- *
86
- * Internally decodes to a buffer, then encodes with the target codec.
87
- *
88
- * @param target - Target codec to convert to
89
- * @param input - The string to convert
90
- * @returns String in the target base encoding
91
- * @throws {InvalidCharacterError} If the input contains invalid characters
92
- *
93
- * @example
94
- * ```typescript
95
- * const base58str = '9Ajdvzr';
96
- * const base64str = Codecs.base58.convertTo(Codecs.base64url, base58str);
97
- * ```
98
- */
99
- convertTo(target: ICodec, input: string): string;
100
- }
20
+ export declare function createCodec(spec: CodecSpec | string): ICodec;
101
21
  /**
102
- * Creates a new codec with the specified character set.
103
- *
104
- * Use this to create codecs for custom base encodings.
105
- * The base is determined by the length of the character set.
106
- *
107
- * @param chars - The character set to use for encoding/decoding
108
- * @returns A new Codec instance
109
- *
110
- * @example
111
- * ```typescript
112
- * import { createCodec } from 'bufferbase';
22
+ * Returns the codec for a spec, building it once per spec object.
113
23
  *
114
- * // Binary codec (base 2)
115
- * const binary = createCodec('01');
116
- * binary.encode(Buffer.from([5])); // '101'
117
- *
118
- * // Octal codec (base 8)
119
- * const octal = createCodec('01234567');
120
- * octal.encode(Buffer.from([255])); // '377'
121
- * ```
24
+ * Named bases always resolve to the same spec object, so they are built once
25
+ * for the lifetime of the process.
122
26
  */
123
- export declare function createCodec(chars: string): Codec;
27
+ export declare function getCodec(spec: CodecSpec): ICodec;
28
+ export { RadixCodec } from './codec-radix.js';
29
+ export { BlockCodec } from './codec-block.js';
30
+ export { Block85Codec } from './codec-block85.js';
31
+ /**
32
+ * @deprecated Use `createCodec({ alphabet, algorithm: 'radix' })`, or
33
+ * `createCodec(alphabet)` for the same thing.
34
+ */
35
+ export declare class Codec extends RadixCodec {
36
+ constructor(chars: string);
37
+ }
124
38
  //# sourceMappingURL=codec.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAcpE;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,KAAM,YAAW,MAAM;IAClC,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,gBAAgB;IAChB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsB;IAElD;;;OAGG;gBACS,KAAK,EAAE,MAAM;IAQzB;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;IA6CjC;;;;;;;;;;;;;;;;;;OAkBG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM;IAgExD;;;;;;;;;;;;;OAaG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAShC;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;CAIjD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,CAEhD"}
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAI9C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,GAAG,MAAM,CAY5D;AAID;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAQhD;AAED,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;GAGG;AACH,qBAAa,KAAM,SAAQ,UAAU;gBACvB,KAAK,EAAE,MAAM;CAG1B"}
package/dist/esm/codec.js CHANGED
@@ -1,242 +1,60 @@
1
- import { Buffer } from 'node:buffer';
2
- import { InvalidCharacterError, BufferSizeError } from './errors.js';
1
+ import { RadixCodec } from './codec-radix.js';
2
+ import { BlockCodec } from './codec-block.js';
3
+ import { Block85Codec } from './codec-block85.js';
3
4
  /**
4
- * Converts a BufferLike (Buffer or Uint8Array) to a Buffer.
5
- * @internal
6
- */
7
- function toBuffer(input) {
8
- if (Buffer.isBuffer(input)) {
9
- return input;
10
- }
11
- return Buffer.from(input);
12
- }
13
- /**
14
- * Codec for encoding and decoding buffers to/from a specific base.
5
+ * Builds the codec described by a spec.
15
6
  *
16
- * Use pre-defined codecs from `Codecs` for common encodings,
17
- * or create custom codecs with `createCodec()`.
7
+ * A bare string is read as a custom alphabet for a radix conversion, which is
8
+ * the only algorithm that works with an alphabet of any length.
18
9
  *
19
10
  * @example
20
11
  * ```typescript
21
- * import { Codec, Codecs, createCodec } from 'bufferbase';
22
- *
23
- * // Use pre-defined codec
24
- * const encoded = Codecs.base58.encode(Buffer.from('Hello'));
12
+ * import { createCodec } from 'bufferbase';
25
13
  *
26
- * // Create custom codec
27
14
  * const binary = createCodec('01');
28
- * const bits = binary.encode(Buffer.from([5])); // '101'
15
+ * binary.encode(Uint8Array.of(5)); // '101'
16
+ *
17
+ * const paddedHex = createCodec({ alphabet: '0123456789abcdef', algorithm: 'block' });
18
+ * paddedHex.encode(Uint8Array.of(0, 1)); // '0001'
29
19
  * ```
30
20
  */
31
- export class Codec {
32
- /**
33
- * Creates a new codec with the specified character set.
34
- * @param chars - The character set to use for encoding/decoding
35
- */
36
- constructor(chars) {
37
- this.chars = chars;
38
- this.charToIndex = new Map();
39
- for (let i = 0; i < chars.length; i++) {
40
- this.charToIndex.set(chars[i], i);
41
- }
42
- }
43
- /**
44
- * Encodes a buffer to a base-encoded string.
45
- *
46
- * Leading zero bytes in the buffer are preserved as the first character
47
- * of the encoding alphabet (e.g., '1' for Base58).
48
- *
49
- * @param input - The buffer or Uint8Array to encode
50
- * @returns Base-encoded string
51
- *
52
- * @example
53
- * ```typescript
54
- * const codec = Codecs.base58;
55
- * codec.encode(Buffer.from('Hello')); // '9Ajdvzr'
56
- * codec.encode(new Uint8Array([0, 0, 1])); // '112' (preserves leading zeros)
57
- * ```
58
- */
59
- encode(input) {
60
- const buffer = toBuffer(input);
61
- if (buffer.length === 0) {
62
- return '';
63
- }
64
- const base = this.chars.length;
65
- const result = [];
66
- // Count leading zeros
67
- let leadingZeros = 0;
68
- for (const byte of buffer) {
69
- if (byte === 0) {
70
- leadingZeros++;
71
- }
72
- else {
73
- break;
74
- }
75
- }
76
- // Convert to base
77
- for (const byte of buffer) {
78
- let carry = byte;
79
- for (let j = 0; j < result.length; j++) {
80
- carry += result[j] * 256;
81
- result[j] = carry % base;
82
- carry = Math.floor(carry / base);
83
- }
84
- while (carry > 0) {
85
- result.push(carry % base);
86
- carry = Math.floor(carry / base);
87
- }
88
- }
89
- // Add leading zeros
90
- for (let i = 0; i < leadingZeros; i++) {
91
- result.push(0);
92
- }
93
- // Reverse and convert to string
94
- return result
95
- .reverse()
96
- .map((index) => this.chars[index])
97
- .join('');
98
- }
99
- /**
100
- * Decodes a base-encoded string to a buffer.
101
- *
102
- * Leading characters that represent zero (first char of alphabet)
103
- * are converted to leading zero bytes in the result.
104
- *
105
- * @param encoded - The string to decode
106
- * @param options - Decode options
107
- * @returns Decoded buffer
108
- * @throws {InvalidCharacterError} If the string contains invalid characters
109
- * @throws {BufferSizeError} If the result exceeds the specified size
110
- *
111
- * @example
112
- * ```typescript
113
- * const codec = Codecs.base58;
114
- * codec.decode('9Ajdvzr'); // Buffer('Hello')
115
- * codec.decode('9Ajdvzr', { size: 32 }); // Pads to 32 bytes
116
- * ```
117
- */
118
- decode(encoded, options) {
119
- if (encoded.length === 0) {
120
- if (options?.size !== undefined && options.size > 0) {
121
- return Buffer.alloc(options.size);
122
- }
123
- return Buffer.alloc(0);
124
- }
125
- const base = this.chars.length;
126
- const zeroChar = this.chars[0];
127
- // Count leading zeros in encoded string
128
- let leadingZeros = 0;
129
- for (const char of encoded) {
130
- if (char === zeroChar) {
131
- leadingZeros++;
132
- }
133
- else {
134
- break;
135
- }
136
- }
137
- // Convert from base
138
- const result = [];
139
- for (const char of encoded) {
140
- const value = this.charToIndex.get(char);
141
- if (value === undefined) {
142
- throw new InvalidCharacterError(char);
143
- }
144
- let carry = value;
145
- for (let i = 0; i < result.length; i++) {
146
- carry += result[i] * base;
147
- result[i] = carry % 256;
148
- carry = Math.floor(carry / 256);
149
- }
150
- while (carry > 0) {
151
- result.push(carry % 256);
152
- carry = Math.floor(carry / 256);
153
- }
154
- }
155
- // Add leading zeros
156
- for (let i = 0; i < leadingZeros; i++) {
157
- result.push(0);
158
- }
159
- result.reverse();
160
- // Handle size option
161
- const size = options?.size;
162
- if (size !== undefined) {
163
- if (result.length > size) {
164
- throw new BufferSizeError(result.length, size);
165
- }
166
- if (result.length < size) {
167
- // Pad with leading zeros
168
- const padded = new Array(size - result.length).fill(0).concat(result);
169
- return Buffer.from(padded);
170
- }
171
- }
172
- return Buffer.from(result);
173
- }
174
- /**
175
- * Validates if a string contains only valid characters for this base.
176
- *
177
- * This is a fast check that doesn't perform full decoding.
178
- *
179
- * @param input - The string to validate
180
- * @returns `true` if all characters are valid, `false` otherwise
181
- *
182
- * @example
183
- * ```typescript
184
- * Codecs.base58.validate('9Ajdvzr'); // true
185
- * Codecs.base58.validate('0invalid'); // false ('0' not in Base58)
186
- * ```
187
- */
188
- validate(input) {
189
- for (const char of input) {
190
- if (!this.charToIndex.has(char)) {
191
- return false;
192
- }
193
- }
194
- return true;
195
- }
196
- /**
197
- * Converts a string from this base to another base.
198
- *
199
- * Internally decodes to a buffer, then encodes with the target codec.
200
- *
201
- * @param target - Target codec to convert to
202
- * @param input - The string to convert
203
- * @returns String in the target base encoding
204
- * @throws {InvalidCharacterError} If the input contains invalid characters
205
- *
206
- * @example
207
- * ```typescript
208
- * const base58str = '9Ajdvzr';
209
- * const base64str = Codecs.base58.convertTo(Codecs.base64url, base58str);
210
- * ```
211
- */
212
- convertTo(target, input) {
213
- const buffer = this.decode(input);
214
- return target.encode(buffer);
21
+ export function createCodec(spec) {
22
+ const resolved = typeof spec === 'string' ? { alphabet: spec, algorithm: 'radix' } : spec;
23
+ switch (resolved.algorithm) {
24
+ case 'radix':
25
+ return new RadixCodec(resolved);
26
+ case 'block':
27
+ return new BlockCodec(resolved);
28
+ case 'block85':
29
+ return new Block85Codec(resolved);
215
30
  }
216
31
  }
32
+ const cache = new WeakMap();
217
33
  /**
218
- * Creates a new codec with the specified character set.
219
- *
220
- * Use this to create codecs for custom base encodings.
221
- * The base is determined by the length of the character set.
222
- *
223
- * @param chars - The character set to use for encoding/decoding
224
- * @returns A new Codec instance
34
+ * Returns the codec for a spec, building it once per spec object.
225
35
  *
226
- * @example
227
- * ```typescript
228
- * import { createCodec } from 'bufferbase';
229
- *
230
- * // Binary codec (base 2)
231
- * const binary = createCodec('01');
232
- * binary.encode(Buffer.from([5])); // '101'
233
- *
234
- * // Octal codec (base 8)
235
- * const octal = createCodec('01234567');
236
- * octal.encode(Buffer.from([255])); // '377'
237
- * ```
36
+ * Named bases always resolve to the same spec object, so they are built once
37
+ * for the lifetime of the process.
238
38
  */
239
- export function createCodec(chars) {
240
- return new Codec(chars);
39
+ export function getCodec(spec) {
40
+ const cached = cache.get(spec);
41
+ if (cached !== undefined) {
42
+ return cached;
43
+ }
44
+ const codec = createCodec(spec);
45
+ cache.set(spec, codec);
46
+ return codec;
47
+ }
48
+ export { RadixCodec } from './codec-radix.js';
49
+ export { BlockCodec } from './codec-block.js';
50
+ export { Block85Codec } from './codec-block85.js';
51
+ /**
52
+ * @deprecated Use `createCodec({ alphabet, algorithm: 'radix' })`, or
53
+ * `createCodec(alphabet)` for the same thing.
54
+ */
55
+ export class Codec extends RadixCodec {
56
+ constructor(chars) {
57
+ super({ alphabet: chars, algorithm: 'radix' });
58
+ }
241
59
  }
242
60
  //# sourceMappingURL=codec.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAErE;;;GAGG;AACH,SAAS,QAAQ,CAAC,KAAiB;IACjC,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;;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