Skip to content

Commit

Permalink
feat: notify upon namespace creation
Browse files Browse the repository at this point in the history
A "new_namespace" event will be emitted when a new namespace is created:

```js
io.on("new_namespace", (namespace) => {
  // ...
});
```

This could be used for example for registering the same middleware for
each namespace.

See #3851
  • Loading branch information
darrachequesne committed May 10, 2021
1 parent 93cce05 commit 499c892
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
19 changes: 12 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import path = require("path");
import engine = require("engine.io");
import { Client } from "./client";
import { EventEmitter } from "events";
import {
ExtendedError,
Namespace,
NamespaceReservedEventsMap,
} from "./namespace";
import { ExtendedError, Namespace, ServerReservedEventsMap } from "./namespace";
import { ParentNamespace } from "./parent-namespace";
import { Adapter, Room, SocketId } from "socket.io-adapter";
import * as parser from "socket.io-parser";
Expand Down Expand Up @@ -176,7 +172,7 @@ export class Server<
> extends StrictEventEmitter<
ServerSideEvents,
EmitEvents,
NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>
ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>
> {
public readonly sockets: Namespace<
ListenEvents,
Expand Down Expand Up @@ -306,7 +302,12 @@ export class Server<
if (err || !allow) {
run();
} else {
fn(this.parentNsps.get(nextFn.value)!.createChild(name));
const namespace = this.parentNsps
.get(nextFn.value)!
.createChild(name);
// @ts-ignore
this.sockets.emitReserved("new_namespace", namespace);
fn(namespace);
}
});
};
Expand Down Expand Up @@ -627,6 +628,10 @@ export class Server<
debug("initializing namespace %s", name);
nsp = new Namespace(this, name);
this._nsps.set(name, nsp);
if (name !== "/") {
// @ts-ignore
this.sockets.emitReserved("new_namespace", nsp);
}
}
if (fn) nsp.on("connect", fn);
return nsp;
Expand Down
18 changes: 16 additions & 2 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,23 @@ export interface NamespaceReservedEventsMap<
) => void;
}

export interface ServerReservedEventsMap<
ListenEvents,
EmitEvents,
ServerSideEvents
> extends NamespaceReservedEventsMap<
ListenEvents,
EmitEvents,
ServerSideEvents
> {
new_namespace: (
namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents>
) => void;
}

export const RESERVED_EVENTS: ReadonlySet<string | Symbol> = new Set<
keyof NamespaceReservedEventsMap<never, never, never>
>(<const>["connect", "connection"]);
keyof ServerReservedEventsMap<never, never, never>
>(<const>["connect", "connection", "new_namespace"]);

export class Namespace<
ListenEvents extends EventsMap = DefaultEventsMap,
Expand Down
31 changes: 30 additions & 1 deletion test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ describe("socket.io", () => {
});
});

it("should close a client without namespace", (done) => {
it("should close a client without namespace (2)", (done) => {
const srv = createServer();
const sio = new Server(srv, {
connectTimeout: 100,
Expand Down Expand Up @@ -886,6 +886,17 @@ describe("socket.io", () => {
});
});

it("should emit an 'new_namespace' event", (done) => {
const sio = new Server();

sio.on("new_namespace", (namespace) => {
expect(namespace.name).to.eql("/nsp");
done();
});

sio.of("/nsp");
});

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

it("should emit an 'new_namespace' event for a dynamic namespace", (done) => {
const srv = createServer();
const sio = new Server(srv);
srv.listen(() => {
sio.of(/^\/dynamic-\d+$/);

sio.on("new_namespace", (namespace) => {
expect(namespace.name).to.be("/dynamic-101");

socket.disconnect();
srv.close();
done();
});

const socket = client(srv, "/dynamic-101");
});
});
});
});

Expand Down

0 comments on commit 499c892

Please sign in to comment.