@solana/codecs-strings 2.0.0-experimental.fc4e943 → 2.0.0-experimental.fcff844

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.
@@ -1,4 +1,4 @@
1
- import { combineCodec, fixEncoder, mergeBytes, fixDecoder, assertByteArrayIsNotEmptyForCodec, assertByteArrayHasEnoughBytesForCodec } from '@solana/codecs-core';
1
+ import { createEncoder, createDecoder, combineCodec, fixEncoder, getEncodedSize, fixDecoder, assertByteArrayIsNotEmptyForCodec, assertByteArrayHasEnoughBytesForCodec } from '@solana/codecs-core';
2
2
  import { getU32Encoder, getU32Decoder } from '@solana/codecs-numbers';
3
3
 
4
4
  // src/assertions.ts
@@ -8,43 +8,38 @@ function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
8
8
  }
9
9
  }
10
10
  var getBaseXEncoder = (alphabet4) => {
11
- const base = alphabet4.length;
12
- const baseBigInt = BigInt(base);
13
- return {
14
- description: `base${base}`,
15
- encode(value) {
11
+ return createEncoder({
12
+ getSizeFromValue: (value) => {
13
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
14
+ if (tailChars === "")
15
+ return value.length;
16
+ const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
17
+ return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
18
+ },
19
+ write(value, bytes, offset) {
16
20
  assertValidBaseString(alphabet4, value);
17
21
  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;
22
+ return offset;
23
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
24
+ if (tailChars === "") {
25
+ bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
26
+ return offset + leadingZeroes.length;
31
27
  }
28
+ let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
32
29
  const tailBytes = [];
33
30
  while (base10Number > 0n) {
34
31
  tailBytes.unshift(Number(base10Number % 256n));
35
32
  base10Number /= 256n;
36
33
  }
37
- return Uint8Array.from(leadingZeroes.concat(tailBytes));
38
- },
39
- fixedSize: null,
40
- maxSize: null
41
- };
34
+ const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
35
+ bytes.set(bytesToAdd, offset);
36
+ return offset + bytesToAdd.length;
37
+ }
38
+ });
42
39
  };
43
40
  var getBaseXDecoder = (alphabet4) => {
44
- const base = alphabet4.length;
45
- const baseBigInt = BigInt(base);
46
- return {
47
- decode(rawBytes, offset = 0) {
41
+ return createDecoder({
42
+ read(rawBytes, offset) {
48
43
  const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
49
44
  if (bytes.length === 0)
50
45
  return ["", 0];
@@ -53,45 +48,52 @@ var getBaseXDecoder = (alphabet4) => {
53
48
  const leadingZeroes = alphabet4[0].repeat(trailIndex);
54
49
  if (trailIndex === bytes.length)
55
50
  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
- };
51
+ const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
52
+ const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
53
+ return [leadingZeroes + tailChars, rawBytes.length];
54
+ }
55
+ });
68
56
  };
69
57
  var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
58
+ function partitionLeadingZeroes(value, zeroCharacter) {
59
+ const leadingZeroIndex = [...value].findIndex((c) => c !== zeroCharacter);
60
+ return leadingZeroIndex === -1 ? [value, ""] : [value.slice(0, leadingZeroIndex), value.slice(leadingZeroIndex)];
61
+ }
62
+ function getBigIntFromBaseX(value, alphabet4) {
63
+ const base = BigInt(alphabet4.length);
64
+ return [...value].reduce((sum, char) => sum * base + BigInt(alphabet4.indexOf(char)), 0n);
65
+ }
66
+ function getBaseXFromBigInt(value, alphabet4) {
67
+ const base = BigInt(alphabet4.length);
68
+ const tailChars = [];
69
+ while (value > 0n) {
70
+ tailChars.unshift(alphabet4[Number(value % base)]);
71
+ value /= base;
72
+ }
73
+ return tailChars.join("");
74
+ }
70
75
 
71
76
  // src/base10.ts
72
77
  var alphabet = "0123456789";
73
78
  var getBase10Encoder = () => getBaseXEncoder(alphabet);
74
79
  var getBase10Decoder = () => getBaseXDecoder(alphabet);
75
80
  var getBase10Codec = () => getBaseXCodec(alphabet);
76
- var getBase16Encoder = () => ({
77
- description: "base16",
78
- encode(value) {
81
+ var getBase16Encoder = () => createEncoder({
82
+ getSizeFromValue: (value) => Math.ceil(value.length / 2),
83
+ write(value, bytes, offset) {
79
84
  const lowercaseValue = value.toLowerCase();
80
85
  assertValidBaseString("0123456789abcdef", lowercaseValue, value);
81
86
  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
87
+ const hexBytes = matches ? matches.map((byte) => parseInt(byte, 16)) : [];
88
+ bytes.set(hexBytes, offset);
89
+ return hexBytes.length + offset;
90
+ }
86
91
  });
87
- var getBase16Decoder = () => ({
88
- decode(bytes, offset = 0) {
92
+ var getBase16Decoder = () => createDecoder({
93
+ read(bytes, offset) {
89
94
  const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
90
95
  return [value, bytes.length];
91
- },
92
- description: "base16",
93
- fixedSize: null,
94
- maxSize: null
96
+ }
95
97
  });
96
98
  var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
97
99
 
@@ -100,29 +102,26 @@ var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
100
102
  var getBase58Encoder = () => getBaseXEncoder(alphabet2);
101
103
  var getBase58Decoder = () => getBaseXDecoder(alphabet2);
102
104
  var getBase58Codec = () => getBaseXCodec(alphabet2);
103
- var getBaseXResliceEncoder = (alphabet4, bits) => ({
104
- description: `base${alphabet4.length}`,
105
- encode(value) {
105
+ var getBaseXResliceEncoder = (alphabet4, bits) => createEncoder({
106
+ getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
107
+ write(value, bytes, offset) {
106
108
  assertValidBaseString(alphabet4, value);
107
109
  if (value === "")
108
- return new Uint8Array();
110
+ return offset;
109
111
  const charIndices = [...value].map((c) => alphabet4.indexOf(c));
110
- return new Uint8Array(reslice(charIndices, bits, 8, false));
111
- },
112
- fixedSize: null,
113
- maxSize: null
112
+ const reslicedBytes = reslice(charIndices, bits, 8, false);
113
+ bytes.set(reslicedBytes, offset);
114
+ return reslicedBytes.length + offset;
115
+ }
114
116
  });
115
- var getBaseXResliceDecoder = (alphabet4, bits) => ({
116
- decode(rawBytes, offset = 0) {
117
+ var getBaseXResliceDecoder = (alphabet4, bits) => createDecoder({
118
+ read(rawBytes, offset = 0) {
117
119
  const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
118
120
  if (bytes.length === 0)
119
121
  return ["", rawBytes.length];
120
122
  const charIndices = reslice([...bytes], 8, bits, true);
121
123
  return [charIndices.map((i) => alphabet4[i]).join(""), rawBytes.length];
122
- },
123
- description: `base${alphabet4.length}`,
124
- fixedSize: null,
125
- maxSize: null
124
+ }
126
125
  });
127
126
  var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
128
127
  function reslice(input, inputBits, outputBits, useRemainder) {
@@ -148,25 +147,22 @@ function reslice(input, inputBits, outputBits, useRemainder) {
148
147
  var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
149
148
  var getBase64Encoder = () => {
150
149
  {
151
- return {
152
- description: `base64`,
153
- encode(value) {
150
+ return createEncoder({
151
+ getSizeFromValue: (value) => Buffer.from(value, "base64").length,
152
+ write(value, bytes, offset) {
154
153
  assertValidBaseString(alphabet3, value.replace(/=/g, ""));
155
- return new Uint8Array(Buffer.from(value, "base64"));
156
- },
157
- fixedSize: null,
158
- maxSize: null
159
- };
154
+ const buffer = Buffer.from(value, "base64");
155
+ bytes.set(buffer, offset);
156
+ return buffer.length + offset;
157
+ }
158
+ });
160
159
  }
161
160
  };
162
161
  var getBase64Decoder = () => {
163
162
  {
164
- return {
165
- decode: (bytes, offset = 0) => [Buffer.from(bytes, offset).toString("base64"), bytes.length],
166
- description: `base64`,
167
- fixedSize: null,
168
- maxSize: null
169
- };
163
+ return createDecoder({
164
+ read: (bytes, offset = 0) => [Buffer.from(bytes, offset).toString("base64"), bytes.length]
165
+ });
170
166
  }
171
167
  };
172
168
  var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
@@ -185,79 +181,73 @@ var o = globalThis.TextEncoder;
185
181
  // src/utf8.ts
186
182
  var getUtf8Encoder = () => {
187
183
  let textEncoder;
188
- return {
189
- description: "utf8",
190
- encode: (value) => new Uint8Array((textEncoder || (textEncoder = new o())).encode(value)),
191
- fixedSize: null,
192
- maxSize: null
193
- };
184
+ return createEncoder({
185
+ getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
186
+ write: (value, bytes, offset) => {
187
+ const bytesToAdd = (textEncoder ||= new o()).encode(value);
188
+ bytes.set(bytesToAdd, offset);
189
+ return offset + bytesToAdd.length;
190
+ }
191
+ });
194
192
  };
195
193
  var getUtf8Decoder = () => {
196
194
  let textDecoder;
197
- return {
198
- decode(bytes, offset = 0) {
199
- const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
195
+ return createDecoder({
196
+ read(bytes, offset) {
197
+ const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
200
198
  return [removeNullCharacters(value), bytes.length];
201
- },
202
- description: "utf8",
203
- fixedSize: null,
204
- maxSize: null
205
- };
199
+ }
200
+ });
206
201
  };
207
202
  var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
208
203
 
209
204
  // src/string.ts
210
- var getStringEncoder = (options = {}) => {
211
- const size = options.size ?? getU32Encoder();
212
- const encoding = options.encoding ?? getUtf8Encoder();
213
- const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
205
+ function getStringEncoder(config = {}) {
206
+ const size = config.size ?? getU32Encoder();
207
+ const encoding = config.encoding ?? getUtf8Encoder();
214
208
  if (size === "variable") {
215
- return { ...encoding, description };
209
+ return encoding;
216
210
  }
217
211
  if (typeof size === "number") {
218
- return fixEncoder(encoding, size, description);
212
+ return fixEncoder(encoding, size);
219
213
  }
220
- return {
221
- description,
222
- encode: (value) => {
223
- const contentBytes = encoding.encode(value);
224
- const lengthBytes = size.encode(contentBytes.length);
225
- return mergeBytes([lengthBytes, contentBytes]);
214
+ return createEncoder({
215
+ getSizeFromValue: (value) => {
216
+ const contentSize = getEncodedSize(value, encoding);
217
+ return getEncodedSize(contentSize, size) + contentSize;
226
218
  },
227
- fixedSize: null,
228
- maxSize: null
229
- };
230
- };
231
- var getStringDecoder = (options = {}) => {
232
- const size = options.size ?? getU32Decoder();
233
- const encoding = options.encoding ?? getUtf8Decoder();
234
- const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
219
+ write: (value, bytes, offset) => {
220
+ const contentSize = getEncodedSize(value, encoding);
221
+ offset = size.write(contentSize, bytes, offset);
222
+ return encoding.write(value, bytes, offset);
223
+ }
224
+ });
225
+ }
226
+ function getStringDecoder(config = {}) {
227
+ const size = config.size ?? getU32Decoder();
228
+ const encoding = config.encoding ?? getUtf8Decoder();
235
229
  if (size === "variable") {
236
- return { ...encoding, description };
230
+ return encoding;
237
231
  }
238
232
  if (typeof size === "number") {
239
- return fixDecoder(encoding, size, description);
233
+ return fixDecoder(encoding, size);
240
234
  }
241
- return {
242
- decode: (bytes, offset = 0) => {
235
+ return createDecoder({
236
+ read: (bytes, offset = 0) => {
243
237
  assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
244
- const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
238
+ const [lengthBigInt, lengthOffset] = size.read(bytes, offset);
245
239
  const length = Number(lengthBigInt);
246
240
  offset = lengthOffset;
247
241
  const contentBytes = bytes.slice(offset, offset + length);
248
242
  assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
249
- const [value, contentOffset] = encoding.decode(contentBytes);
243
+ const [value, contentOffset] = encoding.read(contentBytes, 0);
250
244
  offset += contentOffset;
251
245
  return [value, offset];
252
- },
253
- description,
254
- fixedSize: null,
255
- maxSize: null
256
- };
257
- };
258
- var getStringCodec = (options = {}) => combineCodec(getStringEncoder(options), getStringDecoder(options));
259
- function getSizeDescription(size) {
260
- return typeof size === "object" ? size.description : `${size}`;
246
+ }
247
+ });
248
+ }
249
+ function getStringCodec(config = {}) {
250
+ return combineCodec(getStringEncoder(config), getStringDecoder(config));
261
251
  }
262
252
 
263
253
  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 };
@@ -1 +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.node.ts"],"names":["alphabet","combineCodec","e","TextDecoder","TextEncoder"],"mappings":";AAGO,SAAS,sBAAsBA,WAAkB,WAAmB,aAAa,WAAW;AAC/F,MAAI,CAAC,UAAU,MAAM,IAAI,OAAO,KAAKA,SAAQ,KAAK,CAAC,GAAG;AAElD,UAAM,IAAI,MAAM,6BAA6BA,UAAS,MAAM,UAAU,UAAU,IAAI;AAAA,EACxF;AACJ;;;ACRA,SAAgB,oBAAsC;AAS/C,IAAM,kBAAkB,CAACA,cAAsC;AAClE,QAAM,OAAOA,UAAS;AACtB,QAAM,aAAa,OAAO,IAAI;AAC9B,SAAO;AAAA,IACH,aAAa,OAAO,IAAI;AAAA,IACxB,OAAO,OAA2B;AAE9B,4BAAsBA,WAAU,KAAK;AACrC,UAAI,UAAU;AAAI,eAAO,IAAI,WAAW;AAGxC,YAAM,QAAQ,CAAC,GAAG,KAAK;AACvB,UAAI,aAAa,MAAM,UAAU,OAAK,MAAMA,UAAS,CAAC,CAAC;AACvD,mBAAa,eAAe,KAAK,MAAM,SAAS;AAChD,YAAM,gBAAgB,MAAM,UAAU,EAAE,KAAK,CAAC;AAC9C,UAAI,eAAe,MAAM;AAAQ,eAAO,WAAW,KAAK,aAAa;AAGrE,YAAM,YAAY,MAAM,MAAM,UAAU;AACxC,UAAI,eAAe;AACnB,UAAI,aAAa;AACjB,eAAS,IAAI,UAAU,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG;AAC/C,wBAAgB,aAAa,OAAOA,UAAS,QAAQ,UAAU,CAAC,CAAC,CAAC;AAClE,sBAAc;AAAA,MAClB;AA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AGO,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"]}
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.node.ts"],"names":["alphabet","combineCodec","createDecoder","createEncoder","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O,QAAQ,cAAc;AAC1C,QAAM,WAAW,OAAO,YAAY,eAAe;AAEnD,MAAI,SAAS,YAAY;AACrB,WAAO;AAAA,EACX;AAEA,MAAI,OAAO,SAAS,UAAU;AAC1B,WAAO,WAAW,UAAU,IAAI;AAAA,EACpC;AAEA,SAAOD,eAAc;AAAA,IACjB,MAAM,CAAC,OAAmB,SAAS,MAAM;AACrC,wCAAkC,UAAU,OAAO,MAAM;AACzD,YAAM,CAAC,cAAc,YAAY,IAAI,KAAK,KAAK,OAAO,MAAM;AAC5D,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,KAAK,cAAc,CAAC;AAC5D,gBAAU;AACV,aAAO,CAAC,OAAO,MAAM;AAAA,IACzB;AAAA,EACJ,CAAC;AACL;AAaO,SAAS,eAAe,SAAwD,CAAC,GAAkB;AACtG,SAAOD,cAAa,iBAAiB,MAAM,GAAG,iBAAiB,MAAM,CAAC;AAC1E","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 {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} 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): VariableSizeEncoder<string> => {\n return createEncoder({\n getSizeFromValue: (value: string): number => {\n const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);\n if (tailChars === '') return value.length;\n\n const base10Number = getBigIntFromBaseX(tailChars, alphabet);\n return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);\n },\n write(value: string, bytes, offset) {\n // Check if the value is valid.\n assertValidBaseString(alphabet, value);\n if (value === '') return offset;\n\n // Handle leading zeroes.\n const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);\n if (tailChars === '') {\n bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);\n return offset + leadingZeroes.length;\n }\n\n // From baseX to base10.\n let base10Number = getBigIntFromBaseX(tailChars, alphabet);\n\n // From base10 to bytes.\n const tailBytes: number[] = [];\n while (base10Number > 0n) {\n tailBytes.unshift(Number(base10Number % 256n));\n base10Number /= 256n;\n }\n\n const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];\n bytes.set(bytesToAdd, offset);\n return offset + bytesToAdd.length;\n },\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): VariableSizeDecoder<string> => {\n return createDecoder({\n read(rawBytes, offset): [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 const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);\n\n // From base10 to baseX.\n const tailChars = getBaseXFromBigInt(base10Number, alphabet);\n\n return [leadingZeroes + tailChars, rawBytes.length];\n },\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): VariableSizeCodec<string> =>\n combineCodec(getBaseXEncoder(alphabet), getBaseXDecoder(alphabet));\n\nfunction partitionLeadingZeroes(value: string, zeroCharacter: string): [string, string] {\n const leadingZeroIndex = [...value].findIndex(c => c !== zeroCharacter);\n return leadingZeroIndex === -1 ? [value, ''] : [value.slice(0, leadingZeroIndex), value.slice(leadingZeroIndex)];\n}\n\nfunction getBigIntFromBaseX(value: string, alphabet: string): bigint {\n const base = BigInt(alphabet.length);\n return [...value].reduce((sum, char) => sum * base + BigInt(alphabet.indexOf(char)), 0n);\n}\n\nfunction getBaseXFromBigInt(value: bigint, alphabet: string): string {\n const base = BigInt(alphabet.length);\n const tailChars = [];\n while (value > 0n) {\n tailChars.unshift(alphabet[Number(value % base)]);\n value /= base;\n }\n return tailChars.join('');\n}\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 {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/** Encodes strings in base16. */\nexport const getBase16Encoder = (): VariableSizeEncoder<string> =>\n createEncoder({\n getSizeFromValue: (value: string) => Math.ceil(value.length / 2),\n write(value: string, bytes, offset) {\n const lowercaseValue = value.toLowerCase();\n assertValidBaseString('0123456789abcdef', lowercaseValue, value);\n const matches = lowercaseValue.match(/.{1,2}/g);\n const hexBytes = matches ? matches.map((byte: string) => parseInt(byte, 16)) : [];\n bytes.set(hexBytes, offset);\n return hexBytes.length + offset;\n },\n });\n\n/** Decodes strings in base16. */\nexport const getBase16Decoder = (): VariableSizeDecoder<string> =>\n createDecoder({\n read(bytes, offset) {\n const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');\n return [value, bytes.length];\n },\n });\n\n/** Encodes and decodes strings in base16. */\nexport const getBase16Codec = (): VariableSizeCodec<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 {\n combineCodec,\n createDecoder,\n createEncoder,\n mapDecoder,\n mapEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} 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 = (): VariableSizeEncoder<string> => {\n if (__BROWSER__) {\n return createEncoder({\n getSizeFromValue: (value: string) => {\n try {\n return (atob as Window['atob'])(value).length;\n } catch (e) {\n // TODO: Coded error.\n throw new Error(`Expected a string of base 64, got [${value}].`);\n }\n },\n write(value: string, bytes, offset) {\n try {\n const bytesToAdd = (atob as Window['atob'])(value)\n .split('')\n .map(c => c.charCodeAt(0));\n bytes.set(bytesToAdd, offset);\n return bytesToAdd.length + offset;\n } catch (e) {\n // TODO: Coded error.\n throw new Error(`Expected a string of base 64, got [${value}].`);\n }\n },\n });\n }\n\n if (__NODEJS__) {\n return createEncoder({\n getSizeFromValue: (value: string) => Buffer.from(value, 'base64').length,\n write(value: string, bytes, offset) {\n assertValidBaseString(alphabet, value.replace(/=/g, ''));\n const buffer = Buffer.from(value, 'base64');\n bytes.set(buffer, offset);\n return buffer.length + offset;\n },\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 = (): VariableSizeDecoder<string> => {\n if (__BROWSER__) {\n return createDecoder({\n read(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 });\n }\n\n if (__NODEJS__) {\n return createDecoder({\n read: (bytes, offset = 0) => [Buffer.from(bytes, offset).toString('base64'), bytes.length],\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 = (): VariableSizeCodec<string> => combineCodec(getBase64Encoder(), getBase64Decoder());\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} 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): VariableSizeEncoder<string> =>\n createEncoder({\n getSizeFromValue: (value: string) => Math.floor((value.length * bits) / 8),\n write(value: string, bytes, offset) {\n assertValidBaseString(alphabet, value);\n if (value === '') return offset;\n const charIndices = [...value].map(c => alphabet.indexOf(c));\n const reslicedBytes = reslice(charIndices, bits, 8, false);\n bytes.set(reslicedBytes, offset);\n return reslicedBytes.length + offset;\n },\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): VariableSizeDecoder<string> =>\n createDecoder({\n read(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 });\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): VariableSizeCodec<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 Codec,\n combineCodec,\n createDecoder,\n createEncoder,\n Decoder,\n Encoder,\n fixDecoder,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n fixEncoder,\n getEncodedSize,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\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 config for string codecs. */\nexport type StringCodecConfig<\n TPrefix extends NumberCodec | NumberEncoder | NumberDecoder,\n TEncoding extends Codec<string> | Encoder<string> | Decoder<string>,\n> = {\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 function getStringEncoder<TSize extends number>(\n config: StringCodecConfig<NumberEncoder, Encoder<string>> & { size: TSize },\n): FixedSizeEncoder<string, TSize>;\nexport function getStringEncoder<TSize extends number>(\n config: StringCodecConfig<NumberEncoder, Encoder<string>> & {\n size: 'variable';\n encoding: FixedSizeEncoder<string, TSize>;\n },\n): FixedSizeEncoder<string, TSize>;\nexport function getStringEncoder(\n config?: StringCodecConfig<NumberEncoder, Encoder<string>>,\n): VariableSizeEncoder<string>;\nexport function getStringEncoder(config: StringCodecConfig<NumberEncoder, Encoder<string>> = {}): Encoder<string> {\n const size = config.size ?? getU32Encoder();\n const encoding = config.encoding ?? getUtf8Encoder();\n\n if (size === 'variable') {\n return encoding;\n }\n\n if (typeof size === 'number') {\n return fixEncoder(encoding, size);\n }\n\n return createEncoder({\n getSizeFromValue: (value: string) => {\n const contentSize = getEncodedSize(value, encoding);\n return getEncodedSize(contentSize, size) + contentSize;\n },\n write: (value: string, bytes, offset) => {\n const contentSize = getEncodedSize(value, encoding);\n offset = size.write(contentSize, bytes, offset);\n return encoding.write(value, bytes, offset);\n },\n });\n}\n\n/** Decodes strings from a given encoding and size strategy. */\nexport function getStringDecoder<TSize extends number>(\n config: StringCodecConfig<NumberDecoder, Decoder<string>> & { size: TSize },\n): FixedSizeDecoder<string, TSize>;\nexport function getStringDecoder<TSize extends number>(\n config: StringCodecConfig<NumberDecoder, Decoder<string>> & {\n size: 'variable';\n encoding: FixedSizeDecoder<string, TSize>;\n },\n): FixedSizeDecoder<string, TSize>;\nexport function getStringDecoder(\n config?: StringCodecConfig<NumberDecoder, Decoder<string>>,\n): VariableSizeDecoder<string>;\nexport function getStringDecoder(config: StringCodecConfig<NumberDecoder, Decoder<string>> = {}): Decoder<string> {\n const size = config.size ?? getU32Decoder();\n const encoding = config.encoding ?? getUtf8Decoder();\n\n if (size === 'variable') {\n return encoding;\n }\n\n if (typeof size === 'number') {\n return fixDecoder(encoding, size);\n }\n\n return createDecoder({\n read: (bytes: Uint8Array, offset = 0) => {\n assertByteArrayIsNotEmptyForCodec('string', bytes, offset);\n const [lengthBigInt, lengthOffset] = size.read(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.read(contentBytes, 0);\n offset += contentOffset;\n return [value, offset];\n },\n });\n}\n\n/** Encodes and decodes strings from a given encoding and size strategy. */\nexport function getStringCodec<TSize extends number>(\n config: StringCodecConfig<NumberCodec, Codec<string>> & { size: TSize },\n): FixedSizeCodec<string, string, TSize>;\nexport function getStringCodec<TSize extends number>(\n config: StringCodecConfig<NumberCodec, Codec<string>> & {\n size: 'variable';\n encoding: FixedSizeCodec<string, string, TSize>;\n },\n): FixedSizeCodec<string, string, TSize>;\nexport function getStringCodec(config?: StringCodecConfig<NumberCodec, Codec<string>>): VariableSizeCodec<string>;\nexport function getStringCodec(config: StringCodecConfig<NumberCodec, Codec<string>> = {}): Codec<string> {\n return combineCodec(getStringEncoder(config), getStringDecoder(config));\n}\n","import {\n Codec,\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} 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 = (): VariableSizeEncoder<string> => {\n let textEncoder: TextEncoder;\n return createEncoder({\n getSizeFromValue: value => (textEncoder ||= new TextEncoder()).encode(value).length,\n write: (value: string, bytes, offset) => {\n const bytesToAdd = (textEncoder ||= new TextEncoder()).encode(value);\n bytes.set(bytesToAdd, offset);\n return offset + bytesToAdd.length;\n },\n });\n};\n\n/** Decodes UTF-8 strings using the native `TextDecoder` API. */\nexport const getUtf8Decoder = (): VariableSizeDecoder<string> => {\n let textDecoder: TextDecoder;\n return createDecoder({\n read(bytes, offset) {\n const value = (textDecoder ||= new TextDecoder()).decode(bytes.slice(offset));\n return [removeNullCharacters(value), bytes.length];\n },\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"]}
@@ -2,35 +2,35 @@ this.globalThis = this.globalThis || {};
2
2
  this.globalThis.solanaWeb3 = (function (exports) {
3
3
  'use strict';
4
4
 
5
- function u(e,r,t=r){if(!r.match(new RegExp(`^[${e}]*$`)))throw new Error(`Expected a string of base ${e.length}, got [${t}].`)}function S(e,r,t=0){if(r.length-t<=0)throw new Error(`Codec [${e}] cannot decode empty byte arrays.`)}function x(e,r,t,n=0){let i=t.length-n;if(i<r)throw new Error(`Codec [${e}] expected ${r} bytes, got ${i}.`)}var I=e=>{let r=e.filter(o=>o.length);if(r.length===0)return e.length?e[0]:new Uint8Array;if(r.length===1)return r[0];let t=r.reduce((o,c)=>o+c.length,0),n=new Uint8Array(t),i=0;return r.forEach(o=>{n.set(o,i),i+=o.length;}),n},H=(e,r)=>{if(e.length>=r)return e;let t=new Uint8Array(r).fill(0);return t.set(e),t},U=(e,r)=>H(e.length<=r?e:e.slice(0,r),r);function d(e,r,t){if(e.fixedSize!==r.fixedSize)throw new Error(`Encoder and decoder must have the same fixed size, got [${e.fixedSize}] and [${r.fixedSize}].`);if(e.maxSize!==r.maxSize)throw new Error(`Encoder and decoder must have the same max size, got [${e.maxSize}] and [${r.maxSize}].`);if(t===void 0&&e.description!==r.description)throw new Error(`Encoder and decoder must have the same description, got [${e.description}] and [${r.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.`);return {decode:r.decode,description:t??e.description,encode:e.encode,fixedSize:e.fixedSize,maxSize:e.maxSize}}function y(e,r,t){return {description:t??`fixed(${r}, ${e.description})`,fixedSize:r,maxSize:r}}function w(e,r,t){return {...y(e,r,t),encode:n=>U(e.encode(n),r)}}function N(e,r,t){return {...y(e,r,t),decode:(n,i=0)=>{x("fixCodec",r,n,i),(i>0||n.length>r)&&(n=n.slice(i,i+r)),e.fixedSize!==null&&(n=U(n,e.fixedSize));let[o]=e.decode(n,0);return [o,i+r]}}}var z=e=>{let r=e.length,t=BigInt(r);return {description:`base${r}`,encode(n){if(u(e,n),n==="")return new Uint8Array;let i=[...n],o=i.findIndex(g=>g!==e[0]);o=o===-1?i.length:o;let c=Array(o).fill(0);if(o===i.length)return Uint8Array.from(c);let a=i.slice(o),s=0n,m=1n;for(let g=a.length-1;g>=0;g-=1)s+=m*BigInt(e.indexOf(a[g])),m*=t;let l=[];for(;s>0n;)l.unshift(Number(s%256n)),s/=256n;return Uint8Array.from(c.concat(l))},fixedSize:null,maxSize:null}},p=e=>{let r=e.length,t=BigInt(r);return {decode(n,i=0){let o=i===0?n:n.slice(i);if(o.length===0)return ["",0];let c=o.findIndex(l=>l!==0);c=c===-1?o.length:c;let a=e[0].repeat(c);if(c===o.length)return [a,n.length];let s=o.slice(c).reduce((l,g)=>l*256n+BigInt(g),0n),m=[];for(;s>0n;)m.unshift(e[Number(s%t)]),s/=t;return [a+m.join(""),n.length]},description:`base${r}`,fixedSize:null,maxSize:null}},h=e=>d(z(e),p(e));var E="0123456789",me=()=>z(E),le=()=>p(E),ue=()=>h(E);var P=()=>({description:"base16",encode(e){let r=e.toLowerCase();u("0123456789abcdef",r,e);let t=r.match(/.{1,2}/g);return Uint8Array.from(t?t.map(n=>parseInt(n,16)):[])},fixedSize:null,maxSize:null}),W=()=>({decode(e,r=0){return [e.slice(r).reduce((n,i)=>n+i.toString(16).padStart(2,"0"),""),e.length]},description:"base16",fixedSize:null,maxSize:null}),Be=()=>d(P(),W());var C="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",Ie=()=>z(C),Ue=()=>p(C),ye=()=>h(C);var B=(e,r)=>({description:`base${e.length}`,encode(t){if(u(e,t),t==="")return new Uint8Array;let n=[...t].map(i=>e.indexOf(i));return new Uint8Array(O(n,r,8,!1))},fixedSize:null,maxSize:null}),b=(e,r)=>({decode(t,n=0){let i=n===0?t:t.slice(n);return i.length===0?["",t.length]:[O([...i],8,r,!0).map(c=>e[c]).join(""),t.length]},description:`base${e.length}`,fixedSize:null,maxSize:null}),Te=(e,r)=>d(B(e,r),b(e,r));function O(e,r,t,n){let i=[],o=0,c=0,a=(1<<t)-1;for(let s of e)for(o=o<<r|s,c+=r;c>=t;)c-=t,i.push(o>>c&a);return n&&c>0&&i.push(o<<t-c&a),i}var j=()=>({description:"base64",encode(e){try{let r=atob(e).split("").map(t=>t.charCodeAt(0));return new Uint8Array(r)}catch{throw new Error(`Expected a string of base 64, got [${e}].`)}},fixedSize:null,maxSize:null}),M=()=>({decode(e,r=0){let t=e.slice(r);return [btoa(String.fromCharCode(...t)),e.length]},description:"base64",fixedSize:null,maxSize:null}),Pe=()=>d(j(),M());var _=e=>e.replace(/\u0000/g,""),Me=(e,r)=>e.padEnd(r,"\0");function G(e,r,t,n){if(n<r||n>t)throw new Error(`Codec [${e}] expected number to be in the range [${r}, ${t}], got ${n}.`)}function T(e){let r,t=e.name;return e.size>1&&(r=!("endian"in e.options)||e.options.endian===0,t+=r?"(le)":"(be)"),{description:e.options.description??t,fixedSize:e.size,littleEndian:r,maxSize:e.size}}function J(e){let r=T(e);return {description:r.description,encode(t){e.range&&G(e.name,e.range[0],e.range[1],t);let n=new ArrayBuffer(e.size);return e.set(new DataView(n),t,r.littleEndian),new Uint8Array(n)},fixedSize:r.fixedSize,maxSize:r.maxSize}}function Z(e){let r=T(e);return {decode(t,n=0){S(r.description,t,n),x(r.description,e.size,t,n);let i=new DataView(q(t,n,e.size));return [e.get(i,r.littleEndian),n+e.size]},description:r.description,fixedSize:r.fixedSize,maxSize:r.maxSize}}function q(e,r,t){let n=e.byteOffset+(r??0),i=t??e.byteLength;return e.buffer.slice(n,n+i)}var X=(e={})=>J({name:"u32",options:e,range:[0,+"0xffffffff"],set:(r,t,n)=>r.setUint32(0,t,n),size:4}),V=(e={})=>Z({get:(r,t)=>r.getUint32(0,t),name:"u32",options:e,size:4});var R=globalThis.TextDecoder,L=globalThis.TextEncoder;var v=()=>{let e;return {description:"utf8",encode:r=>new Uint8Array((e||(e=new L)).encode(r)),fixedSize:null,maxSize:null}},D=()=>{let e;return {decode(r,t=0){let n=(e||(e=new R)).decode(r.slice(t));return [_(n),r.length]},description:"utf8",fixedSize:null,maxSize:null}},cr=()=>d(v(),D());var K=(e={})=>{let r=e.size??X(),t=e.encoding??v(),n=e.description??`string(${t.description}; ${k(r)})`;return r==="variable"?{...t,description:n}:typeof r=="number"?w(t,r,n):{description:n,encode:i=>{let o=t.encode(i),c=r.encode(o.length);return I([c,o])},fixedSize:null,maxSize:null}},Q=(e={})=>{let r=e.size??V(),t=e.encoding??D(),n=e.description??`string(${t.description}; ${k(r)})`;return r==="variable"?{...t,description:n}:typeof r=="number"?N(t,r,n):{decode:(i,o=0)=>{S("string",i,o);let[c,a]=r.decode(i,o),s=Number(c);o=a;let m=i.slice(o,o+s);x("string",s,m);let[l,g]=t.decode(m);return o+=g,[l,o]},description:n,fixedSize:null,maxSize:null}},Er=(e={})=>d(K(e),Q(e));function k(e){return typeof e=="object"?e.description:`${e}`}
5
+ function l(e,r,t=r){if(!r.match(new RegExp(`^[${e}]*$`)))throw new Error(`Expected a string of base ${e.length}, got [${t}].`)}function C(e,r,t=0){if(r.length-t<=0)throw new Error(`Codec [${e}] cannot decode empty byte arrays.`)}function z(e,r,t,n=0){let i=t.length-n;if(i<r)throw new Error(`Codec [${e}] expected ${r} bytes, got ${i}.`)}var Z=(e,r)=>{if(e.length>=r)return e;let t=new Uint8Array(r).fill(0);return t.set(e),t},H=(e,r)=>Z(e.length<=r?e:e.slice(0,r),r);function S(e,r){return "fixedSize"in r?r.fixedSize:r.getSizeFromValue(e)}function a(e){return Object.freeze({...e,encode:r=>{let t=new Uint8Array(S(r,e));return e.write(r,t,0),t}})}function s(e){return Object.freeze({...e,decode:(r,t=0)=>e.read(r,t)[0]})}function m(e){return "fixedSize"in e&&typeof e.fixedSize=="number"}function d(e,r){if(m(e)!==m(r))throw new Error("Encoder and decoder must either both be fixed-size or variable-size.");if(m(e)&&m(r)&&e.fixedSize!==r.fixedSize)throw new Error(`Encoder and decoder must have the same fixed size, got [${e.fixedSize}] and [${r.fixedSize}].`);if(!m(e)&&!m(r)&&e.maxSize!==r.maxSize)throw new Error(`Encoder and decoder must have the same max size, got [${e.maxSize}] and [${r.maxSize}].`);return {...r,...e,decode:r.decode,encode:e.encode,read:r.read,write:e.write}}function F(e,r){return a({fixedSize:r,write:(t,n,i)=>{let c=e.encode(t),o=c.length>r?c.slice(0,r):c;return n.set(o,i),i+r}})}function N(e,r){return s({fixedSize:r,read:(t,n)=>{z("fixCodec",r,t,n),(n>0||t.length>r)&&(t=t.slice(n,n+r)),m(e)&&(t=H(t,e.fixedSize));let[i]=e.read(t,0);return [i,n+r]}})}var x=e=>a({getSizeFromValue:r=>{let[t,n]=A(r,e[0]);if(n==="")return r.length;let i=O(n,e);return t.length+Math.ceil(i.toString(16).length/2)},write(r,t,n){if(l(e,r),r==="")return n;let[i,c]=A(r,e[0]);if(c==="")return t.set(new Uint8Array(i.length).fill(0),n),n+i.length;let o=O(c,e),g=[];for(;o>0n;)g.unshift(Number(o%256n)),o/=256n;let u=[...Array(i.length).fill(0),...g];return t.set(u,n),n+u.length}}),b=e=>s({read(r,t){let n=t===0?r:r.slice(t);if(n.length===0)return ["",0];let i=n.findIndex(u=>u!==0);i=i===-1?n.length:i;let c=e[0].repeat(i);if(i===n.length)return [c,r.length];let o=n.slice(i).reduce((u,E)=>u*256n+BigInt(E),0n),g=J(o,e);return [c+g,r.length]}}),h=e=>d(x(e),b(e));function A(e,r){let t=[...e].findIndex(n=>n!==r);return t===-1?[e,""]:[e.slice(0,t),e.slice(t)]}function O(e,r){let t=BigInt(r.length);return [...e].reduce((n,i)=>n*t+BigInt(r.indexOf(i)),0n)}function J(e,r){let t=BigInt(r.length),n=[];for(;e>0n;)n.unshift(r[Number(e%t)]),e/=t;return n.join("")}var p="0123456789",be=()=>x(p),Ee=()=>b(p),Ce=()=>h(p);var P=()=>a({getSizeFromValue:e=>Math.ceil(e.length/2),write(e,r,t){let n=e.toLowerCase();l("0123456789abcdef",n,e);let i=n.match(/.{1,2}/g),c=i?i.map(o=>parseInt(o,16)):[];return r.set(c,t),c.length+t}}),q=()=>s({read(e,r){return [e.slice(r).reduce((n,i)=>n+i.toString(16).padStart(2,"0"),""),e.length]}}),Ve=()=>d(P(),q());var D="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",Te=()=>x(D),ye=()=>b(D),Ae=()=>h(D);var B=(e,r)=>a({getSizeFromValue:t=>Math.floor(t.length*r/8),write(t,n,i){if(l(e,t),t==="")return i;let c=[...t].map(g=>e.indexOf(g)),o=_(c,r,8,!1);return n.set(o,i),o.length+i}}),I=(e,r)=>s({read(t,n=0){let i=n===0?t:t.slice(n);return i.length===0?["",t.length]:[_([...i],8,r,!0).map(o=>e[o]).join(""),t.length]}}),je=(e,r)=>d(B(e,r),I(e,r));function _(e,r,t,n){let i=[],c=0,o=0,g=(1<<t)-1;for(let u of e)for(c=c<<r|u,o+=r;o>=t;)o-=t,i.push(c>>o&g);return n&&o>0&&i.push(c<<t-o&g),i}var K=()=>a({getSizeFromValue:e=>{try{return atob(e).length}catch{throw new Error(`Expected a string of base 64, got [${e}].`)}},write(e,r,t){try{let n=atob(e).split("").map(i=>i.charCodeAt(0));return r.set(n,t),n.length+t}catch{throw new Error(`Expected a string of base 64, got [${e}].`)}}}),Q=()=>s({read(e,r=0){let t=e.slice(r);return [btoa(String.fromCharCode(...t)),e.length]}}),qe=()=>d(K(),Q());var $=e=>e.replace(/\u0000/g,""),Ye=(e,r)=>e.padEnd(r,"\0");function Y(e,r,t,n){if(n<r||n>t)throw new Error(`Codec [${e}] expected number to be in the range [${r}, ${t}], got ${n}.`)}function L(e){return (e==null?void 0:e.endian)!==1}function ee(e){return a({fixedSize:e.size,write(r,t,n){e.range&&Y(e.name,e.range[0],e.range[1],r);let i=new ArrayBuffer(e.size);return e.set(new DataView(i),r,L(e.config)),t.set(new Uint8Array(i),n),n+e.size}})}function re(e){return s({fixedSize:e.size,read(r,t=0){C(e.name,r,t),z(e.name,e.size,r,t);let n=new DataView(te(r,t,e.size));return [e.get(n,L(e.config)),t+e.size]}})}function te(e,r,t){let n=e.byteOffset+(r!=null?r:0),i=t!=null?t:e.byteLength;return e.buffer.slice(n,n+i)}var R=(e={})=>ee({config:e,name:"u32",range:[0,+"0xffffffff"],set:(r,t,n)=>r.setUint32(0,t,n),size:4}),M=(e={})=>re({config:e,get:(r,t)=>r.getUint32(0,t),name:"u32",size:4});var j=globalThis.TextDecoder,v=globalThis.TextEncoder;var w=()=>{let e;return a({getSizeFromValue:r=>(e||(e=new v)).encode(r).length,write:(r,t,n)=>{let i=(e||(e=new v)).encode(r);return t.set(i,n),n+i.length}})},V=()=>{let e;return s({read(r,t){let n=(e||(e=new j)).decode(r.slice(t));return [$(n),r.length]}})},mr=()=>d(w(),V());function ne(e={}){var n,i;let r=(n=e.size)!=null?n:R(),t=(i=e.encoding)!=null?i:w();return r==="variable"?t:typeof r=="number"?F(t,r):a({getSizeFromValue:c=>{let o=S(c,t);return S(o,r)+o},write:(c,o,g)=>{let u=S(c,t);return g=r.write(u,o,g),t.write(c,o,g)}})}function ie(e={}){var n,i;let r=(n=e.size)!=null?n:M(),t=(i=e.encoding)!=null?i:V();return r==="variable"?t:typeof r=="number"?N(t,r):s({read:(c,o=0)=>{C("string",c,o);let[g,u]=r.read(c,o),E=Number(g);o=u;let U=c.slice(o,o+E);z("string",E,U);let[k,W]=t.read(U,0);return o+=W,[k,o]}})}function Nr(e={}){return d(ne(e),ie(e))}
6
6
 
7
- exports.assertValidBaseString = u;
8
- exports.getBase10Codec = ue;
9
- exports.getBase10Decoder = le;
10
- exports.getBase10Encoder = me;
11
- exports.getBase16Codec = Be;
12
- exports.getBase16Decoder = W;
7
+ exports.assertValidBaseString = l;
8
+ exports.getBase10Codec = Ce;
9
+ exports.getBase10Decoder = Ee;
10
+ exports.getBase10Encoder = be;
11
+ exports.getBase16Codec = Ve;
12
+ exports.getBase16Decoder = q;
13
13
  exports.getBase16Encoder = P;
14
- exports.getBase58Codec = ye;
15
- exports.getBase58Decoder = Ue;
16
- exports.getBase58Encoder = Ie;
17
- exports.getBase64Codec = Pe;
18
- exports.getBase64Decoder = M;
19
- exports.getBase64Encoder = j;
14
+ exports.getBase58Codec = Ae;
15
+ exports.getBase58Decoder = ye;
16
+ exports.getBase58Encoder = Te;
17
+ exports.getBase64Codec = qe;
18
+ exports.getBase64Decoder = Q;
19
+ exports.getBase64Encoder = K;
20
20
  exports.getBaseXCodec = h;
21
- exports.getBaseXDecoder = p;
22
- exports.getBaseXEncoder = z;
23
- exports.getBaseXResliceCodec = Te;
24
- exports.getBaseXResliceDecoder = b;
21
+ exports.getBaseXDecoder = b;
22
+ exports.getBaseXEncoder = x;
23
+ exports.getBaseXResliceCodec = je;
24
+ exports.getBaseXResliceDecoder = I;
25
25
  exports.getBaseXResliceEncoder = B;
26
- exports.getStringCodec = Er;
27
- exports.getStringDecoder = Q;
28
- exports.getStringEncoder = K;
29
- exports.getUtf8Codec = cr;
30
- exports.getUtf8Decoder = D;
31
- exports.getUtf8Encoder = v;
32
- exports.padNullCharacters = Me;
33
- exports.removeNullCharacters = _;
26
+ exports.getStringCodec = Nr;
27
+ exports.getStringDecoder = ie;
28
+ exports.getStringEncoder = ne;
29
+ exports.getUtf8Codec = mr;
30
+ exports.getUtf8Decoder = V;
31
+ exports.getUtf8Encoder = w;
32
+ exports.padNullCharacters = Ye;
33
+ exports.removeNullCharacters = $;
34
34
 
35
35
  return exports;
36
36
 
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assertions.d.ts","sourceRoot":"","sources":["../../src/assertions.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,SAAY,QAKhG"}
@@ -1,7 +1,7 @@
1
1
  /** Encodes strings in base10. */
2
- export declare const getBase10Encoder: () => import("@solana/codecs-core").Encoder<string>;
2
+ export declare const getBase10Encoder: () => import("@solana/codecs-core").VariableSizeEncoder<string>;
3
3
  /** Decodes strings in base10. */
4
- export declare const getBase10Decoder: () => import("@solana/codecs-core").Decoder<string>;
4
+ export declare const getBase10Decoder: () => import("@solana/codecs-core").VariableSizeDecoder<string>;
5
5
  /** Encodes and decodes strings in base10. */
6
- export declare const getBase10Codec: () => import("@solana/codecs-core").Codec<string>;
6
+ export declare const getBase10Codec: () => import("@solana/codecs-core").VariableSizeCodec<string>;
7
7
  //# sourceMappingURL=base10.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base10.d.ts","sourceRoot":"","sources":["../../src/base10.ts"],"names":[],"mappings":"AAIA,iCAAiC;AACjC,eAAO,MAAM,gBAAgB,iEAAkC,CAAC;AAEhE,iCAAiC;AACjC,eAAO,MAAM,gBAAgB,iEAAkC,CAAC;AAEhE,6CAA6C;AAC7C,eAAO,MAAM,cAAc,+DAAgC,CAAC"}
@@ -1,8 +1,8 @@
1
- import { Codec, Decoder, Encoder } from '@solana/codecs-core';
1
+ import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
2
2
  /** Encodes strings in base16. */
3
- export declare const getBase16Encoder: () => Encoder<string>;
3
+ export declare const getBase16Encoder: () => VariableSizeEncoder<string>;
4
4
  /** Decodes strings in base16. */
5
- export declare const getBase16Decoder: () => Decoder<string>;
5
+ export declare const getBase16Decoder: () => VariableSizeDecoder<string>;
6
6
  /** Encodes and decodes strings in base16. */
7
- export declare const getBase16Codec: () => Codec<string>;
7
+ export declare const getBase16Codec: () => VariableSizeCodec<string>;
8
8
  //# sourceMappingURL=base16.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base16.d.ts","sourceRoot":"","sources":["../../src/base16.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,qBAAqB,CAAC;AAI7B,iCAAiC;AACjC,eAAO,MAAM,gBAAgB,QAAO,oBAAoB,MAAM,CAWxD,CAAC;AAEP,iCAAiC;AACjC,eAAO,MAAM,gBAAgB,QAAO,oBAAoB,MAAM,CAMxD,CAAC;AAEP,6CAA6C;AAC7C,eAAO,MAAM,cAAc,QAAO,kBAAkB,MAAM,CAAyD,CAAC"}