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.
  • Loading branch information
darrachequesne committed Oct 12, 2020
1 parent ca97bc9 commit f7ed81e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 49 deletions.
13 changes: 2 additions & 11 deletions dist/namespace.js
Expand Up @@ -10,14 +10,6 @@ const socket_io_parser_1 = require("socket.io-parser");
const has_binary2_1 = __importDefault(require("has-binary2"));
const debug_1 = __importDefault(require("debug"));
const debug = debug_1.default("socket.io:namespace");
/**
* Blacklisted events.
*/
const events = [
"connect",
"connection",
"newListener"
];
class Namespace extends events_1.EventEmitter {
/**
* Namespace constructor.
Expand Down Expand Up @@ -158,9 +150,8 @@ class Namespace extends events_1.EventEmitter {
*/
// @ts-ignore
emit(ev, ...args) {
if (~events.indexOf(ev)) {
super.emit.apply(this, arguments);
return this;
if (socket_1.RESERVED_EVENTS.has(ev)) {
throw new Error(`"${ev}" is a reserved event name`);
}
// set up packet object
args.unshift(ev);
Expand Down
3 changes: 2 additions & 1 deletion dist/socket.d.ts
Expand Up @@ -4,6 +4,7 @@ import { Client } from "./client";
import { Namespace } from "./namespace";
import { IncomingMessage } from "http";
import { Room, SocketId } from "socket.io-adapter";
export declare const RESERVED_EVENTS: Set<string>;
/**
* The handshake details
*/
Expand Down Expand Up @@ -76,7 +77,7 @@ export declare class Socket extends EventEmitter {
*
* @return {Socket} self
*/
emit(ev: any): this;
emit(ev: string, ...args: any[]): this;
/**
* Targets a room when broadcasting.
*
Expand Down
19 changes: 8 additions & 11 deletions dist/socket.js
Expand Up @@ -3,25 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Socket = void 0;
exports.Socket = exports.RESERVED_EVENTS = void 0;
const events_1 = require("events");
const socket_io_parser_1 = require("socket.io-parser");
const has_binary2_1 = __importDefault(require("has-binary2"));
const url_1 = __importDefault(require("url"));
const debug_1 = __importDefault(require("debug"));
const base64id_1 = __importDefault(require("base64id"));
const debug = debug_1.default("socket.io:socket");
/**
* Blacklisted events.
*/
const events = [
exports.RESERVED_EVENTS = new Set([
"error",
"connect",
"disconnect",
"disconnecting",
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
"newListener",
"removeListener"
];
]);
class Socket extends events_1.EventEmitter {
/**
* Interface to a `Client` for a given `Namespace`.
Expand Down Expand Up @@ -69,12 +67,11 @@ class Socket extends events_1.EventEmitter {
* @return {Socket} self
*/
// @ts-ignore
emit(ev) {
if (~events.indexOf(ev)) {
super.emit.apply(this, arguments);
return this;
emit(ev, ...args) {
if (exports.RESERVED_EVENTS.has(ev)) {
throw new Error(`"${ev}" is a reserved event name`);
}
const args = Array.prototype.slice.call(arguments);
args.unshift(ev);
const packet = {
type: (this.flags.binary !== undefined
? this.flags.binary
Expand Down
17 changes: 3 additions & 14 deletions lib/namespace.ts
@@ -1,4 +1,4 @@
import { Socket } from "./socket";
import { Socket, RESERVED_EVENTS } from "./socket";
import { Server } from "./index";
import { Client } from "./client";
import { EventEmitter } from "events";
Expand All @@ -9,16 +9,6 @@ import { Adapter, Room, SocketId } from "socket.io-adapter";

const debug = debugModule("socket.io:namespace");

/**
* Blacklisted events.
*/

const events = [
"connect", // for symmetry with client
"connection",
"newListener"
];

export class Namespace extends EventEmitter {
public readonly name: string;
public readonly connected: Map<SocketId, Socket> = new Map();
Expand Down Expand Up @@ -176,9 +166,8 @@ export class Namespace extends EventEmitter {
*/
// @ts-ignore
public emit(ev: string, ...args: any[]): Namespace {
if (~events.indexOf(ev)) {
super.emit.apply(this, arguments);
return this;
if (RESERVED_EVENTS.has(ev)) {
throw new Error(`"${ev}" is a reserved event name`);
}
// set up packet object
args.unshift(ev);
Expand Down
19 changes: 7 additions & 12 deletions lib/socket.ts
Expand Up @@ -12,18 +12,15 @@ import base64id from "base64id";

const debug = debugModule("socket.io:socket");

/**
* Blacklisted events.
*/

const events = [
export const RESERVED_EVENTS = new Set([
"error",
"connect",
"disconnect",
"disconnecting",
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
"newListener",
"removeListener"
];
]);

/**
* The handshake details
Expand Down Expand Up @@ -133,13 +130,11 @@ export class Socket extends EventEmitter {
* @return {Socket} self
*/
// @ts-ignore
public emit(ev) {
if (~events.indexOf(ev)) {
super.emit.apply(this, arguments);
return this;
public emit(ev: string, ...args: any[]) {
if (RESERVED_EVENTS.has(ev)) {
throw new Error(`"${ev}" is a reserved event name`);
}

const args = Array.prototype.slice.call(arguments);
args.unshift(ev);
const packet: any = {
type: (this.flags.binary !== undefined
? this.flags.binary
Expand Down
24 changes: 24 additions & 0 deletions test/socket.io.ts
Expand Up @@ -693,6 +693,14 @@ describe("socket.io", () => {
});
});

it("should throw on reserved event", () => {
const sio = new Server();

expect(() => sio.emit("connect")).to.throwException(
/"connect" is a reserved event name/
);
});

describe("dynamic namespaces", () => {
it("should allow connections to dynamic namespaces with a regex", done => {
const srv = createServer();
Expand Down Expand Up @@ -1657,6 +1665,22 @@ describe("socket.io", () => {
});
});
});

it("should throw on reserved event", done => {
const srv = createServer();
const sio = new Server(srv);

srv.listen(() => {
const socket = client(srv);
sio.on("connection", s => {
expect(() => s.emit("error")).to.throwException(
/"error" is a reserved event name/
);
socket.close();
done();
});
});
});
});

describe("messaging many", () => {
Expand Down

0 comments on commit f7ed81e

Please sign in to comment.