@bare-ts/lib 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +21 -201
  2. package/dist/codec/data.d.ts +1 -1
  3. package/dist/codec/data.js +10 -12
  4. package/dist/codec/float-array.d.ts +9 -9
  5. package/dist/codec/float-array.js +51 -43
  6. package/dist/codec/i16-array.d.ts +5 -5
  7. package/dist/codec/i16-array.js +34 -26
  8. package/dist/codec/i32-array.d.ts +5 -5
  9. package/dist/codec/i32-array.js +34 -26
  10. package/dist/codec/i64-array.d.ts +5 -5
  11. package/dist/codec/i64-array.js +34 -26
  12. package/dist/codec/i8-array.d.ts +1 -1
  13. package/dist/codec/i8-array.js +12 -14
  14. package/dist/codec/primitive.d.ts +3 -1
  15. package/dist/codec/primitive.js +225 -147
  16. package/dist/codec/string.d.ts +1 -1
  17. package/dist/codec/string.js +45 -31
  18. package/dist/codec/u16-array.d.ts +5 -5
  19. package/dist/codec/u16-array.js +34 -26
  20. package/dist/codec/u32-array.d.ts +5 -5
  21. package/dist/codec/u32-array.js +34 -26
  22. package/dist/codec/u64-array.d.ts +5 -5
  23. package/dist/codec/u64-array.js +34 -26
  24. package/dist/codec/u8-array.d.ts +7 -1
  25. package/dist/codec/u8-array.js +22 -17
  26. package/dist/codec/u8-clamped-array.d.ts +1 -1
  27. package/dist/codec/u8-clamped-array.js +13 -15
  28. package/dist/core/bare-error.d.ts +4 -1
  29. package/dist/core/bare-error.js +4 -8
  30. package/dist/core/byte-cursor.d.ts +22 -28
  31. package/dist/core/byte-cursor.js +33 -29
  32. package/dist/core/config.d.ts +3 -7
  33. package/dist/core/config.js +14 -20
  34. package/dist/env/dev.d.ts +1 -0
  35. package/dist/env/dev.development.d.ts +1 -0
  36. package/dist/env/dev.development.js +2 -0
  37. package/dist/env/dev.js +2 -0
  38. package/dist/env/dev.node.d.ts +1 -0
  39. package/dist/env/dev.node.js +2 -0
  40. package/dist/index.cjs +504 -286
  41. package/dist/index.d.cts +16 -0
  42. package/dist/index.d.ts +16 -2
  43. package/dist/index.js +17 -4
  44. package/dist/util/assert.d.ts +6 -8
  45. package/dist/util/assert.js +10 -13
  46. package/dist/util/constants.d.ts +4 -7
  47. package/dist/util/constants.js +11 -30
  48. package/dist/util/validator.d.ts +1 -1
  49. package/dist/util/validator.js +12 -24
  50. package/package.json +31 -24
  51. package/dist/codec/index.d.ts +0 -13
  52. package/dist/codec/index.js +0 -15
  53. package/dist/core/index.d.ts +0 -3
  54. package/dist/core/index.js +0 -5
  55. package/dist/util/util.d.ts +0 -1
  56. package/dist/util/util.js +0 -6
package/dist/index.cjs CHANGED
@@ -1,25 +1,21 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
6
  var __export = (target, all) => {
7
7
  for (var name in all)
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
- var __reExport = (target, module2, copyDefault, desc) => {
11
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
- for (let key of __getOwnPropNames(module2))
13
- if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
15
  }
16
- return target;
16
+ return to;
17
17
  };
18
- var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
- return (module2, temp) => {
20
- return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
- };
22
- })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
19
 
24
20
  // src/index.ts
25
21
  var src_exports = {};
@@ -27,6 +23,7 @@ __export(src_exports, {
27
23
  BareError: () => BareError,
28
24
  ByteCursor: () => ByteCursor,
29
25
  Config: () => Config,
26
+ check: () => check,
30
27
  readBool: () => readBool,
31
28
  readData: () => readData,
32
29
  readF32: () => readF32,
@@ -70,6 +67,9 @@ __export(src_exports, {
70
67
  readU8FixedArray: () => readU8FixedArray,
71
68
  readUint: () => readUint,
72
69
  readUintSafe: () => readUintSafe,
70
+ readUintSafe32: () => readUintSafe32,
71
+ readUnsafeU8FixedArray: () => readUnsafeU8FixedArray,
72
+ reserve: () => reserve,
73
73
  writeBool: () => writeBool,
74
74
  writeData: () => writeData,
75
75
  writeF32: () => writeF32,
@@ -112,52 +112,32 @@ __export(src_exports, {
112
112
  writeU8ClampedFixedArray: () => writeU8ClampedFixedArray,
113
113
  writeU8FixedArray: () => writeU8FixedArray,
114
114
  writeUint: () => writeUint,
115
- writeUintSafe: () => writeUintSafe
115
+ writeUintSafe: () => writeUintSafe,
116
+ writeUintSafe32: () => writeUintSafe32
116
117
  });
118
+ module.exports = __toCommonJS(src_exports);
119
+
120
+ // dist/env/dev.node.js
121
+ var DEV = process.env.NODE_ENV === "development";
117
122
 
118
123
  // src/util/assert.ts
119
124
  var AssertionError = class extends Error {
120
- constructor(message) {
121
- super(message);
125
+ constructor() {
126
+ super(...arguments);
122
127
  this.name = "AssertionError";
123
128
  }
124
129
  };
125
- function assert(test, message) {
130
+ var V8Error = Error;
131
+ function assert(test, message = "") {
126
132
  if (!test) {
127
133
  const e = new AssertionError(message);
128
- if (Error.captureStackTrace) {
129
- Error.captureStackTrace(e, assert);
134
+ if (V8Error.captureStackTrace) {
135
+ V8Error.captureStackTrace(e, assert);
130
136
  }
131
137
  throw e;
132
138
  }
133
139
  }
134
140
 
135
- // src/core/bare-error.ts
136
- var BareError = class extends Error {
137
- constructor(offset, issue, opts) {
138
- super(`(byte:${offset}) ${issue}`);
139
- this.name = "BareError";
140
- this.issue = issue;
141
- this.offset = offset;
142
- this.cause = opts == null ? void 0 : opts.cause;
143
- }
144
- };
145
-
146
- // src/util/constants.ts
147
- var I16_BYTE_COUNT = 2;
148
- var I32_BYTE_COUNT = 4;
149
- var I64_BYTE_COUNT = 8;
150
- var U16_BYTE_COUNT = 2;
151
- var U32_BYTE_COUNT = 4;
152
- var U64_BYTE_COUNT = 8;
153
- var INT_SAFE_MAX_BYTE_COUNT = 8;
154
- var UINT_MAX_BYTE_COUNT = 10;
155
- var UINT_SAFE_MAX_BYTE_COUNT = 8;
156
- var INVALID_UTF8_STRING = "invalid UTF-8 string";
157
- var NON_CANONICAL_REPRESENTATION = "must be canonical";
158
- var TOO_LARGE_BUFFER = "too large buffer";
159
- var TOO_LARGE_NUMBER = "too large number";
160
-
161
141
  // src/util/validator.ts
162
142
  function isI8(val) {
163
143
  return val === val << 24 >> 24;
@@ -171,9 +151,6 @@ function isI32(val) {
171
151
  function isI64(val) {
172
152
  return val === BigInt.asIntN(64, val);
173
153
  }
174
- function isSafeU64(val) {
175
- return Number.isSafeInteger(val) && val >= 0;
176
- }
177
154
  function isU8(val) {
178
155
  return val === (val & 255);
179
156
  }
@@ -186,64 +163,75 @@ function isU32(val) {
186
163
  function isU64(val) {
187
164
  return val === BigInt.asUintN(64, val);
188
165
  }
189
-
190
- // src/core/config.ts
191
- function Config({
192
- initialBufferLength = 1024,
193
- maxBufferLength = 1024 * 1024 * 32,
194
- textDecoderThreshold = 256,
195
- textEncoderThreshold = 256
196
- }) {
197
- const config = {
198
- initialBufferLength,
199
- maxBufferLength,
200
- textDecoderThreshold,
201
- textEncoderThreshold
202
- };
203
- assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
204
- assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
205
- assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
206
- assert(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
207
- assert(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
208
- return config;
166
+ function isU64Safe(val) {
167
+ return Number.isSafeInteger(val) && val >= 0;
209
168
  }
210
169
 
170
+ // src/util/constants.ts
171
+ var TEXT_DECODER_THRESHOLD = 256;
172
+ var TEXT_ENCODER_THRESHOLD = 256;
173
+ var INT_SAFE_MAX_BYTE_COUNT = 8;
174
+ var UINT_MAX_BYTE_COUNT = 10;
175
+ var UINT_SAFE32_MAX_BYTE_COUNT = 5;
176
+ var INVALID_UTF8_STRING = "invalid UTF-8 string";
177
+ var NON_CANONICAL_REPRESENTATION = "must be canonical";
178
+ var TOO_LARGE_BUFFER = "too large buffer";
179
+ var TOO_LARGE_NUMBER = "too large number";
180
+ var IS_LITTLE_ENDIAN_PLATFORM = /* @__PURE__ */ new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
181
+
182
+ // src/core/bare-error.ts
183
+ var BareError = class extends Error {
184
+ constructor(offset, issue, opts) {
185
+ super(`(byte:${offset}) ${issue}`);
186
+ this.name = "BareError";
187
+ this.issue = issue;
188
+ this.offset = offset;
189
+ this.cause = opts?.cause;
190
+ }
191
+ };
192
+
211
193
  // src/core/byte-cursor.ts
212
194
  var ByteCursor = class {
195
+ /**
196
+ * @throws {BareError} Buffer exceeds `config.maxBufferLength`
197
+ */
213
198
  constructor(bytes, config) {
199
+ /**
200
+ * Read and write Offset in {@link view} and {@link bytes}
201
+ */
202
+ this.offset = 0;
214
203
  if (bytes.length > config.maxBufferLength) {
215
204
  throw new BareError(0, TOO_LARGE_BUFFER);
216
205
  }
217
206
  this.bytes = bytes;
218
207
  this.config = config;
219
- this.offset = 0;
220
208
  this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
221
209
  }
222
- check(min) {
223
- if (this.offset + min > this.bytes.length) {
224
- throw new BareError(this.offset, "missing bytes");
225
- }
210
+ };
211
+ function check(bc, min) {
212
+ if (DEV) {
213
+ assert(isU32(min));
226
214
  }
227
- reserve(min) {
228
- const minLen = this.offset + min | 0;
229
- if (minLen > this.bytes.length) {
230
- if (minLen > this.config.maxBufferLength) {
231
- throw new BareError(0, TOO_LARGE_BUFFER);
232
- }
233
- const newLen = Math.min(minLen << 1, this.config.maxBufferLength);
234
- const newBytes = new Uint8Array(newLen);
235
- newBytes.set(this.bytes);
236
- this.bytes = newBytes;
237
- this.view = new DataView(newBytes.buffer);
238
- }
215
+ if (bc.offset + min > bc.bytes.length) {
216
+ throw new BareError(bc.offset, "missing bytes");
239
217
  }
240
- read(len) {
241
- this.check(len);
242
- const offset = this.offset;
243
- this.offset += len;
244
- return this.bytes.subarray(offset, offset + len);
218
+ }
219
+ function reserve(bc, min) {
220
+ if (DEV) {
221
+ assert(isU32(min));
245
222
  }
246
- };
223
+ const minLen = bc.offset + min | 0;
224
+ if (minLen > bc.bytes.length) {
225
+ if (minLen > bc.config.maxBufferLength) {
226
+ throw new BareError(0, TOO_LARGE_BUFFER);
227
+ }
228
+ const newLen = Math.min(minLen << 1, bc.config.maxBufferLength);
229
+ const newBytes = new Uint8Array(newLen);
230
+ newBytes.set(bc.bytes);
231
+ bc.bytes = newBytes;
232
+ bc.view = new DataView(newBytes.buffer);
233
+ }
234
+ }
247
235
 
248
236
  // src/codec/primitive.ts
249
237
  function readBool(bc) {
@@ -258,75 +246,89 @@ function writeBool(bc, x) {
258
246
  writeU8(bc, x ? 1 : 0);
259
247
  }
260
248
  function readF32(bc) {
261
- bc.check(4);
249
+ check(bc, 4);
262
250
  const result = bc.view.getFloat32(bc.offset, true);
263
251
  bc.offset += 4;
264
252
  return result;
265
253
  }
266
254
  function writeF32(bc, x) {
267
- bc.reserve(4);
255
+ reserve(bc, 4);
268
256
  bc.view.setFloat32(bc.offset, x, true);
269
- assert(Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER);
257
+ if (DEV) {
258
+ assert(
259
+ Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON,
260
+ TOO_LARGE_NUMBER
261
+ );
262
+ }
270
263
  bc.offset += 4;
271
264
  }
272
265
  function readF64(bc) {
273
- bc.check(8);
266
+ check(bc, 8);
274
267
  const result = bc.view.getFloat64(bc.offset, true);
275
268
  bc.offset += 8;
276
269
  return result;
277
270
  }
278
271
  function writeF64(bc, x) {
279
- bc.reserve(8);
272
+ reserve(bc, 8);
280
273
  bc.view.setFloat64(bc.offset, x, true);
281
274
  bc.offset += 8;
282
275
  }
283
276
  function readI8(bc) {
284
- bc.check(1);
277
+ check(bc, 1);
285
278
  return bc.view.getInt8(bc.offset++);
286
279
  }
287
280
  function writeI8(bc, x) {
288
- assert(isI8(x), TOO_LARGE_NUMBER);
289
- bc.reserve(1);
281
+ if (DEV) {
282
+ assert(isI8(x), TOO_LARGE_NUMBER);
283
+ }
284
+ reserve(bc, 1);
290
285
  bc.view.setInt8(bc.offset++, x);
291
286
  }
292
287
  function readI16(bc) {
293
- bc.check(2);
288
+ check(bc, 2);
294
289
  const result = bc.view.getInt16(bc.offset, true);
295
290
  bc.offset += 2;
296
291
  return result;
297
292
  }
298
293
  function writeI16(bc, x) {
299
- assert(isI16(x), TOO_LARGE_NUMBER);
300
- bc.reserve(2);
294
+ if (DEV) {
295
+ assert(isI16(x), TOO_LARGE_NUMBER);
296
+ }
297
+ reserve(bc, 2);
301
298
  bc.view.setInt16(bc.offset, x, true);
302
299
  bc.offset += 2;
303
300
  }
304
301
  function readI32(bc) {
305
- bc.check(4);
302
+ check(bc, 4);
306
303
  const result = bc.view.getInt32(bc.offset, true);
307
304
  bc.offset += 4;
308
305
  return result;
309
306
  }
310
307
  function writeI32(bc, x) {
311
- assert(isI32(x), TOO_LARGE_NUMBER);
312
- bc.reserve(4);
308
+ if (DEV) {
309
+ assert(isI32(x), TOO_LARGE_NUMBER);
310
+ }
311
+ reserve(bc, 4);
313
312
  bc.view.setInt32(bc.offset, x, true);
314
313
  bc.offset += 4;
315
314
  }
316
315
  function readI64(bc) {
317
- bc.check(8);
316
+ check(bc, 8);
318
317
  const result = bc.view.getBigInt64(bc.offset, true);
319
318
  bc.offset += 8;
320
319
  return result;
321
320
  }
322
321
  function writeI64(bc, x) {
323
- assert(isI64(x), TOO_LARGE_NUMBER);
324
- bc.reserve(8);
322
+ if (DEV) {
323
+ assert(isI64(x), TOO_LARGE_NUMBER);
324
+ }
325
+ reserve(bc, 8);
325
326
  bc.view.setBigInt64(bc.offset, x, true);
326
327
  bc.offset += 8;
327
328
  }
328
329
  function readI64Safe(bc) {
329
- const result = readU32(bc) + readI32(bc) * 4294967296;
330
+ const result = readU32(bc) + readI32(bc) * /* 2**32 */
331
+ 4294967296;
330
332
  if (!Number.isSafeInteger(result)) {
331
333
  bc.offset -= 8;
332
334
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
@@ -334,14 +336,22 @@ function readI64Safe(bc) {
334
336
  return result;
335
337
  }
336
338
  function writeI64Safe(bc, x) {
337
- assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
338
- const lowest32 = x >>> 0;
339
+ if (DEV) {
340
+ assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
341
+ }
342
+ let lowest32 = x >>> 0;
339
343
  writeU32(bc, lowest32);
340
- let highest32 = x / 4294967296 | 0;
344
+ let highest32 = x / /* 2**32 */
345
+ 4294967296 | 0;
341
346
  if (x < 0) {
342
- highest32 = ~Math.abs(highest32) >>> 0;
347
+ highest32 = ~(Math.abs(highest32) & /* 2**21-1 */
348
+ 2097151) >>> 0;
343
349
  if (lowest32 === 0) {
344
- highest32++;
350
+ if (highest32 === 2097151) {
351
+ lowest32 = 1;
352
+ } else {
353
+ highest32++;
354
+ }
345
355
  }
346
356
  }
347
357
  writeU32(bc, highest32);
@@ -351,21 +361,28 @@ function readInt(bc) {
351
361
  return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
352
362
  }
353
363
  function writeInt(bc, x) {
354
- assert(isI64(x), TOO_LARGE_NUMBER);
355
- const zigZag = x >> BigInt(63) ^ x << BigInt(1);
356
- writeUint(bc, zigZag);
364
+ const truncated = BigInt.asIntN(64, x);
365
+ if (DEV) {
366
+ assert(truncated === x, TOO_LARGE_NUMBER);
367
+ }
368
+ const zigZag = truncated >> BigInt(63) ^ truncated << BigInt(1);
369
+ writeTruncatedUint(bc, zigZag);
357
370
  }
358
371
  function readIntSafe(bc) {
359
372
  const firstByte = readU8(bc);
360
373
  let result = (firstByte & 127) >> 1;
361
374
  if (firstByte >= 128) {
362
- let shiftMul = 64;
375
+ let shiftMul = (
376
+ /* 2**6 */
377
+ 64
378
+ );
363
379
  let byteCount = 1;
364
380
  let byte;
365
381
  do {
366
382
  byte = readU8(bc);
367
383
  result += (byte & 127) * shiftMul;
368
- shiftMul *= 128;
384
+ shiftMul *= /* 2**7 */
385
+ 128;
369
386
  byteCount++;
370
387
  } while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
371
388
  if (byte === 0) {
@@ -384,77 +401,99 @@ function readIntSafe(bc) {
384
401
  return result;
385
402
  }
386
403
  function writeIntSafe(bc, x) {
387
- assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
388
404
  const sign = x < 0 ? 1 : 0;
389
- if (x < 0) {
390
- x = -(x + 1);
391
- }
392
- const firstByte = (x & 63) << 1 | sign;
393
- x = Math.floor(x / 64);
394
- if (x > 0) {
395
- writeU8(bc, 128 | firstByte);
396
- writeUintSafe(bc, x);
405
+ let zigZag = x < 0 ? -(x + 1) : x;
406
+ let first7Bits = (zigZag & 63) << 1 | sign;
407
+ zigZag = Math.floor(zigZag / /* 2**6 */
408
+ 64);
409
+ if (zigZag > 0) {
410
+ if (!Number.isSafeInteger(x)) {
411
+ if (DEV) {
412
+ assert(false, TOO_LARGE_NUMBER);
413
+ }
414
+ const low = zigZag & 32767;
415
+ const high = (zigZag / 32768 >>> 0) * 32768;
416
+ if (first7Bits === 127 && low === 32767 && high === 4294967295) {
417
+ first7Bits &= ~2;
418
+ }
419
+ zigZag = high + low;
420
+ }
421
+ writeU8(bc, 128 | first7Bits);
422
+ writeUintSafe(bc, zigZag);
397
423
  } else {
398
- writeU8(bc, firstByte);
424
+ writeU8(bc, first7Bits);
399
425
  }
400
426
  }
401
427
  function readU8(bc) {
402
- bc.check(1);
428
+ check(bc, 1);
403
429
  return bc.bytes[bc.offset++];
404
430
  }
405
431
  function writeU8(bc, x) {
406
- assert(isU8(x), TOO_LARGE_NUMBER);
407
- bc.reserve(1);
432
+ if (DEV) {
433
+ assert(isU8(x), TOO_LARGE_NUMBER);
434
+ }
435
+ reserve(bc, 1);
408
436
  bc.bytes[bc.offset++] = x;
409
437
  }
410
438
  function readU16(bc) {
411
- bc.check(2);
439
+ check(bc, 2);
412
440
  const result = bc.view.getUint16(bc.offset, true);
413
441
  bc.offset += 2;
414
442
  return result;
415
443
  }
416
444
  function writeU16(bc, x) {
417
- assert(isU16(x), TOO_LARGE_NUMBER);
418
- bc.reserve(2);
445
+ if (DEV) {
446
+ assert(isU16(x), TOO_LARGE_NUMBER);
447
+ }
448
+ reserve(bc, 2);
419
449
  bc.view.setUint16(bc.offset, x, true);
420
450
  bc.offset += 2;
421
451
  }
422
452
  function readU32(bc) {
423
- bc.check(4);
453
+ check(bc, 4);
424
454
  const result = bc.view.getUint32(bc.offset, true);
425
455
  bc.offset += 4;
426
456
  return result;
427
457
  }
428
458
  function writeU32(bc, x) {
429
- assert(isU32(x), TOO_LARGE_NUMBER);
430
- bc.reserve(4);
459
+ if (DEV) {
460
+ assert(isU32(x), TOO_LARGE_NUMBER);
461
+ }
462
+ reserve(bc, 4);
431
463
  bc.view.setUint32(bc.offset, x, true);
432
464
  bc.offset += 4;
433
465
  }
434
466
  function readU64(bc) {
435
- bc.check(8);
467
+ check(bc, 8);
436
468
  const result = bc.view.getBigUint64(bc.offset, true);
437
469
  bc.offset += 8;
438
470
  return result;
439
471
  }
440
472
  function writeU64(bc, x) {
441
- assert(isU64(x), TOO_LARGE_NUMBER);
442
- bc.reserve(8);
473
+ if (DEV) {
474
+ assert(isU64(x), TOO_LARGE_NUMBER);
475
+ }
476
+ reserve(bc, 8);
443
477
  bc.view.setBigUint64(bc.offset, x, true);
444
478
  bc.offset += 8;
445
479
  }
446
480
  function readU64Safe(bc) {
447
- const result = readU32(bc) + readU32(bc) * 4294967296;
448
- if (!isSafeU64(result)) {
481
+ const result = readU32(bc) + readU32(bc) * /* 2**32 */
482
+ 4294967296;
483
+ if (!isU64Safe(result)) {
449
484
  bc.offset -= 8;
450
485
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
451
486
  }
452
487
  return result;
453
488
  }
454
489
  function writeU64Safe(bc, x) {
455
- assert(isSafeU64(x), TOO_LARGE_NUMBER);
490
+ if (DEV) {
491
+ assert(isU64Safe(x), TOO_LARGE_NUMBER);
492
+ }
456
493
  writeU32(bc, x >>> 0);
457
- writeU32(bc, x / 4294967296 >>> 0);
494
+ writeU32(bc, x / /* 2**32 */
495
+ 4294967296 & /* 2**21-1 */
496
+ 2097151);
458
497
  }
459
498
  function readUint(bc) {
460
499
  let low = readU8(bc);
@@ -466,7 +505,8 @@ function readUint(bc) {
466
505
  do {
467
506
  byte = readU8(bc);
468
507
  low += (byte & 127) * shiftMul;
469
- shiftMul *= 128;
508
+ shiftMul *= /* 2**7 */
509
+ 128;
470
510
  byteCount++;
471
511
  } while (byte >= 128 && byteCount < 7);
472
512
  let height = 0;
@@ -474,7 +514,8 @@ function readUint(bc) {
474
514
  while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
475
515
  byte = readU8(bc);
476
516
  height += (byte & 127) * shiftMul;
477
- shiftMul *= 128;
517
+ shiftMul *= /* 2**7 */
518
+ 128;
478
519
  byteCount++;
479
520
  }
480
521
  if (byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
@@ -486,13 +527,20 @@ function readUint(bc) {
486
527
  return BigInt(low);
487
528
  }
488
529
  function writeUint(bc, x) {
489
- assert(isU64(x), TOO_LARGE_NUMBER);
530
+ const truncated = BigInt.asUintN(64, x);
531
+ if (DEV) {
532
+ assert(truncated === x, TOO_LARGE_NUMBER);
533
+ }
534
+ writeTruncatedUint(bc, truncated);
535
+ }
536
+ function writeTruncatedUint(bc, x) {
490
537
  let tmp = Number(BigInt.asUintN(7 * 7, x));
491
538
  let rest = Number(x >> BigInt(7 * 7));
492
539
  let byteCount = 0;
493
540
  while (tmp >= 128 || rest !== 0) {
494
541
  writeU8(bc, 128 | tmp & 127);
495
- tmp = Math.floor(tmp / 128);
542
+ tmp = Math.floor(tmp / /* 2**7 */
543
+ 128);
496
544
  byteCount++;
497
545
  if (byteCount === 7) {
498
546
  tmp = rest;
@@ -501,24 +549,69 @@ function writeUint(bc, x) {
501
549
  }
502
550
  writeU8(bc, tmp);
503
551
  }
552
+ function readUintSafe32(bc) {
553
+ let result = readU8(bc);
554
+ if (result >= 128) {
555
+ result &= 127;
556
+ let shift = 7;
557
+ let byteCount = 1;
558
+ let byte;
559
+ do {
560
+ byte = readU8(bc);
561
+ result += (byte & 127) << shift >>> 0;
562
+ shift += 7;
563
+ byteCount++;
564
+ } while (byte >= 128 && byteCount < UINT_SAFE32_MAX_BYTE_COUNT);
565
+ if (byte === 0) {
566
+ bc.offset -= byteCount - 1;
567
+ throw new BareError(
568
+ bc.offset - byteCount + 1,
569
+ NON_CANONICAL_REPRESENTATION
570
+ );
571
+ }
572
+ if (byteCount === UINT_SAFE32_MAX_BYTE_COUNT && byte > 15) {
573
+ bc.offset -= byteCount - 1;
574
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
575
+ }
576
+ }
577
+ return result;
578
+ }
579
+ function writeUintSafe32(bc, x) {
580
+ if (DEV) {
581
+ assert(isU32(x), TOO_LARGE_NUMBER);
582
+ }
583
+ let zigZag = x >>> 0;
584
+ while (zigZag >= 128) {
585
+ writeU8(bc, 128 | zigZag & 127);
586
+ zigZag >>>= 7;
587
+ }
588
+ writeU8(bc, zigZag);
589
+ }
504
590
  function readUintSafe(bc) {
505
591
  let result = readU8(bc);
506
592
  if (result >= 128) {
507
593
  result &= 127;
508
- let shiftMul = 128;
594
+ let shiftMul = (
595
+ /* 2**7 */
596
+ 128
597
+ );
509
598
  let byteCount = 1;
510
599
  let byte;
511
600
  do {
512
601
  byte = readU8(bc);
513
602
  result += (byte & 127) * shiftMul;
514
- shiftMul *= 128;
603
+ shiftMul *= /* 2**7 */
604
+ 128;
515
605
  byteCount++;
516
- } while (byte >= 128 && byteCount < UINT_SAFE_MAX_BYTE_COUNT);
606
+ } while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
517
607
  if (byte === 0) {
518
608
  bc.offset -= byteCount - 1;
519
- throw new BareError(bc.offset - byteCount + 1, NON_CANONICAL_REPRESENTATION);
609
+ throw new BareError(
610
+ bc.offset - byteCount + 1,
611
+ NON_CANONICAL_REPRESENTATION
612
+ );
520
613
  }
521
- if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
614
+ if (byteCount === INT_SAFE_MAX_BYTE_COUNT && byte > 15) {
522
615
  bc.offset -= byteCount - 1;
523
616
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
524
617
  }
@@ -526,33 +619,51 @@ function readUintSafe(bc) {
526
619
  return result;
527
620
  }
528
621
  function writeUintSafe(bc, x) {
529
- assert(isSafeU64(x), TOO_LARGE_NUMBER);
530
- while (x >= 128) {
531
- writeU8(bc, 128 | x & 127);
532
- x = Math.floor(x / 128);
622
+ if (DEV) {
623
+ assert(isU64Safe(x), TOO_LARGE_NUMBER);
624
+ }
625
+ let byteCount = 1;
626
+ let zigZag = x;
627
+ while (zigZag >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT) {
628
+ writeU8(bc, 128 | zigZag & 127);
629
+ zigZag = Math.floor(zigZag / /* 2**7 */
630
+ 128);
631
+ byteCount++;
533
632
  }
534
- writeU8(bc, x);
633
+ if (byteCount === INT_SAFE_MAX_BYTE_COUNT) {
634
+ zigZag &= 15;
635
+ }
636
+ writeU8(bc, zigZag);
535
637
  }
536
638
 
537
639
  // src/codec/u8-array.ts
538
640
  function readU8Array(bc) {
539
- return readU8FixedArray(bc, readUintSafe(bc));
641
+ return readU8FixedArray(bc, readUintSafe32(bc));
540
642
  }
541
643
  function writeU8Array(bc, x) {
542
- writeUintSafe(bc, x.length);
644
+ writeUintSafe32(bc, x.length);
543
645
  writeU8FixedArray(bc, x);
544
646
  }
545
647
  function readU8FixedArray(bc, len) {
546
- return bc.read(len).slice();
648
+ return readUnsafeU8FixedArray(bc, len).slice();
547
649
  }
548
650
  function writeU8FixedArray(bc, x) {
549
651
  const len = x.length;
550
652
  if (len !== 0) {
551
- bc.reserve(len);
653
+ reserve(bc, len);
552
654
  bc.bytes.set(x, bc.offset);
553
655
  bc.offset += len;
554
656
  }
555
657
  }
658
+ function readUnsafeU8FixedArray(bc, len) {
659
+ if (DEV) {
660
+ assert(isU32(len));
661
+ }
662
+ check(bc, len);
663
+ const offset = bc.offset;
664
+ bc.offset += len;
665
+ return bc.bytes.subarray(offset, offset + len);
666
+ }
556
667
 
557
668
  // src/codec/data.ts
558
669
  function readData(bc) {
@@ -562,173 +673,213 @@ function writeData(bc, x) {
562
673
  writeU8Array(bc, new Uint8Array(x));
563
674
  }
564
675
  function readFixedData(bc, len) {
676
+ if (DEV) {
677
+ assert(isU32(len));
678
+ }
565
679
  return readU8FixedArray(bc, len).buffer;
566
680
  }
567
681
  function writeFixedData(bc, x) {
568
682
  writeU8FixedArray(bc, new Uint8Array(x));
569
683
  }
570
684
 
571
- // src/util/util.ts
572
- var IS_LITTLE_ENDIAN_PLATFORM = /* @__PURE__ */ new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
573
-
574
685
  // src/codec/float-array.ts
575
- var readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLE : readF32FixedArrayBE;
576
- function readF32FixedArrayLE(bc, len) {
686
+ var readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLe : readF32FixedArrayBe;
687
+ function readF32FixedArrayLe(bc, len) {
688
+ if (DEV) {
689
+ assert(isU32(len));
690
+ }
577
691
  const byteLen = len * 4;
578
692
  const result = new Float32Array(readFixedData(bc, byteLen));
579
693
  return result;
580
694
  }
581
- function readF32FixedArrayBE(bc, len) {
582
- bc.check(len * 4);
695
+ function readF32FixedArrayBe(bc, len) {
696
+ if (DEV) {
697
+ assert(isU32(len));
698
+ }
699
+ check(bc, len * 4);
583
700
  const result = new Float32Array(len);
584
- for (let i = 0; i < len; i++)
701
+ for (let i = 0; i < len; i++) {
585
702
  result[i] = readF32(bc);
703
+ }
586
704
  return result;
587
705
  }
588
- var writeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF32FixedArrayLE : writeF32FixedArrayBE;
589
- function writeF32FixedArrayLE(bc, x) {
706
+ var writeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF32FixedArrayLe : writeF32FixedArrayBe;
707
+ function writeF32FixedArrayLe(bc, x) {
590
708
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
591
709
  }
592
- function writeF32FixedArrayBE(bc, val) {
593
- bc.reserve(val.length * 4);
594
- for (let i = 0; i < val.length; i++)
710
+ function writeF32FixedArrayBe(bc, val) {
711
+ reserve(bc, val.length * 4);
712
+ for (let i = 0; i < val.length; i++) {
595
713
  writeF32(bc, val[i]);
714
+ }
596
715
  }
597
716
  function readF32Array(bc) {
598
- return readF32FixedArray(bc, readUintSafe(bc));
717
+ return readF32FixedArray(bc, readUintSafe32(bc));
599
718
  }
600
719
  function writeF32Array(bc, x) {
601
- writeUintSafe(bc, x.length);
720
+ writeUintSafe32(bc, x.length);
602
721
  if (x.length !== 0) {
603
722
  writeF32FixedArray(bc, x);
604
723
  }
605
724
  }
606
- var readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLE : readF64FixedArrayBE;
607
- function readF64FixedArrayLE(bc, len) {
725
+ var readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLe : readF64FixedArrayBe;
726
+ function readF64FixedArrayLe(bc, len) {
727
+ if (DEV) {
728
+ assert(isU32(len));
729
+ }
608
730
  const byteLen = len * 8;
609
731
  const result = new Float64Array(readFixedData(bc, byteLen));
610
732
  return result;
611
733
  }
612
- function readF64FixedArrayBE(bc, len) {
613
- bc.check(len * 8);
734
+ function readF64FixedArrayBe(bc, len) {
735
+ if (DEV) {
736
+ assert(isU32(len));
737
+ }
738
+ check(bc, len * 8);
614
739
  const result = new Float64Array(len);
615
- for (let i = 0; i < len; i++)
740
+ for (let i = 0; i < len; i++) {
616
741
  result[i] = readF64(bc);
742
+ }
617
743
  return result;
618
744
  }
619
- var writeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF64FixedArrayLE : writeF64FixedArrayBE;
620
- function writeF64FixedArrayLE(bc, x) {
745
+ var writeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF64FixedArrayLe : writeF64FixedArrayBe;
746
+ function writeF64FixedArrayLe(bc, x) {
621
747
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
622
748
  }
623
- function writeF64FixedArrayBE(bc, x) {
624
- bc.reserve(x.length * 8);
625
- for (let i = 0; i < x.length; i++)
749
+ function writeF64FixedArrayBe(bc, x) {
750
+ reserve(bc, x.length * 8);
751
+ for (let i = 0; i < x.length; i++) {
626
752
  writeF64(bc, x[i]);
753
+ }
627
754
  }
628
755
  function readF64Array(bc) {
629
- return readF64FixedArray(bc, readUintSafe(bc));
756
+ return readF64FixedArray(bc, readUintSafe32(bc));
630
757
  }
631
758
  function writeF64Array(bc, x) {
632
- writeUintSafe(bc, x.length);
759
+ writeUintSafe32(bc, x.length);
633
760
  if (x.length !== 0) {
634
761
  writeF64FixedArray(bc, x);
635
762
  }
636
763
  }
637
764
 
638
765
  // src/codec/i16-array.ts
639
- var readI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI16FixedArrayLE : readI16FixedArrayBE;
766
+ var readI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI16FixedArrayLe : readI16FixedArrayBe;
640
767
  function readI16Array(bc) {
641
- return readI16FixedArray(bc, readUintSafe(bc));
768
+ return readI16FixedArray(bc, readUintSafe32(bc));
642
769
  }
643
- function readI16FixedArrayLE(bc, len) {
644
- const byteCount = len * I16_BYTE_COUNT;
770
+ function readI16FixedArrayLe(bc, len) {
771
+ if (DEV) {
772
+ assert(isU32(len));
773
+ }
774
+ const byteCount = len * 2;
645
775
  return new Int16Array(readFixedData(bc, byteCount));
646
776
  }
647
- function readI16FixedArrayBE(bc, len) {
648
- bc.check(len * I16_BYTE_COUNT);
777
+ function readI16FixedArrayBe(bc, len) {
778
+ if (DEV) {
779
+ assert(isU32(len));
780
+ }
781
+ check(bc, len * 2);
649
782
  const result = new Int16Array(len);
650
- for (let i = 0; i < len; i++)
783
+ for (let i = 0; i < len; i++) {
651
784
  result[i] = readI16(bc);
785
+ }
652
786
  return result;
653
787
  }
654
- var writeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI16FixedArrayLE : writeI16FixedArrayBE;
788
+ var writeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI16FixedArrayLe : writeI16FixedArrayBe;
655
789
  function writeI16Array(bc, x) {
656
- writeUintSafe(bc, x.length);
790
+ writeUintSafe32(bc, x.length);
657
791
  if (x.length !== 0) {
658
792
  writeI16FixedArray(bc, x);
659
793
  }
660
794
  }
661
- function writeI16FixedArrayLE(bc, x) {
795
+ function writeI16FixedArrayLe(bc, x) {
662
796
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
663
797
  }
664
- function writeI16FixedArrayBE(bc, x) {
665
- bc.reserve(x.length * I16_BYTE_COUNT);
666
- for (let i = 0; i < x.length; i++)
798
+ function writeI16FixedArrayBe(bc, x) {
799
+ reserve(bc, x.length * 2);
800
+ for (let i = 0; i < x.length; i++) {
667
801
  writeI16(bc, x[i]);
802
+ }
668
803
  }
669
804
 
670
805
  // src/codec/i32-array.ts
671
- var readI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI32FixedArrayLE : readI32FixedArrayBE;
806
+ var readI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI32FixedArrayLe : readI32FixedArrayBe;
672
807
  function readI32Array(bc) {
673
- return readI32FixedArray(bc, readUintSafe(bc));
808
+ return readI32FixedArray(bc, readUintSafe32(bc));
674
809
  }
675
- function readI32FixedArrayLE(bc, len) {
676
- const byteCount = len * I32_BYTE_COUNT;
810
+ function readI32FixedArrayLe(bc, len) {
811
+ if (DEV) {
812
+ assert(isU32(len));
813
+ }
814
+ const byteCount = len * 4;
677
815
  return new Int32Array(readFixedData(bc, byteCount));
678
816
  }
679
- function readI32FixedArrayBE(bc, len) {
680
- bc.check(len * I32_BYTE_COUNT);
817
+ function readI32FixedArrayBe(bc, len) {
818
+ if (DEV) {
819
+ assert(isU32(len));
820
+ }
821
+ check(bc, len * 4);
681
822
  const result = new Int32Array(len);
682
- for (let i = 0; i < len; i++)
823
+ for (let i = 0; i < len; i++) {
683
824
  result[i] = readI32(bc);
825
+ }
684
826
  return result;
685
827
  }
686
- var writeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI32FixedArrayLE : writeI32FixedArrayBE;
828
+ var writeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI32FixedArrayLe : writeI32FixedArrayBe;
687
829
  function writeI32Array(bc, x) {
688
- writeUintSafe(bc, x.length);
830
+ writeUintSafe32(bc, x.length);
689
831
  if (x.length !== 0) {
690
832
  writeI32FixedArray(bc, x);
691
833
  }
692
834
  }
693
- function writeI32FixedArrayLE(bc, x) {
835
+ function writeI32FixedArrayLe(bc, x) {
694
836
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
695
837
  }
696
- function writeI32FixedArrayBE(bc, x) {
697
- bc.reserve(x.length * I32_BYTE_COUNT);
698
- for (let i = 0; i < x.length; i++)
838
+ function writeI32FixedArrayBe(bc, x) {
839
+ reserve(bc, x.length * 4);
840
+ for (let i = 0; i < x.length; i++) {
699
841
  writeI32(bc, x[i]);
842
+ }
700
843
  }
701
844
 
702
845
  // src/codec/i64-array.ts
703
- var readI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI64FixedArrayLE : readI64FixedArrayBE;
846
+ var readI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI64FixedArrayLe : readI64FixedArrayBe;
704
847
  function readI64Array(bc) {
705
- return readI64FixedArray(bc, readUintSafe(bc));
848
+ return readI64FixedArray(bc, readUintSafe32(bc));
706
849
  }
707
- function readI64FixedArrayLE(bc, len) {
708
- const byteCount = len * I64_BYTE_COUNT;
850
+ function readI64FixedArrayLe(bc, len) {
851
+ if (DEV) {
852
+ assert(isU32(len));
853
+ }
854
+ const byteCount = len * 8;
709
855
  return new BigInt64Array(readFixedData(bc, byteCount));
710
856
  }
711
- function readI64FixedArrayBE(bc, len) {
712
- bc.check(len * I64_BYTE_COUNT);
857
+ function readI64FixedArrayBe(bc, len) {
858
+ if (DEV) {
859
+ assert(isU32(len));
860
+ }
861
+ check(bc, len * 8);
713
862
  const result = new BigInt64Array(len);
714
- for (let i = 0; i < len; i++)
863
+ for (let i = 0; i < len; i++) {
715
864
  result[i] = readI64(bc);
865
+ }
716
866
  return result;
717
867
  }
718
- var writeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI64FixedArrayLE : writeI64FixedArrayBE;
868
+ var writeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI64FixedArrayLe : writeI64FixedArrayBe;
719
869
  function writeI64Array(bc, x) {
720
- writeUintSafe(bc, x.length);
870
+ writeUintSafe32(bc, x.length);
721
871
  if (x.length !== 0) {
722
872
  writeI64FixedArray(bc, x);
723
873
  }
724
874
  }
725
- function writeI64FixedArrayLE(bc, x) {
875
+ function writeI64FixedArrayLe(bc, x) {
726
876
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
727
877
  }
728
- function writeI64FixedArrayBE(bc, x) {
729
- bc.reserve(x.length * I64_BYTE_COUNT);
730
- for (let i = 0; i < x.length; i++)
878
+ function writeI64FixedArrayBe(bc, x) {
879
+ reserve(bc, x.length * 8);
880
+ for (let i = 0; i < x.length; i++) {
731
881
  writeI64(bc, x[i]);
882
+ }
732
883
  }
733
884
 
734
885
  // src/codec/i8-array.ts
@@ -736,10 +887,13 @@ function readI8Array(bc) {
736
887
  return readI8FixedArray(bc, readUintSafe(bc));
737
888
  }
738
889
  function writeI8Array(bc, x) {
739
- writeUintSafe(bc, x.length);
890
+ writeUintSafe32(bc, x.length);
740
891
  writeI8FixedArray(bc, x);
741
892
  }
742
893
  function readI8FixedArray(bc, len) {
894
+ if (DEV) {
895
+ assert(isU32(len));
896
+ }
743
897
  return new Int8Array(readFixedData(bc, len));
744
898
  }
745
899
  function writeI8FixedArray(bc, x) {
@@ -748,41 +902,44 @@ function writeI8FixedArray(bc, x) {
748
902
 
749
903
  // src/codec/string.ts
750
904
  function readString(bc) {
751
- return readFixedString(bc, readUintSafe(bc));
905
+ return readFixedString(bc, readUintSafe32(bc));
752
906
  }
753
907
  function writeString(bc, x) {
754
- if (x.length < bc.config.textEncoderThreshold) {
908
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
755
909
  const byteLen = utf8ByteLength(x);
756
- writeUintSafe(bc, byteLen);
757
- bc.reserve(byteLen);
910
+ writeUintSafe32(bc, byteLen);
911
+ reserve(bc, byteLen);
758
912
  writeUtf8Js(bc, x);
759
913
  } else {
760
914
  const strBytes = UTF8_ENCODER.encode(x);
761
- writeUintSafe(bc, strBytes.length);
915
+ writeUintSafe32(bc, strBytes.length);
762
916
  writeU8FixedArray(bc, strBytes);
763
917
  }
764
918
  }
765
919
  function readFixedString(bc, byteLen) {
766
- if (byteLen < bc.config.textDecoderThreshold) {
920
+ if (DEV) {
921
+ assert(isU32(byteLen));
922
+ }
923
+ if (byteLen < TEXT_DECODER_THRESHOLD) {
767
924
  return readUtf8Js(bc, byteLen);
768
925
  }
769
926
  try {
770
- return UTF8_DECODER.decode(bc.read(byteLen));
771
- } catch (cause) {
927
+ return UTF8_DECODER.decode(readUnsafeU8FixedArray(bc, byteLen));
928
+ } catch (_cause) {
772
929
  throw new BareError(bc.offset, INVALID_UTF8_STRING);
773
930
  }
774
931
  }
775
932
  function writeFixedString(bc, x) {
776
- if (x.length < bc.config.textEncoderThreshold) {
933
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
777
934
  const byteLen = utf8ByteLength(x);
778
- bc.reserve(byteLen);
935
+ reserve(bc, byteLen);
779
936
  writeUtf8Js(bc, x);
780
937
  } else {
781
938
  writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
782
939
  }
783
940
  }
784
941
  function readUtf8Js(bc, byteLen) {
785
- bc.check(byteLen);
942
+ check(bc, byteLen);
786
943
  let result = "";
787
944
  const bytes = bc.bytes;
788
945
  let offset = bc.offset;
@@ -795,18 +952,29 @@ function readUtf8Js(bc, byteLen) {
795
952
  if (offset < upperOffset && codePoint < 224) {
796
953
  const byte2 = bytes[offset++];
797
954
  codePoint = (byte1 & 31) << 6 | byte2 & 63;
798
- malformed = codePoint >> 7 === 0 || byte1 >> 5 !== 6 || byte2 >> 6 !== 2;
955
+ malformed = codePoint >> 7 === 0 || // non-canonical char
956
+ byte1 >> 5 !== 6 || // invalid tag
957
+ byte2 >> 6 !== 2;
799
958
  } else if (offset + 1 < upperOffset && codePoint < 240) {
800
959
  const byte2 = bytes[offset++];
801
960
  const byte3 = bytes[offset++];
802
961
  codePoint = (byte1 & 15) << 12 | (byte2 & 63) << 6 | byte3 & 63;
803
- malformed = codePoint >> 11 === 0 || codePoint >> 11 === 27 || byte1 >> 4 !== 14 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2;
962
+ malformed = codePoint >> 11 === 0 || // non-canonical char or missing data
963
+ codePoint >> 11 === 27 || // surrogate char (0xD800 <= codePoint <= 0xDFFF)
964
+ byte1 >> 4 !== 14 || // invalid tag
965
+ byte2 >> 6 !== 2 || // invalid tag
966
+ byte3 >> 6 !== 2;
804
967
  } else if (offset + 2 < upperOffset) {
805
968
  const byte2 = bytes[offset++];
806
969
  const byte3 = bytes[offset++];
807
970
  const byte4 = bytes[offset++];
808
971
  codePoint = (byte1 & 7) << 18 | (byte2 & 63) << 12 | (byte3 & 63) << 6 | byte4 & 63;
809
- malformed = codePoint >> 16 === 0 || codePoint > 1114111 || byte1 >> 3 !== 30 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2 || byte4 >> 6 !== 2;
972
+ malformed = codePoint >> 16 === 0 || // non-canonical char or missing data
973
+ codePoint > 1114111 || // too large code point
974
+ byte1 >> 3 !== 30 || // invalid tag
975
+ byte2 >> 6 !== 2 || // invalid tag
976
+ byte3 >> 6 !== 2 || // invalid tag
977
+ byte4 >> 6 !== 2;
810
978
  }
811
979
  if (malformed) {
812
980
  throw new BareError(bc.offset, INVALID_UTF8_STRING);
@@ -863,121 +1031,167 @@ var UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
863
1031
  var UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
864
1032
 
865
1033
  // src/codec/u16-array.ts
866
- var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
1034
+ var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLe : readU16FixedArrayBe;
867
1035
  function readU16Array(bc) {
868
- return readU16FixedArray(bc, readUintSafe(bc));
1036
+ return readU16FixedArray(bc, readUintSafe32(bc));
869
1037
  }
870
- function readU16FixedArrayLE(bc, len) {
871
- const byteCount = len * U16_BYTE_COUNT;
1038
+ function readU16FixedArrayLe(bc, len) {
1039
+ if (DEV) {
1040
+ assert(isU32(len));
1041
+ }
1042
+ const byteCount = len * 2;
872
1043
  return new Uint16Array(readFixedData(bc, byteCount));
873
1044
  }
874
- function readU16FixedArrayBE(bc, len) {
875
- bc.check(len * U16_BYTE_COUNT);
1045
+ function readU16FixedArrayBe(bc, len) {
1046
+ if (DEV) {
1047
+ assert(isU32(len));
1048
+ }
1049
+ check(bc, len * 2);
876
1050
  const result = new Uint16Array(len);
877
- for (let i = 0; i < len; i++)
1051
+ for (let i = 0; i < len; i++) {
878
1052
  result[i] = readU16(bc);
1053
+ }
879
1054
  return result;
880
1055
  }
881
- var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLE : writeU16FixedArrayBE;
1056
+ var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLe : writeU16FixedArrayBe;
882
1057
  function writeU16Array(bc, x) {
883
- writeUintSafe(bc, x.length);
1058
+ writeUintSafe32(bc, x.length);
884
1059
  if (x.length !== 0) {
885
1060
  writeU16FixedArray(bc, x);
886
1061
  }
887
1062
  }
888
- function writeU16FixedArrayLE(bc, x) {
1063
+ function writeU16FixedArrayLe(bc, x) {
889
1064
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
890
1065
  }
891
- function writeU16FixedArrayBE(bc, x) {
892
- bc.reserve(x.length * U16_BYTE_COUNT);
893
- for (let i = 0; i < x.length; i++)
1066
+ function writeU16FixedArrayBe(bc, x) {
1067
+ reserve(bc, x.length * 2);
1068
+ for (let i = 0; i < x.length; i++) {
894
1069
  writeU16(bc, x[i]);
1070
+ }
895
1071
  }
896
1072
 
897
1073
  // src/codec/u32-array.ts
898
- var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
1074
+ var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLe : readU32FixedArrayBe;
899
1075
  function readU32Array(bc) {
900
- return readU32FixedArray(bc, readUintSafe(bc));
1076
+ return readU32FixedArray(bc, readUintSafe32(bc));
901
1077
  }
902
- function readU32FixedArrayLE(bc, len) {
903
- const byteCount = len * U32_BYTE_COUNT;
1078
+ function readU32FixedArrayLe(bc, len) {
1079
+ if (DEV) {
1080
+ assert(isU32(len));
1081
+ }
1082
+ const byteCount = len * 4;
904
1083
  return new Uint32Array(readFixedData(bc, byteCount));
905
1084
  }
906
- function readU32FixedArrayBE(bc, len) {
907
- bc.check(len * U32_BYTE_COUNT);
1085
+ function readU32FixedArrayBe(bc, len) {
1086
+ if (DEV) {
1087
+ assert(isU32(len));
1088
+ }
1089
+ check(bc, len * 4);
908
1090
  const result = new Uint32Array(len);
909
- for (let i = 0; i < len; i++)
1091
+ for (let i = 0; i < len; i++) {
910
1092
  result[i] = readU32(bc);
1093
+ }
911
1094
  return result;
912
1095
  }
913
- var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLE : writeU32FixedArrayBE;
1096
+ var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLe : writeU32FixedArrayBe;
914
1097
  function writeU32Array(bc, x) {
915
- writeUintSafe(bc, x.length);
1098
+ writeUintSafe32(bc, x.length);
916
1099
  if (x.length !== 0) {
917
1100
  writeU32FixedArray(bc, x);
918
1101
  }
919
1102
  }
920
- function writeU32FixedArrayLE(bc, x) {
1103
+ function writeU32FixedArrayLe(bc, x) {
921
1104
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
922
1105
  }
923
- function writeU32FixedArrayBE(bc, x) {
924
- bc.reserve(x.length * U32_BYTE_COUNT);
925
- for (let i = 0; i < x.length; i++)
1106
+ function writeU32FixedArrayBe(bc, x) {
1107
+ reserve(bc, x.length * 4);
1108
+ for (let i = 0; i < x.length; i++) {
926
1109
  writeU32(bc, x[i]);
1110
+ }
927
1111
  }
928
1112
 
929
1113
  // src/codec/u64-array.ts
930
- var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
1114
+ var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLe : readU64FixedArrayBe;
931
1115
  function readU64Array(bc) {
932
- return readU64FixedArray(bc, readUintSafe(bc));
1116
+ return readU64FixedArray(bc, readUintSafe32(bc));
933
1117
  }
934
- function readU64FixedArrayLE(bc, len) {
935
- const byteCount = len * U64_BYTE_COUNT;
1118
+ function readU64FixedArrayLe(bc, len) {
1119
+ if (DEV) {
1120
+ assert(isU32(len));
1121
+ }
1122
+ const byteCount = len * 8;
936
1123
  return new BigUint64Array(readFixedData(bc, byteCount));
937
1124
  }
938
- function readU64FixedArrayBE(bc, len) {
939
- bc.check(len * U64_BYTE_COUNT);
1125
+ function readU64FixedArrayBe(bc, len) {
1126
+ if (DEV) {
1127
+ assert(isU32(len));
1128
+ }
1129
+ check(bc, len * 8);
940
1130
  const result = new BigUint64Array(len);
941
- for (let i = 0; i < len; i++)
1131
+ for (let i = 0; i < len; i++) {
942
1132
  result[i] = readU64(bc);
1133
+ }
943
1134
  return result;
944
1135
  }
945
- var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLE : writeU64FixedArrayBE;
1136
+ var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLe : writeU64FixedArrayBe;
946
1137
  function writeU64Array(bc, x) {
947
- writeUintSafe(bc, x.length);
1138
+ writeUintSafe32(bc, x.length);
948
1139
  if (x.length !== 0) {
949
1140
  writeU64FixedArray(bc, x);
950
1141
  }
951
1142
  }
952
- function writeU64FixedArrayLE(bc, x) {
1143
+ function writeU64FixedArrayLe(bc, x) {
953
1144
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
954
1145
  }
955
- function writeU64FixedArrayBE(bc, x) {
956
- bc.reserve(x.length * U64_BYTE_COUNT);
957
- for (let i = 0; i < x.length; i++)
1146
+ function writeU64FixedArrayBe(bc, x) {
1147
+ reserve(bc, x.length * 8);
1148
+ for (let i = 0; i < x.length; i++) {
958
1149
  writeU64(bc, x[i]);
1150
+ }
959
1151
  }
960
1152
 
961
1153
  // src/codec/u8-clamped-array.ts
962
1154
  function readU8ClampedArray(bc) {
963
- return readU8ClampedFixedArray(bc, readUintSafe(bc));
1155
+ return readU8ClampedFixedArray(bc, readUintSafe32(bc));
964
1156
  }
965
1157
  function writeU8ClampedArray(bc, x) {
966
- writeUintSafe(bc, x.length);
1158
+ writeUintSafe32(bc, x.length);
967
1159
  writeU8ClampedFixedArray(bc, x);
968
1160
  }
969
1161
  function readU8ClampedFixedArray(bc, len) {
1162
+ if (DEV) {
1163
+ assert(isU32(len));
1164
+ }
970
1165
  return new Uint8ClampedArray(readFixedData(bc, len));
971
1166
  }
972
1167
  function writeU8ClampedFixedArray(bc, x) {
973
1168
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
974
1169
  }
975
- module.exports = __toCommonJS(src_exports);
1170
+
1171
+ // src/core/config.ts
1172
+ function Config({
1173
+ initialBufferLength = 1024,
1174
+ maxBufferLength = 1024 * 1024 * 32
1175
+ }) {
1176
+ if (DEV) {
1177
+ assert(isU32(initialBufferLength), TOO_LARGE_NUMBER);
1178
+ assert(isU32(maxBufferLength), TOO_LARGE_NUMBER);
1179
+ assert(
1180
+ initialBufferLength <= maxBufferLength,
1181
+ "initialBufferLength must be lower than or equal to maxBufferLength"
1182
+ );
1183
+ }
1184
+ return {
1185
+ initialBufferLength,
1186
+ maxBufferLength
1187
+ };
1188
+ }
976
1189
  // Annotate the CommonJS export names for ESM import in node:
977
1190
  0 && (module.exports = {
978
1191
  BareError,
979
1192
  ByteCursor,
980
1193
  Config,
1194
+ check,
981
1195
  readBool,
982
1196
  readData,
983
1197
  readF32,
@@ -1021,6 +1235,9 @@ module.exports = __toCommonJS(src_exports);
1021
1235
  readU8FixedArray,
1022
1236
  readUint,
1023
1237
  readUintSafe,
1238
+ readUintSafe32,
1239
+ readUnsafeU8FixedArray,
1240
+ reserve,
1024
1241
  writeBool,
1025
1242
  writeData,
1026
1243
  writeF32,
@@ -1063,5 +1280,6 @@ module.exports = __toCommonJS(src_exports);
1063
1280
  writeU8ClampedFixedArray,
1064
1281
  writeU8FixedArray,
1065
1282
  writeUint,
1066
- writeUintSafe
1283
+ writeUintSafe,
1284
+ writeUintSafe32
1067
1285
  });