@jskit-ai/realtime 0.1.244 → 0.1.245
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 +20 -10
- package/patterns/realtime-application/example/package.json +2 -2
- package/src/client/RealtimeClientProvider.js +0 -6
- package/src/server/RealtimeProvider.js +15 -8
- package/src/server/realtimeAudience.js +56 -37
- package/src/server/realtimeDelivery.js +2 -2
- package/test/entrypoints.boundary.test.js +19 -0
- package/test/providerRuntime.test.js +73 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/realtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.245",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -20,8 +20,7 @@
|
|
|
20
20
|
"socket.io-client": "^4.8.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@jskit-ai/kernel": "0.1.
|
|
24
|
-
"@jskit-ai/shell-web": "0.1.251",
|
|
23
|
+
"@jskit-ai/kernel": "0.1.246",
|
|
25
24
|
"vue": "^3.5.13"
|
|
26
25
|
},
|
|
27
26
|
"description": "Thin, generic realtime runtime wrappers for socket.io server and client.",
|
|
@@ -30,7 +29,7 @@
|
|
|
30
29
|
"capabilities": {
|
|
31
30
|
"provides": [
|
|
32
31
|
"runtime.realtime",
|
|
33
|
-
"
|
|
32
|
+
"client.realtime"
|
|
34
33
|
],
|
|
35
34
|
"requires": [
|
|
36
35
|
"runtime.config",
|
|
@@ -70,16 +69,28 @@
|
|
|
70
69
|
"apiSummary": {
|
|
71
70
|
"surfaces": [
|
|
72
71
|
{
|
|
73
|
-
"subpath": "./server",
|
|
72
|
+
"subpath": "./server/RealtimeProvider",
|
|
74
73
|
"summary": "Exports the explicit RealtimeProvider capability provider."
|
|
75
74
|
},
|
|
75
|
+
{
|
|
76
|
+
"subpath": "./server/runtime",
|
|
77
|
+
"summary": "Exports the Socket.IO server and Redis adapter runtime helpers."
|
|
78
|
+
},
|
|
76
79
|
{
|
|
77
80
|
"subpath": "./client",
|
|
78
|
-
"summary": "Exports RealtimeClientProvider
|
|
81
|
+
"summary": "Exports RealtimeClientProvider and resolveRealtimeClientConfig."
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
"subpath": "./client/RealtimeClientProvider",
|
|
85
|
+
"summary": "Exports RealtimeClientProvider and resolveRealtimeClientConfig."
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"subpath": "./client/composables/useRealtimeEvent",
|
|
89
|
+
"summary": "Exports useRealtimeEvent and useRealtimeSocket; listeners are owned by their Vue effect scope."
|
|
79
90
|
},
|
|
80
91
|
{
|
|
81
|
-
"subpath": "./client/
|
|
82
|
-
"summary": "Exports
|
|
92
|
+
"subpath": "./client/runtime",
|
|
93
|
+
"summary": "Exports createSocketIoClient and disconnectSocketIoClient."
|
|
83
94
|
}
|
|
84
95
|
],
|
|
85
96
|
"capabilities": {
|
|
@@ -87,8 +98,7 @@
|
|
|
87
98
|
"runtime.realtime"
|
|
88
99
|
],
|
|
89
100
|
"client": [
|
|
90
|
-
"client.realtime"
|
|
91
|
-
"realtime.web.connection.indicator"
|
|
101
|
+
"client.realtime"
|
|
92
102
|
]
|
|
93
103
|
}
|
|
94
104
|
},
|
|
@@ -6,11 +6,6 @@ import RealtimeConnectionIndicator from "./components/RealtimeConnectionIndicato
|
|
|
6
6
|
import { createSocketIoClient, disconnectSocketIoClient } from "./runtime.js";
|
|
7
7
|
import { attachSocketConnectionRecovery } from "./connectionRecovery.js";
|
|
8
8
|
|
|
9
|
-
const REALTIME_RUNTIME_CLIENT_API = Object.freeze({
|
|
10
|
-
createSocketIoClient,
|
|
11
|
-
disconnectSocketIoClient
|
|
12
|
-
});
|
|
13
|
-
|
|
14
9
|
function resolveRealtimeClientConfig({ env = {}, mobile = null } = {}) {
|
|
15
10
|
const appConfig = normalizeObject(getClientAppConfig());
|
|
16
11
|
const realtime = normalizeObject(appConfig.realtime);
|
|
@@ -111,7 +106,6 @@ function createRealtimeClient({ config, loggerInput } = {}) {
|
|
|
111
106
|
}
|
|
112
107
|
|
|
113
108
|
return Object.freeze({
|
|
114
|
-
...REALTIME_RUNTIME_CLIENT_API,
|
|
115
109
|
config,
|
|
116
110
|
dispose,
|
|
117
111
|
initialize,
|
|
@@ -3,6 +3,7 @@ import { createProviderLogger } from "@jskit-ai/kernel/shared/support/providerLo
|
|
|
3
3
|
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
4
4
|
import { createRealtimeDelivery } from "./realtimeDelivery.js";
|
|
5
5
|
import {
|
|
6
|
+
createAudienceContributors,
|
|
6
7
|
createSocketRequest,
|
|
7
8
|
realtimeAuthenticationRequired,
|
|
8
9
|
registerSocketAudienceBootstrap,
|
|
@@ -45,7 +46,7 @@ function createRealtimeCapability({ io }) {
|
|
|
45
46
|
async function readRequest() {
|
|
46
47
|
const request = createSocketRequest(socket);
|
|
47
48
|
const actor = await revalidateSocket({
|
|
48
|
-
socket, request, authService: state.authService,
|
|
49
|
+
socket, request, authService: state.authService, audiences: state.audiences
|
|
49
50
|
});
|
|
50
51
|
if (!socket.connected || (!actor && realtimeAuthenticationRequired(state.authService))) {
|
|
51
52
|
throw Object.assign(new Error("Authentication required."), { statusCode: 401 });
|
|
@@ -92,6 +93,14 @@ function createRealtimeCapability({ io }) {
|
|
|
92
93
|
for (const socket of io.sockets.sockets.values()) attach(socket);
|
|
93
94
|
return stop;
|
|
94
95
|
},
|
|
96
|
+
// A package that knows which scopes (kind "user", "workspace" or
|
|
97
|
+
// "workspace_user") an actor belongs to registers them during setup; sockets
|
|
98
|
+
// join, and on revalidation leave, exactly the rooms its scopes name.
|
|
99
|
+
registerAudienceContributor(contributor) {
|
|
100
|
+
const state = stateByCapability.get(capability);
|
|
101
|
+
if (!state) throw new Error("Realtime runtime state is unavailable.");
|
|
102
|
+
state.audiences.register(contributor);
|
|
103
|
+
},
|
|
95
104
|
diagnostics() {
|
|
96
105
|
const state = stateByCapability.get(capability);
|
|
97
106
|
return Object.freeze({
|
|
@@ -117,8 +126,7 @@ const RealtimeProvider = defineProvider({
|
|
|
117
126
|
},
|
|
118
127
|
optional: {
|
|
119
128
|
authService: "auth.service",
|
|
120
|
-
database: "runtime.database"
|
|
121
|
-
workspaces: "workspaces.core"
|
|
129
|
+
database: "runtime.database"
|
|
122
130
|
},
|
|
123
131
|
provides: {
|
|
124
132
|
realtime: "runtime.realtime"
|
|
@@ -129,8 +137,8 @@ const RealtimeProvider = defineProvider({
|
|
|
129
137
|
const realtime = createRealtimeCapability({ io });
|
|
130
138
|
stateByCapability.set(realtime, {
|
|
131
139
|
authenticationRequired: false,
|
|
140
|
+
audiences: createAudienceContributors(),
|
|
132
141
|
authService: null,
|
|
133
|
-
workspaces: null,
|
|
134
142
|
connectionListeners: new Set(),
|
|
135
143
|
delivery: null,
|
|
136
144
|
io,
|
|
@@ -139,20 +147,19 @@ const RealtimeProvider = defineProvider({
|
|
|
139
147
|
});
|
|
140
148
|
return { realtime };
|
|
141
149
|
},
|
|
142
|
-
async boot({ authService, database, env, events
|
|
150
|
+
async boot({ authService, database, env, events }, { outputs }) {
|
|
143
151
|
const state = stateByCapability.get(outputs.realtime);
|
|
144
152
|
if (!state) throw new Error("Realtime runtime state is unavailable.");
|
|
145
153
|
state.authService = authService;
|
|
146
|
-
state.workspaces = workspaces;
|
|
147
154
|
state.delivery = createRealtimeDelivery({
|
|
148
|
-
io: state.io, database, logger: state.providerLogger, authService,
|
|
155
|
+
io: state.io, database, logger: state.providerLogger, authService, audiences: state.audiences
|
|
149
156
|
});
|
|
150
157
|
state.authenticationRequired = realtimeAuthenticationRequired(authService);
|
|
151
158
|
registerSocketAudienceBootstrap({
|
|
152
159
|
io: state.io,
|
|
153
160
|
logger: state.providerLogger,
|
|
154
161
|
authService,
|
|
155
|
-
|
|
162
|
+
audiences: state.audiences
|
|
156
163
|
});
|
|
157
164
|
state.redisConnection = await configureSocketIoRedisAdapter(state.io, {
|
|
158
165
|
logger: state.providerLogger,
|
|
@@ -26,17 +26,6 @@ function roomForWorkspaceUser(workspaceId, userId) {
|
|
|
26
26
|
return `workspace:${String(workspaceId || "").trim()}:user:${String(userId || "").trim()}`;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
function workspaceAudienceRooms(workspaceIds, actorId) {
|
|
30
|
-
const rooms = [];
|
|
31
|
-
for (const rawId of normalizeArray(workspaceIds)) {
|
|
32
|
-
const workspaceId = normalizeRecordId(rawId, { fallback: null });
|
|
33
|
-
if (workspaceId) {
|
|
34
|
-
rooms.push(roomForWorkspace(workspaceId), roomForWorkspaceUser(workspaceId, actorId));
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return rooms;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
29
|
function resolveScopeWorkspaceId(scope = {}) {
|
|
41
30
|
const kind = normalizeText(scope?.kind).toLowerCase();
|
|
42
31
|
if (kind === "workspace") return normalizeRecordId(scope.id, { fallback: null });
|
|
@@ -52,6 +41,47 @@ function resolveScopeUserId(scope = {}) {
|
|
|
52
41
|
return normalizeRecordId(scope?.userId, { fallback: null });
|
|
53
42
|
}
|
|
54
43
|
|
|
44
|
+
// The single mapping from a kernel event scope ({ kind: "user" | "workspace" |
|
|
45
|
+
// "workspace_user" }) to its Socket.IO room; null when the scope names no room.
|
|
46
|
+
function roomForScope(scope = {}) {
|
|
47
|
+
const kind = normalizeText(scope?.kind).toLowerCase();
|
|
48
|
+
const workspaceId = resolveScopeWorkspaceId(scope);
|
|
49
|
+
const userId = resolveScopeUserId(scope);
|
|
50
|
+
if (kind === "workspace" && workspaceId) return roomForWorkspace(workspaceId);
|
|
51
|
+
if (kind === "workspace_user" && workspaceId && userId) return roomForWorkspaceUser(workspaceId, userId);
|
|
52
|
+
if (kind === "user" && userId) return roomForUser(userId);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Packages that know which scopes an actor belongs to (for example workspace
|
|
57
|
+
// memberships) contribute them here; realtime only turns scopes into rooms.
|
|
58
|
+
function createAudienceContributors() {
|
|
59
|
+
const contributors = new Map();
|
|
60
|
+
return Object.freeze({
|
|
61
|
+
register({ id, resolveActorScopes } = {}) {
|
|
62
|
+
const contributorId = normalizeText(id);
|
|
63
|
+
if (!contributorId) throw new TypeError("Realtime audience contributors require an id.");
|
|
64
|
+
if (typeof resolveActorScopes !== "function") {
|
|
65
|
+
throw new TypeError(`Realtime audience contributor "${contributorId}" requires resolveActorScopes().`);
|
|
66
|
+
}
|
|
67
|
+
if (contributors.has(contributorId)) {
|
|
68
|
+
throw new Error(`Realtime audience contributor "${contributorId}" is already registered.`);
|
|
69
|
+
}
|
|
70
|
+
contributors.set(contributorId, resolveActorScopes);
|
|
71
|
+
},
|
|
72
|
+
async resolveActorRooms(actorId) {
|
|
73
|
+
const rooms = new Set();
|
|
74
|
+
for (const resolveActorScopes of contributors.values()) {
|
|
75
|
+
for (const scope of normalizeArray(await resolveActorScopes(actorId))) {
|
|
76
|
+
const room = roomForScope(scope);
|
|
77
|
+
if (room) rooms.add(room);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return rooms;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
55
85
|
function applyAudiencePreset(preset, { event, rooms, flags, logger }) {
|
|
56
86
|
const normalized = normalizeText(preset).toLowerCase();
|
|
57
87
|
if (!normalized || normalized === "none") return;
|
|
@@ -75,14 +105,7 @@ function applyAudiencePreset(preset, { event, rooms, flags, logger }) {
|
|
|
75
105
|
return;
|
|
76
106
|
}
|
|
77
107
|
if (normalized === "event_scope") {
|
|
78
|
-
|
|
79
|
-
const workspaceId = resolveScopeWorkspaceId(event?.scope);
|
|
80
|
-
const userId = resolveScopeUserId(event?.scope);
|
|
81
|
-
if (scopeKind === "workspace" && workspaceId) rooms.add(roomForWorkspace(workspaceId));
|
|
82
|
-
else if (scopeKind === "workspace_user" && workspaceId && userId) {
|
|
83
|
-
rooms.add(roomForWorkspaceUser(workspaceId, userId));
|
|
84
|
-
} else if (scopeKind === "user" && userId) rooms.add(roomForUser(userId));
|
|
85
|
-
else rooms.add(ALL_USERS_ROOM);
|
|
108
|
+
rooms.add(roomForScope(event?.scope) || ALL_USERS_ROOM);
|
|
86
109
|
return;
|
|
87
110
|
}
|
|
88
111
|
logger.warn({ audience: normalized }, "Realtime ignored an unknown audience preset.");
|
|
@@ -247,7 +270,16 @@ function registerRequiredSocketAuthentication({ io, logger, authService }) {
|
|
|
247
270
|
});
|
|
248
271
|
}
|
|
249
272
|
|
|
250
|
-
function
|
|
273
|
+
async function joinContributedRooms(socket, actorId, audiences) {
|
|
274
|
+
const rooms = audiences ? await audiences.resolveActorRooms(actorId) : new Set();
|
|
275
|
+
for (const room of normalizeArray(socket.data?.contributedRooms)) {
|
|
276
|
+
if (!rooms.has(room)) await socket.leave(room);
|
|
277
|
+
}
|
|
278
|
+
for (const room of rooms) await socket.join(room);
|
|
279
|
+
socket.data.contributedRooms = [...rooms];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function registerSocketAudienceBootstrap({ io, logger, authService = null, audiences = null }) {
|
|
251
283
|
if (typeof io?.on !== "function") return;
|
|
252
284
|
registerRequiredSocketAuthentication({ io, logger, authService });
|
|
253
285
|
io.on("connection", async (socket) => {
|
|
@@ -259,20 +291,14 @@ function registerSocketAudienceBootstrap({ io, logger, authService = null, works
|
|
|
259
291
|
rememberSocketActorId(socket, actorId);
|
|
260
292
|
socket.join(ALL_USERS_ROOM);
|
|
261
293
|
socket.join(roomForUser(actorId));
|
|
262
|
-
|
|
263
|
-
const workspaceIds = typeof repository?.listActiveWorkspaceIdsByUserId === "function"
|
|
264
|
-
? await repository.listActiveWorkspaceIdsByUserId(actorId)
|
|
265
|
-
: [];
|
|
266
|
-
for (const room of workspaceAudienceRooms(workspaceIds, actorId)) {
|
|
267
|
-
socket.join(room);
|
|
268
|
-
}
|
|
294
|
+
await joinContributedRooms(socket, actorId, audiences);
|
|
269
295
|
} catch (error) {
|
|
270
296
|
logger.warn({ error: String(error?.message || error) }, "Realtime socket audience bootstrap failed.");
|
|
271
297
|
}
|
|
272
298
|
});
|
|
273
299
|
}
|
|
274
300
|
|
|
275
|
-
async function revalidateSocket({ socket, authService,
|
|
301
|
+
async function revalidateSocket({ socket, authService, audiences = null, request }) {
|
|
276
302
|
const actor = await resolveSocketActor(authService, socket, request);
|
|
277
303
|
const previousActorId = socket.data?.actorId;
|
|
278
304
|
if ((!actor && realtimeAuthenticationRequired(authService)) ||
|
|
@@ -283,19 +309,12 @@ async function revalidateSocket({ socket, authService, workspaces = null, reques
|
|
|
283
309
|
if (!actor) return null;
|
|
284
310
|
|
|
285
311
|
rememberSocketActorId(socket, actor.id);
|
|
286
|
-
|
|
287
|
-
if (typeof repository?.listActiveWorkspaceIdsByUserId !== "function") return actor;
|
|
288
|
-
|
|
289
|
-
const workspaceIds = await repository.listActiveWorkspaceIdsByUserId(actor.id);
|
|
290
|
-
const rooms = new Set(workspaceAudienceRooms(workspaceIds, actor.id));
|
|
291
|
-
for (const room of socket.rooms) {
|
|
292
|
-
if (room.startsWith("workspace:") && !rooms.has(room)) await socket.leave(room);
|
|
293
|
-
}
|
|
294
|
-
for (const room of rooms) await socket.join(room);
|
|
312
|
+
await joinContributedRooms(socket, actor.id, audiences);
|
|
295
313
|
return actor;
|
|
296
314
|
}
|
|
297
315
|
|
|
298
316
|
export {
|
|
317
|
+
createAudienceContributors,
|
|
299
318
|
createSocketRequest,
|
|
300
319
|
realtimeAuthenticationRequired,
|
|
301
320
|
revalidateSocket,
|
|
@@ -19,7 +19,7 @@ function publicRealtimePayload(event) {
|
|
|
19
19
|
return Object.freeze({ ...(payload || {}), ...canonical });
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
function createRealtimeDelivery({ io, database = null, logger, authService = null,
|
|
22
|
+
function createRealtimeDelivery({ io, database = null, logger, authService = null, audiences = null }) {
|
|
23
23
|
if (!io || typeof io.emit !== "function" || typeof io.to !== "function") {
|
|
24
24
|
throw new TypeError("Realtime delivery requires a Socket.IO server.");
|
|
25
25
|
}
|
|
@@ -40,7 +40,7 @@ function createRealtimeDelivery({ io, database = null, logger, authService = nul
|
|
|
40
40
|
|
|
41
41
|
async function revalidate(socket) {
|
|
42
42
|
try {
|
|
43
|
-
return await revalidateSocket({ socket, authService,
|
|
43
|
+
return await revalidateSocket({ socket, authService, audiences });
|
|
44
44
|
} catch (error) {
|
|
45
45
|
socket.disconnect(true);
|
|
46
46
|
logger.warn({ error: String(error?.message || error) }, "Realtime socket revalidation failed.");
|
|
@@ -26,3 +26,22 @@ test("client runtime entrypoint exports client-only helpers", () => {
|
|
|
26
26
|
assert.equal(typeof clientRuntimeApi.createSocketIoClient, "function");
|
|
27
27
|
assert.equal(typeof clientRuntimeApi.disconnectSocketIoClient, "function");
|
|
28
28
|
});
|
|
29
|
+
|
|
30
|
+
test("package metadata describes exactly the exported subpaths and provided capabilities", async () => {
|
|
31
|
+
const { readFile } = await import("node:fs/promises");
|
|
32
|
+
const packageJson = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"));
|
|
33
|
+
const apiSummary = packageJson.jskit.metadata.apiSummary;
|
|
34
|
+
assert.deepEqual(
|
|
35
|
+
apiSummary.surfaces.map((surface) => surface.subpath).sort(),
|
|
36
|
+
Object.keys(packageJson.exports).sort()
|
|
37
|
+
);
|
|
38
|
+
assert.deepEqual(apiSummary.capabilities, {
|
|
39
|
+
server: Object.values(serverApi.RealtimeProvider.provides),
|
|
40
|
+
client: Object.values(clientApi.RealtimeClientProvider.provides)
|
|
41
|
+
});
|
|
42
|
+
assert.deepEqual(
|
|
43
|
+
[...packageJson.jskit.capabilities.provides].sort(),
|
|
44
|
+
[...apiSummary.capabilities.server, ...apiSummary.capabilities.client].sort()
|
|
45
|
+
);
|
|
46
|
+
assert.equal(Object.hasOwn(packageJson.peerDependencies, "@jskit-ai/shell-web"), false);
|
|
47
|
+
});
|
|
@@ -8,19 +8,27 @@ import { CLIENT_APP_CONFIG_GLOBAL_KEY, setClientAppConfig } from "../../kernel/c
|
|
|
8
8
|
import { RealtimeClientProvider } from "../src/client/RealtimeClientProvider.js";
|
|
9
9
|
import { RealtimeProvider } from "../src/server/RealtimeProvider.js";
|
|
10
10
|
import { createSocketIoClient } from "../src/client/runtime.js";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
createAudienceContributors,
|
|
13
|
+
registerSocketAudienceBootstrap,
|
|
14
|
+
revalidateSocket
|
|
15
|
+
} from "../src/server/realtimeAudience.js";
|
|
12
16
|
import { createRealtimeDelivery } from "../src/server/realtimeDelivery.js";
|
|
13
17
|
import { attachDeferredAuthService, createDeferredAuthService } from "../../auth-core/src/server/deferredAuthService.js";
|
|
14
18
|
|
|
15
19
|
const logger = Object.freeze({ debug() {}, info() {}, warn() {}, error() {} });
|
|
16
20
|
|
|
17
|
-
async function connectionRequestFixture(t, authService = null) {
|
|
21
|
+
async function connectionRequestFixture(t, authService = null, audienceContributor = null) {
|
|
18
22
|
const http = createServer();
|
|
19
23
|
const connections = [];
|
|
20
24
|
const clients = [];
|
|
21
25
|
let realtime;
|
|
22
26
|
const probe = defineProvider({ id: "test.connection.request", requires: { realtime: "runtime.realtime" },
|
|
23
|
-
setup(dependencies) {
|
|
27
|
+
setup(dependencies) {
|
|
28
|
+
realtime = dependencies.realtime;
|
|
29
|
+
if (audienceContributor) realtime.registerAudienceContributor(audienceContributor);
|
|
30
|
+
return {};
|
|
31
|
+
} });
|
|
24
32
|
const runtime = createCapabilityRuntime({ providers: [EventProvider, RealtimeProvider, probe], inputs: {
|
|
25
33
|
"runtime.config": {}, "runtime.env": {}, "runtime.fastify": { server: http }, "runtime.logger": logger,
|
|
26
34
|
...(authService ? { "auth.service": authService } : {})
|
|
@@ -99,6 +107,19 @@ test("connection request access revalidates required hosted sessions and retires
|
|
|
99
107
|
assert.ok(reads.every(value => value === "fixture-session"));
|
|
100
108
|
});
|
|
101
109
|
|
|
110
|
+
test("RealtimeProvider joins the rooms a registered audience contributor names", async t => {
|
|
111
|
+
const f = await connectionRequestFixture(t, {
|
|
112
|
+
async authenticateRequest() { return { authenticated: true, actor: { id: "42" } }; }
|
|
113
|
+
}, {
|
|
114
|
+
id: "test.memberships",
|
|
115
|
+
resolveActorScopes: (actorId) => [{ kind: "workspace_user", workspaceId: "7", userId: actorId }]
|
|
116
|
+
});
|
|
117
|
+
const { connection } = await f.connect();
|
|
118
|
+
assert.equal((await connection.readRequest()).user.id, "42");
|
|
119
|
+
assert.equal(connection.socket.rooms.has("workspace:7:user:42"), true);
|
|
120
|
+
assert.equal(connection.socket.rooms.has("user:42"), true);
|
|
121
|
+
});
|
|
122
|
+
|
|
102
123
|
test("RealtimeProvider waits for authentication boot before accessing the auth service", async () => {
|
|
103
124
|
const authService = createDeferredAuthService();
|
|
104
125
|
const authProvider = defineProvider({
|
|
@@ -234,7 +255,18 @@ test("realtime delivery resolves an explicit database-backed audience without ex
|
|
|
234
255
|
assert.equal(Object.hasOwn(io.emitted[0].payload, "realtime"), false);
|
|
235
256
|
});
|
|
236
257
|
|
|
237
|
-
test("socket audience bootstrap authenticates explicitly and joins
|
|
258
|
+
test("socket audience bootstrap authenticates explicitly and joins contributed scope rooms", async () => {
|
|
259
|
+
const audiences = createAudienceContributors();
|
|
260
|
+
audiences.register({
|
|
261
|
+
id: "test.memberships",
|
|
262
|
+
async resolveActorScopes(actorId) {
|
|
263
|
+
assert.equal(actorId, "9");
|
|
264
|
+
return [11, 12].flatMap((workspaceId) => [
|
|
265
|
+
{ kind: "workspace", id: workspaceId },
|
|
266
|
+
{ kind: "workspace_user", workspaceId, userId: actorId }
|
|
267
|
+
]);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
238
270
|
let connectionHandler = null;
|
|
239
271
|
const io = {
|
|
240
272
|
on(eventName, handler) {
|
|
@@ -251,16 +283,7 @@ test("socket audience bootstrap authenticates explicitly and joins actor workspa
|
|
|
251
283
|
return { authenticated: true, actor: { id: 9 } };
|
|
252
284
|
}
|
|
253
285
|
},
|
|
254
|
-
|
|
255
|
-
repositories: {
|
|
256
|
-
workspaceMemberships: {
|
|
257
|
-
async listActiveWorkspaceIdsByUserId(userId) {
|
|
258
|
-
assert.equal(userId, "9");
|
|
259
|
-
return [11, 12];
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
286
|
+
audiences
|
|
264
287
|
});
|
|
265
288
|
const joinedRooms = [];
|
|
266
289
|
const socket = {
|
|
@@ -288,6 +311,41 @@ test("socket audience bootstrap authenticates explicitly and joins actor workspa
|
|
|
288
311
|
]);
|
|
289
312
|
});
|
|
290
313
|
|
|
314
|
+
test("socket revalidation leaves only the contributed rooms the actor lost", async () => {
|
|
315
|
+
let memberships = [11, 12];
|
|
316
|
+
const audiences = createAudienceContributors();
|
|
317
|
+
audiences.register({
|
|
318
|
+
id: "test.memberships",
|
|
319
|
+
resolveActorScopes: () => memberships.map((id) => ({ kind: "workspace", id }))
|
|
320
|
+
});
|
|
321
|
+
assert.throws(() => audiences.register({ id: "test.memberships", resolveActorScopes() {} }), /already registered/);
|
|
322
|
+
assert.throws(() => audiences.register({ id: "test.other" }), /requires resolveActorScopes/);
|
|
323
|
+
const rooms = new Set(["clients", "users", "user:9", "workspace:legacy-app-room"]);
|
|
324
|
+
const socket = {
|
|
325
|
+
data: {},
|
|
326
|
+
rooms,
|
|
327
|
+
disconnect() { assert.fail("The same actor must stay connected."); },
|
|
328
|
+
async join(room) { rooms.add(room); },
|
|
329
|
+
async leave(room) { rooms.delete(room); }
|
|
330
|
+
};
|
|
331
|
+
const authService = {
|
|
332
|
+
async authenticateRequest() {
|
|
333
|
+
return { authenticated: true, actor: { id: 9 } };
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
await revalidateSocket({ socket, authService, audiences, request: {} });
|
|
338
|
+
assert.deepEqual(socket.data.contributedRooms, ["workspace:11", "workspace:12"]);
|
|
339
|
+
|
|
340
|
+
memberships = [12];
|
|
341
|
+
const actor = await revalidateSocket({ socket, authService, audiences, request: {} });
|
|
342
|
+
assert.equal(actor.id, "9");
|
|
343
|
+
assert.deepEqual([...rooms].sort(), [
|
|
344
|
+
"clients", "user:9", "users", "workspace:12", "workspace:legacy-app-room"
|
|
345
|
+
]);
|
|
346
|
+
assert.deepEqual(socket.data.contributedRooms, ["workspace:12"]);
|
|
347
|
+
});
|
|
348
|
+
|
|
291
349
|
test("socket audience bootstrap rejects unauthenticated handshakes when the auth service requires them", async () => {
|
|
292
350
|
let connectionHandler = null;
|
|
293
351
|
let authenticationMiddleware = null;
|
|
@@ -379,8 +437,7 @@ async function startRealtimeClient({ mobile = null } = {}) {
|
|
|
379
437
|
|
|
380
438
|
test("RealtimeClientProvider publishes one explicit client capability", async () => {
|
|
381
439
|
const fixture = await startRealtimeClient();
|
|
382
|
-
assert.
|
|
383
|
-
assert.equal(typeof fixture.realtime.disconnectSocketIoClient, "function");
|
|
440
|
+
assert.deepEqual(Object.keys(fixture.realtime).sort(), ["config", "dispose", "initialize", "socket"]);
|
|
384
441
|
assert.equal(fixture.realtime.config.url, "");
|
|
385
442
|
assert.equal(fixture.registrations.has("realtime.web.connection.indicator"), true);
|
|
386
443
|
assert.equal(
|