@mostra/runtime 0.8.0 → 0.10.0
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 +2879 -8
- package/dist/bin.js.map +1 -1
- package/dist/builder/child/main.js +2 -2
- package/dist/{child-Chiavhdx.js → child-BmNZ4a70.js} +2 -2
- package/dist/{child-Chiavhdx.js.map → child-BmNZ4a70.js.map} +1 -1
- package/dist/deps/child/install.js +1 -1
- package/dist/{directories-mJ1qlVht.js → directories-BLJjeSPD.js} +5 -5
- package/dist/directories-BLJjeSPD.js.map +1 -0
- package/dist/{files-FygSJIzK.js → files-DGeWuE4I.js} +2 -2
- package/dist/{files-FygSJIzK.js.map → files-DGeWuE4I.js.map} +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/{instance-BRfg27Rg.js → instance-FExcyENK.js} +3346 -395
- package/dist/instance-FExcyENK.js.map +1 -0
- package/package.json +7 -5
- package/src/actions/index.ts +163 -10
- package/src/bin.ts +78 -1
- package/src/gateway/actions.ts +20 -4
- package/src/gateway/browser.ts +118 -95
- package/src/gateway/capabilities.ts +15 -6
- package/src/gateway/index.ts +112 -15
- package/src/gateway/routes.ts +15 -7
- package/src/gc/index.ts +14 -0
- package/src/instance/index.ts +41 -2
- package/src/interaction/ephemeral.ts +32 -23
- package/src/interaction/events.ts +21 -1
- package/src/interaction/imports.ts +317 -0
- package/src/interaction/index.ts +172 -19
- package/src/interaction/outbox.ts +9 -0
- package/src/interaction/results.ts +22 -1
- package/src/interaction/runs.ts +52 -2
- package/src/interaction/submissions.ts +17 -1
- package/src/interaction/tally.ts +129 -0
- package/src/ledger/index.ts +104 -9
- package/src/ledger/migrations.ts +183 -0
- package/src/ledger/types.ts +8 -0
- package/src/link/binding.ts +289 -0
- package/src/link/hosted.ts +65 -0
- package/src/link/http.ts +136 -0
- package/src/link/index.ts +8 -0
- package/src/link/manager.ts +1385 -0
- package/src/link/relay.ts +512 -0
- package/src/live/index.ts +112 -2
- package/src/platform/filesystems.ts +78 -0
- package/src/platform/index.ts +1 -0
- package/src/protocol-server/index.ts +18 -1
- package/src/protocol-server/server.ts +38 -7
- package/src/protocol-server/websocket.ts +161 -0
- package/src/publish/bundle.ts +398 -0
- package/src/publish/manifest.ts +9 -0
- package/src/publish/methods.ts +12 -1
- package/src/publish/publisher.ts +127 -0
- package/src/registry/index.ts +35 -14
- package/src/server/api.ts +439 -0
- package/src/server/auth.ts +613 -0
- package/src/server/config.ts +221 -0
- package/src/server/data.ts +369 -0
- package/src/server/directory.ts +141 -0
- package/src/server/hosted.ts +822 -0
- package/src/server/index.ts +368 -0
- package/src/server/relay.ts +1163 -0
- package/src/share/index.ts +110 -6
- package/dist/directories-mJ1qlVht.js.map +0 -1
- package/dist/instance-BRfg27Rg.js.map +0 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hosted artifact's shared state and live values live on its hosted home (D165 item 1;
|
|
3
|
+
* INTERFACES §18.2): the owner's own `state/get`, `state/patch` and `live/push`, and its server
|
|
4
|
+
* code's `state`, act there through the link, refused with `MOSTRA_REMOTE_UNREACHABLE` when it
|
|
5
|
+
* can't be reached, and are never applied here alone.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
type ArtifactRef,
|
|
9
|
+
livePushMethod,
|
|
10
|
+
MutationId,
|
|
11
|
+
stateGetMethod,
|
|
12
|
+
statePatchMethod,
|
|
13
|
+
} from "@mostra/protocol";
|
|
14
|
+
import type { HostedState } from "../actions/index.ts";
|
|
15
|
+
import type { MethodHandler } from "../protocol-server/index.ts";
|
|
16
|
+
import type { Link } from "./manager.ts";
|
|
17
|
+
|
|
18
|
+
const FORWARDED = new Set([stateGetMethod.method, statePatchMethod.method, livePushMethod.method]);
|
|
19
|
+
|
|
20
|
+
/** Server code's `state` for an artifact hosted somewhere, else `undefined`. */
|
|
21
|
+
export function hostedState(link: Link, artifactId: string): HostedState | undefined {
|
|
22
|
+
const server = link.hostedOn(artifactId);
|
|
23
|
+
if (server === undefined) return undefined;
|
|
24
|
+
const artifact = { artifactId };
|
|
25
|
+
return {
|
|
26
|
+
get: (ids) =>
|
|
27
|
+
link.forward(server, stateGetMethod, {
|
|
28
|
+
artifact,
|
|
29
|
+
...(ids !== undefined ? { ids: [...ids] } : {}),
|
|
30
|
+
}),
|
|
31
|
+
patch: (input) =>
|
|
32
|
+
link.forward(server, statePatchMethod, {
|
|
33
|
+
artifact,
|
|
34
|
+
ops: input.ops as never,
|
|
35
|
+
mutationId: input.mutationId ?? MutationId.create(),
|
|
36
|
+
...(input.baseRevision !== undefined ? { baseRevision: input.baseRevision } : {}),
|
|
37
|
+
as: "agent",
|
|
38
|
+
...(input.runId !== undefined ? { causation: { runId: input.runId } } : {}),
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Wraps the instance's `state/get`, `state/patch` and `live/push` so that, for a hosted artifact,
|
|
45
|
+
* they act on its hosted home: the same params, the artifact named by its ID there.
|
|
46
|
+
*/
|
|
47
|
+
export function forwardHosted(
|
|
48
|
+
link: Link,
|
|
49
|
+
resolve: (ref: ArtifactRef) => string,
|
|
50
|
+
handlers: readonly MethodHandler[],
|
|
51
|
+
): MethodHandler[] {
|
|
52
|
+
return handlers.map((entry) => {
|
|
53
|
+
if (!FORWARDED.has(entry.definition.method)) return entry;
|
|
54
|
+
return {
|
|
55
|
+
definition: entry.definition,
|
|
56
|
+
handle: (params, session) => {
|
|
57
|
+
const input = params as { readonly artifact: ArtifactRef };
|
|
58
|
+
const artifactId = resolve(input.artifact);
|
|
59
|
+
const server = link.hostedOn(artifactId);
|
|
60
|
+
if (server === undefined) return entry.handle(params, session);
|
|
61
|
+
return link.forward(server, entry.definition, { ...input, artifact: { artifactId } });
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
package/src/link/http.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Requests from an owner instance to a remote server's API origin over HTTPS (or HTTP for a
|
|
3
|
+
* loopback test server): the device grant, token refreshes and revocations, and version files
|
|
4
|
+
* (INTERFACES §18.2, §18.5). One place, so every request bounds its time and its answer's size and
|
|
5
|
+
* never logs what it sends. `MOSTRA_TEST_SERVER_ADDRESS` (`host:port`) points every request at a
|
|
6
|
+
* test server listening on loopback under the server's own `Host`.
|
|
7
|
+
*/
|
|
8
|
+
import { request as httpRequest, type IncomingHttpHeaders } from "node:http";
|
|
9
|
+
import { request as httpsRequest } from "node:https";
|
|
10
|
+
import { createDiagnostic, RpcError } from "@mostra/protocol";
|
|
11
|
+
|
|
12
|
+
/** How long a request to the server may take. */
|
|
13
|
+
export const REMOTE_REQUEST_TIMEOUT_MS = 30_000;
|
|
14
|
+
/** The largest answer the instance reads (the API answers JSON; files go the other way). */
|
|
15
|
+
const MAX_ANSWER_BYTES = 4 * 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
/** Where requests for `server` actually go: the test override, or the server's own host. */
|
|
18
|
+
export interface RemoteAddress {
|
|
19
|
+
readonly host: string;
|
|
20
|
+
/** Absent: the server URL's own port. */
|
|
21
|
+
readonly port?: number | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Reads the test override, `host:port`, or nothing. */
|
|
25
|
+
export function testServerAddress(value: string | undefined): RemoteAddress | undefined {
|
|
26
|
+
if (!value) return undefined;
|
|
27
|
+
// A bare address keeps every server URL's own port (tests with several servers).
|
|
28
|
+
if (/^[\d.]+$/.test(value)) return { host: value };
|
|
29
|
+
const at = value.lastIndexOf(":");
|
|
30
|
+
const port = Number(value.slice(at + 1));
|
|
31
|
+
if (at <= 0 || !Number.isInteger(port) || port <= 0 || port > 65535) return undefined;
|
|
32
|
+
return { host: value.slice(0, at).replace(/^\[|\]$/g, ""), port };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** An answer: its status, headers and body. */
|
|
36
|
+
export interface RemoteAnswer {
|
|
37
|
+
readonly status: number;
|
|
38
|
+
readonly headers: IncomingHttpHeaders;
|
|
39
|
+
readonly body: Buffer;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The diagnostic for a server that can't be reached, naming it (never what was sent). */
|
|
43
|
+
export function unreachable(server: string, why: string): RpcError {
|
|
44
|
+
return new RpcError(
|
|
45
|
+
createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", {
|
|
46
|
+
message: `${server} can't be reached: ${why}.`,
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Sends one request to `server` (an API origin) and reads its answer. */
|
|
52
|
+
export function remoteRequest(
|
|
53
|
+
server: string,
|
|
54
|
+
path: string,
|
|
55
|
+
init: {
|
|
56
|
+
readonly method?: string;
|
|
57
|
+
readonly headers?: Record<string, string>;
|
|
58
|
+
readonly body?: string | Buffer;
|
|
59
|
+
readonly connectTo?: RemoteAddress;
|
|
60
|
+
readonly timeoutMs?: number;
|
|
61
|
+
} = {},
|
|
62
|
+
): Promise<RemoteAnswer> {
|
|
63
|
+
const url = new URL(path, server);
|
|
64
|
+
const secure = url.protocol === "https:";
|
|
65
|
+
const send = secure ? httpsRequest : httpRequest;
|
|
66
|
+
const port = Number(url.port) || (secure ? 443 : 80);
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
const outgoing = send(
|
|
69
|
+
{
|
|
70
|
+
host: init.connectTo?.host ?? url.hostname,
|
|
71
|
+
port: init.connectTo?.port ?? port,
|
|
72
|
+
path: `${url.pathname}${url.search}`,
|
|
73
|
+
method: init.method ?? "GET",
|
|
74
|
+
headers: {
|
|
75
|
+
host: url.host,
|
|
76
|
+
...(init.body !== undefined
|
|
77
|
+
? { "content-length": String(Buffer.byteLength(init.body)) }
|
|
78
|
+
: {}),
|
|
79
|
+
...init.headers,
|
|
80
|
+
},
|
|
81
|
+
...(secure ? { servername: url.hostname } : {}),
|
|
82
|
+
timeout: init.timeoutMs ?? REMOTE_REQUEST_TIMEOUT_MS,
|
|
83
|
+
},
|
|
84
|
+
(incoming) => {
|
|
85
|
+
const chunks: Buffer[] = [];
|
|
86
|
+
let size = 0;
|
|
87
|
+
incoming.on("data", (chunk: Buffer) => {
|
|
88
|
+
size += chunk.length;
|
|
89
|
+
if (size > MAX_ANSWER_BYTES) {
|
|
90
|
+
incoming.destroy();
|
|
91
|
+
reject(unreachable(server, "its answer was too large"));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
chunks.push(chunk);
|
|
95
|
+
});
|
|
96
|
+
incoming.on("end", () =>
|
|
97
|
+
resolve({
|
|
98
|
+
status: incoming.statusCode ?? 0,
|
|
99
|
+
headers: incoming.headers,
|
|
100
|
+
body: Buffer.concat(chunks),
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
103
|
+
incoming.on("error", (error) => reject(unreachable(server, error.message)));
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
outgoing.on("timeout", () => outgoing.destroy(new Error("no answer in time")));
|
|
107
|
+
outgoing.on("error", (error) => reject(unreachable(server, error.message)));
|
|
108
|
+
if (init.body !== undefined) outgoing.write(init.body);
|
|
109
|
+
outgoing.end();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Posts a form and reads a JSON answer, whatever its status. */
|
|
114
|
+
export async function postForm(
|
|
115
|
+
server: string,
|
|
116
|
+
path: string,
|
|
117
|
+
fields: Record<string, string>,
|
|
118
|
+
connectTo?: RemoteAddress,
|
|
119
|
+
): Promise<{ status: number; json: Record<string, unknown> }> {
|
|
120
|
+
const answer = await remoteRequest(server, path, {
|
|
121
|
+
method: "POST",
|
|
122
|
+
headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" },
|
|
123
|
+
body: new URLSearchParams(fields).toString(),
|
|
124
|
+
...(connectTo ? { connectTo } : {}),
|
|
125
|
+
});
|
|
126
|
+
let json: Record<string, unknown> = {};
|
|
127
|
+
try {
|
|
128
|
+
const parsed = JSON.parse(answer.body.toString("utf8")) as unknown;
|
|
129
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
130
|
+
json = parsed as Record<string, unknown>;
|
|
131
|
+
}
|
|
132
|
+
} catch {
|
|
133
|
+
// Not JSON: the caller reads the status alone.
|
|
134
|
+
}
|
|
135
|
+
return { status: answer.status, json };
|
|
136
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The owner's side of a remote server (ARCHITECTURE §15.3, INTERFACES §18.2, D165): the network
|
|
3
|
+
* binding, sign-in, the sync cursor and the relay. One public entry, as every runtime module
|
|
4
|
+
* (REPO_STANDARDS §3).
|
|
5
|
+
*/
|
|
6
|
+
export { type Binding, ownerInitialize, startBinding } from "./binding.ts";
|
|
7
|
+
export { forwardHosted, hostedState } from "./hosted.ts";
|
|
8
|
+
export { createLink, type Link } from "./manager.ts";
|