bufferbase 2.0.0 → 3.0.1
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 +162 -63
- package/dist/cjs/bases.d.ts +152 -45
- package/dist/cjs/bases.d.ts.map +1 -1
- package/dist/cjs/bases.js +90 -77
- package/dist/cjs/bases.js.map +1 -1
- package/dist/cjs/bufferbase.d.ts +21 -27
- package/dist/cjs/bufferbase.d.ts.map +1 -1
- package/dist/cjs/bufferbase.js +21 -31
- package/dist/cjs/bufferbase.js.map +1 -1
- package/dist/cjs/codec-base.d.ts +56 -0
- package/dist/cjs/codec-base.d.ts.map +1 -0
- package/dist/cjs/codec-base.js +132 -0
- package/dist/cjs/codec-base.js.map +1 -0
- package/dist/cjs/codec-block.d.ts +47 -0
- package/dist/cjs/codec-block.d.ts.map +1 -0
- package/dist/cjs/codec-block.js +213 -0
- package/dist/cjs/codec-block.js.map +1 -0
- package/dist/cjs/codec-block85.d.ts +56 -0
- package/dist/cjs/codec-block85.d.ts.map +1 -0
- package/dist/cjs/codec-block85.js +271 -0
- package/dist/cjs/codec-block85.js.map +1 -0
- package/dist/cjs/codec-radix.d.ts +46 -0
- package/dist/cjs/codec-radix.d.ts.map +1 -0
- package/dist/cjs/codec-radix.js +181 -0
- package/dist/cjs/codec-radix.js.map +1 -0
- package/dist/cjs/codec.d.ts +25 -111
- package/dist/cjs/codec.d.ts.map +1 -1
- package/dist/cjs/codec.js +50 -228
- package/dist/cjs/codec.js.map +1 -1
- package/dist/cjs/codecs.d.ts +8 -59
- package/dist/cjs/codecs.d.ts.map +1 -1
- package/dist/cjs/codecs.js +6 -79
- package/dist/cjs/codecs.js.map +1 -1
- package/dist/cjs/errors.d.ts +65 -48
- package/dist/cjs/errors.d.ts.map +1 -1
- package/dist/cjs/errors.js +89 -51
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/example.js +44 -21
- package/dist/cjs/example.js.map +1 -1
- package/dist/cjs/functions.d.ts +27 -31
- package/dist/cjs/functions.d.ts.map +1 -1
- package/dist/cjs/functions.js +25 -47
- package/dist/cjs/functions.js.map +1 -1
- package/dist/cjs/index.d.ts +7 -5
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +17 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +12 -7
- package/dist/cjs/types.d.ts +108 -71
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/bases.d.ts +152 -45
- package/dist/esm/bases.d.ts.map +1 -1
- package/dist/esm/bases.js +88 -75
- package/dist/esm/bases.js.map +1 -1
- package/dist/esm/bufferbase.d.ts +21 -27
- package/dist/esm/bufferbase.d.ts.map +1 -1
- package/dist/esm/bufferbase.js +18 -30
- package/dist/esm/bufferbase.js.map +1 -1
- package/dist/esm/codec-base.d.ts +56 -0
- package/dist/esm/codec-base.d.ts.map +1 -0
- package/dist/esm/codec-base.js +127 -0
- package/dist/esm/codec-base.js.map +1 -0
- package/dist/esm/codec-block.d.ts +47 -0
- package/dist/esm/codec-block.d.ts.map +1 -0
- package/dist/esm/codec-block.js +209 -0
- package/dist/esm/codec-block.js.map +1 -0
- package/dist/esm/codec-block85.d.ts +56 -0
- package/dist/esm/codec-block85.d.ts.map +1 -0
- package/dist/esm/codec-block85.js +267 -0
- package/dist/esm/codec-block85.js.map +1 -0
- package/dist/esm/codec-radix.d.ts +46 -0
- package/dist/esm/codec-radix.d.ts.map +1 -0
- package/dist/esm/codec-radix.js +177 -0
- package/dist/esm/codec-radix.js.map +1 -0
- package/dist/esm/codec.d.ts +25 -111
- package/dist/esm/codec.d.ts.map +1 -1
- package/dist/esm/codec.js +44 -226
- package/dist/esm/codec.js.map +1 -1
- package/dist/esm/codecs.d.ts +8 -59
- package/dist/esm/codecs.d.ts.map +1 -1
- package/dist/esm/codecs.js +7 -80
- package/dist/esm/codecs.js.map +1 -1
- package/dist/esm/errors.d.ts +65 -48
- package/dist/esm/errors.d.ts.map +1 -1
- package/dist/esm/errors.js +83 -50
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/example.js +45 -22
- package/dist/esm/example.js.map +1 -1
- package/dist/esm/functions.d.ts +27 -31
- package/dist/esm/functions.d.ts.map +1 -1
- package/dist/esm/functions.js +26 -48
- package/dist/esm/functions.js.map +1 -1
- package/dist/esm/index.d.ts +7 -5
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/package.json +12 -7
- package/dist/esm/types.d.ts +108 -71
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +12 -7
- package/src/algorithms.test.ts +442 -0
- package/src/bases.ts +98 -76
- package/src/bufferbase.ts +26 -36
- package/src/codec-base.ts +181 -0
- package/src/codec-block.ts +258 -0
- package/src/codec-block85.ts +315 -0
- package/src/codec-radix.ts +206 -0
- package/src/codec.test.ts +17 -9
- package/src/codec.ts +48 -247
- package/src/codecs.ts +11 -105
- package/src/errors.ts +90 -50
- package/src/example.ts +47 -22
- package/src/functions.ts +34 -54
- package/src/index.ts +27 -7
- package/src/types.ts +113 -86
package/src/functions.ts
CHANGED
|
@@ -1,93 +1,73 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import type { BufferLike, CodecSpec, DecodeOptions } from './types.js';
|
|
2
|
+
import type { Buffer } from 'node:buffer';
|
|
3
|
+
import { resolveSpec, type BaseName } from './bases.js';
|
|
4
|
+
import { getCodec } from './codec.js';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Gets or creates a codec for the specified base.
|
|
10
|
-
*/
|
|
11
|
-
function getCodec(base: BaseName | string): Codec {
|
|
12
|
-
const chars = resolveChars(base);
|
|
13
|
-
let codec = codecCache.get(chars);
|
|
14
|
-
if (!codec) {
|
|
15
|
-
codec = new Codec(chars);
|
|
16
|
-
codecCache.set(chars, codec);
|
|
17
|
-
}
|
|
18
|
-
return codec;
|
|
19
|
-
}
|
|
6
|
+
/** A named encoding, or a spec describing one directly. */
|
|
7
|
+
export type Base = BaseName | CodecSpec;
|
|
20
8
|
|
|
21
9
|
/**
|
|
22
10
|
* Encodes a buffer to a base-encoded string.
|
|
23
11
|
*
|
|
24
12
|
* @param buffer - Buffer or Uint8Array to encode
|
|
25
|
-
* @param base - Base name (e.g
|
|
26
|
-
* @returns Encoded string
|
|
13
|
+
* @param base - Base name (e.g. `'base58'`) or a codec spec
|
|
27
14
|
*
|
|
28
15
|
* @example
|
|
29
16
|
* ```typescript
|
|
30
|
-
* //
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* // Browser Uint8Array
|
|
34
|
-
* const encoded = encode(new Uint8Array([72, 101, 108, 108, 111]), 'base58');
|
|
17
|
+
* encode(Buffer.from('fo'), 'base64'); // 'Zm8='
|
|
18
|
+
* encode(Uint8Array.of(5), { alphabet: '01', algorithm: 'radix' }); // '101'
|
|
35
19
|
* ```
|
|
36
20
|
*/
|
|
37
|
-
export function encode(buffer: BufferLike, base:
|
|
38
|
-
return getCodec(base).encode(buffer);
|
|
21
|
+
export function encode(buffer: BufferLike, base: Base): string {
|
|
22
|
+
return getCodec(resolveSpec(base)).encode(buffer);
|
|
39
23
|
}
|
|
40
24
|
|
|
41
25
|
/**
|
|
42
26
|
* Decodes a base-encoded string to a buffer.
|
|
43
27
|
*
|
|
44
28
|
* @param encoded - String to decode
|
|
45
|
-
* @param base - Base name (e.g
|
|
46
|
-
* @param options - Decode options
|
|
47
|
-
* @
|
|
29
|
+
* @param base - Base name (e.g. `'base58'`) or a codec spec
|
|
30
|
+
* @param options - Decode options, such as a required `size`
|
|
31
|
+
* @throws {InvalidCharacterError} If a character is not in the alphabet
|
|
32
|
+
* @throws {InvalidLengthError} If the length cannot encode a whole number of bytes
|
|
33
|
+
* @throws {NonCanonicalError} If spare bits are set and the codec is strict
|
|
34
|
+
* @throws {BufferSizeError} If the result does not have the requested `size`
|
|
48
35
|
*
|
|
49
36
|
* @example
|
|
50
37
|
* ```typescript
|
|
51
|
-
*
|
|
52
|
-
*
|
|
38
|
+
* decode('Zm8=', 'base64'); // Buffer('fo')
|
|
39
|
+
* decode('9Ajdvzr', 'base58', { size: 32 });
|
|
53
40
|
* ```
|
|
54
41
|
*/
|
|
55
|
-
export function decode(encoded: string, base:
|
|
56
|
-
return getCodec(base).decode(encoded, options);
|
|
42
|
+
export function decode(encoded: string, base: Base, options?: DecodeOptions): Buffer {
|
|
43
|
+
return getCodec(resolveSpec(base)).decode(encoded, options);
|
|
57
44
|
}
|
|
58
45
|
|
|
59
46
|
/**
|
|
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
|
|
47
|
+
* Converts a string from one base to another, through the bytes they describe.
|
|
66
48
|
*
|
|
67
49
|
* @example
|
|
68
50
|
* ```typescript
|
|
69
|
-
*
|
|
51
|
+
* convert('9Ajdvzr', 'base58', 'base64url');
|
|
52
|
+
* convert(legacy, 'radix64', 'base64'); // migrate data written by bufferbase 2
|
|
70
53
|
* ```
|
|
71
54
|
*/
|
|
72
|
-
export function convert(input: string, from:
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
return
|
|
55
|
+
export function convert(input: string, from: Base, to: Base): string {
|
|
56
|
+
const source = getCodec(resolveSpec(from));
|
|
57
|
+
const target = getCodec(resolveSpec(to));
|
|
58
|
+
return source.convertTo(target, input);
|
|
76
59
|
}
|
|
77
60
|
|
|
78
61
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* @param input - String to validate
|
|
82
|
-
* @param base - Base name or character set
|
|
83
|
-
* @returns true if valid, false otherwise
|
|
62
|
+
* Reports whether `decode` would succeed for this input.
|
|
84
63
|
*
|
|
85
64
|
* @example
|
|
86
65
|
* ```typescript
|
|
87
|
-
* validate('
|
|
88
|
-
* validate('
|
|
66
|
+
* validate('Zm8=', 'base64'); // true
|
|
67
|
+
* validate('Zm9=', 'base64'); // false — spare bits are not zero
|
|
68
|
+
* validate('Zm9vd', 'base64'); // false — 5 characters cannot be whole bytes
|
|
89
69
|
* ```
|
|
90
70
|
*/
|
|
91
|
-
export function validate(input: string, base:
|
|
92
|
-
return getCodec(base).validate(input);
|
|
71
|
+
export function validate(input: string, base: Base): boolean {
|
|
72
|
+
return getCodec(resolveSpec(base)).validate(input);
|
|
93
73
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,18 +1,38 @@
|
|
|
1
1
|
// Types
|
|
2
|
-
export type {
|
|
2
|
+
export type {
|
|
3
|
+
Algorithm,
|
|
4
|
+
Block85Spec,
|
|
5
|
+
BlockSpec,
|
|
6
|
+
BufferLike,
|
|
7
|
+
CodecSpec,
|
|
8
|
+
DecodeOptions,
|
|
9
|
+
ICodec,
|
|
10
|
+
RadixSpec,
|
|
11
|
+
} from './types.js';
|
|
12
|
+
export type { BaseName } from './bases.js';
|
|
13
|
+
export type { Base } from './functions.js';
|
|
3
14
|
export type { CodecName } from './codecs.js';
|
|
4
15
|
|
|
5
16
|
// Errors
|
|
6
|
-
export {
|
|
17
|
+
export {
|
|
18
|
+
BufferSizeError,
|
|
19
|
+
CheckSymbolError,
|
|
20
|
+
InvalidCharacterError,
|
|
21
|
+
InvalidCodecError,
|
|
22
|
+
InvalidLengthError,
|
|
23
|
+
NonCanonicalError,
|
|
24
|
+
UnknownBaseError,
|
|
25
|
+
ValueRangeError,
|
|
26
|
+
} from './errors.js';
|
|
7
27
|
|
|
8
|
-
// Codec
|
|
9
|
-
export { Codec, createCodec } from './codec.js';
|
|
28
|
+
// Codec implementations and factory
|
|
29
|
+
export { Block85Codec, BlockCodec, Codec, RadixCodec, createCodec, getCodec } from './codec.js';
|
|
10
30
|
|
|
11
|
-
// Character sets and
|
|
12
|
-
export {
|
|
31
|
+
// Character sets, encoding definitions, and lookup helpers
|
|
32
|
+
export { Bases, Chars, isBaseName, resolveSpec } from './bases.js';
|
|
13
33
|
|
|
14
34
|
// Simple function API
|
|
15
|
-
export {
|
|
35
|
+
export { convert, decode, encode, validate } from './functions.js';
|
|
16
36
|
|
|
17
37
|
// Pre-defined codecs (namespaced)
|
|
18
38
|
export { Codecs } from './codecs.js';
|
package/src/types.ts
CHANGED
|
@@ -4,76 +4,128 @@ import { Buffer } from 'node:buffer';
|
|
|
4
4
|
* Input type accepted by encode functions.
|
|
5
5
|
*
|
|
6
6
|
* Supports both Node.js `Buffer` and standard `Uint8Array` for browser compatibility.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* // Node.js Buffer
|
|
11
|
-
* encode(Buffer.from('Hello'), 'base58');
|
|
12
|
-
*
|
|
13
|
-
* // Browser Uint8Array
|
|
14
|
-
* encode(new Uint8Array([72, 101, 108, 108, 111]), 'base58');
|
|
15
|
-
* ```
|
|
16
7
|
*/
|
|
17
8
|
export type BufferLike = Buffer | Uint8Array;
|
|
18
9
|
|
|
19
10
|
/**
|
|
20
|
-
*
|
|
11
|
+
* How bytes are mapped to characters.
|
|
21
12
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* - `'
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* - `'
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* - `'
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
13
|
+
* An alphabet alone does not determine an encoding: the same 64 characters can
|
|
14
|
+
* be used by a radix conversion or by a bit-block encoding, producing different
|
|
15
|
+
* output. Every codec therefore names its algorithm explicitly.
|
|
16
|
+
*
|
|
17
|
+
* - `'radix'` — treats the whole buffer as one big integer and rewrites it in
|
|
18
|
+
* base `alphabet.length`. Works with any alphabet and any input length.
|
|
19
|
+
* Leading zero bytes are preserved as leading `alphabet[0]` characters.
|
|
20
|
+
* Used by Base58, Base36, and custom alphabets. Not RFC 4648.
|
|
21
|
+
* - `'block'` — regroups the bit stream into `log2(alphabet.length)`-bit chunks,
|
|
22
|
+
* as specified by RFC 4648. Requires an alphabet of 2, 4, 8, 16, 32, or 64
|
|
23
|
+
* characters. Used by Base16, Base32, and Base64.
|
|
24
|
+
* - `'block85'` — converts each 4-byte block to 5 characters (85^5 > 2^32).
|
|
25
|
+
* Requires an alphabet of exactly 85 characters. Used by Ascii85 and Z85.
|
|
26
|
+
*/
|
|
27
|
+
export type Algorithm = 'radix' | 'block' | 'block85';
|
|
28
|
+
|
|
29
|
+
/** Options shared by every algorithm. */
|
|
30
|
+
interface CommonSpec {
|
|
31
|
+
/** Characters used for encoding, ordered by value. */
|
|
32
|
+
readonly alphabet: string;
|
|
33
|
+
/**
|
|
34
|
+
* Reject input that is well-formed but not the canonical encoding of any
|
|
35
|
+
* byte sequence (RFC 4648 §3.5). Defaults to `true`.
|
|
36
|
+
*
|
|
37
|
+
* With `strict: false`, `'Zm9='` and `'Zm8='` both decode to `'fo'`, so
|
|
38
|
+
* decoding is no longer injective. Only disable it to read data produced by
|
|
39
|
+
* a lenient encoder.
|
|
40
|
+
*/
|
|
41
|
+
readonly strict?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Accept either letter case when decoding, and when validating.
|
|
44
|
+
* Requires an alphabet that does not contain both cases of the same letter.
|
|
45
|
+
* Defaults to `false`.
|
|
46
|
+
*/
|
|
47
|
+
readonly caseInsensitive?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Extra characters accepted when decoding, each mapped to the alphabet
|
|
50
|
+
* character it stands for.
|
|
51
|
+
*
|
|
52
|
+
* Crockford's Base32 uses `{ I: '1', L: '1', O: '0' }` so that a symbol
|
|
53
|
+
* written down by hand is read back as the digit it resembles. Encoding
|
|
54
|
+
* never emits them.
|
|
55
|
+
*/
|
|
56
|
+
readonly aliases?: Readonly<Record<string, string>>;
|
|
57
|
+
/**
|
|
58
|
+
* Characters skipped when decoding, such as the hyphens Crockford's Base32
|
|
59
|
+
* allows for readability. Encoding never emits them.
|
|
60
|
+
*/
|
|
61
|
+
readonly ignore?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A codec that rewrites the buffer as an integer in base `alphabet.length`. */
|
|
65
|
+
export interface RadixSpec extends CommonSpec {
|
|
66
|
+
readonly algorithm: 'radix';
|
|
67
|
+
/**
|
|
68
|
+
* Symbols that extend the alphabet for a trailing check symbol, which
|
|
69
|
+
* encodes the value modulo `alphabet.length + checkSymbols.length`.
|
|
70
|
+
*
|
|
71
|
+
* Crockford's Base32 uses `'*~$=U'`, giving a modulus of 37. When set, every
|
|
72
|
+
* encoding carries the symbol and every decoding verifies it.
|
|
73
|
+
*/
|
|
74
|
+
readonly checkSymbols?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A codec that regroups the bit stream, as specified by RFC 4648. */
|
|
78
|
+
export interface BlockSpec extends CommonSpec {
|
|
79
|
+
readonly algorithm: 'block';
|
|
80
|
+
/**
|
|
81
|
+
* Character used to pad the final block to a whole number of characters,
|
|
82
|
+
* or `false` to emit no padding. Defaults to `'='`.
|
|
83
|
+
*/
|
|
84
|
+
readonly pad?: string | false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A codec that converts each 4-byte block to 5 characters. */
|
|
88
|
+
export interface Block85Spec extends CommonSpec {
|
|
89
|
+
readonly algorithm: 'block85';
|
|
90
|
+
/** Write a block of four zero bytes as `'z'`. Defaults to `false`. */
|
|
91
|
+
readonly zeroShortcut?: boolean;
|
|
92
|
+
/** Write a block of four spaces as `'y'`. Defaults to `false`. */
|
|
93
|
+
readonly spaceShortcut?: boolean;
|
|
94
|
+
/** Wrap the output in `'<~'` and `'~>'`. Defaults to `false`. */
|
|
95
|
+
readonly delimiters?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Require the input length to be a multiple of 4 bytes, as Z85 does.
|
|
98
|
+
* Defaults to `false`, which encodes a trailing partial block as
|
|
99
|
+
* `length + 1` characters.
|
|
100
|
+
*/
|
|
101
|
+
readonly requireFullBlocks?: boolean;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A complete codec definition: an alphabet plus the algorithm that uses it.
|
|
37
106
|
*
|
|
38
107
|
* @example
|
|
39
108
|
* ```typescript
|
|
40
|
-
* import { encode,
|
|
109
|
+
* import { encode, type CodecSpec } from 'bufferbase';
|
|
41
110
|
*
|
|
42
|
-
* const
|
|
43
|
-
*
|
|
111
|
+
* const binary: CodecSpec = { alphabet: '01', algorithm: 'radix' };
|
|
112
|
+
* encode(Uint8Array.of(5), binary); // '101'
|
|
44
113
|
* ```
|
|
45
114
|
*/
|
|
46
|
-
export type
|
|
47
|
-
| 'decimal'
|
|
48
|
-
| 'base16'
|
|
49
|
-
| 'hex'
|
|
50
|
-
| 'base32'
|
|
51
|
-
| 'base32crockford'
|
|
52
|
-
| 'base36'
|
|
53
|
-
| 'base52'
|
|
54
|
-
| 'base58'
|
|
55
|
-
| 'base64'
|
|
56
|
-
| 'base64url'
|
|
57
|
-
| 'base64xml'
|
|
58
|
-
| 'base64xmlname'
|
|
59
|
-
| 'ascii85'
|
|
60
|
-
| 'base85'
|
|
61
|
-
| 'z85';
|
|
115
|
+
export type CodecSpec = RadixSpec | BlockSpec | Block85Spec;
|
|
62
116
|
|
|
63
117
|
/**
|
|
64
118
|
* Options for decode operations.
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```typescript
|
|
68
|
-
* // Decode with fixed buffer size (pads with leading zeros)
|
|
69
|
-
* const buffer = decode(encoded, 'base58', { size: 32 });
|
|
70
|
-
* ```
|
|
71
119
|
*/
|
|
72
120
|
export interface DecodeOptions {
|
|
73
121
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
122
|
+
* Required byte length of the result.
|
|
123
|
+
*
|
|
124
|
+
* The decoded buffer must be exactly this long, otherwise `BufferSizeError`
|
|
125
|
+
* is thrown. Because leading zeros carry no value in a radix encoding, a
|
|
126
|
+
* `'radix'` codec pads a shorter result with leading zero bytes to reach it;
|
|
127
|
+
* `'block'` and `'block85'` codecs derive the length from the input and can
|
|
128
|
+
* only check it.
|
|
77
129
|
*/
|
|
78
130
|
size?: number;
|
|
79
131
|
}
|
|
@@ -81,49 +133,24 @@ export interface DecodeOptions {
|
|
|
81
133
|
/**
|
|
82
134
|
* Codec interface for encoding/decoding operations.
|
|
83
135
|
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```typescript
|
|
88
|
-
* import { Codecs } from 'bufferbase';
|
|
89
|
-
*
|
|
90
|
-
* const encoded = Codecs.base58.encode(Buffer.from('Hello'));
|
|
91
|
-
* const decoded = Codecs.base58.decode(encoded);
|
|
92
|
-
* ```
|
|
136
|
+
* `validate(input)` returns `true` exactly when `decode(input)` succeeds.
|
|
93
137
|
*/
|
|
94
138
|
export interface ICodec {
|
|
95
139
|
/** Character set used for encoding. */
|
|
96
140
|
readonly chars: string;
|
|
97
141
|
|
|
98
|
-
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*/
|
|
142
|
+
/** The algorithm this codec applies to `chars`. */
|
|
143
|
+
readonly algorithm: Algorithm;
|
|
144
|
+
|
|
145
|
+
/** Encodes a buffer to a base-encoded string. */
|
|
103
146
|
encode(buffer: BufferLike): string;
|
|
104
147
|
|
|
105
|
-
/**
|
|
106
|
-
* Decodes a base-encoded string to a buffer.
|
|
107
|
-
* @param encoded - The string to decode
|
|
108
|
-
* @param options - Decode options (e.g., fixed size)
|
|
109
|
-
* @returns Decoded buffer
|
|
110
|
-
* @throws {InvalidCharacterError} If the string contains invalid characters
|
|
111
|
-
* @throws {BufferSizeError} If the result exceeds the specified size
|
|
112
|
-
*/
|
|
148
|
+
/** Decodes a base-encoded string to a buffer. */
|
|
113
149
|
decode(encoded: string, options?: DecodeOptions): Buffer;
|
|
114
150
|
|
|
115
|
-
/**
|
|
116
|
-
* Validates if a string contains only valid characters for this base.
|
|
117
|
-
* @param input - The string to validate
|
|
118
|
-
* @returns `true` if valid, `false` otherwise
|
|
119
|
-
*/
|
|
151
|
+
/** Reports whether `decode` would succeed for this input. */
|
|
120
152
|
validate(input: string): boolean;
|
|
121
153
|
|
|
122
|
-
/**
|
|
123
|
-
* Converts a string from this base to another base.
|
|
124
|
-
* @param target - Target codec to convert to
|
|
125
|
-
* @param input - The string to convert
|
|
126
|
-
* @returns String in the target base encoding
|
|
127
|
-
*/
|
|
154
|
+
/** Decodes with this codec, then re-encodes with `target`. */
|
|
128
155
|
convertTo(target: ICodec, input: string): string;
|
|
129
156
|
}
|