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