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.
Files changed (107) 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 +19 -36
  7. package/dist/cjs/bufferbase.d.ts.map +1 -1
  8. package/dist/cjs/bufferbase.js +27 -111
  9. package/dist/cjs/bufferbase.js.map +1 -1
  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/cjs/example.js +20 -27
  23. package/dist/cjs/example.js.map +1 -1
  24. package/dist/cjs/functions.d.ts +62 -0
  25. package/dist/cjs/functions.d.ts.map +1 -0
  26. package/dist/cjs/functions.js +93 -0
  27. package/dist/cjs/functions.js.map +1 -0
  28. package/dist/cjs/index.d.ts +8 -1
  29. package/dist/cjs/index.d.ts.map +1 -1
  30. package/dist/cjs/index.js +25 -4
  31. package/dist/cjs/index.js.map +1 -1
  32. package/dist/cjs/package.json +6 -7
  33. package/dist/cjs/types.d.ts +107 -0
  34. package/dist/cjs/types.d.ts.map +1 -0
  35. package/dist/cjs/types.js +3 -0
  36. package/dist/cjs/types.js.map +1 -0
  37. package/dist/esm/bases.d.ts +82 -0
  38. package/dist/esm/bases.d.ts.map +1 -0
  39. package/dist/esm/bases.js +116 -0
  40. package/dist/esm/bases.js.map +1 -0
  41. package/dist/esm/bufferbase.d.ts +19 -36
  42. package/dist/esm/bufferbase.d.ts.map +1 -1
  43. package/dist/esm/bufferbase.js +25 -107
  44. package/dist/esm/bufferbase.js.map +1 -1
  45. package/dist/esm/codec.d.ts +124 -0
  46. package/dist/esm/codec.d.ts.map +1 -0
  47. package/dist/esm/codec.js +242 -0
  48. package/dist/esm/codec.js.map +1 -0
  49. package/dist/esm/codecs.d.ts +71 -0
  50. package/dist/esm/codecs.d.ts.map +1 -0
  51. package/dist/esm/codecs.js +89 -0
  52. package/dist/esm/codecs.js.map +1 -0
  53. package/dist/esm/errors.d.ts +76 -0
  54. package/dist/esm/errors.d.ts.map +1 -0
  55. package/dist/esm/errors.js +86 -0
  56. package/dist/esm/errors.js.map +1 -0
  57. package/dist/esm/example.js +19 -26
  58. package/dist/esm/example.js.map +1 -1
  59. package/dist/esm/functions.d.ts +62 -0
  60. package/dist/esm/functions.d.ts.map +1 -0
  61. package/dist/esm/functions.js +87 -0
  62. package/dist/esm/functions.js.map +1 -0
  63. package/dist/esm/index.d.ts +8 -1
  64. package/dist/esm/index.d.ts.map +1 -1
  65. package/dist/esm/index.js +12 -1
  66. package/dist/esm/index.js.map +1 -1
  67. package/dist/esm/package.json +6 -7
  68. package/dist/esm/types.d.ts +107 -0
  69. package/dist/esm/types.d.ts.map +1 -0
  70. package/dist/esm/types.js +2 -0
  71. package/dist/esm/types.js.map +1 -0
  72. package/package.json +8 -9
  73. package/src/bases.ts +120 -0
  74. package/src/bufferbase.test.ts +44 -27
  75. package/src/bufferbase.ts +31 -106
  76. package/src/codec.test.ts +320 -0
  77. package/src/codec.ts +266 -0
  78. package/src/codecs.ts +118 -0
  79. package/src/errors.ts +87 -0
  80. package/src/example.ts +20 -30
  81. package/src/functions.ts +93 -0
  82. package/src/index.ts +21 -8
  83. package/src/types.ts +129 -0
  84. package/dist/cjs/bufferbase.d.mts +0 -60
  85. package/dist/cjs/bufferbase.d.mts.map +0 -1
  86. package/dist/cjs/bufferbase.mjs +0 -150
  87. package/dist/cjs/bufferbase.mjs.map +0 -1
  88. package/dist/cjs/example.d.mts +0 -2
  89. package/dist/cjs/example.d.mts.map +0 -1
  90. package/dist/cjs/example.mjs +0 -39
  91. package/dist/cjs/example.mjs.map +0 -1
  92. package/dist/cjs/index.d.mts +0 -2
  93. package/dist/cjs/index.d.mts.map +0 -1
  94. package/dist/cjs/index.mjs +0 -11
  95. package/dist/cjs/index.mjs.map +0 -1
  96. package/dist/esm/bufferbase.d.mts +0 -60
  97. package/dist/esm/bufferbase.d.mts.map +0 -1
  98. package/dist/esm/bufferbase.mjs +0 -141
  99. package/dist/esm/bufferbase.mjs.map +0 -1
  100. package/dist/esm/example.d.mts +0 -2
  101. package/dist/esm/example.d.mts.map +0 -1
  102. package/dist/esm/example.mjs +0 -37
  103. package/dist/esm/example.mjs.map +0 -1
  104. package/dist/esm/index.d.mts +0 -2
  105. package/dist/esm/index.d.mts.map +0 -1
  106. package/dist/esm/index.mjs +0 -2
  107. package/dist/esm/index.mjs.map +0 -1
@@ -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"}
@@ -1,2 +1,9 @@
1
- export { Chars, BufferEncoder, Converter, InvalidCharacterError, Validator, validate, } from './bufferbase.js';
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';
2
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,aAAa,EACb,SAAS,EACT,qBAAqB,EACrB,SAAS,EACT,QAAQ,GACT,MAAM,iBAAiB,CAAC"}
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"}
package/dist/esm/index.js CHANGED
@@ -1,2 +1,13 @@
1
- export { Chars, BufferEncoder, Converter, InvalidCharacterError, Validator, validate, } from './bufferbase.js';
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';
2
13
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,aAAa,EACb,SAAS,EACT,qBAAqB,EACrB,SAAS,EACT,QAAQ,GACT,MAAM,iBAAiB,CAAC"}
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"}
@@ -1,22 +1,21 @@
1
1
  {
2
2
  "name": "bufferbase",
3
- "version": "1.3.0",
3
+ "version": "2.0.0",
4
4
  "description": "Buffer-to-BaseN Encoder, Decoder, Converter, and Validator",
5
5
  "sideEffects": false,
6
6
  "type": "module",
7
7
  "scripts": {
8
- "build": "npm run build:esm && npm run build:cjs && tsconfig-to-dual-package",
8
+ "build": "bun run build:esm && bun run build:cjs && tsconfig-to-dual-package",
9
9
  "build:cjs": "tsc -p tsconfig.cjs.json",
10
10
  "build:esm": "tsc -p tsconfig.json",
11
- "prepublishOnly": "npm run build",
12
- "example": "tsx src/example.ts",
13
- "test": "tsx --test src/*.test.ts"
11
+ "prepublishOnly": "bun run build",
12
+ "example": "bun src/example.ts",
13
+ "test": "bun test"
14
14
  },
15
15
  "devDependencies": {
16
+ "@types/bun": "^1.1.0",
16
17
  "@types/node": "^20.11.5",
17
- "ts-loader": "^9.5.1",
18
18
  "tsconfig-to-dual-package": "^1.2.0",
19
- "tsx": "^4.7.0",
20
19
  "typescript": "^5.3.3"
21
20
  },
22
21
  "repository": {
@@ -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,12 +1,12 @@
1
1
  {
2
2
  "name": "bufferbase",
3
- "version": "1.3.0",
3
+ "version": "2.0.0",
4
4
  "description": "Buffer-to-BaseN Encoder, Decoder, Converter, and Validator",
5
5
  "sideEffects": false,
6
6
  "type": "module",
7
- "main": "./dist/cjs/index.mjs",
7
+ "main": "./dist/cjs/index.js",
8
8
  "module": "./dist/esm/index.js",
9
- "types": "./dist/esm/index.d.mts",
9
+ "types": "./dist/esm/index.d.ts",
10
10
  "exports": {
11
11
  ".": {
12
12
  "import": {
@@ -21,18 +21,17 @@
21
21
  }
22
22
  },
23
23
  "scripts": {
24
- "build": "npm run build:esm && npm run build:cjs && tsconfig-to-dual-package",
24
+ "build": "bun run build:esm && bun run build:cjs && tsconfig-to-dual-package",
25
25
  "build:cjs": "tsc -p tsconfig.cjs.json",
26
26
  "build:esm": "tsc -p tsconfig.json",
27
- "prepublishOnly": "npm run build",
28
- "example": "tsx src/example.ts",
29
- "test": "tsx --test src/*.test.ts"
27
+ "prepublishOnly": "bun run build",
28
+ "example": "bun src/example.ts",
29
+ "test": "bun test"
30
30
  },
31
31
  "devDependencies": {
32
+ "@types/bun": "^1.1.0",
32
33
  "@types/node": "^20.11.5",
33
- "ts-loader": "^9.5.1",
34
34
  "tsconfig-to-dual-package": "^1.2.0",
35
- "tsx": "^4.7.0",
36
35
  "typescript": "^5.3.3"
37
36
  },
38
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
+ }
@@ -1,53 +1,49 @@
1
1
  import { Chars, BufferEncoder } from './bufferbase.js';
2
2
  import { Buffer } from 'node:buffer';
3
- import test from 'node:test'; // ①
4
- import assert from 'node:assert';
3
+ import { describe, test, expect } from 'bun:test';
5
4
 
6
- test('BufferEncoder', async (t) => {
7
- await t.test(
8
- 'encodes and decodes a string correctly with base58chars',
9
- () => {
10
- const encoder = new BufferEncoder(Chars.Base58);
11
- const buffer = Buffer.from('Hello, World!', 'utf8');
5
+ describe('BufferEncoder', () => {
6
+ test('encodes and decodes a string correctly with base58chars', () => {
7
+ const encoder = new BufferEncoder(Chars.Base58);
8
+ const buffer = Buffer.from('Hello, World!', 'utf8');
12
9
 
13
- const encoded = encoder.encode(buffer);
14
- const decoded = encoder.decode(encoded);
10
+ const encoded = encoder.encode(buffer);
11
+ const decoded = encoder.decode(encoded);
15
12
 
16
- assert.strictEqual(decoded.toString('utf8'), 'Hello, World!');
17
- }
18
- );
13
+ expect(decoded.toString('utf8')).toBe('Hello, World!');
14
+ });
19
15
 
20
- await t.test('handles empty buffer correctly', () => {
16
+ test('handles empty buffer correctly', () => {
21
17
  const encoder = new BufferEncoder(Chars.Base58);
22
18
  const buffer = Buffer.alloc(0);
23
19
 
24
20
  const encoded = encoder.encode(buffer);
25
21
  const decoded = encoder.decode(encoded);
26
22
 
27
- assert.strictEqual(decoded.toString('utf8'), '');
23
+ expect(decoded.toString('utf8')).toBe('');
28
24
  });
29
25
 
30
- await t.test('handles long buffer correctly', () => {
26
+ test('handles long buffer correctly', () => {
31
27
  const encoder = new BufferEncoder(Chars.Base58);
32
28
  const buffer = Buffer.from('Hello, World!'.repeat(100), 'utf8');
33
29
 
34
30
  const encoded = encoder.encode(buffer);
35
31
  const decoded = encoder.decode(encoded);
36
32
 
37
- assert.strictEqual(decoded.toString('utf8'), 'Hello, World!'.repeat(100));
33
+ expect(decoded.toString('utf8')).toBe('Hello, World!'.repeat(100));
38
34
  });
39
35
 
40
- await t.test('handles bufferSize argument correctly', () => {
36
+ test('handles bufferSize argument correctly', () => {
41
37
  const encoder = new BufferEncoder(Chars.Base58);
42
38
  const buffer = Buffer.alloc(0);
43
39
 
44
40
  const encoded = encoder.encode(buffer);
45
41
  const decoded = encoder.decode(encoded, 4);
46
42
 
47
- assert.strictEqual(decoded.toString('hex'), '00000000');
43
+ expect(decoded.toString('hex')).toBe('00000000');
48
44
  });
49
45
 
50
- await t.test('encode and decode various chars correctly', () => {
46
+ test('encode and decode various chars correctly', () => {
51
47
  for (const charTable of Object.values(Chars)) {
52
48
  const encoder = new BufferEncoder(charTable);
53
49
  const buffer = Buffer.from('Hello, World!', 'utf8');
@@ -55,11 +51,11 @@ test('BufferEncoder', async (t) => {
55
51
  const encoded = encoder.encode(buffer);
56
52
  const decoded = encoder.decode(encoded);
57
53
 
58
- assert.strictEqual(decoded.toString('utf8'), 'Hello, World!');
54
+ expect(decoded.toString('utf8')).toBe('Hello, World!');
59
55
  }
60
56
  });
61
57
 
62
- await t.test('handles binary powers from 1 to 63 correctly', () => {
58
+ test('handles binary powers from 1 to 63 correctly', () => {
63
59
  const encoder = new BufferEncoder(Chars.Base58);
64
60
  for (let i = 0; i < 64; i++) {
65
61
  const buffer = Buffer.alloc(8);
@@ -68,14 +64,35 @@ test('BufferEncoder', async (t) => {
68
64
  const encoded = encoder.encode(buffer);
69
65
  const decoded = encoder.decode(encoded, 8);
70
66
  const result = decoded.readBigInt64BE();
71
- console.log(encoded, result);
72
- assert.strictEqual(result, num);
67
+ expect(result).toBe(num);
73
68
  }
74
69
  });
75
- await t.test('throws error on invalid characters in decode', () => {
76
- assert.throws(() => {
70
+
71
+ test('throws error on invalid characters in decode', () => {
72
+ expect(() => {
77
73
  const encoder = new BufferEncoder(Chars.Base58);
78
74
  encoder.decode('InvalidString!');
79
- }, new Error('Invalid character found'));
75
+ }).toThrow(/Invalid character found/);
76
+ });
77
+
78
+ test('encodes Uint8Array correctly', () => {
79
+ const encoder = new BufferEncoder(Chars.Base58);
80
+ const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); // 'Hello'
81
+ const buffer = Buffer.from('Hello', 'utf8');
82
+
83
+ const encodedFromUint8 = encoder.encode(uint8Array);
84
+ const encodedFromBuffer = encoder.encode(buffer);
85
+
86
+ expect(encodedFromUint8).toBe(encodedFromBuffer);
87
+ });
88
+
89
+ test('encodes Uint8Array with leading zeros correctly', () => {
90
+ const encoder = new BufferEncoder(Chars.Base58);
91
+ const uint8Array = new Uint8Array([0, 0, 1, 2, 3]);
92
+
93
+ const encoded = encoder.encode(uint8Array);
94
+ const decoded = encoder.decode(encoded);
95
+
96
+ expect(Array.from(decoded)).toEqual([0, 0, 1, 2, 3]);
80
97
  });
81
98
  });