@mostra/runtime 0.9.0 → 0.11.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 +6628 -9
- 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-EnaZPs14.js → instance-DCiUHvgZ.js} +3835 -255
- package/dist/instance-DCiUHvgZ.js.map +1 -0
- package/dist/reference/cli.md +1322 -0
- package/dist/reference/diagnostics.json +1552 -0
- package/dist/reference/errors/MOSTRA_A11Y_MINOR.md +23 -0
- package/dist/reference/errors/MOSTRA_A11Y_SERIOUS.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_CANCELLED.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_EVENT_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_EXPORT_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_INTERRUPTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_PAUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_RUN_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_THREW.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_UNKNOWN_FUNCTION.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_UNSERIALIZABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_BUNDLE_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_DIR_NOT_EMPTY.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_ENTRY_AMBIGUOUS.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_ID_DROPPED.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_NO_ENTRY.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_NO_PAGE.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_PATCH_REJECTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_PATH_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_SOURCE_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_SOURCE_UNREADABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_TEMPLATE_UNKNOWN.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_VERSION_COLLECTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_VERSION_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_CRASHED.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_TIMEOUT.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_UNRESOLVED.md +23 -0
- package/dist/reference/errors/MOSTRA_CLI_CONFIG_UNWRITABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_CLI_UNKNOWN_COMMAND.md +23 -0
- package/dist/reference/errors/MOSTRA_CLI_USAGE.md +23 -0
- package/dist/reference/errors/MOSTRA_CONSOLE_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_CONSOLE_WARNING.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_INSTALL_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_OFFLINE.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_SPEC_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_TIMEOUT.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_WARM_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BIND_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BROWSER_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BUNDLE_DOWNLOAD_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BUNDLE_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BUNDLE_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_HELPER_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_NODE_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_PORT_TAKEN.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_RUNTIME_INSTALLING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_RUNTIME_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_SERVICE_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_SHELL_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_WARM_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_DUPLICATE_ID.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_ID_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_ID_UNSTABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_MUTATION_ID_REUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_SHARED.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_UNBOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_DISK_FULL.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_GC_INCOMPLETE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_GONE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_INTEGRITY.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_LEDGER_UNREADABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_LOCKED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_MISMATCH.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_NETWORK_FILESYSTEM.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_NOT_ALWAYS_ON.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_NOT_OWNED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_SOCKET_TOO_LONG.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_TOO_NEW.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_CONNECTION_CLOSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_CURSOR_COMPACTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_IDENTITY_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_INITIALIZE_ORDER.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_INTERNAL_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_INVALID_PARAMS.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_MALFORMED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_MESSAGE_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_METHOD_UNKNOWN.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_NOTIFICATION_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_PORT_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_REQUEST_ID_REUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_REVISION_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_SHUTDOWN_REFUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_SUBSCRIPTION_UNKNOWN.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_TIMEOUT.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_CONTENT_TYPE.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_CSRF_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_FRAME_SESSION.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_HEADERS_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_HOST_REFUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_NOT_SERVED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_ORIGIN_REFUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_RATE_LIMITED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_REQUEST_MALFORMED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_SCOPE_DENIED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_SESSION_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_SESSION_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_TOKEN_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_TOKEN_REVOKED.md +23 -0
- package/dist/reference/errors/MOSTRA_LIVE_SOURCE_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_LIVE_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_MANIFEST_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_MANIFEST_NOT_LITERAL.md +23 -0
- package/dist/reference/errors/MOSTRA_MANIFEST_UNKNOWN_KEY.md +23 -0
- package/dist/reference/errors/MOSTRA_NETWORK_EXTERNAL_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_NETWORK_REQUEST_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_PLATFORM_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_PUBLISH_ID_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_PUBLISH_SOURCE_CHANGED.md +23 -0
- package/dist/reference/errors/MOSTRA_PUBLISH_VERIFY_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_ACTION_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_ARIA.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_FIELD_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_LIVE_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_PAGE_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_PIXELS.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_CONFIG_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_HOME_MOVING.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_HOME_SUSPENDED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_LIMIT_REACHED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_LOGIN_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_NOT_OWNER_INSTANCE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_NO_INSTANCE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_OPERATOR_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_OWNER_OFFLINE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_PAUSED_BY_OPERATOR.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_RATE_LIMITED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_SIGN_IN_REQUIRED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_UNDER_REVIEW.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_UNREACHABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_BLANK.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_NOT_READY.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_PAGE_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_WORKER_CRASHED.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_WORKER_PARKED.md +23 -0
- package/dist/reference/errors/MOSTRA_SECRET_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_NOT_ENABLED.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_PORTS_EXHAUSTED.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TOOL_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TOOL_NOT_READY.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TTL_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TUNNEL_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_SMOKE_BUDGET.md +23 -0
- package/dist/reference/errors/MOSTRA_SMOKE_NO_EFFECT.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_ALREADY.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_BLOCKED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_CHANNEL_DENIED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_PAUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_POLICY_NONE.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_REVISION_MOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_SIGNAL_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_VIEW_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_TARGET_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_TYPE_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_TYPE_UNTYPED_PACKAGE.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_ACTION_SKIPPED.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_BASELINE_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_CRASHED.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_LIVE_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_NO_BASELINE.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_SECRET_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_SKIP_CONSEQUENCE.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_TIMEOUT.md +23 -0
- package/dist/reference/self-hosting.md +130 -0
- package/dist/reference/telemetry.md +84 -0
- package/dist/skill/SKILL.md +122 -0
- package/dist/skill/assets/examples/dashboard/index.tsx +161 -0
- package/dist/skill/assets/examples/dashboard/live.ts +25 -0
- package/dist/skill/assets/examples/dashboard/styles.css +125 -0
- package/dist/skill/assets/examples/decision/index.tsx +95 -0
- package/dist/skill/assets/examples/decision/styles.css +125 -0
- package/dist/skill/assets/examples/inbox/actions.ts +23 -0
- package/dist/skill/assets/examples/inbox/index.tsx +109 -0
- package/dist/skill/assets/examples/inbox/styles.css +125 -0
- package/dist/skill/assets/studio.css +125 -0
- package/dist/skill/references/answers.md +217 -0
- package/dist/skill/references/design.md +154 -0
- package/dist/skill/references/libraries.md +167 -0
- package/dist/skill/references/long-lived.md +148 -0
- package/dist/skill/references/mcp.md +68 -0
- package/dist/skill/references/page.md +131 -0
- package/dist/skill/references/server.md +146 -0
- package/dist/skill/references/sharing.md +123 -0
- package/dist/skill/references/verify.md +71 -0
- package/package.json +12 -5
- package/src/actions/index.ts +163 -10
- package/src/bin.ts +78 -1
- package/src/gateway/actions.ts +40 -4
- package/src/gateway/browser.ts +162 -95
- package/src/gateway/capabilities.ts +23 -6
- package/src/gateway/http.ts +12 -1
- package/src/gateway/index.ts +282 -20
- package/src/gateway/listener.ts +31 -6
- package/src/gateway/routes.ts +23 -7
- package/src/gc/index.ts +19 -0
- package/src/instance/index.ts +60 -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 +228 -15
- package/src/interaction/outbox.ts +9 -0
- package/src/interaction/pause.ts +106 -0
- package/src/interaction/results.ts +8 -1
- package/src/interaction/runs.ts +52 -2
- package/src/interaction/submissions.ts +59 -2
- package/src/interaction/tally.ts +129 -0
- package/src/interaction/viewers.ts +34 -1
- package/src/ledger/index.ts +124 -10
- package/src/ledger/migrations.ts +234 -0
- package/src/ledger/types.ts +8 -0
- package/src/link/author.ts +215 -0
- package/src/link/binding.ts +289 -0
- package/src/link/hosted.ts +101 -0
- package/src/link/http.ts +141 -0
- package/src/link/index.ts +10 -0
- package/src/link/manager.ts +1489 -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/client.ts +53 -0
- package/src/protocol-server/index.ts +19 -2
- 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 +881 -0
- package/src/server/auth.ts +1139 -0
- package/src/server/authorize.ts +816 -0
- package/src/server/clients.ts +235 -0
- package/src/server/config.ts +228 -0
- package/src/server/data.ts +427 -0
- package/src/server/directory.ts +210 -0
- package/src/server/feed.ts +191 -0
- package/src/server/handle.ts +172 -0
- package/src/server/hosted-pages.ts +532 -0
- package/src/server/hosted.ts +1323 -0
- package/src/server/index.ts +650 -0
- package/src/server/mcp.ts +330 -0
- package/src/server/metering.ts +156 -0
- package/src/server/oauth-clients.ts +510 -0
- package/src/server/operator.ts +498 -0
- package/src/server/providers.ts +284 -0
- package/src/server/relay.ts +1163 -0
- package/src/server/remove.ts +148 -0
- package/src/server/routing.ts +275 -0
- package/src/share/index.ts +110 -6
- package/src/verify/runner.ts +23 -2
- package/dist/directories-mJ1qlVht.js.map +0 -1
- package/dist/instance-EnaZPs14.js.map +0 -1
|
@@ -0,0 +1,1323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hosted home (ARCHITECTURE §15.2, DATA_MODEL §7.2, D165): a Mostra home on a remote server with
|
|
3
|
+
* the same ledger, registry, interaction core and events as a local one, and no sources, builds,
|
|
4
|
+
* verify, workers, dependency store or secrets. Its versions arrive from the owner instance
|
|
5
|
+
* (`hosted/push`): the client files the listing names, checked against `outputDigest`, never the
|
|
6
|
+
* server files; its server code runs on the owner, through the relay.
|
|
7
|
+
*
|
|
8
|
+
* Its host protocol is served by a {@link ProtocolServer} of its own, reached over the network
|
|
9
|
+
* binding (§18.2), so the owner instance is a host of its hosted home as any host is of an
|
|
10
|
+
* instance (invariant 11): it registers `submissions`, and a submission's one delivery owner is its
|
|
11
|
+
* artifact's owner link (D165, after the Opus review).
|
|
12
|
+
*/
|
|
13
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
14
|
+
import { linkSync, mkdirSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { copyFile, readFile, rm, stat } from "node:fs/promises";
|
|
16
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
17
|
+
import { isAbsolute, join, relative } from "node:path";
|
|
18
|
+
import {
|
|
19
|
+
ArtifactManifest,
|
|
20
|
+
artifactGetMethod,
|
|
21
|
+
artifactListMethod,
|
|
22
|
+
artifactPauseMethod,
|
|
23
|
+
createDiagnostic,
|
|
24
|
+
type HostedPushParams,
|
|
25
|
+
type HostedPushResult,
|
|
26
|
+
type HostedRoute,
|
|
27
|
+
hostedPushMethod,
|
|
28
|
+
hostedSubmissionMethod,
|
|
29
|
+
type InstanceInfo,
|
|
30
|
+
type PausedBy,
|
|
31
|
+
RouteId,
|
|
32
|
+
RpcError,
|
|
33
|
+
remoteTokenMethod,
|
|
34
|
+
viewUrlMethod,
|
|
35
|
+
} from "@mostra/protocol";
|
|
36
|
+
import type { Addressing } from "../addressing/index.ts";
|
|
37
|
+
import { coverageMatcher, readFieldCoverage, readServerFunctions } from "../builder/index.ts";
|
|
38
|
+
import { type Faults, noFaults } from "../faults.ts";
|
|
39
|
+
import {
|
|
40
|
+
Capabilities,
|
|
41
|
+
capabilityKey,
|
|
42
|
+
createGateway,
|
|
43
|
+
type FaceSpec,
|
|
44
|
+
type Gateway,
|
|
45
|
+
} from "../gateway/index.ts";
|
|
46
|
+
import { platformName } from "../home/index.ts";
|
|
47
|
+
import { createEphemeralBus, createInteraction, type Interaction } from "../interaction/index.ts";
|
|
48
|
+
import type { Ledger } from "../ledger/index.ts";
|
|
49
|
+
import { createLive, liveMethods } from "../live/index.ts";
|
|
50
|
+
import type { Log } from "../log.ts";
|
|
51
|
+
import { removeTree } from "../platform/index.ts";
|
|
52
|
+
import {
|
|
53
|
+
handler,
|
|
54
|
+
type MethodHandler,
|
|
55
|
+
ProtocolServer,
|
|
56
|
+
type Session,
|
|
57
|
+
} from "../protocol-server/index.ts";
|
|
58
|
+
import { canonicalRef, listingDigest, versionIdFor } from "../publish/index.ts";
|
|
59
|
+
import { openRegistry, type Registry } from "../registry/index.ts";
|
|
60
|
+
import type { Authorization } from "./auth.ts";
|
|
61
|
+
import type { ClientAddresses, RateLimiter } from "./clients.ts";
|
|
62
|
+
import { rotateIncarnation } from "./data.ts";
|
|
63
|
+
import { createOperatorFeed, type OperatorFeed } from "./feed.ts";
|
|
64
|
+
import { type HomeOwner, type HostedPagesOptions, hostedPages } from "./hosted-pages.ts";
|
|
65
|
+
import type { Meter } from "./metering.ts";
|
|
66
|
+
import type { Operator } from "./operator.ts";
|
|
67
|
+
import { createHostedRelay } from "./relay.ts";
|
|
68
|
+
import { purgeArtifact } from "./remove.ts";
|
|
69
|
+
import type { Routing } from "./routing.ts";
|
|
70
|
+
|
|
71
|
+
/** Lower-case Crockford base32, for route labels (one DNS label, 130 bits). */
|
|
72
|
+
const LABEL_ALPHABET = "0123456789abcdefghjkmnpqrstvwxyz";
|
|
73
|
+
|
|
74
|
+
/** A random route label: 26 lower-case Crockford base32 characters (INTERFACES §18.4). */
|
|
75
|
+
|
|
76
|
+
/** A character no part of a listing's path may hold: a backslash, a colon, a control character. */
|
|
77
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it refuses
|
|
78
|
+
const UNSAFE_PART = /[\\:\u0000-\u001f\u007f]/;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether a pushed listing's path is one a version can hold on every system the server runs on: `/`
|
|
82
|
+
* between non-empty parts, none `.` or `..`, and no backslash, colon or control character, which
|
|
83
|
+
* Windows reads as separators, drive or stream names, or refuses (Opus review of WP-29, 5); in NFC,
|
|
84
|
+
* as listings are written (DATA_MODEL §2).
|
|
85
|
+
*/
|
|
86
|
+
function safeListingPath(path: string): boolean {
|
|
87
|
+
if (path.length > 1024 || path.normalize("NFC") !== path) return false;
|
|
88
|
+
return path
|
|
89
|
+
.split("/")
|
|
90
|
+
.every((part) => part !== "" && part !== "." && part !== ".." && !UNSAFE_PART.test(part));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function randomLabel(): string {
|
|
94
|
+
const bytes = randomBytes(26);
|
|
95
|
+
let label = "";
|
|
96
|
+
for (const byte of bytes) label += LABEL_ALPHABET[byte & 31];
|
|
97
|
+
return label;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** What the server keeps of one open hosted home. */
|
|
101
|
+
export interface HostedHome {
|
|
102
|
+
readonly id: string;
|
|
103
|
+
readonly dir: string;
|
|
104
|
+
readonly ledger: Ledger;
|
|
105
|
+
readonly registry: Registry;
|
|
106
|
+
readonly interaction: Interaction;
|
|
107
|
+
/** The home's host protocol, served over the binding. */
|
|
108
|
+
readonly protocol: ProtocolServer;
|
|
109
|
+
/** The home's current incarnation (DATA_MODEL §7.2). */
|
|
110
|
+
incarnation(): string;
|
|
111
|
+
/**
|
|
112
|
+
* The size a pending push of this home gives a client file it names as missing, or `undefined`
|
|
113
|
+
* when none names it: an upload must bring exactly those bytes (Fable review of WP-30, 2).
|
|
114
|
+
*/
|
|
115
|
+
wanted(sha256: string): number | undefined;
|
|
116
|
+
/** Records a file this home uploaded (`PUT /files/<sha256>`, §18.2). */
|
|
117
|
+
uploaded(sha256: string): void;
|
|
118
|
+
/** Pauses or resumes an artifact as its owner or the operator (D165 item 14). */
|
|
119
|
+
pause(
|
|
120
|
+
artifactId: string,
|
|
121
|
+
change: { readonly paused: boolean; readonly by: PausedBy; readonly reason?: string },
|
|
122
|
+
): { readonly paused: boolean; readonly by?: PausedBy };
|
|
123
|
+
/** Quarantines an artifact, or one version of it, here only (D165 item 9). */
|
|
124
|
+
quarantine(artifactId: string, versionId: string | undefined, reason: string): void;
|
|
125
|
+
/** Ends a quarantine: the artifact's, or one version's. */
|
|
126
|
+
release(artifactId: string, versionId: string | undefined): void;
|
|
127
|
+
/** Revokes every live link of an artifact; answers how many were live. */
|
|
128
|
+
revokeLinks(artifactId: string): number;
|
|
129
|
+
/** Revokes one link; `false` when this home has no such link. */
|
|
130
|
+
revokeLink(tokenId: string): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Removes an artifact and everything of it from this home (`ServerHandle.remove`): its rows, its
|
|
133
|
+
* version files no other version of the home names, and its route; its label is kept issued
|
|
134
|
+
* forever, never served again (DATA_MODEL §7.5).
|
|
135
|
+
*/
|
|
136
|
+
remove(artifactId: string): Promise<{ readonly rows: number }>;
|
|
137
|
+
/**
|
|
138
|
+
* The session of the account's default owner instance, while it is connected: where remote MCP's
|
|
139
|
+
* authoring goes (D165 items 1b, 5). The newest session of the link is its own.
|
|
140
|
+
*/
|
|
141
|
+
defaultOwnerSession(): number | undefined;
|
|
142
|
+
/** Whether a binding is open, a stream or poll is served, or a delivery waits. */
|
|
143
|
+
busy(): boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Serves a request the server's front routed here by its exact `Host` (INTERFACES §18.4): the
|
|
146
|
+
* shell on a user-content domain's apex (`/a/<routeId>…`), or a route's frames on its label.
|
|
147
|
+
*/
|
|
148
|
+
serve(request: IncomingMessage, response: ServerResponse, at: HostedAddress): Promise<void>;
|
|
149
|
+
stop(): Promise<void>;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Where a hosted request came in: a domain's apex, or a route's label on it. */
|
|
153
|
+
export type HostedAddress =
|
|
154
|
+
| { readonly kind: "shell"; readonly domain: string; readonly routeId: string }
|
|
155
|
+
| { readonly kind: "frame"; readonly domain: string; readonly routeId: string };
|
|
156
|
+
|
|
157
|
+
/** Options for {@link openHostedHome}. */
|
|
158
|
+
export interface HostedHomeOptions {
|
|
159
|
+
readonly id: string;
|
|
160
|
+
readonly dir: string;
|
|
161
|
+
readonly ledger: Ledger;
|
|
162
|
+
readonly incarnation: string;
|
|
163
|
+
/** The server's directory ledger (routes, files, links). */
|
|
164
|
+
readonly directory: Ledger;
|
|
165
|
+
/** `<data>/files`: version files, content-addressed, stored once for the directory. */
|
|
166
|
+
readonly filesDir: string;
|
|
167
|
+
readonly auth: Authorization;
|
|
168
|
+
readonly runtimeVersion: string;
|
|
169
|
+
readonly log: Log;
|
|
170
|
+
/** `https` behind a TLS terminator; `http` only for loopback tests (§18.1). */
|
|
171
|
+
readonly scheme: "https" | "http";
|
|
172
|
+
/** The user-content domains: new links on `active`, issued ones keep theirs (D137). */
|
|
173
|
+
readonly domains: { readonly active: string; readonly spares: readonly string[] };
|
|
174
|
+
/** The shell's built files. */
|
|
175
|
+
readonly shellDir: string | undefined;
|
|
176
|
+
/** The relay's server settings (`relay` in the configuration, D165 item 4). */
|
|
177
|
+
/** The test fault points (`MOSTRA_TEST_FAULTS`); none by default. */
|
|
178
|
+
readonly faults?: Faults;
|
|
179
|
+
readonly relay?: {
|
|
180
|
+
readonly waitingRunsPerArtifact?: number | undefined;
|
|
181
|
+
readonly drainPerSecond?: number | undefined;
|
|
182
|
+
/** How often waiting and unacknowledged runs meet their limits (tests shorten it). */
|
|
183
|
+
readonly sweepMs?: number | undefined;
|
|
184
|
+
/** How often viewers' live interest is renewed with the owner (tests shorten it). */
|
|
185
|
+
readonly liveRenewMs?: number | undefined;
|
|
186
|
+
/** `rateLimits.perLink.relayedRuns`, in milliseconds. */
|
|
187
|
+
readonly perLink?: { readonly count: number; readonly perMs: number } | undefined;
|
|
188
|
+
};
|
|
189
|
+
/** What the server lends every hosted home: its operator, client addresses and rates (WP-30). */
|
|
190
|
+
readonly services?: HostedServices;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** What the server lends every hosted home (WP-30). */
|
|
194
|
+
export interface HostedServices {
|
|
195
|
+
readonly operator: Operator;
|
|
196
|
+
readonly clients: ClientAddresses;
|
|
197
|
+
readonly rates: RateLimiter;
|
|
198
|
+
readonly rateLimits: HostedPagesOptions["rateLimits"];
|
|
199
|
+
/** Who owns a home, as its pages' sender line names them. */
|
|
200
|
+
readonly owner: (homeId: string) => HomeOwner;
|
|
201
|
+
readonly signInOffered: () => boolean;
|
|
202
|
+
readonly madeWith?: { readonly href?: string } | undefined;
|
|
203
|
+
/** The server's routing by exact `Host`: operator hosts, their frames and link hosts (D167). */
|
|
204
|
+
readonly routing: Routing;
|
|
205
|
+
/** The server's meter: views here, submissions through the operator feed. */
|
|
206
|
+
readonly meter: Meter;
|
|
207
|
+
/** Viewer sign-in on the authorization server (INTERFACES §18.4). */
|
|
208
|
+
readonly signin?: HostedPagesOptions["signin"];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function refuse(code: Parameters<typeof createDiagnostic>[0], message: string): RpcError {
|
|
212
|
+
return new RpcError(createDiagnostic(code, { message }));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Opens the parts of a hosted home over its ledger. */
|
|
216
|
+
export async function openHostedHome(options: HostedHomeOptions): Promise<HostedHome> {
|
|
217
|
+
const faults = options.faults ?? noFaults;
|
|
218
|
+
const { id, dir, ledger, directory, log } = options;
|
|
219
|
+
const registry = await openRegistry({ home: dir, ledger });
|
|
220
|
+
/** Pending pushes: version ID → each missing client file's SHA-256 and the size its listing gives. */
|
|
221
|
+
const pending = new Map<string, Map<string, number>>();
|
|
222
|
+
let incarnation = options.incarnation;
|
|
223
|
+
|
|
224
|
+
const ownerLinkOf = (artifactId: string): string | undefined =>
|
|
225
|
+
directory.read(
|
|
226
|
+
(db) =>
|
|
227
|
+
db.get<{ owner_link_id: string }>(
|
|
228
|
+
"SELECT owner_link_id FROM artifact_route WHERE home_id = ? AND artifact_id = ?",
|
|
229
|
+
id,
|
|
230
|
+
artifactId,
|
|
231
|
+
)?.owner_link_id,
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const coverage = (() => {
|
|
235
|
+
const cache = new Map<string, (fieldId: string) => boolean>();
|
|
236
|
+
return (versionId: string) => {
|
|
237
|
+
let matcher = cache.get(versionId);
|
|
238
|
+
if (!matcher) {
|
|
239
|
+
matcher = coverageMatcher(readFieldCoverage(registry.store.versionDir(versionId)));
|
|
240
|
+
if (cache.size >= 512) cache.delete(cache.keys().next().value as string);
|
|
241
|
+
cache.set(versionId, matcher);
|
|
242
|
+
}
|
|
243
|
+
return matcher;
|
|
244
|
+
};
|
|
245
|
+
})();
|
|
246
|
+
|
|
247
|
+
let protocol: ProtocolServer;
|
|
248
|
+
const interaction = createInteraction({
|
|
249
|
+
// A respondent on a hosted link learns only that the answer was sent (D165 item 1a).
|
|
250
|
+
sentOnly: true,
|
|
251
|
+
ledger,
|
|
252
|
+
log,
|
|
253
|
+
faults: noFaults,
|
|
254
|
+
hosts: {
|
|
255
|
+
registrants: (port) => protocol.registrants(port),
|
|
256
|
+
call: (session, method, params, timeoutMs) =>
|
|
257
|
+
protocol.callSession(session, method, params as never, timeoutMs),
|
|
258
|
+
notify: (session, method, params) => protocol.notify(session, method, params as never),
|
|
259
|
+
drained: (session) => protocol.drained(session),
|
|
260
|
+
},
|
|
261
|
+
expectedHosts: () => [],
|
|
262
|
+
artifacts: { resolve: (ref) => registry.get(canonicalRef(ref)).id },
|
|
263
|
+
coverage,
|
|
264
|
+
// A submission's one delivery owner is its artifact's owner link, connected or not, so it
|
|
265
|
+
// waits for that instance and no other instance of the account receives it (D165 item 1b).
|
|
266
|
+
deliverSnapshots: true,
|
|
267
|
+
deliveryOwners: (artifactId) => {
|
|
268
|
+
const owner = ownerLinkOf(artifactId);
|
|
269
|
+
return owner === undefined ? undefined : [owner];
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
/** The route of an artifact on this home, minted on its first push by `link`. */
|
|
274
|
+
const routeFor = (artifactId: string, link: string): HostedRoute =>
|
|
275
|
+
directory.write((db) => {
|
|
276
|
+
const found = db.get<{
|
|
277
|
+
id: string;
|
|
278
|
+
label: string;
|
|
279
|
+
owner_link_id: string;
|
|
280
|
+
removing_json: string | null;
|
|
281
|
+
}>(
|
|
282
|
+
"SELECT id, label, owner_link_id, removing_json FROM artifact_route WHERE home_id = ? AND artifact_id = ?",
|
|
283
|
+
id,
|
|
284
|
+
artifactId,
|
|
285
|
+
);
|
|
286
|
+
if (found?.removing_json != null) {
|
|
287
|
+
throw refuse(
|
|
288
|
+
"MOSTRA_REMOTE_HOME_MOVING",
|
|
289
|
+
"This artifact is being removed from the server; push it again once that's done.",
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
if (found) {
|
|
293
|
+
if (found.owner_link_id !== link) {
|
|
294
|
+
const owner = db.get<{ name: string }>(
|
|
295
|
+
"SELECT name FROM instance_link WHERE id = ?",
|
|
296
|
+
found.owner_link_id,
|
|
297
|
+
);
|
|
298
|
+
throw refuse(
|
|
299
|
+
"MOSTRA_REMOTE_NOT_OWNER_INSTANCE",
|
|
300
|
+
`The instance "${owner?.name ?? found.owner_link_id}" owns this hosted artifact; push it from there.`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return { routeId: found.id, label: found.label };
|
|
304
|
+
}
|
|
305
|
+
const routeId = RouteId.create();
|
|
306
|
+
let label = randomLabel();
|
|
307
|
+
// A label is issued once, forever (label_issued keeps every one): a collision draws again.
|
|
308
|
+
while (db.get("SELECT 1 FROM label_issued WHERE label = ?", label)) label = randomLabel();
|
|
309
|
+
const at = new Date().toISOString();
|
|
310
|
+
db.run(
|
|
311
|
+
"INSERT INTO label_issued (label, home_id, route_id, kind, issued_at) VALUES (?, ?, ?, 'server', ?)",
|
|
312
|
+
label,
|
|
313
|
+
id,
|
|
314
|
+
routeId,
|
|
315
|
+
at,
|
|
316
|
+
);
|
|
317
|
+
db.run(
|
|
318
|
+
"INSERT INTO artifact_route (id, home_id, artifact_id, owner_link_id, label, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
|
319
|
+
routeId,
|
|
320
|
+
id,
|
|
321
|
+
artifactId,
|
|
322
|
+
link,
|
|
323
|
+
label,
|
|
324
|
+
at,
|
|
325
|
+
);
|
|
326
|
+
log.info("server.route_minted", { home: id, artifactId, routeId });
|
|
327
|
+
return { routeId, label };
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// ── Hosted pages (INTERFACES §18.4) ────────────────────────────────────────────────────
|
|
331
|
+
|
|
332
|
+
// A route being removed serves nothing (its removal finishes on the next call).
|
|
333
|
+
const routeOf = (artifactId: string): { id: string; label: string } | undefined =>
|
|
334
|
+
directory.read((db) =>
|
|
335
|
+
db.get<{ id: string; label: string }>(
|
|
336
|
+
"SELECT id, label FROM artifact_route WHERE home_id = ? AND artifact_id = ? AND removing_json IS NULL",
|
|
337
|
+
id,
|
|
338
|
+
artifactId,
|
|
339
|
+
),
|
|
340
|
+
);
|
|
341
|
+
const artifactOfRoute = (routeId: string): string | undefined =>
|
|
342
|
+
directory.read(
|
|
343
|
+
(db) =>
|
|
344
|
+
db.get<{ artifact_id: string }>(
|
|
345
|
+
"SELECT artifact_id FROM artifact_route WHERE id = ? AND home_id = ? AND removing_json IS NULL",
|
|
346
|
+
routeId,
|
|
347
|
+
id,
|
|
348
|
+
)?.artifact_id,
|
|
349
|
+
);
|
|
350
|
+
const domains = [options.domains.active, ...options.domains.spares];
|
|
351
|
+
const originOf = (host: string): string => `${options.scheme}://${host}`;
|
|
352
|
+
/** The host of one of this server's origins, or `undefined` for another scheme. */
|
|
353
|
+
const hostOfOrigin = (origin: string): string | undefined =>
|
|
354
|
+
origin.startsWith(`${options.scheme}://`) ? origin.slice(options.scheme.length + 3) : undefined;
|
|
355
|
+
/** Whether a shell origin is this home's: a configured apex, or an operator host of it. */
|
|
356
|
+
const servesShell = (origin: string): boolean => {
|
|
357
|
+
const host = hostOfOrigin(origin);
|
|
358
|
+
if (host === undefined) return false;
|
|
359
|
+
return options.services
|
|
360
|
+
? options.services.routing.servesShell(id, host)
|
|
361
|
+
: domains.includes(host);
|
|
362
|
+
};
|
|
363
|
+
const capabilities: Capabilities = new Capabilities({
|
|
364
|
+
ledger,
|
|
365
|
+
log,
|
|
366
|
+
key: capabilityKey(dir, log),
|
|
367
|
+
now: () => Date.now(),
|
|
368
|
+
shellOrigin: () => originOf(options.domains.active),
|
|
369
|
+
servesShell,
|
|
370
|
+
// Every hosted link is indexed in the directory, so the operator's `revokeLink` finds its home.
|
|
371
|
+
onMinted: ({ tokenId, artifactId }) => {
|
|
372
|
+
const route = routeOf(artifactId);
|
|
373
|
+
if (!route) return;
|
|
374
|
+
directory.write((db) =>
|
|
375
|
+
db.run(
|
|
376
|
+
"INSERT OR IGNORE INTO hosted_token (token_id, home_id, route_id) VALUES (?, ?, ?)",
|
|
377
|
+
tokenId,
|
|
378
|
+
id,
|
|
379
|
+
route.id,
|
|
380
|
+
),
|
|
381
|
+
);
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
// A hosted home binds nothing: its faces are served through the front. These answer the few
|
|
385
|
+
// questions the gateway asks of loopback addressing, none of which a hosted face reaches.
|
|
386
|
+
const addressing: Addressing = {
|
|
387
|
+
bindAddress: "",
|
|
388
|
+
urlHost: options.domains.active,
|
|
389
|
+
origin: () => originOf(options.domains.active),
|
|
390
|
+
hosts: () => [],
|
|
391
|
+
leased: () => undefined,
|
|
392
|
+
leases: () => [],
|
|
393
|
+
listen: () => Promise.reject(new Error("A hosted home binds no listener.")),
|
|
394
|
+
frameMode: () => "worker",
|
|
395
|
+
listenFace: () => Promise.reject(new Error("A hosted home binds no listener.")),
|
|
396
|
+
releaseFace: () => undefined,
|
|
397
|
+
faceLocal: () => "",
|
|
398
|
+
};
|
|
399
|
+
const bus = createEphemeralBus();
|
|
400
|
+
// Live values a hosted page reads: tallies (counted here, RM-07) and the values its owner's
|
|
401
|
+
// producers push through the relay. No producer runs here: the home runs no artifact code.
|
|
402
|
+
const noPool = new Proxy(
|
|
403
|
+
{},
|
|
404
|
+
{
|
|
405
|
+
get: () => {
|
|
406
|
+
throw new Error("A hosted home runs no workers.");
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
) as Parameters<typeof createLive>[0]["pool"];
|
|
410
|
+
const live = createLive({
|
|
411
|
+
ledger,
|
|
412
|
+
log,
|
|
413
|
+
bus,
|
|
414
|
+
pool: noPool,
|
|
415
|
+
interaction,
|
|
416
|
+
latest: () => undefined,
|
|
417
|
+
artifactsWithLive: () => [],
|
|
418
|
+
version: () => undefined,
|
|
419
|
+
});
|
|
420
|
+
// The relay: a hosted page's runs and calls run on its owner instance (D165 item 4).
|
|
421
|
+
const relay = createHostedRelay({
|
|
422
|
+
ledger,
|
|
423
|
+
interaction,
|
|
424
|
+
bus,
|
|
425
|
+
log,
|
|
426
|
+
ownerOf: (artifactId) => ownerLinkOf(artifactId),
|
|
427
|
+
// The newest session of a link is its own: an older one is a connection the owner already
|
|
428
|
+
// left, which the server may hold until its silence limit (Opus review of WP-29, 1).
|
|
429
|
+
sessionOf: (linkId) =>
|
|
430
|
+
protocol
|
|
431
|
+
.registrants("relay")
|
|
432
|
+
.filter((entry) => entry.owner === linkId)
|
|
433
|
+
.at(-1)?.session,
|
|
434
|
+
call: (session, method, params, timeoutMs) =>
|
|
435
|
+
protocol.callSession(session, method, params, timeoutMs),
|
|
436
|
+
functions: (versionId) => readServerFunctions(registry.store.versionDir(versionId)),
|
|
437
|
+
...(options.relay?.waitingRunsPerArtifact !== undefined
|
|
438
|
+
? { waitingRunsPerArtifact: options.relay.waitingRunsPerArtifact }
|
|
439
|
+
: {}),
|
|
440
|
+
...(options.relay?.drainPerSecond !== undefined
|
|
441
|
+
? { drainPerSecond: options.relay.drainPerSecond }
|
|
442
|
+
: {}),
|
|
443
|
+
...(options.relay?.sweepMs !== undefined ? { sweepMs: options.relay.sweepMs } : {}),
|
|
444
|
+
...(options.relay?.liveRenewMs !== undefined ? { liveRenewMs: options.relay.liveRenewMs } : {}),
|
|
445
|
+
...(options.relay?.perLink !== undefined ? { perLink: options.relay.perLink } : {}),
|
|
446
|
+
latestVersion: (artifactId) =>
|
|
447
|
+
ledger.read(
|
|
448
|
+
(db) =>
|
|
449
|
+
db.get<{ latest_version_id: string | null }>(
|
|
450
|
+
"SELECT latest_version_id FROM artifact WHERE id = ?",
|
|
451
|
+
artifactId,
|
|
452
|
+
)?.latest_version_id ?? undefined,
|
|
453
|
+
),
|
|
454
|
+
storeLive: (artifactId, versionId, produced) => live.relayed(artifactId, versionId, produced),
|
|
455
|
+
});
|
|
456
|
+
/** A page's live reads: they ask the owner for producers, and say when it is away. */
|
|
457
|
+
const hostedLive: Pick<typeof live, "read"> = {
|
|
458
|
+
read(page, input) {
|
|
459
|
+
const states = live.read(page, input);
|
|
460
|
+
relay.liveWanted(page.artifactId);
|
|
461
|
+
if (!relay.ownerOffline(page.artifactId)) return states;
|
|
462
|
+
return states.map((state) =>
|
|
463
|
+
state.key.startsWith("$tally/") ? state : { ...state, ownerOffline: true },
|
|
464
|
+
);
|
|
465
|
+
},
|
|
466
|
+
};
|
|
467
|
+
const gateway: Gateway = createGateway({
|
|
468
|
+
ledger,
|
|
469
|
+
log,
|
|
470
|
+
addressing,
|
|
471
|
+
capabilities,
|
|
472
|
+
artifacts: {
|
|
473
|
+
get: (ref) => registry.get(canonicalRef(ref)),
|
|
474
|
+
versionDir: (versionId) => registry.store.versionDir(versionId),
|
|
475
|
+
damage: (versionId) => hostedDamage(registry.store.versionDir(versionId)),
|
|
476
|
+
},
|
|
477
|
+
shellDir: options.shellDir,
|
|
478
|
+
interaction,
|
|
479
|
+
bus,
|
|
480
|
+
actions: relay,
|
|
481
|
+
live: hostedLive,
|
|
482
|
+
// Both channels: a hosted home has no host to narrow them (its owner is a host of it, D165).
|
|
483
|
+
controls: async () => ({ submitChannels: ["shell", "artifact"] }),
|
|
484
|
+
activated: () => undefined,
|
|
485
|
+
hosted: {
|
|
486
|
+
shellOrigin: () => originOf(options.domains.active),
|
|
487
|
+
servesShell,
|
|
488
|
+
shellPath: (artifactId) => `/a/${routeOf(artifactId)?.id ?? artifactId}`,
|
|
489
|
+
artifactOrigin: (artifactId, shellOrigin) => {
|
|
490
|
+
const route = routeOf(artifactId);
|
|
491
|
+
const host = hostOfOrigin(shellOrigin);
|
|
492
|
+
if (!route || host === undefined) return undefined;
|
|
493
|
+
const frameDomain = options.services
|
|
494
|
+
? options.services.routing.frameDomainOf(id, route.id, host)
|
|
495
|
+
: domains.includes(host)
|
|
496
|
+
? host
|
|
497
|
+
: undefined;
|
|
498
|
+
return frameDomain === undefined ? undefined : originOf(`${route.label}.${frameDomain}`);
|
|
499
|
+
},
|
|
500
|
+
...(options.services
|
|
501
|
+
? {
|
|
502
|
+
// A new link goes on the operator's host for it when it names one that serves this
|
|
503
|
+
// route (D167, `routing.linkHost`), else on the active domain.
|
|
504
|
+
linkOrigin: async (artifactId: string) => {
|
|
505
|
+
const services = options.services as HostedServices;
|
|
506
|
+
const route = routeOf(artifactId);
|
|
507
|
+
if (!route || !services.operator.has.linkHost) return undefined;
|
|
508
|
+
const wanted = await services.operator.linkHost({
|
|
509
|
+
account: services.owner(id).account,
|
|
510
|
+
home: id,
|
|
511
|
+
routeId: route.id,
|
|
512
|
+
});
|
|
513
|
+
if (wanted === null) return undefined;
|
|
514
|
+
const host = await services.routing.linkHost(id, route.id, wanted);
|
|
515
|
+
return host === undefined ? undefined : originOf(host);
|
|
516
|
+
},
|
|
517
|
+
frameAncestors: (artifactId: string, frameOrigin: string) => {
|
|
518
|
+
const route = routeOf(artifactId);
|
|
519
|
+
const host = hostOfOrigin(frameOrigin);
|
|
520
|
+
if (!route || host === undefined) return "";
|
|
521
|
+
const frameDomain = host.slice(host.indexOf(".") + 1);
|
|
522
|
+
return (options.services as HostedServices).routing
|
|
523
|
+
.ancestorsOf(id, route.id, frameDomain)
|
|
524
|
+
.map(originOf)
|
|
525
|
+
.join(" ");
|
|
526
|
+
},
|
|
527
|
+
}
|
|
528
|
+
: {}),
|
|
529
|
+
...(options.services
|
|
530
|
+
? hostedPages({
|
|
531
|
+
homeId: id,
|
|
532
|
+
ledger,
|
|
533
|
+
interaction,
|
|
534
|
+
capabilities,
|
|
535
|
+
artifact: (artifactId) => registry.get({ artifactId }),
|
|
536
|
+
routeOf,
|
|
537
|
+
owner: () => (options.services as HostedServices).owner(id),
|
|
538
|
+
signInOffered: options.services.signInOffered,
|
|
539
|
+
operator: options.services.operator,
|
|
540
|
+
clients: options.services.clients,
|
|
541
|
+
rates: options.services.rates,
|
|
542
|
+
rateLimits: options.services.rateLimits,
|
|
543
|
+
madeWith: options.services.madeWith,
|
|
544
|
+
...(options.services.signin ? { signin: options.services.signin } : {}),
|
|
545
|
+
viewed: (input) => metered(input.artifactId, input.viewerId),
|
|
546
|
+
log,
|
|
547
|
+
})
|
|
548
|
+
: {}),
|
|
549
|
+
},
|
|
550
|
+
});
|
|
551
|
+
/** The faces of a domain: its shell, and a route's frames, opened when first asked for. */
|
|
552
|
+
const shellSpec = (domain: string): FaceSpec => ({
|
|
553
|
+
by: "hosted",
|
|
554
|
+
group: `hosted:${domain}`,
|
|
555
|
+
listener: "shell",
|
|
556
|
+
via: "token",
|
|
557
|
+
lease: false,
|
|
558
|
+
});
|
|
559
|
+
const ensureShell = async (domain: string): Promise<string> => {
|
|
560
|
+
const spec = shellSpec(domain);
|
|
561
|
+
const handle = await gateway.openFace(spec);
|
|
562
|
+
handle.setOrigin(originOf(domain));
|
|
563
|
+
return handle.key;
|
|
564
|
+
};
|
|
565
|
+
const ensureFrame = async (
|
|
566
|
+
domain: string,
|
|
567
|
+
artifactId: string,
|
|
568
|
+
label: string,
|
|
569
|
+
): Promise<string> => {
|
|
570
|
+
await ensureShell(domain);
|
|
571
|
+
const handle = await gateway.openFace({
|
|
572
|
+
by: "hosted",
|
|
573
|
+
group: `hosted:${domain}`,
|
|
574
|
+
listener: "artifact",
|
|
575
|
+
artifactId,
|
|
576
|
+
via: "token",
|
|
577
|
+
lease: false,
|
|
578
|
+
mode: "worker",
|
|
579
|
+
});
|
|
580
|
+
handle.setOrigin(originOf(`${label}.${domain}`));
|
|
581
|
+
return handle.key;
|
|
582
|
+
};
|
|
583
|
+
const notServed = (response: ServerResponse): void => {
|
|
584
|
+
response.writeHead(404, { "content-type": "application/json", "cache-control": "no-store" });
|
|
585
|
+
response.end(
|
|
586
|
+
JSON.stringify(
|
|
587
|
+
createDiagnostic("MOSTRA_HYGIENE_NOT_SERVED", {
|
|
588
|
+
message: "Nothing is served at this path.",
|
|
589
|
+
}),
|
|
590
|
+
),
|
|
591
|
+
);
|
|
592
|
+
};
|
|
593
|
+
const serve = async (
|
|
594
|
+
request: IncomingMessage,
|
|
595
|
+
response: ServerResponse,
|
|
596
|
+
at: HostedAddress,
|
|
597
|
+
): Promise<void> => {
|
|
598
|
+
const artifactId = artifactOfRoute(at.routeId);
|
|
599
|
+
const route = artifactId === undefined ? undefined : routeOf(artifactId);
|
|
600
|
+
if (artifactId === undefined || !route) {
|
|
601
|
+
notServed(response);
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
if (at.kind === "shell") {
|
|
605
|
+
// The shell's paths name the route; the gateway's routes name the artifact (§18.4).
|
|
606
|
+
const url = request.url ?? "/";
|
|
607
|
+
const prefix = `/a/${at.routeId}`;
|
|
608
|
+
if (!url.startsWith(prefix)) {
|
|
609
|
+
notServed(response);
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
request.url = `/a/${artifactId}${url.slice(prefix.length)}`;
|
|
613
|
+
const key = await ensureShell(at.domain);
|
|
614
|
+
if (!gateway.dispatch(key, request, response)) notServed(response);
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
const key = await ensureFrame(at.domain, artifactId, route.label);
|
|
618
|
+
if (!gateway.dispatch(key, request, response)) notServed(response);
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
/** The client files of a listing this home doesn't hold yet. */
|
|
622
|
+
const missingOf = (files: readonly string[]): string[] =>
|
|
623
|
+
directory.read((db) =>
|
|
624
|
+
files.filter(
|
|
625
|
+
(sha) => !db.get("SELECT 1 FROM home_file WHERE home_id = ? AND sha256 = ?", id, sha),
|
|
626
|
+
),
|
|
627
|
+
);
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Quarantines a route's artifact, or one version of it, in this home only (D165 item 9): its
|
|
631
|
+
* links show the neutral "under review" page and its files are withheld; data is kept. An
|
|
632
|
+
* identical version in another home is untouched, since the row is this home's.
|
|
633
|
+
*/
|
|
634
|
+
const quarantine = (artifactId: string, versionId: string | undefined, reason: string): void => {
|
|
635
|
+
ledger.write((db) =>
|
|
636
|
+
db.run(
|
|
637
|
+
`INSERT INTO quarantine (artifact_id, version_id, reason, at) VALUES (?, ?, ?, ?)
|
|
638
|
+
ON CONFLICT (artifact_id, version_id) DO UPDATE SET reason = excluded.reason`,
|
|
639
|
+
artifactId,
|
|
640
|
+
versionId ?? "",
|
|
641
|
+
reason.slice(0, 1_000),
|
|
642
|
+
new Date().toISOString(),
|
|
643
|
+
),
|
|
644
|
+
);
|
|
645
|
+
log.info("server.quarantined", { home: id, artifactId, versionId: versionId ?? null });
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* `view/url` on a hosted home under the operator's `limits` (`link`): refused with its words, and
|
|
650
|
+
* refused for now when the operator doesn't answer, since a link creates something (D165 item 9).
|
|
651
|
+
*/
|
|
652
|
+
const limitedLinks = (method: MethodHandler): MethodHandler => ({
|
|
653
|
+
definition: method.definition,
|
|
654
|
+
async handle(params, session) {
|
|
655
|
+
const services = options.services;
|
|
656
|
+
if (services) {
|
|
657
|
+
const ref = (params as { artifact?: unknown }).artifact;
|
|
658
|
+
const artifactId = registry.get(canonicalRef(ref as never)).id;
|
|
659
|
+
const route = routeOf(artifactId);
|
|
660
|
+
const verdict = await services.operator.limit({
|
|
661
|
+
account: services.owner(id).account,
|
|
662
|
+
home: id,
|
|
663
|
+
artifact: artifactId,
|
|
664
|
+
...(route ? { routeId: route.id } : {}),
|
|
665
|
+
operation: "link",
|
|
666
|
+
});
|
|
667
|
+
if (verdict.allowed === "unavailable") {
|
|
668
|
+
throw refuse(
|
|
669
|
+
"MOSTRA_REMOTE_OPERATOR_UNAVAILABLE",
|
|
670
|
+
"The server's operator didn't answer in time; try minting the link again.",
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
if (verdict.allowed === false) throw refuse("MOSTRA_REMOTE_LIMIT_REACHED", verdict.message);
|
|
674
|
+
}
|
|
675
|
+
return method.handle(params, session);
|
|
676
|
+
},
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* A view for metering, under the viewer's monthly pseudonymous key (D167): the account for a
|
|
681
|
+
* signed-in viewer, the viewer for a link minted with one, the route's key for anyone else.
|
|
682
|
+
*/
|
|
683
|
+
const metered = (artifactId: string, viewerId: string): void => {
|
|
684
|
+
const meter = options.services?.meter;
|
|
685
|
+
if (!meter?.on) return;
|
|
686
|
+
const viewer = ledger.read((db) =>
|
|
687
|
+
db.get<{ kind: string; external_ref: string | null }>(
|
|
688
|
+
"SELECT kind, external_ref FROM viewer WHERE id = ?",
|
|
689
|
+
viewerId,
|
|
690
|
+
),
|
|
691
|
+
);
|
|
692
|
+
const subject =
|
|
693
|
+
viewer?.kind === "account" || viewer?.kind === "anonymous"
|
|
694
|
+
? `${viewer.kind}:${viewer.external_ref ?? viewerId}`
|
|
695
|
+
: `viewer:${id}:${viewerId}`;
|
|
696
|
+
const route = routeOf(artifactId);
|
|
697
|
+
meter.add("views", {
|
|
698
|
+
account: (options.services as HostedServices).owner(id).account,
|
|
699
|
+
home: id,
|
|
700
|
+
artifactId,
|
|
701
|
+
...(route ? { routeId: route.id } : {}),
|
|
702
|
+
dimensions: { viewerKey: meter.viewerKey(subject) },
|
|
703
|
+
});
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
/** The live links of an artifact, revoked; answers how many were live. */
|
|
707
|
+
const revokeLinks = (artifactId: string): number => {
|
|
708
|
+
const tokens = ledger.read((db) =>
|
|
709
|
+
db
|
|
710
|
+
.all<{ id: string }>(
|
|
711
|
+
"SELECT id FROM token WHERE artifact_id = ? AND revoked_at IS NULL",
|
|
712
|
+
artifactId,
|
|
713
|
+
)
|
|
714
|
+
.map((row) => row.id),
|
|
715
|
+
);
|
|
716
|
+
let revoked = 0;
|
|
717
|
+
for (const tokenId of tokens) if (capabilities.revoke(tokenId) === "revoked") revoked += 1;
|
|
718
|
+
return revoked;
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
const push = async (params: HostedPushParams, session: Session): Promise<HostedPushResult> => {
|
|
722
|
+
const link = session.link?.instanceId;
|
|
723
|
+
if (link === undefined || session.attributes["linkId"] !== link) {
|
|
724
|
+
throw refuse(
|
|
725
|
+
"MOSTRA_REMOTE_NOT_OWNER_INSTANCE",
|
|
726
|
+
"Only an owner instance's binding pushes versions.",
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
const version = params.version;
|
|
730
|
+
// The identity is checked, not believed: the version ID is a digest of the artifact ID and the
|
|
731
|
+
// two digests (D89), and the listing's digest is the output digest (DATA_MODEL §2).
|
|
732
|
+
if (
|
|
733
|
+
versionIdFor(params.artifactId, version.sourceDigest, version.envDigest) !== version.versionId
|
|
734
|
+
) {
|
|
735
|
+
throw refuse("MOSTRA_HOME_INTEGRITY", "The pushed version's ID doesn't match its digests.");
|
|
736
|
+
}
|
|
737
|
+
const listed = version.listing.map(([path, executable, sha256]) => ({
|
|
738
|
+
path,
|
|
739
|
+
executable,
|
|
740
|
+
sha256,
|
|
741
|
+
}));
|
|
742
|
+
if (listingDigest(listed) !== version.outputDigest) {
|
|
743
|
+
throw refuse("MOSTRA_HOME_INTEGRITY", "The pushed listing doesn't match the output digest.");
|
|
744
|
+
}
|
|
745
|
+
const registryEntry = version.listing.find(([path]) => path === "registry.json");
|
|
746
|
+
if (!registryEntry || sha256Text(version.registry) !== registryEntry[2]) {
|
|
747
|
+
throw refuse(
|
|
748
|
+
"MOSTRA_HOME_INTEGRITY",
|
|
749
|
+
"The pushed registry.json isn't the one the listing names.",
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
for (const [path] of version.listing) {
|
|
753
|
+
if (!(path === "registry.json" || path.startsWith("client/") || path.startsWith("server/"))) {
|
|
754
|
+
throw refuse("MOSTRA_HOME_INTEGRITY", `The listing names ${path}, outside a version.`);
|
|
755
|
+
}
|
|
756
|
+
if (!safeListingPath(path)) {
|
|
757
|
+
throw refuse(
|
|
758
|
+
"MOSTRA_HOME_INTEGRITY",
|
|
759
|
+
`The listing names an unsafe path, ${JSON.stringify(path.slice(0, 300))}.`,
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
let manifestDoc: { manifest?: unknown; artifactId?: unknown; versionId?: unknown };
|
|
764
|
+
try {
|
|
765
|
+
manifestDoc = JSON.parse(version.manifest) as typeof manifestDoc;
|
|
766
|
+
} catch {
|
|
767
|
+
throw refuse("MOSTRA_HOME_INTEGRITY", "The pushed manifest.json isn't JSON.");
|
|
768
|
+
}
|
|
769
|
+
if (
|
|
770
|
+
manifestDoc.artifactId !== params.artifactId ||
|
|
771
|
+
manifestDoc.versionId !== version.versionId
|
|
772
|
+
) {
|
|
773
|
+
throw refuse("MOSTRA_HOME_INTEGRITY", "The pushed manifest.json names another version.");
|
|
774
|
+
}
|
|
775
|
+
const manifest = ArtifactManifest.safeParse(manifestDoc.manifest);
|
|
776
|
+
if (!manifest.success)
|
|
777
|
+
throw refuse("MOSTRA_HOME_INTEGRITY", "The pushed artifact manifest isn't valid.");
|
|
778
|
+
|
|
779
|
+
const services = options.services;
|
|
780
|
+
// A first push hosts the artifact: the operator's `limits` judge it as a share, failing
|
|
781
|
+
// closed when it doesn't answer (D165 item 9).
|
|
782
|
+
if (services && routeOf(params.artifactId) === undefined) {
|
|
783
|
+
const verdict = await services.operator.limit({
|
|
784
|
+
account: services.owner(id).account,
|
|
785
|
+
home: id,
|
|
786
|
+
artifact: params.artifactId,
|
|
787
|
+
operation: "share",
|
|
788
|
+
});
|
|
789
|
+
if (verdict.allowed === "unavailable") {
|
|
790
|
+
throw refuse(
|
|
791
|
+
"MOSTRA_REMOTE_OPERATOR_UNAVAILABLE",
|
|
792
|
+
"The server's operator didn't answer in time; the share is tried again later.",
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
if (verdict.allowed === false) throw refuse("MOSTRA_REMOTE_LIMIT_REACHED", verdict.message);
|
|
796
|
+
}
|
|
797
|
+
const route = routeFor(params.artifactId, link);
|
|
798
|
+
if (registry.hasVersion(version.versionId)) {
|
|
799
|
+
const known = registry
|
|
800
|
+
.get({ artifactId: params.artifactId })
|
|
801
|
+
.versions.find((v) => v.id === version.versionId);
|
|
802
|
+
return {
|
|
803
|
+
status: "complete",
|
|
804
|
+
versionId: version.versionId,
|
|
805
|
+
number: known?.number ?? version.number,
|
|
806
|
+
route,
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
// Only client files are stored: server code never reaches the server (D165 item 3).
|
|
810
|
+
const clientFiles = version.listing.filter(([path]) => path.startsWith("client/"));
|
|
811
|
+
// The sizes a listing gives are what the storage limit is asked with, so each is held to the
|
|
812
|
+
// bytes: one file has one size, a file this home holds already has the size it has on disk, and
|
|
813
|
+
// an upload brings exactly the size given (Fable review of WP-30, 2).
|
|
814
|
+
const sizes = new Map<string, number>();
|
|
815
|
+
for (const [path, , sha, size] of clientFiles) {
|
|
816
|
+
if (!Number.isSafeInteger(size) || size < 0 || (sizes.get(sha) ?? size) !== size) {
|
|
817
|
+
throw refuse(
|
|
818
|
+
"MOSTRA_HOME_INTEGRITY",
|
|
819
|
+
`The listing gives ${path} a size its bytes can't have.`,
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
sizes.set(sha, size);
|
|
823
|
+
}
|
|
824
|
+
for (const [sha, size] of sizes) {
|
|
825
|
+
const held = directory.read((db) =>
|
|
826
|
+
db.get("SELECT 1 FROM home_file WHERE home_id = ? AND sha256 = ?", id, sha),
|
|
827
|
+
);
|
|
828
|
+
if (held === undefined) continue;
|
|
829
|
+
const onDisk = await stat(join(options.filesDir, sha)).then(
|
|
830
|
+
(found) => found.size,
|
|
831
|
+
() => undefined,
|
|
832
|
+
);
|
|
833
|
+
if (onDisk !== undefined && onDisk !== size) {
|
|
834
|
+
throw refuse(
|
|
835
|
+
"MOSTRA_HOME_INTEGRITY",
|
|
836
|
+
"The listing gives a held file another size than its bytes.",
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
// The operator's `limits` on a push and the bytes it stores: refused for good with its words,
|
|
841
|
+
// and refused for now (fail closed) when the operator doesn't answer (D165 item 9).
|
|
842
|
+
if (services) {
|
|
843
|
+
const owner = services.owner(id).account;
|
|
844
|
+
for (const [operation, quantity] of [
|
|
845
|
+
["version.push", 1],
|
|
846
|
+
["storage", [...sizes.values()].reduce((sum, size) => sum + size, 0)],
|
|
847
|
+
] as const) {
|
|
848
|
+
const verdict = await services.operator.limit({
|
|
849
|
+
account: owner,
|
|
850
|
+
home: id,
|
|
851
|
+
artifact: params.artifactId,
|
|
852
|
+
routeId: route.routeId,
|
|
853
|
+
operation,
|
|
854
|
+
quantity,
|
|
855
|
+
});
|
|
856
|
+
if (verdict.allowed === "unavailable") {
|
|
857
|
+
throw refuse(
|
|
858
|
+
"MOSTRA_REMOTE_OPERATOR_UNAVAILABLE",
|
|
859
|
+
"The server's operator didn't answer in time; the push is tried again later.",
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
if (verdict.allowed === false) throw refuse("MOSTRA_REMOTE_LIMIT_REACHED", verdict.message);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
const missing = missingOf([...new Set(clientFiles.map(([, , sha]) => sha))]);
|
|
866
|
+
if (missing.length > 0) {
|
|
867
|
+
pending.set(
|
|
868
|
+
version.versionId,
|
|
869
|
+
new Map(missing.map((sha) => [sha, sizes.get(sha) as number])),
|
|
870
|
+
);
|
|
871
|
+
return { status: "missing", files: missing, route };
|
|
872
|
+
}
|
|
873
|
+
pending.delete(version.versionId);
|
|
874
|
+
|
|
875
|
+
// Every file is here: lay the version out as the gateway serves versions, then commit it.
|
|
876
|
+
const stage = join(
|
|
877
|
+
registry.store.stagingDir,
|
|
878
|
+
`${version.versionId}.push-${process.pid}-${Date.now()}`,
|
|
879
|
+
);
|
|
880
|
+
await rm(stage, { recursive: true, force: true });
|
|
881
|
+
mkdirSync(stage, { recursive: true, mode: 0o700 });
|
|
882
|
+
try {
|
|
883
|
+
writeFileSync(join(stage, "manifest.json"), version.manifest);
|
|
884
|
+
writeFileSync(join(stage, "registry.json"), version.registry);
|
|
885
|
+
writeFileSync(join(stage, "lock.yaml"), version.lock);
|
|
886
|
+
// The full listing, server files included, so the version can be checked as built.
|
|
887
|
+
writeFileSync(join(stage, "listing.json"), JSON.stringify(version.listing));
|
|
888
|
+
for (const [path, , sha] of clientFiles) {
|
|
889
|
+
const target = join(stage, ...path.split("/"));
|
|
890
|
+
// Checked again where it lands: nothing a tenant names leaves its own staging directory.
|
|
891
|
+
if (relative(stage, target).startsWith("..") || isAbsolute(relative(stage, target))) {
|
|
892
|
+
throw refuse("MOSTRA_HOME_INTEGRITY", "The listing names a path outside the version.");
|
|
893
|
+
}
|
|
894
|
+
mkdirSync(join(target, ".."), { recursive: true, mode: 0o700 });
|
|
895
|
+
const stored = join(options.filesDir, sha);
|
|
896
|
+
try {
|
|
897
|
+
linkSync(stored, target);
|
|
898
|
+
} catch {
|
|
899
|
+
await copyFile(stored, target);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
await registry.store.promote(stage, version.versionId);
|
|
903
|
+
} catch (error) {
|
|
904
|
+
await rm(stage, { recursive: true, force: true });
|
|
905
|
+
throw error;
|
|
906
|
+
}
|
|
907
|
+
const committed = registry.commit({
|
|
908
|
+
artifactId: params.artifactId,
|
|
909
|
+
bindingPath: undefined,
|
|
910
|
+
number: version.number,
|
|
911
|
+
manifestId: params.artifactId,
|
|
912
|
+
versionId: version.versionId,
|
|
913
|
+
sourceDigest: version.sourceDigest,
|
|
914
|
+
envDigest: version.envDigest,
|
|
915
|
+
outputDigest: version.outputDigest,
|
|
916
|
+
manifest: manifest.data,
|
|
917
|
+
// The owner's policy in force, as the owner's host decided it (D102): it sticks here.
|
|
918
|
+
policy: version.policy,
|
|
919
|
+
forcedCodes: [],
|
|
920
|
+
stillClaims: () => false,
|
|
921
|
+
});
|
|
922
|
+
directory.write((db) => {
|
|
923
|
+
for (const sha of new Set(clientFiles.map(([, , file]) => file))) {
|
|
924
|
+
const added = db.run(
|
|
925
|
+
"INSERT OR IGNORE INTO home_file (home_id, sha256) VALUES (?, ?)",
|
|
926
|
+
id,
|
|
927
|
+
sha,
|
|
928
|
+
);
|
|
929
|
+
if (added.changes > 0) {
|
|
930
|
+
db.run(
|
|
931
|
+
"INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL",
|
|
932
|
+
sha,
|
|
933
|
+
);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
});
|
|
937
|
+
log.info("server.version_pushed", {
|
|
938
|
+
home: id,
|
|
939
|
+
artifactId: params.artifactId,
|
|
940
|
+
versionId: version.versionId,
|
|
941
|
+
});
|
|
942
|
+
// The operator's after-push hook reads the client files; its answer can quarantine the
|
|
943
|
+
// version afterwards, never holding up serving it (D165 item 9).
|
|
944
|
+
if (services?.operator.has.pushed) {
|
|
945
|
+
const files = clientFiles.map(([path]) => path);
|
|
946
|
+
const shaOf = new Map(clientFiles.map(([path, , sha]) => [path, sha]));
|
|
947
|
+
services.operator.pushed(
|
|
948
|
+
{
|
|
949
|
+
routeId: route.routeId,
|
|
950
|
+
label: route.label,
|
|
951
|
+
artifactId: params.artifactId,
|
|
952
|
+
home: id,
|
|
953
|
+
versionId: version.versionId,
|
|
954
|
+
manifest: manifest.data,
|
|
955
|
+
files,
|
|
956
|
+
read: async (path) => {
|
|
957
|
+
const sha = shaOf.get(path);
|
|
958
|
+
if (sha === undefined) throw new Error(`No client file ${path} in this version.`);
|
|
959
|
+
return new Uint8Array(await readFile(join(options.filesDir, sha)));
|
|
960
|
+
},
|
|
961
|
+
},
|
|
962
|
+
async (reason) => {
|
|
963
|
+
quarantine(params.artifactId, version.versionId, reason);
|
|
964
|
+
},
|
|
965
|
+
);
|
|
966
|
+
}
|
|
967
|
+
return { status: "complete", versionId: version.versionId, number: committed.version, route };
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
const describe = (): InstanceInfo => {
|
|
971
|
+
const head = ledger.read(
|
|
972
|
+
(db) =>
|
|
973
|
+
db.get<{ cursor: number | null }>("SELECT MAX(cursor) AS cursor FROM event")?.cursor ?? 0,
|
|
974
|
+
);
|
|
975
|
+
return {
|
|
976
|
+
version: options.runtimeVersion,
|
|
977
|
+
home: `hosted:${id}`,
|
|
978
|
+
platform: platformName(),
|
|
979
|
+
pid: process.pid,
|
|
980
|
+
mode: "hosted",
|
|
981
|
+
incarnation,
|
|
982
|
+
cursor: String(head),
|
|
983
|
+
};
|
|
984
|
+
};
|
|
985
|
+
|
|
986
|
+
protocol = new ProtocolServer({
|
|
987
|
+
log,
|
|
988
|
+
describe,
|
|
989
|
+
servesHome: () => false,
|
|
990
|
+
supportedPorts: ["submissions", "relay"],
|
|
991
|
+
notifications: ["events/notify"],
|
|
992
|
+
admit: (session, params) => {
|
|
993
|
+
// The owner instance is the witness a restore can't roll back: a home that holds less than
|
|
994
|
+
// its owner committed, or saw less of its relay, was restored to an earlier copy, and rotates
|
|
995
|
+
// its incarnation before it answers (INTERFACES §18.2).
|
|
996
|
+
const link = params.link;
|
|
997
|
+
if (link !== undefined && link.instanceId === session.attributes["linkId"]) {
|
|
998
|
+
const marks = ledger.read((db) => ({
|
|
999
|
+
head: db.get<{ head: number | null }>("SELECT MAX(cursor) AS head FROM event")?.head ?? 0,
|
|
1000
|
+
// What the home knows of the link's relay: its runs, and the highest mark its owner
|
|
1001
|
+
// reported, kept so a restore is told once, not at every later reconnect.
|
|
1002
|
+
relayHigh: Math.max(
|
|
1003
|
+
db.get<{ high: number | null }>(
|
|
1004
|
+
"SELECT MAX(relay_seq) AS high FROM relay_run WHERE owner_link_id = ?",
|
|
1005
|
+
link.instanceId,
|
|
1006
|
+
)?.high ?? 0,
|
|
1007
|
+
db.get<{ floor: number }>(
|
|
1008
|
+
"SELECT floor FROM relay_floor WHERE link_id = ?",
|
|
1009
|
+
link.instanceId,
|
|
1010
|
+
)?.floor ?? 0,
|
|
1011
|
+
),
|
|
1012
|
+
}));
|
|
1013
|
+
const followedAhead =
|
|
1014
|
+
link.followed !== undefined &&
|
|
1015
|
+
link.followed.incarnation === incarnation &&
|
|
1016
|
+
Number(link.followed.cursor) > marks.head;
|
|
1017
|
+
const relayAhead = link.relayMark > marks.relayHigh;
|
|
1018
|
+
if (followedAhead || relayAhead) {
|
|
1019
|
+
incarnation = rotateIncarnation(ledger);
|
|
1020
|
+
log.warn("server.home_rotated", {
|
|
1021
|
+
home: id,
|
|
1022
|
+
why: followedAhead ? "owner-cursor" : "owner-relay-mark",
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
ledger.write((db) =>
|
|
1026
|
+
db.run(
|
|
1027
|
+
`INSERT INTO relay_floor (link_id, floor) VALUES (?, ?)
|
|
1028
|
+
ON CONFLICT (link_id) DO UPDATE SET floor = MAX(floor, excluded.floor)`,
|
|
1029
|
+
link.instanceId,
|
|
1030
|
+
link.relayMark,
|
|
1031
|
+
),
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
// An owner link is its own grant's: a session can't speak for another instance's link.
|
|
1035
|
+
if (params.link !== undefined && params.link.instanceId !== session.attributes["linkId"]) {
|
|
1036
|
+
throw refuse(
|
|
1037
|
+
"MOSTRA_REMOTE_NOT_OWNER_INSTANCE",
|
|
1038
|
+
"This binding's sign-in is for another instance link.",
|
|
1039
|
+
);
|
|
1040
|
+
}
|
|
1041
|
+
// A session that takes deliveries or relayed runs is an owner instance's, so it names its own
|
|
1042
|
+
// link: one without a link could otherwise be taken for an owner by its client name (Opus
|
|
1043
|
+
// review of WP-29, 6).
|
|
1044
|
+
if (params.link === undefined && params.ports.length > 0) {
|
|
1045
|
+
throw refuse(
|
|
1046
|
+
"MOSTRA_REMOTE_NOT_OWNER_INSTANCE",
|
|
1047
|
+
"A binding that registers ports must name its own instance link.",
|
|
1048
|
+
);
|
|
1049
|
+
}
|
|
1050
|
+
},
|
|
1051
|
+
onInitialized: (session) => {
|
|
1052
|
+
interaction.connected(session);
|
|
1053
|
+
relay.connected(session);
|
|
1054
|
+
},
|
|
1055
|
+
onClosed: (session) => {
|
|
1056
|
+
interaction.disconnected(session);
|
|
1057
|
+
relay.disconnected(session);
|
|
1058
|
+
},
|
|
1059
|
+
handlers: [
|
|
1060
|
+
...interaction.methods(),
|
|
1061
|
+
handler(artifactListMethod, (params) => ({ artifacts: registry.list(params.filter ?? {}) })),
|
|
1062
|
+
handler(artifactGetMethod, (params) => registry.get(canonicalRef(params.artifact))),
|
|
1063
|
+
handler(hostedPushMethod, (params, session) => push(params, session)),
|
|
1064
|
+
handler(hostedSubmissionMethod, (params, session) => {
|
|
1065
|
+
// Only the owner instance of its artifact reads a submission this way (D165 item 1b),
|
|
1066
|
+
// and only one it was, or is, delivered.
|
|
1067
|
+
const link = session.link?.instanceId;
|
|
1068
|
+
const envelope =
|
|
1069
|
+
link === undefined ? undefined : interaction.envelopeFor(params.submissionId, link);
|
|
1070
|
+
if (!envelope) {
|
|
1071
|
+
throw refuse(
|
|
1072
|
+
"MOSTRA_HOST_INVALID_PARAMS",
|
|
1073
|
+
`This home delivers no submission ${params.submissionId} to this instance.`,
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
return envelope;
|
|
1077
|
+
}),
|
|
1078
|
+
...gateway
|
|
1079
|
+
.methods()
|
|
1080
|
+
.map((method) =>
|
|
1081
|
+
method.definition.method === viewUrlMethod.method ? limitedLinks(method) : method,
|
|
1082
|
+
),
|
|
1083
|
+
handler(artifactPauseMethod, (params) => {
|
|
1084
|
+
// The account's own instances pause and resume as the owner; only the operator, through
|
|
1085
|
+
// its handle, resumes an operator's pause (D165 item 14).
|
|
1086
|
+
const artifactId = registry.get(canonicalRef(params.artifact)).id;
|
|
1087
|
+
return interaction.pause(artifactId, {
|
|
1088
|
+
paused: params.paused,
|
|
1089
|
+
by: "owner",
|
|
1090
|
+
...(params.reason !== undefined ? { reason: params.reason } : {}),
|
|
1091
|
+
});
|
|
1092
|
+
}),
|
|
1093
|
+
...relay.methods(),
|
|
1094
|
+
...liveMethods(live, (ref) => registry.get(canonicalRef(ref)).id),
|
|
1095
|
+
handler(remoteTokenMethod, (params, session) => {
|
|
1096
|
+
const found = options.auth.authenticate(params.accessToken);
|
|
1097
|
+
if (!found || found.grantId !== session.attributes["grantId"]) {
|
|
1098
|
+
throw refuse(
|
|
1099
|
+
"MOSTRA_REMOTE_LOGIN_FAILED",
|
|
1100
|
+
"That access token isn't this binding's grant's.",
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
const replace = session.attributes["replaceToken"] as
|
|
1104
|
+
| ((expiresAt: number) => void)
|
|
1105
|
+
| undefined;
|
|
1106
|
+
replace?.(found.expiresAt);
|
|
1107
|
+
return { expiresAt: new Date(found.expiresAt).toISOString() };
|
|
1108
|
+
}),
|
|
1109
|
+
],
|
|
1110
|
+
});
|
|
1111
|
+
interaction.start();
|
|
1112
|
+
live.start();
|
|
1113
|
+
// The operator's feed of this home's events, and its submissions for metering (§18.6).
|
|
1114
|
+
const feed: OperatorFeed | undefined = options.services
|
|
1115
|
+
? createOperatorFeed({
|
|
1116
|
+
homeId: id,
|
|
1117
|
+
ledger,
|
|
1118
|
+
interaction,
|
|
1119
|
+
operator: options.services.operator,
|
|
1120
|
+
meter: options.services.meter,
|
|
1121
|
+
account: () => (options.services as HostedServices).owner(id).account,
|
|
1122
|
+
routeOf,
|
|
1123
|
+
incarnation: () => incarnation,
|
|
1124
|
+
log,
|
|
1125
|
+
})
|
|
1126
|
+
: undefined;
|
|
1127
|
+
|
|
1128
|
+
return {
|
|
1129
|
+
id,
|
|
1130
|
+
dir,
|
|
1131
|
+
ledger,
|
|
1132
|
+
registry,
|
|
1133
|
+
interaction,
|
|
1134
|
+
protocol,
|
|
1135
|
+
incarnation: () => incarnation,
|
|
1136
|
+
wanted: (sha) => {
|
|
1137
|
+
for (const files of pending.values()) {
|
|
1138
|
+
const size = files.get(sha);
|
|
1139
|
+
if (size !== undefined) return size;
|
|
1140
|
+
}
|
|
1141
|
+
return undefined;
|
|
1142
|
+
},
|
|
1143
|
+
uploaded(sha) {
|
|
1144
|
+
directory.write((db) => {
|
|
1145
|
+
const added = db.run(
|
|
1146
|
+
"INSERT OR IGNORE INTO home_file (home_id, sha256) VALUES (?, ?)",
|
|
1147
|
+
id,
|
|
1148
|
+
sha,
|
|
1149
|
+
);
|
|
1150
|
+
if (added.changes > 0) {
|
|
1151
|
+
db.run(
|
|
1152
|
+
"INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL",
|
|
1153
|
+
sha,
|
|
1154
|
+
);
|
|
1155
|
+
}
|
|
1156
|
+
});
|
|
1157
|
+
for (const files of pending.values()) files.delete(sha);
|
|
1158
|
+
},
|
|
1159
|
+
pause: (artifactId, change) => interaction.pause(artifactId, change),
|
|
1160
|
+
quarantine,
|
|
1161
|
+
release(artifactId, versionId) {
|
|
1162
|
+
ledger.write((db) =>
|
|
1163
|
+
db.run(
|
|
1164
|
+
versionId === undefined
|
|
1165
|
+
? "DELETE FROM quarantine WHERE artifact_id = ?"
|
|
1166
|
+
: "DELETE FROM quarantine WHERE artifact_id = ? AND version_id = ?",
|
|
1167
|
+
...(versionId === undefined ? [artifactId] : [artifactId, versionId]),
|
|
1168
|
+
),
|
|
1169
|
+
);
|
|
1170
|
+
},
|
|
1171
|
+
revokeLinks,
|
|
1172
|
+
revokeLink: (tokenId) => capabilities.revoke(tokenId) !== "unknown",
|
|
1173
|
+
async remove(artifactId) {
|
|
1174
|
+
/** The client files a version's listing names. */
|
|
1175
|
+
const clientFilesOf = async (versionId: string): Promise<string[]> => {
|
|
1176
|
+
try {
|
|
1177
|
+
const listing = JSON.parse(
|
|
1178
|
+
await readFile(join(registry.store.versionDir(versionId), "listing.json"), "utf8"),
|
|
1179
|
+
) as [string, boolean, string, number][];
|
|
1180
|
+
return listing.filter(([path]) => path.startsWith("client/")).map(([, , sha]) => sha);
|
|
1181
|
+
} catch {
|
|
1182
|
+
return [];
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1185
|
+
// The plan is recorded in the directory before anything goes, so a removal cut off between
|
|
1186
|
+
// the two databases is finished by the next call, from the same plan (Fable review, 4).
|
|
1187
|
+
const recorded = directory.read(
|
|
1188
|
+
(db) =>
|
|
1189
|
+
db.get<{ removing_json: string | null }>(
|
|
1190
|
+
"SELECT removing_json FROM artifact_route WHERE home_id = ? AND artifact_id = ?",
|
|
1191
|
+
id,
|
|
1192
|
+
artifactId,
|
|
1193
|
+
)?.removing_json,
|
|
1194
|
+
);
|
|
1195
|
+
let plan: { readonly versions: string[]; readonly going: string[] };
|
|
1196
|
+
if (recorded != null) plan = JSON.parse(recorded) as typeof plan;
|
|
1197
|
+
else {
|
|
1198
|
+
const versions = registry.get({ artifactId }).versions.map((version) => version.id);
|
|
1199
|
+
const files = new Set<string>();
|
|
1200
|
+
for (const versionId of versions)
|
|
1201
|
+
for (const sha of await clientFilesOf(versionId)) files.add(sha);
|
|
1202
|
+
plan = { versions, going: [...files] };
|
|
1203
|
+
directory.write((db) =>
|
|
1204
|
+
db.run(
|
|
1205
|
+
"UPDATE artifact_route SET removing_json = ? WHERE home_id = ? AND artifact_id = ?",
|
|
1206
|
+
JSON.stringify(plan),
|
|
1207
|
+
id,
|
|
1208
|
+
artifactId,
|
|
1209
|
+
),
|
|
1210
|
+
);
|
|
1211
|
+
}
|
|
1212
|
+
const versions = plan.versions;
|
|
1213
|
+
const going = new Set(plan.going);
|
|
1214
|
+
if (registry.list({}).some((summary) => summary.id === artifactId)) revokeLinks(artifactId);
|
|
1215
|
+
const removed = ledger.removal((db) => purgeArtifact(db, artifactId));
|
|
1216
|
+
// A test cuts the removal off here, between its two databases (`remove:after-purge`).
|
|
1217
|
+
faults.at("remove:after-purge");
|
|
1218
|
+
// Files another version of this home still names stay this home's.
|
|
1219
|
+
const kept = new Set<string>();
|
|
1220
|
+
for (const other of registry.list({})) {
|
|
1221
|
+
for (const version of registry.get({ artifactId: other.id }).versions) {
|
|
1222
|
+
for (const sha of await clientFilesOf(version.id)) kept.add(sha);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
const route = directory.read((db) =>
|
|
1226
|
+
db.get<{ id: string }>(
|
|
1227
|
+
"SELECT id FROM artifact_route WHERE home_id = ? AND artifact_id = ?",
|
|
1228
|
+
id,
|
|
1229
|
+
artifactId,
|
|
1230
|
+
),
|
|
1231
|
+
);
|
|
1232
|
+
directory.write((db) => {
|
|
1233
|
+
for (const sha of going) {
|
|
1234
|
+
if (kept.has(sha)) continue;
|
|
1235
|
+
if (
|
|
1236
|
+
db.run("DELETE FROM home_file WHERE home_id = ? AND sha256 = ?", id, sha).changes > 0
|
|
1237
|
+
) {
|
|
1238
|
+
db.run(
|
|
1239
|
+
`UPDATE file_ref SET homes = homes - 1,
|
|
1240
|
+
unreferenced_at = CASE WHEN homes - 1 = 0 THEN ? ELSE NULL END
|
|
1241
|
+
WHERE sha256 = ?`,
|
|
1242
|
+
new Date().toISOString(),
|
|
1243
|
+
sha,
|
|
1244
|
+
);
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
if (route) {
|
|
1248
|
+
db.run("DELETE FROM hosted_token WHERE route_id = ?", route.id);
|
|
1249
|
+
// The route goes; its label stays in `label_issued`, never issued again.
|
|
1250
|
+
db.run("DELETE FROM artifact_route WHERE id = ?", route.id);
|
|
1251
|
+
}
|
|
1252
|
+
});
|
|
1253
|
+
for (const versionId of versions) {
|
|
1254
|
+
await removeTree(registry.store.versionDir(versionId)).catch((error: unknown) =>
|
|
1255
|
+
log.warn("server.remove_files_failed", { versionId, error }),
|
|
1256
|
+
);
|
|
1257
|
+
}
|
|
1258
|
+
const rows = Object.values(removed).reduce((sum, count) => sum + count, 0);
|
|
1259
|
+
log.info("server.artifact_removed", { home: id, artifactId, rows });
|
|
1260
|
+
return { rows };
|
|
1261
|
+
},
|
|
1262
|
+
defaultOwnerSession() {
|
|
1263
|
+
const link = directory.read(
|
|
1264
|
+
(db) =>
|
|
1265
|
+
db.get<{ id: string }>(
|
|
1266
|
+
"SELECT id FROM instance_link WHERE home_id = ? AND is_default = 1 AND revoked_at IS NULL",
|
|
1267
|
+
id,
|
|
1268
|
+
)?.id,
|
|
1269
|
+
);
|
|
1270
|
+
if (link === undefined) return undefined;
|
|
1271
|
+
return protocol
|
|
1272
|
+
.registrants("relay")
|
|
1273
|
+
.filter((entry) => entry.owner === link)
|
|
1274
|
+
.at(-1)?.session;
|
|
1275
|
+
},
|
|
1276
|
+
busy: () => protocol.connections > 0 || bus.activeViews() > 0 || relay.busy(),
|
|
1277
|
+
serve,
|
|
1278
|
+
async stop() {
|
|
1279
|
+
feed?.stop();
|
|
1280
|
+
relay.stop();
|
|
1281
|
+
live.stop();
|
|
1282
|
+
interaction.stop();
|
|
1283
|
+
await gateway.close();
|
|
1284
|
+
await protocol.close();
|
|
1285
|
+
bus.close?.();
|
|
1286
|
+
},
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
/**
|
|
1291
|
+
* What is wrong with a hosted version's stored files (FAILURE_MODEL §3): its listing must be the
|
|
1292
|
+
* one its output digest names, and every client file it lists present with its bytes. Server files
|
|
1293
|
+
* are listed but never stored (D165 item 3).
|
|
1294
|
+
*/
|
|
1295
|
+
async function hostedDamage(versionDir: string): Promise<string | undefined> {
|
|
1296
|
+
try {
|
|
1297
|
+
const manifest = JSON.parse(await readFile(join(versionDir, "manifest.json"), "utf8")) as {
|
|
1298
|
+
outputDigest?: string;
|
|
1299
|
+
};
|
|
1300
|
+
const listing = JSON.parse(await readFile(join(versionDir, "listing.json"), "utf8")) as [
|
|
1301
|
+
string,
|
|
1302
|
+
boolean,
|
|
1303
|
+
string,
|
|
1304
|
+
number,
|
|
1305
|
+
][];
|
|
1306
|
+
const listed = listing.map(([path, executable, sha256]) => ({ path, executable, sha256 }));
|
|
1307
|
+
if (listingDigest(listed) !== manifest.outputDigest)
|
|
1308
|
+
return "its listing doesn't match its digest";
|
|
1309
|
+
for (const [path, , sha256] of listing) {
|
|
1310
|
+
if (!path.startsWith("client/") && path !== "registry.json") continue;
|
|
1311
|
+
const bytes = await readFile(join(versionDir, ...path.split("/")));
|
|
1312
|
+
if (createHash("sha256").update(bytes).digest("hex") !== sha256) return `${path} was changed`;
|
|
1313
|
+
}
|
|
1314
|
+
return undefined;
|
|
1315
|
+
} catch (error) {
|
|
1316
|
+
return `a file is missing (${(error as NodeJS.ErrnoException).code ?? "unreadable"})`;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
/** The SHA-256 of a text's UTF-8 bytes, lowercase hex. */
|
|
1321
|
+
function sha256Text(text: string): string {
|
|
1322
|
+
return createHash("sha256").update(text, "utf8").digest("hex");
|
|
1323
|
+
}
|