@botiverse/porthole 0.2.4 → 0.3.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/README.md +14 -9
- package/dist/check/check.d.ts +83 -0
- package/dist/check/check.js +169 -0
- package/dist/check/drivers.d.ts +24 -0
- package/dist/check/drivers.js +110 -0
- package/dist/check/index.d.ts +8 -0
- package/dist/check/index.js +8 -0
- package/dist/check/lint.d.ts +21 -0
- package/dist/check/lint.js +131 -0
- package/dist/check/page-source.d.ts +2 -0
- package/dist/check/page-source.js +2 -0
- package/dist/check/page.d.ts +51 -0
- package/dist/check/page.js +150 -0
- package/dist/check/tools.d.ts +61 -0
- package/dist/check/tools.js +140 -0
- package/dist/conformance/host-kit.d.ts +10 -2
- package/dist/conformance/host-kit.js +9 -2
- package/dist/displays/displays.d.ts +61 -0
- package/dist/displays/displays.js +113 -0
- package/dist/host/client.d.ts +92 -0
- package/dist/host/client.js +259 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -0
- package/dist/node/index.d.ts +22 -1
- package/dist/node/index.js +46 -1
- package/dist/react/frame.d.ts +71 -3
- package/dist/react/frame.js +120 -19
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -0
- package/dist/react/view.d.ts +54 -0
- package/dist/react/view.js +146 -0
- package/dist/server/bootstrap.d.ts +27 -0
- package/dist/server/bootstrap.js +63 -0
- package/dist/server/host.d.ts +74 -7
- package/dist/server/host.js +237 -9
- package/dist/server/index.d.ts +4 -2
- package/dist/server/index.js +3 -1
- package/dist/server/pages.d.ts +19 -0
- package/dist/server/pages.js +8 -0
- package/dist/server/proxy.d.ts +24 -0
- package/dist/server/proxy.js +67 -0
- package/dist/server/server.js +14 -1
- package/dist/server/types.d.ts +8 -0
- package/docs/ADOPTING.md +191 -104
- package/docs/DECISIONS.md +146 -0
- package/docs/DESIGN.md +58 -0
- package/examples/app.mjs +62 -64
- package/examples/conformance/page.spec.mjs +23 -0
- package/examples/conformance/playwright.config.mjs +1 -1
- package/examples/conformance/porthole.spec.mjs +5 -3
- package/examples/displays.mjs +19 -0
- package/examples/mcp/server.mjs +121 -0
- package/package.json +9 -1
- package/src/check/check.ts +278 -0
- package/src/check/drivers.ts +144 -0
- package/src/check/index.ts +15 -0
- package/src/check/lint.ts +238 -0
- package/src/check/page-source.ts +19 -0
- package/src/check/page.ts +204 -0
- package/src/check/tools.ts +206 -0
- package/src/conformance/host-kit.ts +20 -11
- package/src/displays/displays.ts +191 -0
- package/src/host/client.ts +364 -0
- package/src/index.ts +25 -0
- package/src/node/index.ts +66 -2
- package/src/react/frame.tsx +191 -33
- package/src/react/index.ts +8 -0
- package/src/react/view.tsx +232 -0
- package/src/server/bootstrap.ts +97 -0
- package/src/server/host.ts +331 -15
- package/src/server/index.ts +11 -1
- package/src/server/pages.ts +28 -0
- package/src/server/proxy.ts +86 -0
- package/src/server/server.ts +26 -2
- package/src/server/types.ts +5 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { LocateError } from "./locator.js";
|
|
2
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
3
|
+
/**
|
|
4
|
+
* Whether a sign-in's `next` is the app's authorize route (P072): the open-redirect guard a
|
|
5
|
+
* single-page app runs before going on to it. Null for anything else.
|
|
6
|
+
*/
|
|
7
|
+
export function entryTarget(next, authorizeUrl) {
|
|
8
|
+
if (typeof next !== "string" || next.length > 8_192) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
let target;
|
|
12
|
+
let route;
|
|
13
|
+
try {
|
|
14
|
+
route = new URL(authorizeUrl);
|
|
15
|
+
target = new URL(next, route);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return target.origin === route.origin &&
|
|
21
|
+
target.pathname === route.pathname &&
|
|
22
|
+
target.hash === "" &&
|
|
23
|
+
target.searchParams.has("state")
|
|
24
|
+
? target.href
|
|
25
|
+
: null;
|
|
26
|
+
}
|
|
27
|
+
export function portholeClient(options) {
|
|
28
|
+
const base = (options.base ?? "/porthole").replace(/\/+$/u, "");
|
|
29
|
+
const ask = options.fetch ?? ((input, init) => fetch(input, init));
|
|
30
|
+
const headersNow = () => new Headers(typeof options.headers === "function" ? options.headers() : options.headers);
|
|
31
|
+
const credentials = options.credentials ?? "same-origin";
|
|
32
|
+
const here = () => (typeof location === "undefined" ? "http://localhost/" : location.href);
|
|
33
|
+
const url = (path) => new URL(`${base}${path}`, here()).href;
|
|
34
|
+
let meta = options.meta ?? null;
|
|
35
|
+
const listeners = new Set();
|
|
36
|
+
const links = new Map();
|
|
37
|
+
const keyOf = (instance) => JSON.stringify(instance);
|
|
38
|
+
const set = (next) => {
|
|
39
|
+
if (JSON.stringify(next) !== JSON.stringify(meta)) {
|
|
40
|
+
meta = next;
|
|
41
|
+
for (const listener of listeners) {
|
|
42
|
+
listener();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
let refreshing = null;
|
|
47
|
+
const refresh = () => {
|
|
48
|
+
refreshing ??= (async () => {
|
|
49
|
+
try {
|
|
50
|
+
const answer = await ask(url("/meta"), {
|
|
51
|
+
headers: headersNow(),
|
|
52
|
+
credentials,
|
|
53
|
+
cache: "no-store",
|
|
54
|
+
});
|
|
55
|
+
const data = answer.ok ? await answer.json() : null;
|
|
56
|
+
if (isRecord(data) && typeof data.served === "boolean") {
|
|
57
|
+
set(data);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Keep what is known; the next refresh tries again.
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
refreshing = null;
|
|
65
|
+
}
|
|
66
|
+
return meta;
|
|
67
|
+
})();
|
|
68
|
+
return refreshing;
|
|
69
|
+
};
|
|
70
|
+
if (meta === null) {
|
|
71
|
+
void refresh();
|
|
72
|
+
}
|
|
73
|
+
const configOf = (kind) => {
|
|
74
|
+
if (meta === null || kind === null) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (meta.served && meta.contentSource !== null) {
|
|
78
|
+
const served = options.displays.served(meta.contentSource);
|
|
79
|
+
if (served !== null) {
|
|
80
|
+
return served[kind];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
return options.displays.opaque(kind);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// A url kind has no opaque config: not shown while frames aren't served.
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const issue = async (instance, signal) => {
|
|
92
|
+
const headers = headersNow();
|
|
93
|
+
headers.set("content-type", "application/json");
|
|
94
|
+
headers.set("accept", "application/json");
|
|
95
|
+
const answer = await ask(url("/frames"), {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers,
|
|
98
|
+
credentials,
|
|
99
|
+
redirect: "error",
|
|
100
|
+
body: JSON.stringify({ instance }),
|
|
101
|
+
...(signal === undefined ? {} : { signal }),
|
|
102
|
+
});
|
|
103
|
+
const data = await answer.json().catch(() => null);
|
|
104
|
+
if (!answer.ok) {
|
|
105
|
+
const error = isRecord(data) && isRecord(data.error) ? data.error : {};
|
|
106
|
+
const code = typeof error.code === "string" ? error.code : "failed";
|
|
107
|
+
if (code === "not-served") {
|
|
108
|
+
void refresh();
|
|
109
|
+
}
|
|
110
|
+
throw new LocateError(typeof error.message === "string"
|
|
111
|
+
? error.message
|
|
112
|
+
: `The app's frame endpoint answered ${answer.status}`, answer.status, code);
|
|
113
|
+
}
|
|
114
|
+
if (!isRecord(data) || typeof data.url !== "string" || typeof data.origin !== "string") {
|
|
115
|
+
throw new LocateError("The app's frame endpoint gave no location", answer.status, "bad-location");
|
|
116
|
+
}
|
|
117
|
+
links.set(keyOf(instance), typeof data.link === "string" ? data.link : null);
|
|
118
|
+
return {
|
|
119
|
+
url: data.url,
|
|
120
|
+
origin: data.origin,
|
|
121
|
+
...(typeof data.reports === "boolean" ? { reports: data.reports } : {}),
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
let lastHandoff = 0;
|
|
125
|
+
const handoff = async () => {
|
|
126
|
+
lastHandoff = Date.now();
|
|
127
|
+
try {
|
|
128
|
+
const answer = await ask(url("/handoff"), {
|
|
129
|
+
method: "POST",
|
|
130
|
+
headers: headersNow(),
|
|
131
|
+
credentials,
|
|
132
|
+
cache: "no-store",
|
|
133
|
+
});
|
|
134
|
+
return answer.ok;
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
// Kept fresh while the page is used: on load, focus and visibility, at most every 30 s.
|
|
141
|
+
const fresh = () => {
|
|
142
|
+
if (Date.now() - lastHandoff > 30_000 &&
|
|
143
|
+
(typeof document === "undefined" || document.visibilityState !== "hidden")) {
|
|
144
|
+
void handoff();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const watching = options.handoff === true && typeof window !== "undefined";
|
|
148
|
+
if (watching) {
|
|
149
|
+
void handoff();
|
|
150
|
+
window.addEventListener("focus", fresh);
|
|
151
|
+
document.addEventListener("visibilitychange", fresh);
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
displays: options.displays,
|
|
155
|
+
meta: () => meta,
|
|
156
|
+
subscribe(listener) {
|
|
157
|
+
listeners.add(listener);
|
|
158
|
+
return () => listeners.delete(listener);
|
|
159
|
+
},
|
|
160
|
+
refresh,
|
|
161
|
+
config: (kind) => configOf(kind),
|
|
162
|
+
configFor: (instance) => configOf(options.displays.kindOf(instance)),
|
|
163
|
+
locate: issue,
|
|
164
|
+
async link(instance) {
|
|
165
|
+
const key = keyOf(instance);
|
|
166
|
+
if (!links.has(key)) {
|
|
167
|
+
try {
|
|
168
|
+
await issue(instance);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return links.get(key) ?? null;
|
|
175
|
+
},
|
|
176
|
+
knownLink: (instance) => links.get(keyOf(instance)) ?? null,
|
|
177
|
+
openInTab(instance) {
|
|
178
|
+
// Opened inside the click (a popup blocker lets it), then sent to the address.
|
|
179
|
+
const tab = typeof window === "undefined" ? null : window.open("about:blank", "_blank");
|
|
180
|
+
if (tab === null) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
tab.opener = null;
|
|
184
|
+
void (async () => {
|
|
185
|
+
const link = await this.link(instance);
|
|
186
|
+
if (link === null) {
|
|
187
|
+
tab.close();
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
tab.location.href = link;
|
|
191
|
+
}
|
|
192
|
+
})();
|
|
193
|
+
},
|
|
194
|
+
handoff,
|
|
195
|
+
entryTarget: (next) => entryTarget(next, url("/authorize")),
|
|
196
|
+
close() {
|
|
197
|
+
if (watching) {
|
|
198
|
+
window.removeEventListener("focus", fresh);
|
|
199
|
+
document.removeEventListener("visibilitychange", fresh);
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The fallback for a frame's outcome, worded as Porthole's own pages word it; null when it loaded.
|
|
206
|
+
* `url`: a url-mode app, where a timeout means it couldn't run in a frame.
|
|
207
|
+
*/
|
|
208
|
+
export function frameFallback(outcome, options = {}) {
|
|
209
|
+
if (outcome.state === "loaded") {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
if (outcome.state === "timeout") {
|
|
213
|
+
return options.url === true
|
|
214
|
+
? {
|
|
215
|
+
kind: "needs-tab",
|
|
216
|
+
title: "Open this in a new tab",
|
|
217
|
+
message: "It didn't load inside the page.",
|
|
218
|
+
}
|
|
219
|
+
: { kind: "timeout", title: "This is taking too long", message: "It didn't finish loading." };
|
|
220
|
+
}
|
|
221
|
+
switch (outcome.reason) {
|
|
222
|
+
case "cookies":
|
|
223
|
+
return {
|
|
224
|
+
kind: "needs-tab",
|
|
225
|
+
title: "Open this in a new tab",
|
|
226
|
+
message: "This browser keeps no sign-in for it inside a frame.",
|
|
227
|
+
};
|
|
228
|
+
case "sign-in":
|
|
229
|
+
return {
|
|
230
|
+
kind: "needs-tab",
|
|
231
|
+
title: "Open this in a new tab to sign in",
|
|
232
|
+
message: "Signing in doesn't work inside a frame.",
|
|
233
|
+
};
|
|
234
|
+
case "framing":
|
|
235
|
+
return {
|
|
236
|
+
kind: "needs-tab",
|
|
237
|
+
title: "Open this in a new tab",
|
|
238
|
+
message: "It can't be shown inside this page.",
|
|
239
|
+
};
|
|
240
|
+
case "denied":
|
|
241
|
+
return {
|
|
242
|
+
kind: "denied",
|
|
243
|
+
title: "You can't open this anymore",
|
|
244
|
+
message: "You no longer have access to it.",
|
|
245
|
+
};
|
|
246
|
+
case "session":
|
|
247
|
+
return { kind: "expired", title: "This has expired", message: "Open it again." };
|
|
248
|
+
case "not-found":
|
|
249
|
+
return { kind: "failed", title: "Not found", message: "There is nothing here." };
|
|
250
|
+
case "upstream":
|
|
251
|
+
return {
|
|
252
|
+
kind: "failed",
|
|
253
|
+
title: "The app didn't answer",
|
|
254
|
+
message: "Its server couldn't be reached.",
|
|
255
|
+
};
|
|
256
|
+
default:
|
|
257
|
+
return { kind: "failed", title: "This couldn't be shown", message: "Something went wrong." };
|
|
258
|
+
}
|
|
259
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,10 @@ export type { AdapterConnection, AdapterEvent, AdapterSession, ProtocolAdapter,
|
|
|
24
24
|
export { COPY_TEXT_METHOD, MCP_APPS_METHODS, MCP_APPS_PROTOCOL_VERSION, MCP_APPS_STYLE_VARIABLES, PORTHOLE_STYLES, type HostContext, } from "./protocol/mcp-apps.ts";
|
|
25
25
|
export type { CallContext, CallOutcome, CapabilityHandler, CapabilityHandlers, Confirm, ConfirmAnswer, ConfirmRequest, Proceed, HostFunctionBridge, Refusal, } from "./bridge/types.ts";
|
|
26
26
|
export { createHost } from "./host/create-host.ts";
|
|
27
|
+
export { entryTarget, frameFallback, portholeClient, type ContentClientMeta, type FrameFallback, type PortholeClient, type PortholeClientOptions, } from "./host/client.ts";
|
|
28
|
+
export { defineDisplays, type DisplayKind, type Displays } from "./displays/displays.ts";
|
|
29
|
+
export { fragmentOf, lintContent, type Finding, type LintRule } from "./check/lint.ts";
|
|
30
|
+
export { admitContent, answerGuide, checkTool, guideChanges, guideTool, guideTopics, showTool, type AdmitResult, type JsonSchema, type ToolDefinition, } from "./check/tools.ts";
|
|
27
31
|
export { writeClipboard } from "./host/clipboard.ts";
|
|
28
32
|
export { httpLocator, LocateError, servedOrOpaque, type HttpLocatorOptions, type ServedMeta, } from "./host/locator.ts";
|
|
29
33
|
export type { AnnotationControls, DisplayControls, DisplayEvents, FrameBlock, FrameOutcome, DisplayHandle, HostOptions, MountOptions, PortholeHost, } from "./host/types.ts";
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,12 @@ export { GUIDE_EXAMPLES } from "./guide/examples.js";
|
|
|
21
21
|
export { CHART_PALETTE, DEFAULT_THEME, DEFAULT_THEME_EVENT, DEFAULT_THEME_VARIABLES, resolveTheme, themeDefault, } from "./theme/variables.js";
|
|
22
22
|
export { COPY_TEXT_METHOD, MCP_APPS_METHODS, MCP_APPS_PROTOCOL_VERSION, MCP_APPS_STYLE_VARIABLES, PORTHOLE_STYLES, } from "./protocol/mcp-apps.js";
|
|
23
23
|
export { createHost } from "./host/create-host.js";
|
|
24
|
+
// The client half of the host kit, and the display registry (P072).
|
|
25
|
+
export { entryTarget, frameFallback, portholeClient, } from "./host/client.js";
|
|
26
|
+
export { defineDisplays } from "./displays/displays.js";
|
|
27
|
+
// The agent's side: what content can be told without running it, and its tools (P073).
|
|
28
|
+
export { fragmentOf, lintContent } from "./check/lint.js";
|
|
29
|
+
export { admitContent, answerGuide, checkTool, guideChanges, guideTool, guideTopics, showTool, } from "./check/tools.js";
|
|
24
30
|
export { writeClipboard } from "./host/clipboard.js";
|
|
25
31
|
export { httpLocator, LocateError, servedOrOpaque, } from "./host/locator.js";
|
|
26
32
|
// Annotations (P053): anchors, what a frame reports, and the host's checks on both.
|
package/dist/node/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* server.on("upgrade", porthole.upgrade);
|
|
10
10
|
* server.listen(8080, () => content.start());
|
|
11
11
|
*/
|
|
12
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
12
|
+
import type { IncomingMessage, Server, ServerResponse } from "node:http";
|
|
13
13
|
import type { Duplex } from "node:stream";
|
|
14
14
|
import type { ContentHost, HealthProbe } from "../server/host.ts";
|
|
15
15
|
import type { Access } from "../server/types.ts";
|
|
@@ -46,4 +46,25 @@ export declare function nodeHost<V>(content: ContentHost<V>, app: NodeHostOption
|
|
|
46
46
|
* of the answer read.
|
|
47
47
|
*/
|
|
48
48
|
export declare function nodeProbe(): HealthProbe;
|
|
49
|
+
export interface NodeServerOptions {
|
|
50
|
+
/** The content host. */
|
|
51
|
+
readonly content: ContentHost<unknown>;
|
|
52
|
+
/**
|
|
53
|
+
* The app, fetch-style (Hono's `app.fetch`, or the app's own handler): every request that
|
|
54
|
+
* isn't the content domain's. Or `request`, a Node listener (Express).
|
|
55
|
+
*/
|
|
56
|
+
readonly app?: (request: Request) => Response | Promise<Response>;
|
|
57
|
+
readonly request?: RequestListener;
|
|
58
|
+
readonly upgrade?: UpgradeListener;
|
|
59
|
+
readonly appSocket?: NodeHostOptions["appSocket"];
|
|
60
|
+
/** Whether the app's own requests came over https (behind a TLS edge): their URL's scheme. */
|
|
61
|
+
readonly scheme?: "https" | "http";
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The whole Node server (P072): the content domain to Porthole, everything else to the app, its
|
|
65
|
+
* WebSockets included; the health gate started when it listens and stopped when it closes.
|
|
66
|
+
*
|
|
67
|
+
* nodeServer({ content, app: (request) => app.fetch(request) }).listen(8790);
|
|
68
|
+
*/
|
|
69
|
+
export declare function nodeServer(options: NodeServerOptions): Server;
|
|
49
70
|
export {};
|
package/dist/node/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { request as httpRequest } from "node:http";
|
|
1
|
+
import { createServer as createHttpServer, request as httpRequest } from "node:http";
|
|
2
2
|
import { request as httpsRequest } from "node:https";
|
|
3
3
|
import { Readable } from "node:stream";
|
|
4
|
+
import { withoutPortholeCookies } from "../server/proxy.js";
|
|
4
5
|
/**
|
|
5
6
|
* The Node request as a fetch `Request`: an absolute-form target kept as it came (P056). `signal`
|
|
6
7
|
* (`nodeHost` gives one, aborted when the client goes away) lets an upstream stop its work.
|
|
@@ -172,6 +173,14 @@ export function nodeHost(content, app) {
|
|
|
172
173
|
refuseSocket(socket);
|
|
173
174
|
}
|
|
174
175
|
else {
|
|
176
|
+
// The app's socket never sees Porthole's own cookies, as its HTTP requests don't (P072).
|
|
177
|
+
const cookie = withoutPortholeCookies(request.headers.cookie);
|
|
178
|
+
if (cookie === null) {
|
|
179
|
+
delete request.headers.cookie;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
request.headers.cookie = cookie;
|
|
183
|
+
}
|
|
175
184
|
appSocket(access, request, socket, head);
|
|
176
185
|
}
|
|
177
186
|
}, () => refuseSocket(socket));
|
|
@@ -218,3 +227,39 @@ export function nodeProbe() {
|
|
|
218
227
|
asked.end();
|
|
219
228
|
});
|
|
220
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* The whole Node server (P072): the content domain to Porthole, everything else to the app, its
|
|
232
|
+
* WebSockets included; the health gate started when it listens and stopped when it closes.
|
|
233
|
+
*
|
|
234
|
+
* nodeServer({ content, app: (request) => app.fetch(request) }).listen(8790);
|
|
235
|
+
*/
|
|
236
|
+
export function nodeServer(options) {
|
|
237
|
+
const scheme = options.scheme ?? "http";
|
|
238
|
+
const fromFetch = options.app;
|
|
239
|
+
const request = options.request ??
|
|
240
|
+
(fromFetch === undefined
|
|
241
|
+
? undefined
|
|
242
|
+
: (incoming, response) => {
|
|
243
|
+
Promise.resolve(fromFetch(toRequest(incoming, scheme))).then((answer) => sendResponse(response, answer), () => {
|
|
244
|
+
if (response.headersSent) {
|
|
245
|
+
response.destroy();
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
response.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
|
|
249
|
+
response.end("Something went wrong.");
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
if (request === undefined) {
|
|
253
|
+
throw new TypeError("nodeServer needs the app: app (fetch-style) or request (a Node listener)");
|
|
254
|
+
}
|
|
255
|
+
const porthole = nodeHost(options.content, {
|
|
256
|
+
request,
|
|
257
|
+
...(options.upgrade === undefined ? {} : { upgrade: options.upgrade }),
|
|
258
|
+
...(options.appSocket === undefined ? {} : { appSocket: options.appSocket }),
|
|
259
|
+
});
|
|
260
|
+
const server = createHttpServer(porthole.request);
|
|
261
|
+
server.on("upgrade", porthole.upgrade);
|
|
262
|
+
server.on("listening", () => options.content.start());
|
|
263
|
+
server.on("close", () => options.content.close());
|
|
264
|
+
return server;
|
|
265
|
+
}
|
package/dist/react/frame.d.ts
CHANGED
|
@@ -1,13 +1,78 @@
|
|
|
1
1
|
import { type CSSProperties, type ReactElement, type ReactNode } from "react";
|
|
2
2
|
import type { AnnotateMode, AnnotationMark, AnnotationReport } from "../annotate/types.ts";
|
|
3
|
-
import type { CapabilityHandlers, Confirm, Refusal } from "../bridge/types.ts";
|
|
3
|
+
import type { CapabilityHandler, CapabilityHandlers, Confirm, ConfirmRequest, Refusal } from "../bridge/types.ts";
|
|
4
4
|
import type { CapabilityName } from "../config/capabilities.ts";
|
|
5
5
|
import type { ColorScheme, PortholeConfig } from "../config/schema.ts";
|
|
6
6
|
import type { FrameOutcome, HostErrorKind } from "../host/types.ts";
|
|
7
7
|
import type { InstanceLocator, InstanceRef } from "../isolation/types.ts";
|
|
8
|
+
import { type LiveFrames } from "./live.ts";
|
|
9
|
+
import { type CopyNotice } from "./toast.tsx";
|
|
10
|
+
import type { PortholeClient } from "../host/client.ts";
|
|
8
11
|
/** The height a display holds before it has reported its own. */
|
|
9
12
|
export declare const PLACEHOLDER_HEIGHT_PX = 96;
|
|
13
|
+
interface PageState {
|
|
14
|
+
readonly live: LiveFrames;
|
|
15
|
+
/** The host kit's client (P072): the locator, configs by kind, links. */
|
|
16
|
+
readonly client?: PortholeClient;
|
|
17
|
+
/** Defaults every frame below takes unless it says its own (P072). */
|
|
18
|
+
readonly scheme?: ColorScheme;
|
|
19
|
+
readonly theme?: (scheme: ColorScheme) => Readonly<Record<string, string | null | undefined>>;
|
|
20
|
+
readonly themeKey?: string | number;
|
|
21
|
+
/** The provider's handlers as of now (its own over the default `openLink`). */
|
|
22
|
+
readonly handlers?: () => CapabilityHandlers;
|
|
23
|
+
/** Heights displays reported, by display id: a frame mounting again starts at its last. */
|
|
24
|
+
readonly heights: Map<string, number>;
|
|
25
|
+
readonly confirm?: Confirm;
|
|
26
|
+
readonly hostInfo?: {
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly version: string;
|
|
29
|
+
};
|
|
30
|
+
readonly nonce?: string;
|
|
31
|
+
/** The provider's locator, read when a frame locates (so an inline one restarts nothing). */
|
|
32
|
+
readonly locate?: InstanceLocator["locate"];
|
|
33
|
+
/** The default `copyText`: copies on the host's page and shows the toast (P052). */
|
|
34
|
+
readonly copyText?: CapabilityHandler<"copyText">;
|
|
35
|
+
}
|
|
36
|
+
/** The provider's page state, for the binding's other components (`PortholeView`). */
|
|
37
|
+
export declare const PortholeContext: import("react").Context<PageState>;
|
|
10
38
|
export interface PortholeProviderProps {
|
|
39
|
+
/**
|
|
40
|
+
* The host kit's client (`portholeClient`, P072): it locates served frames (unless `locator` is
|
|
41
|
+
* given), and `PortholeView` takes each kind's config and link from it.
|
|
42
|
+
*/
|
|
43
|
+
readonly client?: PortholeClient;
|
|
44
|
+
/** The color scheme every frame below shows in, unless it says its own. */
|
|
45
|
+
readonly scheme?: ColorScheme;
|
|
46
|
+
/**
|
|
47
|
+
* The host's live values for the theme variables, for every frame below (`cssVariableTheme`
|
|
48
|
+
* reads them from the page's own CSS variables). Change `themeKey` when they change.
|
|
49
|
+
*/
|
|
50
|
+
readonly theme?: (scheme: ColorScheme) => Readonly<Record<string, string | null | undefined>>;
|
|
51
|
+
readonly themeKey?: string | number;
|
|
52
|
+
/** Handlers every frame below has, under its own (a frame's own win). */
|
|
53
|
+
readonly handlers?: CapabilityHandlers;
|
|
54
|
+
/**
|
|
55
|
+
* How `openLink` opens a link where a frame has no handler of its own: a desktop app's
|
|
56
|
+
* external browser. Default: a new tab, `noopener`.
|
|
57
|
+
*/
|
|
58
|
+
readonly openLink?: (url: string) => void;
|
|
59
|
+
/**
|
|
60
|
+
* Shows what a copy button put on the clipboard, in the app's own toast (the words stay
|
|
61
|
+
* Porthole's: `notice`); `copy` copies it again in a click where the first write failed.
|
|
62
|
+
* Omitted: Porthole's `CopyToast`.
|
|
63
|
+
*/
|
|
64
|
+
readonly notify?: (notice: CopyNotice, actions: {
|
|
65
|
+
readonly copy: () => Promise<boolean>;
|
|
66
|
+
}) => void;
|
|
67
|
+
/**
|
|
68
|
+
* Draws the confirmation in the app's own dialog, for the request at the head of the queue:
|
|
69
|
+
* Porthole keeps the queue (one question at a time), runs `accept` inside the click, and counts
|
|
70
|
+
* declines. Omitted: Porthole's `ConfirmDialog`. (`confirm` replaces the whole mechanism.)
|
|
71
|
+
*/
|
|
72
|
+
readonly renderConfirm?: (request: ConfirmRequest, answer: {
|
|
73
|
+
readonly accept: () => void;
|
|
74
|
+
readonly decline: () => void;
|
|
75
|
+
}) => ReactNode;
|
|
11
76
|
/**
|
|
12
77
|
* Who confirms gated calls for every frame below. Omitted: the default dialog (`ConfirmDialog`).
|
|
13
78
|
* Pass your own function to replace it.
|
|
@@ -26,7 +91,8 @@ export interface PortholeProviderProps {
|
|
|
26
91
|
readonly toastClassName?: string;
|
|
27
92
|
/**
|
|
28
93
|
* The page's CSP nonce, where its `script-src` is nonce-based: opaque frames inherit the page's
|
|
29
|
-
* policy, so their shell carries it (P054).
|
|
94
|
+
* policy, so their shell carries it (P054). `"auto"`: read from the page (a `<meta
|
|
95
|
+
* name="csp-nonce">`, else its first script's nonce).
|
|
30
96
|
*/
|
|
31
97
|
readonly nonce?: string;
|
|
32
98
|
readonly children?: ReactNode;
|
|
@@ -54,7 +120,8 @@ export interface PortholeFrameProps {
|
|
|
54
120
|
readonly document?: string;
|
|
55
121
|
/** Domain and external isolation: the instance to show. */
|
|
56
122
|
readonly instance?: InstanceRef;
|
|
57
|
-
|
|
123
|
+
/** The color scheme it shows in. Default: the provider's `scheme`, else light. */
|
|
124
|
+
readonly scheme?: ColorScheme;
|
|
58
125
|
/** The host's live values for the config's theme variables (from its design tokens). */
|
|
59
126
|
readonly theme?: (scheme: ColorScheme) => Readonly<Record<string, string | null | undefined>>;
|
|
60
127
|
/** Handlers for the declared capabilities (the latest ones are called). */
|
|
@@ -140,3 +207,4 @@ export interface PortholeFrameProps {
|
|
|
140
207
|
readonly style?: CSSProperties;
|
|
141
208
|
}
|
|
142
209
|
export declare function PortholeFrame(props: PortholeFrameProps): ReactElement;
|
|
210
|
+
export {};
|