@stage-labs/metro 0.1.0-beta.7 → 0.1.0-beta.70
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/api.js +14 -0
- package/dist/bedrock-eventstream.js +120 -0
- package/dist/bedrock-proxy.js +260 -0
- package/dist/bedrock-upstream.js +72 -0
- package/dist/bedrock.js +106 -0
- package/dist/claude.js +15 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +197 -0
- package/dist/local.js +93 -0
- package/dist/node-name.js +51 -0
- package/dist/plugin.js +80 -0
- package/dist/runtime-install.js +84 -0
- package/dist/runtime.js +70 -0
- package/dist/serve.js +147 -0
- package/dist/service.js +224 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -25
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/package.json +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +311 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +302 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-serve.ts +128 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +204 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/bundle-api.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/claude-api.ts +97 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/claude-files.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-api.ts +307 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth-routes.ts +157 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth.ts +127 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +70 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +330 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/control-api.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/http.ts +384 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity-routes.ts +54 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/local-cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/local-mode.ts +184 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/machine-api.ts +61 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-discovery.ts +117 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/paths.ts +109 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/relay.ts +295 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/server-types.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/servers-api.ts +80 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-api.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +61 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/terminal-api.ts +71 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/terminal-tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/terminal-ws.ts +114 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +352 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/update-api.ts +116 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/vault-api.ts +78 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/vault-types.ts +12 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +84 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-config.ts +144 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-relay.ts +39 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +69 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/file-admin.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/file-source.ts +155 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/key-map.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/local-connectors.ts +277 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +253 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +33 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/servers.ts +83 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/stations.ts +12 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/vault.ts +109 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/accounts.ts +141 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-resolve.ts +200 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach.ts +184 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/lines.ts +82 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/registry.ts +35 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/types.ts +107 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +178 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +327 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/stations.json +36 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -193
- package/dist/cache.js +0 -75
- package/dist/cli/actions.js +0 -139
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -159
- package/dist/cli/skill.js +0 -62
- package/dist/cli/util.js +0 -72
- package/dist/codex-rc.js +0 -236
- package/dist/dispatcher.js +0 -95
- package/dist/history.js +0 -84
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/paths.js +0 -68
- package/dist/stations/discord.js +0 -200
- package/dist/stations/index.js +0 -73
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -213
- package/docs/agents.md +0 -185
- package/docs/uri-scheme.md +0 -71
- package/skills/metro/SKILL.md +0 -241
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { log } from './log.js';
|
|
5
|
+
import { isRecord } from './is-record.js';
|
|
6
|
+
import type { StationName } from '../db/stations.js';
|
|
7
|
+
import { knownAccounts } from '../db/agent-map.js';
|
|
8
|
+
|
|
9
|
+
const INSTALL_TIMEOUT_MS = 15 * 60_000;
|
|
10
|
+
|
|
11
|
+
export interface RuntimeManifest {
|
|
12
|
+
core: Record<string, string>;
|
|
13
|
+
stations: Record<string, Record<string, string>>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RuntimeStore {
|
|
17
|
+
dir: string;
|
|
18
|
+
manifest: RuntimeManifest;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ranges = (raw: unknown): Record<string, string> =>
|
|
22
|
+
isRecord(raw)
|
|
23
|
+
? Object.fromEntries(Object.entries(raw).filter((e): e is [string, string] => typeof e[1] === 'string'))
|
|
24
|
+
: {};
|
|
25
|
+
|
|
26
|
+
export function readManifest(path: string): RuntimeManifest {
|
|
27
|
+
const raw: unknown = JSON.parse(readFileSync(path, 'utf8'));
|
|
28
|
+
if (!isRecord(raw)) throw new Error(`${path} is not a runtime manifest`);
|
|
29
|
+
const stations = isRecord(raw.stations) ? raw.stations : {};
|
|
30
|
+
return {
|
|
31
|
+
core: ranges(raw.core),
|
|
32
|
+
stations: Object.fromEntries(Object.entries(stations).map(([name, deps]) => [name, ranges(deps)])),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function runtimeStore(): RuntimeStore | null {
|
|
37
|
+
const dir = process.env.METRO_RUNTIME_STORE?.trim() ?? '';
|
|
38
|
+
const manifest = process.env.METRO_RUNTIME_MANIFEST?.trim() ?? '';
|
|
39
|
+
if (dir === '' || manifest === '') return null;
|
|
40
|
+
return { dir, manifest: readManifest(manifest) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function dependenciesFor(manifest: RuntimeManifest, stations: Iterable<string>): Record<string, string> {
|
|
44
|
+
const out: Record<string, string> = { ...manifest.core };
|
|
45
|
+
for (const station of new Set(stations)) Object.assign(out, manifest.stations[station] ?? {});
|
|
46
|
+
return Object.fromEntries(Object.entries(out).sort(([a], [b]) => a.localeCompare(b)));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const packageText = (deps: Record<string, string>): string =>
|
|
50
|
+
`${JSON.stringify({ name: 'metro-runtime', private: true, dependencies: deps }, null, 2)}\n`;
|
|
51
|
+
|
|
52
|
+
function current(dir: string): string | null {
|
|
53
|
+
try {
|
|
54
|
+
return readFileSync(join(dir, 'package.json'), 'utf8');
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function installRuntime(store: RuntimeStore, stations: Iterable<string>): boolean {
|
|
61
|
+
const wanted = packageText(dependenciesFor(store.manifest, stations));
|
|
62
|
+
const installed = existsSync(join(store.dir, 'node_modules', '.metro-installed'));
|
|
63
|
+
if (installed && current(store.dir) === wanted) return false;
|
|
64
|
+
mkdirSync(store.dir, { recursive: true });
|
|
65
|
+
writeFileSync(join(store.dir, 'package.json'), wanted);
|
|
66
|
+
log.info({ dir: store.dir }, 'runtime: installing the channel SDKs this machine needs');
|
|
67
|
+
const run = spawnSync('bun', ['install', '--no-summary', '--no-progress'], {
|
|
68
|
+
cwd: store.dir,
|
|
69
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
70
|
+
timeout: INSTALL_TIMEOUT_MS,
|
|
71
|
+
});
|
|
72
|
+
if (run.error !== undefined || run.status !== 0)
|
|
73
|
+
throw new Error(`bun install failed in ${store.dir}: ${run.error?.message ?? `exit ${String(run.status)}`}`);
|
|
74
|
+
writeFileSync(join(store.dir, 'node_modules', '.metro-installed'), `${new Date().toISOString()}\n`);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function ensureStationDeps(station: StationName): void {
|
|
79
|
+
const store = runtimeStore();
|
|
80
|
+
if (store === null) return;
|
|
81
|
+
installRuntime(store, [...knownAccounts().map((a) => a.station), station]);
|
|
82
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chmodSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
renameSync,
|
|
7
|
+
statSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from 'node:fs';
|
|
10
|
+
import { dirname } from 'node:path';
|
|
11
|
+
import { errMsg, log } from './log.js';
|
|
12
|
+
|
|
13
|
+
export function ensureSecureDir(dir: string): void {
|
|
14
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
15
|
+
try {
|
|
16
|
+
chmodSync(dir, 0o700);
|
|
17
|
+
} catch {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function chmodIfExists(path: string, mode = 0o600): void {
|
|
22
|
+
if (!existsSync(path)) return;
|
|
23
|
+
try {
|
|
24
|
+
if ((statSync(path).mode & 0o777) !== mode) chmodSync(path, mode);
|
|
25
|
+
} catch {
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function writeSecure(path: string, data: string): void {
|
|
30
|
+
ensureSecureDir(dirname(path));
|
|
31
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
32
|
+
writeFileSync(tmp, data, { mode: 0o600 });
|
|
33
|
+
try {
|
|
34
|
+
chmodSync(tmp, 0o600);
|
|
35
|
+
} catch {
|
|
36
|
+
}
|
|
37
|
+
renameSync(tmp, path);
|
|
38
|
+
chmodIfExists(path, 0o600);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function readJson<T>(
|
|
42
|
+
path: string,
|
|
43
|
+
fallback: T,
|
|
44
|
+
opts?: { warn?: string },
|
|
45
|
+
): T {
|
|
46
|
+
if (!existsSync(path)) return fallback;
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(readFileSync(path, 'utf8')) as T;
|
|
49
|
+
} catch (err) {
|
|
50
|
+
if (opts?.warn) log.warn({ err: errMsg(err), path }, opts.warn);
|
|
51
|
+
return fallback;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function writeJson(path: string, value: unknown): void {
|
|
56
|
+
writeFileSync(path, JSON.stringify(value, null, 2));
|
|
57
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface ServerEntry {
|
|
2
|
+
id: string;
|
|
3
|
+
host: string;
|
|
4
|
+
name: string | null;
|
|
5
|
+
addedAt: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const HOST_RE = /^[a-z0-9][a-z0-9.-]*(?::\d{1,5})?$/;
|
|
9
|
+
const HOST_MAX = 253;
|
|
10
|
+
const NAME_MAX = 40;
|
|
11
|
+
|
|
12
|
+
export function parseServerHost(raw: string): string | null {
|
|
13
|
+
const host = raw.trim().toLowerCase();
|
|
14
|
+
return host.length <= HOST_MAX && HOST_RE.test(host) ? host : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function parseServerName(raw: unknown): string | null {
|
|
18
|
+
if (typeof raw !== 'string') return null;
|
|
19
|
+
const name = raw.replace(/[\p{Cc}]/gu, '').trim().slice(0, NAME_MAX);
|
|
20
|
+
return name === '' ? null : name;
|
|
21
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { log } from './log.js';
|
|
3
|
+
import { apiFailure, cors, readJsonBody, sendJson } from './api-http.js';
|
|
4
|
+
import { signedIdentity } from './signed-identity.js';
|
|
5
|
+
import type { ServerEntry } from './server-types.js';
|
|
6
|
+
import { parseId } from '../db/ids.js';
|
|
7
|
+
|
|
8
|
+
const PREFIX = '/api/servers';
|
|
9
|
+
|
|
10
|
+
export interface ServersApiDeps {
|
|
11
|
+
list: (subject: string) => Promise<ServerEntry[]>;
|
|
12
|
+
add: (subject: string, body: unknown) => Promise<ServerEntry>;
|
|
13
|
+
rename: (subject: string, id: string, body: unknown) => Promise<ServerEntry>;
|
|
14
|
+
remove: (subject: string, id: string) => Promise<{ id: string; host: string }>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Target = { kind: 'index' } | { kind: 'server'; id: string } | { kind: 'unknown' } | null;
|
|
18
|
+
|
|
19
|
+
function target(path: string): Target {
|
|
20
|
+
if (path === PREFIX || path === `${PREFIX}/`) return { kind: 'index' };
|
|
21
|
+
if (!path.startsWith(`${PREFIX}/`)) return null;
|
|
22
|
+
const segments = path.slice(PREFIX.length + 1).split('/').filter(Boolean);
|
|
23
|
+
const id = parseId(segments[0] ?? '');
|
|
24
|
+
return id === null || segments.length !== 1 ? { kind: 'unknown' } : { kind: 'server', id };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const ALLOWED: Record<'index' | 'server', string[]> = { index: ['GET', 'POST'], server: ['PUT', 'DELETE'] };
|
|
28
|
+
|
|
29
|
+
async function answer(
|
|
30
|
+
req: IncomingMessage,
|
|
31
|
+
deps: ServersApiDeps,
|
|
32
|
+
owner: string,
|
|
33
|
+
tgt: { kind: 'index' } | { kind: 'server'; id: string },
|
|
34
|
+
): Promise<unknown> {
|
|
35
|
+
if (tgt.kind === 'index')
|
|
36
|
+
return req.method === 'GET' ? { servers: await deps.list(owner) } : deps.add(owner, await readJsonBody(req));
|
|
37
|
+
return req.method === 'DELETE' ? deps.remove(owner, tgt.id) : deps.rename(owner, tgt.id, await readJsonBody(req));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function route(
|
|
41
|
+
req: IncomingMessage,
|
|
42
|
+
res: ServerResponse,
|
|
43
|
+
deps: ServersApiDeps,
|
|
44
|
+
tgt: { kind: 'index' } | { kind: 'server'; id: string },
|
|
45
|
+
): Promise<void> {
|
|
46
|
+
try {
|
|
47
|
+
const owner = await signedIdentity(req);
|
|
48
|
+
if (owner === null) {
|
|
49
|
+
sendJson(req, res, 401, { error: 'unauthorized' });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const body = await answer(req, deps, owner, tgt);
|
|
53
|
+
if (req.method !== 'GET') log.info({ method: req.method, target: tgt }, 'servers-api: list changed');
|
|
54
|
+
sendJson(req, res, 200, body);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
apiFailure(req, res, err, 'servers-api');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function handleServersApiRequest(req: IncomingMessage, res: ServerResponse, deps: ServersApiDeps): boolean {
|
|
61
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
62
|
+
const tgt = target(path);
|
|
63
|
+
if (tgt === null) return false;
|
|
64
|
+
if (req.method === 'OPTIONS') {
|
|
65
|
+
res.writeHead(204, cors(req)).end();
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
if (tgt.kind === 'unknown') {
|
|
69
|
+
sendJson(req, res, 404, { error: 'no such server' });
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (!ALLOWED[tgt.kind].includes(req.method ?? '')) {
|
|
73
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
route(req, res, deps, tgt).catch((err: unknown) => {
|
|
77
|
+
apiFailure(req, res, err, 'servers-api');
|
|
78
|
+
});
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { apiFailure, apiSession, cors, sendJson } from './api-http.js';
|
|
3
|
+
|
|
4
|
+
const PATH = '/api/session';
|
|
5
|
+
|
|
6
|
+
export function handleSessionApiRequest(
|
|
7
|
+
req: IncomingMessage,
|
|
8
|
+
res: ServerResponse,
|
|
9
|
+
): boolean {
|
|
10
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
11
|
+
if (path !== PATH) return false;
|
|
12
|
+
if (req.method === 'OPTIONS') {
|
|
13
|
+
res.writeHead(204, cors(req)).end();
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
if (req.method !== 'GET') {
|
|
17
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
apiSession(req)
|
|
21
|
+
.then((session) => {
|
|
22
|
+
if (!session) sendJson(req, res, 401, { error: 'unauthorized' });
|
|
23
|
+
else sendJson(req, res, 200, { subject: session.subject });
|
|
24
|
+
})
|
|
25
|
+
.catch((err: unknown) => {
|
|
26
|
+
apiFailure(req, res, err, 'session');
|
|
27
|
+
});
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { handleSessionApiRequest } from './session-api.js';
|
|
3
|
+
import { handleAgentApiRequest, type AgentApiDeps } from './agent-api.js';
|
|
4
|
+
import {
|
|
5
|
+
handleConnectorApiRequest,
|
|
6
|
+
type ConnectorApiDeps,
|
|
7
|
+
} from './connector-api.js';
|
|
8
|
+
import type { RelayApiDeps } from './relay.js';
|
|
9
|
+
import type { IdentityRouteDeps } from './identity-routes.js';
|
|
10
|
+
import { handleLocalCliRequest, type LocalCliDeps } from './local-cli-api.js';
|
|
11
|
+
import { handleClaudeRequest, type ClaudeApiDeps } from './claude-api.js';
|
|
12
|
+
import type { ModeInfo } from './mode-api.js';
|
|
13
|
+
import { handleVaultApiRequest, type VaultApiDeps } from './vault-api.js';
|
|
14
|
+
import { handleServersApiRequest, type ServersApiDeps } from './servers-api.js';
|
|
15
|
+
import { handleBundleRequest, type BundleApiDeps } from './bundle-api.js';
|
|
16
|
+
import { handleUpdateRequest, type UpdateApiDeps } from './update-api.js';
|
|
17
|
+
import { handleControlRequest, type ControlApiDeps } from './control-api.js';
|
|
18
|
+
import { handleMachineRequest, type MachineApiDeps } from './machine-api.js';
|
|
19
|
+
import { handleTerminalRequest, type TerminalApiDeps } from './terminal-api.js';
|
|
20
|
+
|
|
21
|
+
export interface SessionApis {
|
|
22
|
+
agentApi?: AgentApiDeps;
|
|
23
|
+
bundleApi?: BundleApiDeps;
|
|
24
|
+
vaultApi?: VaultApiDeps;
|
|
25
|
+
serversApi?: ServersApiDeps;
|
|
26
|
+
updateApi?: UpdateApiDeps;
|
|
27
|
+
controlApi?: ControlApiDeps;
|
|
28
|
+
machineApi?: MachineApiDeps;
|
|
29
|
+
terminalApi?: TerminalApiDeps;
|
|
30
|
+
localCli?: LocalCliDeps;
|
|
31
|
+
claudeApi?: ClaudeApiDeps;
|
|
32
|
+
connectorApi?: ConnectorApiDeps;
|
|
33
|
+
relayApi?: RelayApiDeps;
|
|
34
|
+
identity?: IdentityRouteDeps;
|
|
35
|
+
mode?: () => ModeInfo;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const when = <T>(dep: T | undefined, run: (dep: T) => boolean): (() => boolean)[] =>
|
|
39
|
+
dep === undefined ? [] : [() => run(dep)];
|
|
40
|
+
|
|
41
|
+
export function handleSessionApis(
|
|
42
|
+
req: IncomingMessage,
|
|
43
|
+
res: ServerResponse,
|
|
44
|
+
apis: SessionApis,
|
|
45
|
+
): boolean {
|
|
46
|
+
const routes: (() => boolean)[] = [
|
|
47
|
+
() => handleSessionApiRequest(req, res),
|
|
48
|
+
...when(apis.localCli, (d) => handleLocalCliRequest(req, res, d)),
|
|
49
|
+
...when(apis.connectorApi, (d) => handleConnectorApiRequest(req, res, d)),
|
|
50
|
+
...when(apis.bundleApi, (d) => handleBundleRequest(req, res, d)),
|
|
51
|
+
...when(apis.vaultApi, (d) => handleVaultApiRequest(req, res, d)),
|
|
52
|
+
...when(apis.serversApi, (d) => handleServersApiRequest(req, res, d)),
|
|
53
|
+
...when(apis.updateApi, (d) => handleUpdateRequest(req, res, d)),
|
|
54
|
+
...when(apis.controlApi, (d) => handleControlRequest(req, res, d)),
|
|
55
|
+
...when(apis.machineApi, (d) => handleMachineRequest(req, res, d)),
|
|
56
|
+
...when(apis.terminalApi, (d) => handleTerminalRequest(req, res, d)),
|
|
57
|
+
...when(apis.claudeApi, (d) => handleClaudeRequest(req, res, d)),
|
|
58
|
+
...when(apis.agentApi, (d) => handleAgentApiRequest(req, res, d)),
|
|
59
|
+
];
|
|
60
|
+
return routes.some((run) => run());
|
|
61
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import { verifyMessage } from 'viem';
|
|
3
|
+
import { normalizeAddress } from '../db/address.js';
|
|
4
|
+
|
|
5
|
+
export const AUTH_SCHEME = 'Metro';
|
|
6
|
+
const SKEW_MS = 5 * 60_000;
|
|
7
|
+
const SIGNATURE_RE = /^0x[0-9a-fA-F]{130}$/;
|
|
8
|
+
|
|
9
|
+
export const identityChallenge = (method: string, path: string, at: number): string =>
|
|
10
|
+
`metro-auth\n${method} ${path}\n${String(at)}`;
|
|
11
|
+
|
|
12
|
+
export interface SignedHeader {
|
|
13
|
+
address: string;
|
|
14
|
+
at: number;
|
|
15
|
+
signature: `0x${string}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function parseIdentityHeader(header: string): SignedHeader | null {
|
|
19
|
+
const [scheme, rawAddress, rawAt, signature] = header.trim().split(/\s+/);
|
|
20
|
+
if (scheme !== AUTH_SCHEME || rawAddress === undefined || rawAt === undefined || signature === undefined) return null;
|
|
21
|
+
const address = normalizeAddress(rawAddress);
|
|
22
|
+
const at = Number(rawAt);
|
|
23
|
+
if (address === null || !Number.isFinite(at) || !SIGNATURE_RE.test(signature)) return null;
|
|
24
|
+
return { address, at, signature: signature as `0x${string}` };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function signedIdentity(req: IncomingMessage, now = Date.now()): Promise<string | null> {
|
|
28
|
+
const proof = parseIdentityHeader(req.headers.authorization ?? '');
|
|
29
|
+
if (proof === null || Math.abs(now - proof.at) > SKEW_MS) return null;
|
|
30
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
31
|
+
const message = identityChallenge(req.method ?? '', path, proof.at);
|
|
32
|
+
const ok = await verifyMessage({ address: proof.address as `0x${string}`, message, signature: proof.signature }).catch(
|
|
33
|
+
() => false,
|
|
34
|
+
);
|
|
35
|
+
return ok ? proof.address : null;
|
|
36
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { watch, type FSWatcher } from 'node:fs';
|
|
2
|
+
import { join, parse as parsePath } from 'node:path';
|
|
3
|
+
import { errMsg, log } from './log.js';
|
|
4
|
+
import type { Pending } from './protocol.js';
|
|
5
|
+
|
|
6
|
+
export const RESTART_BACKOFFS_MS = [1_000, 5_000, 30_000] as const;
|
|
7
|
+
export const MAX_CONSECUTIVE_FAILS = 5;
|
|
8
|
+
export const HOT_RELOAD_DEBOUNCE_MS = 300;
|
|
9
|
+
export const TRAIN_EXT = /\.(ts|js|mjs)$/;
|
|
10
|
+
|
|
11
|
+
export interface TrainState {
|
|
12
|
+
name: string;
|
|
13
|
+
path: string;
|
|
14
|
+
proc: ReturnType<typeof Bun.spawn> | null;
|
|
15
|
+
pending: Map<string, Pending>;
|
|
16
|
+
buf: string;
|
|
17
|
+
errBuf: string;
|
|
18
|
+
failCount: number;
|
|
19
|
+
restartTimer: ReturnType<typeof setTimeout> | null;
|
|
20
|
+
startedAt: string | null;
|
|
21
|
+
stopped: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function startWatcher(
|
|
25
|
+
dir: string,
|
|
26
|
+
timers: Map<string, ReturnType<typeof setTimeout>>,
|
|
27
|
+
onChange: (name: string, path: string) => void,
|
|
28
|
+
): FSWatcher | null {
|
|
29
|
+
try {
|
|
30
|
+
const w = watch(dir, (_event, filename) => {
|
|
31
|
+
if (!filename) return;
|
|
32
|
+
const base = filename;
|
|
33
|
+
if (!TRAIN_EXT.test(base) || base.startsWith('_') || base.startsWith('.'))
|
|
34
|
+
return;
|
|
35
|
+
const name = parsePath(base).name;
|
|
36
|
+
const existing = timers.get(name);
|
|
37
|
+
if (existing) clearTimeout(existing);
|
|
38
|
+
timers.set(
|
|
39
|
+
name,
|
|
40
|
+
setTimeout(() => {
|
|
41
|
+
timers.delete(name);
|
|
42
|
+
onChange(name, join(dir, base));
|
|
43
|
+
}, HOT_RELOAD_DEBOUNCE_MS),
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
w.on('error', (err) => {
|
|
47
|
+
log.warn({ err: errMsg(err) }, 'train hot-reload: watcher error');
|
|
48
|
+
});
|
|
49
|
+
log.info({ dir }, 'train hot-reload: watching');
|
|
50
|
+
return w;
|
|
51
|
+
} catch (err) {
|
|
52
|
+
log.warn(
|
|
53
|
+
{ err: errMsg(err) },
|
|
54
|
+
'train hot-reload: watch unavailable (on-demand restart only)',
|
|
55
|
+
);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function killGracefully(
|
|
61
|
+
proc: ReturnType<typeof Bun.spawn> | null,
|
|
62
|
+
): Promise<unknown> | null {
|
|
63
|
+
if (proc?.exitCode !== null) return null;
|
|
64
|
+
try {
|
|
65
|
+
proc.kill('SIGTERM');
|
|
66
|
+
} catch {
|
|
67
|
+
}
|
|
68
|
+
const grace = setTimeout(() => {
|
|
69
|
+
try {
|
|
70
|
+
proc.kill('SIGKILL');
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
}, 2_000);
|
|
74
|
+
return proc.exited.finally(() => {
|
|
75
|
+
clearTimeout(grace);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function pumpStream(
|
|
80
|
+
stream: unknown,
|
|
81
|
+
name: string,
|
|
82
|
+
kind: 'stdout' | 'stderr',
|
|
83
|
+
onChunk: (s: string) => void,
|
|
84
|
+
): Promise<void> {
|
|
85
|
+
if (!stream || typeof stream === 'number') return;
|
|
86
|
+
const reader = (stream as ReadableStream<Uint8Array>).getReader();
|
|
87
|
+
const dec = new TextDecoder();
|
|
88
|
+
try {
|
|
89
|
+
while (true) {
|
|
90
|
+
const { value, done } = await reader.read();
|
|
91
|
+
if (done) break;
|
|
92
|
+
onChunk(dec.decode(value, { stream: true }));
|
|
93
|
+
}
|
|
94
|
+
} catch (err) {
|
|
95
|
+
log.debug({ name, err: errMsg(err) }, `train: ${kind} pump ended`);
|
|
96
|
+
}
|
|
97
|
+
}
|