@botiverse/porthole 0.4.1 → 0.5.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.
- package/README.md +3 -1
- package/dist/bridge/bridge.d.ts +24 -2
- package/dist/bridge/bridge.js +102 -17
- package/dist/bridge/navigation.d.ts +11 -0
- package/dist/bridge/navigation.js +30 -0
- package/dist/bridge/refusal.d.ts +53 -0
- package/dist/bridge/refusal.js +121 -0
- package/dist/bridge/types.d.ts +51 -6
- package/dist/check/lint.js +2 -2
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +10 -0
- package/dist/compile/policy.d.ts +5 -0
- package/dist/compile/policy.js +18 -1
- package/dist/config/capabilities.d.ts +30 -2
- package/dist/config/capabilities.js +13 -0
- package/dist/config/schema.d.ts +7 -0
- package/dist/config/validate.js +14 -3
- package/dist/conformance/harness/page.js +1 -0
- package/dist/conformance/harness-source.js +1 -1
- package/dist/displays/displays.d.ts +1 -1
- package/dist/guide/guide.js +58 -8
- package/dist/host/create-host.js +18 -3
- package/dist/host/types.d.ts +25 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -1
- package/dist/libraries/files.generated.d.ts +1 -0
- package/dist/libraries/files.generated.js +8 -0
- package/dist/libraries/libraries.d.ts +36 -0
- package/dist/libraries/libraries.js +40 -0
- package/dist/mcp/adapter.js +49 -5
- package/dist/presets/presets.d.ts +15 -0
- package/dist/presets/presets.js +27 -2
- package/dist/protocol/live-data.d.ts +27 -0
- package/dist/protocol/live-data.js +98 -0
- package/dist/protocol/mcp-apps.d.ts +15 -0
- package/dist/protocol/mcp-apps.js +17 -0
- package/dist/protocol/shell-adapter.js +49 -4
- package/dist/react/dialog.js +2 -0
- package/dist/react/frame.d.ts +30 -1
- package/dist/react/frame.js +63 -1
- package/dist/server/libraries.d.ts +7 -0
- package/dist/server/libraries.js +27 -0
- package/dist/server/server.js +2 -1
- package/dist/shell/runtime.d.ts +5 -1
- package/dist/shell/runtime.js +196 -6
- package/docs/ADOPTING.md +89 -3
- package/docs/DECISIONS.md +221 -0
- package/docs/DESIGN.md +28 -10
- package/package.json +3 -1
- package/src/bridge/bridge.ts +136 -16
- package/src/bridge/navigation.ts +34 -0
- package/src/bridge/refusal.ts +151 -0
- package/src/bridge/types.ts +57 -4
- package/src/check/lint.ts +2 -1
- package/src/check/page.ts +12 -0
- package/src/compile/policy.ts +19 -1
- package/src/config/capabilities.ts +26 -1
- package/src/config/schema.ts +7 -0
- package/src/config/validate.ts +21 -3
- package/src/conformance/harness/page.ts +1 -0
- package/src/displays/displays.ts +1 -1
- package/src/guide/guide.ts +71 -7
- package/src/host/create-host.ts +19 -3
- package/src/host/types.ts +23 -1
- package/src/index.ts +22 -0
- package/src/libraries/files.generated.ts +8 -0
- package/src/libraries/libraries.ts +64 -0
- package/src/mcp/adapter.ts +63 -5
- package/src/presets/presets.ts +46 -3
- package/src/protocol/live-data.ts +138 -0
- package/src/protocol/mcp-apps.ts +19 -0
- package/src/protocol/shell-adapter.ts +50 -5
- package/src/react/dialog.tsx +2 -0
- package/src/react/frame.tsx +93 -0
- package/src/server/libraries.ts +28 -0
- package/src/server/server.ts +2 -1
- package/src/shell/runtime.ts +196 -6
package/src/react/frame.tsx
CHANGED
|
@@ -22,9 +22,12 @@ import type {
|
|
|
22
22
|
Confirm,
|
|
23
23
|
ConfirmAnswer,
|
|
24
24
|
ConfirmRequest,
|
|
25
|
+
Consent,
|
|
26
|
+
DataCache,
|
|
25
27
|
Proceed,
|
|
26
28
|
Refusal,
|
|
27
29
|
} from "../bridge/types.ts";
|
|
30
|
+
import { isRefused, reasonOf, Refused, type RefusalReason } from "../bridge/refusal.ts";
|
|
28
31
|
import type { CapabilityName } from "../config/capabilities.ts";
|
|
29
32
|
import type { ColorScheme, PortholeConfig } from "../config/schema.ts";
|
|
30
33
|
import { writeClipboard } from "../host/clipboard.ts";
|
|
@@ -61,6 +64,11 @@ interface PageState {
|
|
|
61
64
|
/** Heights displays reported, by display id: a frame mounting again starts at its last. */
|
|
62
65
|
readonly heights: Map<string, number>;
|
|
63
66
|
readonly confirm?: Confirm;
|
|
67
|
+
/** The provider's consent and cache (P079), read when a frame asks. */
|
|
68
|
+
readonly consent?: Consent;
|
|
69
|
+
/** The provider's consent is a `Refused` (P080): its reason, by value. */
|
|
70
|
+
readonly refusesAll?: RefusalReason;
|
|
71
|
+
readonly cached?: DataCache;
|
|
64
72
|
readonly hostInfo?: { readonly name: string; readonly version: string };
|
|
65
73
|
readonly nonce?: string;
|
|
66
74
|
/** The provider's locator, read when a frame locates (so an inline one restarts nothing). */
|
|
@@ -120,6 +128,14 @@ export interface PortholeProviderProps {
|
|
|
120
128
|
* Pass your own function to replace it.
|
|
121
129
|
*/
|
|
122
130
|
readonly confirm?: Confirm;
|
|
131
|
+
/**
|
|
132
|
+
* Whether a frame below may make its `consent`-gated calls (read the viewer's data, P079):
|
|
133
|
+
* asked once per display, before the first. Omitted: those calls are refused. A `Refused`
|
|
134
|
+
* refuses them all with its reason, and their handlers may be left out (P080).
|
|
135
|
+
*/
|
|
136
|
+
readonly consent?: Consent | Refused;
|
|
137
|
+
/** What the host last had for a resource, answered stale first (P079). */
|
|
138
|
+
readonly cached?: DataCache;
|
|
123
139
|
/** How the host names itself in the handshake. */
|
|
124
140
|
readonly hostInfo?: { readonly name: string; readonly version: string };
|
|
125
141
|
/** Where domain and external frames load from (see `InstanceLocator`). */
|
|
@@ -185,6 +201,25 @@ export function PortholeProvider(props: PortholeProviderProps): ReactElement {
|
|
|
185
201
|
),
|
|
186
202
|
[],
|
|
187
203
|
);
|
|
204
|
+
// The host's consent and cache as of the last render (P079), read when a frame asks.
|
|
205
|
+
const ownConsent = useRef(props.consent);
|
|
206
|
+
const ownCached = useRef(props.cached);
|
|
207
|
+
useLayoutEffect(() => {
|
|
208
|
+
ownConsent.current = props.consent;
|
|
209
|
+
ownCached.current = props.cached;
|
|
210
|
+
});
|
|
211
|
+
const consent = useCallback<Consent>((request) => {
|
|
212
|
+
const given = ownConsent.current;
|
|
213
|
+
if (isRefused(given)) {
|
|
214
|
+
throw given;
|
|
215
|
+
}
|
|
216
|
+
return given?.(request) ?? false;
|
|
217
|
+
}, []);
|
|
218
|
+
// A Refused for every frame below, by value (an inline one starts nothing again).
|
|
219
|
+
const refusesAll = useByValue(isRefused(props.consent) ? reasonOf(props.consent) : undefined);
|
|
220
|
+
const cached = useCallback<DataCache>((args, context) => ownCached.current?.(args, context), []);
|
|
221
|
+
const consents = props.consent !== undefined;
|
|
222
|
+
const caches = props.cached !== undefined;
|
|
188
223
|
// The default copyText: the host's page writes the clipboard (in the frame's click, which
|
|
189
224
|
// activated this page too) and shows the person what went on it, or offers to copy it.
|
|
190
225
|
const [notice, setNotice] = useState<CopyNotice | null>(null);
|
|
@@ -260,6 +295,9 @@ export function PortholeProvider(props: PortholeProviderProps): ReactElement {
|
|
|
260
295
|
heights,
|
|
261
296
|
confirm,
|
|
262
297
|
copyText,
|
|
298
|
+
...(consents ? { consent } : {}),
|
|
299
|
+
...(refusesAll === undefined ? {} : { refusesAll }),
|
|
300
|
+
...(caches ? { cached } : {}),
|
|
263
301
|
...(hostInfo === undefined ? {} : { hostInfo }),
|
|
264
302
|
...(locates ? { locate } : {}),
|
|
265
303
|
...(nonce === undefined ? {} : { nonce }),
|
|
@@ -274,6 +312,11 @@ export function PortholeProvider(props: PortholeProviderProps): ReactElement {
|
|
|
274
312
|
heights,
|
|
275
313
|
confirm,
|
|
276
314
|
copyText,
|
|
315
|
+
consents,
|
|
316
|
+
consent,
|
|
317
|
+
refusesAll,
|
|
318
|
+
caches,
|
|
319
|
+
cached,
|
|
277
320
|
hostInfo,
|
|
278
321
|
locates,
|
|
279
322
|
locate,
|
|
@@ -372,6 +415,13 @@ export interface PortholeFrameProps {
|
|
|
372
415
|
readonly handlers?: CapabilityHandlers;
|
|
373
416
|
/** Who confirms this frame's gated calls, over the provider's. */
|
|
374
417
|
readonly confirm?: Confirm;
|
|
418
|
+
/**
|
|
419
|
+
* Whether this frame may make its `consent`-gated calls, over the provider's (P079). A `Refused`
|
|
420
|
+
* refuses them all with its reason, and their handlers may be left out (P080).
|
|
421
|
+
*/
|
|
422
|
+
readonly consent?: Consent | Refused;
|
|
423
|
+
/** This frame's data cache, over the provider's (P079). */
|
|
424
|
+
readonly cached?: DataCache;
|
|
375
425
|
/** Bump to run the content again from the start. */
|
|
376
426
|
readonly generation?: number;
|
|
377
427
|
/** Run at once, without waiting to near the screen (it still counts toward the page's most). */
|
|
@@ -403,6 +453,12 @@ export interface PortholeFrameProps {
|
|
|
403
453
|
readonly onError?: (message: string, source: "content" | "host") => void;
|
|
404
454
|
/** The content navigated its frame away; it was started again (or stopped, after a few). */
|
|
405
455
|
readonly onNavigated?: () => void;
|
|
456
|
+
/**
|
|
457
|
+
* The shell stopped the content navigating its frame to `url` (P080): the address whole, the
|
|
458
|
+
* content's word. Served frames only, where the config declares `reportError`; `onContentError`
|
|
459
|
+
* hears it too, without the address's query.
|
|
460
|
+
*/
|
|
461
|
+
readonly onNavigationBlocked?: (blocked: { readonly url: string }) => void;
|
|
406
462
|
readonly onRefused?: (capability: CapabilityName, refusal: Refusal) => void;
|
|
407
463
|
/** Too many declines: the display stops asking until it runs again. */
|
|
408
464
|
readonly onMuted?: () => void;
|
|
@@ -609,6 +665,10 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
|
|
|
609
665
|
const config = useByValue(frame.config);
|
|
610
666
|
const instance = useByValue(frame.instance);
|
|
611
667
|
const fills = config.mode === "url" || frame.fill === true || config.layout.kind === "fill";
|
|
668
|
+
// A consent that refuses everything (P080), this frame's or else the provider's, by value: a
|
|
669
|
+
// display made with it needs no data handlers, so a change makes the display again.
|
|
670
|
+
const ownRefusal = useByValue(isRefused(frame.consent) ? reasonOf(frame.consent) : undefined);
|
|
671
|
+
const refusesAll = frame.consent === undefined ? page.refusesAll : ownRefusal;
|
|
612
672
|
|
|
613
673
|
// The latest props, read when the display calls back (never during render).
|
|
614
674
|
const latest = useEffectEvent(() => frame);
|
|
@@ -620,6 +680,8 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
|
|
|
620
680
|
handlers: pageHandlers,
|
|
621
681
|
copyText: pageCopyText,
|
|
622
682
|
confirm: pageConfirm,
|
|
683
|
+
consent: pageConsent,
|
|
684
|
+
cached: pageCached,
|
|
623
685
|
hostInfo,
|
|
624
686
|
locate,
|
|
625
687
|
nonce,
|
|
@@ -656,6 +718,9 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
|
|
|
656
718
|
case "navigated":
|
|
657
719
|
frame.onNavigated?.();
|
|
658
720
|
return;
|
|
721
|
+
case "navigationBlocked":
|
|
722
|
+
frame.onNavigationBlocked?.(detail as DisplayEvents["navigationBlocked"]);
|
|
723
|
+
return;
|
|
659
724
|
case "refused": {
|
|
660
725
|
const { capability, refusal } = detail as DisplayEvents["refused"];
|
|
661
726
|
frame.onRefused?.(capability, refusal);
|
|
@@ -710,6 +775,8 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
|
|
|
710
775
|
}
|
|
711
776
|
}
|
|
712
777
|
const asks = first.confirm !== undefined || pageConfirm !== undefined;
|
|
778
|
+
const asksConsent = first.consent !== undefined || pageConsent !== undefined;
|
|
779
|
+
const caches = first.cached !== undefined || pageCached !== undefined;
|
|
713
780
|
let display: DisplayHandle;
|
|
714
781
|
try {
|
|
715
782
|
display = createHost(config, {
|
|
@@ -730,6 +797,28 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
|
|
|
730
797
|
),
|
|
731
798
|
}
|
|
732
799
|
: {}),
|
|
800
|
+
...(refusesAll !== undefined
|
|
801
|
+
? { consent: new Refused(refusesAll.code, refusesAll.message) }
|
|
802
|
+
: asksConsent
|
|
803
|
+
? {
|
|
804
|
+
consent: (request: ConfirmRequest) => {
|
|
805
|
+
const given = latest().consent ?? pageConsent ?? (() => false);
|
|
806
|
+
if (isRefused(given)) {
|
|
807
|
+
throw given;
|
|
808
|
+
}
|
|
809
|
+
return given({
|
|
810
|
+
...request,
|
|
811
|
+
display: { ...request.display, id: latest().displayId, title: latest().title },
|
|
812
|
+
});
|
|
813
|
+
},
|
|
814
|
+
}
|
|
815
|
+
: {}),
|
|
816
|
+
...(caches
|
|
817
|
+
? {
|
|
818
|
+
cached: (args: { readonly uri: string }, context: CallContext) =>
|
|
819
|
+
(latest().cached ?? pageCached)?.(args, named(context)),
|
|
820
|
+
}
|
|
821
|
+
: {}),
|
|
733
822
|
theme: (each) => (latest().theme ?? latestPage().theme)?.(each) ?? {},
|
|
734
823
|
...(hostInfo === undefined ? {} : { hostInfo }),
|
|
735
824
|
...(locate === undefined ? {} : { locator: { locate } }),
|
|
@@ -753,6 +842,7 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
|
|
|
753
842
|
"resize",
|
|
754
843
|
"error",
|
|
755
844
|
"navigated",
|
|
845
|
+
"navigationBlocked",
|
|
756
846
|
"refused",
|
|
757
847
|
"muted",
|
|
758
848
|
"outcome",
|
|
@@ -785,6 +875,9 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
|
|
|
785
875
|
pageHandlers,
|
|
786
876
|
pageCopyText,
|
|
787
877
|
pageConfirm,
|
|
878
|
+
pageConsent,
|
|
879
|
+
pageCached,
|
|
880
|
+
refusesAll,
|
|
788
881
|
hostInfo,
|
|
789
882
|
locate,
|
|
790
883
|
nonce,
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { LIBRARY_FILES } from "../libraries/files.generated.ts";
|
|
2
|
+
import { LIBRARY_PATH } from "../libraries/libraries.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Porthole's vendored libraries (P079), as every content origin serves them under `LIBRARY_PATH`:
|
|
6
|
+
* fixed files, the same for everyone, so no ticket or cookie is asked for and they may be cached
|
|
7
|
+
* for good (a path's bytes never change). Same-site only: content origins load them, no one else.
|
|
8
|
+
* Null for any other path (the reserved prefix's 404 answers it).
|
|
9
|
+
*/
|
|
10
|
+
export function libraryFile(pathname: string, method: string): Response | null {
|
|
11
|
+
if (!pathname.startsWith(LIBRARY_PATH) || (method !== "GET" && method !== "HEAD")) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const body = Object.hasOwn(LIBRARY_FILES, pathname) ? LIBRARY_FILES[pathname] : undefined;
|
|
15
|
+
if (body === undefined) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return new Response(method === "HEAD" ? null : body, {
|
|
19
|
+
headers: {
|
|
20
|
+
"content-type": pathname.endsWith(".js")
|
|
21
|
+
? "text/javascript; charset=utf-8"
|
|
22
|
+
: "text/plain; charset=utf-8",
|
|
23
|
+
"cache-control": "public, max-age=31536000, immutable",
|
|
24
|
+
"cross-origin-resource-policy": "same-site",
|
|
25
|
+
"x-content-type-options": "nosniff",
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
package/src/server/server.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { base32, createKeys, newTicket, ticketId } from "./keys.ts";
|
|
|
12
12
|
import { randomBytes } from "./crypto.ts";
|
|
13
13
|
import { isHostLabel, reservedLabels } from "./naming.ts";
|
|
14
14
|
import { enterPage, pageScript, SESSION_PATH, statusPage } from "./pages.ts";
|
|
15
|
+
import { libraryFile } from "./libraries.ts";
|
|
15
16
|
import type { FrameBlock } from "../host/types.ts";
|
|
16
17
|
import { memoryTicketStore } from "./tickets.ts";
|
|
17
18
|
import type {
|
|
@@ -1156,7 +1157,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1156
1157
|
if (url.pathname === ENTRY_PATH && authorizeUrl !== null) {
|
|
1157
1158
|
return await redeem(request, url, label);
|
|
1158
1159
|
}
|
|
1159
|
-
const script = pageScript(url.pathname);
|
|
1160
|
+
const script = pageScript(url.pathname) ?? libraryFile(url.pathname, method);
|
|
1160
1161
|
if (script !== null) {
|
|
1161
1162
|
return script;
|
|
1162
1163
|
}
|
package/src/shell/runtime.ts
CHANGED
|
@@ -16,7 +16,11 @@
|
|
|
16
16
|
* shadow DOM through `setHTMLUnsafe`, `parseHTMLUnsafe`, `document.write`);
|
|
17
17
|
* 5. keeps content off the clipboard: the async Clipboard API refuses, `execCommand` won't copy or
|
|
18
18
|
* cut, and copy and cut events never reach content, so what a person copies is what they
|
|
19
|
-
* selected (P051). Copy buttons go through the host (`copyText`, P052)
|
|
19
|
+
* selected (P051). Copy buttons go through the host (`copyText`, P052);
|
|
20
|
+
* 6. cancels every navigation the frame starts to another document (P079), before it sends a
|
|
21
|
+
* request: a navigation carries data out in its URL, and the embedding page's `frame-src`,
|
|
22
|
+
* the wall for that, must let served frames reach their own content origins. (Opaque frames
|
|
23
|
+
* get no navigate events: there `frame-src` is the wall.)
|
|
20
24
|
* A whole document (an artifact page, an MCP Apps view) is never written in: it is parsed inert,
|
|
21
25
|
* imported, and its scripts run in order, as a fragment's are.
|
|
22
26
|
*
|
|
@@ -72,6 +76,41 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
72
76
|
}
|
|
73
77
|
}
|
|
74
78
|
|
|
79
|
+
// No navigation to another document (P079): a frame that navigates itself carries data out in
|
|
80
|
+
// the URL, and the embedding page's frame-src, the other wall, lets served frames reach every
|
|
81
|
+
// content origin. (An opaque frame gets no navigate events, as the spec says for opaque
|
|
82
|
+
// origins, measured: there frame-src is the wall, P033.) The navigate event comes before any request for every
|
|
83
|
+
// navigation this document starts (location, a link, window.open(…, "_self"), a refresh), and
|
|
84
|
+
// cancelling it stops it. Ours is the first listener, and reads the event through the platform's
|
|
85
|
+
// own getters, kept now, so content can neither stop it nor lie to it. Same-document ones (an
|
|
86
|
+
// anchor) go on, and so do reloads: the same address, on the frame's own origin (or none, in a
|
|
87
|
+
// srcdoc frame), which history can change only within that origin. The host's own (a new
|
|
88
|
+
// frame) are none of this document's. Each one stopped is told to the host (P080), as an error
|
|
89
|
+
// report: below, once the shell can speak.
|
|
90
|
+
let navigationBlocked = (_url) => {};
|
|
91
|
+
const navigationApi = window.navigation;
|
|
92
|
+
const getterOf = (proto, name) => {
|
|
93
|
+
const descriptor = proto ? Object.getOwnPropertyDescriptor(proto, name) : undefined;
|
|
94
|
+
return descriptor && typeof descriptor.get === "function" ? descriptor.get : null;
|
|
95
|
+
};
|
|
96
|
+
const navigateProto = typeof NavigateEvent === "function" ? NavigateEvent.prototype : null;
|
|
97
|
+
const destinationOf = getterOf(navigateProto, "destination");
|
|
98
|
+
const navigationTypeOf = getterOf(navigateProto, "navigationType");
|
|
99
|
+
const destinationProto = typeof NavigationDestination === "function" ? NavigationDestination.prototype : null;
|
|
100
|
+
const sameDocumentOf = getterOf(destinationProto, "sameDocument");
|
|
101
|
+
const urlOf = getterOf(destinationProto, "url");
|
|
102
|
+
const preventNavigation = Event.prototype.preventDefault;
|
|
103
|
+
const applyOf = Reflect.apply;
|
|
104
|
+
if (navigationApi && typeof navigationApi.addEventListener === "function" && destinationOf !== null && sameDocumentOf !== null && navigationTypeOf !== null) {
|
|
105
|
+
navigationApi.addEventListener("navigate", (event) => {
|
|
106
|
+
const destination = applyOf(destinationOf, event, []);
|
|
107
|
+
if (!applyOf(sameDocumentOf, destination, []) && applyOf(navigationTypeOf, event, []) !== "reload") {
|
|
108
|
+
applyOf(preventNavigation, event, []);
|
|
109
|
+
navigationBlocked(urlOf === null ? "" : String(applyOf(urlOf, destination, [])));
|
|
110
|
+
}
|
|
111
|
+
}, true);
|
|
112
|
+
}
|
|
113
|
+
|
|
75
114
|
// No nested frames (each would be a fresh realm with WebRTC intact), no resource hints and no
|
|
76
115
|
// refreshes (they reach the network outside the CSP's fetch directives). Removed as they are
|
|
77
116
|
// added: a mutation observer's microtask runs before a child frame's document loads.
|
|
@@ -442,13 +481,27 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
442
481
|
reported += 1;
|
|
443
482
|
send({ method: "notifications/message", params: { level: "error", logger: "porthole", data: String(message).slice(0, 2000) } });
|
|
444
483
|
};
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
484
|
+
// Nothing secret is ever said from here, so any parent may hear it; what it hears is the
|
|
485
|
+
// content's word, which the host gates before it acts on it.
|
|
486
|
+
const post = (message) => {
|
|
487
|
+
if (window.parent !== window) {
|
|
449
488
|
window.parent.postMessage(Object.assign({ jsonrpc: "2.0" }, message), "*");
|
|
450
489
|
}
|
|
451
490
|
};
|
|
491
|
+
const send = (message) => {
|
|
492
|
+
if (plan.protocol === "shell") {
|
|
493
|
+
post(message);
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
// A navigation the guard above stopped (P080): one of the run's error reports, told even where
|
|
497
|
+
// the content speaks for itself (an MCP Apps view), in Porthole's own notification.
|
|
498
|
+
navigationBlocked = (url) => {
|
|
499
|
+
if (!declared("reportError") || reported >= plan.maxErrorReports || url === "") {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
reported += 1;
|
|
503
|
+
post({ method: "porthole/navigation-blocked", params: { url: url.slice(0, 4096) } });
|
|
504
|
+
};
|
|
452
505
|
|
|
453
506
|
// A parsed tree's scripts are held (each replaced by a numbered marker) and run later, in order,
|
|
454
507
|
// as the page's parser would run them: each made again, live, in its marker's place, and each
|
|
@@ -710,6 +763,132 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
710
763
|
});
|
|
711
764
|
}
|
|
712
765
|
|
|
766
|
+
// Data (P079): readData reads a resource the host names, subscribeData follows one, through
|
|
767
|
+
// MCP's resources/* requests, which the host gates (the viewer's consent, a rate). A value is the
|
|
768
|
+
// first content's: JSON parsed when it says it is JSON, else its text.
|
|
769
|
+
let resourceUpdated = (_uri) => {};
|
|
770
|
+
const valueOf = (result) => {
|
|
771
|
+
const first = result && Array.isArray(result.contents) ? result.contents[0] : undefined;
|
|
772
|
+
if (first === null || typeof first !== "object") {
|
|
773
|
+
return undefined;
|
|
774
|
+
}
|
|
775
|
+
if (typeof first.text === "string") {
|
|
776
|
+
if (/(^|[/+])json(\s*;|$)/i.test(String(first.mimeType || ""))) {
|
|
777
|
+
try {
|
|
778
|
+
return JSON.parse(first.text);
|
|
779
|
+
} catch (_) {}
|
|
780
|
+
}
|
|
781
|
+
return first.text;
|
|
782
|
+
}
|
|
783
|
+
return typeof first.blob === "string" ? first.blob : undefined;
|
|
784
|
+
};
|
|
785
|
+
const staleOf = (result) => Boolean(result && result._meta && result._meta["porthole/stale"] === true);
|
|
786
|
+
const later = (error) => setTimeout(() => {
|
|
787
|
+
throw error;
|
|
788
|
+
});
|
|
789
|
+
if (declared("readData")) {
|
|
790
|
+
lock(window, "readData", (uri) => request("resources/read", { uri: String(uri) }).then(valueOf));
|
|
791
|
+
}
|
|
792
|
+
if (declared("subscribeData")) {
|
|
793
|
+
// One subscription per URI, however many follow it; a read at a time, one more if it changed
|
|
794
|
+
// meanwhile; a late follower gets the last value at once.
|
|
795
|
+
const followed = new Map();
|
|
796
|
+
const tell = (entry, result) => {
|
|
797
|
+
entry.last = result;
|
|
798
|
+
const value = valueOf(result);
|
|
799
|
+
const info = { stale: staleOf(result), uri: entry.uri };
|
|
800
|
+
for (const listener of Array.from(entry.listeners)) {
|
|
801
|
+
try {
|
|
802
|
+
listener.onData(value, info);
|
|
803
|
+
} catch (error) {
|
|
804
|
+
later(error);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
};
|
|
808
|
+
const fail = (entry, error) => {
|
|
809
|
+
for (const listener of Array.from(entry.listeners)) {
|
|
810
|
+
if (typeof listener.onError === "function") {
|
|
811
|
+
try {
|
|
812
|
+
listener.onError(error);
|
|
813
|
+
} catch (thrown) {
|
|
814
|
+
later(thrown);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
const refresh = (entry) => {
|
|
820
|
+
if (entry.reading) {
|
|
821
|
+
entry.again = true;
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
entry.reading = true;
|
|
825
|
+
request("resources/read", { uri: entry.uri })
|
|
826
|
+
.then((result) => {
|
|
827
|
+
if (followed.get(entry.uri) === entry) {
|
|
828
|
+
tell(entry, result);
|
|
829
|
+
}
|
|
830
|
+
}, (error) => fail(entry, error))
|
|
831
|
+
.then(() => {
|
|
832
|
+
entry.reading = false;
|
|
833
|
+
if (entry.again && followed.get(entry.uri) === entry) {
|
|
834
|
+
entry.again = false;
|
|
835
|
+
refresh(entry);
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
};
|
|
839
|
+
resourceUpdated = (uri) => {
|
|
840
|
+
const entry = followed.get(uri);
|
|
841
|
+
if (entry !== undefined) {
|
|
842
|
+
refresh(entry);
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
lock(window, "subscribeData", (uri, onData, onError) => {
|
|
846
|
+
if (typeof onData !== "function") {
|
|
847
|
+
throw new TypeError("subscribeData(uri, onData, onError): onData must be a function");
|
|
848
|
+
}
|
|
849
|
+
const key = String(uri);
|
|
850
|
+
const listener = { onData, onError };
|
|
851
|
+
let entry = followed.get(key);
|
|
852
|
+
if (entry === undefined) {
|
|
853
|
+
const made = { uri: key, listeners: new Set(), reading: false, again: false, last: undefined };
|
|
854
|
+
entry = made;
|
|
855
|
+
followed.set(key, made);
|
|
856
|
+
request("resources/subscribe", { uri: key }).then(() => {
|
|
857
|
+
if (followed.get(key) === made) {
|
|
858
|
+
refresh(made);
|
|
859
|
+
}
|
|
860
|
+
}, (error) => {
|
|
861
|
+
if (followed.get(key) === made) {
|
|
862
|
+
followed.delete(key);
|
|
863
|
+
}
|
|
864
|
+
fail(made, error);
|
|
865
|
+
});
|
|
866
|
+
} else if (entry.last !== undefined) {
|
|
867
|
+
const known = entry;
|
|
868
|
+
Promise.resolve().then(() => {
|
|
869
|
+
if (known.listeners.has(listener)) {
|
|
870
|
+
try {
|
|
871
|
+
onData(valueOf(known.last), { stale: staleOf(known.last), uri: key });
|
|
872
|
+
} catch (error) {
|
|
873
|
+
later(error);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
entry.listeners.add(listener);
|
|
879
|
+
const mine = entry;
|
|
880
|
+
return () => {
|
|
881
|
+
if (!mine.listeners.delete(listener) || mine.listeners.size > 0) {
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
if (followed.get(key) === mine) {
|
|
885
|
+
followed.delete(key);
|
|
886
|
+
request("resources/unsubscribe", { uri: key }).catch(() => {});
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
|
|
713
892
|
window.addEventListener("error", (event) => {
|
|
714
893
|
const error = event.error;
|
|
715
894
|
const where = event.lineno ? " (line " + event.lineno + ")" : "";
|
|
@@ -861,7 +1040,13 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
861
1040
|
if (waiting !== undefined) {
|
|
862
1041
|
pending.delete(data.id);
|
|
863
1042
|
if (data.error) {
|
|
864
|
-
|
|
1043
|
+
// A refusal says why (P080): its code, and a sentence to show the person.
|
|
1044
|
+
const why = data.error.data !== null && typeof data.error.data === "object" ? data.error.data : {};
|
|
1045
|
+
const error = new Error(String(typeof why.message === "string" && why.message !== "" ? why.message : data.error.message || "refused"));
|
|
1046
|
+
if (typeof why.code === "string") {
|
|
1047
|
+
error.code = why.code;
|
|
1048
|
+
}
|
|
1049
|
+
waiting.reject(error);
|
|
865
1050
|
} else {
|
|
866
1051
|
waiting.resolve(data.result);
|
|
867
1052
|
}
|
|
@@ -879,6 +1064,11 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
879
1064
|
case "ui/notifications/host-context-changed":
|
|
880
1065
|
applyTheme(params);
|
|
881
1066
|
break;
|
|
1067
|
+
case "notifications/resources/updated":
|
|
1068
|
+
if (typeof params.uri === "string") {
|
|
1069
|
+
resourceUpdated(params.uri);
|
|
1070
|
+
}
|
|
1071
|
+
break;
|
|
882
1072
|
case "ui/resource-teardown":
|
|
883
1073
|
case "ping":
|
|
884
1074
|
if (data.id !== undefined) {
|