@bare-ts/lib 0.1.0 → 0.1.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.
@@ -0,0 +1,352 @@
1
+ // src/codec/primitive.ts
2
+ import { ok as assert } from "assert";
3
+ import { BareError } from "../core/index.js";
4
+ import {
5
+ isI16,
6
+ isI32,
7
+ isI64,
8
+ isI8,
9
+ isSafeU64,
10
+ isU16,
11
+ isU32,
12
+ isU64,
13
+ isU8
14
+ } 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 decodeBool(bc) {
19
+ const val = decodeU8(bc);
20
+ if (val > 1) {
21
+ bc.offset--;
22
+ throw new BareError(bc.offset, "a bool must be equal to 0 or 1");
23
+ }
24
+ return val !== 0;
25
+ }
26
+ function encodeBool(bc, x) {
27
+ encodeU8(bc, x ? 1 : 0);
28
+ }
29
+ function decodeF32(bc) {
30
+ bc.check(4);
31
+ const result = bc.view.getFloat32(bc.offset, true);
32
+ if (Number.isNaN(result)) {
33
+ throw new BareError(bc.offset, NAN_NOT_ALLOWED);
34
+ }
35
+ bc.offset += 4;
36
+ return result;
37
+ }
38
+ function encodeF32(bc, x) {
39
+ assert(!Number.isNaN(x), NAN_NOT_ALLOWED);
40
+ bc.reserve(4);
41
+ bc.view.setFloat32(bc.offset, x, true);
42
+ assert(Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER);
43
+ bc.offset += 4;
44
+ }
45
+ function decodeF64(bc) {
46
+ bc.check(8);
47
+ const result = bc.view.getFloat64(bc.offset, true);
48
+ if (Number.isNaN(result)) {
49
+ throw new BareError(bc.offset, NAN_NOT_ALLOWED);
50
+ }
51
+ bc.offset += 8;
52
+ return result;
53
+ }
54
+ function encodeF64(bc, x) {
55
+ assert(!Number.isNaN(x), NAN_NOT_ALLOWED);
56
+ bc.reserve(8);
57
+ bc.view.setFloat64(bc.offset, x, true);
58
+ bc.offset += 8;
59
+ }
60
+ function decodeI8(bc) {
61
+ bc.check(1);
62
+ return bc.view.getInt8(bc.offset++);
63
+ }
64
+ function encodeI8(bc, x) {
65
+ assert(isI8(x), TOO_LARGE_NUMBER);
66
+ bc.reserve(1);
67
+ bc.view.setInt8(bc.offset++, x);
68
+ }
69
+ function decodeI16(bc) {
70
+ bc.check(2);
71
+ const result = bc.view.getInt16(bc.offset, true);
72
+ bc.offset += 2;
73
+ return result;
74
+ }
75
+ function encodeI16(bc, x) {
76
+ assert(isI16(x), TOO_LARGE_NUMBER);
77
+ bc.reserve(2);
78
+ bc.view.setInt16(bc.offset, x, true);
79
+ bc.offset += 2;
80
+ }
81
+ function decodeI32(bc) {
82
+ bc.check(4);
83
+ const result = bc.view.getInt32(bc.offset, true);
84
+ bc.offset += 4;
85
+ return result;
86
+ }
87
+ function encodeI32(bc, x) {
88
+ assert(isI32(x), TOO_LARGE_NUMBER);
89
+ bc.reserve(4);
90
+ bc.view.setInt32(bc.offset, x, true);
91
+ bc.offset += 4;
92
+ }
93
+ function decodeI64(bc) {
94
+ bc.check(8);
95
+ const result = bc.view.getBigInt64(bc.offset, true);
96
+ bc.offset += 8;
97
+ return result;
98
+ }
99
+ function encodeI64(bc, x) {
100
+ assert(isI64(x), TOO_LARGE_NUMBER);
101
+ bc.reserve(8);
102
+ bc.view.setBigInt64(bc.offset, x, true);
103
+ bc.offset += 8;
104
+ }
105
+ function decodeI64Safe(bc) {
106
+ const result = decodeU32(bc) + decodeI32(bc) * 4294967296;
107
+ if (!Number.isSafeInteger(result)) {
108
+ bc.offset -= 8;
109
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
110
+ }
111
+ return result;
112
+ }
113
+ function encodeI64Safe(bc, x) {
114
+ assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
115
+ const lowest32 = x >>> 0;
116
+ encodeU32(bc, lowest32);
117
+ let highest32 = x / 4294967296 | 0;
118
+ if (x < 0) {
119
+ highest32 = ~Math.abs(highest32) >>> 0;
120
+ if (lowest32 === 0) {
121
+ highest32++;
122
+ }
123
+ }
124
+ encodeU32(bc, highest32);
125
+ }
126
+ function decodeInt(bc) {
127
+ const zigZag = decodeUint(bc);
128
+ return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
129
+ }
130
+ function encodeInt(bc, x) {
131
+ assert(isI64(x), TOO_LARGE_NUMBER);
132
+ const zigZag = x >> BigInt(63) ^ x << BigInt(1);
133
+ encodeUint(bc, zigZag);
134
+ }
135
+ var INT_SAFE_MAX_BYTE_COUNT = 8;
136
+ function decodeIntSafe(bc) {
137
+ const firstByte = decodeU8(bc);
138
+ let result = (firstByte & 127) >> 1;
139
+ if (firstByte >= 128) {
140
+ let shiftMul = 64;
141
+ let byteCount = 1;
142
+ let byte;
143
+ do {
144
+ byte = decodeU8(bc);
145
+ result += (byte & 127) * shiftMul;
146
+ shiftMul *= 128;
147
+ byteCount++;
148
+ } while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
149
+ if (byte === 0) {
150
+ bc.offset -= byteCount - 1;
151
+ throw new BareError(bc.offset, "must be canonical");
152
+ }
153
+ if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
154
+ bc.offset -= byteCount - 1;
155
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
156
+ }
157
+ }
158
+ const isNeg = (firstByte & 1) === 1;
159
+ if (isNeg) {
160
+ result = -result - 1;
161
+ }
162
+ return result;
163
+ }
164
+ function encodeIntSafe(bc, x) {
165
+ assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
166
+ 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
+ encodeU8(bc, 128 | firstByte);
174
+ encodeUintSafe(bc, x);
175
+ } else {
176
+ encodeU8(bc, firstByte);
177
+ }
178
+ }
179
+ function decodeU8(bc) {
180
+ bc.check(1);
181
+ return bc.view.getUint8(bc.offset++);
182
+ }
183
+ function encodeU8(bc, x) {
184
+ assert(isU8(x), TOO_LARGE_NUMBER);
185
+ bc.reserve(1);
186
+ bc.view.setUint8(bc.offset++, x);
187
+ }
188
+ function decodeU16(bc) {
189
+ bc.check(2);
190
+ const result = bc.view.getUint16(bc.offset, true);
191
+ bc.offset += 2;
192
+ return result;
193
+ }
194
+ function encodeU16(bc, x) {
195
+ assert(isU16(x), TOO_LARGE_NUMBER);
196
+ bc.reserve(2);
197
+ bc.view.setUint16(bc.offset, x, true);
198
+ bc.offset += 2;
199
+ }
200
+ function decodeU32(bc) {
201
+ bc.check(4);
202
+ const result = bc.view.getUint32(bc.offset, true);
203
+ bc.offset += 4;
204
+ return result;
205
+ }
206
+ function encodeU32(bc, x) {
207
+ assert(isU32(x), TOO_LARGE_NUMBER);
208
+ bc.reserve(4);
209
+ bc.view.setUint32(bc.offset, x, true);
210
+ bc.offset += 4;
211
+ }
212
+ function decodeU64(bc) {
213
+ bc.check(8);
214
+ const result = bc.view.getBigUint64(bc.offset, true);
215
+ bc.offset += 8;
216
+ return result;
217
+ }
218
+ function encodeU64(bc, x) {
219
+ assert(isU64(x), TOO_LARGE_NUMBER);
220
+ bc.reserve(8);
221
+ bc.view.setBigUint64(bc.offset, x, true);
222
+ bc.offset += 8;
223
+ }
224
+ function decodeU64Safe(bc) {
225
+ const result = decodeU32(bc) + decodeU32(bc) * 4294967296;
226
+ if (!isSafeU64(result)) {
227
+ bc.offset -= 8;
228
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
229
+ }
230
+ return result;
231
+ }
232
+ function encodeU64Safe(bc, x) {
233
+ assert(isSafeU64(x), TOO_LARGE_NUMBER);
234
+ encodeU32(bc, x >>> 0);
235
+ encodeU32(bc, x / 4294967296 >>> 0);
236
+ }
237
+ var UINT_MAX_BYTE_COUNT = 10;
238
+ function decodeUint(bc) {
239
+ let byteCount = 0;
240
+ let byte;
241
+ let low = 0;
242
+ let shiftMul = 1;
243
+ do {
244
+ byte = decodeU8(bc);
245
+ low += (byte & 127) * shiftMul;
246
+ shiftMul *= 128;
247
+ byteCount++;
248
+ } while (byte >= 128 && byteCount < 7);
249
+ let height = 0;
250
+ shiftMul = 1;
251
+ while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
252
+ byte = decodeU8(bc);
253
+ height += (byte & 127) * shiftMul;
254
+ shiftMul *= 128;
255
+ byteCount++;
256
+ }
257
+ if (byteCount !== 1 && byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
258
+ bc.offset -= byteCount;
259
+ throw new BareError(bc.offset, NON_CANONICAL_REPRESENTATION);
260
+ }
261
+ return BigInt(low) + (BigInt(height) << BigInt(7 * 7));
262
+ }
263
+ function encodeUint(bc, x) {
264
+ assert(isU64(x), TOO_LARGE_NUMBER);
265
+ let tmp = Number(BigInt.asUintN(7 * 7, x));
266
+ let rest = Number(x >> BigInt(7 * 7));
267
+ let byteCount = 0;
268
+ while (tmp >= 128 || rest !== 0) {
269
+ encodeU8(bc, 128 | tmp & 127);
270
+ tmp = Math.floor(tmp / 128);
271
+ byteCount++;
272
+ if (byteCount === 7) {
273
+ tmp = rest;
274
+ rest = 0;
275
+ }
276
+ }
277
+ encodeU8(bc, tmp);
278
+ }
279
+ var UINT_SAFE_MAX_BYTE_COUNT = 8;
280
+ function decodeUintSafe(bc) {
281
+ let result = 0;
282
+ let shiftMul = 1;
283
+ let byteCount = 0;
284
+ let byte;
285
+ do {
286
+ byte = decodeU8(bc);
287
+ result += (byte & 127) * shiftMul;
288
+ shiftMul *= 128;
289
+ byteCount++;
290
+ } while (byte >= 128 && byteCount < UINT_SAFE_MAX_BYTE_COUNT);
291
+ if (byteCount !== 1 && byte === 0) {
292
+ bc.offset -= byteCount - 1;
293
+ throw new BareError(bc.offset - byteCount + 1, NON_CANONICAL_REPRESENTATION);
294
+ }
295
+ if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
296
+ bc.offset -= byteCount - 1;
297
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
298
+ }
299
+ return result;
300
+ }
301
+ function encodeUintSafe(bc, x) {
302
+ assert(isSafeU64(x), TOO_LARGE_NUMBER);
303
+ while (x >= 128) {
304
+ encodeU8(bc, 128 | x & 127);
305
+ x = Math.floor(x / 128);
306
+ }
307
+ encodeU8(bc, x);
308
+ }
309
+ function decodeVoid(_dc) {
310
+ return void 0;
311
+ }
312
+ function encodeVoid(_dc, _x) {
313
+ }
314
+ export {
315
+ decodeBool,
316
+ decodeF32,
317
+ decodeF64,
318
+ decodeI16,
319
+ decodeI32,
320
+ decodeI64,
321
+ decodeI64Safe,
322
+ decodeI8,
323
+ decodeInt,
324
+ decodeIntSafe,
325
+ decodeU16,
326
+ decodeU32,
327
+ decodeU64,
328
+ decodeU64Safe,
329
+ decodeU8,
330
+ decodeUint,
331
+ decodeUintSafe,
332
+ decodeVoid,
333
+ encodeBool,
334
+ encodeF32,
335
+ encodeF64,
336
+ encodeI16,
337
+ encodeI32,
338
+ encodeI64,
339
+ encodeI64Safe,
340
+ encodeI8,
341
+ encodeInt,
342
+ encodeIntSafe,
343
+ encodeU16,
344
+ encodeU32,
345
+ encodeU64,
346
+ encodeU64Safe,
347
+ encodeU8,
348
+ encodeUint,
349
+ encodeUintSafe,
350
+ encodeVoid
351
+ };
352
+ //# sourceMappingURL=primitive.js.map
@@ -0,0 +1,122 @@
1
+ // src/codec/string.ts
2
+ import { BareError } from "../core/bare-error.js";
3
+ import { decodeUintSafe, encodeUintSafe } from "./primitive.js";
4
+ function decodeString(bc) {
5
+ const strBytesLen = decodeUintSafe(bc);
6
+ const strBytes = bc.read(strBytesLen);
7
+ try {
8
+ return strBytesLen < bc.config.textDecoderThreshold ? decodeUtf8Js(strBytes) : UTF8_DECODER.decode(strBytes);
9
+ } catch (cause) {
10
+ throw new BareError(bc.offset - strBytesLen, "invalid UTF-8 string", {
11
+ cause
12
+ });
13
+ }
14
+ }
15
+ function encodeString(bc, x) {
16
+ if (x.length < bc.config.textEncoderThreshold) {
17
+ const byteLen = utf8ByteLength(x);
18
+ encodeUintSafe(bc, byteLen);
19
+ bc.reserve(byteLen);
20
+ encodeUtf8Js(bc, x);
21
+ } else {
22
+ const strBytes = UTF8_ENCODER.encode(x);
23
+ encodeUintSafe(bc, strBytes.length);
24
+ bc.write(strBytes);
25
+ }
26
+ }
27
+ function decodeUtf8Js(bytes) {
28
+ const bytesLen = bytes.length;
29
+ let result = "";
30
+ let i = 0;
31
+ while (i < bytesLen) {
32
+ let codePoint = bytes[i++] | 0;
33
+ switch (Math.clz32(~codePoint << 24)) {
34
+ case 0:
35
+ break;
36
+ case 2: {
37
+ if (i < bytesLen) {
38
+ codePoint = (codePoint & 31) << 6 | bytes[i] & 63;
39
+ }
40
+ if (codePoint >> 7 === 0 || bytes[i] >> 6 !== 2) {
41
+ throw TypeError("Decoding failed");
42
+ }
43
+ i += 1;
44
+ break;
45
+ }
46
+ case 3: {
47
+ if (i + 1 < bytesLen) {
48
+ codePoint = (codePoint & 15) << 12 | (bytes[i] & 63) << 6 | bytes[i + 1] & 63;
49
+ }
50
+ if (codePoint >> 11 === 0 || bytes[i] >> 6 !== 2 || bytes[i + 1] >> 6 !== 2 || codePoint >> 11 === 27) {
51
+ throw TypeError("Decoding failed");
52
+ }
53
+ i += 2;
54
+ break;
55
+ }
56
+ case 4: {
57
+ if (i + 2 < bytesLen) {
58
+ codePoint = (codePoint & 7) << 18 | (bytes[i] & 63) << 12 | (bytes[i + 1] & 63) << 6 | bytes[i + 2] & 63;
59
+ }
60
+ if (codePoint >> 16 === 0 || codePoint > 1114111 || bytes[i] >> 6 !== 2 || bytes[i + 1] >> 6 !== 2 || bytes[i + 2] >> 6 !== 2) {
61
+ throw TypeError("Decoding failed");
62
+ }
63
+ i += 3;
64
+ break;
65
+ }
66
+ default:
67
+ throw TypeError("Decoding failed");
68
+ }
69
+ result += String.fromCodePoint(codePoint);
70
+ }
71
+ return result;
72
+ }
73
+ function encodeUtf8Js(bc, s) {
74
+ const sLen = s.length;
75
+ let offset = bc.offset;
76
+ const view = bc.view;
77
+ let i = 0;
78
+ while (i < sLen) {
79
+ const codePoint = s.codePointAt(i++);
80
+ if (codePoint < 128) {
81
+ view.setUint8(offset++, codePoint);
82
+ } else if (codePoint < 2048) {
83
+ view.setUint8(offset++, 192 | codePoint >> 6);
84
+ view.setUint8(offset++, 128 | codePoint & 63);
85
+ } else if (codePoint < 65536) {
86
+ view.setUint8(offset++, 224 | codePoint >> 12);
87
+ view.setUint8(offset++, 128 | codePoint >> 6 & 63);
88
+ view.setUint8(offset++, 128 | codePoint & 63);
89
+ } else {
90
+ view.setUint8(offset++, 240 | codePoint >> 18);
91
+ view.setUint8(offset++, 128 | codePoint >> 12 & 63);
92
+ view.setUint8(offset++, 128 | codePoint >> 6 & 63);
93
+ view.setUint8(offset++, 128 | codePoint & 63);
94
+ i++;
95
+ }
96
+ }
97
+ bc.offset = offset;
98
+ }
99
+ function utf8ByteLength(s) {
100
+ const sLen = s.length;
101
+ let result = sLen;
102
+ for (let i = 0; i < sLen; i++) {
103
+ const codePoint = s.codePointAt(i);
104
+ if (codePoint >= 128) {
105
+ result++;
106
+ if (codePoint >= 2048) {
107
+ result++;
108
+ if (codePoint >= 65536) {
109
+ i++;
110
+ }
111
+ }
112
+ }
113
+ }
114
+ return result;
115
+ }
116
+ var UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
117
+ var UTF8_ENCODER = new TextEncoder();
118
+ export {
119
+ decodeString,
120
+ encodeString
121
+ };
122
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1,50 @@
1
+ // src/codec/u16-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
+ import { decodeFixedData } from "./data.js";
5
+ import {
6
+ decodeU16,
7
+ decodeUintSafe,
8
+ encodeU16,
9
+ encodeUintSafe
10
+ } from "./primitive.js";
11
+ var U16_BYTE_COUNT = 2;
12
+ var decodeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU16FixedArrayLE : decodeU16FixedArrayBE;
13
+ function decodeU16Array(bc) {
14
+ return decodeU16FixedArray(bc, decodeUintSafe(bc));
15
+ }
16
+ function decodeU16FixedArrayLE(bc, len) {
17
+ const byteCount = len * U16_BYTE_COUNT;
18
+ return new Uint16Array(decodeFixedData(bc, byteCount));
19
+ }
20
+ function decodeU16FixedArrayBE(bc, len) {
21
+ bc.check(len * U16_BYTE_COUNT);
22
+ const result = new Uint16Array(len);
23
+ for (let i = 0; i < len; i++)
24
+ result[i] = decodeU16(bc);
25
+ return result;
26
+ }
27
+ var encodeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU16FixedArrayLE : encodeU16FixedArrayBE;
28
+ function encodeU16Array(bc, x) {
29
+ encodeUintSafe(bc, x.length);
30
+ if (x.length !== 0) {
31
+ encodeU16FixedArray(bc, x, x.length);
32
+ }
33
+ }
34
+ function encodeU16FixedArrayLE(bc, x, len) {
35
+ assert(x.length === len);
36
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
+ }
38
+ function encodeU16FixedArrayBE(bc, x, len) {
39
+ assert(x.length === len);
40
+ bc.reserve(x.length * U16_BYTE_COUNT);
41
+ for (let i = 0; i < x.length; i++)
42
+ encodeU16(bc, x[i]);
43
+ }
44
+ export {
45
+ decodeU16Array,
46
+ decodeU16FixedArray,
47
+ encodeU16Array,
48
+ encodeU16FixedArray
49
+ };
50
+ //# sourceMappingURL=u16-array.js.map
@@ -0,0 +1,50 @@
1
+ // src/codec/u32-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
+ import { decodeFixedData } from "./data.js";
5
+ import {
6
+ decodeU32,
7
+ decodeUintSafe,
8
+ encodeU32,
9
+ encodeUintSafe
10
+ } from "./primitive.js";
11
+ var U32_BYTE_COUNT = 4;
12
+ var decodeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU32FixedArrayLE : decodeU32FixedArrayBE;
13
+ function decodeU32Array(bc) {
14
+ return decodeU32FixedArray(bc, decodeUintSafe(bc));
15
+ }
16
+ function decodeU32FixedArrayLE(bc, len) {
17
+ const byteCount = len * U32_BYTE_COUNT;
18
+ return new Uint32Array(decodeFixedData(bc, byteCount));
19
+ }
20
+ function decodeU32FixedArrayBE(bc, len) {
21
+ bc.check(len * U32_BYTE_COUNT);
22
+ const result = new Uint32Array(len);
23
+ for (let i = 0; i < len; i++)
24
+ result[i] = decodeU32(bc);
25
+ return result;
26
+ }
27
+ var encodeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU32FixedArrayLE : encodeU32FixedArrayBE;
28
+ function encodeU32Array(bc, x) {
29
+ encodeUintSafe(bc, x.length);
30
+ if (x.length !== 0) {
31
+ encodeU32FixedArray(bc, x, x.length);
32
+ }
33
+ }
34
+ function encodeU32FixedArrayLE(bc, x, len) {
35
+ assert(x.length === len);
36
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
+ }
38
+ function encodeU32FixedArrayBE(bc, x, len) {
39
+ assert(x.length === len);
40
+ bc.reserve(x.length * U32_BYTE_COUNT);
41
+ for (let i = 0; i < x.length; i++)
42
+ encodeU32(bc, x[i]);
43
+ }
44
+ export {
45
+ decodeU32Array,
46
+ decodeU32FixedArray,
47
+ encodeU32Array,
48
+ encodeU32FixedArray
49
+ };
50
+ //# sourceMappingURL=u32-array.js.map
@@ -0,0 +1,50 @@
1
+ // src/codec/u64-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
4
+ import { decodeFixedData } from "./data.js";
5
+ import {
6
+ decodeU64,
7
+ decodeUintSafe,
8
+ encodeU64,
9
+ encodeUintSafe
10
+ } from "./primitive.js";
11
+ var U64_BYTE_COUNT = 8;
12
+ var decodeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU64FixedArrayLE : decodeU64FixedArrayBE;
13
+ function decodeU64Array(bc) {
14
+ return decodeU64FixedArray(bc, decodeUintSafe(bc));
15
+ }
16
+ function decodeU64FixedArrayLE(bc, len) {
17
+ const byteCount = len * U64_BYTE_COUNT;
18
+ return new BigUint64Array(decodeFixedData(bc, byteCount));
19
+ }
20
+ function decodeU64FixedArrayBE(bc, len) {
21
+ bc.check(len * U64_BYTE_COUNT);
22
+ const result = new BigUint64Array(len);
23
+ for (let i = 0; i < len; i++)
24
+ result[i] = decodeU64(bc);
25
+ return result;
26
+ }
27
+ var encodeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU64FixedArrayLE : encodeU64FixedArrayBE;
28
+ function encodeU64Array(bc, x) {
29
+ encodeUintSafe(bc, x.length);
30
+ if (x.length !== 0) {
31
+ encodeU64FixedArray(bc, x, x.length);
32
+ }
33
+ }
34
+ function encodeU64FixedArrayLE(bc, x, len) {
35
+ assert(x.length === len);
36
+ bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
37
+ }
38
+ function encodeU64FixedArrayBE(bc, x, len) {
39
+ assert(x.length === len);
40
+ bc.reserve(x.length * U64_BYTE_COUNT);
41
+ for (let i = 0; i < x.length; i++)
42
+ encodeU64(bc, x[i]);
43
+ }
44
+ export {
45
+ decodeU64Array,
46
+ decodeU64FixedArray,
47
+ encodeU64Array,
48
+ encodeU64FixedArray
49
+ };
50
+ //# sourceMappingURL=u64-array.js.map
@@ -0,0 +1,25 @@
1
+ // src/codec/u8-array.ts
2
+ import { ok as assert } from "assert";
3
+ import { decodeUintSafe, encodeUintSafe } from "./primitive.js";
4
+ function decodeU8Array(bc) {
5
+ const len = decodeUintSafe(bc);
6
+ return bc.read(len).slice();
7
+ }
8
+ function encodeU8Array(bc, x) {
9
+ encodeUintSafe(bc, x.length);
10
+ bc.write(x);
11
+ }
12
+ function decodeU8FixedArray(bc, len) {
13
+ return bc.read(len).slice();
14
+ }
15
+ function encodeU8FixedArray(bc, x, len) {
16
+ assert(x.length === len);
17
+ bc.write(x);
18
+ }
19
+ export {
20
+ decodeU8Array,
21
+ decodeU8FixedArray,
22
+ encodeU8Array,
23
+ encodeU8FixedArray
24
+ };
25
+ //# sourceMappingURL=u8-array.js.map
@@ -0,0 +1,24 @@
1
+ // src/codec/u8-clamped-array.ts
2
+ import { decodeFixedData } from "./data.js";
3
+ import { decodeUintSafe, encodeUintSafe } from "./primitive.js";
4
+ import { encodeU8FixedArray } from "./u8-array.js";
5
+ function decodeU8ClampedArray(bc) {
6
+ return decodeU8ClampedFixedArray(bc, decodeUintSafe(bc));
7
+ }
8
+ function encodeU8ClampedArray(bc, x) {
9
+ encodeUintSafe(bc, x.length);
10
+ encodeU8ClampedFixedArray(bc, x, x.length);
11
+ }
12
+ function decodeU8ClampedFixedArray(bc, len) {
13
+ return new Uint8ClampedArray(decodeFixedData(bc, len));
14
+ }
15
+ function encodeU8ClampedFixedArray(bc, x, len) {
16
+ encodeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength), len);
17
+ }
18
+ export {
19
+ decodeU8ClampedArray,
20
+ decodeU8ClampedFixedArray,
21
+ encodeU8ClampedArray,
22
+ encodeU8ClampedFixedArray
23
+ };
24
+ //# sourceMappingURL=u8-clamped-array.js.map
@@ -0,0 +1,14 @@
1
+ // src/core/bare-error.ts
2
+ var BareError = class extends Error {
3
+ constructor(offset, issue, opts) {
4
+ super(`(byte:${offset}) ${issue}`);
5
+ this.name = "BareError";
6
+ this.issue = issue;
7
+ this.offset = offset;
8
+ this.cause = opts == null ? void 0 : opts.cause;
9
+ }
10
+ };
11
+ export {
12
+ BareError
13
+ };
14
+ //# sourceMappingURL=bare-error.js.map