bufferbase 1.3.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 +186 -17
- package/dist/cjs/bases.d.ts +189 -0
- package/dist/cjs/bases.d.ts.map +1 -0
- package/dist/cjs/bases.js +134 -0
- package/dist/cjs/bases.js.map +1 -0
- package/dist/cjs/bufferbase.d.ts +22 -45
- package/dist/cjs/bufferbase.d.ts.map +1 -1
- package/dist/cjs/bufferbase.js +26 -120
- 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 +38 -0
- package/dist/cjs/codec.d.ts.map +1 -0
- package/dist/cjs/codec.js +69 -0
- package/dist/cjs/codec.js.map +1 -0
- package/dist/cjs/codecs.d.ts +20 -0
- package/dist/cjs/codecs.d.ts.map +1 -0
- package/dist/cjs/codecs.js +19 -0
- package/dist/cjs/codecs.js.map +1 -0
- package/dist/cjs/errors.d.ts +93 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +130 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/example.js +49 -33
- package/dist/cjs/example.js.map +1 -1
- package/dist/cjs/functions.d.ts +58 -0
- package/dist/cjs/functions.d.ts.map +1 -0
- package/dist/cjs/functions.js +71 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.d.ts +10 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +35 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +17 -12
- package/dist/cjs/types.d.ts +144 -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 +189 -0
- package/dist/esm/bases.d.ts.map +1 -0
- package/dist/esm/bases.js +129 -0
- package/dist/esm/bases.js.map +1 -0
- package/dist/esm/bufferbase.d.ts +22 -45
- package/dist/esm/bufferbase.d.ts.map +1 -1
- package/dist/esm/bufferbase.js +21 -115
- 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 +38 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +60 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/codecs.d.ts +20 -0
- package/dist/esm/codecs.d.ts.map +1 -0
- package/dist/esm/codecs.js +16 -0
- package/dist/esm/codecs.js.map +1 -0
- package/dist/esm/errors.d.ts +93 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +119 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/example.js +48 -32
- package/dist/esm/example.js.map +1 -1
- package/dist/esm/functions.d.ts +58 -0
- package/dist/esm/functions.d.ts.map +1 -0
- package/dist/esm/functions.js +65 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.d.ts +10 -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 +17 -12
- package/dist/esm/types.d.ts +144 -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 +19 -14
- package/src/algorithms.test.ts +442 -0
- package/src/bases.ts +142 -0
- package/src/bufferbase.test.ts +44 -27
- package/src/bufferbase.ts +32 -117
- 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 +328 -0
- package/src/codec.ts +67 -0
- package/src/codecs.ts +24 -0
- package/src/errors.ts +127 -0
- package/src/example.ts +49 -34
- package/src/functions.ts +73 -0
- package/src/index.ts +39 -6
- package/src/types.ts +156 -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
package/dist/esm/bufferbase.d.ts
CHANGED
|
@@ -1,60 +1,37 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Base52: string;
|
|
12
|
-
Base58: string;
|
|
13
|
-
Base64_STD: string;
|
|
14
|
-
Base64_URL_SAFE: string;
|
|
15
|
-
Base64_XML_NMTOKEN: string;
|
|
16
|
-
Base64_XML_NAME: string;
|
|
17
|
-
Ascii85: string;
|
|
18
|
-
Base85: string;
|
|
19
|
-
Z85: string;
|
|
20
|
-
};
|
|
21
|
-
export declare class InvalidCharacterError extends Error {
|
|
22
|
-
message: string;
|
|
23
|
-
}
|
|
24
|
-
export declare class BufferSizeError extends Error {
|
|
25
|
-
message: string;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Encodes and decodes buffers to and from a base.
|
|
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.
|
|
29
10
|
*/
|
|
11
|
+
import type { Buffer } from 'node:buffer';
|
|
12
|
+
import type { BufferLike } from './types.js';
|
|
13
|
+
/** @deprecated Import `Chars` from the package root. */
|
|
14
|
+
export { Chars } from './bases.js';
|
|
15
|
+
/** @deprecated Import these from the package root. */
|
|
16
|
+
export { BufferSizeError, InvalidCharacterError } from './errors.js';
|
|
17
|
+
/** @deprecated Use `createCodec(alphabet)`. */
|
|
30
18
|
export declare class BufferEncoder {
|
|
31
|
-
private
|
|
19
|
+
private readonly codec;
|
|
32
20
|
constructor(baseChars: string);
|
|
33
|
-
|
|
34
|
-
* Encodes a buffer to a string.
|
|
35
|
-
*/
|
|
36
|
-
encode(buffer: Buffer): string;
|
|
37
|
-
/**
|
|
38
|
-
* Decodes a string to a buffer.
|
|
39
|
-
*/
|
|
21
|
+
encode(buffer: BufferLike): string;
|
|
40
22
|
decode(encoded: string, bufferSize?: number): Buffer;
|
|
41
23
|
}
|
|
42
|
-
/**
|
|
43
|
-
* Creates a converter function that can convert between two bases.
|
|
44
|
-
*/
|
|
24
|
+
/** @deprecated Use `convert()` or `codec.convertTo()`. */
|
|
45
25
|
export declare class Converter {
|
|
46
|
-
decoder: BufferEncoder;
|
|
47
|
-
encoder: BufferEncoder;
|
|
26
|
+
readonly decoder: BufferEncoder;
|
|
27
|
+
readonly encoder: BufferEncoder;
|
|
48
28
|
constructor(inputBase: string, outputBase: string);
|
|
49
|
-
/**
|
|
50
|
-
* Converts a string from the input base to the output base.
|
|
51
|
-
*/
|
|
52
29
|
convert(input: string): string;
|
|
53
30
|
}
|
|
31
|
+
/** @deprecated Use `validate()` or `codec.validate()`. */
|
|
54
32
|
export declare class Validator {
|
|
55
|
-
|
|
33
|
+
private readonly codec;
|
|
56
34
|
constructor(inputBase: string);
|
|
57
35
|
validate(input: string): boolean;
|
|
58
36
|
}
|
|
59
|
-
export declare const validate: (input: string, base: string) => boolean;
|
|
60
37
|
//# sourceMappingURL=bufferbase.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bufferbase.d.ts","sourceRoot":"","sources":["../../src/bufferbase.ts"],"names":[],"mappings":"
|
|
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,141 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
Base52: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
|
11
|
-
Base58: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
|
12
|
-
Base64_STD: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
|
13
|
-
Base64_URL_SAFE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
|
|
14
|
-
Base64_XML_NMTOKEN: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._',
|
|
15
|
-
Base64_XML_NAME: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:',
|
|
16
|
-
Ascii85: '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstu',
|
|
17
|
-
Base85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~',
|
|
18
|
-
Z85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#',
|
|
19
|
-
};
|
|
20
|
-
export class InvalidCharacterError extends Error {
|
|
21
|
-
constructor() {
|
|
22
|
-
super(...arguments);
|
|
23
|
-
this.message = 'Invalid character found';
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export class BufferSizeError extends Error {
|
|
27
|
-
constructor() {
|
|
28
|
-
super(...arguments);
|
|
29
|
-
this.message = 'Buffer size exceeds specified size';
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Encodes and decodes buffers to and from a base.
|
|
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.
|
|
34
10
|
*/
|
|
11
|
+
import { RadixCodec } from './codec-radix.js';
|
|
12
|
+
/** @deprecated Import `Chars` from the package root. */
|
|
13
|
+
export { Chars } from './bases.js';
|
|
14
|
+
/** @deprecated Import these from the package root. */
|
|
15
|
+
export { BufferSizeError, InvalidCharacterError } from './errors.js';
|
|
16
|
+
/** @deprecated Use `createCodec(alphabet)`. */
|
|
35
17
|
export class BufferEncoder {
|
|
36
18
|
constructor(baseChars) {
|
|
37
|
-
this.
|
|
19
|
+
this.codec = new RadixCodec({ alphabet: baseChars, algorithm: 'radix' });
|
|
38
20
|
}
|
|
39
|
-
/**
|
|
40
|
-
* Encodes a buffer to a string.
|
|
41
|
-
*/
|
|
42
21
|
encode(buffer) {
|
|
43
|
-
|
|
44
|
-
for (const byte of buffer) {
|
|
45
|
-
let carry = byte;
|
|
46
|
-
for (let j = 0; j < result.length; j++) {
|
|
47
|
-
carry += result[j] * 256;
|
|
48
|
-
result[j] = carry % this.baseChars.length;
|
|
49
|
-
carry = Math.floor(carry / this.baseChars.length);
|
|
50
|
-
}
|
|
51
|
-
while (carry > 0) {
|
|
52
|
-
result.push(carry % this.baseChars.length);
|
|
53
|
-
carry = Math.floor(carry / this.baseChars.length);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
for (const byte of buffer) {
|
|
57
|
-
if (byte === 0) {
|
|
58
|
-
result.push(0);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return result
|
|
65
|
-
.reverse()
|
|
66
|
-
.map((index) => this.baseChars[index])
|
|
67
|
-
.join('');
|
|
22
|
+
return this.codec.encode(buffer);
|
|
68
23
|
}
|
|
69
|
-
/**
|
|
70
|
-
* Decodes a string to a buffer.
|
|
71
|
-
*/
|
|
72
24
|
decode(encoded, bufferSize) {
|
|
73
|
-
|
|
74
|
-
for (const char of encoded) {
|
|
75
|
-
const value = this.baseChars.indexOf(char);
|
|
76
|
-
if (value === -1)
|
|
77
|
-
throw new InvalidCharacterError();
|
|
78
|
-
let carry = value;
|
|
79
|
-
let tempResult = Buffer.alloc(result.length);
|
|
80
|
-
let i;
|
|
81
|
-
for (i = 0; i < result.length; i++) {
|
|
82
|
-
carry += result[i] * this.baseChars.length;
|
|
83
|
-
tempResult[i] = carry % 256;
|
|
84
|
-
carry = Math.floor(carry / 256);
|
|
85
|
-
}
|
|
86
|
-
while (carry > 0) {
|
|
87
|
-
tempResult = Buffer.concat([tempResult, Buffer.from([carry % 256])]);
|
|
88
|
-
carry = Math.floor(carry / 256);
|
|
89
|
-
}
|
|
90
|
-
result = tempResult;
|
|
91
|
-
}
|
|
92
|
-
// buffer size
|
|
93
|
-
if (bufferSize && result.length < bufferSize) {
|
|
94
|
-
result = Buffer.concat([
|
|
95
|
-
result,
|
|
96
|
-
Buffer.from(Array(bufferSize - result.length).fill(0)),
|
|
97
|
-
]);
|
|
98
|
-
}
|
|
99
|
-
if (bufferSize && result.length > bufferSize) {
|
|
100
|
-
throw new BufferSizeError();
|
|
101
|
-
}
|
|
102
|
-
result.reverse();
|
|
103
|
-
return result;
|
|
25
|
+
return this.codec.decode(encoded, bufferSize === undefined ? undefined : { size: bufferSize });
|
|
104
26
|
}
|
|
105
27
|
}
|
|
106
|
-
/**
|
|
107
|
-
* Creates a converter function that can convert between two bases.
|
|
108
|
-
*/
|
|
28
|
+
/** @deprecated Use `convert()` or `codec.convertTo()`. */
|
|
109
29
|
export class Converter {
|
|
110
30
|
constructor(inputBase, outputBase) {
|
|
111
31
|
this.decoder = new BufferEncoder(inputBase);
|
|
112
32
|
this.encoder = new BufferEncoder(outputBase);
|
|
113
33
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Converts a string from the input base to the output base.
|
|
116
|
-
*/
|
|
117
34
|
convert(input) {
|
|
118
35
|
return this.encoder.encode(this.decoder.decode(input));
|
|
119
36
|
}
|
|
120
37
|
}
|
|
38
|
+
/** @deprecated Use `validate()` or `codec.validate()`. */
|
|
121
39
|
export class Validator {
|
|
122
40
|
constructor(inputBase) {
|
|
123
|
-
this.
|
|
41
|
+
this.codec = new RadixCodec({ alphabet: inputBase, algorithm: 'radix' });
|
|
124
42
|
}
|
|
125
43
|
validate(input) {
|
|
126
|
-
|
|
127
|
-
this.decorder.decode(input);
|
|
128
|
-
return true;
|
|
129
|
-
}
|
|
130
|
-
catch (e) {
|
|
131
|
-
if (e instanceof InvalidCharacterError) {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
throw e;
|
|
135
|
-
}
|
|
44
|
+
return this.codec.validate(input);
|
|
136
45
|
}
|
|
137
46
|
}
|
|
138
|
-
export const validate = (input, base) => {
|
|
139
|
-
return new Validator(base).validate(input);
|
|
140
|
-
};
|
|
141
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,CAAC;AAErE,+CAA+C;AAC/C,MAAM,OAAO,aAAa;IAGxB,YAAY,SAAiB;QAC3B,I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
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,CAAC;QACpC,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;YAChB,OAAO,IAAI,CAA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AAO,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"}
|