Comparing version 2.2.0 to 2.2.1
@@ -9,24 +9,29 @@ 'use strict'; | ||
/** | ||
* Merges an array of buffers into a new buffer. | ||
* | ||
* @param {Buffer[]} list The array of buffers to concat | ||
* @param {Number} totalLength The total length of buffers in the list | ||
* @return {Buffer} The resulting buffer | ||
* @public | ||
*/ | ||
const concat = (list, totalLength) => { | ||
const target = Buffer.allocUnsafe(totalLength); | ||
var offset = 0; | ||
for (var i = 0; i < list.length; i++) { | ||
const buf = list[i]; | ||
buf.copy(target, offset); | ||
offset += buf.length; | ||
} | ||
return target; | ||
}; | ||
try { | ||
const bufferUtil = require('bufferutil'); | ||
module.exports = bufferUtil.BufferUtil || bufferUtil; | ||
module.exports = Object.assign({ concat }, bufferUtil.BufferUtil || bufferUtil); | ||
} catch (e) { | ||
/** | ||
* Merges an array of buffers into a target buffer. | ||
* | ||
* @param {Buffer} target The target buffer | ||
* @param {Buffer[]} buffers The array of buffers to merge | ||
* @public | ||
*/ | ||
const merge = (target, buffers) => { | ||
var offset = 0; | ||
for (var i = 0; i < buffers.length; i++) { | ||
const buf = buffers[i]; | ||
buf.copy(target, offset); | ||
offset += buf.length; | ||
} | ||
}; | ||
/** | ||
* Masks a buffer using the given mask. | ||
@@ -62,3 +67,3 @@ * | ||
module.exports = { merge, mask, unmask }; | ||
module.exports = { concat, mask, unmask }; | ||
} |
@@ -5,2 +5,4 @@ 'use strict'; | ||
const bufferUtil = require('./BufferUtil'); | ||
const AVAILABLE_WINDOW_BITS = [8, 9, 10, 11, 12, 13, 14, 15]; | ||
@@ -13,3 +15,3 @@ const DEFAULT_WINDOW_BITS = 15; | ||
/** | ||
* Per-message Compression Extensions implementation | ||
* Per-message Deflate implementation. | ||
*/ | ||
@@ -27,8 +29,11 @@ class PerMessageDeflate { | ||
static get extensionName () { | ||
return 'permessage-deflate'; | ||
} | ||
/** | ||
* Create extension parameters offer | ||
* | ||
* @api public | ||
* @public | ||
*/ | ||
offer () { | ||
@@ -56,3 +61,3 @@ var params = {}; | ||
* | ||
* @api public | ||
* @public | ||
*/ | ||
@@ -76,3 +81,3 @@ accept (paramsList) { | ||
* | ||
* @api public | ||
* @public | ||
*/ | ||
@@ -101,5 +106,4 @@ cleanup () { | ||
* | ||
* @api private | ||
* @private | ||
*/ | ||
acceptAsServer (paramsList) { | ||
@@ -156,5 +160,4 @@ var accepted = {}; | ||
* | ||
* @api privaye | ||
* @private | ||
*/ | ||
acceptAsClient (paramsList) { | ||
@@ -182,5 +185,4 @@ var params = paramsList[0]; | ||
* | ||
* @api private | ||
* @private | ||
*/ | ||
normalizeParams (paramsList) { | ||
@@ -272,3 +274,3 @@ return paramsList.map((params) => { | ||
if ( | ||
fin && this.params[`${endpoint}_no_context_takeover`] || | ||
(fin && this.params[`${endpoint}_no_context_takeover`]) || | ||
this._inflate.pendingClose | ||
@@ -288,3 +290,3 @@ ) { | ||
if (err) callback(err); | ||
else callback(null, Buffer.concat(buffers, totalLength)); | ||
else callback(null, bufferUtil.concat(buffers, totalLength)); | ||
}); | ||
@@ -296,5 +298,4 @@ } | ||
* | ||
* @api public | ||
* @public | ||
*/ | ||
compress (data, fin, callback) { | ||
@@ -306,6 +307,6 @@ if (!data || data.length === 0) { | ||
var endpoint = this._isServer ? 'server' : 'client'; | ||
const endpoint = this._isServer ? 'server' : 'client'; | ||
if (!this._deflate) { | ||
var maxWindowBits = this.params[endpoint + '_max_window_bits']; | ||
const maxWindowBits = this.params[`${endpoint}_max_window_bits`]; | ||
this._deflate = zlib.createDeflateRaw({ | ||
@@ -319,5 +320,10 @@ flush: zlib.Z_SYNC_FLUSH, | ||
var totalLength = 0; | ||
const buffers = []; | ||
const onData = (data) => buffers.push(data); | ||
const onData = (data) => { | ||
totalLength += data.length; | ||
buffers.push(data); | ||
}; | ||
const onError = (err) => { | ||
@@ -327,8 +333,14 @@ cleanup(); | ||
}; | ||
const cleanup = () => { | ||
if (!this._deflate) return; | ||
this._deflate.removeListener('error', onError); | ||
this._deflate.removeListener('data', onData); | ||
this._deflate.writeInProgress = false; | ||
if ((fin && this.params[endpoint + '_no_context_takeover']) || this._deflate.pendingClose) { | ||
if ( | ||
(fin && this.params[`${endpoint}_no_context_takeover`]) || | ||
this._deflate.pendingClose | ||
) { | ||
this._deflate.close(); | ||
@@ -343,6 +355,4 @@ this._deflate = null; | ||
cleanup(); | ||
var data = Buffer.concat(buffers); | ||
if (fin) { | ||
data = data.slice(0, data.length - 4); | ||
} | ||
var data = bufferUtil.concat(buffers, totalLength); | ||
if (fin) data = data.slice(0, data.length - 4); | ||
callback(null, data); | ||
@@ -353,4 +363,2 @@ }); | ||
PerMessageDeflate.extensionName = 'permessage-deflate'; | ||
module.exports = PerMessageDeflate; |
@@ -275,3 +275,3 @@ /*! | ||
this.payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4, true); | ||
this.payloadLength = (num * Math.pow(2, 32)) + buf.readUInt32BE(4, true); | ||
this.haveLength(); | ||
@@ -536,3 +536,3 @@ } | ||
if (fragments.length === 1) return fragments[0]; | ||
if (fragments.length > 1) return Buffer.concat(fragments, messageLength); | ||
if (fragments.length > 1) return bufferUtil.concat(fragments, messageLength); | ||
return constants.EMPTY_BUFFER; | ||
@@ -539,0 +539,0 @@ } |
@@ -53,3 +53,3 @@ /*! | ||
static frame (data, options) { | ||
const merge = data.length < 1024 || options.mask && options.readOnly; | ||
const merge = data.length < 1024 || (options.mask && options.readOnly); | ||
var offset = options.mask ? 6 : 2; | ||
@@ -56,0 +56,0 @@ var payloadLength = data.length; |
@@ -23,4 +23,4 @@ /*! | ||
const protocolVersions = [8, 13]; | ||
const closeTimeout = 30 * 1000; // Allow 30 seconds to terminate the connection cleanly. | ||
const protocolVersion = 13; | ||
@@ -280,3 +280,3 @@ /** | ||
if (this.readyState === WebSocket.CLOSING) { | ||
if (this._closeCode) this.terminate(); | ||
if (this._closeCode && this._socket) this._socket.end(); | ||
return; | ||
@@ -289,5 +289,4 @@ } | ||
if (this._closeCode) { | ||
this.terminate(); | ||
} else { | ||
if (this._socket) { | ||
if (this._closeCode) this._socket.end(); | ||
// | ||
@@ -397,13 +396,3 @@ // Ensure that the connection is cleaned up even when the closing | ||
if (this._socket) { | ||
this.readyState = WebSocket.CLOSING; | ||
this._socket.end(); | ||
// | ||
// Add a timeout to ensure that the connection is completely cleaned up | ||
// within 30 seconds, even if the other peer does not send a FIN packet. | ||
// | ||
clearTimeout(this._closeTimer); | ||
this._closeTimer = setTimeout(this._finalize, closeTimeout, true); | ||
} | ||
this.finalize(true); | ||
} | ||
@@ -511,11 +500,11 @@ } | ||
options = Object.assign({ | ||
protocolVersion: protocolVersions[1], | ||
protocol: protocols.join(','), | ||
perMessageDeflate: true, | ||
localAddress: null, | ||
protocolVersion, | ||
headers: null, | ||
family: null, | ||
origin: null, | ||
agent: null, | ||
host: null, | ||
family: null, | ||
@@ -535,4 +524,7 @@ // | ||
if (options.protocolVersion !== 8 && options.protocolVersion !== 13) { | ||
throw new Error('unsupported protocol version'); | ||
if (protocolVersions.indexOf(options.protocolVersion) === -1) { | ||
throw new Error( | ||
`unsupported protocol version: ${options.protocolVersion} ` + | ||
`(supported versions: ${protocolVersions.join(', ')})` | ||
); | ||
} | ||
@@ -539,0 +531,0 @@ |
@@ -156,3 +156,3 @@ /*! | ||
req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket' || | ||
!req.headers['sec-websocket-key'] || version !== 8 && version !== 13 || | ||
!req.headers['sec-websocket-key'] || (version !== 8 && version !== 13) || | ||
!this.shouldHandle(req) | ||
@@ -159,0 +159,0 @@ ) { |
@@ -5,3 +5,3 @@ { | ||
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"license": "MIT", | ||
@@ -33,8 +33,9 @@ "main": "index.js", | ||
"benchmark": "~2.1.2", | ||
"bufferutil": "~2.0.0", | ||
"eslint": "~3.16.0", | ||
"eslint-config-semistandard": "~7.0.0", | ||
"eslint-config-standard": "~6.2.1", | ||
"eslint-plugin-promise": "~3.4.0", | ||
"eslint-plugin-standard": "~2.0.1", | ||
"bufferutil": "~3.0.0", | ||
"eslint": "~3.17.0", | ||
"eslint-config-standard": "~8.0.0-beta.1", | ||
"eslint-plugin-import": "~2.2.0", | ||
"eslint-plugin-node": "~4.2.0", | ||
"eslint-plugin-promise": "~3.5.0", | ||
"eslint-plugin-standard": "~2.1.0", | ||
"istanbul": "~0.4.5", | ||
@@ -41,0 +42,0 @@ "mocha": "~3.2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83045
2325
11