1- const zlib = require ( 'zlib' ) ;
2-
31module . exports = class {
42 constructor ( obj ) {
53 this . payload = new Uint8Array ( obj ) ;
64 this . offset = 0 ;
75 }
8-
96 set ( obj ) {
107 this . payload = new Uint8Array ( obj ) ;
118 }
12-
139 write ( a1 ) {
1410 this . payload [ this . offset ++ ] = a1 ;
1511 }
16-
12+ read ( ) {
13+ return this . payload [ this . offset ++ ] ;
14+ }
1715 writeUInt ( a1 ) {
1816 this . write ( a1 & 0xFF ) ;
1917 }
20-
2118 writeByte ( a1 ) {
2219 this . write ( a1 ) ;
2320 }
24-
2521 writeBoolean ( a1 ) {
26- this . write ( a1 ? 1 : 0 ) ;
22+ this . write ( a1 ? 1 : 0 ) ;
2723 }
28-
2924 writeInt ( a1 ) {
3025 this . write ( ( a1 >> 24 ) & 0xFF ) ;
3126 this . write ( ( a1 >> 16 ) & 0xFF ) ;
3227 this . write ( ( a1 >> 8 ) & 0xFF ) ;
3328 this . write ( a1 & 0xFF ) ;
3429 }
35-
36- writeIntLittleEndian ( value ) {
37- this . write ( value & 0xFF ) ;
38- this . write ( ( value >> 8 ) & 0xFF ) ;
39- this . write ( ( value >> 16 ) & 0xFF ) ;
40- this . write ( ( value >> 24 ) & 0xFF ) ;
41- }
42-
43- writeCompressedString ( data ) {
44- const compressedText = zlib . deflateSync ( Buffer . from ( data ) ) ;
45- const totalLength = compressedText . length + 4 ;
46- this . writeInt ( totalLength ) ;
47- this . writeIntLittleEndian ( data . length ) ;
48- const newPayload = new Uint8Array ( this . payload . length + compressedText . length ) ;
49- newPayload . set ( this . payload . slice ( 0 , this . offset ) ) ;
50- newPayload . set ( compressedText , this . offset ) ;
51- this . payload = newPayload ;
52- this . offset += compressedText . length ;
53- }
54-
5530 writeString ( a1 ) {
5631 if ( ! a1 ) return this . writeInt ( - 1 ) ;
5732 let b = new Uint8Array ( Buffer . from ( a1 ) ) ;
@@ -60,13 +35,12 @@ module.exports = class {
6035 this . write ( b [ strOffset ] ) ;
6136 }
6237 }
63-
6438 writeVInt ( a1 ) {
65- let v1 = ( ( ( a1 >> 25 ) & 0x40 ) | ( a1 & 0x3F ) ) ,
66- v2 = ( ( a1 ^ ( a1 >> 31 ) ) >> 6 ) , v3 ;
67-
39+ let v1 = ( ( ( a1 >> 25 ) & 0x40 ) | ( a1 & 0x3F ) ) ,
40+ v2 = ( ( a1 ^ ( a1 >> 31 ) ) >> 6 ) , v3
41+
6842 a1 >>= 6 ;
69- if ( v2 === 0 ) {
43+ if ( v2 == 0 ) {
7044 this . writeByte ( v1 ) ;
7145 } else {
7246 this . writeByte ( v1 | 0x80 ) ;
@@ -77,7 +51,7 @@ module.exports = class {
7751 }
7852 this . writeByte ( ( a1 & 0x7F ) | v3 ) ;
7953 a1 >>= 7 ;
80- while ( v2 !== 0 ) {
54+ while ( v2 != 0 ) {
8155 v2 >>= 7 ;
8256 v3 = 0 ;
8357 if ( v2 > 0 ) {
@@ -88,53 +62,46 @@ module.exports = class {
8862 }
8963 }
9064 }
91-
9265 writeDataReference ( a1 , a2 ) {
9366 this . writeVInt ( a1 ) ;
94- if ( a1 === 0 ) return ;
67+ if ( a1 == 0 ) return ;
9568 this . writeVInt ( a2 ) ;
9669 }
97-
9870 readDataReference ( ) {
9971 let a1 = this . readVInt ( ) ;
100- return [ a1 , a1 === 0 ? 0 : this . readVInt ( ) ] ;
72+ return [ a1 , a1 == 0 ? 0 : this . readVInt ( ) ] ;
10173 }
102-
10374 readInt ( ) {
10475 return ( this . read ( ) << 24 | this . read ( ) << 16 | this . read ( ) << 8 | this . read ( ) ) ;
10576 }
106-
10777 readByte ( ) {
10878 return this . read ( ) ;
10979 }
110-
11180 readBytes ( size ) {
11281 let result = new Uint8Array ( size ) ;
11382 for ( let index = 0 ; index < size ; index ++ ) {
11483 result [ index ] = this . readByte ( ) ;
11584 }
11685 return result ;
11786 }
118-
11987 readBoolean ( ) {
12088 return Boolean ( this . read ( ) ) ;
12189 }
122-
12390 readString ( ) {
12491 let len = this . readInt ( ) ;
125- if ( len <= 0 || len === 4294967295 ) {
92+ if ( len <= 0 || len == 4294967295 ) {
12693 return "" ;
12794 }
12895 return Buffer . from ( this . readBytes ( len ) ) . toString ( ) ;
12996 }
130-
13197 readVInt ( ) {
98+ // this method is discovered by nameless#1347
13299 let result = 0 ,
133- shift = 0 , b , seventh , msb , n ;
100+ shift = 0 , b , seventh , msb , n ;
134101
135102 while ( true ) {
136103 b = this . read ( ) ;
137- if ( shift === 0 ) {
104+ if ( shift == 0 ) {
138105 seventh = ( b & 0x40 ) >> 6 ;
139106 msb = ( b & 0x80 ) >> 7 ;
140107 n = b << 1 ;
@@ -149,8 +116,7 @@ module.exports = class {
149116 }
150117 return ( result >> 1 ) ^ ( - ( result & 1 ) ) ;
151118 }
152-
153119 getBytes ( ) {
154120 return this . payload . slice ( 0 , this . offset ) ;
155121 }
156- } ;
122+ }
0 commit comments