@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.
@@ -21,23 +21,6 @@ this.globalThis.solanaWeb3 = (function (exports) {
21
21
  throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
22
22
  }
23
23
  }
24
- var mergeBytes = (byteArrays) => {
25
- const nonEmptyByteArrays = byteArrays.filter((arr) => arr.length);
26
- if (nonEmptyByteArrays.length === 0) {
27
- return byteArrays.length ? byteArrays[0] : new Uint8Array();
28
- }
29
- if (nonEmptyByteArrays.length === 1) {
30
- return nonEmptyByteArrays[0];
31
- }
32
- const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);
33
- const result = new Uint8Array(totalLength);
34
- let offset = 0;
35
- nonEmptyByteArrays.forEach((arr) => {
36
- result.set(arr, offset);
37
- offset += arr.length;
38
- });
39
- return result;
40
- };
41
24
  var padBytes = (bytes, length) => {
42
25
  if (bytes.length >= length)
43
26
  return bytes;
@@ -46,99 +29,113 @@ this.globalThis.solanaWeb3 = (function (exports) {
46
29
  return paddedBytes;
47
30
  };
48
31
  var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
49
- function combineCodec(encoder, decoder, description) {
50
- if (encoder.fixedSize !== decoder.fixedSize) {
32
+ function getEncodedSize(value, encoder) {
33
+ return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value);
34
+ }
35
+ function createEncoder(encoder) {
36
+ return Object.freeze({
37
+ ...encoder,
38
+ encode: (value) => {
39
+ const bytes = new Uint8Array(getEncodedSize(value, encoder));
40
+ encoder.write(value, bytes, 0);
41
+ return bytes;
42
+ }
43
+ });
44
+ }
45
+ function createDecoder(decoder) {
46
+ return Object.freeze({
47
+ ...decoder,
48
+ decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0]
49
+ });
50
+ }
51
+ function isFixedSize(codec) {
52
+ return "fixedSize" in codec && typeof codec.fixedSize === "number";
53
+ }
54
+ function combineCodec(encoder, decoder) {
55
+ if (isFixedSize(encoder) !== isFixedSize(decoder)) {
56
+ throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`);
57
+ }
58
+ if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) {
51
59
  throw new Error(
52
60
  `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
53
61
  );
54
62
  }
55
- if (encoder.maxSize !== decoder.maxSize) {
63
+ if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) {
56
64
  throw new Error(
57
65
  `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
58
66
  );
59
67
  }
60
- if (description === void 0 && encoder.description !== decoder.description) {
61
- throw new Error(
62
- `Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.`
63
- );
64
- }
65
68
  return {
69
+ ...decoder,
70
+ ...encoder,
66
71
  decode: decoder.decode,
67
- description: description ?? encoder.description,
68
72
  encode: encoder.encode,
69
- fixedSize: encoder.fixedSize,
70
- maxSize: encoder.maxSize
73
+ read: decoder.read,
74
+ write: encoder.write
71
75
  };
72
76
  }
73
- function fixCodecHelper(data, fixedBytes, description) {
74
- return {
75
- description: description ?? `fixed(${fixedBytes}, ${data.description})`,
77
+ function fixEncoder(encoder, fixedBytes) {
78
+ return createEncoder({
76
79
  fixedSize: fixedBytes,
77
- maxSize: fixedBytes
78
- };
79
- }
80
- function fixEncoder(encoder, fixedBytes, description) {
81
- return {
82
- ...fixCodecHelper(encoder, fixedBytes, description),
83
- encode: (value) => fixBytes(encoder.encode(value), fixedBytes)
84
- };
80
+ write: (value, bytes, offset) => {
81
+ const variableByteArray = encoder.encode(value);
82
+ const fixedByteArray = variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray;
83
+ bytes.set(fixedByteArray, offset);
84
+ return offset + fixedBytes;
85
+ }
86
+ });
85
87
  }
86
- function fixDecoder(decoder, fixedBytes, description) {
87
- return {
88
- ...fixCodecHelper(decoder, fixedBytes, description),
89
- decode: (bytes, offset = 0) => {
88
+ function fixDecoder(decoder, fixedBytes) {
89
+ return createDecoder({
90
+ fixedSize: fixedBytes,
91
+ read: (bytes, offset) => {
90
92
  assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
91
93
  if (offset > 0 || bytes.length > fixedBytes) {
92
94
  bytes = bytes.slice(offset, offset + fixedBytes);
93
95
  }
94
- if (decoder.fixedSize !== null) {
96
+ if (isFixedSize(decoder)) {
95
97
  bytes = fixBytes(bytes, decoder.fixedSize);
96
98
  }
97
- const [value] = decoder.decode(bytes, 0);
99
+ const [value] = decoder.read(bytes, 0);
98
100
  return [value, offset + fixedBytes];
99
101
  }
100
- };
102
+ });
101
103
  }
102
104
 
103
105
  // src/baseX.ts
104
106
  var getBaseXEncoder = (alphabet4) => {
105
- const base = alphabet4.length;
106
- const baseBigInt = BigInt(base);
107
- return {
108
- description: `base${base}`,
109
- encode(value) {
107
+ return createEncoder({
108
+ getSizeFromValue: (value) => {
109
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
110
+ if (tailChars === "")
111
+ return value.length;
112
+ const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
113
+ return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
114
+ },
115
+ write(value, bytes, offset) {
110
116
  assertValidBaseString(alphabet4, value);
111
117
  if (value === "")
112
- return new Uint8Array();
113
- const chars = [...value];
114
- let trailIndex = chars.findIndex((c) => c !== alphabet4[0]);
115
- trailIndex = trailIndex === -1 ? chars.length : trailIndex;
116
- const leadingZeroes = Array(trailIndex).fill(0);
117
- if (trailIndex === chars.length)
118
- return Uint8Array.from(leadingZeroes);
119
- const tailChars = chars.slice(trailIndex);
120
- let base10Number = 0n;
121
- let baseXPower = 1n;
122
- for (let i = tailChars.length - 1; i >= 0; i -= 1) {
123
- base10Number += baseXPower * BigInt(alphabet4.indexOf(tailChars[i]));
124
- baseXPower *= baseBigInt;
118
+ return offset;
119
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
120
+ if (tailChars === "") {
121
+ bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
122
+ return offset + leadingZeroes.length;
125
123
  }
124
+ let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
126
125
  const tailBytes = [];
127
126
  while (base10Number > 0n) {
128
127
  tailBytes.unshift(Number(base10Number % 256n));
129
128
  base10Number /= 256n;
130
129
  }
131
- return Uint8Array.from(leadingZeroes.concat(tailBytes));
132
- },
133
- fixedSize: null,
134
- maxSize: null
135
- };
130
+ const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
131
+ bytes.set(bytesToAdd, offset);
132
+ return offset + bytesToAdd.length;
133
+ }
134
+ });
136
135
  };
137
136
  var getBaseXDecoder = (alphabet4) => {
138
- const base = alphabet4.length;
139
- const baseBigInt = BigInt(base);
140
- return {
141
- decode(rawBytes, offset = 0) {
137
+ return createDecoder({
138
+ read(rawBytes, offset) {
142
139
  const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
143
140
  if (bytes.length === 0)
144
141
  return ["", 0];
@@ -147,20 +144,30 @@ this.globalThis.solanaWeb3 = (function (exports) {
147
144
  const leadingZeroes = alphabet4[0].repeat(trailIndex);
148
145
  if (trailIndex === bytes.length)
149
146
  return [leadingZeroes, rawBytes.length];
150
- let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
151
- const tailChars = [];
152
- while (base10Number > 0n) {
153
- tailChars.unshift(alphabet4[Number(base10Number % baseBigInt)]);
154
- base10Number /= baseBigInt;
155
- }
156
- return [leadingZeroes + tailChars.join(""), rawBytes.length];
157
- },
158
- description: `base${base}`,
159
- fixedSize: null,
160
- maxSize: null
161
- };
147
+ const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
148
+ const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
149
+ return [leadingZeroes + tailChars, rawBytes.length];
150
+ }
151
+ });
162
152
  };
163
153
  var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
154
+ function partitionLeadingZeroes(value, zeroCharacter) {
155
+ const leadingZeroIndex = [...value].findIndex((c) => c !== zeroCharacter);
156
+ return leadingZeroIndex === -1 ? [value, ""] : [value.slice(0, leadingZeroIndex), value.slice(leadingZeroIndex)];
157
+ }
158
+ function getBigIntFromBaseX(value, alphabet4) {
159
+ const base = BigInt(alphabet4.length);
160
+ return [...value].reduce((sum, char) => sum * base + BigInt(alphabet4.indexOf(char)), 0n);
161
+ }
162
+ function getBaseXFromBigInt(value, alphabet4) {
163
+ const base = BigInt(alphabet4.length);
164
+ const tailChars = [];
165
+ while (value > 0n) {
166
+ tailChars.unshift(alphabet4[Number(value % base)]);
167
+ value /= base;
168
+ }
169
+ return tailChars.join("");
170
+ }
164
171
 
165
172
  // src/base10.ts
166
173
  var alphabet = "0123456789";
@@ -169,25 +176,22 @@ this.globalThis.solanaWeb3 = (function (exports) {
169
176
  var getBase10Codec = () => getBaseXCodec(alphabet);
170
177
 
171
178
  // src/base16.ts
172
- var getBase16Encoder = () => ({
173
- description: "base16",
174
- encode(value) {
179
+ var getBase16Encoder = () => createEncoder({
180
+ getSizeFromValue: (value) => Math.ceil(value.length / 2),
181
+ write(value, bytes, offset) {
175
182
  const lowercaseValue = value.toLowerCase();
176
183
  assertValidBaseString("0123456789abcdef", lowercaseValue, value);
177
184
  const matches = lowercaseValue.match(/.{1,2}/g);
178
- return Uint8Array.from(matches ? matches.map((byte) => parseInt(byte, 16)) : []);
179
- },
180
- fixedSize: null,
181
- maxSize: null
185
+ const hexBytes = matches ? matches.map((byte) => parseInt(byte, 16)) : [];
186
+ bytes.set(hexBytes, offset);
187
+ return hexBytes.length + offset;
188
+ }
182
189
  });
183
- var getBase16Decoder = () => ({
184
- decode(bytes, offset = 0) {
190
+ var getBase16Decoder = () => createDecoder({
191
+ read(bytes, offset) {
185
192
  const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
186
193
  return [value, bytes.length];
187
- },
188
- description: "base16",
189
- fixedSize: null,
190
- maxSize: null
194
+ }
191
195
  });
192
196
  var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
193
197
 
@@ -198,29 +202,26 @@ this.globalThis.solanaWeb3 = (function (exports) {
198
202
  var getBase58Codec = () => getBaseXCodec(alphabet2);
199
203
 
200
204
  // src/baseX-reslice.ts
201
- var getBaseXResliceEncoder = (alphabet4, bits) => ({
202
- description: `base${alphabet4.length}`,
203
- encode(value) {
205
+ var getBaseXResliceEncoder = (alphabet4, bits) => createEncoder({
206
+ getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
207
+ write(value, bytes, offset) {
204
208
  assertValidBaseString(alphabet4, value);
205
209
  if (value === "")
206
- return new Uint8Array();
210
+ return offset;
207
211
  const charIndices = [...value].map((c) => alphabet4.indexOf(c));
208
- return new Uint8Array(reslice(charIndices, bits, 8, false));
209
- },
210
- fixedSize: null,
211
- maxSize: null
212
+ const reslicedBytes = reslice(charIndices, bits, 8, false);
213
+ bytes.set(reslicedBytes, offset);
214
+ return reslicedBytes.length + offset;
215
+ }
212
216
  });
213
- var getBaseXResliceDecoder = (alphabet4, bits) => ({
214
- decode(rawBytes, offset = 0) {
217
+ var getBaseXResliceDecoder = (alphabet4, bits) => createDecoder({
218
+ read(rawBytes, offset = 0) {
215
219
  const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
216
220
  if (bytes.length === 0)
217
221
  return ["", rawBytes.length];
218
222
  const charIndices = reslice([...bytes], 8, bits, true);
219
223
  return [charIndices.map((i) => alphabet4[i]).join(""), rawBytes.length];
220
- },
221
- description: `base${alphabet4.length}`,
222
- fixedSize: null,
223
- maxSize: null
224
+ }
224
225
  });
225
226
  var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
226
227
  function reslice(input, inputBits, outputBits, useRemainder) {
@@ -243,33 +244,35 @@ this.globalThis.solanaWeb3 = (function (exports) {
243
244
  }
244
245
  var getBase64Encoder = () => {
245
246
  {
246
- return {
247
- description: `base64`,
248
- encode(value) {
247
+ return createEncoder({
248
+ getSizeFromValue: (value) => {
249
249
  try {
250
- const bytes = atob(value).split("").map((c) => c.charCodeAt(0));
251
- return new Uint8Array(bytes);
250
+ return atob(value).length;
252
251
  } catch (e2) {
253
252
  throw new Error(`Expected a string of base 64, got [${value}].`);
254
253
  }
255
254
  },
256
- fixedSize: null,
257
- maxSize: null
258
- };
255
+ write(value, bytes, offset) {
256
+ try {
257
+ const bytesToAdd = atob(value).split("").map((c) => c.charCodeAt(0));
258
+ bytes.set(bytesToAdd, offset);
259
+ return bytesToAdd.length + offset;
260
+ } catch (e2) {
261
+ throw new Error(`Expected a string of base 64, got [${value}].`);
262
+ }
263
+ }
264
+ });
259
265
  }
260
266
  };
261
267
  var getBase64Decoder = () => {
262
268
  {
263
- return {
264
- decode(bytes, offset = 0) {
269
+ return createDecoder({
270
+ read(bytes, offset = 0) {
265
271
  const slice = bytes.slice(offset);
266
272
  const value = btoa(String.fromCharCode(...slice));
267
273
  return [value, bytes.length];
268
- },
269
- description: `base64`,
270
- fixedSize: null,
271
- maxSize: null
272
- };
274
+ }
275
+ });
273
276
  }
274
277
  };
275
278
  var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
@@ -289,66 +292,50 @@ this.globalThis.solanaWeb3 = (function (exports) {
289
292
  );
290
293
  }
291
294
  }
292
- function sharedNumberFactory(input) {
293
- let littleEndian;
294
- let defaultDescription = input.name;
295
- if (input.size > 1) {
296
- littleEndian = !("endian" in input.options) || input.options.endian === 0;
297
- defaultDescription += littleEndian ? "(le)" : "(be)";
298
- }
299
- return {
300
- description: input.options.description ?? defaultDescription,
301
- fixedSize: input.size,
302
- littleEndian,
303
- maxSize: input.size
304
- };
295
+ function isLittleEndian(config) {
296
+ return (config == null ? void 0 : config.endian) === 1 ? false : true;
305
297
  }
306
298
  function numberEncoderFactory(input) {
307
- const codecData = sharedNumberFactory(input);
308
- return {
309
- description: codecData.description,
310
- encode(value) {
299
+ return createEncoder({
300
+ fixedSize: input.size,
301
+ write(value, bytes, offset) {
311
302
  if (input.range) {
312
303
  assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
313
304
  }
314
305
  const arrayBuffer = new ArrayBuffer(input.size);
315
- input.set(new DataView(arrayBuffer), value, codecData.littleEndian);
316
- return new Uint8Array(arrayBuffer);
317
- },
318
- fixedSize: codecData.fixedSize,
319
- maxSize: codecData.maxSize
320
- };
306
+ input.set(new DataView(arrayBuffer), value, isLittleEndian(input.config));
307
+ bytes.set(new Uint8Array(arrayBuffer), offset);
308
+ return offset + input.size;
309
+ }
310
+ });
321
311
  }
322
312
  function numberDecoderFactory(input) {
323
- const codecData = sharedNumberFactory(input);
324
- return {
325
- decode(bytes, offset = 0) {
326
- assertByteArrayIsNotEmptyForCodec(codecData.description, bytes, offset);
327
- assertByteArrayHasEnoughBytesForCodec(codecData.description, input.size, bytes, offset);
313
+ return createDecoder({
314
+ fixedSize: input.size,
315
+ read(bytes, offset = 0) {
316
+ assertByteArrayIsNotEmptyForCodec(input.name, bytes, offset);
317
+ assertByteArrayHasEnoughBytesForCodec(input.name, input.size, bytes, offset);
328
318
  const view = new DataView(toArrayBuffer(bytes, offset, input.size));
329
- return [input.get(view, codecData.littleEndian), offset + input.size];
330
- },
331
- description: codecData.description,
332
- fixedSize: codecData.fixedSize,
333
- maxSize: codecData.maxSize
334
- };
319
+ return [input.get(view, isLittleEndian(input.config)), offset + input.size];
320
+ }
321
+ });
335
322
  }
336
323
  function toArrayBuffer(bytes, offset, length) {
337
- const bytesOffset = bytes.byteOffset + (offset ?? 0);
338
- const bytesLength = length ?? bytes.byteLength;
324
+ const bytesOffset = bytes.byteOffset + (offset != null ? offset : 0);
325
+ const bytesLength = length != null ? length : bytes.byteLength;
339
326
  return bytes.buffer.slice(bytesOffset, bytesOffset + bytesLength);
340
327
  }
341
- var getU32Encoder = (options = {}) => numberEncoderFactory({
328
+ var getU32Encoder = (config = {}) => numberEncoderFactory({
329
+ config,
342
330
  name: "u32",
343
- options,
344
331
  range: [0, Number("0xffffffff")],
345
332
  set: (view, value, le) => view.setUint32(0, value, le),
346
333
  size: 4
347
334
  });
348
- var getU32Decoder = (options = {}) => numberDecoderFactory({
335
+ var getU32Decoder = (config = {}) => numberDecoderFactory({
336
+ config,
349
337
  get: (view, le) => view.getUint32(0, le),
350
338
  name: "u32",
351
- options,
352
339
  size: 4
353
340
  });
354
341
 
@@ -359,79 +346,75 @@ this.globalThis.solanaWeb3 = (function (exports) {
359
346
  // src/utf8.ts
360
347
  var getUtf8Encoder = () => {
361
348
  let textEncoder;
362
- return {
363
- description: "utf8",
364
- encode: (value) => new Uint8Array((textEncoder || (textEncoder = new o())).encode(value)),
365
- fixedSize: null,
366
- maxSize: null
367
- };
349
+ return createEncoder({
350
+ getSizeFromValue: (value) => (textEncoder || (textEncoder = new o())).encode(value).length,
351
+ write: (value, bytes, offset) => {
352
+ const bytesToAdd = (textEncoder || (textEncoder = new o())).encode(value);
353
+ bytes.set(bytesToAdd, offset);
354
+ return offset + bytesToAdd.length;
355
+ }
356
+ });
368
357
  };
369
358
  var getUtf8Decoder = () => {
370
359
  let textDecoder;
371
- return {
372
- decode(bytes, offset = 0) {
360
+ return createDecoder({
361
+ read(bytes, offset) {
373
362
  const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
374
363
  return [removeNullCharacters(value), bytes.length];
375
- },
376
- description: "utf8",
377
- fixedSize: null,
378
- maxSize: null
379
- };
364
+ }
365
+ });
380
366
  };
381
367
  var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
382
368
 
383
369
  // src/string.ts
384
- var getStringEncoder = (options = {}) => {
385
- const size = options.size ?? getU32Encoder();
386
- const encoding = options.encoding ?? getUtf8Encoder();
387
- const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
370
+ function getStringEncoder(config = {}) {
371
+ var _a, _b;
372
+ const size = (_a = config.size) != null ? _a : getU32Encoder();
373
+ const encoding = (_b = config.encoding) != null ? _b : getUtf8Encoder();
388
374
  if (size === "variable") {
389
- return { ...encoding, description };
375
+ return encoding;
390
376
  }
391
377
  if (typeof size === "number") {
392
- return fixEncoder(encoding, size, description);
378
+ return fixEncoder(encoding, size);
393
379
  }
394
- return {
395
- description,
396
- encode: (value) => {
397
- const contentBytes = encoding.encode(value);
398
- const lengthBytes = size.encode(contentBytes.length);
399
- return mergeBytes([lengthBytes, contentBytes]);
380
+ return createEncoder({
381
+ getSizeFromValue: (value) => {
382
+ const contentSize = getEncodedSize(value, encoding);
383
+ return getEncodedSize(contentSize, size) + contentSize;
400
384
  },
401
- fixedSize: null,
402
- maxSize: null
403
- };
404
- };
405
- var getStringDecoder = (options = {}) => {
406
- const size = options.size ?? getU32Decoder();
407
- const encoding = options.encoding ?? getUtf8Decoder();
408
- const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
385
+ write: (value, bytes, offset) => {
386
+ const contentSize = getEncodedSize(value, encoding);
387
+ offset = size.write(contentSize, bytes, offset);
388
+ return encoding.write(value, bytes, offset);
389
+ }
390
+ });
391
+ }
392
+ function getStringDecoder(config = {}) {
393
+ var _a, _b;
394
+ const size = (_a = config.size) != null ? _a : getU32Decoder();
395
+ const encoding = (_b = config.encoding) != null ? _b : getUtf8Decoder();
409
396
  if (size === "variable") {
410
- return { ...encoding, description };
397
+ return encoding;
411
398
  }
412
399
  if (typeof size === "number") {
413
- return fixDecoder(encoding, size, description);
400
+ return fixDecoder(encoding, size);
414
401
  }
415
- return {
416
- decode: (bytes, offset = 0) => {
402
+ return createDecoder({
403
+ read: (bytes, offset = 0) => {
417
404
  assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
418
- const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
405
+ const [lengthBigInt, lengthOffset] = size.read(bytes, offset);
419
406
  const length = Number(lengthBigInt);
420
407
  offset = lengthOffset;
421
408
  const contentBytes = bytes.slice(offset, offset + length);
422
409
  assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
423
- const [value, contentOffset] = encoding.decode(contentBytes);
410
+ const [value, contentOffset] = encoding.read(contentBytes, 0);
424
411
  offset += contentOffset;
425
412
  return [value, offset];
426
- },
427
- description,
428
- fixedSize: null,
429
- maxSize: null
430
- };
431
- };
432
- var getStringCodec = (options = {}) => combineCodec(getStringEncoder(options), getStringDecoder(options));
433
- function getSizeDescription(size) {
434
- return typeof size === "object" ? size.description : `${size}`;
413
+ }
414
+ });
415
+ }
416
+ function getStringCodec(config = {}) {
417
+ return combineCodec(getStringEncoder(config), getStringDecoder(config));
435
418
  }
436
419
 
437
420
  exports.assertValidBaseString = assertValidBaseString;