bufferbase 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -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 +13 -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 +13 -7
- package/dist/esm/types.d.ts +108 -71
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +13 -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
package/dist/esm/bases.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
import { UnknownBaseError } from './errors.js';
|
|
2
2
|
/**
|
|
3
|
-
* Character sets
|
|
3
|
+
* Character sets, ordered by value.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* import { Chars, createCodec } from 'bufferbase';
|
|
11
|
-
*
|
|
12
|
-
* const codec = createCodec(Chars.Base58);
|
|
13
|
-
* const encoded = codec.encode(Buffer.from('Hello'));
|
|
14
|
-
* ```
|
|
5
|
+
* A character set does not by itself describe an encoding — see `Bases` for
|
|
6
|
+
* the alphabet/algorithm pairs that do.
|
|
15
7
|
*/
|
|
16
8
|
export const Chars = {
|
|
17
9
|
/** Base 10: 0-9 */
|
|
18
10
|
Decimal: '0123456789',
|
|
19
|
-
/**
|
|
11
|
+
/** RFC 4648 Table 5 — Base16: 0-9A-F */
|
|
20
12
|
Base16: '0123456789ABCDEF',
|
|
21
|
-
/** RFC 4648 Base32: A-Z2-7 */
|
|
13
|
+
/** RFC 4648 Table 3 — Base32: A-Z2-7 */
|
|
22
14
|
Base32: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
|
|
15
|
+
/** RFC 4648 Table 4 — Base32 with extended hex alphabet: 0-9A-V */
|
|
16
|
+
Base32Hex: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
|
|
23
17
|
/** Crockford's Base32: 0-9A-HJKMNP-TV-Z (no I, L, O, U) */
|
|
24
18
|
Base32Crockford: '0123456789ABCDEFGHJKMNPQRSTVWXYZ',
|
|
25
19
|
/** Alphanumeric: 0-9A-Z */
|
|
@@ -28,89 +22,108 @@ export const Chars = {
|
|
|
28
22
|
Base52: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
|
29
23
|
/** Bitcoin alphabet: 1-9A-HJ-NP-Za-km-z (no 0, O, I, l) */
|
|
30
24
|
Base58: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
|
31
|
-
/**
|
|
25
|
+
/** RFC 4648 Table 1 — Base64: A-Za-z0-9+/ */
|
|
32
26
|
Base64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
|
33
|
-
/** URL-safe Base64: A-Za-z0-9-_ */
|
|
27
|
+
/** RFC 4648 Table 2 — URL-safe Base64: A-Za-z0-9-_ */
|
|
34
28
|
Base64Url: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
|
|
35
29
|
/** XML token Base64: A-Za-z0-9._ */
|
|
36
30
|
Base64Xml: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._',
|
|
37
31
|
/** XML name Base64: A-Za-z0-9_: */
|
|
38
32
|
Base64XmlName: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:',
|
|
39
|
-
/**
|
|
40
|
-
Ascii85: '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstu',
|
|
41
|
-
/**
|
|
42
|
-
Base85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~',
|
|
43
|
-
/** ZeroMQ Z85 encoding */
|
|
33
|
+
/** Ascii85: the 85 printable characters from `!` (0x21) to `u` (0x75) */
|
|
34
|
+
Ascii85: '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu',
|
|
35
|
+
/** ZeroMQ Z85 (RFC 32): avoids the quote and backslash characters */
|
|
44
36
|
Z85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#',
|
|
37
|
+
/** The five symbols Crockford's Base32 adds for its check symbol, giving a modulus of 37 */
|
|
38
|
+
CrockfordCheck: '*~$=U',
|
|
45
39
|
};
|
|
40
|
+
/** The characters Crockford's Base32 reads as the digit they resemble. */
|
|
41
|
+
const CROCKFORD_ALIASES = { I: '1', L: '1', O: '0' };
|
|
46
42
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @internal
|
|
49
|
-
*/
|
|
50
|
-
const baseNameToChars = {
|
|
51
|
-
decimal: Chars.Decimal,
|
|
52
|
-
base16: Chars.Base16,
|
|
53
|
-
hex: Chars.Base16,
|
|
54
|
-
base32: Chars.Base32,
|
|
55
|
-
base32crockford: Chars.Base32Crockford,
|
|
56
|
-
base36: Chars.Base36,
|
|
57
|
-
base52: Chars.Base52,
|
|
58
|
-
base58: Chars.Base58,
|
|
59
|
-
base64: Chars.Base64,
|
|
60
|
-
base64url: Chars.Base64Url,
|
|
61
|
-
base64xml: Chars.Base64Xml,
|
|
62
|
-
base64xmlname: Chars.Base64XmlName,
|
|
63
|
-
ascii85: Chars.Ascii85,
|
|
64
|
-
base85: Chars.Base85,
|
|
65
|
-
z85: Chars.Z85,
|
|
66
|
-
};
|
|
67
|
-
/**
|
|
68
|
-
* Resolves a base name or character set string to a character set.
|
|
69
|
-
*
|
|
70
|
-
* This function is used internally by the function API to convert
|
|
71
|
-
* base names (like `'base58'`) to their character sets.
|
|
43
|
+
* The encodings this package knows by name.
|
|
72
44
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
45
|
+
* Each entry pairs an alphabet with the algorithm that reads it, because the
|
|
46
|
+
* alphabet alone is ambiguous: `base64` and `radix64` share Table 1 but
|
|
47
|
+
* produce different strings for the same bytes.
|
|
76
48
|
*
|
|
77
49
|
* @example
|
|
78
50
|
* ```typescript
|
|
79
|
-
* import {
|
|
51
|
+
* import { encode, Bases } from 'bufferbase';
|
|
80
52
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* resolveChars('invalid'); // Throws UnknownBaseError
|
|
53
|
+
* encode(Buffer.from('fo'), 'base64'); // 'Zm8=' — RFC 4648
|
|
54
|
+
* encode(Buffer.from('fo'), 'radix64'); // 'GZv' — radix conversion
|
|
84
55
|
* ```
|
|
85
56
|
*/
|
|
86
|
-
export
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
57
|
+
export const Bases = {
|
|
58
|
+
// Radix conversion. No external standard fixes these, so nothing here can
|
|
59
|
+
// disagree with one.
|
|
60
|
+
decimal: { alphabet: Chars.Decimal, algorithm: 'radix' },
|
|
61
|
+
base36: { alphabet: Chars.Base36, algorithm: 'radix', caseInsensitive: true },
|
|
62
|
+
base52: { alphabet: Chars.Base52, algorithm: 'radix' },
|
|
63
|
+
base58: { alphabet: Chars.Base58, algorithm: 'radix' },
|
|
64
|
+
/**
|
|
65
|
+
* Crockford's Base32, which specifies a notation for numbers rather than for
|
|
66
|
+
* byte streams, so a radix conversion is the right reading of it.
|
|
67
|
+
*
|
|
68
|
+
* Decoding accepts either case, reads `I` and `L` as `1` and `O` as `0`, and
|
|
69
|
+
* ignores hyphens.
|
|
70
|
+
*/
|
|
71
|
+
base32crockford: {
|
|
72
|
+
alphabet: Chars.Base32Crockford,
|
|
73
|
+
algorithm: 'radix',
|
|
74
|
+
caseInsensitive: true,
|
|
75
|
+
aliases: CROCKFORD_ALIASES,
|
|
76
|
+
ignore: '-',
|
|
77
|
+
},
|
|
78
|
+
/** Crockford's Base32 carrying its trailing check symbol. */
|
|
79
|
+
base32crockfordcheck: {
|
|
80
|
+
alphabet: Chars.Base32Crockford,
|
|
81
|
+
algorithm: 'radix',
|
|
82
|
+
caseInsensitive: true,
|
|
83
|
+
aliases: CROCKFORD_ALIASES,
|
|
84
|
+
ignore: '-',
|
|
85
|
+
checkSymbols: Chars.CrockfordCheck,
|
|
86
|
+
},
|
|
87
|
+
// Bit-block encoding, RFC 4648.
|
|
88
|
+
base16: { alphabet: Chars.Base16, algorithm: 'block', pad: false, caseInsensitive: true },
|
|
89
|
+
hex: { alphabet: Chars.Base16, algorithm: 'block', pad: false, caseInsensitive: true },
|
|
90
|
+
base32: { alphabet: Chars.Base32, algorithm: 'block', caseInsensitive: true },
|
|
91
|
+
base32hex: { alphabet: Chars.Base32Hex, algorithm: 'block', caseInsensitive: true },
|
|
92
|
+
base64: { alphabet: Chars.Base64, algorithm: 'block' },
|
|
93
|
+
/** Unpadded, as used by JWT and other URL-facing formats. */
|
|
94
|
+
base64url: { alphabet: Chars.Base64Url, algorithm: 'block', pad: false },
|
|
95
|
+
base64xml: { alphabet: Chars.Base64Xml, algorithm: 'block', pad: false },
|
|
96
|
+
base64xmlname: { alphabet: Chars.Base64XmlName, algorithm: 'block', pad: false },
|
|
97
|
+
// Four bytes to five characters.
|
|
98
|
+
/** Ascii85 with the zero-block shortcut, matching Python's `a85encode`. */
|
|
99
|
+
ascii85: { alphabet: Chars.Ascii85, algorithm: 'block85', zeroShortcut: true },
|
|
100
|
+
/** ZeroMQ Z85, which has no padding and so encodes whole blocks only. */
|
|
101
|
+
z85: { alphabet: Chars.Z85, algorithm: 'block85', requireFullBlocks: true },
|
|
102
|
+
// Radix conversion over the RFC alphabets.
|
|
103
|
+
radix16: { alphabet: Chars.Base16, algorithm: 'radix' },
|
|
104
|
+
radix32: { alphabet: Chars.Base32, algorithm: 'radix' },
|
|
105
|
+
radix64: { alphabet: Chars.Base64, algorithm: 'radix' },
|
|
106
|
+
radix64url: { alphabet: Chars.Base64Url, algorithm: 'radix' },
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Resolves a base name to its codec spec, and passes a spec through unchanged.
|
|
110
|
+
*
|
|
111
|
+
* @throws {UnknownBaseError} If the name is not one of `Bases`
|
|
112
|
+
*/
|
|
113
|
+
export function resolveSpec(base) {
|
|
114
|
+
if (typeof base !== 'string') {
|
|
94
115
|
return base;
|
|
95
116
|
}
|
|
96
|
-
|
|
117
|
+
const spec = Bases[base.toLowerCase()];
|
|
118
|
+
if (spec === undefined) {
|
|
119
|
+
throw new UnknownBaseError(base);
|
|
120
|
+
}
|
|
121
|
+
return spec;
|
|
97
122
|
}
|
|
98
123
|
/**
|
|
99
|
-
* Checks
|
|
100
|
-
*
|
|
101
|
-
* @param value - The string to check
|
|
102
|
-
* @returns `true` if the value is a valid `BaseName`, `false` otherwise
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* import { isBaseName } from 'bufferbase';
|
|
107
|
-
*
|
|
108
|
-
* isBaseName('base58'); // true
|
|
109
|
-
* isBaseName('BASE58'); // true (case-insensitive)
|
|
110
|
-
* isBaseName('unknown'); // false
|
|
111
|
-
* ```
|
|
124
|
+
* Checks whether a string names one of the encodings in `Bases`.
|
|
112
125
|
*/
|
|
113
126
|
export function isBaseName(value) {
|
|
114
|
-
return value.toLowerCase() in
|
|
127
|
+
return value.toLowerCase() in Bases;
|
|
115
128
|
}
|
|
116
129
|
//# sourceMappingURL=bases.js.map
|
package/dist/esm/bases.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bases.js","sourceRoot":"","sources":["../../src/bases.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C
|
|
1
|
+
{"version":3,"file":"bases.js","sourceRoot":"","sources":["../../src/bases.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,mBAAmB;IACnB,OAAO,EAAE,YAAY;IACrB,wCAAwC;IACxC,MAAM,EAAE,kBAAkB;IAC1B,wCAAwC;IACxC,MAAM,EAAE,kCAAkC;IAC1C,mEAAmE;IACnE,SAAS,EAAE,kCAAkC;IAC7C,2DAA2D;IAC3D,eAAe,EAAE,kCAAkC;IACnD,2BAA2B;IAC3B,MAAM,EAAE,sCAAsC;IAC9C,2BAA2B;IAC3B,MAAM,EAAE,sDAAsD;IAC9D,2DAA2D;IAC3D,MAAM,EAAE,4DAA4D;IACpE,6CAA6C;IAC7C,MAAM,EAAE,kEAAkE;IAC1E,sDAAsD;IACtD,SAAS,EAAE,kEAAkE;IAC7E,oCAAoC;IACpC,SAAS,EAAE,kEAAkE;IAC7E,mCAAmC;IACnC,aAAa,EAAE,kEAAkE;IACjF,yEAAyE;IACzE,OAAO,EAAE,yFAAyF;IAClG,qEAAqE;IACrE,GAAG,EAAE,uFAAuF;IAC5F,4FAA4F;IAC5F,cAAc,EAAE,OAAO;CACf,CAAC;AAEX,0EAA0E;AAC1E,MAAM,iBAAiB,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE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;AAO/C;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,IAA0B;IACpD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,IAAI,GAA0B,KAAK,CAAC,IAAI,CAAC,WAAW,EAAc,CAAC,CAAC;IAC1E,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,MAAM,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,KAAK,CAAC,WAAW,EAAE,IAAI,KAAK,CAAC;AACtC,CAAC"}
|
package/dist/esm/bufferbase.d.ts
CHANGED
|
@@ -1,43 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @deprecated Use the
|
|
3
|
-
* - `encode(buffer, 'base58')` or `base58.encode(buffer)`
|
|
4
|
-
* - `decode(encoded, 'base58')` or `base58.decode(encoded)`
|
|
5
|
-
* - `convert(input, 'base58', 'base64')` or `base58.convertTo(
|
|
6
|
-
* - `validate(input, 'base58')` or `base58.validate(input)`
|
|
2
|
+
* @deprecated Use the current API instead:
|
|
3
|
+
* - `encode(buffer, 'base58')` or `Codecs.base58.encode(buffer)`
|
|
4
|
+
* - `decode(encoded, 'base58')` or `Codecs.base58.decode(encoded)`
|
|
5
|
+
* - `convert(input, 'base58', 'base64')` or `Codecs.base58.convertTo(...)`
|
|
6
|
+
* - `validate(input, 'base58')` or `Codecs.base58.validate(input)`
|
|
7
|
+
*
|
|
8
|
+
* These classes take a bare alphabet, which always meant a radix conversion,
|
|
9
|
+
* so they keep behaving exactly as they did.
|
|
7
10
|
*/
|
|
8
|
-
import { Buffer } from 'node:buffer';
|
|
9
|
-
import {
|
|
11
|
+
import type { Buffer } from 'node:buffer';
|
|
12
|
+
import type { BufferLike } from './types.js';
|
|
13
|
+
/** @deprecated Import `Chars` from the package root. */
|
|
10
14
|
export { Chars } from './bases.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
15
|
+
/** @deprecated Import these from the package root. */
|
|
16
|
+
export { BufferSizeError, InvalidCharacterError } from './errors.js';
|
|
17
|
+
/** @deprecated Use `createCodec(alphabet)`. */
|
|
15
18
|
export declare class BufferEncoder {
|
|
16
|
-
private
|
|
17
|
-
private codec;
|
|
19
|
+
private readonly codec;
|
|
18
20
|
constructor(baseChars: string);
|
|
19
|
-
encode(buffer:
|
|
21
|
+
encode(buffer: BufferLike): string;
|
|
20
22
|
decode(encoded: string, bufferSize?: number): Buffer;
|
|
21
23
|
}
|
|
22
|
-
/**
|
|
23
|
-
* @deprecated Use `convert()` function or `codec.convertTo()` instead.
|
|
24
|
-
*/
|
|
24
|
+
/** @deprecated Use `convert()` or `codec.convertTo()`. */
|
|
25
25
|
export declare class Converter {
|
|
26
|
-
decoder: BufferEncoder;
|
|
27
|
-
encoder: BufferEncoder;
|
|
26
|
+
readonly decoder: BufferEncoder;
|
|
27
|
+
readonly encoder: BufferEncoder;
|
|
28
28
|
constructor(inputBase: string, outputBase: string);
|
|
29
29
|
convert(input: string): string;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
* @deprecated Use `validate()` function or `codec.validate()` instead.
|
|
33
|
-
*/
|
|
31
|
+
/** @deprecated Use `validate()` or `codec.validate()`. */
|
|
34
32
|
export declare class Validator {
|
|
35
|
-
private codec;
|
|
33
|
+
private readonly codec;
|
|
36
34
|
constructor(inputBase: string);
|
|
37
35
|
validate(input: string): boolean;
|
|
38
36
|
}
|
|
39
|
-
/**
|
|
40
|
-
* @deprecated Use `validate()` from the main module instead.
|
|
41
|
-
*/
|
|
42
|
-
export declare const validate: (input: string, base: string) => boolean;
|
|
43
37
|
//# sourceMappingURL=bufferbase.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bufferbase.d.ts","sourceRoot":"","sources":["../../src/bufferbase.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"bufferbase.d.ts","sourceRoot":"","sources":["../../src/bufferbase.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG7C,wDAAwD;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,sDAAsD;AACtD,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAErE,+CAA+C;AAC/C,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;gBAEvB,SAAS,EAAE,MAAM;IAI7B,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM;IAIlC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM;CAGrD;AAED,0DAA0D;AAC1D,qBAAa,SAAS;IACpB,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;gBAEpB,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;IAKjD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAG/B;AAED,0DAA0D;AAC1D,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;gBAEvB,SAAS,EAAE,MAAM;IAI7B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;CAGjC"}
|
package/dist/esm/bufferbase.js
CHANGED
|
@@ -1,35 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @deprecated Use the
|
|
3
|
-
* - `encode(buffer, 'base58')` or `base58.encode(buffer)`
|
|
4
|
-
* - `decode(encoded, 'base58')` or `base58.decode(encoded)`
|
|
5
|
-
* - `convert(input, 'base58', 'base64')` or `base58.convertTo(
|
|
6
|
-
* - `validate(input, 'base58')` or `base58.validate(input)`
|
|
2
|
+
* @deprecated Use the current API instead:
|
|
3
|
+
* - `encode(buffer, 'base58')` or `Codecs.base58.encode(buffer)`
|
|
4
|
+
* - `decode(encoded, 'base58')` or `Codecs.base58.decode(encoded)`
|
|
5
|
+
* - `convert(input, 'base58', 'base64')` or `Codecs.base58.convertTo(...)`
|
|
6
|
+
* - `validate(input, 'base58')` or `Codecs.base58.validate(input)`
|
|
7
|
+
*
|
|
8
|
+
* These classes take a bare alphabet, which always meant a radix conversion,
|
|
9
|
+
* so they keep behaving exactly as they did.
|
|
7
10
|
*/
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
import { InvalidCharacterError, BufferSizeError } from './errors.js';
|
|
11
|
-
// Re-export Chars for backward compatibility
|
|
11
|
+
import { RadixCodec } from './codec-radix.js';
|
|
12
|
+
/** @deprecated Import `Chars` from the package root. */
|
|
12
13
|
export { Chars } from './bases.js';
|
|
13
|
-
|
|
14
|
-
export {
|
|
15
|
-
/**
|
|
16
|
-
* @deprecated Use `Codec` or `createCodec()` instead.
|
|
17
|
-
*/
|
|
14
|
+
/** @deprecated Import these from the package root. */
|
|
15
|
+
export { BufferSizeError, InvalidCharacterError } from './errors.js';
|
|
16
|
+
/** @deprecated Use `createCodec(alphabet)`. */
|
|
18
17
|
export class BufferEncoder {
|
|
19
18
|
constructor(baseChars) {
|
|
20
|
-
this.
|
|
21
|
-
this.codec = new Codec(baseChars);
|
|
19
|
+
this.codec = new RadixCodec({ alphabet: baseChars, algorithm: 'radix' });
|
|
22
20
|
}
|
|
23
21
|
encode(buffer) {
|
|
24
22
|
return this.codec.encode(buffer);
|
|
25
23
|
}
|
|
26
24
|
decode(encoded, bufferSize) {
|
|
27
|
-
return this.codec.decode(encoded, bufferSize
|
|
25
|
+
return this.codec.decode(encoded, bufferSize === undefined ? undefined : { size: bufferSize });
|
|
28
26
|
}
|
|
29
27
|
}
|
|
30
|
-
/**
|
|
31
|
-
* @deprecated Use `convert()` function or `codec.convertTo()` instead.
|
|
32
|
-
*/
|
|
28
|
+
/** @deprecated Use `convert()` or `codec.convertTo()`. */
|
|
33
29
|
export class Converter {
|
|
34
30
|
constructor(inputBase, outputBase) {
|
|
35
31
|
this.decoder = new BufferEncoder(inputBase);
|
|
@@ -39,21 +35,13 @@ export class Converter {
|
|
|
39
35
|
return this.encoder.encode(this.decoder.decode(input));
|
|
40
36
|
}
|
|
41
37
|
}
|
|
42
|
-
/**
|
|
43
|
-
* @deprecated Use `validate()` function or `codec.validate()` instead.
|
|
44
|
-
*/
|
|
38
|
+
/** @deprecated Use `validate()` or `codec.validate()`. */
|
|
45
39
|
export class Validator {
|
|
46
40
|
constructor(inputBase) {
|
|
47
|
-
this.codec = new
|
|
41
|
+
this.codec = new RadixCodec({ alphabet: inputBase, algorithm: 'radix' });
|
|
48
42
|
}
|
|
49
43
|
validate(input) {
|
|
50
44
|
return this.codec.validate(input);
|
|
51
45
|
}
|
|
52
46
|
}
|
|
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
47
|
//# sourceMappingURL=bufferbase.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bufferbase.js","sourceRoot":"","sources":["../../src/bufferbase.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"bufferbase.js","sourceRoot":"","sources":["../../src/bufferbase.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,wDAAwD;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,sDAAsD;AACtD,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAA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ffer';
|
|
2
|
+
import type { Algorithm, BufferLike, CodecSpec, DecodeOptions, ICodec } from './types.js';
|
|
3
|
+
/** Converts a `BufferLike` to a `Buffer` without copying when possible. */
|
|
4
|
+
export declare function toBuffer(input: BufferLike): Buffer;
|
|
5
|
+
/**
|
|
6
|
+
* Shared alphabet handling for the three codec implementations.
|
|
7
|
+
*
|
|
8
|
+
* Subclasses provide the mapping between bytes and characters; this class owns
|
|
9
|
+
* the alphabet, the character lookup, optional case folding, and `convertTo`.
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class BaseCodec implements ICodec {
|
|
12
|
+
/** Character set used for encoding. */
|
|
13
|
+
readonly chars: string;
|
|
14
|
+
/** The algorithm this codec applies to `chars`. */
|
|
15
|
+
abstract readonly algorithm: Algorithm;
|
|
16
|
+
/** Whether non-canonical input is rejected. */
|
|
17
|
+
protected readonly strict: boolean;
|
|
18
|
+
private readonly charToIndex;
|
|
19
|
+
/** Characters dropped from the input before decoding. */
|
|
20
|
+
private readonly ignored;
|
|
21
|
+
/**
|
|
22
|
+
* The alphabet as one byte per character, or `null` when some character does
|
|
23
|
+
* not fit in a byte.
|
|
24
|
+
*
|
|
25
|
+
* Assembling output as bytes and decoding it once is several times faster
|
|
26
|
+
* than concatenating characters, and every alphabet in practical use is
|
|
27
|
+
* single-byte.
|
|
28
|
+
*/
|
|
29
|
+
protected readonly codes: Uint8Array | null;
|
|
30
|
+
/**
|
|
31
|
+
* The value of every decodable character, indexed by code unit, or `-1`.
|
|
32
|
+
* `null` when some character does not fit in a byte.
|
|
33
|
+
*
|
|
34
|
+
* Reading a character by index and looking it up here avoids allocating a
|
|
35
|
+
* string and hashing it for every character of the input.
|
|
36
|
+
*/
|
|
37
|
+
private readonly valueByCode;
|
|
38
|
+
protected constructor(spec: CodecSpec);
|
|
39
|
+
/** Reads the first `length` bytes of `written` back as text. */
|
|
40
|
+
protected textFrom(written: Uint8Array, length: number): string;
|
|
41
|
+
/** Drops the characters this codec ignores, such as readability hyphens. */
|
|
42
|
+
protected strip(input: string): string;
|
|
43
|
+
/** Returns the value of a character, or `undefined` if it is not in the alphabet. */
|
|
44
|
+
protected valueOf(char: string): number | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Returns the value of the character at `index`, or `-1` if it is not
|
|
47
|
+
* decodable. This is the hot path of every decoder.
|
|
48
|
+
*/
|
|
49
|
+
protected valueAt(input: string, index: number): number;
|
|
50
|
+
abstract encode(buffer: BufferLike): string;
|
|
51
|
+
abstract decode(encoded: string, options?: DecodeOptions): Buffer;
|
|
52
|
+
abstract validate(input: string): boolean;
|
|
53
|
+
/** Decodes with this codec, then re-encodes with `target`. */
|
|
54
|
+
convertTo(target: ICodec, input: string): string;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=codec-base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-base.d.ts","sourceRoot":"","sources":["../../src/codec-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AA6B1F,2EAA2E;AAC3E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAElD;AAED;;;;;GAKG;AACH,8BAAsB,SAAU,YAAW,MAAM;IAC/C,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,mDAAmD;IACnD,QAAQ,CAAC,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAEvC,+CAA+C;IAC/C,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IAEnC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsB;IAElD,yDAAyD;IACzD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsB;IAE9C;;;;;;;OAOG;IACH,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IAE5C;;;;;;OAMG;IACH,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAEhD,SAAS,aAAa,IAAI,EAAE,SAAS;IA0DrC,gEAAgE;IAChE,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAI/D,4EAA4E;IAC5E,SAAS,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IActC,qFAAqF;IACrF,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAInD;;;OAGG;IACH,SAAS,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IAUvD,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM;IAC3C,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM;IACjE,QAAQ,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAEzC,8DAA8D;IAC9D,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;CAGjD"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { InvalidCodecError } from './errors.js';
|
|
3
|
+
/** Returns one byte per character, or `null` if the alphabet needs more. */
|
|
4
|
+
function singleByteCodes(alphabet) {
|
|
5
|
+
const codes = new Uint8Array(alphabet.length);
|
|
6
|
+
for (let i = 0; i < alphabet.length; i++) {
|
|
7
|
+
const code = alphabet.charCodeAt(i);
|
|
8
|
+
if (code > 0xff) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
codes[i] = code;
|
|
12
|
+
}
|
|
13
|
+
return codes;
|
|
14
|
+
}
|
|
15
|
+
/** Indexes the decodable characters by code unit, or `null` if any needs more than a byte. */
|
|
16
|
+
function codeTable(charToIndex) {
|
|
17
|
+
const table = new Int16Array(256).fill(-1);
|
|
18
|
+
for (const [char, value] of charToIndex) {
|
|
19
|
+
const code = char.charCodeAt(0);
|
|
20
|
+
if (char.length !== 1 || code > 0xff) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
table[code] = value;
|
|
24
|
+
}
|
|
25
|
+
return table;
|
|
26
|
+
}
|
|
27
|
+
/** Converts a `BufferLike` to a `Buffer` without copying when possible. */
|
|
28
|
+
export function toBuffer(input) {
|
|
29
|
+
return Buffer.isBuffer(input) ? input : Buffer.from(input.buffer, input.byteOffset, input.length);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Shared alphabet handling for the three codec implementations.
|
|
33
|
+
*
|
|
34
|
+
* Subclasses provide the mapping between bytes and characters; this class owns
|
|
35
|
+
* the alphabet, the character lookup, optional case folding, and `convertTo`.
|
|
36
|
+
*/
|
|
37
|
+
export class BaseCodec {
|
|
38
|
+
constructor(spec) {
|
|
39
|
+
const { alphabet, strict = true, caseInsensitive = false, aliases, ignore } = spec;
|
|
40
|
+
if (alphabet.length < 2) {
|
|
41
|
+
throw new InvalidCodecError(`alphabet needs at least 2 characters, got ${alphabet.length}`);
|
|
42
|
+
}
|
|
43
|
+
if (new Set(alphabet).size !== alphabet.length) {
|
|
44
|
+
throw new InvalidCodecError('alphabet contains duplicate characters');
|
|
45
|
+
}
|
|
46
|
+
if ([...alphabet].some((char) => char.length !== 1)) {
|
|
47
|
+
throw new InvalidCodecError('every alphabet character must be a single code unit');
|
|
48
|
+
}
|
|
49
|
+
if (caseInsensitive && new Set(alphabet.toLowerCase()).size !== alphabet.length) {
|
|
50
|
+
throw new InvalidCodecError('caseInsensitive needs an alphabet that does not contain both cases of a letter');
|
|
51
|
+
}
|
|
52
|
+
this.chars = alphabet;
|
|
53
|
+
this.strict = strict;
|
|
54
|
+
this.charToIndex = new Map();
|
|
55
|
+
const accept = (char, value) => {
|
|
56
|
+
this.charToIndex.set(char, value);
|
|
57
|
+
if (caseInsensitive) {
|
|
58
|
+
this.charToIndex.set(char.toLowerCase(), value);
|
|
59
|
+
this.charToIndex.set(char.toUpperCase(), value);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
for (let i = 0; i < alphabet.length; i++) {
|
|
63
|
+
accept(alphabet[i], i);
|
|
64
|
+
}
|
|
65
|
+
for (const [from, to] of Object.entries(aliases ?? {})) {
|
|
66
|
+
if (from.length !== 1) {
|
|
67
|
+
throw new InvalidCodecError(`alias '${from}' must be a single character`);
|
|
68
|
+
}
|
|
69
|
+
const value = this.charToIndex.get(to);
|
|
70
|
+
if (value === undefined) {
|
|
71
|
+
throw new InvalidCodecError(`alias '${from}' points at '${to}', which is not in the alphabet`);
|
|
72
|
+
}
|
|
73
|
+
if (alphabet.includes(from)) {
|
|
74
|
+
throw new InvalidCodecError(`alias '${from}' is already in the alphabet`);
|
|
75
|
+
}
|
|
76
|
+
accept(from, value);
|
|
77
|
+
}
|
|
78
|
+
this.codes = singleByteCodes(alphabet);
|
|
79
|
+
this.valueByCode = codeTable(this.charToIndex);
|
|
80
|
+
this.ignored = new Set(ignore ?? '');
|
|
81
|
+
for (const char of this.ignored) {
|
|
82
|
+
if (this.charToIndex.has(char)) {
|
|
83
|
+
throw new InvalidCodecError(`ignored character '${char}' is also decodable`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** Reads the first `length` bytes of `written` back as text. */
|
|
88
|
+
textFrom(written, length) {
|
|
89
|
+
return Buffer.from(written.buffer, written.byteOffset, length).toString('latin1');
|
|
90
|
+
}
|
|
91
|
+
/** Drops the characters this codec ignores, such as readability hyphens. */
|
|
92
|
+
strip(input) {
|
|
93
|
+
if (this.ignored.size === 0) {
|
|
94
|
+
return input;
|
|
95
|
+
}
|
|
96
|
+
let out = '';
|
|
97
|
+
for (let i = 0; i < input.length; i++) {
|
|
98
|
+
const char = input[i];
|
|
99
|
+
if (!this.ignored.has(char)) {
|
|
100
|
+
out += char;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
/** Returns the value of a character, or `undefined` if it is not in the alphabet. */
|
|
106
|
+
valueOf(char) {
|
|
107
|
+
return this.charToIndex.get(char);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns the value of the character at `index`, or `-1` if it is not
|
|
111
|
+
* decodable. This is the hot path of every decoder.
|
|
112
|
+
*/
|
|
113
|
+
valueAt(input, index) {
|
|
114
|
+
const table = this.valueByCode;
|
|
115
|
+
if (table === null) {
|
|
116
|
+
const value = this.charToIndex.get(input[index]);
|
|
117
|
+
return value === undefined ? -1 : value;
|
|
118
|
+
}
|
|
119
|
+
const code = input.charCodeAt(index);
|
|
120
|
+
return code > 0xff ? -1 : table[code];
|
|
121
|
+
}
|
|
122
|
+
/** Decodes with this codec, then re-encodes with `target`. */
|
|
123
|
+
convertTo(target, input) {
|
|
124
|
+
return target.encode(this.decode(input));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=codec-base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-base.js","sourceRoot":"","sources":["../../src/codec-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,4EAA4E;AAC5E,SAAS,eAAe,CAAC,QAAgB;IACvC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC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IACxC,CAAC;IAMD,8DAA8D;IAC9D,SAAS,CAAC,MAAc,EAAE,KAAa;QACrC,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC3C,CAAC;CACF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import type { BlockSpec, BufferLike, DecodeOptions } from './types.js';
|
|
3
|
+
import { BaseCodec } from './codec-base.js';
|
|
4
|
+
/**
|
|
5
|
+
* Regroups the bit stream into fixed-width chunks, as specified by RFC 4648.
|
|
6
|
+
*
|
|
7
|
+
* Each character carries `log2(alphabet.length)` bits, so the alphabet must
|
|
8
|
+
* hold 2, 4, 8, 16, 32, or 64 characters. Unlike a radix conversion the output
|
|
9
|
+
* length is a fixed function of the input length, which is what makes Base64
|
|
10
|
+
* and Base32 splittable and streamable.
|
|
11
|
+
*
|
|
12
|
+
* A trailing partial block leaves spare bits in the final character. RFC 4648
|
|
13
|
+
* §3.5 requires those bits to be zero for the encoding to be canonical; input
|
|
14
|
+
* that sets them is rejected unless the spec passes `strict: false`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { Codecs } from 'bufferbase';
|
|
19
|
+
*
|
|
20
|
+
* Codecs.base64.encode(Buffer.from('fo')); // 'Zm8='
|
|
21
|
+
* Codecs.base64.validate('Zm9='); // false — spare bits are not zero
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class BlockCodec extends BaseCodec {
|
|
25
|
+
readonly algorithm: "block";
|
|
26
|
+
/** Bits carried by one character. */
|
|
27
|
+
private readonly bitsPerChar;
|
|
28
|
+
/** Characters in a whole block. */
|
|
29
|
+
private readonly charsPerBlock;
|
|
30
|
+
private readonly padChar;
|
|
31
|
+
private readonly native;
|
|
32
|
+
constructor(spec: BlockSpec);
|
|
33
|
+
encode(input: BufferLike): string;
|
|
34
|
+
/** The character-by-character path, for an alphabet that is not single-byte. */
|
|
35
|
+
private encodeAsText;
|
|
36
|
+
decode(encoded: string, options?: DecodeOptions): Buffer;
|
|
37
|
+
validate(input: string): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Strips and checks the padding, then verifies that what remains can be the
|
|
40
|
+
* encoding of some byte sequence. Returns the data characters.
|
|
41
|
+
*
|
|
42
|
+
* Everything `decode` can reject other than `DecodeOptions.size` is checked
|
|
43
|
+
* here, so `validate` and `decode` always agree.
|
|
44
|
+
*/
|
|
45
|
+
private checkStructure;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=codec-block.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-block.d.ts","sourceRoot":"","sources":["../../src/codec-block.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACvE,OAAO,EAAE,SAAS,EAAY,MAAM,iBAAiB,CAAC;AAwCtD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,UAAW,SAAQ,SAAS;IACvC,QAAQ,CAAC,SAAS,EAAG,OAAO,CAAU;IAEtC,qCAAqC;IACrC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,mCAAmC;IACnC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;gBAElC,IAAI,EAAE,SAAS;IA0B3B,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;IA4CjC,gFAAgF;IAChF,OAAO,CAAC,YAAY;IA0BpB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM;IA4BxD,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAShC;;;;;;OAMG;IACH,OAAO,CAAC,cAAc;CA4CvB"}
|