bufferbase 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -18
- package/dist/cjs/bases.d.ts +82 -0
- package/dist/cjs/bases.d.ts.map +1 -0
- package/dist/cjs/bases.js +121 -0
- package/dist/cjs/bases.js.map +1 -0
- package/dist/cjs/bufferbase.d.ts +19 -36
- package/dist/cjs/bufferbase.d.ts.map +1 -1
- package/dist/cjs/bufferbase.js +27 -111
- package/dist/cjs/bufferbase.js.map +1 -1
- package/dist/cjs/codec.d.ts +124 -0
- package/dist/cjs/codec.d.ts.map +1 -0
- package/dist/cjs/codec.js +247 -0
- package/dist/cjs/codec.js.map +1 -0
- package/dist/cjs/codecs.d.ts +71 -0
- package/dist/cjs/codecs.d.ts.map +1 -0
- package/dist/cjs/codecs.js +92 -0
- package/dist/cjs/codecs.js.map +1 -0
- package/dist/cjs/errors.d.ts +76 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +92 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/example.js +20 -27
- package/dist/cjs/example.js.map +1 -1
- package/dist/cjs/functions.d.ts +62 -0
- package/dist/cjs/functions.d.ts.map +1 -0
- package/dist/cjs/functions.js +93 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.d.ts +8 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +25 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +6 -7
- package/dist/cjs/types.d.ts +107 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/bases.d.ts +82 -0
- package/dist/esm/bases.d.ts.map +1 -0
- package/dist/esm/bases.js +116 -0
- package/dist/esm/bases.js.map +1 -0
- package/dist/esm/bufferbase.d.ts +19 -36
- package/dist/esm/bufferbase.d.ts.map +1 -1
- package/dist/esm/bufferbase.js +25 -107
- package/dist/esm/bufferbase.js.map +1 -1
- package/dist/esm/codec.d.ts +124 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +242 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/codecs.d.ts +71 -0
- package/dist/esm/codecs.d.ts.map +1 -0
- package/dist/esm/codecs.js +89 -0
- package/dist/esm/codecs.js.map +1 -0
- package/dist/esm/errors.d.ts +76 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +86 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/example.js +19 -26
- package/dist/esm/example.js.map +1 -1
- package/dist/esm/functions.d.ts +62 -0
- package/dist/esm/functions.d.ts.map +1 -0
- package/dist/esm/functions.js +87 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.d.ts +8 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +12 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/package.json +6 -7
- package/dist/esm/types.d.ts +107 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +8 -9
- package/src/bases.ts +120 -0
- package/src/bufferbase.test.ts +44 -27
- package/src/bufferbase.ts +31 -106
- package/src/codec.test.ts +320 -0
- package/src/codec.ts +266 -0
- package/src/codecs.ts +118 -0
- package/src/errors.ts +87 -0
- package/src/example.ts +20 -30
- package/src/functions.ts +93 -0
- package/src/index.ts +21 -8
- package/src/types.ts +129 -0
- package/dist/cjs/bufferbase.d.mts +0 -60
- package/dist/cjs/bufferbase.d.mts.map +0 -1
- package/dist/cjs/bufferbase.mjs +0 -150
- package/dist/cjs/bufferbase.mjs.map +0 -1
- package/dist/cjs/example.d.mts +0 -2
- package/dist/cjs/example.d.mts.map +0 -1
- package/dist/cjs/example.mjs +0 -39
- package/dist/cjs/example.mjs.map +0 -1
- package/dist/cjs/index.d.mts +0 -2
- package/dist/cjs/index.d.mts.map +0 -1
- package/dist/cjs/index.mjs +0 -11
- package/dist/cjs/index.mjs.map +0 -1
- package/dist/esm/bufferbase.d.mts +0 -60
- package/dist/esm/bufferbase.d.mts.map +0 -1
- package/dist/esm/bufferbase.mjs +0 -141
- package/dist/esm/bufferbase.mjs.map +0 -1
- package/dist/esm/example.d.mts +0 -2
- package/dist/esm/example.d.mts.map +0 -1
- package/dist/esm/example.mjs +0 -37
- package/dist/esm/example.mjs.map +0 -1
- package/dist/esm/index.d.mts +0 -2
- package/dist/esm/index.d.mts.map +0 -1
- package/dist/esm/index.mjs +0 -2
- package/dist/esm/index.mjs.map +0 -1
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { UnknownBaseError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Character sets for common base encodings.
|
|
4
|
+
*
|
|
5
|
+
* Use these with `createCodec()` or the deprecated `BufferEncoder` class
|
|
6
|
+
* when you need direct access to character sets.
|
|
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
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const Chars = {
|
|
17
|
+
/** Base 10: 0-9 */
|
|
18
|
+
Decimal: '0123456789',
|
|
19
|
+
/** Hexadecimal: 0-9A-F */
|
|
20
|
+
Base16: '0123456789ABCDEF',
|
|
21
|
+
/** RFC 4648 Base32: A-Z2-7 */
|
|
22
|
+
Base32: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
|
|
23
|
+
/** Crockford's Base32: 0-9A-HJKMNP-TV-Z (no I, L, O, U) */
|
|
24
|
+
Base32Crockford: '0123456789ABCDEFGHJKMNPQRSTVWXYZ',
|
|
25
|
+
/** Alphanumeric: 0-9A-Z */
|
|
26
|
+
Base36: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
27
|
+
/** Letters only: A-Za-z */
|
|
28
|
+
Base52: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
|
29
|
+
/** Bitcoin alphabet: 1-9A-HJ-NP-Za-km-z (no 0, O, I, l) */
|
|
30
|
+
Base58: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
|
31
|
+
/** Standard Base64: A-Za-z0-9+/ */
|
|
32
|
+
Base64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
|
33
|
+
/** URL-safe Base64: A-Za-z0-9-_ */
|
|
34
|
+
Base64Url: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
|
|
35
|
+
/** XML token Base64: A-Za-z0-9._ */
|
|
36
|
+
Base64Xml: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._',
|
|
37
|
+
/** XML name Base64: A-Za-z0-9_: */
|
|
38
|
+
Base64XmlName: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:',
|
|
39
|
+
/** ASCII85 encoding */
|
|
40
|
+
Ascii85: '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstu',
|
|
41
|
+
/** Base85 encoding */
|
|
42
|
+
Base85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~',
|
|
43
|
+
/** ZeroMQ Z85 encoding */
|
|
44
|
+
Z85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#',
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Internal mapping from base names to character sets.
|
|
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.
|
|
72
|
+
*
|
|
73
|
+
* @param base - Base name (e.g., `'base58'`) or custom character set string
|
|
74
|
+
* @returns The character set string
|
|
75
|
+
* @throws {UnknownBaseError} If base name is not recognized and string length <= 15
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { resolveChars } from 'bufferbase';
|
|
80
|
+
*
|
|
81
|
+
* resolveChars('base58'); // Returns Bitcoin alphabet
|
|
82
|
+
* resolveChars('0123456789ABCDEF'); // Returns the string as-is (custom charset)
|
|
83
|
+
* resolveChars('invalid'); // Throws UnknownBaseError
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function resolveChars(base) {
|
|
87
|
+
const normalized = base.toLowerCase();
|
|
88
|
+
if (normalized in baseNameToChars) {
|
|
89
|
+
return baseNameToChars[normalized];
|
|
90
|
+
}
|
|
91
|
+
// If not a known base name, treat as custom character set
|
|
92
|
+
// (must be longer than 15 chars to distinguish from typos)
|
|
93
|
+
if (base.length > 15) {
|
|
94
|
+
return base;
|
|
95
|
+
}
|
|
96
|
+
throw new UnknownBaseError(base);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Checks if a string is a valid base name.
|
|
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
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export function isBaseName(value) {
|
|
114
|
+
return value.toLowerCase() in baseNameToChars;
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=bases.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bases.js","sourceRoot":"","sources":["../../src/bases.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,mBAAmB;IACnB,OAAO,EAAE,YAAY;IACrB,0BAA0B;IAC1B,MAAM,EAAE,kBAAkB;IAC1B,8BAA8B;IAC9B,MAAM,EAAE,kCAAkC;IAC1C,2DAA2D;IAC3D,eAAe,EAAE,kCAAkC;IACnD,2BAA2B;IAC3B,MAAM,EAAE,sCAAsC;IAC9C,2BAA2B;IAC3B,MAAM,EAAE,sDAAsD;IAC9D,2DAA2D;IAC3D,MAAM,EAAE,4DAA4D;IACpE,mCAAmC;IACnC,MAAM,EAAE,kEAAkE;IAC1E,mCAAmC;IACnC,SAAS,EAAE,kEAAkE;IAC7E,oCAAoC;IACpC,SAAS,EAAE,kEAAkE;IAC7E,mCAAmC;IACnC,aAAa,EAAE,kEAAkE;IACjF,uBAAuB;IACvB,OAAO,EAAE,uFAAuF;IAChG,sBAAsB;IACtB,MAAM,EAAE,uFAAuF;IAC/F,0BAA0B;IAC1B,GAAG,EAAE,uFAAuF;CACpF,CAAC;AAEX;;;GAGG;AACH,MAAM,eAAe,GAA6B;IAChD,OAAO,EAAE,KAAK,CAAC,OAAO;IACtB,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,GAAG,EAAE,KAAK,CAAC,MAAM;IACjB,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,eAAe,EAAE,KAAK,CAAC,eAAe;IACtC,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,SAAS,EAAE,KAAK,CAAC,SAAS;IAC1B,SAAS,EAAE,KAAK,CAAC,SAAS;IAC1B,aAAa,EAAE,KAAK,CAAC,aAAa;IAClC,OAAO,EAAE,KAAK,CAAC,OAAO;IACtB,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,GAAG,EAAE,KAAK,CAAC,GAAG;CACf,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,YAAY,CAAC,IAAuB;IAClD,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,EAAc,CAAC;IAClD,IAAI,UAAU,IAAI,eAAe,EAAE,CAAC;QAClC,OAAO,eAAe,CAAC,UAAU,CAAC,CAAC;IACrC,CAAC;IACD,0DAA0D;IAC1D,2DAA2D;IAC3D,IAAI,IAAI,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,KAAK,CAAC,WAAW,EAAE,IAAI,eAAe,CAAC;AAChD,CAAC"}
|
package/dist/esm/bufferbase.d.ts
CHANGED
|
@@ -1,60 +1,43 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
2
|
+
* @deprecated Use the new API instead:
|
|
3
|
+
* - `encode(buffer, 'base58')` or `base58.encode(buffer)`
|
|
4
|
+
* - `decode(encoded, 'base58')` or `base58.decode(encoded)`
|
|
5
|
+
* - `convert(input, 'base58', 'base64')` or `base58.convertTo(base64, input)`
|
|
6
|
+
* - `validate(input, 'base58')` or `base58.validate(input)`
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Base32Crockford: string;
|
|
10
|
-
Base36: string;
|
|
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
|
-
}
|
|
8
|
+
import { Buffer } from 'node:buffer';
|
|
9
|
+
import { InvalidCharacterError, BufferSizeError } from './errors.js';
|
|
10
|
+
export { Chars } from './bases.js';
|
|
11
|
+
export { InvalidCharacterError, BufferSizeError };
|
|
27
12
|
/**
|
|
28
|
-
*
|
|
13
|
+
* @deprecated Use `Codec` or `createCodec()` instead.
|
|
29
14
|
*/
|
|
30
15
|
export declare class BufferEncoder {
|
|
31
16
|
private baseChars;
|
|
17
|
+
private codec;
|
|
32
18
|
constructor(baseChars: string);
|
|
33
|
-
/**
|
|
34
|
-
* Encodes a buffer to a string.
|
|
35
|
-
*/
|
|
36
19
|
encode(buffer: Buffer): string;
|
|
37
|
-
/**
|
|
38
|
-
* Decodes a string to a buffer.
|
|
39
|
-
*/
|
|
40
20
|
decode(encoded: string, bufferSize?: number): Buffer;
|
|
41
21
|
}
|
|
42
22
|
/**
|
|
43
|
-
*
|
|
23
|
+
* @deprecated Use `convert()` function or `codec.convertTo()` instead.
|
|
44
24
|
*/
|
|
45
25
|
export declare class Converter {
|
|
46
26
|
decoder: BufferEncoder;
|
|
47
27
|
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
|
+
/**
|
|
32
|
+
* @deprecated Use `validate()` function or `codec.validate()` instead.
|
|
33
|
+
*/
|
|
54
34
|
export declare class Validator {
|
|
55
|
-
|
|
35
|
+
private codec;
|
|
56
36
|
constructor(inputBase: string);
|
|
57
37
|
validate(input: string): boolean;
|
|
58
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated Use `validate()` from the main module instead.
|
|
41
|
+
*/
|
|
59
42
|
export declare const validate: (input: string, base: string) => boolean;
|
|
60
43
|
//# 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;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGrE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,CAAC;AAElD;;GAEG;AACH,qBAAa,aAAa;IAGZ,OAAO,CAAC,SAAS;IAF7B,OAAO,CAAC,KAAK,CAAQ;gBAED,SAAS,EAAE,MAAM;IAIrC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAI9B,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM;CAGrD;AAED;;GAEG;AACH,qBAAa,SAAS;IACpB,OAAO,EAAE,aAAa,CAAC;IACvB,OAAO,EAAE,aAAa,CAAC;gBAEX,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;IAKjD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAG/B;AAED;;GAEG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,KAAK,CAAQ;gBAET,SAAS,EAAE,MAAM;IAI7B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;CAGjC;AAED;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,EAAE,MAAM,MAAM,KAAG,OAEtD,CAAC"}
|
package/dist/esm/bufferbase.js
CHANGED
|
@@ -1,140 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @deprecated Use the new API instead:
|
|
3
|
+
* - `encode(buffer, 'base58')` or `base58.encode(buffer)`
|
|
4
|
+
* - `decode(encoded, 'base58')` or `base58.decode(encoded)`
|
|
5
|
+
* - `convert(input, 'base58', 'base64')` or `base58.convertTo(base64, input)`
|
|
6
|
+
* - `validate(input, 'base58')` or `base58.validate(input)`
|
|
3
7
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
}
|
|
8
|
+
import { Buffer } from 'node:buffer';
|
|
9
|
+
import { Codec } from './codec.js';
|
|
10
|
+
import { InvalidCharacterError, BufferSizeError } from './errors.js';
|
|
11
|
+
// Re-export Chars for backward compatibility
|
|
12
|
+
export { Chars } from './bases.js';
|
|
13
|
+
// Re-export errors
|
|
14
|
+
export { InvalidCharacterError, BufferSizeError };
|
|
32
15
|
/**
|
|
33
|
-
*
|
|
16
|
+
* @deprecated Use `Codec` or `createCodec()` instead.
|
|
34
17
|
*/
|
|
35
18
|
export class BufferEncoder {
|
|
36
19
|
constructor(baseChars) {
|
|
37
20
|
this.baseChars = baseChars;
|
|
21
|
+
this.codec = new Codec(baseChars);
|
|
38
22
|
}
|
|
39
|
-
/**
|
|
40
|
-
* Encodes a buffer to a string.
|
|
41
|
-
*/
|
|
42
23
|
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('');
|
|
24
|
+
return this.codec.encode(buffer);
|
|
68
25
|
}
|
|
69
|
-
/**
|
|
70
|
-
* Decodes a string to a buffer.
|
|
71
|
-
*/
|
|
72
26
|
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;
|
|
27
|
+
return this.codec.decode(encoded, bufferSize !== undefined ? { size: bufferSize } : undefined);
|
|
104
28
|
}
|
|
105
29
|
}
|
|
106
30
|
/**
|
|
107
|
-
*
|
|
31
|
+
* @deprecated Use `convert()` function or `codec.convertTo()` instead.
|
|
108
32
|
*/
|
|
109
33
|
export class Converter {
|
|
110
34
|
constructor(inputBase, outputBase) {
|
|
111
35
|
this.decoder = new BufferEncoder(inputBase);
|
|
112
36
|
this.encoder = new BufferEncoder(outputBase);
|
|
113
37
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Converts a string from the input base to the output base.
|
|
116
|
-
*/
|
|
117
38
|
convert(input) {
|
|
118
39
|
return this.encoder.encode(this.decoder.decode(input));
|
|
119
40
|
}
|
|
120
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated Use `validate()` function or `codec.validate()` instead.
|
|
44
|
+
*/
|
|
121
45
|
export class Validator {
|
|
122
46
|
constructor(inputBase) {
|
|
123
|
-
this.
|
|
47
|
+
this.codec = new Codec(inputBase);
|
|
124
48
|
}
|
|
125
49
|
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
|
-
}
|
|
50
|
+
return this.codec.validate(input);
|
|
136
51
|
}
|
|
137
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Use `validate()` from the main module instead.
|
|
55
|
+
*/
|
|
138
56
|
export const validate = (input, base) => {
|
|
139
57
|
return new Validator(base).validate(input);
|
|
140
58
|
};
|
|
@@ -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;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAErE,6CAA6C;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,mBAAmB;AACnB,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,CAAC;AAElD;;GAEG;AACH,MAAM,OAAO,aAAa;IAGxB,YAAoB,SAAiB;QAAjB,cAAS,GAAT,SAAS,CAAQ;QACnC,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,MAAM,CAAC,MAAc;QACnB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,MAAM,CAAC,OAAe,EAAE,UAAmB;QACzC,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IACjG,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,SAAS;IAIpB,YAAY,SAAiB,EAAE,UAAkB;QAC/C,IAAI,CAAC,OAAO,GAAG,IAAI,aAAa,CAAC,SAAS,CAAC,CAAC;QAC5C,IAAI,CAAC,OAAO,GAAG,IAAI,aAAa,CAAC,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,CAAC,KAAa;QACnB,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACzD,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,SAAS;IAGpB,YAAY,SAAiB;QAC3B,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,QAAQ,CAAC,KAAa;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAE,IAAY,EAAW,EAAE;IAC/D,OAAO,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC7C,CAAC,CAAC"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import type { ICodec, DecodeOptions, BufferLike } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Codec for encoding and decoding buffers to/from a specific base.
|
|
5
|
+
*
|
|
6
|
+
* Use pre-defined codecs from `Codecs` for common encodings,
|
|
7
|
+
* or create custom codecs with `createCodec()`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Codec, Codecs, createCodec } from 'bufferbase';
|
|
12
|
+
*
|
|
13
|
+
* // Use pre-defined codec
|
|
14
|
+
* const encoded = Codecs.base58.encode(Buffer.from('Hello'));
|
|
15
|
+
*
|
|
16
|
+
* // Create custom codec
|
|
17
|
+
* const binary = createCodec('01');
|
|
18
|
+
* const bits = binary.encode(Buffer.from([5])); // '101'
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class Codec implements ICodec {
|
|
22
|
+
/** Character set used for encoding. */
|
|
23
|
+
readonly chars: string;
|
|
24
|
+
/** @internal */
|
|
25
|
+
private readonly charToIndex;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new codec with the specified character set.
|
|
28
|
+
* @param chars - The character set to use for encoding/decoding
|
|
29
|
+
*/
|
|
30
|
+
constructor(chars: string);
|
|
31
|
+
/**
|
|
32
|
+
* Encodes a buffer to a base-encoded string.
|
|
33
|
+
*
|
|
34
|
+
* Leading zero bytes in the buffer are preserved as the first character
|
|
35
|
+
* of the encoding alphabet (e.g., '1' for Base58).
|
|
36
|
+
*
|
|
37
|
+
* @param input - The buffer or Uint8Array to encode
|
|
38
|
+
* @returns Base-encoded string
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const codec = Codecs.base58;
|
|
43
|
+
* codec.encode(Buffer.from('Hello')); // '9Ajdvzr'
|
|
44
|
+
* codec.encode(new Uint8Array([0, 0, 1])); // '112' (preserves leading zeros)
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
encode(input: BufferLike): string;
|
|
48
|
+
/**
|
|
49
|
+
* Decodes a base-encoded string to a buffer.
|
|
50
|
+
*
|
|
51
|
+
* Leading characters that represent zero (first char of alphabet)
|
|
52
|
+
* are converted to leading zero bytes in the result.
|
|
53
|
+
*
|
|
54
|
+
* @param encoded - The string to decode
|
|
55
|
+
* @param options - Decode options
|
|
56
|
+
* @returns Decoded buffer
|
|
57
|
+
* @throws {InvalidCharacterError} If the string contains invalid characters
|
|
58
|
+
* @throws {BufferSizeError} If the result exceeds the specified size
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const codec = Codecs.base58;
|
|
63
|
+
* codec.decode('9Ajdvzr'); // Buffer('Hello')
|
|
64
|
+
* codec.decode('9Ajdvzr', { size: 32 }); // Pads to 32 bytes
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
decode(encoded: string, options?: DecodeOptions): Buffer;
|
|
68
|
+
/**
|
|
69
|
+
* Validates if a string contains only valid characters for this base.
|
|
70
|
+
*
|
|
71
|
+
* This is a fast check that doesn't perform full decoding.
|
|
72
|
+
*
|
|
73
|
+
* @param input - The string to validate
|
|
74
|
+
* @returns `true` if all characters are valid, `false` otherwise
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* Codecs.base58.validate('9Ajdvzr'); // true
|
|
79
|
+
* Codecs.base58.validate('0invalid'); // false ('0' not in Base58)
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
validate(input: string): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Converts a string from this base to another base.
|
|
85
|
+
*
|
|
86
|
+
* Internally decodes to a buffer, then encodes with the target codec.
|
|
87
|
+
*
|
|
88
|
+
* @param target - Target codec to convert to
|
|
89
|
+
* @param input - The string to convert
|
|
90
|
+
* @returns String in the target base encoding
|
|
91
|
+
* @throws {InvalidCharacterError} If the input contains invalid characters
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const base58str = '9Ajdvzr';
|
|
96
|
+
* const base64str = Codecs.base58.convertTo(Codecs.base64url, base58str);
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
convertTo(target: ICodec, input: string): string;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Creates a new codec with the specified character set.
|
|
103
|
+
*
|
|
104
|
+
* Use this to create codecs for custom base encodings.
|
|
105
|
+
* The base is determined by the length of the character set.
|
|
106
|
+
*
|
|
107
|
+
* @param chars - The character set to use for encoding/decoding
|
|
108
|
+
* @returns A new Codec instance
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* import { createCodec } from 'bufferbase';
|
|
113
|
+
*
|
|
114
|
+
* // Binary codec (base 2)
|
|
115
|
+
* const binary = createCodec('01');
|
|
116
|
+
* binary.encode(Buffer.from([5])); // '101'
|
|
117
|
+
*
|
|
118
|
+
* // Octal codec (base 8)
|
|
119
|
+
* const octal = createCodec('01234567');
|
|
120
|
+
* octal.encode(Buffer.from([255])); // '377'
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare function createCodec(chars: string): Codec;
|
|
124
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAcpE;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,KAAM,YAAW,MAAM;IAClC,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,gBAAgB;IAChB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsB;IAElD;;;OAGG;gBACS,KAAK,EAAE,MAAM;IAQzB;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;IA6CjC;;;;;;;;;;;;;;;;;;OAkBG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM;IAgExD;;;;;;;;;;;;;OAaG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAShC;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;CAIjD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,CAEhD"}
|