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.
Files changed (101) hide show
  1. package/README.md +99 -18
  2. package/dist/cjs/bases.d.ts +82 -0
  3. package/dist/cjs/bases.d.ts.map +1 -0
  4. package/dist/cjs/bases.js +121 -0
  5. package/dist/cjs/bases.js.map +1 -0
  6. package/dist/cjs/bufferbase.d.ts +43 -0
  7. package/dist/cjs/bufferbase.d.ts.map +1 -0
  8. package/dist/cjs/bufferbase.js +66 -0
  9. package/dist/cjs/bufferbase.js.map +1 -0
  10. package/dist/cjs/codec.d.ts +124 -0
  11. package/dist/cjs/codec.d.ts.map +1 -0
  12. package/dist/cjs/codec.js +247 -0
  13. package/dist/cjs/codec.js.map +1 -0
  14. package/dist/cjs/codecs.d.ts +71 -0
  15. package/dist/cjs/codecs.d.ts.map +1 -0
  16. package/dist/cjs/codecs.js +92 -0
  17. package/dist/cjs/codecs.js.map +1 -0
  18. package/dist/cjs/errors.d.ts +76 -0
  19. package/dist/cjs/errors.d.ts.map +1 -0
  20. package/dist/cjs/errors.js +92 -0
  21. package/dist/cjs/errors.js.map +1 -0
  22. package/dist/{example.d.ts.map → cjs/example.d.ts.map} +1 -1
  23. package/dist/cjs/example.js +32 -0
  24. package/dist/cjs/example.js.map +1 -0
  25. package/dist/cjs/functions.d.ts +62 -0
  26. package/dist/cjs/functions.d.ts.map +1 -0
  27. package/dist/cjs/functions.js +93 -0
  28. package/dist/cjs/functions.js.map +1 -0
  29. package/dist/cjs/index.d.ts +9 -0
  30. package/dist/cjs/index.d.ts.map +1 -0
  31. package/dist/cjs/index.js +32 -0
  32. package/dist/cjs/index.js.map +1 -0
  33. package/dist/cjs/package.json +50 -0
  34. package/dist/cjs/types.d.ts +107 -0
  35. package/dist/cjs/types.d.ts.map +1 -0
  36. package/dist/cjs/types.js +3 -0
  37. package/dist/cjs/types.js.map +1 -0
  38. package/dist/esm/bases.d.ts +82 -0
  39. package/dist/esm/bases.d.ts.map +1 -0
  40. package/dist/esm/bases.js +116 -0
  41. package/dist/esm/bases.js.map +1 -0
  42. package/dist/esm/bufferbase.d.ts +43 -0
  43. package/dist/esm/bufferbase.d.ts.map +1 -0
  44. package/dist/esm/bufferbase.js +59 -0
  45. package/dist/esm/bufferbase.js.map +1 -0
  46. package/dist/esm/codec.d.ts +124 -0
  47. package/dist/esm/codec.d.ts.map +1 -0
  48. package/dist/esm/codec.js +242 -0
  49. package/dist/esm/codec.js.map +1 -0
  50. package/dist/esm/codecs.d.ts +71 -0
  51. package/dist/esm/codecs.d.ts.map +1 -0
  52. package/dist/esm/codecs.js +89 -0
  53. package/dist/esm/codecs.js.map +1 -0
  54. package/dist/esm/errors.d.ts +76 -0
  55. package/dist/esm/errors.d.ts.map +1 -0
  56. package/dist/esm/errors.js +86 -0
  57. package/dist/esm/errors.js.map +1 -0
  58. package/dist/esm/example.d.ts +2 -0
  59. package/dist/esm/example.d.ts.map +1 -0
  60. package/dist/esm/example.js +30 -0
  61. package/dist/esm/example.js.map +1 -0
  62. package/dist/esm/functions.d.ts +62 -0
  63. package/dist/esm/functions.d.ts.map +1 -0
  64. package/dist/esm/functions.js +87 -0
  65. package/dist/esm/functions.js.map +1 -0
  66. package/dist/esm/index.d.ts +9 -0
  67. package/dist/esm/index.d.ts.map +1 -0
  68. package/dist/esm/index.js +13 -0
  69. package/dist/esm/index.js.map +1 -0
  70. package/dist/esm/package.json +50 -0
  71. package/dist/esm/types.d.ts +107 -0
  72. package/dist/esm/types.d.ts.map +1 -0
  73. package/dist/esm/types.js +2 -0
  74. package/dist/esm/types.js.map +1 -0
  75. package/package.json +26 -11
  76. package/src/bases.ts +120 -0
  77. package/src/bufferbase.test.ts +44 -27
  78. package/src/bufferbase.ts +31 -106
  79. package/src/codec.test.ts +320 -0
  80. package/src/codec.ts +266 -0
  81. package/src/codecs.ts +118 -0
  82. package/src/errors.ts +87 -0
  83. package/src/example.ts +20 -30
  84. package/src/functions.ts +93 -0
  85. package/src/index.ts +21 -8
  86. package/src/types.ts +129 -0
  87. package/dist/bufferbase.d.ts +0 -60
  88. package/dist/bufferbase.d.ts.map +0 -1
  89. package/dist/bufferbase.js +0 -141
  90. package/dist/bufferbase.js.map +0 -1
  91. package/dist/example.js +0 -37
  92. package/dist/example.js.map +0 -1
  93. package/dist/index.d.ts +0 -2
  94. package/dist/index.d.ts.map +0 -1
  95. package/dist/index.js +0 -2
  96. package/dist/index.js.map +0 -1
  97. package/dist/sample.d.ts +0 -2
  98. package/dist/sample.d.ts.map +0 -1
  99. package/dist/sample.js +0 -37
  100. package/dist/sample.js.map +0 -1
  101. /package/dist/{example.d.ts → cjs/example.d.ts} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C;;OAEG;gBACS,IAAI,CAAC,EAAE,MAAM;CAK1B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC;;;OAGG;gBACS,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;CAI7C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC;;OAEG;gBACS,IAAI,EAAE,MAAM;CAIzB"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Error thrown when an invalid character is encountered during decoding.
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * import { decode, InvalidCharacterError } from 'bufferbase';
7
+ *
8
+ * try {
9
+ * decode('invalid!', 'base58');
10
+ * } catch (e) {
11
+ * if (e instanceof InvalidCharacterError) {
12
+ * console.log(e.message); // "Invalid character found: '!'"
13
+ * }
14
+ * }
15
+ * ```
16
+ */
17
+ export class InvalidCharacterError extends Error {
18
+ /**
19
+ * @param char - The invalid character that was encountered (optional)
20
+ */
21
+ constructor(char) {
22
+ const message = char ? `Invalid character found: '${char}'` : 'Invalid character found';
23
+ super(message);
24
+ this.name = 'InvalidCharacterError';
25
+ }
26
+ }
27
+ /**
28
+ * Error thrown when decoded buffer size exceeds the specified size.
29
+ *
30
+ * This error is thrown when using the `size` option in decode operations
31
+ * and the decoded data is larger than the specified size.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * import { decode, BufferSizeError } from 'bufferbase';
36
+ *
37
+ * try {
38
+ * // Trying to decode into a buffer too small
39
+ * decode('72k1xXWG59fYdzSNoA', 'base58', { size: 2 });
40
+ * } catch (e) {
41
+ * if (e instanceof BufferSizeError) {
42
+ * console.log(e.message); // "Buffer size 13 exceeds specified size 2"
43
+ * }
44
+ * }
45
+ * ```
46
+ */
47
+ export class BufferSizeError extends Error {
48
+ /**
49
+ * @param actual - The actual decoded buffer size
50
+ * @param expected - The expected (specified) buffer size
51
+ */
52
+ constructor(actual, expected) {
53
+ super(`Buffer size ${actual} exceeds specified size ${expected}`);
54
+ this.name = 'BufferSizeError';
55
+ }
56
+ }
57
+ /**
58
+ * Error thrown when an unknown base name is provided.
59
+ *
60
+ * This error is thrown when using the function API with an invalid base name
61
+ * that is neither a known encoding name nor a valid custom character set
62
+ * (character sets must be longer than 15 characters).
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * import { encode, UnknownBaseError } from 'bufferbase';
67
+ *
68
+ * try {
69
+ * encode(Buffer.from('test'), 'invalidbase');
70
+ * } catch (e) {
71
+ * if (e instanceof UnknownBaseError) {
72
+ * console.log(e.message); // "Unknown base encoding: 'invalidbase'"
73
+ * }
74
+ * }
75
+ * ```
76
+ */
77
+ export class UnknownBaseError extends Error {
78
+ /**
79
+ * @param name - The unknown base name that was provided
80
+ */
81
+ constructor(name) {
82
+ super(`Unknown base encoding: '${name}'`);
83
+ this.name = 'UnknownBaseError';
84
+ }
85
+ }
86
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C;;OAEG;IACH,YAAY,IAAa;QACvB,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,6BAA6B,IAAI,GAAG,CAAC,CAAC,CAAC,yBAAyB,CAAC;QACxF,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACtC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACxC;;;OAGG;IACH,YAAY,MAAc,EAAE,QAAgB;QAC1C,KAAK,CAAC,eAAe,MAAM,2BAA2B,QAAQ,EAAE,CAAC,CAAC;QAClE,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC;;OAEG;IACH,YAAY,IAAY;QACtB,KAAK,CAAC,2BAA2B,IAAI,GAAG,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=example.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":""}
@@ -0,0 +1,30 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { encode, decode, convert, validate, Codecs } from './index.js';
3
+ // Example buffer
4
+ const bytes = Buffer.from('Hello, World!', 'utf8');
5
+ // Using simple function API
6
+ const base32encoded = encode(bytes, 'base32crockford');
7
+ const base58encoded = convert(base32encoded, 'base32crockford', 'base58');
8
+ const base64encoded = convert(base58encoded, 'base58', 'base64url');
9
+ const isValidAsBase64 = validate(base64encoded, 'base64url');
10
+ const decoded = decode(base64encoded, 'base64url');
11
+ console.log('=== Function API ===');
12
+ console.table({
13
+ bytes: bytes.toString('utf8'),
14
+ base32encoded,
15
+ base58encoded,
16
+ base64encoded,
17
+ isValidAsBase64,
18
+ decoded: decoded.toString('utf8'),
19
+ });
20
+ // Using Codecs object
21
+ console.log('\n=== Codecs API ===');
22
+ const encoded = Codecs.base58.encode(bytes);
23
+ const converted = Codecs.base58.convertTo(Codecs.base64url, encoded);
24
+ console.table({
25
+ original: bytes.toString('utf8'),
26
+ base58: encoded,
27
+ base64url: converted,
28
+ valid: Codecs.base64url.validate(converted),
29
+ });
30
+ //# sourceMappingURL=example.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEvE,iBAAiB;AACjB,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;AAEnD,4BAA4B;AAC5B,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC;AACvD,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,EAAE,iBAAiB,EAAE,QAAQ,CAAC,CAAC;AAC1E,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;AACpE,MAAM,eAAe,GAAG,QAAQ,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;AAC7D,MAAM,OAAO,GAAG,MAAM,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;AAEnD,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AACpC,OAAO,CAAC,KAAK,CAAC;IACZ,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC7B,aAAa;IACb,aAAa;IACb,aAAa;IACb,eAAe;IACf,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;CAClC,CAAC,CAAC;AAEH,sBAAsB;AACtB,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AACpC,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC5C,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;AACrE,OAAO,CAAC,KAAK,CAAC;IACZ,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;IAChC,MAAM,EAAE,OAAO;IACf,SAAS,EAAE,SAAS;IACpB,KAAK,EAAE,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC;CAC5C,CAAC,CAAC"}
@@ -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,87 @@
1
+ import { Codec } from './codec.js';
2
+ import { resolveChars } from './bases.js';
3
+ // Cache for codec instances
4
+ const codecCache = new Map();
5
+ /**
6
+ * Gets or creates a codec for the specified base.
7
+ */
8
+ function getCodec(base) {
9
+ const chars = resolveChars(base);
10
+ let codec = codecCache.get(chars);
11
+ if (!codec) {
12
+ codec = new Codec(chars);
13
+ codecCache.set(chars, codec);
14
+ }
15
+ return codec;
16
+ }
17
+ /**
18
+ * Encodes a buffer to a base-encoded string.
19
+ *
20
+ * @param buffer - Buffer or Uint8Array to encode
21
+ * @param base - Base name (e.g., 'base58') or custom character set
22
+ * @returns Encoded string
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * // Node.js Buffer
27
+ * const encoded = encode(Buffer.from('Hello'), 'base58');
28
+ *
29
+ * // Browser Uint8Array
30
+ * const encoded = encode(new Uint8Array([72, 101, 108, 108, 111]), 'base58');
31
+ * ```
32
+ */
33
+ export function encode(buffer, base) {
34
+ return getCodec(base).encode(buffer);
35
+ }
36
+ /**
37
+ * Decodes a base-encoded string to a buffer.
38
+ *
39
+ * @param encoded - String to decode
40
+ * @param base - Base name (e.g., 'base58') or custom character set
41
+ * @param options - Decode options (e.g., { size: 32 })
42
+ * @returns Decoded buffer
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * const buffer = decode('9Ajdvz', 'base58');
47
+ * const fixedSize = decode('9Ajdvz', 'base58', { size: 32 });
48
+ * ```
49
+ */
50
+ export function decode(encoded, base, options) {
51
+ return getCodec(base).decode(encoded, options);
52
+ }
53
+ /**
54
+ * Converts a string from one base to another.
55
+ *
56
+ * @param input - String to convert
57
+ * @param from - Source base name or character set
58
+ * @param to - Target base name or character set
59
+ * @returns Converted string
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const base64 = convert('9Ajdvz', 'base58', 'base64url');
64
+ * ```
65
+ */
66
+ export function convert(input, from, to) {
67
+ const fromCodec = getCodec(from);
68
+ const toCodec = getCodec(to);
69
+ return fromCodec.convertTo(toCodec, input);
70
+ }
71
+ /**
72
+ * Validates if a string contains only valid characters for the specified base.
73
+ *
74
+ * @param input - String to validate
75
+ * @param base - Base name or character set
76
+ * @returns true if valid, false otherwise
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * validate('9Ajdvz', 'base58'); // true
81
+ * validate('0Ajdvz', 'base58'); // false ('0' is not in base58)
82
+ * ```
83
+ */
84
+ export function validate(input, base) {
85
+ return getCodec(base).validate(input);
86
+ }
87
+ //# sourceMappingURL=functions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,4BAA4B;AAC5B,MAAM,UAAU,GAAG,IAAI,GAAG,EAAiB,CAAC;AAE5C;;GAEG;AACH,SAAS,QAAQ,CAAC,IAAuB;IACvC,MAAM,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACjC,IAAI,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,KAAK,GAAG,IAAI,KAAK,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,MAAM,UAAU,MAAM,CAAC,MAAkB,EAAE,IAAuB;IAChE,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACvC,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,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,MAAM,UAAU,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,MAAM,UAAU,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,13 @@
1
+ // Errors
2
+ export { InvalidCharacterError, BufferSizeError, UnknownBaseError } from './errors.js';
3
+ // Codec class and factory
4
+ export { Codec, createCodec } from './codec.js';
5
+ // Character sets and utilities
6
+ export { Chars, resolveChars, isBaseName } from './bases.js';
7
+ // Simple function API
8
+ export { encode, decode, convert, validate } from './functions.js';
9
+ // Pre-defined codecs (namespaced)
10
+ export { Codecs } from './codecs.js';
11
+ // Deprecated exports for backward compatibility
12
+ export { BufferEncoder, Converter, Validator } from './bufferbase.js';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAIA,SAAS;AACT,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEvF,0BAA0B;AAC1B,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEhD,+BAA+B;AAC/B,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7D,sBAAsB;AACtB,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAEnE,kCAAkC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,gDAAgD;AAChD,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
@@ -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": "module",
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,2 @@
1
+ import { Buffer } from 'node:buffer';
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,22 +1,37 @@
1
1
  {
2
2
  "name": "bufferbase",
3
- "version": "1.2.3",
3
+ "version": "2.0.0",
4
4
  "description": "Buffer-to-BaseN Encoder, Decoder, Converter, and Validator",
5
- "type": "module",
6
- "types": "./dist/index.d.ts",
7
5
  "sideEffects": false,
8
- "main": "./dist/index.js",
9
- "exports": "./dist/index.js",
6
+ "type": "module",
7
+ "main": "./dist/cjs/index.js",
8
+ "module": "./dist/esm/index.js",
9
+ "types": "./dist/esm/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": {
13
+ "types": "./dist/esm/index.d.ts",
14
+ "default": "./dist/esm/index.js"
15
+ },
16
+ "require": {
17
+ "types": "./dist/cjs/index.d.ts",
18
+ "default": "./dist/cjs/index.js"
19
+ },
20
+ "default": "./dist/cjs/index.js"
21
+ }
22
+ },
10
23
  "scripts": {
11
- "build": "tsc",
12
- "prepublishOnly": "npm run build",
13
- "example": "tsx src/example.ts",
14
- "test": "tsx --test src/*.test.ts"
24
+ "build": "bun run build:esm && bun run build:cjs && tsconfig-to-dual-package",
25
+ "build:cjs": "tsc -p tsconfig.cjs.json",
26
+ "build:esm": "tsc -p tsconfig.json",
27
+ "prepublishOnly": "bun run build",
28
+ "example": "bun src/example.ts",
29
+ "test": "bun test"
15
30
  },
16
31
  "devDependencies": {
32
+ "@types/bun": "^1.1.0",
17
33
  "@types/node": "^20.11.5",
18
- "ts-loader": "^9.5.1",
19
- "tsx": "^4.7.0",
34
+ "tsconfig-to-dual-package": "^1.2.0",
20
35
  "typescript": "^5.3.3"
21
36
  },
22
37
  "repository": {
package/src/bases.ts ADDED
@@ -0,0 +1,120 @@
1
+ import type { BaseName } from './types.js';
2
+ import { UnknownBaseError } from './errors.js';
3
+
4
+ /**
5
+ * Character sets for common base encodings.
6
+ *
7
+ * Use these with `createCodec()` or the deprecated `BufferEncoder` class
8
+ * when you need direct access to character sets.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { Chars, createCodec } from 'bufferbase';
13
+ *
14
+ * const codec = createCodec(Chars.Base58);
15
+ * const encoded = codec.encode(Buffer.from('Hello'));
16
+ * ```
17
+ */
18
+ export const Chars = {
19
+ /** Base 10: 0-9 */
20
+ Decimal: '0123456789',
21
+ /** Hexadecimal: 0-9A-F */
22
+ Base16: '0123456789ABCDEF',
23
+ /** RFC 4648 Base32: A-Z2-7 */
24
+ Base32: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
25
+ /** Crockford's Base32: 0-9A-HJKMNP-TV-Z (no I, L, O, U) */
26
+ Base32Crockford: '0123456789ABCDEFGHJKMNPQRSTVWXYZ',
27
+ /** Alphanumeric: 0-9A-Z */
28
+ Base36: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
29
+ /** Letters only: A-Za-z */
30
+ Base52: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
31
+ /** Bitcoin alphabet: 1-9A-HJ-NP-Za-km-z (no 0, O, I, l) */
32
+ Base58: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
33
+ /** Standard Base64: A-Za-z0-9+/ */
34
+ Base64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
35
+ /** URL-safe Base64: A-Za-z0-9-_ */
36
+ Base64Url: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
37
+ /** XML token Base64: A-Za-z0-9._ */
38
+ Base64Xml: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._',
39
+ /** XML name Base64: A-Za-z0-9_: */
40
+ Base64XmlName: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:',
41
+ /** ASCII85 encoding */
42
+ Ascii85: '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstu',
43
+ /** Base85 encoding */
44
+ Base85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~',
45
+ /** ZeroMQ Z85 encoding */
46
+ Z85: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#',
47
+ } as const;
48
+
49
+ /**
50
+ * Internal mapping from base names to character sets.
51
+ * @internal
52
+ */
53
+ const baseNameToChars: Record<BaseName, string> = {
54
+ decimal: Chars.Decimal,
55
+ base16: Chars.Base16,
56
+ hex: Chars.Base16,
57
+ base32: Chars.Base32,
58
+ base32crockford: Chars.Base32Crockford,
59
+ base36: Chars.Base36,
60
+ base52: Chars.Base52,
61
+ base58: Chars.Base58,
62
+ base64: Chars.Base64,
63
+ base64url: Chars.Base64Url,
64
+ base64xml: Chars.Base64Xml,
65
+ base64xmlname: Chars.Base64XmlName,
66
+ ascii85: Chars.Ascii85,
67
+ base85: Chars.Base85,
68
+ z85: Chars.Z85,
69
+ };
70
+
71
+ /**
72
+ * Resolves a base name or character set string to a character set.
73
+ *
74
+ * This function is used internally by the function API to convert
75
+ * base names (like `'base58'`) to their character sets.
76
+ *
77
+ * @param base - Base name (e.g., `'base58'`) or custom character set string
78
+ * @returns The character set string
79
+ * @throws {UnknownBaseError} If base name is not recognized and string length <= 15
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * import { resolveChars } from 'bufferbase';
84
+ *
85
+ * resolveChars('base58'); // Returns Bitcoin alphabet
86
+ * resolveChars('0123456789ABCDEF'); // Returns the string as-is (custom charset)
87
+ * resolveChars('invalid'); // Throws UnknownBaseError
88
+ * ```
89
+ */
90
+ export function resolveChars(base: BaseName | string): string {
91
+ const normalized = base.toLowerCase() as BaseName;
92
+ if (normalized in baseNameToChars) {
93
+ return baseNameToChars[normalized];
94
+ }
95
+ // If not a known base name, treat as custom character set
96
+ // (must be longer than 15 chars to distinguish from typos)
97
+ if (base.length > 15) {
98
+ return base;
99
+ }
100
+ throw new UnknownBaseError(base);
101
+ }
102
+
103
+ /**
104
+ * Checks if a string is a valid base name.
105
+ *
106
+ * @param value - The string to check
107
+ * @returns `true` if the value is a valid `BaseName`, `false` otherwise
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * import { isBaseName } from 'bufferbase';
112
+ *
113
+ * isBaseName('base58'); // true
114
+ * isBaseName('BASE58'); // true (case-insensitive)
115
+ * isBaseName('unknown'); // false
116
+ * ```
117
+ */
118
+ export function isBaseName(value: string): value is BaseName {
119
+ return value.toLowerCase() in baseNameToChars;
120
+ }