@solana/codecs-strings 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.
Files changed (38) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +25 -0
  3. package/dist/index.browser.cjs +298 -0
  4. package/dist/index.browser.cjs.map +1 -0
  5. package/dist/index.browser.js +270 -0
  6. package/dist/index.browser.js.map +1 -0
  7. package/dist/index.development.js +469 -0
  8. package/dist/index.development.js.map +1 -0
  9. package/dist/index.native.js +251 -0
  10. package/dist/index.native.js.map +1 -0
  11. package/dist/index.node.cjs +293 -0
  12. package/dist/index.node.cjs.map +1 -0
  13. package/dist/index.node.js +265 -0
  14. package/dist/index.node.js.map +1 -0
  15. package/dist/index.production.min.js +37 -0
  16. package/dist/types/assertions.d.ts +5 -0
  17. package/dist/types/assertions.d.ts.map +1 -0
  18. package/dist/types/base10.d.ts +7 -0
  19. package/dist/types/base10.d.ts.map +1 -0
  20. package/dist/types/base16.d.ts +8 -0
  21. package/dist/types/base16.d.ts.map +1 -0
  22. package/dist/types/base58.d.ts +7 -0
  23. package/dist/types/base58.d.ts.map +1 -0
  24. package/dist/types/base64.d.ts +8 -0
  25. package/dist/types/base64.d.ts.map +1 -0
  26. package/dist/types/baseX-reslice.d.ts +20 -0
  27. package/dist/types/baseX-reslice.d.ts.map +1 -0
  28. package/dist/types/baseX.d.ts +24 -0
  29. package/dist/types/baseX.d.ts.map +1 -0
  30. package/dist/types/index.d.ts +11 -0
  31. package/dist/types/index.d.ts.map +1 -0
  32. package/dist/types/null-characters.d.ts +5 -0
  33. package/dist/types/null-characters.d.ts.map +1 -0
  34. package/dist/types/string.d.ts +25 -0
  35. package/dist/types/string.d.ts.map +1 -0
  36. package/dist/types/utf8.d.ts +8 -0
  37. package/dist/types/utf8.d.ts.map +1 -0
  38. package/package.json +104 -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-strings/experimental.svg?style=flat
10
+ [npm-image]: https://img.shields.io/npm/v/@solana/codecs-strings/experimental.svg?style=flat
11
+ [npm-url]: https://www.npmjs.com/package/@solana/codecs-strings/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-strings
16
+
17
+ This package contains codecs for strings of different sizes and encodings. 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,298 @@
1
+ 'use strict';
2
+
3
+ var codecsCore = require('@solana/codecs-core');
4
+ var codecsNumbers = require('@solana/codecs-numbers');
5
+
6
+ // src/assertions.ts
7
+ function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
8
+ if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
9
+ throw new Error(`Expected a string of base ${alphabet4.length}, got [${givenValue}].`);
10
+ }
11
+ }
12
+ var getBaseXEncoder = (alphabet4) => {
13
+ const base = alphabet4.length;
14
+ const baseBigInt = BigInt(base);
15
+ return {
16
+ description: `base${base}`,
17
+ encode(value) {
18
+ assertValidBaseString(alphabet4, value);
19
+ if (value === "")
20
+ return new Uint8Array();
21
+ const chars = [...value];
22
+ let trailIndex = chars.findIndex((c) => c !== alphabet4[0]);
23
+ trailIndex = trailIndex === -1 ? chars.length : trailIndex;
24
+ const leadingZeroes = Array(trailIndex).fill(0);
25
+ if (trailIndex === chars.length)
26
+ return Uint8Array.from(leadingZeroes);
27
+ const tailChars = chars.slice(trailIndex);
28
+ let base10Number = 0n;
29
+ let baseXPower = 1n;
30
+ for (let i = tailChars.length - 1; i >= 0; i -= 1) {
31
+ base10Number += baseXPower * BigInt(alphabet4.indexOf(tailChars[i]));
32
+ baseXPower *= baseBigInt;
33
+ }
34
+ const tailBytes = [];
35
+ while (base10Number > 0n) {
36
+ tailBytes.unshift(Number(base10Number % 256n));
37
+ base10Number /= 256n;
38
+ }
39
+ return Uint8Array.from(leadingZeroes.concat(tailBytes));
40
+ },
41
+ fixedSize: null,
42
+ maxSize: null
43
+ };
44
+ };
45
+ var getBaseXDecoder = (alphabet4) => {
46
+ const base = alphabet4.length;
47
+ const baseBigInt = BigInt(base);
48
+ return {
49
+ decode(rawBytes, offset = 0) {
50
+ const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
51
+ if (bytes.length === 0)
52
+ return ["", 0];
53
+ let trailIndex = bytes.findIndex((n) => n !== 0);
54
+ trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
55
+ const leadingZeroes = alphabet4[0].repeat(trailIndex);
56
+ if (trailIndex === bytes.length)
57
+ return [leadingZeroes, rawBytes.length];
58
+ let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
59
+ const tailChars = [];
60
+ while (base10Number > 0n) {
61
+ tailChars.unshift(alphabet4[Number(base10Number % baseBigInt)]);
62
+ base10Number /= baseBigInt;
63
+ }
64
+ return [leadingZeroes + tailChars.join(""), rawBytes.length];
65
+ },
66
+ description: `base${base}`,
67
+ fixedSize: null,
68
+ maxSize: null
69
+ };
70
+ };
71
+ var getBaseXCodec = (alphabet4) => codecsCore.combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
72
+
73
+ // src/base10.ts
74
+ var alphabet = "0123456789";
75
+ var getBase10Encoder = () => getBaseXEncoder(alphabet);
76
+ var getBase10Decoder = () => getBaseXDecoder(alphabet);
77
+ var getBase10Codec = () => getBaseXCodec(alphabet);
78
+ var getBase16Encoder = () => ({
79
+ description: "base16",
80
+ encode(value) {
81
+ const lowercaseValue = value.toLowerCase();
82
+ assertValidBaseString("0123456789abcdef", lowercaseValue, value);
83
+ const matches = lowercaseValue.match(/.{1,2}/g);
84
+ return Uint8Array.from(matches ? matches.map((byte) => parseInt(byte, 16)) : []);
85
+ },
86
+ fixedSize: null,
87
+ maxSize: null
88
+ });
89
+ var getBase16Decoder = () => ({
90
+ decode(bytes, offset = 0) {
91
+ const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
92
+ return [value, bytes.length];
93
+ },
94
+ description: "base16",
95
+ fixedSize: null,
96
+ maxSize: null
97
+ });
98
+ var getBase16Codec = () => codecsCore.combineCodec(getBase16Encoder(), getBase16Decoder());
99
+
100
+ // src/base58.ts
101
+ var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
102
+ var getBase58Encoder = () => getBaseXEncoder(alphabet2);
103
+ var getBase58Decoder = () => getBaseXDecoder(alphabet2);
104
+ var getBase58Codec = () => getBaseXCodec(alphabet2);
105
+ var getBaseXResliceEncoder = (alphabet4, bits) => ({
106
+ description: `base${alphabet4.length}`,
107
+ encode(value) {
108
+ assertValidBaseString(alphabet4, value);
109
+ if (value === "")
110
+ return new Uint8Array();
111
+ const charIndices = [...value].map((c) => alphabet4.indexOf(c));
112
+ return new Uint8Array(reslice(charIndices, bits, 8, false));
113
+ },
114
+ fixedSize: null,
115
+ maxSize: null
116
+ });
117
+ var getBaseXResliceDecoder = (alphabet4, bits) => ({
118
+ decode(rawBytes, offset = 0) {
119
+ const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
120
+ if (bytes.length === 0)
121
+ return ["", rawBytes.length];
122
+ const charIndices = reslice([...bytes], 8, bits, true);
123
+ return [charIndices.map((i) => alphabet4[i]).join(""), rawBytes.length];
124
+ },
125
+ description: `base${alphabet4.length}`,
126
+ fixedSize: null,
127
+ maxSize: null
128
+ });
129
+ var getBaseXResliceCodec = (alphabet4, bits) => codecsCore.combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
130
+ function reslice(input, inputBits, outputBits, useRemainder) {
131
+ const output = [];
132
+ let accumulator = 0;
133
+ let bitsInAccumulator = 0;
134
+ const mask = (1 << outputBits) - 1;
135
+ for (const value of input) {
136
+ accumulator = accumulator << inputBits | value;
137
+ bitsInAccumulator += inputBits;
138
+ while (bitsInAccumulator >= outputBits) {
139
+ bitsInAccumulator -= outputBits;
140
+ output.push(accumulator >> bitsInAccumulator & mask);
141
+ }
142
+ }
143
+ if (useRemainder && bitsInAccumulator > 0) {
144
+ output.push(accumulator << outputBits - bitsInAccumulator & mask);
145
+ }
146
+ return output;
147
+ }
148
+ var getBase64Encoder = () => {
149
+ {
150
+ return {
151
+ description: `base64`,
152
+ encode(value) {
153
+ try {
154
+ const bytes = atob(value).split("").map((c) => c.charCodeAt(0));
155
+ return new Uint8Array(bytes);
156
+ } catch (e2) {
157
+ throw new Error(`Expected a string of base 64, got [${value}].`);
158
+ }
159
+ },
160
+ fixedSize: null,
161
+ maxSize: null
162
+ };
163
+ }
164
+ };
165
+ var getBase64Decoder = () => {
166
+ {
167
+ return {
168
+ decode(bytes, offset = 0) {
169
+ const slice = bytes.slice(offset);
170
+ const value = btoa(String.fromCharCode(...slice));
171
+ return [value, bytes.length];
172
+ },
173
+ description: `base64`,
174
+ fixedSize: null,
175
+ maxSize: null
176
+ };
177
+ }
178
+ };
179
+ var getBase64Codec = () => codecsCore.combineCodec(getBase64Encoder(), getBase64Decoder());
180
+
181
+ // src/null-characters.ts
182
+ var removeNullCharacters = (value) => (
183
+ // eslint-disable-next-line no-control-regex
184
+ value.replace(/\u0000/g, "")
185
+ );
186
+ var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
187
+
188
+ // ../text-encoding-impl/dist/index.browser.js
189
+ var e = globalThis.TextDecoder;
190
+ var o = globalThis.TextEncoder;
191
+
192
+ // src/utf8.ts
193
+ var getUtf8Encoder = () => {
194
+ let textEncoder;
195
+ return {
196
+ description: "utf8",
197
+ encode: (value) => new Uint8Array((textEncoder || (textEncoder = new o())).encode(value)),
198
+ fixedSize: null,
199
+ maxSize: null
200
+ };
201
+ };
202
+ var getUtf8Decoder = () => {
203
+ let textDecoder;
204
+ return {
205
+ decode(bytes, offset = 0) {
206
+ const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
207
+ return [removeNullCharacters(value), bytes.length];
208
+ },
209
+ description: "utf8",
210
+ fixedSize: null,
211
+ maxSize: null
212
+ };
213
+ };
214
+ var getUtf8Codec = () => codecsCore.combineCodec(getUtf8Encoder(), getUtf8Decoder());
215
+
216
+ // src/string.ts
217
+ var getStringEncoder = (options = {}) => {
218
+ const size = options.size ?? codecsNumbers.getU32Encoder();
219
+ const encoding = options.encoding ?? getUtf8Encoder();
220
+ const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
221
+ if (size === "variable") {
222
+ return { ...encoding, description };
223
+ }
224
+ if (typeof size === "number") {
225
+ return codecsCore.fixEncoder(encoding, size, description);
226
+ }
227
+ return {
228
+ description,
229
+ encode: (value) => {
230
+ const contentBytes = encoding.encode(value);
231
+ const lengthBytes = size.encode(contentBytes.length);
232
+ return codecsCore.mergeBytes([lengthBytes, contentBytes]);
233
+ },
234
+ fixedSize: null,
235
+ maxSize: null
236
+ };
237
+ };
238
+ var getStringDecoder = (options = {}) => {
239
+ const size = options.size ?? codecsNumbers.getU32Decoder();
240
+ const encoding = options.encoding ?? getUtf8Decoder();
241
+ const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
242
+ if (size === "variable") {
243
+ return { ...encoding, description };
244
+ }
245
+ if (typeof size === "number") {
246
+ return codecsCore.fixDecoder(encoding, size, description);
247
+ }
248
+ return {
249
+ decode: (bytes, offset = 0) => {
250
+ codecsCore.assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
251
+ const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
252
+ const length = Number(lengthBigInt);
253
+ offset = lengthOffset;
254
+ const contentBytes = bytes.slice(offset, offset + length);
255
+ codecsCore.assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
256
+ const [value, contentOffset] = encoding.decode(contentBytes);
257
+ offset += contentOffset;
258
+ return [value, offset];
259
+ },
260
+ description,
261
+ fixedSize: null,
262
+ maxSize: null
263
+ };
264
+ };
265
+ var getStringCodec = (options = {}) => codecsCore.combineCodec(getStringEncoder(options), getStringDecoder(options));
266
+ function getSizeDescription(size) {
267
+ return typeof size === "object" ? size.description : `${size}`;
268
+ }
269
+
270
+ exports.assertValidBaseString = assertValidBaseString;
271
+ exports.getBase10Codec = getBase10Codec;
272
+ exports.getBase10Decoder = getBase10Decoder;
273
+ exports.getBase10Encoder = getBase10Encoder;
274
+ exports.getBase16Codec = getBase16Codec;
275
+ exports.getBase16Decoder = getBase16Decoder;
276
+ exports.getBase16Encoder = getBase16Encoder;
277
+ exports.getBase58Codec = getBase58Codec;
278
+ exports.getBase58Decoder = getBase58Decoder;
279
+ exports.getBase58Encoder = getBase58Encoder;
280
+ exports.getBase64Codec = getBase64Codec;
281
+ exports.getBase64Decoder = getBase64Decoder;
282
+ exports.getBase64Encoder = getBase64Encoder;
283
+ exports.getBaseXCodec = getBaseXCodec;
284
+ exports.getBaseXDecoder = getBaseXDecoder;
285
+ exports.getBaseXEncoder = getBaseXEncoder;
286
+ exports.getBaseXResliceCodec = getBaseXResliceCodec;
287
+ exports.getBaseXResliceDecoder = getBaseXResliceDecoder;
288
+ exports.getBaseXResliceEncoder = getBaseXResliceEncoder;
289
+ exports.getStringCodec = getStringCodec;
290
+ exports.getStringDecoder = getStringDecoder;
291
+ exports.getStringEncoder = getStringEncoder;
292
+ exports.getUtf8Codec = getUtf8Codec;
293
+ exports.getUtf8Decoder = getUtf8Decoder;
294
+ exports.getUtf8Encoder = getUtf8Encoder;
295
+ exports.padNullCharacters = padNullCharacters;
296
+ exports.removeNullCharacters = removeNullCharacters;
297
+ //# sourceMappingURL=out.js.map
298
+ //# sourceMappingURL=index.browser.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/assertions.ts","../src/baseX.ts","../src/base10.ts","../src/base16.ts","../src/base58.ts","../src/base64.ts","../src/baseX-reslice.ts","../src/null-characters.ts","../src/string.ts","../src/utf8.ts","../../text-encoding-impl/src/index.browser.ts"],"names":["alphabet","combineCodec","e","TextDecoder","TextEncoder"],"mappings":";AAGO,SAAS,sBAAsBA,WAAkB,WAAmB,aAAa,WAAW;AAC/F,MAAI,CAAC,UAAU,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A,IACjD;AAAA,IACA,WAAW;AAAA,IACX,SAAS;AAAA,EACb;AACJ;AAGO,IAAM,mBAAmB,CAAC,UAA8D,CAAC,MAAuB;AACnH,QAAM,OAAO,QAAQ,QAAQ,cAAc;AAC3C,QAAM,WAAW,QAAQ,YAAY,eAAe;AACpD,QAAM,cAAc,QAAQ,eAAe,UAAU,SAAS,WAAW,KAAK,mBAAmB,IAAI,CAAC;AAEtG,MAAI,SAAS,YAAY;AACrB,WAAO,EAAE,GAAG,UAAU,YAAY;AAAA,EACtC;AAEA,MAAI,OAAO,SAAS,UAAU;AAC1B,WAAO,WAAW,UAAU,MAAM,WAAW;AAAA,EACjD;AAEA,SAAO;AAAA,IACH,QAAQ,CAAC,OAAmB,SAAS,MAAM;AACvC,wCAAkC,UAAU,OAAO,MAAM;AACzD,YAAM,CAAC,cAAc,YAAY,IAAI,KAAK,OAAO,OAAO,MAAM;AAC9D,YAAM,SAAS,OAAO,YAAY;AAClC,eAAS;AACT,YAAM,eAAe,MAAM,MAAM,QAAQ,SAAS,MAAM;AACxD,4CAAsC,UAAU,QAAQ,YAAY;AACpE,YAAM,CAAC,OAAO,aAAa,IAAI,SAAS,OAAO,YAAY;AAC3D,gBAAU;AACV,aAAO,CAAC,OAAO,MAAM;AAAA,IACzB;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,SAAS;AAAA,EACb;AACJ;AAGO,IAAM,iBAAiB,CAAC,UAA0D,CAAC,MACtFA,cAAa,iBAAiB,OAAO,GAAG,iBAAiB,OAAO,CAAC;AAErE,SAAS,mBAAmB,MAA+C;AACvE,SAAO,OAAO,SAAS,WAAW,KAAK,cAAc,GAAG,IAAI;AAChE","sourcesContent":["/**\n * Asserts that a given string matches a given alphabet.\n */\nexport function assertValidBaseString(alphabet: string, testValue: string, givenValue = testValue) {\n if (!testValue.match(new RegExp(`^[${alphabet}]*$`))) {\n // TODO: Coded error.\n throw new Error(`Expected a string of base ${alphabet.length}, got [${givenValue}].`);\n }\n}\n","import { Codec, combineCodec, Decoder, Encoder } from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/**\n * Encodes a string using a custom alphabet by dividing\n * by the base and handling leading zeroes.\n * @see {@link getBaseXCodec} for a more detailed description.\n */\nexport const getBaseXEncoder = (alphabet: string): Encoder<string> => {\n const base = alphabet.length;\n const baseBigInt = BigInt(base);\n return {\n description: `base${base}`,\n encode(value: string): Uint8Array {\n // Check if the value is valid.\n assertValidBaseString(alphabet, value);\n if (value === '') return new Uint8Array();\n\n // Handle leading zeroes.\n const chars = [...value];\n let trailIndex = chars.findIndex(c => c !== alphabet[0]);\n trailIndex = trailIndex === -1 ? chars.length : trailIndex;\n const leadingZeroes = Array(trailIndex).fill(0);\n if (trailIndex === chars.length) return Uint8Array.from(leadingZeroes);\n\n // From baseX to base10.\n const tailChars = chars.slice(trailIndex);\n let base10Number = 0n;\n let baseXPower = 1n;\n for (let i = tailChars.length - 1; i >= 0; i -= 1) {\n base10Number += baseXPower * BigInt(alphabet.indexOf(tailChars[i]));\n baseXPower *= baseBigInt;\n }\n\n // From base10 to bytes.\n const tailBytes = [];\n while (base10Number > 0n) {\n tailBytes.unshift(Number(base10Number % 256n));\n base10Number /= 256n;\n }\n return Uint8Array.from(leadingZeroes.concat(tailBytes));\n },\n fixedSize: null,\n maxSize: null,\n };\n};\n\n/**\n * Decodes a string using a custom alphabet by dividing\n * by the base and handling leading zeroes.\n * @see {@link getBaseXCodec} for a more detailed description.\n */\nexport const getBaseXDecoder = (alphabet: string): Decoder<string> => {\n const base = alphabet.length;\n const baseBigInt = BigInt(base);\n return {\n decode(rawBytes, offset = 0): [string, number] {\n const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);\n if (bytes.length === 0) return ['', 0];\n\n // Handle leading zeroes.\n let trailIndex = bytes.findIndex(n => n !== 0);\n trailIndex = trailIndex === -1 ? bytes.length : trailIndex;\n const leadingZeroes = alphabet[0].repeat(trailIndex);\n if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];\n\n // From bytes to base10.\n let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);\n\n // From base10 to baseX.\n const tailChars = [];\n while (base10Number > 0n) {\n tailChars.unshift(alphabet[Number(base10Number % baseBigInt)]);\n base10Number /= baseBigInt;\n }\n\n return [leadingZeroes + tailChars.join(''), rawBytes.length];\n },\n description: `base${base}`,\n fixedSize: null,\n maxSize: null,\n };\n};\n\n/**\n * A string codec that requires a custom alphabet and uses\n * the length of that alphabet as the base. It then divides\n * the input by the base as many times as necessary to get\n * the output. It also supports leading zeroes by using the\n * first character of the alphabet as the zero character.\n *\n * This can be used to create codecs such as base10 or base58.\n */\nexport const getBaseXCodec = (alphabet: string): Codec<string> =>\n combineCodec(getBaseXEncoder(alphabet), getBaseXDecoder(alphabet));\n","import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';\n\nconst alphabet = '0123456789';\n\n/** Encodes strings in base10. */\nexport const getBase10Encoder = () => getBaseXEncoder(alphabet);\n\n/** Decodes strings in base10. */\nexport const getBase10Decoder = () => getBaseXDecoder(alphabet);\n\n/** Encodes and decodes strings in base10. */\nexport const getBase10Codec = () => getBaseXCodec(alphabet);\n","import { Codec, combineCodec, Decoder, Encoder } from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/** Encodes strings in base16. */\nexport const getBase16Encoder = (): Encoder<string> => ({\n description: 'base16',\n encode(value: string) {\n const lowercaseValue = value.toLowerCase();\n assertValidBaseString('0123456789abcdef', lowercaseValue, value);\n const matches = lowercaseValue.match(/.{1,2}/g);\n return Uint8Array.from(matches ? matches.map((byte: string) => parseInt(byte, 16)) : []);\n },\n fixedSize: null,\n maxSize: null,\n});\n\n/** Decodes strings in base16. */\nexport const getBase16Decoder = (): Decoder<string> => ({\n decode(bytes, offset = 0) {\n const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');\n return [value, bytes.length];\n },\n description: 'base16',\n fixedSize: null,\n maxSize: null,\n});\n\n/** Encodes and decodes strings in base16. */\nexport const getBase16Codec = (): Codec<string> => combineCodec(getBase16Encoder(), getBase16Decoder());\n","import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';\n\nconst alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';\n\n/** Encodes strings in base58. */\nexport const getBase58Encoder = () => getBaseXEncoder(alphabet);\n\n/** Decodes strings in base58. */\nexport const getBase58Decoder = () => getBaseXDecoder(alphabet);\n\n/** Encodes and decodes strings in base58. */\nexport const getBase58Codec = () => getBaseXCodec(alphabet);\n","import { combineCodec, Decoder, Encoder, mapDecoder, mapEncoder } from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\nimport { getBaseXResliceDecoder, getBaseXResliceEncoder } from './baseX-reslice';\n\nconst alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';\n\n/** Encodes strings in base64. */\nexport const getBase64Encoder = (): Encoder<string> => {\n if (__BROWSER__) {\n return {\n description: `base64`,\n encode(value: string): Uint8Array {\n try {\n const bytes = (atob as Window['atob'])(value)\n .split('')\n .map(c => c.charCodeAt(0));\n return new Uint8Array(bytes);\n } catch (e) {\n // TODO: Coded error.\n throw new Error(`Expected a string of base 64, got [${value}].`);\n }\n },\n fixedSize: null,\n maxSize: null,\n };\n }\n\n if (__NODEJS__) {\n return {\n description: `base64`,\n encode(value: string): Uint8Array {\n assertValidBaseString(alphabet, value.replace(/=/g, ''));\n return new Uint8Array(Buffer.from(value, 'base64'));\n },\n fixedSize: null,\n maxSize: null,\n };\n }\n\n return mapEncoder(getBaseXResliceEncoder(alphabet, 6), (value: string): string => value.replace(/=/g, ''));\n};\n\n/** Decodes strings in base64. */\nexport const getBase64Decoder = (): Decoder<string> => {\n if (__BROWSER__) {\n return {\n decode(bytes, offset = 0) {\n const slice = bytes.slice(offset);\n const value = (btoa as Window['btoa'])(String.fromCharCode(...slice));\n return [value, bytes.length];\n },\n description: `base64`,\n fixedSize: null,\n maxSize: null,\n };\n }\n\n if (__NODEJS__) {\n return {\n decode: (bytes, offset = 0) => [Buffer.from(bytes, offset).toString('base64'), bytes.length],\n description: `base64`,\n fixedSize: null,\n maxSize: null,\n };\n }\n\n return mapDecoder(getBaseXResliceDecoder(alphabet, 6), (value: string): string =>\n value.padEnd(Math.ceil(value.length / 4) * 4, '=')\n );\n};\n\n/** Encodes and decodes strings in base64. */\nexport const getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());\n","import { Codec, combineCodec, Decoder, Encoder } from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/**\n * Encodes a string using a custom alphabet by reslicing the bits of the byte array.\n * @see {@link getBaseXResliceCodec} for a more detailed description.\n */\nexport const getBaseXResliceEncoder = (alphabet: string, bits: number): Encoder<string> => ({\n description: `base${alphabet.length}`,\n encode(value: string): Uint8Array {\n assertValidBaseString(alphabet, value);\n if (value === '') return new Uint8Array();\n const charIndices = [...value].map(c => alphabet.indexOf(c));\n return new Uint8Array(reslice(charIndices, bits, 8, false));\n },\n fixedSize: null,\n maxSize: null,\n});\n\n/**\n * Decodes a string using a custom alphabet by reslicing the bits of the byte array.\n * @see {@link getBaseXResliceCodec} for a more detailed description.\n */\nexport const getBaseXResliceDecoder = (alphabet: string, bits: number): Decoder<string> => ({\n decode(rawBytes, offset = 0): [string, number] {\n const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);\n if (bytes.length === 0) return ['', rawBytes.length];\n const charIndices = reslice([...bytes], 8, bits, true);\n return [charIndices.map(i => alphabet[i]).join(''), rawBytes.length];\n },\n description: `base${alphabet.length}`,\n fixedSize: null,\n maxSize: null,\n});\n\n/**\n * A string serializer that reslices bytes into custom chunks\n * of bits that are then mapped to a custom alphabet.\n *\n * This can be used to create serializers whose alphabet\n * is a power of 2 such as base16 or base64.\n */\nexport const getBaseXResliceCodec = (alphabet: string, bits: number): Codec<string> =>\n combineCodec(getBaseXResliceEncoder(alphabet, bits), getBaseXResliceDecoder(alphabet, bits));\n\n/** Helper function to reslice the bits inside bytes. */\nfunction reslice(input: number[], inputBits: number, outputBits: number, useRemainder: boolean): number[] {\n const output = [];\n let accumulator = 0;\n let bitsInAccumulator = 0;\n const mask = (1 << outputBits) - 1;\n for (const value of input) {\n accumulator = (accumulator << inputBits) | value;\n bitsInAccumulator += inputBits;\n while (bitsInAccumulator >= outputBits) {\n bitsInAccumulator -= outputBits;\n output.push((accumulator >> bitsInAccumulator) & mask);\n }\n }\n if (useRemainder && bitsInAccumulator > 0) {\n output.push((accumulator << (outputBits - bitsInAccumulator)) & mask);\n }\n return output;\n}\n","/**Removes null characters from a string. */\nexport const removeNullCharacters = (value: string) =>\n // eslint-disable-next-line no-control-regex\n value.replace(/\\u0000/g, '');\n\n/** Pads a string with null characters at the end. */\nexport const padNullCharacters = (value: string, chars: number) => value.padEnd(chars, '\\u0000');\n","import {\n assertByteArrayHasEnoughBytesForCodec,\n assertByteArrayIsNotEmptyForCodec,\n BaseCodecOptions,\n Codec,\n CodecData,\n combineCodec,\n Decoder,\n Encoder,\n fixDecoder,\n fixEncoder,\n mergeBytes,\n} from '@solana/codecs-core';\nimport { getU32Decoder, getU32Encoder, NumberCodec, NumberDecoder, NumberEncoder } from '@solana/codecs-numbers';\n\nimport { getUtf8Decoder, getUtf8Encoder } from './utf8';\n\n/** Defines the options for string codecs. */\nexport type StringCodecOptions<\n TPrefix extends NumberCodec | NumberEncoder | NumberDecoder,\n TEncoding extends Codec<string> | Encoder<string> | Decoder<string>\n> = BaseCodecOptions & {\n /**\n * The size of the string. It can be one of the following:\n * - a {@link NumberCodec} that prefixes the string with its size.\n * - a fixed number of bytes.\n * - or `'variable'` to use the rest of the byte array.\n * @defaultValue u32 prefix.\n */\n size?: TPrefix | number | 'variable';\n\n /**\n * The codec to use for encoding and decoding the content.\n * @defaultValue UTF-8 encoding.\n */\n encoding?: TEncoding;\n};\n\n/** Encodes strings from a given encoding and size strategy. */\nexport const getStringEncoder = (options: StringCodecOptions<NumberEncoder, Encoder<string>> = {}): Encoder<string> => {\n const size = options.size ?? getU32Encoder();\n const encoding = options.encoding ?? getUtf8Encoder();\n const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;\n\n if (size === 'variable') {\n return { ...encoding, description };\n }\n\n if (typeof size === 'number') {\n return fixEncoder(encoding, size, description);\n }\n\n return {\n description,\n encode: (value: string) => {\n const contentBytes = encoding.encode(value);\n const lengthBytes = size.encode(contentBytes.length);\n return mergeBytes([lengthBytes, contentBytes]);\n },\n fixedSize: null,\n maxSize: null,\n };\n};\n\n/** Decodes strings from a given encoding and size strategy. */\nexport const getStringDecoder = (options: StringCodecOptions<NumberDecoder, Decoder<string>> = {}): Decoder<string> => {\n const size = options.size ?? getU32Decoder();\n const encoding = options.encoding ?? getUtf8Decoder();\n const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;\n\n if (size === 'variable') {\n return { ...encoding, description };\n }\n\n if (typeof size === 'number') {\n return fixDecoder(encoding, size, description);\n }\n\n return {\n decode: (bytes: Uint8Array, offset = 0) => {\n assertByteArrayIsNotEmptyForCodec('string', bytes, offset);\n const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);\n const length = Number(lengthBigInt);\n offset = lengthOffset;\n const contentBytes = bytes.slice(offset, offset + length);\n assertByteArrayHasEnoughBytesForCodec('string', length, contentBytes);\n const [value, contentOffset] = encoding.decode(contentBytes);\n offset += contentOffset;\n return [value, offset];\n },\n description,\n fixedSize: null,\n maxSize: null,\n };\n};\n\n/** Encodes and decodes strings from a given encoding and size strategy. */\nexport const getStringCodec = (options: StringCodecOptions<NumberCodec, Codec<string>> = {}): Codec<string> =>\n combineCodec(getStringEncoder(options), getStringDecoder(options));\n\nfunction getSizeDescription(size: CodecData | number | 'variable'): string {\n return typeof size === 'object' ? size.description : `${size}`;\n}\n","import { Codec, combineCodec, Decoder, Encoder } from '@solana/codecs-core';\nimport { TextDecoder, TextEncoder } from 'text-encoding-impl';\n\nimport { removeNullCharacters } from './null-characters';\n\n/** Encodes UTF-8 strings using the native `TextEncoder` API. */\nexport const getUtf8Encoder = (): Encoder<string> => {\n let textEncoder: TextEncoder;\n return {\n description: 'utf8',\n encode: (value: string) => new Uint8Array((textEncoder ||= new TextEncoder()).encode(value)),\n fixedSize: null,\n maxSize: null,\n };\n};\n\n/** Decodes UTF-8 strings using the native `TextDecoder` API. */\nexport const getUtf8Decoder = (): Decoder<string> => {\n let textDecoder: TextDecoder;\n return {\n decode(bytes, offset = 0) {\n const value = (textDecoder ||= new TextDecoder()).decode(bytes.slice(offset));\n return [removeNullCharacters(value), bytes.length];\n },\n description: 'utf8',\n fixedSize: null,\n maxSize: null,\n };\n};\n\n/** Encodes and decodes UTF-8 strings using the native `TextEncoder` and `TextDecoder` API. */\nexport const getUtf8Codec = (): Codec<string> => combineCodec(getUtf8Encoder(), getUtf8Decoder());\n","export const TextDecoder = globalThis.TextDecoder;\nexport const TextEncoder = globalThis.TextEncoder;\n"]}
@@ -0,0 +1,270 @@
1
+ import { combineCodec, fixEncoder, mergeBytes, fixDecoder, assertByteArrayIsNotEmptyForCodec, assertByteArrayHasEnoughBytesForCodec } from '@solana/codecs-core';
2
+ import { getU32Encoder, getU32Decoder } from '@solana/codecs-numbers';
3
+
4
+ // src/assertions.ts
5
+ function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
6
+ if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
7
+ throw new Error(`Expected a string of base ${alphabet4.length}, got [${givenValue}].`);
8
+ }
9
+ }
10
+ var getBaseXEncoder = (alphabet4) => {
11
+ const base = alphabet4.length;
12
+ const baseBigInt = BigInt(base);
13
+ return {
14
+ description: `base${base}`,
15
+ encode(value) {
16
+ assertValidBaseString(alphabet4, value);
17
+ if (value === "")
18
+ return new Uint8Array();
19
+ const chars = [...value];
20
+ let trailIndex = chars.findIndex((c) => c !== alphabet4[0]);
21
+ trailIndex = trailIndex === -1 ? chars.length : trailIndex;
22
+ const leadingZeroes = Array(trailIndex).fill(0);
23
+ if (trailIndex === chars.length)
24
+ return Uint8Array.from(leadingZeroes);
25
+ const tailChars = chars.slice(trailIndex);
26
+ let base10Number = 0n;
27
+ let baseXPower = 1n;
28
+ for (let i = tailChars.length - 1; i >= 0; i -= 1) {
29
+ base10Number += baseXPower * BigInt(alphabet4.indexOf(tailChars[i]));
30
+ baseXPower *= baseBigInt;
31
+ }
32
+ const tailBytes = [];
33
+ while (base10Number > 0n) {
34
+ tailBytes.unshift(Number(base10Number % 256n));
35
+ base10Number /= 256n;
36
+ }
37
+ return Uint8Array.from(leadingZeroes.concat(tailBytes));
38
+ },
39
+ fixedSize: null,
40
+ maxSize: null
41
+ };
42
+ };
43
+ var getBaseXDecoder = (alphabet4) => {
44
+ const base = alphabet4.length;
45
+ const baseBigInt = BigInt(base);
46
+ return {
47
+ decode(rawBytes, offset = 0) {
48
+ const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
49
+ if (bytes.length === 0)
50
+ return ["", 0];
51
+ let trailIndex = bytes.findIndex((n) => n !== 0);
52
+ trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
53
+ const leadingZeroes = alphabet4[0].repeat(trailIndex);
54
+ if (trailIndex === bytes.length)
55
+ return [leadingZeroes, rawBytes.length];
56
+ let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
57
+ const tailChars = [];
58
+ while (base10Number > 0n) {
59
+ tailChars.unshift(alphabet4[Number(base10Number % baseBigInt)]);
60
+ base10Number /= baseBigInt;
61
+ }
62
+ return [leadingZeroes + tailChars.join(""), rawBytes.length];
63
+ },
64
+ description: `base${base}`,
65
+ fixedSize: null,
66
+ maxSize: null
67
+ };
68
+ };
69
+ var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
70
+
71
+ // src/base10.ts
72
+ var alphabet = "0123456789";
73
+ var getBase10Encoder = () => getBaseXEncoder(alphabet);
74
+ var getBase10Decoder = () => getBaseXDecoder(alphabet);
75
+ var getBase10Codec = () => getBaseXCodec(alphabet);
76
+ var getBase16Encoder = () => ({
77
+ description: "base16",
78
+ encode(value) {
79
+ const lowercaseValue = value.toLowerCase();
80
+ assertValidBaseString("0123456789abcdef", lowercaseValue, value);
81
+ const matches = lowercaseValue.match(/.{1,2}/g);
82
+ return Uint8Array.from(matches ? matches.map((byte) => parseInt(byte, 16)) : []);
83
+ },
84
+ fixedSize: null,
85
+ maxSize: null
86
+ });
87
+ var getBase16Decoder = () => ({
88
+ decode(bytes, offset = 0) {
89
+ const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
90
+ return [value, bytes.length];
91
+ },
92
+ description: "base16",
93
+ fixedSize: null,
94
+ maxSize: null
95
+ });
96
+ var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
97
+
98
+ // src/base58.ts
99
+ var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
100
+ var getBase58Encoder = () => getBaseXEncoder(alphabet2);
101
+ var getBase58Decoder = () => getBaseXDecoder(alphabet2);
102
+ var getBase58Codec = () => getBaseXCodec(alphabet2);
103
+ var getBaseXResliceEncoder = (alphabet4, bits) => ({
104
+ description: `base${alphabet4.length}`,
105
+ encode(value) {
106
+ assertValidBaseString(alphabet4, value);
107
+ if (value === "")
108
+ return new Uint8Array();
109
+ const charIndices = [...value].map((c) => alphabet4.indexOf(c));
110
+ return new Uint8Array(reslice(charIndices, bits, 8, false));
111
+ },
112
+ fixedSize: null,
113
+ maxSize: null
114
+ });
115
+ var getBaseXResliceDecoder = (alphabet4, bits) => ({
116
+ decode(rawBytes, offset = 0) {
117
+ const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
118
+ if (bytes.length === 0)
119
+ return ["", rawBytes.length];
120
+ const charIndices = reslice([...bytes], 8, bits, true);
121
+ return [charIndices.map((i) => alphabet4[i]).join(""), rawBytes.length];
122
+ },
123
+ description: `base${alphabet4.length}`,
124
+ fixedSize: null,
125
+ maxSize: null
126
+ });
127
+ var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
128
+ function reslice(input, inputBits, outputBits, useRemainder) {
129
+ const output = [];
130
+ let accumulator = 0;
131
+ let bitsInAccumulator = 0;
132
+ const mask = (1 << outputBits) - 1;
133
+ for (const value of input) {
134
+ accumulator = accumulator << inputBits | value;
135
+ bitsInAccumulator += inputBits;
136
+ while (bitsInAccumulator >= outputBits) {
137
+ bitsInAccumulator -= outputBits;
138
+ output.push(accumulator >> bitsInAccumulator & mask);
139
+ }
140
+ }
141
+ if (useRemainder && bitsInAccumulator > 0) {
142
+ output.push(accumulator << outputBits - bitsInAccumulator & mask);
143
+ }
144
+ return output;
145
+ }
146
+ var getBase64Encoder = () => {
147
+ {
148
+ return {
149
+ description: `base64`,
150
+ encode(value) {
151
+ try {
152
+ const bytes = atob(value).split("").map((c) => c.charCodeAt(0));
153
+ return new Uint8Array(bytes);
154
+ } catch (e2) {
155
+ throw new Error(`Expected a string of base 64, got [${value}].`);
156
+ }
157
+ },
158
+ fixedSize: null,
159
+ maxSize: null
160
+ };
161
+ }
162
+ };
163
+ var getBase64Decoder = () => {
164
+ {
165
+ return {
166
+ decode(bytes, offset = 0) {
167
+ const slice = bytes.slice(offset);
168
+ const value = btoa(String.fromCharCode(...slice));
169
+ return [value, bytes.length];
170
+ },
171
+ description: `base64`,
172
+ fixedSize: null,
173
+ maxSize: null
174
+ };
175
+ }
176
+ };
177
+ var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
178
+
179
+ // src/null-characters.ts
180
+ var removeNullCharacters = (value) => (
181
+ // eslint-disable-next-line no-control-regex
182
+ value.replace(/\u0000/g, "")
183
+ );
184
+ var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
185
+
186
+ // ../text-encoding-impl/dist/index.browser.js
187
+ var e = globalThis.TextDecoder;
188
+ var o = globalThis.TextEncoder;
189
+
190
+ // src/utf8.ts
191
+ var getUtf8Encoder = () => {
192
+ let textEncoder;
193
+ return {
194
+ description: "utf8",
195
+ encode: (value) => new Uint8Array((textEncoder || (textEncoder = new o())).encode(value)),
196
+ fixedSize: null,
197
+ maxSize: null
198
+ };
199
+ };
200
+ var getUtf8Decoder = () => {
201
+ let textDecoder;
202
+ return {
203
+ decode(bytes, offset = 0) {
204
+ const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
205
+ return [removeNullCharacters(value), bytes.length];
206
+ },
207
+ description: "utf8",
208
+ fixedSize: null,
209
+ maxSize: null
210
+ };
211
+ };
212
+ var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
213
+
214
+ // src/string.ts
215
+ var getStringEncoder = (options = {}) => {
216
+ const size = options.size ?? getU32Encoder();
217
+ const encoding = options.encoding ?? getUtf8Encoder();
218
+ const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
219
+ if (size === "variable") {
220
+ return { ...encoding, description };
221
+ }
222
+ if (typeof size === "number") {
223
+ return fixEncoder(encoding, size, description);
224
+ }
225
+ return {
226
+ description,
227
+ encode: (value) => {
228
+ const contentBytes = encoding.encode(value);
229
+ const lengthBytes = size.encode(contentBytes.length);
230
+ return mergeBytes([lengthBytes, contentBytes]);
231
+ },
232
+ fixedSize: null,
233
+ maxSize: null
234
+ };
235
+ };
236
+ var getStringDecoder = (options = {}) => {
237
+ const size = options.size ?? getU32Decoder();
238
+ const encoding = options.encoding ?? getUtf8Decoder();
239
+ const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
240
+ if (size === "variable") {
241
+ return { ...encoding, description };
242
+ }
243
+ if (typeof size === "number") {
244
+ return fixDecoder(encoding, size, description);
245
+ }
246
+ return {
247
+ decode: (bytes, offset = 0) => {
248
+ assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
249
+ const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
250
+ const length = Number(lengthBigInt);
251
+ offset = lengthOffset;
252
+ const contentBytes = bytes.slice(offset, offset + length);
253
+ assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
254
+ const [value, contentOffset] = encoding.decode(contentBytes);
255
+ offset += contentOffset;
256
+ return [value, offset];
257
+ },
258
+ description,
259
+ fixedSize: null,
260
+ maxSize: null
261
+ };
262
+ };
263
+ var getStringCodec = (options = {}) => combineCodec(getStringEncoder(options), getStringDecoder(options));
264
+ function getSizeDescription(size) {
265
+ return typeof size === "object" ? size.description : `${size}`;
266
+ }
267
+
268
+ export { assertValidBaseString, getBase10Codec, getBase10Decoder, getBase10Encoder, getBase16Codec, getBase16Decoder, getBase16Encoder, getBase58Codec, getBase58Decoder, getBase58Encoder, getBase64Codec, getBase64Decoder, getBase64Encoder, getBaseXCodec, getBaseXDecoder, getBaseXEncoder, getBaseXResliceCodec, getBaseXResliceDecoder, getBaseXResliceEncoder, getStringCodec, getStringDecoder, getStringEncoder, getUtf8Codec, getUtf8Decoder, getUtf8Encoder, padNullCharacters, removeNullCharacters };
269
+ //# sourceMappingURL=out.js.map
270
+ //# sourceMappingURL=index.browser.js.map