Comparing version 2.2.1 to 2.2.2
'use strict'; | ||
/** | ||
* Module exports. | ||
* Parse the `Sec-WebSocket-Extensions` header into an object. | ||
* | ||
* @param {String} value field value of the header | ||
* @return {Object} The parsed object | ||
* @public | ||
*/ | ||
exports.parse = parse; | ||
exports.format = format; | ||
/** | ||
* Parse extensions header value | ||
*/ | ||
function parse (value) { | ||
const parse = (value) => { | ||
value = value || ''; | ||
var extensions = {}; | ||
const extensions = {}; | ||
value.split(',').forEach(function (v) { | ||
var params = v.split(';'); | ||
var token = params.shift().trim(); | ||
var paramsList = extensions[token] = extensions[token] || []; | ||
var parsedParams = {}; | ||
value.split(',').forEach((v) => { | ||
const params = v.split(';'); | ||
const token = params.shift().trim(); | ||
const paramsList = extensions[token] = extensions[token] || []; | ||
const parsedParams = {}; | ||
params.forEach(function (param) { | ||
var parts = param.trim().split('='); | ||
var key = parts[0]; | ||
params.forEach((param) => { | ||
const parts = param.trim().split('='); | ||
const key = parts[0]; | ||
var value = parts[1]; | ||
if (value === undefined) { | ||
@@ -47,24 +44,25 @@ value = true; | ||
return extensions; | ||
} | ||
}; | ||
/** | ||
* Format extensions header value | ||
* Serialize a parsed `Sec-WebSocket-Extensions` header to a string. | ||
* | ||
* @param {Object} value The object to format | ||
* @return {String} A string representing the given value | ||
* @public | ||
*/ | ||
function format (value) { | ||
return Object.keys(value).map(function (token) { | ||
const format = (value) => { | ||
return Object.keys(value).map((token) => { | ||
var paramsList = value[token]; | ||
if (!Array.isArray(paramsList)) { | ||
paramsList = [paramsList]; | ||
} | ||
return paramsList.map(function (params) { | ||
return [token].concat(Object.keys(params).map(function (k) { | ||
if (!Array.isArray(paramsList)) paramsList = [paramsList]; | ||
return paramsList.map((params) => { | ||
return [token].concat(Object.keys(params).map((k) => { | ||
var p = params[k]; | ||
if (!Array.isArray(p)) p = [p]; | ||
return p.map(function (v) { | ||
return v === true ? k : k + '=' + v; | ||
}).join('; '); | ||
return p.map((v) => v === true ? k : `${k}=${v}`).join('; '); | ||
})).join('; '); | ||
}).join(', '); | ||
}).join(', '); | ||
} | ||
}; | ||
module.exports = { format, parse }; |
@@ -32,8 +32,10 @@ 'use strict'; | ||
/** | ||
* Create extension parameters offer | ||
* Create extension parameters offer. | ||
* | ||
* @return {Object} Extension parameters | ||
* @public | ||
*/ | ||
offer () { | ||
var params = {}; | ||
const params = {}; | ||
if (this._options.serverNoContextTakeover) { | ||
@@ -53,2 +55,3 @@ params.server_no_context_takeover = true; | ||
} | ||
return params; | ||
@@ -58,4 +61,6 @@ } | ||
/** | ||
* Accept extension offer | ||
* Accept extension offer. | ||
* | ||
* @param {Array} paramsList Extension parameters | ||
* @return {Object} Accepted configuration | ||
* @public | ||
@@ -78,3 +83,3 @@ */ | ||
/** | ||
* Releases all resources used by the extension | ||
* Releases all resources used by the extension. | ||
* | ||
@@ -103,26 +108,32 @@ * @public | ||
/** | ||
* Accept extension offer from client | ||
* Accept extension offer from client. | ||
* | ||
* @param {Array} paramsList Extension parameters | ||
* @return {Object} Accepted configuration | ||
* @private | ||
*/ | ||
acceptAsServer (paramsList) { | ||
var accepted = {}; | ||
var result = paramsList.some((params) => { | ||
accepted = {}; | ||
if (this._options.serverNoContextTakeover === false && params.server_no_context_takeover) { | ||
const accepted = {}; | ||
const result = paramsList.some((params) => { | ||
if (( | ||
this._options.serverNoContextTakeover === false && | ||
params.server_no_context_takeover | ||
) || ( | ||
this._options.serverMaxWindowBits === false && | ||
params.server_max_window_bits | ||
) || ( | ||
typeof this._options.serverMaxWindowBits === 'number' && | ||
typeof params.server_max_window_bits === 'number' && | ||
this._options.serverMaxWindowBits > params.server_max_window_bits | ||
) || ( | ||
typeof this._options.clientMaxWindowBits === 'number' && | ||
!params.client_max_window_bits | ||
)) { | ||
return; | ||
} | ||
if (this._options.serverMaxWindowBits === false && params.server_max_window_bits) { | ||
return; | ||
} | ||
if (typeof this._options.serverMaxWindowBits === 'number' && | ||
typeof params.server_max_window_bits === 'number' && | ||
this._options.serverMaxWindowBits > params.server_max_window_bits) { | ||
return; | ||
} | ||
if (typeof this._options.clientMaxWindowBits === 'number' && !params.client_max_window_bits) { | ||
return; | ||
} | ||
if (this._options.serverNoContextTakeover || params.server_no_context_takeover) { | ||
if ( | ||
this._options.serverNoContextTakeover || | ||
params.server_no_context_takeover | ||
) { | ||
accepted.server_no_context_takeover = true; | ||
@@ -133,3 +144,6 @@ } | ||
} | ||
if (this._options.clientNoContextTakeover !== false && params.client_no_context_takeover) { | ||
if ( | ||
this._options.clientNoContextTakeover !== false && | ||
params.client_no_context_takeover | ||
) { | ||
accepted.client_no_context_takeover = true; | ||
@@ -144,3 +158,6 @@ } | ||
accepted.client_max_window_bits = this._options.clientMaxWindowBits; | ||
} else if (this._options.clientMaxWindowBits !== false && typeof params.client_max_window_bits === 'number') { | ||
} else if ( | ||
this._options.clientMaxWindowBits !== false && | ||
typeof params.client_max_window_bits === 'number' | ||
) { | ||
accepted.client_max_window_bits = params.client_max_window_bits; | ||
@@ -151,5 +168,3 @@ } | ||
if (!result) { | ||
throw new Error(`Doesn't support the offered configuration`); | ||
} | ||
if (!result) throw new Error(`Doesn't support the offered configuration`); | ||
@@ -160,10 +175,16 @@ return accepted; | ||
/** | ||
* Accept extension response from server | ||
* Accept extension response from server. | ||
* | ||
* @param {Array} paramsList Extension parameters | ||
* @return {Object} Accepted configuration | ||
* @private | ||
*/ | ||
acceptAsClient (paramsList) { | ||
var params = paramsList[0]; | ||
const params = paramsList[0]; | ||
if (this._options.clientNoContextTakeover != null) { | ||
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) { | ||
if ( | ||
this._options.clientNoContextTakeover === false && | ||
params.client_no_context_takeover | ||
) { | ||
throw new Error('Invalid value for "client_no_context_takeover"'); | ||
@@ -173,10 +194,17 @@ } | ||
if (this._options.clientMaxWindowBits != null) { | ||
if (this._options.clientMaxWindowBits === false && params.client_max_window_bits) { | ||
if ( | ||
this._options.clientMaxWindowBits === false && | ||
params.client_max_window_bits | ||
) { | ||
throw new Error('Invalid value for "client_max_window_bits"'); | ||
} | ||
if (typeof this._options.clientMaxWindowBits === 'number' && | ||
(!params.client_max_window_bits || params.client_max_window_bits > this._options.clientMaxWindowBits)) { | ||
if ( | ||
typeof this._options.clientMaxWindowBits === 'number' && ( | ||
!params.client_max_window_bits || | ||
params.client_max_window_bits > this._options.clientMaxWindowBits | ||
)) { | ||
throw new Error('Invalid value for "client_max_window_bits"'); | ||
} | ||
} | ||
return params; | ||
@@ -186,4 +214,6 @@ } | ||
/** | ||
* Normalize extensions parameters | ||
* Normalize extensions parameters. | ||
* | ||
* @param {Array} paramsList Extension parameters | ||
* @return {Array} Normalized extensions parameters | ||
* @private | ||
@@ -196,3 +226,3 @@ */ | ||
if (value.length > 1) { | ||
throw new Error('Multiple extension parameters for ' + key); | ||
throw new Error(`Multiple extension parameters for ${key}`); | ||
} | ||
@@ -298,4 +328,7 @@ | ||
/** | ||
* Compress message | ||
* Compress data. | ||
* | ||
* @param {Buffer} data Data to compress | ||
* @param {Boolean} fin Specifies whether or not this is the last fragment | ||
* @param {Function} callback Callback | ||
* @public | ||
@@ -302,0 +335,0 @@ */ |
@@ -378,3 +378,3 @@ /*! | ||
/** | ||
* Half-close the socket sending a FIN packet. | ||
* Forcibly close the connection. | ||
* | ||
@@ -381,0 +381,0 @@ * @public |
@@ -5,3 +5,3 @@ { | ||
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"license": "MIT", | ||
@@ -34,3 +34,3 @@ "main": "index.js", | ||
"bufferutil": "~3.0.0", | ||
"eslint": "~3.17.0", | ||
"eslint": "~3.18.0", | ||
"eslint-config-standard": "~8.0.0-beta.1", | ||
@@ -37,0 +37,0 @@ "eslint-plugin-import": "~2.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
83932
2354