@solana/codecs-core 2.0.0-experimental.5e8ac8d
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/LICENSE +20 -0
- package/README.md +25 -0
- package/dist/index.browser.cjs +185 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.js +168 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.development.js +191 -0
- package/dist/index.development.js.map +1 -0
- package/dist/index.native.js +168 -0
- package/dist/index.native.js.map +1 -0
- package/dist/index.node.cjs +185 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.js +168 -0
- package/dist/index.node.js.map +1 -0
- package/dist/index.production.min.js +26 -0
- package/dist/types/assertions.d.ts +16 -0
- package/dist/types/assertions.d.ts.map +1 -0
- package/dist/types/bytes.d.ts +17 -0
- package/dist/types/bytes.d.ts.map +1 -0
- package/dist/types/codec.d.ts +56 -0
- package/dist/types/codec.d.ts.map +1 -0
- package/dist/types/combine-codec.d.ts +8 -0
- package/dist/types/combine-codec.d.ts.map +1 -0
- package/dist/types/fix-codec.d.ts +26 -0
- package/dist/types/fix-codec.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/map-codec.d.ts +15 -0
- package/dist/types/map-codec.d.ts.map +1 -0
- package/dist/types/reverse-codec.d.ts +14 -0
- package/dist/types/reverse-codec.d.ts.map +1 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2018 Solana Labs, Inc
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[![npm][npm-image]][npm-url]
|
|
2
|
+
[![npm-downloads][npm-downloads-image]][npm-url]
|
|
3
|
+
[![semantic-release][semantic-release-image]][semantic-release-url]
|
|
4
|
+
<br />
|
|
5
|
+
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
|
|
6
|
+
|
|
7
|
+
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
|
|
8
|
+
[code-style-prettier-url]: https://github.com/prettier/prettier
|
|
9
|
+
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/codecs-core/experimental.svg?style=flat
|
|
10
|
+
[npm-image]: https://img.shields.io/npm/v/@solana/codecs-core/experimental.svg?style=flat
|
|
11
|
+
[npm-url]: https://www.npmjs.com/package/@solana/codecs-core/v/experimental
|
|
12
|
+
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
|
13
|
+
[semantic-release-url]: https://github.com/semantic-release/semantic-release
|
|
14
|
+
|
|
15
|
+
# @solana/codecs-core
|
|
16
|
+
|
|
17
|
+
This package contains the core types and function for encoding and decoding data structures on Solana. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@experimental`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).
|
|
18
|
+
|
|
19
|
+
## Types
|
|
20
|
+
|
|
21
|
+
TODO
|
|
22
|
+
|
|
23
|
+
## Functions
|
|
24
|
+
|
|
25
|
+
TODO
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/assertions.ts
|
|
4
|
+
function assertByteArrayIsNotEmptyForCodec(codecDescription, bytes, offset = 0) {
|
|
5
|
+
if (bytes.length - offset <= 0) {
|
|
6
|
+
throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function assertByteArrayHasEnoughBytesForCodec(codecDescription, expected, bytes, offset = 0) {
|
|
10
|
+
const bytesLength = bytes.length - offset;
|
|
11
|
+
if (bytesLength < expected) {
|
|
12
|
+
throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function assertFixedSizeCodec(data, message) {
|
|
16
|
+
if (data.fixedSize === null) {
|
|
17
|
+
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one.");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/bytes.ts
|
|
22
|
+
var mergeBytes = (byteArrays) => {
|
|
23
|
+
const nonEmptyByteArrays = byteArrays.filter((arr) => arr.length);
|
|
24
|
+
if (nonEmptyByteArrays.length === 0) {
|
|
25
|
+
return byteArrays.length ? byteArrays[0] : new Uint8Array();
|
|
26
|
+
}
|
|
27
|
+
if (nonEmptyByteArrays.length === 1) {
|
|
28
|
+
return nonEmptyByteArrays[0];
|
|
29
|
+
}
|
|
30
|
+
const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);
|
|
31
|
+
const result = new Uint8Array(totalLength);
|
|
32
|
+
let offset = 0;
|
|
33
|
+
nonEmptyByteArrays.forEach((arr) => {
|
|
34
|
+
result.set(arr, offset);
|
|
35
|
+
offset += arr.length;
|
|
36
|
+
});
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
var padBytes = (bytes, length) => {
|
|
40
|
+
if (bytes.length >= length)
|
|
41
|
+
return bytes;
|
|
42
|
+
const paddedBytes = new Uint8Array(length).fill(0);
|
|
43
|
+
paddedBytes.set(bytes);
|
|
44
|
+
return paddedBytes;
|
|
45
|
+
};
|
|
46
|
+
var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
|
|
47
|
+
|
|
48
|
+
// src/combine-codec.ts
|
|
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
|
+
|
|
74
|
+
// src/fix-codec.ts
|
|
75
|
+
function fixCodecHelper(data, fixedBytes, description) {
|
|
76
|
+
return {
|
|
77
|
+
description: description ?? `fixed(${fixedBytes}, ${data.description})`,
|
|
78
|
+
fixedSize: fixedBytes,
|
|
79
|
+
maxSize: fixedBytes
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function fixEncoder(encoder, fixedBytes, description) {
|
|
83
|
+
return {
|
|
84
|
+
...fixCodecHelper(encoder, fixedBytes, description),
|
|
85
|
+
encode: (value) => fixBytes(encoder.encode(value), fixedBytes)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function fixDecoder(decoder, fixedBytes, description) {
|
|
89
|
+
return {
|
|
90
|
+
...fixCodecHelper(decoder, fixedBytes, description),
|
|
91
|
+
decode: (bytes, offset = 0) => {
|
|
92
|
+
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
|
|
93
|
+
if (offset > 0 || bytes.length > fixedBytes) {
|
|
94
|
+
bytes = bytes.slice(offset, offset + fixedBytes);
|
|
95
|
+
}
|
|
96
|
+
if (decoder.fixedSize !== null) {
|
|
97
|
+
bytes = fixBytes(bytes, decoder.fixedSize);
|
|
98
|
+
}
|
|
99
|
+
const [value] = decoder.decode(bytes, 0);
|
|
100
|
+
return [value, offset + fixedBytes];
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function fixCodec(codec, fixedBytes, description) {
|
|
105
|
+
return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/map-codec.ts
|
|
109
|
+
function mapEncoder(encoder, unmap) {
|
|
110
|
+
return {
|
|
111
|
+
description: encoder.description,
|
|
112
|
+
encode: (value) => encoder.encode(unmap(value)),
|
|
113
|
+
fixedSize: encoder.fixedSize,
|
|
114
|
+
maxSize: encoder.maxSize
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function mapDecoder(decoder, map) {
|
|
118
|
+
return {
|
|
119
|
+
decode: (bytes, offset = 0) => {
|
|
120
|
+
const [value, length] = decoder.decode(bytes, offset);
|
|
121
|
+
return [map(value, bytes, offset), length];
|
|
122
|
+
},
|
|
123
|
+
description: decoder.description,
|
|
124
|
+
fixedSize: decoder.fixedSize,
|
|
125
|
+
maxSize: decoder.maxSize
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function mapCodec(codec, unmap, map) {
|
|
129
|
+
return {
|
|
130
|
+
decode: map ? mapDecoder(codec, map).decode : codec.decode,
|
|
131
|
+
description: codec.description,
|
|
132
|
+
encode: mapEncoder(codec, unmap).encode,
|
|
133
|
+
fixedSize: codec.fixedSize,
|
|
134
|
+
maxSize: codec.maxSize
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/reverse-codec.ts
|
|
139
|
+
function reverseEncoder(encoder) {
|
|
140
|
+
assertFixedSizeCodec(encoder, "Cannot reverse a codec of variable size.");
|
|
141
|
+
return {
|
|
142
|
+
...encoder,
|
|
143
|
+
encode: (value) => encoder.encode(value).reverse()
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function reverseDecoder(decoder) {
|
|
147
|
+
assertFixedSizeCodec(decoder, "Cannot reverse a codec of variable size.");
|
|
148
|
+
return {
|
|
149
|
+
...decoder,
|
|
150
|
+
decode: (bytes, offset = 0) => {
|
|
151
|
+
const reverseEnd = offset + decoder.fixedSize;
|
|
152
|
+
if (offset === 0 && bytes.length === reverseEnd) {
|
|
153
|
+
return decoder.decode(bytes.reverse(), offset);
|
|
154
|
+
}
|
|
155
|
+
const newBytes = mergeBytes([
|
|
156
|
+
...offset === 0 ? [] : [bytes.slice(0, offset)],
|
|
157
|
+
bytes.slice(offset, reverseEnd).reverse(),
|
|
158
|
+
...bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)]
|
|
159
|
+
]);
|
|
160
|
+
return decoder.decode(newBytes, offset);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function reverseCodec(codec) {
|
|
165
|
+
return combineCodec(reverseEncoder(codec), reverseDecoder(codec));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
exports.assertByteArrayHasEnoughBytesForCodec = assertByteArrayHasEnoughBytesForCodec;
|
|
169
|
+
exports.assertByteArrayIsNotEmptyForCodec = assertByteArrayIsNotEmptyForCodec;
|
|
170
|
+
exports.assertFixedSizeCodec = assertFixedSizeCodec;
|
|
171
|
+
exports.combineCodec = combineCodec;
|
|
172
|
+
exports.fixBytes = fixBytes;
|
|
173
|
+
exports.fixCodec = fixCodec;
|
|
174
|
+
exports.fixDecoder = fixDecoder;
|
|
175
|
+
exports.fixEncoder = fixEncoder;
|
|
176
|
+
exports.mapCodec = mapCodec;
|
|
177
|
+
exports.mapDecoder = mapDecoder;
|
|
178
|
+
exports.mapEncoder = mapEncoder;
|
|
179
|
+
exports.mergeBytes = mergeBytes;
|
|
180
|
+
exports.padBytes = padBytes;
|
|
181
|
+
exports.reverseCodec = reverseCodec;
|
|
182
|
+
exports.reverseDecoder = reverseDecoder;
|
|
183
|
+
exports.reverseEncoder = reverseEncoder;
|
|
184
|
+
//# sourceMappingURL=out.js.map
|
|
185
|
+
//# sourceMappingURL=index.browser.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assertions.ts","../src/bytes.ts","../src/combine-codec.ts","../src/fix-codec.ts","../src/map-codec.ts","../src/reverse-codec.ts"],"names":[],"mappings":";AAKO,SAAS,kCAAkC,kBAA0B,OAAmB,SAAS,GAAG;AACvG,MAAI,MAAM,SAAS,UAAU,GAAG;AAE5B,UAAM,IAAI,MAAM,UAAU,gBAAgB,oCAAoC;AAAA,EAClF;AACJ;AAKO,SAAS,sCACZ,kBACA,UACA,OACA,SAAS,GACX;AACE,QAAM,cAAc,MAAM,SAAS;AACn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AAO,MAAM,QAAQ,GAAG,MAAM;AAAA,MACjD;AACA,YAAM,WAAW,WAAW;AAAA,QACxB,GAAI,WAAW,IAAI,CAAC,IAAI,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC;AAAA,QAC/C,MAAM,MAAM,QAAQ,UAAU,EAAE,QAAQ;AAAA,QACxC,GAAI,MAAM,WAAW,aAAa,CAAC,IAAI,CAAC,MAAM,MAAM,UAAU,CAAC;AAAA,MACnE,CAAC;AACD,aAAO,QAAQ,OAAO,UAAU,MAAM;AAAA,IAC1C;AAAA,EACJ;AACJ;AAKO,SAAS,aAAiC,OAAiC;AAC9E,SAAO,aAAa,eAAe,KAAK,GAAG,eAAe,KAAK,CAAC;AACpE","sourcesContent":["import { CodecData } from './codec';\n\n/**\n * Asserts that a given byte array is not empty.\n */\nexport function assertByteArrayIsNotEmptyForCodec(codecDescription: string, bytes: Uint8Array, offset = 0) {\n if (bytes.length - offset <= 0) {\n // TODO: Coded error.\n throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);\n }\n}\n\n/**\n * Asserts that a given byte array has enough bytes to decode.\n */\nexport function assertByteArrayHasEnoughBytesForCodec(\n codecDescription: string,\n expected: number,\n bytes: Uint8Array,\n offset = 0\n) {\n const bytesLength = bytes.length - offset;\n if (bytesLength < expected) {\n // TODO: Coded error.\n throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);\n }\n}\n\n/**\n * Asserts that a given codec is fixed-size codec.\n */\nexport function assertFixedSizeCodec(\n data: Pick<CodecData, 'fixedSize'>,\n message?: string\n): asserts data is { fixedSize: number } {\n if (data.fixedSize === null) {\n // TODO: Coded error.\n throw new Error(message ?? 'Expected a fixed-size codec, got a variable-size one.');\n }\n}\n","/**\n * Concatenates an array of `Uint8Array`s into a single `Uint8Array`.\n * Reuses the original byte array when applicable.\n */\nexport const mergeBytes = (byteArrays: Uint8Array[]): Uint8Array => {\n const nonEmptyByteArrays = byteArrays.filter(arr => arr.length);\n if (nonEmptyByteArrays.length === 0) {\n return byteArrays.length ? byteArrays[0] : new Uint8Array();\n }\n\n if (nonEmptyByteArrays.length === 1) {\n return nonEmptyByteArrays[0];\n }\n\n const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);\n const result = new Uint8Array(totalLength);\n let offset = 0;\n nonEmptyByteArrays.forEach(arr => {\n result.set(arr, offset);\n offset += arr.length;\n });\n return result;\n};\n\n/**\n * Pads a `Uint8Array` with zeroes to the specified length.\n * If the array is longer than the specified length, it is returned as-is.\n */\nexport const padBytes = (bytes: Uint8Array, length: number): Uint8Array => {\n if (bytes.length >= length) return bytes;\n const paddedBytes = new Uint8Array(length).fill(0);\n paddedBytes.set(bytes);\n return paddedBytes;\n};\n\n/**\n * Fixes a `Uint8Array` to the specified length.\n * If the array is longer than the specified length, it is truncated.\n * If the array is shorter than the specified length, it is padded with zeroes.\n */\nexport const fixBytes = (bytes: Uint8Array, length: number): Uint8Array =>\n padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);\n","import { Codec, Decoder, Encoder } from './codec';\n\n/**\n * Combines an encoder and a decoder into a codec.\n * The encoder and decoder must have the same fixed size, max size and description.\n * If a description is provided, it will override the encoder and decoder descriptions.\n */\nexport function combineCodec<From, To extends From = From>(\n encoder: Encoder<From>,\n decoder: Decoder<To>,\n description?: string\n): Codec<From, To> {\n if (encoder.fixedSize !== decoder.fixedSize) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`\n );\n }\n\n if (encoder.maxSize !== decoder.maxSize) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`\n );\n }\n\n if (description === undefined && encoder.description !== decoder.description) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. ` +\n `Pass a custom description as a third argument if you want to override the description and bypass this error.`\n );\n }\n\n return {\n decode: decoder.decode,\n description: description ?? encoder.description,\n encode: encoder.encode,\n fixedSize: encoder.fixedSize,\n maxSize: encoder.maxSize,\n };\n}\n","import { assertByteArrayHasEnoughBytesForCodec } from './assertions';\nimport { fixBytes } from './bytes';\nimport { Codec, CodecData, Decoder, Encoder } from './codec';\nimport { combineCodec } from './combine-codec';\n\nfunction fixCodecHelper(data: CodecData, fixedBytes: number, description?: string): CodecData {\n return {\n description: description ?? `fixed(${fixedBytes}, ${data.description})`,\n fixedSize: fixedBytes,\n maxSize: fixedBytes,\n };\n}\n\n/**\n * Creates a fixed-size encoder from a given encoder.\n *\n * @param encoder - The encoder to wrap into a fixed-size encoder.\n * @param fixedBytes - The fixed number of bytes to write.\n * @param description - A custom description for the encoder.\n */\nexport function fixEncoder<T>(encoder: Encoder<T>, fixedBytes: number, description?: string): Encoder<T> {\n return {\n ...fixCodecHelper(encoder, fixedBytes, description),\n encode: (value: T) => fixBytes(encoder.encode(value), fixedBytes),\n };\n}\n\n/**\n * Creates a fixed-size decoder from a given decoder.\n *\n * @param decoder - The decoder to wrap into a fixed-size decoder.\n * @param fixedBytes - The fixed number of bytes to read.\n * @param description - A custom description for the decoder.\n */\nexport function fixDecoder<T>(decoder: Decoder<T>, fixedBytes: number, description?: string): Decoder<T> {\n return {\n ...fixCodecHelper(decoder, fixedBytes, description),\n decode: (bytes: Uint8Array, offset = 0) => {\n assertByteArrayHasEnoughBytesForCodec('fixCodec', fixedBytes, bytes, offset);\n // Slice the byte array to the fixed size if necessary.\n if (offset > 0 || bytes.length > fixedBytes) {\n bytes = bytes.slice(offset, offset + fixedBytes);\n }\n // If the nested decoder is fixed-size, pad and truncate the byte array accordingly.\n if (decoder.fixedSize !== null) {\n bytes = fixBytes(bytes, decoder.fixedSize);\n }\n // Decode the value using the nested decoder.\n const [value] = decoder.decode(bytes, 0);\n return [value, offset + fixedBytes];\n },\n };\n}\n\n/**\n * Creates a fixed-size codec from a given codec.\n *\n * @param codec - The codec to wrap into a fixed-size codec.\n * @param fixedBytes - The fixed number of bytes to read/write.\n * @param description - A custom description for the codec.\n */\nexport function fixCodec<T, U extends T = T>(\n codec: Codec<T, U>,\n fixedBytes: number,\n description?: string\n): Codec<T, U> {\n return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description));\n}\n","import { Codec, Decoder, Encoder } from './codec';\n\n/**\n * Converts an encoder A to a encoder B by mapping their values.\n */\nexport function mapEncoder<T, U>(encoder: Encoder<T>, unmap: (value: U) => T): Encoder<U> {\n return {\n description: encoder.description,\n encode: (value: U) => encoder.encode(unmap(value)),\n fixedSize: encoder.fixedSize,\n maxSize: encoder.maxSize,\n };\n}\n\n/**\n * Converts an decoder A to a decoder B by mapping their values.\n */\nexport function mapDecoder<T, U>(\n decoder: Decoder<T>,\n map: (value: T, bytes: Uint8Array, offset: number) => U\n): Decoder<U> {\n return {\n decode: (bytes: Uint8Array, offset = 0) => {\n const [value, length] = decoder.decode(bytes, offset);\n return [map(value, bytes, offset), length];\n },\n description: decoder.description,\n fixedSize: decoder.fixedSize,\n maxSize: decoder.maxSize,\n };\n}\n\n/**\n * Converts a codec A to a codec B by mapping their values.\n */\nexport function mapCodec<NewFrom, OldFrom, To extends NewFrom & OldFrom>(\n codec: Codec<OldFrom, To>,\n unmap: (value: NewFrom) => OldFrom\n): Codec<NewFrom, To>;\nexport function mapCodec<NewFrom, OldFrom, NewTo extends NewFrom = NewFrom, OldTo extends OldFrom = OldFrom>(\n codec: Codec<OldFrom, OldTo>,\n unmap: (value: NewFrom) => OldFrom,\n map: (value: OldTo, bytes: Uint8Array, offset: number) => NewTo\n): Codec<NewFrom, NewTo>;\nexport function mapCodec<NewFrom, OldFrom, NewTo extends NewFrom = NewFrom, OldTo extends OldFrom = OldFrom>(\n codec: Codec<OldFrom, OldTo>,\n unmap: (value: NewFrom) => OldFrom,\n map?: (value: OldTo, bytes: Uint8Array, offset: number) => NewTo\n): Codec<NewFrom, NewTo> {\n return {\n decode: map ? mapDecoder(codec, map).decode : (codec.decode as unknown as Decoder<NewTo>['decode']),\n description: codec.description,\n encode: mapEncoder(codec, unmap).encode,\n fixedSize: codec.fixedSize,\n maxSize: codec.maxSize,\n };\n}\n","import { assertFixedSizeCodec } from './assertions';\nimport { mergeBytes } from './bytes';\nimport { Codec, Decoder, Encoder } from './codec';\nimport { combineCodec } from './combine-codec';\n\n/**\n * Reverses the bytes of a fixed-size encoder.\n */\nexport function reverseEncoder<T>(encoder: Encoder<T>): Encoder<T> {\n assertFixedSizeCodec(encoder, 'Cannot reverse a codec of variable size.');\n return {\n ...encoder,\n encode: (value: T) => encoder.encode(value).reverse(),\n };\n}\n\n/**\n * Reverses the bytes of a fixed-size decoder.\n */\nexport function reverseDecoder<T>(decoder: Decoder<T>): Decoder<T> {\n assertFixedSizeCodec(decoder, 'Cannot reverse a codec of variable size.');\n return {\n ...decoder,\n decode: (bytes: Uint8Array, offset = 0) => {\n const reverseEnd = offset + decoder.fixedSize;\n if (offset === 0 && bytes.length === reverseEnd) {\n return decoder.decode(bytes.reverse(), offset);\n }\n const newBytes = mergeBytes([\n ...(offset === 0 ? [] : [bytes.slice(0, offset)]),\n bytes.slice(offset, reverseEnd).reverse(),\n ...(bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)]),\n ]);\n return decoder.decode(newBytes, offset);\n },\n };\n}\n\n/**\n * Reverses the bytes of a fixed-size codec.\n */\nexport function reverseCodec<T, U extends T = T>(codec: Codec<T, U>): Codec<T, U> {\n return combineCodec(reverseEncoder(codec), reverseDecoder(codec));\n}\n"]}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// src/assertions.ts
|
|
2
|
+
function assertByteArrayIsNotEmptyForCodec(codecDescription, bytes, offset = 0) {
|
|
3
|
+
if (bytes.length - offset <= 0) {
|
|
4
|
+
throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
function assertByteArrayHasEnoughBytesForCodec(codecDescription, expected, bytes, offset = 0) {
|
|
8
|
+
const bytesLength = bytes.length - offset;
|
|
9
|
+
if (bytesLength < expected) {
|
|
10
|
+
throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function assertFixedSizeCodec(data, message) {
|
|
14
|
+
if (data.fixedSize === null) {
|
|
15
|
+
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one.");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/bytes.ts
|
|
20
|
+
var mergeBytes = (byteArrays) => {
|
|
21
|
+
const nonEmptyByteArrays = byteArrays.filter((arr) => arr.length);
|
|
22
|
+
if (nonEmptyByteArrays.length === 0) {
|
|
23
|
+
return byteArrays.length ? byteArrays[0] : new Uint8Array();
|
|
24
|
+
}
|
|
25
|
+
if (nonEmptyByteArrays.length === 1) {
|
|
26
|
+
return nonEmptyByteArrays[0];
|
|
27
|
+
}
|
|
28
|
+
const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);
|
|
29
|
+
const result = new Uint8Array(totalLength);
|
|
30
|
+
let offset = 0;
|
|
31
|
+
nonEmptyByteArrays.forEach((arr) => {
|
|
32
|
+
result.set(arr, offset);
|
|
33
|
+
offset += arr.length;
|
|
34
|
+
});
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
var padBytes = (bytes, length) => {
|
|
38
|
+
if (bytes.length >= length)
|
|
39
|
+
return bytes;
|
|
40
|
+
const paddedBytes = new Uint8Array(length).fill(0);
|
|
41
|
+
paddedBytes.set(bytes);
|
|
42
|
+
return paddedBytes;
|
|
43
|
+
};
|
|
44
|
+
var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
|
|
45
|
+
|
|
46
|
+
// src/combine-codec.ts
|
|
47
|
+
function combineCodec(encoder, decoder, description) {
|
|
48
|
+
if (encoder.fixedSize !== decoder.fixedSize) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (encoder.maxSize !== decoder.maxSize) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
if (description === void 0 && encoder.description !== decoder.description) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`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.`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
decode: decoder.decode,
|
|
65
|
+
description: description ?? encoder.description,
|
|
66
|
+
encode: encoder.encode,
|
|
67
|
+
fixedSize: encoder.fixedSize,
|
|
68
|
+
maxSize: encoder.maxSize
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/fix-codec.ts
|
|
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
|
+
function fixCodec(codec, fixedBytes, description) {
|
|
103
|
+
return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/map-codec.ts
|
|
107
|
+
function mapEncoder(encoder, unmap) {
|
|
108
|
+
return {
|
|
109
|
+
description: encoder.description,
|
|
110
|
+
encode: (value) => encoder.encode(unmap(value)),
|
|
111
|
+
fixedSize: encoder.fixedSize,
|
|
112
|
+
maxSize: encoder.maxSize
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function mapDecoder(decoder, map) {
|
|
116
|
+
return {
|
|
117
|
+
decode: (bytes, offset = 0) => {
|
|
118
|
+
const [value, length] = decoder.decode(bytes, offset);
|
|
119
|
+
return [map(value, bytes, offset), length];
|
|
120
|
+
},
|
|
121
|
+
description: decoder.description,
|
|
122
|
+
fixedSize: decoder.fixedSize,
|
|
123
|
+
maxSize: decoder.maxSize
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function mapCodec(codec, unmap, map) {
|
|
127
|
+
return {
|
|
128
|
+
decode: map ? mapDecoder(codec, map).decode : codec.decode,
|
|
129
|
+
description: codec.description,
|
|
130
|
+
encode: mapEncoder(codec, unmap).encode,
|
|
131
|
+
fixedSize: codec.fixedSize,
|
|
132
|
+
maxSize: codec.maxSize
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/reverse-codec.ts
|
|
137
|
+
function reverseEncoder(encoder) {
|
|
138
|
+
assertFixedSizeCodec(encoder, "Cannot reverse a codec of variable size.");
|
|
139
|
+
return {
|
|
140
|
+
...encoder,
|
|
141
|
+
encode: (value) => encoder.encode(value).reverse()
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function reverseDecoder(decoder) {
|
|
145
|
+
assertFixedSizeCodec(decoder, "Cannot reverse a codec of variable size.");
|
|
146
|
+
return {
|
|
147
|
+
...decoder,
|
|
148
|
+
decode: (bytes, offset = 0) => {
|
|
149
|
+
const reverseEnd = offset + decoder.fixedSize;
|
|
150
|
+
if (offset === 0 && bytes.length === reverseEnd) {
|
|
151
|
+
return decoder.decode(bytes.reverse(), offset);
|
|
152
|
+
}
|
|
153
|
+
const newBytes = mergeBytes([
|
|
154
|
+
...offset === 0 ? [] : [bytes.slice(0, offset)],
|
|
155
|
+
bytes.slice(offset, reverseEnd).reverse(),
|
|
156
|
+
...bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)]
|
|
157
|
+
]);
|
|
158
|
+
return decoder.decode(newBytes, offset);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function reverseCodec(codec) {
|
|
163
|
+
return combineCodec(reverseEncoder(codec), reverseDecoder(codec));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertFixedSizeCodec, combineCodec, fixBytes, fixCodec, fixDecoder, fixEncoder, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder };
|
|
167
|
+
//# sourceMappingURL=out.js.map
|
|
168
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assertions.ts","../src/bytes.ts","../src/combine-codec.ts","../src/fix-codec.ts","../src/map-codec.ts","../src/reverse-codec.ts"],"names":[],"mappings":";AAKO,SAAS,kCAAkC,kBAA0B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iC;AAC/D,uBAAqB,SAAS,0CAA0C;AACxE,SAAO;AAAA,IACH,GAAG;AAAA,IACH,QAAQ,CAAC,OAAmB,SAAS,MAAM;AACvC,YAAM,aAAa,SAAS,QAAQ;AACpC,UAAI,WAAW,KAAK,MAAM,WAAW,YAAY;AAC7C,eAAO,QAAQ,OAAO,MAAM,QAAQ,GAAG,MAAM;AAAA,MACjD;AACA,YAAM,WAAW,WAAW;AAAA,QACxB,GAAI,WAAW,IAAI,CAAC,IAAI,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC;AAAA,QAC/C,MAAM,MAAM,QAAQ,UAAU,EAAE,QAAQ;AAAA,QACxC,GAAI,MAAM,WAAW,aAAa,CAAC,IAAI,CAAC,MAAM,MAAM,UAAU,CAAC;AAAA,MACnE,CAAC;AACD,aAAO,QAAQ,OAAO,UAAU,MAAM;AAAA,IAC1C;AAAA,EACJ;AACJ;AAKO,SAAS,aAAiC,OAAiC;AAC9E,SAAO,aAAa,eAAe,KAAK,GAAG,eAAe,KAAK,CAAC;AACpE","sourcesContent":["import { CodecData } from './codec';\n\n/**\n * Asserts that a given byte array is not empty.\n */\nexport function assertByteArrayIsNotEmptyForCodec(codecDescription: string, bytes: Uint8Array, offset = 0) {\n if (bytes.length - offset <= 0) {\n // TODO: Coded error.\n throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);\n }\n}\n\n/**\n * Asserts that a given byte array has enough bytes to decode.\n */\nexport function assertByteArrayHasEnoughBytesForCodec(\n codecDescription: string,\n expected: number,\n bytes: Uint8Array,\n offset = 0\n) {\n const bytesLength = bytes.length - offset;\n if (bytesLength < expected) {\n // TODO: Coded error.\n throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);\n }\n}\n\n/**\n * Asserts that a given codec is fixed-size codec.\n */\nexport function assertFixedSizeCodec(\n data: Pick<CodecData, 'fixedSize'>,\n message?: string\n): asserts data is { fixedSize: number } {\n if (data.fixedSize === null) {\n // TODO: Coded error.\n throw new Error(message ?? 'Expected a fixed-size codec, got a variable-size one.');\n }\n}\n","/**\n * Concatenates an array of `Uint8Array`s into a single `Uint8Array`.\n * Reuses the original byte array when applicable.\n */\nexport const mergeBytes = (byteArrays: Uint8Array[]): Uint8Array => {\n const nonEmptyByteArrays = byteArrays.filter(arr => arr.length);\n if (nonEmptyByteArrays.length === 0) {\n return byteArrays.length ? byteArrays[0] : new Uint8Array();\n }\n\n if (nonEmptyByteArrays.length === 1) {\n return nonEmptyByteArrays[0];\n }\n\n const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);\n const result = new Uint8Array(totalLength);\n let offset = 0;\n nonEmptyByteArrays.forEach(arr => {\n result.set(arr, offset);\n offset += arr.length;\n });\n return result;\n};\n\n/**\n * Pads a `Uint8Array` with zeroes to the specified length.\n * If the array is longer than the specified length, it is returned as-is.\n */\nexport const padBytes = (bytes: Uint8Array, length: number): Uint8Array => {\n if (bytes.length >= length) return bytes;\n const paddedBytes = new Uint8Array(length).fill(0);\n paddedBytes.set(bytes);\n return paddedBytes;\n};\n\n/**\n * Fixes a `Uint8Array` to the specified length.\n * If the array is longer than the specified length, it is truncated.\n * If the array is shorter than the specified length, it is padded with zeroes.\n */\nexport const fixBytes = (bytes: Uint8Array, length: number): Uint8Array =>\n padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);\n","import { Codec, Decoder, Encoder } from './codec';\n\n/**\n * Combines an encoder and a decoder into a codec.\n * The encoder and decoder must have the same fixed size, max size and description.\n * If a description is provided, it will override the encoder and decoder descriptions.\n */\nexport function combineCodec<From, To extends From = From>(\n encoder: Encoder<From>,\n decoder: Decoder<To>,\n description?: string\n): Codec<From, To> {\n if (encoder.fixedSize !== decoder.fixedSize) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`\n );\n }\n\n if (encoder.maxSize !== decoder.maxSize) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`\n );\n }\n\n if (description === undefined && encoder.description !== decoder.description) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. ` +\n `Pass a custom description as a third argument if you want to override the description and bypass this error.`\n );\n }\n\n return {\n decode: decoder.decode,\n description: description ?? encoder.description,\n encode: encoder.encode,\n fixedSize: encoder.fixedSize,\n maxSize: encoder.maxSize,\n };\n}\n","import { assertByteArrayHasEnoughBytesForCodec } from './assertions';\nimport { fixBytes } from './bytes';\nimport { Codec, CodecData, Decoder, Encoder } from './codec';\nimport { combineCodec } from './combine-codec';\n\nfunction fixCodecHelper(data: CodecData, fixedBytes: number, description?: string): CodecData {\n return {\n description: description ?? `fixed(${fixedBytes}, ${data.description})`,\n fixedSize: fixedBytes,\n maxSize: fixedBytes,\n };\n}\n\n/**\n * Creates a fixed-size encoder from a given encoder.\n *\n * @param encoder - The encoder to wrap into a fixed-size encoder.\n * @param fixedBytes - The fixed number of bytes to write.\n * @param description - A custom description for the encoder.\n */\nexport function fixEncoder<T>(encoder: Encoder<T>, fixedBytes: number, description?: string): Encoder<T> {\n return {\n ...fixCodecHelper(encoder, fixedBytes, description),\n encode: (value: T) => fixBytes(encoder.encode(value), fixedBytes),\n };\n}\n\n/**\n * Creates a fixed-size decoder from a given decoder.\n *\n * @param decoder - The decoder to wrap into a fixed-size decoder.\n * @param fixedBytes - The fixed number of bytes to read.\n * @param description - A custom description for the decoder.\n */\nexport function fixDecoder<T>(decoder: Decoder<T>, fixedBytes: number, description?: string): Decoder<T> {\n return {\n ...fixCodecHelper(decoder, fixedBytes, description),\n decode: (bytes: Uint8Array, offset = 0) => {\n assertByteArrayHasEnoughBytesForCodec('fixCodec', fixedBytes, bytes, offset);\n // Slice the byte array to the fixed size if necessary.\n if (offset > 0 || bytes.length > fixedBytes) {\n bytes = bytes.slice(offset, offset + fixedBytes);\n }\n // If the nested decoder is fixed-size, pad and truncate the byte array accordingly.\n if (decoder.fixedSize !== null) {\n bytes = fixBytes(bytes, decoder.fixedSize);\n }\n // Decode the value using the nested decoder.\n const [value] = decoder.decode(bytes, 0);\n return [value, offset + fixedBytes];\n },\n };\n}\n\n/**\n * Creates a fixed-size codec from a given codec.\n *\n * @param codec - The codec to wrap into a fixed-size codec.\n * @param fixedBytes - The fixed number of bytes to read/write.\n * @param description - A custom description for the codec.\n */\nexport function fixCodec<T, U extends T = T>(\n codec: Codec<T, U>,\n fixedBytes: number,\n description?: string\n): Codec<T, U> {\n return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description));\n}\n","import { Codec, Decoder, Encoder } from './codec';\n\n/**\n * Converts an encoder A to a encoder B by mapping their values.\n */\nexport function mapEncoder<T, U>(encoder: Encoder<T>, unmap: (value: U) => T): Encoder<U> {\n return {\n description: encoder.description,\n encode: (value: U) => encoder.encode(unmap(value)),\n fixedSize: encoder.fixedSize,\n maxSize: encoder.maxSize,\n };\n}\n\n/**\n * Converts an decoder A to a decoder B by mapping their values.\n */\nexport function mapDecoder<T, U>(\n decoder: Decoder<T>,\n map: (value: T, bytes: Uint8Array, offset: number) => U\n): Decoder<U> {\n return {\n decode: (bytes: Uint8Array, offset = 0) => {\n const [value, length] = decoder.decode(bytes, offset);\n return [map(value, bytes, offset), length];\n },\n description: decoder.description,\n fixedSize: decoder.fixedSize,\n maxSize: decoder.maxSize,\n };\n}\n\n/**\n * Converts a codec A to a codec B by mapping their values.\n */\nexport function mapCodec<NewFrom, OldFrom, To extends NewFrom & OldFrom>(\n codec: Codec<OldFrom, To>,\n unmap: (value: NewFrom) => OldFrom\n): Codec<NewFrom, To>;\nexport function mapCodec<NewFrom, OldFrom, NewTo extends NewFrom = NewFrom, OldTo extends OldFrom = OldFrom>(\n codec: Codec<OldFrom, OldTo>,\n unmap: (value: NewFrom) => OldFrom,\n map: (value: OldTo, bytes: Uint8Array, offset: number) => NewTo\n): Codec<NewFrom, NewTo>;\nexport function mapCodec<NewFrom, OldFrom, NewTo extends NewFrom = NewFrom, OldTo extends OldFrom = OldFrom>(\n codec: Codec<OldFrom, OldTo>,\n unmap: (value: NewFrom) => OldFrom,\n map?: (value: OldTo, bytes: Uint8Array, offset: number) => NewTo\n): Codec<NewFrom, NewTo> {\n return {\n decode: map ? mapDecoder(codec, map).decode : (codec.decode as unknown as Decoder<NewTo>['decode']),\n description: codec.description,\n encode: mapEncoder(codec, unmap).encode,\n fixedSize: codec.fixedSize,\n maxSize: codec.maxSize,\n };\n}\n","import { assertFixedSizeCodec } from './assertions';\nimport { mergeBytes } from './bytes';\nimport { Codec, Decoder, Encoder } from './codec';\nimport { combineCodec } from './combine-codec';\n\n/**\n * Reverses the bytes of a fixed-size encoder.\n */\nexport function reverseEncoder<T>(encoder: Encoder<T>): Encoder<T> {\n assertFixedSizeCodec(encoder, 'Cannot reverse a codec of variable size.');\n return {\n ...encoder,\n encode: (value: T) => encoder.encode(value).reverse(),\n };\n}\n\n/**\n * Reverses the bytes of a fixed-size decoder.\n */\nexport function reverseDecoder<T>(decoder: Decoder<T>): Decoder<T> {\n assertFixedSizeCodec(decoder, 'Cannot reverse a codec of variable size.');\n return {\n ...decoder,\n decode: (bytes: Uint8Array, offset = 0) => {\n const reverseEnd = offset + decoder.fixedSize;\n if (offset === 0 && bytes.length === reverseEnd) {\n return decoder.decode(bytes.reverse(), offset);\n }\n const newBytes = mergeBytes([\n ...(offset === 0 ? [] : [bytes.slice(0, offset)]),\n bytes.slice(offset, reverseEnd).reverse(),\n ...(bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)]),\n ]);\n return decoder.decode(newBytes, offset);\n },\n };\n}\n\n/**\n * Reverses the bytes of a fixed-size codec.\n */\nexport function reverseCodec<T, U extends T = T>(codec: Codec<T, U>): Codec<T, U> {\n return combineCodec(reverseEncoder(codec), reverseDecoder(codec));\n}\n"]}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
this.globalThis = this.globalThis || {};
|
|
2
|
+
this.globalThis.solanaWeb3 = (function (exports) {
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
// src/assertions.ts
|
|
6
|
+
function assertByteArrayIsNotEmptyForCodec(codecDescription, bytes, offset = 0) {
|
|
7
|
+
if (bytes.length - offset <= 0) {
|
|
8
|
+
throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function assertByteArrayHasEnoughBytesForCodec(codecDescription, expected, bytes, offset = 0) {
|
|
12
|
+
const bytesLength = bytes.length - offset;
|
|
13
|
+
if (bytesLength < expected) {
|
|
14
|
+
throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function assertFixedSizeCodec(data, message) {
|
|
18
|
+
if (data.fixedSize === null) {
|
|
19
|
+
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one.");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/bytes.ts
|
|
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
|
+
|
|
50
|
+
// src/combine-codec.ts
|
|
51
|
+
function combineCodec(encoder, decoder, description) {
|
|
52
|
+
if (encoder.fixedSize !== decoder.fixedSize) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
if (encoder.maxSize !== decoder.maxSize) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
if (description === void 0 && encoder.description !== decoder.description) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`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.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
decode: decoder.decode,
|
|
69
|
+
description: description ?? encoder.description,
|
|
70
|
+
encode: encoder.encode,
|
|
71
|
+
fixedSize: encoder.fixedSize,
|
|
72
|
+
maxSize: encoder.maxSize
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/fix-codec.ts
|
|
77
|
+
function fixCodecHelper(data, fixedBytes, description) {
|
|
78
|
+
return {
|
|
79
|
+
description: description ?? `fixed(${fixedBytes}, ${data.description})`,
|
|
80
|
+
fixedSize: fixedBytes,
|
|
81
|
+
maxSize: fixedBytes
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function fixEncoder(encoder, fixedBytes, description) {
|
|
85
|
+
return {
|
|
86
|
+
...fixCodecHelper(encoder, fixedBytes, description),
|
|
87
|
+
encode: (value) => fixBytes(encoder.encode(value), fixedBytes)
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function fixDecoder(decoder, fixedBytes, description) {
|
|
91
|
+
return {
|
|
92
|
+
...fixCodecHelper(decoder, fixedBytes, description),
|
|
93
|
+
decode: (bytes, offset = 0) => {
|
|
94
|
+
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
|
|
95
|
+
if (offset > 0 || bytes.length > fixedBytes) {
|
|
96
|
+
bytes = bytes.slice(offset, offset + fixedBytes);
|
|
97
|
+
}
|
|
98
|
+
if (decoder.fixedSize !== null) {
|
|
99
|
+
bytes = fixBytes(bytes, decoder.fixedSize);
|
|
100
|
+
}
|
|
101
|
+
const [value] = decoder.decode(bytes, 0);
|
|
102
|
+
return [value, offset + fixedBytes];
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function fixCodec(codec, fixedBytes, description) {
|
|
107
|
+
return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/map-codec.ts
|
|
111
|
+
function mapEncoder(encoder, unmap) {
|
|
112
|
+
return {
|
|
113
|
+
description: encoder.description,
|
|
114
|
+
encode: (value) => encoder.encode(unmap(value)),
|
|
115
|
+
fixedSize: encoder.fixedSize,
|
|
116
|
+
maxSize: encoder.maxSize
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function mapDecoder(decoder, map) {
|
|
120
|
+
return {
|
|
121
|
+
decode: (bytes, offset = 0) => {
|
|
122
|
+
const [value, length] = decoder.decode(bytes, offset);
|
|
123
|
+
return [map(value, bytes, offset), length];
|
|
124
|
+
},
|
|
125
|
+
description: decoder.description,
|
|
126
|
+
fixedSize: decoder.fixedSize,
|
|
127
|
+
maxSize: decoder.maxSize
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function mapCodec(codec, unmap, map) {
|
|
131
|
+
return {
|
|
132
|
+
decode: map ? mapDecoder(codec, map).decode : codec.decode,
|
|
133
|
+
description: codec.description,
|
|
134
|
+
encode: mapEncoder(codec, unmap).encode,
|
|
135
|
+
fixedSize: codec.fixedSize,
|
|
136
|
+
maxSize: codec.maxSize
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/reverse-codec.ts
|
|
141
|
+
function reverseEncoder(encoder) {
|
|
142
|
+
assertFixedSizeCodec(encoder, "Cannot reverse a codec of variable size.");
|
|
143
|
+
return {
|
|
144
|
+
...encoder,
|
|
145
|
+
encode: (value) => encoder.encode(value).reverse()
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function reverseDecoder(decoder) {
|
|
149
|
+
assertFixedSizeCodec(decoder, "Cannot reverse a codec of variable size.");
|
|
150
|
+
return {
|
|
151
|
+
...decoder,
|
|
152
|
+
decode: (bytes, offset = 0) => {
|
|
153
|
+
const reverseEnd = offset + decoder.fixedSize;
|
|
154
|
+
if (offset === 0 && bytes.length === reverseEnd) {
|
|
155
|
+
return decoder.decode(bytes.reverse(), offset);
|
|
156
|
+
}
|
|
157
|
+
const newBytes = mergeBytes([
|
|
158
|
+
...offset === 0 ? [] : [bytes.slice(0, offset)],
|
|
159
|
+
bytes.slice(offset, reverseEnd).reverse(),
|
|
160
|
+
...bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)]
|
|
161
|
+
]);
|
|
162
|
+
return decoder.decode(newBytes, offset);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function reverseCodec(codec) {
|
|
167
|
+
return combineCodec(reverseEncoder(codec), reverseDecoder(codec));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
exports.assertByteArrayHasEnoughBytesForCodec = assertByteArrayHasEnoughBytesForCodec;
|
|
171
|
+
exports.assertByteArrayIsNotEmptyForCodec = assertByteArrayIsNotEmptyForCodec;
|
|
172
|
+
exports.assertFixedSizeCodec = assertFixedSizeCodec;
|
|
173
|
+
exports.combineCodec = combineCodec;
|
|
174
|
+
exports.fixBytes = fixBytes;
|
|
175
|
+
exports.fixCodec = fixCodec;
|
|
176
|
+
exports.fixDecoder = fixDecoder;
|
|
177
|
+
exports.fixEncoder = fixEncoder;
|
|
178
|
+
exports.mapCodec = mapCodec;
|
|
179
|
+
exports.mapDecoder = mapDecoder;
|
|
180
|
+
exports.mapEncoder = mapEncoder;
|
|
181
|
+
exports.mergeBytes = mergeBytes;
|
|
182
|
+
exports.padBytes = padBytes;
|
|
183
|
+
exports.reverseCodec = reverseCodec;
|
|
184
|
+
exports.reverseDecoder = reverseDecoder;
|
|
185
|
+
exports.reverseEncoder = reverseEncoder;
|
|
186
|
+
|
|
187
|
+
return exports;
|
|
188
|
+
|
|
189
|
+
})({});
|
|
190
|
+
//# sourceMappingURL=out.js.map
|
|
191
|
+
//# sourceMappingURL=index.development.js.map
|