bufferbase 1.2.3 → 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 +43 -0
- package/dist/cjs/bufferbase.d.ts.map +1 -0
- package/dist/cjs/bufferbase.js +66 -0
- package/dist/cjs/bufferbase.js.map +1 -0
- 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/{example.d.ts.map → cjs/example.d.ts.map} +1 -1
- package/dist/cjs/example.js +32 -0
- package/dist/cjs/example.js.map +1 -0
- 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 +9 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +32 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +50 -0
- 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 +43 -0
- package/dist/esm/bufferbase.d.ts.map +1 -0
- package/dist/esm/bufferbase.js +59 -0
- package/dist/esm/bufferbase.js.map +1 -0
- 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.d.ts +2 -0
- package/dist/esm/example.d.ts.map +1 -0
- package/dist/esm/example.js +30 -0
- package/dist/esm/example.js.map +1 -0
- 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 +9 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +13 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +50 -0
- 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 +26 -11
- 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/bufferbase.d.ts +0 -60
- package/dist/bufferbase.d.ts.map +0 -1
- package/dist/bufferbase.js +0 -141
- package/dist/bufferbase.js.map +0 -1
- package/dist/example.js +0 -37
- package/dist/example.js.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/dist/sample.d.ts +0 -2
- package/dist/sample.d.ts.map +0 -1
- package/dist/sample.js +0 -37
- package/dist/sample.js.map +0 -1
- /package/dist/{example.d.ts → cjs/example.d.ts} +0 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { BaseName, DecodeOptions, BufferLike } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Encodes a buffer to a base-encoded string.
|
|
4
|
+
*
|
|
5
|
+
* @param buffer - Buffer or Uint8Array to encode
|
|
6
|
+
* @param base - Base name (e.g., 'base58') or custom character set
|
|
7
|
+
* @returns Encoded string
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Node.js Buffer
|
|
12
|
+
* const encoded = encode(Buffer.from('Hello'), 'base58');
|
|
13
|
+
*
|
|
14
|
+
* // Browser Uint8Array
|
|
15
|
+
* const encoded = encode(new Uint8Array([72, 101, 108, 108, 111]), 'base58');
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function encode(buffer: BufferLike, base: BaseName | string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Decodes a base-encoded string to a buffer.
|
|
21
|
+
*
|
|
22
|
+
* @param encoded - String to decode
|
|
23
|
+
* @param base - Base name (e.g., 'base58') or custom character set
|
|
24
|
+
* @param options - Decode options (e.g., { size: 32 })
|
|
25
|
+
* @returns Decoded buffer
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const buffer = decode('9Ajdvz', 'base58');
|
|
30
|
+
* const fixedSize = decode('9Ajdvz', 'base58', { size: 32 });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function decode(encoded: string, base: BaseName | string, options?: DecodeOptions): Buffer;
|
|
34
|
+
/**
|
|
35
|
+
* Converts a string from one base to another.
|
|
36
|
+
*
|
|
37
|
+
* @param input - String to convert
|
|
38
|
+
* @param from - Source base name or character set
|
|
39
|
+
* @param to - Target base name or character set
|
|
40
|
+
* @returns Converted string
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const base64 = convert('9Ajdvz', 'base58', 'base64url');
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function convert(input: string, from: BaseName | string, to: BaseName | string): string;
|
|
48
|
+
/**
|
|
49
|
+
* Validates if a string contains only valid characters for the specified base.
|
|
50
|
+
*
|
|
51
|
+
* @param input - String to validate
|
|
52
|
+
* @param base - Base name or character set
|
|
53
|
+
* @returns true if valid, false otherwise
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* validate('9Ajdvz', 'base58'); // true
|
|
58
|
+
* validate('0Ajdvz', 'base58'); // false ('0' is not in base58)
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function validate(input: string, base: BaseName | string): boolean;
|
|
62
|
+
//# sourceMappingURL=functions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAoBtE;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,CAE1E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAEhG;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,CAI7F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,OAAO,CAExE"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encode = encode;
|
|
4
|
+
exports.decode = decode;
|
|
5
|
+
exports.convert = convert;
|
|
6
|
+
exports.validate = validate;
|
|
7
|
+
const codec_js_1 = require("./codec.js");
|
|
8
|
+
const bases_js_1 = require("./bases.js");
|
|
9
|
+
// Cache for codec instances
|
|
10
|
+
const codecCache = new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Gets or creates a codec for the specified base.
|
|
13
|
+
*/
|
|
14
|
+
function getCodec(base) {
|
|
15
|
+
const chars = (0, bases_js_1.resolveChars)(base);
|
|
16
|
+
let codec = codecCache.get(chars);
|
|
17
|
+
if (!codec) {
|
|
18
|
+
codec = new codec_js_1.Codec(chars);
|
|
19
|
+
codecCache.set(chars, codec);
|
|
20
|
+
}
|
|
21
|
+
return codec;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Encodes a buffer to a base-encoded string.
|
|
25
|
+
*
|
|
26
|
+
* @param buffer - Buffer or Uint8Array to encode
|
|
27
|
+
* @param base - Base name (e.g., 'base58') or custom character set
|
|
28
|
+
* @returns Encoded string
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // Node.js Buffer
|
|
33
|
+
* const encoded = encode(Buffer.from('Hello'), 'base58');
|
|
34
|
+
*
|
|
35
|
+
* // Browser Uint8Array
|
|
36
|
+
* const encoded = encode(new Uint8Array([72, 101, 108, 108, 111]), 'base58');
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function encode(buffer, base) {
|
|
40
|
+
return getCodec(base).encode(buffer);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Decodes a base-encoded string to a buffer.
|
|
44
|
+
*
|
|
45
|
+
* @param encoded - String to decode
|
|
46
|
+
* @param base - Base name (e.g., 'base58') or custom character set
|
|
47
|
+
* @param options - Decode options (e.g., { size: 32 })
|
|
48
|
+
* @returns Decoded buffer
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const buffer = decode('9Ajdvz', 'base58');
|
|
53
|
+
* const fixedSize = decode('9Ajdvz', 'base58', { size: 32 });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
function decode(encoded, base, options) {
|
|
57
|
+
return getCodec(base).decode(encoded, options);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts a string from one base to another.
|
|
61
|
+
*
|
|
62
|
+
* @param input - String to convert
|
|
63
|
+
* @param from - Source base name or character set
|
|
64
|
+
* @param to - Target base name or character set
|
|
65
|
+
* @returns Converted string
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const base64 = convert('9Ajdvz', 'base58', 'base64url');
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
function convert(input, from, to) {
|
|
73
|
+
const fromCodec = getCodec(from);
|
|
74
|
+
const toCodec = getCodec(to);
|
|
75
|
+
return fromCodec.convertTo(toCodec, input);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Validates if a string contains only valid characters for the specified base.
|
|
79
|
+
*
|
|
80
|
+
* @param input - String to validate
|
|
81
|
+
* @param base - Base name or character set
|
|
82
|
+
* @returns true if valid, false otherwise
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* validate('9Ajdvz', 'base58'); // true
|
|
87
|
+
* validate('0Ajdvz', 'base58'); // false ('0' is not in base58)
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
function validate(input, base) {
|
|
91
|
+
return getCodec(base).validate(input);
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=functions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":";;AAoCA,wBAEC;AAgBD,wBAEC;AAeD,0BAIC;AAeD,4BAEC;AA3FD,yCAAmC;AACnC,yCAA0C;AAE1C,4BAA4B;AAC5B,MAAM,UAAU,GAAG,IAAI,GAAG,EAAiB,CAAC;AAE5C;;GAEG;AACH,SAAS,QAAQ,CAAC,IAAuB;IACvC,MAAM,KAAK,GAAG,IAAA,uBAAY,EAAC,IAAI,CAAC,CAAC;IACjC,IAAI,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,KAAK,GAAG,IAAI,gBAAK,CAAC,KAAK,CAAC,CAAC;QACzB,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,MAAM,CAAC,MAAkB,EAAE,IAAuB;IAChE,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACvC,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAgB,MAAM,CAAC,OAAe,EAAE,IAAuB,EAAE,OAAuB;IACtF,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACjD,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,OAAO,CAAC,KAAa,EAAE,IAAuB,EAAE,EAAqB;IACnF,MAAM,SAAS,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,OAAO,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC7B,OAAO,SAAS,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,QAAQ,CAAC,KAAa,EAAE,IAAuB;IAC7D,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { BaseName, DecodeOptions, ICodec, BufferLike } from './types.js';
|
|
2
|
+
export type { CodecName } from './codecs.js';
|
|
3
|
+
export { InvalidCharacterError, BufferSizeError, UnknownBaseError } from './errors.js';
|
|
4
|
+
export { Codec, createCodec } from './codec.js';
|
|
5
|
+
export { Chars, resolveChars, isBaseName } from './bases.js';
|
|
6
|
+
export { encode, decode, convert, validate } from './functions.js';
|
|
7
|
+
export { Codecs } from './codecs.js';
|
|
8
|
+
export { BufferEncoder, Converter, Validator } from './bufferbase.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC9E,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAG7C,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAGvF,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGhD,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG7D,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAGnE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Validator = exports.Converter = exports.BufferEncoder = exports.Codecs = exports.validate = exports.convert = exports.decode = exports.encode = exports.isBaseName = exports.resolveChars = exports.Chars = exports.createCodec = exports.Codec = exports.UnknownBaseError = exports.BufferSizeError = exports.InvalidCharacterError = void 0;
|
|
4
|
+
// Errors
|
|
5
|
+
var errors_js_1 = require("./errors.js");
|
|
6
|
+
Object.defineProperty(exports, "InvalidCharacterError", { enumerable: true, get: function () { return errors_js_1.InvalidCharacterError; } });
|
|
7
|
+
Object.defineProperty(exports, "BufferSizeError", { enumerable: true, get: function () { return errors_js_1.BufferSizeError; } });
|
|
8
|
+
Object.defineProperty(exports, "UnknownBaseError", { enumerable: true, get: function () { return errors_js_1.UnknownBaseError; } });
|
|
9
|
+
// Codec class and factory
|
|
10
|
+
var codec_js_1 = require("./codec.js");
|
|
11
|
+
Object.defineProperty(exports, "Codec", { enumerable: true, get: function () { return codec_js_1.Codec; } });
|
|
12
|
+
Object.defineProperty(exports, "createCodec", { enumerable: true, get: function () { return codec_js_1.createCodec; } });
|
|
13
|
+
// Character sets and utilities
|
|
14
|
+
var bases_js_1 = require("./bases.js");
|
|
15
|
+
Object.defineProperty(exports, "Chars", { enumerable: true, get: function () { return bases_js_1.Chars; } });
|
|
16
|
+
Object.defineProperty(exports, "resolveChars", { enumerable: true, get: function () { return bases_js_1.resolveChars; } });
|
|
17
|
+
Object.defineProperty(exports, "isBaseName", { enumerable: true, get: function () { return bases_js_1.isBaseName; } });
|
|
18
|
+
// Simple function API
|
|
19
|
+
var functions_js_1 = require("./functions.js");
|
|
20
|
+
Object.defineProperty(exports, "encode", { enumerable: true, get: function () { return functions_js_1.encode; } });
|
|
21
|
+
Object.defineProperty(exports, "decode", { enumerable: true, get: function () { return functions_js_1.decode; } });
|
|
22
|
+
Object.defineProperty(exports, "convert", { enumerable: true, get: function () { return functions_js_1.convert; } });
|
|
23
|
+
Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return functions_js_1.validate; } });
|
|
24
|
+
// Pre-defined codecs (namespaced)
|
|
25
|
+
var codecs_js_1 = require("./codecs.js");
|
|
26
|
+
Object.defineProperty(exports, "Codecs", { enumerable: true, get: function () { return codecs_js_1.Codecs; } });
|
|
27
|
+
// Deprecated exports for backward compatibility
|
|
28
|
+
var bufferbase_js_1 = require("./bufferbase.js");
|
|
29
|
+
Object.defineProperty(exports, "BufferEncoder", { enumerable: true, get: function () { return bufferbase_js_1.BufferEncoder; } });
|
|
30
|
+
Object.defineProperty(exports, "Converter", { enumerable: true, get: function () { return bufferbase_js_1.Converter; } });
|
|
31
|
+
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return bufferbase_js_1.Validator; } });
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAIA,SAAS;AACT,yCAAuF;AAA9E,kHAAA,qBAAqB,OAAA;AAAE,4GAAA,eAAe,OAAA;AAAE,6GAAA,gBAAgB,OAAA;AAEjE,0BAA0B;AAC1B,uCAAgD;AAAvC,iGAAA,KAAK,OAAA;AAAE,uGAAA,WAAW,OAAA;AAE3B,+BAA+B;AAC/B,uCAA6D;AAApD,iGAAA,KAAK,OAAA;AAAE,wGAAA,YAAY,OAAA;AAAE,sGAAA,UAAU,OAAA;AAExC,sBAAsB;AACtB,+CAAmE;AAA1D,sGAAA,MAAM,OAAA;AAAE,sGAAA,MAAM,OAAA;AAAE,uGAAA,OAAO,OAAA;AAAE,wGAAA,QAAQ,OAAA;AAE1C,kCAAkC;AAClC,yCAAqC;AAA5B,mGAAA,MAAM,OAAA;AAEf,gDAAgD;AAChD,iDAAsE;AAA7D,8GAAA,aAAa,OAAA;AAAE,0GAAA,SAAS,OAAA;AAAE,0GAAA,SAAS,OAAA"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bufferbase",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Buffer-to-BaseN Encoder, Decoder, Converter, and Validator",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "bun run build:esm && bun run build:cjs && tsconfig-to-dual-package",
|
|
9
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
10
|
+
"build:esm": "tsc -p tsconfig.json",
|
|
11
|
+
"prepublishOnly": "bun run build",
|
|
12
|
+
"example": "bun src/example.ts",
|
|
13
|
+
"test": "bun test"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/bun": "^1.1.0",
|
|
17
|
+
"@types/node": "^20.11.5",
|
|
18
|
+
"tsconfig-to-dual-package": "^1.2.0",
|
|
19
|
+
"typescript": "^5.3.3"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/misebox/bufferbase.git"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"number",
|
|
31
|
+
"encode",
|
|
32
|
+
"convert",
|
|
33
|
+
"decimal",
|
|
34
|
+
"base",
|
|
35
|
+
"base16",
|
|
36
|
+
"base32",
|
|
37
|
+
"crockford",
|
|
38
|
+
"base36",
|
|
39
|
+
"base58",
|
|
40
|
+
"base64",
|
|
41
|
+
"Ascii85",
|
|
42
|
+
"base85"
|
|
43
|
+
],
|
|
44
|
+
"author": "misebox",
|
|
45
|
+
"license": "ISC",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/misebox/bufferbase/issues"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/misebox/bufferbase#readme"
|
|
50
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/**
|
|
3
|
+
* Input type accepted by encode functions.
|
|
4
|
+
*
|
|
5
|
+
* Supports both Node.js `Buffer` and standard `Uint8Array` for browser compatibility.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Node.js Buffer
|
|
10
|
+
* encode(Buffer.from('Hello'), 'base58');
|
|
11
|
+
*
|
|
12
|
+
* // Browser Uint8Array
|
|
13
|
+
* encode(new Uint8Array([72, 101, 108, 108, 111]), 'base58');
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type BufferLike = Buffer | Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Supported base encoding names.
|
|
19
|
+
*
|
|
20
|
+
* These names can be used with the function API:
|
|
21
|
+
* - `'decimal'` - Base 10 (0-9)
|
|
22
|
+
* - `'base16'` / `'hex'` - Hexadecimal (0-9A-F)
|
|
23
|
+
* - `'base32'` - RFC 4648 Base32 (A-Z2-7)
|
|
24
|
+
* - `'base32crockford'` - Crockford's Base32 (0-9A-HJKMNP-TV-Z)
|
|
25
|
+
* - `'base36'` - Alphanumeric (0-9A-Z)
|
|
26
|
+
* - `'base52'` - Letters only (A-Za-z)
|
|
27
|
+
* - `'base58'` - Bitcoin alphabet (no 0OIl)
|
|
28
|
+
* - `'base64'` - Standard Base64 (A-Za-z0-9+/)
|
|
29
|
+
* - `'base64url'` - URL-safe Base64 (A-Za-z0-9-_)
|
|
30
|
+
* - `'base64xml'` - XML token Base64 (A-Za-z0-9._)
|
|
31
|
+
* - `'base64xmlname'` - XML name Base64 (A-Za-z0-9_:)
|
|
32
|
+
* - `'ascii85'` - ASCII85 encoding
|
|
33
|
+
* - `'base85'` - Base85 encoding
|
|
34
|
+
* - `'z85'` - ZeroMQ Z85 encoding
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import { encode, decode } from 'bufferbase';
|
|
39
|
+
*
|
|
40
|
+
* const encoded = encode(Buffer.from('Hello'), 'base58');
|
|
41
|
+
* const decoded = decode(encoded, 'base58');
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export type BaseName = 'decimal' | 'base16' | 'hex' | 'base32' | 'base32crockford' | 'base36' | 'base52' | 'base58' | 'base64' | 'base64url' | 'base64xml' | 'base64xmlname' | 'ascii85' | 'base85' | 'z85';
|
|
45
|
+
/**
|
|
46
|
+
* Options for decode operations.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Decode with fixed buffer size (pads with leading zeros)
|
|
51
|
+
* const buffer = decode(encoded, 'base58', { size: 32 });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export interface DecodeOptions {
|
|
55
|
+
/**
|
|
56
|
+
* Expected buffer size in bytes.
|
|
57
|
+
* - If decoded result is smaller, pads with leading zeros.
|
|
58
|
+
* - If decoded result is larger, throws `BufferSizeError`.
|
|
59
|
+
*/
|
|
60
|
+
size?: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Codec interface for encoding/decoding operations.
|
|
64
|
+
*
|
|
65
|
+
* Implemented by the `Codec` class and used by pre-defined codecs in `Codecs`.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import { Codecs } from 'bufferbase';
|
|
70
|
+
*
|
|
71
|
+
* const encoded = Codecs.base58.encode(Buffer.from('Hello'));
|
|
72
|
+
* const decoded = Codecs.base58.decode(encoded);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export interface ICodec {
|
|
76
|
+
/** Character set used for encoding. */
|
|
77
|
+
readonly chars: string;
|
|
78
|
+
/**
|
|
79
|
+
* Encodes a buffer to a base-encoded string.
|
|
80
|
+
* @param buffer - The buffer or Uint8Array to encode
|
|
81
|
+
* @returns Base-encoded string
|
|
82
|
+
*/
|
|
83
|
+
encode(buffer: BufferLike): string;
|
|
84
|
+
/**
|
|
85
|
+
* Decodes a base-encoded string to a buffer.
|
|
86
|
+
* @param encoded - The string to decode
|
|
87
|
+
* @param options - Decode options (e.g., fixed size)
|
|
88
|
+
* @returns Decoded buffer
|
|
89
|
+
* @throws {InvalidCharacterError} If the string contains invalid characters
|
|
90
|
+
* @throws {BufferSizeError} If the result exceeds the specified size
|
|
91
|
+
*/
|
|
92
|
+
decode(encoded: string, options?: DecodeOptions): Buffer;
|
|
93
|
+
/**
|
|
94
|
+
* Validates if a string contains only valid characters for this base.
|
|
95
|
+
* @param input - The string to validate
|
|
96
|
+
* @returns `true` if valid, `false` otherwise
|
|
97
|
+
*/
|
|
98
|
+
validate(input: string): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Converts a string from this base to another base.
|
|
101
|
+
* @param target - Target codec to convert to
|
|
102
|
+
* @param input - The string to convert
|
|
103
|
+
* @returns String in the target base encoding
|
|
104
|
+
*/
|
|
105
|
+
convertTo(target: ICodec, input: string): string;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,UAAU,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,MAAM,QAAQ,GAChB,SAAS,GACT,QAAQ,GACR,KAAK,GACL,QAAQ,GACR,iBAAiB,GACjB,QAAQ,GACR,QAAQ,GACR,QAAQ,GACR,QAAQ,GACR,WAAW,GACX,WAAW,GACX,eAAe,GACf,SAAS,GACT,QAAQ,GACR,KAAK,CAAC;AAEV;;;;;;;;GAQG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,MAAM;IACrB,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;;;OAIG;IACH,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC;IAEnC;;;;;;;OAOG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IAEzD;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAEjC;;;;;OAKG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;CAClD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { BaseName } from './types.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 declare const Chars: {
|
|
17
|
+
/** Base 10: 0-9 */
|
|
18
|
+
readonly Decimal: "0123456789";
|
|
19
|
+
/** Hexadecimal: 0-9A-F */
|
|
20
|
+
readonly Base16: "0123456789ABCDEF";
|
|
21
|
+
/** RFC 4648 Base32: A-Z2-7 */
|
|
22
|
+
readonly Base32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
23
|
+
/** Crockford's Base32: 0-9A-HJKMNP-TV-Z (no I, L, O, U) */
|
|
24
|
+
readonly Base32Crockford: "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
25
|
+
/** Alphanumeric: 0-9A-Z */
|
|
26
|
+
readonly Base36: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
27
|
+
/** Letters only: A-Za-z */
|
|
28
|
+
readonly Base52: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
29
|
+
/** Bitcoin alphabet: 1-9A-HJ-NP-Za-km-z (no 0, O, I, l) */
|
|
30
|
+
readonly Base58: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
31
|
+
/** Standard Base64: A-Za-z0-9+/ */
|
|
32
|
+
readonly Base64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
33
|
+
/** URL-safe Base64: A-Za-z0-9-_ */
|
|
34
|
+
readonly Base64Url: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
35
|
+
/** XML token Base64: A-Za-z0-9._ */
|
|
36
|
+
readonly Base64Xml: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
|
|
37
|
+
/** XML name Base64: A-Za-z0-9_: */
|
|
38
|
+
readonly Base64XmlName: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:";
|
|
39
|
+
/** ASCII85 encoding */
|
|
40
|
+
readonly Ascii85: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstu";
|
|
41
|
+
/** Base85 encoding */
|
|
42
|
+
readonly Base85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~";
|
|
43
|
+
/** ZeroMQ Z85 encoding */
|
|
44
|
+
readonly Z85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Resolves a base name or character set string to a character set.
|
|
48
|
+
*
|
|
49
|
+
* This function is used internally by the function API to convert
|
|
50
|
+
* base names (like `'base58'`) to their character sets.
|
|
51
|
+
*
|
|
52
|
+
* @param base - Base name (e.g., `'base58'`) or custom character set string
|
|
53
|
+
* @returns The character set string
|
|
54
|
+
* @throws {UnknownBaseError} If base name is not recognized and string length <= 15
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { resolveChars } from 'bufferbase';
|
|
59
|
+
*
|
|
60
|
+
* resolveChars('base58'); // Returns Bitcoin alphabet
|
|
61
|
+
* resolveChars('0123456789ABCDEF'); // Returns the string as-is (custom charset)
|
|
62
|
+
* resolveChars('invalid'); // Throws UnknownBaseError
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function resolveChars(base: BaseName | string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Checks if a string is a valid base name.
|
|
68
|
+
*
|
|
69
|
+
* @param value - The string to check
|
|
70
|
+
* @returns `true` if the value is a valid `BaseName`, `false` otherwise
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* import { isBaseName } from 'bufferbase';
|
|
75
|
+
*
|
|
76
|
+
* isBaseName('base58'); // true
|
|
77
|
+
* isBaseName('BASE58'); // true (case-insensitive)
|
|
78
|
+
* isBaseName('unknown'); // false
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function isBaseName(value: string): value is BaseName;
|
|
82
|
+
//# sourceMappingURL=bases.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bases.d.ts","sourceRoot":"","sources":["../../src/bases.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,KAAK;IAChB,mBAAmB;;IAEnB,0BAA0B;;IAE1B,8BAA8B;;IAE9B,2DAA2D;;IAE3D,2BAA2B;;IAE3B,2BAA2B;;IAE3B,2DAA2D;;IAE3D,mCAAmC;;IAEnC,mCAAmC;;IAEnC,oCAAoC;;IAEpC,mCAAmC;;IAEnC,uBAAuB;;IAEvB,sBAAsB;;IAEtB,0BAA0B;;CAElB,CAAC;AAwBX;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,CAW5D;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,QAAQ,CAE3D"}
|
|
@@ -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"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
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)`
|
|
7
|
+
*/
|
|
8
|
+
import { Buffer } from 'node:buffer';
|
|
9
|
+
import { InvalidCharacterError, BufferSizeError } from './errors.js';
|
|
10
|
+
export { Chars } from './bases.js';
|
|
11
|
+
export { InvalidCharacterError, BufferSizeError };
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `Codec` or `createCodec()` instead.
|
|
14
|
+
*/
|
|
15
|
+
export declare class BufferEncoder {
|
|
16
|
+
private baseChars;
|
|
17
|
+
private codec;
|
|
18
|
+
constructor(baseChars: string);
|
|
19
|
+
encode(buffer: Buffer): string;
|
|
20
|
+
decode(encoded: string, bufferSize?: number): Buffer;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `convert()` function or `codec.convertTo()` instead.
|
|
24
|
+
*/
|
|
25
|
+
export declare class Converter {
|
|
26
|
+
decoder: BufferEncoder;
|
|
27
|
+
encoder: BufferEncoder;
|
|
28
|
+
constructor(inputBase: string, outputBase: string);
|
|
29
|
+
convert(input: string): string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated Use `validate()` function or `codec.validate()` instead.
|
|
33
|
+
*/
|
|
34
|
+
export declare class Validator {
|
|
35
|
+
private codec;
|
|
36
|
+
constructor(inputBase: string);
|
|
37
|
+
validate(input: string): boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated Use `validate()` from the main module instead.
|
|
41
|
+
*/
|
|
42
|
+
export declare const validate: (input: string, base: string) => boolean;
|
|
43
|
+
//# sourceMappingURL=bufferbase.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|