@@ -24,12 +24,16 @@ class Sender {
2424 */
2525 constructor ( socket , extensions ) {
2626 this . perMessageDeflate = ( extensions || { } ) [ PerMessageDeflate . extensionName ] ;
27+ this . _socket = socket ;
28+
2729 this . firstFragment = true ;
28- this . processing = false ;
2930 this . compress = false ;
30- this . _socket = socket ;
31- this . onerror = null ;
31+
32+ this . processing = false ;
33+ this . bufferedBytes = 0 ;
3234 this . queue = [ ] ;
35+
36+ this . onerror = null ;
3337 }
3438
3539 /**
@@ -86,10 +90,23 @@ class Sender {
8690 * @public
8791 */
8892 ping ( data , mask ) {
93+ var readOnly = true ;
94+
95+ if ( data && ! Buffer . isBuffer ( data ) ) {
96+ if ( data instanceof ArrayBuffer ) {
97+ data = Buffer . from ( data ) ;
98+ } else if ( ArrayBuffer . isView ( data ) ) {
99+ data = viewToBuffer ( data ) ;
100+ } else {
101+ data = Buffer . from ( data ) ;
102+ readOnly = false ;
103+ }
104+ }
105+
89106 if ( this . perMessageDeflate ) {
90- this . enqueue ( [ this . doPing , data , mask ] ) ;
107+ this . enqueue ( [ this . doPing , data , mask , readOnly ] ) ;
91108 } else {
92- this . doPing ( data , mask ) ;
109+ this . doPing ( data , mask , readOnly ) ;
93110 }
94111 }
95112
@@ -98,14 +115,15 @@ class Sender {
98115 *
99116 * @param {* } data The message to send
100117 * @param {Boolean } mask Specifies whether or not to mask `data`
118+ * @param {Boolean } readOnly Specifies whether `data` can be modified
101119 * @private
102120 */
103- doPing ( data , mask ) {
121+ doPing ( data , mask , readOnly ) {
104122 this . frameAndSend ( data , {
105- readOnly : true ,
106123 opcode : 0x09 ,
107124 rsv1 : false ,
108125 fin : true ,
126+ readOnly,
109127 mask
110128 } ) ;
111129
@@ -120,10 +138,23 @@ class Sender {
120138 * @public
121139 */
122140 pong ( data , mask ) {
141+ var readOnly = true ;
142+
143+ if ( data && ! Buffer . isBuffer ( data ) ) {
144+ if ( data instanceof ArrayBuffer ) {
145+ data = Buffer . from ( data ) ;
146+ } else if ( ArrayBuffer . isView ( data ) ) {
147+ data = viewToBuffer ( data ) ;
148+ } else {
149+ data = Buffer . from ( data ) ;
150+ readOnly = false ;
151+ }
152+ }
153+
123154 if ( this . perMessageDeflate ) {
124- this . enqueue ( [ this . doPong , data , mask ] ) ;
155+ this . enqueue ( [ this . doPong , data , mask , readOnly ] ) ;
125156 } else {
126- this . doPong ( data , mask ) ;
157+ this . doPong ( data , mask , readOnly ) ;
127158 }
128159 }
129160
@@ -132,14 +163,15 @@ class Sender {
132163 *
133164 * @param {* } data The message to send
134165 * @param {Boolean } mask Specifies whether or not to mask `data`
166+ * @param {Boolean } readOnly Specifies whether `data` can be modified
135167 * @private
136168 */
137- doPong ( data , mask ) {
169+ doPong ( data , mask , readOnly ) {
138170 this . frameAndSend ( data , {
139- readOnly : true ,
140171 opcode : 0x0a ,
141172 rsv1 : false ,
142173 fin : true ,
174+ readOnly,
143175 mask
144176 } ) ;
145177
@@ -243,7 +275,7 @@ class Sender {
243275 /**
244276 * Frames and sends a piece of data according to the HyBi WebSocket protocol.
245277 *
246- * @param {* } data The data to send
278+ * @param {Buffer } data The data to send
247279 * @param {Object } options Options object
248280 * @param {Number } options.opcode The opcode
249281 * @param {Boolean } options.readOnly Specifies whether `data` can be modified
@@ -267,17 +299,6 @@ class Sender {
267299 return ;
268300 }
269301
270- if ( ! Buffer . isBuffer ( data ) ) {
271- if ( data instanceof ArrayBuffer ) {
272- data = Buffer . from ( data ) ;
273- } else if ( ArrayBuffer . isView ( data ) ) {
274- data = viewToBuffer ( data ) ;
275- } else {
276- data = Buffer . from ( data ) ;
277- options . readOnly = false ;
278- }
279- }
280-
281302 const mergeBuffers = data . length < 1024 || options . mask && options . readOnly ;
282303 var dataOffset = options . mask ? 6 : 2 ;
283304 var payloadLength = data . length ;
@@ -334,12 +355,13 @@ class Sender {
334355 dequeue ( ) {
335356 if ( this . processing ) return ;
336357
337- const handler = this . queue . shift ( ) ;
338- if ( ! handler ) return ;
358+ const params = this . queue . shift ( ) ;
359+ if ( ! params ) return ;
339360
361+ if ( params [ 1 ] ) this . bufferedBytes -= params [ 1 ] . length ;
340362 this . processing = true ;
341363
342- handler [ 0 ] . apply ( this , handler . slice ( 1 ) ) ;
364+ params [ 0 ] . apply ( this , params . slice ( 1 ) ) ;
343365 }
344366
345367 /**
@@ -361,6 +383,7 @@ class Sender {
361383 * @private
362384 */
363385 enqueue ( params ) {
386+ if ( params [ 1 ] ) this . bufferedBytes += params [ 1 ] . length ;
364387 this . queue . push ( params ) ;
365388 this . dequeue ( ) ;
366389 }
0 commit comments