@bare-ts/lib 0.3.0 → 0.5.0

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