akanjs 3.2.0-rc.11 → 3.2.0-rc.13
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/ARCHITECTURE.md +17 -17
- package/CHANGELOG.md +24 -0
- package/base/base.ts +2 -1
- package/client/clientRuntime.ts +1 -0
- package/client/cn.ts +2 -2
- package/client/csrTypes.ts +2 -0
- package/client/frameConfig.ts +1 -0
- package/client/index.ts +249 -26
- package/client/locale.ts +1 -0
- package/client/route/RouteDefinition.ts +24 -7
- package/client/route/routeArgs.ts +7 -0
- package/client/router.ts +12 -2
- package/client/rscNavigation.ts +1 -0
- package/common/CodeTranscript.ts +1 -1
- package/constant/constantRegistry.ts +4 -8
- package/constant/crystalize.ts +1 -2
- package/constant/getDefault.ts +1 -1
- package/constant/immerify.ts +1 -1
- package/constant/index.ts +78 -21
- package/constant/purify.ts +4 -9
- package/constant/serialize.ts +4 -2
- package/constant/types.ts +1 -0
- package/constant/via.ts +1 -2
- package/dictionary/locale.ts +1 -1
- package/document/databaseRegistry.ts +2 -1
- package/document/documentSchema.ts +1 -0
- package/document/filterMeta.ts +4 -1
- package/document/into.ts +2 -1
- package/document/loaderInfo.ts +2 -1
- package/document/types.ts +1 -1
- package/fetch/client/fetchClient.ts +3 -3
- package/fetch/client/httpClient.ts +2 -0
- package/fetch/client/wsClient.ts +1 -0
- package/package.json +1 -1
- package/server/akanApp.ts +103 -1270
- package/server/akanServer.ts +1 -1
- package/server/console.ts +3 -2
- package/server/gateway/childSupervisor.ts +325 -0
- package/server/gateway/devHostChannel.ts +40 -0
- package/server/gateway/gatewayChildren.ts +135 -0
- package/server/gateway/gatewayConfig.ts +182 -0
- package/server/gateway/gatewayHttpProxy.ts +231 -0
- package/server/gateway/gatewayLogging.ts +111 -0
- package/server/gateway/gatewayRooms.ts +134 -0
- package/server/gateway/gatewayStatus.ts +116 -0
- package/server/gateway/gatewayStatusPage.ts +54 -0
- package/server/gateway/gatewayWebSocketRelay.ts +149 -0
- package/server/index.ts +257 -17
- package/server/logging/logControlSocket.ts +42 -3
- package/server/mcp/McpDispatcher.ts +6 -24
- package/server/rscClient.tsx +14 -8
- package/server/sitemap.ts +1 -0
- package/service/injectInfo.ts +2 -1
- package/service/predefinedAdaptor/sql/driver/bunSqlite.ts +3 -3
- package/service/predefinedAdaptor/sql/driver/postgres.ts +1 -1
- package/service/serviceRegistry.ts +1 -0
- package/signal/SignalFailure.ts +21 -0
- package/signal/agentTurnStream.ts +5 -2
- package/signal/index.ts +298 -30
- package/signal/resolveFieldContext.ts +34 -0
- package/signal/serializer/fetch.serializer.ts +7 -10
- package/signal/signalContext.ts +4 -31
- package/signal/trace.ts +3 -0
- package/store/baseSt.ts +1 -0
- package/store/index.ts +125 -18
- package/store/stateBuilder.ts +2 -0
- package/types/base/base.d.ts +2 -1
- package/types/client/clientRuntime.d.ts +1 -0
- package/types/client/cn.d.ts +2 -2
- package/types/client/csrTypes.d.ts +2 -0
- package/types/client/frameConfig.d.ts +1 -0
- package/types/client/index.d.ts +25 -26
- package/types/client/locale.d.ts +1 -0
- package/types/client/route/RouteDefinition.d.ts +2 -2
- package/types/client/route/routeArgs.d.ts +6 -0
- package/types/client/router.d.ts +2 -1
- package/types/client/rscNavigation.d.ts +1 -0
- package/types/common/CodeTranscript.d.ts +1 -1
- package/types/constant/constantRegistry.d.ts +3 -1
- package/types/constant/crystalize.d.ts +1 -1
- package/types/constant/getDefault.d.ts +1 -1
- package/types/constant/immerify.d.ts +1 -1
- package/types/constant/index.d.ts +21 -21
- package/types/constant/purify.d.ts +2 -1
- package/types/constant/serialize.d.ts +1 -1
- package/types/constant/types.d.ts +1 -0
- package/types/dictionary/base.dictionary.d.ts +1 -1
- package/types/dictionary/dictionary.d.ts +8 -8
- package/types/dictionary/locale.d.ts +1 -1
- package/types/document/databaseRegistry.d.ts +2 -1
- package/types/document/documentSchema.d.ts +1 -0
- package/types/document/filterMeta.d.ts +4 -1
- package/types/document/into.d.ts +1 -1
- package/types/document/loaderInfo.d.ts +2 -1
- package/types/document/types.d.ts +1 -1
- package/types/fetch/client/fetchClient.d.ts +2 -1
- package/types/fetch/client/httpClient.d.ts +2 -0
- package/types/fetch/client/wsClient.d.ts +1 -0
- package/types/server/akanApp.d.ts +2 -20
- package/types/server/console.d.ts +3 -2
- package/types/server/gateway/childSupervisor.d.ts +33 -0
- package/types/server/gateway/devHostChannel.d.ts +13 -0
- package/types/server/gateway/gatewayChildren.d.ts +47 -0
- package/types/server/gateway/gatewayConfig.d.ts +82 -0
- package/types/server/gateway/gatewayHttpProxy.d.ts +22 -0
- package/types/server/gateway/gatewayLogging.d.ts +16 -0
- package/types/server/gateway/gatewayRooms.d.ts +22 -0
- package/types/server/gateway/gatewayStatus.d.ts +62 -0
- package/types/server/gateway/gatewayStatusPage.d.ts +9 -0
- package/types/server/gateway/gatewayWebSocketRelay.d.ts +17 -0
- package/types/server/index.d.ts +17 -17
- package/types/server/logging/rotatingLogWriter.d.ts +1 -1
- package/types/server/sitemap.d.ts +1 -0
- package/types/service/injectInfo.d.ts +2 -1
- package/types/service/predefinedAdaptor/sql/driver/bunSqlite.d.ts +1 -14
- package/types/service/predefinedAdaptor/sql/driver/postgres.d.ts +0 -8
- package/types/service/serviceRegistry.d.ts +1 -0
- package/types/signal/SignalFailure.d.ts +1 -0
- package/types/signal/agentTurnStream.d.ts +1 -0
- package/types/signal/index.d.ts +31 -30
- package/types/signal/resolveFieldContext.d.ts +15 -0
- package/types/signal/serializer/fetch.serializer.d.ts +3 -1
- package/types/signal/signalContext.d.ts +1 -13
- package/types/signal/trace.d.ts +3 -0
- package/types/store/baseSt.d.ts +1 -0
- package/types/store/index.d.ts +17 -17
- package/types/store/stateBuilder.d.ts +2 -0
- package/types/ui/Agent/Chat.d.ts +5 -2
- package/types/ui/Agent/ChatHeader.d.ts +13 -0
- package/types/ui/Agent/ChatPendingCards.d.ts +23 -0
- package/types/ui/Reference/origin.d.ts +1 -0
- package/types/ui/overlayLayer.d.ts +6 -1
- package/types/vendor/@akanjs/native/packages/cli/src/lib/project.d.ts +4 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/relaunch.d.ts +1 -0
- package/types/vendor/@akanjs/native/plugins/external-view/src/index.d.ts +14 -10
- package/types/vendor/use-agentic/failureCode.d.ts +4 -0
- package/types/vendor/use-agentic/types.d.ts +6 -0
- package/types/webkit/CsrGuards.d.ts +2 -0
- package/ui/Agent/Chat.tsx +96 -116
- package/ui/Agent/ChatHeader.tsx +59 -0
- package/ui/Agent/ChatPendingCards.tsx +49 -0
- package/ui/Agent/fetchRunner.ts +8 -1
- package/ui/BottomSheet.tsx +1 -1
- package/ui/Dialog/LegacyModal.tsx +1 -1
- package/ui/Dialog/Modal.tsx +1 -1
- package/ui/Popconfirm.tsx +1 -1
- package/ui/Reference/origin.ts +1 -0
- package/ui/overlayLayer.ts +22 -1
- package/vendor/@akanjs/native/CHANGELOG.md +6 -1
- package/vendor/@akanjs/native/native/desktop/src/external.rs +213 -111
- package/vendor/@akanjs/native/native/desktop/src/lib.rs +12 -4
- package/vendor/@akanjs/native/native/desktop/src/linux/external.rs +275 -0
- package/vendor/@akanjs/native/native/desktop/src/linux/mod.rs +1 -0
- package/vendor/@akanjs/native/packages/cli/src/lib/native-plugins.ts +5 -3
- package/vendor/@akanjs/native/packages/cli/src/lib/project.ts +25 -1
- package/vendor/@akanjs/native/packages/desktop/src/deeplinks.ts +2 -2
- package/vendor/@akanjs/native/packages/desktop/src/host.ts +2 -2
- package/vendor/@akanjs/native/packages/desktop/src/relaunch.ts +8 -3
- package/vendor/@akanjs/native/plugins/app/src/desktop.ts +2 -2
- package/vendor/@akanjs/native/plugins/autostart/src/desktop.ts +2 -1
- package/vendor/@akanjs/native/plugins/external-view/native-plugin.json +4 -1
- package/vendor/@akanjs/native/plugins/external-view/src/index.ts +14 -10
- package/vendor/use-agentic/AgentSession.ts +11 -11
- package/vendor/use-agentic/CHANGELOG.md +6 -0
- package/vendor/use-agentic/Compaction.ts +2 -1
- package/vendor/use-agentic/ToolRunner.ts +3 -15
- package/vendor/use-agentic/Transcript.ts +6 -3
- package/vendor/use-agentic/WIRE.md +12 -1
- package/vendor/use-agentic/failureCode.ts +44 -0
- package/vendor/use-agentic/httpRunner.ts +14 -3
- package/vendor/use-agentic/types.ts +6 -0
- package/webkit/CsrGuards.ts +7 -2
- package/webkit/useLocation.ts +3 -2
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { Logger } from "akanjs/common";
|
|
2
|
+
import type { AkanMetricsReport } from "akanjs/service";
|
|
3
|
+
import { ProcessMetricsCollector } from "../processMetricsCollector";
|
|
4
|
+
import type { GatewayChildren } from "./gatewayChildren";
|
|
5
|
+
import type { GatewayConfig } from "./gatewayConfig";
|
|
6
|
+
import type { GatewayRooms } from "./gatewayRooms";
|
|
7
|
+
|
|
8
|
+
export class GatewayStatus {
|
|
9
|
+
readonly #config: GatewayConfig;
|
|
10
|
+
readonly #children: GatewayChildren;
|
|
11
|
+
readonly #rooms: GatewayRooms;
|
|
12
|
+
readonly #logger: Logger;
|
|
13
|
+
#gatewayMetrics: AkanMetricsReport = {};
|
|
14
|
+
#proxyHopCount = 0;
|
|
15
|
+
#proxyHopSumMs = 0;
|
|
16
|
+
#proxyHopMaxMs = 0;
|
|
17
|
+
|
|
18
|
+
constructor(config: GatewayConfig, children: GatewayChildren, rooms: GatewayRooms, logger: Logger) {
|
|
19
|
+
this.#config = config;
|
|
20
|
+
this.#children = children;
|
|
21
|
+
this.#rooms = rooms;
|
|
22
|
+
this.#logger = logger;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
healthStatus() {
|
|
26
|
+
return {
|
|
27
|
+
status: this.#children.stopping ? "stopping" : "running",
|
|
28
|
+
pid: process.pid,
|
|
29
|
+
children: [...this.#children.values()].map((child) => ({
|
|
30
|
+
idx: child.idx,
|
|
31
|
+
role: child.role,
|
|
32
|
+
status: child.status,
|
|
33
|
+
ready: child.ready,
|
|
34
|
+
pid: child.pid,
|
|
35
|
+
upstream: child.upstream,
|
|
36
|
+
restartAttempts: child.restart.attempts,
|
|
37
|
+
restartCount: child.restartCount,
|
|
38
|
+
restartPending: child.restartPending,
|
|
39
|
+
lastExitCode: child.lastExitCode,
|
|
40
|
+
lastRestartAt: child.lastRestartAt,
|
|
41
|
+
lastRestartReason: child.lastRestartReason,
|
|
42
|
+
lastErrorMessage: child.lastErrorMessage,
|
|
43
|
+
})),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
readyStatus() {
|
|
48
|
+
return {
|
|
49
|
+
ready: this.#isReady(),
|
|
50
|
+
pid: process.pid,
|
|
51
|
+
children: [...this.#children.values()].map(({ idx, role, status, ready }) => ({ idx, role, status, ready })),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
#isReady() {
|
|
56
|
+
if (this.#children.stopping) return false;
|
|
57
|
+
if (this.#config.replica.federation + this.#config.replica.all > 0)
|
|
58
|
+
return this.#children.federationCandidates().length > 0;
|
|
59
|
+
return [...this.#children.values()].some((child) => child.ready && !this.#children.isUnavailable(child));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
metricsStatus() {
|
|
63
|
+
return {
|
|
64
|
+
rooms: this.#rooms.roomCount,
|
|
65
|
+
sockets: this.#rooms.socketCount,
|
|
66
|
+
gateway: this.#gatewayMetrics,
|
|
67
|
+
proxyHop: this.#proxyHopCount
|
|
68
|
+
? {
|
|
69
|
+
count: this.#proxyHopCount,
|
|
70
|
+
meanMs: Math.round((this.#proxyHopSumMs / this.#proxyHopCount) * 1000) / 1000,
|
|
71
|
+
maxMs: Math.round(this.#proxyHopMaxMs * 1000) / 1000,
|
|
72
|
+
}
|
|
73
|
+
: null,
|
|
74
|
+
children: [...this.#children.values()].map((child) => ({
|
|
75
|
+
idx: child.idx,
|
|
76
|
+
role: child.role,
|
|
77
|
+
metrics: child.metrics,
|
|
78
|
+
rooms: this.#rooms.roomCountOf(child.idx),
|
|
79
|
+
restartAttempts: child.restart.attempts,
|
|
80
|
+
restartCount: child.restartCount,
|
|
81
|
+
restartPending: child.restartPending,
|
|
82
|
+
lastExitCode: child.lastExitCode,
|
|
83
|
+
lastRestartAt: child.lastRestartAt,
|
|
84
|
+
lastRestartReason: child.lastRestartReason,
|
|
85
|
+
})),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
recordProxyHop(durationMs: number) {
|
|
90
|
+
this.#proxyHopCount += 1;
|
|
91
|
+
this.#proxyHopSumMs += durationMs;
|
|
92
|
+
this.#proxyHopMaxMs = Math.max(this.#proxyHopMaxMs, durationMs);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async reportMetrics() {
|
|
96
|
+
this.#gatewayMetrics = await ProcessMetricsCollector.collect({ role: "gateway" });
|
|
97
|
+
if (process.env.AKAN_MEMORY_LOG !== "1") return;
|
|
98
|
+
this.#logger.verbose(
|
|
99
|
+
`memory role=gateway ${ProcessMetricsCollector.format(this.#gatewayMetrics)} children=${this.#children.size}`,
|
|
100
|
+
);
|
|
101
|
+
for (const child of this.#children.values()) {
|
|
102
|
+
if (!child.metrics.rssBytes) continue;
|
|
103
|
+
const rooms = this.#rooms.roomCountOf(child.idx);
|
|
104
|
+
this.#logger.verbose(
|
|
105
|
+
`memory role=${child.role} idx=${child.idx} ${ProcessMetricsCollector.format({
|
|
106
|
+
...child.metrics,
|
|
107
|
+
pid: child.metrics.pid ?? child.pid,
|
|
108
|
+
})} activeRequests=${child.metrics.activeRequests ?? 0} activeWebSockets=${
|
|
109
|
+
child.metrics.activeWebSockets ?? 0
|
|
110
|
+
} rooms=${rooms} rscPending=${child.metrics.rscPendingRenderCount ?? 0} rscModules=${
|
|
111
|
+
child.metrics.rscLoadedRouteModuleCount ?? 0
|
|
112
|
+
}`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
|
|
2
|
+
import { applySecurityHeaders } from "../assetHeaders";
|
|
3
|
+
|
|
4
|
+
export class GatewayStatusPage {
|
|
5
|
+
static headers(contentType: string): Headers {
|
|
6
|
+
const headers = new Headers({ "content-type": contentType, "cache-control": "no-store" });
|
|
7
|
+
return applySecurityHeaders(headers, { html: contentType.startsWith("text/html") });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static html({ heading, detail, note }: { heading: string; detail: string; note: string }) {
|
|
11
|
+
return `<!doctype html>
|
|
12
|
+
<html>
|
|
13
|
+
<head>
|
|
14
|
+
<meta charset="utf-8" />
|
|
15
|
+
<title>${GatewayStatusPage.#escapeHtml(heading)}</title>
|
|
16
|
+
<style>
|
|
17
|
+
body { margin: 0; padding: 48px 24px; background: #111827; color: #e5e7eb; font-family: ui-sans-serif, system-ui, sans-serif; }
|
|
18
|
+
main { max-width: 720px; margin: 0 auto; }
|
|
19
|
+
h1 { font-size: 20px; color: #f87171; }
|
|
20
|
+
pre { padding: 16px; border-radius: 8px; background: #1f2937; color: #fca5a5; white-space: pre-wrap; word-break: break-word; }
|
|
21
|
+
p { color: #9ca3af; font-size: 14px; }
|
|
22
|
+
</style>
|
|
23
|
+
</head>
|
|
24
|
+
<body>
|
|
25
|
+
<main>
|
|
26
|
+
<h1>${GatewayStatusPage.#escapeHtml(heading)}</h1>
|
|
27
|
+
<pre>${GatewayStatusPage.#escapeHtml(detail)}</pre>
|
|
28
|
+
<p>${GatewayStatusPage.#escapeHtml(note)}</p>
|
|
29
|
+
</main>
|
|
30
|
+
<script>
|
|
31
|
+
const poll = async () => {
|
|
32
|
+
try {
|
|
33
|
+
const res = await fetch(location.href, { cache: "no-store" });
|
|
34
|
+
if (res.ok) { location.reload(); return; }
|
|
35
|
+
} catch {}
|
|
36
|
+
setTimeout(poll, 1000);
|
|
37
|
+
};
|
|
38
|
+
setTimeout(poll, 1000);
|
|
39
|
+
</script>
|
|
40
|
+
</body>
|
|
41
|
+
</html>`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static #escapeHtml(text: string) {
|
|
45
|
+
const replacements: Record<string, string> = {
|
|
46
|
+
"&": "&",
|
|
47
|
+
"<": "<",
|
|
48
|
+
">": ">",
|
|
49
|
+
'"': """,
|
|
50
|
+
"'": "'",
|
|
51
|
+
};
|
|
52
|
+
return text.replace(/[&<>"']/g, (ch) => replacements[ch] ?? ch);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { TrustedProxy } from "akanjs/common";
|
|
2
|
+
import { CrossSiteGuard } from "../../signal/CrossSiteGuard";
|
|
3
|
+
import { makeAkanChildProxyHeaders } from "../akanAppHeaders";
|
|
4
|
+
import { WsUpgrade } from "../routing/wsUpgrade";
|
|
5
|
+
import type { GatewayChildren } from "./gatewayChildren";
|
|
6
|
+
import type { GatewayConfig } from "./gatewayConfig";
|
|
7
|
+
|
|
8
|
+
export interface GatewayWsData {
|
|
9
|
+
childIdx: number;
|
|
10
|
+
upstream: WebSocket;
|
|
11
|
+
clientIp: string | null;
|
|
12
|
+
pending: (string | ArrayBuffer)[];
|
|
13
|
+
pendingBytes: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const relayableCloseCode = (code: number): number => {
|
|
17
|
+
if (code >= 3000 && code <= 4999) return code;
|
|
18
|
+
if (code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006) return code;
|
|
19
|
+
return 1001;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export class GatewayWebSocketRelay {
|
|
23
|
+
|
|
24
|
+
static readonly #wsPendingLimitBytes = 1024 * 1024;
|
|
25
|
+
static readonly #wsUpstreamOpenTimeoutMs = 10_000;
|
|
26
|
+
|
|
27
|
+
readonly #config: GatewayConfig;
|
|
28
|
+
readonly #children: GatewayChildren;
|
|
29
|
+
readonly #wsCaps = {
|
|
30
|
+
total: GatewayWebSocketRelay.#positiveIntEnv("AKAN_MAX_WEBSOCKETS"),
|
|
31
|
+
perIp: GatewayWebSocketRelay.#positiveIntEnv("AKAN_MAX_WEBSOCKETS_PER_IP"),
|
|
32
|
+
};
|
|
33
|
+
#wsOpen = 0;
|
|
34
|
+
readonly #wsOpenByIp = new Map<string, number>();
|
|
35
|
+
|
|
36
|
+
constructor(config: GatewayConfig, children: GatewayChildren) {
|
|
37
|
+
this.#config = config;
|
|
38
|
+
this.#children = children;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
upgrade(req: Request, server: Bun.Server<GatewayWsData>): Response | undefined {
|
|
42
|
+
const child = this.#children.pickFederationChild();
|
|
43
|
+
|
|
44
|
+
const upstream = child?.upstream
|
|
45
|
+
? (child.wsUpstream ?? this.#config.childUpstreamOf(child.idx, child.role).ws)
|
|
46
|
+
: null;
|
|
47
|
+
if (!child || !upstream) return new Response("No websocket upstream is ready", { status: 503 });
|
|
48
|
+
const url = new URL(req.url);
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
CrossSiteGuard.assertOrigin(req, url, url.pathname === "/_akan/hmr" ? "hmr" : "websocket");
|
|
52
|
+
} catch {
|
|
53
|
+
return new Response("Forbidden", { status: 403 });
|
|
54
|
+
}
|
|
55
|
+
if (!WsUpgrade.isRequested(req)) return WsUpgrade.required();
|
|
56
|
+
const peer = server.requestIP(req) ?? undefined;
|
|
57
|
+
const clientIp = TrustedProxy.clientAddress(req.headers, peer?.address);
|
|
58
|
+
const refused = this.#refuseWebSocket(clientIp);
|
|
59
|
+
if (refused) return refused;
|
|
60
|
+
const upstreamWs = new WebSocket(`ws://${upstream.host}:${upstream.port}${url.pathname}${url.search}`, {
|
|
61
|
+
headers: makeAkanChildProxyHeaders(req, child.idx, peer),
|
|
62
|
+
} as unknown as string[]);
|
|
63
|
+
|
|
64
|
+
const upgraded = server.upgrade(req, {
|
|
65
|
+
data: { childIdx: child.idx, upstream: upstreamWs, clientIp, pending: [], pendingBytes: 0 },
|
|
66
|
+
});
|
|
67
|
+
if (!upgraded) {
|
|
68
|
+
upstreamWs.close();
|
|
69
|
+
return new Response("WebSocket upgrade failed", { status: 500 });
|
|
70
|
+
}
|
|
71
|
+
child.metrics.activeWebSockets = (child.metrics.activeWebSockets ?? 0) + 1;
|
|
72
|
+
this.#wsOpen += 1;
|
|
73
|
+
if (clientIp) this.#wsOpenByIp.set(clientIp, (this.#wsOpenByIp.get(clientIp) ?? 0) + 1);
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
#refuseWebSocket(clientIp: string | null): Response | null {
|
|
78
|
+
const headers = { "cache-control": "no-store" };
|
|
79
|
+
if (this.#wsCaps.total && this.#wsOpen >= this.#wsCaps.total)
|
|
80
|
+
return new Response("Too many open websockets", { status: 503, headers });
|
|
81
|
+
if (this.#wsCaps.perIp && clientIp && (this.#wsOpenByIp.get(clientIp) ?? 0) >= this.#wsCaps.perIp)
|
|
82
|
+
return new Response("Too many open websockets from this address", { status: 429, headers });
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static #positiveIntEnv(name: string): number | null {
|
|
87
|
+
const value = Number(process.env[name]);
|
|
88
|
+
return Number.isInteger(value) && value > 0 ? value : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
open(ws: Bun.ServerWebSocket<GatewayWsData>) {
|
|
92
|
+
const upstream = ws.data.upstream;
|
|
93
|
+
const openTimer =
|
|
94
|
+
upstream.readyState === WebSocket.CONNECTING
|
|
95
|
+
? setTimeout(() => {
|
|
96
|
+
if (upstream.readyState === WebSocket.OPEN) return;
|
|
97
|
+
upstream.close();
|
|
98
|
+
ws.close(1013, "upstream websocket did not open");
|
|
99
|
+
}, GatewayWebSocketRelay.#wsUpstreamOpenTimeoutMs)
|
|
100
|
+
: null;
|
|
101
|
+
upstream.addEventListener("open", () => {
|
|
102
|
+
if (openTimer) clearTimeout(openTimer);
|
|
103
|
+
for (const message of ws.data.pending.splice(0)) upstream.send(message);
|
|
104
|
+
ws.data.pendingBytes = 0;
|
|
105
|
+
});
|
|
106
|
+
upstream.addEventListener("message", (event) => {
|
|
107
|
+
const result = ws.send(event.data as string | ArrayBuffer);
|
|
108
|
+
if (result === 0) upstream.close();
|
|
109
|
+
});
|
|
110
|
+
upstream.addEventListener("close", (event) => {
|
|
111
|
+
if (openTimer) clearTimeout(openTimer);
|
|
112
|
+
ws.close(relayableCloseCode(event.code), event.reason);
|
|
113
|
+
});
|
|
114
|
+
upstream.addEventListener("error", () => ws.close(1011, "upstream websocket error"));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
message(ws: Bun.ServerWebSocket<GatewayWsData>, message: string | ArrayBuffer | Uint8Array) {
|
|
118
|
+
const upstream = ws.data.upstream;
|
|
119
|
+
const payload =
|
|
120
|
+
typeof message === "string"
|
|
121
|
+
? message
|
|
122
|
+
: message instanceof ArrayBuffer
|
|
123
|
+
? message
|
|
124
|
+
: new Uint8Array(message).slice().buffer;
|
|
125
|
+
if (upstream.readyState === WebSocket.OPEN) {
|
|
126
|
+
upstream.send(payload);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const size = typeof payload === "string" ? Buffer.byteLength(payload) : payload.byteLength;
|
|
130
|
+
if (ws.data.pendingBytes + size > GatewayWebSocketRelay.#wsPendingLimitBytes) {
|
|
131
|
+
ws.data.pending.length = 0;
|
|
132
|
+
ws.close(1009, "too much data before the upstream opened");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
ws.data.pending.push(payload as string | ArrayBuffer);
|
|
136
|
+
ws.data.pendingBytes += size;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
close(ws: Bun.ServerWebSocket<GatewayWsData>, code: number, reason: string) {
|
|
140
|
+
ws.data.upstream.close(relayableCloseCode(code), reason);
|
|
141
|
+
const child = this.#children.get(ws.data.childIdx);
|
|
142
|
+
if (child) child.metrics.activeWebSockets = Math.max(0, (child.metrics.activeWebSockets ?? 1) - 1);
|
|
143
|
+
this.#wsOpen = Math.max(0, this.#wsOpen - 1);
|
|
144
|
+
const { clientIp } = ws.data;
|
|
145
|
+
const remaining = clientIp ? (this.#wsOpenByIp.get(clientIp) ?? 1) - 1 : 0;
|
|
146
|
+
if (clientIp && remaining > 0) this.#wsOpenByIp.set(clientIp, remaining);
|
|
147
|
+
else if (clientIp) this.#wsOpenByIp.delete(clientIp);
|
|
148
|
+
}
|
|
149
|
+
}
|
package/server/index.ts
CHANGED
|
@@ -1,21 +1,261 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
export { AkanApp, type AkanAppOptions } from "./akanApp";
|
|
2
|
+
export { AkanLib, type AkanLibProps, type DatabaseModule, type ScalarModule, type ServiceModule } from "./akanLib";
|
|
3
|
+
export { type AdaptorOverride, AkanOption, createDefaultAkanOption } from "./akanOption";
|
|
4
|
+
export {
|
|
5
|
+
AkanServer,
|
|
6
|
+
type AkanServerConsoleInfo,
|
|
7
|
+
type AkanServerOptions,
|
|
8
|
+
type AkanServerProps,
|
|
9
|
+
type McpServerOption,
|
|
10
|
+
} from "./akanServer";
|
|
11
|
+
export {
|
|
12
|
+
type BuildChangesPayload,
|
|
13
|
+
type BuilderControl,
|
|
14
|
+
type BuilderCsrReq,
|
|
15
|
+
type BuilderCsrRes,
|
|
16
|
+
type BuilderEvent,
|
|
17
|
+
type BuilderFailureKind,
|
|
18
|
+
type BuilderGraphReq,
|
|
19
|
+
type BuilderGraphRes,
|
|
20
|
+
type BuilderMessage,
|
|
21
|
+
type BuilderMetrics,
|
|
22
|
+
type BuilderReq,
|
|
23
|
+
type BuilderRequest,
|
|
24
|
+
type BuilderRes,
|
|
25
|
+
type BuilderResponse,
|
|
26
|
+
BuilderRpc,
|
|
27
|
+
type BuilderRpcEventHandlers,
|
|
28
|
+
type BuilderRpcTimeout,
|
|
29
|
+
type BuilderRpcUnavailable,
|
|
30
|
+
type BuilderStateReason,
|
|
31
|
+
type BuildFrameworkPayload,
|
|
32
|
+
type BuildGraphPayload,
|
|
33
|
+
type BuildPhase,
|
|
34
|
+
type BuildRouteClientResult,
|
|
35
|
+
type BuildRouteResultPayload,
|
|
36
|
+
type ClientManifest,
|
|
37
|
+
type ClientManifestEntry,
|
|
38
|
+
type CsrUpdatedPayload,
|
|
39
|
+
type CssPayload,
|
|
40
|
+
DevBuildRecovery,
|
|
41
|
+
type DevBuildStatus,
|
|
42
|
+
type DevChangeAction,
|
|
43
|
+
type DevChangePlan,
|
|
44
|
+
type DevChangeRole,
|
|
45
|
+
type HmrTrace,
|
|
46
|
+
type InvalidateClientEntriesOptions,
|
|
47
|
+
type MatchedRoute,
|
|
48
|
+
type MergedManifest,
|
|
49
|
+
ModulePreloadPlan,
|
|
50
|
+
type ModulePreloadTable,
|
|
51
|
+
type PagesBundlePayload,
|
|
52
|
+
ROUTE_SEED_INDEX_JSON,
|
|
53
|
+
RouteClientCache,
|
|
54
|
+
type RouteSeedEntry,
|
|
55
|
+
type RouteSeedIndex,
|
|
56
|
+
RouteSeedIndexStore,
|
|
57
|
+
type RoutesManifest,
|
|
58
|
+
RoutesManifestStore,
|
|
59
|
+
type SsrUpdatedPayload,
|
|
60
|
+
} from "./artifact";
|
|
61
|
+
export {
|
|
62
|
+
type AkanConsoleContext,
|
|
63
|
+
type AkanConsoleOptions,
|
|
64
|
+
assertAkanConsoleAllowed,
|
|
65
|
+
createAkanConsoleContext,
|
|
66
|
+
createAkanConsoleLogCommands,
|
|
67
|
+
evaluateAkanConsoleInput,
|
|
68
|
+
getAkanConsoleMethods,
|
|
69
|
+
isAkanConsoleInputComplete,
|
|
70
|
+
parseAkanConsoleQuery,
|
|
71
|
+
startAkanConsole,
|
|
72
|
+
} from "./console";
|
|
73
|
+
export { Transaction, Try } from "./decorators";
|
|
74
|
+
export {
|
|
75
|
+
type ArgType,
|
|
76
|
+
type ConstantData,
|
|
77
|
+
type ConstantFieldKind,
|
|
78
|
+
type ConstantFieldNode,
|
|
79
|
+
type ConstantModelNode,
|
|
80
|
+
type ConstantModelView,
|
|
81
|
+
type ConstantModelViewKey,
|
|
82
|
+
ConstantSerializer,
|
|
83
|
+
type DepEdge,
|
|
84
|
+
type DepEdgeKind,
|
|
85
|
+
type DepNode,
|
|
86
|
+
type DepNodeKind,
|
|
87
|
+
type DepsData,
|
|
88
|
+
DepsSerializer,
|
|
89
|
+
type DepsSerializerContext,
|
|
90
|
+
type DevtoolsApp,
|
|
91
|
+
type DevtoolsEnvelope,
|
|
92
|
+
type DevtoolsIndex,
|
|
93
|
+
DevtoolsJson,
|
|
94
|
+
DevtoolsRouter,
|
|
95
|
+
type DevtoolsRouterContext,
|
|
96
|
+
type DictionaryData,
|
|
97
|
+
type DictionaryModuleNode,
|
|
98
|
+
type EndpointNode,
|
|
99
|
+
type EnumNode,
|
|
100
|
+
type FieldType,
|
|
101
|
+
type FilterArg,
|
|
102
|
+
type InternalNode,
|
|
103
|
+
type InternalType,
|
|
104
|
+
type RelationEdge,
|
|
105
|
+
type RouteRow,
|
|
106
|
+
type SerializedArg,
|
|
107
|
+
type SerializedReturns,
|
|
108
|
+
type SignalData,
|
|
109
|
+
type SignalNode,
|
|
110
|
+
SignalSerializer,
|
|
111
|
+
type SignalSerializerContext,
|
|
112
|
+
type SliceNode,
|
|
113
|
+
type Unserializable,
|
|
114
|
+
} from "./devtools";
|
|
9
115
|
export type { ChangeBatch, ChangeKind } from "./hmr/wsHub";
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
116
|
+
export {
|
|
117
|
+
MachineAccount,
|
|
118
|
+
type MachineProof,
|
|
119
|
+
MachineToken,
|
|
120
|
+
type MachineTokenIssueOptions,
|
|
121
|
+
type MachineTokenVerdict,
|
|
122
|
+
} from "./machine";
|
|
123
|
+
export {
|
|
124
|
+
DocPath,
|
|
125
|
+
type DocSlot,
|
|
126
|
+
type MigrationAction,
|
|
127
|
+
type MigrationApplyResult,
|
|
128
|
+
type MigrationArgs,
|
|
129
|
+
type MigrationChange,
|
|
130
|
+
MigrationChangeLog,
|
|
131
|
+
type MigrationChangeOp,
|
|
132
|
+
MigrationChanges,
|
|
133
|
+
MigrationCommand,
|
|
134
|
+
type MigrationCommandResult,
|
|
135
|
+
MigrationContext,
|
|
136
|
+
type MigrationContextOption,
|
|
137
|
+
MigrationDefinition,
|
|
138
|
+
type MigrationEntry,
|
|
139
|
+
MigrationError,
|
|
140
|
+
type MigrationErrorCode,
|
|
141
|
+
type MigrationGet,
|
|
142
|
+
MigrationLedger,
|
|
143
|
+
type MigrationListItem,
|
|
144
|
+
MigrationLock,
|
|
145
|
+
type MigrationParamInfo,
|
|
146
|
+
type MigrationParamOption,
|
|
147
|
+
type MigrationParamSpec,
|
|
148
|
+
MigrationParams,
|
|
149
|
+
type MigrationParamValue,
|
|
150
|
+
type MigrationPatch,
|
|
151
|
+
type MigrationPlan,
|
|
152
|
+
type MigrationPlanResult,
|
|
153
|
+
MigrationPulse,
|
|
154
|
+
type MigrationRevertOutcome,
|
|
155
|
+
type MigrationRewriteOption,
|
|
156
|
+
type MigrationRollbackResult,
|
|
157
|
+
type MigrationRollbackRun,
|
|
158
|
+
type MigrationRun,
|
|
159
|
+
type MigrationRunKind,
|
|
160
|
+
MigrationRunner,
|
|
161
|
+
type MigrationRunnerOption,
|
|
162
|
+
type MigrationRunStatus,
|
|
163
|
+
MigrationScope,
|
|
164
|
+
type MigrationScopeOption,
|
|
165
|
+
type MigrationSettings,
|
|
166
|
+
type MigrationSkip,
|
|
167
|
+
type MigrationState,
|
|
168
|
+
type MigrationStatus,
|
|
169
|
+
Migrations,
|
|
170
|
+
MigrationTable,
|
|
171
|
+
type MigrationVerifyResult,
|
|
172
|
+
migration,
|
|
173
|
+
migrationActions,
|
|
174
|
+
} from "./migration";
|
|
175
|
+
export { NativeFile, type NativeFileMode } from "./nativeFile";
|
|
176
|
+
export { NativeSecret } from "./nativeSecret";
|
|
177
|
+
export {
|
|
178
|
+
type OAuthAddressLookup,
|
|
179
|
+
OAuthAuthorize,
|
|
180
|
+
type OAuthAuthorizeContext,
|
|
181
|
+
type OAuthAuthorizeErrorCode,
|
|
182
|
+
type OAuthAuthorizeParams,
|
|
183
|
+
type OAuthAuthorizeResult,
|
|
184
|
+
type OAuthClientAuthMethod,
|
|
185
|
+
type OAuthClientCredential,
|
|
186
|
+
OAuthClientIdMetadata,
|
|
187
|
+
type OAuthClientIdMetadataFetchOptions,
|
|
188
|
+
type OAuthClientIdMetadataResult,
|
|
189
|
+
type OAuthClientMetadata,
|
|
190
|
+
type OAuthClientRecord,
|
|
191
|
+
type OAuthClientSource,
|
|
192
|
+
type OAuthDocumentFetch,
|
|
193
|
+
OAuthErrors,
|
|
194
|
+
OAuthMetadata,
|
|
195
|
+
type OAuthMetadataProps,
|
|
196
|
+
OAuthPkce,
|
|
197
|
+
OAuthRedirect,
|
|
198
|
+
type OAuthRedirectPolicy,
|
|
199
|
+
OAuthRegistration,
|
|
200
|
+
type OAuthRegistrationErrorCode,
|
|
201
|
+
type OAuthRegistrationResult,
|
|
202
|
+
OAuthRevocation,
|
|
203
|
+
type OAuthRevocationParams,
|
|
204
|
+
type OAuthRevocationParseResult,
|
|
205
|
+
OAuthToken,
|
|
206
|
+
type OAuthTokenErrorCode,
|
|
207
|
+
type OAuthTokenParams,
|
|
208
|
+
type OAuthTokenParseResult,
|
|
209
|
+
type OAuthTokenSuccess,
|
|
210
|
+
type OAuthTokenTypeHint,
|
|
211
|
+
} from "./oauth";
|
|
212
|
+
export { ProcessMetricsCollector } from "./processMetricsCollector";
|
|
213
|
+
export {
|
|
214
|
+
AkanResponse,
|
|
215
|
+
copyBunRequestFields,
|
|
216
|
+
HostBasePathWebProxy,
|
|
217
|
+
LocaleWebProxy,
|
|
218
|
+
type WebProxy,
|
|
219
|
+
type WebProxyCls,
|
|
220
|
+
type WebProxyMatcher,
|
|
221
|
+
type WebProxyNextInit,
|
|
222
|
+
type WebProxyRegistration,
|
|
223
|
+
type WebProxyResult,
|
|
224
|
+
type WebProxyReturn,
|
|
225
|
+
WebProxyRunner,
|
|
226
|
+
type WebProxyRunResult,
|
|
227
|
+
} from "./proxy";
|
|
17
228
|
export type { SessionRefreshHook } from "./sessionRefresh";
|
|
18
229
|
|
|
19
|
-
export
|
|
230
|
+
export {
|
|
231
|
+
createDefaultSitemapUrls,
|
|
232
|
+
createDefaultSitemapXml,
|
|
233
|
+
createSitemapXml,
|
|
234
|
+
type DefaultSitemapOptions,
|
|
235
|
+
getSitemapBasePath,
|
|
236
|
+
isSitemapPath,
|
|
237
|
+
} from "./sitemap";
|
|
20
238
|
export type { SsrManifest, SsrManifestEntry } from "./ssrTypes";
|
|
21
|
-
export
|
|
239
|
+
export {
|
|
240
|
+
type AkanImageConfig,
|
|
241
|
+
type AkanImageFormat,
|
|
242
|
+
type AkanImagePattern,
|
|
243
|
+
type BaseBuildArtifact,
|
|
244
|
+
type CssAsset,
|
|
245
|
+
type DictAsset,
|
|
246
|
+
defaultAkanImageConfig,
|
|
247
|
+
getAkanImageWidths,
|
|
248
|
+
getWebConfigFromEnv,
|
|
249
|
+
type HttpRoutes,
|
|
250
|
+
type LocalFile,
|
|
251
|
+
type LocalPublish,
|
|
252
|
+
type MobileDeepLinkAssociation,
|
|
253
|
+
mergeAkanImageConfig,
|
|
254
|
+
type RenderState,
|
|
255
|
+
type ResolvedAkanImageConfig,
|
|
256
|
+
resolveWebConfig,
|
|
257
|
+
type SignalRouteOptions,
|
|
258
|
+
type SignalRoutes,
|
|
259
|
+
type WebsocketRoute,
|
|
260
|
+
type WebsocketRoutes,
|
|
261
|
+
} from "./types";
|
|
@@ -31,6 +31,8 @@ export class LogControlSocket {
|
|
|
31
31
|
static readonly maxPendingBytes = 1024 * 1024;
|
|
32
32
|
static readonly maxInboxBytes = 64 * 1024;
|
|
33
33
|
|
|
34
|
+
static readonly #turns = new Map<string, Promise<void>>();
|
|
35
|
+
|
|
34
36
|
static pathIn(runtimeDir: string) {
|
|
35
37
|
return path.join(runtimeDir, LogControlSocket.fileName);
|
|
36
38
|
}
|
|
@@ -39,6 +41,7 @@ export class LogControlSocket {
|
|
|
39
41
|
readonly #hub: LogHub;
|
|
40
42
|
readonly #logger = new Logger("LogControlSocket");
|
|
41
43
|
#server: ReturnType<typeof Bun.listen<ClientState>> | null = null;
|
|
44
|
+
#generation = 0;
|
|
42
45
|
|
|
43
46
|
constructor(hub: LogHub, runtimeDir: string) {
|
|
44
47
|
this.#hub = hub;
|
|
@@ -59,10 +62,38 @@ export class LogControlSocket {
|
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
async start() {
|
|
65
|
+
const generation = ++this.#generation;
|
|
66
|
+
await this.#takeTurn(() => this.#listen(generation));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async stop() {
|
|
70
|
+
this.#generation += 1;
|
|
71
|
+
await this.#takeTurn(() => this.#close());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async #takeTurn(task: () => Promise<void>) {
|
|
75
|
+
const turns = LogControlSocket.#turns;
|
|
76
|
+
const turn = (turns.get(this.path) ?? Promise.resolve()).then(task);
|
|
77
|
+
const settled = turn.catch(() => undefined);
|
|
78
|
+
turns.set(this.path, settled);
|
|
79
|
+
try {
|
|
80
|
+
await turn;
|
|
81
|
+
} finally {
|
|
82
|
+
if (turns.get(this.path) === settled) turns.delete(this.path);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#abandoned(generation: number) {
|
|
87
|
+
return generation !== this.#generation || this.#hub.closed;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async #listen(generation: number) {
|
|
62
91
|
await mkdir(path.dirname(this.path), { recursive: true });
|
|
63
92
|
|
|
64
93
|
if (await LogControlSocket.#answers(this.path)) throw new Error("another server is listening on it");
|
|
94
|
+
if (this.#abandoned(generation)) return;
|
|
65
95
|
await rm(this.path, { force: true });
|
|
96
|
+
if (this.#abandoned(generation)) return;
|
|
66
97
|
|
|
67
98
|
const umask = process.platform === "win32" ? null : process.umask(0o177);
|
|
68
99
|
try {
|
|
@@ -84,12 +115,20 @@ export class LogControlSocket {
|
|
|
84
115
|
} finally {
|
|
85
116
|
if (umask !== null) process.umask(umask);
|
|
86
117
|
}
|
|
87
|
-
|
|
118
|
+
try {
|
|
119
|
+
await chmod(this.path, 0o600);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
await this.#close();
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
if (this.#abandoned(generation)) await this.#close();
|
|
88
125
|
}
|
|
89
126
|
|
|
90
|
-
async
|
|
91
|
-
this.#server
|
|
127
|
+
async #close() {
|
|
128
|
+
const server = this.#server;
|
|
92
129
|
this.#server = null;
|
|
130
|
+
if (!server) return;
|
|
131
|
+
server.stop(true);
|
|
93
132
|
await rm(this.path, { force: true });
|
|
94
133
|
}
|
|
95
134
|
|