Skip to content

Commit

Permalink
feat: throw upon reserved event names
Browse files Browse the repository at this point in the history
These events cannot be used by the end users, because they are part of
the Socket.IO public API, so using them will now throw an error
explicitly.

Related: socketio/socket.io@f7ed81e
  • Loading branch information
darrachequesne committed Oct 12, 2020
1 parent 132f8ec commit 6494f61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
29 changes: 11 additions & 18 deletions build/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,19 @@ const component_bind_1 = __importDefault(require("component-bind"));
const has_binary2_1 = __importDefault(require("has-binary2"));
const debug = require("debug")("socket.io-client:socket");
/**
* Internal events (blacklisted).
* Internal events.
* These events can't be emitted by the user.
*
* @api private
*/
const events = {
const RESERVED_EVENTS = {
connect: 1,
connect_error: 1,
connect_timeout: 1,
connecting: 1,
disconnect: 1,
disconnecting: 1,
error: 1,
reconnect: 1,
reconnect_attempt: 1,
reconnect_failed: 1,
reconnect_error: 1,
reconnecting: 1,
ping: 1,
pong: 1,
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
newListener: 1,
removeListener: 1,
};
class Socket extends component_emitter_1.default {
/**
Expand Down Expand Up @@ -88,7 +82,7 @@ class Socket extends component_emitter_1.default {
this.io.open(); // ensure open
if ("open" === this.io.readyState)
this.onopen();
this.emit("connecting");
super.emit("connecting");
return this;
}
connect() {
Expand All @@ -99,7 +93,7 @@ class Socket extends component_emitter_1.default {
this.io.open(); // ensure open
if ("open" === this.io.readyState)
this.onopen();
this.emit("connecting");
super.emit("connecting");
return this;
}
/**
Expand All @@ -123,9 +117,8 @@ class Socket extends component_emitter_1.default {
* @api public
*/
emit(ev) {
if (events.hasOwnProperty(ev)) {
super.emit.apply(this, arguments);
return this;
if (RESERVED_EVENTS.hasOwnProperty(ev)) {
throw new Error('"' + ev + '" is a reserved event name');
}
const args = to_array_1.default(arguments);
const packet = {
Expand Down Expand Up @@ -294,7 +287,7 @@ class Socket extends component_emitter_1.default {
this.id = id;
this.connected = true;
this.disconnected = false;
this.emit("connect");
super.emit("connect");
this.emitBuffered();
}
/**
Expand Down
29 changes: 11 additions & 18 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ import { Manager } from "./manager";
const debug = require("debug")("socket.io-client:socket");

/**
* Internal events (blacklisted).
* Internal events.
* These events can't be emitted by the user.
*
* @api private
*/

const events = {
const RESERVED_EVENTS = {
connect: 1,
connect_error: 1,
connect_timeout: 1,
connecting: 1,
disconnect: 1,
disconnecting: 1,
error: 1,
reconnect: 1,
reconnect_attempt: 1,
reconnect_failed: 1,
reconnect_error: 1,
reconnecting: 1,
ping: 1,
pong: 1,
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
newListener: 1,
removeListener: 1,
};

export class Socket extends Emitter {
Expand Down Expand Up @@ -97,7 +91,7 @@ export class Socket extends Emitter {
this.subEvents();
if (!this.io.reconnecting) this.io.open(); // ensure open
if ("open" === this.io.readyState) this.onopen();
this.emit("connecting");
super.emit("connecting");
return this;
}

Expand All @@ -107,7 +101,7 @@ export class Socket extends Emitter {
this.subEvents();
if (!this.io.reconnecting) this.io.open(); // ensure open
if ("open" === this.io.readyState) this.onopen();
this.emit("connecting");
super.emit("connecting");
return this;
}

Expand All @@ -133,9 +127,8 @@ export class Socket extends Emitter {
* @api public
*/
emit(ev) {
if (events.hasOwnProperty(ev)) {
super.emit.apply(this, arguments);
return this;
if (RESERVED_EVENTS.hasOwnProperty(ev)) {
throw new Error('"' + ev + '" is a reserved event name');
}

const args = toArray(arguments);
Expand Down Expand Up @@ -324,7 +317,7 @@ export class Socket extends Emitter {
this.id = id;
this.connected = true;
this.disconnected = false;
this.emit("connect");
super.emit("connect");
this.emitBuffered();
}

Expand Down
8 changes: 8 additions & 0 deletions test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,12 @@ describe("socket", function () {
done();
});
});

it("should throw on reserved event", () => {
const socket = io("/no", { forceNew: true });

expect(() => socket.emit("disconnecting", "goodbye")).to.throwException(
/"disconnecting" is a reserved event name/
);
});
});

0 comments on commit 6494f61

Please sign in to comment.