@sapphire/string-store 1.1.0-next.7c2bed20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,423 @@
1
+ /**
2
+ * A pointer to a position in a buffer.
3
+ *
4
+ * This is used to keep track of the current position in a buffer while allowing
5
+ * the position to be updated by multiple different functions.
6
+ *
7
+ * @privateRemarks
8
+ *
9
+ * This class draws inspiration from the following constructs:
10
+ * - `int*` in C/C++
11
+ * - `ref int` in C#
12
+ * - `*mut i32` in Rust
13
+ */
14
+ declare class Pointer {
15
+ #private;
16
+ get value(): number;
17
+ add(value: number): this;
18
+ static from(pointer: PointerLike): Pointer;
19
+ }
20
+ type PointerLike = Pointer | {
21
+ valueOf(): number;
22
+ } | {
23
+ [Symbol.toPrimitive](hint: 'number'): number;
24
+ };
25
+
26
+ declare class UnalignedUint16Array {
27
+ #private;
28
+ constructor(maxLength: number);
29
+ get maxLength(): number;
30
+ get maxBitLength(): number;
31
+ get length(): number;
32
+ get bitLength(): number;
33
+ writeBit(value: number): void;
34
+ writeInt2(value: number): void;
35
+ writeInt4(value: number): void;
36
+ writeInt8(value: number): void;
37
+ writeInt16(value: number): void;
38
+ writeInt32(value: number): void;
39
+ writeInt64(value: number): void;
40
+ writeBigInt32(value: bigint): void;
41
+ writeBigInt64(value: bigint): void;
42
+ writeFloat32(value: number): void;
43
+ writeFloat64(value: number): void;
44
+ readBit(offset: PointerLike): 0 | 1;
45
+ readInt2(offset: PointerLike): number;
46
+ readInt4(offset: PointerLike): number;
47
+ readInt8(offset: PointerLike): number;
48
+ readInt16(offset: PointerLike): number;
49
+ readInt32(offset: PointerLike): number;
50
+ readInt64(offset: PointerLike): number;
51
+ readBigInt32(offset: PointerLike): bigint;
52
+ readBigInt64(offset: PointerLike): bigint;
53
+ readFloat32(offset: PointerLike): number;
54
+ readFloat64(offset: PointerLike): number;
55
+ toString(): string;
56
+ toArray(): Uint16Array;
57
+ static from(value: string | UnalignedUint16Array): UnalignedUint16Array;
58
+ }
59
+
60
+ interface IType<ValueType, BitSize extends number | null> {
61
+ /**
62
+ * Serialize a value to a buffer.
63
+ *
64
+ * @param buffer The buffer to write to
65
+ * @param value The value to write
66
+ */
67
+ serialize(buffer: UnalignedUint16Array, value: Readonly<ValueType>): void;
68
+ /**
69
+ * Deserialize a value from a buffer.
70
+ *
71
+ * @param buffer The buffer to read from
72
+ * @param pointer The pointer indicating the current position in the buffer
73
+ */
74
+ deserialize(buffer: UnalignedUint16Array, pointer: Pointer): ValueType;
75
+ /**
76
+ * The size of the value in bits, or `null` if the size is variable.
77
+ */
78
+ readonly BIT_SIZE: BitSize;
79
+ }
80
+
81
+ declare class Schema<Id extends number = number, Entries extends object = object> {
82
+ #private;
83
+ /**
84
+ * Creates a new schema.
85
+ *
86
+ * @param id The id of the schema
87
+ */
88
+ constructor(id: Id);
89
+ /**
90
+ * The id of the schema.
91
+ */
92
+ get id(): Id;
93
+ /**
94
+ * The total bit size of the schema.
95
+ *
96
+ * @remarks
97
+ *
98
+ * If any of the entries have a bit size of `null`, the bit size of the
99
+ * schema will also be `null`.
100
+ */
101
+ get bitSize(): number | null;
102
+ /**
103
+ * Get a property from the schema.
104
+ *
105
+ * @param name The name of the property
106
+ * @returns The specified property
107
+ *
108
+ * @remarks
109
+ *
110
+ * If the property does not exist, an error will be thrown.
111
+ */
112
+ get<const Name extends keyof Entries & string>(name: Name): Entries[Name];
113
+ /**
114
+ * Serialize a value into a buffer.
115
+ *
116
+ * @param buffer The buffer to serialize
117
+ * @param value The value to serialize into the buffer
118
+ *
119
+ * @remarks
120
+ *
121
+ * The schema's ID is written to the buffer first, followed by each property
122
+ * in the schema.
123
+ */
124
+ serialize(buffer: UnalignedUint16Array, value: Readonly<UnwrapSchemaEntries<Entries>>): void;
125
+ /**
126
+ * Deserialize a value from a buffer.
127
+ *
128
+ * @param buffer The buffer to deserialize
129
+ * @param pointer The pointer to where the buffer should be read from
130
+ * @returns The deserialized value
131
+ *
132
+ * @remarks
133
+ *
134
+ * Unlike {@link Schema.serialize}, this method does not read the schema's ID
135
+ * from the buffer, that is reserved for the {@link SchemaStore}.
136
+ */
137
+ deserialize(buffer: UnalignedUint16Array, pointer: PointerLike): UnwrapSchemaEntries<Entries>;
138
+ /**
139
+ * Adds an array property to the schema.
140
+ *
141
+ * @seealso {@link Schema.fixedLengthArray} for a fixed length array
142
+ *
143
+ * @param name The name of the property
144
+ * @param type The type of the entry in the array
145
+ * @returns The modified schema
146
+ */
147
+ array<const Name extends string, const ValueType, const ValueBitSize extends number | null>(name: Name, type: IType<ValueType, ValueBitSize>): Merge$1<Id, Entries, Name, IType<ValueType[], null>>;
148
+ /**
149
+ * Adds a fixed length array property to the schema.
150
+ *
151
+ * @seealso {@link Schema.array} for a dynamic length array
152
+ *
153
+ * @param name The name of the property
154
+ * @param type The type of the entry in the array
155
+ * @param length The length of the array
156
+ * @returns The modified schema
157
+ */
158
+ fixedLengthArray<const Name extends string, const ValueType, const ValueBitSize extends number | null>(name: Name, type: IType<ValueType, ValueBitSize>, length: number): Merge$1<Id, Entries, Name, IType<ValueType[], ValueBitSize extends null ? null : number>>;
159
+ /**
160
+ * Adds a string property to the schema.
161
+ *
162
+ * @param name The name of the property
163
+ * @returns The modified schema
164
+ */
165
+ string<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<string, null>>;
166
+ /**
167
+ * Adds a boolean property to the schema.
168
+ *
169
+ * @param name The name of the property
170
+ * @returns The modified schema
171
+ */
172
+ boolean<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<boolean, 1>>;
173
+ /**
174
+ * Adds a bit property to the schema.
175
+ *
176
+ * @param name The name of the property
177
+ * @returns The modified schema
178
+ */
179
+ bit<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 1>>;
180
+ /**
181
+ * Adds a 2-bit integer property to the schema.
182
+ *
183
+ * @param name The name of the property
184
+ * @returns The modified schema
185
+ */
186
+ int2<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 2>>;
187
+ /**
188
+ * Adds a 4-bit integer property to the schema.
189
+ *
190
+ * @param name The name of the property
191
+ * @returns The modified schema
192
+ */
193
+ int4<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 4>>;
194
+ /**
195
+ * Adds a 8-bit integer property to the schema.
196
+ *
197
+ * @param name The name of the property
198
+ * @returns The modified schema
199
+ */
200
+ int8<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 8>>;
201
+ /**
202
+ * Adds a 16-bit integer property to the schema.
203
+ *
204
+ * @param name The name of the property
205
+ * @returns The modified schema
206
+ */
207
+ int16<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 16>>;
208
+ /**
209
+ * Adds a 32-bit integer property to the schema.
210
+ *
211
+ * @param name The name of the property
212
+ * @returns The modified schema
213
+ */
214
+ int32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32>>;
215
+ /**
216
+ * Adds a 64-bit integer property to the schema.
217
+ *
218
+ * @param name The name of the property
219
+ * @returns The modified schema
220
+ */
221
+ int64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64>>;
222
+ /**
223
+ * Adds a 32-bit big integer property to the schema.
224
+ *
225
+ * @param name The name of the property
226
+ * @returns The modified schema
227
+ */
228
+ bigInt32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 32>>;
229
+ /**
230
+ * Adds a 64-bit big integer property to the schema.
231
+ *
232
+ * @param name The name of the property
233
+ * @returns The modified schema
234
+ */
235
+ bigInt64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64>>;
236
+ /**
237
+ * Adds a 32-bit floating point number property to the schema.
238
+ *
239
+ * @param name The name of the property
240
+ * @returns The modified schema
241
+ */
242
+ float32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32>>;
243
+ /**
244
+ * Adds a 64-bit floating point number property to the schema.
245
+ *
246
+ * @param name The name of the property
247
+ * @returns The modified schema
248
+ */
249
+ float64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64>>;
250
+ /**
251
+ * Adds a 64-bit big integer property to the schema, similar to {@link Schema.bigInt64}.
252
+ *
253
+ * @param name The name of the property
254
+ * @returns The modified schema
255
+ */
256
+ snowflake<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64>>;
257
+ /**
258
+ * Iterates over the schema's property names.
259
+ *
260
+ * @returns An iterator for the schema's property names
261
+ */
262
+ keys(): IterableIterator<KeyOfSchema<this>>;
263
+ /**
264
+ * Iterates over the schema's property values
265
+ *
266
+ * @returns An iterator for the schema's property values
267
+ */
268
+ values(): IterableIterator<ValueOfSchema<this>>;
269
+ /**
270
+ * Iterates over the schema's property entries
271
+ *
272
+ * @returns An iterator for the schema's property entries
273
+ */
274
+ entries(): IterableIterator<EntryOfSchema<this>>;
275
+ /**
276
+ * Iterates over the schema's property entries
277
+ *
278
+ * @returns An iterator for the schema's property entries
279
+ */
280
+ [Symbol.iterator](): IterableIterator<EntryOfSchema<this>>;
281
+ }
282
+ type Merge$1<Id extends number, Entries extends object, EntryName extends string, EntryType extends IType<any, number | null>> = EntryName extends keyof Entries ? never : Schema<Id, {
283
+ [K in EntryName | keyof Entries]: K extends keyof Entries ? Entries[K] : EntryType;
284
+ }>;
285
+ type KeyOfSchema<SchemaValue extends object> = SchemaValue extends Schema<infer _, infer Type> ? keyof Type & string : never;
286
+ type ValueOfSchema<SchemaValue extends object> = SchemaValue extends Schema<infer _, infer Type> ? {
287
+ [K in keyof Type]: Type[K];
288
+ }[keyof Type] : never;
289
+ type EntryOfSchema<SchemaValue extends object> = SchemaValue extends Schema<infer _, infer Type> ? {
290
+ [K in keyof Type]: readonly [K, Type[K]];
291
+ }[keyof Type] : never;
292
+ type UnwrapSchemaType<Type extends object> = Type extends IType<infer T, infer _> ? T : never;
293
+ type UnwrapSchemaEntries<Entries extends object> = {
294
+ [K in keyof Entries]: UnwrapSchemaType<Entries[K] & object>;
295
+ } & object;
296
+ type UnwrapSchema<SchemaValue extends object> = SchemaValue extends Schema<infer _, infer Type> ? UnwrapSchemaEntries<Type> : never;
297
+
298
+ declare class SchemaStore<Entries extends object = object> {
299
+ #private;
300
+ /**
301
+ * The default maximum array length for schemas
302
+ */
303
+ defaultMaximumArrayLength: number;
304
+ /**
305
+ * Creates a new schema store
306
+ *
307
+ * @param defaultMaximumArrayLength The default maximum array length for schemas
308
+ */
309
+ constructor(defaultMaximumArrayLength?: number);
310
+ /**
311
+ * Adds a schema to the store
312
+ *
313
+ * @param schema The schema to add to the store
314
+ * @returns The modified store
315
+ *
316
+ * @remarks
317
+ *
318
+ * An error will be thrown if a schema with the same id already exists in the store.
319
+ */
320
+ add<const Id extends number, const SchemaType extends object>(schema: Schema<Id, SchemaType>): Merge<Entries, Id, typeof schema>;
321
+ /**
322
+ * Gets a schema from the store
323
+ *
324
+ * @param id The id of the schema to get
325
+ * @returns The schema with the given id
326
+ *
327
+ * @remarks
328
+ *
329
+ * An error will be thrown if a schema with the given id does not exist in the store.
330
+ */
331
+ get<const Id extends KeyOfStore<this>>(id: Id): Entries[Id];
332
+ /**
333
+ * Serializes a value using the schema with the given id
334
+ *
335
+ * @param id The id of the schema to use for serialization
336
+ * @param value The value to serialize
337
+ * @returns The serialized buffer
338
+ */
339
+ serialize<const Id extends KeyOfStore<this>>(id: Id, value: Readonly<UnwrapSchema<Entries[Id] & object>>): UnalignedUint16Array;
340
+ /**
341
+ * Deserializes a buffer
342
+ *
343
+ * @param buffer The buffer to deserialize
344
+ * @returns The resolved value, including the id of the schema used for deserialization
345
+ */
346
+ deserialize(buffer: string | UnalignedUint16Array): DeserializationResult<Entries>;
347
+ /**
348
+ * Iterates over the stores's schema identifiers.
349
+ *
350
+ * @returns An iterator for the stores's schema identifiers
351
+ */
352
+ keys(): IterableIterator<KeyOfStore<this>>;
353
+ /**
354
+ * Iterates over the stores's schemas.
355
+ *
356
+ * @returns An iterator for the stores's schemas
357
+ */
358
+ values(): IterableIterator<ValueOfStore<this>>;
359
+ /**
360
+ * Iterates over the stores's schema entries.
361
+ *
362
+ * @returns An iterator for the stores's schema entries
363
+ */
364
+ entries(): IterableIterator<EntryOfStore<this>>;
365
+ /**
366
+ * Iterates over the stores's schema entries.
367
+ *
368
+ * @returns An iterator for the stores's schema entries
369
+ */
370
+ [Symbol.iterator](): IterableIterator<EntryOfStore<this>>;
371
+ }
372
+ type Merge<Entries extends object, Id extends number, Type extends object> = Id extends keyof Entries ? never : SchemaStore<{
373
+ [K in Id | keyof Entries]: K extends keyof Entries ? Entries[K] : Type;
374
+ }>;
375
+ type KeyOfStore<SchemaStoreValue extends object> = SchemaStoreValue extends SchemaStore<infer Schemas> ? keyof Schemas & number : never;
376
+ type ValueOfStore<SchemaStoreValue extends object> = SchemaStoreValue extends SchemaStore<infer Schemas> ? Schemas[keyof Schemas & number] : never;
377
+ type EntryOfStore<SchemaStoreValue extends object> = SchemaStoreValue extends SchemaStore<infer Schemas> ? {
378
+ [K in keyof Schemas]: readonly [K & number, Schemas[K]];
379
+ }[keyof Schemas] : never;
380
+ type DeserializationResult<SchemaStoreEntries extends object> = {
381
+ [K in keyof SchemaStoreEntries]: {
382
+ id: K;
383
+ data: UnwrapSchema<SchemaStoreEntries[K] & object>;
384
+ };
385
+ }[keyof SchemaStoreEntries];
386
+
387
+ declare function ArrayType<ValueType, ValueBitSize extends number | null>(type: IType<ValueType, ValueBitSize>): IType<ValueType[], null>;
388
+
389
+ declare const BigInt32Type: IType<bigint, 32>;
390
+
391
+ declare const BigInt64Type: IType<bigint, 64>;
392
+
393
+ declare const BitType: IType<number, 1>;
394
+
395
+ declare const BooleanType: IType<boolean, 1>;
396
+
397
+ declare function FixedLengthArrayType<ValueType, ValueBitSize extends number | null>(type: IType<ValueType, ValueBitSize>, length: number): IType<ValueType[], ValueBitSize extends null ? null : number>;
398
+
399
+ declare const Float32Type: IType<number, 32>;
400
+
401
+ declare const Float64Type: IType<number, 64>;
402
+
403
+ declare const Int16Type: IType<number, 16>;
404
+
405
+ declare const Int2Type: IType<number, 2>;
406
+
407
+ declare const Int32Type: IType<number, 32>;
408
+
409
+ declare const Int4Type: IType<number, 4>;
410
+
411
+ declare const Int64Type: IType<number, 64>;
412
+
413
+ declare const Int8Type: IType<number, 8>;
414
+
415
+ declare const SnowflakeType: {
416
+ readonly serialize: (buffer: UnalignedUint16Array, value: bigint | string) => void;
417
+ readonly deserialize: (buffer: UnalignedUint16Array, offset: Pointer) => bigint;
418
+ readonly BIT_SIZE: 64;
419
+ };
420
+
421
+ declare const StringType: IType<string, null>;
422
+
423
+ export { ArrayType, BigInt32Type, BigInt64Type, BitType, BooleanType, type DeserializationResult, type EntryOfSchema, type EntryOfStore, FixedLengthArrayType, Float32Type, Float64Type, type IType, Int16Type, Int2Type, Int32Type, Int4Type, Int64Type, Int8Type, type KeyOfSchema, type KeyOfStore, Pointer, type PointerLike, Schema, SchemaStore, SnowflakeType, StringType, UnalignedUint16Array, type UnwrapSchema, type UnwrapSchemaEntries, type UnwrapSchemaType, type ValueOfSchema, type ValueOfStore };