@mostra/runtime 0.6.1 → 0.6.2
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/dist/bin.js +1 -1
- package/dist/index.js +1 -1
- package/dist/{instance-BfGOToHo.js → instance-DpD340-W.js} +60 -3
- package/dist/instance-DpD340-W.js.map +1 -0
- package/package.json +5 -5
- package/src/gateway/index.ts +7 -0
- package/src/gateway/listener.ts +63 -0
- package/src/instance/index.ts +3 -0
- package/dist/instance-BfGOToHo.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mostra/runtime",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "The Mostra runtime: the supervisor that owns a home, its ledger, its child processes and the host protocol server (mostra-runtime host|daemon).",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "The Mostra Authors",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@hono/node-server": "2.1.5",
|
|
38
38
|
"@mdx-js/mdx": "3.1.1",
|
|
39
|
-
"@mostra/protocol": "0.6.
|
|
39
|
+
"@mostra/protocol": "0.6.2",
|
|
40
40
|
"@tailwindcss/vite": "4.3.3",
|
|
41
41
|
"@vitejs/plugin-react": "6.1.2",
|
|
42
42
|
"better-sqlite3": "13.0.3",
|
|
@@ -54,11 +54,11 @@
|
|
|
54
54
|
"zod": "4.6.7"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@mostra/shell": "0.6.
|
|
57
|
+
"@mostra/shell": "0.6.2",
|
|
58
58
|
"@mostra/tools": "0.0.0",
|
|
59
|
-
"@mostra/verify": "0.6.
|
|
59
|
+
"@mostra/verify": "0.6.2",
|
|
60
60
|
"@mostra/warm": "0.0.0",
|
|
61
|
-
"@mostra/worker": "0.6.
|
|
61
|
+
"@mostra/worker": "0.6.2",
|
|
62
62
|
"@types/better-sqlite3": "9.6.0",
|
|
63
63
|
"@types/node": "24.19.2",
|
|
64
64
|
"@types/semver": "7.8.0",
|
package/src/gateway/index.ts
CHANGED
|
@@ -125,6 +125,11 @@ export interface GatewayOptions {
|
|
|
125
125
|
readonly via: IdentityVia;
|
|
126
126
|
readonly headers: Headers;
|
|
127
127
|
}) => Promise<IdentityOutcome>;
|
|
128
|
+
/**
|
|
129
|
+
* Logs every connection and request of every listener (`MOSTRA_TEST_REQUEST_LOG`, #66): tests
|
|
130
|
+
* only, so a request a browser saw go unanswered shows whether it reached the gateway.
|
|
131
|
+
*/
|
|
132
|
+
readonly traceRequests?: boolean;
|
|
128
133
|
/** The browser-protocol revisions served; `BROWSER_PROTOCOL_REVISIONS` unless a test adds one. */
|
|
129
134
|
readonly revisions?: readonly string[];
|
|
130
135
|
/**
|
|
@@ -1042,6 +1047,7 @@ export function createGateway(options: GatewayOptions): Gateway {
|
|
|
1042
1047
|
artifactRoutes(artifactId, () => port),
|
|
1043
1048
|
),
|
|
1044
1049
|
...(options.dev ? { upgrade: devSocket(artifactId, () => port) } : {}),
|
|
1050
|
+
...(options.traceRequests ? { traceRequests: true } : {}),
|
|
1045
1051
|
});
|
|
1046
1052
|
port = await addressing.listen(listener.server, { purpose: "artifact", owner: artifactId });
|
|
1047
1053
|
artifactListeners.set(artifactId, { listener, port });
|
|
@@ -1102,6 +1108,7 @@ export function createGateway(options: GatewayOptions): Gateway {
|
|
|
1102
1108
|
log,
|
|
1103
1109
|
frameAncestors: () => "'none'",
|
|
1104
1110
|
configure: (app) => mount(app, shellChecks, shellRoutes),
|
|
1111
|
+
...(options.traceRequests ? { traceRequests: true } : {}),
|
|
1105
1112
|
});
|
|
1106
1113
|
shellPort = await addressing.listen(shellListener.server, {
|
|
1107
1114
|
purpose: "shell",
|
package/src/gateway/listener.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* theirs, WP-22), and every response leaves with the base headers.
|
|
7
7
|
*/
|
|
8
8
|
import { createServer, type IncomingMessage, type Server } from "node:http";
|
|
9
|
+
import type { Socket } from "node:net";
|
|
9
10
|
import type { Duplex } from "node:stream";
|
|
10
11
|
import { getRequestListener } from "@hono/node-server";
|
|
11
12
|
import {
|
|
@@ -29,6 +30,15 @@ import { refuse } from "./routes.ts";
|
|
|
29
30
|
*/
|
|
30
31
|
export const MAX_HEADER_SIZE: number = 1024 * 1024;
|
|
31
32
|
|
|
33
|
+
/**
|
|
34
|
+
* How long a listener keeps an idle connection open: longer than browsers keep theirs (Chromium
|
|
35
|
+
* 300 s, Firefox 115 s), so the browser reuses its connections and closes them itself (D157 item
|
|
36
|
+
* 2). Node's default, 5 s, made the gateway close first, and each connection it closed stayed in
|
|
37
|
+
* TIME_WAIT on its side, where Chromium's random local ports on Windows collided with it and
|
|
38
|
+
* failed the connect at once with `ERR_NO_BUFFER_SPACE` (#66).
|
|
39
|
+
*/
|
|
40
|
+
export const KEEP_ALIVE_MS: number = 305_000;
|
|
41
|
+
|
|
32
42
|
/** A listener: its server and its Hono app. */
|
|
33
43
|
export interface Listener {
|
|
34
44
|
readonly server: Server;
|
|
@@ -114,6 +124,53 @@ function headersTooLarge(socket: Duplex): void {
|
|
|
114
124
|
});
|
|
115
125
|
}
|
|
116
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Records every connection and request of a listener in the supervisor's log: its arrival and its
|
|
129
|
+
* end, whether it was answered, with the time between them and the client's port, so a request a
|
|
130
|
+
* browser saw go unanswered shows whether it ever reached the gateway (#66). Only a request's
|
|
131
|
+
* method and path are kept, never its query (a frame ticket travels there) or its headers; a test
|
|
132
|
+
* switch (`MOSTRA_TEST_REQUEST_LOG`), since a long-lived daemon would log every asset it serves.
|
|
133
|
+
*/
|
|
134
|
+
function traceRequests(server: Server, log: Log, origin: () => string): void {
|
|
135
|
+
server.on("connection", (socket: Socket) => {
|
|
136
|
+
const opened = Date.now();
|
|
137
|
+
const client = socket.remotePort;
|
|
138
|
+
log.info("gateway.connection", { origin: origin(), client });
|
|
139
|
+
socket.once("close", (hadError: boolean) =>
|
|
140
|
+
log.info("gateway.connection_closed", {
|
|
141
|
+
origin: origin(),
|
|
142
|
+
client,
|
|
143
|
+
ms: Date.now() - opened,
|
|
144
|
+
read: socket.bytesRead,
|
|
145
|
+
written: socket.bytesWritten,
|
|
146
|
+
...(hadError ? { hadError } : {}),
|
|
147
|
+
}),
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
server.prependListener("request", (incoming: IncomingMessage, outgoing) => {
|
|
151
|
+
const started = Date.now();
|
|
152
|
+
const fields = {
|
|
153
|
+
origin: origin(),
|
|
154
|
+
client: incoming.socket.remotePort,
|
|
155
|
+
method: incoming.method,
|
|
156
|
+
path: (incoming.url ?? "").replace(/[?#].*$/s, ""),
|
|
157
|
+
};
|
|
158
|
+
log.info("gateway.request", fields);
|
|
159
|
+
// `finished`: the whole answer was handed to the connection. A stream (events, a long poll)
|
|
160
|
+
// the browser leaves ends unfinished with its headers sent; a request that got nothing ends
|
|
161
|
+
// with neither.
|
|
162
|
+
outgoing.once("close", () =>
|
|
163
|
+
log.info("gateway.request_closed", {
|
|
164
|
+
...fields,
|
|
165
|
+
status: outgoing.statusCode,
|
|
166
|
+
headersSent: outgoing.headersSent,
|
|
167
|
+
finished: outgoing.writableFinished,
|
|
168
|
+
ms: Date.now() - started,
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
117
174
|
/** Creates a listener whose routes `configure` mounts; the server isn't bound yet. */
|
|
118
175
|
export function createListener(options: {
|
|
119
176
|
readonly policy: () => RequestPolicy;
|
|
@@ -129,6 +186,8 @@ export function createListener(options: {
|
|
|
129
186
|
socket: Duplex,
|
|
130
187
|
head: Buffer,
|
|
131
188
|
) => Refusal | undefined;
|
|
189
|
+
/** Logs every connection and request ({@link traceRequests}): tests only. */
|
|
190
|
+
readonly traceRequests?: boolean;
|
|
132
191
|
}): Listener {
|
|
133
192
|
const app = new Hono<HttpEnv>();
|
|
134
193
|
options.configure(app);
|
|
@@ -194,6 +253,7 @@ export function createListener(options: {
|
|
|
194
253
|
}
|
|
195
254
|
void listener(incoming, outgoing);
|
|
196
255
|
});
|
|
256
|
+
if (options.traceRequests) traceRequests(server, options.log, () => options.policy().origin);
|
|
197
257
|
// A client that sends something Node can't parse gets a 400 and its connection closed.
|
|
198
258
|
server.on("clientError", (error: NodeJS.ErrnoException, socket) => {
|
|
199
259
|
if (refusedSockets.has(socket)) return;
|
|
@@ -239,6 +299,9 @@ export function createListener(options: {
|
|
|
239
299
|
});
|
|
240
300
|
server.headersTimeout = 30_000;
|
|
241
301
|
server.requestTimeout = 120_000;
|
|
302
|
+
// An idle connection waits for its next request without these two limits, which count from a
|
|
303
|
+
// request's first byte.
|
|
304
|
+
server.keepAliveTimeout = KEEP_ALIVE_MS;
|
|
242
305
|
|
|
243
306
|
return {
|
|
244
307
|
server,
|
package/src/instance/index.ts
CHANGED
|
@@ -611,9 +611,12 @@ async function assemble(home: OwnedHome, options: StartInstanceOptions): Promise
|
|
|
611
611
|
const shellDir = locateShell(environment);
|
|
612
612
|
if (shellDir === undefined)
|
|
613
613
|
log.warn("gateway.shell_missing", { code: "MOSTRA_ENV_SHELL_MISSING" });
|
|
614
|
+
const traceRequests = Boolean(environment["MOSTRA_TEST_REQUEST_LOG"]);
|
|
615
|
+
if (traceRequests) log.warn("instance.test_request_log", {});
|
|
614
616
|
const gateway: Gateway = createGateway({
|
|
615
617
|
ledger: home.ledger,
|
|
616
618
|
log,
|
|
619
|
+
...(traceRequests ? { traceRequests } : {}),
|
|
617
620
|
addressing,
|
|
618
621
|
capabilities,
|
|
619
622
|
artifacts: {
|