@botiverse/porthole 0.13.0 → 0.14.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/CHANGELOG.md +770 -0
- package/dist/annotate/types.d.ts +36 -4
- package/dist/annotate/types.js +6 -0
- package/dist/annotate/validate.d.ts +2 -1
- package/dist/annotate/validate.js +30 -6
- package/dist/check/page-source.js +1 -1
- package/dist/compile/policy.d.ts +7 -0
- package/dist/compile/policy.js +13 -1
- 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/host/create-host.d.ts +2 -0
- package/dist/host/create-host.js +78 -3
- package/dist/host/pass-on.d.ts +15 -0
- package/dist/host/pass-on.js +65 -0
- package/dist/host/types.d.ts +29 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/isolation/types.d.ts +8 -0
- package/dist/isolation/types.js +8 -0
- package/dist/platform/focus-guard.d.ts +71 -2
- package/dist/platform/focus-guard.js +187 -12
- package/dist/platform/types.d.ts +8 -1
- package/dist/platform/web.js +60 -4
- package/dist/presets/presets.d.ts +2 -0
- package/dist/presets/presets.js +1 -0
- package/dist/react/frame.d.ts +4 -3
- package/dist/server/files.js +19 -3
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +5 -0
- package/dist/server/naming.js +4 -0
- package/dist/server/nested.d.ts +59 -0
- package/dist/server/nested.js +96 -0
- package/dist/server/server.js +193 -23
- package/dist/server/types.d.ts +5 -0
- package/dist/shell/annotate.d.ts +4 -3
- package/dist/shell/annotate.js +179 -9
- 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.d.ts +6 -0
- package/dist/shell/runtime.js +60 -15
- package/docs/ADOPTING.md +85 -2
- package/docs/ARCHITECTURE.md +28 -0
- package/docs/DECISIONS.md +416 -1
- package/docs/DESIGN.md +21 -4
- package/docs/THREAT_MODEL.md +102 -5
- package/package.json +6 -5
- package/src/annotate/types.ts +32 -4
- package/src/annotate/validate.ts +34 -8
- package/src/compile/policy.ts +14 -1
- 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/host/create-host.ts +81 -4
- package/src/host/pass-on.ts +73 -0
- package/src/host/types.ts +27 -0
- package/src/index.ts +4 -1
- package/src/isolation/types.ts +12 -0
- package/src/platform/focus-guard.ts +258 -15
- package/src/platform/types.ts +8 -1
- package/src/platform/web.ts +81 -9
- package/src/presets/presets.ts +3 -0
- package/src/react/frame.tsx +4 -3
- package/src/server/files.ts +19 -3
- package/src/server/index.ts +14 -0
- package/src/server/naming.ts +4 -0
- package/src/server/nested.ts +116 -0
- package/src/server/server.ts +224 -21
- package/src/server/types.ts +5 -0
- package/src/shell/annotate.ts +179 -9
- package/src/shell/frame.ts +268 -0
- package/src/shell/plan.ts +2 -1
- package/src/shell/runtime.ts +61 -15
package/dist/shell/annotate.js
CHANGED
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
* functions as they were. Plain ES2020 like the rest of the shell; no template literals (it sits
|
|
5
5
|
* in one).
|
|
6
6
|
*
|
|
7
|
-
* The host drives it (`porthole/annotate.start|stop|show|focus|clear`) and it
|
|
8
|
-
* (`porthole/annotate.selected|unselected|cancelled|anchored|activated`):
|
|
7
|
+
* The host drives it (`porthole/annotate.start|stop|show|focus|clear|track|clearSelection`) and it
|
|
8
|
+
* reports (`porthole/annotate.selected|unselected|cancelled|anchored|activated|tracked`):
|
|
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
|
+
* the host's `clearSelection` (P115) takes the page's selection away and tells nothing back;
|
|
13
14
|
* - marks: each anchor is found in the content as it is now (its CSS selector, its text where it
|
|
14
15
|
* was, its text anywhere, its words with any spacing) and drawn over it, in a layer of the
|
|
15
16
|
* shell's own (a closed shadow root), again whenever the content changes; what can't be found
|
|
@@ -39,6 +40,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
39
40
|
cancelled: "porthole/annotate.cancelled",
|
|
40
41
|
anchored: "porthole/annotate.anchored",
|
|
41
42
|
activated: "porthole/annotate.activated",
|
|
43
|
+
track: "porthole/annotate.track",
|
|
44
|
+
tracked: "porthole/annotate.tracked",
|
|
45
|
+
clearSelection: "porthole/annotate.clearSelection",
|
|
42
46
|
};
|
|
43
47
|
// The DOM's own functions, taken before content runs.
|
|
44
48
|
const elementFromPoint = Document.prototype.elementFromPoint;
|
|
@@ -48,9 +52,13 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
48
52
|
const createElement = Document.prototype.createElement;
|
|
49
53
|
const createElementNS = Document.prototype.createElementNS;
|
|
50
54
|
const selectionOf = Document.prototype.getSelection;
|
|
55
|
+
const removeAllRanges = Selection.prototype.removeAllRanges;
|
|
51
56
|
const elementRect = Element.prototype.getBoundingClientRect;
|
|
52
57
|
const rangeRect = Range.prototype.getBoundingClientRect;
|
|
53
58
|
const rangeRects = Range.prototype.getClientRects;
|
|
59
|
+
const setStart = Range.prototype.setStart;
|
|
60
|
+
const setStartBefore = Range.prototype.setStartBefore;
|
|
61
|
+
const compareBoundaryPoints = Range.prototype.compareBoundaryPoints;
|
|
54
62
|
const attachShadow = Element.prototype.attachShadow;
|
|
55
63
|
const getAttribute = Element.prototype.getAttribute;
|
|
56
64
|
const listenOn = EventTarget.prototype.addEventListener;
|
|
@@ -389,7 +397,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
389
397
|
return shadow;
|
|
390
398
|
};
|
|
391
399
|
|
|
392
|
-
let mode = { elements: false, text: false };
|
|
400
|
+
let mode = { elements: false, text: false, hold: false };
|
|
393
401
|
let hover = null;
|
|
394
402
|
let marks = [];
|
|
395
403
|
// The markers as last drawn, for a click to find: { id, element }. Each mark keeps its
|
|
@@ -817,7 +825,76 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
817
825
|
}
|
|
818
826
|
};
|
|
819
827
|
|
|
820
|
-
|
|
828
|
+
// The mark the host follows (P112): its box at each animation frame where it moved, at most
|
|
829
|
+
// one every 16 ms, read through the platform's own frame clock (taken before content runs).
|
|
830
|
+
const nextFrame = window.requestAnimationFrame.bind(window);
|
|
831
|
+
let tracking = null;
|
|
832
|
+
let trackedBox = null;
|
|
833
|
+
let trackedAt = 0;
|
|
834
|
+
let trackFrame = 0;
|
|
835
|
+
const trackTick = () => {
|
|
836
|
+
trackFrame = 0;
|
|
837
|
+
if (tracking === null) {
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
const mark = marks.find((each) => each.id === tracking);
|
|
841
|
+
if (mark !== undefined && mark.found !== null) {
|
|
842
|
+
const rect = box(rectOf(mark.found));
|
|
843
|
+
const now = performance.now();
|
|
844
|
+
if (apart(trackedBox, rect) && now - trackedAt >= 16) {
|
|
845
|
+
trackedBox = rect;
|
|
846
|
+
trackedAt = now;
|
|
847
|
+
tell(M.tracked, { id: tracking, rect });
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
trackFrame = nextFrame(trackTick);
|
|
851
|
+
};
|
|
852
|
+
const track = (id) => {
|
|
853
|
+
tracking = typeof id === "string" ? id : null;
|
|
854
|
+
trackedBox = null;
|
|
855
|
+
if (tracking !== null && trackFrame === 0) {
|
|
856
|
+
trackFrame = nextFrame(trackTick);
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
|
|
860
|
+
// Where each found mark starts in the document, in order (P112): its place in a list the host
|
|
861
|
+
// sorts, offscreen ones too. Read now, every time results are told.
|
|
862
|
+
const startOf = (found) => {
|
|
863
|
+
const range = createRange.call(document);
|
|
864
|
+
if (found.range) {
|
|
865
|
+
setStart.call(range, found.range.startContainer, found.range.startOffset);
|
|
866
|
+
} else {
|
|
867
|
+
setStartBefore.call(range, found.element);
|
|
868
|
+
}
|
|
869
|
+
return range;
|
|
870
|
+
};
|
|
871
|
+
const ordersOf = () => {
|
|
872
|
+
const placed = [];
|
|
873
|
+
for (const mark of marks) {
|
|
874
|
+
if (mark.found !== null) {
|
|
875
|
+
try {
|
|
876
|
+
placed.push({ mark, start: startOf(mark.found) });
|
|
877
|
+
} catch (_) {}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
placed.sort((a, b) => {
|
|
881
|
+
try {
|
|
882
|
+
return compareBoundaryPoints.call(a.start, Range.START_TO_START, b.start);
|
|
883
|
+
} catch (_) {
|
|
884
|
+
return 0;
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
const orders = new Map();
|
|
888
|
+
placed.forEach((each, at) => orders.set(each.mark, at));
|
|
889
|
+
return orders;
|
|
890
|
+
};
|
|
891
|
+
const resultOf = (mark, orders) => {
|
|
892
|
+
if (mark.found === null) {
|
|
893
|
+
return { id: mark.id, state: "orphaned" };
|
|
894
|
+
}
|
|
895
|
+
const order = (orders || ordersOf()).get(mark);
|
|
896
|
+
return Object.assign({ id: mark.id, state: "attached", rect: box(rectOf(mark.found)) }, order === undefined ? {} : { order });
|
|
897
|
+
};
|
|
821
898
|
// Two boxes more than 0.5 px apart on any side (or one of them unknown).
|
|
822
899
|
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
900
|
// What the host was last told of each mark (mark.told), so a settle tells only what moved.
|
|
@@ -826,7 +903,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
826
903
|
if (!told || told.state !== result.state) {
|
|
827
904
|
return true;
|
|
828
905
|
}
|
|
829
|
-
return result.state === "attached" && apart(told.rect, result.rect);
|
|
906
|
+
return result.state === "attached" && (apart(told.rect, result.rect) || told.order !== result.order);
|
|
830
907
|
};
|
|
831
908
|
const report = (pairs) => {
|
|
832
909
|
if (pairs.length > 0) {
|
|
@@ -836,7 +913,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
836
913
|
tell(M.anchored, { results: pairs.map((pair) => pair.result) });
|
|
837
914
|
}
|
|
838
915
|
};
|
|
839
|
-
const resultsOf = (only) =>
|
|
916
|
+
const resultsOf = (only) => {
|
|
917
|
+
const orders = ordersOf();
|
|
918
|
+
return marks.map((mark) => ({ mark, result: resultOf(mark, orders) })).filter((pair) => !only || moved(pair.mark, pair.result));
|
|
919
|
+
};
|
|
840
920
|
// Finds every mark again; tells the host every result (after show, or when a state changed),
|
|
841
921
|
// only the marks that moved (once the content is quiet), or nothing.
|
|
842
922
|
const anchorAll = (all, quiet) => {
|
|
@@ -1142,6 +1222,66 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1142
1222
|
redraw();
|
|
1143
1223
|
}
|
|
1144
1224
|
}, true);
|
|
1225
|
+
|
|
1226
|
+
// Holding a block (P112, hold mode): a finger held still for 500 ms selects the block under
|
|
1227
|
+
// it, and the click its release makes is the shell's; every other tap is the page's, as is
|
|
1228
|
+
// any mouse. On the platform's own timer (taken before content runs).
|
|
1229
|
+
const later = window.setTimeout.bind(window);
|
|
1230
|
+
const cancelLater = window.clearTimeout.bind(window);
|
|
1231
|
+
const HOLD_MS = 500;
|
|
1232
|
+
const HOLD_SLOP = 10;
|
|
1233
|
+
let holding = null;
|
|
1234
|
+
let swallowUntil = 0;
|
|
1235
|
+
const endHold = () => {
|
|
1236
|
+
if (holding !== null) {
|
|
1237
|
+
cancelLater(holding.timer);
|
|
1238
|
+
holding = null;
|
|
1239
|
+
}
|
|
1240
|
+
};
|
|
1241
|
+
window.addEventListener("pointerdown", (event) => {
|
|
1242
|
+
if (!mode.hold || mode.elements || event.pointerType !== "touch" || !event.isPrimary) {
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
endHold();
|
|
1246
|
+
const x = event.clientX;
|
|
1247
|
+
const y = event.clientY;
|
|
1248
|
+
holding = {
|
|
1249
|
+
x,
|
|
1250
|
+
y,
|
|
1251
|
+
timer: later(() => {
|
|
1252
|
+
holding = null;
|
|
1253
|
+
if (!mode.hold) {
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
const element = under(x, y);
|
|
1257
|
+
const anchor = element === null ? null : elementAnchor(element, textIndex());
|
|
1258
|
+
if (anchor === null) {
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
swallowUntil = performance.now() + 2000;
|
|
1262
|
+
tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
|
|
1263
|
+
}, HOLD_MS),
|
|
1264
|
+
};
|
|
1265
|
+
}, true);
|
|
1266
|
+
window.addEventListener("pointermove", (event) => {
|
|
1267
|
+
if (holding !== null && event.pointerType === "touch" && Math.abs(event.clientX - holding.x) + Math.abs(event.clientY - holding.y) > HOLD_SLOP) {
|
|
1268
|
+
endHold();
|
|
1269
|
+
}
|
|
1270
|
+
}, true);
|
|
1271
|
+
for (const type of ["pointerup", "pointercancel"]) {
|
|
1272
|
+
window.addEventListener(type, endHold, true);
|
|
1273
|
+
}
|
|
1274
|
+
for (const type of ["click", "contextmenu"]) {
|
|
1275
|
+
window.addEventListener(type, (event) => {
|
|
1276
|
+
if (swallowUntil > performance.now()) {
|
|
1277
|
+
if (type === "click") {
|
|
1278
|
+
swallowUntil = 0;
|
|
1279
|
+
}
|
|
1280
|
+
event.preventDefault();
|
|
1281
|
+
event.stopImmediatePropagation();
|
|
1282
|
+
}
|
|
1283
|
+
}, true);
|
|
1284
|
+
}
|
|
1145
1285
|
window.addEventListener("click", (event) => {
|
|
1146
1286
|
if (!mode.elements) {
|
|
1147
1287
|
return;
|
|
@@ -1170,7 +1310,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1170
1310
|
tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
|
|
1171
1311
|
}, true);
|
|
1172
1312
|
window.addEventListener("keydown", (event) => {
|
|
1173
|
-
if (
|
|
1313
|
+
if (event.key !== "Escape") {
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
if (mode.elements) {
|
|
1174
1317
|
event.preventDefault();
|
|
1175
1318
|
event.stopImmediatePropagation();
|
|
1176
1319
|
mode = { elements: false, text: mode.text };
|
|
@@ -1178,6 +1321,12 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1178
1321
|
setCursor(false);
|
|
1179
1322
|
draw();
|
|
1180
1323
|
tell(M.cancelled, {});
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
// Selecting text (P112): the person wants out too. The key stays the page's (a page that
|
|
1327
|
+
// closes its own dialog on Escape still does); the host decides what ends.
|
|
1328
|
+
if (mode.text) {
|
|
1329
|
+
tell(M.cancelled, {});
|
|
1181
1330
|
}
|
|
1182
1331
|
}, true);
|
|
1183
1332
|
|
|
@@ -1244,7 +1393,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1244
1393
|
const params = data.params !== null && typeof data.params === "object" ? data.params : {};
|
|
1245
1394
|
switch (data.method) {
|
|
1246
1395
|
case M.start:
|
|
1247
|
-
mode = { elements: params.elements === true, text: params.text === true };
|
|
1396
|
+
mode = { elements: params.elements === true, text: params.text === true, hold: params.elements === "hold" };
|
|
1248
1397
|
hover = null;
|
|
1249
1398
|
reported = null;
|
|
1250
1399
|
reportedBox = null;
|
|
@@ -1276,7 +1425,28 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
1276
1425
|
case M.focus:
|
|
1277
1426
|
focus(String(params.id));
|
|
1278
1427
|
break;
|
|
1428
|
+
case M.track:
|
|
1429
|
+
track(params.id);
|
|
1430
|
+
break;
|
|
1431
|
+
case M.clearSelection: {
|
|
1432
|
+
// The host used the person's selection (P115): it goes, and with it what the shell
|
|
1433
|
+
// held of it (a report waiting to settle, the one last told, a scroll's follow-up), so
|
|
1434
|
+
// nothing is told back: the host knows, and the selectionchange this makes finds
|
|
1435
|
+
// nothing to report. Marks, a pick and the mode stay as they are. With nothing
|
|
1436
|
+
// selected, nothing changes.
|
|
1437
|
+
cancelLater(selectionTimer);
|
|
1438
|
+
selectionTimer = 0;
|
|
1439
|
+
reported = null;
|
|
1440
|
+
reportedBox = null;
|
|
1441
|
+
selectionScrolled = false;
|
|
1442
|
+
const selection = selectionOf.call(document);
|
|
1443
|
+
if (selection !== null) {
|
|
1444
|
+
removeAllRanges.call(selection);
|
|
1445
|
+
}
|
|
1446
|
+
break;
|
|
1447
|
+
}
|
|
1279
1448
|
case M.clear:
|
|
1449
|
+
track(null);
|
|
1280
1450
|
pendingFocus = null;
|
|
1281
1451
|
marks = [];
|
|
1282
1452
|
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.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a draft holds back (P117), spliced into the runtime: a tag start the content was cut in
|
|
3
|
+
* (a trailing `<`, `</`, or `<` and a tag's name or attributes, with no `>` yet) is left out until
|
|
4
|
+
* more comes. Parsed as it is, a cut right after `<` or `</` draws them as text.
|
|
5
|
+
*/
|
|
6
|
+
export declare const HELD_BACK: string;
|
|
1
7
|
/**
|
|
2
8
|
* The shell's runtime: the code that runs first in the frame, before any content. Plain ES2020,
|
|
3
9
|
* no imports and no build step, so every engine (a browser frame now, a native web view later)
|