@bare-ts/lib 0.5.0 → 0.6.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 (62) hide show
  1. package/README.md +3 -3
  2. package/dist/codec/data.d.ts +1 -1
  3. package/dist/codec/data.js +10 -14
  4. package/dist/codec/f32-array.d.ts +5 -0
  5. package/dist/codec/f32-array.js +51 -0
  6. package/dist/codec/f64-array.d.ts +5 -0
  7. package/dist/codec/f64-array.js +51 -0
  8. package/dist/codec/{primitive.d.ts → fixed-primitive.d.ts} +1 -11
  9. package/dist/codec/fixed-primitive.js +197 -0
  10. package/dist/codec/i16-array.d.ts +1 -1
  11. package/dist/codec/i16-array.js +31 -32
  12. package/dist/codec/i32-array.d.ts +1 -1
  13. package/dist/codec/i32-array.js +31 -32
  14. package/dist/codec/i64-array.d.ts +1 -1
  15. package/dist/codec/i64-array.js +31 -32
  16. package/dist/codec/i8-array.d.ts +1 -1
  17. package/dist/codec/i8-array.js +11 -10
  18. package/dist/codec/int.d.ts +5 -0
  19. package/dist/codec/int.js +78 -0
  20. package/dist/codec/string.d.ts +1 -1
  21. package/dist/codec/string.js +143 -114
  22. package/dist/codec/u16-array.d.ts +1 -1
  23. package/dist/codec/u16-array.js +31 -32
  24. package/dist/codec/u32-array.d.ts +1 -1
  25. package/dist/codec/u32-array.js +31 -32
  26. package/dist/codec/u64-array.d.ts +1 -1
  27. package/dist/codec/u64-array.js +31 -32
  28. package/dist/codec/u8-array.d.ts +1 -1
  29. package/dist/codec/u8-array.js +25 -19
  30. package/dist/codec/u8-clamped-array.d.ts +1 -1
  31. package/dist/codec/u8-clamped-array.js +11 -10
  32. package/dist/codec/uint.d.ts +8 -0
  33. package/dist/codec/uint.js +138 -0
  34. package/dist/core/bare-error.js +12 -8
  35. package/dist/core/byte-cursor.d.ts +1 -1
  36. package/dist/core/byte-cursor.js +102 -30
  37. package/dist/core/config.js +12 -17
  38. package/dist/index.cjs +147 -90
  39. package/dist/index.d.cts +21 -16
  40. package/dist/index.d.ts +21 -16
  41. package/dist/index.js +9 -3
  42. package/dist/util/assert.d.ts +10 -1
  43. package/dist/util/assert.js +24 -13
  44. package/dist/util/constants.js +6 -2
  45. package/dist/util/validator.d.ts +27 -0
  46. package/dist/util/validator.js +38 -10
  47. package/imports/dev.d.ts +4 -0
  48. package/imports/dev.development.d.ts +4 -0
  49. package/imports/dev.development.js +4 -0
  50. package/imports/dev.js +4 -0
  51. package/imports/dev.node.d.ts +4 -0
  52. package/imports/dev.node.js +4 -0
  53. package/package.json +20 -16
  54. package/dist/codec/float-array.d.ts +0 -9
  55. package/dist/codec/float-array.js +0 -93
  56. package/dist/codec/primitive.js +0 -423
  57. package/dist/env/dev.d.ts +0 -1
  58. package/dist/env/dev.development.d.ts +0 -1
  59. package/dist/env/dev.development.js +0 -2
  60. package/dist/env/dev.js +0 -2
  61. package/dist/env/dev.node.d.ts +0 -1
  62. package/dist/env/dev.node.js +0 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bare-ts/lib",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "TypeScript library for BARE, a compact and simple binary-serialization format",
5
5
  "keywords": [
6
6
  "bare",
@@ -39,9 +39,9 @@
39
39
  },
40
40
  "imports": {
41
41
  "#dev": {
42
- "development": "./dist/env/dev.development.js",
43
- "node": "./dist/env/dev.node.js",
44
- "default": "./dist/env/dev.js"
42
+ "development": "./imports/dev.development.js",
43
+ "node": "./imports/dev.node.js",
44
+ "default": "./imports/dev.js"
45
45
  }
46
46
  },
47
47
  "sideEffects": false,
@@ -49,26 +49,30 @@
49
49
  "dist/**/*.cjs",
50
50
  "dist/**/*.d.cts",
51
51
  "dist/**/*.d.ts",
52
- "dist/**/*.js"
52
+ "dist/**/*.js",
53
+ "imports/**/*.d.ts",
54
+ "imports/**/*.js"
53
55
  ],
54
- "directories": {
55
- "test": "tests"
56
- },
57
56
  "scripts": {
58
- "build": "sh ./scripts/build.sh",
57
+ "build": "npm run build:esm && npm run build:d.cts && npm run build:cjs",
58
+ "build:d.cts": "cp -f dist/index.d.ts dist/index.d.cts",
59
+ "build:cjs": "esbuild src/index.ts --bundle --target=es2020 --platform=node > dist/index.cjs",
60
+ "build:esm": "tsc --build src/",
61
+ "check": "tsc --build src src/tsconfig-test.json && biome ci --error-on-warnings .",
59
62
  "clean": "rm -rf dist coverage",
60
63
  "coverage": "c8 --reporter=lcovonly npm test",
61
- "format": "rome format --write .",
62
- "prepare": "validate-commit-msg",
63
- "prepublishOnly": "npm run clean && npm test",
64
- "test": "sh ./scripts/test.sh",
64
+ "format": "biome check --linter-enabled=false --write .",
65
+ "lint": "biome lint --error-on-warnings .",
66
+ "prepublishOnly": "npm run clean && npm run build && npm test",
67
+ "test": "npm run test:dev && npm run test:prod",
68
+ "test:dev": "NODE_ENV=development node --test && npm run check",
69
+ "test:prod": "node --test && npm run check",
65
70
  "version": "sh ./scripts/version.sh"
66
71
  },
67
72
  "devDependencies": {
68
73
  "@biomejs/biome": "2.3.4",
74
+ "@types/node": "20.19.21",
69
75
  "esbuild": "0.27.0",
70
- "oletus": "4.0.0",
71
- "typescript": "5.9.3",
72
- "validate-commit-message": "3.2.0"
76
+ "typescript": "5.9.3"
73
77
  }
74
78
  }
@@ -1,9 +0,0 @@
1
- import { type ByteCursor } from "../core/byte-cursor.js";
2
- export declare const readF32FixedArray: (bc: ByteCursor, len: number) => Float32Array<ArrayBuffer>;
3
- export declare const writeF32FixedArray: (bc: ByteCursor, x: Float32Array) => void;
4
- export declare function readF32Array(bc: ByteCursor): Float32Array<ArrayBuffer>;
5
- export declare function writeF32Array(bc: ByteCursor, x: Float32Array): void;
6
- export declare const readF64FixedArray: (bc: ByteCursor, len: number) => Float64Array<ArrayBuffer>;
7
- export declare const writeF64FixedArray: (bc: ByteCursor, x: Float64Array) => void;
8
- export declare function readF64Array(bc: ByteCursor): Float64Array<ArrayBuffer>;
9
- export declare function writeF64Array(bc: ByteCursor, x: Float64Array): void;
@@ -1,93 +0,0 @@
1
- "use strict";
2
- import { check, reserve } from "../core/byte-cursor.js";
3
- import { assert, DEV } from "../util/assert.js";
4
- import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
- import { isU32 } from "../util/validator.js";
6
- import { readFixedData } from "./data.js";
7
- import {
8
- readF32,
9
- readF64,
10
- readUintSafe32,
11
- writeF32,
12
- writeF64,
13
- writeUintSafe32
14
- } from "./primitive.js";
15
- import { writeU8FixedArray } from "./u8-array.js";
16
- export const readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLe : readF32FixedArrayBe;
17
- function readF32FixedArrayLe(bc, len) {
18
- if (DEV) {
19
- assert(isU32(len));
20
- }
21
- const byteLen = len * 4;
22
- const result = new Float32Array(readFixedData(bc, byteLen));
23
- return result;
24
- }
25
- function readF32FixedArrayBe(bc, len) {
26
- if (DEV) {
27
- assert(isU32(len));
28
- }
29
- check(bc, len * 4);
30
- const result = new Float32Array(len);
31
- for (let i = 0; i < len; i++) {
32
- result[i] = readF32(bc);
33
- }
34
- return result;
35
- }
36
- export const writeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF32FixedArrayLe : writeF32FixedArrayBe;
37
- function writeF32FixedArrayLe(bc, x) {
38
- writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
39
- }
40
- function writeF32FixedArrayBe(bc, val) {
41
- reserve(bc, val.length * 4);
42
- for (let i = 0; i < val.length; i++) {
43
- writeF32(bc, val[i]);
44
- }
45
- }
46
- export function readF32Array(bc) {
47
- return readF32FixedArray(bc, readUintSafe32(bc));
48
- }
49
- export function writeF32Array(bc, x) {
50
- writeUintSafe32(bc, x.length);
51
- if (x.length > 0) {
52
- writeF32FixedArray(bc, x);
53
- }
54
- }
55
- export const readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLe : readF64FixedArrayBe;
56
- function readF64FixedArrayLe(bc, len) {
57
- if (DEV) {
58
- assert(isU32(len));
59
- }
60
- const byteLen = len * 8;
61
- const result = new Float64Array(readFixedData(bc, byteLen));
62
- return result;
63
- }
64
- function readF64FixedArrayBe(bc, len) {
65
- if (DEV) {
66
- assert(isU32(len));
67
- }
68
- check(bc, len * 8);
69
- const result = new Float64Array(len);
70
- for (let i = 0; i < len; i++) {
71
- result[i] = readF64(bc);
72
- }
73
- return result;
74
- }
75
- export const writeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeF64FixedArrayLe : writeF64FixedArrayBe;
76
- function writeF64FixedArrayLe(bc, x) {
77
- writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
78
- }
79
- function writeF64FixedArrayBe(bc, x) {
80
- reserve(bc, x.length * 8);
81
- for (let i = 0; i < x.length; i++) {
82
- writeF64(bc, x[i]);
83
- }
84
- }
85
- export function readF64Array(bc) {
86
- return readF64FixedArray(bc, readUintSafe32(bc));
87
- }
88
- export function writeF64Array(bc, x) {
89
- writeUintSafe32(bc, x.length);
90
- if (x.length > 0) {
91
- writeF64FixedArray(bc, x);
92
- }
93
- }
@@ -1,423 +0,0 @@
1
- "use strict";
2
- import { BareError } from "../core/bare-error.js";
3
- import { check, reserve } from "../core/byte-cursor.js";
4
- import { assert, DEV } from "../util/assert.js";
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,
14
- isI16,
15
- isI32,
16
- isI64,
17
- isU8,
18
- isU16,
19
- isU32,
20
- isU64,
21
- isU64Safe
22
- } from "../util/validator.js";
23
- export function readBool(bc) {
24
- const val = readU8(bc);
25
- if (val > 1) {
26
- bc.offset--;
27
- throw new BareError(bc.offset, "a bool must be equal to 0 or 1");
28
- }
29
- return val > 0;
30
- }
31
- export function writeBool(bc, x) {
32
- writeU8(bc, x ? 1 : 0);
33
- }
34
- export function readF32(bc) {
35
- check(bc, 4);
36
- const result = bc.view.getFloat32(bc.offset, true);
37
- bc.offset += 4;
38
- return result;
39
- }
40
- export function writeF32(bc, x) {
41
- reserve(bc, 4);
42
- bc.view.setFloat32(bc.offset, x, true);
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
- }
49
- bc.offset += 4;
50
- }
51
- export function readF64(bc) {
52
- check(bc, 8);
53
- const result = bc.view.getFloat64(bc.offset, true);
54
- bc.offset += 8;
55
- return result;
56
- }
57
- export function writeF64(bc, x) {
58
- reserve(bc, 8);
59
- bc.view.setFloat64(bc.offset, x, true);
60
- bc.offset += 8;
61
- }
62
- export function readI8(bc) {
63
- check(bc, 1);
64
- return bc.view.getInt8(bc.offset++);
65
- }
66
- export function writeI8(bc, x) {
67
- if (DEV) {
68
- assert(isI8(x), TOO_LARGE_NUMBER);
69
- }
70
- reserve(bc, 1);
71
- bc.view.setInt8(bc.offset++, x);
72
- }
73
- export function readI16(bc) {
74
- check(bc, 2);
75
- const result = bc.view.getInt16(bc.offset, true);
76
- bc.offset += 2;
77
- return result;
78
- }
79
- export function writeI16(bc, x) {
80
- if (DEV) {
81
- assert(isI16(x), TOO_LARGE_NUMBER);
82
- }
83
- reserve(bc, 2);
84
- bc.view.setInt16(bc.offset, x, true);
85
- bc.offset += 2;
86
- }
87
- export function readI32(bc) {
88
- check(bc, 4);
89
- const result = bc.view.getInt32(bc.offset, true);
90
- bc.offset += 4;
91
- return result;
92
- }
93
- export function writeI32(bc, x) {
94
- if (DEV) {
95
- assert(isI32(x), TOO_LARGE_NUMBER);
96
- }
97
- reserve(bc, 4);
98
- bc.view.setInt32(bc.offset, x, true);
99
- bc.offset += 4;
100
- }
101
- export function readI64(bc) {
102
- check(bc, 8);
103
- const result = bc.view.getBigInt64(bc.offset, true);
104
- bc.offset += 8;
105
- return result;
106
- }
107
- export function writeI64(bc, x) {
108
- if (DEV) {
109
- assert(isI64(x), TOO_LARGE_NUMBER);
110
- }
111
- reserve(bc, 8);
112
- bc.view.setBigInt64(bc.offset, x, true);
113
- bc.offset += 8;
114
- }
115
- export function readI64Safe(bc) {
116
- const result = readU32(bc) + readI32(bc) * /* 2**32 */
117
- 4294967296;
118
- if (!Number.isSafeInteger(result)) {
119
- bc.offset -= 8;
120
- throw new BareError(bc.offset, TOO_LARGE_NUMBER);
121
- }
122
- return result;
123
- }
124
- export function writeI64Safe(bc, x) {
125
- if (DEV) {
126
- assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
127
- }
128
- let lowest32 = x >>> 0;
129
- writeU32(bc, lowest32);
130
- let highest32 = x / /* 2**32 */
131
- 4294967296 | 0;
132
- if (x < 0) {
133
- highest32 = ~(Math.abs(highest32) & /* 2**21-1 */
134
- 2097151) >>> 0;
135
- if (lowest32 === 0) {
136
- if (highest32 === 2097151) {
137
- lowest32 = 1;
138
- } else {
139
- highest32++;
140
- }
141
- }
142
- }
143
- writeU32(bc, highest32);
144
- }
145
- export function readInt(bc) {
146
- const zigZag = readUint(bc);
147
- return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
148
- }
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);
156
- }
157
- export function readIntSafe(bc) {
158
- const firstByte = readU8(bc);
159
- let result = (firstByte & 127) >> 1;
160
- if (firstByte >= 128) {
161
- let shiftMul = (
162
- /* 2**6 */
163
- 64
164
- );
165
- let byteCount = 1;
166
- let byte;
167
- do {
168
- byte = readU8(bc);
169
- result += (byte & 127) * shiftMul;
170
- shiftMul *= /* 2**7 */
171
- 128;
172
- byteCount++;
173
- } while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
174
- if (byte === 0) {
175
- bc.offset -= byteCount - 1;
176
- throw new BareError(bc.offset, "must be canonical");
177
- }
178
- if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
179
- bc.offset -= byteCount - 1;
180
- throw new BareError(bc.offset, TOO_LARGE_NUMBER);
181
- }
182
- }
183
- const isNeg = (firstByte & 1) === 1;
184
- if (isNeg) {
185
- result = -result - 1;
186
- }
187
- return result;
188
- }
189
- export function writeIntSafe(bc, x) {
190
- const sign = x < 0 ? 1 : 0;
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);
209
- } else {
210
- writeU8(bc, first7Bits);
211
- }
212
- }
213
- export function readU8(bc) {
214
- check(bc, 1);
215
- return bc.bytes[bc.offset++];
216
- }
217
- export function writeU8(bc, x) {
218
- if (DEV) {
219
- assert(isU8(x), TOO_LARGE_NUMBER);
220
- }
221
- reserve(bc, 1);
222
- bc.bytes[bc.offset++] = x;
223
- }
224
- export function readU16(bc) {
225
- check(bc, 2);
226
- const result = bc.view.getUint16(bc.offset, true);
227
- bc.offset += 2;
228
- return result;
229
- }
230
- export function writeU16(bc, x) {
231
- if (DEV) {
232
- assert(isU16(x), TOO_LARGE_NUMBER);
233
- }
234
- reserve(bc, 2);
235
- bc.view.setUint16(bc.offset, x, true);
236
- bc.offset += 2;
237
- }
238
- export function readU32(bc) {
239
- check(bc, 4);
240
- const result = bc.view.getUint32(bc.offset, true);
241
- bc.offset += 4;
242
- return result;
243
- }
244
- export function writeU32(bc, x) {
245
- if (DEV) {
246
- assert(isU32(x), TOO_LARGE_NUMBER);
247
- }
248
- reserve(bc, 4);
249
- bc.view.setUint32(bc.offset, x, true);
250
- bc.offset += 4;
251
- }
252
- export function readU64(bc) {
253
- check(bc, 8);
254
- const result = bc.view.getBigUint64(bc.offset, true);
255
- bc.offset += 8;
256
- return result;
257
- }
258
- export function writeU64(bc, x) {
259
- if (DEV) {
260
- assert(isU64(x), TOO_LARGE_NUMBER);
261
- }
262
- reserve(bc, 8);
263
- bc.view.setBigUint64(bc.offset, x, true);
264
- bc.offset += 8;
265
- }
266
- export function readU64Safe(bc) {
267
- const result = readU32(bc) + readU32(bc) * /* 2**32 */
268
- 4294967296;
269
- if (!isU64Safe(result)) {
270
- bc.offset -= 8;
271
- throw new BareError(bc.offset, TOO_LARGE_NUMBER);
272
- }
273
- return result;
274
- }
275
- export function writeU64Safe(bc, x) {
276
- if (DEV) {
277
- assert(isU64Safe(x), TOO_LARGE_NUMBER);
278
- }
279
- writeU32(bc, x >>> 0);
280
- writeU32(bc, x / /* 2**32 */
281
- 4294967296 & /* 2**21-1 */
282
- 2097151);
283
- }
284
- export function readUint(bc) {
285
- let low = readU8(bc);
286
- if (low >= 128) {
287
- low &= 127;
288
- let shiftMul = 128;
289
- let byteCount = 1;
290
- let byte;
291
- do {
292
- byte = readU8(bc);
293
- low += (byte & 127) * shiftMul;
294
- shiftMul *= /* 2**7 */
295
- 128;
296
- byteCount++;
297
- } while (byte >= 128 && byteCount < 7);
298
- let height = 0;
299
- shiftMul = 1;
300
- while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
301
- byte = readU8(bc);
302
- height += (byte & 127) * shiftMul;
303
- shiftMul *= /* 2**7 */
304
- 128;
305
- byteCount++;
306
- }
307
- if (byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
308
- bc.offset -= byteCount;
309
- throw new BareError(bc.offset, NON_CANONICAL_REPRESENTATION);
310
- }
311
- return BigInt(low) + (BigInt(height) << BigInt(7 * 7));
312
- }
313
- return BigInt(low);
314
- }
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) {
323
- let tmp = Number(BigInt.asUintN(7 * 7, x));
324
- let rest = Number(x >> BigInt(7 * 7));
325
- let byteCount = 0;
326
- while (tmp >= 128 || rest > 0) {
327
- writeU8(bc, 128 | tmp & 127);
328
- tmp = Math.floor(tmp / /* 2**7 */
329
- 128);
330
- byteCount++;
331
- if (byteCount === 7) {
332
- tmp = rest;
333
- rest = 0;
334
- }
335
- }
336
- writeU8(bc, tmp);
337
- }
338
- export function readUintSafe32(bc) {
339
- let result = readU8(bc);
340
- if (result >= 128) {
341
- result &= 127;
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 | x & 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
- );
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
- }
package/dist/env/dev.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const DEV = false;
@@ -1 +0,0 @@
1
- export declare const DEV = true;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- export const DEV = true;
package/dist/env/dev.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- export const DEV = false;
@@ -1 +0,0 @@
1
- export declare const DEV: boolean;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- export const DEV = process.env.NODE_ENV === "development";