@sapphire/string-store 3.0.1-next.c1299bac → 4.0.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.
@@ -1,1415 +0,0 @@
1
- var SapphireStringStore = (function (exports) {
2
- 'use strict';
3
-
4
- var __defProp = Object.defineProperty;
5
- var __typeError = (msg) => {
6
- throw TypeError(msg);
7
- };
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
- var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
11
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
12
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
13
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
14
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
15
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
16
- var __privateWrapper = (obj, member, setter, getter) => ({
17
- set _(value) {
18
- __privateSet(obj, member, value);
19
- },
20
- get _() {
21
- return __privateGet(obj, member, getter);
22
- }
23
- });
24
-
25
- // src/lib/shared/codepoints.ts
26
- var codepointRanges = [
27
- {
28
- start: 19968,
29
- end: 40956,
30
- indexStart: 0,
31
- indexEnd: 20988
32
- },
33
- {
34
- start: 94208,
35
- end: 100343,
36
- indexStart: 20989,
37
- indexEnd: 27125
38
- },
39
- {
40
- start: 131072,
41
- end: 169482,
42
- indexStart: 27126,
43
- indexEnd: 65536
44
- }
45
- ];
46
-
47
- // src/lib/shared/_common.ts
48
- function isArrayLike(object) {
49
- if (!isObject(object)) return false;
50
- if (Array.isArray(object)) return true;
51
- if (!hasLength(object)) return false;
52
- if (!isValidLength(object.length)) return false;
53
- return object.length === 0 || object.length - 1 in object;
54
- }
55
- __name(isArrayLike, "isArrayLike");
56
- function isObject(item) {
57
- return typeof item === "object" && item !== null;
58
- }
59
- __name(isObject, "isObject");
60
- function hasLength(item) {
61
- return "length" in item && typeof item.length === "number";
62
- }
63
- __name(hasLength, "hasLength");
64
- function isValidLength(length) {
65
- return Number.isSafeInteger(length) && length >= 0 && length < 2147483648;
66
- }
67
- __name(isValidLength, "isValidLength");
68
-
69
- // src/lib/shared/Pointer.ts
70
- var _value;
71
- var _Pointer = class _Pointer {
72
- constructor() {
73
- __privateAdd(this, _value, 0);
74
- }
75
- get value() {
76
- return __privateGet(this, _value);
77
- }
78
- add(value) {
79
- const added = __privateGet(this, _value) + value;
80
- if (!isValidLength(added)) {
81
- throw new RangeError(`The pointer value cannot be an invalid length value`);
82
- }
83
- __privateSet(this, _value, added);
84
- return this;
85
- }
86
- static from(pointer) {
87
- if (pointer instanceof _Pointer) {
88
- return pointer;
89
- }
90
- const instance = new _Pointer();
91
- instance.add(Number(pointer));
92
- return instance;
93
- }
94
- };
95
- _value = new WeakMap();
96
- __name(_Pointer, "Pointer");
97
- var Pointer = _Pointer;
98
-
99
- // src/lib/buffer/UnalignedUint16Array.ts
100
- var ConverterUint8 = new Uint8Array(8);
101
- var ConverterUint16 = new Uint16Array(ConverterUint8.buffer);
102
- var ConverterUint32 = new Uint32Array(ConverterUint8.buffer);
103
- var ConverterUint64 = new BigUint64Array(ConverterUint8.buffer);
104
- var ConverterInt32 = new Int32Array(ConverterUint8.buffer);
105
- var ConverterInt64 = new BigInt64Array(ConverterUint8.buffer);
106
- var ConverterFloat = new Float32Array(ConverterUint8.buffer);
107
- var ConverterDouble = new Float64Array(ConverterUint8.buffer);
108
- var _buffer, _bitLength, _wordIndex, _wordLength, _UnalignedUint16Array_instances, uint16ToCodepoint_fn, readBit_fn, readByte_fn, bufferRead16_fn, bufferRead32_fn, bufferRead64_fn, writeBit_fn, bufferWrite16_fn, _UnalignedUint16Array_static, codepointToUint16_fn;
109
- var _UnalignedUint16Array = class _UnalignedUint16Array {
110
- constructor(maxLength) {
111
- __privateAdd(this, _UnalignedUint16Array_instances);
112
- __privateAdd(this, _buffer);
113
- __privateAdd(this, _bitLength, 0);
114
- __privateAdd(this, _wordIndex, 0);
115
- __privateAdd(this, _wordLength, 0);
116
- __privateSet(this, _buffer, new Uint16Array(maxLength));
117
- }
118
- at(index) {
119
- return __privateGet(this, _buffer).at(index);
120
- }
121
- get maxLength() {
122
- return __privateGet(this, _buffer).length;
123
- }
124
- get maxBitLength() {
125
- return __privateGet(this, _buffer).length * 16;
126
- }
127
- get length() {
128
- return __privateGet(this, _wordLength);
129
- }
130
- get bitLength() {
131
- return __privateGet(this, _bitLength);
132
- }
133
- writeBit(value) {
134
- __privateMethod(this, _UnalignedUint16Array_instances, writeBit_fn).call(this, value);
135
- }
136
- writeInt2(value) {
137
- this.writeBit(value & 1);
138
- this.writeBit(value >> 1);
139
- }
140
- writeInt4(value) {
141
- this.writeInt2(value & 3);
142
- this.writeInt2(value >> 2);
143
- }
144
- writeInt8(value) {
145
- this.writeInt4(value & 15);
146
- this.writeInt4(value >> 4);
147
- }
148
- writeInt16(value) {
149
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, value);
150
- }
151
- writeInt32(value) {
152
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, value);
153
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, value >> 16);
154
- }
155
- writeInt64(value) {
156
- this.writeBigInt64(BigInt(value));
157
- }
158
- writeBigInt32(value) {
159
- ConverterInt64[0] = value;
160
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[0]);
161
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[1]);
162
- }
163
- writeBigInt64(value) {
164
- ConverterInt64[0] = value;
165
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[0]);
166
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[1]);
167
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[2]);
168
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[3]);
169
- }
170
- writeFloat32(value) {
171
- ConverterFloat[0] = value;
172
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[0]);
173
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[1]);
174
- }
175
- writeFloat64(value) {
176
- ConverterDouble[0] = value;
177
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[0]);
178
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[1]);
179
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[2]);
180
- __privateMethod(this, _UnalignedUint16Array_instances, bufferWrite16_fn).call(this, ConverterUint16[3]);
181
- }
182
- readBit(offset) {
183
- const ptr = Pointer.from(offset);
184
- return __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr);
185
- }
186
- readInt2(offset) {
187
- return this.readUint2(offset) << 30 >> 30;
188
- }
189
- readUint2(offset) {
190
- const ptr = Pointer.from(offset);
191
- return __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 1;
192
- }
193
- readInt4(offset) {
194
- return this.readUint4(offset) << 28 >> 28;
195
- }
196
- readUint4(offset) {
197
- const ptr = Pointer.from(offset);
198
- return __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 1 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 2 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 3;
199
- }
200
- readInt8(offset) {
201
- return this.readUint8(offset) << 24 >> 24;
202
- }
203
- readUint8(offset) {
204
- const ptr = Pointer.from(offset);
205
- return __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
206
- }
207
- readInt16(offset) {
208
- return this.readUint16(offset) << 16 >> 16;
209
- }
210
- readUint16(offset) {
211
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead16_fn).call(this, Pointer.from(offset));
212
- return ConverterUint16[0];
213
- }
214
- readInt32(offset) {
215
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead32_fn).call(this, Pointer.from(offset));
216
- return ConverterInt32[0];
217
- }
218
- readUint32(offset) {
219
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead32_fn).call(this, Pointer.from(offset));
220
- return ConverterUint32[0];
221
- }
222
- readInt64(offset) {
223
- return Number(this.readBigInt64(offset));
224
- }
225
- readUint64(offset) {
226
- return Number(this.readBigUint64(offset));
227
- }
228
- readBigInt32(offset) {
229
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead32_fn).call(this, Pointer.from(offset));
230
- return BigInt(ConverterInt32[0]);
231
- }
232
- readBigUint32(offset) {
233
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead32_fn).call(this, Pointer.from(offset));
234
- return BigInt(ConverterUint32[0]);
235
- }
236
- readBigInt64(offset) {
237
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead64_fn).call(this, Pointer.from(offset));
238
- return ConverterInt64[0];
239
- }
240
- readBigUint64(offset) {
241
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead64_fn).call(this, Pointer.from(offset));
242
- return ConverterUint64[0];
243
- }
244
- readFloat32(offset) {
245
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead32_fn).call(this, Pointer.from(offset));
246
- return ConverterFloat[0];
247
- }
248
- readFloat64(offset) {
249
- __privateMethod(this, _UnalignedUint16Array_instances, bufferRead64_fn).call(this, Pointer.from(offset));
250
- return ConverterDouble[0];
251
- }
252
- toString() {
253
- let result = "";
254
- for (let i = 0; i < this.length; i++) {
255
- result += String.fromCodePoint(__privateMethod(this, _UnalignedUint16Array_instances, uint16ToCodepoint_fn).call(this, __privateGet(this, _buffer)[i]));
256
- }
257
- return result;
258
- }
259
- toArray() {
260
- return __privateGet(this, _buffer).slice(0, this.length);
261
- }
262
- static from(value) {
263
- var _a;
264
- if (typeof value !== "string") return value;
265
- const codepoints = Array.from(value, (char) => char.codePointAt(0));
266
- const buffer = new _UnalignedUint16Array(value.length);
267
- for (let i = 0; i < codepoints.length; i++) {
268
- const index = __privateMethod(_a = _UnalignedUint16Array, _UnalignedUint16Array_static, codepointToUint16_fn).call(_a, codepoints[i]);
269
- __privateGet(buffer, _buffer)[i] = index;
270
- }
271
- __privateSet(buffer, _bitLength, value.length << 4);
272
- return buffer;
273
- }
274
- };
275
- _buffer = new WeakMap();
276
- _bitLength = new WeakMap();
277
- _wordIndex = new WeakMap();
278
- _wordLength = new WeakMap();
279
- _UnalignedUint16Array_instances = new WeakSet();
280
- uint16ToCodepoint_fn = /* @__PURE__ */ __name(function(index) {
281
- for (const range of codepointRanges) {
282
- if (index >= range.indexStart && index <= range.indexEnd) {
283
- return range.start + (index - range.indexStart);
284
- }
285
- }
286
- throw new RangeError(`Index ${index} is out of range`);
287
- }, "#uint16ToCodepoint");
288
- readBit_fn = /* @__PURE__ */ __name(function(pointer) {
289
- const bitOffset = pointer.value;
290
- const index = bitOffset >> 4;
291
- const bitIndex = bitOffset & 15;
292
- pointer.add(1);
293
- return __privateGet(this, _buffer)[index] >> bitIndex & 1;
294
- }, "#readBit");
295
- readByte_fn = /* @__PURE__ */ __name(function(ptr) {
296
- return __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 1 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 2 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 3 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 4 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 5 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 6 | __privateMethod(this, _UnalignedUint16Array_instances, readBit_fn).call(this, ptr) << 7;
297
- }, "#readByte");
298
- bufferRead16_fn = /* @__PURE__ */ __name(function(ptr) {
299
- ConverterUint8[0] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
300
- ConverterUint8[1] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
301
- }, "#bufferRead16");
302
- bufferRead32_fn = /* @__PURE__ */ __name(function(ptr) {
303
- ConverterUint8[0] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
304
- ConverterUint8[1] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
305
- ConverterUint8[2] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
306
- ConverterUint8[3] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
307
- }, "#bufferRead32");
308
- bufferRead64_fn = /* @__PURE__ */ __name(function(ptr) {
309
- ConverterUint8[0] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
310
- ConverterUint8[1] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
311
- ConverterUint8[2] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
312
- ConverterUint8[3] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
313
- ConverterUint8[4] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
314
- ConverterUint8[5] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
315
- ConverterUint8[6] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
316
- ConverterUint8[7] = __privateMethod(this, _UnalignedUint16Array_instances, readByte_fn).call(this, ptr);
317
- }, "#bufferRead64");
318
- writeBit_fn = /* @__PURE__ */ __name(function(value) {
319
- if (__privateGet(this, _wordIndex) === this.maxLength) {
320
- throw new RangeError(`The buffer is full`);
321
- }
322
- if (value) {
323
- const index = __privateGet(this, _wordIndex);
324
- const bitIndex = this.bitLength & 15;
325
- __privateGet(this, _buffer)[index] |= 1 << bitIndex;
326
- }
327
- if ((__privateGet(this, _bitLength) & 15) === 0) __privateWrapper(this, _wordLength)._++;
328
- __privateWrapper(this, _bitLength)._++;
329
- if ((__privateGet(this, _bitLength) & 15) === 0) __privateWrapper(this, _wordIndex)._++;
330
- }, "#writeBit");
331
- bufferWrite16_fn = /* @__PURE__ */ __name(function(value) {
332
- const wordIndex = __privateGet(this, _wordIndex);
333
- const bitIndex = this.bitLength & 15;
334
- if (wordIndex + (bitIndex === 0 ? 0 : 1) === this.maxLength) {
335
- throw new RangeError(`The buffer is full`);
336
- }
337
- if (bitIndex === 0) {
338
- __privateGet(this, _buffer)[wordIndex] = value;
339
- } else {
340
- value &= 65535;
341
- __privateGet(this, _buffer)[wordIndex] |= value << bitIndex;
342
- __privateGet(this, _buffer)[wordIndex + 1] = value >> 16 - bitIndex;
343
- }
344
- __privateSet(this, _bitLength, __privateGet(this, _bitLength) + 16);
345
- __privateWrapper(this, _wordIndex)._++;
346
- __privateWrapper(this, _wordLength)._++;
347
- }, "#bufferWrite16");
348
- _UnalignedUint16Array_static = new WeakSet();
349
- codepointToUint16_fn = /* @__PURE__ */ __name(function(codepoint) {
350
- for (const range of codepointRanges) {
351
- if (codepoint >= range.start && codepoint <= range.end) {
352
- return range.indexStart + (codepoint - range.start);
353
- }
354
- }
355
- throw new RangeError(`Codepoint ${codepoint} is out of range`);
356
- }, "#codepointToUint16");
357
- __privateAdd(_UnalignedUint16Array, _UnalignedUint16Array_static);
358
- __name(_UnalignedUint16Array, "UnalignedUint16Array");
359
- var UnalignedUint16Array = _UnalignedUint16Array;
360
-
361
- // src/lib/types/Array.ts
362
- function ArrayType(type) {
363
- return {
364
- serialize(buffer, values) {
365
- if (!isArrayLike(values)) {
366
- throw new TypeError(`Expected an array, got ${values}`);
367
- }
368
- buffer.writeInt16(values.length);
369
- for (const value of values) {
370
- type.serialize(buffer, value);
371
- }
372
- },
373
- deserialize(buffer, pointer) {
374
- const length = buffer.readUint16(pointer);
375
- const value = [];
376
- for (let i = 0; i < length; i++) {
377
- value.push(type.deserialize(buffer, pointer));
378
- }
379
- return value;
380
- },
381
- BIT_SIZE: null
382
- };
383
- }
384
- __name(ArrayType, "ArrayType");
385
-
386
- // src/lib/types/BigInt32.ts
387
- var BigInt32Type = {
388
- serialize(buffer, value) {
389
- buffer.writeBigInt32(value);
390
- },
391
- deserialize(buffer, pointer) {
392
- return buffer.readBigInt32(pointer);
393
- },
394
- BIT_SIZE: 32
395
- };
396
-
397
- // src/lib/types/BigInt64.ts
398
- var BigInt64Type = {
399
- serialize(buffer, value) {
400
- buffer.writeBigInt64(value);
401
- },
402
- deserialize(buffer, pointer) {
403
- return buffer.readBigInt64(pointer);
404
- },
405
- BIT_SIZE: 64
406
- };
407
-
408
- // src/lib/types/BigUint32.ts
409
- var BigUint32Type = {
410
- serialize(buffer, value) {
411
- buffer.writeBigInt32(value);
412
- },
413
- deserialize(buffer, pointer) {
414
- return buffer.readBigUint32(pointer);
415
- },
416
- BIT_SIZE: 32
417
- };
418
-
419
- // src/lib/types/BigUint64.ts
420
- var BigUint64Type = {
421
- serialize(buffer, value) {
422
- buffer.writeBigInt64(value);
423
- },
424
- deserialize(buffer, pointer) {
425
- return buffer.readBigUint64(pointer);
426
- },
427
- BIT_SIZE: 64
428
- };
429
-
430
- // src/lib/types/Bit.ts
431
- var BitType = {
432
- serialize(buffer, value) {
433
- buffer.writeBit(value & 1);
434
- },
435
- deserialize(buffer, pointer) {
436
- return buffer.readBit(pointer);
437
- },
438
- BIT_SIZE: 1
439
- };
440
-
441
- // src/lib/types/Boolean.ts
442
- var BooleanType = {
443
- serialize(buffer, value) {
444
- buffer.writeBit(Number(value));
445
- },
446
- deserialize(buffer, pointer) {
447
- return buffer.readBit(pointer) === 1;
448
- },
449
- BIT_SIZE: 1
450
- };
451
-
452
- // src/lib/types/Constant.ts
453
- function ConstantType(constantValue) {
454
- return {
455
- serialize(_buffer2, _value2) {
456
- },
457
- deserialize(_buffer2, _pointer) {
458
- return constantValue;
459
- },
460
- BIT_SIZE: 0
461
- };
462
- }
463
- __name(ConstantType, "ConstantType");
464
-
465
- // src/lib/types/FixedLengthArray.ts
466
- function FixedLengthArrayType(type, length) {
467
- return {
468
- serialize(buffer, values) {
469
- if (!isArrayLike(values) || values.length !== length) {
470
- throw new TypeError(`Expected array of length ${length}, got ${values.length}`);
471
- }
472
- for (let i = 0; i < length; i++) {
473
- type.serialize(buffer, values[i]);
474
- }
475
- },
476
- deserialize(buffer, pointer) {
477
- const value = [];
478
- for (let i = 0; i < length; i++) {
479
- value.push(type.deserialize(buffer, pointer));
480
- }
481
- return value;
482
- },
483
- BIT_SIZE: typeof type.BIT_SIZE === "number" ? type.BIT_SIZE * length : null
484
- };
485
- }
486
- __name(FixedLengthArrayType, "FixedLengthArrayType");
487
-
488
- // src/lib/types/Float32.ts
489
- var Float32Type = {
490
- serialize(buffer, value) {
491
- buffer.writeFloat32(value);
492
- },
493
- deserialize(buffer, pointer) {
494
- return buffer.readFloat32(pointer);
495
- },
496
- BIT_SIZE: 32
497
- };
498
-
499
- // src/lib/types/Float64.ts
500
- var Float64Type = {
501
- serialize(buffer, value) {
502
- buffer.writeFloat64(value);
503
- },
504
- deserialize(buffer, pointer) {
505
- return buffer.readFloat64(pointer);
506
- },
507
- BIT_SIZE: 64
508
- };
509
-
510
- // src/lib/types/Int16.ts
511
- var Int16Type = {
512
- serialize(buffer, value) {
513
- buffer.writeInt16(value);
514
- },
515
- deserialize(buffer, pointer) {
516
- return buffer.readInt16(pointer);
517
- },
518
- BIT_SIZE: 16
519
- };
520
-
521
- // src/lib/types/Int2.ts
522
- var Int2Type = {
523
- serialize(buffer, value) {
524
- buffer.writeInt2(value);
525
- },
526
- deserialize(buffer, pointer) {
527
- return buffer.readInt2(pointer);
528
- },
529
- BIT_SIZE: 2
530
- };
531
-
532
- // src/lib/types/Int32.ts
533
- var Int32Type = {
534
- serialize(buffer, value) {
535
- buffer.writeInt32(value);
536
- },
537
- deserialize(buffer, pointer) {
538
- return buffer.readInt32(pointer);
539
- },
540
- BIT_SIZE: 32
541
- };
542
-
543
- // src/lib/types/Int4.ts
544
- var Int4Type = {
545
- serialize(buffer, value) {
546
- buffer.writeInt4(value);
547
- },
548
- deserialize(buffer, pointer) {
549
- return buffer.readInt4(pointer);
550
- },
551
- BIT_SIZE: 4
552
- };
553
-
554
- // src/lib/types/Int64.ts
555
- var Int64Type = {
556
- serialize(buffer, value) {
557
- buffer.writeInt64(value);
558
- },
559
- deserialize(buffer, pointer) {
560
- return buffer.readInt64(pointer);
561
- },
562
- BIT_SIZE: 64
563
- };
564
-
565
- // src/lib/types/Int8.ts
566
- var Int8Type = {
567
- serialize(buffer, value) {
568
- buffer.writeInt8(value);
569
- },
570
- deserialize(buffer, pointer) {
571
- return buffer.readInt8(pointer);
572
- },
573
- BIT_SIZE: 8
574
- };
575
-
576
- // src/lib/types/Nullable.ts
577
- function NullableType(type) {
578
- return {
579
- serialize(buffer, value) {
580
- if (value === null || value === void 0) {
581
- buffer.writeBit(0);
582
- } else {
583
- buffer.writeBit(1);
584
- type.serialize(buffer, value);
585
- }
586
- },
587
- deserialize(buffer, pointer) {
588
- return buffer.readBit(pointer) ? type.deserialize(buffer, pointer) : null;
589
- },
590
- BIT_SIZE: null
591
- };
592
- }
593
- __name(NullableType, "NullableType");
594
-
595
- // src/lib/types/Snowflake.ts
596
- var SnowflakeType = {
597
- serialize(buffer, value) {
598
- buffer.writeBigInt64(BigInt(value));
599
- },
600
- deserialize(buffer, offset) {
601
- return buffer.readBigUint64(offset);
602
- },
603
- BIT_SIZE: 64
604
- };
605
-
606
- // src/lib/types/String.ts
607
- var encoder = new TextEncoder();
608
- var decoder = new TextDecoder();
609
- var StringType = {
610
- serialize(buffer, value) {
611
- const encoded = encoder.encode(value);
612
- buffer.writeInt16(encoded.length);
613
- for (const byte of encoded) {
614
- buffer.writeInt8(byte);
615
- }
616
- },
617
- deserialize(buffer, pointer) {
618
- const length = buffer.readInt16(pointer);
619
- const bytes = new Uint8Array(length);
620
- for (let i = 0; i < length; i++) {
621
- bytes[i] = buffer.readInt8(pointer);
622
- }
623
- return decoder.decode(bytes);
624
- },
625
- BIT_SIZE: null
626
- };
627
-
628
- // src/lib/types/Uint16.ts
629
- var Uint16Type = {
630
- serialize(buffer, value) {
631
- buffer.writeInt16(value);
632
- },
633
- deserialize(buffer, pointer) {
634
- return buffer.readUint16(pointer);
635
- },
636
- BIT_SIZE: 16
637
- };
638
-
639
- // src/lib/types/Uint2.ts
640
- var Uint2Type = {
641
- serialize(buffer, value) {
642
- buffer.writeInt2(value);
643
- },
644
- deserialize(buffer, pointer) {
645
- return buffer.readUint2(pointer);
646
- },
647
- BIT_SIZE: 2
648
- };
649
-
650
- // src/lib/types/Uint32.ts
651
- var Uint32Type = {
652
- serialize(buffer, value) {
653
- buffer.writeInt32(value);
654
- },
655
- deserialize(buffer, pointer) {
656
- return buffer.readUint32(pointer);
657
- },
658
- BIT_SIZE: 32
659
- };
660
-
661
- // src/lib/types/Uint4.ts
662
- var Uint4Type = {
663
- serialize(buffer, value) {
664
- buffer.writeInt4(value);
665
- },
666
- deserialize(buffer, pointer) {
667
- return buffer.readUint4(pointer);
668
- },
669
- BIT_SIZE: 4
670
- };
671
-
672
- // src/lib/types/Uint64.ts
673
- var Uint64Type = {
674
- serialize(buffer, value) {
675
- buffer.writeInt64(value);
676
- },
677
- deserialize(buffer, pointer) {
678
- return buffer.readUint64(pointer);
679
- },
680
- BIT_SIZE: 64
681
- };
682
-
683
- // src/lib/types/Uint8.ts
684
- var Uint8Type = {
685
- serialize(buffer, value) {
686
- buffer.writeInt8(value);
687
- },
688
- deserialize(buffer, pointer) {
689
- return buffer.readUint8(pointer);
690
- },
691
- BIT_SIZE: 8
692
- };
693
-
694
- // src/lib/types/index.ts
695
- var t = {
696
- array: ArrayType,
697
- bigInt32: BigInt32Type,
698
- bigInt64: BigInt64Type,
699
- bigUint32: BigUint32Type,
700
- bigUint64: BigUint64Type,
701
- bit: BitType,
702
- boolean: BooleanType,
703
- constant: ConstantType,
704
- fixedLengthArray: FixedLengthArrayType,
705
- float32: Float32Type,
706
- float64: Float64Type,
707
- int16: Int16Type,
708
- int2: Int2Type,
709
- int32: Int32Type,
710
- int4: Int4Type,
711
- int64: Int64Type,
712
- int8: Int8Type,
713
- nullable: NullableType,
714
- snowflake: SnowflakeType,
715
- string: StringType,
716
- uint16: Uint16Type,
717
- uint2: Uint2Type,
718
- uint32: Uint32Type,
719
- uint4: Uint4Type,
720
- uint64: Uint64Type,
721
- uint8: Uint8Type
722
- };
723
-
724
- // src/lib/schema/Schema.ts
725
- var _id, _types, _bitSize, _Schema_instances, addType_fn;
726
- var _Schema = class _Schema {
727
- /**
728
- * Creates a new schema.
729
- *
730
- * @param id The id of the schema
731
- */
732
- constructor(id) {
733
- __privateAdd(this, _Schema_instances);
734
- __privateAdd(this, _id);
735
- __privateAdd(this, _types, /* @__PURE__ */ new Map());
736
- __privateAdd(this, _bitSize, 0);
737
- __privateSet(this, _id, id);
738
- }
739
- /**
740
- * The id of the schema.
741
- */
742
- get id() {
743
- return __privateGet(this, _id);
744
- }
745
- /**
746
- * The bit size of the entries in the schema.
747
- *
748
- * @remarks
749
- *
750
- * If any of the entries have a bit size of `null`, the bit size of the
751
- * schema will also be `null`.
752
- */
753
- get bitSize() {
754
- return __privateGet(this, _bitSize);
755
- }
756
- /**
757
- * The total bit size of the entries in the schema and the ID.
758
- *
759
- * @remarks
760
- *
761
- * If any of the entries have a bit size of `null`, the total bit size of
762
- * the schema will also be `null`.
763
- */
764
- get totalBitSize() {
765
- return __privateGet(this, _bitSize) === null ? null : __privateGet(this, _bitSize) + 16;
766
- }
767
- /**
768
- * Get a property from the schema.
769
- *
770
- * @param name The name of the property
771
- * @returns The specified property
772
- *
773
- * @remarks
774
- *
775
- * If the property does not exist, an error will be thrown.
776
- */
777
- get(name) {
778
- const type = __privateGet(this, _types).get(name);
779
- if (!type) throw new Error(`Schema with id ${__privateGet(this, _id)} does not have a property with name "${name}"`);
780
- return type;
781
- }
782
- /**
783
- * Create a buffer and serialize a value into it, then convert it to a string
784
- *
785
- * @param value The value to serialize into the buffer
786
- * @param defaultMaximumArrayLength The default maximum array length, if any
787
- * @returns The newly created string.
788
- *
789
- * @seealso This method calls {@link Schema.serializeRaw} before calling `toString()` to its result.
790
- */
791
- serialize(value, defaultMaximumArrayLength = 100) {
792
- return this.serializeRaw(value, defaultMaximumArrayLength).toString();
793
- }
794
- /**
795
- * Create a buffer and serialize a value into it.
796
- *
797
- * @param value The value to serialize into the buffer
798
- * @param defaultMaximumArrayLength The default maximum array length, if any
799
- * @returns The newly created buffer.
800
- */
801
- serializeRaw(value, defaultMaximumArrayLength = 100) {
802
- const buffer = new UnalignedUint16Array(this.totalBitSize ?? defaultMaximumArrayLength);
803
- this.serializeInto(buffer, value);
804
- return buffer;
805
- }
806
- /**
807
- * Serialize a value into a buffer.
808
- *
809
- * @param buffer The buffer to serialize
810
- * @param value The value to serialize into the buffer
811
- *
812
- * @remarks
813
- *
814
- * The schema's ID is written to the buffer first, followed by each property
815
- * in the schema.
816
- */
817
- serializeInto(buffer, value) {
818
- buffer.writeInt16(__privateGet(this, _id));
819
- for (const [name, type] of this) {
820
- type.serialize(buffer, value[name]);
821
- }
822
- }
823
- /**
824
- * Deserialize a value from a buffer.
825
- *
826
- * @param buffer The buffer to deserialize
827
- * @param pointer The pointer to where the buffer should be read from
828
- * @returns The deserialized value
829
- *
830
- * @remarks
831
- *
832
- * Unlike {@link Schema.serializeInto}, this method does not read the schema's ID
833
- * from the buffer, that is reserved for the {@link SchemaStore}.
834
- */
835
- deserialize(buffer, pointer) {
836
- buffer = UnalignedUint16Array.from(buffer);
837
- pointer = Pointer.from(pointer);
838
- const result = /* @__PURE__ */ Object.create(null);
839
- for (const [name, type] of this) {
840
- result[name] = type.deserialize(buffer, pointer);
841
- }
842
- return result;
843
- }
844
- /**
845
- * Adds an array property to the schema.
846
- *
847
- * @seealso {@link Schema.fixedLengthArray} for a fixed length array
848
- *
849
- * @param name The name of the property
850
- * @param type The type of the entry in the array
851
- * @returns The modified schema
852
- */
853
- array(name, type) {
854
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.array(type));
855
- }
856
- /**
857
- * Adds a fixed length array property to the schema.
858
- *
859
- * @seealso {@link Schema.array} for a dynamic length array
860
- *
861
- * @param name The name of the property
862
- * @param type The type of the entry in the array
863
- * @param length The length of the array
864
- * @returns The modified schema
865
- */
866
- fixedLengthArray(name, type, length) {
867
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.fixedLengthArray(type, length));
868
- }
869
- /**
870
- * Adds a string property to the schema.
871
- *
872
- * @param name The name of the property
873
- * @returns The modified schema
874
- */
875
- string(name) {
876
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.string);
877
- }
878
- /**
879
- * Adds a boolean property to the schema.
880
- *
881
- * @param name The name of the property
882
- * @returns The modified schema
883
- */
884
- boolean(name) {
885
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.boolean);
886
- }
887
- /**
888
- * Adds a bit property to the schema.
889
- *
890
- * @param name The name of the property
891
- * @returns The modified schema
892
- */
893
- bit(name) {
894
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.bit);
895
- }
896
- /**
897
- * Adds a 2-bit integer property to the schema.
898
- *
899
- * @remarks
900
- *
901
- * The range of values is from -2 to 1, inclusive.
902
- *
903
- * @param name The name of the property
904
- * @returns The modified schema
905
- */
906
- int2(name) {
907
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.int2);
908
- }
909
- /**
910
- * Adds a 2-bit unsigned integer property to the schema.
911
- *
912
- * @remarks
913
- *
914
- * The range of values is from 0 to 3, inclusive.
915
- *
916
- * @param name The name of the property
917
- * @returns The modified schema
918
- */
919
- uint2(name) {
920
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.uint2);
921
- }
922
- /**
923
- * Adds a 4-bit integer property to the schema.
924
- *
925
- * @remarks
926
- *
927
- * The range of values is from -8 to 7, inclusive.
928
- *
929
- * @param name The name of the property
930
- * @returns The modified schema
931
- */
932
- int4(name) {
933
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.int4);
934
- }
935
- /**
936
- * Adds a 4-bit unsigned integer property to the schema.
937
- *
938
- * @remarks
939
- *
940
- * The range of values is from 0 to 15, inclusive.
941
- *
942
- * @param name The name of the property
943
- * @returns The modified schema
944
- */
945
- uint4(name) {
946
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.uint4);
947
- }
948
- /**
949
- * Adds a 8-bit integer property to the schema.
950
- *
951
- * @remarks
952
- *
953
- * The range of values is from -128 to 127, inclusive.
954
- *
955
- * @param name The name of the property
956
- * @returns The modified schema
957
- */
958
- int8(name) {
959
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.int8);
960
- }
961
- /**
962
- * Adds a 8-bit unsigned integer property to the schema.
963
- *
964
- * @remarks
965
- *
966
- * The range of values is from 0 to 255, inclusive.
967
- *
968
- * @param name The name of the property
969
- * @returns The modified schema
970
- */
971
- uint8(name) {
972
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.uint8);
973
- }
974
- /**
975
- * Adds a 16-bit integer property to the schema.
976
- *
977
- * @remarks
978
- *
979
- * The range of values is from -32768 to 32767, inclusive.
980
- *
981
- * @param name The name of the property
982
- * @returns The modified schema
983
- */
984
- int16(name) {
985
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.int16);
986
- }
987
- /**
988
- * Adds a 16-bit unsigned integer property to the schema.
989
- *
990
- * @remarks
991
- *
992
- * The range of values is from 0 to 65535, inclusive.
993
- *
994
- * @param name The name of the property
995
- * @returns The modified schema
996
- */
997
- uint16(name) {
998
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.uint16);
999
- }
1000
- /**
1001
- * Adds a 32-bit integer property to the schema.
1002
- *
1003
- * @remarks
1004
- *
1005
- * The range of values is from -2_147_483_648 to 2_147_483_647, inclusive.
1006
- *
1007
- * @param name The name of the property
1008
- * @returns The modified schema
1009
- */
1010
- int32(name) {
1011
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.int32);
1012
- }
1013
- /**
1014
- * Adds a 32-bit unsigned integer property to the schema.
1015
- *
1016
- * @remarks
1017
- *
1018
- * The range of values is from 0 to 4_294_967_295, inclusive.
1019
- *
1020
- * @param name The name of the property
1021
- * @returns The modified schema
1022
- */
1023
- uint32(name) {
1024
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.uint32);
1025
- }
1026
- /**
1027
- * Adds a 64-bit integer property to the schema.
1028
- *
1029
- * @remarks
1030
- *
1031
- * The range of values is from -9_223_372_036_854_775_808 to 9_223_372_036_854_775_807, inclusive.
1032
- *
1033
- * However, it may run into precision issues past the range of `-9_007_199_254_740_991` to `9_007_199_254_740_991`
1034
- *
1035
- * @param name The name of the property
1036
- * @returns The modified schema
1037
- */
1038
- int64(name) {
1039
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.int64);
1040
- }
1041
- /**
1042
- * Adds a 64-bit unsigned integer property to the schema.
1043
- *
1044
- * @remarks
1045
- *
1046
- * The range of values is from 0 to 18_446_744_073_709_551_615, inclusive.
1047
- *
1048
- * However, it may run into precision issues past `9_007_199_254_740_991`
1049
- *
1050
- * @param name The name of the property
1051
- * @returns The modified schema
1052
- */
1053
- uint64(name) {
1054
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.uint64);
1055
- }
1056
- /**
1057
- * Adds a 32-bit big integer property to the schema.
1058
- *
1059
- * @remarks
1060
- *
1061
- * The range of values is from -2_147_483_648n to 2_147_483_647n, inclusive.
1062
- *
1063
- * @param name The name of the property
1064
- * @returns The modified schema
1065
- */
1066
- bigInt32(name) {
1067
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.bigInt32);
1068
- }
1069
- /**
1070
- * Adds a 32-bit big integer property to the schema.
1071
- *
1072
- * @remarks
1073
- *
1074
- * The range of values is from 0n to 4_294_967_295n, inclusive.
1075
- *
1076
- * @param name The name of the property
1077
- * @returns The modified schema
1078
- */
1079
- bigUint32(name) {
1080
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.bigUint32);
1081
- }
1082
- /**
1083
- * Adds a 64-bit big integer property to the schema.
1084
- *
1085
- * @remarks
1086
- *
1087
- * The range of values is from -9_223_372_036_854_775_808n to 9_223_372_036_854_775_807n, inclusive.
1088
- *
1089
- * @param name The name of the property
1090
- * @returns The modified schema
1091
- */
1092
- bigInt64(name) {
1093
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.bigInt64);
1094
- }
1095
- /**
1096
- * Adds a 64-bit big integer property to the schema.
1097
- *
1098
- * @remarks
1099
- *
1100
- * The range of values is from 0n to 18_446_744_073_709_551_615n, inclusive.
1101
- *
1102
- * @param name The name of the property
1103
- * @returns The modified schema
1104
- */
1105
- bigUint64(name) {
1106
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.bigUint64);
1107
- }
1108
- /**
1109
- * Adds a 32-bit floating point number property to the schema.
1110
- *
1111
- * @remarks
1112
- *
1113
- * The range of values is from -3.4028234663852886e+38 to 3.4028234663852886e+38, inclusive.
1114
- *
1115
- * @param name The name of the property
1116
- * @returns The modified schema
1117
- */
1118
- float32(name) {
1119
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.float32);
1120
- }
1121
- /**
1122
- * Adds a 64-bit floating point number property to the schema.
1123
- *
1124
- * @remarks
1125
- *
1126
- * The range of values is from -1.7976931348623157e+308 to 1.7976931348623157e+308, inclusive.
1127
- *
1128
- * @param name The name of the property
1129
- * @returns The modified schema
1130
- */
1131
- float64(name) {
1132
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.float64);
1133
- }
1134
- /**
1135
- * Adds a nullable property to the schema.
1136
- *
1137
- * @param name The name of the property
1138
- * @param type The type of the underlying value
1139
- * @returns The modified schema
1140
- */
1141
- nullable(name, type) {
1142
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.nullable(type));
1143
- }
1144
- /**
1145
- * Adds a 64-bit big integer property to the schema, similar to {@link Schema.bigUint64}.
1146
- *
1147
- * @remarks
1148
- *
1149
- * The range of values is from 0n to 18_446_744_073_709_551_615n, inclusive.
1150
- *
1151
- * @param name The name of the property
1152
- * @returns The modified schema
1153
- */
1154
- snowflake(name) {
1155
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.snowflake);
1156
- }
1157
- /**
1158
- * Adds a constant value in the schema, this will **not** be serialized and
1159
- * can be used to add extra data without making the payload bigger.
1160
- *
1161
- * @param name The name of the property
1162
- * @param constantValue The value to add to the schema
1163
- * @returns The modified schema
1164
- */
1165
- constant(name, constantValue) {
1166
- return __privateMethod(this, _Schema_instances, addType_fn).call(this, name, t.constant(constantValue));
1167
- }
1168
- /**
1169
- * Iterates over the schema's property names.
1170
- *
1171
- * @returns An iterator for the schema's property names
1172
- */
1173
- keys() {
1174
- return __privateGet(this, _types).keys();
1175
- }
1176
- /**
1177
- * Iterates over the schema's property values
1178
- *
1179
- * @returns An iterator for the schema's property values
1180
- */
1181
- values() {
1182
- return __privateGet(this, _types).values();
1183
- }
1184
- /**
1185
- * Iterates over the schema's property entries
1186
- *
1187
- * @returns An iterator for the schema's property entries
1188
- */
1189
- entries() {
1190
- return __privateGet(this, _types).entries();
1191
- }
1192
- /**
1193
- * Iterates over the schema's property entries
1194
- *
1195
- * @returns An iterator for the schema's property entries
1196
- */
1197
- [Symbol.iterator]() {
1198
- return this.entries();
1199
- }
1200
- };
1201
- _id = new WeakMap();
1202
- _types = new WeakMap();
1203
- _bitSize = new WeakMap();
1204
- _Schema_instances = new WeakSet();
1205
- addType_fn = /* @__PURE__ */ __name(function(name, type) {
1206
- if (__privateGet(this, _types).has(name)) {
1207
- throw new Error(`Schema with id ${__privateGet(this, _id)} already has a property with name "${name}"`);
1208
- }
1209
- __privateGet(this, _types).set(name, type);
1210
- if (type.BIT_SIZE === null) {
1211
- __privateSet(this, _bitSize, null);
1212
- } else if (__privateGet(this, _bitSize) !== null) {
1213
- __privateSet(this, _bitSize, __privateGet(this, _bitSize) + type.BIT_SIZE);
1214
- }
1215
- return this;
1216
- }, "#addType");
1217
- __name(_Schema, "Schema");
1218
- var Schema = _Schema;
1219
-
1220
- // src/lib/schema/SchemaStore.ts
1221
- var _schemas;
1222
- var _SchemaStore = class _SchemaStore {
1223
- /**
1224
- * Creates a new schema store
1225
- *
1226
- * @param defaultMaximumArrayLength The default maximum array length for schemas
1227
- */
1228
- constructor(defaultMaximumArrayLength = 100) {
1229
- /**
1230
- * The default maximum array length for schemas
1231
- */
1232
- __publicField(this, "defaultMaximumArrayLength");
1233
- __privateAdd(this, _schemas, /* @__PURE__ */ new Map());
1234
- this.defaultMaximumArrayLength = defaultMaximumArrayLength;
1235
- }
1236
- /**
1237
- * Adds a schema to the store
1238
- *
1239
- * @param schema The schema to add to the store
1240
- * @returns The modified store
1241
- *
1242
- * @remarks
1243
- *
1244
- * An error will be thrown if a schema with the same id already exists in the store.
1245
- */
1246
- add(schema) {
1247
- if (__privateGet(this, _schemas).has(schema.id)) {
1248
- throw new Error(`Schema with id ${schema.id} already exists`);
1249
- }
1250
- __privateGet(this, _schemas).set(schema.id, schema);
1251
- return this;
1252
- }
1253
- /**
1254
- * Gets a schema from the store
1255
- *
1256
- * @param id The id of the schema to get
1257
- * @returns The schema with the given id
1258
- *
1259
- * @remarks
1260
- *
1261
- * An error will be thrown if a schema with the given id does not exist in the store.
1262
- */
1263
- get(id) {
1264
- const schema = __privateGet(this, _schemas).get(id);
1265
- if (!schema) throw new Error(`Schema with id ${id} does not exist`);
1266
- return schema;
1267
- }
1268
- /**
1269
- * Serializes a value using the schema with the given id
1270
- *
1271
- * @param id The id of the schema to use for serialization
1272
- * @param value The value to serialize
1273
- * @returns The serialized string
1274
- */
1275
- serialize(id, value) {
1276
- return this.serializeRaw(id, value).toString();
1277
- }
1278
- /**
1279
- * Serializes a value using the schema with the given id
1280
- *
1281
- * @param id The id of the schema to use for serialization
1282
- * @param value The value to serialize
1283
- * @returns The serialized buffer
1284
- */
1285
- serializeRaw(id, value) {
1286
- const schema = this.get(id);
1287
- return schema.serializeRaw(value, this.defaultMaximumArrayLength);
1288
- }
1289
- /**
1290
- * Deserializes a buffer
1291
- *
1292
- * @param buffer The buffer to deserialize
1293
- * @returns The resolved value, including the id of the schema used for deserialization
1294
- */
1295
- deserialize(buffer) {
1296
- buffer = UnalignedUint16Array.from(buffer);
1297
- const pointer = new Pointer();
1298
- const id = buffer.readInt16(pointer);
1299
- const schema = this.get(id);
1300
- return { id, data: schema.deserialize(buffer, pointer) };
1301
- }
1302
- /**
1303
- * Gets the serialized ID from a buffer
1304
- *
1305
- * @param buffer The buffer to read the ID from
1306
- * @returns The deserialized ID
1307
- *
1308
- * @remarks
1309
- *
1310
- * If an empty value is passed, a {@linkcode RangeError} will be thrown.
1311
- */
1312
- getIdentifier(buffer) {
1313
- if (buffer.length === 0) {
1314
- throw new RangeError("Expected a non-empty value");
1315
- }
1316
- if (typeof buffer === "string") {
1317
- buffer = UnalignedUint16Array.from(buffer[0]);
1318
- }
1319
- return buffer.at(0);
1320
- }
1321
- /**
1322
- * Iterates over the stores's schema identifiers.
1323
- *
1324
- * @returns An iterator for the stores's schema identifiers
1325
- */
1326
- keys() {
1327
- return __privateGet(this, _schemas).keys();
1328
- }
1329
- /**
1330
- * Iterates over the stores's schemas.
1331
- *
1332
- * @returns An iterator for the stores's schemas
1333
- */
1334
- values() {
1335
- return __privateGet(this, _schemas).values();
1336
- }
1337
- /**
1338
- * Iterates over the stores's schema entries.
1339
- *
1340
- * @returns An iterator for the stores's schema entries
1341
- */
1342
- entries() {
1343
- return __privateGet(this, _schemas).entries();
1344
- }
1345
- /**
1346
- * Iterates over the stores's schema entries.
1347
- *
1348
- * @returns An iterator for the stores's schema entries
1349
- */
1350
- [Symbol.iterator]() {
1351
- return this.entries();
1352
- }
1353
- };
1354
- _schemas = new WeakMap();
1355
- __name(_SchemaStore, "SchemaStore");
1356
- var SchemaStore = _SchemaStore;
1357
-
1358
- // src/lib/utilities.ts
1359
- function toUTF16(buffer) {
1360
- let result = "";
1361
- for (const value of new Uint16Array(buffer.buffer)) {
1362
- result += String.fromCharCode(value);
1363
- }
1364
- return result;
1365
- }
1366
- __name(toUTF16, "toUTF16");
1367
- function fromUTF16(buffer) {
1368
- const result = new Uint16Array(buffer.length);
1369
- for (let i = 0; i < buffer.length; i++) {
1370
- result[i] = buffer.charCodeAt(i);
1371
- }
1372
- return result;
1373
- }
1374
- __name(fromUTF16, "fromUTF16");
1375
-
1376
- exports.ArrayType = ArrayType;
1377
- exports.BigInt32Type = BigInt32Type;
1378
- exports.BigInt64Type = BigInt64Type;
1379
- exports.BigUint32Type = BigUint32Type;
1380
- exports.BigUint64Type = BigUint64Type;
1381
- exports.BitType = BitType;
1382
- exports.BooleanType = BooleanType;
1383
- exports.ConstantType = ConstantType;
1384
- exports.FixedLengthArrayType = FixedLengthArrayType;
1385
- exports.Float32Type = Float32Type;
1386
- exports.Float64Type = Float64Type;
1387
- exports.Int16Type = Int16Type;
1388
- exports.Int2Type = Int2Type;
1389
- exports.Int32Type = Int32Type;
1390
- exports.Int4Type = Int4Type;
1391
- exports.Int64Type = Int64Type;
1392
- exports.Int8Type = Int8Type;
1393
- exports.NullableType = NullableType;
1394
- exports.Pointer = Pointer;
1395
- exports.Schema = Schema;
1396
- exports.SchemaStore = SchemaStore;
1397
- exports.SnowflakeType = SnowflakeType;
1398
- exports.StringType = StringType;
1399
- exports.Uint16Type = Uint16Type;
1400
- exports.Uint2Type = Uint2Type;
1401
- exports.Uint32Type = Uint32Type;
1402
- exports.Uint4Type = Uint4Type;
1403
- exports.Uint64Type = Uint64Type;
1404
- exports.Uint8Type = Uint8Type;
1405
- exports.UnalignedUint16Array = UnalignedUint16Array;
1406
- exports.codepointRanges = codepointRanges;
1407
- exports.fromUTF16 = fromUTF16;
1408
- exports.t = t;
1409
- exports.toUTF16 = toUTF16;
1410
-
1411
- return exports;
1412
-
1413
- })({});
1414
- //# sourceMappingURL=index.global.js.map
1415
- //# sourceMappingURL=index.global.js.map