bufferbase 1.3.0 → 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.
- package/README.md +99 -18
- package/dist/cjs/bases.d.ts +82 -0
- package/dist/cjs/bases.d.ts.map +1 -0
- package/dist/cjs/bases.js +121 -0
- package/dist/cjs/bases.js.map +1 -0
- package/dist/cjs/bufferbase.d.ts +19 -36
- package/dist/cjs/bufferbase.d.ts.map +1 -1
- package/dist/cjs/bufferbase.js +27 -111
- package/dist/cjs/bufferbase.js.map +1 -1
- package/dist/cjs/codec.d.ts +124 -0
- package/dist/cjs/codec.d.ts.map +1 -0
- package/dist/cjs/codec.js +247 -0
- package/dist/cjs/codec.js.map +1 -0
- package/dist/cjs/codecs.d.ts +71 -0
- package/dist/cjs/codecs.d.ts.map +1 -0
- package/dist/cjs/codecs.js +92 -0
- package/dist/cjs/codecs.js.map +1 -0
- package/dist/cjs/errors.d.ts +76 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +92 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/example.js +20 -27
- package/dist/cjs/example.js.map +1 -1
- package/dist/cjs/functions.d.ts +62 -0
- package/dist/cjs/functions.d.ts.map +1 -0
- package/dist/cjs/functions.js +93 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.d.ts +8 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +25 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +6 -7
- package/dist/cjs/types.d.ts +107 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/bases.d.ts +82 -0
- package/dist/esm/bases.d.ts.map +1 -0
- package/dist/esm/bases.js +116 -0
- package/dist/esm/bases.js.map +1 -0
- package/dist/esm/bufferbase.d.ts +19 -36
- package/dist/esm/bufferbase.d.ts.map +1 -1
- package/dist/esm/bufferbase.js +25 -107
- package/dist/esm/bufferbase.js.map +1 -1
- package/dist/esm/codec.d.ts +124 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +242 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/codecs.d.ts +71 -0
- package/dist/esm/codecs.d.ts.map +1 -0
- package/dist/esm/codecs.js +89 -0
- package/dist/esm/codecs.js.map +1 -0
- package/dist/esm/errors.d.ts +76 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +86 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/example.js +19 -26
- package/dist/esm/example.js.map +1 -1
- package/dist/esm/functions.d.ts +62 -0
- package/dist/esm/functions.d.ts.map +1 -0
- package/dist/esm/functions.js +87 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.d.ts +8 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +12 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/package.json +6 -7
- package/dist/esm/types.d.ts +107 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +8 -9
- package/src/bases.ts +120 -0
- package/src/bufferbase.test.ts +44 -27
- package/src/bufferbase.ts +31 -106
- package/src/codec.test.ts +320 -0
- package/src/codec.ts +266 -0
- package/src/codecs.ts +118 -0
- package/src/errors.ts +87 -0
- package/src/example.ts +20 -30
- package/src/functions.ts +93 -0
- package/src/index.ts +21 -8
- package/src/types.ts +129 -0
- package/dist/cjs/bufferbase.d.mts +0 -60
- package/dist/cjs/bufferbase.d.mts.map +0 -1
- package/dist/cjs/bufferbase.mjs +0 -150
- package/dist/cjs/bufferbase.mjs.map +0 -1
- package/dist/cjs/example.d.mts +0 -2
- package/dist/cjs/example.d.mts.map +0 -1
- package/dist/cjs/example.mjs +0 -39
- package/dist/cjs/example.mjs.map +0 -1
- package/dist/cjs/index.d.mts +0 -2
- package/dist/cjs/index.d.mts.map +0 -1
- package/dist/cjs/index.mjs +0 -11
- package/dist/cjs/index.mjs.map +0 -1
- package/dist/esm/bufferbase.d.mts +0 -60
- package/dist/esm/bufferbase.d.mts.map +0 -1
- package/dist/esm/bufferbase.mjs +0 -141
- package/dist/esm/bufferbase.mjs.map +0 -1
- package/dist/esm/example.d.mts +0 -2
- package/dist/esm/example.d.mts.map +0 -1
- package/dist/esm/example.mjs +0 -37
- package/dist/esm/example.mjs.map +0 -1
- package/dist/esm/index.d.mts +0 -2
- package/dist/esm/index.d.mts.map +0 -1
- package/dist/esm/index.mjs +0 -2
- package/dist/esm/index.mjs.map +0 -1
|
@@ -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,KAAK,CAAC;IACf,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,KAAK;IAOhB;;;OAGG;IACH,YAAY,KAAa;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,IAAI,GAAG,EAAE,CAAC;QAC7B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,KAAiB;QACtB,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC/B,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,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IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBAChC,OAAO,KAAK,CAAC;YACf,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,MAAc,EAAE,KAAa;QACrC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClC,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,OAAO,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { 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,KAA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+
* 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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C;;OAEG;gBACS,IAAI,CAAC,EAAE,MAAM;CAK1B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC;;;OAGG;gBACS,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;CAI7C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC;;OAEG;gBACS,IAAI,EAAE,MAAM;CAIzB"}
|
|
@@ -0,0 +1,86 @@
|
|
|
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 class InvalidCharacterError extends Error {
|
|
18
|
+
/**
|
|
19
|
+
* @param char - The invalid character that was encountered (optional)
|
|
20
|
+
*/
|
|
21
|
+
constructor(char) {
|
|
22
|
+
const message = char ? `Invalid character found: '${char}'` : 'Invalid character found';
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = 'InvalidCharacterError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Error thrown when decoded buffer size exceeds the specified size.
|
|
29
|
+
*
|
|
30
|
+
* This error is thrown when using the `size` option in decode operations
|
|
31
|
+
* and the decoded data is larger than the specified size.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { decode, BufferSizeError } from 'bufferbase';
|
|
36
|
+
*
|
|
37
|
+
* try {
|
|
38
|
+
* // Trying to decode into a buffer too small
|
|
39
|
+
* decode('72k1xXWG59fYdzSNoA', 'base58', { size: 2 });
|
|
40
|
+
* } catch (e) {
|
|
41
|
+
* if (e instanceof BufferSizeError) {
|
|
42
|
+
* console.log(e.message); // "Buffer size 13 exceeds specified size 2"
|
|
43
|
+
* }
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export class BufferSizeError extends Error {
|
|
48
|
+
/**
|
|
49
|
+
* @param actual - The actual decoded buffer size
|
|
50
|
+
* @param expected - The expected (specified) buffer size
|
|
51
|
+
*/
|
|
52
|
+
constructor(actual, expected) {
|
|
53
|
+
super(`Buffer size ${actual} exceeds specified size ${expected}`);
|
|
54
|
+
this.name = 'BufferSizeError';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Error thrown when an unknown base name is provided.
|
|
59
|
+
*
|
|
60
|
+
* This error is thrown when using the function API with an invalid base name
|
|
61
|
+
* that is neither a known encoding name nor a valid custom character set
|
|
62
|
+
* (character sets must be longer than 15 characters).
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { encode, UnknownBaseError } from 'bufferbase';
|
|
67
|
+
*
|
|
68
|
+
* try {
|
|
69
|
+
* encode(Buffer.from('test'), 'invalidbase');
|
|
70
|
+
* } catch (e) {
|
|
71
|
+
* if (e instanceof UnknownBaseError) {
|
|
72
|
+
* console.log(e.message); // "Unknown base encoding: 'invalidbase'"
|
|
73
|
+
* }
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export class UnknownBaseError extends Error {
|
|
78
|
+
/**
|
|
79
|
+
* @param name - The unknown base name that was provided
|
|
80
|
+
*/
|
|
81
|
+
constructor(name) {
|
|
82
|
+
super(`Unknown base encoding: '${name}'`);
|
|
83
|
+
this.name = 'UnknownBaseError';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C;;OAEG;IACH,YAAY,IAAa;QACvB,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,6BAA6B,IAAI,GAAG,CAAC,CAAC,CAAC,yBAAyB,CAAC;QACxF,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACtC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACxC;;;OAGG;IACH,YAAY,MAAc,EAAE,QAAgB;QAC1C,KAAK,CAAC,eAAe,MAAM,2BAA2B,QAAQ,EAAE,CAAC,CAAC;QAClE,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC;;OAEG;IACH,YAAY,IAAY;QACtB,KAAK,CAAC,2BAA2B,IAAI,GAAG,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF"}
|
package/dist/esm/example.js
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { encode, decode, convert, validate, Codecs } from './index.js';
|
|
2
3
|
// Example buffer
|
|
3
4
|
const bytes = Buffer.from('Hello, World!', 'utf8');
|
|
4
|
-
//
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const converter_58to64 = new Converter(Chars.Base58, Chars.Base64_URL_SAFE);
|
|
12
|
-
const base64encoded = converter_58to64.convert(base58encoded);
|
|
13
|
-
// Validating Base64
|
|
14
|
-
const validatorB64 = new Validator(Chars.Base64_URL_SAFE);
|
|
15
|
-
const isValidAsBase64 = validatorB64.validate(base64encoded);
|
|
16
|
-
// Decoding Base64
|
|
17
|
-
const decoder = new BufferEncoder(Chars.Base64_URL_SAFE);
|
|
18
|
-
const decoded = decoder.decode(base64encoded);
|
|
5
|
+
// Using simple function API
|
|
6
|
+
const base32encoded = encode(bytes, 'base32crockford');
|
|
7
|
+
const base58encoded = convert(base32encoded, 'base32crockford', 'base58');
|
|
8
|
+
const base64encoded = convert(base58encoded, 'base58', 'base64url');
|
|
9
|
+
const isValidAsBase64 = validate(base64encoded, 'base64url');
|
|
10
|
+
const decoded = decode(base64encoded, 'base64url');
|
|
11
|
+
console.log('=== Function API ===');
|
|
19
12
|
console.table({
|
|
20
13
|
bytes: bytes.toString('utf8'),
|
|
21
14
|
base32encoded,
|
|
@@ -24,14 +17,14 @@ console.table({
|
|
|
24
17
|
isValidAsBase64,
|
|
25
18
|
decoded: decoded.toString('utf8'),
|
|
26
19
|
});
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
20
|
+
// Using Codecs object
|
|
21
|
+
console.log('\n=== Codecs API ===');
|
|
22
|
+
const encoded = Codecs.base58.encode(bytes);
|
|
23
|
+
const converted = Codecs.base58.convertTo(Codecs.base64url, encoded);
|
|
24
|
+
console.table({
|
|
25
|
+
original: bytes.toString('utf8'),
|
|
26
|
+
base58: encoded,
|
|
27
|
+
base64url: converted,
|
|
28
|
+
valid: Codecs.base64url.validate(converted),
|
|
29
|
+
});
|
|
37
30
|
//# sourceMappingURL=example.js.map
|
package/dist/esm/example.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEvE,iBAAiB;AACjB,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;AAEnD,4BAA4B;AAC5B,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC;AACvD,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,EAAE,iBAAiB,EAAE,QAAQ,CAAC,CAAC;AAC1E,MAAM,aAAa,GAAG,OA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