1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
import { describe, it, expect } from "bun:test";
import { Server, Socket } from "socket.io";
import { waitFor, eioHandshake, eioPush, eioPoll, fail, success } from "./support/util.ts";
import { createServer, Server as HttpServer } from "http";
import { Adapter } from "socket.io-adapter";
async function init(httpServer: HttpServer, io: Server) {
// Engine.IO handshake
const sid = await eioHandshake(httpServer);
// Socket.IO handshake
await eioPush(httpServer, sid, "40");
const handshakeBody = await eioPoll(httpServer, sid);
expect(handshakeBody.startsWith("40")).toBe(true);
const handshake = JSON.parse(handshakeBody.substring(2));
expect(handshake.sid).not.toBe(undefined);
// in that case, the handshake also contains a private session ID
expect(handshake.pid).not.toBe(undefined);
io.emit("hello");
const message = await eioPoll(httpServer, sid);
expect(message.startsWith('42["hello"')).toBe(true);
const offset = JSON.parse(message.substring(2))[1];
// in that case, each packet also includes an offset in the data array
expect(offset).not.toBe(undefined);
await eioPush(httpServer, sid, "1");
return [handshake.sid, handshake.pid, offset];
}
describe("connection state recovery", () => {
it("should restore session and missed packets", done => {
const httpServer = createServer().listen(0);
const io = new Server(httpServer, {
connectionStateRecovery: {},
});
const timeout = setTimeout(() => {
fail(done, io, new Error("timeout"));
}, 200);
let serverSocket: Socket | undefined;
io.once("connection", socket => {
socket.join("room1");
serverSocket = socket;
});
(async () => {
try {
const [sid, pid, offset] = await init(httpServer, io);
io.emit("hello1"); // broadcast
io.to("room1").emit("hello2"); // broadcast to room
serverSocket?.emit("hello3"); // direct message
const newSid = await eioHandshake(httpServer);
await eioPush(httpServer, newSid, `40{"pid":"${pid}","offset":"${offset}"}`);
const payload = await eioPoll(httpServer, newSid);
clearTimeout(timeout);
const packets = payload.split("\x1e");
expect(packets.length).toBe(4);
// note: EVENT packets are received before the CONNECT packet, which is a bit weird
// see also: https://github.com/socketio/socket.io-deno/commit/518f534e1c205b746b1cb21fe76b187dabc96f34
expect(packets[0].startsWith('42["hello1"')).toBe(true);
expect(packets[1].startsWith('42["hello2"')).toBe(true);
expect(packets[2].startsWith('42["hello3"')).toBe(true);
expect(packets[3]).toBe(`40{"sid":"${sid}","pid":"${pid}"}`);
success(done, io);
} catch (err) {
fail(done, io, err);
}
})();
});
it("should restore rooms and data attributes", done => {
const httpServer = createServer().listen(0);
const io = new Server(httpServer, {
connectionStateRecovery: {},
});
const timeout = setTimeout(() => {
fail(done, io, new Error("timeout"));
}, 200);
io.once("connection", socket => {
expect(socket.recovered).toBe(false);
socket.join("room1");
socket.join("room2");
socket.data.foo = "bar";
});
(async () => {
try {
const [sid, pid, offset] = await init(httpServer, io);
const newSid = await eioHandshake(httpServer);
const [socket] = await Promise.all([
waitFor<Socket>(io, "connection"),
eioPush(httpServer, newSid, `40{"pid":"${pid}","offset":"${offset}"}`),
]);
clearTimeout(timeout);
expect(socket.id).toBe(sid);
expect(socket.recovered).toBe(true);
expect(socket.rooms.has(socket.id)).toBe(true);
expect(socket.rooms.has("room1")).toBe(true);
expect(socket.rooms.has("room2")).toBe(true);
expect(socket.data.foo).toBe("bar");
await eioPoll(httpServer, newSid); // drain buffer
success(done, io);
} catch (err) {
fail(done, io, err);
}
})();
});
it("should not run middlewares upon recovery by default", done => {
const httpServer = createServer().listen(0);
const io = new Server(httpServer, {
connectionStateRecovery: {},
});
const timeout = setTimeout(() => {
fail(done, io, new Error("timeout"));
}, 200);
(async () => {
try {
const [_, pid, offset] = await init(httpServer, io);
io.use((socket, next) => {
socket.data.middlewareWasCalled = true;
next();
});
const newSid = await eioHandshake(httpServer);
const [socket] = await Promise.all([
waitFor<Socket>(io, "connection"),
eioPush(httpServer, newSid, `40{"pid":"${pid}","offset":"${offset}"}`),
]);
clearTimeout(timeout);
expect(socket.recovered).toBe(true);
expect(socket.data.middlewareWasCalled).toBe(undefined);
await eioPoll(httpServer, newSid); // drain buffer
success(done, io);
} catch (err) {
fail(done, io, err);
}
})();
});
it("should run middlewares even upon recovery", done => {
const httpServer = createServer().listen(0);
const io = new Server(httpServer, {
connectionStateRecovery: {
skipMiddlewares: false,
},
});
const timeout = setTimeout(() => {
fail(done, io, new Error("timeout"));
}, 200);
(async () => {
try {
const [_, pid, offset] = await init(httpServer, io);
io.use((socket, next) => {
socket.data.middlewareWasCalled = true;
next();
});
const newSid = await eioHandshake(httpServer);
const [socket] = await Promise.all([
waitFor<Socket>(io, "connection"),
eioPush(httpServer, newSid, `40{"pid":"${pid}","offset":"${offset}"}`),
]);
clearTimeout(timeout);
expect(socket.recovered).toBe(true);
expect(socket.data.middlewareWasCalled).toBe(true);
await eioPoll(httpServer, newSid); // drain buffer
success(done, io);
} catch (err) {
fail(done, io, err);
}
})();
});
it("should fail to restore an unknown session", done => {
const httpServer = createServer().listen(0);
const io = new Server(httpServer, {
connectionStateRecovery: {},
});
const timeout = setTimeout(() => {
fail(done, io, new Error("timeout"));
}, 200);
(async () => {
try {
// Engine.IO handshake
const sid = await eioHandshake(httpServer);
// Socket.IO handshake
await eioPush(httpServer, sid, '40{"pid":"foo","offset":"bar"}');
const handshakeBody = await eioPoll(httpServer, sid);
clearTimeout(timeout);
expect(handshakeBody.startsWith("40")).toBe(true);
const handshake = JSON.parse(handshakeBody.substring(2));
expect(handshake.sid).not.toBe("foo");
expect(handshake.pid).not.toBe("bar");
success(done, io);
} catch (err) {
fail(done, io, err);
}
})();
});
it("should be disabled by default", done => {
const httpServer = createServer().listen(0);
const io = new Server(httpServer);
const timeout = setTimeout(() => {
fail(done, io, new Error("timeout"));
}, 200);
(async () => {
try {
// Engine.IO handshake
const sid = await eioHandshake(httpServer);
// Socket.IO handshake
await eioPush(httpServer, sid, "40");
const handshakeBody = await eioPoll(httpServer, sid);
clearTimeout(timeout);
expect(handshakeBody.startsWith("40")).toBe(true);
const handshake = JSON.parse(handshakeBody.substring(2));
expect(handshake.sid).not.toBe(undefined);
expect(handshake.pid).toBe(undefined);
success(done, io);
} catch (err) {
fail(done, io, err);
}
})();
});
it("should not call adapter#persistSession or adapter#restoreSession if disabled", done => {
const httpServer = createServer().listen(0);
let io: Server;
class DummyAdapter extends Adapter {
override persistSession() {
fail(done, io, new Error("should not happen"));
return Promise.reject("should not happen");
}
override restoreSession() {
fail(done, io, new Error("should not happen"));
return Promise.reject("should not happen");
}
}
io = new Server(httpServer, {
adapter: DummyAdapter,
});
const timeout = setTimeout(() => {
fail(done, io, new Error("timeout"));
}, 200);
(async () => {
try {
// Engine.IO handshake
const sid = await eioHandshake(httpServer);
await eioPush(httpServer, sid, '40{"pid":"foo","offset":"bar"}');
await eioPoll(httpServer, sid);
await eioPush(httpServer, sid, "1");
clearTimeout(timeout);
success(done, io);
} catch (err) {
fail(done, io, err);
}
})();
});
});
|