@jskit-ai/realtime 0.2.2 → 0.2.3
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
CHANGED
|
@@ -19,14 +19,27 @@ 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.
|
|
36
|
+
function createPassChecks(authService, audiences) {
|
|
37
|
+
return {
|
|
38
|
+
authenticateRequest: sharedWithinPass(
|
|
39
|
+
(request) => authService.authenticateRequest(request, { syncProfile: false }),
|
|
40
|
+
JSON.stringify
|
|
41
|
+
),
|
|
42
|
+
audiences: audiences && { resolveActorRooms: sharedWithinPass((actorId) => audiences.resolveActorRooms(actorId)) }
|
|
30
43
|
};
|
|
31
44
|
}
|
|
32
45
|
|
|
@@ -49,9 +62,14 @@ function createRealtimeDelivery({ io, database = null, logger, authService = nul
|
|
|
49
62
|
let revalidationTimer = null;
|
|
50
63
|
let stopped = false;
|
|
51
64
|
|
|
52
|
-
async function revalidate(socket,
|
|
65
|
+
async function revalidate(socket, checks) {
|
|
53
66
|
try {
|
|
54
|
-
return await revalidateSocket({
|
|
67
|
+
return await revalidateSocket({
|
|
68
|
+
socket,
|
|
69
|
+
authService,
|
|
70
|
+
audiences: checks.audiences,
|
|
71
|
+
authenticateRequest: checks.authenticateRequest
|
|
72
|
+
});
|
|
55
73
|
} catch (error) {
|
|
56
74
|
socket.disconnect(true);
|
|
57
75
|
logger.warn({ error: String(error?.message || error) }, "Realtime socket revalidation failed.");
|
|
@@ -68,10 +86,10 @@ function createRealtimeDelivery({ io, database = null, logger, authService = nul
|
|
|
68
86
|
}
|
|
69
87
|
|
|
70
88
|
async function deliverLocal({ eventName, payload, targets }) {
|
|
71
|
-
const
|
|
89
|
+
const checks = createPassChecks(authService, audiences);
|
|
72
90
|
await Promise.all(candidateSockets(targets).map(async (socket) => {
|
|
73
91
|
try {
|
|
74
|
-
const actor = await revalidate(socket,
|
|
92
|
+
const actor = await revalidate(socket, checks);
|
|
75
93
|
if (!socket.connected) return;
|
|
76
94
|
if (!targets.broadcastAllClients && !targets.rooms.some((room) => socket.rooms.has(room))) return;
|
|
77
95
|
if (typeof authService.realtime?.authorizeEvent === "function") {
|
|
@@ -99,8 +117,8 @@ function createRealtimeDelivery({ io, database = null, logger, authService = nul
|
|
|
99
117
|
function scheduleRevalidation() {
|
|
100
118
|
if (stopped) return;
|
|
101
119
|
revalidationTimer = setTimeout(async () => {
|
|
102
|
-
const
|
|
103
|
-
await Promise.all([...io.sockets.sockets.values()].map((socket) => revalidate(socket,
|
|
120
|
+
const checks = createPassChecks(authService, audiences);
|
|
121
|
+
await Promise.all([...io.sockets.sockets.values()].map((socket) => revalidate(socket, checks)));
|
|
104
122
|
scheduleRevalidation();
|
|
105
123
|
}, 30_000);
|
|
106
124
|
revalidationTimer.unref();
|
|
@@ -13,16 +13,19 @@ 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
|
|
16
|
+
// authentication's session cookie, so a test sees which sockets were checked,
|
|
17
|
+
// and `profileSyncs` those that asked the provider to sync the profile.
|
|
17
18
|
async function signedInSockets(t, { audiences = null } = {}) {
|
|
18
19
|
const httpServer = createServer();
|
|
19
20
|
const io = createSocketIoServer({ httpServer });
|
|
20
21
|
const sessions = new Map();
|
|
21
22
|
const checks = [];
|
|
23
|
+
const profileSyncs = [];
|
|
22
24
|
const authService = {
|
|
23
25
|
realtime: { requireAuthentication: true },
|
|
24
|
-
async authenticateRequest(request) {
|
|
26
|
+
async authenticateRequest(request, { syncProfile = true } = {}) {
|
|
25
27
|
checks.push(request.cookies.session);
|
|
28
|
+
if (syncProfile) profileSyncs.push(request.cookies.session);
|
|
26
29
|
const id = sessions.get(request.cookies.session);
|
|
27
30
|
return { authenticated: Boolean(id), actor: id ? { id } : null };
|
|
28
31
|
}
|
|
@@ -41,6 +44,7 @@ async function signedInSockets(t, { audiences = null } = {}) {
|
|
|
41
44
|
const url = `http://127.0.0.1:${httpServer.address().port}`;
|
|
42
45
|
return {
|
|
43
46
|
checks,
|
|
47
|
+
profileSyncs,
|
|
44
48
|
sessions,
|
|
45
49
|
io,
|
|
46
50
|
publish: (audience, name = "item.changed") => delivery.handle({
|
|
@@ -215,15 +219,45 @@ test("membership changes and revoked logins take effect on the very next publish
|
|
|
215
219
|
assert.equal(bob.received.length, received);
|
|
216
220
|
});
|
|
217
221
|
|
|
222
|
+
test("revalidation never syncs profiles, and one actor's logins share a room lookup", async (t) => {
|
|
223
|
+
let roomLookups = 0;
|
|
224
|
+
const audiences = createAudienceContributors();
|
|
225
|
+
audiences.register({
|
|
226
|
+
id: "test.memberships",
|
|
227
|
+
resolveActorScopes() {
|
|
228
|
+
roomLookups += 1;
|
|
229
|
+
return [{ kind: "workspace", id: 7 }];
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
const f = await signedInSockets(t, { audiences });
|
|
233
|
+
f.sessions.set("alice-laptop", "1").set("alice-phone", "1");
|
|
234
|
+
const laptop = await f.connect("alice-laptop");
|
|
235
|
+
const phone = await f.connect("alice-phone");
|
|
236
|
+
assert.deepEqual(f.profileSyncs.sort(), ["alice-laptop", "alice-phone"], "Connecting signs in as usual");
|
|
237
|
+
|
|
238
|
+
f.profileSyncs.length = 0;
|
|
239
|
+
roomLookups = 0;
|
|
240
|
+
const reached = [arrival(laptop.client, "item.changed"), arrival(phone.client, "item.changed")];
|
|
241
|
+
await f.publish({ workspaceId: 7 });
|
|
242
|
+
await Promise.all(reached);
|
|
243
|
+
assert.deepEqual(f.profileSyncs, []);
|
|
244
|
+
assert.equal(roomLookups, 1, "Both logins are Alice, so one publish looks up her rooms once");
|
|
245
|
+
});
|
|
246
|
+
|
|
218
247
|
test("idle socket sessions are revalidated without an application event, once per login", async (t) => {
|
|
219
248
|
t.mock.timers.enable({ apis: ["setTimeout"] });
|
|
220
249
|
const tab = () => ({ data: { actorId: "1" }, handshake: {}, disconnect() { this.connected = false; }, connected: true });
|
|
221
250
|
const sockets = [tab(), tab()];
|
|
222
251
|
const io = { emit() {}, to() {}, on() {}, off() {}, sockets: { sockets: new Map(sockets.map((socket, index) => [`s${index}`, socket])) } };
|
|
223
252
|
let checks = 0;
|
|
253
|
+
let options = null;
|
|
224
254
|
const delivery = createRealtimeDelivery({ io, logger, authService: {
|
|
225
255
|
realtime: { requireAuthentication: true },
|
|
226
|
-
async authenticateRequest() {
|
|
256
|
+
async authenticateRequest(_request, passOptions) {
|
|
257
|
+
checks += 1;
|
|
258
|
+
options = passOptions;
|
|
259
|
+
return { authenticated: false };
|
|
260
|
+
}
|
|
227
261
|
} });
|
|
228
262
|
t.after(() => delivery.stop());
|
|
229
263
|
delivery.start();
|
|
@@ -231,6 +265,7 @@ test("idle socket sessions are revalidated without an application event, once pe
|
|
|
231
265
|
await new Promise((resolve) => setImmediate(resolve));
|
|
232
266
|
assert.deepEqual(sockets.map((socket) => socket.connected), [false, false]);
|
|
233
267
|
assert.equal(checks, 1, "Identical handshakes share one check per pass");
|
|
268
|
+
assert.deepEqual(options, { syncProfile: false });
|
|
234
269
|
});
|
|
235
270
|
|
|
236
271
|
test("delivery failures never reject a committed mutation and room unions emit once", async () => {
|