bufferbase 1.2.3 → 2.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 (101) hide show
  1. package/README.md +99 -18
  2. package/dist/cjs/bases.d.ts +82 -0
  3. package/dist/cjs/bases.d.ts.map +1 -0
  4. package/dist/cjs/bases.js +121 -0
  5. package/dist/cjs/bases.js.map +1 -0
  6. package/dist/cjs/bufferbase.d.ts +43 -0
  7. package/dist/cjs/bufferbase.d.ts.map +1 -0
  8. package/dist/cjs/bufferbase.js +66 -0
  9. package/dist/cjs/bufferbase.js.map +1 -0
  10. package/dist/cjs/codec.d.ts +124 -0
  11. package/dist/cjs/codec.d.ts.map +1 -0
  12. package/dist/cjs/codec.js +247 -0
  13. package/dist/cjs/codec.js.map +1 -0
  14. package/dist/cjs/codecs.d.ts +71 -0
  15. package/dist/cjs/codecs.d.ts.map +1 -0
  16. package/dist/cjs/codecs.js +92 -0
  17. package/dist/cjs/codecs.js.map +1 -0
  18. package/dist/cjs/errors.d.ts +76 -0
  19. package/dist/cjs/errors.d.ts.map +1 -0
  20. package/dist/cjs/errors.js +92 -0
  21. package/dist/cjs/errors.js.map +1 -0
  22. package/dist/{example.d.ts.map → cjs/example.d.ts.map} +1 -1
  23. package/dist/cjs/example.js +32 -0
  24. package/dist/cjs/example.js.map +1 -0
  25. package/dist/cjs/functions.d.ts +62 -0
  26. package/dist/cjs/functions.d.ts.map +1 -0
  27. package/dist/cjs/functions.js +93 -0
  28. package/dist/cjs/functions.js.map +1 -0
  29. package/dist/cjs/index.d.ts +9 -0
  30. package/dist/cjs/index.d.ts.map +1 -0
  31. package/dist/cjs/index.js +32 -0
  32. package/dist/cjs/index.js.map +1 -0
  33. package/dist/cjs/package.json +50 -0
  34. package/dist/cjs/types.d.ts +107 -0
  35. package/dist/cjs/types.d.ts.map +1 -0
  36. package/dist/cjs/types.js +3 -0
  37. package/dist/cjs/types.js.map +1 -0
  38. package/dist/esm/bases.d.ts +82 -0
  39. package/dist/esm/bases.d.ts.map +1 -0
  40. package/dist/esm/bases.js +116 -0
  41. package/dist/esm/bases.js.map +1 -0
  42. package/dist/esm/bufferbase.d.ts +43 -0
  43. package/dist/esm/bufferbase.d.ts.map +1 -0
  44. package/dist/esm/bufferbase.js +59 -0
  45. package/dist/esm/bufferbase.js.map +1 -0
  46. package/dist/esm/codec.d.ts +124 -0
  47. package/dist/esm/codec.d.ts.map +1 -0
  48. package/dist/esm/codec.js +242 -0
  49. package/dist/esm/codec.js.map +1 -0
  50. package/dist/esm/codecs.d.ts +71 -0
  51. package/dist/esm/codecs.d.ts.map +1 -0
  52. package/dist/esm/codecs.js +89 -0
  53. package/dist/esm/codecs.js.map +1 -0
  54. package/dist/esm/errors.d.ts +76 -0
  55. package/dist/esm/errors.d.ts.map +1 -0
  56. package/dist/esm/errors.js +86 -0
  57. package/dist/esm/errors.js.map +1 -0
  58. package/dist/esm/example.d.ts +2 -0
  59. package/dist/esm/example.d.ts.map +1 -0
  60. package/dist/esm/example.js +30 -0
  61. package/dist/esm/example.js.map +1 -0
  62. package/dist/esm/functions.d.ts +62 -0
  63. package/dist/esm/functions.d.ts.map +1 -0
  64. package/dist/esm/functions.js +87 -0
  65. package/dist/esm/functions.js.map +1 -0
  66. package/dist/esm/index.d.ts +9 -0
  67. package/dist/esm/index.d.ts.map +1 -0
  68. package/dist/esm/index.js +13 -0
  69. package/dist/esm/index.js.map +1 -0
  70. package/dist/esm/package.json +50 -0
  71. package/dist/esm/types.d.ts +107 -0
  72. package/dist/esm/types.d.ts.map +1 -0
  73. package/dist/esm/types.js +2 -0
  74. package/dist/esm/types.js.map +1 -0
  75. package/package.json +26 -11
  76. package/src/bases.ts +120 -0
  77. package/src/bufferbase.test.ts +44 -27
  78. package/src/bufferbase.ts +31 -106
  79. package/src/codec.test.ts +320 -0
  80. package/src/codec.ts +266 -0
  81. package/src/codecs.ts +118 -0
  82. package/src/errors.ts +87 -0
  83. package/src/example.ts +20 -30
  84. package/src/functions.ts +93 -0
  85. package/src/index.ts +21 -8
  86. package/src/types.ts +129 -0
  87. package/dist/bufferbase.d.ts +0 -60
  88. package/dist/bufferbase.d.ts.map +0 -1
  89. package/dist/bufferbase.js +0 -141
  90. package/dist/bufferbase.js.map +0 -1
  91. package/dist/example.js +0 -37
  92. package/dist/example.js.map +0 -1
  93. package/dist/index.d.ts +0 -2
  94. package/dist/index.d.ts.map +0 -1
  95. package/dist/index.js +0 -2
  96. package/dist/index.js.map +0 -1
  97. package/dist/sample.d.ts +0 -2
  98. package/dist/sample.d.ts.map +0 -1
  99. package/dist/sample.js +0 -37
  100. package/dist/sample.js.map +0 -1
  101. /package/dist/{example.d.ts → cjs/example.d.ts} +0 -0
@@ -0,0 +1,59 @@
1
+ /**
2
+ * @deprecated Use the new API instead:
3
+ * - `encode(buffer, 'base58')` or `base58.encode(buffer)`
4
+ * - `decode(encoded, 'base58')` or `base58.decode(encoded)`
5
+ * - `convert(input, 'base58', 'base64')` or `base58.convertTo(base64, input)`
6
+ * - `validate(input, 'base58')` or `base58.validate(input)`
7
+ */
8
+ import { Buffer } from 'node:buffer';
9
+ import { Codec } from './codec.js';
10
+ import { InvalidCharacterError, BufferSizeError } from './errors.js';
11
+ // Re-export Chars for backward compatibility
12
+ export { Chars } from './bases.js';
13
+ // Re-export errors
14
+ export { InvalidCharacterError, BufferSizeError };
15
+ /**
16
+ * @deprecated Use `Codec` or `createCodec()` instead.
17
+ */
18
+ export class BufferEncoder {
19
+ constructor(baseChars) {
20
+ this.baseChars = baseChars;
21
+ this.codec = new Codec(baseChars);
22
+ }
23
+ encode(buffer) {
24
+ return this.codec.encode(buffer);
25
+ }
26
+ decode(encoded, bufferSize) {
27
+ return this.codec.decode(encoded, bufferSize !== undefined ? { size: bufferSize } : undefined);
28
+ }
29
+ }
30
+ /**
31
+ * @deprecated Use `convert()` function or `codec.convertTo()` instead.
32
+ */
33
+ export class Converter {
34
+ constructor(inputBase, outputBase) {
35
+ this.decoder = new BufferEncoder(inputBase);
36
+ this.encoder = new BufferEncoder(outputBase);
37
+ }
38
+ convert(input) {
39
+ return this.encoder.encode(this.decoder.decode(input));
40
+ }
41
+ }
42
+ /**
43
+ * @deprecated Use `validate()` function or `codec.validate()` instead.
44
+ */
45
+ export class Validator {
46
+ constructor(inputBase) {
47
+ this.codec = new Codec(inputBase);
48
+ }
49
+ validate(input) {
50
+ return this.codec.validate(input);
51
+ }
52
+ }
53
+ /**
54
+ * @deprecated Use `validate()` from the main module instead.
55
+ */
56
+ export const validate = (input, base) => {
57
+ return new Validator(base).validate(input);
58
+ };
59
+ //# sourceMappingURL=bufferbase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bufferbase.js","sourceRoot":"","sources":["../../src/bufferbase.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAErE,6CAA6C;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,mBAAmB;AACnB,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,CAAC;AAElD;;GAEG;AACH,MAAM,OAAO,aAAa;IAGxB,YAAoB,SAAiB;QAAjB,cAAS,GAAT,SAAS,CAAQ;QACnC,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,MAAM,CAAC,MAAc;QACnB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,MAAM,CAAC,OAAe,EAAE,UAAmB;QACzC,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IACjG,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,SAAS;IAIpB,YAAY,SAAiB,EAAE,UAAkB;QAC/C,IAAI,CAAC,OAAO,GAAG,IAAI,aAAa,CAAC,SAAS,CAAC,CAAC;QAC5C,IAAI,CAAC,OAAO,GAAG,IAAI,aAAa,CAAC,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,CAAC,KAAa;QACnB,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACzD,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,SAAS;IAGpB,YAAY,SAAiB;QAC3B,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,QAAQ,CAAC,KAAa;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAE,IAAY,EAAW,EAAE;IAC/D,OAAO,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC7C,CAAC,CAAC"}
@@ -0,0 +1,124 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import type { ICodec, DecodeOptions, BufferLike } from './types.js';
3
+ /**
4
+ * Codec for encoding and decoding buffers to/from a specific base.
5
+ *
6
+ * Use pre-defined codecs from `Codecs` for common encodings,
7
+ * or create custom codecs with `createCodec()`.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { Codec, Codecs, createCodec } from 'bufferbase';
12
+ *
13
+ * // Use pre-defined codec
14
+ * const encoded = Codecs.base58.encode(Buffer.from('Hello'));
15
+ *
16
+ * // Create custom codec
17
+ * const binary = createCodec('01');
18
+ * const bits = binary.encode(Buffer.from([5])); // '101'
19
+ * ```
20
+ */
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
+ }
101
+ /**
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';
113
+ *
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
+ * ```
122
+ */
123
+ export declare function createCodec(chars: string): Codec;
124
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,242 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { InvalidCharacterError, BufferSizeError } from './errors.js';
3
+ /**
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.
15
+ *
16
+ * Use pre-defined codecs from `Codecs` for common encodings,
17
+ * or create custom codecs with `createCodec()`.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * import { Codec, Codecs, createCodec } from 'bufferbase';
22
+ *
23
+ * // Use pre-defined codec
24
+ * const encoded = Codecs.base58.encode(Buffer.from('Hello'));
25
+ *
26
+ * // Create custom codec
27
+ * const binary = createCodec('01');
28
+ * const bits = binary.encode(Buffer.from([5])); // '101'
29
+ * ```
30
+ */
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);
215
+ }
216
+ }
217
+ /**
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
225
+ *
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
+ * ```
238
+ */
239
+ export function createCodec(chars) {
240
+ return new Codec(chars);
241
+ }
242
+ //# sourceMappingURL=codec.js.map
@@ -0,0 +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,KAA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{ Codec } from './codec.js';
2
+ /**
3
+ * Pre-defined codec instances for common base encodings.
4
+ *
5
+ * Use these for encoding/decoding without creating codec instances manually.
6
+ * Each codec provides `encode()`, `decode()`, `validate()`, and `convertTo()` methods.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { Codecs } from 'bufferbase';
11
+ *
12
+ * // Encode to Base58
13
+ * const encoded = Codecs.base58.encode(Buffer.from('Hello'));
14
+ *
15
+ * // Decode from Base58
16
+ * const decoded = Codecs.base58.decode(encoded);
17
+ *
18
+ * // Convert between bases
19
+ * const base64 = Codecs.base58.convertTo(Codecs.base64url, encoded);
20
+ *
21
+ * // Validate
22
+ * Codecs.base58.validate(encoded); // true
23
+ * ```
24
+ *
25
+ * Available codecs:
26
+ * - `decimal` - Base 10 (0-9)
27
+ * - `base16` / `hex` - Hexadecimal (0-9A-F)
28
+ * - `base32` - RFC 4648 Base32 (A-Z2-7)
29
+ * - `base32crockford` - Crockford's Base32
30
+ * - `base36` - Alphanumeric (0-9A-Z)
31
+ * - `base52` - Letters only (A-Za-z)
32
+ * - `base58` - Bitcoin alphabet
33
+ * - `base64` - Standard Base64
34
+ * - `base64url` - URL-safe Base64
35
+ * - `base64xml` - XML token Base64
36
+ * - `base64xmlname` - XML name Base64
37
+ * - `ascii85` - ASCII85 encoding
38
+ * - `base85` - Base85 encoding
39
+ * - `z85` - ZeroMQ Z85 encoding
40
+ */
41
+ export declare const Codecs: {
42
+ readonly decimal: Codec;
43
+ readonly base16: Codec;
44
+ readonly hex: Codec;
45
+ readonly base32: Codec;
46
+ readonly base32crockford: Codec;
47
+ readonly base36: Codec;
48
+ readonly base52: Codec;
49
+ readonly base58: Codec;
50
+ readonly base64: Codec;
51
+ readonly base64url: Codec;
52
+ readonly base64xml: Codec;
53
+ readonly base64xmlname: Codec;
54
+ readonly ascii85: Codec;
55
+ readonly base85: Codec;
56
+ readonly z85: Codec;
57
+ };
58
+ /**
59
+ * Type representing valid codec names in the `Codecs` object.
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * import { Codecs, CodecName } from 'bufferbase';
64
+ *
65
+ * function getCodec(name: CodecName) {
66
+ * return Codecs[name];
67
+ * }
68
+ * ```
69
+ */
70
+ export type CodecName = keyof typeof Codecs;
71
+ //# sourceMappingURL=codecs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codecs.d.ts","sourceRoot":"","sources":["../../src/codecs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAgDnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;CAgBT,CAAC;AAEX;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,OAAO,MAAM,CAAC"}
@@ -0,0 +1,89 @@
1
+ import { Codec } from './codec.js';
2
+ import { Chars } from './bases.js';
3
+ /** Decimal (base 10) codec: 0-9 */
4
+ const decimal = new Codec(Chars.Decimal);
5
+ /** Hexadecimal (base 16) codec: 0-9A-F */
6
+ const base16 = new Codec(Chars.Base16);
7
+ /** Alias for base16 */
8
+ const hex = base16;
9
+ /** RFC 4648 Base32 codec: A-Z2-7 */
10
+ const base32 = new Codec(Chars.Base32);
11
+ /** Crockford's Base32 codec: 0-9A-HJKMNP-TV-Z (no I, L, O, U) */
12
+ const base32crockford = new Codec(Chars.Base32Crockford);
13
+ /** Base36 codec: 0-9A-Z */
14
+ const base36 = new Codec(Chars.Base36);
15
+ /** Base52 codec: A-Za-z (letters only) */
16
+ const base52 = new Codec(Chars.Base52);
17
+ /** Bitcoin Base58 codec: 1-9A-HJ-NP-Za-km-z (no 0, O, I, l) */
18
+ const base58 = new Codec(Chars.Base58);
19
+ /** Standard Base64 codec: A-Za-z0-9+/ */
20
+ const base64 = new Codec(Chars.Base64);
21
+ /** URL-safe Base64 codec: A-Za-z0-9-_ */
22
+ const base64url = new Codec(Chars.Base64Url);
23
+ /** XML token Base64 codec: A-Za-z0-9._ */
24
+ const base64xml = new Codec(Chars.Base64Xml);
25
+ /** XML name Base64 codec: A-Za-z0-9_: */
26
+ const base64xmlname = new Codec(Chars.Base64XmlName);
27
+ /** ASCII85 codec */
28
+ const ascii85 = new Codec(Chars.Ascii85);
29
+ /** Base85 codec */
30
+ const base85 = new Codec(Chars.Base85);
31
+ /** ZeroMQ Z85 codec */
32
+ const z85 = new Codec(Chars.Z85);
33
+ /**
34
+ * Pre-defined codec instances for common base encodings.
35
+ *
36
+ * Use these for encoding/decoding without creating codec instances manually.
37
+ * Each codec provides `encode()`, `decode()`, `validate()`, and `convertTo()` methods.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * import { Codecs } from 'bufferbase';
42
+ *
43
+ * // Encode to Base58
44
+ * const encoded = Codecs.base58.encode(Buffer.from('Hello'));
45
+ *
46
+ * // Decode from Base58
47
+ * const decoded = Codecs.base58.decode(encoded);
48
+ *
49
+ * // Convert between bases
50
+ * const base64 = Codecs.base58.convertTo(Codecs.base64url, encoded);
51
+ *
52
+ * // Validate
53
+ * Codecs.base58.validate(encoded); // true
54
+ * ```
55
+ *
56
+ * Available codecs:
57
+ * - `decimal` - Base 10 (0-9)
58
+ * - `base16` / `hex` - Hexadecimal (0-9A-F)
59
+ * - `base32` - RFC 4648 Base32 (A-Z2-7)
60
+ * - `base32crockford` - Crockford's Base32
61
+ * - `base36` - Alphanumeric (0-9A-Z)
62
+ * - `base52` - Letters only (A-Za-z)
63
+ * - `base58` - Bitcoin alphabet
64
+ * - `base64` - Standard Base64
65
+ * - `base64url` - URL-safe Base64
66
+ * - `base64xml` - XML token Base64
67
+ * - `base64xmlname` - XML name Base64
68
+ * - `ascii85` - ASCII85 encoding
69
+ * - `base85` - Base85 encoding
70
+ * - `z85` - ZeroMQ Z85 encoding
71
+ */
72
+ export const Codecs = {
73
+ decimal,
74
+ base16,
75
+ hex,
76
+ base32,
77
+ base32crockford,
78
+ base36,
79
+ base52,
80
+ base58,
81
+ base64,
82
+ base64url,
83
+ base64xml,
84
+ base64xmlname,
85
+ ascii85,
86
+ base85,
87
+ z85,
88
+ };
89
+ //# sourceMappingURL=codecs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codecs.js","sourceRoot":"","sources":["../../src/codecs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,mCAAmC;AACnC,MAAM,OAAO,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AAEzC,0CAA0C;AAC1C,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAEvC,uBAAuB;AACvB,MAAM,GAAG,GAAG,MAAM,CAAC;AAEnB,oCAAoC;AACpC,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAEvC,iEAAiE;AACjE,MAAM,eAAe,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;AAEzD,2BAA2B;AAC3B,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAEvC,0CAA0C;AAC1C,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAEvC,+DAA+D;AAC/D,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAEvC,yCAAyC;AACzC,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAEvC,yCAAyC;AACzC,MAAM,SAAS,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAE7C,0CAA0C;AAC1C,MAAM,SAAS,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAE7C,yCAAyC;AACzC,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;AAErD,oBAAoB;AACpB,MAAM,OAAO,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AAEzC,mBAAmB;AACnB,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAEvC,uBAAuB;AACvB,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,OAAO;IACP,MAAM;IACN,GAAG;IACH,MAAM;IACN,eAAe;IACf,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,SAAS;IACT,SAAS;IACT,aAAa;IACb,OAAO;IACP,MAAM;IACN,GAAG;CACK,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Error thrown when an invalid character is encountered during decoding.
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * import { decode, InvalidCharacterError } from 'bufferbase';
7
+ *
8
+ * try {
9
+ * decode('invalid!', 'base58');
10
+ * } catch (e) {
11
+ * if (e instanceof InvalidCharacterError) {
12
+ * console.log(e.message); // "Invalid character found: '!'"
13
+ * }
14
+ * }
15
+ * ```
16
+ */
17
+ export declare class InvalidCharacterError extends Error {
18
+ /**
19
+ * @param char - The invalid character that was encountered (optional)
20
+ */
21
+ constructor(char?: string);
22
+ }
23
+ /**
24
+ * Error thrown when decoded buffer size exceeds the specified size.
25
+ *
26
+ * This error is thrown when using the `size` option in decode operations
27
+ * and the decoded data is larger than the specified size.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * import { decode, BufferSizeError } from 'bufferbase';
32
+ *
33
+ * try {
34
+ * // Trying to decode into a buffer too small
35
+ * decode('72k1xXWG59fYdzSNoA', 'base58', { size: 2 });
36
+ * } catch (e) {
37
+ * if (e instanceof BufferSizeError) {
38
+ * console.log(e.message); // "Buffer size 13 exceeds specified size 2"
39
+ * }
40
+ * }
41
+ * ```
42
+ */
43
+ export declare class BufferSizeError extends Error {
44
+ /**
45
+ * @param actual - The actual decoded buffer size
46
+ * @param expected - The expected (specified) buffer size
47
+ */
48
+ constructor(actual: number, expected: number);
49
+ }
50
+ /**
51
+ * Error thrown when an unknown base name is provided.
52
+ *
53
+ * This error is thrown when using the function API with an invalid base name
54
+ * that is neither a known encoding name nor a valid custom character set
55
+ * (character sets must be longer than 15 characters).
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * import { encode, UnknownBaseError } from 'bufferbase';
60
+ *
61
+ * try {
62
+ * encode(Buffer.from('test'), 'invalidbase');
63
+ * } catch (e) {
64
+ * if (e instanceof UnknownBaseError) {
65
+ * console.log(e.message); // "Unknown base encoding: 'invalidbase'"
66
+ * }
67
+ * }
68
+ * ```
69
+ */
70
+ export declare class UnknownBaseError extends Error {
71
+ /**
72
+ * @param name - The unknown base name that was provided
73
+ */
74
+ constructor(name: string);
75
+ }
76
+ //# sourceMappingURL=errors.d.ts.map