@botiverse/porthole 0.2.3 → 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 +356 -0
- 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,364 @@
|
|
|
1
|
+
import type { PortholeConfig } from "../config/schema.ts";
|
|
2
|
+
import type { Displays } from "../displays/displays.ts";
|
|
3
|
+
import type { FrameLocation, InstanceLocator, InstanceRef } from "../isolation/types.ts";
|
|
4
|
+
import { LocateError } from "./locator.ts";
|
|
5
|
+
import type { FrameOutcome } from "./types.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The client half of the host kit (P072): what the app's page needs from its server's Porthole
|
|
9
|
+
* routes (`ContentHost.routes`, under `/porthole`): whether frames are served and so which config
|
|
10
|
+
* each kind shows under, frame locations, clean addresses, and (for an app whose page holds a
|
|
11
|
+
* token rather than a cookie) the viewer handoff.
|
|
12
|
+
*
|
|
13
|
+
* const porthole = portholeClient({ displays }); // base "/porthole"
|
|
14
|
+
* const config = porthole.config("page"); // null until meta is known
|
|
15
|
+
* createHost(config, { locator: porthole }).mount(box, { …, instance });
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** What `GET <prefix>/meta` answers (`ContentHost.clientMeta`). */
|
|
19
|
+
export interface ContentClientMeta {
|
|
20
|
+
readonly served: boolean;
|
|
21
|
+
readonly contentSource: string | null;
|
|
22
|
+
readonly frameSources: readonly string[];
|
|
23
|
+
readonly health: {
|
|
24
|
+
readonly state: "off" | "checking" | "healthy" | "unhealthy";
|
|
25
|
+
readonly reason?: string;
|
|
26
|
+
} | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface PortholeClientOptions<N extends string> {
|
|
30
|
+
readonly displays: Displays<N>;
|
|
31
|
+
/** Where the server's Porthole routes are: a path (`/porthole`) or a URL (a desktop app's). */
|
|
32
|
+
readonly base?: string;
|
|
33
|
+
/** Headers for each request (an `authorization`), or a function giving them now. */
|
|
34
|
+
readonly headers?: HeadersInit | (() => HeadersInit);
|
|
35
|
+
/** Default `same-origin` (the app's own cookies); `include` for a base on another origin. */
|
|
36
|
+
readonly credentials?: RequestCredentials;
|
|
37
|
+
/** Meta the server put in the page, so frames needn't wait for a request. */
|
|
38
|
+
readonly meta?: ContentClientMeta;
|
|
39
|
+
/**
|
|
40
|
+
* Keep the viewer handoff fresh (`entry.handoff`, P072): an app whose page authenticates with
|
|
41
|
+
* a token. Done now, when the page regains focus, and when it becomes visible.
|
|
42
|
+
*/
|
|
43
|
+
readonly handoff?: boolean;
|
|
44
|
+
readonly fetch?: typeof fetch;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface PortholeClient<N extends string = string> extends InstanceLocator {
|
|
48
|
+
readonly displays: Displays<N>;
|
|
49
|
+
/** The meta as last fetched, or null before it is known. */
|
|
50
|
+
meta(): ContentClientMeta | null;
|
|
51
|
+
/** Called when the meta changes (frames served or not). */
|
|
52
|
+
subscribe(listener: () => void): () => void;
|
|
53
|
+
/** Fetches the meta again. */
|
|
54
|
+
refresh(): Promise<ContentClientMeta | null>;
|
|
55
|
+
/**
|
|
56
|
+
* The config a kind shows under now: served while frames are served, else opaque (a url kind:
|
|
57
|
+
* null). Null until the meta is known, so a frame never starts opaque and then restarts served.
|
|
58
|
+
*/
|
|
59
|
+
config(kind: N): PortholeConfig | null;
|
|
60
|
+
/** The same, for an instance's kind. */
|
|
61
|
+
configFor(instance: InstanceRef): PortholeConfig | null;
|
|
62
|
+
/** An instance's clean address (`entry.pathOf` on the server), or null. */
|
|
63
|
+
link(instance: InstanceRef): Promise<string | null>;
|
|
64
|
+
/** The clean address if already known (from locating the instance), for rendering. */
|
|
65
|
+
knownLink(instance: InstanceRef): string | null;
|
|
66
|
+
/**
|
|
67
|
+
* Opens an instance's clean address in a new tab: call it in the click (the tab opens at once,
|
|
68
|
+
* then goes to the address).
|
|
69
|
+
*/
|
|
70
|
+
openInTab(instance: InstanceRef): void;
|
|
71
|
+
/** Hands the signed-in viewer to the authorize route (`entry.handoff`); true if it took. */
|
|
72
|
+
handoff(): Promise<boolean>;
|
|
73
|
+
/** `next` if it is this server's authorize route (after a sign-in), else null (`entryTarget`). */
|
|
74
|
+
entryTarget(next: unknown): string | null;
|
|
75
|
+
/** Stops refreshing the handoff. */
|
|
76
|
+
close(): void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
80
|
+
value !== null && typeof value === "object" && !Array.isArray(value);
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Whether a sign-in's `next` is the app's authorize route (P072): the open-redirect guard a
|
|
84
|
+
* single-page app runs before going on to it. Null for anything else.
|
|
85
|
+
*/
|
|
86
|
+
export function entryTarget(next: unknown, authorizeUrl: string): string | null {
|
|
87
|
+
if (typeof next !== "string" || next.length > 8_192) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
let target: URL;
|
|
91
|
+
let route: URL;
|
|
92
|
+
try {
|
|
93
|
+
route = new URL(authorizeUrl);
|
|
94
|
+
target = new URL(next, route);
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return target.origin === route.origin &&
|
|
99
|
+
target.pathname === route.pathname &&
|
|
100
|
+
target.hash === "" &&
|
|
101
|
+
target.searchParams.has("state")
|
|
102
|
+
? target.href
|
|
103
|
+
: null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function portholeClient<N extends string>(
|
|
107
|
+
options: PortholeClientOptions<N>,
|
|
108
|
+
): PortholeClient<N> {
|
|
109
|
+
const base = (options.base ?? "/porthole").replace(/\/+$/u, "");
|
|
110
|
+
const ask =
|
|
111
|
+
options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init));
|
|
112
|
+
const headersNow = (): Headers =>
|
|
113
|
+
new Headers(typeof options.headers === "function" ? options.headers() : options.headers);
|
|
114
|
+
const credentials = options.credentials ?? "same-origin";
|
|
115
|
+
const here = () => (typeof location === "undefined" ? "http://localhost/" : location.href);
|
|
116
|
+
const url = (path: string) => new URL(`${base}${path}`, here()).href;
|
|
117
|
+
|
|
118
|
+
let meta: ContentClientMeta | null = options.meta ?? null;
|
|
119
|
+
const listeners = new Set<() => void>();
|
|
120
|
+
const links = new Map<string, string | null>();
|
|
121
|
+
const keyOf = (instance: InstanceRef) => JSON.stringify(instance);
|
|
122
|
+
|
|
123
|
+
const set = (next: ContentClientMeta | null) => {
|
|
124
|
+
if (JSON.stringify(next) !== JSON.stringify(meta)) {
|
|
125
|
+
meta = next;
|
|
126
|
+
for (const listener of listeners) {
|
|
127
|
+
listener();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
let refreshing: Promise<ContentClientMeta | null> | null = null;
|
|
132
|
+
const refresh = (): Promise<ContentClientMeta | null> => {
|
|
133
|
+
refreshing ??= (async () => {
|
|
134
|
+
try {
|
|
135
|
+
const answer = await ask(url("/meta"), {
|
|
136
|
+
headers: headersNow(),
|
|
137
|
+
credentials,
|
|
138
|
+
cache: "no-store",
|
|
139
|
+
});
|
|
140
|
+
const data: unknown = answer.ok ? await answer.json() : null;
|
|
141
|
+
if (isRecord(data) && typeof data.served === "boolean") {
|
|
142
|
+
set(data as unknown as ContentClientMeta);
|
|
143
|
+
}
|
|
144
|
+
} catch {
|
|
145
|
+
// Keep what is known; the next refresh tries again.
|
|
146
|
+
} finally {
|
|
147
|
+
refreshing = null;
|
|
148
|
+
}
|
|
149
|
+
return meta;
|
|
150
|
+
})();
|
|
151
|
+
return refreshing;
|
|
152
|
+
};
|
|
153
|
+
if (meta === null) {
|
|
154
|
+
void refresh();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const configOf = (kind: N | null): PortholeConfig | null => {
|
|
158
|
+
if (meta === null || kind === null) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
if (meta.served && meta.contentSource !== null) {
|
|
162
|
+
const served = options.displays.served(meta.contentSource);
|
|
163
|
+
if (served !== null) {
|
|
164
|
+
return served[kind];
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
return options.displays.opaque(kind);
|
|
169
|
+
} catch {
|
|
170
|
+
// A url kind has no opaque config: not shown while frames aren't served.
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const issue = async (instance: InstanceRef, signal?: AbortSignal): Promise<FrameLocation> => {
|
|
176
|
+
const headers = headersNow();
|
|
177
|
+
headers.set("content-type", "application/json");
|
|
178
|
+
headers.set("accept", "application/json");
|
|
179
|
+
const answer = await ask(url("/frames"), {
|
|
180
|
+
method: "POST",
|
|
181
|
+
headers,
|
|
182
|
+
credentials,
|
|
183
|
+
redirect: "error",
|
|
184
|
+
body: JSON.stringify({ instance }),
|
|
185
|
+
...(signal === undefined ? {} : { signal }),
|
|
186
|
+
});
|
|
187
|
+
const data: unknown = await answer.json().catch(() => null);
|
|
188
|
+
if (!answer.ok) {
|
|
189
|
+
const error = isRecord(data) && isRecord(data.error) ? data.error : {};
|
|
190
|
+
const code = typeof error.code === "string" ? error.code : "failed";
|
|
191
|
+
if (code === "not-served") {
|
|
192
|
+
void refresh();
|
|
193
|
+
}
|
|
194
|
+
throw new LocateError(
|
|
195
|
+
typeof error.message === "string"
|
|
196
|
+
? error.message
|
|
197
|
+
: `The app's frame endpoint answered ${answer.status}`,
|
|
198
|
+
answer.status,
|
|
199
|
+
code,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (!isRecord(data) || typeof data.url !== "string" || typeof data.origin !== "string") {
|
|
203
|
+
throw new LocateError(
|
|
204
|
+
"The app's frame endpoint gave no location",
|
|
205
|
+
answer.status,
|
|
206
|
+
"bad-location",
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
links.set(keyOf(instance), typeof data.link === "string" ? data.link : null);
|
|
210
|
+
return {
|
|
211
|
+
url: data.url,
|
|
212
|
+
origin: data.origin,
|
|
213
|
+
...(typeof data.reports === "boolean" ? { reports: data.reports } : {}),
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
let lastHandoff = 0;
|
|
218
|
+
const handoff = async (): Promise<boolean> => {
|
|
219
|
+
lastHandoff = Date.now();
|
|
220
|
+
try {
|
|
221
|
+
const answer = await ask(url("/handoff"), {
|
|
222
|
+
method: "POST",
|
|
223
|
+
headers: headersNow(),
|
|
224
|
+
credentials,
|
|
225
|
+
cache: "no-store",
|
|
226
|
+
});
|
|
227
|
+
return answer.ok;
|
|
228
|
+
} catch {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
// Kept fresh while the page is used: on load, focus and visibility, at most every 30 s.
|
|
233
|
+
const fresh = () => {
|
|
234
|
+
if (
|
|
235
|
+
Date.now() - lastHandoff > 30_000 &&
|
|
236
|
+
(typeof document === "undefined" || document.visibilityState !== "hidden")
|
|
237
|
+
) {
|
|
238
|
+
void handoff();
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const watching = options.handoff === true && typeof window !== "undefined";
|
|
242
|
+
if (watching) {
|
|
243
|
+
void handoff();
|
|
244
|
+
window.addEventListener("focus", fresh);
|
|
245
|
+
document.addEventListener("visibilitychange", fresh);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
displays: options.displays,
|
|
250
|
+
meta: () => meta,
|
|
251
|
+
subscribe(listener) {
|
|
252
|
+
listeners.add(listener);
|
|
253
|
+
return () => listeners.delete(listener);
|
|
254
|
+
},
|
|
255
|
+
refresh,
|
|
256
|
+
config: (kind) => configOf(kind),
|
|
257
|
+
configFor: (instance) => configOf(options.displays.kindOf(instance)),
|
|
258
|
+
locate: issue,
|
|
259
|
+
async link(instance) {
|
|
260
|
+
const key = keyOf(instance);
|
|
261
|
+
if (!links.has(key)) {
|
|
262
|
+
try {
|
|
263
|
+
await issue(instance);
|
|
264
|
+
} catch {
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return links.get(key) ?? null;
|
|
269
|
+
},
|
|
270
|
+
knownLink: (instance) => links.get(keyOf(instance)) ?? null,
|
|
271
|
+
openInTab(instance) {
|
|
272
|
+
// Opened inside the click (a popup blocker lets it), then sent to the address.
|
|
273
|
+
const tab = typeof window === "undefined" ? null : window.open("about:blank", "_blank");
|
|
274
|
+
if (tab === null) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
tab.opener = null;
|
|
278
|
+
void (async () => {
|
|
279
|
+
const link = await this.link(instance);
|
|
280
|
+
if (link === null) {
|
|
281
|
+
tab.close();
|
|
282
|
+
} else {
|
|
283
|
+
tab.location.href = link;
|
|
284
|
+
}
|
|
285
|
+
})();
|
|
286
|
+
},
|
|
287
|
+
handoff,
|
|
288
|
+
entryTarget: (next) => entryTarget(next, url("/authorize")),
|
|
289
|
+
close() {
|
|
290
|
+
if (watching) {
|
|
291
|
+
window.removeEventListener("focus", fresh);
|
|
292
|
+
document.removeEventListener("visibilitychange", fresh);
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** What a frame that showed nothing should offer the person, by its outcome (P072). */
|
|
299
|
+
export interface FrameFallback {
|
|
300
|
+
/** `needs-tab`: open it in a new tab (the browser keeps no cookie in a frame, sign-in, framing). */
|
|
301
|
+
readonly kind: "needs-tab" | "denied" | "expired" | "failed" | "timeout";
|
|
302
|
+
readonly title: string;
|
|
303
|
+
readonly message: string;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The fallback for a frame's outcome, worded as Porthole's own pages word it; null when it loaded.
|
|
308
|
+
* `url`: a url-mode app, where a timeout means it couldn't run in a frame.
|
|
309
|
+
*/
|
|
310
|
+
export function frameFallback(
|
|
311
|
+
outcome: FrameOutcome,
|
|
312
|
+
options: { readonly url?: boolean } = {},
|
|
313
|
+
): FrameFallback | null {
|
|
314
|
+
if (outcome.state === "loaded") {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
if (outcome.state === "timeout") {
|
|
318
|
+
return options.url === true
|
|
319
|
+
? {
|
|
320
|
+
kind: "needs-tab",
|
|
321
|
+
title: "Open this in a new tab",
|
|
322
|
+
message: "It didn't load inside the page.",
|
|
323
|
+
}
|
|
324
|
+
: { kind: "timeout", title: "This is taking too long", message: "It didn't finish loading." };
|
|
325
|
+
}
|
|
326
|
+
switch (outcome.reason) {
|
|
327
|
+
case "cookies":
|
|
328
|
+
return {
|
|
329
|
+
kind: "needs-tab",
|
|
330
|
+
title: "Open this in a new tab",
|
|
331
|
+
message: "This browser keeps no sign-in for it inside a frame.",
|
|
332
|
+
};
|
|
333
|
+
case "sign-in":
|
|
334
|
+
return {
|
|
335
|
+
kind: "needs-tab",
|
|
336
|
+
title: "Open this in a new tab to sign in",
|
|
337
|
+
message: "Signing in doesn't work inside a frame.",
|
|
338
|
+
};
|
|
339
|
+
case "framing":
|
|
340
|
+
return {
|
|
341
|
+
kind: "needs-tab",
|
|
342
|
+
title: "Open this in a new tab",
|
|
343
|
+
message: "It can't be shown inside this page.",
|
|
344
|
+
};
|
|
345
|
+
case "denied":
|
|
346
|
+
return {
|
|
347
|
+
kind: "denied",
|
|
348
|
+
title: "You can't open this anymore",
|
|
349
|
+
message: "You no longer have access to it.",
|
|
350
|
+
};
|
|
351
|
+
case "session":
|
|
352
|
+
return { kind: "expired", title: "This has expired", message: "Open it again." };
|
|
353
|
+
case "not-found":
|
|
354
|
+
return { kind: "failed", title: "Not found", message: "There is nothing here." };
|
|
355
|
+
case "upstream":
|
|
356
|
+
return {
|
|
357
|
+
kind: "failed",
|
|
358
|
+
title: "The app didn't answer",
|
|
359
|
+
message: "Its server couldn't be reached.",
|
|
360
|
+
};
|
|
361
|
+
default:
|
|
362
|
+
return { kind: "failed", title: "This couldn't be shown", message: "Something went wrong." };
|
|
363
|
+
}
|
|
364
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -130,6 +130,31 @@ export type {
|
|
|
130
130
|
Refusal,
|
|
131
131
|
} from "./bridge/types.ts";
|
|
132
132
|
export { createHost } from "./host/create-host.ts";
|
|
133
|
+
// The client half of the host kit, and the display registry (P072).
|
|
134
|
+
export {
|
|
135
|
+
entryTarget,
|
|
136
|
+
frameFallback,
|
|
137
|
+
portholeClient,
|
|
138
|
+
type ContentClientMeta,
|
|
139
|
+
type FrameFallback,
|
|
140
|
+
type PortholeClient,
|
|
141
|
+
type PortholeClientOptions,
|
|
142
|
+
} from "./host/client.ts";
|
|
143
|
+
export { defineDisplays, type DisplayKind, type Displays } from "./displays/displays.ts";
|
|
144
|
+
// The agent's side: what content can be told without running it, and its tools (P073).
|
|
145
|
+
export { fragmentOf, lintContent, type Finding, type LintRule } from "./check/lint.ts";
|
|
146
|
+
export {
|
|
147
|
+
admitContent,
|
|
148
|
+
answerGuide,
|
|
149
|
+
checkTool,
|
|
150
|
+
guideChanges,
|
|
151
|
+
guideTool,
|
|
152
|
+
guideTopics,
|
|
153
|
+
showTool,
|
|
154
|
+
type AdmitResult,
|
|
155
|
+
type JsonSchema,
|
|
156
|
+
type ToolDefinition,
|
|
157
|
+
} from "./check/tools.ts";
|
|
133
158
|
export { writeClipboard } from "./host/clipboard.ts";
|
|
134
159
|
export {
|
|
135
160
|
httpLocator,
|
package/src/node/index.ts
CHANGED
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
* server.on("upgrade", porthole.upgrade);
|
|
10
10
|
* server.listen(8080, () => content.start());
|
|
11
11
|
*/
|
|
12
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
13
|
-
import { request as httpRequest } from "node:http";
|
|
12
|
+
import type { IncomingMessage, Server, ServerResponse } from "node:http";
|
|
13
|
+
import { createServer as createHttpServer, request as httpRequest } from "node:http";
|
|
14
14
|
import { request as httpsRequest } from "node:https";
|
|
15
15
|
import type { LookupFunction } from "node:net";
|
|
16
16
|
import type { Duplex } from "node:stream";
|
|
17
17
|
import { Readable } from "node:stream";
|
|
18
18
|
import type { ContentHost, HealthProbe } from "../server/host.ts";
|
|
19
|
+
import { withoutPortholeCookies } from "../server/proxy.ts";
|
|
19
20
|
import type { Access } from "../server/types.ts";
|
|
20
21
|
|
|
21
22
|
type RequestListener = (request: IncomingMessage, response: ServerResponse) => void;
|
|
@@ -219,6 +220,13 @@ export function nodeHost<V>(content: ContentHost<V>, app: NodeHostOptions): Node
|
|
|
219
220
|
if (access === null) {
|
|
220
221
|
refuseSocket(socket);
|
|
221
222
|
} else {
|
|
223
|
+
// The app's socket never sees Porthole's own cookies, as its HTTP requests don't (P072).
|
|
224
|
+
const cookie = withoutPortholeCookies(request.headers.cookie);
|
|
225
|
+
if (cookie === null) {
|
|
226
|
+
delete request.headers.cookie;
|
|
227
|
+
} else {
|
|
228
|
+
request.headers.cookie = cookie;
|
|
229
|
+
}
|
|
222
230
|
appSocket(access, request, socket, head);
|
|
223
231
|
}
|
|
224
232
|
},
|
|
@@ -282,3 +290,59 @@ export function nodeProbe(): HealthProbe {
|
|
|
282
290
|
asked.end();
|
|
283
291
|
});
|
|
284
292
|
}
|
|
293
|
+
|
|
294
|
+
export interface NodeServerOptions {
|
|
295
|
+
/** The content host. */
|
|
296
|
+
readonly content: ContentHost<unknown>;
|
|
297
|
+
/**
|
|
298
|
+
* The app, fetch-style (Hono's `app.fetch`, or the app's own handler): every request that
|
|
299
|
+
* isn't the content domain's. Or `request`, a Node listener (Express).
|
|
300
|
+
*/
|
|
301
|
+
readonly app?: (request: Request) => Response | Promise<Response>;
|
|
302
|
+
readonly request?: RequestListener;
|
|
303
|
+
readonly upgrade?: UpgradeListener;
|
|
304
|
+
readonly appSocket?: NodeHostOptions["appSocket"];
|
|
305
|
+
/** Whether the app's own requests came over https (behind a TLS edge): their URL's scheme. */
|
|
306
|
+
readonly scheme?: "https" | "http";
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* The whole Node server (P072): the content domain to Porthole, everything else to the app, its
|
|
311
|
+
* WebSockets included; the health gate started when it listens and stopped when it closes.
|
|
312
|
+
*
|
|
313
|
+
* nodeServer({ content, app: (request) => app.fetch(request) }).listen(8790);
|
|
314
|
+
*/
|
|
315
|
+
export function nodeServer(options: NodeServerOptions): Server {
|
|
316
|
+
const scheme = options.scheme ?? "http";
|
|
317
|
+
const fromFetch = options.app;
|
|
318
|
+
const request: RequestListener | undefined =
|
|
319
|
+
options.request ??
|
|
320
|
+
(fromFetch === undefined
|
|
321
|
+
? undefined
|
|
322
|
+
: (incoming, response) => {
|
|
323
|
+
Promise.resolve(fromFetch(toRequest(incoming, scheme))).then(
|
|
324
|
+
(answer) => sendResponse(response, answer),
|
|
325
|
+
() => {
|
|
326
|
+
if (response.headersSent) {
|
|
327
|
+
response.destroy();
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
response.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
|
|
331
|
+
response.end("Something went wrong.");
|
|
332
|
+
},
|
|
333
|
+
);
|
|
334
|
+
});
|
|
335
|
+
if (request === undefined) {
|
|
336
|
+
throw new TypeError("nodeServer needs the app: app (fetch-style) or request (a Node listener)");
|
|
337
|
+
}
|
|
338
|
+
const porthole = nodeHost(options.content, {
|
|
339
|
+
request,
|
|
340
|
+
...(options.upgrade === undefined ? {} : { upgrade: options.upgrade }),
|
|
341
|
+
...(options.appSocket === undefined ? {} : { appSocket: options.appSocket }),
|
|
342
|
+
});
|
|
343
|
+
const server = createHttpServer(porthole.request);
|
|
344
|
+
server.on("upgrade", porthole.upgrade);
|
|
345
|
+
server.on("listening", () => options.content.start());
|
|
346
|
+
server.on("close", () => options.content.close());
|
|
347
|
+
return server;
|
|
348
|
+
}
|