1+ const zlib = require ( 'zlib' ) ;
2+
13module . exports = class {
24 constructor ( obj ) {
35 this . payload = new Uint8Array ( obj ) ;
46 this . offset = 0 ;
57 }
8+
69 set ( obj ) {
710 this . payload = new Uint8Array ( obj ) ;
811 }
12+
913 write ( a1 ) {
1014 this . payload [ this . offset ++ ] = a1 ;
1115 }
12- read ( ) {
13- return this . payload [ this . offset ++ ] ;
14- }
16+
1517 writeUInt ( a1 ) {
1618 this . write ( a1 & 0xFF ) ;
1719 }
20+
1821 writeByte ( a1 ) {
1922 this . write ( a1 ) ;
2023 }
24+
2125 writeBoolean ( a1 ) {
22- this . write ( a1 ? 1 : 0 ) ;
26+ this . write ( a1 ? 1 : 0 ) ;
2327 }
28+
2429 writeInt ( a1 ) {
2530 this . write ( ( a1 >> 24 ) & 0xFF ) ;
2631 this . write ( ( a1 >> 16 ) & 0xFF ) ;
2732 this . write ( ( a1 >> 8 ) & 0xFF ) ;
2833 this . write ( a1 & 0xFF ) ;
2934 }
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+
3055 writeString ( a1 ) {
3156 if ( ! a1 ) return this . writeInt ( - 1 ) ;
3257 let b = new Uint8Array ( Buffer . from ( a1 ) ) ;
@@ -35,12 +60,13 @@ module.exports = class {
3560 this . write ( b [ strOffset ] ) ;
3661 }
3762 }
63+
3864 writeVInt ( a1 ) {
39- let v1 = ( ( ( a1 >> 25 ) & 0x40 ) | ( a1 & 0x3F ) ) ,
40- v2 = ( ( a1 ^ ( a1 >> 31 ) ) >> 6 ) , v3
41-
65+ let v1 = ( ( ( a1 >> 25 ) & 0x40 ) | ( a1 & 0x3F ) ) ,
66+ v2 = ( ( a1 ^ ( a1 >> 31 ) ) >> 6 ) , v3 ;
67+
4268 a1 >>= 6 ;
43- if ( v2 == 0 ) {
69+ if ( v2 === 0 ) {
4470 this . writeByte ( v1 ) ;
4571 } else {
4672 this . writeByte ( v1 | 0x80 ) ;
@@ -51,7 +77,7 @@ module.exports = class {
5177 }
5278 this . writeByte ( ( a1 & 0x7F ) | v3 ) ;
5379 a1 >>= 7 ;
54- while ( v2 != 0 ) {
80+ while ( v2 !== 0 ) {
5581 v2 >>= 7 ;
5682 v3 = 0 ;
5783 if ( v2 > 0 ) {
@@ -62,46 +88,53 @@ module.exports = class {
6288 }
6389 }
6490 }
91+
6592 writeDataReference ( a1 , a2 ) {
6693 this . writeVInt ( a1 ) ;
67- if ( a1 == 0 ) return ;
94+ if ( a1 === 0 ) return ;
6895 this . writeVInt ( a2 ) ;
6996 }
97+
7098 readDataReference ( ) {
7199 let a1 = this . readVInt ( ) ;
72- return [ a1 , a1 == 0 ? 0 : this . readVInt ( ) ] ;
100+ return [ a1 , a1 === 0 ? 0 : this . readVInt ( ) ] ;
73101 }
102+
74103 readInt ( ) {
75104 return ( this . read ( ) << 24 | this . read ( ) << 16 | this . read ( ) << 8 | this . read ( ) ) ;
76105 }
106+
77107 readByte ( ) {
78108 return this . read ( ) ;
79109 }
110+
80111 readBytes ( size ) {
81112 let result = new Uint8Array ( size ) ;
82113 for ( let index = 0 ; index < size ; index ++ ) {
83114 result [ index ] = this . readByte ( ) ;
84115 }
85116 return result ;
86117 }
118+
87119 readBoolean ( ) {
88120 return Boolean ( this . read ( ) ) ;
89121 }
122+
90123 readString ( ) {
91124 let len = this . readInt ( ) ;
92- if ( len <= 0 || len == 4294967295 ) {
125+ if ( len <= 0 || len === 4294967295 ) {
93126 return "" ;
94127 }
95128 return Buffer . from ( this . readBytes ( len ) ) . toString ( ) ;
96129 }
130+
97131 readVInt ( ) {
98- // this method is discovered by nameless#1347
99132 let result = 0 ,
100- shift = 0 , b , seventh , msb , n ;
133+ shift = 0 , b , seventh , msb , n ;
101134
102135 while ( true ) {
103136 b = this . read ( ) ;
104- if ( shift == 0 ) {
137+ if ( shift === 0 ) {
105138 seventh = ( b & 0x40 ) >> 6 ;
106139 msb = ( b & 0x80 ) >> 7 ;
107140 n = b << 1 ;
@@ -116,7 +149,8 @@ module.exports = class {
116149 }
117150 return ( result >> 1 ) ^ ( - ( result & 1 ) ) ;
118151 }
152+
119153 getBytes ( ) {
120154 return this . payload . slice ( 0 , this . offset ) ;
121155 }
122- }
156+ } ;
0 commit comments