Skip to content

Commit

Permalink
[fix] Make read-only properties read-only
Browse files Browse the repository at this point in the history
Fixes #1814
  • Loading branch information
lpinca committed Nov 7, 2020
1 parent 7d39f19 commit eabed8f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/websocket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class WebSocketServer extends EventEmitter {

if (protocol) {
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
ws.protocol = protocol;
ws._protocol = protocol;
}
}

Expand Down
61 changes: 42 additions & 19 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ class WebSocket extends EventEmitter {
constructor(address, protocols, options) {
super();

this.readyState = WebSocket.CONNECTING;
this.protocol = '';

this._binaryType = BINARY_TYPES[0];
this._closeCode = 1006;
this._closeFrameReceived = false;
this._closeFrameSent = false;
this._closeMessage = '';
this._closeTimer = null;
this._closeCode = 1006;
this._extensions = {};
this._protocol = '';
this._readyState = WebSocket.CONNECTING;
this._receiver = null;
this._sender = null;
this._socket = null;
Expand Down Expand Up @@ -126,6 +125,27 @@ class WebSocket extends EventEmitter {
return Object.keys(this._extensions).join();
}

/**
* @type {String}
*/
get protocol() {
return this._protocol;
}

/**
* @type {Number}
*/
get readyState() {
return this._readyState;
}

/**
* @type {String}
*/
get url() {
return this._url;
}

/**
* Set up the socket and the internal resources.
*
Expand Down Expand Up @@ -166,7 +186,7 @@ class WebSocket extends EventEmitter {
socket.on('end', socketOnEnd);
socket.on('error', socketOnError);

this.readyState = WebSocket.OPEN;
this._readyState = WebSocket.OPEN;
this.emit('open');
}

Expand All @@ -177,7 +197,7 @@ class WebSocket extends EventEmitter {
*/
emitClose() {
if (!this._socket) {
this.readyState = WebSocket.CLOSED;
this._readyState = WebSocket.CLOSED;
this.emit('close', this._closeCode, this._closeMessage);
return;
}
Expand All @@ -187,7 +207,7 @@ class WebSocket extends EventEmitter {
}

this._receiver.removeAllListeners();
this.readyState = WebSocket.CLOSED;
this._readyState = WebSocket.CLOSED;
this.emit('close', this._closeCode, this._closeMessage);
}

Expand Down Expand Up @@ -222,7 +242,7 @@ class WebSocket extends EventEmitter {
return;
}

this.readyState = WebSocket.CLOSING;
this._readyState = WebSocket.CLOSING;
this._sender.close(code, data, !this._isServer, (err) => {
//
// This error is handled by the `'error'` listener on the socket. We only
Expand Down Expand Up @@ -367,14 +387,17 @@ class WebSocket extends EventEmitter {
}

if (this._socket) {
this.readyState = WebSocket.CLOSING;
this._readyState = WebSocket.CLOSING;
this._socket.destroy();
}
}
}

readyStates.forEach((readyState, i) => {
WebSocket[readyState] = i;
Object.defineProperty(WebSocket, readyState, {
enumerable: true,
value: i
});
});

//
Expand Down Expand Up @@ -474,10 +497,10 @@ function initAsClient(websocket, address, protocols, options) {

if (address instanceof URL) {
parsedUrl = address;
websocket.url = address.href;
websocket._url = address.href;
} else {
parsedUrl = new URL(address);
websocket.url = address;
websocket._url = address;
}

const isUnixSocket = parsedUrl.protocol === 'ws+unix:';
Expand Down Expand Up @@ -552,7 +575,7 @@ function initAsClient(websocket, address, protocols, options) {
if (websocket._req.aborted) return;

req = websocket._req = null;
websocket.readyState = WebSocket.CLOSING;
websocket._readyState = WebSocket.CLOSING;
websocket.emit('error', err);
websocket.emitClose();
});
Expand Down Expand Up @@ -623,7 +646,7 @@ function initAsClient(websocket, address, protocols, options) {
return;
}

if (serverProt) websocket.protocol = serverProt;
if (serverProt) websocket._protocol = serverProt;

if (perMessageDeflate) {
try {
Expand Down Expand Up @@ -688,7 +711,7 @@ function tlsConnect(options) {
* @private
*/
function abortHandshake(websocket, stream, message) {
websocket.readyState = WebSocket.CLOSING;
websocket._readyState = WebSocket.CLOSING;

const err = new Error(message);
Error.captureStackTrace(err, abortHandshake);
Expand Down Expand Up @@ -777,7 +800,7 @@ function receiverOnError(err) {

websocket._socket.removeListener('data', socketOnData);

websocket.readyState = WebSocket.CLOSING;
websocket._readyState = WebSocket.CLOSING;
websocket._closeCode = err[kStatusCode];
websocket.emit('error', err);
websocket._socket.destroy();
Expand Down Expand Up @@ -836,7 +859,7 @@ function socketOnClose() {
this.removeListener('close', socketOnClose);
this.removeListener('end', socketOnEnd);

websocket.readyState = WebSocket.CLOSING;
websocket._readyState = WebSocket.CLOSING;

//
// The close frame might not have been received or the `'end'` event emitted,
Expand Down Expand Up @@ -887,7 +910,7 @@ function socketOnData(chunk) {
function socketOnEnd() {
const websocket = this[kWebSocket];

websocket.readyState = WebSocket.CLOSING;
websocket._readyState = WebSocket.CLOSING;
websocket._receiver.end();
this.end();
}
Expand All @@ -904,7 +927,7 @@ function socketOnError() {
this.on('error', NOOP);

if (websocket) {
websocket.readyState = WebSocket.CLOSING;
websocket._readyState = WebSocket.CLOSING;
this.destroy();
}
}

0 comments on commit eabed8f

Please sign in to comment.