@dasl/tile-loader 4.0.0 → 5.1.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 +168 -72
- package/dist/index.d.ts +107 -31
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +272 -46
- package/dist/index.js.map +1 -1
- package/dist/mount.js +3 -3
- package/dist/mount.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +322 -57
- package/src/mount.ts +3 -3
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAwCA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE7E,MAAM,WAAW,gBAAgB;IAC/B,+FAA+F;IAC/F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,oFAAoF;IACpF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAwCA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE7E,MAAM,WAAW,gBAAgB;IAC/B,+FAA+F;IAC/F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,oFAAoF;IACpF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;;OAIG;IACH,WAAW,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;IACjC;;;;;;;;OAQG;IACH,aAAa,CAAC,EAAE,MAAM,CA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EAAE,YAAY,CAAA;KAAE,GAAG,OAAO,CAAC,WAAW,CAAC;IAyDnG;;;;;;;;OAQG;IACH,aAAa,IAAK,MAAM;IAIxB;;;;OAIG;IACH,YAAY,CAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO;IAI/D,mEAAmE;IACnE,IAAI,SAAS,IAAK,MAAM,GAAG,SAAS,CAEnC;IACD;;;;;OAKG;IACH,IAAI,CAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO;IA0FvC;;;OAGG;IACH,MAAM,CAAE,KAAK,CAAC,EAAE,cAAc,CAAC,OAAO,CAAC;IAOvC;;;;;OAKG;IACH,KAAK;IAGL;;;;;OAKG;IACH,MAAM,CAAE,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAE,IAAI,CAAC,cAAc,EAAE,OAAO,GAAG,YAAY,CAAM;IAanG;;;;;;;;;;;;;OAaG;IACH,KAAK;IAoDL,oDAAoD;IACpD,aAAa;IAGb;;;;OAIG;IACH,YAAY,CAAE,GAAG,EAAE,iBAAiB;IAmCpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwCG;IACH,KAAK,IAAK,OAAO,CAAC,OAAO,CAAC;IAkH1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8DG;IACH,aAAa,CAAE,MAAM,SAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,iBAAiB;CA8DxE"}
|
package/dist/index.js
CHANGED
|
@@ -51,8 +51,9 @@ const CLOSE_MS = 2500;
|
|
|
51
51
|
// How long the wiping of an origin gets when it is asked for from a frame of
|
|
52
52
|
// our own (TileMothership.wipeOrigin), before it is given up on and counted
|
|
53
53
|
// as not done. A guess, and a generous one: nothing waits on it but the
|
|
54
|
-
// promise close() returns, and
|
|
55
|
-
//
|
|
54
|
+
// promise close() returns, and a tile that starts on the origin its CID
|
|
55
|
+
// names, and it has to cover a slow network and a tile that holds its own
|
|
56
|
+
// pages up on their way out.
|
|
56
57
|
const WIPE_MS = 10000;
|
|
57
58
|
// How long after an origin was wiped it is wiped once more (Tile.close()).
|
|
58
59
|
// The first wipe comes after everything the tile's pages did, but a worker
|
|
@@ -60,7 +61,12 @@ const WIPE_MS = 10000;
|
|
|
60
61
|
// stop one after its page is gone (up to two seconds, in Chromium): what it
|
|
61
62
|
// writes in that moment lands on an origin that was just wiped. A guess,
|
|
62
63
|
// with a margin over that. A wipe that did not go through the first time
|
|
63
|
-
// gets its second chance then too.
|
|
64
|
+
// gets its second chance then too. On an origin named by a tile's CID, which
|
|
65
|
+
// the next run of that tile on the page is on too, that run waits for this
|
|
66
|
+
// second wipe before it starts: it is the wipe it starts after. And the wipe
|
|
67
|
+
// before a tile starts there, which a worker of a run that was never closed
|
|
68
|
+
// can be writing through in the same way, gets its second chance then too,
|
|
69
|
+
// before the tile is given up on.
|
|
64
70
|
const WIPE_AGAIN_MS = 3000;
|
|
65
71
|
const WORKER_PFX = 'tiles-worker-';
|
|
66
72
|
const SND_WORKER_LOAD = `${WORKER_PFX}load`; // tell worker to roll
|
|
@@ -120,6 +126,22 @@ const FRAME_REFUSE = ['sync-xhr'];
|
|
|
120
126
|
* there (the README says what, in each).
|
|
121
127
|
*/
|
|
122
128
|
export const REFUSABLE = refusableFeatures;
|
|
129
|
+
// Whether a frame's window still shows the empty document a frame starts
|
|
130
|
+
// with, which is this page's own: about:blank, which only a window of this
|
|
131
|
+
// page's origin lets us read. (A window we can read that shows anything
|
|
132
|
+
// else is not that one: a page of this origin, as the dock would be for a
|
|
133
|
+
// host on the loading server's own origin.)
|
|
134
|
+
function initial(win) {
|
|
135
|
+
try {
|
|
136
|
+
return win.location.href === 'about:blank';
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// How many messages are kept for a frame that has not loaded yet (see
|
|
143
|
+
// Tile.post()): more than any host sends that soon, and few enough to hold.
|
|
144
|
+
const EARLY_MAX = 256;
|
|
123
145
|
// A timer is set for no longer than this: a longer wait comes round to none.
|
|
124
146
|
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
125
147
|
// A frame that is out of the document when its deadline passes is looked at
|
|
@@ -268,7 +290,7 @@ export class TileMothership {
|
|
|
268
290
|
this.#log(used, 'error', `[S:${id}]`, ...asList(ev.data.msg));
|
|
269
291
|
// With a code, it is what kept the runtime from starting the tile.
|
|
270
292
|
const { code } = ev.data;
|
|
271
|
-
if (code === 'worker' || code === 'unsupported')
|
|
293
|
+
if (code === 'worker' || code === 'unsupported' || code === 'frame')
|
|
272
294
|
tile.failed(code);
|
|
273
295
|
}
|
|
274
296
|
else if (action === RCV_DOCK_STOPPED) {
|
|
@@ -536,13 +558,16 @@ export class TileMothership {
|
|
|
536
558
|
* `sizing` goes along: the shuttle measures the content of a tile that
|
|
537
559
|
* declares `fit` (see @dasl/tile-loader/size). So does whether its origin
|
|
538
560
|
* is its own, made for this load: one that is can be thrown away with it.
|
|
561
|
+
* The origin named by a tile's CID is wiped too, but by us, and not under
|
|
562
|
+
* another run of the tile there, which needs its worker: it is not this
|
|
563
|
+
* run's to throw away on its own.
|
|
539
564
|
*/
|
|
540
565
|
loadPayload(tile) {
|
|
541
566
|
const sizing = tile.manifest?.sizing;
|
|
542
567
|
return {
|
|
543
568
|
id: tile.runtimeId,
|
|
544
569
|
webrtcGuard: this.#conf.webrtcGuard ?? 'exhaust',
|
|
545
|
-
ephemeral: tile.ephemeral,
|
|
570
|
+
ephemeral: tile.ephemeral && !tile.sharedOrigin,
|
|
546
571
|
...(sizing ? { sizing } : {}),
|
|
547
572
|
};
|
|
548
573
|
}
|
|
@@ -554,9 +579,9 @@ export class TileMothership {
|
|
|
554
579
|
return Number.isFinite(ms) ? Math.min(ms, MAX_TIMER_MS) : 0;
|
|
555
580
|
}
|
|
556
581
|
/**
|
|
557
|
-
* Whether tiles
|
|
558
|
-
* `namedOrigin`)
|
|
559
|
-
*
|
|
582
|
+
* Whether what tiles store goes with each run, their origins wiped (no
|
|
583
|
+
* `namedOrigin`): an origin of their own for every load, or, with
|
|
584
|
+
* `cidOrigins`, the one their CID names. See `tile.ephemeral`.
|
|
560
585
|
*/
|
|
561
586
|
get ephemeral() {
|
|
562
587
|
return !this.#namedHost();
|
|
@@ -612,7 +637,8 @@ export class TileMothership {
|
|
|
612
637
|
}
|
|
613
638
|
/**
|
|
614
639
|
* @internal Has a tile origin wiped, from a frame of our own making: what
|
|
615
|
-
* closing a tile comes down to once its frame is out of the document
|
|
640
|
+
* closing a tile comes down to once its frame is out of the document, and
|
|
641
|
+
* what a tile on the origin named by its CID starts after.
|
|
616
642
|
*
|
|
617
643
|
* The wipe answers a page of the origin it empties and no one else, and
|
|
618
644
|
* this page is not one. So a frame is made, out of sight, that loads the
|
|
@@ -633,6 +659,25 @@ export class TileMothership {
|
|
|
633
659
|
* false when it says not, or says nothing for WIPE_MS.
|
|
634
660
|
*/
|
|
635
661
|
wipeOrigin(origin, credentialless = false) {
|
|
662
|
+
return this.clearOrigin(origin, credentialless).then(answer => answer === 'wiped');
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* @internal Has a tile origin wiped, as wipeOrigin() does, and says how it
|
|
666
|
+
* went: `wiped`; `held`, not wiped because a run of the tile there holds
|
|
667
|
+
* the run's lock; or `failed`, not wiped, or not as far as can be told.
|
|
668
|
+
*
|
|
669
|
+
* On an origin named by a tile's CID the runs of that tile share one store
|
|
670
|
+
* with whatever other page of the site runs it, where the browser keeps
|
|
671
|
+
* one store for the site: another tab, another copy of the loader on this
|
|
672
|
+
* page. Each holds the run's lock for as long as it runs, and the runtime
|
|
673
|
+
* wipes nothing while one does (see the run's lock in @dasl/tile-server's
|
|
674
|
+
* dock/dock.js): the last of them to be closed has it wiped. Only a runtime
|
|
675
|
+
* whose dock says that it holds runs to the lock (`lock` in its ready
|
|
676
|
+
* message, which that of @dasl/tile-server 4.0.0 does not say) is taken
|
|
677
|
+
* at its word that one does: from any other, a wipe that was not done is
|
|
678
|
+
* one that failed.
|
|
679
|
+
*/
|
|
680
|
+
clearOrigin(origin, credentialless = false) {
|
|
636
681
|
const loadOrigin = this.getLoadOrigin();
|
|
637
682
|
const url = new URL(dockPath, loadOrigin);
|
|
638
683
|
url.searchParams.set('tile', new URL(origin).host);
|
|
@@ -646,20 +691,26 @@ export class TileMothership {
|
|
|
646
691
|
hidden: '',
|
|
647
692
|
style: { display: 'none' },
|
|
648
693
|
});
|
|
649
|
-
const done = (
|
|
694
|
+
const done = (answer) => {
|
|
650
695
|
clearTimeout(timer);
|
|
651
696
|
window.removeEventListener('message', onMessage);
|
|
652
697
|
ifr.remove();
|
|
653
|
-
resolve(
|
|
698
|
+
resolve(answer);
|
|
654
699
|
};
|
|
700
|
+
// Whether that frame's dock said its runtime holds runs to the lock.
|
|
701
|
+
let locks = false;
|
|
655
702
|
// Only that frame's window, on the loading server, speaks for it.
|
|
656
703
|
const onMessage = (ev) => {
|
|
657
704
|
if (!ev.source || ev.source !== ifr.contentWindow || ev.origin !== loadOrigin)
|
|
658
705
|
return;
|
|
659
|
-
if (ev.data?.action ===
|
|
660
|
-
|
|
706
|
+
if (ev.data?.action === RCV_DOCK_READY)
|
|
707
|
+
locks = ev.data.payload?.lock === true;
|
|
708
|
+
if (ev.data?.action !== RCV_SHUTTLE_CLOSED)
|
|
709
|
+
return;
|
|
710
|
+
const { wiped, held } = ev.data.payload ?? {};
|
|
711
|
+
done(wiped !== false ? 'wiped' : locks && held === true ? 'held' : 'failed');
|
|
661
712
|
};
|
|
662
|
-
const timer = setTimeout(() => done(
|
|
713
|
+
const timer = setTimeout(() => done('failed'), WIPE_MS);
|
|
663
714
|
window.addEventListener('message', onMessage);
|
|
664
715
|
ifr.addEventListener('load', () => {
|
|
665
716
|
ifr.contentWindow?.postMessage({ action: SND_SHUTTLE_CLOSE, payload: { wipe: true } }, loadOrigin);
|
|
@@ -740,6 +791,30 @@ export class TileMothership {
|
|
|
740
791
|
return false;
|
|
741
792
|
}
|
|
742
793
|
}
|
|
794
|
+
const SHARED = new Map();
|
|
795
|
+
function sharedOrigin(origin, credentialless) {
|
|
796
|
+
const key = credentialless ? `${origin} credentialless` : origin;
|
|
797
|
+
let shared = SHARED.get(key);
|
|
798
|
+
if (!shared)
|
|
799
|
+
SHARED.set(key, shared = { origin, credentialless, tiles: new Set(), ready: Promise.resolve(true), again: false });
|
|
800
|
+
return shared;
|
|
801
|
+
}
|
|
802
|
+
// How many other tiles of the page run on that store now, `tile` left out
|
|
803
|
+
// (and taken out); those that no longer run there are forgotten, and one
|
|
804
|
+
// that comes back there joins it again (see Tile.start()).
|
|
805
|
+
function othersOn(shared, tile) {
|
|
806
|
+
for (const ref of shared.tiles) {
|
|
807
|
+
const other = ref.deref();
|
|
808
|
+
if (!other || other === tile || !other.runsOn(shared))
|
|
809
|
+
shared.tiles.delete(ref);
|
|
810
|
+
}
|
|
811
|
+
return shared.tiles.size;
|
|
812
|
+
}
|
|
813
|
+
// An origin a tile shares with the other tiles of the page there: one named
|
|
814
|
+
// by a CID, as the address of its frame names it.
|
|
815
|
+
function sharedName(origin) {
|
|
816
|
+
return origin && isCidLabel(new URL(origin).hostname.split('.')[0]) ? origin : undefined;
|
|
817
|
+
}
|
|
743
818
|
// The loading server is at `load.<base>`, and a tile origin at `<label>.<base>`.
|
|
744
819
|
const LOAD_LABEL = 'load.';
|
|
745
820
|
const LETTERS = 'abcdefghijklmnopqrstuvwxyz';
|
|
@@ -835,14 +910,38 @@ export class Tile extends EventTarget {
|
|
|
835
910
|
return this.#identity.manifestBytes;
|
|
836
911
|
}
|
|
837
912
|
/**
|
|
838
|
-
* Whether
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
* (`cidOrigins`)
|
|
842
|
-
* kept, for
|
|
913
|
+
* Whether what this tile stores goes with each run of it: the origin it
|
|
914
|
+
* runs on is wiped when it is closed, and holds nothing from before when
|
|
915
|
+
* it starts. It is, on an origin made for one load and on the origin named
|
|
916
|
+
* by its CID (`cidOrigins`), unless its mothership names an origin for its
|
|
917
|
+
* tiles (`namedOrigin`): that one is kept, for a tile to find what it
|
|
918
|
+
* stored there again.
|
|
843
919
|
*/
|
|
844
920
|
get ephemeral() {
|
|
845
|
-
return this.#mothership.ephemeral
|
|
921
|
+
return this.#mothership.ephemeral;
|
|
922
|
+
}
|
|
923
|
+
// The store of an origin named by a CID that this run of the tile is on,
|
|
924
|
+
// with whatever other tiles of the page are there (see SHARED), once it
|
|
925
|
+
// has started there; the wipe it waits for before its runtime is told to
|
|
926
|
+
// load it; how it is known there; and which start, or close, came last: a
|
|
927
|
+
// start that waits goes no further once another has come.
|
|
928
|
+
#joined;
|
|
929
|
+
#ready;
|
|
930
|
+
#ref = new WeakRef(this);
|
|
931
|
+
#turn = 0;
|
|
932
|
+
/** @internal The origin named by a CID this run of the tile shares with the page's other tiles there, if it has started there. */
|
|
933
|
+
get sharedOrigin() {
|
|
934
|
+
return this.#joined?.origin;
|
|
935
|
+
}
|
|
936
|
+
/** @internal Whether this tile runs on that store now: it has started there, in a frame that is in the document. */
|
|
937
|
+
runsOn(shared) {
|
|
938
|
+
return this.#joined === shared && this.#frame?.deref()?.isConnected === true;
|
|
939
|
+
}
|
|
940
|
+
// The store of the origin named by a CID that a frame's address names, if
|
|
941
|
+
// it names one, of the kind of frame it is.
|
|
942
|
+
#sharedIn(ifr) {
|
|
943
|
+
const origin = this.ephemeral ? sharedName(this.#mothership.originNamedIn(ifr?.src)) : undefined;
|
|
944
|
+
return origin ? sharedOrigin(origin, ifr?.credentialless === true) : undefined;
|
|
846
945
|
}
|
|
847
946
|
/**
|
|
848
947
|
* @internal The bytes of the manifest that names this tile's origin, when
|
|
@@ -934,7 +1033,8 @@ export class Tile extends EventTarget {
|
|
|
934
1033
|
* this one run of the tile. It is the same address every time it is asked
|
|
935
1034
|
* for, so that it can sit in a template that is rendered again and again,
|
|
936
1035
|
* until the tile is closed: after that it is another run, on another
|
|
937
|
-
* origin.
|
|
1036
|
+
* origin. (With `cidOrigins`, the origin its CID names, every run: the
|
|
1037
|
+
* next one finds it wiped.)
|
|
938
1038
|
*/
|
|
939
1039
|
getLoadSource() {
|
|
940
1040
|
this.#source ??= this.#mothership.getLoadSource(this);
|
|
@@ -962,8 +1062,30 @@ export class Tile extends EventTarget {
|
|
|
962
1062
|
*/
|
|
963
1063
|
post(action, payload) {
|
|
964
1064
|
this.#track();
|
|
965
|
-
const
|
|
966
|
-
win
|
|
1065
|
+
const ifr = this.#frame?.deref();
|
|
1066
|
+
const win = ifr?.contentWindow;
|
|
1067
|
+
if (!win)
|
|
1068
|
+
return;
|
|
1069
|
+
// A frame shows the empty document it starts with, which is this page's
|
|
1070
|
+
// own, until what it loads arrives: a message posted to the dock's
|
|
1071
|
+
// origin before that is dropped (Chrome says so in the console). The
|
|
1072
|
+
// size a host's tileSize() grants as the frame goes into the document
|
|
1073
|
+
// is one, and so is anything a host sends that soon. Those are kept, and
|
|
1074
|
+
// posted when the frame loads, in the order they came: the dock holds
|
|
1075
|
+
// what it is told before its runtime is up.
|
|
1076
|
+
if (initial(win)) {
|
|
1077
|
+
if (this.#early.length < EARLY_MAX)
|
|
1078
|
+
this.#early.push({ action, payload });
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
win.postMessage({ id: this.#runtimeId, action, payload }, this.#mothership.getLoadOrigin());
|
|
1082
|
+
}
|
|
1083
|
+
// What was posted to the frame before it had anything but its empty
|
|
1084
|
+
// document in it, to be posted once it loads.
|
|
1085
|
+
#early = [];
|
|
1086
|
+
#postEarly() {
|
|
1087
|
+
for (const { action, payload } of this.#early.splice(0))
|
|
1088
|
+
this.post(action, payload);
|
|
967
1089
|
}
|
|
968
1090
|
// The frame's window is how the tile's messages are known for its own. It
|
|
969
1091
|
// is there once the frame is in a document, and it is a new one if the
|
|
@@ -1081,15 +1203,77 @@ export class Tile extends EventTarget {
|
|
|
1081
1203
|
clearTimeout(this.#deadline);
|
|
1082
1204
|
this.dispatchEvent(Object.assign(new Event('error'), { reason, ...detail }));
|
|
1083
1205
|
}
|
|
1084
|
-
/**
|
|
1206
|
+
/**
|
|
1207
|
+
* @internal Tells the runtime in the frame to load the tile. On an origin
|
|
1208
|
+
* named by a CID, the first tile of the page to start there has it wiped
|
|
1209
|
+
* first, whatever an earlier run left there, closed or not (a page that
|
|
1210
|
+
* went without closing it, one that crashed): the runtime is told once
|
|
1211
|
+
* that is done, and is not told at all if it was not, the second time it
|
|
1212
|
+
* was tried either (`wipe`). A tile that starts there while another of
|
|
1213
|
+
* the page runs there finds what that one has: they share the origin, and
|
|
1214
|
+
* wiping it would take it from that one.
|
|
1215
|
+
* A frame that loads again goes on with the run it is in, unless it was
|
|
1216
|
+
* out of the document while the others there went on without it (they
|
|
1217
|
+
* may have had the origin wiped meanwhile, or be about to): then it
|
|
1218
|
+
* starts there as any other tile does.
|
|
1219
|
+
*/
|
|
1085
1220
|
start() {
|
|
1086
1221
|
this.#started = true;
|
|
1222
|
+
const turn = ++this.#turn;
|
|
1223
|
+
const ifr = this.#frame?.deref();
|
|
1224
|
+
const shared = this.#sharedIn(ifr);
|
|
1225
|
+
if (shared && (this.#joined !== shared || !shared.tiles.has(this.#ref)))
|
|
1226
|
+
this.#ready = this.#join(shared);
|
|
1227
|
+
const ready = this.#ready;
|
|
1228
|
+
if (!ready)
|
|
1229
|
+
return this.#launch();
|
|
1230
|
+
void ready.then((wiped) => {
|
|
1231
|
+
// Still this start: the host may have closed the tile meanwhile, or
|
|
1232
|
+
// rendered it again, or its frame loaded again.
|
|
1233
|
+
if (this.#turn !== turn)
|
|
1234
|
+
return;
|
|
1235
|
+
if (wiped)
|
|
1236
|
+
return this.#launch();
|
|
1237
|
+
// It runs there no more: the next to start there has it wiped again.
|
|
1238
|
+
shared?.tiles.delete(this.#ref);
|
|
1239
|
+
this.#joined = this.#ready = undefined;
|
|
1240
|
+
this.failed('wipe');
|
|
1241
|
+
});
|
|
1242
|
+
}
|
|
1243
|
+
#launch() {
|
|
1087
1244
|
// A start, and its time, from here: what a tile does before it has its
|
|
1088
|
-
// runtime load it (one that fetches its content first, say
|
|
1089
|
-
// runtime being late.
|
|
1245
|
+
// runtime load it (one that fetches its content first, say, or waits
|
|
1246
|
+
// for its origin to be wiped) is not the runtime being late.
|
|
1090
1247
|
this.#watch();
|
|
1091
1248
|
this.post(SND_SHUTTLE_LOAD, this.#mothership.loadPayload(this));
|
|
1092
1249
|
}
|
|
1250
|
+
// This run of the tile is on that store of an origin named by a CID, with
|
|
1251
|
+
// the other tiles of the page there. Resolves to whether it may start:
|
|
1252
|
+
// once the origin is wiped, if this is the first of them, or at once with
|
|
1253
|
+
// what the others started with.
|
|
1254
|
+
#join(shared) {
|
|
1255
|
+
// The first of the page there has it wiped, after whatever wipe is still
|
|
1256
|
+
// to come there; unless what is still to come is the second wipe of the
|
|
1257
|
+
// last close there, which comes after all that the tiles closed there
|
|
1258
|
+
// did, and is the wipe it starts after.
|
|
1259
|
+
if (!othersOn(shared, this) && !shared.again)
|
|
1260
|
+
shared.ready = shared.ready.then(() => this.#wipeFirst(shared));
|
|
1261
|
+
shared.again = false;
|
|
1262
|
+
shared.tiles.add(this.#ref);
|
|
1263
|
+
this.#joined = shared;
|
|
1264
|
+
return shared.ready;
|
|
1265
|
+
}
|
|
1266
|
+
// The wipe the first tile of the page there starts after; once more a few
|
|
1267
|
+
// seconds later if it did not go through (see WIPE_AGAIN_MS). Held is as
|
|
1268
|
+
// good: a run of another page is there (another tab of the site, another
|
|
1269
|
+
// copy of the loader), and the tile starts beside it, on what it has, as
|
|
1270
|
+
// the second tile of a page does.
|
|
1271
|
+
async #wipeFirst(shared) {
|
|
1272
|
+
const wipe = () => this.#mothership.clearOrigin(shared.origin, shared.credentialless).then(answer => answer !== 'failed');
|
|
1273
|
+
return await wipe() || new Promise((resolve) => {
|
|
1274
|
+
setTimeout(() => { void wipe().then(resolve); }, WIPE_AGAIN_MS);
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1093
1277
|
/** @internal The runtime says it has cleaned up. */
|
|
1094
1278
|
runtimeClosed() {
|
|
1095
1279
|
this.#runtimeDone?.();
|
|
@@ -1106,6 +1290,7 @@ export class Tile extends EventTarget {
|
|
|
1106
1290
|
this.#track();
|
|
1107
1291
|
this.#onFrameLoad = () => {
|
|
1108
1292
|
this.#track();
|
|
1293
|
+
this.#postEarly();
|
|
1109
1294
|
// The frame has loaded: the time a start is given runs from when the
|
|
1110
1295
|
// runtime in it is told to load the tile (see start()).
|
|
1111
1296
|
clearTimeout(this.#deadline);
|
|
@@ -1130,8 +1315,11 @@ export class Tile extends EventTarget {
|
|
|
1130
1315
|
if (this.#window)
|
|
1131
1316
|
this.#mothership.undock(this.#window);
|
|
1132
1317
|
this.#frame = this.#window = this.#onFrameLoad = this.#runtimeId = undefined;
|
|
1318
|
+
this.#early.length = 0;
|
|
1133
1319
|
this.#started = false;
|
|
1134
1320
|
this.#bound = false;
|
|
1321
|
+
this.#joined = this.#ready = undefined;
|
|
1322
|
+
this.#turn++;
|
|
1135
1323
|
}
|
|
1136
1324
|
/**
|
|
1137
1325
|
* Closes the tile: its frame is removed from the document, the tile
|
|
@@ -1144,8 +1332,11 @@ export class Tile extends EventTarget {
|
|
|
1144
1332
|
* reach is what the tile left in the browser. Every tile runs on an origin
|
|
1145
1333
|
* made for that one load, and its worker's registration, and anything the
|
|
1146
1334
|
* tile stored, would stay there for good. `close()` has that origin wiped
|
|
1147
|
-
* (not a named origin, `namedOrigin`,
|
|
1148
|
-
*
|
|
1335
|
+
* (not a named origin, `namedOrigin`, which is kept on purpose: see
|
|
1336
|
+
* `tile.ephemeral`). So it does the origin named by the tile's CID
|
|
1337
|
+
* (`cidOrigins`), but only once the last tile of the page there is
|
|
1338
|
+
* closed: the same tile shown twice on a page is on it twice, and the
|
|
1339
|
+
* other still needs what is there.
|
|
1149
1340
|
*
|
|
1150
1341
|
* It does not matter what became of the frame first, nor what the tile
|
|
1151
1342
|
* did. The frame's address names the origin (getLoadSource() does, and so
|
|
@@ -1163,8 +1354,11 @@ export class Tile extends EventTarget {
|
|
|
1163
1354
|
* CLOSE_MS to answer. Out of the document, there is no one to ask.
|
|
1164
1355
|
*
|
|
1165
1356
|
* What it resolves to: true when the origin was wiped, and when the tile
|
|
1166
|
-
* never started (there was nothing to wipe); false for a named origin
|
|
1167
|
-
* for the origin named by the tile's CID
|
|
1357
|
+
* never started (there was nothing to wipe); false for a named origin,
|
|
1358
|
+
* which is kept; for the origin named by the tile's CID while another tile
|
|
1359
|
+
* of the page still runs there, whose close has it wiped, or a run of
|
|
1360
|
+
* another page (another tab of the site, another copy of the loader)
|
|
1361
|
+
* holds it to its lock, which the runtime does not wipe under; and when the
|
|
1168
1362
|
* wipe could not be done or was not confirmed, which is also logged as a
|
|
1169
1363
|
* warning.
|
|
1170
1364
|
*/
|
|
@@ -1190,18 +1384,29 @@ export class Tile extends EventTarget {
|
|
|
1190
1384
|
// expected of it, and a deadline that runs out while its runtime is
|
|
1191
1385
|
// asked to close is not the tile failing to start.
|
|
1192
1386
|
this.awaits();
|
|
1387
|
+
// And a start that waits for its origin to be wiped goes no further.
|
|
1388
|
+
this.#turn++;
|
|
1193
1389
|
const ifr = this.#frame?.deref();
|
|
1194
1390
|
// There is something to wipe once a runtime was started on an origin
|
|
1195
|
-
// made for this load: nothing was put there before that.
|
|
1391
|
+
// made for this load: nothing was put there before that. On the origin
|
|
1392
|
+
// named by a CID, not while another tile of the page runs there: the
|
|
1393
|
+
// last of them to be closed has it wiped. Its store is the one this run
|
|
1394
|
+
// joined; or, for a run that never got that far (its wipe did not go
|
|
1395
|
+
// through), the one its frame's address names.
|
|
1196
1396
|
const started = this.#started;
|
|
1197
|
-
const
|
|
1397
|
+
const shared = this.#joined ?? this.#sharedIn(ifr);
|
|
1398
|
+
const held = shared ? othersOn(shared, this) > 0 : false;
|
|
1399
|
+
const wipe = this.ephemeral && started && !held;
|
|
1198
1400
|
// Which origin, the frame's address says, when it is one of ours.
|
|
1199
|
-
const origin = wipe ? this.#mothership.originNamedIn(ifr?.src) : undefined;
|
|
1200
|
-
const credentialless = ifr?.credentialless === true;
|
|
1401
|
+
const origin = wipe ? shared?.origin ?? this.#mothership.originNamedIn(ifr?.src) : undefined;
|
|
1402
|
+
const credentialless = shared?.credentialless ?? ifr?.credentialless === true;
|
|
1403
|
+
// Whether the wipe was held by a run of another page there.
|
|
1404
|
+
let elsewhere = false;
|
|
1201
1405
|
{
|
|
1202
1406
|
// The runtime in the frame is only asked when we cannot do without it:
|
|
1203
|
-
// for an origin we were not told of, and for a named origin,
|
|
1204
|
-
//
|
|
1407
|
+
// for an origin we were not told of, and for a named origin, or one
|
|
1408
|
+
// named by a CID that another tile still runs on, where there is
|
|
1409
|
+
// nothing to wipe and it just closes.
|
|
1205
1410
|
if (ifr?.isConnected && ifr.contentWindow && started && !origin) {
|
|
1206
1411
|
const win = ifr.contentWindow;
|
|
1207
1412
|
const loadOrigin = this.#mothership.getLoadOrigin();
|
|
@@ -1224,7 +1429,7 @@ export class Tile extends EventTarget {
|
|
|
1224
1429
|
setTimeout(resolve);
|
|
1225
1430
|
};
|
|
1226
1431
|
setTimeout(resolve, CLOSE_MS);
|
|
1227
|
-
this.post(SND_SHUTTLE_CLOSE, { wipe
|
|
1432
|
+
this.post(SND_SHUTTLE_CLOSE, { wipe });
|
|
1228
1433
|
});
|
|
1229
1434
|
window.removeEventListener('message', onAnswer);
|
|
1230
1435
|
this.#runtimeDone = undefined;
|
|
@@ -1238,7 +1443,27 @@ export class Tile extends EventTarget {
|
|
|
1238
1443
|
this.#source = undefined;
|
|
1239
1444
|
}
|
|
1240
1445
|
ifr?.remove?.();
|
|
1241
|
-
if (origin) {
|
|
1446
|
+
if (origin && shared) {
|
|
1447
|
+
// After any wipe still to come there (a tile that started there and
|
|
1448
|
+
// was closed before its wipe was done); and the second, in a moment,
|
|
1449
|
+
// is what the next tile of the page to start there waits for. Either
|
|
1450
|
+
// may be held by a run of another page there, which still needs what
|
|
1451
|
+
// is there: the last of them has it wiped, and a tile of this page
|
|
1452
|
+
// that starts meanwhile starts beside it.
|
|
1453
|
+
const clear = () => this.#mothership.clearOrigin(origin, credentialless);
|
|
1454
|
+
const first = shared.ready.then(clear);
|
|
1455
|
+
const second = first.then(() => new Promise((resolve) => {
|
|
1456
|
+
setTimeout(() => { void clear().then(answer => resolve(answer !== 'failed')); }, WIPE_AGAIN_MS);
|
|
1457
|
+
}));
|
|
1458
|
+
shared.ready = second;
|
|
1459
|
+
shared.again = true;
|
|
1460
|
+
void second.then(() => { if (shared.ready === second)
|
|
1461
|
+
shared.again = false; });
|
|
1462
|
+
const answer = await first;
|
|
1463
|
+
outcome.wiped = answer === 'wiped';
|
|
1464
|
+
elsewhere = answer === 'held';
|
|
1465
|
+
}
|
|
1466
|
+
else if (origin) {
|
|
1242
1467
|
outcome.wiped = await this.#mothership.wipeOrigin(origin, credentialless);
|
|
1243
1468
|
// And once more in a moment, for what a worker of the tile's may
|
|
1244
1469
|
// have written since (see WIPE_AGAIN_MS), and for a wipe that did
|
|
@@ -1248,7 +1473,7 @@ export class Tile extends EventTarget {
|
|
|
1248
1473
|
}
|
|
1249
1474
|
else if (!started)
|
|
1250
1475
|
outcome.wiped = this.ephemeral;
|
|
1251
|
-
if (wipe && !outcome.wiped) {
|
|
1476
|
+
if (wipe && !outcome.wiped && !elsewhere) {
|
|
1252
1477
|
console.warn(origin
|
|
1253
1478
|
? `[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}).`
|
|
1254
1479
|
: '[tiles] A closed tile\'s origin was not wiped: its frame was out of the document, or did not answer, or said that it could not, and its address does not name the origin. Load tile.getLoadSource() in a frame of your own, or close the tile before removing the frame.');
|
|
@@ -1281,11 +1506,11 @@ export class Tile extends EventTarget {
|
|
|
1281
1506
|
* everything inside cookies and storage of its own, empty to begin with
|
|
1282
1507
|
* and gone with the page, and keeps its password manager out. Other
|
|
1283
1508
|
* browsers ignore the attribute. When nothing is said, the frame is
|
|
1284
|
-
* credentialless if the tile
|
|
1285
|
-
* (`tile.ephemeral`)
|
|
1286
|
-
*
|
|
1287
|
-
* tile to find its storage again, which such
|
|
1288
|
-
* page, so its frame is a plain one. `true` or
|
|
1509
|
+
* credentialless if what the tile stores goes with its run
|
|
1510
|
+
* (`tile.ephemeral`), on an origin made for this one load or on the one
|
|
1511
|
+
* its CID names: nothing is lost by its storage going with the page. A
|
|
1512
|
+
* named origin is there for a tile to find its storage again, which such
|
|
1513
|
+
* a frame loses with every page, so its frame is a plain one. `true` or
|
|
1289
1514
|
* `false` for a host that wants it otherwise, either way, and anything
|
|
1290
1515
|
* else throws.
|
|
1291
1516
|
*
|
|
@@ -1351,8 +1576,9 @@ export class Tile extends EventTarget {
|
|
|
1351
1576
|
throw new Error(`A tile's frame cannot be given ${refused.join(', ')}: the runtime grants ${granted.join(', ')} and nothing else.`);
|
|
1352
1577
|
if (!needed.every(token => sandbox.includes(token)))
|
|
1353
1578
|
throw new Error(`A tile's frame needs ${needed.join(' and ')}: no tile runs without them.`);
|
|
1354
|
-
// An origin made for this load is thrown away after it, so
|
|
1355
|
-
//
|
|
1579
|
+
// An origin made for this load is thrown away after it, and so is the
|
|
1580
|
+
// one named by the tile's CID, so nothing is lost by the frame's storage
|
|
1581
|
+
// going with the page. A host that says
|
|
1356
1582
|
// otherwise says true or false, and nothing else: any other value would
|
|
1357
1583
|
// decide it unnoticed, and the empty text an HTML attribute has when it
|
|
1358
1584
|
// is there would count as false.
|