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