Skip to content

Commit 99456cc

Browse files
committedSep 20, 2018
Fixing buffer deprecation warning.
1 parent fbee2fc commit 99456cc

9 files changed

+41
-28
lines changed
 

‎lib/WebSocketClient.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var https = require('https');
2323
var url = require('url');
2424
var crypto = require('crypto');
2525
var WebSocketConnection = require('./WebSocketConnection');
26+
var bufferAllocUnsafe = utils.bufferAllocUnsafe;
2627

2728
var protocolSeparators = [
2829
'(', ')', '<', '>', '@',
@@ -166,7 +167,7 @@ WebSocketClient.prototype.connect = function(requestUrl, protocols, origin, head
166167
this.url.port = defaultPorts[this.url.protocol];
167168
}
168169

169-
var nonce = new Buffer(16);
170+
var nonce = bufferAllocUnsafe(16);
170171
for (var i=0; i < 16; i++) {
171172
nonce[i] = Math.round(Math.random()*0xFF);
172173
}

‎lib/WebSocketConnection.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var EventEmitter = require('events').EventEmitter;
2020
var WebSocketFrame = require('./WebSocketFrame');
2121
var BufferList = require('../vendor/FastBufferList');
2222
var Validation = require('./Validation').Validation;
23+
var bufferAllocUnsafe = utils.bufferAllocUnsafe;
24+
var bufferFromString = utils.bufferFromString;
2325

2426
// Connected, fully-open, ready to send and receive frames
2527
const STATE_OPEN = 'open';
@@ -73,8 +75,8 @@ function WebSocketConnection(socket, extensions, protocol, maskOutgoingPackets,
7375
// We re-use the same buffers for the mask and frame header for all frames
7476
// received on each connection to avoid a small memory allocation for each
7577
// frame.
76-
this.maskBytes = new Buffer(4);
77-
this.frameHeader = new Buffer(10);
78+
this.maskBytes = bufferAllocUnsafe(4);
79+
this.frameHeader = bufferAllocUnsafe(10);
7880

7981
// the BufferList will handle the data streaming in
8082
this.bufferList = new BufferList();
@@ -585,7 +587,7 @@ WebSocketConnection.prototype.processFrame = function(frame) {
585587
// message now. We also have to decode the utf-8 data
586588
// for text frames after combining all the fragments.
587589
var bytesCopied = 0;
588-
var binaryPayload = new Buffer(this.fragmentationSize);
590+
var binaryPayload = bufferAllocUnsafe(this.fragmentationSize);
589591
var opcode = this.frameQueue[0].opcode;
590592
this.frameQueue.forEach(function (currentFrame) {
591593
currentFrame.binaryPayload.copy(binaryPayload, bytesCopied);
@@ -725,7 +727,7 @@ WebSocketConnection.prototype.send = function(data, cb) {
725727
};
726728

727729
WebSocketConnection.prototype.sendUTF = function(data, cb) {
728-
data = new Buffer(data.toString(), 'utf8');
730+
data = bufferFromString(data.toString(), 'utf8');
729731
this._debug('sendUTF: %d bytes', data.length);
730732
var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
731733
frame.opcode = 0x01; // WebSocketOpcode.TEXT_FRAME
@@ -751,7 +753,7 @@ WebSocketConnection.prototype.ping = function(data) {
751753
frame.fin = true;
752754
if (data) {
753755
if (!Buffer.isBuffer(data)) {
754-
data = new Buffer(data.toString(), 'utf8');
756+
data = bufferFromString(data.toString(), 'utf8');
755757
}
756758
if (data.length > 125) {
757759
this._debug('WebSocket: Data for ping is longer than 125 bytes. Truncating.');
@@ -844,7 +846,7 @@ WebSocketConnection.prototype.sendCloseFrame = function(reasonCode, description,
844846
frame.opcode = 0x08; // WebSocketOpcode.CONNECTION_CLOSE
845847
frame.closeStatus = reasonCode;
846848
if (typeof(description) === 'string') {
847-
frame.binaryPayload = new Buffer(description, 'utf8');
849+
frame.binaryPayload = bufferFromString(description, 'utf8');
848850
}
849851

850852
this.sendFrame(frame, cb);

‎lib/WebSocketFrame.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
***********************************************************************/
1616

1717
var bufferUtil = require('./BufferUtil').BufferUtil;
18+
var bufferAllocUnsafe = require('./utils').bufferAllocUnsafe;
1819

1920
const DECODE_HEADER = 1;
2021
const WAITING_FOR_16_BIT_LENGTH = 2;
@@ -130,7 +131,7 @@ WebSocketFrame.prototype.addData = function(bufferList) {
130131
}
131132

132133
if (this.length === 0) {
133-
this.binaryPayload = new Buffer(0);
134+
this.binaryPayload = bufferAllocUnsafe(0);
134135
this.parseState = COMPLETE;
135136
return true;
136137
}
@@ -145,7 +146,7 @@ WebSocketFrame.prototype.addData = function(bufferList) {
145146
if (this.opcode === 0x08) { // WebSocketOpcode.CONNECTION_CLOSE
146147
if (this.length === 1) {
147148
// Invalid length for a close frame. Must be zero or at least two.
148-
this.binaryPayload = new Buffer(0);
149+
this.binaryPayload = bufferAllocUnsafe(0);
149150
this.invalidCloseFrameLength = true;
150151
}
151152
if (this.length >= 2) {
@@ -203,7 +204,7 @@ WebSocketFrame.prototype.toBuffer = function(nullMask) {
203204
if (this.binaryPayload) {
204205
this.length += this.binaryPayload.length;
205206
}
206-
data = new Buffer(this.length);
207+
data = bufferAllocUnsafe(this.length);
207208
data.writeUInt16BE(this.closeStatus, 0);
208209
if (this.length > 2) {
209210
this.binaryPayload.copy(data, 2);
@@ -232,7 +233,7 @@ WebSocketFrame.prototype.toBuffer = function(nullMask) {
232233
headerLength += 8;
233234
}
234235

235-
var output = new Buffer(this.length + headerLength + (this.mask ? 4 : 0));
236+
var output = bufferAllocUnsafe(this.length + headerLength + (this.mask ? 4 : 0));
236237

237238
// write the frame header
238239
output[0] = firstByte;

‎lib/utils.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ exports.eventEmitterListenerCount =
1010
require('events').EventEmitter.listenerCount ||
1111
function(emitter, type) { return emitter.listeners(type).length; };
1212

13+
exports.bufferAllocUnsafe = Buffer.allocUnsafe ?
14+
Buffer.allocUnsafe :
15+
function oldBufferAllocUnsafe(size) { return new Buffer(size); };
1316

14-
15-
17+
exports.bufferFromString = Buffer.from ?
18+
Buffer.from :
19+
function oldBufferFromString(string, encoding) {
20+
return new Buffer(string, encoding);
21+
};
1622

1723
exports.BufferingLogger = function createBufferingLogger(identifier, uniqueID) {
1824
var logFunction = require('debug')(identifier);

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"dependencies": {
3030
"debug": "^2.2.0",
3131
"nan": "^2.3.3",
32-
"typedarray-to-buffer": "^3.1.2",
32+
"typedarray-to-buffer": "^3.1.5",
3333
"yaeti": "^0.0.6"
3434
},
3535
"devDependencies": {
@@ -39,7 +39,7 @@
3939
"gulp-jshint": "^2.0.4",
4040
"jshint-stylish": "^2.2.1",
4141
"jshint": "^2.0.0",
42-
"tape": "^4.0.1"
42+
"tape": "^4.9.1"
4343
},
4444
"config": {
4545
"verbose": false

‎test/autobahn/fuzzingclient.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"servers": [
77
{
8-
"agent": "WebSocket-Node 1.0.25",
8+
"agent": "WebSocket-Node 1.0.27",
99
"url": "ws://127.0.0.1:8080",
1010
"options": {"version": 18}
1111
}

‎test/scripts/fragmentation-test-server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
var WebSocketServer = require('../../lib/WebSocketServer');
2020
var WebSocketRouter = require('../../lib/WebSocketRouter');
21+
var bufferAllocUnsafe = require('../../lib/utils').bufferAllocUnsafe;
2122
var http = require('http');
2223
var fs = require('fs');
2324

@@ -121,7 +122,7 @@ router.mount('*', 'fragmentation-test', function(request) {
121122
requestedLength = parseInt(match[1], 10);
122123

123124
// Generate random binary data.
124-
var buffer = new Buffer(requestedLength);
125+
var buffer = bufferAllocUnsafe(requestedLength);
125126
for (var i=0; i < requestedLength; i++) {
126127
buffer[i] = Math.ceil(Math.random()*255);
127128
}

‎test/unit/websocketFrame.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
var test = require('tape');
44
var bufferEqual = require('buffer-equal');
55
var WebSocketFrame = require('../../lib/WebSocketFrame');
6+
var utils = require('../../lib/utils');
7+
var bufferAllocUnsafe = utils.bufferAllocUnsafe;
8+
var bufferFromString = utils.bufferFromString;
9+
610

711
test('Serializing a WebSocket Frame with no data', function(t) {
812
t.plan(2);
913

1014
// WebSocketFrame uses a per-connection buffer for the mask bytes
1115
// and the frame header to avoid allocating tons of small chunks of RAM.
12-
var maskBytesBuffer = new Buffer(4);
13-
var frameHeaderBuffer = new Buffer(10);
16+
var maskBytesBuffer = bufferAllocUnsafe(4);
17+
var frameHeaderBuffer = bufferAllocUnsafe(10);
1418

1519
var frameBytes;
1620
var frame = new WebSocketFrame(maskBytesBuffer, frameHeaderBuffer, {});
@@ -24,9 +28,9 @@ test('Serializing a WebSocket Frame with no data', function(t) {
2428

2529
t.assert(
2630
bufferEqual
27-
(frameBytes, new Buffer('898000000000', 'hex')),
31+
(frameBytes, bufferFromString('898000000000', 'hex')),
2832
'Generated bytes should be correct'
2933
);
3034

3135
t.end();
32-
});
36+
});

‎vendor/FastBufferList.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Treat a linked list of buffers as a single variable-size buffer.
77
var Buffer = require('buffer').Buffer;
88
var EventEmitter = require('events').EventEmitter;
9+
var bufferAllocUnsafe = require('../lib/utils').bufferAllocUnsafe;
910

1011
module.exports = BufferList;
1112
module.exports.BufferList = BufferList; // backwards compatibility
@@ -21,9 +22,6 @@ function BufferList(opts) {
2122
// makes take() return a Buffer instead.
2223
self.encoding = opts.encoding;
2324

24-
// constructor to use for Buffer-esque operations
25-
self.construct = opts.construct || Buffer;
26-
2725
var head = { next : null, buffer : null };
2826
var last = { next : null, buffer : null };
2927

@@ -68,7 +66,7 @@ function BufferList(opts) {
6866
// If fn's result is a true value, cut out early.
6967
// Returns this (self).
7068
self.forEach = function (fn) {
71-
if (!head.buffer) return new self.construct(0);
69+
if (!head.buffer) return bufferAllocUnsafe(0);
7270

7371
if (head.buffer.length - offset <= 0) return self;
7472
var firstBuf = head.buffer.slice(offset);
@@ -87,11 +85,11 @@ function BufferList(opts) {
8785
// Create a single Buffer out of all the chunks or some subset specified by
8886
// start and one-past the end (like slice) in bytes.
8987
self.join = function (start, end) {
90-
if (!head.buffer) return new self.construct(0);
88+
if (!head.buffer) return bufferAllocUnsafe(0);
9189
if (start == undefined) start = 0;
9290
if (end == undefined) end = self.length;
9391

94-
var big = new self.construct(end - start);
92+
var big = bufferAllocUnsafe(end - start);
9593
var ix = 0;
9694
self.forEach(function (buffer) {
9795
if (start < (ix + buffer.length) && ix < end) {
@@ -111,7 +109,7 @@ function BufferList(opts) {
111109
};
112110

113111
self.joinInto = function (targetBuffer, targetStart, sourceStart, sourceEnd) {
114-
if (!head.buffer) return new self.construct(0);
112+
if (!head.buffer) return new bufferAllocUnsafe(0);
115113
if (sourceStart == undefined) sourceStart = 0;
116114
if (sourceEnd == undefined) sourceEnd = self.length;
117115

0 commit comments

Comments
 (0)
Please sign in to comment.