Skip to content

Commit eabed8f

Browse files
committed
[fix] Make read-only properties read-only
Fixes #1814
1 parent 7d39f19 commit eabed8f

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

lib/websocket-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ class WebSocketServer extends EventEmitter {
299299

300300
if (protocol) {
301301
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
302-
ws.protocol = protocol;
302+
ws._protocol = protocol;
303303
}
304304
}
305305

lib/websocket.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ class WebSocket extends EventEmitter {
4343
constructor(address, protocols, options) {
4444
super();
4545

46-
this.readyState = WebSocket.CONNECTING;
47-
this.protocol = '';
48-
4946
this._binaryType = BINARY_TYPES[0];
47+
this._closeCode = 1006;
5048
this._closeFrameReceived = false;
5149
this._closeFrameSent = false;
5250
this._closeMessage = '';
5351
this._closeTimer = null;
54-
this._closeCode = 1006;
5552
this._extensions = {};
53+
this._protocol = '';
54+
this._readyState = WebSocket.CONNECTING;
5655
this._receiver = null;
5756
this._sender = null;
5857
this._socket = null;
@@ -126,6 +125,27 @@ class WebSocket extends EventEmitter {
126125
return Object.keys(this._extensions).join();
127126
}
128127

128+
/**
129+
* @type {String}
130+
*/
131+
get protocol() {
132+
return this._protocol;
133+
}
134+
135+
/**
136+
* @type {Number}
137+
*/
138+
get readyState() {
139+
return this._readyState;
140+
}
141+
142+
/**
143+
* @type {String}
144+
*/
145+
get url() {
146+
return this._url;
147+
}
148+
129149
/**
130150
* Set up the socket and the internal resources.
131151
*
@@ -166,7 +186,7 @@ class WebSocket extends EventEmitter {
166186
socket.on('end', socketOnEnd);
167187
socket.on('error', socketOnError);
168188

169-
this.readyState = WebSocket.OPEN;
189+
this._readyState = WebSocket.OPEN;
170190
this.emit('open');
171191
}
172192

@@ -177,7 +197,7 @@ class WebSocket extends EventEmitter {
177197
*/
178198
emitClose() {
179199
if (!this._socket) {
180-
this.readyState = WebSocket.CLOSED;
200+
this._readyState = WebSocket.CLOSED;
181201
this.emit('close', this._closeCode, this._closeMessage);
182202
return;
183203
}
@@ -187,7 +207,7 @@ class WebSocket extends EventEmitter {
187207
}
188208

189209
this._receiver.removeAllListeners();
190-
this.readyState = WebSocket.CLOSED;
210+
this._readyState = WebSocket.CLOSED;
191211
this.emit('close', this._closeCode, this._closeMessage);
192212
}
193213

@@ -222,7 +242,7 @@ class WebSocket extends EventEmitter {
222242
return;
223243
}
224244

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

369389
if (this._socket) {
370-
this.readyState = WebSocket.CLOSING;
390+
this._readyState = WebSocket.CLOSING;
371391
this._socket.destroy();
372392
}
373393
}
374394
}
375395

376396
readyStates.forEach((readyState, i) => {
377-
WebSocket[readyState] = i;
397+
Object.defineProperty(WebSocket, readyState, {
398+
enumerable: true,
399+
value: i
400+
});
378401
});
379402

380403
//
@@ -474,10 +497,10 @@ function initAsClient(websocket, address, protocols, options) {
474497

475498
if (address instanceof URL) {
476499
parsedUrl = address;
477-
websocket.url = address.href;
500+
websocket._url = address.href;
478501
} else {
479502
parsedUrl = new URL(address);
480-
websocket.url = address;
503+
websocket._url = address;
481504
}
482505

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

554577
req = websocket._req = null;
555-
websocket.readyState = WebSocket.CLOSING;
578+
websocket._readyState = WebSocket.CLOSING;
556579
websocket.emit('error', err);
557580
websocket.emitClose();
558581
});
@@ -623,7 +646,7 @@ function initAsClient(websocket, address, protocols, options) {
623646
return;
624647
}
625648

626-
if (serverProt) websocket.protocol = serverProt;
649+
if (serverProt) websocket._protocol = serverProt;
627650

628651
if (perMessageDeflate) {
629652
try {
@@ -688,7 +711,7 @@ function tlsConnect(options) {
688711
* @private
689712
*/
690713
function abortHandshake(websocket, stream, message) {
691-
websocket.readyState = WebSocket.CLOSING;
714+
websocket._readyState = WebSocket.CLOSING;
692715

693716
const err = new Error(message);
694717
Error.captureStackTrace(err, abortHandshake);
@@ -777,7 +800,7 @@ function receiverOnError(err) {
777800

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

780-
websocket.readyState = WebSocket.CLOSING;
803+
websocket._readyState = WebSocket.CLOSING;
781804
websocket._closeCode = err[kStatusCode];
782805
websocket.emit('error', err);
783806
websocket._socket.destroy();
@@ -836,7 +859,7 @@ function socketOnClose() {
836859
this.removeListener('close', socketOnClose);
837860
this.removeListener('end', socketOnEnd);
838861

839-
websocket.readyState = WebSocket.CLOSING;
862+
websocket._readyState = WebSocket.CLOSING;
840863

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

890-
websocket.readyState = WebSocket.CLOSING;
913+
websocket._readyState = WebSocket.CLOSING;
891914
websocket._receiver.end();
892915
this.end();
893916
}
@@ -904,7 +927,7 @@ function socketOnError() {
904927
this.on('error', NOOP);
905928

906929
if (websocket) {
907-
websocket.readyState = WebSocket.CLOSING;
930+
websocket._readyState = WebSocket.CLOSING;
908931
this.destroy();
909932
}
910933
}

0 commit comments

Comments
 (0)