@mostra/runtime 0.10.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 +4058 -310
- package/dist/bin.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/{instance-FExcyENK.js → instance-DCiUHvgZ.js} +682 -47
- 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 +10 -5
- package/src/gateway/actions.ts +20 -0
- package/src/gateway/browser.ts +44 -0
- package/src/gateway/capabilities.ts +8 -0
- package/src/gateway/http.ts +12 -1
- package/src/gateway/index.ts +170 -5
- package/src/gateway/listener.ts +31 -6
- package/src/gateway/routes.ts +8 -0
- package/src/gc/index.ts +5 -0
- package/src/instance/index.ts +20 -1
- package/src/interaction/index.ts +60 -0
- package/src/interaction/pause.ts +106 -0
- package/src/interaction/submissions.ts +42 -1
- package/src/interaction/viewers.ts +34 -1
- package/src/ledger/index.ts +20 -1
- package/src/ledger/migrations.ts +51 -0
- package/src/link/author.ts +215 -0
- package/src/link/hosted.ts +37 -1
- package/src/link/http.ts +6 -1
- package/src/link/index.ts +3 -1
- package/src/link/manager.ts +138 -34
- package/src/protocol-server/client.ts +53 -0
- package/src/protocol-server/index.ts +1 -1
- package/src/server/api.ts +514 -72
- package/src/server/auth.ts +600 -74
- package/src/server/authorize.ts +816 -0
- package/src/server/clients.ts +235 -0
- package/src/server/config.ts +7 -0
- package/src/server/data.ts +59 -1
- package/src/server/directory.ts +69 -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 +519 -18
- package/src/server/index.ts +343 -61
- 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/remove.ts +148 -0
- package/src/server/routing.ts +275 -0
- package/src/verify/runner.ts +23 -2
- package/dist/instance-FExcyENK.js.map +0 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server handle an operator module receives (INTERFACES §18.6, D165 item 9): quarantine,
|
|
3
|
+
* pause, suspension, link revocation, moving and restoring homes, and deletion. Every target names
|
|
4
|
+
* a hosted route (`rte_…`), never a bare artifact or version ID, since those repeat across homes
|
|
5
|
+
* when a project is copied: acting on one route never touches another home hosting the same
|
|
6
|
+
* artifact ID or an identical version. Each call is durable when it resolves.
|
|
7
|
+
*/
|
|
8
|
+
import { createDiagnostic, RpcError } from "@mostra/protocol";
|
|
9
|
+
import { OPERATOR_REVISION, type ServerHandle } from "@mostra/protocol/operator";
|
|
10
|
+
import type { Ledger } from "../ledger/index.ts";
|
|
11
|
+
import type { Log } from "../log.ts";
|
|
12
|
+
import type { DataDirectory } from "./data.ts";
|
|
13
|
+
import type { HostedHome } from "./hosted.ts";
|
|
14
|
+
|
|
15
|
+
/** Options for {@link createHandle}. */
|
|
16
|
+
export interface HandleOptions {
|
|
17
|
+
readonly apiOrigin: string;
|
|
18
|
+
/** The data directory; set once it is open (the handle is made before the operator loads). */
|
|
19
|
+
readonly data: () => DataDirectory<HostedHome>;
|
|
20
|
+
readonly directory: () => Ledger;
|
|
21
|
+
/** Closes the bindings of a suspended home at once (the API origin's). */
|
|
22
|
+
readonly suspended: (homeId: string) => void;
|
|
23
|
+
readonly log: Log;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function unknownTarget(message: string): RpcError {
|
|
27
|
+
return new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message }));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Builds the handle. */
|
|
31
|
+
export function createHandle(options: HandleOptions): ServerHandle {
|
|
32
|
+
const { log } = options;
|
|
33
|
+
|
|
34
|
+
/** A route's home and artifact; an unknown route is refused, never guessed. */
|
|
35
|
+
const route = (routeId: string): { readonly home: string; readonly artifactId: string } => {
|
|
36
|
+
const row = options
|
|
37
|
+
.directory()
|
|
38
|
+
.read((db) =>
|
|
39
|
+
db.get<{ home_id: string; artifact_id: string }>(
|
|
40
|
+
"SELECT home_id, artifact_id FROM artifact_route WHERE id = ?",
|
|
41
|
+
routeId,
|
|
42
|
+
),
|
|
43
|
+
);
|
|
44
|
+
if (!row) throw unknownTarget(`No hosted route ${routeId}.`);
|
|
45
|
+
return { home: row.home_id, artifactId: row.artifact_id };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const homeOf = async (homeId: string): Promise<HostedHome> =>
|
|
49
|
+
(await options.data().home(homeId)).assembled;
|
|
50
|
+
|
|
51
|
+
const knownHome = (homeId: string): void => {
|
|
52
|
+
const found = options
|
|
53
|
+
.directory()
|
|
54
|
+
.read((db) => db.get("SELECT 1 FROM home WHERE id = ?", homeId));
|
|
55
|
+
if (!found) throw unknownTarget(`No hosted home ${homeId}.`);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
revision: OPERATOR_REVISION,
|
|
60
|
+
apiOrigin: options.apiOrigin,
|
|
61
|
+
|
|
62
|
+
async quarantine(target, reason) {
|
|
63
|
+
const found = route(target.routeId);
|
|
64
|
+
const home = await homeOf(found.home);
|
|
65
|
+
home.quarantine(
|
|
66
|
+
found.artifactId,
|
|
67
|
+
"versionId" in target ? target.versionId : undefined,
|
|
68
|
+
reason,
|
|
69
|
+
);
|
|
70
|
+
log.info("operator.quarantine", { routeId: target.routeId });
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
async release(target) {
|
|
74
|
+
const found = route(target.routeId);
|
|
75
|
+
const home = await homeOf(found.home);
|
|
76
|
+
home.release(found.artifactId, "versionId" in target ? target.versionId : undefined);
|
|
77
|
+
log.info("operator.release", { routeId: target.routeId });
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
async pause(routeId, reason) {
|
|
81
|
+
const found = route(routeId);
|
|
82
|
+
(await homeOf(found.home)).pause(found.artifactId, { paused: true, by: "operator", reason });
|
|
83
|
+
log.info("operator.pause", { routeId });
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
async resume(routeId) {
|
|
87
|
+
const found = route(routeId);
|
|
88
|
+
(await homeOf(found.home)).pause(found.artifactId, { paused: false, by: "operator" });
|
|
89
|
+
log.info("operator.resume", { routeId });
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
async suspendHome(homeId, reason) {
|
|
93
|
+
knownHome(homeId);
|
|
94
|
+
options
|
|
95
|
+
.directory()
|
|
96
|
+
.write((db) =>
|
|
97
|
+
db.run(
|
|
98
|
+
"UPDATE home SET suspended_at = COALESCE(suspended_at, ?), suspend_reason = ? WHERE id = ?",
|
|
99
|
+
new Date().toISOString(),
|
|
100
|
+
reason.slice(0, 1_000),
|
|
101
|
+
homeId,
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
options.suspended(homeId);
|
|
105
|
+
await options.data().closeHome(homeId);
|
|
106
|
+
log.info("operator.suspend_home", { home: homeId });
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
async resumeHome(homeId) {
|
|
110
|
+
knownHome(homeId);
|
|
111
|
+
options
|
|
112
|
+
.directory()
|
|
113
|
+
.write((db) =>
|
|
114
|
+
db.run("UPDATE home SET suspended_at = NULL, suspend_reason = NULL WHERE id = ?", homeId),
|
|
115
|
+
);
|
|
116
|
+
log.info("operator.resume_home", { home: homeId });
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
async revokeLinks(routeId) {
|
|
120
|
+
const found = route(routeId);
|
|
121
|
+
const count = (await homeOf(found.home)).revokeLinks(found.artifactId);
|
|
122
|
+
log.info("operator.revoke_links", { routeId, count });
|
|
123
|
+
return count;
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
async revokeLink(tokenId) {
|
|
127
|
+
const row = options
|
|
128
|
+
.directory()
|
|
129
|
+
.read((db) =>
|
|
130
|
+
db.get<{ home_id: string }>(
|
|
131
|
+
"SELECT home_id FROM hosted_token WHERE token_id = ?",
|
|
132
|
+
tokenId,
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
if (!row) throw unknownTarget(`No hosted link ${tokenId}.`);
|
|
136
|
+
if (!(await homeOf(row.home_id)).revokeLink(tokenId)) {
|
|
137
|
+
throw unknownTarget(`No hosted link ${tokenId}.`);
|
|
138
|
+
}
|
|
139
|
+
log.info("operator.revoke_link", { tokenId });
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
async drainHome(homeId) {
|
|
143
|
+
knownHome(homeId);
|
|
144
|
+
options.suspended(homeId);
|
|
145
|
+
await options.data().drain(homeId);
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
async releaseHome(homeId) {
|
|
149
|
+
knownHome(homeId);
|
|
150
|
+
options.data().undrain(homeId);
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
async restored(homeId) {
|
|
154
|
+
knownHome(homeId);
|
|
155
|
+
await options.data().restored(homeId);
|
|
156
|
+
log.info("operator.restored", { home: homeId });
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
async remove(routeId) {
|
|
160
|
+
const found = route(routeId);
|
|
161
|
+
const home = await homeOf(found.home);
|
|
162
|
+
try {
|
|
163
|
+
await home.remove(found.artifactId);
|
|
164
|
+
} finally {
|
|
165
|
+
// The home reopens from its ledger on its next request, holding nothing of the artifact,
|
|
166
|
+
// and nothing a removal cut off part-way left open in it (second Fable review, 8).
|
|
167
|
+
await options.data().closeHome(found.home);
|
|
168
|
+
}
|
|
169
|
+
log.info("operator.remove", { routeId });
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hosted home's pages beyond the local gateway's (ARCHITECTURE §15.6 to §15.8, INTERFACES
|
|
3
|
+
* §18.4, D161, D165 items 8, 9, 13, 14): who a browser is on a link minted without a viewer, what
|
|
4
|
+
* the trusted chrome shows around a view, which views and submissions the server's rules refuse
|
|
5
|
+
* before they reach the ledger, and the shell routes of a hosted home of its own (the report
|
|
6
|
+
* control, an anonymous viewer's name; sign-in arrives with the authorization server).
|
|
7
|
+
*
|
|
8
|
+
* Everything here answers through the gateway's one checked route table and its hooks
|
|
9
|
+
* (`HostedAddressing`), so every rule is applied in the same order as every other route's (D96).
|
|
10
|
+
*/
|
|
11
|
+
import { randomBytes } from "node:crypto";
|
|
12
|
+
import {
|
|
13
|
+
type ArtifactDetail,
|
|
14
|
+
createDiagnostic,
|
|
15
|
+
type HostedChrome,
|
|
16
|
+
type Refusal,
|
|
17
|
+
refusal,
|
|
18
|
+
} from "@mostra/protocol";
|
|
19
|
+
import type { LinkPolicy } from "@mostra/protocol/operator";
|
|
20
|
+
import {
|
|
21
|
+
type Capabilities,
|
|
22
|
+
type HostedAddressing,
|
|
23
|
+
isRefusal,
|
|
24
|
+
jsonResponse,
|
|
25
|
+
type RouteContext,
|
|
26
|
+
type RouteSpec,
|
|
27
|
+
readJsonObject,
|
|
28
|
+
} from "../gateway/index.ts";
|
|
29
|
+
import type { Interaction } from "../interaction/index.ts";
|
|
30
|
+
import type { Ledger } from "../ledger/index.ts";
|
|
31
|
+
import type { Log } from "../log.ts";
|
|
32
|
+
import type { ClientAddresses, RateLimit, RateLimiter } from "./clients.ts";
|
|
33
|
+
import type { Operator } from "./operator.ts";
|
|
34
|
+
|
|
35
|
+
/** How long the server's reports from one address may come: 20 an hour (the operator's inbox). */
|
|
36
|
+
const REPORT_RATE: RateLimit = { count: 20, perMs: 60 * 60_000 };
|
|
37
|
+
|
|
38
|
+
/** Who owns a hosted home, as its pages name them (D161's sender line). */
|
|
39
|
+
export interface HomeOwner {
|
|
40
|
+
readonly account: string;
|
|
41
|
+
/** The account's display name. */
|
|
42
|
+
readonly name?: string;
|
|
43
|
+
/** The label of the identity provider that verified the name; absent: unverified. */
|
|
44
|
+
readonly verifiedBy?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Options for {@link hostedPages}. */
|
|
48
|
+
export interface HostedPagesOptions {
|
|
49
|
+
readonly homeId: string;
|
|
50
|
+
readonly ledger: Ledger;
|
|
51
|
+
readonly interaction: Interaction;
|
|
52
|
+
readonly capabilities: Capabilities;
|
|
53
|
+
readonly artifact: (artifactId: string) => ArtifactDetail;
|
|
54
|
+
readonly routeOf: (
|
|
55
|
+
artifactId: string,
|
|
56
|
+
) => { readonly id: string; readonly label: string } | undefined;
|
|
57
|
+
readonly owner: () => HomeOwner;
|
|
58
|
+
/** Whether the server can sign a viewer in (an identity provider is configured or supplied). */
|
|
59
|
+
readonly signInOffered: () => boolean;
|
|
60
|
+
readonly operator: Operator;
|
|
61
|
+
readonly clients: ClientAddresses;
|
|
62
|
+
readonly rates: RateLimiter;
|
|
63
|
+
/** `rateLimits` of the configuration, in milliseconds (D165 item 13). */
|
|
64
|
+
readonly rateLimits: {
|
|
65
|
+
readonly perIpSubmissions?: RateLimit | undefined;
|
|
66
|
+
readonly perLinkSubmissions?: RateLimit | undefined;
|
|
67
|
+
};
|
|
68
|
+
/** The configuration's made-with mark (off unless an operator shows it, D161). */
|
|
69
|
+
readonly madeWith?: { readonly href?: string } | undefined;
|
|
70
|
+
/** Viewer sign-in on the authorization server, when one is configured (INTERFACES §18.4). */
|
|
71
|
+
readonly signin?: {
|
|
72
|
+
readonly start: (
|
|
73
|
+
signin: { homeId: string; routeId: string; sessionId: string; shellOrigin: string },
|
|
74
|
+
challenge: string,
|
|
75
|
+
) => { readonly url: string; readonly state: string };
|
|
76
|
+
readonly finish: (input: {
|
|
77
|
+
state: string;
|
|
78
|
+
code: string;
|
|
79
|
+
verifier: string;
|
|
80
|
+
routeId: string;
|
|
81
|
+
sessionId: string;
|
|
82
|
+
}) =>
|
|
83
|
+
| { readonly accountId: string; readonly name?: string; readonly provider?: string }
|
|
84
|
+
| { readonly refused: string };
|
|
85
|
+
};
|
|
86
|
+
/** Told each view the server admitted, for metering (`views`). */
|
|
87
|
+
readonly viewed?: (input: { readonly artifactId: string; readonly viewerId: string }) => void;
|
|
88
|
+
readonly log: Log;
|
|
89
|
+
readonly now?: () => number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** A refusal with any cataloged code (the gateway's own `refusal` takes HYGIENE codes only). */
|
|
93
|
+
function refuseWith(
|
|
94
|
+
code: Parameters<typeof createDiagnostic>[0],
|
|
95
|
+
message: string,
|
|
96
|
+
status: number,
|
|
97
|
+
): Refusal {
|
|
98
|
+
return { status, diagnostic: createDiagnostic(code, { message }) };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The milliseconds of a duration such as `7d` (checked by the operator's schema). */
|
|
102
|
+
function durationMs(text: string): number {
|
|
103
|
+
const unit = { s: 1_000, m: 60_000, h: 3_600_000, d: 86_400_000 }[text.slice(-1)] ?? 0;
|
|
104
|
+
return Number(text.slice(0, -1)) * unit;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The gateway hooks of one hosted home's pages. */
|
|
108
|
+
export function hostedPages(
|
|
109
|
+
options: HostedPagesOptions,
|
|
110
|
+
): Required<
|
|
111
|
+
Pick<
|
|
112
|
+
HostedAddressing,
|
|
113
|
+
| "client"
|
|
114
|
+
| "anonymousViewer"
|
|
115
|
+
| "admitView"
|
|
116
|
+
| "blocked"
|
|
117
|
+
| "chrome"
|
|
118
|
+
| "admitSubmit"
|
|
119
|
+
| "shellRoutes"
|
|
120
|
+
>
|
|
121
|
+
> {
|
|
122
|
+
const { ledger, interaction, capabilities, operator, log } = options;
|
|
123
|
+
const now = options.now ?? (() => Date.now());
|
|
124
|
+
|
|
125
|
+
/** A route's quarantine: the whole route's, or this version's (D165 item 9). */
|
|
126
|
+
const quarantineOf = (artifactId: string, versionId: string): string | undefined =>
|
|
127
|
+
ledger.read(
|
|
128
|
+
(db) =>
|
|
129
|
+
db.get<{ reason: string }>(
|
|
130
|
+
"SELECT reason FROM quarantine WHERE artifact_id = ? AND version_id IN ('', ?) LIMIT 1",
|
|
131
|
+
artifactId,
|
|
132
|
+
versionId,
|
|
133
|
+
)?.reason,
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const underReview = (): Refusal =>
|
|
137
|
+
refuseWith(
|
|
138
|
+
"MOSTRA_REMOTE_UNDER_REVIEW",
|
|
139
|
+
"This page is under review by the server's operator.",
|
|
140
|
+
403,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const policyFor = (artifactId: string, tokenId?: string): Promise<LinkPolicy> => {
|
|
144
|
+
const route = options.routeOf(artifactId);
|
|
145
|
+
if (!route) return Promise.resolve({});
|
|
146
|
+
return operator.linkPolicy({
|
|
147
|
+
account: options.owner().account,
|
|
148
|
+
home: options.homeId,
|
|
149
|
+
routeId: route.id,
|
|
150
|
+
artifactId,
|
|
151
|
+
...(tokenId !== undefined ? { tokenId } : {}),
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/** A link past the operator's longest lifetime or idle limit: revoked now, 410 (D165 item 9). */
|
|
156
|
+
const expired = (tokenId: string, policy: LinkPolicy): Refusal | undefined => {
|
|
157
|
+
if (policy.maxTtl === undefined && policy.idleExpiry === undefined) return undefined;
|
|
158
|
+
const used = ledger.read((db) =>
|
|
159
|
+
db.get<{ created_at: string; last: string | null }>(
|
|
160
|
+
`SELECT t.created_at, (SELECT MAX(last_seen_at) FROM session s WHERE s.token_id = t.id) AS last
|
|
161
|
+
FROM token t WHERE t.id = ?`,
|
|
162
|
+
tokenId,
|
|
163
|
+
),
|
|
164
|
+
);
|
|
165
|
+
if (!used) return undefined;
|
|
166
|
+
const minted = Date.parse(used.created_at);
|
|
167
|
+
const lastUse = Math.max(minted, used.last === null ? 0 : Date.parse(used.last));
|
|
168
|
+
const tooOld = policy.maxTtl !== undefined && now() - minted > durationMs(policy.maxTtl);
|
|
169
|
+
const idle = policy.idleExpiry !== undefined && now() - lastUse > durationMs(policy.idleExpiry);
|
|
170
|
+
if (!tooOld && !idle) return undefined;
|
|
171
|
+
capabilities.revoke(tokenId);
|
|
172
|
+
log.info("hosted.link_expired", { tokenId, why: tooOld ? "max-ttl" : "idle" });
|
|
173
|
+
return refusal("MOSTRA_HYGIENE_TOKEN_REVOKED", "This link has expired.", 410);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const limitRefusal = (
|
|
177
|
+
verdict: Awaited<ReturnType<Operator["limit"]>>,
|
|
178
|
+
): (Refusal & { readonly retryAfter?: number }) | undefined => {
|
|
179
|
+
if (verdict.allowed !== false) return undefined;
|
|
180
|
+
return {
|
|
181
|
+
...refuseWith("MOSTRA_REMOTE_LIMIT_REACHED", verdict.message, 403),
|
|
182
|
+
...(verdict.retryAfterMs !== undefined
|
|
183
|
+
? { retryAfter: Math.max(1, Math.ceil(verdict.retryAfterMs / 1000)) }
|
|
184
|
+
: {}),
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const rateRefusal = (retryAfterS: number): Refusal & { readonly retryAfter: number } => ({
|
|
189
|
+
...refuseWith(
|
|
190
|
+
"MOSTRA_REMOTE_RATE_LIMITED",
|
|
191
|
+
"Too many answers came through here in a short time; try again shortly.",
|
|
192
|
+
429,
|
|
193
|
+
),
|
|
194
|
+
retryAfter: retryAfterS,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const shellRoutes = (helpers: Parameters<NonNullable<HostedAddressing["shellRoutes"]>>[0]) => {
|
|
198
|
+
const meta = (context: RouteContext) => ({
|
|
199
|
+
method: context.request.method,
|
|
200
|
+
path: new URL(context.request.url).pathname,
|
|
201
|
+
document: false,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
/** The report control (D161): reaches the operator's `abuse.report`. */
|
|
205
|
+
const report = async (context: RouteContext): Promise<Response> => {
|
|
206
|
+
const artifactId = context.artifactId as string;
|
|
207
|
+
const body = await readJsonObject(context.request, 16 * 1024);
|
|
208
|
+
if (isRefusal(body)) return helpers.refuse(body, meta(context));
|
|
209
|
+
const reason = body["reason"];
|
|
210
|
+
const detail = body["detail"];
|
|
211
|
+
if (
|
|
212
|
+
typeof reason !== "string" ||
|
|
213
|
+
!/^[a-z][a-z0-9-]{0,31}$/.test(reason) ||
|
|
214
|
+
(detail !== undefined && (typeof detail !== "string" || detail.length > 2_000))
|
|
215
|
+
) {
|
|
216
|
+
return helpers.refuse(
|
|
217
|
+
refusal(
|
|
218
|
+
"MOSTRA_HYGIENE_REQUEST_MALFORMED",
|
|
219
|
+
"A report names a `reason` (a short word) and at most 2000 characters of `detail`.",
|
|
220
|
+
),
|
|
221
|
+
meta(context),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
const limited = options.rates.take(
|
|
225
|
+
`report:${options.clients.keyOf(context.client)}`,
|
|
226
|
+
REPORT_RATE,
|
|
227
|
+
);
|
|
228
|
+
if (!limited.ok) return helpers.refuse(rateRefusal(limited.retryAfterS), meta(context));
|
|
229
|
+
const route = options.routeOf(artifactId);
|
|
230
|
+
if (!route) {
|
|
231
|
+
return helpers.refuse(
|
|
232
|
+
refusal("MOSTRA_HYGIENE_NOT_SERVED", "No route here."),
|
|
233
|
+
meta(context),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
await operator.report({
|
|
237
|
+
routeId: route.id,
|
|
238
|
+
label: route.label,
|
|
239
|
+
artifactId,
|
|
240
|
+
home: options.homeId,
|
|
241
|
+
...(context.session?.versionId !== undefined
|
|
242
|
+
? { versionId: context.session.versionId }
|
|
243
|
+
: {}),
|
|
244
|
+
reason,
|
|
245
|
+
...(typeof detail === "string" && detail !== "" ? { detail } : {}),
|
|
246
|
+
at: new Date(now()).toISOString(),
|
|
247
|
+
});
|
|
248
|
+
log.info("hosted.reported", { routeId: route.id, reason });
|
|
249
|
+
return jsonResponse({}, { status: 202 });
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/** An anonymous viewer's self-chosen name, recorded unverified (D165 item 8). */
|
|
253
|
+
const name = async (context: RouteContext): Promise<Response> => {
|
|
254
|
+
const body = await readJsonObject(context.request, 4 * 1024);
|
|
255
|
+
if (isRefusal(body)) return helpers.refuse(body, meta(context));
|
|
256
|
+
const value = body["name"];
|
|
257
|
+
const cleaned =
|
|
258
|
+
value === null
|
|
259
|
+
? null
|
|
260
|
+
: typeof value === "string"
|
|
261
|
+
? value.replace(/\s+/g, " ").trim().slice(0, 64)
|
|
262
|
+
: undefined;
|
|
263
|
+
if (cleaned === undefined || cleaned === "") {
|
|
264
|
+
return helpers.refuse(
|
|
265
|
+
refusal("MOSTRA_HYGIENE_REQUEST_MALFORMED", "`name` is a short text, or null."),
|
|
266
|
+
meta(context),
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
const viewerId = interaction.viewerOf(context.session?.viewerId);
|
|
270
|
+
if (!interaction.nameAnonymous(viewerId, cleaned)) {
|
|
271
|
+
return helpers.refuse(
|
|
272
|
+
refusal(
|
|
273
|
+
"MOSTRA_HYGIENE_SCOPE_DENIED",
|
|
274
|
+
"Only a viewer who isn't signed in names themselves here; a signed-in name is the account's.",
|
|
275
|
+
),
|
|
276
|
+
meta(context),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
return jsonResponse({ viewer: interaction.viewer(viewerId) });
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* A page's sign-in, from a click in the trusted chrome only (INTERFACES §18.4): the address of
|
|
284
|
+
* the authorization server, with `state` bound to this route and this shell session.
|
|
285
|
+
*/
|
|
286
|
+
const signinStart = async (context: RouteContext): Promise<Response> => {
|
|
287
|
+
const body = await readJsonObject(context.request, 4 * 1024);
|
|
288
|
+
if (isRefusal(body)) return helpers.refuse(body, meta(context));
|
|
289
|
+
const challenge = body["challenge"];
|
|
290
|
+
const route = options.routeOf(context.artifactId as string);
|
|
291
|
+
if (
|
|
292
|
+
typeof challenge !== "string" ||
|
|
293
|
+
!/^[A-Za-z0-9_-]{43}$/.test(challenge) ||
|
|
294
|
+
!route ||
|
|
295
|
+
!options.signin ||
|
|
296
|
+
!context.session
|
|
297
|
+
) {
|
|
298
|
+
return helpers.refuse(
|
|
299
|
+
refusal(
|
|
300
|
+
"MOSTRA_HYGIENE_REQUEST_MALFORMED",
|
|
301
|
+
"A sign-in starts with a PKCE S256 challenge.",
|
|
302
|
+
),
|
|
303
|
+
meta(context),
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const started = options.signin.start(
|
|
307
|
+
{
|
|
308
|
+
homeId: options.homeId,
|
|
309
|
+
routeId: route.id,
|
|
310
|
+
sessionId: context.session.id,
|
|
311
|
+
// The shell's own origin: the pipeline checked the request's Origin is exactly it.
|
|
312
|
+
shellOrigin: context.request.headers.get("origin") ?? "",
|
|
313
|
+
},
|
|
314
|
+
challenge,
|
|
315
|
+
);
|
|
316
|
+
return jsonResponse(started);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/** The code back from the authorization server: the session acts as the account from now on. */
|
|
320
|
+
const signinFinish = async (context: RouteContext): Promise<Response> => {
|
|
321
|
+
const body = await readJsonObject(context.request, 8 * 1024);
|
|
322
|
+
if (isRefusal(body)) return helpers.refuse(body, meta(context));
|
|
323
|
+
const route = options.routeOf(context.artifactId as string);
|
|
324
|
+
const { code, verifier, state } = body as Record<string, unknown>;
|
|
325
|
+
if (
|
|
326
|
+
typeof code !== "string" ||
|
|
327
|
+
typeof verifier !== "string" ||
|
|
328
|
+
typeof state !== "string" ||
|
|
329
|
+
!route ||
|
|
330
|
+
!options.signin ||
|
|
331
|
+
!context.session
|
|
332
|
+
) {
|
|
333
|
+
return helpers.refuse(
|
|
334
|
+
refusal(
|
|
335
|
+
"MOSTRA_HYGIENE_REQUEST_MALFORMED",
|
|
336
|
+
"Send the sign-in's code, verifier and state.",
|
|
337
|
+
),
|
|
338
|
+
meta(context),
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
const finished = options.signin.finish({
|
|
342
|
+
code,
|
|
343
|
+
verifier,
|
|
344
|
+
state,
|
|
345
|
+
routeId: route.id,
|
|
346
|
+
sessionId: context.session.id,
|
|
347
|
+
});
|
|
348
|
+
if ("refused" in finished) {
|
|
349
|
+
log.warn("hosted.signin_refused", { routeId: route.id });
|
|
350
|
+
return helpers.refuse(
|
|
351
|
+
refusal("MOSTRA_HYGIENE_SCOPE_DENIED", finished.refused, 403),
|
|
352
|
+
meta(context),
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
// A link minted for a named person keeps acting as that person (D104); any other session
|
|
356
|
+
// now acts as the account, its name verified by its provider (D165 item 8).
|
|
357
|
+
const viewerId = interaction.viewerForAccount(finished.accountId, finished.name);
|
|
358
|
+
if (context.session.grant.viewerId === undefined) {
|
|
359
|
+
const sessionId = context.session.id;
|
|
360
|
+
ledger.write((db) =>
|
|
361
|
+
db.run("UPDATE session SET viewer_id = ? WHERE id = ?", viewerId, sessionId),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
log.info("hosted.signed_in", { routeId: route.id });
|
|
365
|
+
return jsonResponse({ viewer: interaction.viewer(viewerId) });
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const routes: RouteSpec[] = [
|
|
369
|
+
{
|
|
370
|
+
method: "POST",
|
|
371
|
+
path: "/a/:artifactId/_mostra/session/signin/start",
|
|
372
|
+
intent: "mutation",
|
|
373
|
+
auth: "session",
|
|
374
|
+
handler: signinStart,
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
method: "POST",
|
|
378
|
+
path: "/a/:artifactId/_mostra/session/signin",
|
|
379
|
+
intent: "mutation",
|
|
380
|
+
auth: "session",
|
|
381
|
+
handler: signinFinish,
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
method: "POST",
|
|
385
|
+
path: "/a/:artifactId/_mostra/report",
|
|
386
|
+
intent: "mutation",
|
|
387
|
+
auth: "session",
|
|
388
|
+
handler: report,
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
method: "POST",
|
|
392
|
+
path: "/a/:artifactId/_mostra/session/name",
|
|
393
|
+
intent: "mutation",
|
|
394
|
+
auth: "session",
|
|
395
|
+
handler: name,
|
|
396
|
+
},
|
|
397
|
+
];
|
|
398
|
+
return routes;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
client: (incoming) => options.clients.of(incoming),
|
|
403
|
+
|
|
404
|
+
anonymousViewer: (browser) =>
|
|
405
|
+
interaction.viewerForAnonymous(
|
|
406
|
+
// Without the route's key (storage blocked), a fresh viewer for this session alone: its
|
|
407
|
+
// draft is this page's until a reload, never anyone else's.
|
|
408
|
+
browser ?? `session:${randomBytes(16).toString("hex")}`,
|
|
409
|
+
),
|
|
410
|
+
|
|
411
|
+
async admitView(input) {
|
|
412
|
+
if (quarantineOf(input.artifactId, input.versionId) !== undefined) return underReview();
|
|
413
|
+
const policy = await policyFor(input.artifactId, input.tokenId);
|
|
414
|
+
const gone = expired(input.tokenId, policy);
|
|
415
|
+
if (gone) return gone;
|
|
416
|
+
const route = options.routeOf(input.artifactId);
|
|
417
|
+
// `view` proceeds when the operator doesn't answer: nothing a person sees is held up by it.
|
|
418
|
+
const verdict = await operator.limit({
|
|
419
|
+
account: options.owner().account,
|
|
420
|
+
home: options.homeId,
|
|
421
|
+
artifact: input.artifactId,
|
|
422
|
+
...(route ? { routeId: route.id } : {}),
|
|
423
|
+
operation: "view",
|
|
424
|
+
});
|
|
425
|
+
const refused = limitRefusal(verdict);
|
|
426
|
+
if (refused) return refused;
|
|
427
|
+
return undefined;
|
|
428
|
+
},
|
|
429
|
+
|
|
430
|
+
blocked: (artifactId, versionId) =>
|
|
431
|
+
quarantineOf(artifactId, versionId) !== undefined ? underReview() : undefined,
|
|
432
|
+
|
|
433
|
+
async chrome(input) {
|
|
434
|
+
const owner = options.owner();
|
|
435
|
+
const viewer = interaction.viewer(input.viewerId);
|
|
436
|
+
const policy = await policyFor(input.artifactId, input.tokenId);
|
|
437
|
+
options.viewed?.({ artifactId: input.artifactId, viewerId: input.viewerId });
|
|
438
|
+
const chrome: HostedChrome = {
|
|
439
|
+
...(owner.name !== undefined
|
|
440
|
+
? {
|
|
441
|
+
sender: {
|
|
442
|
+
name: owner.name,
|
|
443
|
+
...(owner.verifiedBy !== undefined ? { verifiedBy: owner.verifiedBy } : {}),
|
|
444
|
+
},
|
|
445
|
+
}
|
|
446
|
+
: {}),
|
|
447
|
+
viewer:
|
|
448
|
+
viewer.kind === "account"
|
|
449
|
+
? { kind: "account", ...(viewer.name ? { name: viewer.name } : {}), verified: true }
|
|
450
|
+
: viewer.kind === "anonymous"
|
|
451
|
+
? {
|
|
452
|
+
kind: "anonymous",
|
|
453
|
+
...(viewer.name ? { name: viewer.name } : {}),
|
|
454
|
+
verified: false,
|
|
455
|
+
}
|
|
456
|
+
: { kind: "named", ...(viewer.name ? { name: viewer.name } : {}), verified: false },
|
|
457
|
+
signIn: viewer.kind !== "account" && options.signInOffered(),
|
|
458
|
+
...(policy.footer !== undefined ? { footer: policy.footer } : {}),
|
|
459
|
+
...(policy.afterSubmit !== undefined ? { afterSubmit: policy.afterSubmit } : {}),
|
|
460
|
+
...((policy.madeWith?.show ?? options.madeWith !== undefined)
|
|
461
|
+
? {
|
|
462
|
+
madeWith: {
|
|
463
|
+
...((policy.madeWith?.href ?? options.madeWith?.href)
|
|
464
|
+
? { href: (policy.madeWith?.href ?? options.madeWith?.href) as string }
|
|
465
|
+
: {}),
|
|
466
|
+
},
|
|
467
|
+
}
|
|
468
|
+
: {}),
|
|
469
|
+
...(policy.interstitial !== undefined ? { interstitial: policy.interstitial } : {}),
|
|
470
|
+
};
|
|
471
|
+
return chrome;
|
|
472
|
+
},
|
|
473
|
+
|
|
474
|
+
async admitSubmit(page, context) {
|
|
475
|
+
const detail = options.artifact(page.artifactId);
|
|
476
|
+
// `hosted.submissions: "signed-in"`: an account, or a link minted for a named person.
|
|
477
|
+
if (detail.manifest?.hosted?.submissions === "signed-in") {
|
|
478
|
+
const viewer = interaction.viewer(page.viewerId);
|
|
479
|
+
const named = context.session?.grant.viewerId !== undefined;
|
|
480
|
+
if (viewer.kind !== "account" && !named) {
|
|
481
|
+
return refuseWith(
|
|
482
|
+
"MOSTRA_REMOTE_SIGN_IN_REQUIRED",
|
|
483
|
+
"This page takes answers from signed-in viewers; sign in, then submit again. The draft is kept.",
|
|
484
|
+
403,
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
// The server's rates (D165 item 13), then the operator's `limits`. The rates are taken first,
|
|
489
|
+
// together or not at all (Fable review, 8), so an attempt past them never reaches the
|
|
490
|
+
// operator, concurrent ones included; an attempt the operator then refuses gets its tokens
|
|
491
|
+
// back (second Fable review, 2). Nothing is recorded past either, and the draft is kept.
|
|
492
|
+
const limits = options.rateLimits;
|
|
493
|
+
const wanted: { key: string; rate: NonNullable<typeof limits.perIpSubmissions> }[] = [];
|
|
494
|
+
if (limits.perIpSubmissions) {
|
|
495
|
+
wanted.push({
|
|
496
|
+
key: `submit-ip:${options.clients.keyOf(context.client)}`,
|
|
497
|
+
rate: limits.perIpSubmissions,
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
if (limits.perLinkSubmissions && page.tokenId !== undefined) {
|
|
501
|
+
wanted.push({ key: `submit-link:${page.tokenId}`, rate: limits.perLinkSubmissions });
|
|
502
|
+
}
|
|
503
|
+
const taken = options.rates.takeAll(wanted);
|
|
504
|
+
if (!taken.ok) return rateRefusal(taken.retryAfterS);
|
|
505
|
+
// The operator's `limits`: a refusal keeps the draft; no answer lets the submission through.
|
|
506
|
+
const route = options.routeOf(page.artifactId);
|
|
507
|
+
const verdict = await operator.limit({
|
|
508
|
+
account: options.owner().account,
|
|
509
|
+
home: options.homeId,
|
|
510
|
+
artifact: page.artifactId,
|
|
511
|
+
...(route ? { routeId: route.id } : {}),
|
|
512
|
+
operation: "submit",
|
|
513
|
+
});
|
|
514
|
+
const refused = limitRefusal(verdict);
|
|
515
|
+
if (refused) options.rates.refundAll(wanted);
|
|
516
|
+
return refused;
|
|
517
|
+
},
|
|
518
|
+
|
|
519
|
+
shellRoutes,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** A diagnostic for a hosted home this server won't serve now: suspended, or moving. */
|
|
524
|
+
export function homeUnavailable(why: "suspended" | "moving"): ReturnType<typeof createDiagnostic> {
|
|
525
|
+
return why === "suspended"
|
|
526
|
+
? createDiagnostic("MOSTRA_REMOTE_HOME_SUSPENDED", {
|
|
527
|
+
message: "This account's pages are suspended by the server's operator.",
|
|
528
|
+
})
|
|
529
|
+
: createDiagnostic("MOSTRA_REMOTE_HOME_MOVING", {
|
|
530
|
+
message: "This account's pages are moving to another machine; try again in a minute.",
|
|
531
|
+
});
|
|
532
|
+
}
|