From 5daf2f9ce3c11ba5f09d7743a52ee51570b791c7 Mon Sep 17 00:00:00 2001 From: Brian McKelvey Date: Wed, 19 Sep 2018 17:17:35 -0700 Subject: [PATCH] Fixing buffer deprecation warning. --- lib/WebSocketClient.js | 3 ++- lib/WebSocketConnection.js | 14 ++++++++------ lib/WebSocketFrame.js | 9 +++++---- lib/utils.js | 10 ++++++++-- package.json | 4 ++-- test/autobahn/fuzzingclient.json | 2 +- test/scripts/fragmentation-test-server.js | 3 ++- test/unit/websocketFrame.js | 12 ++++++++---- vendor/FastBufferList.js | 12 +++++------- 9 files changed, 41 insertions(+), 28 deletions(-) diff --git a/lib/WebSocketClient.js b/lib/WebSocketClient.js index e282e968..fb7572b3 100644 --- a/lib/WebSocketClient.js +++ b/lib/WebSocketClient.js @@ -23,6 +23,7 @@ var https = require('https'); var url = require('url'); var crypto = require('crypto'); var WebSocketConnection = require('./WebSocketConnection'); +var bufferAllocUnsafe = utils.bufferAllocUnsafe; var protocolSeparators = [ '(', ')', '<', '>', '@', @@ -166,7 +167,7 @@ WebSocketClient.prototype.connect = function(requestUrl, protocols, origin, head this.url.port = defaultPorts[this.url.protocol]; } - var nonce = new Buffer(16); + var nonce = bufferAllocUnsafe(16); for (var i=0; i < 16; i++) { nonce[i] = Math.round(Math.random()*0xFF); } diff --git a/lib/WebSocketConnection.js b/lib/WebSocketConnection.js index 2c19aab9..9f2750cc 100644 --- a/lib/WebSocketConnection.js +++ b/lib/WebSocketConnection.js @@ -20,6 +20,8 @@ var EventEmitter = require('events').EventEmitter; var WebSocketFrame = require('./WebSocketFrame'); var BufferList = require('../vendor/FastBufferList'); var Validation = require('./Validation').Validation; +var bufferAllocUnsafe = utils.bufferAllocUnsafe; +var bufferFromString = utils.bufferFromString; // Connected, fully-open, ready to send and receive frames const STATE_OPEN = 'open'; @@ -73,8 +75,8 @@ function WebSocketConnection(socket, extensions, protocol, maskOutgoingPackets, // We re-use the same buffers for the mask and frame header for all frames // received on each connection to avoid a small memory allocation for each // frame. - this.maskBytes = new Buffer(4); - this.frameHeader = new Buffer(10); + this.maskBytes = bufferAllocUnsafe(4); + this.frameHeader = bufferAllocUnsafe(10); // the BufferList will handle the data streaming in this.bufferList = new BufferList(); @@ -585,7 +587,7 @@ WebSocketConnection.prototype.processFrame = function(frame) { // message now. We also have to decode the utf-8 data // for text frames after combining all the fragments. var bytesCopied = 0; - var binaryPayload = new Buffer(this.fragmentationSize); + var binaryPayload = bufferAllocUnsafe(this.fragmentationSize); var opcode = this.frameQueue[0].opcode; this.frameQueue.forEach(function (currentFrame) { currentFrame.binaryPayload.copy(binaryPayload, bytesCopied); @@ -725,7 +727,7 @@ WebSocketConnection.prototype.send = function(data, cb) { }; WebSocketConnection.prototype.sendUTF = function(data, cb) { - data = new Buffer(data.toString(), 'utf8'); + data = bufferFromString(data.toString(), 'utf8'); this._debug('sendUTF: %d bytes', data.length); var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config); frame.opcode = 0x01; // WebSocketOpcode.TEXT_FRAME @@ -751,7 +753,7 @@ WebSocketConnection.prototype.ping = function(data) { frame.fin = true; if (data) { if (!Buffer.isBuffer(data)) { - data = new Buffer(data.toString(), 'utf8'); + data = bufferFromString(data.toString(), 'utf8'); } if (data.length > 125) { this._debug('WebSocket: Data for ping is longer than 125 bytes. Truncating.'); @@ -844,7 +846,7 @@ WebSocketConnection.prototype.sendCloseFrame = function(reasonCode, description, frame.opcode = 0x08; // WebSocketOpcode.CONNECTION_CLOSE frame.closeStatus = reasonCode; if (typeof(description) === 'string') { - frame.binaryPayload = new Buffer(description, 'utf8'); + frame.binaryPayload = bufferFromString(description, 'utf8'); } this.sendFrame(frame, cb); diff --git a/lib/WebSocketFrame.js b/lib/WebSocketFrame.js index dcec9ca3..e4a3fb1e 100644 --- a/lib/WebSocketFrame.js +++ b/lib/WebSocketFrame.js @@ -15,6 +15,7 @@ ***********************************************************************/ var bufferUtil = require('./BufferUtil').BufferUtil; +var bufferAllocUnsafe = require('./utils').bufferAllocUnsafe; const DECODE_HEADER = 1; const WAITING_FOR_16_BIT_LENGTH = 2; @@ -130,7 +131,7 @@ WebSocketFrame.prototype.addData = function(bufferList) { } if (this.length === 0) { - this.binaryPayload = new Buffer(0); + this.binaryPayload = bufferAllocUnsafe(0); this.parseState = COMPLETE; return true; } @@ -145,7 +146,7 @@ WebSocketFrame.prototype.addData = function(bufferList) { if (this.opcode === 0x08) { // WebSocketOpcode.CONNECTION_CLOSE if (this.length === 1) { // Invalid length for a close frame. Must be zero or at least two. - this.binaryPayload = new Buffer(0); + this.binaryPayload = bufferAllocUnsafe(0); this.invalidCloseFrameLength = true; } if (this.length >= 2) { @@ -203,7 +204,7 @@ WebSocketFrame.prototype.toBuffer = function(nullMask) { if (this.binaryPayload) { this.length += this.binaryPayload.length; } - data = new Buffer(this.length); + data = bufferAllocUnsafe(this.length); data.writeUInt16BE(this.closeStatus, 0); if (this.length > 2) { this.binaryPayload.copy(data, 2); @@ -232,7 +233,7 @@ WebSocketFrame.prototype.toBuffer = function(nullMask) { headerLength += 8; } - var output = new Buffer(this.length + headerLength + (this.mask ? 4 : 0)); + var output = bufferAllocUnsafe(this.length + headerLength + (this.mask ? 4 : 0)); // write the frame header output[0] = firstByte; diff --git a/lib/utils.js b/lib/utils.js index 6506dc97..02f1c396 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -10,9 +10,15 @@ exports.eventEmitterListenerCount = require('events').EventEmitter.listenerCount || function(emitter, type) { return emitter.listeners(type).length; }; +exports.bufferAllocUnsafe = Buffer.allocUnsafe ? + Buffer.allocUnsafe : + function oldBufferAllocUnsafe(size) { return new Buffer(size); }; - - +exports.bufferFromString = Buffer.from ? + Buffer.from : + function oldBufferFromString(string, encoding) { + return new Buffer(string, encoding); + }; exports.BufferingLogger = function createBufferingLogger(identifier, uniqueID) { var logFunction = require('debug')(identifier); diff --git a/package.json b/package.json index 517731b9..9c8730f5 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "dependencies": { "debug": "^2.2.0", "nan": "^2.3.3", - "typedarray-to-buffer": "^3.1.2", + "typedarray-to-buffer": "^3.1.5", "yaeti": "^0.0.6" }, "devDependencies": { @@ -39,7 +39,7 @@ "gulp-jshint": "^2.0.4", "jshint-stylish": "^2.2.1", "jshint": "^2.0.0", - "tape": "^4.0.1" + "tape": "^4.9.1" }, "config": { "verbose": false diff --git a/test/autobahn/fuzzingclient.json b/test/autobahn/fuzzingclient.json index a750ce34..dc2b89d7 100644 --- a/test/autobahn/fuzzingclient.json +++ b/test/autobahn/fuzzingclient.json @@ -5,7 +5,7 @@ "servers": [ { - "agent": "WebSocket-Node 1.0.25", + "agent": "WebSocket-Node 1.0.27", "url": "ws://127.0.0.1:8080", "options": {"version": 18} } diff --git a/test/scripts/fragmentation-test-server.js b/test/scripts/fragmentation-test-server.js index b1d71e59..27762226 100755 --- a/test/scripts/fragmentation-test-server.js +++ b/test/scripts/fragmentation-test-server.js @@ -18,6 +18,7 @@ var WebSocketServer = require('../../lib/WebSocketServer'); var WebSocketRouter = require('../../lib/WebSocketRouter'); +var bufferAllocUnsafe = require('../../lib/utils').bufferAllocUnsafe; var http = require('http'); var fs = require('fs'); @@ -121,7 +122,7 @@ router.mount('*', 'fragmentation-test', function(request) { requestedLength = parseInt(match[1], 10); // Generate random binary data. - var buffer = new Buffer(requestedLength); + var buffer = bufferAllocUnsafe(requestedLength); for (var i=0; i < requestedLength; i++) { buffer[i] = Math.ceil(Math.random()*255); } diff --git a/test/unit/websocketFrame.js b/test/unit/websocketFrame.js index 034612e3..fbf3b16d 100644 --- a/test/unit/websocketFrame.js +++ b/test/unit/websocketFrame.js @@ -3,14 +3,18 @@ var test = require('tape'); var bufferEqual = require('buffer-equal'); var WebSocketFrame = require('../../lib/WebSocketFrame'); +var utils = require('../../lib/utils'); +var bufferAllocUnsafe = utils.bufferAllocUnsafe; +var bufferFromString = utils.bufferFromString; + test('Serializing a WebSocket Frame with no data', function(t) { t.plan(2); // WebSocketFrame uses a per-connection buffer for the mask bytes // and the frame header to avoid allocating tons of small chunks of RAM. - var maskBytesBuffer = new Buffer(4); - var frameHeaderBuffer = new Buffer(10); + var maskBytesBuffer = bufferAllocUnsafe(4); + var frameHeaderBuffer = bufferAllocUnsafe(10); var frameBytes; var frame = new WebSocketFrame(maskBytesBuffer, frameHeaderBuffer, {}); @@ -24,9 +28,9 @@ test('Serializing a WebSocket Frame with no data', function(t) { t.assert( bufferEqual - (frameBytes, new Buffer('898000000000', 'hex')), + (frameBytes, bufferFromString('898000000000', 'hex')), 'Generated bytes should be correct' ); t.end(); -}); \ No newline at end of file +}); diff --git a/vendor/FastBufferList.js b/vendor/FastBufferList.js index aabf096a..c2d965ef 100644 --- a/vendor/FastBufferList.js +++ b/vendor/FastBufferList.js @@ -6,6 +6,7 @@ // Treat a linked list of buffers as a single variable-size buffer. var Buffer = require('buffer').Buffer; var EventEmitter = require('events').EventEmitter; +var bufferAllocUnsafe = require('../lib/utils').bufferAllocUnsafe; module.exports = BufferList; module.exports.BufferList = BufferList; // backwards compatibility @@ -21,9 +22,6 @@ function BufferList(opts) { // makes take() return a Buffer instead. self.encoding = opts.encoding; - // constructor to use for Buffer-esque operations - self.construct = opts.construct || Buffer; - var head = { next : null, buffer : null }; var last = { next : null, buffer : null }; @@ -68,7 +66,7 @@ function BufferList(opts) { // If fn's result is a true value, cut out early. // Returns this (self). self.forEach = function (fn) { - if (!head.buffer) return new self.construct(0); + if (!head.buffer) return bufferAllocUnsafe(0); if (head.buffer.length - offset <= 0) return self; var firstBuf = head.buffer.slice(offset); @@ -87,11 +85,11 @@ function BufferList(opts) { // Create a single Buffer out of all the chunks or some subset specified by // start and one-past the end (like slice) in bytes. self.join = function (start, end) { - if (!head.buffer) return new self.construct(0); + if (!head.buffer) return bufferAllocUnsafe(0); if (start == undefined) start = 0; if (end == undefined) end = self.length; - var big = new self.construct(end - start); + var big = bufferAllocUnsafe(end - start); var ix = 0; self.forEach(function (buffer) { if (start < (ix + buffer.length) && ix < end) { @@ -111,7 +109,7 @@ function BufferList(opts) { }; self.joinInto = function (targetBuffer, targetStart, sourceStart, sourceEnd) { - if (!head.buffer) return new self.construct(0); + if (!head.buffer) return new bufferAllocUnsafe(0); if (sourceStart == undefined) sourceStart = 0; if (sourceEnd == undefined) sourceEnd = self.length;