@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,43 +1,51 @@
1
- // src/codec/i64-array.ts
2
- import { I64_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 { readI64, readUintSafe, writeI64, writeUintSafe } from "./primitive.js";
7
+ import {
8
+ readI64,
9
+ readUintSafe32,
10
+ writeI64,
11
+ writeUintSafe32
12
+ } from "./primitive.js";
6
13
  import { writeU8FixedArray } from "./u8-array.js";
7
- var readI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI64FixedArrayLE : readI64FixedArrayBE;
8
- function readI64Array(bc) {
9
- return readI64FixedArray(bc, readUintSafe(bc));
14
+ export const readI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI64FixedArrayLe : readI64FixedArrayBe;
15
+ export function readI64Array(bc) {
16
+ return readI64FixedArray(bc, readUintSafe32(bc));
10
17
  }
11
- function readI64FixedArrayLE(bc, len) {
12
- const byteCount = len * I64_BYTE_COUNT;
18
+ function readI64FixedArrayLe(bc, len) {
19
+ if (DEV) {
20
+ assert(isU32(len));
21
+ }
22
+ const byteCount = len * 8;
13
23
  return new BigInt64Array(readFixedData(bc, byteCount));
14
24
  }
15
- function readI64FixedArrayBE(bc, len) {
16
- bc.check(len * I64_BYTE_COUNT);
25
+ function readI64FixedArrayBe(bc, len) {
26
+ if (DEV) {
27
+ assert(isU32(len));
28
+ }
29
+ check(bc, len * 8);
17
30
  const result = new BigInt64Array(len);
18
- for (let i = 0; i < len; i++)
31
+ for (let i = 0; i < len; i++) {
19
32
  result[i] = readI64(bc);
33
+ }
20
34
  return result;
21
35
  }
22
- var writeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI64FixedArrayLE : writeI64FixedArrayBE;
23
- function writeI64Array(bc, x) {
24
- writeUintSafe(bc, x.length);
36
+ export const writeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI64FixedArrayLe : writeI64FixedArrayBe;
37
+ export function writeI64Array(bc, x) {
38
+ writeUintSafe32(bc, x.length);
25
39
  if (x.length !== 0) {
26
40
  writeI64FixedArray(bc, x);
27
41
  }
28
42
  }
29
- function writeI64FixedArrayLE(bc, x) {
43
+ function writeI64FixedArrayLe(bc, x) {
30
44
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
31
45
  }
32
- function writeI64FixedArrayBE(bc, x) {
33
- bc.reserve(x.length * I64_BYTE_COUNT);
34
- for (let i = 0; i < x.length; i++)
46
+ function writeI64FixedArrayBe(bc, x) {
47
+ reserve(bc, x.length * 8);
48
+ for (let i = 0; i < x.length; i++) {
35
49
  writeI64(bc, x[i]);
50
+ }
36
51
  }
37
- export {
38
- readI64Array,
39
- readI64FixedArray,
40
- writeI64Array,
41
- writeI64FixedArray
42
- };
43
- //# sourceMappingURL=i64-array.js.map
@@ -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 readI8Array(bc: ByteCursor): Int8Array;
3
3
  export declare function writeI8Array(bc: ByteCursor, x: Int8Array): void;
4
4
  export declare function readI8FixedArray(bc: ByteCursor, len: number): Int8Array;
@@ -1,24 +1,22 @@
1
- // src/codec/i8-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 { readUintSafe, writeUintSafe32 } from "./primitive.js";
4
6
  import { writeU8FixedArray } from "./u8-array.js";
5
- function readI8Array(bc) {
7
+ export function readI8Array(bc) {
6
8
  return readI8FixedArray(bc, readUintSafe(bc));
7
9
  }
8
- function writeI8Array(bc, x) {
9
- writeUintSafe(bc, x.length);
10
+ export function writeI8Array(bc, x) {
11
+ writeUintSafe32(bc, x.length);
10
12
  writeI8FixedArray(bc, x);
11
13
  }
12
- function readI8FixedArray(bc, len) {
14
+ export function readI8FixedArray(bc, len) {
15
+ if (DEV) {
16
+ assert(isU32(len));
17
+ }
13
18
  return new Int8Array(readFixedData(bc, len));
14
19
  }
15
- function writeI8FixedArray(bc, x) {
20
+ export function writeI8FixedArray(bc, x) {
16
21
  writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
17
22
  }
18
- export {
19
- readI8Array,
20
- readI8FixedArray,
21
- writeI8Array,
22
- writeI8FixedArray
23
- };
24
- //# sourceMappingURL=i8-array.js.map
@@ -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 readBool(bc: ByteCursor): boolean;
3
3
  export declare function writeBool(bc: ByteCursor, x: boolean): void;
4
4
  export declare function readF32(bc: ByteCursor): number;
@@ -31,5 +31,7 @@ export declare function readU64Safe(bc: ByteCursor): number;
31
31
  export declare function writeU64Safe(bc: ByteCursor, x: number): void;
32
32
  export declare function readUint(bc: ByteCursor): bigint;
33
33
  export declare function writeUint(bc: ByteCursor, x: bigint): void;
34
+ export declare function readUintSafe32(bc: ByteCursor): number;
35
+ export declare function writeUintSafe32(bc: ByteCursor, x: number): void;
34
36
  export declare function readUintSafe(bc: ByteCursor): number;
35
37
  export declare function writeUintSafe(bc: ByteCursor, x: number): void;
@@ -1,25 +1,26 @@
1
- // src/codec/primitive.ts
2
- import { assert } from "../util/assert.js";
3
- import { BareError } from "../core/index.js";
1
+ "use strict";
2
+ import { BareError } from "../core/bare-error.js";
3
+ import { check, reserve } from "../core/byte-cursor.js";
4
+ import { DEV, assert } from "../util/assert.js";
4
5
  import {
5
6
  INT_SAFE_MAX_BYTE_COUNT,
6
7
  NON_CANONICAL_REPRESENTATION,
7
8
  TOO_LARGE_NUMBER,
8
9
  UINT_MAX_BYTE_COUNT,
9
- UINT_SAFE_MAX_BYTE_COUNT
10
+ UINT_SAFE32_MAX_BYTE_COUNT
10
11
  } from "../util/constants.js";
11
12
  import {
13
+ isI8,
12
14
  isI16,
13
15
  isI32,
14
16
  isI64,
15
- isI8,
16
- isSafeU64,
17
+ isU8,
17
18
  isU16,
18
19
  isU32,
19
20
  isU64,
20
- isU8
21
+ isU64Safe
21
22
  } from "../util/validator.js";
22
- function readBool(bc) {
23
+ export function readBool(bc) {
23
24
  const val = readU8(bc);
24
25
  if (val > 1) {
25
26
  bc.offset--;
@@ -27,118 +28,147 @@ function readBool(bc) {
27
28
  }
28
29
  return val !== 0;
29
30
  }
30
- function writeBool(bc, x) {
31
+ export function writeBool(bc, x) {
31
32
  writeU8(bc, x ? 1 : 0);
32
33
  }
33
- function readF32(bc) {
34
- bc.check(4);
34
+ export function readF32(bc) {
35
+ check(bc, 4);
35
36
  const result = bc.view.getFloat32(bc.offset, true);
36
37
  bc.offset += 4;
37
38
  return result;
38
39
  }
39
- function writeF32(bc, x) {
40
- bc.reserve(4);
40
+ export function writeF32(bc, x) {
41
+ reserve(bc, 4);
41
42
  bc.view.setFloat32(bc.offset, x, true);
42
- assert(Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER);
43
+ if (DEV) {
44
+ assert(
45
+ Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON,
46
+ TOO_LARGE_NUMBER
47
+ );
48
+ }
43
49
  bc.offset += 4;
44
50
  }
45
- function readF64(bc) {
46
- bc.check(8);
51
+ export function readF64(bc) {
52
+ check(bc, 8);
47
53
  const result = bc.view.getFloat64(bc.offset, true);
48
54
  bc.offset += 8;
49
55
  return result;
50
56
  }
51
- function writeF64(bc, x) {
52
- bc.reserve(8);
57
+ export function writeF64(bc, x) {
58
+ reserve(bc, 8);
53
59
  bc.view.setFloat64(bc.offset, x, true);
54
60
  bc.offset += 8;
55
61
  }
56
- function readI8(bc) {
57
- bc.check(1);
62
+ export function readI8(bc) {
63
+ check(bc, 1);
58
64
  return bc.view.getInt8(bc.offset++);
59
65
  }
60
- function writeI8(bc, x) {
61
- assert(isI8(x), TOO_LARGE_NUMBER);
62
- bc.reserve(1);
66
+ export function writeI8(bc, x) {
67
+ if (DEV) {
68
+ assert(isI8(x), TOO_LARGE_NUMBER);
69
+ }
70
+ reserve(bc, 1);
63
71
  bc.view.setInt8(bc.offset++, x);
64
72
  }
65
- function readI16(bc) {
66
- bc.check(2);
73
+ export function readI16(bc) {
74
+ check(bc, 2);
67
75
  const result = bc.view.getInt16(bc.offset, true);
68
76
  bc.offset += 2;
69
77
  return result;
70
78
  }
71
- function writeI16(bc, x) {
72
- assert(isI16(x), TOO_LARGE_NUMBER);
73
- bc.reserve(2);
79
+ export function writeI16(bc, x) {
80
+ if (DEV) {
81
+ assert(isI16(x), TOO_LARGE_NUMBER);
82
+ }
83
+ reserve(bc, 2);
74
84
  bc.view.setInt16(bc.offset, x, true);
75
85
  bc.offset += 2;
76
86
  }
77
- function readI32(bc) {
78
- bc.check(4);
87
+ export function readI32(bc) {
88
+ check(bc, 4);
79
89
  const result = bc.view.getInt32(bc.offset, true);
80
90
  bc.offset += 4;
81
91
  return result;
82
92
  }
83
- function writeI32(bc, x) {
84
- assert(isI32(x), TOO_LARGE_NUMBER);
85
- bc.reserve(4);
93
+ export function writeI32(bc, x) {
94
+ if (DEV) {
95
+ assert(isI32(x), TOO_LARGE_NUMBER);
96
+ }
97
+ reserve(bc, 4);
86
98
  bc.view.setInt32(bc.offset, x, true);
87
99
  bc.offset += 4;
88
100
  }
89
- function readI64(bc) {
90
- bc.check(8);
101
+ export function readI64(bc) {
102
+ check(bc, 8);
91
103
  const result = bc.view.getBigInt64(bc.offset, true);
92
104
  bc.offset += 8;
93
105
  return result;
94
106
  }
95
- function writeI64(bc, x) {
96
- assert(isI64(x), TOO_LARGE_NUMBER);
97
- bc.reserve(8);
107
+ export function writeI64(bc, x) {
108
+ if (DEV) {
109
+ assert(isI64(x), TOO_LARGE_NUMBER);
110
+ }
111
+ reserve(bc, 8);
98
112
  bc.view.setBigInt64(bc.offset, x, true);
99
113
  bc.offset += 8;
100
114
  }
101
- function readI64Safe(bc) {
102
- const result = readU32(bc) + readI32(bc) * 4294967296;
115
+ export function readI64Safe(bc) {
116
+ const result = readU32(bc) + readI32(bc) * /* 2**32 */
117
+ 4294967296;
103
118
  if (!Number.isSafeInteger(result)) {
104
119
  bc.offset -= 8;
105
120
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
106
121
  }
107
122
  return result;
108
123
  }
109
- function writeI64Safe(bc, x) {
110
- assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
111
- const lowest32 = x >>> 0;
124
+ export function writeI64Safe(bc, x) {
125
+ if (DEV) {
126
+ assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
127
+ }
128
+ let lowest32 = x >>> 0;
112
129
  writeU32(bc, lowest32);
113
- let highest32 = x / 4294967296 | 0;
130
+ let highest32 = x / /* 2**32 */
131
+ 4294967296 | 0;
114
132
  if (x < 0) {
115
- highest32 = ~Math.abs(highest32) >>> 0;
133
+ highest32 = ~(Math.abs(highest32) & /* 2**21-1 */
134
+ 2097151) >>> 0;
116
135
  if (lowest32 === 0) {
117
- highest32++;
136
+ if (highest32 === 2097151) {
137
+ lowest32 = 1;
138
+ } else {
139
+ highest32++;
140
+ }
118
141
  }
119
142
  }
120
143
  writeU32(bc, highest32);
121
144
  }
122
- function readInt(bc) {
145
+ export function readInt(bc) {
123
146
  const zigZag = readUint(bc);
124
147
  return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
125
148
  }
126
- function writeInt(bc, x) {
127
- assert(isI64(x), TOO_LARGE_NUMBER);
128
- const zigZag = x >> BigInt(63) ^ x << BigInt(1);
129
- writeUint(bc, zigZag);
149
+ export function writeInt(bc, x) {
150
+ const truncated = BigInt.asIntN(64, x);
151
+ if (DEV) {
152
+ assert(truncated === x, TOO_LARGE_NUMBER);
153
+ }
154
+ const zigZag = truncated >> BigInt(63) ^ truncated << BigInt(1);
155
+ writeTruncatedUint(bc, zigZag);
130
156
  }
131
- function readIntSafe(bc) {
157
+ export function readIntSafe(bc) {
132
158
  const firstByte = readU8(bc);
133
159
  let result = (firstByte & 127) >> 1;
134
160
  if (firstByte >= 128) {
135
- let shiftMul = 64;
161
+ let shiftMul = (
162
+ /* 2**6 */
163
+ 64
164
+ );
136
165
  let byteCount = 1;
137
166
  let byte;
138
167
  do {
139
168
  byte = readU8(bc);
140
169
  result += (byte & 127) * shiftMul;
141
- shiftMul *= 128;
170
+ shiftMul *= /* 2**7 */
171
+ 128;
142
172
  byteCount++;
143
173
  } while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
144
174
  if (byte === 0) {
@@ -156,80 +186,102 @@ function readIntSafe(bc) {
156
186
  }
157
187
  return result;
158
188
  }
159
- function writeIntSafe(bc, x) {
160
- assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
189
+ export function writeIntSafe(bc, x) {
161
190
  const sign = x < 0 ? 1 : 0;
162
- if (x < 0) {
163
- x = -(x + 1);
164
- }
165
- const firstByte = (x & 63) << 1 | sign;
166
- x = Math.floor(x / 64);
167
- if (x > 0) {
168
- writeU8(bc, 128 | firstByte);
169
- writeUintSafe(bc, x);
191
+ let zigZag = x < 0 ? -(x + 1) : x;
192
+ let first7Bits = (zigZag & 63) << 1 | sign;
193
+ zigZag = Math.floor(zigZag / /* 2**6 */
194
+ 64);
195
+ if (zigZag > 0) {
196
+ if (!Number.isSafeInteger(x)) {
197
+ if (DEV) {
198
+ assert(false, TOO_LARGE_NUMBER);
199
+ }
200
+ const low = zigZag & 32767;
201
+ const high = (zigZag / 32768 >>> 0) * 32768;
202
+ if (first7Bits === 127 && low === 32767 && high === 4294967295) {
203
+ first7Bits &= ~2;
204
+ }
205
+ zigZag = high + low;
206
+ }
207
+ writeU8(bc, 128 | first7Bits);
208
+ writeUintSafe(bc, zigZag);
170
209
  } else {
171
- writeU8(bc, firstByte);
210
+ writeU8(bc, first7Bits);
172
211
  }
173
212
  }
174
- function readU8(bc) {
175
- bc.check(1);
213
+ export function readU8(bc) {
214
+ check(bc, 1);
176
215
  return bc.bytes[bc.offset++];
177
216
  }
178
- function writeU8(bc, x) {
179
- assert(isU8(x), TOO_LARGE_NUMBER);
180
- bc.reserve(1);
217
+ export function writeU8(bc, x) {
218
+ if (DEV) {
219
+ assert(isU8(x), TOO_LARGE_NUMBER);
220
+ }
221
+ reserve(bc, 1);
181
222
  bc.bytes[bc.offset++] = x;
182
223
  }
183
- function readU16(bc) {
184
- bc.check(2);
224
+ export function readU16(bc) {
225
+ check(bc, 2);
185
226
  const result = bc.view.getUint16(bc.offset, true);
186
227
  bc.offset += 2;
187
228
  return result;
188
229
  }
189
- function writeU16(bc, x) {
190
- assert(isU16(x), TOO_LARGE_NUMBER);
191
- bc.reserve(2);
230
+ export function writeU16(bc, x) {
231
+ if (DEV) {
232
+ assert(isU16(x), TOO_LARGE_NUMBER);
233
+ }
234
+ reserve(bc, 2);
192
235
  bc.view.setUint16(bc.offset, x, true);
193
236
  bc.offset += 2;
194
237
  }
195
- function readU32(bc) {
196
- bc.check(4);
238
+ export function readU32(bc) {
239
+ check(bc, 4);
197
240
  const result = bc.view.getUint32(bc.offset, true);
198
241
  bc.offset += 4;
199
242
  return result;
200
243
  }
201
- function writeU32(bc, x) {
202
- assert(isU32(x), TOO_LARGE_NUMBER);
203
- bc.reserve(4);
244
+ export function writeU32(bc, x) {
245
+ if (DEV) {
246
+ assert(isU32(x), TOO_LARGE_NUMBER);
247
+ }
248
+ reserve(bc, 4);
204
249
  bc.view.setUint32(bc.offset, x, true);
205
250
  bc.offset += 4;
206
251
  }
207
- function readU64(bc) {
208
- bc.check(8);
252
+ export function readU64(bc) {
253
+ check(bc, 8);
209
254
  const result = bc.view.getBigUint64(bc.offset, true);
210
255
  bc.offset += 8;
211
256
  return result;
212
257
  }
213
- function writeU64(bc, x) {
214
- assert(isU64(x), TOO_LARGE_NUMBER);
215
- bc.reserve(8);
258
+ export function writeU64(bc, x) {
259
+ if (DEV) {
260
+ assert(isU64(x), TOO_LARGE_NUMBER);
261
+ }
262
+ reserve(bc, 8);
216
263
  bc.view.setBigUint64(bc.offset, x, true);
217
264
  bc.offset += 8;
218
265
  }
219
- function readU64Safe(bc) {
220
- const result = readU32(bc) + readU32(bc) * 4294967296;
221
- if (!isSafeU64(result)) {
266
+ export function readU64Safe(bc) {
267
+ const result = readU32(bc) + readU32(bc) * /* 2**32 */
268
+ 4294967296;
269
+ if (!isU64Safe(result)) {
222
270
  bc.offset -= 8;
223
271
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
224
272
  }
225
273
  return result;
226
274
  }
227
- function writeU64Safe(bc, x) {
228
- assert(isSafeU64(x), TOO_LARGE_NUMBER);
275
+ export function writeU64Safe(bc, x) {
276
+ if (DEV) {
277
+ assert(isU64Safe(x), TOO_LARGE_NUMBER);
278
+ }
229
279
  writeU32(bc, x >>> 0);
230
- writeU32(bc, x / 4294967296 >>> 0);
280
+ writeU32(bc, x / /* 2**32 */
281
+ 4294967296 & /* 2**21-1 */
282
+ 2097151);
231
283
  }
232
- function readUint(bc) {
284
+ export function readUint(bc) {
233
285
  let low = readU8(bc);
234
286
  if (low >= 128) {
235
287
  low &= 127;
@@ -239,7 +291,8 @@ function readUint(bc) {
239
291
  do {
240
292
  byte = readU8(bc);
241
293
  low += (byte & 127) * shiftMul;
242
- shiftMul *= 128;
294
+ shiftMul *= /* 2**7 */
295
+ 128;
243
296
  byteCount++;
244
297
  } while (byte >= 128 && byteCount < 7);
245
298
  let height = 0;
@@ -247,7 +300,8 @@ function readUint(bc) {
247
300
  while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
248
301
  byte = readU8(bc);
249
302
  height += (byte & 127) * shiftMul;
250
- shiftMul *= 128;
303
+ shiftMul *= /* 2**7 */
304
+ 128;
251
305
  byteCount++;
252
306
  }
253
307
  if (byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
@@ -258,14 +312,21 @@ function readUint(bc) {
258
312
  }
259
313
  return BigInt(low);
260
314
  }
261
- function writeUint(bc, x) {
262
- assert(isU64(x), TOO_LARGE_NUMBER);
315
+ export function writeUint(bc, x) {
316
+ const truncated = BigInt.asUintN(64, x);
317
+ if (DEV) {
318
+ assert(truncated === x, TOO_LARGE_NUMBER);
319
+ }
320
+ writeTruncatedUint(bc, truncated);
321
+ }
322
+ function writeTruncatedUint(bc, x) {
263
323
  let tmp = Number(BigInt.asUintN(7 * 7, x));
264
324
  let rest = Number(x >> BigInt(7 * 7));
265
325
  let byteCount = 0;
266
326
  while (tmp >= 128 || rest !== 0) {
267
327
  writeU8(bc, 128 | tmp & 127);
268
- tmp = Math.floor(tmp / 128);
328
+ tmp = Math.floor(tmp / /* 2**7 */
329
+ 128);
269
330
  byteCount++;
270
331
  if (byteCount === 7) {
271
332
  tmp = rest;
@@ -274,72 +335,89 @@ function writeUint(bc, x) {
274
335
  }
275
336
  writeU8(bc, tmp);
276
337
  }
277
- function readUintSafe(bc) {
338
+ export function readUintSafe32(bc) {
278
339
  let result = readU8(bc);
279
340
  if (result >= 128) {
280
341
  result &= 127;
281
- let shiftMul = 128;
342
+ let shift = 7;
343
+ let byteCount = 1;
344
+ let byte;
345
+ do {
346
+ byte = readU8(bc);
347
+ result += (byte & 127) << shift >>> 0;
348
+ shift += 7;
349
+ byteCount++;
350
+ } while (byte >= 128 && byteCount < UINT_SAFE32_MAX_BYTE_COUNT);
351
+ if (byte === 0) {
352
+ bc.offset -= byteCount - 1;
353
+ throw new BareError(
354
+ bc.offset - byteCount + 1,
355
+ NON_CANONICAL_REPRESENTATION
356
+ );
357
+ }
358
+ if (byteCount === UINT_SAFE32_MAX_BYTE_COUNT && byte > 15) {
359
+ bc.offset -= byteCount - 1;
360
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
361
+ }
362
+ }
363
+ return result;
364
+ }
365
+ export function writeUintSafe32(bc, x) {
366
+ if (DEV) {
367
+ assert(isU32(x), TOO_LARGE_NUMBER);
368
+ }
369
+ let zigZag = x >>> 0;
370
+ while (zigZag >= 128) {
371
+ writeU8(bc, 128 | zigZag & 127);
372
+ zigZag >>>= 7;
373
+ }
374
+ writeU8(bc, zigZag);
375
+ }
376
+ export function readUintSafe(bc) {
377
+ let result = readU8(bc);
378
+ if (result >= 128) {
379
+ result &= 127;
380
+ let shiftMul = (
381
+ /* 2**7 */
382
+ 128
383
+ );
282
384
  let byteCount = 1;
283
385
  let byte;
284
386
  do {
285
387
  byte = readU8(bc);
286
388
  result += (byte & 127) * shiftMul;
287
- shiftMul *= 128;
389
+ shiftMul *= /* 2**7 */
390
+ 128;
288
391
  byteCount++;
289
- } while (byte >= 128 && byteCount < UINT_SAFE_MAX_BYTE_COUNT);
392
+ } while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
290
393
  if (byte === 0) {
291
394
  bc.offset -= byteCount - 1;
292
- throw new BareError(bc.offset - byteCount + 1, NON_CANONICAL_REPRESENTATION);
395
+ throw new BareError(
396
+ bc.offset - byteCount + 1,
397
+ NON_CANONICAL_REPRESENTATION
398
+ );
293
399
  }
294
- if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
400
+ if (byteCount === INT_SAFE_MAX_BYTE_COUNT && byte > 15) {
295
401
  bc.offset -= byteCount - 1;
296
402
  throw new BareError(bc.offset, TOO_LARGE_NUMBER);
297
403
  }
298
404
  }
299
405
  return result;
300
406
  }
301
- function writeUintSafe(bc, x) {
302
- assert(isSafeU64(x), TOO_LARGE_NUMBER);
303
- while (x >= 128) {
304
- writeU8(bc, 128 | x & 127);
305
- x = Math.floor(x / 128);
407
+ export function writeUintSafe(bc, x) {
408
+ if (DEV) {
409
+ assert(isU64Safe(x), TOO_LARGE_NUMBER);
410
+ }
411
+ let byteCount = 1;
412
+ let zigZag = x;
413
+ while (zigZag >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT) {
414
+ writeU8(bc, 128 | zigZag & 127);
415
+ zigZag = Math.floor(zigZag / /* 2**7 */
416
+ 128);
417
+ byteCount++;
306
418
  }
307
- writeU8(bc, x);
308
- }
309
- export {
310
- readBool,
311
- readF32,
312
- readF64,
313
- readI16,
314
- readI32,
315
- readI64,
316
- readI64Safe,
317
- readI8,
318
- readInt,
319
- readIntSafe,
320
- readU16,
321
- readU32,
322
- readU64,
323
- readU64Safe,
324
- readU8,
325
- readUint,
326
- readUintSafe,
327
- writeBool,
328
- writeF32,
329
- writeF64,
330
- writeI16,
331
- writeI32,
332
- writeI64,
333
- writeI64Safe,
334
- writeI8,
335
- writeInt,
336
- writeIntSafe,
337
- writeU16,
338
- writeU32,
339
- writeU64,
340
- writeU64Safe,
341
- writeU8,
342
- writeUint,
343
- writeUintSafe
344
- };
345
- //# sourceMappingURL=primitive.js.map
419
+ if (byteCount === INT_SAFE_MAX_BYTE_COUNT) {
420
+ zigZag &= 15;
421
+ }
422
+ writeU8(bc, zigZag);
423
+ }