pw-js-world 0.0.5-dev.05281d1 → 0.0.5-dev.24aedef

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,762 +0,0 @@
1
- /**
2
- * CREDIT: Anatoly for making this Buffer Reader so I don't have to!
3
- * Source: https://github.com/Anatoly03/pixelwalker.js/blob/9bb3c7e39a45006086a2abae8c515599bd3db835/src/util/buffer-reader.ts
4
- * License: ISC
5
- */
6
-
7
- /**
8
- * Data during the communication in the process is dynamic
9
- * typed. Every entry is followed by a byte identifying the
10
- * type, followed by data. The type header is noted by its'
11
- * 7-bit notation.
12
- */
13
- export enum ComponentTypeHeader {
14
- String = 0,
15
- Byte = 1,
16
- Int16 = 2,
17
- Int32 = 3,
18
- Int64 = 4,
19
- Float = 5,
20
- Double = 6,
21
- Boolean = 7,
22
- ByteArray = 8,
23
- }
24
-
25
- /**
26
- * A Buffer reader is a special buffer extension made to perform
27
- * game-specific tasks in the field of communication.
28
- *
29
- * @implements Buffer
30
- */
31
- export default class BufferReader {
32
- private buffer: Buffer;
33
- private offset: number = 0;
34
-
35
- /**
36
- *
37
- */
38
- private constructor(buffer: Buffer) {
39
- this.buffer = buffer;
40
- }
41
-
42
- //
43
- //
44
- // Static Methods
45
- //
46
- //
47
-
48
- /**
49
- *
50
- */
51
- public static from(from: Uint8Array | Buffer): BufferReader {
52
- if (from instanceof Buffer) return new BufferReader(from);
53
- return new BufferReader(Buffer.from(from));
54
- }
55
-
56
- /**
57
- *
58
- */
59
- public static alloc(amount: number) {
60
- return BufferReader.from(Buffer.alloc(amount));
61
- }
62
-
63
- //
64
- //
65
- // Message Component Types
66
- //
67
- //
68
-
69
- /**
70
- * @param {string} value
71
- * @returns {Buffer}
72
- */
73
- public static String(value: string = ''): Buffer {
74
- const stringByteLen = Buffer.byteLength(value);
75
- const lengthByteCount = this.length7BitInt(stringByteLen);
76
-
77
- const buffer = BufferReader.alloc(1 + lengthByteCount + stringByteLen);
78
- buffer.writeUInt8(ComponentTypeHeader.String);
79
- buffer.write7BitInt(stringByteLen);
80
- buffer.write(value);
81
-
82
- return buffer.toBuffer();
83
- }
84
-
85
- /**
86
- * @param {number} value
87
- * @returns {Buffer}
88
- */
89
- public static Byte(value: number = 0): Buffer {
90
- const buffer = BufferReader.alloc(2);
91
- buffer.writeUInt8(ComponentTypeHeader.Byte);
92
- buffer.writeUInt8(value);
93
- return buffer.toBuffer();
94
- }
95
-
96
- /**
97
- * @param {number} value
98
- * @returns {Buffer}
99
- */
100
- public static Int16(value: number = 0): Buffer {
101
- const buffer = BufferReader.alloc(3);
102
- buffer.writeUInt8(ComponentTypeHeader.Int16);
103
- buffer.writeUInt16BE(value);
104
- return buffer.toBuffer();
105
- }
106
-
107
- /**
108
- * @param {number} value
109
- * @returns {Buffer}
110
- */
111
- public static Int32(value: number = 0): Buffer {
112
- const buffer = BufferReader.alloc(5);
113
- buffer.writeUInt8(ComponentTypeHeader.Int32);
114
- buffer.writeInt32BE(value);
115
- return buffer.toBuffer();
116
- }
117
-
118
- /**
119
- * @param {bigint} value
120
- * @returns {Buffer}
121
- */
122
- public static Int64(value: bigint = BigInt(0).valueOf()): Buffer {
123
- const buffer = BufferReader.alloc(9);
124
- buffer.writeUInt8(ComponentTypeHeader.Int64);
125
- buffer.writeBigInt64BE(value);
126
- return buffer.toBuffer();
127
- }
128
-
129
- /**
130
- * @param {number} value
131
- * @returns {Buffer}
132
- */
133
- public static Float(value: number = 0): Buffer {
134
- const buffer = BufferReader.alloc(5);
135
- buffer.writeUInt8(ComponentTypeHeader.Float);
136
- buffer.writeFloatBE(value);
137
- return buffer.toBuffer();
138
- }
139
-
140
- /**
141
- * @param {number} value
142
- * @returns {Buffer}
143
- */
144
- public static Double(value: number = 0): Buffer {
145
- const buffer = BufferReader.alloc(9);
146
- buffer.writeUInt8(ComponentTypeHeader.Double);
147
- buffer.writeDoubleBE(value);
148
- return buffer.toBuffer();
149
- }
150
-
151
- /**
152
- * @param {boolean} value
153
- * @returns {Buffer}
154
- */
155
- public static Boolean(value: boolean = false): Buffer {
156
- const buffer = BufferReader.alloc(2);
157
- buffer.writeUInt8(ComponentTypeHeader.Boolean);
158
- buffer.writeUInt8(value ? 1 : 0);
159
- return buffer.toBuffer();
160
- }
161
-
162
- /**
163
- * @param {Uint8Array} buffer
164
- * @returns {Buffer}
165
- */
166
- public static ByteArray(buffer: Buffer = Buffer.from([0])): Buffer {
167
- const bufferLength = Buffer.byteLength(buffer);
168
-
169
- const prefix = BufferReader.alloc(2);
170
- prefix.writeUInt8(ComponentTypeHeader.ByteArray);
171
- prefix.writeUInt8(bufferLength);
172
-
173
- return Buffer.concat([prefix.toBuffer(), buffer]);
174
- }
175
-
176
- /**
177
- * @param {number} value
178
- * @returns {Buffer}
179
- */
180
- public static Magic(value: number): Buffer {
181
- if (value === undefined) throw new Error('Received undefined magic byte')
182
- return Buffer.from([value]);
183
- }
184
-
185
- /**
186
- * @param {number} value
187
- * @returns {Buffer}
188
- */
189
- public static Bit7(value: number = 0): Buffer {
190
- const buffer = BufferReader.alloc(this.length7BitInt(value));
191
- buffer.write7BitInt(value);
192
- return buffer.toBuffer();
193
- }
194
-
195
- /**
196
- * @param tt
197
- * @param value
198
- */
199
- public static Dynamic(tt: ComponentTypeHeader, value: boolean | number | bigint | string | Buffer): Buffer {
200
- switch (tt) {
201
- case ComponentTypeHeader.String:
202
- return this.String(value as string);
203
- case ComponentTypeHeader.Byte:
204
- return this.Byte(value as number);
205
- case ComponentTypeHeader.Int16:
206
- return this.Int16(value as number);
207
- case ComponentTypeHeader.Int32:
208
- return this.Int32(value as number);
209
- case ComponentTypeHeader.Int64:
210
- return this.Int64(value as bigint);
211
- case ComponentTypeHeader.Float:
212
- return this.Float(value as number);
213
- case ComponentTypeHeader.Double:
214
- return this.Double(value as number);
215
- case ComponentTypeHeader.Boolean:
216
- return this.Boolean(value as boolean);
217
- case ComponentTypeHeader.ByteArray:
218
- return this.ByteArray(value as Buffer);
219
- }
220
- }
221
-
222
- //
223
- //
224
- // Overrides
225
- //
226
- //
227
-
228
- /**
229
- *
230
- */
231
- public get length() {
232
- return this.buffer.length - this.offset;
233
- }
234
-
235
- /**
236
- *
237
- */
238
- public subarray(start: number = this.offset, end: number = this.length): BufferReader {
239
- return new BufferReader(this.buffer.subarray(start, this.offset + end));
240
- }
241
-
242
- /**
243
- *
244
- */
245
- public write(value: string) {
246
- return (this.offset = this.buffer.write(value, this.offset));
247
- }
248
-
249
- /**
250
- *
251
- */
252
- public writeBigInt64BE(value: bigint) {
253
- return (this.offset = this.buffer.writeBigInt64BE(value, this.offset));
254
- }
255
-
256
- /**
257
- *
258
- */
259
- public writeBigInt64LE(value: bigint) {
260
- return (this.offset = this.buffer.writeBigInt64LE(value, this.offset));
261
- }
262
-
263
- /**
264
- *
265
- */
266
- public writeUInt8(value: number) {
267
- return (this.offset = this.buffer.writeUInt8(value, this.offset));
268
- }
269
-
270
- /**
271
- *
272
- */
273
- public writeUInt16LE(value: number) {
274
- return (this.offset = this.buffer.writeUInt16LE(value, this.offset));
275
- }
276
-
277
- /**
278
- *
279
- */
280
- public writeUInt16BE(value: number) {
281
- return (this.offset = this.buffer.writeUInt16BE(value, this.offset));
282
- }
283
-
284
- /**
285
- *
286
- */
287
- public writeUInt32LE(value: number) {
288
- return (this.offset = this.buffer.writeUInt32LE(value, this.offset));
289
- }
290
-
291
- /**
292
- *
293
- */
294
- public writeUInt32BE(value: number) {
295
- return (this.offset = this.buffer.writeUInt32BE(value, this.offset));
296
- }
297
-
298
- /**
299
- *
300
- */
301
- public writeInt8(value: number) {
302
- return (this.offset = this.buffer.writeInt8(value, this.offset));
303
- }
304
-
305
- /**
306
- *
307
- */
308
- public writeInt16LE(value: number) {
309
- return (this.offset = this.buffer.writeInt16LE(value, this.offset));
310
- }
311
-
312
- /**
313
- *
314
- */
315
- public writeInt16BE(value: number) {
316
- return (this.offset = this.buffer.writeInt16BE(value, this.offset));
317
- }
318
-
319
- /**
320
- *
321
- */
322
- public writeInt32LE(value: number) {
323
- return (this.offset = this.buffer.writeInt32LE(value, this.offset));
324
- }
325
-
326
- /**
327
- *
328
- */
329
- public writeInt32BE(value: number) {
330
- return (this.offset = this.buffer.writeInt32BE(value, this.offset));
331
- }
332
-
333
- /**
334
- *
335
- */
336
- public writeFloatLE(value: number) {
337
- return (this.offset = this.buffer.writeFloatLE(value, this.offset));
338
- }
339
-
340
- /**
341
- *
342
- */
343
- public writeFloatBE(value: number) {
344
- return (this.offset = this.buffer.writeFloatBE(value, this.offset));
345
- }
346
-
347
- /**
348
- *
349
- */
350
- public writeDoubleLE(value: number) {
351
- return (this.offset = this.buffer.writeDoubleLE(value, this.offset));
352
- }
353
-
354
- /**
355
- *
356
- */
357
- public writeDoubleBE(value: number) {
358
- return (this.offset = this.buffer.writeDoubleBE(value, this.offset));
359
- }
360
-
361
- /**
362
- *
363
- */
364
- public readBigUInt64BE() {
365
- const tmp = this.buffer.readBigUInt64BE(this.offset);
366
- this.offset += 8;
367
- return tmp;
368
- }
369
-
370
- /**
371
- *
372
- */
373
- public readBigUInt64LE() {
374
- const tmp = this.buffer.readBigUInt64LE(this.offset);
375
- this.offset += 8;
376
- return tmp;
377
- }
378
-
379
- /**
380
- *
381
- */
382
- public readBigInt64BE() {
383
- const tmp = this.buffer.readBigInt64BE(this.offset);
384
- this.offset += 8;
385
- return tmp;
386
- }
387
-
388
- /**
389
- *
390
- */
391
- public readBigInt64LE() {
392
- const tmp = this.buffer.readBigInt64LE(this.offset);
393
- this.offset += 8;
394
- return tmp;
395
- }
396
-
397
- // public readUIntLE() {
398
- // const tmp = this.buffer.readUIntLE(this.offset);
399
- // this.offset += 1;
400
- // return tmp;
401
- // }
402
-
403
- // public readUIntBE() {
404
- // const tmp = this.buffer.readUIntBE(this.offset);
405
- // this.offset += 1;
406
- // return tmp;
407
- // }
408
-
409
- // public readIntLE() {
410
- // const tmp = this.buffer.readIntLE(this.offset);
411
- // this.offset += 1;
412
- // return tmp;
413
- // }
414
-
415
- // public readIntBE() {
416
- // const tmp = this.buffer.readIntBE(this.offset);
417
- // this.offset += 1;
418
- // return tmp;
419
- // }
420
-
421
- /**
422
- *
423
- */
424
- public expectUInt8(value: number) {
425
- const tmp = this.buffer.readUInt8(this.offset);
426
- this.offset += 1;
427
- if (tmp !== value) throw new Error(`Expected ${value} but got ${tmp}`);
428
- return tmp;
429
- }
430
-
431
- /**
432
- *
433
- */
434
- public readUInt8() {
435
- const tmp = this.buffer.readUInt8(this.offset);
436
- this.offset += 1;
437
- return tmp;
438
- }
439
-
440
- /**
441
- *
442
- */
443
- public readUInt16LE() {
444
- const tmp = this.buffer.readUInt16LE(this.offset);
445
- this.offset += 2;
446
- return tmp;
447
- }
448
-
449
- /**
450
- *
451
- */
452
- public readUInt16BE() {
453
- const tmp = this.buffer.readUInt16BE(this.offset);
454
- this.offset += 2;
455
- return tmp;
456
- }
457
-
458
- /**
459
- *
460
- */
461
- public readUInt32LE() {
462
- const tmp = this.buffer.readUInt32LE(this.offset);
463
- this.offset += 4;
464
- return tmp;
465
- }
466
-
467
- /**
468
- *
469
- */
470
- public readUInt32BE() {
471
- const tmp = this.buffer.readUInt32BE(this.offset);
472
- this.offset += 4;
473
- return tmp;
474
- }
475
-
476
- /**
477
- *
478
- */
479
- public readInt8() {
480
- const tmp = this.buffer.readInt8(this.offset);
481
- this.offset += 1;
482
- return tmp;
483
- }
484
-
485
- /**
486
- *
487
- */
488
- public readInt16LE() {
489
- const tmp = this.buffer.readInt16LE(this.offset);
490
- this.offset += 2;
491
- return tmp;
492
- }
493
-
494
- /**
495
- *
496
- */
497
- public readInt16BE() {
498
- const tmp = this.buffer.readInt16BE(this.offset);
499
- this.offset += 2;
500
- return tmp;
501
- }
502
-
503
- /**
504
- *
505
- */
506
- public readInt32LE() {
507
- const tmp = this.buffer.readInt32LE(this.offset);
508
- this.offset += 4;
509
- return tmp;
510
- }
511
-
512
- /**
513
- *
514
- */
515
- public readInt32BE() {
516
- const tmp = this.buffer.readInt32BE(this.offset);
517
- this.offset += 4;
518
- return tmp;
519
- }
520
-
521
- /**
522
- *
523
- */
524
- public readFloatLE() {
525
- const tmp = this.buffer.readFloatLE(this.offset);
526
- this.offset += 4;
527
- return tmp;
528
- }
529
-
530
- /**
531
- *
532
- */
533
- public readFloatBE() {
534
- const tmp = this.buffer.readFloatBE(this.offset);
535
- this.offset += 4;
536
- return tmp;
537
- }
538
-
539
- /**
540
- *
541
- */
542
- public readDoubleLE() {
543
- const tmp = this.buffer.readDoubleLE(this.offset);
544
- this.offset += 8;
545
- return tmp;
546
- }
547
-
548
- /**
549
- *
550
- */
551
- public readDoubleBE() {
552
- const tmp = this.buffer.readDoubleBE(this.offset);
553
- this.offset += 8;
554
- return tmp;
555
- }
556
-
557
- public read(tt: ComponentTypeHeader, littleEndian?: boolean): string | number | bigint | boolean | Buffer;
558
- public read(tt: ComponentTypeHeader.String, littleEndian?: boolean): string;
559
- public read(tt: ComponentTypeHeader.Byte, littleEndian?: boolean): number;
560
- public read(tt: ComponentTypeHeader.Int16, littleEndian?: boolean): number;
561
- public read(tt: ComponentTypeHeader.Int32, littleEndian?: boolean): number;
562
- public read(tt: ComponentTypeHeader.Int64, littleEndian?: boolean): bigint;
563
- public read(tt: ComponentTypeHeader.Float, littleEndian?: boolean): number;
564
- public read(tt: ComponentTypeHeader.Double, littleEndian?: boolean): number;
565
- public read(tt: ComponentTypeHeader.Boolean, littleEndian?: boolean): boolean;
566
- public read(tt: ComponentTypeHeader.ByteArray, littleEndian?: boolean): Buffer;
567
-
568
- public read(tt: ComponentTypeHeader, little = true): string | number | bigint | boolean | Buffer {
569
- switch (tt) {
570
- case ComponentTypeHeader.String:
571
- return this.readDynamicBuffer().toString("ascii");
572
- case ComponentTypeHeader.Byte:
573
- return this.readUInt8();
574
- case ComponentTypeHeader.Int16:
575
- return little ? this.readInt16LE() : this.readInt16BE();
576
- case ComponentTypeHeader.Int32:
577
- return little ? this.readInt32LE() : this.readInt32BE();
578
- case ComponentTypeHeader.Int64:
579
- return little ? this.readBigInt64LE() : this.readBigInt64BE();
580
- case ComponentTypeHeader.Float:
581
- return little ? this.readFloatLE() : this.readFloatBE();
582
- case ComponentTypeHeader.Double:
583
- return little ? this.readDoubleLE() : this.readDoubleBE();
584
- case ComponentTypeHeader.Boolean:
585
- return !!this.readUInt8();
586
- case ComponentTypeHeader.ByteArray:
587
- return this.readDynamicBuffer();
588
- }
589
- }
590
-
591
- //
592
- //
593
- // Methods
594
- //
595
- //
596
-
597
- /**
598
- *
599
- */
600
- public toBuffer(): Buffer {
601
- return this.buffer;
602
- }
603
-
604
- /**
605
- * https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer
606
- */
607
- public toArrayBuffer(): ArrayBuffer {
608
- const arrayBuffer = new ArrayBuffer(this.length);
609
- const view = new Uint8Array(arrayBuffer);
610
- for (let i = 0; i < this.length; ++i) {
611
- view[i] = this.buffer[i];
612
- }
613
- return arrayBuffer;
614
- }
615
-
616
- /**
617
- *
618
- */
619
- public at(idx: number) {
620
- return this.buffer[idx];
621
- }
622
-
623
- /**
624
- * Advanced the buffer reader by pffset.
625
- */
626
- public advanceOffset(relativeOffset = 1): this {
627
- this.offset += relativeOffset;
628
- return this;
629
- }
630
-
631
- /**
632
- * This function reads how many bytes a normal integer would take
633
- * as a 7-bit number
634
- *
635
- * 1(000 0001) 0(111 1110)
636
- */
637
- public static length7BitInt(value: number): number {
638
- let size = 0;
639
- do (value >>= 7), size++;
640
- while (value > 0);
641
- return size;
642
- }
643
-
644
- /**
645
- * Reads in an integer in 7-bit notation. A 7-bit integer
646
- * encoding splits a number into a variable size of bits,
647
- * in which the first bit is set while bytes are following.
648
- *
649
- * @example
650
- *
651
- * ```
652
- * 1111 0000 1010 1010 1000 0000 0000 0001 Reading In
653
- * ^--- ---- ^--- ---- ^--- ---- ^--- ----
654
- * 111 0000 010 1010 000 0000 000 0001 Writing Out
655
- * ```
656
- */
657
- public read7BitInt(): number {
658
- let value = 0,
659
- shift = 0,
660
- byte = 0xff;
661
-
662
- while (byte & 0x80) {
663
- byte = this.readUInt8();
664
- value |= (byte & 0x7f) << shift;
665
- shift += 7;
666
- }
667
-
668
- return value;
669
- }
670
-
671
- /**
672
- * Write a normal integer value into buffer at offset.
673
- */
674
- public write7BitInt(value: number) {
675
- while (value >= 128) {
676
- this.writeUInt8((value & 127) | 128);
677
- value >>= 7;
678
- }
679
- this.writeUInt8(value);
680
- }
681
-
682
- /**
683
- * Reads a dynamic buffer which is prepended by its' length
684
- * in 7-bit encoding.
685
- */
686
- public readDynamicBuffer() {
687
- const length = this.read7BitInt();
688
- const value = this.subarray(this.offset, length);
689
- this.offset += length;
690
- return value.toBuffer();
691
- }
692
-
693
- /**
694
- * Append a buffer to the current buffer. Asserts the cursor
695
- * to be at the end of the current buffer.
696
- */
697
- public append(buffer: Buffer) {
698
- if (this.offset !== this.length - 1) throw new Error("Cursor hasn't finished reading yet.");
699
- this.buffer = Buffer.concat([this.buffer, buffer]);
700
- return this;
701
- }
702
-
703
- /**
704
- * Keep Deserializing the buffer for typed data until
705
- * you reach the end of the buffer. Typed data consists
706
- * of a type indicator in 7-bit-encoding and data following
707
- * accordingly.
708
- */
709
- public deserialize() {
710
- const arr: (string | number | boolean | Buffer | bigint)[] = [];
711
-
712
- while (this.offset < this.length) {
713
- const type = this.read7BitInt();
714
-
715
- switch (type) {
716
- case ComponentTypeHeader.String:
717
- arr.push(this.readDynamicBuffer().toString("ascii"));
718
- break;
719
- case ComponentTypeHeader.Byte:
720
- arr.push(this.readUInt8());
721
- break;
722
- case ComponentTypeHeader.Int16: // = Int16 (short)
723
- arr.push(this.readInt16BE());
724
- break;
725
- case ComponentTypeHeader.Int32: // = Int32
726
- arr.push(this.readInt32BE());
727
- break;
728
- case ComponentTypeHeader.Int64:
729
- arr.push(this.readBigInt64BE());
730
- break;
731
- case ComponentTypeHeader.Float:
732
- arr.push(this.readFloatBE());
733
- break;
734
- case ComponentTypeHeader.Double:
735
- arr.push(this.readDoubleBE());
736
- break;
737
- case ComponentTypeHeader.Boolean:
738
- arr.push(!!this.readUInt8()); // !! is truthy
739
- break;
740
- case ComponentTypeHeader.ByteArray:
741
- arr.push(this.readDynamicBuffer());
742
- break;
743
- default:
744
- throw new Error(`While serializing a buffer for data, an unexpected type 0x${type.toString(16)} was read. Expected one of 0-8`);
745
- }
746
- }
747
-
748
- return arr;
749
- }
750
-
751
- [Symbol.for("nodejs.util.inspect.custom")]() {
752
- let s = '<BufferReader';
753
- let copy = BufferReader.from(this.buffer);
754
- copy.offset = this.offset;
755
-
756
- for (let i = 0; i < 20 && this.offset + i < this.length - 1; i++) {
757
- s += ' ' + copy.readUInt8().toString(16).padStart(2, '0');
758
- }
759
-
760
- return s + '>';
761
- }
762
- }