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