@bare-ts/lib 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ // src/core/byte-cursor.ts
2
+ import { ok as assert } from "assert";
3
+ import { BareError } from "../core/bare-error.js";
4
+ var TOO_LARGE_BUFFER = "too large buffer";
5
+ var ByteCursor = class {
6
+ constructor(bytes, config) {
7
+ this.config = config;
8
+ this.offset = 0;
9
+ this.view = bytes instanceof Uint8Array ? new DataView(bytes.buffer, bytes.byteOffset, bytes.length) : new DataView(bytes);
10
+ if (this.view.byteLength > config.maxBufferLength) {
11
+ throw new BareError(0, TOO_LARGE_BUFFER);
12
+ }
13
+ }
14
+ check(min) {
15
+ if (this.offset + min > this.view.byteLength) {
16
+ throw new BareError(this.offset, "missing bytes");
17
+ }
18
+ }
19
+ reserve(min) {
20
+ const { config, offset, view } = this;
21
+ if (offset + min > view.byteLength) {
22
+ const bytes = new Uint8Array(view.buffer);
23
+ const minExtraLength = min + offset - view.byteLength;
24
+ assert(bytes.length === view.byteOffset + view.byteLength, "un-growable buffer");
25
+ assert(bytes.length + minExtraLength <= config.maxBufferLength, TOO_LARGE_BUFFER);
26
+ const newLen = Math.min(bytes.length + Math.max(minExtraLength, bytes.length), config.maxBufferLength);
27
+ const newBytes = new Uint8Array(newLen);
28
+ newBytes.set(bytes);
29
+ this.view = new DataView(newBytes.buffer, view.byteOffset);
30
+ }
31
+ }
32
+ read(len) {
33
+ this.check(len);
34
+ const bufferOffset = this.view.byteOffset + this.offset;
35
+ this.offset += len;
36
+ return new Uint8Array(this.view.buffer, bufferOffset, len);
37
+ }
38
+ write(bytes) {
39
+ const len = bytes.length;
40
+ if (len !== 0) {
41
+ this.reserve(len);
42
+ const bufferOffset = this.view.byteOffset + this.offset;
43
+ const buffer = new Uint8Array(this.view.buffer);
44
+ buffer.set(bytes, bufferOffset);
45
+ this.offset += len;
46
+ }
47
+ }
48
+ };
49
+ export {
50
+ ByteCursor
51
+ };
52
+ //# sourceMappingURL=byte-cursor.js.map
@@ -0,0 +1,26 @@
1
+ // src/core/config.ts
2
+ import { ok as assert } from "assert";
3
+ import { isU32 } from "../util/validator.js";
4
+ var TOO_LARGE_NUMBER = "too large number";
5
+ var initialBufferLength = 1024;
6
+ var maxBufferLength = 1024 * 1024 * 32;
7
+ var textDecoderThreshold = 256;
8
+ var textEncoderThreshold = 256;
9
+ function Config(part) {
10
+ const config = Object.assign({
11
+ initialBufferLength,
12
+ maxBufferLength,
13
+ textDecoderThreshold,
14
+ textEncoderThreshold
15
+ }, part);
16
+ assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
17
+ assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
18
+ assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
19
+ assert(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
20
+ assert(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
21
+ return config;
22
+ }
23
+ export {
24
+ Config
25
+ };
26
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1,5 @@
1
+ // src/core/index.ts
2
+ export * from "./bare-error.js";
3
+ export * from "./config.js";
4
+ export * from "./byte-cursor.js";
5
+ //# sourceMappingURL=index.js.map
package/dist/index.cjs CHANGED
@@ -1,8 +1,6 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8
6
  var __export = (target, all) => {
@@ -17,9 +15,6 @@ var __reExport = (target, module2, copyDefault, desc) => {
17
15
  }
18
16
  return target;
19
17
  };
20
- var __toESM = (module2, isNodeMode) => {
21
- return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
22
- };
23
18
  var __toCommonJS = /* @__PURE__ */ ((cache) => {
24
19
  return (module2, temp) => {
25
20
  return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
@@ -121,10 +116,10 @@ __export(src_exports, {
121
116
  });
122
117
 
123
118
  // src/codec/u8-array.ts
124
- var import_assert4 = __toESM(require("assert"), 1);
119
+ var import_assert4 = require("assert");
125
120
 
126
121
  // src/codec/primitive.ts
127
- var import_assert3 = __toESM(require("assert"), 1);
122
+ var import_assert3 = require("assert");
128
123
 
129
124
  // src/core/bare-error.ts
130
125
  var BareError = class extends Error {
@@ -138,7 +133,7 @@ var BareError = class extends Error {
138
133
  };
139
134
 
140
135
  // src/core/config.ts
141
- var import_assert = __toESM(require("assert"), 1);
136
+ var import_assert = require("assert");
142
137
 
143
138
  // src/util/validator.ts
144
139
  function isI8(val) {
@@ -182,16 +177,16 @@ function Config(part) {
182
177
  textDecoderThreshold,
183
178
  textEncoderThreshold
184
179
  }, part);
185
- (0, import_assert.default)(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
186
- (0, import_assert.default)(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
187
- (0, import_assert.default)(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
188
- (0, import_assert.default)(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
189
- (0, import_assert.default)(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
180
+ (0, import_assert.ok)(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
181
+ (0, import_assert.ok)(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
182
+ (0, import_assert.ok)(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
183
+ (0, import_assert.ok)(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
184
+ (0, import_assert.ok)(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
190
185
  return config;
191
186
  }
192
187
 
193
188
  // src/core/byte-cursor.ts
194
- var import_assert2 = __toESM(require("assert"), 1);
189
+ var import_assert2 = require("assert");
195
190
  var TOO_LARGE_BUFFER = "too large buffer";
196
191
  var ByteCursor = class {
197
192
  constructor(bytes, config) {
@@ -212,8 +207,8 @@ var ByteCursor = class {
212
207
  if (offset + min > view.byteLength) {
213
208
  const bytes = new Uint8Array(view.buffer);
214
209
  const minExtraLength = min + offset - view.byteLength;
215
- (0, import_assert2.default)(bytes.length === view.byteOffset + view.byteLength, "un-growable buffer");
216
- (0, import_assert2.default)(bytes.length + minExtraLength <= config.maxBufferLength, TOO_LARGE_BUFFER);
210
+ (0, import_assert2.ok)(bytes.length === view.byteOffset + view.byteLength, "un-growable buffer");
211
+ (0, import_assert2.ok)(bytes.length + minExtraLength <= config.maxBufferLength, TOO_LARGE_BUFFER);
217
212
  const newLen = Math.min(bytes.length + Math.max(minExtraLength, bytes.length), config.maxBufferLength);
218
213
  const newBytes = new Uint8Array(newLen);
219
214
  newBytes.set(bytes);
@@ -230,8 +225,9 @@ var ByteCursor = class {
230
225
  const len = bytes.length;
231
226
  if (len !== 0) {
232
227
  this.reserve(len);
233
- const writableArea = new Uint8Array(this.view.buffer, this.offset, len);
234
- writableArea.set(bytes);
228
+ const bufferOffset = this.view.byteOffset + this.offset;
229
+ const buffer = new Uint8Array(this.view.buffer);
230
+ buffer.set(bytes, bufferOffset);
235
231
  this.offset += len;
236
232
  }
237
233
  }
@@ -262,10 +258,10 @@ function decodeF32(bc) {
262
258
  return result;
263
259
  }
264
260
  function encodeF32(bc, x) {
265
- (0, import_assert3.default)(!Number.isNaN(x), NAN_NOT_ALLOWED);
261
+ (0, import_assert3.ok)(!Number.isNaN(x), NAN_NOT_ALLOWED);
266
262
  bc.reserve(4);
267
263
  bc.view.setFloat32(bc.offset, x, true);
268
- (0, import_assert3.default)(Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER2);
264
+ (0, import_assert3.ok)(Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER2);
269
265
  bc.offset += 4;
270
266
  }
271
267
  function decodeF64(bc) {
@@ -278,7 +274,7 @@ function decodeF64(bc) {
278
274
  return result;
279
275
  }
280
276
  function encodeF64(bc, x) {
281
- (0, import_assert3.default)(!Number.isNaN(x), NAN_NOT_ALLOWED);
277
+ (0, import_assert3.ok)(!Number.isNaN(x), NAN_NOT_ALLOWED);
282
278
  bc.reserve(8);
283
279
  bc.view.setFloat64(bc.offset, x, true);
284
280
  bc.offset += 8;
@@ -288,7 +284,7 @@ function decodeI8(bc) {
288
284
  return bc.view.getInt8(bc.offset++);
289
285
  }
290
286
  function encodeI8(bc, x) {
291
- (0, import_assert3.default)(isI8(x), TOO_LARGE_NUMBER2);
287
+ (0, import_assert3.ok)(isI8(x), TOO_LARGE_NUMBER2);
292
288
  bc.reserve(1);
293
289
  bc.view.setInt8(bc.offset++, x);
294
290
  }
@@ -299,7 +295,7 @@ function decodeI16(bc) {
299
295
  return result;
300
296
  }
301
297
  function encodeI16(bc, x) {
302
- (0, import_assert3.default)(isI16(x), TOO_LARGE_NUMBER2);
298
+ (0, import_assert3.ok)(isI16(x), TOO_LARGE_NUMBER2);
303
299
  bc.reserve(2);
304
300
  bc.view.setInt16(bc.offset, x, true);
305
301
  bc.offset += 2;
@@ -311,7 +307,7 @@ function decodeI32(bc) {
311
307
  return result;
312
308
  }
313
309
  function encodeI32(bc, x) {
314
- (0, import_assert3.default)(isI32(x), TOO_LARGE_NUMBER2);
310
+ (0, import_assert3.ok)(isI32(x), TOO_LARGE_NUMBER2);
315
311
  bc.reserve(4);
316
312
  bc.view.setInt32(bc.offset, x, true);
317
313
  bc.offset += 4;
@@ -323,7 +319,7 @@ function decodeI64(bc) {
323
319
  return result;
324
320
  }
325
321
  function encodeI64(bc, x) {
326
- (0, import_assert3.default)(isI64(x), TOO_LARGE_NUMBER2);
322
+ (0, import_assert3.ok)(isI64(x), TOO_LARGE_NUMBER2);
327
323
  bc.reserve(8);
328
324
  bc.view.setBigInt64(bc.offset, x, true);
329
325
  bc.offset += 8;
@@ -337,7 +333,7 @@ function decodeI64Safe(bc) {
337
333
  return result;
338
334
  }
339
335
  function encodeI64Safe(bc, x) {
340
- (0, import_assert3.default)(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
336
+ (0, import_assert3.ok)(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
341
337
  const lowest32 = x >>> 0;
342
338
  encodeU32(bc, lowest32);
343
339
  let highest32 = x / 4294967296 | 0;
@@ -354,7 +350,7 @@ function decodeInt(bc) {
354
350
  return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
355
351
  }
356
352
  function encodeInt(bc, x) {
357
- (0, import_assert3.default)(isI64(x), TOO_LARGE_NUMBER2);
353
+ (0, import_assert3.ok)(isI64(x), TOO_LARGE_NUMBER2);
358
354
  const zigZag = x >> BigInt(63) ^ x << BigInt(1);
359
355
  encodeUint(bc, zigZag);
360
356
  }
@@ -388,7 +384,7 @@ function decodeIntSafe(bc) {
388
384
  return result;
389
385
  }
390
386
  function encodeIntSafe(bc, x) {
391
- (0, import_assert3.default)(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
387
+ (0, import_assert3.ok)(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
392
388
  const sign = x < 0 ? 1 : 0;
393
389
  if (x < 0) {
394
390
  x = -(x + 1);
@@ -407,7 +403,7 @@ function decodeU8(bc) {
407
403
  return bc.view.getUint8(bc.offset++);
408
404
  }
409
405
  function encodeU8(bc, x) {
410
- (0, import_assert3.default)(isU8(x), TOO_LARGE_NUMBER2);
406
+ (0, import_assert3.ok)(isU8(x), TOO_LARGE_NUMBER2);
411
407
  bc.reserve(1);
412
408
  bc.view.setUint8(bc.offset++, x);
413
409
  }
@@ -418,7 +414,7 @@ function decodeU16(bc) {
418
414
  return result;
419
415
  }
420
416
  function encodeU16(bc, x) {
421
- (0, import_assert3.default)(isU16(x), TOO_LARGE_NUMBER2);
417
+ (0, import_assert3.ok)(isU16(x), TOO_LARGE_NUMBER2);
422
418
  bc.reserve(2);
423
419
  bc.view.setUint16(bc.offset, x, true);
424
420
  bc.offset += 2;
@@ -430,7 +426,7 @@ function decodeU32(bc) {
430
426
  return result;
431
427
  }
432
428
  function encodeU32(bc, x) {
433
- (0, import_assert3.default)(isU32(x), TOO_LARGE_NUMBER2);
429
+ (0, import_assert3.ok)(isU32(x), TOO_LARGE_NUMBER2);
434
430
  bc.reserve(4);
435
431
  bc.view.setUint32(bc.offset, x, true);
436
432
  bc.offset += 4;
@@ -442,7 +438,7 @@ function decodeU64(bc) {
442
438
  return result;
443
439
  }
444
440
  function encodeU64(bc, x) {
445
- (0, import_assert3.default)(isU64(x), TOO_LARGE_NUMBER2);
441
+ (0, import_assert3.ok)(isU64(x), TOO_LARGE_NUMBER2);
446
442
  bc.reserve(8);
447
443
  bc.view.setBigUint64(bc.offset, x, true);
448
444
  bc.offset += 8;
@@ -456,7 +452,7 @@ function decodeU64Safe(bc) {
456
452
  return result;
457
453
  }
458
454
  function encodeU64Safe(bc, x) {
459
- (0, import_assert3.default)(isSafeU64(x), TOO_LARGE_NUMBER2);
455
+ (0, import_assert3.ok)(isSafeU64(x), TOO_LARGE_NUMBER2);
460
456
  encodeU32(bc, x >>> 0);
461
457
  encodeU32(bc, x / 4294967296 >>> 0);
462
458
  }
@@ -487,7 +483,7 @@ function decodeUint(bc) {
487
483
  return BigInt(low) + (BigInt(height) << BigInt(7 * 7));
488
484
  }
489
485
  function encodeUint(bc, x) {
490
- (0, import_assert3.default)(isU64(x), TOO_LARGE_NUMBER2);
486
+ (0, import_assert3.ok)(isU64(x), TOO_LARGE_NUMBER2);
491
487
  let tmp = Number(BigInt.asUintN(7 * 7, x));
492
488
  let rest = Number(x >> BigInt(7 * 7));
493
489
  let byteCount = 0;
@@ -525,7 +521,7 @@ function decodeUintSafe(bc) {
525
521
  return result;
526
522
  }
527
523
  function encodeUintSafe(bc, x) {
528
- (0, import_assert3.default)(isSafeU64(x), TOO_LARGE_NUMBER2);
524
+ (0, import_assert3.ok)(isSafeU64(x), TOO_LARGE_NUMBER2);
529
525
  while (x >= 128) {
530
526
  encodeU8(bc, 128 | x & 127);
531
527
  x = Math.floor(x / 128);
@@ -551,7 +547,7 @@ function decodeU8FixedArray(bc, len) {
551
547
  return bc.read(len).slice();
552
548
  }
553
549
  function encodeU8FixedArray(bc, x, len) {
554
- (0, import_assert4.default)(x.length === len);
550
+ (0, import_assert4.ok)(x.length === len);
555
551
  bc.write(x);
556
552
  }
557
553
 
@@ -570,7 +566,7 @@ function encodeFixedData(bc, x, len) {
570
566
  }
571
567
 
572
568
  // src/codec/float-array.ts
573
- var import_assert5 = __toESM(require("assert"), 1);
569
+ var import_assert5 = require("assert");
574
570
 
575
571
  // src/util/util.ts
576
572
  var IS_LITTLE_ENDIAN_PLATFORM = new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
@@ -595,12 +591,12 @@ function decodeF32FixedArrayBE(bc, len) {
595
591
  }
596
592
  var encodeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeF32FixedArrayLE : encodeF32FixedArrayBE;
597
593
  function encodeF32FixedArrayLE(bc, x, len) {
598
- (0, import_assert5.default)(x.length === len);
599
- (0, import_assert5.default)(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
594
+ (0, import_assert5.ok)(x.length === len);
595
+ (0, import_assert5.ok)(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
600
596
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
601
597
  }
602
598
  function encodeF32FixedArrayBE(bc, val, len) {
603
- (0, import_assert5.default)(val.length === len);
599
+ (0, import_assert5.ok)(val.length === len);
604
600
  bc.reserve(val.length * 4);
605
601
  for (let i = 0; i < val.length; i++)
606
602
  encodeF32(bc, val[i]);
@@ -632,12 +628,12 @@ function decodeF64FixedArrayBE(bc, len) {
632
628
  }
633
629
  var encodeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeF64FixedArrayLE : encodeF64FixedArrayBE;
634
630
  function encodeF64FixedArrayLE(bc, x, len) {
635
- (0, import_assert5.default)(x.length === len);
636
- (0, import_assert5.default)(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
631
+ (0, import_assert5.ok)(x.length === len);
632
+ (0, import_assert5.ok)(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
637
633
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
638
634
  }
639
635
  function encodeF64FixedArrayBE(bc, x, len) {
640
- (0, import_assert5.default)(x.length === len);
636
+ (0, import_assert5.ok)(x.length === len);
641
637
  bc.reserve(x.length * 8);
642
638
  for (let i = 0; i < x.length; i++)
643
639
  encodeF64(bc, x[i]);
@@ -653,7 +649,7 @@ function encodeF64Array(bc, x) {
653
649
  }
654
650
 
655
651
  // src/codec/i16-array.ts
656
- var import_assert6 = __toESM(require("assert"), 1);
652
+ var import_assert6 = require("assert");
657
653
  var I16_BYTE_COUNT = 2;
658
654
  var decodeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI16FixedArrayLE : decodeI16FixedArrayBE;
659
655
  function decodeI16Array(bc) {
@@ -678,18 +674,18 @@ function encodeI16Array(bc, x) {
678
674
  }
679
675
  }
680
676
  function encodeI16FixedArrayLE(bc, x, len) {
681
- (0, import_assert6.default)(x.length === len);
677
+ (0, import_assert6.ok)(x.length === len);
682
678
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
683
679
  }
684
680
  function encodeI16FixedArrayBE(bc, x, len) {
685
- (0, import_assert6.default)(x.length === len);
681
+ (0, import_assert6.ok)(x.length === len);
686
682
  bc.reserve(x.length * I16_BYTE_COUNT);
687
683
  for (let i = 0; i < x.length; i++)
688
684
  encodeI16(bc, x[i]);
689
685
  }
690
686
 
691
687
  // src/codec/i32-array.ts
692
- var import_assert7 = __toESM(require("assert"), 1);
688
+ var import_assert7 = require("assert");
693
689
  var I32_BYTE_COUNT = 4;
694
690
  var decodeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI32FixedArrayLE : decodeI32FixedArrayBE;
695
691
  function decodeI32Array(bc) {
@@ -714,18 +710,18 @@ function encodeI32Array(bc, x) {
714
710
  }
715
711
  }
716
712
  function encodeI32FixedArrayLE(bc, x, len) {
717
- (0, import_assert7.default)(x.length === len);
713
+ (0, import_assert7.ok)(x.length === len);
718
714
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
719
715
  }
720
716
  function encodeI32FixedArrayBE(bc, x, len) {
721
- (0, import_assert7.default)(x.length === len);
717
+ (0, import_assert7.ok)(x.length === len);
722
718
  bc.reserve(x.length * I32_BYTE_COUNT);
723
719
  for (let i = 0; i < x.length; i++)
724
720
  encodeI32(bc, x[i]);
725
721
  }
726
722
 
727
723
  // src/codec/i64-array.ts
728
- var import_assert8 = __toESM(require("assert"), 1);
724
+ var import_assert8 = require("assert");
729
725
  var I64_BYTE_COUNT = 8;
730
726
  var decodeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI64FixedArrayLE : decodeI64FixedArrayBE;
731
727
  function decodeI64Array(bc) {
@@ -750,11 +746,11 @@ function encodeI64Array(bc, x) {
750
746
  }
751
747
  }
752
748
  function encodeI64FixedArrayLE(bc, x, len) {
753
- (0, import_assert8.default)(x.length === len);
749
+ (0, import_assert8.ok)(x.length === len);
754
750
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
755
751
  }
756
752
  function encodeI64FixedArrayBE(bc, x, len) {
757
- (0, import_assert8.default)(x.length === len);
753
+ (0, import_assert8.ok)(x.length === len);
758
754
  bc.reserve(x.length * I64_BYTE_COUNT);
759
755
  for (let i = 0; i < x.length; i++)
760
756
  encodeI64(bc, x[i]);
@@ -873,20 +869,17 @@ function encodeUtf8Js(bc, s) {
873
869
  }
874
870
  function utf8ByteLength(s) {
875
871
  const sLen = s.length;
876
- let result = 0;
877
- let i = 0;
878
- while (i < sLen) {
872
+ let result = sLen;
873
+ for (let i = 0; i < sLen; i++) {
879
874
  const codePoint = s.codePointAt(i);
880
- i++;
881
- if (codePoint < 128) {
875
+ if (codePoint >= 128) {
882
876
  result++;
883
- } else if (codePoint < 2048) {
884
- result += 2;
885
- } else if (codePoint < 65536) {
886
- result += 3;
887
- } else {
888
- result += 4;
889
- i++;
877
+ if (codePoint >= 2048) {
878
+ result++;
879
+ if (codePoint >= 65536) {
880
+ i++;
881
+ }
882
+ }
890
883
  }
891
884
  }
892
885
  return result;
@@ -895,7 +888,7 @@ var UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
895
888
  var UTF8_ENCODER = new TextEncoder();
896
889
 
897
890
  // src/codec/u16-array.ts
898
- var import_assert9 = __toESM(require("assert"), 1);
891
+ var import_assert9 = require("assert");
899
892
  var U16_BYTE_COUNT = 2;
900
893
  var decodeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU16FixedArrayLE : decodeU16FixedArrayBE;
901
894
  function decodeU16Array(bc) {
@@ -920,18 +913,18 @@ function encodeU16Array(bc, x) {
920
913
  }
921
914
  }
922
915
  function encodeU16FixedArrayLE(bc, x, len) {
923
- (0, import_assert9.default)(x.length === len);
916
+ (0, import_assert9.ok)(x.length === len);
924
917
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
925
918
  }
926
919
  function encodeU16FixedArrayBE(bc, x, len) {
927
- (0, import_assert9.default)(x.length === len);
920
+ (0, import_assert9.ok)(x.length === len);
928
921
  bc.reserve(x.length * U16_BYTE_COUNT);
929
922
  for (let i = 0; i < x.length; i++)
930
923
  encodeU16(bc, x[i]);
931
924
  }
932
925
 
933
926
  // src/codec/u32-array.ts
934
- var import_assert10 = __toESM(require("assert"), 1);
927
+ var import_assert10 = require("assert");
935
928
  var U32_BYTE_COUNT = 4;
936
929
  var decodeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU32FixedArrayLE : decodeU32FixedArrayBE;
937
930
  function decodeU32Array(bc) {
@@ -956,18 +949,18 @@ function encodeU32Array(bc, x) {
956
949
  }
957
950
  }
958
951
  function encodeU32FixedArrayLE(bc, x, len) {
959
- (0, import_assert10.default)(x.length === len);
952
+ (0, import_assert10.ok)(x.length === len);
960
953
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
961
954
  }
962
955
  function encodeU32FixedArrayBE(bc, x, len) {
963
- (0, import_assert10.default)(x.length === len);
956
+ (0, import_assert10.ok)(x.length === len);
964
957
  bc.reserve(x.length * U32_BYTE_COUNT);
965
958
  for (let i = 0; i < x.length; i++)
966
959
  encodeU32(bc, x[i]);
967
960
  }
968
961
 
969
962
  // src/codec/u64-array.ts
970
- var import_assert11 = __toESM(require("assert"), 1);
963
+ var import_assert11 = require("assert");
971
964
  var U64_BYTE_COUNT = 8;
972
965
  var decodeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU64FixedArrayLE : decodeU64FixedArrayBE;
973
966
  function decodeU64Array(bc) {
@@ -992,11 +985,11 @@ function encodeU64Array(bc, x) {
992
985
  }
993
986
  }
994
987
  function encodeU64FixedArrayLE(bc, x, len) {
995
- (0, import_assert11.default)(x.length === len);
988
+ (0, import_assert11.ok)(x.length === len);
996
989
  bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
997
990
  }
998
991
  function encodeU64FixedArrayBE(bc, x, len) {
999
- (0, import_assert11.default)(x.length === len);
992
+ (0, import_assert11.ok)(x.length === len);
1000
993
  bc.reserve(x.length * U64_BYTE_COUNT);
1001
994
  for (let i = 0; i < x.length; i++)
1002
995
  encodeU64(bc, x[i]);
@@ -1109,4 +1102,3 @@ module.exports = __toCommonJS(src_exports);
1109
1102
  encodeUintSafe,
1110
1103
  encodeVoid
1111
1104
  });
1112
- //# sourceMappingURL=index.cjs.map