Skip to content

Commit c5dce43

Browse files
fix: ignore sessionStore in the fetchSockets method
This field may contain circular references, which make `JSON.stringify` throw. Related: #421
1 parent 214b5d1 commit c5dce43

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,16 @@ export class RedisAdapter extends Adapter {
298298

299299
response = JSON.stringify({
300300
requestId: request.requestId,
301-
sockets: localSockets.map((socket) => ({
302-
id: socket.id,
303-
handshake: socket.handshake,
304-
rooms: [...socket.rooms],
305-
data: socket.data,
306-
})),
301+
sockets: localSockets.map((socket) => {
302+
// remove sessionStore from handshake, as it may contain circular references
303+
const { sessionStore, ...handshake } = socket.handshake;
304+
return {
305+
id: socket.id,
306+
handshake,
307+
rooms: [...socket.rooms],
308+
data: socket.data,
309+
};
310+
}),
307311
});
308312

309313
this.pubClient.publish(this.responseChannel, response);

test/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ const shouldNotHappen = (done) => () => done(new Error("should not happen"));
327327
describe("fetchSockets", () => {
328328
it("returns all socket instances", async () => {
329329
socket2.data = "test";
330+
socket2.handshake.sessionStore = "not included";
330331

331332
const sockets = await namespace1.fetchSockets();
332333
expect(sockets).to.be.an(Array);
@@ -339,6 +340,7 @@ const shouldNotHappen = (done) => () => done(new Error("should not happen"));
339340
(socket) => socket.id === socket2.id
340341
);
341342
expect(remoteSocket2 === socket2).to.be(false);
343+
delete socket2.handshake.sessionStore;
342344
expect(remoteSocket2.handshake).to.eql(socket2.handshake);
343345
expect(remoteSocket2.data).to.eql("test");
344346
expect(remoteSocket2.rooms.size).to.eql(1);

0 commit comments

Comments
 (0)