akanjs 3.3.0-rc.63 → 3.3.0-rc.65
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/client/csrTypes.ts +6 -2
- package/common/websocketContract.ts +2 -0
- package/package.json +1 -1
- package/server/akanApp.ts +3 -0
- package/server/akanServer.ts +4 -1
- package/server/index.ts +1 -0
- package/server/routeState.ts +1 -1
- package/server/routeTreeBuilder.ts +6 -0
- package/server/routing/apiRouter.ts +3 -0
- package/server/routing/appWsData.ts +4 -1
- package/server/routing/serverListener.ts +2 -0
- package/server/routing/wsCredentialRevocation.ts +55 -0
- package/server/rscPatchPlanner.ts +35 -9
- package/server/rscWorkerCache.ts +1 -1
- package/service/ipcTypes.ts +2 -1
- package/service/predefinedAdaptor/solidPubsub.adaptor.ts +16 -0
- package/service/predefinedAdaptor/websocket.adaptor.ts +56 -16
- package/types/client/csrTypes.d.ts +6 -2
- package/types/common/websocketContract.d.ts +1 -0
- package/types/server/index.d.ts +1 -0
- package/types/server/routing/appWsData.d.ts +1 -0
- package/types/server/routing/wsCredentialRevocation.d.ts +13 -0
- package/types/server/rscPatchPlanner.d.ts +1 -1
- package/types/server/rscWorkerCache.d.ts +1 -1
- package/types/service/ipcTypes.d.ts +5 -1
- package/types/service/predefinedAdaptor/solidPubsub.adaptor.d.ts +3 -1
- package/types/service/predefinedAdaptor/websocket.adaptor.d.ts +7 -0
package/client/csrTypes.ts
CHANGED
|
@@ -58,8 +58,10 @@ export interface PageConfig {
|
|
|
58
58
|
ssr?: SsrRenderMode;
|
|
59
59
|
/**
|
|
60
60
|
* Opt-in suffix-only RSC commits under `AKAN_PUBLIC_RSC_PARTIAL_COMMIT=1`: the page's `.head()` is plain `title`,
|
|
61
|
-
* `meta` and `link` tags, and the layouts above it
|
|
62
|
-
*
|
|
61
|
+
* `meta` and `link` tags, and the layouts above it draw on the server only from the args they declare. A move to a
|
|
62
|
+
* sibling page, or to another param or query of this page that no layout above declares with `.param()` or
|
|
63
|
+
* `.search()`, then renders the page alone and keeps those layouts as drawn; what follows the path in them is read
|
|
64
|
+
* on the client (`usePathCtx()`, `Link`'s `activeClassName`).
|
|
63
65
|
*/
|
|
64
66
|
rscPatchHeadSafe?: boolean;
|
|
65
67
|
/** `false` answers a `Link` prefetch of the page (on a layout, of every page under it) without rendering it, for a
|
|
@@ -180,6 +182,8 @@ export interface RouteRender {
|
|
|
180
182
|
getRouteDefinition?: () => PromiseOrObject<RouteDefinition | undefined>;
|
|
181
183
|
/** The query names the level reads; `undefined` for a legacy module, which receives every one. */
|
|
182
184
|
getSearchNames?: () => PromiseOrObject<string[] | undefined>;
|
|
185
|
+
/** The params the level reads beyond `lang`; `undefined` for a legacy module, which receives every one. */
|
|
186
|
+
getParamNames?: () => PromiseOrObject<string[] | undefined>;
|
|
183
187
|
}
|
|
184
188
|
export interface RouteViewport {
|
|
185
189
|
/** `viewport-fit=cover`: the page runs under the notch and home indicator, and `env(safe-area-inset-*)` measures them. */
|
|
@@ -32,6 +32,8 @@ export interface WebsocketHeartbeatAckData {
|
|
|
32
32
|
export const websocketCredentialContract = {
|
|
33
33
|
expiredCode: 4401,
|
|
34
34
|
expiredReason: "credential expired",
|
|
35
|
+
|
|
36
|
+
revokedReason: "credential revoked",
|
|
35
37
|
expiresAtOf: (jwt: string): number | null => {
|
|
36
38
|
try {
|
|
37
39
|
const { exp } = decodeJwtPayload<{ exp?: unknown }>(jwt);
|
package/package.json
CHANGED
package/server/akanApp.ts
CHANGED
|
@@ -313,6 +313,9 @@ export class AkanApp {
|
|
|
313
313
|
case "live.models":
|
|
314
314
|
this.#rooms.setLiveModels(idx, message.models);
|
|
315
315
|
return;
|
|
316
|
+
case "ws.revoke":
|
|
317
|
+
this.#children.fanoutToFederation(message, idx);
|
|
318
|
+
return;
|
|
316
319
|
case "metrics.report":
|
|
317
320
|
this.#children.updateMetrics(idx, message.metrics);
|
|
318
321
|
return;
|
package/server/akanServer.ts
CHANGED
|
@@ -32,6 +32,7 @@ import { BuiltinRoutes } from "./routing/builtinRoutes";
|
|
|
32
32
|
import { RouteHosts } from "./routing/routeHosts";
|
|
33
33
|
import { type AkanAppPrepared, ServerListener } from "./routing/serverListener";
|
|
34
34
|
import type { WebSurfaceHold } from "./routing/webSurfaceHold";
|
|
35
|
+
import { WsCredentialRevocation } from "./routing/wsCredentialRevocation";
|
|
35
36
|
import { ServerConfig } from "./serverConfig";
|
|
36
37
|
import { ServerMetrics } from "./serverMetrics";
|
|
37
38
|
import { SessionRefresh } from "./sessionRefresh";
|
|
@@ -525,12 +526,14 @@ export class AkanServer {
|
|
|
525
526
|
if (shouldListen) return this.listen();
|
|
526
527
|
return await this.#start.unwound(async () => {
|
|
527
528
|
const websocket = this.#di.getWebsocketAdaptor();
|
|
528
|
-
if (websocket)
|
|
529
|
+
if (websocket) {
|
|
529
530
|
SignalResolver.setLocalPublish(
|
|
530
531
|
(roomId, data) => this.#listener.publishLocal(roomId, data),
|
|
531
532
|
websocket,
|
|
532
533
|
this.#di.live,
|
|
533
534
|
);
|
|
535
|
+
WsCredentialRevocation.use(websocket);
|
|
536
|
+
}
|
|
534
537
|
this.status = "running";
|
|
535
538
|
if (isNoListenCommand) return this;
|
|
536
539
|
Logger.role = this.serverMode;
|
package/server/index.ts
CHANGED
package/server/routeState.ts
CHANGED
|
@@ -335,6 +335,11 @@ export class RouteTreeBuilder {
|
|
|
335
335
|
if (definition) return definition.args.filter((arg) => arg.kind === "search").map((arg) => arg.name);
|
|
336
336
|
return parseRouteModuleKey(key).isInternalRootLayout ? [] : undefined;
|
|
337
337
|
},
|
|
338
|
+
getParamNames: async () => {
|
|
339
|
+
const { definition } = await loadModule();
|
|
340
|
+
if (definition) return definition.args.filter((arg) => arg.kind === "param").map((arg) => arg.name);
|
|
341
|
+
return parseRouteModuleKey(key).isInternalRootLayout ? [] : undefined;
|
|
342
|
+
},
|
|
338
343
|
};
|
|
339
344
|
if (kind === "page") {
|
|
340
345
|
routeRender.getPageConfig = pageConfigOf;
|
|
@@ -368,6 +373,7 @@ export class RouteTreeBuilder {
|
|
|
368
373
|
return createElement(mod.default as never, { children } as never);
|
|
369
374
|
}) as RouteRender["render"],
|
|
370
375
|
getSearchNames: () => [],
|
|
376
|
+
getParamNames: () => [],
|
|
371
377
|
};
|
|
372
378
|
}
|
|
373
379
|
|
|
@@ -22,6 +22,7 @@ import { DevSurfacePolicy } from "./devSurfacePolicy";
|
|
|
22
22
|
import type { HostAllowlist } from "./hostAllowlist";
|
|
23
23
|
import { ServerLimits } from "./serverLimits";
|
|
24
24
|
import { WsCredentialExpiry } from "./wsCredentialExpiry";
|
|
25
|
+
import { WsCredentialRevocation } from "./wsCredentialRevocation";
|
|
25
26
|
import { WsUpgrade } from "./wsUpgrade";
|
|
26
27
|
|
|
27
28
|
export interface HmrStateSource {
|
|
@@ -183,6 +184,7 @@ export class ApiRouter {
|
|
|
183
184
|
}
|
|
184
185
|
SignalResolver.handleWsOpen(ws, registry);
|
|
185
186
|
WsCredentialExpiry.watch(ws);
|
|
187
|
+
WsCredentialRevocation.track(ws);
|
|
186
188
|
},
|
|
187
189
|
message: async (ws, message) => {
|
|
188
190
|
const data = ws.data as WsTaggedData | undefined;
|
|
@@ -249,6 +251,7 @@ export class ApiRouter {
|
|
|
249
251
|
return;
|
|
250
252
|
}
|
|
251
253
|
WsCredentialExpiry.unwatch(ws);
|
|
254
|
+
WsCredentialRevocation.untrack(ws);
|
|
252
255
|
SignalResolver.handleWsClose(ws, registry, live);
|
|
253
256
|
},
|
|
254
257
|
};
|
|
@@ -57,8 +57,11 @@ export class AppWsData {
|
|
|
57
57
|
return TrustedProxy.clientAddress(this.headers, ws.remoteAddress);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
credentialToken(): string | null {
|
|
61
|
+
return bearerTokenOf(this.headers.get("authorization")) ?? readSessionToken(this.jar).token ?? null;
|
|
62
|
+
}
|
|
60
63
|
credentialExpiresAt(): number | null {
|
|
61
|
-
const token =
|
|
64
|
+
const token = this.credentialToken();
|
|
62
65
|
return token ? websocketCredentialContract.expiresAtOf(token) : null;
|
|
63
66
|
}
|
|
64
67
|
constructor(headers: Headers) {
|
|
@@ -19,6 +19,7 @@ import { localRoomsOf } from "./localRooms";
|
|
|
19
19
|
import { RoomReplay } from "./roomReplay";
|
|
20
20
|
import { ServerLimits } from "./serverLimits";
|
|
21
21
|
import type { WebSurfaceHold } from "./webSurfaceHold";
|
|
22
|
+
import { WsCredentialRevocation } from "./wsCredentialRevocation";
|
|
22
23
|
|
|
23
24
|
export interface AkanAppPrepared {
|
|
24
25
|
routes: SignalRoutes["routes"];
|
|
@@ -255,6 +256,7 @@ export class ServerListener {
|
|
|
255
256
|
coalesces: (roomId) => SignalResolver.coalescesRoom(roomId),
|
|
256
257
|
});
|
|
257
258
|
SignalResolver.setLocalPublish(rooms.publish, websocket, this.#di.live, roomReplay, rooms.resync);
|
|
259
|
+
WsCredentialRevocation.use(websocket);
|
|
258
260
|
this.#localPublish = rooms.publish;
|
|
259
261
|
this.#roomReplay = roomReplay;
|
|
260
262
|
this.#websocket = websocket;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { decodeJwtPayload, websocketCredentialContract } from "akanjs/common";
|
|
2
|
+
import type { WebsocketAdaptor, WsCredentialClaims } from "akanjs/service";
|
|
3
|
+
import { AppWsData } from "./appWsData";
|
|
4
|
+
|
|
5
|
+
export class WsCredentialRevocation {
|
|
6
|
+
static readonly #sockets = new Set<Bun.ServerWebSocket<unknown>>();
|
|
7
|
+
static #websocket: WebsocketAdaptor | null = null;
|
|
8
|
+
|
|
9
|
+
static use(websocket: WebsocketAdaptor) {
|
|
10
|
+
WsCredentialRevocation.#websocket = websocket;
|
|
11
|
+
websocket.onRevocation?.((claims) => WsCredentialRevocation.#closeMatching(claims));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static track(ws: Bun.ServerWebSocket<unknown>) {
|
|
15
|
+
WsCredentialRevocation.#sockets.add(ws);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static untrack(ws: Bun.ServerWebSocket<unknown>) {
|
|
19
|
+
WsCredentialRevocation.#sockets.delete(ws);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Closes, on every server of the app, each socket whose credential carries one of the values under one of the
|
|
24
|
+
* claims, with the code a client renews and reconnects on. Its frames already answer to the account middleware;
|
|
25
|
+
* what this ends is the broadcasts to the rooms it joined before.
|
|
26
|
+
*/
|
|
27
|
+
static revoke(claims: WsCredentialClaims) {
|
|
28
|
+
if (!Object.values(claims).some((values) => values.length)) return;
|
|
29
|
+
WsCredentialRevocation.#closeMatching(claims);
|
|
30
|
+
WsCredentialRevocation.#websocket?.publishRevocation?.(claims);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static #closeMatching(claims: WsCredentialClaims) {
|
|
34
|
+
const revoked = Object.entries(claims).map(([claim, values]) => [claim, new Set(values)] as const);
|
|
35
|
+
for (const ws of WsCredentialRevocation.#sockets) {
|
|
36
|
+
const token = AppWsData.of(ws).credentialToken();
|
|
37
|
+
const payload = token ? WsCredentialRevocation.#payloadOf(token) : null;
|
|
38
|
+
if (!payload) continue;
|
|
39
|
+
const matches = revoked.some(([claim, values]) => {
|
|
40
|
+
const value = payload[claim];
|
|
41
|
+
return typeof value === "string" && values.has(value);
|
|
42
|
+
});
|
|
43
|
+
if (matches) ws.close(websocketCredentialContract.expiredCode, websocketCredentialContract.revokedReason);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static #payloadOf(token: string): Record<string, unknown> | null {
|
|
48
|
+
try {
|
|
49
|
+
const payload = decodeJwtPayload<unknown>(token);
|
|
50
|
+
return payload && typeof payload === "object" ? (payload as Record<string, unknown>) : null;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { PageState, PathRoute } from "akanjs/client";
|
|
2
|
-
import type
|
|
1
|
+
import type { PageState, PathRoute, RouteRender } from "akanjs/client";
|
|
2
|
+
import { type Logger, parseSearchParams } from "akanjs/common";
|
|
3
3
|
import type { ReactNode } from "react";
|
|
4
4
|
import { RouteHead } from "../client/route/RouteHead";
|
|
5
5
|
import { akanHeadSnapshotKey, createAkanLocaleAlternateHeadSnapshot, mergeAkanHeadSnapshots } from "./head";
|
|
@@ -21,7 +21,8 @@ import { resolveAkanRscHeadSafePatchDecision } from "./rscWorkerCache";
|
|
|
21
21
|
type RouteHeadLayers = Awaited<ReturnType<typeof RouteElementComposer.resolveHeadLayers>>;
|
|
22
22
|
type RouteMatch = { pathRoute: PathRoute; params: Record<string, string> };
|
|
23
23
|
type SearchParams = Record<string, string | string[]>;
|
|
24
|
-
type
|
|
24
|
+
type RouteRead = { params: Record<string, string>; searchParams: SearchParams };
|
|
25
|
+
type FrameChange = "params-changed" | "search-changed" | "layouts-changed" | "page-state-changed" | "frame-unknown";
|
|
25
26
|
|
|
26
27
|
export interface RouteHeadSnapshots {
|
|
27
28
|
layouts: AkanHeadSnapshotV1;
|
|
@@ -135,7 +136,7 @@ export class RscPatchPlanner {
|
|
|
135
136
|
const routeHeadMounted = routeHeadKey !== undefined;
|
|
136
137
|
const frameChange =
|
|
137
138
|
pageConfig?.rscPatchHeadSafe === true && routeHeadMounted
|
|
138
|
-
? await this.#frameChangeOf(url, match, current, patchDecision.patch.patchStartIndex)
|
|
139
|
+
? await this.#frameChangeOf(url, match, searchParams, current, patchDecision.patch.patchStartIndex)
|
|
139
140
|
: undefined;
|
|
140
141
|
let admittedHead: ResolvedRouteHead | undefined;
|
|
141
142
|
if (pageConfig?.rscPatchHeadSafe === true && routeHeadMounted && !frameChange) {
|
|
@@ -183,20 +184,27 @@ export class RscPatchPlanner {
|
|
|
183
184
|
async #frameChangeOf(
|
|
184
185
|
url: URL,
|
|
185
186
|
match: RouteMatch,
|
|
187
|
+
searchParams: SearchParams,
|
|
186
188
|
current: AkanRouterStateV1 | null,
|
|
187
189
|
patchStartIndex: number,
|
|
188
190
|
): Promise<FrameChange | undefined> {
|
|
189
191
|
const currentUrl = current && URL.canParse(current.href) ? new URL(current.href) : null;
|
|
190
192
|
const currentMatch = currentUrl ? RouteTreeBuilder.match(currentUrl.pathname, this.#props.routes()) : null;
|
|
191
|
-
if (!currentMatch) return "frame-unknown";
|
|
192
|
-
const paramNames = new Set([...Object.keys(currentMatch.params), ...Object.keys(match.params)]);
|
|
193
|
-
if ([...paramNames].some((name) => currentMatch.params[name] !== match.params[name])) return "params-changed";
|
|
193
|
+
if (!currentUrl || !currentMatch) return "frame-unknown";
|
|
194
194
|
const keptLayouts = (route: PathRoute) =>
|
|
195
195
|
[...route.renderRootLayouts, ...route.renderLayouts].slice(0, patchStartIndex);
|
|
196
196
|
const [fromLayouts, toLayouts] = [keptLayouts(currentMatch.pathRoute), keptLayouts(match.pathRoute)];
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
const sameLayouts =
|
|
198
|
+
fromLayouts.length === toLayouts.length && fromLayouts.every((layout, index) => layout === toLayouts[index]);
|
|
199
|
+
if (currentMatch.params.lang !== match.params.lang) return "params-changed";
|
|
200
|
+
if (!sameLayouts) return "layouts-changed";
|
|
199
201
|
try {
|
|
202
|
+
const readChange = await RscPatchPlanner.#readChangeOf(
|
|
203
|
+
toLayouts,
|
|
204
|
+
{ params: currentMatch.params, searchParams: parseSearchParams(currentUrl.search) },
|
|
205
|
+
{ params: match.params, searchParams },
|
|
206
|
+
);
|
|
207
|
+
if (readChange) return readChange;
|
|
200
208
|
const basePath = this.#props.basePathOf(url);
|
|
201
209
|
const [from, to] = await Promise.all([
|
|
202
210
|
RouteElementComposer.resolveSsrFramePathRoute({ pathRoute: currentMatch.pathRoute, basePath }),
|
|
@@ -214,6 +222,24 @@ export class RscPatchPlanner {
|
|
|
214
222
|
}
|
|
215
223
|
}
|
|
216
224
|
|
|
225
|
+
static async #readChangeOf(
|
|
226
|
+
layouts: RouteRender[],
|
|
227
|
+
from: RouteRead,
|
|
228
|
+
to: RouteRead,
|
|
229
|
+
): Promise<"params-changed" | "search-changed" | undefined> {
|
|
230
|
+
const reads = await Promise.all(
|
|
231
|
+
layouts.map(async (layout) => ({
|
|
232
|
+
params: await layout.getParamNames?.(),
|
|
233
|
+
search: (await layout.getSearchNames?.()) ?? [],
|
|
234
|
+
})),
|
|
235
|
+
);
|
|
236
|
+
const everyParam = [...new Set([...Object.keys(from.params), ...Object.keys(to.params)])];
|
|
237
|
+
if (reads.some(({ params }) => (params ?? everyParam).some((name) => from.params[name] !== to.params[name])))
|
|
238
|
+
return "params-changed";
|
|
239
|
+
const moved = (name: string) => JSON.stringify(from.searchParams[name]) !== JSON.stringify(to.searchParams[name]);
|
|
240
|
+
return reads.some(({ search }) => search.some(moved)) ? "search-changed" : undefined;
|
|
241
|
+
}
|
|
242
|
+
|
|
217
243
|
static #keptFrameOf({ cache: _cache, ssr: _ssr, ...frame }: PageState) {
|
|
218
244
|
return frame;
|
|
219
245
|
}
|
package/server/rscWorkerCache.ts
CHANGED
|
@@ -281,7 +281,7 @@ export function resolveAkanRscHeadSafePatchDecision({
|
|
|
281
281
|
pageConfig?: PageConfig;
|
|
282
282
|
/** Whether the page the patch leaves from shows its head through `AkanRouteHead`, the only head a patch can swap. */
|
|
283
283
|
routeHeadMounted?: boolean;
|
|
284
|
-
frameChange?: "params-changed" | "layouts-changed" | "page-state-changed" | "frame-unknown";
|
|
284
|
+
frameChange?: "params-changed" | "search-changed" | "layouts-changed" | "page-state-changed" | "frame-unknown";
|
|
285
285
|
headSnapshot?: AkanHeadSnapshotV1;
|
|
286
286
|
/** Whether the target's layouts head differs from the one the page left from shows, which a patch would keep. */
|
|
287
287
|
layoutsHeadChanged?: boolean;
|
package/service/ipcTypes.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LogRecord } from "akanjs/common";
|
|
2
|
-
import type { LiveChange } from "./predefinedAdaptor/websocket.adaptor";
|
|
2
|
+
import type { LiveChange, WsCredentialClaims } from "./predefinedAdaptor/websocket.adaptor";
|
|
3
3
|
|
|
4
4
|
export type AkanChildRole = "all" | "federation" | "batch";
|
|
5
5
|
|
|
@@ -218,6 +218,7 @@ export type AkanIpcMessage =
|
|
|
218
218
|
| { type: "pubsub.snapshot.request" }
|
|
219
219
|
| { type: "pubsub.snapshot"; rooms: string[]; liveModels?: string[]; pid?: number }
|
|
220
220
|
| { type: "live.change"; change: LiveChange; origin?: string }
|
|
221
|
+
| { type: "ws.revoke"; claims: WsCredentialClaims; origin?: string }
|
|
221
222
|
/** Child → gateway: every model the child holds live rooms for. */
|
|
222
223
|
| { type: "live.models"; models: string[]; pid?: number }
|
|
223
224
|
| { type: "queue.enqueued"; queue: string; name: string; jobId: string }
|
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
LiveChange,
|
|
6
6
|
LiveChangeHandler,
|
|
7
7
|
WebsocketAdaptor,
|
|
8
|
+
WsCredentialClaims,
|
|
8
9
|
WsRedisEventHandler,
|
|
9
10
|
WsSocketData,
|
|
10
11
|
} from "./websocket.adaptor";
|
|
@@ -34,6 +35,7 @@ export class SolidPubSub
|
|
|
34
35
|
#lastAck = 0;
|
|
35
36
|
#eventHandler: WsRedisEventHandler | null = null;
|
|
36
37
|
#changeHandler: LiveChangeHandler | null = null;
|
|
38
|
+
#revocationHandler: ((claims: WsCredentialClaims) => void) | null = null;
|
|
37
39
|
#liveModels: string[] = [];
|
|
38
40
|
readonly #messageHandler = (message: unknown) => {
|
|
39
41
|
if (!message || typeof message !== "object") return;
|
|
@@ -43,6 +45,7 @@ export class SolidPubSub
|
|
|
43
45
|
data?: unknown;
|
|
44
46
|
origin?: string;
|
|
45
47
|
change?: LiveChange;
|
|
48
|
+
claims?: WsCredentialClaims;
|
|
46
49
|
ack?: number;
|
|
47
50
|
};
|
|
48
51
|
if (data.type === "pubsub.subscribed" && data.ack !== undefined) {
|
|
@@ -57,6 +60,10 @@ export class SolidPubSub
|
|
|
57
60
|
this.#changeHandler?.(data.change);
|
|
58
61
|
return;
|
|
59
62
|
}
|
|
63
|
+
if (data.type === "ws.revoke" && data.claims && data.origin !== this.serverId) {
|
|
64
|
+
this.#revocationHandler?.(data.claims);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
60
67
|
if (data.type === "pubsub.snapshot.request") {
|
|
61
68
|
sendAkanIpc({
|
|
62
69
|
type: "pubsub.snapshot",
|
|
@@ -77,6 +84,7 @@ export class SolidPubSub
|
|
|
77
84
|
this.#acks.clear();
|
|
78
85
|
this.#eventHandler = null;
|
|
79
86
|
this.#changeHandler = null;
|
|
87
|
+
this.#revocationHandler = null;
|
|
80
88
|
this.#liveModels = [];
|
|
81
89
|
this.#socketRooms.clear();
|
|
82
90
|
}
|
|
@@ -93,6 +101,14 @@ export class SolidPubSub
|
|
|
93
101
|
this.#changeHandler = handler;
|
|
94
102
|
}
|
|
95
103
|
|
|
104
|
+
publishRevocation(claims: WsCredentialClaims): void {
|
|
105
|
+
sendAkanIpc({ type: "ws.revoke", claims, origin: this.serverId });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
onRevocation(handler: (claims: WsCredentialClaims) => void): void {
|
|
109
|
+
this.#revocationHandler = handler;
|
|
110
|
+
}
|
|
111
|
+
|
|
96
112
|
reportLiveModels(models: string[]): void {
|
|
97
113
|
this.#liveModels = models;
|
|
98
114
|
sendAkanIpc({ type: "live.models", models, pid: process.pid });
|
|
@@ -18,6 +18,9 @@ export interface LiveChange {
|
|
|
18
18
|
}
|
|
19
19
|
export type LiveChangeHandler = (change: LiveChange) => void;
|
|
20
20
|
|
|
21
|
+
/** Claim name to values: a socket whose credential carries any one of them is closed. */
|
|
22
|
+
export type WsCredentialClaims = Record<string, string[]>;
|
|
23
|
+
|
|
21
24
|
export interface WebsocketPublishOption {
|
|
22
25
|
/** Only the newest frame for the room matters, so an undelivered older one may be dropped for it. */
|
|
23
26
|
coalesce?: boolean;
|
|
@@ -44,6 +47,9 @@ export interface WebsocketAdaptor {
|
|
|
44
47
|
onChange?(handler: LiveChangeHandler): void;
|
|
45
48
|
/** The models this server holds live rooms for, each time that changes, so changes to the rest pass it by. */
|
|
46
49
|
reportLiveModels?(models: string[]): void;
|
|
50
|
+
/** To the app's other servers, each closing its own sockets whose credential carries one of the claims. */
|
|
51
|
+
publishRevocation?(claims: WsCredentialClaims): void;
|
|
52
|
+
onRevocation?(handler: (claims: WsCredentialClaims) => void): void;
|
|
47
53
|
/** Resolves once what another server publishes to the room reaches this one, or sooner if that link is down or stalls. */
|
|
48
54
|
joinRoom(ws: Bun.ServerWebSocket<unknown>, room: string, option?: WebsocketJoinOption): Promise<void>;
|
|
49
55
|
leaveRoom(ws: Bun.ServerWebSocket<unknown>, room: string): Promise<void>;
|
|
@@ -67,6 +73,13 @@ interface HeldRoom {
|
|
|
67
73
|
channel: string;
|
|
68
74
|
}
|
|
69
75
|
|
|
76
|
+
interface RedisHandlers {
|
|
77
|
+
event: WsRedisEventHandler | null;
|
|
78
|
+
change: LiveChangeHandler | null;
|
|
79
|
+
recovery: ((rooms: string[]) => void) | null;
|
|
80
|
+
revocation: ((claims: WsCredentialClaims) => void) | null;
|
|
81
|
+
}
|
|
82
|
+
|
|
70
83
|
interface ChannelHold {
|
|
71
84
|
rooms: number;
|
|
72
85
|
reply?: Promise<void>;
|
|
@@ -92,7 +105,7 @@ export class WebSocketRedisAdaptor
|
|
|
92
105
|
{
|
|
93
106
|
|
|
94
107
|
static readonly #version = 1;
|
|
95
|
-
static readonly #kind = { json: 0x6a, bytes: 0x62, change: 0x63 } as const;
|
|
108
|
+
static readonly #kind = { json: 0x6a, bytes: 0x62, change: 0x63, revoke: 0x72 } as const;
|
|
96
109
|
|
|
97
110
|
static readonly #shards = 16_384;
|
|
98
111
|
|
|
@@ -102,9 +115,7 @@ export class WebSocketRedisAdaptor
|
|
|
102
115
|
readonly #rooms = new Map<string, HeldRoom>();
|
|
103
116
|
readonly #socketRooms = new Map<string, Set<string>>();
|
|
104
117
|
readonly #channels = new Map<string, ChannelHold>();
|
|
105
|
-
#
|
|
106
|
-
#changeHandler: LiveChangeHandler | null = null;
|
|
107
|
-
#recoveryHandler: ((rooms: string[]) => void) | null = null;
|
|
118
|
+
readonly #handlers: RedisHandlers = { event: null, change: null, recovery: null, revocation: null };
|
|
108
119
|
#publisherReady = false;
|
|
109
120
|
#draining = false;
|
|
110
121
|
#drained: Promise<void> = Promise.resolve();
|
|
@@ -130,6 +141,10 @@ export class WebSocketRedisAdaptor
|
|
|
130
141
|
return `${this.channelPrefix}ws:change:${refName}`;
|
|
131
142
|
}
|
|
132
143
|
|
|
144
|
+
get #revocationChannel() {
|
|
145
|
+
return `${this.channelPrefix}ws:revoke`;
|
|
146
|
+
}
|
|
147
|
+
|
|
133
148
|
override async onInit() {
|
|
134
149
|
|
|
135
150
|
this.publisher.on("ready", () => {
|
|
@@ -165,6 +180,7 @@ export class WebSocketRedisAdaptor
|
|
|
165
180
|
if (this.#destroyed) return;
|
|
166
181
|
|
|
167
182
|
const replies = [...this.#channels].map(([channel, hold]) => this.#subscribe(channel, hold));
|
|
183
|
+
if (this.#handlers.revocation) this.#subscribeRevocations();
|
|
168
184
|
const recovering = !this.#subscriberReady && this.#subscriberStarted && this.#channels.size > 0;
|
|
169
185
|
this.#subscriberReady = true;
|
|
170
186
|
this.#subscriberStarted = true;
|
|
@@ -173,7 +189,7 @@ export class WebSocketRedisAdaptor
|
|
|
173
189
|
|
|
174
190
|
let lost = false;
|
|
175
191
|
const recover = () => {
|
|
176
|
-
if (!lost) this.#
|
|
192
|
+
if (!lost) this.#handlers.recovery?.([...this.#rooms.keys()]);
|
|
177
193
|
};
|
|
178
194
|
const timer = setTimeout(recover, WebSocketRedisAdaptor.#subscribeTimeoutMs);
|
|
179
195
|
const lose = () => {
|
|
@@ -221,9 +237,7 @@ export class WebSocketRedisAdaptor
|
|
|
221
237
|
|
|
222
238
|
if (this.publisher.status !== "end") this.publisher.disconnect();
|
|
223
239
|
}
|
|
224
|
-
this.#
|
|
225
|
-
this.#changeHandler = null;
|
|
226
|
-
this.#recoveryHandler = null;
|
|
240
|
+
Object.assign(this.#handlers, { event: null, change: null, recovery: null, revocation: null });
|
|
227
241
|
this.#rooms.clear();
|
|
228
242
|
this.#socketRooms.clear();
|
|
229
243
|
this.#channels.clear();
|
|
@@ -257,7 +271,28 @@ export class WebSocketRedisAdaptor
|
|
|
257
271
|
}
|
|
258
272
|
|
|
259
273
|
onChange(handler: LiveChangeHandler): void {
|
|
260
|
-
this.#
|
|
274
|
+
this.#handlers.change = handler;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
publishRevocation(claims: WsCredentialClaims): void {
|
|
278
|
+
this.#send({
|
|
279
|
+
channel: this.#revocationChannel,
|
|
280
|
+
roomId: "",
|
|
281
|
+
message: this.#pack("revoke", "", JSON.stringify(claims)),
|
|
282
|
+
coalesce: false,
|
|
283
|
+
seq: this.#sent++,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
onRevocation(handler: (claims: WsCredentialClaims) => void): void {
|
|
288
|
+
this.#handlers.revocation = handler;
|
|
289
|
+
if (this.#subscriberReady) this.#subscribeRevocations();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#subscribeRevocations() {
|
|
293
|
+
this.subscriber.subscribe(this.#revocationChannel).catch((err: Error) => {
|
|
294
|
+
this.logger.warn(`Subscribe to ${this.#revocationChannel} failed: ${err.message}`);
|
|
295
|
+
});
|
|
261
296
|
}
|
|
262
297
|
|
|
263
298
|
#send(buffered: BufferedMessage) {
|
|
@@ -275,15 +310,15 @@ export class WebSocketRedisAdaptor
|
|
|
275
310
|
}
|
|
276
311
|
|
|
277
312
|
setEventHandler(handler: WsRedisEventHandler): void {
|
|
278
|
-
this.#
|
|
313
|
+
this.#handlers.event = handler;
|
|
279
314
|
}
|
|
280
315
|
|
|
281
316
|
clearEventHandler(): void {
|
|
282
|
-
this.#
|
|
317
|
+
this.#handlers.event = null;
|
|
283
318
|
}
|
|
284
319
|
|
|
285
320
|
onRecovered(handler: (rooms: string[]) => void): void {
|
|
286
|
-
this.#
|
|
321
|
+
this.#handlers.recovery = handler;
|
|
287
322
|
}
|
|
288
323
|
|
|
289
324
|
async joinRoom(ws: Bun.ServerWebSocket<unknown>, room: string, { live }: WebsocketJoinOption = {}): Promise<void> {
|
|
@@ -399,22 +434,27 @@ export class WebSocketRedisAdaptor
|
|
|
399
434
|
const unpacked = this.#unpack(message);
|
|
400
435
|
if (!unpacked || unpacked.origin === this.serverId) return;
|
|
401
436
|
if (unpacked.kind === WebSocketRedisAdaptor.#kind.change) {
|
|
402
|
-
this.#
|
|
437
|
+
this.#handlers.change?.(JSON.parse(unpacked.payload.toString()) as LiveChange);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (unpacked.kind === WebSocketRedisAdaptor.#kind.revoke) {
|
|
441
|
+
this.#handlers.revocation?.(JSON.parse(unpacked.payload.toString()) as WsCredentialClaims);
|
|
403
442
|
return;
|
|
404
443
|
}
|
|
405
444
|
|
|
406
|
-
|
|
445
|
+
const handleEvent = this.#handlers.event;
|
|
446
|
+
if (!handleEvent || !this.#rooms.has(unpacked.roomId)) return;
|
|
407
447
|
const data =
|
|
408
448
|
unpacked.kind === WebSocketRedisAdaptor.#kind.bytes
|
|
409
449
|
? new Uint8Array(unpacked.payload)
|
|
410
450
|
: (JSON.parse(unpacked.payload.toString()) as unknown);
|
|
411
|
-
|
|
451
|
+
handleEvent(unpacked.roomId, data);
|
|
412
452
|
} catch (err) {
|
|
413
453
|
this.logger.warn(`Failed to handle Redis WS message: ${err instanceof Error ? err.message : String(err)}`);
|
|
414
454
|
}
|
|
415
455
|
}
|
|
416
456
|
|
|
417
|
-
#pack(kind: "json" | "bytes" | "change", roomId: string, payload: string | Uint8Array): Buffer {
|
|
457
|
+
#pack(kind: "json" | "bytes" | "change" | "revoke", roomId: string, payload: string | Uint8Array): Buffer {
|
|
418
458
|
const origin = Buffer.from(this.serverId, "utf-8");
|
|
419
459
|
const room = Buffer.from(roomId, "utf-8");
|
|
420
460
|
const header = Buffer.alloc(2 + 2 + origin.length + 2 + room.length);
|
|
@@ -51,8 +51,10 @@ export interface PageConfig {
|
|
|
51
51
|
ssr?: SsrRenderMode;
|
|
52
52
|
/**
|
|
53
53
|
* Opt-in suffix-only RSC commits under `AKAN_PUBLIC_RSC_PARTIAL_COMMIT=1`: the page's `.head()` is plain `title`,
|
|
54
|
-
* `meta` and `link` tags, and the layouts above it
|
|
55
|
-
*
|
|
54
|
+
* `meta` and `link` tags, and the layouts above it draw on the server only from the args they declare. A move to a
|
|
55
|
+
* sibling page, or to another param or query of this page that no layout above declares with `.param()` or
|
|
56
|
+
* `.search()`, then renders the page alone and keeps those layouts as drawn; what follows the path in them is read
|
|
57
|
+
* on the client (`usePathCtx()`, `Link`'s `activeClassName`).
|
|
56
58
|
*/
|
|
57
59
|
rscPatchHeadSafe?: boolean;
|
|
58
60
|
/** `false` answers a `Link` prefetch of the page (on a layout, of every page under it) without rendering it, for a
|
|
@@ -187,6 +189,8 @@ export interface RouteRender {
|
|
|
187
189
|
getRouteDefinition?: () => PromiseOrObject<RouteDefinition | undefined>;
|
|
188
190
|
/** The query names the level reads; `undefined` for a legacy module, which receives every one. */
|
|
189
191
|
getSearchNames?: () => PromiseOrObject<string[] | undefined>;
|
|
192
|
+
/** The params the level reads beyond `lang`; `undefined` for a legacy module, which receives every one. */
|
|
193
|
+
getParamNames?: () => PromiseOrObject<string[] | undefined>;
|
|
190
194
|
}
|
|
191
195
|
export interface RouteViewport {
|
|
192
196
|
/** `viewport-fit=cover`: the page runs under the notch and home indicator, and `env(safe-area-inset-*)` measures them. */
|
|
@@ -22,6 +22,7 @@ export interface WebsocketHeartbeatAckData {
|
|
|
22
22
|
export declare const websocketCredentialContract: {
|
|
23
23
|
readonly expiredCode: 4401;
|
|
24
24
|
readonly expiredReason: "credential expired";
|
|
25
|
+
readonly revokedReason: "credential revoked";
|
|
25
26
|
readonly expiresAtOf: (jwt: string) => number | null;
|
|
26
27
|
};
|
|
27
28
|
export declare const websocketHeartbeatContract: {
|
package/types/server/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export { type OAuthAddressLookup, OAuthAuthorize, type OAuthAuthorizeContext, ty
|
|
|
19
19
|
export { ProcessMetricsCollector } from "./processMetricsCollector.d.ts";
|
|
20
20
|
export { AkanResponse, copyBunRequestFields, HostBasePathWebProxy, LocaleWebProxy, type WebProxy, type WebProxyCls, type WebProxyMatcher, type WebProxyNextInit, type WebProxyRegistration, type WebProxyResult, type WebProxyReturn, WebProxyRunner, type WebProxyRunResult, } from "./proxy.d.ts";
|
|
21
21
|
export { type RemoteAccess, type RemoteAddressLookup, type RemoteAuthServer, type RemoteClient, RemoteError, type RemoteErrorCode, RemoteFetch, type RemoteFetchOptions, RemoteMcp, type RemoteMcpCallResult, type RemoteMcpContent, type RemoteMcpOptions, type RemoteMcpTool, RemoteOAuth, type RemoteRequest, type RemoteResponse, type RemoteScreen, RemoteSource, type RemoteSourceProps, type RemoteTokens, } from "./remote.d.ts";
|
|
22
|
+
export { WsCredentialRevocation } from "./routing/wsCredentialRevocation.d.ts";
|
|
22
23
|
export type { SessionRefreshHook } from "./sessionRefresh.d.ts";
|
|
23
24
|
export { createDefaultSitemapUrls, createDefaultSitemapXml, createSitemapXml, type DefaultSitemapOptions, getSitemapBasePath, } from "./sitemap.d.ts";
|
|
24
25
|
export type { SsrManifest, SsrManifestEntry } from "./ssrTypes.d.ts";
|
|
@@ -17,6 +17,7 @@ export declare class AppWsData {
|
|
|
17
17
|
get ip(): string | null;
|
|
18
18
|
get port(): number | null;
|
|
19
19
|
ipOf(ws: Bun.ServerWebSocket<unknown>): string | null;
|
|
20
|
+
credentialToken(): string | null;
|
|
20
21
|
credentialExpiresAt(): number | null;
|
|
21
22
|
constructor(headers: Headers);
|
|
22
23
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { WebsocketAdaptor, WsCredentialClaims } from "akanjs/service";
|
|
2
|
+
export declare class WsCredentialRevocation {
|
|
3
|
+
#private;
|
|
4
|
+
static use(websocket: WebsocketAdaptor): void;
|
|
5
|
+
static track(ws: Bun.ServerWebSocket<unknown>): void;
|
|
6
|
+
static untrack(ws: Bun.ServerWebSocket<unknown>): void;
|
|
7
|
+
/**
|
|
8
|
+
* Closes, on every server of the app, each socket whose credential carries one of the values under one of the
|
|
9
|
+
* claims, with the code a client renews and reconnects on. Its frames already answer to the account middleware;
|
|
10
|
+
* what this ends is the broadcasts to the rooms it joined before.
|
|
11
|
+
*/
|
|
12
|
+
static revoke(claims: WsCredentialClaims): void;
|
|
13
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PathRoute } from "akanjs/client";
|
|
2
|
-
import type
|
|
2
|
+
import { type Logger } from "akanjs/common";
|
|
3
3
|
import type { ReactNode } from "react";
|
|
4
4
|
import { RouteElementComposer } from "./routeElementComposer.d.ts";
|
|
5
5
|
import { type AkanHeadSnapshotV1, type AkanRouterStateV1, type AkanRscPatchDecision, readAkanRouterStateRequest } from "./routeState.d.ts";
|
|
@@ -129,7 +129,7 @@ export declare function resolveAkanRscHeadSafePatchDecision({ partialCommitEnabl
|
|
|
129
129
|
pageConfig?: PageConfig;
|
|
130
130
|
/** Whether the page the patch leaves from shows its head through `AkanRouteHead`, the only head a patch can swap. */
|
|
131
131
|
routeHeadMounted?: boolean;
|
|
132
|
-
frameChange?: "params-changed" | "layouts-changed" | "page-state-changed" | "frame-unknown";
|
|
132
|
+
frameChange?: "params-changed" | "search-changed" | "layouts-changed" | "page-state-changed" | "frame-unknown";
|
|
133
133
|
headSnapshot?: AkanHeadSnapshotV1;
|
|
134
134
|
/** Whether the target's layouts head differs from the one the page left from shows, which a patch would keep. */
|
|
135
135
|
layoutsHeadChanged?: boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LogRecord } from "akanjs/common";
|
|
2
|
-
import type { LiveChange } from "./predefinedAdaptor/websocket.adaptor";
|
|
2
|
+
import type { LiveChange, WsCredentialClaims } from "./predefinedAdaptor/websocket.adaptor";
|
|
3
3
|
export type AkanChildRole = "all" | "federation" | "batch";
|
|
4
4
|
export type AkanChildStatus = "starting" | "ready" | "healthy" | "draining" | "unhealthy" | "exited" | "crashed";
|
|
5
5
|
/** A replica's RSC worker; `null` on a replica that renders no pages. */
|
|
@@ -264,6 +264,10 @@ export type AkanIpcMessage = {
|
|
|
264
264
|
type: "live.change";
|
|
265
265
|
change: LiveChange;
|
|
266
266
|
origin?: string;
|
|
267
|
+
} | {
|
|
268
|
+
type: "ws.revoke";
|
|
269
|
+
claims: WsCredentialClaims;
|
|
270
|
+
origin?: string;
|
|
267
271
|
}
|
|
268
272
|
/** Child → gateway: every model the child holds live rooms for. */
|
|
269
273
|
| {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LiveChange, LiveChangeHandler, WebsocketAdaptor, WsRedisEventHandler } from "./websocket.adaptor";
|
|
1
|
+
import type { LiveChange, LiveChangeHandler, WebsocketAdaptor, WsCredentialClaims, WsRedisEventHandler } from "./websocket.adaptor";
|
|
2
2
|
declare const SolidPubSub_base: import("..").AdaptorCls<{}, {
|
|
3
3
|
serverId: import("..").InjectInfo<"env", string, {
|
|
4
4
|
[key: string]: any;
|
|
@@ -14,6 +14,8 @@ export declare class SolidPubSub extends SolidPubSub_base implements WebsocketAd
|
|
|
14
14
|
publish(roomId: string, data: unknown): void;
|
|
15
15
|
publishChange(change: LiveChange): void;
|
|
16
16
|
onChange(handler: LiveChangeHandler): void;
|
|
17
|
+
publishRevocation(claims: WsCredentialClaims): void;
|
|
18
|
+
onRevocation(handler: (claims: WsCredentialClaims) => void): void;
|
|
17
19
|
reportLiveModels(models: string[]): void;
|
|
18
20
|
setEventHandler(handler: WsRedisEventHandler): void;
|
|
19
21
|
clearEventHandler(): void;
|
|
@@ -11,6 +11,8 @@ export interface LiveChange {
|
|
|
11
11
|
previous?: string;
|
|
12
12
|
}
|
|
13
13
|
export type LiveChangeHandler = (change: LiveChange) => void;
|
|
14
|
+
/** Claim name to values: a socket whose credential carries any one of them is closed. */
|
|
15
|
+
export type WsCredentialClaims = Record<string, string[]>;
|
|
14
16
|
export interface WebsocketPublishOption {
|
|
15
17
|
/** Only the newest frame for the room matters, so an undelivered older one may be dropped for it. */
|
|
16
18
|
coalesce?: boolean;
|
|
@@ -35,6 +37,9 @@ export interface WebsocketAdaptor {
|
|
|
35
37
|
onChange?(handler: LiveChangeHandler): void;
|
|
36
38
|
/** The models this server holds live rooms for, each time that changes, so changes to the rest pass it by. */
|
|
37
39
|
reportLiveModels?(models: string[]): void;
|
|
40
|
+
/** To the app's other servers, each closing its own sockets whose credential carries one of the claims. */
|
|
41
|
+
publishRevocation?(claims: WsCredentialClaims): void;
|
|
42
|
+
onRevocation?(handler: (claims: WsCredentialClaims) => void): void;
|
|
38
43
|
/** Resolves once what another server publishes to the room reaches this one, or sooner if that link is down or stalls. */
|
|
39
44
|
joinRoom(ws: Bun.ServerWebSocket<unknown>, room: string, option?: WebsocketJoinOption): Promise<void>;
|
|
40
45
|
leaveRoom(ws: Bun.ServerWebSocket<unknown>, room: string): Promise<void>;
|
|
@@ -60,6 +65,8 @@ export declare class WebSocketRedisAdaptor extends WebSocketRedisAdaptor_base im
|
|
|
60
65
|
publish(roomId: string, data: unknown, { coalesce }?: WebsocketPublishOption): void;
|
|
61
66
|
publishChange(change: LiveChange): void;
|
|
62
67
|
onChange(handler: LiveChangeHandler): void;
|
|
68
|
+
publishRevocation(claims: WsCredentialClaims): void;
|
|
69
|
+
onRevocation(handler: (claims: WsCredentialClaims) => void): void;
|
|
63
70
|
setEventHandler(handler: WsRedisEventHandler): void;
|
|
64
71
|
clearEventHandler(): void;
|
|
65
72
|
onRecovered(handler: (rooms: string[]) => void): void;
|