Comparing version 8.2.3 to 8.3.0
@@ -78,4 +78,4 @@ /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */ | ||
} else if (payloadLength === 127) { | ||
target.writeUInt32BE(0, 2); | ||
target.writeUInt32BE(data.length, 6); | ||
target[2] = target[3] = 0; | ||
target.writeUIntBE(data.length, 4, 6); | ||
} | ||
@@ -82,0 +82,0 @@ |
@@ -50,19 +50,4 @@ 'use strict'; | ||
function createWebSocketStream(ws, options) { | ||
let resumeOnReceiverDrain = true; | ||
let terminateOnDestroy = true; | ||
function receiverOnDrain() { | ||
if (resumeOnReceiverDrain) ws._socket.resume(); | ||
} | ||
if (ws.readyState === ws.CONNECTING) { | ||
ws.once('open', function open() { | ||
ws._receiver.removeAllListeners('drain'); | ||
ws._receiver.on('drain', receiverOnDrain); | ||
}); | ||
} else { | ||
ws._receiver.removeAllListeners('drain'); | ||
ws._receiver.on('drain', receiverOnDrain); | ||
} | ||
const duplex = new Duplex({ | ||
@@ -80,6 +65,3 @@ ...options, | ||
if (!duplex.push(data)) { | ||
resumeOnReceiverDrain = false; | ||
ws._socket.pause(); | ||
} | ||
if (!duplex.push(data)) ws.pause(); | ||
}); | ||
@@ -160,6 +142,3 @@ | ||
duplex._read = function () { | ||
if (ws.readyState === ws.OPEN && !resumeOnReceiverDrain) { | ||
resumeOnReceiverDrain = true; | ||
if (!ws._receiver._writableState.needDrain) ws._socket.resume(); | ||
} | ||
if (ws.isPaused) ws.resume(); | ||
}; | ||
@@ -166,0 +145,0 @@ |
@@ -61,2 +61,3 @@ /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Readable$" }] */ | ||
this._extensions = {}; | ||
this._paused = false; | ||
this._protocol = ''; | ||
@@ -129,2 +130,9 @@ this._readyState = WebSocket.CONNECTING; | ||
/** | ||
* @type {Boolean} | ||
*/ | ||
get isPaused() { | ||
return this._paused; | ||
} | ||
/** | ||
* @type {Function} | ||
@@ -318,2 +326,19 @@ */ | ||
/** | ||
* Pause the socket. | ||
* | ||
* @public | ||
*/ | ||
pause() { | ||
if ( | ||
this.readyState === WebSocket.CONNECTING || | ||
this.readyState === WebSocket.CLOSED | ||
) { | ||
return; | ||
} | ||
this._paused = true; | ||
this._socket.pause(); | ||
} | ||
/** | ||
* Send a ping. | ||
@@ -383,2 +408,19 @@ * | ||
/** | ||
* Resume the socket. | ||
* | ||
* @public | ||
*/ | ||
resume() { | ||
if ( | ||
this.readyState === WebSocket.CONNECTING || | ||
this.readyState === WebSocket.CLOSED | ||
) { | ||
return; | ||
} | ||
this._paused = false; | ||
if (!this._receiver._writableState.needDrain) this._socket.resume(); | ||
} | ||
/** | ||
* Send a data message. | ||
@@ -525,2 +567,3 @@ * | ||
'extensions', | ||
'isPaused', | ||
'protocol', | ||
@@ -638,15 +681,22 @@ 'readyState', | ||
const isUnixSocket = parsedUrl.protocol === 'ws+unix:'; | ||
let invalidURLMessage; | ||
if (parsedUrl.protocol !== 'ws:' && !isSecure && !isUnixSocket) { | ||
throw new SyntaxError( | ||
'The URL\'s protocol must be one of "ws:", "wss:", or "ws+unix:"' | ||
); | ||
invalidURLMessage = | ||
'The URL\'s protocol must be one of "ws:", "wss:", or "ws+unix:"'; | ||
} else if (isUnixSocket && !parsedUrl.pathname) { | ||
invalidURLMessage = "The URL's pathname is empty"; | ||
} else if (parsedUrl.hash) { | ||
invalidURLMessage = 'The URL contains a fragment identifier'; | ||
} | ||
if (isUnixSocket && !parsedUrl.pathname) { | ||
throw new SyntaxError("The URL's pathname is empty"); | ||
} | ||
if (invalidURLMessage) { | ||
const err = new SyntaxError(invalidURLMessage); | ||
if (parsedUrl.hash) { | ||
throw new SyntaxError('The URL contains a fragment identifier'); | ||
if (websocket._redirects === 0) { | ||
throw err; | ||
} else { | ||
emitErrorAndClose(websocket, err); | ||
return; | ||
} | ||
} | ||
@@ -733,5 +783,3 @@ | ||
req = websocket._req = null; | ||
websocket._readyState = WebSocket.CLOSING; | ||
websocket.emit('error', err); | ||
websocket.emitClose(); | ||
emitErrorAndClose(websocket, err); | ||
}); | ||
@@ -756,4 +804,12 @@ | ||
const addr = new URL(location, address); | ||
let addr; | ||
try { | ||
addr = new URL(location, address); | ||
} catch (e) { | ||
const err = new SyntaxError(`Invalid URL: ${location}`); | ||
emitErrorAndClose(websocket, err); | ||
return; | ||
} | ||
initAsClient(websocket, addr, protocols, options); | ||
@@ -861,2 +917,15 @@ } else if (!websocket.emit('unexpected-response', req, res)) { | ||
/** | ||
* Emit the `'error'` and `'close'` event. | ||
* | ||
* @param {WebSocket} websocket The WebSocket instance | ||
* @param {Error} The error to emit | ||
* @private | ||
*/ | ||
function emitErrorAndClose(websocket, err) { | ||
websocket._readyState = WebSocket.CLOSING; | ||
websocket.emit('error', err); | ||
websocket.emitClose(); | ||
} | ||
/** | ||
* Create a `net.Socket` and initiate a connection. | ||
@@ -987,3 +1056,5 @@ * | ||
function receiverOnDrain() { | ||
this[kWebSocket]._socket.resume(); | ||
const websocket = this[kWebSocket]; | ||
if (!websocket.isPaused) websocket._socket.resume(); | ||
} | ||
@@ -990,0 +1061,0 @@ |
{ | ||
"name": "ws", | ||
"version": "8.2.3", | ||
"version": "8.3.0", | ||
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js", | ||
@@ -53,3 +53,3 @@ "keywords": [ | ||
"bufferutil": "^4.0.1", | ||
"eslint": "^7.2.0", | ||
"eslint": "^8.0.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
@@ -56,0 +56,0 @@ "eslint-plugin-prettier": "^4.0.0", |
@@ -151,4 +151,4 @@ # ws: a Node.js WebSocket library | ||
ws.on('message', function incoming(message) { | ||
console.log('received: %s', message); | ||
ws.on('message', function message(data) { | ||
console.log('received: %s', data); | ||
}); | ||
@@ -183,4 +183,4 @@ ``` | ||
wss.on('connection', function connection(ws) { | ||
ws.on('message', function incoming(message) { | ||
console.log('received: %s', message); | ||
ws.on('message', function message(data) { | ||
console.log('received: %s', data); | ||
}); | ||
@@ -206,4 +206,4 @@ | ||
wss.on('connection', function connection(ws) { | ||
ws.on('message', function incoming(message) { | ||
console.log('received: %s', message); | ||
ws.on('message', function message(data) { | ||
console.log('received: %s', data); | ||
}); | ||
@@ -265,4 +265,4 @@ | ||
wss.on('connection', function connection(ws, request, client) { | ||
ws.on('message', function message(msg) { | ||
console.log(`Received message ${msg} from user ${client}`); | ||
ws.on('message', function message(data) { | ||
console.log(`Received message ${data} from user ${client}`); | ||
}); | ||
@@ -273,3 +273,3 @@ }); | ||
// This function is not defined on purpose. Implement it with your own logic. | ||
authenticate(request, (err, client) => { | ||
authenticate(request, function next(err, client) { | ||
if (err || !client) { | ||
@@ -303,3 +303,3 @@ socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); | ||
wss.on('connection', function connection(ws) { | ||
ws.on('message', function incoming(data, isBinary) { | ||
ws.on('message', function message(data, isBinary) { | ||
wss.clients.forEach(function each(client) { | ||
@@ -323,3 +323,3 @@ if (client.readyState === WebSocket.OPEN) { | ||
wss.on('connection', function connection(ws) { | ||
ws.on('message', function incoming(data, isBinary) { | ||
ws.on('message', function message(data, isBinary) { | ||
wss.clients.forEach(function each(client) { | ||
@@ -352,3 +352,3 @@ if (client !== ws && client.readyState === WebSocket.OPEN) { | ||
ws.on('message', function incoming(data) { | ||
ws.on('message', function message(data) { | ||
console.log(`Roundtrip time: ${Date.now() - data} ms`); | ||
@@ -355,0 +355,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
127471
3732