@@ -11,7 +11,7 @@ const { EMPTY_BUFFER } = require('./constants');
1111const { isValidStatusCode } = require ( './validation' ) ;
1212const { mask : applyMask , toBuffer } = require ( './buffer-util' ) ;
1313
14- const mask = Buffer . alloc ( 4 ) ;
14+ const _mask = Buffer . alloc ( 4 ) ;
1515
1616/**
1717 * HyBi Sender implementation.
@@ -42,8 +42,9 @@ class Sender {
4242 * @param {Object } options Options object
4343 * @param {Boolean } [options.fin=false] Specifies whether or not to set the
4444 * FIN bit
45- * @param {Boolean } [options.mask=false] Specifies whether or not to mask
46- * `data`
45+ * @param {(Boolean|Buffer) } [options.mask=false] Specifies whether or not to
46+ * mask `data`. If a `Buffer` is provided, then its contents is used as
47+ * the masking key
4748 * @param {Number } options.opcode The opcode
4849 * @param {Boolean } [options.readOnly=false] Specifies whether `data` can be
4950 * modified
@@ -81,14 +82,24 @@ class Sender {
8182
8283 if ( ! options . mask ) return [ target , data ] ;
8384
84- randomFillSync ( mask , 0 , 4 ) ;
85+ let mask = _mask ;
86+
87+ if ( Buffer . isBuffer ( options . mask ) ) {
88+ mask = options . mask ;
89+ } else {
90+ randomFillSync ( _mask , 0 , 4 ) ;
91+ }
8592
8693 target [ 1 ] |= 0x80 ;
8794 target [ offset - 4 ] = mask [ 0 ] ;
8895 target [ offset - 3 ] = mask [ 1 ] ;
8996 target [ offset - 2 ] = mask [ 2 ] ;
9097 target [ offset - 1 ] = mask [ 3 ] ;
9198
99+ if ( ( mask [ 0 ] | mask [ 1 ] | mask [ 2 ] | mask [ 3 ] ) === 0 ) {
100+ return [ target , data ] ;
101+ }
102+
92103 if ( merge ) {
93104 applyMask ( data , mask , target , offset , data . length ) ;
94105 return [ target ] ;
@@ -103,7 +114,9 @@ class Sender {
103114 *
104115 * @param {Number } [code] The status code component of the body
105116 * @param {(String|Buffer) } [data] The message component of the body
106- * @param {Boolean } [mask=false] Specifies whether or not to mask the message
117+ * @param {(Boolean|Buffer) } [mask=false] Specifies whether or not to mask the
118+ * message. If a `Buffer` is provided, then its contents is used as the
119+ * masking key
107120 * @param {Function } [cb] Callback
108121 * @public
109122 */
@@ -145,7 +158,9 @@ class Sender {
145158 * Frames and sends a close message.
146159 *
147160 * @param {Buffer } data The message to send
148- * @param {Boolean } [mask=false] Specifies whether or not to mask `data`
161+ * @param {(Boolean|Buffer) } [mask=false] Specifies whether or not to mask
162+ * `data`. If a `Buffer` is provided, then its contents is used as the
163+ * masking key
149164 * @param {Function } [cb] Callback
150165 * @private
151166 */
@@ -166,7 +181,9 @@ class Sender {
166181 * Sends a ping message to the other peer.
167182 *
168183 * @param {* } data The message to send
169- * @param {Boolean } [mask=false] Specifies whether or not to mask `data`
184+ * @param {(Boolean|Buffer) } [mask=false] Specifies whether or not to mask
185+ * `data`. If a `Buffer` is provided, then its contents is used as the
186+ * masking key
170187 * @param {Function } [cb] Callback
171188 * @public
172189 */
@@ -188,7 +205,9 @@ class Sender {
188205 * Frames and sends a ping message.
189206 *
190207 * @param {Buffer } data The message to send
191- * @param {Boolean } [mask=false] Specifies whether or not to mask `data`
208+ * @param {(Boolean|Buffer) } [mask=false] Specifies whether or not to mask
209+ * `data`. If a `Buffer` is provided, then its contents is used as the
210+ * masking key
192211 * @param {Boolean } [readOnly=false] Specifies whether `data` can be modified
193212 * @param {Function } [cb] Callback
194213 * @private
@@ -210,7 +229,9 @@ class Sender {
210229 * Sends a pong message to the other peer.
211230 *
212231 * @param {* } data The message to send
213- * @param {Boolean } [mask=false] Specifies whether or not to mask `data`
232+ * @param {(Boolean|Buffer) } [mask=false] Specifies whether or not to mask
233+ * `data`. If a `Buffer` is provided, then its contents is used as the
234+ * masking key
214235 * @param {Function } [cb] Callback
215236 * @public
216237 */
@@ -232,7 +253,9 @@ class Sender {
232253 * Frames and sends a pong message.
233254 *
234255 * @param {Buffer } data The message to send
235- * @param {Boolean } [mask=false] Specifies whether or not to mask `data`
256+ * @param {(Boolean|Buffer) } [mask=false] Specifies whether or not to mask
257+ * `data`. If a `Buffer` is provided, then its contents is used as the
258+ * masking key
236259 * @param {Boolean } [readOnly=false] Specifies whether `data` can be modified
237260 * @param {Function } [cb] Callback
238261 * @private
@@ -261,8 +284,9 @@ class Sender {
261284 * compress `data`
262285 * @param {Boolean } [options.fin=false] Specifies whether the fragment is the
263286 * last one
264- * @param {Boolean } [options.mask=false] Specifies whether or not to mask
265- * `data`
287+ * @param {(Boolean|Buffer) } [options.mask=false] Specifies whether or not to
288+ * mask `data`. If a `Buffer` is provided, then its contents is used as
289+ * the masking key
266290 * @param {Function } [cb] Callback
267291 * @public
268292 */
@@ -331,8 +355,9 @@ class Sender {
331355 * @param {Number } options.opcode The opcode
332356 * @param {Boolean } [options.fin=false] Specifies whether or not to set the
333357 * FIN bit
334- * @param {Boolean } [options.mask=false] Specifies whether or not to mask
335- * `data`
358+ * @param {(Boolean|Buffer) } [options.mask=false] Specifies whether or not to
359+ * mask `data`. If a `Buffer` is provided, then its contents is used as
360+ * the masking key
336361 * @param {Boolean } [options.readOnly=false] Specifies whether `data` can be
337362 * modified
338363 * @param {Boolean } [options.rsv1=false] Specifies whether or not to set the
@@ -348,6 +373,7 @@ class Sender {
348373
349374 const perMessageDeflate = this . _extensions [ PerMessageDeflate . extensionName ] ;
350375
376+ if ( options . mask && options . mask . length ) this . _bufferedBytes += 4 ;
351377 this . _bufferedBytes += data . length ;
352378 this . _deflating = true ;
353379 perMessageDeflate . compress ( data , options . fin , ( _ , buf ) => {
@@ -367,6 +393,7 @@ class Sender {
367393 return ;
368394 }
369395
396+ if ( options . mask && options . mask . length ) this . _bufferedBytes -= 4 ;
370397 this . _bufferedBytes -= data . length ;
371398 this . _deflating = false ;
372399 options . readOnly = false ;
@@ -383,7 +410,9 @@ class Sender {
383410 dequeue ( ) {
384411 while ( ! this . _deflating && this . _queue . length ) {
385412 const params = this . _queue . shift ( ) ;
413+ const mask = params [ 0 ] === this . dispatch ? params [ 3 ] . mask : params [ 2 ] ;
386414
415+ if ( mask && mask . length ) this . _bufferedBytes -= 4 ;
387416 this . _bufferedBytes -= params [ 1 ] . length ;
388417 Reflect . apply ( params [ 0 ] , this , params . slice ( 1 ) ) ;
389418 }
@@ -396,7 +425,11 @@ class Sender {
396425 * @private
397426 */
398427 enqueue ( params ) {
428+ const mask = params [ 0 ] === this . dispatch ? params [ 3 ] . mask : params [ 2 ] ;
429+
430+ if ( mask && mask . length ) this . _bufferedBytes += 4 ;
399431 this . _bufferedBytes += params [ 1 ] . length ;
432+
400433 this . _queue . push ( params ) ;
401434 }
402435
0 commit comments