@mml-io/delta-net-protocol 0.0.0-experimental-bcf0b7c-20250715

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/README.md +6 -0
  2. package/build/BufferReader.d.ts +92 -0
  3. package/build/BufferWriter.d.ts +98 -0
  4. package/build/DeflateCompressor.d.ts +14 -0
  5. package/build/delta-net-v0.1/constants.d.ts +1 -0
  6. package/build/delta-net-v0.1/decodeClientMessages.d.ts +3 -0
  7. package/build/delta-net-v0.1/decodeServerMessages.d.ts +6 -0
  8. package/build/delta-net-v0.1/encodeClientMessage.d.ts +3 -0
  9. package/build/delta-net-v0.1/encodeServerMessage.d.ts +3 -0
  10. package/build/delta-net-v0.1/index.d.ts +7 -0
  11. package/build/delta-net-v0.1/messageTypes.d.ts +12 -0
  12. package/build/delta-net-v0.1/messages/from-client/clientCustom.d.ts +9 -0
  13. package/build/delta-net-v0.1/messages/from-client/connectUser.d.ts +31 -0
  14. package/build/delta-net-v0.1/messages/from-client/index.d.ts +9 -0
  15. package/build/delta-net-v0.1/messages/from-client/pong.d.ts +8 -0
  16. package/build/delta-net-v0.1/messages/from-client/setUserComponents.d.ts +27 -0
  17. package/build/delta-net-v0.1/messages/from-server/error.d.ts +22 -0
  18. package/build/delta-net-v0.1/messages/from-server/index.d.ts +15 -0
  19. package/build/delta-net-v0.1/messages/from-server/initialCheckout.d.ts +56 -0
  20. package/build/delta-net-v0.1/messages/from-server/ping.d.ts +8 -0
  21. package/build/delta-net-v0.1/messages/from-server/serverCustom.d.ts +9 -0
  22. package/build/delta-net-v0.1/messages/from-server/tick.d.ts +60 -0
  23. package/build/delta-net-v0.1/messages/from-server/userIndex.d.ts +8 -0
  24. package/build/delta-net-v0.1/messages/from-server/warning.d.ts +8 -0
  25. package/build/delta-net-v0.1/messages/index.d.ts +2 -0
  26. package/build/index.d.ts +4 -0
  27. package/build/index.js +1104 -0
  28. package/build/index.js.map +7 -0
  29. package/package.json +28 -0
package/build/index.js ADDED
@@ -0,0 +1,1104 @@
1
+ // src/BufferReader.ts
2
+ var textDecoder = new TextDecoder();
3
+ var BufferReader = class {
4
+ buffer;
5
+ offset;
6
+ /**
7
+ * Creates a new BufferReader instance.
8
+ * @param buffer - The Uint8Array to read from
9
+ */
10
+ constructor(buffer) {
11
+ this.buffer = buffer;
12
+ this.offset = 0;
13
+ }
14
+ /**
15
+ * Reads a single unsigned 8-bit integer from the buffer.
16
+ * @returns The read value
17
+ */
18
+ readUInt8() {
19
+ return this.buffer[this.offset++];
20
+ }
21
+ /**
22
+ * Reads a boolean value from the buffer.
23
+ * @returns true if the read byte is 1, false otherwise
24
+ */
25
+ readBoolean() {
26
+ return this.readUInt8() === 1;
27
+ }
28
+ /**
29
+ * Reads a specified number of bytes from the buffer.
30
+ * @param length - The number of bytes to read
31
+ * @returns A new Uint8Array containing the read bytes
32
+ */
33
+ readBytes(length) {
34
+ const bytes = this.buffer.subarray(this.offset, this.offset + length);
35
+ this.offset += length;
36
+ return bytes;
37
+ }
38
+ /**
39
+ * Reads a length-prefixed byte array from the buffer.
40
+ * The length is encoded as a varint.
41
+ * @returns A new Uint8Array containing the read bytes
42
+ */
43
+ readUVarintPrefixedBytes() {
44
+ const length = this.readUVarint();
45
+ return this.readBytes(length);
46
+ }
47
+ /**
48
+ * Reads a varint-encoded integer from the buffer.
49
+ * Varints are variable-length integers that use the high bit of each byte to indicate if more bytes follow.
50
+ * @param signed - Whether to interpret the value as a signed integer
51
+ * @returns The decoded integer value
52
+ * @throws Error if the varint encoding is invalid
53
+ */
54
+ readUVarint(signed = false) {
55
+ let lo = 0;
56
+ let hi = 0;
57
+ let i = 0;
58
+ for (; i < 4; ++i) {
59
+ lo = (lo | (this.buffer[this.offset] & 127) << i * 7) >>> 0;
60
+ if (this.buffer[this.offset++] < 128) {
61
+ return signed ? loAndHiAsSigned(lo, hi) : loAndHiAsUnsigned(lo, hi);
62
+ }
63
+ }
64
+ lo = (lo | (this.buffer[this.offset] & 127) << 28) >>> 0;
65
+ hi = (hi | (this.buffer[this.offset] & 127) >> 4) >>> 0;
66
+ if (this.buffer[this.offset++] < 128) {
67
+ return signed ? loAndHiAsSigned(lo, hi) : loAndHiAsUnsigned(lo, hi);
68
+ }
69
+ i = 0;
70
+ for (; i < 5; ++i) {
71
+ hi = (hi | (this.buffer[this.offset] & 127) << i * 7 + 3) >>> 0;
72
+ if (this.buffer[this.offset++] < 128) {
73
+ return signed ? loAndHiAsSigned(lo, hi) : loAndHiAsUnsigned(lo, hi);
74
+ }
75
+ }
76
+ throw Error("invalid varint encoding");
77
+ }
78
+ /**
79
+ * Reads a string from the buffer with a specified byte length.
80
+ * Optimized for ASCII strings, falls back to TextDecoder for non-ASCII.
81
+ * @param byteLength - The number of bytes to read
82
+ * @returns The decoded string
83
+ */
84
+ readStringBytes(byteLength) {
85
+ let string = "";
86
+ let hasNonAscii = false;
87
+ for (let i = 0; i < byteLength; i++) {
88
+ const charValue = this.buffer[this.offset + i];
89
+ if (charValue < 128) {
90
+ string += String.fromCharCode(charValue);
91
+ } else {
92
+ hasNonAscii = true;
93
+ break;
94
+ }
95
+ }
96
+ if (!hasNonAscii) {
97
+ this.offset += byteLength;
98
+ return string;
99
+ }
100
+ const result = textDecoder.decode(this.buffer.subarray(this.offset, this.offset + byteLength));
101
+ this.offset += byteLength;
102
+ return result;
103
+ }
104
+ /**
105
+ * Reads a length-prefixed string from the buffer.
106
+ * The length is encoded as an unsigned varint.
107
+ * @returns The decoded string
108
+ */
109
+ readUVarintPrefixedString() {
110
+ const readLength = this.readUVarint();
111
+ return this.readStringBytes(readLength);
112
+ }
113
+ /**
114
+ * Reads a length-prefixed string from the buffer.
115
+ * The length is encoded as a signed varint.
116
+ * @returns A tuple containing the decoded string and a boolean indicating if the length was negative
117
+ */
118
+ readVarintPrefixedString() {
119
+ const length = this.readVarint();
120
+ const negativeLength = length < 0;
121
+ const readLength = negativeLength ? -length : length;
122
+ const result = this.readStringBytes(readLength);
123
+ return [result, negativeLength];
124
+ }
125
+ /**
126
+ * Reads a signed varint-encoded integer from the buffer.
127
+ * @returns The decoded signed integer value
128
+ */
129
+ readVarint() {
130
+ return this.readUVarint(true);
131
+ }
132
+ /**
133
+ * Reads a varint-encoded bigint from the buffer.
134
+ * Varints are variable-length integers that use the high bit of each byte to indicate if more bytes follow.
135
+ * @param signed - Whether to interpret the value as a signed integer
136
+ * @returns The decoded bigint value
137
+ * @throws Error if the varint encoding is invalid
138
+ */
139
+ readUVarintBigInt(signed = false) {
140
+ let result = 0n;
141
+ let shift = 0n;
142
+ let byte;
143
+ let bytesRead = 0;
144
+ do {
145
+ if (bytesRead >= 10) {
146
+ throw Error("invalid varint encoding");
147
+ }
148
+ byte = this.buffer[this.offset++];
149
+ result |= BigInt(byte & 127) << shift;
150
+ shift += 7n;
151
+ bytesRead++;
152
+ } while (byte >= 128);
153
+ if (signed) {
154
+ return result & 1n ? -(result + 1n) / 2n : result / 2n;
155
+ }
156
+ return result;
157
+ }
158
+ /**
159
+ * Reads a signed varint-encoded integer from the buffer.
160
+ * @returns The decoded signed integer value
161
+ */
162
+ readBigIntVarint() {
163
+ return this.readUVarintBigInt(true);
164
+ }
165
+ /**
166
+ * Reads an array of boolean values from the buffer.
167
+ * The booleans are packed into bytes (8 booleans per byte).
168
+ * @returns An array of boolean values
169
+ */
170
+ readLengthPrefixedBoolArray() {
171
+ const length = this.readUVarint();
172
+ const numBytes = Math.ceil(length / 8);
173
+ const result = new Array(length);
174
+ for (let i = 0; i < length; i++) {
175
+ const byteIndex = Math.floor(i / 8);
176
+ const bitPosition = i % 8;
177
+ result[i] = !!(this.buffer[this.offset + byteIndex] & 1 << bitPosition);
178
+ }
179
+ this.offset += numBytes;
180
+ return result;
181
+ }
182
+ /**
183
+ * Checks if the reader has reached the end of the buffer.
184
+ * @returns true if all bytes have been read, false otherwise
185
+ */
186
+ isEnd() {
187
+ return this.offset >= this.buffer.length;
188
+ }
189
+ };
190
+ function loAndHiAsSigned(lo, hi) {
191
+ const value = lo + hi * 4294967296;
192
+ if (value & 1) {
193
+ return -(value + 1) / 2;
194
+ }
195
+ return value / 2;
196
+ }
197
+ function loAndHiAsUnsigned(lo, hi) {
198
+ return lo + hi * 4294967296;
199
+ }
200
+
201
+ // src/BufferWriter.ts
202
+ var textEncoder = new TextEncoder();
203
+ var BufferWriter = class {
204
+ buffer;
205
+ offset;
206
+ /**
207
+ * Creates a new BufferWriter instance.
208
+ * @param initialLength - The initial size of the buffer in bytes
209
+ */
210
+ constructor(initialLength) {
211
+ this.buffer = new Uint8Array(initialLength);
212
+ this.offset = 0;
213
+ }
214
+ /**
215
+ * Writes an unsigned 8-bit integer to the buffer.
216
+ * @param value - The value to write (will be truncated to 8 bits)
217
+ */
218
+ writeUint8(value) {
219
+ this.ensureCapacity(1);
220
+ this.buffer[this.offset] = value & 255;
221
+ this.offset += 1;
222
+ }
223
+ /**
224
+ * Writes a boolean value to the buffer.
225
+ * @param bool - The boolean value to write (true = 1, false = 0)
226
+ */
227
+ writeBoolean(bool) {
228
+ this.writeUint8(bool ? 1 : 0);
229
+ }
230
+ /**
231
+ * Writes an array of bytes to the buffer without a length prefix.
232
+ * @param bytes - The bytes to write
233
+ */
234
+ writeUnprefixedBytes(bytes) {
235
+ this.ensureCapacity(bytes.byteLength);
236
+ this.buffer.set(bytes, this.offset);
237
+ this.offset += bytes.byteLength;
238
+ }
239
+ /**
240
+ * Writes a length-prefixed array of bytes to the buffer.
241
+ * The length is encoded as an unsigned varint.
242
+ * @param bytes - The bytes to write
243
+ */
244
+ writeUVarintLengthPrefixedBytes(bytes) {
245
+ if (!(bytes instanceof Uint8Array)) {
246
+ throw new Error("bytes must be a Uint8Array");
247
+ }
248
+ this.writeUVarint(bytes.byteLength);
249
+ this.writeUnprefixedBytes(bytes);
250
+ }
251
+ /**
252
+ * Gets the written bytes as a Uint8Array.
253
+ * @returns A new Uint8Array containing only the written bytes
254
+ */
255
+ getBuffer() {
256
+ return this.buffer.subarray(0, this.offset);
257
+ }
258
+ /**
259
+ * Gets the number of bytes written so far.
260
+ * @returns The current write offset
261
+ */
262
+ getWrittenLength() {
263
+ return this.offset;
264
+ }
265
+ /**
266
+ * Ensures the buffer has enough capacity for the required space.
267
+ * @param neededSpace - The number of additional bytes needed
268
+ */
269
+ ensureCapacity(neededSpace) {
270
+ while (this.offset + neededSpace > this.buffer.length) {
271
+ this.expandBuffer();
272
+ }
273
+ }
274
+ /**
275
+ * Expands the buffer by doubling its current length.
276
+ */
277
+ expandBuffer() {
278
+ const newBuffer = new Uint8Array(this.buffer.length * 2);
279
+ newBuffer.set(this.buffer);
280
+ this.buffer = newBuffer;
281
+ }
282
+ /**
283
+ * Writes an unsigned varint to the buffer.
284
+ * Varints are variable-length integers that use the high bit of each byte to indicate if more bytes follow.
285
+ * @param x - The value to write
286
+ */
287
+ writeUVarint(x) {
288
+ if (x <= 268435455) {
289
+ this.ensureCapacity(4);
290
+ while (x >= 128) {
291
+ this.buffer[this.offset] = x & 127 | 128;
292
+ this.offset++;
293
+ x >>>= 7;
294
+ }
295
+ this.buffer[this.offset] = x & 127;
296
+ this.offset++;
297
+ return;
298
+ }
299
+ this.ensureCapacity(10);
300
+ let lo = 0;
301
+ let hi = 0;
302
+ if (x !== 0) {
303
+ lo = x >>> 0;
304
+ hi = (x - lo) / 4294967296 >>> 0;
305
+ }
306
+ while (hi) {
307
+ this.buffer[this.offset++] = lo & 127 | 128;
308
+ lo = (lo >>> 7 | hi << 25) >>> 0;
309
+ hi >>>= 7;
310
+ }
311
+ while (lo > 127) {
312
+ this.buffer[this.offset++] = lo & 127 | 128;
313
+ lo = lo >>> 7;
314
+ }
315
+ this.buffer[this.offset++] = lo;
316
+ }
317
+ /**
318
+ * Writes an unsigned varint to the buffer.
319
+ * Varints are variable-length integers that use the high bit of each byte to indicate if more bytes follow.
320
+ * @param x - The value to write
321
+ */
322
+ writeUVarintBigInt(x) {
323
+ this.ensureCapacity(10);
324
+ while (x >= 0x80n) {
325
+ this.buffer[this.offset] = Number(x & 0x7fn) | 128;
326
+ this.offset++;
327
+ x >>= 7n;
328
+ }
329
+ this.buffer[this.offset] = Number(x & 0x7fn);
330
+ this.offset++;
331
+ }
332
+ /**
333
+ * Writes a signed varint to the buffer using zigzag encoding.
334
+ * @param x - The signed value to write
335
+ */
336
+ writeVarint(x) {
337
+ if (x >= 0) {
338
+ this.writeUVarint(x * 2);
339
+ } else {
340
+ this.writeUVarint(-x * 2 - 1);
341
+ }
342
+ }
343
+ /**
344
+ * Writes a signed varint to the buffer using zigzag encoding.
345
+ * @param x - The signed value to write
346
+ */
347
+ writeBigIntVarint(x) {
348
+ if (x >= 0n) {
349
+ this.writeUVarintBigInt(x * 2n);
350
+ } else {
351
+ this.writeUVarintBigInt(-x * 2n - 1n);
352
+ }
353
+ }
354
+ /**
355
+ * Writes an array of boolean values to the buffer.
356
+ * The booleans are packed into bytes (8 booleans per byte).
357
+ * @param data - The array of boolean values to write
358
+ */
359
+ writeLengthPrefixedBoolArray(data) {
360
+ this.writeUVarint(data.length);
361
+ const numBytes = Math.ceil(data.length / 8);
362
+ this.ensureCapacity(numBytes + 4);
363
+ for (let i = 0; i < data.length; i++) {
364
+ if (data[i]) {
365
+ const byteIndex = Math.floor(i / 8);
366
+ const bitPosition = i % 8;
367
+ this.buffer[this.offset + byteIndex] |= 1 << bitPosition;
368
+ }
369
+ }
370
+ this.offset += numBytes;
371
+ }
372
+ /**
373
+ * Writes a length-prefixed string to the buffer.
374
+ * Optimized for ASCII strings, falls back to TextEncoder for non-ASCII.
375
+ * @param value - The string to write
376
+ * @param varint - Whether to use signed varint for length (default: false)
377
+ * @param negativeLength - Whether the length should be negative (only used if varint is true)
378
+ */
379
+ writeLengthPrefixedString(value, varint = false, negativeLength = false) {
380
+ const originalOffset = this.offset;
381
+ if (varint) {
382
+ this.writeVarint(negativeLength ? -value.length : value.length);
383
+ } else {
384
+ this.writeUVarint(value.length);
385
+ }
386
+ this.ensureCapacity(value.length);
387
+ let nonAscii = false;
388
+ for (let i = 0; i < value.length; i++) {
389
+ const charCode = value.charCodeAt(i);
390
+ if (charCode > 127) {
391
+ nonAscii = true;
392
+ break;
393
+ }
394
+ this.buffer[this.offset++] = charCode;
395
+ }
396
+ if (!nonAscii) {
397
+ return;
398
+ }
399
+ this.offset = originalOffset;
400
+ let encodedLength = value.length;
401
+ this.ensureCapacity(encodedLength);
402
+ while (true) {
403
+ this.offset = originalOffset;
404
+ if (varint) {
405
+ this.writeVarint(negativeLength ? -encodedLength : encodedLength);
406
+ } else {
407
+ this.writeUVarint(encodedLength);
408
+ }
409
+ const offsetAfterVarint = this.offset;
410
+ const varintLength = offsetAfterVarint - originalOffset;
411
+ const writeBuffer = new Uint8Array(this.buffer.buffer, this.offset);
412
+ const { read, written } = textEncoder.encodeInto(value, writeBuffer);
413
+ if (read !== value.length) {
414
+ this.expandBuffer();
415
+ continue;
416
+ }
417
+ if (written !== encodedLength) {
418
+ encodedLength = written;
419
+ this.offset = originalOffset;
420
+ if (varint) {
421
+ this.writeVarint(negativeLength ? -encodedLength : encodedLength);
422
+ } else {
423
+ this.writeUVarint(encodedLength);
424
+ }
425
+ const newOffsetAfterVarint = this.offset;
426
+ const actualVarintLength = newOffsetAfterVarint - originalOffset;
427
+ if (actualVarintLength !== varintLength) {
428
+ continue;
429
+ } else {
430
+ }
431
+ }
432
+ this.offset += written;
433
+ return;
434
+ }
435
+ }
436
+ };
437
+ function zigzagEncode(value) {
438
+ return value >= 0 ? value * 2 : -value * 2 - 1;
439
+ }
440
+
441
+ // src/DeflateCompressor.ts
442
+ import { deflate, inflate } from "pako";
443
+ var nodeZlibFunctions = null;
444
+ try {
445
+ const isNode = typeof process !== "undefined" && process.versions && typeof process.versions.node === "string";
446
+ if (isNode) {
447
+ (async () => {
448
+ try {
449
+ const nodeZlib = await import("zlib");
450
+ nodeZlibFunctions = {
451
+ deflateSync: (data) => {
452
+ const result = nodeZlib.deflateSync(data);
453
+ return new Uint8Array(result);
454
+ },
455
+ inflateSync: (data) => {
456
+ const result = nodeZlib.inflateSync(data);
457
+ return new Uint8Array(result);
458
+ }
459
+ };
460
+ } catch (e) {
461
+ console.log("nodeZlib not available - sync", e);
462
+ }
463
+ })();
464
+ }
465
+ } catch (e) {
466
+ console.log("nodeZlib not available - sync", e);
467
+ }
468
+ var CompressionLibraryChoice = /* @__PURE__ */ ((CompressionLibraryChoice2) => {
469
+ CompressionLibraryChoice2["PAKO"] = "PAKO";
470
+ CompressionLibraryChoice2["NODE_ZLIB"] = "NODE_ZLIB";
471
+ CompressionLibraryChoice2["NO_PREFERENCE"] = "NO_PREFERENCE";
472
+ CompressionLibraryChoice2["NONE"] = "NONE";
473
+ return CompressionLibraryChoice2;
474
+ })(CompressionLibraryChoice || {});
475
+ function getCompressFunction(compressionLibrary) {
476
+ switch (compressionLibrary) {
477
+ case "PAKO" /* PAKO */:
478
+ return deflate;
479
+ case "NODE_ZLIB" /* NODE_ZLIB */:
480
+ if (nodeZlibFunctions) {
481
+ return nodeZlibFunctions.deflateSync;
482
+ } else {
483
+ throw new Error("node:zlib not available");
484
+ }
485
+ case "NO_PREFERENCE" /* NO_PREFERENCE */:
486
+ if (nodeZlibFunctions) {
487
+ return nodeZlibFunctions.deflateSync;
488
+ } else {
489
+ return deflate;
490
+ }
491
+ case "NONE" /* NONE */:
492
+ return (data) => {
493
+ return new Uint8Array(data);
494
+ };
495
+ }
496
+ }
497
+ function getDecompressFunction(compressionLibrary) {
498
+ switch (compressionLibrary) {
499
+ case "PAKO" /* PAKO */:
500
+ return inflate;
501
+ case "NODE_ZLIB" /* NODE_ZLIB */:
502
+ if (nodeZlibFunctions) {
503
+ return nodeZlibFunctions.inflateSync;
504
+ } else {
505
+ throw new Error("node:zlib not available");
506
+ }
507
+ case "NO_PREFERENCE" /* NO_PREFERENCE */:
508
+ if (nodeZlibFunctions) {
509
+ return nodeZlibFunctions.inflateSync;
510
+ } else {
511
+ return inflate;
512
+ }
513
+ case "NONE" /* NONE */:
514
+ return (data) => {
515
+ return new Uint8Array(data);
516
+ };
517
+ }
518
+ }
519
+ var DeflateCompressor = class _DeflateCompressor {
520
+ static compress(data, compressionLibrary = "NO_PREFERENCE" /* NO_PREFERENCE */) {
521
+ return getCompressFunction(compressionLibrary)(data);
522
+ }
523
+ static decompress(compressed, compressionLibrary = "NO_PREFERENCE" /* NO_PREFERENCE */) {
524
+ return getDecompressFunction(compressionLibrary)(compressed);
525
+ }
526
+ static varIntCompress(data, length, compressionLibrary = "NO_PREFERENCE" /* NO_PREFERENCE */) {
527
+ if (length > data.length) {
528
+ throw new Error("length is greater than the data length");
529
+ }
530
+ const writer = new BufferWriter(length);
531
+ for (let i = 0; i < length; i++) {
532
+ writer.writeBigIntVarint(data[i]);
533
+ }
534
+ const uint8Array = writer.getBuffer();
535
+ return [uint8Array, _DeflateCompressor.compress(uint8Array, compressionLibrary)];
536
+ }
537
+ static varIntDecompress(compressed, length, compressionLibrary = "NO_PREFERENCE" /* NO_PREFERENCE */) {
538
+ const data = _DeflateCompressor.decompress(compressed, compressionLibrary);
539
+ const buffer = new BigInt64Array(length);
540
+ const reader = new BufferReader(data);
541
+ for (let i = 0; i < length; i++) {
542
+ buffer[i] = reader.readBigIntVarint();
543
+ }
544
+ return buffer;
545
+ }
546
+ static varIntBytesCompress(data, length, compressionLibrary = "NO_PREFERENCE" /* NO_PREFERENCE */) {
547
+ if (length > data.length) {
548
+ throw new Error("length is greater than the data length");
549
+ }
550
+ const writer = new BufferWriter(length);
551
+ for (let i = 0; i < length; i++) {
552
+ const value = data[i];
553
+ if (value === null || value === void 0) {
554
+ writer.writeUVarintLengthPrefixedBytes(new Uint8Array(0));
555
+ } else {
556
+ writer.writeUVarintLengthPrefixedBytes(value);
557
+ }
558
+ }
559
+ const uint8Array = writer.getBuffer();
560
+ return [uint8Array, _DeflateCompressor.compress(uint8Array, compressionLibrary)];
561
+ }
562
+ static varIntBytesDecompress(compressed, length, compressionLibrary = "NO_PREFERENCE" /* NO_PREFERENCE */) {
563
+ const data = _DeflateCompressor.decompress(compressed, compressionLibrary);
564
+ const buffer = new Array(length);
565
+ const reader = new BufferReader(data);
566
+ for (let i = 0; i < length; i++) {
567
+ buffer[i] = reader.readUVarintPrefixedBytes();
568
+ }
569
+ return buffer;
570
+ }
571
+ };
572
+
573
+ // src/delta-net-v0.1/constants.ts
574
+ var deltaNetProtocolSubProtocol_v0_1 = "delta-net-v0.1";
575
+
576
+ // src/delta-net-v0.1/messageTypes.ts
577
+ var InitialCheckoutMessageType = 1;
578
+ var ServerCustomMessageType = 2;
579
+ var UserIndexMessageType = 3;
580
+ var TickMessageType = 4;
581
+ var PingMessageType = 5;
582
+ var WarningMessageType = 6;
583
+ var ErrorMessageType = 7;
584
+ var ConnectUserMessageType = 64;
585
+ var ClientCustomMessageType = 65;
586
+ var SetUserComponentsMessageType = 66;
587
+ var SetUserStateMessageType = 67;
588
+ var PongMessageType = 68;
589
+
590
+ // src/delta-net-v0.1/messages/from-client/clientCustom.ts
591
+ function encodeClientCustom(msg, writer = new BufferWriter(64)) {
592
+ writer.writeUint8(ClientCustomMessageType);
593
+ writer.writeUVarint(msg.customType);
594
+ writer.writeLengthPrefixedString(msg.contents);
595
+ return writer;
596
+ }
597
+ function decodeClientCustom(buffer) {
598
+ const customType = buffer.readUVarint();
599
+ const contents = buffer.readUVarintPrefixedString();
600
+ return {
601
+ type: "clientCustom",
602
+ customType,
603
+ contents
604
+ };
605
+ }
606
+
607
+ // src/delta-net-v0.1/messages/from-client/connectUser.ts
608
+ function encodeConnectUser(connectUserMessage, writer) {
609
+ writer.writeUint8(ConnectUserMessageType);
610
+ writer.writeLengthPrefixedString(connectUserMessage.token);
611
+ writer.writeBoolean(connectUserMessage.observer ?? false);
612
+ writer.writeUVarint(connectUserMessage.components.length);
613
+ for (const [componentId, componentValue] of connectUserMessage.components) {
614
+ writer.writeUVarint(componentId);
615
+ writer.writeBigIntVarint(componentValue);
616
+ }
617
+ writer.writeUVarint(connectUserMessage.states.length);
618
+ for (const [stateId, stateValue] of connectUserMessage.states) {
619
+ writer.writeUVarint(stateId);
620
+ writer.writeUVarintLengthPrefixedBytes(stateValue);
621
+ }
622
+ }
623
+ function decodeConnectUser(buffer) {
624
+ const token = buffer.readUVarintPrefixedString();
625
+ const observer = buffer.readBoolean();
626
+ const componentsLength = buffer.readUVarint();
627
+ const components = [];
628
+ for (let i = 0; i < componentsLength; i++) {
629
+ const componentId = buffer.readUVarint();
630
+ const componentValue = buffer.readBigIntVarint();
631
+ components.push([componentId, componentValue]);
632
+ }
633
+ const statesLength = buffer.readUVarint();
634
+ const states = [];
635
+ for (let i = 0; i < statesLength; i++) {
636
+ const stateId = buffer.readUVarint();
637
+ const stateValue = buffer.readUVarintPrefixedBytes();
638
+ states.push([stateId, stateValue]);
639
+ }
640
+ return {
641
+ type: "connectUser",
642
+ token,
643
+ observer,
644
+ components,
645
+ states
646
+ };
647
+ }
648
+
649
+ // src/delta-net-v0.1/messages/from-client/pong.ts
650
+ function encodePong(pongMessage, writer) {
651
+ writer.writeUint8(PongMessageType);
652
+ writer.writeUVarint(pongMessage.pong);
653
+ }
654
+ function decodePong(buffer) {
655
+ const pong = buffer.readUVarint();
656
+ return {
657
+ type: "pong",
658
+ pong
659
+ };
660
+ }
661
+
662
+ // src/delta-net-v0.1/messages/from-client/setUserComponents.ts
663
+ function encodeSetUserComponents(message, writer) {
664
+ writer.writeUint8(SetUserComponentsMessageType);
665
+ writer.writeUVarint(message.components.length);
666
+ for (const [componentId, componentValue] of message.components) {
667
+ writer.writeUVarint(componentId);
668
+ writer.writeBigIntVarint(componentValue);
669
+ }
670
+ writer.writeUVarint(message.states.length);
671
+ for (const [stateId, stateValue] of message.states) {
672
+ writer.writeUVarint(stateId);
673
+ writer.writeUVarintLengthPrefixedBytes(stateValue);
674
+ }
675
+ }
676
+ function decodeSetUserComponents(buffer) {
677
+ const componentsLength = buffer.readUVarint();
678
+ const components = [];
679
+ for (let i = 0; i < componentsLength; i++) {
680
+ const componentId = buffer.readUVarint();
681
+ const componentValue = buffer.readBigIntVarint();
682
+ components.push([componentId, componentValue]);
683
+ }
684
+ const statesLength = buffer.readUVarint();
685
+ const states = [];
686
+ for (let i = 0; i < statesLength; i++) {
687
+ const stateId = buffer.readUVarint();
688
+ const stateValue = buffer.readUVarintPrefixedBytes();
689
+ states.push([stateId, stateValue]);
690
+ }
691
+ return {
692
+ type: "setUserComponents",
693
+ components,
694
+ states
695
+ };
696
+ }
697
+
698
+ // src/delta-net-v0.1/decodeClientMessages.ts
699
+ function decodeClientMessages(buffer) {
700
+ const messages = [];
701
+ while (!buffer.isEnd()) {
702
+ const messageType = buffer.readUInt8();
703
+ switch (messageType) {
704
+ case ConnectUserMessageType:
705
+ messages.push(decodeConnectUser(buffer));
706
+ break;
707
+ case SetUserComponentsMessageType:
708
+ messages.push(decodeSetUserComponents(buffer));
709
+ break;
710
+ case PongMessageType:
711
+ messages.push(decodePong(buffer));
712
+ break;
713
+ case ClientCustomMessageType:
714
+ messages.push(decodeClientCustom(buffer));
715
+ break;
716
+ default:
717
+ throw new Error(`Unknown message type: ${messageType}`);
718
+ }
719
+ }
720
+ return messages;
721
+ }
722
+
723
+ // src/delta-net-v0.1/messages/from-server/error.ts
724
+ var DeltaNetV01ServerErrors;
725
+ ((DeltaNetV01ServerErrors2) => {
726
+ DeltaNetV01ServerErrors2.USER_ALREADY_AUTHENTICATED_ERROR_TYPE = "USER_ALREADY_AUTHENTICATED";
727
+ DeltaNetV01ServerErrors2.USER_NOT_AUTHENTICATED_ERROR_TYPE = "USER_NOT_AUTHENTICATED";
728
+ DeltaNetV01ServerErrors2.AUTHENTICATION_IN_PROGRESS_ERROR_TYPE = "AUTHENTICATION_IN_PROGRESS";
729
+ DeltaNetV01ServerErrors2.OBSERVER_CANNOT_SEND_STATE_UPDATES_ERROR_TYPE = "OBSERVER_CANNOT_SEND_STATE_UPDATES";
730
+ DeltaNetV01ServerErrors2.UNSUPPORTED_WEBSOCKET_SUBPROTOCOL_ERROR_TYPE = "UNSUPPORTED_WEBSOCKET_SUBPROTOCOL";
731
+ DeltaNetV01ServerErrors2.USER_NETWORKING_UNKNOWN_ERROR_TYPE = "USER_NETWORKING_UNKNOWN_ERROR";
732
+ DeltaNetV01ServerErrors2.USER_AUTHENTICATION_FAILED_ERROR_TYPE = "USER_AUTHENTICATION_FAILED";
733
+ DeltaNetV01ServerErrors2.USER_NETWORKING_CONNECTION_LIMIT_REACHED_ERROR_TYPE = "CONNECTION_LIMIT_REACHED";
734
+ DeltaNetV01ServerErrors2.USER_NETWORKING_SERVER_SHUTDOWN_ERROR_TYPE = "SERVER_SHUTDOWN";
735
+ })(DeltaNetV01ServerErrors || (DeltaNetV01ServerErrors = {}));
736
+ function encodeError(msg, writer = new BufferWriter(64)) {
737
+ writer.writeUint8(ErrorMessageType);
738
+ writer.writeLengthPrefixedString(msg.errorType);
739
+ writer.writeLengthPrefixedString(msg.message);
740
+ writer.writeBoolean(msg.retryable);
741
+ return writer;
742
+ }
743
+ function decodeError(buffer) {
744
+ const errorType = buffer.readUVarintPrefixedString();
745
+ const message = buffer.readUVarintPrefixedString();
746
+ const retryable = buffer.readBoolean();
747
+ return {
748
+ type: "error",
749
+ errorType,
750
+ message,
751
+ retryable
752
+ };
753
+ }
754
+
755
+ // src/delta-net-v0.1/messages/from-server/initialCheckout.ts
756
+ function encodeInitialCheckout(msg, writer = new BufferWriter(64)) {
757
+ writer.writeUint8(InitialCheckoutMessageType);
758
+ writer.writeUVarint(msg.serverTime);
759
+ writer.writeUVarint(msg.indicesCount);
760
+ writer.writeUVarint(msg.components.length);
761
+ for (const { componentId, deltas, values } of msg.components) {
762
+ writer.writeUVarint(componentId);
763
+ const [, valuesBytes] = DeflateCompressor.varIntCompress(values, msg.indicesCount);
764
+ writer.writeUVarintLengthPrefixedBytes(valuesBytes);
765
+ const [, deltaBytes] = DeflateCompressor.varIntCompress(deltas, msg.indicesCount);
766
+ writer.writeUVarintLengthPrefixedBytes(deltaBytes);
767
+ }
768
+ writer.writeUVarint(msg.states.length);
769
+ for (const { stateId, values } of msg.states) {
770
+ writer.writeUVarint(stateId);
771
+ const [, stateBytes] = DeflateCompressor.varIntBytesCompress(values, msg.indicesCount);
772
+ writer.writeUVarintLengthPrefixedBytes(stateBytes);
773
+ }
774
+ return writer;
775
+ }
776
+ var lastInitialCheckoutDebugData = {
777
+ componentsByteLength: 0,
778
+ statesByteLength: 0
779
+ };
780
+ function decodeInitialCheckout(buffer, opts) {
781
+ let componentsByteLength = 0;
782
+ let statesByteLength = 0;
783
+ const serverTime = buffer.readUVarint();
784
+ const indicesLength = buffer.readUVarint();
785
+ const componentsLength = buffer.readUVarint();
786
+ const components = [];
787
+ for (let i = 0; i < componentsLength; i++) {
788
+ const componentId = buffer.readUVarint();
789
+ const valuesBytes = buffer.readUVarintPrefixedBytes();
790
+ const values = DeflateCompressor.varIntDecompress(valuesBytes, indicesLength);
791
+ const deltaBytes = buffer.readUVarintPrefixedBytes();
792
+ componentsByteLength += valuesBytes.length + deltaBytes.length;
793
+ if (opts == null ? void 0 : opts.ignoreData) {
794
+ components.push({ componentId, deltas: new BigInt64Array(indicesLength), values });
795
+ } else {
796
+ const deltas = DeflateCompressor.varIntDecompress(deltaBytes, indicesLength);
797
+ components.push({ componentId, deltas, values });
798
+ }
799
+ }
800
+ const statesLength = buffer.readUVarint();
801
+ const states = [];
802
+ for (let i = 0; i < statesLength; i++) {
803
+ const stateId = buffer.readUVarint();
804
+ const valuesBytes = buffer.readUVarintPrefixedBytes();
805
+ statesByteLength += valuesBytes.length;
806
+ if (opts == null ? void 0 : opts.ignoreData) {
807
+ const emptyValues = new Array(indicesLength).fill(new Uint8Array(0));
808
+ states.push({ stateId, values: emptyValues });
809
+ } else {
810
+ const values = DeflateCompressor.varIntBytesDecompress(valuesBytes, indicesLength);
811
+ states.push({ stateId, values });
812
+ }
813
+ }
814
+ lastInitialCheckoutDebugData.componentsByteLength = componentsByteLength;
815
+ lastInitialCheckoutDebugData.statesByteLength = statesByteLength;
816
+ return {
817
+ type: "initialCheckout",
818
+ serverTime,
819
+ indicesCount: indicesLength,
820
+ components,
821
+ states
822
+ };
823
+ }
824
+
825
+ // src/delta-net-v0.1/messages/from-server/ping.ts
826
+ function encodePing(pingMessage, writer = new BufferWriter(8)) {
827
+ writer.writeUint8(PingMessageType);
828
+ writer.writeUVarint(pingMessage.ping);
829
+ return writer;
830
+ }
831
+ function decodePing(buffer) {
832
+ const ping = buffer.readUVarint();
833
+ return {
834
+ type: "ping",
835
+ ping
836
+ };
837
+ }
838
+
839
+ // src/delta-net-v0.1/messages/from-server/serverCustom.ts
840
+ function encodeServerCustom(msg, writer = new BufferWriter(64)) {
841
+ writer.writeUint8(ServerCustomMessageType);
842
+ writer.writeUVarint(msg.customType);
843
+ writer.writeLengthPrefixedString(msg.contents);
844
+ return writer;
845
+ }
846
+ function decodeServerCustom(buffer) {
847
+ const customType = buffer.readUVarint();
848
+ const contents = buffer.readUVarintPrefixedString();
849
+ return {
850
+ type: "serverCustom",
851
+ customType,
852
+ contents
853
+ };
854
+ }
855
+
856
+ // src/delta-net-v0.1/messages/from-server/tick.ts
857
+ function encodeTick(msg, writer = new BufferWriter(64)) {
858
+ writer.writeUint8(TickMessageType);
859
+ writer.writeUVarint(msg.serverTime);
860
+ writer.writeUVarint(msg.removedIndices.length);
861
+ for (const index of msg.removedIndices) {
862
+ writer.writeUVarint(index);
863
+ }
864
+ writer.writeUVarint(msg.indicesCount);
865
+ writer.writeUVarint(msg.componentDeltaDeltas.length);
866
+ for (const componentTick of msg.componentDeltaDeltas) {
867
+ writer.writeUVarint(componentTick.componentId);
868
+ const [, deltaDeltasBytes] = DeflateCompressor.varIntCompress(
869
+ componentTick.deltaDeltas,
870
+ msg.indicesCount
871
+ );
872
+ writer.writeUVarintLengthPrefixedBytes(deltaDeltasBytes);
873
+ }
874
+ writer.writeUVarint(msg.states.length);
875
+ for (const state of msg.states) {
876
+ writer.writeUVarint(state.stateId);
877
+ writer.writeUVarint(state.updatedStates.length);
878
+ const indices = new BigInt64Array(state.updatedStates.length);
879
+ const values = new Array(state.updatedStates.length);
880
+ for (let i = 0; i < state.updatedStates.length; i++) {
881
+ const [index, value] = state.updatedStates[i];
882
+ indices[i] = BigInt(index);
883
+ values[i] = value;
884
+ }
885
+ const [, compressedIndices] = DeflateCompressor.varIntCompress(indices, indices.length);
886
+ writer.writeUVarintLengthPrefixedBytes(compressedIndices);
887
+ const [, compressedValues] = DeflateCompressor.varIntBytesCompress(values, values.length);
888
+ writer.writeUVarintLengthPrefixedBytes(compressedValues);
889
+ }
890
+ return writer;
891
+ }
892
+ var lastTickDebugData = {
893
+ componentsByteLength: 0,
894
+ statesByteLength: 0
895
+ };
896
+ function decodeTick(buffer, opts) {
897
+ let componentsByteLength = 0;
898
+ let statesByteLength = 0;
899
+ const serverTime = buffer.readUVarint();
900
+ const removedIndicesLength = buffer.readUVarint();
901
+ const removedIndices = [];
902
+ for (let i = 0; i < removedIndicesLength; i++) {
903
+ removedIndices.push(buffer.readUVarint());
904
+ }
905
+ const indicesCount = buffer.readUVarint();
906
+ const componentsLength = buffer.readUVarint();
907
+ const components = [];
908
+ for (let i = 0; i < componentsLength; i++) {
909
+ const componentId = buffer.readUVarint();
910
+ const deltaDeltaBytes = buffer.readUVarintPrefixedBytes();
911
+ componentsByteLength += deltaDeltaBytes.byteLength;
912
+ if (opts == null ? void 0 : opts.ignoreData) {
913
+ components.push({ componentId, deltaDeltas: new BigInt64Array(indicesCount) });
914
+ } else {
915
+ const deltaDeltas = DeflateCompressor.varIntDecompress(deltaDeltaBytes, indicesCount);
916
+ components.push({ componentId, deltaDeltas });
917
+ }
918
+ }
919
+ const statesLength = buffer.readUVarint();
920
+ const states = [];
921
+ for (let i = 0; i < statesLength; i++) {
922
+ const stateId = buffer.readUVarint();
923
+ const stateCount = buffer.readUVarint();
924
+ const state = {
925
+ stateId,
926
+ updatedStates: []
927
+ };
928
+ const compressedIndices = buffer.readUVarintPrefixedBytes();
929
+ const compressedValues = buffer.readUVarintPrefixedBytes();
930
+ statesByteLength += compressedIndices.byteLength;
931
+ statesByteLength += compressedValues.byteLength;
932
+ if (stateCount > 0) {
933
+ const indices = DeflateCompressor.varIntDecompress(compressedIndices, stateCount);
934
+ const values = DeflateCompressor.varIntBytesDecompress(compressedValues, stateCount);
935
+ for (let j = 0; j < stateCount; j++) {
936
+ const index = Number(indices[j]);
937
+ const value = values[j];
938
+ state.updatedStates.push([index, value]);
939
+ }
940
+ }
941
+ states.push(state);
942
+ }
943
+ lastTickDebugData.componentsByteLength = componentsByteLength;
944
+ lastTickDebugData.statesByteLength = statesByteLength;
945
+ return {
946
+ type: "tick",
947
+ serverTime,
948
+ removedIndices,
949
+ indicesCount,
950
+ componentDeltaDeltas: components,
951
+ states
952
+ };
953
+ }
954
+
955
+ // src/delta-net-v0.1/messages/from-server/userIndex.ts
956
+ function encodeUserIndex(msg, writer = new BufferWriter(64)) {
957
+ writer.writeUint8(UserIndexMessageType);
958
+ writer.writeUVarint(msg.index);
959
+ return writer;
960
+ }
961
+ function decodeUserIndex(buffer) {
962
+ const index = buffer.readUVarint();
963
+ return {
964
+ type: "userIndex",
965
+ index
966
+ };
967
+ }
968
+
969
+ // src/delta-net-v0.1/messages/from-server/warning.ts
970
+ function encodeWarning(msg, writer = new BufferWriter(64)) {
971
+ writer.writeUint8(WarningMessageType);
972
+ writer.writeLengthPrefixedString(msg.message);
973
+ return writer;
974
+ }
975
+ function decodeWarning(buffer) {
976
+ const message = buffer.readUVarintPrefixedString();
977
+ return {
978
+ type: "warning",
979
+ message
980
+ };
981
+ }
982
+
983
+ // src/delta-net-v0.1/decodeServerMessages.ts
984
+ function decodeServerMessages(buffer, opts) {
985
+ const messages = [];
986
+ while (!buffer.isEnd()) {
987
+ const messageType = buffer.readUInt8();
988
+ switch (messageType) {
989
+ case InitialCheckoutMessageType:
990
+ messages.push(decodeInitialCheckout(buffer, opts));
991
+ break;
992
+ case UserIndexMessageType:
993
+ messages.push(decodeUserIndex(buffer));
994
+ break;
995
+ case TickMessageType:
996
+ messages.push(decodeTick(buffer, opts));
997
+ break;
998
+ case ServerCustomMessageType:
999
+ messages.push(decodeServerCustom(buffer));
1000
+ break;
1001
+ case PingMessageType:
1002
+ messages.push(decodePing(buffer));
1003
+ break;
1004
+ case WarningMessageType:
1005
+ messages.push(decodeWarning(buffer));
1006
+ break;
1007
+ case ErrorMessageType:
1008
+ messages.push(decodeError(buffer));
1009
+ break;
1010
+ default:
1011
+ throw new Error(`Unknown message type: ${messageType}`);
1012
+ }
1013
+ }
1014
+ return messages;
1015
+ }
1016
+
1017
+ // src/delta-net-v0.1/encodeClientMessage.ts
1018
+ function encodeClientMessage(message, writer) {
1019
+ const type = message.type;
1020
+ switch (type) {
1021
+ case "connectUser":
1022
+ return encodeConnectUser(message, writer);
1023
+ case "setUserComponents":
1024
+ return encodeSetUserComponents(message, writer);
1025
+ case "pong":
1026
+ return encodePong(message, writer);
1027
+ case "clientCustom":
1028
+ return encodeClientCustom(message, writer);
1029
+ default:
1030
+ throw new Error(`Unknown message type: ${type}`);
1031
+ }
1032
+ }
1033
+
1034
+ // src/delta-net-v0.1/encodeServerMessage.ts
1035
+ function encodeServerMessage(message, writer) {
1036
+ switch (message.type) {
1037
+ case "initialCheckout":
1038
+ return encodeInitialCheckout(message, writer);
1039
+ case "tick":
1040
+ return encodeTick(message, writer);
1041
+ case "userIndex":
1042
+ return encodeUserIndex(message, writer);
1043
+ case "ping":
1044
+ return encodePing(message, writer);
1045
+ case "serverCustom":
1046
+ return encodeServerCustom(message, writer);
1047
+ case "warning":
1048
+ return encodeWarning(message, writer);
1049
+ case "error":
1050
+ return encodeError(message, writer);
1051
+ default:
1052
+ throw new Error(`Unknown message type: ${message.type}`);
1053
+ }
1054
+ }
1055
+ export {
1056
+ BufferReader,
1057
+ BufferWriter,
1058
+ ClientCustomMessageType,
1059
+ CompressionLibraryChoice,
1060
+ ConnectUserMessageType,
1061
+ DeflateCompressor,
1062
+ DeltaNetV01ServerErrors,
1063
+ ErrorMessageType,
1064
+ InitialCheckoutMessageType,
1065
+ PingMessageType,
1066
+ PongMessageType,
1067
+ ServerCustomMessageType,
1068
+ SetUserComponentsMessageType,
1069
+ SetUserStateMessageType,
1070
+ TickMessageType,
1071
+ UserIndexMessageType,
1072
+ WarningMessageType,
1073
+ decodeClientCustom,
1074
+ decodeClientMessages,
1075
+ decodeConnectUser,
1076
+ decodeError,
1077
+ decodeInitialCheckout,
1078
+ decodePing,
1079
+ decodePong,
1080
+ decodeServerCustom,
1081
+ decodeServerMessages,
1082
+ decodeSetUserComponents,
1083
+ decodeTick,
1084
+ decodeUserIndex,
1085
+ decodeWarning,
1086
+ deltaNetProtocolSubProtocol_v0_1,
1087
+ encodeClientCustom,
1088
+ encodeClientMessage,
1089
+ encodeConnectUser,
1090
+ encodeError,
1091
+ encodeInitialCheckout,
1092
+ encodePing,
1093
+ encodePong,
1094
+ encodeServerCustom,
1095
+ encodeServerMessage,
1096
+ encodeSetUserComponents,
1097
+ encodeTick,
1098
+ encodeUserIndex,
1099
+ encodeWarning,
1100
+ lastInitialCheckoutDebugData,
1101
+ lastTickDebugData,
1102
+ zigzagEncode
1103
+ };
1104
+ //# sourceMappingURL=index.js.map