@bare-ts/lib 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +21 -201
  2. package/dist/codec/data.d.ts +1 -1
  3. package/dist/codec/data.js +10 -12
  4. package/dist/codec/float-array.d.ts +9 -9
  5. package/dist/codec/float-array.js +51 -43
  6. package/dist/codec/i16-array.d.ts +5 -5
  7. package/dist/codec/i16-array.js +34 -26
  8. package/dist/codec/i32-array.d.ts +5 -5
  9. package/dist/codec/i32-array.js +34 -26
  10. package/dist/codec/i64-array.d.ts +5 -5
  11. package/dist/codec/i64-array.js +34 -26
  12. package/dist/codec/i8-array.d.ts +1 -1
  13. package/dist/codec/i8-array.js +12 -14
  14. package/dist/codec/primitive.d.ts +3 -1
  15. package/dist/codec/primitive.js +225 -147
  16. package/dist/codec/string.d.ts +1 -1
  17. package/dist/codec/string.js +45 -31
  18. package/dist/codec/u16-array.d.ts +5 -5
  19. package/dist/codec/u16-array.js +34 -26
  20. package/dist/codec/u32-array.d.ts +5 -5
  21. package/dist/codec/u32-array.js +34 -26
  22. package/dist/codec/u64-array.d.ts +5 -5
  23. package/dist/codec/u64-array.js +34 -26
  24. package/dist/codec/u8-array.d.ts +7 -1
  25. package/dist/codec/u8-array.js +22 -17
  26. package/dist/codec/u8-clamped-array.d.ts +1 -1
  27. package/dist/codec/u8-clamped-array.js +13 -15
  28. package/dist/core/bare-error.d.ts +4 -1
  29. package/dist/core/bare-error.js +4 -8
  30. package/dist/core/byte-cursor.d.ts +22 -28
  31. package/dist/core/byte-cursor.js +33 -29
  32. package/dist/core/config.d.ts +3 -7
  33. package/dist/core/config.js +14 -20
  34. package/dist/env/dev.d.ts +1 -0
  35. package/dist/env/dev.development.d.ts +1 -0
  36. package/dist/env/dev.development.js +2 -0
  37. package/dist/env/dev.js +2 -0
  38. package/dist/env/dev.node.d.ts +1 -0
  39. package/dist/env/dev.node.js +2 -0
  40. package/dist/index.cjs +504 -286
  41. package/dist/index.d.cts +16 -0
  42. package/dist/index.d.ts +16 -2
  43. package/dist/index.js +17 -4
  44. package/dist/util/assert.d.ts +6 -8
  45. package/dist/util/assert.js +10 -13
  46. package/dist/util/constants.d.ts +4 -7
  47. package/dist/util/constants.js +11 -30
  48. package/dist/util/validator.d.ts +1 -1
  49. package/dist/util/validator.js +12 -24
  50. package/package.json +31 -24
  51. package/dist/codec/index.d.ts +0 -13
  52. package/dist/codec/index.js +0 -15
  53. package/dist/core/index.d.ts +0 -3
  54. package/dist/core/index.js +0 -5
  55. package/dist/util/util.d.ts +0 -1
  56. package/dist/util/util.js +0 -6
@@ -1,4 +1,4 @@
1
- import type { ByteCursor } from "../core/index.js";
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
2
  export declare function readString(bc: ByteCursor): string;
3
3
  export declare function writeString(bc: ByteCursor, x: string): void;
4
4
  export declare function readFixedString(bc: ByteCursor, byteLen: number): string;
@@ -1,44 +1,54 @@
1
- // src/codec/string.ts
1
+ "use strict";
2
2
  import { BareError } from "../core/bare-error.js";
3
- import { INVALID_UTF8_STRING } from "../util/constants.js";
4
- import { readUintSafe, writeUintSafe } from "./primitive.js";
5
- import { writeU8FixedArray } from "./u8-array.js";
6
- function readString(bc) {
7
- return readFixedString(bc, readUintSafe(bc));
3
+ import { check, reserve } from "../core/byte-cursor.js";
4
+ import { DEV, assert } from "../util/assert.js";
5
+ import {
6
+ INVALID_UTF8_STRING,
7
+ TEXT_DECODER_THRESHOLD,
8
+ TEXT_ENCODER_THRESHOLD
9
+ } from "../util/constants.js";
10
+ import { isU32 } from "../util/validator.js";
11
+ import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
12
+ import { readUnsafeU8FixedArray, writeU8FixedArray } from "./u8-array.js";
13
+ export function readString(bc) {
14
+ return readFixedString(bc, readUintSafe32(bc));
8
15
  }
9
- function writeString(bc, x) {
10
- if (x.length < bc.config.textEncoderThreshold) {
16
+ export function writeString(bc, x) {
17
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
11
18
  const byteLen = utf8ByteLength(x);
12
- writeUintSafe(bc, byteLen);
13
- bc.reserve(byteLen);
19
+ writeUintSafe32(bc, byteLen);
20
+ reserve(bc, byteLen);
14
21
  writeUtf8Js(bc, x);
15
22
  } else {
16
23
  const strBytes = UTF8_ENCODER.encode(x);
17
- writeUintSafe(bc, strBytes.length);
24
+ writeUintSafe32(bc, strBytes.length);
18
25
  writeU8FixedArray(bc, strBytes);
19
26
  }
20
27
  }
21
- function readFixedString(bc, byteLen) {
22
- if (byteLen < bc.config.textDecoderThreshold) {
28
+ export function readFixedString(bc, byteLen) {
29
+ if (DEV) {
30
+ assert(isU32(byteLen));
31
+ }
32
+ if (byteLen < TEXT_DECODER_THRESHOLD) {
23
33
  return readUtf8Js(bc, byteLen);
24
34
  }
25
35
  try {
26
- return UTF8_DECODER.decode(bc.read(byteLen));
27
- } catch (cause) {
36
+ return UTF8_DECODER.decode(readUnsafeU8FixedArray(bc, byteLen));
37
+ } catch (_cause) {
28
38
  throw new BareError(bc.offset, INVALID_UTF8_STRING);
29
39
  }
30
40
  }
31
- function writeFixedString(bc, x) {
32
- if (x.length < bc.config.textEncoderThreshold) {
41
+ export function writeFixedString(bc, x) {
42
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
33
43
  const byteLen = utf8ByteLength(x);
34
- bc.reserve(byteLen);
44
+ reserve(bc, byteLen);
35
45
  writeUtf8Js(bc, x);
36
46
  } else {
37
47
  writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
38
48
  }
39
49
  }
40
50
  function readUtf8Js(bc, byteLen) {
41
- bc.check(byteLen);
51
+ check(bc, byteLen);
42
52
  let result = "";
43
53
  const bytes = bc.bytes;
44
54
  let offset = bc.offset;
@@ -51,18 +61,29 @@ function readUtf8Js(bc, byteLen) {
51
61
  if (offset < upperOffset && codePoint < 224) {
52
62
  const byte2 = bytes[offset++];
53
63
  codePoint = (byte1 & 31) << 6 | byte2 & 63;
54
- malformed = codePoint >> 7 === 0 || byte1 >> 5 !== 6 || byte2 >> 6 !== 2;
64
+ malformed = codePoint >> 7 === 0 || // non-canonical char
65
+ byte1 >> 5 !== 6 || // invalid tag
66
+ byte2 >> 6 !== 2;
55
67
  } else if (offset + 1 < upperOffset && codePoint < 240) {
56
68
  const byte2 = bytes[offset++];
57
69
  const byte3 = bytes[offset++];
58
70
  codePoint = (byte1 & 15) << 12 | (byte2 & 63) << 6 | byte3 & 63;
59
- malformed = codePoint >> 11 === 0 || codePoint >> 11 === 27 || byte1 >> 4 !== 14 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2;
71
+ malformed = codePoint >> 11 === 0 || // non-canonical char or missing data
72
+ codePoint >> 11 === 27 || // surrogate char (0xD800 <= codePoint <= 0xDFFF)
73
+ byte1 >> 4 !== 14 || // invalid tag
74
+ byte2 >> 6 !== 2 || // invalid tag
75
+ byte3 >> 6 !== 2;
60
76
  } else if (offset + 2 < upperOffset) {
61
77
  const byte2 = bytes[offset++];
62
78
  const byte3 = bytes[offset++];
63
79
  const byte4 = bytes[offset++];
64
80
  codePoint = (byte1 & 7) << 18 | (byte2 & 63) << 12 | (byte3 & 63) << 6 | byte4 & 63;
65
- malformed = codePoint >> 16 === 0 || codePoint > 1114111 || byte1 >> 3 !== 30 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2 || byte4 >> 6 !== 2;
81
+ malformed = codePoint >> 16 === 0 || // non-canonical char or missing data
82
+ codePoint > 1114111 || // too large code point
83
+ byte1 >> 3 !== 30 || // invalid tag
84
+ byte2 >> 6 !== 2 || // invalid tag
85
+ byte3 >> 6 !== 2 || // invalid tag
86
+ byte4 >> 6 !== 2;
66
87
  }
67
88
  if (malformed) {
68
89
  throw new BareError(bc.offset, INVALID_UTF8_STRING);
@@ -115,12 +136,5 @@ function utf8ByteLength(s) {
115
136
  }
116
137
  return result;
117
138
  }
118
- var UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
119
- var UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
120
- export {
121
- readFixedString,
122
- readString,
123
- writeFixedString,
124
- writeString
125
- };
126
- //# sourceMappingURL=string.js.map
139
+ const UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
140
+ const UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
@@ -1,8 +1,8 @@
1
- import type { ByteCursor } from "../core/index.js";
2
- export declare const readU16FixedArray: typeof readU16FixedArrayLE;
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
+ export declare const readU16FixedArray: typeof readU16FixedArrayLe;
3
3
  export declare function readU16Array(bc: ByteCursor): Uint16Array;
4
- declare function readU16FixedArrayLE(bc: ByteCursor, len: number): Uint16Array;
5
- export declare const writeU16FixedArray: typeof writeU16FixedArrayLE;
4
+ declare function readU16FixedArrayLe(bc: ByteCursor, len: number): Uint16Array;
5
+ export declare const writeU16FixedArray: typeof writeU16FixedArrayLe;
6
6
  export declare function writeU16Array(bc: ByteCursor, x: Uint16Array): void;
7
- declare function writeU16FixedArrayLE(bc: ByteCursor, x: Uint16Array): void;
7
+ declare function writeU16FixedArrayLe(bc: ByteCursor, x: Uint16Array): void;
8
8
  export {};
@@ -1,43 +1,51 @@
1
- // src/codec/u16-array.ts
2
- import { U16_BYTE_COUNT } from "../util/constants.js";
3
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { DEV, assert } from "../util/assert.js";
4
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
+ import { isU32 } from "../util/validator.js";
4
6
  import { readFixedData } from "./data.js";
5
- import { readU16, readUintSafe, writeU16, writeUintSafe } from "./primitive.js";
7
+ import {
8
+ readU16,
9
+ readUintSafe32,
10
+ writeU16,
11
+ writeUintSafe32
12
+ } from "./primitive.js";
6
13
  import { writeU8FixedArray } from "./u8-array.js";
7
- var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
8
- function readU16Array(bc) {
9
- return readU16FixedArray(bc, readUintSafe(bc));
14
+ export const readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLe : readU16FixedArrayBe;
15
+ export function readU16Array(bc) {
16
+ return readU16FixedArray(bc, readUintSafe32(bc));
10
17
  }
11
- function readU16FixedArrayLE(bc, len) {
12
- const byteCount = len * U16_BYTE_COUNT;
18
+ function readU16FixedArrayLe(bc, len) {
19
+ if (DEV) {
20
+ assert(isU32(len));
21
+ }
22
+ const byteCount = len * 2;
13
23
  return new Uint16Array(readFixedData(bc, byteCount));
14
24
  }
15
- function readU16FixedArrayBE(bc, len) {
16
- bc.check(len * U16_BYTE_COUNT);
25
+ function readU16FixedArrayBe(bc, len) {
26
+ if (DEV) {
27
+ assert(isU32(len));
28
+ }
29
+ check(bc, len * 2);
17
30
  const result = new Uint16Array(len);
18
- for (let i = 0; i < len; i++)
31
+ for (let i = 0; i < len; i++) {
19
32
  result[i] = readU16(bc);
33
+ }
20
34
  return result;
21
35
  }
22
- var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLE : writeU16FixedArrayBE;
23
- function writeU16Array(bc, x) {
24
- writeUintSafe(bc, x.length);
36
+ export const writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLe : writeU16FixedArrayBe;
37
+ export function writeU16Array(bc, x) {
38
+ writeUintSafe32(bc, x.length);
25
39
  if (x.length !== 0) {
26
40
  writeU16FixedArray(bc, x);
27
41
  }
28
42
  }
29
- function writeU16FixedArrayLE(bc, x) {
43
+ function writeU16FixedArrayLe(bc, x) {
30
44
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
31
45
  }
32
- function writeU16FixedArrayBE(bc, x) {
33
- bc.reserve(x.length * U16_BYTE_COUNT);
34
- for (let i = 0; i < x.length; i++)
46
+ function writeU16FixedArrayBe(bc, x) {
47
+ reserve(bc, x.length * 2);
48
+ for (let i = 0; i < x.length; i++) {
35
49
  writeU16(bc, x[i]);
50
+ }
36
51
  }
37
- export {
38
- readU16Array,
39
- readU16FixedArray,
40
- writeU16Array,
41
- writeU16FixedArray
42
- };
43
- //# sourceMappingURL=u16-array.js.map
@@ -1,8 +1,8 @@
1
- import type { ByteCursor } from "../core/index.js";
2
- export declare const readU32FixedArray: typeof readU32FixedArrayLE;
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
+ export declare const readU32FixedArray: typeof readU32FixedArrayLe;
3
3
  export declare function readU32Array(bc: ByteCursor): Uint32Array;
4
- declare function readU32FixedArrayLE(bc: ByteCursor, len: number): Uint32Array;
5
- export declare const writeU32FixedArray: typeof writeU32FixedArrayLE;
4
+ declare function readU32FixedArrayLe(bc: ByteCursor, len: number): Uint32Array;
5
+ export declare const writeU32FixedArray: typeof writeU32FixedArrayLe;
6
6
  export declare function writeU32Array(bc: ByteCursor, x: Uint32Array): void;
7
- declare function writeU32FixedArrayLE(bc: ByteCursor, x: Uint32Array): void;
7
+ declare function writeU32FixedArrayLe(bc: ByteCursor, x: Uint32Array): void;
8
8
  export {};
@@ -1,43 +1,51 @@
1
- // src/codec/u32-array.ts
2
- import { U32_BYTE_COUNT } from "../util/constants.js";
3
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { DEV, assert } from "../util/assert.js";
4
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
+ import { isU32 } from "../util/validator.js";
4
6
  import { readFixedData } from "./data.js";
5
- import { readU32, readUintSafe, writeU32, writeUintSafe } from "./primitive.js";
7
+ import {
8
+ readU32,
9
+ readUintSafe32,
10
+ writeU32,
11
+ writeUintSafe32
12
+ } from "./primitive.js";
6
13
  import { writeU8FixedArray } from "./u8-array.js";
7
- var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
8
- function readU32Array(bc) {
9
- return readU32FixedArray(bc, readUintSafe(bc));
14
+ export const readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLe : readU32FixedArrayBe;
15
+ export function readU32Array(bc) {
16
+ return readU32FixedArray(bc, readUintSafe32(bc));
10
17
  }
11
- function readU32FixedArrayLE(bc, len) {
12
- const byteCount = len * U32_BYTE_COUNT;
18
+ function readU32FixedArrayLe(bc, len) {
19
+ if (DEV) {
20
+ assert(isU32(len));
21
+ }
22
+ const byteCount = len * 4;
13
23
  return new Uint32Array(readFixedData(bc, byteCount));
14
24
  }
15
- function readU32FixedArrayBE(bc, len) {
16
- bc.check(len * U32_BYTE_COUNT);
25
+ function readU32FixedArrayBe(bc, len) {
26
+ if (DEV) {
27
+ assert(isU32(len));
28
+ }
29
+ check(bc, len * 4);
17
30
  const result = new Uint32Array(len);
18
- for (let i = 0; i < len; i++)
31
+ for (let i = 0; i < len; i++) {
19
32
  result[i] = readU32(bc);
33
+ }
20
34
  return result;
21
35
  }
22
- var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLE : writeU32FixedArrayBE;
23
- function writeU32Array(bc, x) {
24
- writeUintSafe(bc, x.length);
36
+ export const writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLe : writeU32FixedArrayBe;
37
+ export function writeU32Array(bc, x) {
38
+ writeUintSafe32(bc, x.length);
25
39
  if (x.length !== 0) {
26
40
  writeU32FixedArray(bc, x);
27
41
  }
28
42
  }
29
- function writeU32FixedArrayLE(bc, x) {
43
+ function writeU32FixedArrayLe(bc, x) {
30
44
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
31
45
  }
32
- function writeU32FixedArrayBE(bc, x) {
33
- bc.reserve(x.length * U32_BYTE_COUNT);
34
- for (let i = 0; i < x.length; i++)
46
+ function writeU32FixedArrayBe(bc, x) {
47
+ reserve(bc, x.length * 4);
48
+ for (let i = 0; i < x.length; i++) {
35
49
  writeU32(bc, x[i]);
50
+ }
36
51
  }
37
- export {
38
- readU32Array,
39
- readU32FixedArray,
40
- writeU32Array,
41
- writeU32FixedArray
42
- };
43
- //# sourceMappingURL=u32-array.js.map
@@ -1,8 +1,8 @@
1
- import type { ByteCursor } from "../core/index.js";
2
- export declare const readU64FixedArray: typeof readU64FixedArrayLE;
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
+ export declare const readU64FixedArray: typeof readU64FixedArrayLe;
3
3
  export declare function readU64Array(bc: ByteCursor): BigUint64Array;
4
- declare function readU64FixedArrayLE(bc: ByteCursor, len: number): BigUint64Array;
5
- export declare const writeU64FixedArray: typeof writeU64FixedArrayLE;
4
+ declare function readU64FixedArrayLe(bc: ByteCursor, len: number): BigUint64Array;
5
+ export declare const writeU64FixedArray: typeof writeU64FixedArrayLe;
6
6
  export declare function writeU64Array(bc: ByteCursor, x: BigUint64Array): void;
7
- declare function writeU64FixedArrayLE(bc: ByteCursor, x: BigUint64Array): void;
7
+ declare function writeU64FixedArrayLe(bc: ByteCursor, x: BigUint64Array): void;
8
8
  export {};
@@ -1,43 +1,51 @@
1
- // src/codec/u64-array.ts
2
- import { U64_BYTE_COUNT } from "../util/constants.js";
3
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { DEV, assert } from "../util/assert.js";
4
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
+ import { isU32 } from "../util/validator.js";
4
6
  import { readFixedData } from "./data.js";
5
- import { readU64, readUintSafe, writeU64, writeUintSafe } from "./primitive.js";
7
+ import {
8
+ readU64,
9
+ readUintSafe32,
10
+ writeU64,
11
+ writeUintSafe32
12
+ } from "./primitive.js";
6
13
  import { writeU8FixedArray } from "./u8-array.js";
7
- var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
8
- function readU64Array(bc) {
9
- return readU64FixedArray(bc, readUintSafe(bc));
14
+ export const readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLe : readU64FixedArrayBe;
15
+ export function readU64Array(bc) {
16
+ return readU64FixedArray(bc, readUintSafe32(bc));
10
17
  }
11
- function readU64FixedArrayLE(bc, len) {
12
- const byteCount = len * U64_BYTE_COUNT;
18
+ function readU64FixedArrayLe(bc, len) {
19
+ if (DEV) {
20
+ assert(isU32(len));
21
+ }
22
+ const byteCount = len * 8;
13
23
  return new BigUint64Array(readFixedData(bc, byteCount));
14
24
  }
15
- function readU64FixedArrayBE(bc, len) {
16
- bc.check(len * U64_BYTE_COUNT);
25
+ function readU64FixedArrayBe(bc, len) {
26
+ if (DEV) {
27
+ assert(isU32(len));
28
+ }
29
+ check(bc, len * 8);
17
30
  const result = new BigUint64Array(len);
18
- for (let i = 0; i < len; i++)
31
+ for (let i = 0; i < len; i++) {
19
32
  result[i] = readU64(bc);
33
+ }
20
34
  return result;
21
35
  }
22
- var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLE : writeU64FixedArrayBE;
23
- function writeU64Array(bc, x) {
24
- writeUintSafe(bc, x.length);
36
+ export const writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLe : writeU64FixedArrayBe;
37
+ export function writeU64Array(bc, x) {
38
+ writeUintSafe32(bc, x.length);
25
39
  if (x.length !== 0) {
26
40
  writeU64FixedArray(bc, x);
27
41
  }
28
42
  }
29
- function writeU64FixedArrayLE(bc, x) {
43
+ function writeU64FixedArrayLe(bc, x) {
30
44
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
31
45
  }
32
- function writeU64FixedArrayBE(bc, x) {
33
- bc.reserve(x.length * U64_BYTE_COUNT);
34
- for (let i = 0; i < x.length; i++)
46
+ function writeU64FixedArrayBe(bc, x) {
47
+ reserve(bc, x.length * 8);
48
+ for (let i = 0; i < x.length; i++) {
35
49
  writeU64(bc, x[i]);
50
+ }
36
51
  }
37
- export {
38
- readU64Array,
39
- readU64FixedArray,
40
- writeU64Array,
41
- writeU64FixedArray
42
- };
43
- //# sourceMappingURL=u64-array.js.map
@@ -1,5 +1,11 @@
1
- import type { ByteCursor } from "../core/index.js";
1
+ import { type ByteCursor } from "../core/byte-cursor.js";
2
2
  export declare function readU8Array(bc: ByteCursor): Uint8Array;
3
3
  export declare function writeU8Array(bc: ByteCursor, x: Uint8Array): void;
4
4
  export declare function readU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
5
5
  export declare function writeU8FixedArray(bc: ByteCursor, x: Uint8Array): void;
6
+ /**
7
+ * Advance `bc` by `len` bytes and return a view of the read bytes.
8
+ *
9
+ * WARNING: The returned array should not be modified.
10
+ */
11
+ export declare function readUnsafeU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
@@ -1,27 +1,32 @@
1
- // src/codec/u8-array.ts
2
- import { readUintSafe, writeUintSafe } from "./primitive.js";
3
- function readU8Array(bc) {
4
- return readU8FixedArray(bc, readUintSafe(bc));
1
+ "use strict";
2
+ import { check, reserve } from "../core/byte-cursor.js";
3
+ import { DEV, assert } from "../util/assert.js";
4
+ import { isU32 } from "../util/validator.js";
5
+ import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
6
+ export function readU8Array(bc) {
7
+ return readU8FixedArray(bc, readUintSafe32(bc));
5
8
  }
6
- function writeU8Array(bc, x) {
7
- writeUintSafe(bc, x.length);
9
+ export function writeU8Array(bc, x) {
10
+ writeUintSafe32(bc, x.length);
8
11
  writeU8FixedArray(bc, x);
9
12
  }
10
- function readU8FixedArray(bc, len) {
11
- return bc.read(len).slice();
13
+ export function readU8FixedArray(bc, len) {
14
+ return readUnsafeU8FixedArray(bc, len).slice();
12
15
  }
13
- function writeU8FixedArray(bc, x) {
16
+ export function writeU8FixedArray(bc, x) {
14
17
  const len = x.length;
15
18
  if (len !== 0) {
16
- bc.reserve(len);
19
+ reserve(bc, len);
17
20
  bc.bytes.set(x, bc.offset);
18
21
  bc.offset += len;
19
22
  }
20
23
  }
21
- export {
22
- readU8Array,
23
- readU8FixedArray,
24
- writeU8Array,
25
- writeU8FixedArray
26
- };
27
- //# sourceMappingURL=u8-array.js.map
24
+ export function readUnsafeU8FixedArray(bc, len) {
25
+ if (DEV) {
26
+ assert(isU32(len));
27
+ }
28
+ check(bc, len);
29
+ const offset = bc.offset;
30
+ bc.offset += len;
31
+ return bc.bytes.subarray(offset, offset + len);
32
+ }
@@ -1,4 +1,4 @@
1
- import type { ByteCursor } from "../core/index.js";
1
+ import type { ByteCursor } from "../core/byte-cursor.js";
2
2
  export declare function readU8ClampedArray(bc: ByteCursor): Uint8ClampedArray;
3
3
  export declare function writeU8ClampedArray(bc: ByteCursor, x: Uint8ClampedArray): void;
4
4
  export declare function readU8ClampedFixedArray(bc: ByteCursor, len: number): Uint8ClampedArray;
@@ -1,24 +1,22 @@
1
- // src/codec/u8-clamped-array.ts
1
+ "use strict";
2
+ import { DEV, assert } from "../util/assert.js";
3
+ import { isU32 } from "../util/validator.js";
2
4
  import { readFixedData } from "./data.js";
3
- import { readUintSafe, writeUintSafe } from "./primitive.js";
5
+ import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
4
6
  import { writeU8FixedArray } from "./u8-array.js";
5
- function readU8ClampedArray(bc) {
6
- return readU8ClampedFixedArray(bc, readUintSafe(bc));
7
+ export function readU8ClampedArray(bc) {
8
+ return readU8ClampedFixedArray(bc, readUintSafe32(bc));
7
9
  }
8
- function writeU8ClampedArray(bc, x) {
9
- writeUintSafe(bc, x.length);
10
+ export function writeU8ClampedArray(bc, x) {
11
+ writeUintSafe32(bc, x.length);
10
12
  writeU8ClampedFixedArray(bc, x);
11
13
  }
12
- function readU8ClampedFixedArray(bc, len) {
14
+ export function readU8ClampedFixedArray(bc, len) {
15
+ if (DEV) {
16
+ assert(isU32(len));
17
+ }
13
18
  return new Uint8ClampedArray(readFixedData(bc, len));
14
19
  }
15
- function writeU8ClampedFixedArray(bc, x) {
20
+ export function writeU8ClampedFixedArray(bc, x) {
16
21
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
17
22
  }
18
- export {
19
- readU8ClampedArray,
20
- readU8ClampedFixedArray,
21
- writeU8ClampedArray,
22
- writeU8ClampedFixedArray
23
- };
24
- //# sourceMappingURL=u8-clamped-array.js.map
@@ -1,5 +1,8 @@
1
+ /**
2
+ * @sealed
3
+ */
1
4
  export declare class BareError extends Error {
2
- readonly name: "BareError";
5
+ name: string;
3
6
  readonly cause: unknown;
4
7
  readonly issue: string;
5
8
  readonly offset: number;
@@ -1,14 +1,10 @@
1
- // src/core/bare-error.ts
2
- var BareError = class extends Error {
1
+ "use strict";
2
+ export class BareError extends Error {
3
3
  constructor(offset, issue, opts) {
4
4
  super(`(byte:${offset}) ${issue}`);
5
5
  this.name = "BareError";
6
6
  this.issue = issue;
7
7
  this.offset = offset;
8
- this.cause = opts == null ? void 0 : opts.cause;
8
+ this.cause = opts?.cause;
9
9
  }
10
- };
11
- export {
12
- BareError
13
- };
14
- //# sourceMappingURL=bare-error.js.map
10
+ }
@@ -1,16 +1,20 @@
1
1
  import type { Config } from "./config.js";
2
2
  /**
3
- * @invariant bytes.buffer === view.buffer
4
- * @invariant bytes.byteOffset === view.byteOffset
5
- * @invariant bytes.byteLength === view.byteLength
6
- * @invariant 0 <= offset <= bytes.byteLength
7
- * @invariant bytes.byteLength <= config.maxBufferLength
3
+ * @invariant `bytes.buffer === view.buffer`
4
+ * @invariant `bytes.byteOffset === view.byteOffset`
5
+ * @invariant `bytes.byteLength === view.byteLength`
6
+ * @invariant `0 <= offset <= bytes.byteLength`
7
+ * @invariant `bytes.byteLength <= config.maxBufferLength`
8
8
  *
9
+ * ```txt
9
10
  * | {bytes,view}.buffer |
10
11
  * | bytes |
11
12
  * | view |
12
13
  * |<------ offset ------>|
13
14
  * |<----------- config.maxBufferLength ------------>|
15
+ * ```
16
+ *
17
+ * @sealed
14
18
  */
15
19
  export declare class ByteCursor {
16
20
  bytes: Uint8Array;
@@ -21,29 +25,19 @@ export declare class ByteCursor {
21
25
  offset: number;
22
26
  view: DataView;
23
27
  /**
24
- * @param bytes read and/or write buffer
25
- * @param config runtime configuration
26
- * @throw BareError when the buffer exceeds the maximum allowed length
27
- * `config.maxBufferLength`
28
+ * @throws {BareError} Buffer exceeds `config.maxBufferLength`
28
29
  */
29
30
  constructor(bytes: Uint8Array, config: Config);
30
- /**
31
- * @param min number of needed bytes
32
- * @throw BareError when there is not enough bytes
33
- */
34
- check(min: number): void;
35
- /**
36
- * @param min number of bytes to reserve
37
- * @throw BareError when the buffer exceeds the maximum allowed length
38
- * `config.maxBufferLength`
39
- */
40
- reserve(min: number): void;
41
- /**
42
- * Advance cursor by {@code len} bytes and return a view of the read bytes.
43
- * The returned array should not be modified.
44
- *
45
- * @param len number of bytes to read
46
- * @returns read bytes
47
- */
48
- read(len: number): Uint8Array;
49
31
  }
32
+ /**
33
+ * Check that `min` number of bytes are available.
34
+ *
35
+ * @throws {BareError} bytes are missing.
36
+ */
37
+ export declare function check(bc: ByteCursor, min: number): void;
38
+ /**
39
+ * Reserve `min` number of bytes.
40
+ *
41
+ * @throws {BareError} Buffer exceeds `config.maxBufferLength`.
42
+ */
43
+ export declare function reserve(bc: ByteCursor, min: number): void;