@jskit-ai/realtime 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/patterns/realtime-application/example/package.json +1 -1
- package/src/client/RealtimeClientProvider.js +15 -1
- package/src/client/connectionRecovery.js +15 -2
- package/src/server/realtimeAudience.js +32 -17
- package/src/server/realtimeDelivery.js +33 -14
- package/test/authorization.test.js +73 -5
- package/test/clientRuntime.test.js +21 -0
- package/test/providerRuntime.test.js +10 -1
package/package.json
CHANGED
|
@@ -49,6 +49,7 @@ function createRealtimeClient({ config, loggerInput } = {}) {
|
|
|
49
49
|
const servedClientBuild = trackServedClientBuild(socket, logger);
|
|
50
50
|
const detach = [];
|
|
51
51
|
let initialized = false;
|
|
52
|
+
let beforeReconnect = null;
|
|
52
53
|
|
|
53
54
|
function initialize(vueApp = null) {
|
|
54
55
|
if (initialized) {
|
|
@@ -57,7 +58,7 @@ function createRealtimeClient({ config, loggerInput } = {}) {
|
|
|
57
58
|
initialized = true;
|
|
58
59
|
|
|
59
60
|
if (typeof socket.on === "function") {
|
|
60
|
-
detach.push(attachSocketConnectionRecovery(socket));
|
|
61
|
+
detach.push(attachSocketConnectionRecovery(socket, { prepareReconnect: () => beforeReconnect?.() }));
|
|
61
62
|
const onConnect = () => logger.debug({ socketConnected: true }, "Realtime client socket connected.");
|
|
62
63
|
const onDisconnect = (reason) =>
|
|
63
64
|
logger.debug(
|
|
@@ -108,11 +109,24 @@ function createRealtimeClient({ config, loggerInput } = {}) {
|
|
|
108
109
|
initialized = false;
|
|
109
110
|
}
|
|
110
111
|
|
|
112
|
+
// The step to run before recovery reconnects after the server refused or
|
|
113
|
+
// dropped the socket, such as auth refreshing the session.
|
|
114
|
+
function setBeforeReconnect(prepare) {
|
|
115
|
+
if (typeof prepare !== "function") {
|
|
116
|
+
throw new TypeError("setBeforeReconnect requires a function.");
|
|
117
|
+
}
|
|
118
|
+
beforeReconnect = prepare;
|
|
119
|
+
return () => {
|
|
120
|
+
if (beforeReconnect === prepare) beforeReconnect = null;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
111
124
|
return Object.freeze({
|
|
112
125
|
config,
|
|
113
126
|
dispose,
|
|
114
127
|
initialize,
|
|
115
128
|
onServedClientBuild: servedClientBuild.subscribe,
|
|
129
|
+
setBeforeReconnect,
|
|
116
130
|
socket
|
|
117
131
|
});
|
|
118
132
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { reconnectSocketIoClient } from "./runtime.js";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// `prepareReconnect` runs before each recovery reconnect; auth uses it to
|
|
4
|
+
// refresh an expired session over HTTP so the handshake carries fresh cookies.
|
|
5
|
+
function attachSocketConnectionRecovery(socket, { prepareReconnect = null } = {}) {
|
|
4
6
|
let timer = null;
|
|
5
7
|
let delay = 1_000;
|
|
6
8
|
let recovering = false;
|
|
@@ -18,11 +20,22 @@ function attachSocketConnectionRecovery(socket) {
|
|
|
18
20
|
!document?.hidden && window?.navigator?.onLine !== false;
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
async function retry() {
|
|
24
|
+
if (prepareReconnect) {
|
|
25
|
+
try {
|
|
26
|
+
await prepareReconnect();
|
|
27
|
+
} catch {
|
|
28
|
+
// The handshake then decides; a refusal schedules the next retry.
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (canRetry()) socket.connect();
|
|
32
|
+
}
|
|
33
|
+
|
|
21
34
|
function scheduleRetry() {
|
|
22
35
|
if (timer !== null || !canRetry()) return;
|
|
23
36
|
timer = setTimeout(() => {
|
|
24
37
|
timer = null;
|
|
25
|
-
if (canRetry())
|
|
38
|
+
if (canRetry()) void retry();
|
|
26
39
|
}, delay);
|
|
27
40
|
delay = Math.min(delay * 2, 30_000);
|
|
28
41
|
}
|
|
@@ -238,13 +238,16 @@ function createSocketRequest(socket) {
|
|
|
238
238
|
return request;
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
241
|
+
// A socket cannot store a refreshed session, so its checks never refresh one.
|
|
242
|
+
// A provider that would have to (its access token expired) answers
|
|
243
|
+
// `refreshRequired`, and the client refreshes over HTTP before reconnecting.
|
|
244
|
+
async function resolveSocketAuthentication(authService, socket, request = createSocketRequest(socket),
|
|
245
|
+
authenticateRequest = (value) => authService.authenticateRequest(value, { refreshSession: false })) {
|
|
246
|
+
if (typeof authService?.authenticateRequest !== "function") return { actor: null, refreshRequired: false };
|
|
244
247
|
const result = await authenticateRequest(request);
|
|
245
248
|
const id = result?.authenticated === true
|
|
246
249
|
? normalizeRecordId(result?.actor?.id, { fallback: null }) : null;
|
|
247
|
-
return id ? { ...result.actor, id } : null;
|
|
250
|
+
return { actor: id ? { ...result.actor, id } : null, refreshRequired: result?.refreshRequired === true };
|
|
248
251
|
}
|
|
249
252
|
|
|
250
253
|
function realtimeAuthenticationRequired(authService = null) {
|
|
@@ -257,28 +260,42 @@ function authenticationRequiredError() {
|
|
|
257
260
|
return error;
|
|
258
261
|
}
|
|
259
262
|
|
|
263
|
+
function sessionRefreshRequiredError() {
|
|
264
|
+
const error = new Error("Session refresh required.");
|
|
265
|
+
error.data = Object.freeze({ code: "SESSION_REFRESH_REQUIRED" });
|
|
266
|
+
return error;
|
|
267
|
+
}
|
|
268
|
+
|
|
260
269
|
function rememberSocketActorId(socket, actorId) {
|
|
261
270
|
socket.data = socket.data && typeof socket.data === "object" ? socket.data : {};
|
|
262
271
|
socket.data.actorId = actorId;
|
|
263
272
|
}
|
|
264
273
|
|
|
265
|
-
|
|
266
|
-
|
|
274
|
+
// Every handshake is authenticated here. A login that needs a session refresh
|
|
275
|
+
// is refused even when sign-in is optional, so the client refreshes and
|
|
276
|
+
// reconnects instead of silently connecting without its user's rooms.
|
|
277
|
+
function registerSocketAuthentication({ io, logger, authService }) {
|
|
278
|
+
const required = realtimeAuthenticationRequired(authService);
|
|
279
|
+
if (!required && typeof authService?.authenticateRequest !== "function") return;
|
|
267
280
|
if (typeof io?.use !== "function") {
|
|
268
|
-
throw new TypeError("Realtime
|
|
281
|
+
throw new TypeError("Realtime socket authentication requires Socket.IO middleware support.");
|
|
269
282
|
}
|
|
270
283
|
io.use(async (socket, next) => {
|
|
271
284
|
try {
|
|
272
|
-
const
|
|
273
|
-
if (
|
|
285
|
+
const { actor, refreshRequired } = await resolveSocketAuthentication(authService, socket);
|
|
286
|
+
if (refreshRequired) {
|
|
287
|
+
next(sessionRefreshRequiredError());
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (!actor && required) {
|
|
274
291
|
next(authenticationRequiredError());
|
|
275
292
|
return;
|
|
276
293
|
}
|
|
277
|
-
rememberSocketActorId(socket,
|
|
294
|
+
if (actor) rememberSocketActorId(socket, actor.id);
|
|
278
295
|
next();
|
|
279
296
|
} catch (error) {
|
|
280
297
|
logger.warn({ error: String(error?.message || error) }, "Realtime socket authentication failed.");
|
|
281
|
-
next(authenticationRequiredError());
|
|
298
|
+
next(required ? authenticationRequiredError() : undefined);
|
|
282
299
|
}
|
|
283
300
|
});
|
|
284
301
|
}
|
|
@@ -294,14 +311,12 @@ async function joinContributedRooms(socket, actorId, audiences) {
|
|
|
294
311
|
|
|
295
312
|
function registerSocketAudienceBootstrap({ io, logger, authService = null, audiences = null }) {
|
|
296
313
|
if (typeof io?.on !== "function") return;
|
|
297
|
-
|
|
314
|
+
registerSocketAuthentication({ io, logger, authService });
|
|
298
315
|
io.on("connection", async (socket) => {
|
|
299
316
|
try {
|
|
300
317
|
socket.join(ALL_CLIENTS_ROOM);
|
|
301
|
-
const actorId = normalizeRecordId(socket?.data?.actorId, { fallback: null })
|
|
302
|
-
|| (await resolveSocketActor(authService, socket))?.id;
|
|
318
|
+
const actorId = normalizeRecordId(socket?.data?.actorId, { fallback: null });
|
|
303
319
|
if (!actorId) return;
|
|
304
|
-
rememberSocketActorId(socket, actorId);
|
|
305
320
|
socket.join(ALL_USERS_ROOM);
|
|
306
321
|
socket.join(roomForUser(actorId));
|
|
307
322
|
await joinContributedRooms(socket, actorId, audiences);
|
|
@@ -312,9 +327,9 @@ function registerSocketAudienceBootstrap({ io, logger, authService = null, audie
|
|
|
312
327
|
}
|
|
313
328
|
|
|
314
329
|
async function revalidateSocket({ socket, authService, audiences = null, request, authenticateRequest }) {
|
|
315
|
-
const actor = await
|
|
330
|
+
const { actor, refreshRequired } = await resolveSocketAuthentication(authService, socket, request, authenticateRequest);
|
|
316
331
|
const previousActorId = socket.data?.actorId;
|
|
317
|
-
if ((!actor && realtimeAuthenticationRequired(authService)) ||
|
|
332
|
+
if (refreshRequired || (!actor && realtimeAuthenticationRequired(authService)) ||
|
|
318
333
|
(previousActorId && previousActorId !== actor?.id)) {
|
|
319
334
|
socket.disconnect(true);
|
|
320
335
|
return null;
|
|
@@ -19,14 +19,28 @@ function publicRealtimePayload(event) {
|
|
|
19
19
|
return Object.freeze({ ...(payload || {}), ...canonical });
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
// One
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
// One answer per question within one pass.
|
|
23
|
+
function sharedWithinPass(load, keyOf = (value) => value) {
|
|
24
|
+
const answers = new Map();
|
|
25
|
+
return (value) => {
|
|
26
|
+
const key = keyOf(value);
|
|
27
|
+
if (!answers.has(key)) answers.set(key, (async () => load(value))());
|
|
28
|
+
return answers.get(key);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The checks of one publish or 30-second pass. Identical socket requests share
|
|
33
|
+
// one authentication (the auth service sees only the request), and sockets of
|
|
34
|
+
// one actor share its contributed rooms. Revalidation asks only who is signed
|
|
35
|
+
// in, so the provider reads the app profile instead of synchronizing it, and
|
|
36
|
+
// never refreshes a session the socket could not store.
|
|
37
|
+
function createPassChecks(authService, audiences) {
|
|
38
|
+
return {
|
|
39
|
+
authenticateRequest: sharedWithinPass(
|
|
40
|
+
(request) => authService.authenticateRequest(request, { syncProfile: false, refreshSession: false }),
|
|
41
|
+
JSON.stringify
|
|
42
|
+
),
|
|
43
|
+
audiences: audiences && { resolveActorRooms: sharedWithinPass((actorId) => audiences.resolveActorRooms(actorId)) }
|
|
30
44
|
};
|
|
31
45
|
}
|
|
32
46
|
|
|
@@ -49,9 +63,14 @@ function createRealtimeDelivery({ io, database = null, logger, authService = nul
|
|
|
49
63
|
let revalidationTimer = null;
|
|
50
64
|
let stopped = false;
|
|
51
65
|
|
|
52
|
-
async function revalidate(socket,
|
|
66
|
+
async function revalidate(socket, checks) {
|
|
53
67
|
try {
|
|
54
|
-
return await revalidateSocket({
|
|
68
|
+
return await revalidateSocket({
|
|
69
|
+
socket,
|
|
70
|
+
authService,
|
|
71
|
+
audiences: checks.audiences,
|
|
72
|
+
authenticateRequest: checks.authenticateRequest
|
|
73
|
+
});
|
|
55
74
|
} catch (error) {
|
|
56
75
|
socket.disconnect(true);
|
|
57
76
|
logger.warn({ error: String(error?.message || error) }, "Realtime socket revalidation failed.");
|
|
@@ -68,10 +87,10 @@ function createRealtimeDelivery({ io, database = null, logger, authService = nul
|
|
|
68
87
|
}
|
|
69
88
|
|
|
70
89
|
async function deliverLocal({ eventName, payload, targets }) {
|
|
71
|
-
const
|
|
90
|
+
const checks = createPassChecks(authService, audiences);
|
|
72
91
|
await Promise.all(candidateSockets(targets).map(async (socket) => {
|
|
73
92
|
try {
|
|
74
|
-
const actor = await revalidate(socket,
|
|
93
|
+
const actor = await revalidate(socket, checks);
|
|
75
94
|
if (!socket.connected) return;
|
|
76
95
|
if (!targets.broadcastAllClients && !targets.rooms.some((room) => socket.rooms.has(room))) return;
|
|
77
96
|
if (typeof authService.realtime?.authorizeEvent === "function") {
|
|
@@ -99,8 +118,8 @@ function createRealtimeDelivery({ io, database = null, logger, authService = nul
|
|
|
99
118
|
function scheduleRevalidation() {
|
|
100
119
|
if (stopped) return;
|
|
101
120
|
revalidationTimer = setTimeout(async () => {
|
|
102
|
-
const
|
|
103
|
-
await Promise.all([...io.sockets.sockets.values()].map((socket) => revalidate(socket,
|
|
121
|
+
const checks = createPassChecks(authService, audiences);
|
|
122
|
+
await Promise.all([...io.sockets.sockets.values()].map((socket) => revalidate(socket, checks)));
|
|
104
123
|
scheduleRevalidation();
|
|
105
124
|
}, 30_000);
|
|
106
125
|
revalidationTimer.unref();
|
|
@@ -13,16 +13,25 @@ const logger = { debug() {}, warn() {} };
|
|
|
13
13
|
const arrival = (emitter, name) => once(emitter, name, { signal: AbortSignal.timeout(5_000) });
|
|
14
14
|
|
|
15
15
|
// Real sockets whose session cookies name their actors. `checks` lists each
|
|
16
|
-
// authentication's session cookie, so a test sees which sockets were checked
|
|
17
|
-
|
|
16
|
+
// authentication's session cookie, so a test sees which sockets were checked,
|
|
17
|
+
// `profileSyncs` those that asked the provider to sync the profile, and
|
|
18
|
+
// `sessionRefreshes` those that let it refresh the session. Sessions in
|
|
19
|
+
// `expired` answer that they need a refresh.
|
|
20
|
+
async function signedInSockets(t, { audiences = null, requireAuthentication = true } = {}) {
|
|
18
21
|
const httpServer = createServer();
|
|
19
22
|
const io = createSocketIoServer({ httpServer });
|
|
20
23
|
const sessions = new Map();
|
|
24
|
+
const expired = new Set();
|
|
21
25
|
const checks = [];
|
|
26
|
+
const profileSyncs = [];
|
|
27
|
+
const sessionRefreshes = [];
|
|
22
28
|
const authService = {
|
|
23
|
-
realtime: { requireAuthentication
|
|
24
|
-
async authenticateRequest(request) {
|
|
29
|
+
realtime: { requireAuthentication },
|
|
30
|
+
async authenticateRequest(request, { syncProfile = true, refreshSession = true } = {}) {
|
|
25
31
|
checks.push(request.cookies.session);
|
|
32
|
+
if (syncProfile) profileSyncs.push(request.cookies.session);
|
|
33
|
+
if (refreshSession) sessionRefreshes.push(request.cookies.session);
|
|
34
|
+
if (expired.has(request.cookies.session)) return { authenticated: false, refreshRequired: true };
|
|
26
35
|
const id = sessions.get(request.cookies.session);
|
|
27
36
|
return { authenticated: Boolean(id), actor: id ? { id } : null };
|
|
28
37
|
}
|
|
@@ -41,8 +50,12 @@ async function signedInSockets(t, { audiences = null } = {}) {
|
|
|
41
50
|
const url = `http://127.0.0.1:${httpServer.address().port}`;
|
|
42
51
|
return {
|
|
43
52
|
checks,
|
|
53
|
+
expired,
|
|
54
|
+
profileSyncs,
|
|
55
|
+
sessionRefreshes,
|
|
44
56
|
sessions,
|
|
45
57
|
io,
|
|
58
|
+
url,
|
|
46
59
|
publish: (audience, name = "item.changed") => delivery.handle({
|
|
47
60
|
type: "entity.changed", realtime: { audience, event: name, payload: { name } }
|
|
48
61
|
}),
|
|
@@ -215,15 +228,69 @@ test("membership changes and revoked logins take effect on the very next publish
|
|
|
215
228
|
assert.equal(bob.received.length, received);
|
|
216
229
|
});
|
|
217
230
|
|
|
231
|
+
test("revalidation never syncs profiles, and one actor's logins share a room lookup", async (t) => {
|
|
232
|
+
let roomLookups = 0;
|
|
233
|
+
const audiences = createAudienceContributors();
|
|
234
|
+
audiences.register({
|
|
235
|
+
id: "test.memberships",
|
|
236
|
+
resolveActorScopes() {
|
|
237
|
+
roomLookups += 1;
|
|
238
|
+
return [{ kind: "workspace", id: 7 }];
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
const f = await signedInSockets(t, { audiences });
|
|
242
|
+
f.sessions.set("alice-laptop", "1").set("alice-phone", "1");
|
|
243
|
+
const laptop = await f.connect("alice-laptop");
|
|
244
|
+
const phone = await f.connect("alice-phone");
|
|
245
|
+
assert.deepEqual(f.profileSyncs.sort(), ["alice-laptop", "alice-phone"], "Connecting signs in as usual");
|
|
246
|
+
|
|
247
|
+
f.profileSyncs.length = 0;
|
|
248
|
+
roomLookups = 0;
|
|
249
|
+
const reached = [arrival(laptop.client, "item.changed"), arrival(phone.client, "item.changed")];
|
|
250
|
+
await f.publish({ workspaceId: 7 });
|
|
251
|
+
await Promise.all(reached);
|
|
252
|
+
assert.deepEqual(f.profileSyncs, []);
|
|
253
|
+
assert.equal(roomLookups, 1, "Both logins are Alice, so one publish looks up her rooms once");
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("a login whose session needs refreshing is dropped and refused until the client refreshes", async (t) => {
|
|
257
|
+
const f = await signedInSockets(t, { requireAuthentication: false });
|
|
258
|
+
f.sessions.set("alice", "1");
|
|
259
|
+
const alice = await f.connect("alice");
|
|
260
|
+
|
|
261
|
+
f.expired.add("alice");
|
|
262
|
+
const disconnected = arrival(alice.client, "disconnect");
|
|
263
|
+
await f.publish({ userId: 1 });
|
|
264
|
+
assert.equal((await disconnected)[0], "io server disconnect");
|
|
265
|
+
assert.equal(alice.received.length, 0);
|
|
266
|
+
|
|
267
|
+
// Even with sign-in optional, the handshake refuses instead of connecting
|
|
268
|
+
// without Alice's rooms, so the client refreshes and reconnects.
|
|
269
|
+
const again = connect(f.url, { transports: ["websocket"], extraHeaders: { Cookie: "session=alice" }, reconnection: false });
|
|
270
|
+
t.after(() => again.disconnect());
|
|
271
|
+
const [refused] = await arrival(again, "connect_error");
|
|
272
|
+
assert.equal(refused.data.code, "SESSION_REFRESH_REQUIRED");
|
|
273
|
+
|
|
274
|
+
f.expired.delete("alice");
|
|
275
|
+
again.connect();
|
|
276
|
+
await arrival(again, "connect");
|
|
277
|
+
assert.deepEqual(f.sessionRefreshes, [], "A socket never lets the provider refresh a session it cannot store");
|
|
278
|
+
});
|
|
279
|
+
|
|
218
280
|
test("idle socket sessions are revalidated without an application event, once per login", async (t) => {
|
|
219
281
|
t.mock.timers.enable({ apis: ["setTimeout"] });
|
|
220
282
|
const tab = () => ({ data: { actorId: "1" }, handshake: {}, disconnect() { this.connected = false; }, connected: true });
|
|
221
283
|
const sockets = [tab(), tab()];
|
|
222
284
|
const io = { emit() {}, to() {}, on() {}, off() {}, sockets: { sockets: new Map(sockets.map((socket, index) => [`s${index}`, socket])) } };
|
|
223
285
|
let checks = 0;
|
|
286
|
+
let options = null;
|
|
224
287
|
const delivery = createRealtimeDelivery({ io, logger, authService: {
|
|
225
288
|
realtime: { requireAuthentication: true },
|
|
226
|
-
async authenticateRequest() {
|
|
289
|
+
async authenticateRequest(_request, passOptions) {
|
|
290
|
+
checks += 1;
|
|
291
|
+
options = passOptions;
|
|
292
|
+
return { authenticated: false };
|
|
293
|
+
}
|
|
227
294
|
} });
|
|
228
295
|
t.after(() => delivery.stop());
|
|
229
296
|
delivery.start();
|
|
@@ -231,6 +298,7 @@ test("idle socket sessions are revalidated without an application event, once pe
|
|
|
231
298
|
await new Promise((resolve) => setImmediate(resolve));
|
|
232
299
|
assert.deepEqual(sockets.map((socket) => socket.connected), [false, false]);
|
|
233
300
|
assert.equal(checks, 1, "Identical handshakes share one check per pass");
|
|
301
|
+
assert.deepEqual(options, { syncProfile: false, refreshSession: false });
|
|
234
302
|
});
|
|
235
303
|
|
|
236
304
|
test("delivery failures never reject a committed mutation and room unions emit once", async () => {
|
|
@@ -117,6 +117,27 @@ test("server disconnect recovery backs off middleware rejection and resets after
|
|
|
117
117
|
assert.equal(socket.connect.mock.callCount(), 8);
|
|
118
118
|
});
|
|
119
119
|
|
|
120
|
+
test("recovery runs its prepare step, such as a session refresh, before reconnecting", async (t) => {
|
|
121
|
+
t.mock.timers.enable({ apis: ["setTimeout"] });
|
|
122
|
+
const socket = Object.assign(new EventEmitter(), {
|
|
123
|
+
active: false,
|
|
124
|
+
connected: false,
|
|
125
|
+
connect: t.mock.fn(),
|
|
126
|
+
io: { reconnection: () => true }
|
|
127
|
+
});
|
|
128
|
+
let finishPreparing;
|
|
129
|
+
const prepareReconnect = t.mock.fn(() => new Promise((resolve) => { finishPreparing = resolve; }));
|
|
130
|
+
t.after(attachSocketConnectionRecovery(socket, { prepareReconnect }));
|
|
131
|
+
|
|
132
|
+
socket.emit("connect_error", new Error("Session refresh required."));
|
|
133
|
+
t.mock.timers.tick(1_000);
|
|
134
|
+
assert.equal(prepareReconnect.mock.callCount(), 1);
|
|
135
|
+
assert.equal(socket.connect.mock.callCount(), 0, "The handshake waits for the refreshed session");
|
|
136
|
+
finishPreparing();
|
|
137
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
138
|
+
assert.equal(socket.connect.mock.callCount(), 1);
|
|
139
|
+
});
|
|
140
|
+
|
|
120
141
|
test("recovery leaves transport retries and intentional disconnects to Socket.IO", (t) => {
|
|
121
142
|
const { socket, detach } = recoverySocket(t);
|
|
122
143
|
socket.active = true;
|
|
@@ -347,9 +347,13 @@ test("socket audience bootstrap authenticates explicitly and joins contributed s
|
|
|
347
347
|
}
|
|
348
348
|
});
|
|
349
349
|
let connectionHandler = null;
|
|
350
|
+
let authenticationMiddleware = null;
|
|
350
351
|
const io = {
|
|
351
352
|
on(eventName, handler) {
|
|
352
353
|
if (eventName === "connection") connectionHandler = handler;
|
|
354
|
+
},
|
|
355
|
+
use(handler) {
|
|
356
|
+
authenticationMiddleware = handler;
|
|
353
357
|
}
|
|
354
358
|
};
|
|
355
359
|
const authenticateCalls = [];
|
|
@@ -374,6 +378,9 @@ test("socket audience bootstrap authenticates explicitly and joins contributed s
|
|
|
374
378
|
data: {},
|
|
375
379
|
join(room) { joinedRooms.push(room); }
|
|
376
380
|
};
|
|
381
|
+
const handshakeErrors = [];
|
|
382
|
+
await authenticationMiddleware(socket, (error) => handshakeErrors.push(error));
|
|
383
|
+
assert.deepEqual(handshakeErrors, [undefined]);
|
|
377
384
|
await connectionHandler(socket);
|
|
378
385
|
assert.deepEqual(authenticateCalls, [{
|
|
379
386
|
cookies: { session: "abc123", theme: "dark" },
|
|
@@ -516,7 +523,9 @@ async function startRealtimeClient({ mobile = null } = {}) {
|
|
|
516
523
|
|
|
517
524
|
test("RealtimeClientProvider publishes one explicit client capability", async () => {
|
|
518
525
|
const fixture = await startRealtimeClient();
|
|
519
|
-
assert.deepEqual(Object.keys(fixture.realtime).sort(), [
|
|
526
|
+
assert.deepEqual(Object.keys(fixture.realtime).sort(), [
|
|
527
|
+
"config", "dispose", "initialize", "onServedClientBuild", "setBeforeReconnect", "socket"
|
|
528
|
+
]);
|
|
520
529
|
assert.equal(fixture.realtime.config.url, "");
|
|
521
530
|
assert.equal(fixture.registrations.has("realtime.web.connection.indicator"), true);
|
|
522
531
|
assert.equal(
|