@jskit-ai/realtime 0.2.24 → 0.2.26
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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- A publish to a user or workspace room no longer reads the memberships of every online actor. It asks each audience contributor about the actors of the sockets outside its target rooms only for those rooms' scopes, `resolveActorsScopes(actorIds, { scopes })`, and keeps only those rooms of the answer, while the actors already inside are looked up in full for their checks. A contributor that ignores `scopes` still answers correctly. Before, every write action read the memberships of every signed-in socket's actor, so the cost grew with everyone online, also for an event to one user.
|
|
5
6
|
- Since 0.2.20, an event a background job publishes (an action run with no request, so no request workspace) no longer reads its audience's `audienceUsers` in the `workspaceId` its payload names: with no workspace or user in its scope it reads with no context, so an audience over an owned table reaches nobody. Run the job's action with the workspace in its context (`context.workspace`, or a visibility scope naming it) so its audience reads there.
|
|
6
7
|
- Since 0.2.20, `all_workspace_users` on an event written on a customer's or a public surface reaches the users of the workspace the request resolved; before, such an event reached no workspace's users. An audience meant for the writer alone names the user instead.
|
|
7
8
|
- **Security.** An event written on a customer's or a public surface (scoped globally) reads its audience's `audienceUsers` in the workspace the request resolved, which the kernel adds to its global scope as `workspaceId` (`forWorkspace` of `realtime.audience-scope`), and `all_workspace_users` reaches that workspace. The payload never chooses the scope: an app's payload may copy an input id, and reading in a workspace the payload named let one event read in, and reach, another workspace. Before, such an event read with no context, so an audience over an owned table reached no customer. A scope that names a workspace or a user keeps it, and an event with neither reads with no context.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/realtime",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"socket.io-client": "^4.8.3"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@jskit-ai/kernel": "0.2.
|
|
27
|
+
"@jskit-ai/kernel": "0.2.18",
|
|
28
28
|
"vue": "^3.5.13"
|
|
29
29
|
},
|
|
30
30
|
"description": "Thin, generic realtime runtime wrappers for socket.io server and client.",
|
|
@@ -71,11 +71,28 @@ function roomForScope(scope) {
|
|
|
71
71
|
return null;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* The scope whose room roomForScope() names, or null for a room it never names.
|
|
76
|
+
* @param {string} room
|
|
77
|
+
*/
|
|
78
|
+
function scopeForRoom(room) {
|
|
79
|
+
const text = String(room);
|
|
80
|
+
const workspaceUser = /^workspace:([^:]+):user:([^:]+)$/u.exec(text);
|
|
81
|
+
const workspace = /^workspace:([^:]+)$/u.exec(text);
|
|
82
|
+
const user = /^user:([^:]+)$/u.exec(text);
|
|
83
|
+
const scope = workspaceUser
|
|
84
|
+
? { kind: "workspace_user", workspaceId: workspaceUser[1], userId: workspaceUser[2] }
|
|
85
|
+
: workspace ? { kind: "workspace", id: workspace[1] } : user ? { kind: "user", id: user[1] } : null;
|
|
86
|
+
return scope && roomForScope(scope) === text ? scope : null;
|
|
87
|
+
}
|
|
88
|
+
|
|
74
89
|
/**
|
|
75
90
|
* Packages that know which scopes an actor belongs to (for example workspace
|
|
76
91
|
* memberships) contribute them here; realtime only turns scopes into rooms.
|
|
77
92
|
* A contributor answers for many actors at once, so a publish that may let
|
|
78
93
|
* every online actor into a room asks each contributor once, not once per actor.
|
|
94
|
+
* A publish asks only about its target rooms: it names their scopes, so a
|
|
95
|
+
* contributor reads only those, and keeps only those rooms of each answer.
|
|
79
96
|
*/
|
|
80
97
|
function createAudienceContributors() {
|
|
81
98
|
const contributors = new Map();
|
|
@@ -105,22 +122,28 @@ function createAudienceContributors() {
|
|
|
105
122
|
|
|
106
123
|
/**
|
|
107
124
|
* Every actor's contributed rooms, with one call per contributor whatever
|
|
108
|
-
* the number of actors.
|
|
125
|
+
* the number of actors. With `rooms`, only which of those rooms each actor
|
|
126
|
+
* is contributed into: the contributors are asked about those rooms' scopes
|
|
127
|
+
* only, and none is asked when no contributor could name one of them.
|
|
109
128
|
* @type {ActorsRoomsResolver}
|
|
110
129
|
*/
|
|
111
|
-
async function resolveActorsRooms(actorIds) {
|
|
130
|
+
async function resolveActorsRooms(actorIds, { rooms: asked } = {}) {
|
|
112
131
|
const roomsByActorId = new Map();
|
|
113
132
|
for (const actorId of actorIds) roomsByActorId.set(actorId, new Set());
|
|
114
133
|
if (roomsByActorId.size === 0) return roomsByActorId;
|
|
134
|
+
const askedRooms = asked === undefined ? null : new Set(asked);
|
|
135
|
+
const scopes = askedRooms === null ? null : [...askedRooms].map(scopeForRoom).filter(Boolean);
|
|
136
|
+
if (scopes !== null && scopes.length === 0) return roomsByActorId;
|
|
115
137
|
for (const { id, resolveActorsScopes } of contributors.values()) {
|
|
116
|
-
const
|
|
138
|
+
const actors = [...roomsByActorId.keys()];
|
|
139
|
+
const scopesByActorId = await (scopes === null ? resolveActorsScopes(actors) : resolveActorsScopes(actors, { scopes }));
|
|
117
140
|
if (!(scopesByActorId instanceof Map)) {
|
|
118
141
|
throw new TypeError(`Realtime audience contributor "${id}" must resolve to a Map of actor ids to scopes.`);
|
|
119
142
|
}
|
|
120
143
|
for (const [actorId, rooms] of roomsByActorId) {
|
|
121
144
|
for (const scope of flattenEntries(scopesByActorId.get(actorId))) {
|
|
122
145
|
const room = roomForScope(scope);
|
|
123
|
-
if (room) rooms.add(room);
|
|
146
|
+
if (room && (askedRooms === null || askedRooms.has(room))) rooms.add(room);
|
|
124
147
|
}
|
|
125
148
|
}
|
|
126
149
|
}
|
|
@@ -43,7 +43,7 @@ function sharedWithinPass(load, keyOf) {
|
|
|
43
43
|
* one authentication (the auth service sees only the request), and sockets of
|
|
44
44
|
* one actor share its contributed rooms. The rooms of the actors a pass names
|
|
45
45
|
* up front are one bulk resolution, which the sockets it revalidates then
|
|
46
|
-
* share, so no actor's rooms are looked up twice in a pass. Revalidation asks
|
|
46
|
+
* share, so no actor's rooms are looked up in full twice in a pass. Revalidation asks
|
|
47
47
|
* only who is signed in, so the provider reads the app profile instead of
|
|
48
48
|
* synchronizing it, and never refreshes a session the socket could not store.
|
|
49
49
|
* @param {AuthService} authService
|
|
@@ -129,9 +129,14 @@ function createRealtimeDelivery({ io, logger, authService, audiences: { mayContr
|
|
|
129
129
|
* A socket outside every target room can enter one only by revalidation
|
|
130
130
|
* joining a contributed room, and the rooms it joins are exactly its actor's
|
|
131
131
|
* (a socket whose login now names another actor is disconnected instead). So
|
|
132
|
-
* a socket outside is checked only when its actor
|
|
133
|
-
*
|
|
134
|
-
*
|
|
132
|
+
* a socket outside is checked only when its actor is contributed into a
|
|
133
|
+
* target room, or when it has no actor yet and may sign in now. One bulk
|
|
134
|
+
* question asks the contributors about the outside actors and the target
|
|
135
|
+
* rooms only, so a publish to one workspace reads that workspace's
|
|
136
|
+
* memberships among them, not every online actor's, and a publish to user
|
|
137
|
+
* rooms reads none when no contributor names user rooms. The inside sockets'
|
|
138
|
+
* actors are looked up in full meanwhile, for their revalidation. Any other
|
|
139
|
+
* socket is left to the 30-second pass.
|
|
135
140
|
* @param {{ broadcastAllClients: boolean, rooms: readonly string[] }} targets
|
|
136
141
|
* @param {ReturnType<typeof createPassChecks>} checks
|
|
137
142
|
*/
|
|
@@ -142,21 +147,20 @@ function createRealtimeDelivery({ io, logger, authService, audiences: { mayContr
|
|
|
142
147
|
const contributable = targets.rooms.filter((room) => mayContribute(room));
|
|
143
148
|
if (contributable.length === 0) return sockets.filter(inTargets);
|
|
144
149
|
|
|
145
|
-
|
|
146
|
-
checks.lookUpActorsRooms(
|
|
147
|
-
const
|
|
150
|
+
const inside = sockets.filter(inTargets);
|
|
151
|
+
checks.lookUpActorsRooms(inside.map(rememberedActorId).filter(Boolean));
|
|
152
|
+
const outsideActorIds = new Set(sockets.filter((socket) => !inTargets(socket)).map(rememberedActorId).filter(Boolean));
|
|
153
|
+
let enteringRooms = null;
|
|
154
|
+
try {
|
|
155
|
+
enteringRooms = await resolveActorsRooms([...outsideActorIds], { rooms: contributable });
|
|
156
|
+
} catch {
|
|
157
|
+
// Every outside socket is revalidated instead, which meets a failing lookup and disconnects it, as before.
|
|
158
|
+
}
|
|
159
|
+
return sockets.filter((socket) => {
|
|
148
160
|
if (inTargets(socket)) return true;
|
|
149
161
|
const actorId = rememberedActorId(socket);
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const rooms = await checks.audiences.resolveActorRooms(actorId);
|
|
153
|
-
return contributable.some((room) => rooms.has(room));
|
|
154
|
-
} catch {
|
|
155
|
-
// Revalidating it meets the same failure and disconnects it, as before.
|
|
156
|
-
return true;
|
|
157
|
-
}
|
|
158
|
-
}));
|
|
159
|
-
return sockets.filter((_socket, index) => entering[index]);
|
|
162
|
+
return !actorId || enteringRooms === null || enteringRooms.get(actorId).size > 0;
|
|
163
|
+
});
|
|
160
164
|
}
|
|
161
165
|
|
|
162
166
|
async function deliverLocal({ eventName, payload, targets }) {
|
package/src/server/types.js
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
* @typedef {(actorId: string) => Promise<Set<string>>} ActorRoomsResolver
|
|
8
8
|
*/
|
|
9
9
|
/**
|
|
10
|
-
* The rooms each actor's contributed scopes name, every asked actor included.
|
|
11
|
-
* @typedef {(actorIds: string[]) => Promise<Map<string, Set<string>>>} ActorsRoomsResolver
|
|
10
|
+
* The rooms each actor's contributed scopes name, every asked actor included; with `rooms`, only those of them.
|
|
11
|
+
* @typedef {(actorIds: string[], question?: { rooms?: readonly string[] }) => Promise<Map<string, Set<string>>>} ActorsRoomsResolver
|
|
12
12
|
*/
|
|
13
13
|
/** @typedef {ReturnType<typeof import("./realtimeAudience.js").createAudienceContributors>} AudienceContributors */
|
|
14
14
|
/**
|
|
@@ -27,8 +27,10 @@
|
|
|
27
27
|
* as a list of one.
|
|
28
28
|
* @typedef {object} RealtimeAudienceContributor
|
|
29
29
|
* @property {string} id
|
|
30
|
-
* @property {(actorIds: string[]) => Map<string, unknown> | Promise<Map<string, unknown>>} resolveActorsScopes
|
|
31
|
-
* Returns, or resolves to, each actor's scopes by actor id; an actor it leaves out has none.
|
|
30
|
+
* @property {(actorIds: string[], question?: { scopes: object[] }) => Map<string, unknown> | Promise<Map<string, unknown>>} resolveActorsScopes
|
|
31
|
+
* Returns, or resolves to, each actor's scopes by actor id; an actor it leaves out has none. With `question.scopes`,
|
|
32
|
+
* the caller asks only whether the actors belong to those scopes, so the contributor may read only them; any other
|
|
33
|
+
* scope it answers is left out.
|
|
32
34
|
*/
|
|
33
35
|
|
|
34
36
|
export {};
|
|
@@ -259,17 +259,19 @@ test("revalidation never syncs profiles, and one actor's logins share a room loo
|
|
|
259
259
|
assert.equal(roomLookups, 1, "Both logins are Alice, so one publish looks up her rooms once");
|
|
260
260
|
});
|
|
261
261
|
|
|
262
|
-
test("a workspace event checks only its members' logins,
|
|
262
|
+
test("a workspace event checks only its members' logins, asking about the 10 or 200 other actors once and only about its room", async (t) => {
|
|
263
263
|
const members = new Set(["1", "2"]);
|
|
264
264
|
const lookups = [];
|
|
265
265
|
const audiences = createAudienceContributors();
|
|
266
266
|
audiences.register({
|
|
267
267
|
id: "test.memberships",
|
|
268
|
-
|
|
269
|
-
|
|
268
|
+
// It answers every scope of each actor, asked about some or not: realtime keeps only the rooms it asked about.
|
|
269
|
+
resolveActorsScopes(actorIds, question) {
|
|
270
|
+
lookups.push(question ? { actors: actorIds.length, scopes: question.scopes } : { actors: actorIds.length });
|
|
270
271
|
return scopesByActor(actorIds, (actorId) => [{ kind: "workspace", id: members.has(actorId) ? 7 : 8 }]);
|
|
271
272
|
}
|
|
272
273
|
});
|
|
274
|
+
const aboutRoom = [{ kind: "workspace", id: "7" }];
|
|
273
275
|
const f = await signedInSockets(t, { audiences });
|
|
274
276
|
f.sessions.set("alice", "1").set("bob", "2");
|
|
275
277
|
const alice = await f.connect("alice");
|
|
@@ -295,9 +297,9 @@ test("a workspace event checks only its members' logins, with one room lookup am
|
|
|
295
297
|
publishes.push({ checks: [...f.checks].sort(), lookups: [...lookups] });
|
|
296
298
|
}
|
|
297
299
|
assert.deepEqual(publishes, [
|
|
298
|
-
{ checks: ["alice", "bob"], lookups: [
|
|
299
|
-
{ checks: ["alice", "bob"], lookups: [
|
|
300
|
-
], "Only the members' logins are checked
|
|
300
|
+
{ checks: ["alice", "bob"], lookups: [{ actors: 2 }, { actors: 10, scopes: aboutRoom }] },
|
|
301
|
+
{ checks: ["alice", "bob"], lookups: [{ actors: 2 }, { actors: 200, scopes: aboutRoom }] }
|
|
302
|
+
], "Only the members' logins are checked and their rooms looked up; the others are asked about workspace 7 only, in one question");
|
|
301
303
|
|
|
302
304
|
// Events on one connection arrive in order, so no outsider received the publishes.
|
|
303
305
|
const markers = outsiders.map(({ client }) => arrival(client, "marker"));
|
|
@@ -305,7 +307,7 @@ test("a workspace event checks only its members' logins, with one room lookup am
|
|
|
305
307
|
await Promise.all(markers);
|
|
306
308
|
assert.equal(outsiders.every(({ received }) => received.length === 0), true);
|
|
307
309
|
|
|
308
|
-
// Granted: an outsider joins the very next publish, through the same single
|
|
310
|
+
// Granted: an outsider joins the very next publish, through the same single question; only its rooms are then looked up.
|
|
309
311
|
members.add("105");
|
|
310
312
|
f.checks.length = 0;
|
|
311
313
|
lookups.length = 0;
|
|
@@ -313,7 +315,36 @@ test("a workspace event checks only its members' logins, with one room lookup am
|
|
|
313
315
|
await f.publish({ workspaceId: 7 });
|
|
314
316
|
await granted;
|
|
315
317
|
assert.deepEqual(f.checks.sort(), ["alice", "bob", "outsider-5"]);
|
|
316
|
-
assert.deepEqual(lookups, [
|
|
318
|
+
assert.deepEqual(lookups, [{ actors: 2 }, { actors: 200, scopes: aboutRoom }, { actors: 1 }]);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("a user event asks about the other actors only for the user's room, and a contributor of no user rooms reads nothing", async (t) => {
|
|
322
|
+
const reads = [];
|
|
323
|
+
const audiences = createAudienceContributors();
|
|
324
|
+
audiences.register({
|
|
325
|
+
id: "test.memberships",
|
|
326
|
+
// As workspaces-core's contributor: it names workspace rooms only, so a question about other scopes reads nothing.
|
|
327
|
+
resolveActorsScopes(actorIds, question) {
|
|
328
|
+
const scopes = question?.scopes;
|
|
329
|
+
reads.push(scopes ? { actors: actorIds.length, scopes } : { actors: actorIds.length });
|
|
330
|
+
if (scopes && !scopes.some((scope) => scope.kind === "workspace")) return new Map();
|
|
331
|
+
return scopesByActor(actorIds, () => [{ kind: "workspace", id: 7 }]);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
const f = await signedInSockets(t, { audiences });
|
|
335
|
+
f.sessions.set("alice", "1").set("bob", "2").set("carol", "3");
|
|
336
|
+
const alice = await f.connect("alice");
|
|
337
|
+
await f.connect("bob");
|
|
338
|
+
await f.connect("carol");
|
|
339
|
+
|
|
340
|
+
f.checks.length = 0;
|
|
341
|
+
reads.length = 0;
|
|
342
|
+
const reached = arrival(alice.client, "item.changed");
|
|
343
|
+
await f.publish({ userId: 1 });
|
|
344
|
+
await reached;
|
|
345
|
+
assert.deepEqual(f.checks, ["alice"]);
|
|
346
|
+
assert.deepEqual(reads, [{ actors: 1 }, { actors: 2, scopes: [{ kind: "user", id: "1" }] }],
|
|
347
|
+
"Alice's rooms are looked up for her check; Bob and Carol are asked only about user 1's room");
|
|
317
348
|
});
|
|
318
349
|
|
|
319
350
|
test("a login whose session needs refreshing is dropped and refused until the client refreshes", async (t) => {
|