fix: properly track updates to socket.data

Before this commit, attributes added to `socket.data` within a
middleware were lost.

Related: 3773fe4b1c
This commit is contained in:
Corey 2022-05-02 17:39:42 +01:00 committed by Damien Arrachequesne
parent 4359536a4b
commit e17e9e1dbe
No known key found for this signature in database
GPG Key ID: 544D14663E7F7CF0
2 changed files with 46 additions and 0 deletions

View File

@ -321,6 +321,8 @@ const registerListeners = (
});
};
const data = socket.data || {}; // could be set in a middleware
socket.data = createProxy({
_admin: {
clientId: clientId.substring(0, 12), // this information is quite sensitive
@ -328,6 +330,10 @@ const registerListeners = (
},
});
for (const key in data) {
socket.data[key] = createProxy(data[key]);
}
adminNamespace.emit("socket_connected", serialize(socket, nsp.name));
socket.conn.on("upgrade", (transport: any) => {

View File

@ -300,6 +300,46 @@ describe("Socket.IO Admin (server instrumentation)", () => {
adminSocket.disconnect();
});
it("emits event when socket.data is updated", async () => {
instrument(io, {
auth: false,
});
const adminSocket = ioc(`http://localhost:${port}/admin`);
await waitFor(adminSocket, "connect");
const clientSocket = ioc(`http://localhost:${port}`, {
forceNew: true,
transports: ["polling"],
});
io.use((socket, next) => {
socket.data = socket.data || {};
socket.data.count = 1;
socket.data.array = [1];
next();
});
const serverSocket = await waitFor(io, "connection");
const socket = await waitFor(adminSocket, "socket_connected");
expect(socket.data).to.eql({ count: 1, array: [1] });
serverSocket.data.count++;
const updatedSocket1 = await waitFor(adminSocket, "socket_updated");
expect(updatedSocket1.data).to.eql({ count: 2, array: [1] });
serverSocket.data.array.push(2);
const updatedSocket2 = await waitFor(adminSocket, "socket_updated");
expect(updatedSocket2.data).to.eql({ count: 2, array: [1, 2] });
adminSocket.disconnect();
clientSocket.disconnect();
});
it("performs administrative tasks", async () => {
instrument(io, {
auth: false,