@superbee/view-host 0.0.0-stage → 0.1.1-pre.1

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.
@@ -0,0 +1,154 @@
1
+ /**
2
+ * An operation-mapped View bridge host: the parent-page half of the View protocol
3
+ * (`docs/VIEW-PROTOCOL.md`, bridge `v0` and the v1 `read-versioned` and `action.propose`), for a
4
+ * page that runs a View in a sandboxed frame. It parses what the View's frame posts, maps each data
5
+ * request onto one named operation that the page's `execute` callback runs on the person's behalf,
6
+ * and answers in the protocol's vocabulary. It has no data semantics of its own:
7
+ *
8
+ * | request | operation |
9
+ * | --- | --- |
10
+ * | `query` | `documents.query.v1` |
11
+ * | `read`, `read-versioned`, `render-document` | `documents.read.v1` |
12
+ * | `relationships` | `documents.relationships.v1` |
13
+ * | `edges` | `documents.edges.v1` |
14
+ * | `graph` | `bundles.graph.v1` |
15
+ * | `action.propose` (v1) | the page's `proposeAction` (`documents.update.v1` behind its confirmation) |
16
+ *
17
+ * A request whose operation the caller does not offer is not declared in `hello` and is refused
18
+ * with `FORBIDDEN`. A `query` asks for up to 500 rows; the host reads `documents.query.v1` a page of
19
+ * at most 100 at a time (`./query-pages.ts`, the rule the local View bridge shares), so one `query`
20
+ * is several `execute` calls, and answers an exact `count` and `more`. Rows carry raw frontmatter:
21
+ * `query.kind-projection` is not declared.
22
+ *
23
+ * The bundle is fixed when the host is made: every operation input carries that `bundleId` and a
24
+ * View cannot name another. Nothing here builds an address: `hello.bundle` is `{ root: null, name }`,
25
+ * and `open-page` asks the page's own resolver.
26
+ *
27
+ * A host that stops answering must remove the frame. The protocol's reference client never times a
28
+ * request out, so a frame whose requests go unanswered waits forever: when `admitted()` turns false
29
+ * the host calls `invalidate()` and answers nothing more, and when `stop()` runs waiting requests are
30
+ * dropped. In both cases the page removes the View's frame (`invalidate` is where it does so).
31
+ */
32
+ import { ACTION_BRIDGE_PROTOCOL, type DocumentAction } from "./action-bridge.js";
33
+ export { ReadAdapterError } from "./read-adapter-error.js";
34
+ /** The row orders `documents.query.v1` answers: canonical id (the default) or newest first. */
35
+ export declare const QUERY_ORDERS: readonly ["id", "newest"];
36
+ /** `bundles.graph.v1`'s ceilings, which equal the protocol's `graph` ceilings. */
37
+ export declare const GRAPH_MAX_DOCUMENTS = 1000;
38
+ export declare const GRAPH_MAX_RELATIONSHIPS = 10000;
39
+ export declare const BRIDGE_PROTOCOL = "v0";
40
+ export { ACTION_BRIDGE_PROTOCOL };
41
+ /** The operations a View's requests map onto, by the names the caller's `execute` runs them under. */
42
+ export declare const VIEW_BRIDGE_OPERATIONS: readonly ["documents.read.v1", "documents.query.v1", "documents.relationships.v1", "documents.edges.v1", "bundles.graph.v1", "documents.update.v1"];
43
+ export type ViewBridgeOperation = (typeof VIEW_BRIDGE_OPERATIONS)[number];
44
+ /** The ceilings this host enforces where the caller offers the request; `bridgeLimits` zeroes the
45
+ * ones it does not offer, which is how the protocol expresses "not offered". */
46
+ export declare const BRIDGE_LIMITS: Readonly<{
47
+ query: 500;
48
+ edges: 1000;
49
+ graphDocuments: 1000;
50
+ graphRelationships: 10000;
51
+ replyBytes: number;
52
+ }>;
53
+ export type BridgeErrorCode = "USAGE" | "FORBIDDEN" | "REVOKED" | "TOO_LARGE" | "RUNTIME" | "NOT_FOUND";
54
+ /** What the page's reader answers for `record.open`. */
55
+ export type ViewBridgeUiResult = {
56
+ ok: true;
57
+ } | {
58
+ ok: false;
59
+ code: "unsupported" | "invalid_input" | "not_found" | "denied" | "unavailable" | "cancelled" | "busy";
60
+ };
61
+ /** The View's frame window as the host uses it: where replies are posted. */
62
+ export interface ViewBridgeFrame {
63
+ postMessage(message: unknown, targetOrigin: string): void;
64
+ }
65
+ /** A `message` event the page received, as the host reads it. */
66
+ export interface ViewBridgeMessage {
67
+ readonly source: unknown;
68
+ readonly origin: string;
69
+ readonly data: unknown;
70
+ }
71
+ /** The one bundle the View reads: its id (sent with every operation) and the name the page shows
72
+ * for it (`hello.bundle.name`, never an internal id). */
73
+ export interface ViewBridgeBundle {
74
+ readonly id: string;
75
+ readonly name: string;
76
+ }
77
+ /** Runs one operation on the person's behalf, as the caller runs it, with the host's input (the fixed
78
+ * `bundleId` included) and answers the operation's result envelope. */
79
+ export type ViewBridgeExecute = (operation: ViewBridgeOperation, input: unknown, signal: AbortSignal) => Promise<unknown>;
80
+ /** Everything else the host is given, in one object so later options are added without changing
81
+ * the call. */
82
+ export interface ViewBridgeHostOptions {
83
+ /** The operations the caller runs on the person's behalf for this View; a request mapped to any
84
+ * other is not declared in `hello` and is refused with FORBIDDEN. */
85
+ readonly operations: readonly ViewBridgeOperation[];
86
+ /** Whether the View may still be answered (the page's binding, grant or lifetime). Checked before
87
+ * and after every operation; when false the host calls `invalidate` and answers nothing. */
88
+ admitted?(): boolean;
89
+ /** The page removes the View's frame (and stops this host). */
90
+ invalidate?(): void;
91
+ /** Called once, on the first bridge request of any type: a View need not send `hello`. */
92
+ onReady?(): void;
93
+ /** `render-document`: the shared inert Markdown renderer (`@superbee/markdown-renderer/static`'s
94
+ * `renderDocumentToStaticHtml`), so a View receives the same markup on every host. Absent, the
95
+ * request is not offered. */
96
+ renderDocument?(document: {
97
+ id: string;
98
+ body: string;
99
+ }): {
100
+ html: string;
101
+ bounded: boolean;
102
+ };
103
+ /** v1 `action.propose`, behind the page's own confirmation; offered only with `documents.read.v1`
104
+ * and `documents.update.v1` in `operations`. */
105
+ proposeAction?(action: DocumentAction, signal: AbortSignal): Promise<unknown>;
106
+ /** `host` capability `record.open`: the page opens its own reader for one document. */
107
+ recordAvailable?(): boolean;
108
+ openRecord?(documentId: string, signal: AbortSignal): Promise<ViewBridgeUiResult>;
109
+ /** The page owns the frame's presentation: `hello.host.frame` tells the View so, `theme` carries
110
+ * the page's tokens, and `frame.resize` lets the View report its content height. `resize` returns
111
+ * the height the frame now draws at, within the page's bounds; `maxHeight` is the largest. */
112
+ frame?: {
113
+ resize(height: number): number;
114
+ readonly maxHeight: number;
115
+ readonly theme?: Readonly<Record<string, string>>;
116
+ };
117
+ /** `open-page`: the page opens the View its bundle registers as `registryId` (a sibling of this
118
+ * View in the same bundle), resolving it from what the page already knows, never from the
119
+ * request, and answers whether it did. Absent, `open-page` is not offered. */
120
+ openPage?(registryId: string): boolean;
121
+ }
122
+ /** The registry capabilities this host declares for `operations` and the page's services, sorted as
123
+ * the protocol requires. `documents.query.v1` answers the OR of field values, the terminal filter,
124
+ * a total count and newest-first order, so `query.field-or`, `query.open`, `query.count` and
125
+ * `query.newest` are declared with it. Rows carry raw frontmatter, so `query.kind-projection` is
126
+ * not. Whether `query` is offered at all is `limits.query`, not a capability. */
127
+ export declare function bridgeCapabilities(operations: ReadonlySet<ViewBridgeOperation>, services?: {
128
+ renderDocument?: boolean;
129
+ recordOpen?: boolean;
130
+ frameResize?: boolean;
131
+ openPage?: boolean;
132
+ }): string[];
133
+ /** The declared ceilings: `0` for a request the caller does not offer. */
134
+ export declare function bridgeLimits(operations: ReadonlySet<ViewBridgeOperation>): {
135
+ query: number;
136
+ edges: number;
137
+ graphDocuments: number;
138
+ graphRelationships: number;
139
+ replyBytes: number;
140
+ };
141
+ /** The host for one View frame. `frame` is the frame's window (the only source whose messages are
142
+ * admitted, and only from the opaque origin `"null"` a `sandbox="allow-scripts"` frame has);
143
+ * `bundle` is fixed for the host's life; `execute` runs an operation on the person's behalf. The
144
+ * page forwards every `message` event to `receive`, calls `refresh` when its data may have
145
+ * changed, and `stop` when it removes the frame. */
146
+ export declare function createViewBridgeHost(frame: ViewBridgeFrame, bundle: ViewBridgeBundle, execute: ViewBridgeExecute, options: ViewBridgeHostOptions): {
147
+ /** An empty `change` after `subscribe`: the refresh signal a host without deltas may send. */
148
+ refresh(): void;
149
+ /** The frame is gone, or about to be: nothing waiting is answered and nothing more is read. */
150
+ stop(): void;
151
+ receive(event: ViewBridgeMessage): Promise<void>;
152
+ };
153
+ export type ViewBridgeHost = ReturnType<typeof createViewBridgeHost>;
154
+ //# sourceMappingURL=view-bridge-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