bufferbase 1.3.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +186 -17
- package/dist/cjs/bases.d.ts +189 -0
- package/dist/cjs/bases.d.ts.map +1 -0
- package/dist/cjs/bases.js +134 -0
- package/dist/cjs/bases.js.map +1 -0
- package/dist/cjs/bufferbase.d.ts +22 -45
- package/dist/cjs/bufferbase.d.ts.map +1 -1
- package/dist/cjs/bufferbase.js +26 -120
- 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 +38 -0
- package/dist/cjs/codec.d.ts.map +1 -0
- package/dist/cjs/codec.js +69 -0
- package/dist/cjs/codec.js.map +1 -0
- package/dist/cjs/codecs.d.ts +20 -0
- package/dist/cjs/codecs.d.ts.map +1 -0
- package/dist/cjs/codecs.js +19 -0
- package/dist/cjs/codecs.js.map +1 -0
- package/dist/cjs/errors.d.ts +93 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +130 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/example.js +49 -33
- package/dist/cjs/example.js.map +1 -1
- package/dist/cjs/functions.d.ts +58 -0
- package/dist/cjs/functions.d.ts.map +1 -0
- package/dist/cjs/functions.js +71 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.d.ts +10 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +35 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +17 -12
- package/dist/cjs/types.d.ts +144 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/bases.d.ts +189 -0
- package/dist/esm/bases.d.ts.map +1 -0
- package/dist/esm/bases.js +129 -0
- package/dist/esm/bases.js.map +1 -0
- package/dist/esm/bufferbase.d.ts +22 -45
- package/dist/esm/bufferbase.d.ts.map +1 -1
- package/dist/esm/bufferbase.js +21 -115
- 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 +38 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +60 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/codecs.d.ts +20 -0
- package/dist/esm/codecs.d.ts.map +1 -0
- package/dist/esm/codecs.js +16 -0
- package/dist/esm/codecs.js.map +1 -0
- package/dist/esm/errors.d.ts +93 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +119 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/example.js +48 -32
- package/dist/esm/example.js.map +1 -1
- package/dist/esm/functions.d.ts +58 -0
- package/dist/esm/functions.d.ts.map +1 -0
- package/dist/esm/functions.js +65 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.d.ts +10 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +12 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/package.json +17 -12
- package/dist/esm/types.d.ts +144 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +19 -14
- package/src/algorithms.test.ts +442 -0
- package/src/bases.ts +142 -0
- package/src/bufferbase.test.ts +44 -27
- package/src/bufferbase.ts +32 -117
- 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 +328 -0
- package/src/codec.ts +67 -0
- package/src/codecs.ts +24 -0
- package/src/errors.ts +127 -0
- package/src/example.ts +49 -34
- package/src/functions.ts +73 -0
- package/src/index.ts +39 -6
- package/src/types.ts +156 -0
- package/dist/cjs/bufferbase.d.mts +0 -60
- package/dist/cjs/bufferbase.d.mts.map +0 -1
- package/dist/cjs/bufferbase.mjs +0 -150
- package/dist/cjs/bufferbase.mjs.map +0 -1
- package/dist/cjs/example.d.mts +0 -2
- package/dist/cjs/example.d.mts.map +0 -1
- package/dist/cjs/example.mjs +0 -39
- package/dist/cjs/example.mjs.map +0 -1
- package/dist/cjs/index.d.mts +0 -2
- package/dist/cjs/index.d.mts.map +0 -1
- package/dist/cjs/index.mjs +0 -11
- package/dist/cjs/index.mjs.map +0 -1
- package/dist/esm/bufferbase.d.mts +0 -60
- package/dist/esm/bufferbase.d.mts.map +0 -1
- package/dist/esm/bufferbase.mjs +0 -141
- package/dist/esm/bufferbase.mjs.map +0 -1
- package/dist/esm/example.d.mts +0 -2
- package/dist/esm/example.d.mts.map +0 -1
- package/dist/esm/example.mjs +0 -37
- package/dist/esm/example.mjs.map +0 -1
- package/dist/esm/index.d.mts +0 -2
- package/dist/esm/index.d.mts.map +0 -1
- package/dist/esm/index.mjs +0 -2
- package/dist/esm/index.mjs.map +0 -1
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Codec = exports.Block85Codec = exports.BlockCodec = exports.RadixCodec = void 0;
|
|
4
|
+
exports.createCodec = createCodec;
|
|
5
|
+
exports.getCodec = getCodec;
|
|
6
|
+
const codec_radix_js_1 = require("./codec-radix.js");
|
|
7
|
+
const codec_block_js_1 = require("./codec-block.js");
|
|
8
|
+
const codec_block85_js_1 = require("./codec-block85.js");
|
|
9
|
+
/**
|
|
10
|
+
* Builds the codec described by a spec.
|
|
11
|
+
*
|
|
12
|
+
* A bare string is read as a custom alphabet for a radix conversion, which is
|
|
13
|
+
* the only algorithm that works with an alphabet of any length.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { createCodec } from 'bufferbase';
|
|
18
|
+
*
|
|
19
|
+
* const binary = createCodec('01');
|
|
20
|
+
* binary.encode(Uint8Array.of(5)); // '101'
|
|
21
|
+
*
|
|
22
|
+
* const paddedHex = createCodec({ alphabet: '0123456789abcdef', algorithm: 'block' });
|
|
23
|
+
* paddedHex.encode(Uint8Array.of(0, 1)); // '0001'
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function createCodec(spec) {
|
|
27
|
+
const resolved = typeof spec === 'string' ? { alphabet: spec, algorithm: 'radix' } : spec;
|
|
28
|
+
switch (resolved.algorithm) {
|
|
29
|
+
case 'radix':
|
|
30
|
+
return new codec_radix_js_1.RadixCodec(resolved);
|
|
31
|
+
case 'block':
|
|
32
|
+
return new codec_block_js_1.BlockCodec(resolved);
|
|
33
|
+
case 'block85':
|
|
34
|
+
return new codec_block85_js_1.Block85Codec(resolved);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const cache = new WeakMap();
|
|
38
|
+
/**
|
|
39
|
+
* Returns the codec for a spec, building it once per spec object.
|
|
40
|
+
*
|
|
41
|
+
* Named bases always resolve to the same spec object, so they are built once
|
|
42
|
+
* for the lifetime of the process.
|
|
43
|
+
*/
|
|
44
|
+
function getCodec(spec) {
|
|
45
|
+
const cached = cache.get(spec);
|
|
46
|
+
if (cached !== undefined) {
|
|
47
|
+
return cached;
|
|
48
|
+
}
|
|
49
|
+
const codec = createCodec(spec);
|
|
50
|
+
cache.set(spec, codec);
|
|
51
|
+
return codec;
|
|
52
|
+
}
|
|
53
|
+
var codec_radix_js_2 = require("./codec-radix.js");
|
|
54
|
+
Object.defineProperty(exports, "RadixCodec", { enumerable: true, get: function () { return codec_radix_js_2.RadixCodec; } });
|
|
55
|
+
var codec_block_js_2 = require("./codec-block.js");
|
|
56
|
+
Object.defineProperty(exports, "BlockCodec", { enumerable: true, get: function () { return codec_block_js_2.BlockCodec; } });
|
|
57
|
+
var codec_block85_js_2 = require("./codec-block85.js");
|
|
58
|
+
Object.defineProperty(exports, "Block85Codec", { enumerable: true, get: function () { return codec_block85_js_2.Block85Codec; } });
|
|
59
|
+
/**
|
|
60
|
+
* @deprecated Use `createCodec({ alphabet, algorithm: 'radix' })`, or
|
|
61
|
+
* `createCodec(alphabet)` for the same thing.
|
|
62
|
+
*/
|
|
63
|
+
class Codec extends codec_radix_js_1.RadixCodec {
|
|
64
|
+
constructor(chars) {
|
|
65
|
+
super({ alphabet: chars, algorithm: 'radix' });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.Codec = Codec;
|
|
69
|
+
//# sourceMappingURL=codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":";;;AAsBA,kCAYC;AAUD,4BAQC;AAnDD,qDAA8C;AAC9C,qDAA8C;AAC9C,yDAAkD;AAElD;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,WAAW,CAAC,IAAwB;IAClD,MAAM,QAAQ,GACZ,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IAE3E,QAAQ,QAAQ,CAAC,SAAS,EAAE,CAAC;QAC3B,KAAK,OAAO;YACV,OAAO,IAAI,2BAAU,CAAC,QAAQ,CAAC,CAAC;QAClC,KAAK,OAAO;YACV,OAAO,IAAI,2BAAU,CAAC,QAAQ,CAAC,CAAC;QAClC,KAAK,SAAS;YACZ,OAAO,IAAI,+BAAY,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC;AACH,CAAC;AAED,MAAM,KAAK,GAAG,IAAI,OAAO,EAAqB,CAAC;AAE/C;;;;;GAKG;AACH,SAAgB,QAAQ,CAAC,IAAe;IACtC,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/B,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAChC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACvB,OAAO,KAAK,CAAC;AACf,CAAC;AAED,mDAA8C;AAArC,4GAAA,UAAU,OAAA;AACnB,mDAA8C;AAArC,4GAAA,UAAU,OAAA;AACnB,uDAAkD;AAAzC,gHAAA,YAAY,OAAA;AAErB;;;GAGG;AACH,MAAa,KAAM,SAAQ,2BAAU;IACnC,YAAY,KAAa;QACvB,KAAK,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC;IACjD,CAAC;CACF;AAJD,sBAIC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ICodec } from './types.js';
|
|
2
|
+
import { type BaseName } from './bases.js';
|
|
3
|
+
/**
|
|
4
|
+
* A ready-made codec for every name in `Bases`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { Codecs } from 'bufferbase';
|
|
9
|
+
*
|
|
10
|
+
* Codecs.base58.encode(Buffer.from('Hello')); // '9Ajdvzr'
|
|
11
|
+
* Codecs.base64.encode(Buffer.from('fo')); // 'Zm8='
|
|
12
|
+
* Codecs.base58.convertTo(Codecs.base64url, '9Ajdvzr');
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare const Codecs: Record<BaseName, ICodec>;
|
|
16
|
+
/**
|
|
17
|
+
* Type representing valid codec names in the `Codecs` object.
|
|
18
|
+
*/
|
|
19
|
+
export type CodecName = BaseName;
|
|
20
|
+
//# sourceMappingURL=codecs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codecs.d.ts","sourceRoot":"","sources":["../../src/codecs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAS,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGlD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,MAAM,EAEd,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AAE9B;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,QAAQ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Codecs = void 0;
|
|
4
|
+
const bases_js_1 = require("./bases.js");
|
|
5
|
+
const codec_js_1 = require("./codec.js");
|
|
6
|
+
/**
|
|
7
|
+
* A ready-made codec for every name in `Bases`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Codecs } from 'bufferbase';
|
|
12
|
+
*
|
|
13
|
+
* Codecs.base58.encode(Buffer.from('Hello')); // '9Ajdvzr'
|
|
14
|
+
* Codecs.base64.encode(Buffer.from('fo')); // 'Zm8='
|
|
15
|
+
* Codecs.base58.convertTo(Codecs.base64url, '9Ajdvzr');
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
exports.Codecs = Object.fromEntries(Object.entries(bases_js_1.Bases).map(([name, spec]) => [name, (0, codec_js_1.getCodec)(spec)]));
|
|
19
|
+
//# sourceMappingURL=codecs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codecs.js","sourceRoot":"","sources":["../../src/codecs.ts"],"names":[],"mappings":";;;AACA,yCAAkD;AAClD,yCAAsC;AAEtC;;;;;;;;;;;GAWG;AACU,QAAA,MAAM,GAAG,MAAM,CAAC,WAAW,CACtC,MAAM,CAAC,OAAO,CAAC,gBAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,IAAA,mBAAQ,EAAC,IAAI,CAAC,CAAC,CAAC,CACxC,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an invalid character is encountered during decoding.
|
|
3
|
+
*/
|
|
4
|
+
export declare class InvalidCharacterError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* @param char - The invalid character that was encountered (optional)
|
|
7
|
+
*/
|
|
8
|
+
constructor(char?: string);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Error thrown when the decoded buffer does not have the size requested
|
|
12
|
+
* through `DecodeOptions.size`.
|
|
13
|
+
*/
|
|
14
|
+
export declare class BufferSizeError extends Error {
|
|
15
|
+
/**
|
|
16
|
+
* @param actual - The actual decoded buffer size
|
|
17
|
+
* @param expected - The expected (specified) buffer size
|
|
18
|
+
*/
|
|
19
|
+
constructor(actual: number, expected: number);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Error thrown when an unknown base name is provided.
|
|
23
|
+
*/
|
|
24
|
+
export declare class UnknownBaseError extends Error {
|
|
25
|
+
/**
|
|
26
|
+
* @param name - The unknown base name that was provided
|
|
27
|
+
*/
|
|
28
|
+
constructor(name: string);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Error thrown when a string cannot be the encoding of any byte sequence
|
|
32
|
+
* because of its length.
|
|
33
|
+
*
|
|
34
|
+
* A Base64 string of 5 characters, for example, describes 30 bits: too many
|
|
35
|
+
* for 3 bytes and too few for 4.
|
|
36
|
+
*/
|
|
37
|
+
export declare class InvalidLengthError extends Error {
|
|
38
|
+
/**
|
|
39
|
+
* @param length - The length that cannot occur
|
|
40
|
+
* @param detail - What the length should have been
|
|
41
|
+
*/
|
|
42
|
+
constructor(length: number, detail: string);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Error thrown when input is well-formed but is not the canonical encoding of
|
|
46
|
+
* the bytes it decodes to (RFC 4648 §3.5).
|
|
47
|
+
*
|
|
48
|
+
* Accepting such input makes decoding non-injective: `'Zm8='` and `'Zm9='`
|
|
49
|
+
* would both decode to `'fo'`. Pass `strict: false` in the codec spec to allow
|
|
50
|
+
* it.
|
|
51
|
+
*/
|
|
52
|
+
export declare class NonCanonicalError extends Error {
|
|
53
|
+
/**
|
|
54
|
+
* @param detail - What made the input non-canonical
|
|
55
|
+
*/
|
|
56
|
+
constructor(detail: string);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Error thrown when a codec spec cannot be realised, such as a `'block'`
|
|
60
|
+
* algorithm with an alphabet whose length is not a power of two.
|
|
61
|
+
*/
|
|
62
|
+
export declare class InvalidCodecError extends Error {
|
|
63
|
+
/**
|
|
64
|
+
* @param detail - Why the spec is unusable
|
|
65
|
+
*/
|
|
66
|
+
constructor(detail: string);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Error thrown when a group of characters denotes a number too large for the
|
|
70
|
+
* bytes it is supposed to encode.
|
|
71
|
+
*
|
|
72
|
+
* In an Ascii85 group, five characters must denote a value below 2^32; `'s8W-!'`
|
|
73
|
+
* does not, so it encodes nothing. Unlike `NonCanonicalError` this is never
|
|
74
|
+
* allowed, because there is no byte sequence to decode to.
|
|
75
|
+
*/
|
|
76
|
+
export declare class ValueRangeError extends Error {
|
|
77
|
+
/**
|
|
78
|
+
* @param group - The group of characters that is out of range
|
|
79
|
+
*/
|
|
80
|
+
constructor(group: string);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Error thrown when a trailing check symbol does not match the value it
|
|
84
|
+
* follows, which means the string was mistyped or corrupted.
|
|
85
|
+
*/
|
|
86
|
+
export declare class CheckSymbolError extends Error {
|
|
87
|
+
/**
|
|
88
|
+
* @param found - The check symbol present in the input
|
|
89
|
+
* @param expected - The check symbol the value calls for
|
|
90
|
+
*/
|
|
91
|
+
constructor(found: string, expected: string);
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -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;;;GAGG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC;;;OAGG;gBACS,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;CAI7C;AAED;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC;;OAEG;gBACS,IAAI,EAAE,MAAM;CAOzB;AAED;;;;;;GAMG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C;;;OAGG;gBACS,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAI3C;AAED;;;;;;;GAOG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C;;OAEG;gBACS,MAAM,EAAE,MAAM;CAI3B;AAED;;;GAGG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C;;OAEG;gBACS,MAAM,EAAE,MAAM;CAI3B;AAED;;;;;;;GAOG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC;;OAEG;gBACS,KAAK,EAAE,MAAM;CAI1B;AAED;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC;;;OAGG;gBACS,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;CAI5C"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CheckSymbolError = exports.ValueRangeError = exports.InvalidCodecError = exports.NonCanonicalError = exports.InvalidLengthError = exports.UnknownBaseError = exports.BufferSizeError = exports.InvalidCharacterError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when an invalid character is encountered during decoding.
|
|
6
|
+
*/
|
|
7
|
+
class InvalidCharacterError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* @param char - The invalid character that was encountered (optional)
|
|
10
|
+
*/
|
|
11
|
+
constructor(char) {
|
|
12
|
+
const message = char ? `Invalid character found: '${char}'` : 'Invalid character found';
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'InvalidCharacterError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.InvalidCharacterError = InvalidCharacterError;
|
|
18
|
+
/**
|
|
19
|
+
* Error thrown when the decoded buffer does not have the size requested
|
|
20
|
+
* through `DecodeOptions.size`.
|
|
21
|
+
*/
|
|
22
|
+
class BufferSizeError extends Error {
|
|
23
|
+
/**
|
|
24
|
+
* @param actual - The actual decoded buffer size
|
|
25
|
+
* @param expected - The expected (specified) buffer size
|
|
26
|
+
*/
|
|
27
|
+
constructor(actual, expected) {
|
|
28
|
+
super(`Buffer size ${actual} does not match specified size ${expected}`);
|
|
29
|
+
this.name = 'BufferSizeError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.BufferSizeError = BufferSizeError;
|
|
33
|
+
/**
|
|
34
|
+
* Error thrown when an unknown base name is provided.
|
|
35
|
+
*/
|
|
36
|
+
class UnknownBaseError extends Error {
|
|
37
|
+
/**
|
|
38
|
+
* @param name - The unknown base name that was provided
|
|
39
|
+
*/
|
|
40
|
+
constructor(name) {
|
|
41
|
+
super(`Unknown base encoding: '${name}'. ` +
|
|
42
|
+
'Pass a CodecSpec such as { alphabet, algorithm: \'radix\' } to use a custom alphabet.');
|
|
43
|
+
this.name = 'UnknownBaseError';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.UnknownBaseError = UnknownBaseError;
|
|
47
|
+
/**
|
|
48
|
+
* Error thrown when a string cannot be the encoding of any byte sequence
|
|
49
|
+
* because of its length.
|
|
50
|
+
*
|
|
51
|
+
* A Base64 string of 5 characters, for example, describes 30 bits: too many
|
|
52
|
+
* for 3 bytes and too few for 4.
|
|
53
|
+
*/
|
|
54
|
+
class InvalidLengthError extends Error {
|
|
55
|
+
/**
|
|
56
|
+
* @param length - The length that cannot occur
|
|
57
|
+
* @param detail - What the length should have been
|
|
58
|
+
*/
|
|
59
|
+
constructor(length, detail) {
|
|
60
|
+
super(`Invalid encoded length ${length}: ${detail}`);
|
|
61
|
+
this.name = 'InvalidLengthError';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.InvalidLengthError = InvalidLengthError;
|
|
65
|
+
/**
|
|
66
|
+
* Error thrown when input is well-formed but is not the canonical encoding of
|
|
67
|
+
* the bytes it decodes to (RFC 4648 §3.5).
|
|
68
|
+
*
|
|
69
|
+
* Accepting such input makes decoding non-injective: `'Zm8='` and `'Zm9='`
|
|
70
|
+
* would both decode to `'fo'`. Pass `strict: false` in the codec spec to allow
|
|
71
|
+
* it.
|
|
72
|
+
*/
|
|
73
|
+
class NonCanonicalError extends Error {
|
|
74
|
+
/**
|
|
75
|
+
* @param detail - What made the input non-canonical
|
|
76
|
+
*/
|
|
77
|
+
constructor(detail) {
|
|
78
|
+
super(`Non-canonical encoding: ${detail}`);
|
|
79
|
+
this.name = 'NonCanonicalError';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.NonCanonicalError = NonCanonicalError;
|
|
83
|
+
/**
|
|
84
|
+
* Error thrown when a codec spec cannot be realised, such as a `'block'`
|
|
85
|
+
* algorithm with an alphabet whose length is not a power of two.
|
|
86
|
+
*/
|
|
87
|
+
class InvalidCodecError extends Error {
|
|
88
|
+
/**
|
|
89
|
+
* @param detail - Why the spec is unusable
|
|
90
|
+
*/
|
|
91
|
+
constructor(detail) {
|
|
92
|
+
super(`Invalid codec: ${detail}`);
|
|
93
|
+
this.name = 'InvalidCodecError';
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.InvalidCodecError = InvalidCodecError;
|
|
97
|
+
/**
|
|
98
|
+
* Error thrown when a group of characters denotes a number too large for the
|
|
99
|
+
* bytes it is supposed to encode.
|
|
100
|
+
*
|
|
101
|
+
* In an Ascii85 group, five characters must denote a value below 2^32; `'s8W-!'`
|
|
102
|
+
* does not, so it encodes nothing. Unlike `NonCanonicalError` this is never
|
|
103
|
+
* allowed, because there is no byte sequence to decode to.
|
|
104
|
+
*/
|
|
105
|
+
class ValueRangeError extends Error {
|
|
106
|
+
/**
|
|
107
|
+
* @param group - The group of characters that is out of range
|
|
108
|
+
*/
|
|
109
|
+
constructor(group) {
|
|
110
|
+
super(`Encoded group '${group}' exceeds the maximum value for its block`);
|
|
111
|
+
this.name = 'ValueRangeError';
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.ValueRangeError = ValueRangeError;
|
|
115
|
+
/**
|
|
116
|
+
* Error thrown when a trailing check symbol does not match the value it
|
|
117
|
+
* follows, which means the string was mistyped or corrupted.
|
|
118
|
+
*/
|
|
119
|
+
class CheckSymbolError extends Error {
|
|
120
|
+
/**
|
|
121
|
+
* @param found - The check symbol present in the input
|
|
122
|
+
* @param expected - The check symbol the value calls for
|
|
123
|
+
*/
|
|
124
|
+
constructor(found, expected) {
|
|
125
|
+
super(`Check symbol '${found}' does not match the expected '${expected}'`);
|
|
126
|
+
this.name = 'CheckSymbolError';
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.CheckSymbolError = CheckSymbolError;
|
|
130
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,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;AATD,sDASC;AAED;;;GAGG;AACH,MAAa,eAAgB,SAAQ,KAAK;IACxC;;;OAGG;IACH,YAAY,MAAc,EAAE,QAAgB;QAC1C,KAAK,CAAC,eAAe,MAAM,kCAAkC,QAAQ,EAAE,CAAC,CAAC;QACzE,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AATD,0CASC;AAED;;GAEG;AACH,MAAa,gBAAiB,SAAQ,KAAK;IACzC;;OAEG;IACH,YAAY,IAAY;QACtB,KAAK,CACH,2BAA2B,IAAI,KAAK;YAClC,uFAAuF,CAC1F,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AAXD,4CAWC;AAED;;;;;;GAMG;AACH,MAAa,kBAAmB,SAAQ,KAAK;IAC3C;;;OAGG;IACH,YAAY,MAAc,EAAE,MAAc;QACxC,KAAK,CAAC,0BAA0B,MAAM,KAAK,MAAM,EAAE,CAAC,CAAC;QACrD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AATD,gDASC;AAED;;;;;;;GAOG;AACH,MAAa,iBAAkB,SAAQ,KAAK;IAC1C;;OAEG;IACH,YAAY,MAAc;QACxB,KAAK,CAAC,2BAA2B,MAAM,EAAE,CAAC,CAAC;QAC3C,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AARD,8CAQC;AAED;;;GAGG;AACH,MAAa,iBAAkB,SAAQ,KAAK;IAC1C;;OAEG;IACH,YAAY,MAAc;QACxB,KAAK,CAAC,kBAAkB,MAAM,EAAE,CAAC,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AARD,8CAQC;AAED;;;;;;;GAOG;AACH,MAAa,eAAgB,SAAQ,KAAK;IACxC;;OAEG;IACH,YAAY,KAAa;QACvB,KAAK,CAAC,kBAAkB,KAAK,2CAA2C,CAAC,CAAC;QAC1E,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AARD,0CAQC;AAED;;;GAGG;AACH,MAAa,gBAAiB,SAAQ,KAAK;IACzC;;;OAGG;IACH,YAAY,KAAa,EAAE,QAAgB;QACzC,KAAK,CAAC,iBAAiB,KAAK,kCAAkC,QAAQ,GAAG,CAAC,CAAC;QAC3E,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AATD,4CASC"}
|
package/dist/cjs/example.js
CHANGED
|
@@ -1,39 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const bytes = Buffer.from('Hello, World!', 'utf8');
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
const decoder = new bufferbase_js_1.BufferEncoder(bufferbase_js_1.Chars.Base64_URL_SAFE);
|
|
20
|
-
const decoded = decoder.decode(base64encoded);
|
|
3
|
+
const node_buffer_1 = require("node:buffer");
|
|
4
|
+
const index_js_1 = require("./index.js");
|
|
5
|
+
const bytes = node_buffer_1.Buffer.from('Hello, World!', 'utf8');
|
|
6
|
+
// The same bytes under each algorithm.
|
|
7
|
+
console.log('=== Algorithms ===');
|
|
8
|
+
console.table({
|
|
9
|
+
'block base64': (0, index_js_1.encode)(bytes, 'base64'),
|
|
10
|
+
'block base32': (0, index_js_1.encode)(bytes, 'base32'),
|
|
11
|
+
'block85 ascii85': (0, index_js_1.encode)(bytes, 'ascii85'),
|
|
12
|
+
'radix base58': (0, index_js_1.encode)(bytes, 'base58'),
|
|
13
|
+
'radix base32crockford': (0, index_js_1.encode)(bytes, 'base32crockford'),
|
|
14
|
+
});
|
|
15
|
+
// Converting goes through the bytes, so any pair works.
|
|
16
|
+
console.log('\n=== Function API ===');
|
|
17
|
+
const base58 = (0, index_js_1.encode)(bytes, 'base58');
|
|
18
|
+
const base64url = (0, index_js_1.convert)(base58, 'base58', 'base64url');
|
|
21
19
|
console.table({
|
|
22
20
|
bytes: bytes.toString('utf8'),
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
base58,
|
|
22
|
+
base64url,
|
|
23
|
+
valid: (0, index_js_1.validate)(base64url, 'base64url'),
|
|
24
|
+
decoded: (0, index_js_1.decode)(base64url, 'base64url').toString('utf8'),
|
|
25
|
+
});
|
|
26
|
+
// validate() agrees with decode(): it checks length, padding and canonicality.
|
|
27
|
+
console.log('\n=== Rejected input ===');
|
|
28
|
+
console.table({
|
|
29
|
+
"'Zm8=' (canonical)": (0, index_js_1.validate)('Zm8=', 'base64'),
|
|
30
|
+
"'Zm9=' (spare bits set)": (0, index_js_1.validate)('Zm9=', 'base64'),
|
|
31
|
+
"'Zm9vd' (5 characters)": (0, index_js_1.validate)('Zm9vd', 'base64'),
|
|
32
|
+
});
|
|
33
|
+
// Crockford's Base32 reads what a person is likely to have written down.
|
|
34
|
+
console.log('\n=== Crockford ===');
|
|
35
|
+
const crockford = (0, index_js_1.encode)(bytes, 'base32crockfordcheck');
|
|
36
|
+
console.table({
|
|
37
|
+
encoded: crockford,
|
|
38
|
+
'as typed': (0, index_js_1.decode)(`${crockford.slice(0, 4)}-${crockford.slice(4)}`.toLowerCase(), 'base32crockfordcheck').toString('utf8'),
|
|
39
|
+
'one character changed': (0, index_js_1.validate)(`${crockford.slice(0, -2)}Z${crockford.slice(-1)}`, 'base32crockfordcheck'),
|
|
40
|
+
});
|
|
41
|
+
// Codecs, and a custom alphabet.
|
|
42
|
+
console.log('\n=== Codecs and custom alphabets ===');
|
|
43
|
+
const binary = (0, index_js_1.createCodec)('01');
|
|
44
|
+
const adobe = (0, index_js_1.createCodec)({
|
|
45
|
+
alphabet: index_js_1.Chars.Ascii85,
|
|
46
|
+
algorithm: 'block85',
|
|
47
|
+
zeroShortcut: true,
|
|
48
|
+
delimiters: true,
|
|
49
|
+
});
|
|
50
|
+
console.table({
|
|
51
|
+
'Codecs.base58': index_js_1.Codecs.base58.encode(bytes),
|
|
52
|
+
'binary of [5]': binary.encode(Uint8Array.of(5)),
|
|
53
|
+
'adobe ascii85': adobe.encode(bytes),
|
|
28
54
|
});
|
|
29
|
-
// ┌─────────────────┬─────────────────────────┐
|
|
30
|
-
// │ (index) │ Values │
|
|
31
|
-
// ├─────────────────┼─────────────────────────┤
|
|
32
|
-
// │ bytes │ 'Hello, World!' │
|
|
33
|
-
// │ base32encoded │ '4GSBCDHQJR82QDXS6RS11' │
|
|
34
|
-
// │ base58encoded │ '72k1xXWG59fYdzSNoA' │
|
|
35
|
-
// │ base64encoded │ 'BIZWxsbywgV29ybGQh' │
|
|
36
|
-
// │ isValidAsBase64 │ true │
|
|
37
|
-
// │ decoded │ 'Hello, World!' │
|
|
38
|
-
// └─────────────────┴─────────────────────────┘
|
|
39
55
|
//# sourceMappingURL=example.js.map
|
package/dist/cjs/example.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":";;AAAA,
|
|
1
|
+
{"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { BufferLike, CodecSpec, DecodeOptions } from './types.js';
|
|
2
|
+
import type { Buffer } from 'node:buffer';
|
|
3
|
+
import { type BaseName } from './bases.js';
|
|
4
|
+
/** A named encoding, or a spec describing one directly. */
|
|
5
|
+
export type Base = BaseName | CodecSpec;
|
|
6
|
+
/**
|
|
7
|
+
* Encodes a buffer to a base-encoded string.
|
|
8
|
+
*
|
|
9
|
+
* @param buffer - Buffer or Uint8Array to encode
|
|
10
|
+
* @param base - Base name (e.g. `'base58'`) or a codec spec
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* encode(Buffer.from('fo'), 'base64'); // 'Zm8='
|
|
15
|
+
* encode(Uint8Array.of(5), { alphabet: '01', algorithm: 'radix' }); // '101'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function encode(buffer: BufferLike, base: Base): 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 a codec spec
|
|
24
|
+
* @param options - Decode options, such as a required `size`
|
|
25
|
+
* @throws {InvalidCharacterError} If a character is not in the alphabet
|
|
26
|
+
* @throws {InvalidLengthError} If the length cannot encode a whole number of bytes
|
|
27
|
+
* @throws {NonCanonicalError} If spare bits are set and the codec is strict
|
|
28
|
+
* @throws {BufferSizeError} If the result does not have the requested `size`
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* decode('Zm8=', 'base64'); // Buffer('fo')
|
|
33
|
+
* decode('9Ajdvzr', 'base58', { size: 32 });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function decode(encoded: string, base: Base, options?: DecodeOptions): Buffer;
|
|
37
|
+
/**
|
|
38
|
+
* Converts a string from one base to another, through the bytes they describe.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* convert('9Ajdvzr', 'base58', 'base64url');
|
|
43
|
+
* convert(legacy, 'radix64', 'base64'); // migrate data written by bufferbase 2
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function convert(input: string, from: Base, to: Base): string;
|
|
47
|
+
/**
|
|
48
|
+
* Reports whether `decode` would succeed for this input.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* validate('Zm8=', 'base64'); // true
|
|
53
|
+
* validate('Zm9=', 'base64'); // false — spare bits are not zero
|
|
54
|
+
* validate('Zm9vd', 'base64'); // false — 5 characters cannot be whole bytes
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function validate(input: string, base: Base): boolean;
|
|
58
|
+
//# 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,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAe,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGxD,2DAA2D;AAC3D,MAAM,MAAM,IAAI,GAAG,QAAQ,GAAG,SAAS,CAAC;AAExC;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,GAAG,MAAM,CAE7D;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAEnF;AAED;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,GAAG,MAAM,CAInE;AAED;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAE3D"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encode = encode;
|
|
4
|
+
exports.decode = decode;
|
|
5
|
+
exports.convert = convert;
|
|
6
|
+
exports.validate = validate;
|
|
7
|
+
const bases_js_1 = require("./bases.js");
|
|
8
|
+
const codec_js_1 = require("./codec.js");
|
|
9
|
+
/**
|
|
10
|
+
* Encodes a buffer to a base-encoded string.
|
|
11
|
+
*
|
|
12
|
+
* @param buffer - Buffer or Uint8Array to encode
|
|
13
|
+
* @param base - Base name (e.g. `'base58'`) or a codec spec
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* encode(Buffer.from('fo'), 'base64'); // 'Zm8='
|
|
18
|
+
* encode(Uint8Array.of(5), { alphabet: '01', algorithm: 'radix' }); // '101'
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function encode(buffer, base) {
|
|
22
|
+
return (0, codec_js_1.getCodec)((0, bases_js_1.resolveSpec)(base)).encode(buffer);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Decodes a base-encoded string to a buffer.
|
|
26
|
+
*
|
|
27
|
+
* @param encoded - String to decode
|
|
28
|
+
* @param base - Base name (e.g. `'base58'`) or a codec spec
|
|
29
|
+
* @param options - Decode options, such as a required `size`
|
|
30
|
+
* @throws {InvalidCharacterError} If a character is not in the alphabet
|
|
31
|
+
* @throws {InvalidLengthError} If the length cannot encode a whole number of bytes
|
|
32
|
+
* @throws {NonCanonicalError} If spare bits are set and the codec is strict
|
|
33
|
+
* @throws {BufferSizeError} If the result does not have the requested `size`
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* decode('Zm8=', 'base64'); // Buffer('fo')
|
|
38
|
+
* decode('9Ajdvzr', 'base58', { size: 32 });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function decode(encoded, base, options) {
|
|
42
|
+
return (0, codec_js_1.getCodec)((0, bases_js_1.resolveSpec)(base)).decode(encoded, options);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Converts a string from one base to another, through the bytes they describe.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* convert('9Ajdvzr', 'base58', 'base64url');
|
|
50
|
+
* convert(legacy, 'radix64', 'base64'); // migrate data written by bufferbase 2
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function convert(input, from, to) {
|
|
54
|
+
const source = (0, codec_js_1.getCodec)((0, bases_js_1.resolveSpec)(from));
|
|
55
|
+
const target = (0, codec_js_1.getCodec)((0, bases_js_1.resolveSpec)(to));
|
|
56
|
+
return source.convertTo(target, input);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Reports whether `decode` would succeed for this input.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* validate('Zm8=', 'base64'); // true
|
|
64
|
+
* validate('Zm9=', 'base64'); // false — spare bits are not zero
|
|
65
|
+
* validate('Zm9vd', 'base64'); // false — 5 characters cannot be whole bytes
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
function validate(input, base) {
|
|
69
|
+
return (0, codec_js_1.getCodec)((0, bases_js_1.resolveSpec)(base)).validate(input);
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=functions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":";;AAoBA,wBAEC;AAmBD,wBAEC;AAWD,0BAIC;AAYD,4BAEC;AAtED,yCAAwD;AACxD,yCAAsC;AAKtC;;;;;;;;;;;GAWG;AACH,SAAgB,MAAM,CAAC,MAAkB,EAAE,IAAU;IACnD,OAAO,IAAA,mBAAQ,EAAC,IAAA,sBAAW,EAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,MAAM,CAAC,OAAe,EAAE,IAAU,EAAE,OAAuB;IACzE,OAAO,IAAA,mBAAQ,EAAC,IAAA,sBAAW,EAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,OAAO,CAAC,KAAa,EAAE,IAAU,EAAE,EAAQ;IACzD,MAAM,MAAM,GAAG,IAAA,mBAAQ,EAAC,IAAA,sBAAW,EAAC,IAAI,CAAC,CAAC,CAAC;IAC3C,MAAM,MAAM,GAAG,IAAA,mBAAQ,EAAC,IAAA,sBAAW,EAAC,EAAE,CAAC,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AACzC,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,QAAQ,CAAC,KAAa,EAAE,IAAU;IAChD,OAAO,IAAA,mBAAQ,EAAC,IAAA,sBAAW,EAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACrD,CAAC"}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { Algorithm, Block85Spec, BlockSpec, BufferLike, CodecSpec, DecodeOptions, ICodec, RadixSpec, } from './types.js';
|
|
2
|
+
export type { BaseName } from './bases.js';
|
|
3
|
+
export type { Base } from './functions.js';
|
|
4
|
+
export type { CodecName } from './codecs.js';
|
|
5
|
+
export { BufferSizeError, CheckSymbolError, InvalidCharacterError, InvalidCodecError, InvalidLengthError, NonCanonicalError, UnknownBaseError, ValueRangeError, } from './errors.js';
|
|
6
|
+
export { Block85Codec, BlockCodec, Codec, RadixCodec, createCodec, getCodec } from './codec.js';
|
|
7
|
+
export { Bases, Chars, isBaseName, resolveSpec } from './bases.js';
|
|
8
|
+
export { convert, decode, encode, validate } from './functions.js';
|
|
9
|
+
export { Codecs } from './codecs.js';
|
|
10
|
+
export { BufferEncoder, Converter, Validator } from './bufferbase.js';
|
|
2
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,SAAS,EACT,WAAW,EACX,SAAS,EACT,UAAU,EACV,SAAS,EACT,aAAa,EACb,MAAM,EACN,SAAS,GACV,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,YAAY,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC3C,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAG7C,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,GAChB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGhG,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGnE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,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"}
|