@@ -20,6 +20,8 @@ var EventEmitter = require('events').EventEmitter;
20
20
var WebSocketFrame = require ( './WebSocketFrame' ) ;
21
21
var BufferList = require ( '../vendor/FastBufferList' ) ;
22
22
var Validation = require ( './Validation' ) . Validation ;
23
+ var bufferAllocUnsafe = utils . bufferAllocUnsafe ;
24
+ var bufferFromString = utils . bufferFromString ;
23
25
24
26
// Connected, fully-open, ready to send and receive frames
25
27
const STATE_OPEN = 'open' ;
@@ -73,8 +75,8 @@ function WebSocketConnection(socket, extensions, protocol, maskOutgoingPackets,
73
75
// We re-use the same buffers for the mask and frame header for all frames
74
76
// received on each connection to avoid a small memory allocation for each
75
77
// frame.
76
- this . maskBytes = new Buffer ( 4 ) ;
77
- this . frameHeader = new Buffer ( 10 ) ;
78
+ this . maskBytes = bufferAllocUnsafe ( 4 ) ;
79
+ this . frameHeader = bufferAllocUnsafe ( 10 ) ;
78
80
79
81
// the BufferList will handle the data streaming in
80
82
this . bufferList = new BufferList ( ) ;
@@ -585,7 +587,7 @@ WebSocketConnection.prototype.processFrame = function(frame) {
585
587
// message now. We also have to decode the utf-8 data
586
588
// for text frames after combining all the fragments.
587
589
var bytesCopied = 0 ;
588
- var binaryPayload = new Buffer ( this . fragmentationSize ) ;
590
+ var binaryPayload = bufferAllocUnsafe ( this . fragmentationSize ) ;
589
591
var opcode = this . frameQueue [ 0 ] . opcode ;
590
592
this . frameQueue . forEach ( function ( currentFrame ) {
591
593
currentFrame . binaryPayload . copy ( binaryPayload , bytesCopied ) ;
@@ -725,7 +727,7 @@ WebSocketConnection.prototype.send = function(data, cb) {
725
727
} ;
726
728
727
729
WebSocketConnection . prototype . sendUTF = function ( data , cb ) {
728
- data = new Buffer ( data . toString ( ) , 'utf8' ) ;
730
+ data = bufferFromString ( data . toString ( ) , 'utf8' ) ;
729
731
this . _debug ( 'sendUTF: %d bytes' , data . length ) ;
730
732
var frame = new WebSocketFrame ( this . maskBytes , this . frameHeader , this . config ) ;
731
733
frame . opcode = 0x01 ; // WebSocketOpcode.TEXT_FRAME
@@ -751,7 +753,7 @@ WebSocketConnection.prototype.ping = function(data) {
751
753
frame . fin = true ;
752
754
if ( data ) {
753
755
if ( ! Buffer . isBuffer ( data ) ) {
754
- data = new Buffer ( data . toString ( ) , 'utf8' ) ;
756
+ data = bufferFromString ( data . toString ( ) , 'utf8' ) ;
755
757
}
756
758
if ( data . length > 125 ) {
757
759
this . _debug ( 'WebSocket: Data for ping is longer than 125 bytes. Truncating.' ) ;
@@ -844,7 +846,7 @@ WebSocketConnection.prototype.sendCloseFrame = function(reasonCode, description,
844
846
frame . opcode = 0x08 ; // WebSocketOpcode.CONNECTION_CLOSE
845
847
frame . closeStatus = reasonCode ;
846
848
if ( typeof ( description ) === 'string' ) {
847
- frame . binaryPayload = new Buffer ( description , 'utf8' ) ;
849
+ frame . binaryPayload = bufferFromString ( description , 'utf8' ) ;
848
850
}
849
851
850
852
this . sendFrame ( frame , cb ) ;
0 commit comments