fix(server): add support for dynamic namespaces

This fix depends on the "new_namespace" event emitted by the server,
which was added in `socket.io@4.1.0`.

Related: https://github.com/socketio/socket.io-admin-ui/issues/6
This commit is contained in:
Damien Arrachequesne 2021-06-02 08:46:11 +02:00
parent 1cf991e49a
commit 74f1c20f6a
No known key found for this signature in database
GPG Key ID: 544D14663E7F7CF0
4 changed files with 38 additions and 12 deletions

View File

@ -397,6 +397,7 @@ export function instrument(io: Server, opts: Partial<InstrumentOptions>) {
});
io._nsps.forEach((nsp) => registerListeners(adminNamespace, nsp));
io.on("new_namespace", (nsp) => registerListeners(adminNamespace, nsp));
}
export { InMemoryStore, RedisStore } from "./stores";

22
package-lock.json generated
View File

@ -964,9 +964,9 @@
"dev": true
},
"engine.io": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-5.0.0.tgz",
"integrity": "sha512-BATIdDV3H1SrE9/u2BAotvsmjJg0t1P4+vGedImSs1lkFAtQdvk4Ev1y4LDiPF7BPWgXWEG+NDY+nLvW3UrMWw==",
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-5.1.1.tgz",
"integrity": "sha512-aMWot7H5aC8L4/T8qMYbLdvKlZOdJTH54FxfdFunTGvhMx1BHkJOntWArsVfgAZVwAO9LC2sryPWRcEeUzCe5w==",
"dev": true,
"requires": {
"accepts": "~1.3.4",
@ -2177,9 +2177,9 @@
"integrity": "sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA=="
},
"socket.io": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.0.1.tgz",
"integrity": "sha512-g8eZB9lV0f4X4gndG0k7YZAywOg1VxYgCUspS4V+sDqsgI/duqd0AW84pKkbGj/wQwxrqrEq+VZrspRfTbHTAQ==",
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.1.2.tgz",
"integrity": "sha512-xK0SD1C7hFrh9+bYoYCdVt+ncixkSLKtNLCax5aEy1o3r5PaO5yQhVb97exIe67cE7lAK+EpyMytXWTWmyZY8w==",
"dev": true,
"requires": {
"@types/cookie": "^0.4.0",
@ -2188,15 +2188,15 @@
"accepts": "~1.3.4",
"base64id": "~2.0.0",
"debug": "~4.3.1",
"engine.io": "~5.0.0",
"socket.io-adapter": "~2.2.0",
"engine.io": "~5.1.0",
"socket.io-adapter": "~2.3.0",
"socket.io-parser": "~4.0.3"
}
},
"socket.io-adapter": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.2.0.tgz",
"integrity": "sha512-rG49L+FwaVEwuAdeBRq49M97YI3ElVabJPzvHT9S6a2CWhDKnjSFasvwAwSYPRhQzfn4NtDIbCaGYgOCOU/rlg==",
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.3.1.tgz",
"integrity": "sha512-8cVkRxI8Nt2wadkY6u60Y4rpW3ejA1rxgcK2JuyIhmF+RMNpTy1QRtkHIDUOf3B4HlQwakMsWbKftMv/71VMmw==",
"dev": true
},
"socket.io-client": {

View File

@ -49,7 +49,7 @@
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"redis": "^3.0.2",
"socket.io": "^4.0.1",
"socket.io": "^4.1.2",
"socket.io-client": "^4.0.1",
"socket.io-v3": "npm:socket.io@^3.1.2",
"ts-node": "^9.1.1",

View File

@ -336,6 +336,31 @@ describe("Socket.IO Admin (server instrumentation)", () => {
adminSocket.disconnect();
});
it("supports dynamic namespaces", async function () {
// requires `socket.io>=4.1.0` with the "new_namespace" event
if (version === "v3") {
return this.skip();
}
instrument(io, {
auth: false,
});
io.of(/\/dynamic-\d+/);
const adminSocket = ioc(`http://localhost:${port}/admin`);
await waitFor(adminSocket, "connect");
const clientSocket = ioc(`http://localhost:${port}/dynamic-101`, {
forceNew: true,
});
const socket = await waitFor(adminSocket, "socket_connected");
expect(socket.nsp).to.eql("/dynamic-101");
clientSocket.disconnect();
adminSocket.disconnect();
});
});
});