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.
- package/README.md +162 -63
- package/dist/cjs/bases.d.ts +152 -45
- package/dist/cjs/bases.d.ts.map +1 -1
- package/dist/cjs/bases.js +90 -77
- package/dist/cjs/bases.js.map +1 -1
- package/dist/cjs/bufferbase.d.ts +21 -27
- package/dist/cjs/bufferbase.d.ts.map +1 -1
- package/dist/cjs/bufferbase.js +21 -31
- package/dist/cjs/bufferbase.js.map +1 -1
- package/dist/cjs/codec-base.d.ts +56 -0
- package/dist/cjs/codec-base.d.ts.map +1 -0
- package/dist/cjs/codec-base.js +132 -0
- package/dist/cjs/codec-base.js.map +1 -0
- package/dist/cjs/codec-block.d.ts +47 -0
- package/dist/cjs/codec-block.d.ts.map +1 -0
- package/dist/cjs/codec-block.js +213 -0
- package/dist/cjs/codec-block.js.map +1 -0
- package/dist/cjs/codec-block85.d.ts +56 -0
- package/dist/cjs/codec-block85.d.ts.map +1 -0
- package/dist/cjs/codec-block85.js +271 -0
- package/dist/cjs/codec-block85.js.map +1 -0
- package/dist/cjs/codec-radix.d.ts +46 -0
- package/dist/cjs/codec-radix.d.ts.map +1 -0
- package/dist/cjs/codec-radix.js +181 -0
- package/dist/cjs/codec-radix.js.map +1 -0
- package/dist/cjs/codec.d.ts +25 -111
- package/dist/cjs/codec.d.ts.map +1 -1
- package/dist/cjs/codec.js +50 -228
- package/dist/cjs/codec.js.map +1 -1
- package/dist/cjs/codecs.d.ts +8 -59
- package/dist/cjs/codecs.d.ts.map +1 -1
- package/dist/cjs/codecs.js +6 -79
- package/dist/cjs/codecs.js.map +1 -1
- package/dist/cjs/errors.d.ts +65 -48
- package/dist/cjs/errors.d.ts.map +1 -1
- package/dist/cjs/errors.js +89 -51
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/example.js +44 -21
- package/dist/cjs/example.js.map +1 -1
- package/dist/cjs/functions.d.ts +27 -31
- package/dist/cjs/functions.d.ts.map +1 -1
- package/dist/cjs/functions.js +25 -47
- package/dist/cjs/functions.js.map +1 -1
- package/dist/cjs/index.d.ts +7 -5
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +17 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +12 -7
- package/dist/cjs/types.d.ts +108 -71
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/bases.d.ts +152 -45
- package/dist/esm/bases.d.ts.map +1 -1
- package/dist/esm/bases.js +88 -75
- package/dist/esm/bases.js.map +1 -1
- package/dist/esm/bufferbase.d.ts +21 -27
- package/dist/esm/bufferbase.d.ts.map +1 -1
- package/dist/esm/bufferbase.js +18 -30
- package/dist/esm/bufferbase.js.map +1 -1
- package/dist/esm/codec-base.d.ts +56 -0
- package/dist/esm/codec-base.d.ts.map +1 -0
- package/dist/esm/codec-base.js +127 -0
- package/dist/esm/codec-base.js.map +1 -0
- package/dist/esm/codec-block.d.ts +47 -0
- package/dist/esm/codec-block.d.ts.map +1 -0
- package/dist/esm/codec-block.js +209 -0
- package/dist/esm/codec-block.js.map +1 -0
- package/dist/esm/codec-block85.d.ts +56 -0
- package/dist/esm/codec-block85.d.ts.map +1 -0
- package/dist/esm/codec-block85.js +267 -0
- package/dist/esm/codec-block85.js.map +1 -0
- package/dist/esm/codec-radix.d.ts +46 -0
- package/dist/esm/codec-radix.d.ts.map +1 -0
- package/dist/esm/codec-radix.js +177 -0
- package/dist/esm/codec-radix.js.map +1 -0
- package/dist/esm/codec.d.ts +25 -111
- package/dist/esm/codec.d.ts.map +1 -1
- package/dist/esm/codec.js +44 -226
- package/dist/esm/codec.js.map +1 -1
- package/dist/esm/codecs.d.ts +8 -59
- package/dist/esm/codecs.d.ts.map +1 -1
- package/dist/esm/codecs.js +7 -80
- package/dist/esm/codecs.js.map +1 -1
- package/dist/esm/errors.d.ts +65 -48
- package/dist/esm/errors.d.ts.map +1 -1
- package/dist/esm/errors.js +83 -50
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/example.js +45 -22
- package/dist/esm/example.js.map +1 -1
- package/dist/esm/functions.d.ts +27 -31
- package/dist/esm/functions.d.ts.map +1 -1
- package/dist/esm/functions.js +26 -48
- package/dist/esm/functions.js.map +1 -1
- package/dist/esm/index.d.ts +7 -5
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/package.json +12 -7
- package/dist/esm/types.d.ts +108 -71
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +12 -7
- package/src/algorithms.test.ts +442 -0
- package/src/bases.ts +98 -76
- package/src/bufferbase.ts +26 -36
- package/src/codec-base.ts +181 -0
- package/src/codec-block.ts +258 -0
- package/src/codec-block85.ts +315 -0
- package/src/codec-radix.ts +206 -0
- package/src/codec.test.ts +17 -9
- package/src/codec.ts +48 -247
- package/src/codecs.ts +11 -105
- package/src/errors.ts +90 -50
- package/src/example.ts +47 -22
- package/src/functions.ts +34 -54
- package/src/index.ts +27 -7
- 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,CAAC,WAAW,GAAG,IAAI,GAAG,EAAE,CAAC;YAC7B,OAAO;QACT,CAAC;QACD,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACvC,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;YAC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AG,KAAK,CAAC;YAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;gBACtC,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;gBACzB,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,GAAG,CAAC;gBACvB,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;YAClC,CAAC;YACD,OAAO,KAAK,GAAG,CAAC,EAAE,CAAC;gBACjB,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;gBACxB,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;YAClC,CAAC;QACH,CAAC;QAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAChB,CAAC;QACD,KAAK,CAAC,OAAO,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,oDAAoD;IAC5C,WAAW,CAAC,KAAuB,EAAE,OAAe;QAC1D,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,SAAS,GAAG,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC;QACjD,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF"}
|
package/dist/esm/codec.d.ts
CHANGED
|
@@ -1,124 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { CodecSpec, ICodec } from './types.js';
|
|
2
|
+
import { RadixCodec } from './codec-radix.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Builds the codec described by a spec.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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 {
|
|
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
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
-
*
|
|
115
|
-
*
|
|
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
|
|
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
|
package/dist/esm/codec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,
|
|
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 {
|
|
2
|
-
import {
|
|
1
|
+
import { RadixCodec } from './codec-radix.js';
|
|
2
|
+
import { BlockCodec } from './codec-block.js';
|
|
3
|
+
import { Block85Codec } from './codec-block85.js';
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
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
|
-
*
|
|
17
|
-
*
|
|
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 {
|
|
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
|
-
*
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
227
|
-
*
|
|
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
|
|
240
|
-
|
|
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
|
package/dist/esm/codec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,WAAW,CAAC,IAAwB;IAClD,MAAM,QAAQ,GACZ,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IAE3E,QAAQ,QAAQ,CAAC,SAAS,EAAE,CAAC;QAC3B,KAAK,OAAO;YACV,OAAO,IAAI,UAAU,CAAC,QAAQ,CAAC,CAAC;QAClC,KAAK,OAAO;YACV,OAAO,IAAI,UAAU,CAAC,QAAQ,CAAC,CAAC;QAClC,KAAK,SAAS;YACZ,OAAO,IAAI,YAAY,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC;AACH,CAAC;AAED,MAAM,KAAK,GAAG,IAAI,OAA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