@solana/codecs-strings 2.0.0-experimental.ffeddf6 → 2.0.0-preview.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 +194 -4
- package/dist/index.browser.cjs +148 -132
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +149 -133
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +120 -117
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +130 -130
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +131 -131
- package/dist/index.node.js.map +1 -1
- package/dist/types/assertions.d.ts.map +1 -0
- package/dist/types/base10.d.ts +3 -3
- package/dist/types/base10.d.ts.map +1 -0
- package/dist/types/base16.d.ts +4 -4
- package/dist/types/base16.d.ts.map +1 -0
- package/dist/types/base58.d.ts +3 -3
- package/dist/types/base58.d.ts.map +1 -0
- package/dist/types/base64.d.ts +4 -4
- package/dist/types/base64.d.ts.map +1 -0
- package/dist/types/baseX-reslice.d.ts +4 -4
- package/dist/types/baseX-reslice.d.ts.map +1 -0
- package/dist/types/baseX.d.ts +4 -4
- package/dist/types/baseX.d.ts.map +1 -0
- package/dist/types/index.d.ts +10 -10
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/null-characters.d.ts.map +1 -0
- package/dist/types/string.d.ts +32 -11
- package/dist/types/string.d.ts.map +1 -0
- package/dist/types/utf8.d.ts +3 -3
- package/dist/types/utf8.d.ts.map +1 -0
- package/package.json +14 -34
- package/dist/index.development.js +0 -469
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -37
package/dist/types/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export * from './assertions';
|
|
2
|
-
export * from './base10';
|
|
3
|
-
export * from './base16';
|
|
4
|
-
export * from './base58';
|
|
5
|
-
export * from './base64';
|
|
6
|
-
export * from './baseX';
|
|
7
|
-
export * from './baseX-reslice';
|
|
8
|
-
export * from './null-characters';
|
|
9
|
-
export * from './string';
|
|
10
|
-
export * from './utf8';
|
|
1
|
+
export * from './assertions.js';
|
|
2
|
+
export * from './base10.js';
|
|
3
|
+
export * from './base16.js';
|
|
4
|
+
export * from './base58.js';
|
|
5
|
+
export * from './base64.js';
|
|
6
|
+
export * from './baseX.js';
|
|
7
|
+
export * from './baseX-reslice.js';
|
|
8
|
+
export * from './null-characters.js';
|
|
9
|
+
export * from './string.js';
|
|
10
|
+
export * from './utf8.js';
|
|
11
11
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"null-characters.d.ts","sourceRoot":"","sources":["../../src/null-characters.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,eAAO,MAAM,oBAAoB,UAAW,MAAM,WAElB,CAAC;AAEjC,qDAAqD;AACrD,eAAO,MAAM,iBAAiB,UAAW,MAAM,SAAS,MAAM,WAAkC,CAAC"}
|
package/dist/types/string.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
|
|
2
2
|
import { NumberCodec, NumberDecoder, NumberEncoder } from '@solana/codecs-numbers';
|
|
3
|
-
/** Defines the
|
|
4
|
-
export type
|
|
3
|
+
/** Defines the config for string codecs. */
|
|
4
|
+
export type StringCodecConfig<TPrefix extends NumberCodec | NumberDecoder | NumberEncoder, TEncoding extends Codec<string> | Decoder<string> | Encoder<string>> = {
|
|
5
|
+
/**
|
|
6
|
+
* The codec to use for encoding and decoding the content.
|
|
7
|
+
* @defaultValue UTF-8 encoding.
|
|
8
|
+
*/
|
|
9
|
+
encoding?: TEncoding;
|
|
5
10
|
/**
|
|
6
11
|
* The size of the string. It can be one of the following:
|
|
7
12
|
* - a {@link NumberCodec} that prefixes the string with its size.
|
|
@@ -10,16 +15,32 @@ export type StringCodecOptions<TPrefix extends NumberCodec | NumberEncoder | Num
|
|
|
10
15
|
* @defaultValue u32 prefix.
|
|
11
16
|
*/
|
|
12
17
|
size?: TPrefix | number | 'variable';
|
|
13
|
-
/**
|
|
14
|
-
* The codec to use for encoding and decoding the content.
|
|
15
|
-
* @defaultValue UTF-8 encoding.
|
|
16
|
-
*/
|
|
17
|
-
encoding?: TEncoding;
|
|
18
18
|
};
|
|
19
19
|
/** Encodes strings from a given encoding and size strategy. */
|
|
20
|
-
export declare
|
|
20
|
+
export declare function getStringEncoder<TSize extends number>(config: StringCodecConfig<NumberEncoder, Encoder<string>> & {
|
|
21
|
+
size: TSize;
|
|
22
|
+
}): FixedSizeEncoder<string, TSize>;
|
|
23
|
+
export declare function getStringEncoder<TSize extends number>(config: StringCodecConfig<NumberEncoder, Encoder<string>> & {
|
|
24
|
+
encoding: FixedSizeEncoder<string, TSize>;
|
|
25
|
+
size: 'variable';
|
|
26
|
+
}): FixedSizeEncoder<string, TSize>;
|
|
27
|
+
export declare function getStringEncoder(config?: StringCodecConfig<NumberEncoder, Encoder<string>>): VariableSizeEncoder<string>;
|
|
21
28
|
/** Decodes strings from a given encoding and size strategy. */
|
|
22
|
-
export declare
|
|
29
|
+
export declare function getStringDecoder<TSize extends number>(config: StringCodecConfig<NumberDecoder, Decoder<string>> & {
|
|
30
|
+
size: TSize;
|
|
31
|
+
}): FixedSizeDecoder<string, TSize>;
|
|
32
|
+
export declare function getStringDecoder<TSize extends number>(config: StringCodecConfig<NumberDecoder, Decoder<string>> & {
|
|
33
|
+
encoding: FixedSizeDecoder<string, TSize>;
|
|
34
|
+
size: 'variable';
|
|
35
|
+
}): FixedSizeDecoder<string, TSize>;
|
|
36
|
+
export declare function getStringDecoder(config?: StringCodecConfig<NumberDecoder, Decoder<string>>): VariableSizeDecoder<string>;
|
|
23
37
|
/** Encodes and decodes strings from a given encoding and size strategy. */
|
|
24
|
-
export declare
|
|
38
|
+
export declare function getStringCodec<TSize extends number>(config: StringCodecConfig<NumberCodec, Codec<string>> & {
|
|
39
|
+
size: TSize;
|
|
40
|
+
}): FixedSizeCodec<string, string, TSize>;
|
|
41
|
+
export declare function getStringCodec<TSize extends number>(config: StringCodecConfig<NumberCodec, Codec<string>> & {
|
|
42
|
+
encoding: FixedSizeCodec<string, string, TSize>;
|
|
43
|
+
size: 'variable';
|
|
44
|
+
}): FixedSizeCodec<string, string, TSize>;
|
|
45
|
+
export declare function getStringCodec(config?: StringCodecConfig<NumberCodec, Codec<string>>): VariableSizeCodec<string>;
|
|
25
46
|
//# sourceMappingURL=string.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/string.ts"],"names":[],"mappings":"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"}
|
package/dist/types/utf8.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Codec,
|
|
1
|
+
import { Codec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
|
|
2
2
|
/** Encodes UTF-8 strings using the native `TextEncoder` API. */
|
|
3
|
-
export declare const getUtf8Encoder: () =>
|
|
3
|
+
export declare const getUtf8Encoder: () => VariableSizeEncoder<string>;
|
|
4
4
|
/** Decodes UTF-8 strings using the native `TextDecoder` API. */
|
|
5
|
-
export declare const getUtf8Decoder: () =>
|
|
5
|
+
export declare const getUtf8Decoder: () => VariableSizeDecoder<string>;
|
|
6
6
|
/** Encodes and decodes UTF-8 strings using the native `TextEncoder` and `TextDecoder` API. */
|
|
7
7
|
export declare const getUtf8Codec: () => Codec<string>;
|
|
8
8
|
//# sourceMappingURL=utf8.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utf8.d.ts","sourceRoot":"","sources":["../../src/utf8.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,EAIL,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,qBAAqB,CAAC;AAK7B,gEAAgE;AAChE,eAAO,MAAM,cAAc,QAAO,oBAAoB,MAAM,CAU3D,CAAC;AAEF,gEAAgE;AAChE,eAAO,MAAM,cAAc,QAAO,oBAAoB,MAAM,CAQ3D,CAAC;AAEF,8FAA8F;AAC9F,eAAO,MAAM,YAAY,QAAO,MAAM,MAAM,CAAqD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/codecs-strings",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-preview.1",
|
|
4
4
|
"description": "Codecs for strings of different sizes and encodings",
|
|
5
5
|
"exports": {
|
|
6
6
|
"browser": {
|
|
@@ -49,31 +49,9 @@
|
|
|
49
49
|
"node": ">=17.4"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@solana/codecs-core": "2.0.0-
|
|
53
|
-
"@solana/codecs-numbers": "2.0.0-
|
|
54
|
-
|
|
55
|
-
"devDependencies": {
|
|
56
|
-
"@solana/eslint-config-solana": "^1.0.2",
|
|
57
|
-
"@swc/jest": "^0.2.29",
|
|
58
|
-
"@types/jest": "^29.5.6",
|
|
59
|
-
"@typescript-eslint/eslint-plugin": "^6.7.0",
|
|
60
|
-
"@typescript-eslint/parser": "^6.3.0",
|
|
61
|
-
"agadoo": "^3.0.0",
|
|
62
|
-
"eslint": "^8.45.0",
|
|
63
|
-
"eslint-plugin-jest": "^27.4.2",
|
|
64
|
-
"eslint-plugin-sort-keys-fix": "^1.1.2",
|
|
65
|
-
"jest": "^29.7.0",
|
|
66
|
-
"jest-environment-jsdom": "^29.7.0",
|
|
67
|
-
"jest-runner-eslint": "^2.1.2",
|
|
68
|
-
"jest-runner-prettier": "^1.0.0",
|
|
69
|
-
"prettier": "^2.8",
|
|
70
|
-
"tsup": "7.2.0",
|
|
71
|
-
"typescript": "^5.2.2",
|
|
72
|
-
"version-from-git": "^1.1.1",
|
|
73
|
-
"build-scripts": "0.0.0",
|
|
74
|
-
"test-config": "0.0.0",
|
|
75
|
-
"text-encoding-impl": "0.0.0",
|
|
76
|
-
"tsconfig": "0.0.0"
|
|
52
|
+
"@solana/codecs-core": "2.0.0-preview.1",
|
|
53
|
+
"@solana/codecs-numbers": "2.0.0-preview.1",
|
|
54
|
+
"@solana/errors": "2.0.0-preview.1"
|
|
77
55
|
},
|
|
78
56
|
"peerDependencies": {
|
|
79
57
|
"fastestsmallesttextencoderdecoder": "^1.0.22"
|
|
@@ -87,18 +65,20 @@
|
|
|
87
65
|
]
|
|
88
66
|
},
|
|
89
67
|
"scripts": {
|
|
90
|
-
"
|
|
91
|
-
"compile:
|
|
92
|
-
"
|
|
93
|
-
"
|
|
68
|
+
"benchmark": "./src/__benchmarks__/run.ts",
|
|
69
|
+
"compile:js": "tsup --config build-scripts/tsup.config.package.ts",
|
|
70
|
+
"compile:typedefs": "tsc -p ./tsconfig.declarations.json && node ../../node_modules/@solana/build-scripts/add-js-extension-to-types.mjs",
|
|
71
|
+
"dev": "jest -c ../../node_modules/@solana/test-config/jest-dev.config.ts --rootDir . --watch",
|
|
72
|
+
"publish-impl": "npm view $npm_package_name@$npm_package_version > /dev/null 2>&1 || pnpm publish --tag preview --access public --no-git-checks",
|
|
73
|
+
"publish-packages": "pnpm prepublishOnly && pnpm publish-impl",
|
|
94
74
|
"style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/* package.json",
|
|
95
|
-
"test:lint": "jest -c node_modules/test-config/jest-lint.config.ts --rootDir . --silent",
|
|
96
|
-
"test:prettier": "jest -c node_modules/test-config/jest-prettier.config.ts --rootDir . --silent",
|
|
75
|
+
"test:lint": "jest -c ../../node_modules/@solana/test-config/jest-lint.config.ts --rootDir . --silent",
|
|
76
|
+
"test:prettier": "jest -c ../../node_modules/@solana/test-config/jest-prettier.config.ts --rootDir . --silent",
|
|
97
77
|
"test:treeshakability:browser": "agadoo dist/index.browser.js",
|
|
98
78
|
"test:treeshakability:native": "agadoo dist/index.native.js",
|
|
99
79
|
"test:treeshakability:node": "agadoo dist/index.node.js",
|
|
100
80
|
"test:typecheck": "tsc --noEmit",
|
|
101
|
-
"test:unit:browser": "jest -c node_modules/test-config/jest-unit.config.browser.ts --rootDir . --silent",
|
|
102
|
-
"test:unit:node": "jest -c node_modules/test-config/jest-unit.config.node.ts --rootDir . --silent"
|
|
81
|
+
"test:unit:browser": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.browser.ts --rootDir . --silent",
|
|
82
|
+
"test:unit:node": "jest -c ../../node_modules/@solana/test-config/jest-unit.config.node.ts --rootDir . --silent"
|
|
103
83
|
}
|
|
104
84
|
}
|
|
@@ -1,469 +0,0 @@
|
|
|
1
|
-
this.globalThis = this.globalThis || {};
|
|
2
|
-
this.globalThis.solanaWeb3 = (function (exports) {
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
|
-
// src/assertions.ts
|
|
6
|
-
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
|
|
7
|
-
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
|
|
8
|
-
throw new Error(`Expected a string of base ${alphabet4.length}, got [${givenValue}].`);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// ../codecs-core/dist/index.browser.js
|
|
13
|
-
function assertByteArrayIsNotEmptyForCodec(codecDescription, bytes, offset = 0) {
|
|
14
|
-
if (bytes.length - offset <= 0) {
|
|
15
|
-
throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
function assertByteArrayHasEnoughBytesForCodec(codecDescription, expected, bytes, offset = 0) {
|
|
19
|
-
const bytesLength = bytes.length - offset;
|
|
20
|
-
if (bytesLength < expected) {
|
|
21
|
-
throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
var mergeBytes = (byteArrays) => {
|
|
25
|
-
const nonEmptyByteArrays = byteArrays.filter((arr) => arr.length);
|
|
26
|
-
if (nonEmptyByteArrays.length === 0) {
|
|
27
|
-
return byteArrays.length ? byteArrays[0] : new Uint8Array();
|
|
28
|
-
}
|
|
29
|
-
if (nonEmptyByteArrays.length === 1) {
|
|
30
|
-
return nonEmptyByteArrays[0];
|
|
31
|
-
}
|
|
32
|
-
const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);
|
|
33
|
-
const result = new Uint8Array(totalLength);
|
|
34
|
-
let offset = 0;
|
|
35
|
-
nonEmptyByteArrays.forEach((arr) => {
|
|
36
|
-
result.set(arr, offset);
|
|
37
|
-
offset += arr.length;
|
|
38
|
-
});
|
|
39
|
-
return result;
|
|
40
|
-
};
|
|
41
|
-
var padBytes = (bytes, length) => {
|
|
42
|
-
if (bytes.length >= length)
|
|
43
|
-
return bytes;
|
|
44
|
-
const paddedBytes = new Uint8Array(length).fill(0);
|
|
45
|
-
paddedBytes.set(bytes);
|
|
46
|
-
return paddedBytes;
|
|
47
|
-
};
|
|
48
|
-
var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
|
|
49
|
-
function combineCodec(encoder, decoder, description) {
|
|
50
|
-
if (encoder.fixedSize !== decoder.fixedSize) {
|
|
51
|
-
throw new Error(
|
|
52
|
-
`Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
if (encoder.maxSize !== decoder.maxSize) {
|
|
56
|
-
throw new Error(
|
|
57
|
-
`Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
if (description === void 0 && encoder.description !== decoder.description) {
|
|
61
|
-
throw new Error(
|
|
62
|
-
`Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.`
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
return {
|
|
66
|
-
decode: decoder.decode,
|
|
67
|
-
description: description ?? encoder.description,
|
|
68
|
-
encode: encoder.encode,
|
|
69
|
-
fixedSize: encoder.fixedSize,
|
|
70
|
-
maxSize: encoder.maxSize
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
function fixCodecHelper(data, fixedBytes, description) {
|
|
74
|
-
return {
|
|
75
|
-
description: description ?? `fixed(${fixedBytes}, ${data.description})`,
|
|
76
|
-
fixedSize: fixedBytes,
|
|
77
|
-
maxSize: fixedBytes
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
function fixEncoder(encoder, fixedBytes, description) {
|
|
81
|
-
return {
|
|
82
|
-
...fixCodecHelper(encoder, fixedBytes, description),
|
|
83
|
-
encode: (value) => fixBytes(encoder.encode(value), fixedBytes)
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
function fixDecoder(decoder, fixedBytes, description) {
|
|
87
|
-
return {
|
|
88
|
-
...fixCodecHelper(decoder, fixedBytes, description),
|
|
89
|
-
decode: (bytes, offset = 0) => {
|
|
90
|
-
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
|
|
91
|
-
if (offset > 0 || bytes.length > fixedBytes) {
|
|
92
|
-
bytes = bytes.slice(offset, offset + fixedBytes);
|
|
93
|
-
}
|
|
94
|
-
if (decoder.fixedSize !== null) {
|
|
95
|
-
bytes = fixBytes(bytes, decoder.fixedSize);
|
|
96
|
-
}
|
|
97
|
-
const [value] = decoder.decode(bytes, 0);
|
|
98
|
-
return [value, offset + fixedBytes];
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// src/baseX.ts
|
|
104
|
-
var getBaseXEncoder = (alphabet4) => {
|
|
105
|
-
const base = alphabet4.length;
|
|
106
|
-
const baseBigInt = BigInt(base);
|
|
107
|
-
return {
|
|
108
|
-
description: `base${base}`,
|
|
109
|
-
encode(value) {
|
|
110
|
-
assertValidBaseString(alphabet4, value);
|
|
111
|
-
if (value === "")
|
|
112
|
-
return new Uint8Array();
|
|
113
|
-
const chars = [...value];
|
|
114
|
-
let trailIndex = chars.findIndex((c) => c !== alphabet4[0]);
|
|
115
|
-
trailIndex = trailIndex === -1 ? chars.length : trailIndex;
|
|
116
|
-
const leadingZeroes = Array(trailIndex).fill(0);
|
|
117
|
-
if (trailIndex === chars.length)
|
|
118
|
-
return Uint8Array.from(leadingZeroes);
|
|
119
|
-
const tailChars = chars.slice(trailIndex);
|
|
120
|
-
let base10Number = 0n;
|
|
121
|
-
let baseXPower = 1n;
|
|
122
|
-
for (let i = tailChars.length - 1; i >= 0; i -= 1) {
|
|
123
|
-
base10Number += baseXPower * BigInt(alphabet4.indexOf(tailChars[i]));
|
|
124
|
-
baseXPower *= baseBigInt;
|
|
125
|
-
}
|
|
126
|
-
const tailBytes = [];
|
|
127
|
-
while (base10Number > 0n) {
|
|
128
|
-
tailBytes.unshift(Number(base10Number % 256n));
|
|
129
|
-
base10Number /= 256n;
|
|
130
|
-
}
|
|
131
|
-
return Uint8Array.from(leadingZeroes.concat(tailBytes));
|
|
132
|
-
},
|
|
133
|
-
fixedSize: null,
|
|
134
|
-
maxSize: null
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
var getBaseXDecoder = (alphabet4) => {
|
|
138
|
-
const base = alphabet4.length;
|
|
139
|
-
const baseBigInt = BigInt(base);
|
|
140
|
-
return {
|
|
141
|
-
decode(rawBytes, offset = 0) {
|
|
142
|
-
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
143
|
-
if (bytes.length === 0)
|
|
144
|
-
return ["", 0];
|
|
145
|
-
let trailIndex = bytes.findIndex((n) => n !== 0);
|
|
146
|
-
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
|
|
147
|
-
const leadingZeroes = alphabet4[0].repeat(trailIndex);
|
|
148
|
-
if (trailIndex === bytes.length)
|
|
149
|
-
return [leadingZeroes, rawBytes.length];
|
|
150
|
-
let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
|
|
151
|
-
const tailChars = [];
|
|
152
|
-
while (base10Number > 0n) {
|
|
153
|
-
tailChars.unshift(alphabet4[Number(base10Number % baseBigInt)]);
|
|
154
|
-
base10Number /= baseBigInt;
|
|
155
|
-
}
|
|
156
|
-
return [leadingZeroes + tailChars.join(""), rawBytes.length];
|
|
157
|
-
},
|
|
158
|
-
description: `base${base}`,
|
|
159
|
-
fixedSize: null,
|
|
160
|
-
maxSize: null
|
|
161
|
-
};
|
|
162
|
-
};
|
|
163
|
-
var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
|
|
164
|
-
|
|
165
|
-
// src/base10.ts
|
|
166
|
-
var alphabet = "0123456789";
|
|
167
|
-
var getBase10Encoder = () => getBaseXEncoder(alphabet);
|
|
168
|
-
var getBase10Decoder = () => getBaseXDecoder(alphabet);
|
|
169
|
-
var getBase10Codec = () => getBaseXCodec(alphabet);
|
|
170
|
-
|
|
171
|
-
// src/base16.ts
|
|
172
|
-
var getBase16Encoder = () => ({
|
|
173
|
-
description: "base16",
|
|
174
|
-
encode(value) {
|
|
175
|
-
const lowercaseValue = value.toLowerCase();
|
|
176
|
-
assertValidBaseString("0123456789abcdef", lowercaseValue, value);
|
|
177
|
-
const matches = lowercaseValue.match(/.{1,2}/g);
|
|
178
|
-
return Uint8Array.from(matches ? matches.map((byte) => parseInt(byte, 16)) : []);
|
|
179
|
-
},
|
|
180
|
-
fixedSize: null,
|
|
181
|
-
maxSize: null
|
|
182
|
-
});
|
|
183
|
-
var getBase16Decoder = () => ({
|
|
184
|
-
decode(bytes, offset = 0) {
|
|
185
|
-
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
|
|
186
|
-
return [value, bytes.length];
|
|
187
|
-
},
|
|
188
|
-
description: "base16",
|
|
189
|
-
fixedSize: null,
|
|
190
|
-
maxSize: null
|
|
191
|
-
});
|
|
192
|
-
var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
|
|
193
|
-
|
|
194
|
-
// src/base58.ts
|
|
195
|
-
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
196
|
-
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
|
|
197
|
-
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
|
|
198
|
-
var getBase58Codec = () => getBaseXCodec(alphabet2);
|
|
199
|
-
|
|
200
|
-
// src/baseX-reslice.ts
|
|
201
|
-
var getBaseXResliceEncoder = (alphabet4, bits) => ({
|
|
202
|
-
description: `base${alphabet4.length}`,
|
|
203
|
-
encode(value) {
|
|
204
|
-
assertValidBaseString(alphabet4, value);
|
|
205
|
-
if (value === "")
|
|
206
|
-
return new Uint8Array();
|
|
207
|
-
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
|
|
208
|
-
return new Uint8Array(reslice(charIndices, bits, 8, false));
|
|
209
|
-
},
|
|
210
|
-
fixedSize: null,
|
|
211
|
-
maxSize: null
|
|
212
|
-
});
|
|
213
|
-
var getBaseXResliceDecoder = (alphabet4, bits) => ({
|
|
214
|
-
decode(rawBytes, offset = 0) {
|
|
215
|
-
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
216
|
-
if (bytes.length === 0)
|
|
217
|
-
return ["", rawBytes.length];
|
|
218
|
-
const charIndices = reslice([...bytes], 8, bits, true);
|
|
219
|
-
return [charIndices.map((i) => alphabet4[i]).join(""), rawBytes.length];
|
|
220
|
-
},
|
|
221
|
-
description: `base${alphabet4.length}`,
|
|
222
|
-
fixedSize: null,
|
|
223
|
-
maxSize: null
|
|
224
|
-
});
|
|
225
|
-
var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
|
|
226
|
-
function reslice(input, inputBits, outputBits, useRemainder) {
|
|
227
|
-
const output = [];
|
|
228
|
-
let accumulator = 0;
|
|
229
|
-
let bitsInAccumulator = 0;
|
|
230
|
-
const mask = (1 << outputBits) - 1;
|
|
231
|
-
for (const value of input) {
|
|
232
|
-
accumulator = accumulator << inputBits | value;
|
|
233
|
-
bitsInAccumulator += inputBits;
|
|
234
|
-
while (bitsInAccumulator >= outputBits) {
|
|
235
|
-
bitsInAccumulator -= outputBits;
|
|
236
|
-
output.push(accumulator >> bitsInAccumulator & mask);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
if (useRemainder && bitsInAccumulator > 0) {
|
|
240
|
-
output.push(accumulator << outputBits - bitsInAccumulator & mask);
|
|
241
|
-
}
|
|
242
|
-
return output;
|
|
243
|
-
}
|
|
244
|
-
var getBase64Encoder = () => {
|
|
245
|
-
{
|
|
246
|
-
return {
|
|
247
|
-
description: `base64`,
|
|
248
|
-
encode(value) {
|
|
249
|
-
try {
|
|
250
|
-
const bytes = atob(value).split("").map((c) => c.charCodeAt(0));
|
|
251
|
-
return new Uint8Array(bytes);
|
|
252
|
-
} catch (e2) {
|
|
253
|
-
throw new Error(`Expected a string of base 64, got [${value}].`);
|
|
254
|
-
}
|
|
255
|
-
},
|
|
256
|
-
fixedSize: null,
|
|
257
|
-
maxSize: null
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
var getBase64Decoder = () => {
|
|
262
|
-
{
|
|
263
|
-
return {
|
|
264
|
-
decode(bytes, offset = 0) {
|
|
265
|
-
const slice = bytes.slice(offset);
|
|
266
|
-
const value = btoa(String.fromCharCode(...slice));
|
|
267
|
-
return [value, bytes.length];
|
|
268
|
-
},
|
|
269
|
-
description: `base64`,
|
|
270
|
-
fixedSize: null,
|
|
271
|
-
maxSize: null
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
|
|
276
|
-
|
|
277
|
-
// src/null-characters.ts
|
|
278
|
-
var removeNullCharacters = (value) => (
|
|
279
|
-
// eslint-disable-next-line no-control-regex
|
|
280
|
-
value.replace(/\u0000/g, "")
|
|
281
|
-
);
|
|
282
|
-
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
|
|
283
|
-
|
|
284
|
-
// ../codecs-numbers/dist/index.browser.js
|
|
285
|
-
function assertNumberIsBetweenForCodec(codecDescription, min, max, value) {
|
|
286
|
-
if (value < min || value > max) {
|
|
287
|
-
throw new Error(
|
|
288
|
-
`Codec [${codecDescription}] expected number to be in the range [${min}, ${max}], got ${value}.`
|
|
289
|
-
);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
function sharedNumberFactory(input) {
|
|
293
|
-
let littleEndian;
|
|
294
|
-
let defaultDescription = input.name;
|
|
295
|
-
if (input.size > 1) {
|
|
296
|
-
littleEndian = !("endian" in input.options) || input.options.endian === 0;
|
|
297
|
-
defaultDescription += littleEndian ? "(le)" : "(be)";
|
|
298
|
-
}
|
|
299
|
-
return {
|
|
300
|
-
description: input.options.description ?? defaultDescription,
|
|
301
|
-
fixedSize: input.size,
|
|
302
|
-
littleEndian,
|
|
303
|
-
maxSize: input.size
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
function numberEncoderFactory(input) {
|
|
307
|
-
const codecData = sharedNumberFactory(input);
|
|
308
|
-
return {
|
|
309
|
-
description: codecData.description,
|
|
310
|
-
encode(value) {
|
|
311
|
-
if (input.range) {
|
|
312
|
-
assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
|
|
313
|
-
}
|
|
314
|
-
const arrayBuffer = new ArrayBuffer(input.size);
|
|
315
|
-
input.set(new DataView(arrayBuffer), value, codecData.littleEndian);
|
|
316
|
-
return new Uint8Array(arrayBuffer);
|
|
317
|
-
},
|
|
318
|
-
fixedSize: codecData.fixedSize,
|
|
319
|
-
maxSize: codecData.maxSize
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
function numberDecoderFactory(input) {
|
|
323
|
-
const codecData = sharedNumberFactory(input);
|
|
324
|
-
return {
|
|
325
|
-
decode(bytes, offset = 0) {
|
|
326
|
-
assertByteArrayIsNotEmptyForCodec(codecData.description, bytes, offset);
|
|
327
|
-
assertByteArrayHasEnoughBytesForCodec(codecData.description, input.size, bytes, offset);
|
|
328
|
-
const view = new DataView(toArrayBuffer(bytes, offset, input.size));
|
|
329
|
-
return [input.get(view, codecData.littleEndian), offset + input.size];
|
|
330
|
-
},
|
|
331
|
-
description: codecData.description,
|
|
332
|
-
fixedSize: codecData.fixedSize,
|
|
333
|
-
maxSize: codecData.maxSize
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
function toArrayBuffer(bytes, offset, length) {
|
|
337
|
-
const bytesOffset = bytes.byteOffset + (offset ?? 0);
|
|
338
|
-
const bytesLength = length ?? bytes.byteLength;
|
|
339
|
-
return bytes.buffer.slice(bytesOffset, bytesOffset + bytesLength);
|
|
340
|
-
}
|
|
341
|
-
var getU32Encoder = (options = {}) => numberEncoderFactory({
|
|
342
|
-
name: "u32",
|
|
343
|
-
options,
|
|
344
|
-
range: [0, Number("0xffffffff")],
|
|
345
|
-
set: (view, value, le) => view.setUint32(0, value, le),
|
|
346
|
-
size: 4
|
|
347
|
-
});
|
|
348
|
-
var getU32Decoder = (options = {}) => numberDecoderFactory({
|
|
349
|
-
get: (view, le) => view.getUint32(0, le),
|
|
350
|
-
name: "u32",
|
|
351
|
-
options,
|
|
352
|
-
size: 4
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
// ../text-encoding-impl/dist/index.browser.js
|
|
356
|
-
var e = globalThis.TextDecoder;
|
|
357
|
-
var o = globalThis.TextEncoder;
|
|
358
|
-
|
|
359
|
-
// src/utf8.ts
|
|
360
|
-
var getUtf8Encoder = () => {
|
|
361
|
-
let textEncoder;
|
|
362
|
-
return {
|
|
363
|
-
description: "utf8",
|
|
364
|
-
encode: (value) => new Uint8Array((textEncoder || (textEncoder = new o())).encode(value)),
|
|
365
|
-
fixedSize: null,
|
|
366
|
-
maxSize: null
|
|
367
|
-
};
|
|
368
|
-
};
|
|
369
|
-
var getUtf8Decoder = () => {
|
|
370
|
-
let textDecoder;
|
|
371
|
-
return {
|
|
372
|
-
decode(bytes, offset = 0) {
|
|
373
|
-
const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
|
|
374
|
-
return [removeNullCharacters(value), bytes.length];
|
|
375
|
-
},
|
|
376
|
-
description: "utf8",
|
|
377
|
-
fixedSize: null,
|
|
378
|
-
maxSize: null
|
|
379
|
-
};
|
|
380
|
-
};
|
|
381
|
-
var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
|
|
382
|
-
|
|
383
|
-
// src/string.ts
|
|
384
|
-
var getStringEncoder = (options = {}) => {
|
|
385
|
-
const size = options.size ?? getU32Encoder();
|
|
386
|
-
const encoding = options.encoding ?? getUtf8Encoder();
|
|
387
|
-
const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
|
|
388
|
-
if (size === "variable") {
|
|
389
|
-
return { ...encoding, description };
|
|
390
|
-
}
|
|
391
|
-
if (typeof size === "number") {
|
|
392
|
-
return fixEncoder(encoding, size, description);
|
|
393
|
-
}
|
|
394
|
-
return {
|
|
395
|
-
description,
|
|
396
|
-
encode: (value) => {
|
|
397
|
-
const contentBytes = encoding.encode(value);
|
|
398
|
-
const lengthBytes = size.encode(contentBytes.length);
|
|
399
|
-
return mergeBytes([lengthBytes, contentBytes]);
|
|
400
|
-
},
|
|
401
|
-
fixedSize: null,
|
|
402
|
-
maxSize: null
|
|
403
|
-
};
|
|
404
|
-
};
|
|
405
|
-
var getStringDecoder = (options = {}) => {
|
|
406
|
-
const size = options.size ?? getU32Decoder();
|
|
407
|
-
const encoding = options.encoding ?? getUtf8Decoder();
|
|
408
|
-
const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
|
|
409
|
-
if (size === "variable") {
|
|
410
|
-
return { ...encoding, description };
|
|
411
|
-
}
|
|
412
|
-
if (typeof size === "number") {
|
|
413
|
-
return fixDecoder(encoding, size, description);
|
|
414
|
-
}
|
|
415
|
-
return {
|
|
416
|
-
decode: (bytes, offset = 0) => {
|
|
417
|
-
assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
|
|
418
|
-
const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
|
|
419
|
-
const length = Number(lengthBigInt);
|
|
420
|
-
offset = lengthOffset;
|
|
421
|
-
const contentBytes = bytes.slice(offset, offset + length);
|
|
422
|
-
assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
|
|
423
|
-
const [value, contentOffset] = encoding.decode(contentBytes);
|
|
424
|
-
offset += contentOffset;
|
|
425
|
-
return [value, offset];
|
|
426
|
-
},
|
|
427
|
-
description,
|
|
428
|
-
fixedSize: null,
|
|
429
|
-
maxSize: null
|
|
430
|
-
};
|
|
431
|
-
};
|
|
432
|
-
var getStringCodec = (options = {}) => combineCodec(getStringEncoder(options), getStringDecoder(options));
|
|
433
|
-
function getSizeDescription(size) {
|
|
434
|
-
return typeof size === "object" ? size.description : `${size}`;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
exports.assertValidBaseString = assertValidBaseString;
|
|
438
|
-
exports.getBase10Codec = getBase10Codec;
|
|
439
|
-
exports.getBase10Decoder = getBase10Decoder;
|
|
440
|
-
exports.getBase10Encoder = getBase10Encoder;
|
|
441
|
-
exports.getBase16Codec = getBase16Codec;
|
|
442
|
-
exports.getBase16Decoder = getBase16Decoder;
|
|
443
|
-
exports.getBase16Encoder = getBase16Encoder;
|
|
444
|
-
exports.getBase58Codec = getBase58Codec;
|
|
445
|
-
exports.getBase58Decoder = getBase58Decoder;
|
|
446
|
-
exports.getBase58Encoder = getBase58Encoder;
|
|
447
|
-
exports.getBase64Codec = getBase64Codec;
|
|
448
|
-
exports.getBase64Decoder = getBase64Decoder;
|
|
449
|
-
exports.getBase64Encoder = getBase64Encoder;
|
|
450
|
-
exports.getBaseXCodec = getBaseXCodec;
|
|
451
|
-
exports.getBaseXDecoder = getBaseXDecoder;
|
|
452
|
-
exports.getBaseXEncoder = getBaseXEncoder;
|
|
453
|
-
exports.getBaseXResliceCodec = getBaseXResliceCodec;
|
|
454
|
-
exports.getBaseXResliceDecoder = getBaseXResliceDecoder;
|
|
455
|
-
exports.getBaseXResliceEncoder = getBaseXResliceEncoder;
|
|
456
|
-
exports.getStringCodec = getStringCodec;
|
|
457
|
-
exports.getStringDecoder = getStringDecoder;
|
|
458
|
-
exports.getStringEncoder = getStringEncoder;
|
|
459
|
-
exports.getUtf8Codec = getUtf8Codec;
|
|
460
|
-
exports.getUtf8Decoder = getUtf8Decoder;
|
|
461
|
-
exports.getUtf8Encoder = getUtf8Encoder;
|
|
462
|
-
exports.padNullCharacters = padNullCharacters;
|
|
463
|
-
exports.removeNullCharacters = removeNullCharacters;
|
|
464
|
-
|
|
465
|
-
return exports;
|
|
466
|
-
|
|
467
|
-
})({});
|
|
468
|
-
//# sourceMappingURL=out.js.map
|
|
469
|
-
//# sourceMappingURL=index.development.js.map
|