@dasl/tile-loader 3.2.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +680 -37
- package/dist/at.d.ts.map +1 -1
- package/dist/at.js +11 -3
- package/dist/at.js.map +1 -1
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +6 -2
- package/dist/car.js.map +1 -1
- package/dist/guard.d.ts +110 -10
- package/dist/guard.d.ts.map +1 -1
- package/dist/guard.js +647 -59
- package/dist/guard.js.map +1 -1
- package/dist/index.d.ts +176 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +265 -35
- package/dist/index.js.map +1 -1
- package/dist/lib/tab-order.d.ts +16 -0
- package/dist/lib/tab-order.d.ts.map +1 -0
- package/dist/lib/tab-order.js +66 -0
- package/dist/lib/tab-order.js.map +1 -0
- package/dist/mount.d.ts.map +1 -1
- package/dist/mount.js +4 -1
- package/dist/mount.js.map +1 -1
- package/package.json +2 -2
- package/src/at.ts +11 -3
- package/src/car.ts +6 -2
- package/src/guard.ts +701 -55
- package/src/index.ts +300 -35
- package/src/lib/tab-order.ts +66 -0
- package/src/mount.ts +4 -1
package/src/index.ts
CHANGED
|
@@ -37,7 +37,7 @@ The tile-loading architecture has four levels that all communicate together:
|
|
|
37
37
|
|
|
38
38
|
import { el } from "./lib/el.js";
|
|
39
39
|
import { TileLoadError } from "./lib/errors.js";
|
|
40
|
-
import { tileResponseHeaders, dockPath } from "@dasl/tile-lexicon";
|
|
40
|
+
import { tileResponseHeaders, dockPath, tileSandbox, refusableFeatures, isCidLabel } from "@dasl/tile-lexicon";
|
|
41
41
|
import type { AnyMasl, MaslHeaders, MaslResponse } from "@dasl/tile-lexicon";
|
|
42
42
|
|
|
43
43
|
export interface MothershipConfig {
|
|
@@ -57,6 +57,23 @@ export interface MothershipConfig {
|
|
|
57
57
|
namedOrigin?: string;
|
|
58
58
|
/** @deprecated The old name of `namedOrigin`. It goes in the next major version. */
|
|
59
59
|
tileHost?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Whether a tile runs on the origin its own content names: `<cid>.<base>`,
|
|
62
|
+
* where `<cid>` is the CID of the tile's manifest, as its loader computed
|
|
63
|
+
* it from the bytes it loaded (the record's `tile` on AT, the header of a
|
|
64
|
+
* .tile file). Such an origin is kept: what the tile stores there it finds
|
|
65
|
+
* again the next time it runs, on any page of the same site. It is shared
|
|
66
|
+
* by every run of that tile on that site, and a new version of the tile,
|
|
67
|
+
* which has another manifest, starts on another origin, with nothing.
|
|
68
|
+
*
|
|
69
|
+
* Anyone can name such an origin, so the loading server's runtime holds it
|
|
70
|
+
* to the one tile whose manifest it is named by, and the loader runs a
|
|
71
|
+
* tile there only when the loading server says that its runtime does. A
|
|
72
|
+
* tile its loader has no manifest bytes for (a webxdc tile, one in memory)
|
|
73
|
+
* runs on an origin made for the load, as without this; `tile.ephemeral`
|
|
74
|
+
* says which. False when left out. Not with `namedOrigin`.
|
|
75
|
+
*/
|
|
76
|
+
cidOrigins?: boolean;
|
|
60
77
|
/**
|
|
61
78
|
* Whether the shuttle spends the renderer's WebRTC budget before the tile
|
|
62
79
|
* loads (the default). A host that blocks WebRTC itself (an Electron app with
|
|
@@ -124,6 +141,14 @@ export interface TileIdentity {
|
|
|
124
141
|
manifestCid?: string;
|
|
125
142
|
/** Whether it was checked against a CID its host expected. */
|
|
126
143
|
pinned?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* The bytes of its manifest as its source holds them (DRISL), and their
|
|
146
|
+
* CID: the record's `tile` on AT (`manifestCid`), a .tile file's header
|
|
147
|
+
* (`cid`). What names the origin of a tile whose mothership runs tiles
|
|
148
|
+
* on the origin named by their CID (`cidOrigins`), and what the runtime
|
|
149
|
+
* there is handed to check it against.
|
|
150
|
+
*/
|
|
151
|
+
manifestBytes?: { cid: string; bytes: Uint8Array };
|
|
127
152
|
}
|
|
128
153
|
export interface TileLoader {
|
|
129
154
|
load (url: string, mothership: TileMothership, options?: TileLoadOptions): Promise<Tile | false | undefined>;
|
|
@@ -195,6 +220,30 @@ const asList = (msg: unknown): unknown[] => (Array.isArray(msg) ? msg.slice(0, L
|
|
|
195
220
|
// (MothershipConfig.startDeadline). A guess: on a busy machine a tile with
|
|
196
221
|
// nothing wrong with it has been seen to take eight seconds.
|
|
197
222
|
const START_DEADLINE_MS = 15000;
|
|
223
|
+
// The sandbox of the frame renderContent() makes, when its host says none:
|
|
224
|
+
// what a tile needs to run, and forms to submit to itself. No dialogs and no
|
|
225
|
+
// downloads, which the runtime would grant (see renderContent()).
|
|
226
|
+
const FRAME_SANDBOX = 'allow-scripts allow-same-origin allow-forms';
|
|
227
|
+
// What the frame renderContent() makes refuses a tile when its host says
|
|
228
|
+
// nothing of it: synchronous requests. One holds up the thread the tile
|
|
229
|
+
// shares with its runtime and gets the tile nothing: its own files never
|
|
230
|
+
// come that way, since Chrome and Safari do not hand such a request to the
|
|
231
|
+
// tile's worker, and Firefox hands it to a worker whose answer has to come
|
|
232
|
+
// through that same thread. What is lost is a synchronous request for a
|
|
233
|
+
// blob: or data: address.
|
|
234
|
+
const FRAME_REFUSE: readonly string[] = ['sync-xhr'];
|
|
235
|
+
/**
|
|
236
|
+
* The features a host can refuse a tile (`FrameOptions.refuse`), by name:
|
|
237
|
+
* `refusableFeatures` in @dasl/tile-lexicon. They are those of its user's
|
|
238
|
+
* devices and attention that the runtime hands on to a tile whose host's
|
|
239
|
+
* frame allows them, and those a browser gives every frame unasked
|
|
240
|
+
* (picture-in-picture, the gamepad, synchronous requests, the client hints
|
|
241
|
+
* and more), which a tile keeps unless they are refused. `refuse: REFUSABLE`
|
|
242
|
+
* refuses all of them. Chrome knows every name; Firefox and Safari go by the
|
|
243
|
+
* names they know and pass over the others, so the same list refuses less
|
|
244
|
+
* there (the README says what, in each).
|
|
245
|
+
*/
|
|
246
|
+
export const REFUSABLE: readonly string[] = refusableFeatures;
|
|
198
247
|
// A timer is set for no longer than this: a longer wait comes round to none.
|
|
199
248
|
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
200
249
|
// A frame that is out of the document when its deadline passes is looked at
|
|
@@ -230,7 +279,9 @@ export interface StoppedEvent extends Event {
|
|
|
230
279
|
* - `version`: the dock named a generation of the runtime that this loader
|
|
231
280
|
* does not speak (`generation`): the loader in the page and the loading
|
|
232
281
|
* server are too far apart. Nothing is stopped here either, but little
|
|
233
|
-
* can be expected of the tile.
|
|
282
|
+
* can be expected of the tile. For a tile on the origin named by its CID
|
|
283
|
+
* (`cidOrigins`), also: the dock did not say that its runtime holds such
|
|
284
|
+
* an origin to its tile. That tile is not run there.
|
|
234
285
|
* - `worker`: the runtime could not start the tile's service worker. Some
|
|
235
286
|
* browsers refuse one to a frame of another site once the user blocks
|
|
236
287
|
* cross-site storage.
|
|
@@ -238,6 +289,10 @@ export interface StoppedEvent extends Event {
|
|
|
238
289
|
* - `content`: the tile's root document is not to be had: resolving it
|
|
239
290
|
* failed, or the tile's worker gave up waiting for it (after a minute: a
|
|
240
291
|
* deadline shorter than that has reported it first, as `timeout`).
|
|
292
|
+
* - `integrity`: the runtime on the origin named by the tile's CID refused
|
|
293
|
+
* the manifest it was handed: it is not the one that CID names, or not
|
|
294
|
+
* one it could read (a .tile file whose header is not in DRISL's one
|
|
295
|
+
* form). Nothing is served there.
|
|
241
296
|
*
|
|
242
297
|
* A start (a frame the tile is attached to, each time it loads) fires one at
|
|
243
298
|
* most. `worker` and `unsupported` are the runtime's own account, which
|
|
@@ -248,7 +303,7 @@ export interface StoppedEvent extends Event {
|
|
|
248
303
|
* tile is there; tiles that share a named origin can do it to one another.
|
|
249
304
|
*/
|
|
250
305
|
export interface TileErrorEvent extends Event {
|
|
251
|
-
reason: 'timeout' | 'version' | 'worker' | 'unsupported' | 'content';
|
|
306
|
+
reason: 'timeout' | 'version' | 'worker' | 'unsupported' | 'content' | 'integrity';
|
|
252
307
|
/** With `timeout`: what the tile was still waiting for. */
|
|
253
308
|
stage?: 'dock' | 'shuttle' | 'worker' | 'content';
|
|
254
309
|
/** With `version`: the generation the dock named, when it named a number. */
|
|
@@ -343,6 +398,22 @@ interface Usage {
|
|
|
343
398
|
logged: number; // lines in the console
|
|
344
399
|
}
|
|
345
400
|
|
|
401
|
+
/**
|
|
402
|
+
* What a host can ask of the frame `renderContent()` makes for a tile. Each
|
|
403
|
+
* is checked, and one the loader cannot honour throws: a host is never left
|
|
404
|
+
* believing in a frame it does not have.
|
|
405
|
+
*/
|
|
406
|
+
export interface FrameOptions {
|
|
407
|
+
/** The frame's sandbox attribute in full: `allow-scripts allow-same-origin allow-forms` when left out. */
|
|
408
|
+
sandbox?: string;
|
|
409
|
+
/** Whether the frame is credentialless: `true` or `false`; when left out, it is on an origin made for the load, and not on a named origin or the origin named by the tile's CID (`tile.ephemeral`). */
|
|
410
|
+
credentialless?: boolean;
|
|
411
|
+
/** The frame's tabindex, a whole number from -32767 to 32767: `-1` has the Tab key pass over the tile. None when left out. The focus guard takes a frame it guards out of the tab order whatever this says (see `tabOrder` in @dasl/tile-loader/guard). */
|
|
412
|
+
tabindex?: number;
|
|
413
|
+
/** Features to take from the tile, by name (`['sync-xhr', 'picture-in-picture', 'gamepad']`), among those of `REFUSABLE`: the list in full, as `sandbox` is the attribute in full. `['sync-xhr']` when left out; `[]` refuses nothing. */
|
|
414
|
+
refuse?: readonly string[];
|
|
415
|
+
}
|
|
416
|
+
|
|
346
417
|
/** A tiles-protocol message from a tile: `protocol` ("data"), `kind` ("payload", "ready"), and its `payload`. The mothership attaches no meaning to any of them. */
|
|
347
418
|
export interface ProtocolEvent extends Event {
|
|
348
419
|
protocol: string;
|
|
@@ -381,6 +452,11 @@ export class TileMothership {
|
|
|
381
452
|
constructor (conf: MothershipConfig = {}) {
|
|
382
453
|
this.#conf = conf;
|
|
383
454
|
this.#budget = budgetFrom(conf.budget);
|
|
455
|
+
// An origin named by a tile's CID is its tile's: no configuration names
|
|
456
|
+
// it, and a mothership runs a tile either there or on the one it names.
|
|
457
|
+
const named = this.#namedHost();
|
|
458
|
+
if (named && conf.cidOrigins) throw new Error('A mothership runs tiles on a named origin (namedOrigin) or on the origins named by their CID (cidOrigins), not both.');
|
|
459
|
+
if (named && isCidLabel(named.split('.')[0].toLowerCase())) throw new Error(`The origin named by a tile's CID is that tile's, and is not named: ${named}. Run tiles there with cidOrigins.`);
|
|
384
460
|
}
|
|
385
461
|
// What init() has listening, kept so that destroy() can take it away.
|
|
386
462
|
#listener: ((ev: MessageEvent) => void) | undefined;
|
|
@@ -412,6 +488,9 @@ export class TileMothership {
|
|
|
412
488
|
// The worker gave up on a request of its tile's that we never
|
|
413
489
|
// answered. Before the tile is up, that is its root document.
|
|
414
490
|
if (ev.data.code === 'unanswered') tile.failed('content');
|
|
491
|
+
// The runtime on the origin named by the tile's CID refused what it
|
|
492
|
+
// was handed for the tile's manifest.
|
|
493
|
+
if (ev.data.code === 'integrity') tile.failed('integrity');
|
|
415
494
|
}
|
|
416
495
|
else if (action === TILES_ERROR) {
|
|
417
496
|
this.#log(used, 'error', `[S:${id}]`, ...asList(ev.data.msg));
|
|
@@ -434,12 +513,20 @@ export class TileMothership {
|
|
|
434
513
|
// is in the generation it names; a number we do not know is said to
|
|
435
514
|
// the host, and the rest is left to happen or not.
|
|
436
515
|
const generation = ev.data.payload?.generation;
|
|
437
|
-
|
|
438
|
-
|
|
516
|
+
// And a tile on the origin named by its CID is run there only by a
|
|
517
|
+
// runtime that holds that origin to it, which the dock says.
|
|
518
|
+
tile.dockSaid(ev.data.payload?.cid === true);
|
|
519
|
+
if (!GENERATIONS.includes(generation)) tile.failed('version', typeof generation === 'number' ? { generation } : {});
|
|
520
|
+
else if (tile.cidOrigin && !tile.bound) tile.failed('version');
|
|
521
|
+
else tile.awaits('shuttle');
|
|
439
522
|
}
|
|
440
523
|
else if (action === RCV_SHUTTLE_READY) {
|
|
524
|
+
// The runtime on the origin named by the tile's CID is handed the
|
|
525
|
+
// manifest that CID names, and nothing if it does not hold it.
|
|
526
|
+
const manifest = tile.cidOrigin;
|
|
527
|
+
if (manifest && !tile.bound) return tile.failed('version');
|
|
441
528
|
tile.awaits('worker');
|
|
442
|
-
tile.post(SND_WORKER_LOAD, { id });
|
|
529
|
+
tile.post(SND_WORKER_LOAD, { id, ...(manifest ? { manifest } : {}) });
|
|
443
530
|
}
|
|
444
531
|
else if (action === RCV_WORKER_READY) {
|
|
445
532
|
tile.dispatchEvent(new Event('load'));
|
|
@@ -660,7 +747,7 @@ export class TileMothership {
|
|
|
660
747
|
return {
|
|
661
748
|
id: tile.runtimeId,
|
|
662
749
|
webrtcGuard: this.#conf.webrtcGuard ?? 'exhaust',
|
|
663
|
-
ephemeral:
|
|
750
|
+
ephemeral: tile.ephemeral,
|
|
664
751
|
...(sizing ? { sizing } : {}),
|
|
665
752
|
};
|
|
666
753
|
}
|
|
@@ -670,10 +757,25 @@ export class TileMothership {
|
|
|
670
757
|
if (typeof ms !== 'number' || Number.isNaN(ms) || ms < 0) return START_DEADLINE_MS;
|
|
671
758
|
return Number.isFinite(ms) ? Math.min(ms, MAX_TIMER_MS) : 0;
|
|
672
759
|
}
|
|
673
|
-
/**
|
|
760
|
+
/**
|
|
761
|
+
* Whether tiles get an origin of their own for every load (no
|
|
762
|
+
* `namedOrigin`). With `cidOrigins`, those that have a CID to be named by
|
|
763
|
+
* do not: see `tile.ephemeral`.
|
|
764
|
+
*/
|
|
674
765
|
get ephemeral (): boolean {
|
|
675
766
|
return !this.#namedHost();
|
|
676
767
|
}
|
|
768
|
+
/**
|
|
769
|
+
* @internal The host of the origin named by a tile's CID: when tiles run
|
|
770
|
+
* there (`cidOrigins`), the tile has a CID to be named by, and the loading
|
|
771
|
+
* server's tile origins are under its own host (`load.<base>`).
|
|
772
|
+
*/
|
|
773
|
+
cidHost (tile: Tile): string | undefined {
|
|
774
|
+
const cid = tile.manifestBytes?.cid;
|
|
775
|
+
if (this.#conf.cidOrigins !== true || !isCidLabel(cid)) return undefined;
|
|
776
|
+
const { host } = new URL(this.getLoadOrigin());
|
|
777
|
+
return host.startsWith(LOAD_LABEL) ? `${cid}.${host.slice(LOAD_LABEL.length)}` : undefined;
|
|
778
|
+
}
|
|
677
779
|
// The named origin's host, if the configuration names one.
|
|
678
780
|
#namedHost (): string | undefined {
|
|
679
781
|
const named = this.#conf.namedOrigin ?? this.#conf.tileHost;
|
|
@@ -780,11 +882,13 @@ export class TileMothership {
|
|
|
780
882
|
* loading server would make it, so an address is for one frame. Named
|
|
781
883
|
* here, the origin is one the page knows, which is what lets it be wiped
|
|
782
884
|
* once the frame its tile ran in is gone (see Tile.close()); and it saves
|
|
783
|
-
* the redirect in which the loading server would have picked it.
|
|
885
|
+
* the redirect in which the loading server would have picked it. With
|
|
886
|
+
* `cidOrigins`, for a `tile` that has a CID to be named by, it is the
|
|
887
|
+
* origin that CID names.
|
|
784
888
|
*/
|
|
785
|
-
getLoadSource (): string {
|
|
889
|
+
getLoadSource (tile?: Tile): string {
|
|
786
890
|
const url = new URL(dockPath, this.getLoadOrigin());
|
|
787
|
-
const named = this.#namedHost() ?? this.#freshHost();
|
|
891
|
+
const named = this.#namedHost() ?? (tile && this.cidHost(tile)) ?? this.#freshHost();
|
|
788
892
|
if (named) url.searchParams.set('tile', named);
|
|
789
893
|
return url.href;
|
|
790
894
|
}
|
|
@@ -917,11 +1021,52 @@ export class Tile extends EventTarget {
|
|
|
917
1021
|
get pinned (): boolean {
|
|
918
1022
|
return this.#identity.pinned === true;
|
|
919
1023
|
}
|
|
1024
|
+
/** @internal The bytes of its manifest, and their CID, where its loader has them. */
|
|
1025
|
+
get manifestBytes (): TileIdentity['manifestBytes'] {
|
|
1026
|
+
return this.#identity.manifestBytes;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Whether the origin this tile runs on is made for one load and wiped when
|
|
1030
|
+
* it is closed. It is unless its mothership names an origin for its tiles
|
|
1031
|
+
* (`namedOrigin`), or runs them on the origin named by their CID
|
|
1032
|
+
* (`cidOrigins`) and this tile has a CID to be named by: such an origin is
|
|
1033
|
+
* kept, for the tile to find what it stored there again.
|
|
1034
|
+
*/
|
|
1035
|
+
get ephemeral (): boolean {
|
|
1036
|
+
return this.#mothership.ephemeral && !this.#mothership.cidHost(this);
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* @internal The bytes of the manifest that names this tile's origin, when
|
|
1040
|
+
* it runs on the origin named by its CID: what that origin's runtime is
|
|
1041
|
+
* handed, to check it against its name.
|
|
1042
|
+
*/
|
|
1043
|
+
get cidOrigin (): Uint8Array | undefined {
|
|
1044
|
+
return this.#mothership.cidHost(this) ? this.#identity.manifestBytes?.bytes : undefined;
|
|
1045
|
+
}
|
|
1046
|
+
// Whether the dock of the frame the tile is in now said that its runtime
|
|
1047
|
+
// holds an origin named by a CID to its tile.
|
|
1048
|
+
#bound = false;
|
|
1049
|
+
/** @internal What the dock said of itself: whether its runtime holds an origin named by a CID to its tile. */
|
|
1050
|
+
dockSaid (bound: boolean) {
|
|
1051
|
+
this.#bound = bound;
|
|
1052
|
+
}
|
|
1053
|
+
/** @internal Whether it said so. */
|
|
1054
|
+
get bound (): boolean {
|
|
1055
|
+
return this.#bound;
|
|
1056
|
+
}
|
|
920
1057
|
async resolvePath (path: string): Promise<MaslResponse> {
|
|
921
1058
|
const u = new URL(`fake:${path}`);
|
|
922
1059
|
return this.#pathLoader.resolvePath(u.pathname);
|
|
923
1060
|
}
|
|
924
|
-
|
|
1061
|
+
/**
|
|
1062
|
+
* A card for the tile: what its manifest says of it, and its pictures,
|
|
1063
|
+
* which runs the tile when it is clicked. `options.contentHeight` presets
|
|
1064
|
+
* the height of the tile's frame, and `options.frame` is what the host
|
|
1065
|
+
* asks of that frame, as with `renderContent()`: it is checked now, and
|
|
1066
|
+
* the card is refused if the frame could not be made as asked.
|
|
1067
|
+
*/
|
|
1068
|
+
async renderCard (options?: { contentHeight?: number, frame?: FrameOptions }): Promise<HTMLElement> {
|
|
1069
|
+
this.#frameAttributes(options?.frame);
|
|
925
1070
|
const card = el('div', { style: {
|
|
926
1071
|
border: '1px solid lightgrey',
|
|
927
1072
|
'border-radius': '3px',
|
|
@@ -931,7 +1076,8 @@ export class Tile extends EventTarget {
|
|
|
931
1076
|
const tileRenderer = await this.renderContent(
|
|
932
1077
|
options?.contentHeight ||
|
|
933
1078
|
this.#manifest?.sizing?.height ||
|
|
934
|
-
Math.max(card.offsetHeight, 300)
|
|
1079
|
+
Math.max(card.offsetHeight, 300),
|
|
1080
|
+
options?.frame,
|
|
935
1081
|
);
|
|
936
1082
|
card.replaceWith(tileRenderer);
|
|
937
1083
|
});
|
|
@@ -985,7 +1131,7 @@ export class Tile extends EventTarget {
|
|
|
985
1131
|
* origin.
|
|
986
1132
|
*/
|
|
987
1133
|
getLoadSource (): string {
|
|
988
|
-
this.#source ??= this.#mothership.getLoadSource();
|
|
1134
|
+
this.#source ??= this.#mothership.getLoadSource(this);
|
|
989
1135
|
return this.#source;
|
|
990
1136
|
}
|
|
991
1137
|
/**
|
|
@@ -1008,9 +1154,24 @@ export class Tile extends EventTarget {
|
|
|
1008
1154
|
* with it. A tile whose frame is gone says nothing.
|
|
1009
1155
|
*/
|
|
1010
1156
|
post (action: string, payload?: unknown) {
|
|
1157
|
+
this.#track();
|
|
1011
1158
|
const win = this.#frame?.deref()?.contentWindow;
|
|
1012
1159
|
win?.postMessage({ id: this.#runtimeId, action, payload }, this.#mothership.getLoadOrigin());
|
|
1013
1160
|
}
|
|
1161
|
+
// The frame's window is how the tile's messages are known for its own. It
|
|
1162
|
+
// is there once the frame is in a document, and it is a new one if the
|
|
1163
|
+
// frame is taken out and put back, so it is taken down at each load, and
|
|
1164
|
+
// whenever something is posted to it: the dock answers the first thing it
|
|
1165
|
+
// is told, which can come before its frame's load event (what the host
|
|
1166
|
+
// sends the tile meanwhile, its size say), with its word on what its
|
|
1167
|
+
// runtime holds (see RCV_DOCK_READY), and that must not go unheard.
|
|
1168
|
+
#track () {
|
|
1169
|
+
const win = this.#frame?.deref()?.contentWindow as object | null | undefined;
|
|
1170
|
+
if (!win || win === this.#window) return;
|
|
1171
|
+
if (this.#window) this.#mothership.undock(this.#window);
|
|
1172
|
+
this.#window = win;
|
|
1173
|
+
this.#mothership.dock(win, this);
|
|
1174
|
+
}
|
|
1014
1175
|
// --- Starting
|
|
1015
1176
|
// What the host hears of a tile that does not start. A start is a frame
|
|
1016
1177
|
// the tile is attached to, and each time that frame loads (which is when
|
|
@@ -1124,19 +1285,9 @@ export class Tile extends EventTarget {
|
|
|
1124
1285
|
this.#letGo();
|
|
1125
1286
|
this.#runtimeId = crypto.randomUUID(); // we might want to make that pluggable
|
|
1126
1287
|
this.#frame = new WeakRef(ifr);
|
|
1127
|
-
|
|
1128
|
-
// It is there once the frame is in a document, and it is a new one if
|
|
1129
|
-
// the frame is taken out and put back, so it is taken down at each load.
|
|
1130
|
-
const track = () => {
|
|
1131
|
-
const win = ifr.contentWindow as object | null;
|
|
1132
|
-
if (!win || win === this.#window) return;
|
|
1133
|
-
if (this.#window) this.#mothership.undock(this.#window);
|
|
1134
|
-
this.#window = win;
|
|
1135
|
-
this.#mothership.dock(win, this);
|
|
1136
|
-
};
|
|
1137
|
-
track();
|
|
1288
|
+
this.#track();
|
|
1138
1289
|
this.#onFrameLoad = () => {
|
|
1139
|
-
track();
|
|
1290
|
+
this.#track();
|
|
1140
1291
|
// The frame has loaded: the time a start is given runs from when the
|
|
1141
1292
|
// runtime in it is told to load the tile (see start()).
|
|
1142
1293
|
clearTimeout(this.#deadline);
|
|
@@ -1159,6 +1310,7 @@ export class Tile extends EventTarget {
|
|
|
1159
1310
|
if (this.#window) this.#mothership.undock(this.#window);
|
|
1160
1311
|
this.#frame = this.#window = this.#onFrameLoad = this.#runtimeId = undefined;
|
|
1161
1312
|
this.#started = false;
|
|
1313
|
+
this.#bound = false;
|
|
1162
1314
|
}
|
|
1163
1315
|
/**
|
|
1164
1316
|
* Closes the tile: its frame is removed from the document, the tile
|
|
@@ -1171,7 +1323,8 @@ export class Tile extends EventTarget {
|
|
|
1171
1323
|
* reach is what the tile left in the browser. Every tile runs on an origin
|
|
1172
1324
|
* made for that one load, and its worker's registration, and anything the
|
|
1173
1325
|
* tile stored, would stay there for good. `close()` has that origin wiped
|
|
1174
|
-
* (not a named origin, `namedOrigin`,
|
|
1326
|
+
* (not a named origin, `namedOrigin`, nor the origin named by the tile's
|
|
1327
|
+
* CID, `cidOrigins`: both are kept on purpose, see `tile.ephemeral`).
|
|
1175
1328
|
*
|
|
1176
1329
|
* It does not matter what became of the frame first, nor what the tile
|
|
1177
1330
|
* did. The frame's address names the origin (getLoadSource() does, and so
|
|
@@ -1189,9 +1342,10 @@ export class Tile extends EventTarget {
|
|
|
1189
1342
|
* CLOSE_MS to answer. Out of the document, there is no one to ask.
|
|
1190
1343
|
*
|
|
1191
1344
|
* What it resolves to: true when the origin was wiped, and when the tile
|
|
1192
|
-
* never started (there was nothing to wipe); false for a named origin
|
|
1193
|
-
*
|
|
1194
|
-
* logged as a
|
|
1345
|
+
* never started (there was nothing to wipe); false for a named origin and
|
|
1346
|
+
* for the origin named by the tile's CID, which are kept, and when the
|
|
1347
|
+
* wipe could not be done or was not confirmed, which is also logged as a
|
|
1348
|
+
* warning.
|
|
1195
1349
|
*/
|
|
1196
1350
|
close (): Promise<boolean> {
|
|
1197
1351
|
const ifr = this.#frame?.deref();
|
|
@@ -1217,7 +1371,7 @@ export class Tile extends EventTarget {
|
|
|
1217
1371
|
// There is something to wipe once a runtime was started on an origin
|
|
1218
1372
|
// made for this load: nothing was put there before that.
|
|
1219
1373
|
const started = this.#started;
|
|
1220
|
-
const wipe = this
|
|
1374
|
+
const wipe = this.ephemeral && started;
|
|
1221
1375
|
// Which origin, the frame's address says, when it is one of ours.
|
|
1222
1376
|
const origin = wipe ? this.#mothership.originNamedIn(ifr?.src) : undefined;
|
|
1223
1377
|
const credentialless = (ifr as { credentialless?: boolean } | undefined)?.credentialless === true;
|
|
@@ -1246,7 +1400,7 @@ export class Tile extends EventTarget {
|
|
|
1246
1400
|
setTimeout(resolve);
|
|
1247
1401
|
};
|
|
1248
1402
|
setTimeout(resolve, CLOSE_MS);
|
|
1249
|
-
this.post(SND_SHUTTLE_CLOSE, { wipe: this
|
|
1403
|
+
this.post(SND_SHUTTLE_CLOSE, { wipe: this.ephemeral });
|
|
1250
1404
|
});
|
|
1251
1405
|
window.removeEventListener('message', onAnswer);
|
|
1252
1406
|
this.#runtimeDone = undefined;
|
|
@@ -1268,7 +1422,7 @@ export class Tile extends EventTarget {
|
|
|
1268
1422
|
// nothing is said of how it went.
|
|
1269
1423
|
setTimeout(() => { void this.#mothership.wipeOrigin(origin, credentialless); }, WIPE_AGAIN_MS);
|
|
1270
1424
|
}
|
|
1271
|
-
else if (!started) outcome.wiped = this
|
|
1425
|
+
else if (!started) outcome.wiped = this.ephemeral;
|
|
1272
1426
|
if (wipe && !outcome.wiped) {
|
|
1273
1427
|
console.warn(origin
|
|
1274
1428
|
? `[tiles] A closed tile's origin was not wiped, as far as can be told: its loading server said so, or did not answer in time (${origin}).`
|
|
@@ -1277,9 +1431,73 @@ export class Tile extends EventTarget {
|
|
|
1277
1431
|
this.dispatchEvent(new Event('close'));
|
|
1278
1432
|
}
|
|
1279
1433
|
}
|
|
1280
|
-
|
|
1434
|
+
/**
|
|
1435
|
+
* The frame that runs the tile, for the host to put in its document.
|
|
1436
|
+
*
|
|
1437
|
+
* The frame is sandboxed. The runtime in it has a sandbox of its own
|
|
1438
|
+
* (`tileSandbox` in @dasl/tile-lexicon), and a frame's sandbox only ever
|
|
1439
|
+
* takes away: a tile gets what both grant. It needs scripts and its own
|
|
1440
|
+
* origin to run at all (its worker cannot be registered without them), and
|
|
1441
|
+
* forms to submit to itself. The runtime grants dialogs (`allow-modals`:
|
|
1442
|
+
* alert, confirm, prompt, print) and downloads (`allow-downloads`) besides;
|
|
1443
|
+
* a tile in someone else's page has no call for either, so the frame
|
|
1444
|
+
* leaves them out.
|
|
1445
|
+
*
|
|
1446
|
+
* `options.sandbox` is the frame's sandbox attribute in full, for a host
|
|
1447
|
+
* that wants it otherwise: wider where its user runs one tile on purpose,
|
|
1448
|
+
* narrower (no forms) if it likes. `allow-scripts` and `allow-same-origin`
|
|
1449
|
+
* have to be in it, and nothing can be that the runtime does not grant,
|
|
1450
|
+
* since it would be there for nothing. Either mistake throws.
|
|
1451
|
+
*
|
|
1452
|
+
* `options.credentialless` makes the frame credentialless. A tile's origin
|
|
1453
|
+
* is a subdomain of the loading server's site, and cookies go by site: in
|
|
1454
|
+
* a plain frame a tile reads the cookies the site keeps for its whole
|
|
1455
|
+
* domain, and sets some there. In a credentialless frame Chromium gives
|
|
1456
|
+
* everything inside cookies and storage of its own, empty to begin with
|
|
1457
|
+
* and gone with the page, and keeps its password manager out. Other
|
|
1458
|
+
* browsers ignore the attribute. When nothing is said, the frame is
|
|
1459
|
+
* credentialless if the tile's origin was made for this one load
|
|
1460
|
+
* (`tile.ephemeral`): nothing is lost by its storage going with the page.
|
|
1461
|
+
* A named origin, or the origin named by the tile's CID, is there for a
|
|
1462
|
+
* tile to find its storage again, which such a frame loses with every
|
|
1463
|
+
* page, so its frame is a plain one. `true` or
|
|
1464
|
+
* `false` for a host that wants it otherwise, either way, and anything
|
|
1465
|
+
* else throws.
|
|
1466
|
+
*
|
|
1467
|
+
* `options.tabindex` is the frame's tabindex: `-1` has the Tab key pass
|
|
1468
|
+
* over the tile. Whoever has the focus in a tile types into the tile,
|
|
1469
|
+
* every key, Tab and Escape with the rest, and the host hears none of
|
|
1470
|
+
* them: a tile can keep the keyboard it is given, and each key counts as
|
|
1471
|
+
* its user having acted in it. A page with more on it than tiles may want
|
|
1472
|
+
* no one to end up there by tabbing down it, and then gives its user a
|
|
1473
|
+
* way in of its own. The focus guard (`guardFocus` in
|
|
1474
|
+
* @dasl/tile-loader/guard) does both for a frame it guards, whatever this
|
|
1475
|
+
* says: it takes the frame out of the tab order and puts a stop of its own
|
|
1476
|
+
* in its place, unless its host tells it otherwise. This is about the Tab
|
|
1477
|
+
* key and no more: a tile's script can still take the focus itself. A
|
|
1478
|
+
* whole number from -32767 to 32767, or it throws; the frame has no
|
|
1479
|
+
* tabindex, and is in the tab order as any frame is, when nothing is said.
|
|
1480
|
+
*
|
|
1481
|
+
* `options.refuse` names features to take from the tile, among REFUSABLE:
|
|
1482
|
+
* those a browser gives every frame unasked (picture-in-picture, the
|
|
1483
|
+
* gamepad, synchronous requests, the client hints and more), and those the
|
|
1484
|
+
* runtime hands on to a tile when its host's frame allows them. Each is
|
|
1485
|
+
* written into the frame's `allow` attribute as `'none'`, which binds
|
|
1486
|
+
* everything inside the frame, the runtime and every frame the tile makes.
|
|
1487
|
+
* It only ever takes away, and only where the browser knows the name:
|
|
1488
|
+
* Chrome knows them all, Firefox and Safari far fewer, and pass over the
|
|
1489
|
+
* others. A name the loader does not know throws, since it would take
|
|
1490
|
+
* nothing away anywhere. When nothing is said, the frame refuses
|
|
1491
|
+
* synchronous requests (`sync-xhr`), which hold up the tile and its
|
|
1492
|
+
* runtime and never bring a tile its own files. A host's list is the list
|
|
1493
|
+
* in full, as `options.sandbox` is the attribute: it replaces that one, so
|
|
1494
|
+
* a host that refuses more names `sync-xhr` in it to keep that refused,
|
|
1495
|
+
* and `[]` refuses nothing and leaves the frame with no `allow` attribute.
|
|
1496
|
+
*/
|
|
1497
|
+
renderContent (height = 300, options?: FrameOptions): HTMLIFrameElement {
|
|
1281
1498
|
const ifr = el('iframe', {
|
|
1282
|
-
src: this.#mothership.getLoadSource(),
|
|
1499
|
+
src: this.#mothership.getLoadSource(this),
|
|
1500
|
+
...this.#frameAttributes(options),
|
|
1283
1501
|
style: {
|
|
1284
1502
|
display: 'block',
|
|
1285
1503
|
width: '100%',
|
|
@@ -1290,4 +1508,51 @@ export class Tile extends EventTarget {
|
|
|
1290
1508
|
this.attachIframe(ifr);
|
|
1291
1509
|
return ifr;
|
|
1292
1510
|
}
|
|
1511
|
+
// The attributes of the frame renderContent() makes, from what its host
|
|
1512
|
+
// asked, each checked: one that cannot be honoured throws (see
|
|
1513
|
+
// renderContent()).
|
|
1514
|
+
#frameAttributes (options?: FrameOptions): Record<string, string | number | undefined> {
|
|
1515
|
+
// The first two no tile runs without; the rest of what the runtime
|
|
1516
|
+
// grants is the host's to keep or leave out.
|
|
1517
|
+
const needed = ['allow-scripts', 'allow-same-origin'];
|
|
1518
|
+
const granted = tileSandbox.split(' ');
|
|
1519
|
+
const asked = options?.sandbox ?? FRAME_SANDBOX;
|
|
1520
|
+
if (typeof asked !== 'string') throw new Error(`A tile's frame takes its sandbox as the text of the attribute: ${FRAME_SANDBOX} when nothing is said.`);
|
|
1521
|
+
// Read as HTML reads the attribute: any case, any spacing.
|
|
1522
|
+
const sandbox = asked.toLowerCase().split(/\s+/).filter(Boolean);
|
|
1523
|
+
const refused = sandbox.filter(token => !granted.includes(token));
|
|
1524
|
+
if (refused.length) throw new Error(`A tile's frame cannot be given ${refused.join(', ')}: the runtime grants ${granted.join(', ')} and nothing else.`);
|
|
1525
|
+
if (!needed.every(token => sandbox.includes(token))) throw new Error(`A tile's frame needs ${needed.join(' and ')}: no tile runs without them.`);
|
|
1526
|
+
// An origin made for this load is thrown away after it, so nothing is
|
|
1527
|
+
// lost by the frame's storage going with the page. A host that says
|
|
1528
|
+
// otherwise says true or false, and nothing else: any other value would
|
|
1529
|
+
// decide it unnoticed, and the empty text an HTML attribute has when it
|
|
1530
|
+
// is there would count as false.
|
|
1531
|
+
const credentialless = options?.credentialless ?? this.ephemeral;
|
|
1532
|
+
if (typeof credentialless !== 'boolean') throw new Error(`A tile's frame is credentialless or it is not: the option takes true or false.`);
|
|
1533
|
+
// A tabindex the browser cannot read is no tabindex: the frame would be
|
|
1534
|
+
// in the tab order and its host none the wiser. Only a whole number will
|
|
1535
|
+
// do, and not too large a one: Chromium takes a number it cannot hold for
|
|
1536
|
+
// no tabindex either, and the largest are not even written in digits
|
|
1537
|
+
// ("1e+21", which reads as 1). 32767 is as far as HTML ever said one goes.
|
|
1538
|
+
const tabindex = options?.tabindex ?? undefined;
|
|
1539
|
+
if (tabindex !== undefined && (!Number.isInteger(tabindex) || Math.abs(tabindex) > 32767)) throw new Error(`A tile's frame takes a whole number for its tabindex, from -32767 to 32767: -1 has the Tab key pass over the tile, 0 lets it in.`);
|
|
1540
|
+
// Names, each one the loader knows, and each refused once. Read with its
|
|
1541
|
+
// holes, which every way of going through a list but this one passes
|
|
1542
|
+
// over: a list with a hole in it is not a list of names, and one that
|
|
1543
|
+
// is all holes would refuse nothing, its host none the wiser.
|
|
1544
|
+
const listed = options?.refuse ?? FRAME_REFUSE;
|
|
1545
|
+
const refuse = Array.isArray(listed) ? Array.from(listed) : listed;
|
|
1546
|
+
if (!Array.isArray(refuse) || refuse.some(name => typeof name !== 'string')) throw new Error(`A tile's frame takes a list of the names of features to refuse the tile: ${JSON.stringify(FRAME_REFUSE)} when nothing is said, [] to refuse nothing.`);
|
|
1547
|
+
const unknown = refuse.filter(name => !REFUSABLE.includes(name));
|
|
1548
|
+
if (unknown.length) throw new Error(`A tile's frame cannot refuse ${unknown.map(name => JSON.stringify(name)).join(', ')}: the names it knows are those of REFUSABLE, ${REFUSABLE.join(', ')}.`);
|
|
1549
|
+
const allow = [...new Set(refuse)].map(name => `${name} 'none'`).join('; ');
|
|
1550
|
+
return {
|
|
1551
|
+
sandbox: sandbox.join(' '),
|
|
1552
|
+
// The attribute is there or it is not; it has no value.
|
|
1553
|
+
credentialless: credentialless ? '' : undefined,
|
|
1554
|
+
tabindex,
|
|
1555
|
+
allow: allow || undefined,
|
|
1556
|
+
};
|
|
1557
|
+
}
|
|
1293
1558
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// A tile's frame out of the tab order, for as long as something needs it
|
|
2
|
+
// out: a layer of the page's over the tile, which is where Tab stops then,
|
|
3
|
+
// or a tile that waits to be picked. More than one may need it out at once,
|
|
4
|
+
// and each lets go in its own time. Were each to note the frame's tabindex
|
|
5
|
+
// and put back what it found, one that let go after another had taken the
|
|
6
|
+
// frame out would put it back in under the other's layer, or one that took
|
|
7
|
+
// it out after another would leave it out for good. So they all go through
|
|
8
|
+
// here: the frame is out while anything holds it, and once the last lets go
|
|
9
|
+
// it is given back the tabindex its host gave it, or none if it had none.
|
|
10
|
+
// And a frame held out can be let in for a moment, for one Tab of the
|
|
11
|
+
// user's into it, which goes through here as well (see `letIn`).
|
|
12
|
+
|
|
13
|
+
interface Held {
|
|
14
|
+
own: string | null; // the host's tabindex, as it was at the first hold
|
|
15
|
+
holds: number;
|
|
16
|
+
open?: object; // the moment it is let in for, while it is (see `letIn`)
|
|
17
|
+
}
|
|
18
|
+
const held = new WeakMap<Element, Held>();
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Takes `frame` out of the tab order until the function it returns is
|
|
22
|
+
* called. That puts it back only if nothing else holds it out by then; a
|
|
23
|
+
* second call does nothing.
|
|
24
|
+
*/
|
|
25
|
+
export function holdOut (frame: Element): () => void {
|
|
26
|
+
let hold = held.get(frame);
|
|
27
|
+
if (!hold) {
|
|
28
|
+
hold = { own: frame.getAttribute('tabindex'), holds: 0 };
|
|
29
|
+
held.set(frame, hold);
|
|
30
|
+
}
|
|
31
|
+
const mine = hold;
|
|
32
|
+
mine.holds++;
|
|
33
|
+
// (Out, whatever let it in a moment ago: something needs it out now.)
|
|
34
|
+
mine.open = undefined;
|
|
35
|
+
frame.setAttribute('tabindex', '-1');
|
|
36
|
+
let done = false;
|
|
37
|
+
return () => {
|
|
38
|
+
if (done) return;
|
|
39
|
+
done = true;
|
|
40
|
+
if (--mine.holds > 0) return;
|
|
41
|
+
held.delete(frame);
|
|
42
|
+
if (mine.own === null) frame.removeAttribute('tabindex');
|
|
43
|
+
else frame.setAttribute('tabindex', mine.own);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Lets `frame`, which something holds out of the tab order, into it for a
|
|
49
|
+
* moment, until the function it returns is called or something takes it out
|
|
50
|
+
* again (a hold taken meanwhile): the user's next Tab can then take them into
|
|
51
|
+
* it, as a Tab into any frame in the tab order does. Then it is out, for as
|
|
52
|
+
* long as anything holds it, and as its host made it once nothing does. A
|
|
53
|
+
* frame nothing holds out is where its host put it, and is left so.
|
|
54
|
+
*/
|
|
55
|
+
export function letIn (frame: Element): () => void {
|
|
56
|
+
const hold = held.get(frame);
|
|
57
|
+
if (!hold) return () => {};
|
|
58
|
+
const moment = {};
|
|
59
|
+
hold.open = moment;
|
|
60
|
+
frame.setAttribute('tabindex', '0');
|
|
61
|
+
return () => {
|
|
62
|
+
if (hold.open !== moment) return;
|
|
63
|
+
hold.open = undefined;
|
|
64
|
+
if (held.get(frame) === hold) frame.setAttribute('tabindex', '-1');
|
|
65
|
+
};
|
|
66
|
+
}
|
package/src/mount.ts
CHANGED
|
@@ -19,7 +19,10 @@
|
|
|
19
19
|
//
|
|
20
20
|
// A tile that is put away is closed (Tile.close()): its frame goes, and the
|
|
21
21
|
// origin it ran on is wiped. Shown again, it starts afresh, on another
|
|
22
|
-
// origin. So this is for tiles whose state the host does not mean to keep
|
|
22
|
+
// origin. So this is for tiles whose state the host does not mean to keep,
|
|
23
|
+
// unless they run on an origin that is kept (a named one, or the one a
|
|
24
|
+
// tile's CID names: see `tile.ephemeral`), where a tile shown again finds
|
|
25
|
+
// what it stored there.
|
|
23
26
|
//
|
|
24
27
|
// Starting a tile is the costly moment (its runtime spends the browser's
|
|
25
28
|
// WebRTC budget first, where there is one), so tiles are also started a few
|