@botiverse/porthole 0.12.0 → 0.14.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/annotate/types.d.ts +32 -3
- package/dist/annotate/types.js +4 -0
- package/dist/annotate/validate.js +19 -2
- package/dist/bridge/bridge.d.ts +8 -1
- package/dist/bridge/bridge.js +7 -0
- package/dist/bridge/unseen.d.ts +8 -0
- package/dist/bridge/unseen.js +17 -0
- package/dist/check/files.js +14 -10
- package/dist/check/lint.js +1 -1
- package/dist/check/page-source.js +1 -1
- package/dist/check/scroll.d.ts +29 -13
- package/dist/check/scroll.js +74 -18
- package/dist/compile/policy.d.ts +7 -0
- package/dist/compile/policy.js +13 -1
- package/dist/config/capabilities.d.ts +16 -3
- package/dist/config/capabilities.js +1 -0
- package/dist/config/schema.d.ts +11 -0
- package/dist/config/validate.js +19 -0
- package/dist/conformance/harness-source.js +1 -1
- package/dist/displays/displays.js +3 -1
- package/dist/guide/limits.js +1 -1
- package/dist/guide/runtime.js +1 -1
- package/dist/host/create-host.d.ts +2 -0
- package/dist/host/create-host.js +100 -3
- package/dist/host/pass-on.d.ts +15 -0
- package/dist/host/pass-on.js +65 -0
- package/dist/host/types.d.ts +37 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/isolation/types.d.ts +8 -0
- package/dist/isolation/types.js +8 -0
- package/dist/platform/focus-guard.d.ts +212 -0
- package/dist/platform/focus-guard.js +620 -0
- package/dist/platform/types.d.ts +25 -0
- package/dist/platform/web.js +80 -2
- package/dist/presets/presets.d.ts +2 -0
- package/dist/presets/presets.js +1 -0
- package/dist/react/frame.d.ts +8 -0
- package/dist/react/frame.js +4 -0
- package/dist/runtime/requests.js +34 -12
- package/dist/runtime/types.generated.js +1 -1
- package/dist/server/files.js +19 -3
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +4 -0
- package/dist/server/nested.d.ts +37 -0
- package/dist/server/nested.js +70 -0
- package/dist/server/server.js +57 -7
- package/dist/shell/annotate.d.ts +1 -1
- package/dist/shell/annotate.js +157 -7
- package/dist/shell/frame.d.ts +45 -0
- package/dist/shell/frame.js +250 -0
- package/dist/shell/plan.js +2 -1
- package/dist/shell/runtime.js +49 -14
- package/docs/ADOPTING.md +140 -7
- package/docs/ARCHITECTURE.md +23 -0
- package/docs/DECISIONS.md +564 -5
- package/docs/DESIGN.md +18 -2
- package/docs/THREAT_MODEL.md +192 -30
- package/package.json +1 -1
- package/src/annotate/types.ts +28 -3
- package/src/annotate/validate.ts +23 -2
- package/src/bridge/bridge.ts +17 -1
- package/src/bridge/unseen.ts +24 -0
- package/src/check/files.ts +14 -10
- package/src/check/lint.ts +1 -1
- package/src/check/scroll.ts +106 -38
- package/src/compile/policy.ts +14 -1
- package/src/config/capabilities.ts +16 -3
- package/src/config/schema.ts +11 -0
- package/src/config/validate.ts +23 -0
- package/src/displays/displays.ts +3 -1
- package/src/guide/limits.ts +1 -1
- package/src/guide/runtime.ts +1 -1
- package/src/host/create-host.ts +105 -4
- package/src/host/pass-on.ts +73 -0
- package/src/host/types.ts +33 -0
- package/src/index.ts +5 -1
- package/src/isolation/types.ts +12 -0
- package/src/platform/focus-guard.ts +815 -0
- package/src/platform/types.ts +24 -0
- package/src/platform/web.ts +96 -2
- package/src/presets/presets.ts +3 -0
- package/src/react/frame.tsx +10 -0
- package/src/runtime/requests.ts +36 -14
- package/src/runtime/types.generated.ts +1 -1
- package/src/server/files.ts +19 -3
- package/src/server/index.ts +10 -0
- package/src/server/nested.ts +86 -0
- package/src/server/server.ts +65 -6
- package/src/shell/annotate.ts +157 -7
- package/src/shell/frame.ts +268 -0
- package/src/shell/plan.ts +2 -1
- package/src/shell/runtime.ts +49 -14
- package/types/runtime/v1/comments.d.ts +8 -4
package/dist/shell/annotate.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* (`porthole/annotate.selected|unselected|cancelled|anchored|activated`):
|
|
9
9
|
* - picking an element: the element under the pointer (or finger) is outlined with its name; a
|
|
10
10
|
* click or tap picks it, and the content's own handlers never see that click (unless the content
|
|
11
|
-
* patched the DOM's built-ins, P105); Escape cancels;
|
|
11
|
+
* patched the DOM's built-ins, P105); Escape cancels (while selecting text too, P112);
|
|
12
12
|
* - selecting text: each selection that settles is reported, with where it is, and its clearing;
|
|
13
13
|
* - marks: each anchor is found in the content as it is now (its CSS selector, its text where it
|
|
14
14
|
* was, its text anywhere, its words with any spacing) and drawn over it, in a layer of the
|
|
@@ -39,6 +39,8 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
39
39
|
cancelled: "porthole/annotate.cancelled",
|
|
40
40
|
anchored: "porthole/annotate.anchored",
|
|
41
41
|
activated: "porthole/annotate.activated",
|
|
42
|
+
track: "porthole/annotate.track",
|
|
43
|
+
tracked: "porthole/annotate.tracked",
|
|
42
44
|
};
|
|
43
45
|
// The DOM's own functions, taken before content runs.
|
|
44
46
|
const elementFromPoint = Document.prototype.elementFromPoint;
|
|
@@ -51,6 +53,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
51
53
|
const elementRect = Element.prototype.getBoundingClientRect;
|
|
52
54
|
const rangeRect = Range.prototype.getBoundingClientRect;
|
|
53
55
|
const rangeRects = Range.prototype.getClientRects;
|
|
56
|
+
const setStart = Range.prototype.setStart;
|
|
57
|
+
const setStartBefore = Range.prototype.setStartBefore;
|
|
58
|
+
const compareBoundaryPoints = Range.prototype.compareBoundaryPoints;
|
|
54
59
|
const attachShadow = Element.prototype.attachShadow;
|
|
55
60
|
const getAttribute = Element.prototype.getAttribute;
|
|
56
61
|
const listenOn = EventTarget.prototype.addEventListener;
|
|
@@ -389,7 +394,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
389
394
|
return shadow;
|
|
390
395
|
};
|
|
391
396
|
|
|
392
|
-
let mode = { elements: false, text: false };
|
|
397
|
+
let mode = { elements: false, text: false, hold: false };
|
|
393
398
|
let hover = null;
|
|
394
399
|
let marks = [];
|
|
395
400
|
// The markers as last drawn, for a click to find: { id, element }. Each mark keeps its
|
|
@@ -817,7 +822,76 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
817
822
|
}
|
|
818
823
|
};
|
|
819
824
|
|
|
820
|
-
|
|
825
|
+
// The mark the host follows (P112): its box at each animation frame where it moved, at most
|
|
826
|
+
// one every 16 ms, read through the platform's own frame clock (taken before content runs).
|
|
827
|
+
const nextFrame = window.requestAnimationFrame.bind(window);
|
|
828
|
+
let tracking = null;
|
|
829
|
+
let trackedBox = null;
|
|
830
|
+
let trackedAt = 0;
|
|
831
|
+
let trackFrame = 0;
|
|
832
|
+
const trackTick = () => {
|
|
833
|
+
trackFrame = 0;
|
|
834
|
+
if (tracking === null) {
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
const mark = marks.find((each) => each.id === tracking);
|
|
838
|
+
if (mark !== undefined && mark.found !== null) {
|
|
839
|
+
const rect = box(rectOf(mark.found));
|
|
840
|
+
const now = performance.now();
|
|
841
|
+
if (apart(trackedBox, rect) && now - trackedAt >= 16) {
|
|
842
|
+
trackedBox = rect;
|
|
843
|
+
trackedAt = now;
|
|
844
|
+
tell(M.tracked, { id: tracking, rect });
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
trackFrame = nextFrame(trackTick);
|
|
848
|
+
};
|
|
849
|
+
const track = (id) => {
|
|
850
|
+
tracking = typeof id === "string" ? id : null;
|
|
851
|
+
trackedBox = null;
|
|
852
|
+
if (tracking !== null && trackFrame === 0) {
|
|
853
|
+
trackFrame = nextFrame(trackTick);
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
// Where each found mark starts in the document, in order (P112): its place in a list the host
|
|
858
|
+
// sorts, offscreen ones too. Read now, every time results are told.
|
|
859
|
+
const startOf = (found) => {
|
|
860
|
+
const range = createRange.call(document);
|
|
861
|
+
if (found.range) {
|
|
862
|
+
setStart.call(range, found.range.startContainer, found.range.startOffset);
|
|
863
|
+
} else {
|
|
864
|
+
setStartBefore.call(range, found.element);
|
|
865
|
+
}
|
|
866
|
+
return range;
|
|
867
|
+
};
|
|
868
|
+
const ordersOf = () => {
|
|
869
|
+
const placed = [];
|
|
870
|
+
for (const mark of marks) {
|
|
871
|
+
if (mark.found !== null) {
|
|
872
|
+
try {
|
|
873
|
+
placed.push({ mark, start: startOf(mark.found) });
|
|
874
|
+
} catch (_) {}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
placed.sort((a, b) => {
|
|
878
|
+
try {
|
|
879
|
+
return compareBoundaryPoints.call(a.start, Range.START_TO_START, b.start);
|
|
880
|
+
} catch (_) {
|
|
881
|
+
return 0;
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
const orders = new Map();
|
|
885
|
+
placed.forEach((each, at) => orders.set(each.mark, at));
|
|
886
|
+
return orders;
|
|
887
|
+
};
|
|
888
|
+
const resultOf = (mark, orders) => {
|
|
889
|
+
if (mark.found === null) {
|
|
890
|
+
return { id: mark.id, state: "orphaned" };
|
|
891
|
+
}
|
|
892
|
+
const order = (orders || ordersOf()).get(mark);
|
|
893
|
+
return Object.assign({ id: mark.id, state: "attached", rect: box(rectOf(mark.found)) }, order === undefined ? {} : { order });
|
|
894
|
+
};
|
|
821
895
|
// Two boxes more than 0.5 px apart on any side (or one of them unknown).
|
|
822
896
|
const apart = (a, b) => !a || !b || Math.abs(a.x - b.x) > 0.5 || Math.abs(a.y - b.y) > 0.5 || Math.abs(a.width - b.width) > 0.5 || Math.abs(a.height - b.height) > 0.5;
|
|
823
897
|
// What the host was last told of each mark (mark.told), so a settle tells only what moved.
|
|
@@ -826,7 +900,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
826
900
|
if (!told || told.state !== result.state) {
|
|
827
901
|
return true;
|
|
828
902
|
}
|
|
829
|
-
return result.state === "attached" && apart(told.rect, result.rect);
|
|
903
|
+
return result.state === "attached" && (apart(told.rect, result.rect) || told.order !== result.order);
|
|
830
904
|
};
|
|
831
905
|
const report = (pairs) => {
|
|
832
906
|
if (pairs.length > 0) {
|
|
@@ -836,7 +910,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
836
910
|
tell(M.anchored, { results: pairs.map((pair) => pair.result) });
|
|
837
911
|
}
|
|
838
912
|
};
|
|
839
|
-
const resultsOf = (only) =>
|
|
913
|
+
const resultsOf = (only) => {
|
|
914
|
+
const orders = ordersOf();
|
|
915
|
+
return marks.map((mark) => ({ mark, result: resultOf(mark, orders) })).filter((pair) => !only || moved(pair.mark, pair.result));
|
|
916
|
+
};
|
|
840
917
|
// Finds every mark again; tells the host every result (after show, or when a state changed),
|
|
841
918
|
// only the marks that moved (once the content is quiet), or nothing.
|
|
842
919
|
const anchorAll = (all, quiet) => {
|
|
@@ -1142,6 +1219,66 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1142
1219
|
redraw();
|
|
1143
1220
|
}
|
|
1144
1221
|
}, true);
|
|
1222
|
+
|
|
1223
|
+
// Holding a block (P112, hold mode): a finger held still for 500 ms selects the block under
|
|
1224
|
+
// it, and the click its release makes is the shell's; every other tap is the page's, as is
|
|
1225
|
+
// any mouse. On the platform's own timer (taken before content runs).
|
|
1226
|
+
const later = window.setTimeout.bind(window);
|
|
1227
|
+
const cancelLater = window.clearTimeout.bind(window);
|
|
1228
|
+
const HOLD_MS = 500;
|
|
1229
|
+
const HOLD_SLOP = 10;
|
|
1230
|
+
let holding = null;
|
|
1231
|
+
let swallowUntil = 0;
|
|
1232
|
+
const endHold = () => {
|
|
1233
|
+
if (holding !== null) {
|
|
1234
|
+
cancelLater(holding.timer);
|
|
1235
|
+
holding = null;
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1238
|
+
window.addEventListener("pointerdown", (event) => {
|
|
1239
|
+
if (!mode.hold || mode.elements || event.pointerType !== "touch" || !event.isPrimary) {
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
endHold();
|
|
1243
|
+
const x = event.clientX;
|
|
1244
|
+
const y = event.clientY;
|
|
1245
|
+
holding = {
|
|
1246
|
+
x,
|
|
1247
|
+
y,
|
|
1248
|
+
timer: later(() => {
|
|
1249
|
+
holding = null;
|
|
1250
|
+
if (!mode.hold) {
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1253
|
+
const element = under(x, y);
|
|
1254
|
+
const anchor = element === null ? null : elementAnchor(element, textIndex());
|
|
1255
|
+
if (anchor === null) {
|
|
1256
|
+
return;
|
|
1257
|
+
}
|
|
1258
|
+
swallowUntil = performance.now() + 2000;
|
|
1259
|
+
tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
|
|
1260
|
+
}, HOLD_MS),
|
|
1261
|
+
};
|
|
1262
|
+
}, true);
|
|
1263
|
+
window.addEventListener("pointermove", (event) => {
|
|
1264
|
+
if (holding !== null && event.pointerType === "touch" && Math.abs(event.clientX - holding.x) + Math.abs(event.clientY - holding.y) > HOLD_SLOP) {
|
|
1265
|
+
endHold();
|
|
1266
|
+
}
|
|
1267
|
+
}, true);
|
|
1268
|
+
for (const type of ["pointerup", "pointercancel"]) {
|
|
1269
|
+
window.addEventListener(type, endHold, true);
|
|
1270
|
+
}
|
|
1271
|
+
for (const type of ["click", "contextmenu"]) {
|
|
1272
|
+
window.addEventListener(type, (event) => {
|
|
1273
|
+
if (swallowUntil > performance.now()) {
|
|
1274
|
+
if (type === "click") {
|
|
1275
|
+
swallowUntil = 0;
|
|
1276
|
+
}
|
|
1277
|
+
event.preventDefault();
|
|
1278
|
+
event.stopImmediatePropagation();
|
|
1279
|
+
}
|
|
1280
|
+
}, true);
|
|
1281
|
+
}
|
|
1145
1282
|
window.addEventListener("click", (event) => {
|
|
1146
1283
|
if (!mode.elements) {
|
|
1147
1284
|
return;
|
|
@@ -1170,7 +1307,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1170
1307
|
tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
|
|
1171
1308
|
}, true);
|
|
1172
1309
|
window.addEventListener("keydown", (event) => {
|
|
1173
|
-
if (
|
|
1310
|
+
if (event.key !== "Escape") {
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
if (mode.elements) {
|
|
1174
1314
|
event.preventDefault();
|
|
1175
1315
|
event.stopImmediatePropagation();
|
|
1176
1316
|
mode = { elements: false, text: mode.text };
|
|
@@ -1178,6 +1318,12 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1178
1318
|
setCursor(false);
|
|
1179
1319
|
draw();
|
|
1180
1320
|
tell(M.cancelled, {});
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
// Selecting text (P112): the person wants out too. The key stays the page's (a page that
|
|
1324
|
+
// closes its own dialog on Escape still does); the host decides what ends.
|
|
1325
|
+
if (mode.text) {
|
|
1326
|
+
tell(M.cancelled, {});
|
|
1181
1327
|
}
|
|
1182
1328
|
}, true);
|
|
1183
1329
|
|
|
@@ -1244,7 +1390,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1244
1390
|
const params = data.params !== null && typeof data.params === "object" ? data.params : {};
|
|
1245
1391
|
switch (data.method) {
|
|
1246
1392
|
case M.start:
|
|
1247
|
-
mode = { elements: params.elements === true, text: params.text === true };
|
|
1393
|
+
mode = { elements: params.elements === true, text: params.text === true, hold: params.elements === "hold" };
|
|
1248
1394
|
hover = null;
|
|
1249
1395
|
reported = null;
|
|
1250
1396
|
reportedBox = null;
|
|
@@ -1276,7 +1422,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1276
1422
|
case M.focus:
|
|
1277
1423
|
focus(String(params.id));
|
|
1278
1424
|
break;
|
|
1425
|
+
case M.track:
|
|
1426
|
+
track(params.id);
|
|
1427
|
+
break;
|
|
1279
1428
|
case M.clear:
|
|
1429
|
+
track(null);
|
|
1280
1430
|
pendingFocus = null;
|
|
1281
1431
|
marks = [];
|
|
1282
1432
|
active = null;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Porthole's shell for a nested page (P111): the whole document at the address the host frames,
|
|
3
|
+
* with no content in it. It frames the page (the same origin's sealed files' address) in a child
|
|
4
|
+
* sandboxed without `allow-same-origin`, so the page's origin is opaque and nothing of the shell's
|
|
5
|
+
* realm is reachable from it: not its DOM, its globals, its storage, or the window the host hears.
|
|
6
|
+
*
|
|
7
|
+
* The shell:
|
|
8
|
+
* - passes every message between the host (its parent, from the host origins) and the page (its
|
|
9
|
+
* child's window, by `event.source` alone: the page's origin is "null") unchanged, and drops any
|
|
10
|
+
* the page sends in the shell's own namespace (`porthole/shell/`), so the shell's words to the
|
|
11
|
+
* host are only its own;
|
|
12
|
+
* - tells the host its own state (`porthole/shell/state`): whether its transient activation is on,
|
|
13
|
+
* and since when, and whether the page's focus is in it. A gesture in the page activates the
|
|
14
|
+
* page and its ancestors, the shell among them, and nothing on the host's page activates the
|
|
15
|
+
* shell (activation goes up, never down), so the shell's activation turning on is a press or
|
|
16
|
+
* key of the person's in this page. It is read at every animation frame, on its window's focus
|
|
17
|
+
* and blur, and before each message of the page's is passed on (a call made in a click's handler
|
|
18
|
+
* arrives after the state that says so). What it can't see: a gesture while its activation is
|
|
19
|
+
* already on (within 5 s of the last one) turns nothing on;
|
|
20
|
+
* - renews the page's sealed files' address before it ends, and gives the page the new one
|
|
21
|
+
* (`porthole/files-renewed`): the page's own loads aren't its origin's (an opaque origin's never
|
|
22
|
+
* are), so only the shell, on that origin, may renew it (P083);
|
|
23
|
+
* - passes its focus to the page (the host's own move into the frame, or a Tab).
|
|
24
|
+
*
|
|
25
|
+
* Plain ES2020, no imports and no build step, like the runtime. `__PLAN__` is its plan.
|
|
26
|
+
*/
|
|
27
|
+
export declare const FRAME_SHELL_RUNTIME: string;
|
|
28
|
+
export { SHELL_METHOD_PREFIX, SHELL_STATE_METHOD } from "../isolation/types.ts";
|
|
29
|
+
export interface FrameShellOptions {
|
|
30
|
+
/** The host origins: the shell takes messages from its parent only from these. */
|
|
31
|
+
readonly origins: readonly string[];
|
|
32
|
+
/** The page's address: absolute, on the shell's own origin, under `/__porthole/f/`. */
|
|
33
|
+
readonly page: string;
|
|
34
|
+
/** The page frame's accessible name. */
|
|
35
|
+
readonly title?: string;
|
|
36
|
+
/** Where the page's sealed address is renewed (`<base>__porthole/renew`), and when it ends. */
|
|
37
|
+
readonly renew?: {
|
|
38
|
+
readonly url: string;
|
|
39
|
+
readonly expires: number;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** The shell's script for `options`: what its document runs, and what its CSP's hash names. */
|
|
43
|
+
export declare function frameShellScript(options: FrameShellOptions): string;
|
|
44
|
+
/** The shell's document around its script: nothing else (its CSP allows that script alone). */
|
|
45
|
+
export declare function frameShellDocument(script: string): string;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { PAGE_SANDBOX_TOKENS } from "../compile/policy.js";
|
|
2
|
+
import { hostOriginProblem } from "../isolation/served.js";
|
|
3
|
+
/**
|
|
4
|
+
* Porthole's shell for a nested page (P111): the whole document at the address the host frames,
|
|
5
|
+
* with no content in it. It frames the page (the same origin's sealed files' address) in a child
|
|
6
|
+
* sandboxed without `allow-same-origin`, so the page's origin is opaque and nothing of the shell's
|
|
7
|
+
* realm is reachable from it: not its DOM, its globals, its storage, or the window the host hears.
|
|
8
|
+
*
|
|
9
|
+
* The shell:
|
|
10
|
+
* - passes every message between the host (its parent, from the host origins) and the page (its
|
|
11
|
+
* child's window, by `event.source` alone: the page's origin is "null") unchanged, and drops any
|
|
12
|
+
* the page sends in the shell's own namespace (`porthole/shell/`), so the shell's words to the
|
|
13
|
+
* host are only its own;
|
|
14
|
+
* - tells the host its own state (`porthole/shell/state`): whether its transient activation is on,
|
|
15
|
+
* and since when, and whether the page's focus is in it. A gesture in the page activates the
|
|
16
|
+
* page and its ancestors, the shell among them, and nothing on the host's page activates the
|
|
17
|
+
* shell (activation goes up, never down), so the shell's activation turning on is a press or
|
|
18
|
+
* key of the person's in this page. It is read at every animation frame, on its window's focus
|
|
19
|
+
* and blur, and before each message of the page's is passed on (a call made in a click's handler
|
|
20
|
+
* arrives after the state that says so). What it can't see: a gesture while its activation is
|
|
21
|
+
* already on (within 5 s of the last one) turns nothing on;
|
|
22
|
+
* - renews the page's sealed files' address before it ends, and gives the page the new one
|
|
23
|
+
* (`porthole/files-renewed`): the page's own loads aren't its origin's (an opaque origin's never
|
|
24
|
+
* are), so only the shell, on that origin, may renew it (P083);
|
|
25
|
+
* - passes its focus to the page (the host's own move into the frame, or a Tab).
|
|
26
|
+
*
|
|
27
|
+
* Plain ES2020, no imports and no build step, like the runtime. `__PLAN__` is its plan.
|
|
28
|
+
*/
|
|
29
|
+
export const FRAME_SHELL_RUNTIME = String.raw `(() => {
|
|
30
|
+
"use strict";
|
|
31
|
+
const plan = __PLAN__;
|
|
32
|
+
const PREFIX = "porthole/shell/";
|
|
33
|
+
const own = location.origin;
|
|
34
|
+
let page = null;
|
|
35
|
+
try {
|
|
36
|
+
page = new URL(plan.page);
|
|
37
|
+
} catch (_) {}
|
|
38
|
+
// Only a page of this origin's sealed files: nothing else is ever framed here.
|
|
39
|
+
if (page === null || page.origin !== own || page.pathname.indexOf("/__porthole/f/") !== 0) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
// Belt and braces: a plan that would give the page this origin frames nothing.
|
|
43
|
+
if (typeof plan.sandbox !== "string" || /allow-same-origin/i.test(plan.sandbox)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const root = document.documentElement;
|
|
47
|
+
root.style.height = "100%";
|
|
48
|
+
document.body.style.margin = "0";
|
|
49
|
+
document.body.style.height = "100%";
|
|
50
|
+
document.body.style.background = "transparent";
|
|
51
|
+
const frame = document.createElement("iframe");
|
|
52
|
+
// Scripts only: never allow-same-origin (the page would have this origin, and so this realm).
|
|
53
|
+
frame.setAttribute("sandbox", plan.sandbox);
|
|
54
|
+
frame.setAttribute("allow", "clipboard-read 'none'; clipboard-write 'none'");
|
|
55
|
+
frame.setAttribute("referrerpolicy", "no-referrer");
|
|
56
|
+
frame.title = typeof plan.title === "string" ? plan.title : "Content";
|
|
57
|
+
frame.style.display = "block";
|
|
58
|
+
frame.style.border = "0";
|
|
59
|
+
frame.style.width = "100%";
|
|
60
|
+
frame.style.height = "100%";
|
|
61
|
+
frame.style.background = "transparent";
|
|
62
|
+
frame.src = page.href;
|
|
63
|
+
document.body.append(frame);
|
|
64
|
+
|
|
65
|
+
const framed = window.parent !== window;
|
|
66
|
+
const web = plan.origins.filter((origin) => origin.indexOf("https://") === 0 || origin.indexOf("http://") === 0);
|
|
67
|
+
// To the host: each web host origin (only the parent's is delivered); a desktop app's own scheme
|
|
68
|
+
// is no target a browser takes, so where one is listed and the parent isn't a known web origin,
|
|
69
|
+
// "*" (frame-ancestors holds the parent to the host origins; nothing said here is secret).
|
|
70
|
+
const toHost = (message) => {
|
|
71
|
+
if (!framed) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
for (const origin of web) {
|
|
75
|
+
try {
|
|
76
|
+
window.parent.postMessage(message, origin);
|
|
77
|
+
} catch (_) {}
|
|
78
|
+
}
|
|
79
|
+
const parent = location.ancestorOrigins && location.ancestorOrigins.length > 0 ? location.ancestorOrigins[0] : null;
|
|
80
|
+
if (web.length < plan.origins.length && (parent === null || web.indexOf(parent) === -1)) {
|
|
81
|
+
try {
|
|
82
|
+
window.parent.postMessage(message, "*");
|
|
83
|
+
} catch (_) {}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// The shell's own state: its transient activation (and when it turned on), the page's focus.
|
|
88
|
+
// "since" is when the shell saw activation turn on, which a page that holds the thread the shell
|
|
89
|
+
// shares with it (WebKit, Firefox) can push later than the press: so the shell looks every 50 ms
|
|
90
|
+
// and beats as often, and each report says how long it went without a look before (gap). The
|
|
91
|
+
// host takes an activation the shell saw after a stall for uncertain (P111, after its review).
|
|
92
|
+
const activation = navigator.userActivation;
|
|
93
|
+
let active = false;
|
|
94
|
+
let since = 0;
|
|
95
|
+
let focused = false;
|
|
96
|
+
let lookedAt = performance.now();
|
|
97
|
+
let toldAt = 0;
|
|
98
|
+
const tell = (gap) => {
|
|
99
|
+
toldAt = performance.now();
|
|
100
|
+
toHost({
|
|
101
|
+
jsonrpc: "2.0",
|
|
102
|
+
method: PREFIX + "state",
|
|
103
|
+
params: {
|
|
104
|
+
supported: activation !== undefined,
|
|
105
|
+
active,
|
|
106
|
+
ago: active ? Math.max(0, Math.round(performance.now() - since)) : null,
|
|
107
|
+
focused,
|
|
108
|
+
gap: Math.max(0, Math.round(gap)),
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
const look = () => {
|
|
113
|
+
const now = performance.now();
|
|
114
|
+
const gap = now - lookedAt;
|
|
115
|
+
lookedAt = now;
|
|
116
|
+
const on = activation !== undefined && activation.isActive === true;
|
|
117
|
+
const inside = document.hasFocus();
|
|
118
|
+
let changed = false;
|
|
119
|
+
if (on !== active) {
|
|
120
|
+
active = on;
|
|
121
|
+
if (on) {
|
|
122
|
+
since = performance.now();
|
|
123
|
+
}
|
|
124
|
+
changed = true;
|
|
125
|
+
}
|
|
126
|
+
if (inside !== focused) {
|
|
127
|
+
focused = inside;
|
|
128
|
+
changed = true;
|
|
129
|
+
}
|
|
130
|
+
if (changed) {
|
|
131
|
+
tell(gap);
|
|
132
|
+
} else if (now - toldAt >= 50) {
|
|
133
|
+
// The heartbeat: the host knows the shell was looking, every 50 ms.
|
|
134
|
+
toldAt = now;
|
|
135
|
+
toHost({ jsonrpc: "2.0", method: PREFIX + "beat", params: { gap: Math.max(0, Math.round(gap)) } });
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const tick = () => {
|
|
139
|
+
look();
|
|
140
|
+
requestAnimationFrame(tick);
|
|
141
|
+
};
|
|
142
|
+
requestAnimationFrame(tick);
|
|
143
|
+
setInterval(look, 50);
|
|
144
|
+
addEventListener("blur", look);
|
|
145
|
+
addEventListener("focus", (event) => {
|
|
146
|
+
// The host's move into the frame (or a Tab) lands here: on to the page.
|
|
147
|
+
if (event.target === window && document.activeElement !== frame) {
|
|
148
|
+
frame.focus();
|
|
149
|
+
}
|
|
150
|
+
look();
|
|
151
|
+
});
|
|
152
|
+
document.addEventListener("visibilitychange", look);
|
|
153
|
+
tell(0);
|
|
154
|
+
|
|
155
|
+
addEventListener("message", (event) => {
|
|
156
|
+
const source = event.source;
|
|
157
|
+
if (source !== null && source === frame.contentWindow) {
|
|
158
|
+
const data = event.data;
|
|
159
|
+
// Never the shell's words in the page's mouth.
|
|
160
|
+
if (data !== null && typeof data === "object" && typeof data.method === "string" && data.method.indexOf(PREFIX) === 0) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
// What the shell knows now goes first: a call in a click's handler follows its activation.
|
|
164
|
+
look();
|
|
165
|
+
toHost(data);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (framed && source === window.parent && plan.origins.indexOf(event.origin) !== -1) {
|
|
169
|
+
const child = frame.contentWindow;
|
|
170
|
+
if (child !== null) {
|
|
171
|
+
child.postMessage(event.data, "*");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// The page's sealed address, renewed before it ends (P083): the page can't, the shell can.
|
|
177
|
+
if (plan.renew && typeof plan.renew.url === "string" && typeof plan.renew.expires === "number") {
|
|
178
|
+
const prefix = own + "/__porthole/f/";
|
|
179
|
+
let renewUrl = plan.renew.url;
|
|
180
|
+
const renewIn = (expires) => Math.max(30000, expires - Date.now() - 300000);
|
|
181
|
+
const renew = () => {
|
|
182
|
+
fetch(renewUrl, { credentials: "omit", cache: "no-store" })
|
|
183
|
+
.then((answer) => (answer.ok ? answer.json() : null))
|
|
184
|
+
.then((renewed) => {
|
|
185
|
+
if (!renewed || typeof renewed.base !== "string" || typeof renewed.expires !== "number" || renewed.base.indexOf(prefix) !== 0) {
|
|
186
|
+
setTimeout(renew, 60000);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
renewUrl = renewed.base + "__porthole/renew";
|
|
190
|
+
const child = frame.contentWindow;
|
|
191
|
+
if (child !== null) {
|
|
192
|
+
child.postMessage({ jsonrpc: "2.0", method: "porthole/files-renewed", params: { base: renewed.base } }, "*");
|
|
193
|
+
}
|
|
194
|
+
setTimeout(renew, renewIn(renewed.expires));
|
|
195
|
+
})
|
|
196
|
+
.catch(() => setTimeout(renew, 60000));
|
|
197
|
+
};
|
|
198
|
+
setTimeout(renew, renewIn(plan.renew.expires));
|
|
199
|
+
}
|
|
200
|
+
})();`;
|
|
201
|
+
export { SHELL_METHOD_PREFIX, SHELL_STATE_METHOD } from "../isolation/types.js";
|
|
202
|
+
/** JSON that is safe inside a `<script>`. */
|
|
203
|
+
const scriptJson = (value) => JSON.stringify(value)
|
|
204
|
+
.replace(/</gu, "\\u003c")
|
|
205
|
+
.replace(/\u2028/gu, "\\u2028")
|
|
206
|
+
.replace(/\u2029/gu, "\\u2029");
|
|
207
|
+
/** The shell's script for `options`: what its document runs, and what its CSP's hash names. */
|
|
208
|
+
export function frameShellScript(options) {
|
|
209
|
+
for (const origin of options.origins) {
|
|
210
|
+
const problem = hostOriginProblem(origin);
|
|
211
|
+
if (problem !== null) {
|
|
212
|
+
throw new TypeError(`frameShellScript: ${problem}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
let page;
|
|
216
|
+
try {
|
|
217
|
+
page = new URL(options.page);
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
throw new TypeError("frameShellScript: page is an absolute URL");
|
|
221
|
+
}
|
|
222
|
+
if (!page.pathname.startsWith("/__porthole/f/") || page.hash !== "") {
|
|
223
|
+
throw new TypeError("frameShellScript: page is under /__porthole/f/, with no fragment");
|
|
224
|
+
}
|
|
225
|
+
return FRAME_SHELL_RUNTIME.replace("__PLAN__", () => scriptJson({
|
|
226
|
+
origins: options.origins,
|
|
227
|
+
page: page.href,
|
|
228
|
+
sandbox: PAGE_SANDBOX_TOKENS.join(" "),
|
|
229
|
+
...(options.title === undefined ? {} : { title: options.title }),
|
|
230
|
+
...(options.renew === undefined
|
|
231
|
+
? {}
|
|
232
|
+
: { renew: { url: options.renew.url, expires: options.renew.expires } }),
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
/** The shell's document around its script: nothing else (its CSP allows that script alone). */
|
|
236
|
+
export function frameShellDocument(script) {
|
|
237
|
+
return `<!doctype html>
|
|
238
|
+
<html lang="en">
|
|
239
|
+
<head>
|
|
240
|
+
<meta charset="utf-8">
|
|
241
|
+
<meta name="color-scheme" content="light dark">
|
|
242
|
+
<meta name="referrer" content="no-referrer">
|
|
243
|
+
<title>Porthole</title>
|
|
244
|
+
</head>
|
|
245
|
+
<body>
|
|
246
|
+
<script>${script}</script>
|
|
247
|
+
</body>
|
|
248
|
+
</html>
|
|
249
|
+
`;
|
|
250
|
+
}
|
package/dist/shell/plan.js
CHANGED
|
@@ -38,7 +38,8 @@ export function shellPlan(config) {
|
|
|
38
38
|
themeVariables: Object.keys(config.theme.variables),
|
|
39
39
|
themeEvent: config.theme.event,
|
|
40
40
|
themeDocuments: config.theme.documents ?? "declared",
|
|
41
|
-
storage:
|
|
41
|
+
// An opaque origin has no storage: an opaque frame's, and a nested page's (P111).
|
|
42
|
+
storage: config.isolation === "opaque" || config.nested === true ? "memory" : "native",
|
|
42
43
|
maxErrorReports: config.limits.maxErrorReports,
|
|
43
44
|
maxPromptChars: config.limits.maxPromptChars,
|
|
44
45
|
maxCopyChars: MAX_COPY_CHARS,
|
package/dist/shell/runtime.js
CHANGED
|
@@ -68,6 +68,9 @@ export const SHELL_RUNTIME = String.raw `(() => {
|
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
+
// Whether the page touched storage, told to the host once (P111): yes, never what it keeps.
|
|
72
|
+
let storageTouched = false;
|
|
73
|
+
let tellStorage = () => {};
|
|
71
74
|
if (plan.storage === "memory") {
|
|
72
75
|
// An opaque origin has no storage: a map stands in, for as long as the frame lives.
|
|
73
76
|
const store = () => {
|
|
@@ -84,8 +87,18 @@ export const SHELL_RUNTIME = String.raw `(() => {
|
|
|
84
87
|
};
|
|
85
88
|
};
|
|
86
89
|
for (const name of ["localStorage", "sessionStorage"]) {
|
|
90
|
+
const kept = store();
|
|
87
91
|
try {
|
|
88
|
-
Object.defineProperty(window, name, {
|
|
92
|
+
Object.defineProperty(window, name, {
|
|
93
|
+
configurable: true,
|
|
94
|
+
get() {
|
|
95
|
+
if (!storageTouched) {
|
|
96
|
+
storageTouched = true;
|
|
97
|
+
tellStorage();
|
|
98
|
+
}
|
|
99
|
+
return kept;
|
|
100
|
+
},
|
|
101
|
+
});
|
|
89
102
|
} catch (_) {}
|
|
90
103
|
}
|
|
91
104
|
}
|
|
@@ -115,6 +128,8 @@ export const SHELL_RUNTIME = String.raw `(() => {
|
|
|
115
128
|
// takes the next load for that page, not for content that left). Anything else stays stopped:
|
|
116
129
|
// another path of the origin, a file that isn't a page, another origin.
|
|
117
130
|
let navigating = (_url) => {};
|
|
131
|
+
// A nested page's files' address renewed by its shell (P111), which the shell's message gives.
|
|
132
|
+
let adoptFiles = (_base) => false;
|
|
118
133
|
let filesBase = plan.files && typeof plan.files.base === "string" ? plan.files.base : null;
|
|
119
134
|
// Every address the page's files had (P083: a sealed one is renewed): what reports say as "./".
|
|
120
135
|
const filesBases = filesBase === null ? [] : [filesBase];
|
|
@@ -880,6 +895,11 @@ export const SHELL_RUNTIME = String.raw `(() => {
|
|
|
880
895
|
const filesSet = plan.files && plan.files.set && typeof plan.files.set === "object" ? { id: String(plan.files.set.id), version: String(plan.files.set.version) } : null;
|
|
881
896
|
const notice = (url, more) => post({ method: "porthole/navigating", params: Object.assign({ url: ownAddresses(url).slice(0, 4096) }, filesSet === null ? {} : { set: filesSet }, more) });
|
|
882
897
|
navigating = (url) => notice(url, {});
|
|
898
|
+
// The page used storage (P111): said once, as soon as the shell can speak; never keys or values.
|
|
899
|
+
tellStorage = () => post({ method: "porthole/storage-used", params: {} });
|
|
900
|
+
if (storageTouched) {
|
|
901
|
+
tellStorage();
|
|
902
|
+
}
|
|
883
903
|
if (arrived) {
|
|
884
904
|
notice(location.href, { arrived: true });
|
|
885
905
|
}
|
|
@@ -887,28 +907,38 @@ export const SHELL_RUNTIME = String.raw `(() => {
|
|
|
887
907
|
// it ends, the page moved to the new one in place (the same page, the same history entry). Its
|
|
888
908
|
// modules keep the address they loaded from; the server serves their own loads till the session
|
|
889
909
|
// ends.
|
|
910
|
+
const replace = History.prototype.replaceState;
|
|
911
|
+
// The files' origin, read from the address itself: a nested page's own origin is opaque (P111).
|
|
912
|
+
const filesOrigin = filesBase === null ? null : new NativeURL(filesBase).origin;
|
|
913
|
+
// A renewed sealed address (P083): one segment under the same origin's sealed prefix, checked
|
|
914
|
+
// through the platform's own functions (content may have replaced a prototype's, and the base
|
|
915
|
+
// decides where this page may go, ownPage); the page moves there in place.
|
|
916
|
+
const adopt = (base) => {
|
|
917
|
+
const prefix = filesOrigin + "/__porthole/f/";
|
|
918
|
+
if (filesBase === null || typeof base !== "string" || callOf(indexIn, base, [prefix]) !== 0 || callOf(indexIn, callOf(sliceOf, base, [prefix.length, -1]), ["/"]) !== -1 || callOf(sliceOf, base, [-1]) !== "/") {
|
|
919
|
+
return false;
|
|
920
|
+
}
|
|
921
|
+
const here = location.href;
|
|
922
|
+
if (here.indexOf(filesBase) === 0) {
|
|
923
|
+
try {
|
|
924
|
+
applyOf(replace, history, [history.state, "", base + here.slice(filesBase.length)]);
|
|
925
|
+
} catch (_) {}
|
|
926
|
+
}
|
|
927
|
+
filesBase = base;
|
|
928
|
+
filesBases.push(base);
|
|
929
|
+
return true;
|
|
930
|
+
};
|
|
931
|
+
adoptFiles = adopt;
|
|
890
932
|
if (filesBase !== null && typeof plan.files.expires === "number") {
|
|
891
933
|
const nativeFetch = window.fetch.bind(window);
|
|
892
|
-
const replace = History.prototype.replaceState;
|
|
893
934
|
const renewIn = (expires) => Math.max(30000, expires - Date.now() - 300000);
|
|
894
935
|
const renew = () => {
|
|
895
936
|
nativeFetch(filesBase + "__porthole/renew", { credentials: "omit", cache: "no-store" })
|
|
896
937
|
.then((answer) => (answer.ok ? answer.json() : null))
|
|
897
938
|
.then((renewed) => {
|
|
898
|
-
|
|
899
|
-
// and the base decides where this page may go (ownPage).
|
|
900
|
-
const prefix = location.origin + "/__porthole/f/";
|
|
901
|
-
if (!renewed || typeof renewed.base !== "string" || typeof renewed.expires !== "number" || callOf(indexIn, renewed.base, [prefix]) !== 0 || callOf(indexIn, callOf(sliceOf, renewed.base, [prefix.length, -1]), ["/"]) !== -1 || callOf(sliceOf, renewed.base, [-1]) !== "/") {
|
|
939
|
+
if (!renewed || typeof renewed.expires !== "number" || !adopt(renewed.base)) {
|
|
902
940
|
return;
|
|
903
941
|
}
|
|
904
|
-
const here = location.href;
|
|
905
|
-
if (here.indexOf(filesBase) === 0) {
|
|
906
|
-
try {
|
|
907
|
-
applyOf(replace, history, [history.state, "", renewed.base + here.slice(filesBase.length)]);
|
|
908
|
-
} catch (_) {}
|
|
909
|
-
}
|
|
910
|
-
filesBase = renewed.base;
|
|
911
|
-
filesBases.push(renewed.base);
|
|
912
942
|
setTimeout(renew, renewIn(renewed.expires));
|
|
913
943
|
})
|
|
914
944
|
.catch(() => setTimeout(renew, 60000));
|
|
@@ -1465,6 +1495,11 @@ export const SHELL_RUNTIME = String.raw `(() => {
|
|
|
1465
1495
|
case "ui/notifications/tool-input":
|
|
1466
1496
|
void render(params.arguments);
|
|
1467
1497
|
break;
|
|
1498
|
+
case "porthole/files-renewed":
|
|
1499
|
+
// From the page's parent only (the listener's check): a nested page's shell renewed its
|
|
1500
|
+
// files' sealed address, which this page can't (its loads aren't its origin's own, P111).
|
|
1501
|
+
adoptFiles(params.base);
|
|
1502
|
+
break;
|
|
1468
1503
|
case "ui/notifications/host-context-changed":
|
|
1469
1504
|
applyTheme(params);
|
|
1470
1505
|
break;
|