Skip to content

Commit

Permalink
fix: ignore sessionStore in the fetchSockets method
Browse files Browse the repository at this point in the history
This field may contain circular references, which make `JSON.stringify`
throw.

Related: #421
  • Loading branch information
darrachequesne committed Nov 15, 2021
1 parent 214b5d1 commit c5dce43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,16 @@ export class RedisAdapter extends Adapter {

response = JSON.stringify({
requestId: request.requestId,
sockets: localSockets.map((socket) => ({
id: socket.id,
handshake: socket.handshake,
rooms: [...socket.rooms],
data: socket.data,
})),
sockets: localSockets.map((socket) => {
// remove sessionStore from handshake, as it may contain circular references
const { sessionStore, ...handshake } = socket.handshake;
return {
id: socket.id,
handshake,
rooms: [...socket.rooms],
data: socket.data,
};
}),
});

this.pubClient.publish(this.responseChannel, response);
Expand Down
2 changes: 2 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ const shouldNotHappen = (done) => () => done(new Error("should not happen"));
describe("fetchSockets", () => {
it("returns all socket instances", async () => {
socket2.data = "test";
socket2.handshake.sessionStore = "not included";

const sockets = await namespace1.fetchSockets();
expect(sockets).to.be.an(Array);
Expand All @@ -339,6 +340,7 @@ const shouldNotHappen = (done) => () => done(new Error("should not happen"));
(socket) => socket.id === socket2.id
);
expect(remoteSocket2 === socket2).to.be(false);
delete socket2.handshake.sessionStore;
expect(remoteSocket2.handshake).to.eql(socket2.handshake);
expect(remoteSocket2.data).to.eql("test");
expect(remoteSocket2.rooms.size).to.eql(1);
Expand Down

0 comments on commit c5dce43

Please sign in to comment.