@@ -24,12 +24,16 @@ class Sender {
24
24
*/
25
25
constructor ( socket , extensions ) {
26
26
this . perMessageDeflate = ( extensions || { } ) [ PerMessageDeflate . extensionName ] ;
27
+ this . _socket = socket ;
28
+
27
29
this . firstFragment = true ;
28
- this . processing = false ;
29
30
this . compress = false ;
30
- this . _socket = socket ;
31
- this . onerror = null ;
31
+
32
+ this . processing = false ;
33
+ this . bufferedBytes = 0 ;
32
34
this . queue = [ ] ;
35
+
36
+ this . onerror = null ;
33
37
}
34
38
35
39
/**
@@ -86,10 +90,23 @@ class Sender {
86
90
* @public
87
91
*/
88
92
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
+
89
106
if ( this . perMessageDeflate ) {
90
- this . enqueue ( [ this . doPing , data , mask ] ) ;
107
+ this . enqueue ( [ this . doPing , data , mask , readOnly ] ) ;
91
108
} else {
92
- this . doPing ( data , mask ) ;
109
+ this . doPing ( data , mask , readOnly ) ;
93
110
}
94
111
}
95
112
@@ -98,14 +115,15 @@ class Sender {
98
115
*
99
116
* @param {* } data The message to send
100
117
* @param {Boolean } mask Specifies whether or not to mask `data`
118
+ * @param {Boolean } readOnly Specifies whether `data` can be modified
101
119
* @private
102
120
*/
103
- doPing ( data , mask ) {
121
+ doPing ( data , mask , readOnly ) {
104
122
this . frameAndSend ( data , {
105
- readOnly : true ,
106
123
opcode : 0x09 ,
107
124
rsv1 : false ,
108
125
fin : true ,
126
+ readOnly,
109
127
mask
110
128
} ) ;
111
129
@@ -120,10 +138,23 @@ class Sender {
120
138
* @public
121
139
*/
122
140
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
+
123
154
if ( this . perMessageDeflate ) {
124
- this . enqueue ( [ this . doPong , data , mask ] ) ;
155
+ this . enqueue ( [ this . doPong , data , mask , readOnly ] ) ;
125
156
} else {
126
- this . doPong ( data , mask ) ;
157
+ this . doPong ( data , mask , readOnly ) ;
127
158
}
128
159
}
129
160
@@ -132,14 +163,15 @@ class Sender {
132
163
*
133
164
* @param {* } data The message to send
134
165
* @param {Boolean } mask Specifies whether or not to mask `data`
166
+ * @param {Boolean } readOnly Specifies whether `data` can be modified
135
167
* @private
136
168
*/
137
- doPong ( data , mask ) {
169
+ doPong ( data , mask , readOnly ) {
138
170
this . frameAndSend ( data , {
139
- readOnly : true ,
140
171
opcode : 0x0a ,
141
172
rsv1 : false ,
142
173
fin : true ,
174
+ readOnly,
143
175
mask
144
176
} ) ;
145
177
@@ -243,7 +275,7 @@ class Sender {
243
275
/**
244
276
* Frames and sends a piece of data according to the HyBi WebSocket protocol.
245
277
*
246
- * @param {* } data The data to send
278
+ * @param {Buffer } data The data to send
247
279
* @param {Object } options Options object
248
280
* @param {Number } options.opcode The opcode
249
281
* @param {Boolean } options.readOnly Specifies whether `data` can be modified
@@ -267,17 +299,6 @@ class Sender {
267
299
return ;
268
300
}
269
301
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
-
281
302
const mergeBuffers = data . length < 1024 || options . mask && options . readOnly ;
282
303
var dataOffset = options . mask ? 6 : 2 ;
283
304
var payloadLength = data . length ;
@@ -334,12 +355,13 @@ class Sender {
334
355
dequeue ( ) {
335
356
if ( this . processing ) return ;
336
357
337
- const handler = this . queue . shift ( ) ;
338
- if ( ! handler ) return ;
358
+ const params = this . queue . shift ( ) ;
359
+ if ( ! params ) return ;
339
360
361
+ if ( params [ 1 ] ) this . bufferedBytes -= params [ 1 ] . length ;
340
362
this . processing = true ;
341
363
342
- handler [ 0 ] . apply ( this , handler . slice ( 1 ) ) ;
364
+ params [ 0 ] . apply ( this , params . slice ( 1 ) ) ;
343
365
}
344
366
345
367
/**
@@ -361,6 +383,7 @@ class Sender {
361
383
* @private
362
384
*/
363
385
enqueue ( params ) {
386
+ if ( params [ 1 ] ) this . bufferedBytes += params [ 1 ] . length ;
364
387
this . queue . push ( params ) ;
365
388
this . dequeue ( ) ;
366
389
}
0 commit comments