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
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import type { BufferLike, DecodeOptions, RadixSpec } from './types.js';
|
|
3
|
+
import { BaseCodec } from './codec-base.js';
|
|
4
|
+
/**
|
|
5
|
+
* Reads the buffer as one big integer and rewrites it in base
|
|
6
|
+
* `alphabet.length`.
|
|
7
|
+
*
|
|
8
|
+
* This is the Base58 family of encodings. It accepts any alphabet and any
|
|
9
|
+
* input length, but it is not RFC 4648: the output length is not a fixed
|
|
10
|
+
* function of the input length, so it cannot be streamed, and encoding is
|
|
11
|
+
* O(n²) in the input size.
|
|
12
|
+
*
|
|
13
|
+
* Leading zero bytes carry no value in an integer, so they are recorded
|
|
14
|
+
* separately as leading `alphabet[0]` characters.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { RadixCodec } from 'bufferbase';
|
|
19
|
+
*
|
|
20
|
+
* const binary = new RadixCodec({ alphabet: '01', algorithm: 'radix' });
|
|
21
|
+
* binary.encode(Uint8Array.of(5)); // '101'
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class RadixCodec extends BaseCodec {
|
|
25
|
+
readonly algorithm: "radix";
|
|
26
|
+
/** Alphabet plus the extra check symbols, or `null` when unchecked. */
|
|
27
|
+
private readonly checkChars;
|
|
28
|
+
/** Value of every character a check symbol may be written as. */
|
|
29
|
+
private readonly checkValues;
|
|
30
|
+
constructor(spec: RadixSpec);
|
|
31
|
+
encode(input: BufferLike): string;
|
|
32
|
+
decode(encoded: string, options?: DecodeOptions): Buffer;
|
|
33
|
+
validate(input: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Decodes the body, verifying the check symbol when the codec carries one.
|
|
36
|
+
*
|
|
37
|
+
* Everything `decode` can reject other than `DecodeOptions.size` is checked
|
|
38
|
+
* here, so `validate` and `decode` always agree.
|
|
39
|
+
*/
|
|
40
|
+
private read;
|
|
41
|
+
/** Rewrites the digits of `body` as bytes, most significant first. */
|
|
42
|
+
private toBytes;
|
|
43
|
+
/** The value the bytes denote, modulo `divisor`. */
|
|
44
|
+
private remainderOf;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=codec-radix.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-radix.d.ts","sourceRoot":"","sources":["../../src/codec-radix.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvE,OAAO,EAAE,SAAS,EAAY,MAAM,iBAAiB,CAAC;AAStD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,UAAW,SAAQ,SAAS;IACvC,QAAQ,CAAC,SAAS,EAAG,OAAO,CAAU;IAEtC,uEAAuE;IACvE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgB;IAE3C,iEAAiE;IACjE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA8B;gBAE9C,IAAI,EAAE,SAAS;IA6B3B,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;IAgCjC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM;IAexD,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAmBhC;;;;;OAKG;IACH,OAAO,CAAC,IAAI;IAmBZ,sEAAsE;IACtE,OAAO,CAAC,OAAO;IAoCf,oDAAoD;IACpD,OAAO,CAAC,WAAW;CAOpB"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RadixCodec = void 0;
|
|
4
|
+
const node_buffer_1 = require("node:buffer");
|
|
5
|
+
const codec_base_js_1 = require("./codec-base.js");
|
|
6
|
+
const errors_js_1 = require("./errors.js");
|
|
7
|
+
/**
|
|
8
|
+
* Reads the buffer as one big integer and rewrites it in base
|
|
9
|
+
* `alphabet.length`.
|
|
10
|
+
*
|
|
11
|
+
* This is the Base58 family of encodings. It accepts any alphabet and any
|
|
12
|
+
* input length, but it is not RFC 4648: the output length is not a fixed
|
|
13
|
+
* function of the input length, so it cannot be streamed, and encoding is
|
|
14
|
+
* O(n²) in the input size.
|
|
15
|
+
*
|
|
16
|
+
* Leading zero bytes carry no value in an integer, so they are recorded
|
|
17
|
+
* separately as leading `alphabet[0]` characters.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { RadixCodec } from 'bufferbase';
|
|
22
|
+
*
|
|
23
|
+
* const binary = new RadixCodec({ alphabet: '01', algorithm: 'radix' });
|
|
24
|
+
* binary.encode(Uint8Array.of(5)); // '101'
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
class RadixCodec extends codec_base_js_1.BaseCodec {
|
|
28
|
+
constructor(spec) {
|
|
29
|
+
super(spec);
|
|
30
|
+
this.algorithm = 'radix';
|
|
31
|
+
const extra = spec.checkSymbols;
|
|
32
|
+
if (extra === undefined || extra === '') {
|
|
33
|
+
this.checkChars = null;
|
|
34
|
+
this.checkValues = new Map();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const combined = spec.alphabet + extra;
|
|
38
|
+
if (new Set(combined).size !== combined.length) {
|
|
39
|
+
throw new errors_js_1.InvalidCodecError('checkSymbols repeat a character of the alphabet');
|
|
40
|
+
}
|
|
41
|
+
// The symbol is matched by value, so that a case-insensitive codec accepts
|
|
42
|
+
// it in either case just as it does the rest of the string.
|
|
43
|
+
const values = new Map();
|
|
44
|
+
for (let i = 0; i < combined.length; i++) {
|
|
45
|
+
const char = combined[i];
|
|
46
|
+
values.set(char, i);
|
|
47
|
+
if (spec.caseInsensitive === true) {
|
|
48
|
+
values.set(char.toLowerCase(), i);
|
|
49
|
+
values.set(char.toUpperCase(), i);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
this.checkChars = combined;
|
|
53
|
+
this.checkValues = values;
|
|
54
|
+
}
|
|
55
|
+
encode(input) {
|
|
56
|
+
const buffer = (0, codec_base_js_1.toBuffer)(input);
|
|
57
|
+
const base = this.chars.length;
|
|
58
|
+
const digits = [];
|
|
59
|
+
for (const byte of buffer) {
|
|
60
|
+
let carry = byte;
|
|
61
|
+
for (let i = 0; i < digits.length; i++) {
|
|
62
|
+
carry += digits[i] * 256;
|
|
63
|
+
digits[i] = carry % base;
|
|
64
|
+
carry = Math.floor(carry / base);
|
|
65
|
+
}
|
|
66
|
+
while (carry > 0) {
|
|
67
|
+
digits.push(carry % base);
|
|
68
|
+
carry = Math.floor(carry / base);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (let i = 0; i < buffer.length && buffer[i] === 0; i++) {
|
|
72
|
+
digits.push(0);
|
|
73
|
+
}
|
|
74
|
+
let out = '';
|
|
75
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
76
|
+
out += this.chars[digits[i]];
|
|
77
|
+
}
|
|
78
|
+
if (this.checkChars !== null) {
|
|
79
|
+
out += this.checkChars[this.remainderOf(buffer, this.checkChars.length)];
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
decode(encoded, options) {
|
|
84
|
+
const bytes = this.read(this.strip(encoded));
|
|
85
|
+
const size = options?.size;
|
|
86
|
+
if (size === undefined) {
|
|
87
|
+
return node_buffer_1.Buffer.from(bytes);
|
|
88
|
+
}
|
|
89
|
+
if (bytes.length > size) {
|
|
90
|
+
throw new errors_js_1.BufferSizeError(bytes.length, size);
|
|
91
|
+
}
|
|
92
|
+
const padded = node_buffer_1.Buffer.alloc(size);
|
|
93
|
+
node_buffer_1.Buffer.from(bytes).copy(padded, size - bytes.length);
|
|
94
|
+
return padded;
|
|
95
|
+
}
|
|
96
|
+
validate(input) {
|
|
97
|
+
const stripped = this.strip(input);
|
|
98
|
+
if (this.checkChars === null) {
|
|
99
|
+
// Every string over the alphabet decodes, so the characters settle it.
|
|
100
|
+
for (let i = 0; i < stripped.length; i++) {
|
|
101
|
+
if (this.valueAt(stripped, i) < 0) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
this.read(stripped);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Decodes the body, verifying the check symbol when the codec carries one.
|
|
117
|
+
*
|
|
118
|
+
* Everything `decode` can reject other than `DecodeOptions.size` is checked
|
|
119
|
+
* here, so `validate` and `decode` always agree.
|
|
120
|
+
*/
|
|
121
|
+
read(input) {
|
|
122
|
+
let body = input;
|
|
123
|
+
if (this.checkChars !== null) {
|
|
124
|
+
if (body.length === 0) {
|
|
125
|
+
throw new errors_js_1.InvalidLengthError(0, 'a checked encoding always carries a check symbol');
|
|
126
|
+
}
|
|
127
|
+
const found = body.slice(-1);
|
|
128
|
+
body = body.slice(0, -1);
|
|
129
|
+
const bytes = this.toBytes(body);
|
|
130
|
+
const expected = this.remainderOf(bytes, this.checkChars.length);
|
|
131
|
+
if (this.checkValues.get(found) !== expected) {
|
|
132
|
+
throw new errors_js_1.CheckSymbolError(found, this.checkChars[expected]);
|
|
133
|
+
}
|
|
134
|
+
return bytes;
|
|
135
|
+
}
|
|
136
|
+
return this.toBytes(input);
|
|
137
|
+
}
|
|
138
|
+
/** Rewrites the digits of `body` as bytes, most significant first. */
|
|
139
|
+
toBytes(body) {
|
|
140
|
+
const base = this.chars.length;
|
|
141
|
+
const bytes = [];
|
|
142
|
+
let leadingZeros = 0;
|
|
143
|
+
let stillLeading = true;
|
|
144
|
+
for (let at = 0; at < body.length; at++) {
|
|
145
|
+
const value = this.valueAt(body, at);
|
|
146
|
+
if (value < 0) {
|
|
147
|
+
throw new errors_js_1.InvalidCharacterError(body[at]);
|
|
148
|
+
}
|
|
149
|
+
if (stillLeading && value === 0) {
|
|
150
|
+
leadingZeros++;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
stillLeading = false;
|
|
154
|
+
let carry = value;
|
|
155
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
156
|
+
carry += bytes[i] * base;
|
|
157
|
+
bytes[i] = carry % 256;
|
|
158
|
+
carry = Math.floor(carry / 256);
|
|
159
|
+
}
|
|
160
|
+
while (carry > 0) {
|
|
161
|
+
bytes.push(carry % 256);
|
|
162
|
+
carry = Math.floor(carry / 256);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
for (let i = 0; i < leadingZeros; i++) {
|
|
166
|
+
bytes.push(0);
|
|
167
|
+
}
|
|
168
|
+
bytes.reverse();
|
|
169
|
+
return bytes;
|
|
170
|
+
}
|
|
171
|
+
/** The value the bytes denote, modulo `divisor`. */
|
|
172
|
+
remainderOf(bytes, divisor) {
|
|
173
|
+
let remainder = 0;
|
|
174
|
+
for (const byte of bytes) {
|
|
175
|
+
remainder = (remainder * 256 + byte) % divisor;
|
|
176
|
+
}
|
|
177
|
+
return remainder;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.RadixCodec = RadixCodec;
|
|
181
|
+
//# sourceMappingURL=codec-radix.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-radix.js","sourceRoot":"","sources":["../../src/codec-radix.ts"],"names":[],"mappings":";;;AAAA,6CAAqC;AAErC,mDAAsD;AACtD,2CAMqB;AAErB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,UAAW,SAAQ,yBAAS;IASvC,YAAY,IAAe;QACzB,KAAK,CAAC,IAAI,CAAC,CAAC;QATL,cAAS,GAAG,OAAgB,CAAC;QAWpC,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC;QAChC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;YACxC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YACvB,IAAI,CAAC,WAAW,GAAG,IAAI,GAAG,EAAE,CAAC;YAC7B,OAAO;QACT,CAAC;QACD,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACvC,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;YAC/C,MAAM,IAAI,6BAAiB,CAAC,iDAAiD,CAAC,CAAC;QACjF,CAAC;QAED,2EAA2E;QAC3E,4DAA4D;QAC5D,MAAM,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;QACzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EA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AAC,GAAG,KAAK,GAAG,GAAG,CAAC;gBACvB,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;YAClC,CAAC;YACD,OAAO,KAAK,GAAG,CAAC,EAAE,CAAC;gBACjB,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;gBACxB,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;YAClC,CAAC;QACH,CAAC;QAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAChB,CAAC;QACD,KAAK,CAAC,OAAO,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,oDAAoD;IAC5C,WAAW,CAAC,KAAuB,EAAE,OAAe;QAC1D,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,SAAS,GAAG,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC;QACjD,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AA9KD,gCA8KC"}
|
package/dist/cjs/codec.d.ts
CHANGED
|
@@ -1,124 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { CodecSpec, ICodec } from './types.js';
|
|
2
|
+
import { RadixCodec } from './codec-radix.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Builds the codec described by a spec.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* A bare string is read as a custom alphabet for a radix conversion, which is
|
|
7
|
+
* the only algorithm that works with an alphabet of any length.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```typescript
|
|
11
|
-
* import {
|
|
12
|
-
*
|
|
13
|
-
* // Use pre-defined codec
|
|
14
|
-
* const encoded = Codecs.base58.encode(Buffer.from('Hello'));
|
|
11
|
+
* import { createCodec } from 'bufferbase';
|
|
15
12
|
*
|
|
16
|
-
* // Create custom codec
|
|
17
13
|
* const binary = createCodec('01');
|
|
18
|
-
*
|
|
14
|
+
* binary.encode(Uint8Array.of(5)); // '101'
|
|
15
|
+
*
|
|
16
|
+
* const paddedHex = createCodec({ alphabet: '0123456789abcdef', algorithm: 'block' });
|
|
17
|
+
* paddedHex.encode(Uint8Array.of(0, 1)); // '0001'
|
|
19
18
|
* ```
|
|
20
19
|
*/
|
|
21
|
-
export declare
|
|
22
|
-
/** Character set used for encoding. */
|
|
23
|
-
readonly chars: string;
|
|
24
|
-
/** @internal */
|
|
25
|
-
private readonly charToIndex;
|
|
26
|
-
/**
|
|
27
|
-
* Creates a new codec with the specified character set.
|
|
28
|
-
* @param chars - The character set to use for encoding/decoding
|
|
29
|
-
*/
|
|
30
|
-
constructor(chars: string);
|
|
31
|
-
/**
|
|
32
|
-
* Encodes a buffer to a base-encoded string.
|
|
33
|
-
*
|
|
34
|
-
* Leading zero bytes in the buffer are preserved as the first character
|
|
35
|
-
* of the encoding alphabet (e.g., '1' for Base58).
|
|
36
|
-
*
|
|
37
|
-
* @param input - The buffer or Uint8Array to encode
|
|
38
|
-
* @returns Base-encoded string
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```typescript
|
|
42
|
-
* const codec = Codecs.base58;
|
|
43
|
-
* codec.encode(Buffer.from('Hello')); // '9Ajdvzr'
|
|
44
|
-
* codec.encode(new Uint8Array([0, 0, 1])); // '112' (preserves leading zeros)
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
encode(input: BufferLike): string;
|
|
48
|
-
/**
|
|
49
|
-
* Decodes a base-encoded string to a buffer.
|
|
50
|
-
*
|
|
51
|
-
* Leading characters that represent zero (first char of alphabet)
|
|
52
|
-
* are converted to leading zero bytes in the result.
|
|
53
|
-
*
|
|
54
|
-
* @param encoded - The string to decode
|
|
55
|
-
* @param options - Decode options
|
|
56
|
-
* @returns Decoded buffer
|
|
57
|
-
* @throws {InvalidCharacterError} If the string contains invalid characters
|
|
58
|
-
* @throws {BufferSizeError} If the result exceeds the specified size
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```typescript
|
|
62
|
-
* const codec = Codecs.base58;
|
|
63
|
-
* codec.decode('9Ajdvzr'); // Buffer('Hello')
|
|
64
|
-
* codec.decode('9Ajdvzr', { size: 32 }); // Pads to 32 bytes
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
decode(encoded: string, options?: DecodeOptions): Buffer;
|
|
68
|
-
/**
|
|
69
|
-
* Validates if a string contains only valid characters for this base.
|
|
70
|
-
*
|
|
71
|
-
* This is a fast check that doesn't perform full decoding.
|
|
72
|
-
*
|
|
73
|
-
* @param input - The string to validate
|
|
74
|
-
* @returns `true` if all characters are valid, `false` otherwise
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```typescript
|
|
78
|
-
* Codecs.base58.validate('9Ajdvzr'); // true
|
|
79
|
-
* Codecs.base58.validate('0invalid'); // false ('0' not in Base58)
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
validate(input: string): boolean;
|
|
83
|
-
/**
|
|
84
|
-
* Converts a string from this base to another base.
|
|
85
|
-
*
|
|
86
|
-
* Internally decodes to a buffer, then encodes with the target codec.
|
|
87
|
-
*
|
|
88
|
-
* @param target - Target codec to convert to
|
|
89
|
-
* @param input - The string to convert
|
|
90
|
-
* @returns String in the target base encoding
|
|
91
|
-
* @throws {InvalidCharacterError} If the input contains invalid characters
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```typescript
|
|
95
|
-
* const base58str = '9Ajdvzr';
|
|
96
|
-
* const base64str = Codecs.base58.convertTo(Codecs.base64url, base58str);
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
convertTo(target: ICodec, input: string): string;
|
|
100
|
-
}
|
|
20
|
+
export declare function createCodec(spec: CodecSpec | string): ICodec;
|
|
101
21
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* Use this to create codecs for custom base encodings.
|
|
105
|
-
* The base is determined by the length of the character set.
|
|
106
|
-
*
|
|
107
|
-
* @param chars - The character set to use for encoding/decoding
|
|
108
|
-
* @returns A new Codec instance
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```typescript
|
|
112
|
-
* import { createCodec } from 'bufferbase';
|
|
22
|
+
* Returns the codec for a spec, building it once per spec object.
|
|
113
23
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* binary.encode(Buffer.from([5])); // '101'
|
|
117
|
-
*
|
|
118
|
-
* // Octal codec (base 8)
|
|
119
|
-
* const octal = createCodec('01234567');
|
|
120
|
-
* octal.encode(Buffer.from([255])); // '377'
|
|
121
|
-
* ```
|
|
24
|
+
* Named bases always resolve to the same spec object, so they are built once
|
|
25
|
+
* for the lifetime of the process.
|
|
122
26
|
*/
|
|
123
|
-
export declare function
|
|
27
|
+
export declare function getCodec(spec: CodecSpec): ICodec;
|
|
28
|
+
export { RadixCodec } from './codec-radix.js';
|
|
29
|
+
export { BlockCodec } from './codec-block.js';
|
|
30
|
+
export { Block85Codec } from './codec-block85.js';
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated Use `createCodec({ alphabet, algorithm: 'radix' })`, or
|
|
33
|
+
* `createCodec(alphabet)` for the same thing.
|
|
34
|
+
*/
|
|
35
|
+
export declare class Codec extends RadixCodec {
|
|
36
|
+
constructor(chars: string);
|
|
37
|
+
}
|
|
124
38
|
//# sourceMappingURL=codec.d.ts.map
|
package/dist/cjs/codec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAI9C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,GAAG,MAAM,CAY5D;AAID;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAQhD;AAED,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;GAGG;AACH,qBAAa,KAAM,SAAQ,UAAU;gBACvB,KAAK,EAAE,MAAM;CAG1B"}
|