@bare-ts/lib 0.4.1 → 0.6.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 (63) hide show
  1. package/README.md +9 -10
  2. package/dist/codec/data.d.ts +1 -1
  3. package/dist/codec/data.js +11 -15
  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 +4 -7
  11. package/dist/codec/i16-array.js +32 -33
  12. package/dist/codec/i32-array.d.ts +4 -7
  13. package/dist/codec/i32-array.js +32 -33
  14. package/dist/codec/i64-array.d.ts +4 -7
  15. package/dist/codec/i64-array.js +32 -33
  16. package/dist/codec/i8-array.d.ts +3 -3
  17. package/dist/codec/i8-array.js +12 -11
  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 +144 -115
  22. package/dist/codec/u16-array.d.ts +4 -7
  23. package/dist/codec/u16-array.js +32 -33
  24. package/dist/codec/u32-array.d.ts +4 -7
  25. package/dist/codec/u32-array.js +32 -33
  26. package/dist/codec/u64-array.d.ts +4 -7
  27. package/dist/codec/u64-array.js +32 -33
  28. package/dist/codec/u8-array.d.ts +3 -3
  29. package/dist/codec/u8-array.js +26 -20
  30. package/dist/codec/u8-clamped-array.d.ts +3 -3
  31. package/dist/codec/u8-clamped-array.js +12 -11
  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 +103 -31
  37. package/dist/core/config.d.ts +2 -4
  38. package/dist/core/config.js +14 -18
  39. package/dist/index.cjs +194 -134
  40. package/dist/index.d.cts +21 -16
  41. package/dist/index.d.ts +21 -16
  42. package/dist/index.js +12 -6
  43. package/dist/util/assert.d.ts +10 -1
  44. package/dist/util/assert.js +24 -13
  45. package/dist/util/constants.js +6 -2
  46. package/dist/util/validator.d.ts +27 -0
  47. package/dist/util/validator.js +38 -10
  48. package/imports/dev.d.ts +4 -0
  49. package/imports/dev.development.d.ts +4 -0
  50. package/imports/dev.development.js +4 -0
  51. package/imports/dev.js +4 -0
  52. package/imports/dev.node.d.ts +4 -0
  53. package/imports/dev.node.js +4 -0
  54. package/package.json +22 -21
  55. package/dist/codec/float-array.d.ts +0 -14
  56. package/dist/codec/float-array.js +0 -93
  57. package/dist/codec/primitive.js +0 -423
  58. package/dist/env/dev.d.ts +0 -1
  59. package/dist/env/dev.development.d.ts +0 -1
  60. package/dist/env/dev.development.js +0 -2
  61. package/dist/env/dev.js +0 -2
  62. package/dist/env/dev.node.d.ts +0 -1
  63. package/dist/env/dev.node.js +0 -2
@@ -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 { DEV, assert } 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 | 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
- );
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";