@dasl/tile-loader 3.1.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 +1463 -36
- package/dist/at.d.ts +126 -12
- package/dist/at.d.ts.map +1 -1
- package/dist/at.js +582 -90
- package/dist/at.js.map +1 -1
- package/dist/car.d.ts +21 -2
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +82 -15
- package/dist/car.js.map +1 -1
- package/dist/content-scheme.d.ts +17 -3
- package/dist/content-scheme.d.ts.map +1 -1
- package/dist/content-scheme.js +32 -6
- package/dist/content-scheme.js.map +1 -1
- package/dist/guard.d.ts +342 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +1544 -0
- package/dist/guard.js.map +1 -0
- package/dist/index.d.ts +416 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +999 -74
- package/dist/index.js.map +1 -1
- package/dist/lib/cid.d.ts +25 -0
- package/dist/lib/cid.d.ts.map +1 -0
- package/dist/lib/cid.js +65 -0
- package/dist/lib/cid.js.map +1 -0
- package/dist/lib/defaults.d.ts +8 -0
- package/dist/lib/defaults.d.ts.map +1 -0
- package/dist/lib/defaults.js +14 -0
- package/dist/lib/defaults.js.map +1 -0
- package/dist/lib/destinations.d.ts +25 -0
- package/dist/lib/destinations.d.ts.map +1 -0
- package/dist/lib/destinations.js +94 -0
- package/dist/lib/destinations.js.map +1 -0
- package/dist/lib/drisl.d.ts +10 -0
- package/dist/lib/drisl.d.ts.map +1 -0
- package/dist/lib/drisl.js +169 -0
- package/dist/lib/drisl.js.map +1 -0
- package/dist/lib/errors.d.ts +80 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +44 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/fetching.d.ts +24 -0
- package/dist/lib/fetching.d.ts.map +1 -0
- package/dist/lib/fetching.js +135 -0
- package/dist/lib/fetching.js.map +1 -0
- package/dist/lib/limits.d.ts +34 -0
- package/dist/lib/limits.d.ts.map +1 -0
- package/dist/lib/limits.js +33 -0
- package/dist/lib/limits.js.map +1 -0
- package/dist/lib/pointer.d.ts +83 -0
- package/dist/lib/pointer.d.ts.map +1 -0
- package/dist/lib/pointer.js +244 -0
- package/dist/lib/pointer.js.map +1 -0
- 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/link.d.ts +110 -13
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +336 -43
- package/dist/link.js.map +1 -1
- package/dist/memory.d.ts +1 -0
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +5 -0
- package/dist/memory.js.map +1 -1
- package/dist/mount.d.ts +54 -0
- package/dist/mount.d.ts.map +1 -0
- package/dist/mount.js +289 -0
- package/dist/mount.js.map +1 -0
- package/dist/size.d.ts +43 -2
- package/dist/size.d.ts.map +1 -1
- package/dist/size.js +296 -15
- package/dist/size.js.map +1 -1
- package/package.json +13 -3
- package/src/at.ts +590 -84
- package/src/car.ts +79 -16
- package/src/content-scheme.ts +38 -7
- package/src/guard.ts +1659 -0
- package/src/index.ts +1124 -74
- package/src/lib/cid.ts +65 -0
- package/src/lib/defaults.ts +12 -0
- package/src/lib/destinations.ts +79 -0
- package/src/lib/drisl.ts +160 -0
- package/src/lib/errors.ts +111 -0
- package/src/lib/fetching.ts +138 -0
- package/src/lib/limits.ts +61 -0
- package/src/lib/pointer.ts +290 -0
- package/src/lib/tab-order.ts +66 -0
- package/src/link.ts +365 -48
- package/src/memory.ts +5 -0
- package/src/mount.ts +359 -0
- package/src/size.ts +304 -16
package/dist/index.js
CHANGED
|
@@ -34,7 +34,10 @@ The tile-loading architecture has four levels that all communicate together:
|
|
|
34
34
|
requests up, which the shuttle then hands over to the mothership.
|
|
35
35
|
*/
|
|
36
36
|
import { el } from "./lib/el.js";
|
|
37
|
-
import {
|
|
37
|
+
import { TileLoadError } from "./lib/errors.js";
|
|
38
|
+
import { tileResponseHeaders, dockPath, tileSandbox, refusableFeatures, isCidLabel } from "@dasl/tile-lexicon";
|
|
39
|
+
// What a loader says went wrong, for a host to tell one failure from another.
|
|
40
|
+
export { TileLoadError } from "./lib/errors.js";
|
|
38
41
|
const TILES_PFX = 'tiles-';
|
|
39
42
|
const SHUTTLE_PFX = 'tiles-shuttle-';
|
|
40
43
|
const SND_SHUTTLE_LOAD = `${SHUTTLE_PFX}load`; // tell worker to roll
|
|
@@ -45,6 +48,20 @@ const SND_SHUTTLE_CLOSE = `${SHUTTLE_PFX}close`; // we are done with this tile:
|
|
|
45
48
|
const RCV_SHUTTLE_CLOSED = `${SHUTTLE_PFX}closed`; // it has
|
|
46
49
|
// How long a runtime gets to clean up after itself before its frame goes anyway.
|
|
47
50
|
const CLOSE_MS = 2500;
|
|
51
|
+
// How long the wiping of an origin gets when it is asked for from a frame of
|
|
52
|
+
// our own (TileMothership.wipeOrigin), before it is given up on and counted
|
|
53
|
+
// as not done. A guess, and a generous one: nothing waits on it but the
|
|
54
|
+
// promise close() returns, and it has to cover a slow network and a tile
|
|
55
|
+
// that holds its own pages up on their way out.
|
|
56
|
+
const WIPE_MS = 10000;
|
|
57
|
+
// How long after an origin was wiped it is wiped once more (Tile.close()).
|
|
58
|
+
// The first wipe comes after everything the tile's pages did, but a worker
|
|
59
|
+
// of the tile's has a thread of its own, and a browser can take a moment to
|
|
60
|
+
// stop one after its page is gone (up to two seconds, in Chromium): what it
|
|
61
|
+
// writes in that moment lands on an origin that was just wiped. A guess,
|
|
62
|
+
// with a margin over that. A wipe that did not go through the first time
|
|
63
|
+
// gets its second chance then too.
|
|
64
|
+
const WIPE_AGAIN_MS = 3000;
|
|
48
65
|
const WORKER_PFX = 'tiles-worker-';
|
|
49
66
|
const SND_WORKER_LOAD = `${WORKER_PFX}load`; // tell worker to roll
|
|
50
67
|
const RCV_WORKER_READY = `${WORKER_PFX}ready`; // worker ready
|
|
@@ -53,13 +70,136 @@ const SND_WORKER_RESPONSE = `${WORKER_PFX}response`; // respond to a worker
|
|
|
53
70
|
const TILES_WARNING = `${TILES_PFX}warn`; // worker warnings
|
|
54
71
|
const TILES_ERROR = `${SHUTTLE_PFX}error`; // shuttle errors
|
|
55
72
|
const RCV_DOCK_STOPPED = 'tiles-dock-stopped'; // the dock stopped the tile
|
|
73
|
+
const RCV_DOCK_READY = 'tiles-dock-ready'; // the dock is there, and of which generation
|
|
74
|
+
// The generations of the runtime this loader speaks: what a dock names when
|
|
75
|
+
// it says it is ready (see GENERATION in @dasl/tile-server's dock.js). A dock
|
|
76
|
+
// from before that message names none, and is taken as it comes.
|
|
77
|
+
const GENERATIONS = [3];
|
|
56
78
|
// Tiles protocols (https://dasl.ing/tp-data.html and friends): the shuttle
|
|
57
79
|
// relays `tiles-protocol-up-<protocol>-<kind>` from the tile to us, and
|
|
58
80
|
// `tiles-protocol-down-<protocol>-<kind>` from us to the tile.
|
|
59
81
|
const PROTOCOL_UP = 'tiles-protocol-up-';
|
|
60
82
|
const PROTOCOL_DOWN = 'tiles-protocol-down-';
|
|
61
|
-
// Warnings and errors come as a list
|
|
62
|
-
|
|
83
|
+
// Warnings and errors come as a list of things to log; anything else is
|
|
84
|
+
// logged as it is. The runtime's own lists are two or three texts, but the
|
|
85
|
+
// tile can have its runtime say anything, and a console writes out what it
|
|
86
|
+
// is handed: every entry, and some things in full each time they are there,
|
|
87
|
+
// however little it took to say them. So the first few entries are logged,
|
|
88
|
+
// however many the list says it has, and of those what is a text, a number
|
|
89
|
+
// or a boolean; anything else is named by its kind ("[object Array]").
|
|
90
|
+
const LOG_MAX = 8;
|
|
91
|
+
const loggable = (v) => (v === undefined || v === null || typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean'
|
|
92
|
+
? v
|
|
93
|
+
: Object.prototype.toString.call(v));
|
|
94
|
+
const asList = (msg) => (Array.isArray(msg) ? msg.slice(0, LOG_MAX) : [msg]).map(loggable);
|
|
95
|
+
// How long a tile gets to start before its host is told that it has not
|
|
96
|
+
// (MothershipConfig.startDeadline). A guess: on a busy machine a tile with
|
|
97
|
+
// nothing wrong with it has been seen to take eight seconds.
|
|
98
|
+
const START_DEADLINE_MS = 15000;
|
|
99
|
+
// The sandbox of the frame renderContent() makes, when its host says none:
|
|
100
|
+
// what a tile needs to run, and forms to submit to itself. No dialogs and no
|
|
101
|
+
// downloads, which the runtime would grant (see renderContent()).
|
|
102
|
+
const FRAME_SANDBOX = 'allow-scripts allow-same-origin allow-forms';
|
|
103
|
+
// What the frame renderContent() makes refuses a tile when its host says
|
|
104
|
+
// nothing of it: synchronous requests. One holds up the thread the tile
|
|
105
|
+
// shares with its runtime and gets the tile nothing: its own files never
|
|
106
|
+
// come that way, since Chrome and Safari do not hand such a request to the
|
|
107
|
+
// tile's worker, and Firefox hands it to a worker whose answer has to come
|
|
108
|
+
// through that same thread. What is lost is a synchronous request for a
|
|
109
|
+
// blob: or data: address.
|
|
110
|
+
const FRAME_REFUSE = ['sync-xhr'];
|
|
111
|
+
/**
|
|
112
|
+
* The features a host can refuse a tile (`FrameOptions.refuse`), by name:
|
|
113
|
+
* `refusableFeatures` in @dasl/tile-lexicon. They are those of its user's
|
|
114
|
+
* devices and attention that the runtime hands on to a tile whose host's
|
|
115
|
+
* frame allows them, and those a browser gives every frame unasked
|
|
116
|
+
* (picture-in-picture, the gamepad, synchronous requests, the client hints
|
|
117
|
+
* and more), which a tile keeps unless they are refused. `refuse: REFUSABLE`
|
|
118
|
+
* refuses all of them. Chrome knows every name; Firefox and Safari go by the
|
|
119
|
+
* names they know and pass over the others, so the same list refuses less
|
|
120
|
+
* there (the README says what, in each).
|
|
121
|
+
*/
|
|
122
|
+
export const REFUSABLE = refusableFeatures;
|
|
123
|
+
// A timer is set for no longer than this: a longer wait comes round to none.
|
|
124
|
+
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
125
|
+
// A frame that is out of the document when its deadline passes is looked at
|
|
126
|
+
// again later, and no sooner than this, however short the deadline.
|
|
127
|
+
const OUT_MS = 1000;
|
|
128
|
+
// What a tile's runtime comes up through, in order: what a start is waiting
|
|
129
|
+
// for, until it waits for nothing.
|
|
130
|
+
const STAGES = ['dock', 'shuttle', 'worker', 'content'];
|
|
131
|
+
// What a tile may ask of the page it runs in, when the host does not say
|
|
132
|
+
// (MothershipConfig.budget). Guesses, every one, at what an ordinary tile
|
|
133
|
+
// never comes near and a page can bear from each tile it shows.
|
|
134
|
+
const BUDGET = {
|
|
135
|
+
// A tile that tells its host of every frame it draws, or of the pointer as
|
|
136
|
+
// it moves, sends 60 to 120 messages a second.
|
|
137
|
+
messagesPerSecond: 200,
|
|
138
|
+
messageBurst: 400,
|
|
139
|
+
// More than a browser asks of one server at once over HTTP/1.1 (six), so
|
|
140
|
+
// that a loader which fetches is not slowed much; few enough that what a
|
|
141
|
+
// loader is at work on for one tile stays small. (An answer counts until
|
|
142
|
+
// it is posted, not until the tile has it: what is on its way down, copied
|
|
143
|
+
// at every hop, is as much as the tile asked for faster than it reads.)
|
|
144
|
+
requestsInFlight: 16,
|
|
145
|
+
requestsQueued: 2000,
|
|
146
|
+
logLines: 10,
|
|
147
|
+
};
|
|
148
|
+
// A resource that has been resolving for this long no longer counts as in
|
|
149
|
+
// flight: a server that stalls on some of a tile's resources does not get to
|
|
150
|
+
// keep the others waiting behind them for good.
|
|
151
|
+
const HOLD_MS = 10000;
|
|
152
|
+
// No resource of a tile's has a path longer than this. A request for one is
|
|
153
|
+
// answered without being kept: what waits its turn has to stay small.
|
|
154
|
+
const MAX_PATH = 8192;
|
|
155
|
+
// Requests that waited their turn are started this many at a time, and the
|
|
156
|
+
// page's thread is given back before the next lot. A loader that answers
|
|
157
|
+
// some resources at once would otherwise have each one start the next as it
|
|
158
|
+
// ended, and all that waited would be resolved in one go.
|
|
159
|
+
const RUN = 16;
|
|
160
|
+
// What a tile's runtime says for itself (that it is ready, that the tile was
|
|
161
|
+
// closed, or stopped) is counted apart from what the tile says, and each
|
|
162
|
+
// kind apart from the others. A tile can spend its own budget, and it can
|
|
163
|
+
// have its runtime say any of these as often as it likes: neither may keep
|
|
164
|
+
// its host from hearing that it was stopped. (What is left to a tile is to
|
|
165
|
+
// have its runtime say that very thing, and then the host has heard it.)
|
|
166
|
+
// There are a few of each in a tile's life, and a few are all that is heard.
|
|
167
|
+
const OWN = new Set([RCV_DOCK_READY, RCV_DOCK_STOPPED, RCV_SHUTTLE_READY, RCV_SHUTTLE_CLOSED, RCV_WORKER_READY]);
|
|
168
|
+
const OWN_BURST = 20;
|
|
169
|
+
const OWN_PER_SECOND = 2;
|
|
170
|
+
// The budget a host gave, with ours wherever it gave none we can use.
|
|
171
|
+
function budgetFrom(given) {
|
|
172
|
+
const budget = { ...BUDGET };
|
|
173
|
+
for (const name of Object.keys(BUDGET)) {
|
|
174
|
+
const n = given?.[name];
|
|
175
|
+
// (Nothing is ever resolved with no request in flight.)
|
|
176
|
+
if (typeof n === 'number' && n >= (name === 'requestsInFlight' ? 1 : 0))
|
|
177
|
+
budget[name] = n;
|
|
178
|
+
}
|
|
179
|
+
return budget;
|
|
180
|
+
}
|
|
181
|
+
// So many at once, and so many a second from then on.
|
|
182
|
+
class Allowance {
|
|
183
|
+
#burst;
|
|
184
|
+
#perSecond;
|
|
185
|
+
#left;
|
|
186
|
+
#at = performance.now();
|
|
187
|
+
constructor(burst, perSecond) {
|
|
188
|
+
this.#burst = this.#left = burst;
|
|
189
|
+
this.#perSecond = perSecond;
|
|
190
|
+
}
|
|
191
|
+
// Takes one, if there is one to take.
|
|
192
|
+
spend() {
|
|
193
|
+
const now = performance.now();
|
|
194
|
+
if (now > this.#at)
|
|
195
|
+
this.#left = Math.min(this.#burst, this.#left + (now - this.#at) * this.#perSecond / 1000);
|
|
196
|
+
this.#at = now;
|
|
197
|
+
if (this.#left < 1)
|
|
198
|
+
return false;
|
|
199
|
+
this.#left--;
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
63
203
|
export class TileMothership {
|
|
64
204
|
#loaders = [];
|
|
65
205
|
#conf;
|
|
@@ -70,11 +210,31 @@ export class TileMothership {
|
|
|
70
210
|
// loader, for some loaders the tile's whole content) is the host's alone to
|
|
71
211
|
// keep or drop. Nothing has to be called for that.
|
|
72
212
|
#tiles = new WeakMap();
|
|
213
|
+
// What each tile has used of its budget: weakly too.
|
|
214
|
+
#budget;
|
|
215
|
+
#usage = new WeakMap();
|
|
73
216
|
constructor(conf = {}) {
|
|
74
217
|
this.#conf = conf;
|
|
218
|
+
this.#budget = budgetFrom(conf.budget);
|
|
219
|
+
// An origin named by a tile's CID is its tile's: no configuration names
|
|
220
|
+
// it, and a mothership runs a tile either there or on the one it names.
|
|
221
|
+
const named = this.#namedHost();
|
|
222
|
+
if (named && conf.cidOrigins)
|
|
223
|
+
throw new Error('A mothership runs tiles on a named origin (namedOrigin) or on the origins named by their CID (cidOrigins), not both.');
|
|
224
|
+
if (named && isCidLabel(named.split('.')[0].toLowerCase()))
|
|
225
|
+
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.`);
|
|
75
226
|
}
|
|
227
|
+
// What init() has listening, kept so that destroy() can take it away.
|
|
228
|
+
#listener;
|
|
229
|
+
/**
|
|
230
|
+
* Starts listening to the tiles' docks. Once is enough, and once is all it
|
|
231
|
+
* does: called again it changes nothing, where a second listener would
|
|
232
|
+
* answer every request twice.
|
|
233
|
+
*/
|
|
76
234
|
init() {
|
|
77
|
-
|
|
235
|
+
if (this.#listener)
|
|
236
|
+
return;
|
|
237
|
+
this.#listener = async (ev) => {
|
|
78
238
|
const { action } = ev.data || {};
|
|
79
239
|
if (typeof action !== 'string')
|
|
80
240
|
return;
|
|
@@ -87,46 +247,81 @@ export class TileMothership {
|
|
|
87
247
|
if (!tile || ev.origin !== this.getLoadOrigin())
|
|
88
248
|
return;
|
|
89
249
|
const id = tile.runtimeId;
|
|
250
|
+
// A tile says as much as it likes, and can have its runtime say the
|
|
251
|
+
// same: what is listened to is bounded. Requests for its resources
|
|
252
|
+
// have a bound of their own, below.
|
|
253
|
+
const used = this.#used(tile);
|
|
254
|
+
if (action !== RCV_WORKER_REQUEST && !this.#admit(used, action))
|
|
255
|
+
return;
|
|
90
256
|
if (action === TILES_WARNING) {
|
|
91
|
-
|
|
257
|
+
this.#log(used, 'warn', `[W:${id}]`, ...asList(ev.data.msg));
|
|
258
|
+
// The worker gave up on a request of its tile's that we never
|
|
259
|
+
// answered. Before the tile is up, that is its root document.
|
|
260
|
+
if (ev.data.code === 'unanswered')
|
|
261
|
+
tile.failed('content');
|
|
262
|
+
// The runtime on the origin named by the tile's CID refused what it
|
|
263
|
+
// was handed for the tile's manifest.
|
|
264
|
+
if (ev.data.code === 'integrity')
|
|
265
|
+
tile.failed('integrity');
|
|
92
266
|
}
|
|
93
267
|
else if (action === TILES_ERROR) {
|
|
94
|
-
|
|
268
|
+
this.#log(used, 'error', `[S:${id}]`, ...asList(ev.data.msg));
|
|
269
|
+
// With a code, it is what kept the runtime from starting the tile.
|
|
270
|
+
const { code } = ev.data;
|
|
271
|
+
if (code === 'worker' || code === 'unsupported')
|
|
272
|
+
tile.failed(code);
|
|
95
273
|
}
|
|
96
274
|
else if (action === RCV_DOCK_STOPPED) {
|
|
97
275
|
const reason = ev.data.payload?.reason === 'blocked' ? 'blocked' : 'navigated';
|
|
98
|
-
|
|
276
|
+
this.#log(used, 'error', `[D:${id}] The tile's runtime tried to navigate (${reason}); the tile was stopped.`);
|
|
99
277
|
tile.dispatchEvent(Object.assign(new Event('stopped'), { reason }));
|
|
278
|
+
// The host has been told what became of it: it is waited for no longer.
|
|
279
|
+
tile.awaits();
|
|
100
280
|
}
|
|
101
281
|
else if (action === RCV_SHUTTLE_CLOSED) {
|
|
102
282
|
tile.runtimeClosed();
|
|
103
283
|
}
|
|
284
|
+
else if (action === RCV_DOCK_READY) {
|
|
285
|
+
// The loading server is there. Whether we can talk to what it runs
|
|
286
|
+
// is in the generation it names; a number we do not know is said to
|
|
287
|
+
// the host, and the rest is left to happen or not.
|
|
288
|
+
const generation = ev.data.payload?.generation;
|
|
289
|
+
// And a tile on the origin named by its CID is run there only by a
|
|
290
|
+
// runtime that holds that origin to it, which the dock says.
|
|
291
|
+
tile.dockSaid(ev.data.payload?.cid === true);
|
|
292
|
+
if (!GENERATIONS.includes(generation))
|
|
293
|
+
tile.failed('version', typeof generation === 'number' ? { generation } : {});
|
|
294
|
+
else if (tile.cidOrigin && !tile.bound)
|
|
295
|
+
tile.failed('version');
|
|
296
|
+
else
|
|
297
|
+
tile.awaits('shuttle');
|
|
298
|
+
}
|
|
104
299
|
else if (action === RCV_SHUTTLE_READY) {
|
|
105
|
-
tile
|
|
300
|
+
// The runtime on the origin named by the tile's CID is handed the
|
|
301
|
+
// manifest that CID names, and nothing if it does not hold it.
|
|
302
|
+
const manifest = tile.cidOrigin;
|
|
303
|
+
if (manifest && !tile.bound)
|
|
304
|
+
return tile.failed('version');
|
|
305
|
+
tile.awaits('worker');
|
|
306
|
+
tile.post(SND_WORKER_LOAD, { id, ...(manifest ? { manifest } : {}) });
|
|
106
307
|
}
|
|
107
308
|
else if (action === RCV_WORKER_READY) {
|
|
108
309
|
tile.dispatchEvent(new Event('load'));
|
|
310
|
+
tile.awaits('content');
|
|
109
311
|
}
|
|
110
312
|
else if (action === RCV_WORKER_REQUEST) {
|
|
111
313
|
const { type, payload } = ev.data;
|
|
112
314
|
if (type === 'resolve-path') {
|
|
315
|
+
// The worker always sends one; without it there is no request to answer.
|
|
316
|
+
if (!payload || typeof payload !== 'object')
|
|
317
|
+
return;
|
|
113
318
|
const { path, requestId } = payload;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
console.error(`Could not resolve ${path}:`, err);
|
|
121
|
-
res = { ok: false, status: 502, statusText: 'Bad Gateway' };
|
|
122
|
-
}
|
|
123
|
-
// Every document the worker serves, 404s included, carries the
|
|
124
|
-
// policy: the tile can script anything on its origin, so nothing
|
|
125
|
-
// on it may be unconfined.
|
|
126
|
-
const response = res.ok
|
|
127
|
-
? { status: res.status, headers: this.hardenResponse(res.headers), body: res.body }
|
|
128
|
-
: { status: res.status, headers: this.hardenResponse({ 'content-type': 'text/plain' }), body: undefined };
|
|
129
|
-
tile.post(SND_WORKER_RESPONSE, { requestId, response });
|
|
319
|
+
// Nor is there one without a path, which is a string, and a number
|
|
320
|
+
// to answer it by: nothing else is the worker's, and nothing else is
|
|
321
|
+
// ours to resolve or spell out.
|
|
322
|
+
if (typeof path !== 'string' || typeof requestId !== 'number')
|
|
323
|
+
return;
|
|
324
|
+
this.#request(tile, used, path, requestId);
|
|
130
325
|
}
|
|
131
326
|
}
|
|
132
327
|
else if (action.startsWith(PROTOCOL_UP)) {
|
|
@@ -137,9 +332,184 @@ export class TileMothership {
|
|
|
137
332
|
const protocol = rest.slice(0, dash);
|
|
138
333
|
const kind = rest.slice(dash + 1);
|
|
139
334
|
const { payload } = ev.data;
|
|
140
|
-
|
|
335
|
+
// What the dock said of the user as it passed this on, when it said.
|
|
336
|
+
const activated = ev.data.dock?.activated;
|
|
337
|
+
const dock = typeof activated === 'boolean' ? { dock: { activated } } : {};
|
|
338
|
+
tile.dispatchEvent(Object.assign(new Event('protocol'), { protocol, kind, payload, ...dock }));
|
|
141
339
|
}
|
|
142
|
-
}
|
|
340
|
+
};
|
|
341
|
+
window.addEventListener('message', this.#listener);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Stops listening: what init() did, undone. The page's listener is what
|
|
345
|
+
* keeps a mothership, with its loaders and all they hold, for as long as
|
|
346
|
+
* the page lives; without it they are the host's to keep or drop. Tiles
|
|
347
|
+
* that are still running get no more answers, so close them first.
|
|
348
|
+
* init() starts it again.
|
|
349
|
+
*/
|
|
350
|
+
destroy() {
|
|
351
|
+
if (!this.#listener)
|
|
352
|
+
return;
|
|
353
|
+
window.removeEventListener('message', this.#listener);
|
|
354
|
+
this.#listener = undefined;
|
|
355
|
+
}
|
|
356
|
+
// What a tile has used of its budget. A tile that is rendered again starts
|
|
357
|
+
// again, and its budget with it.
|
|
358
|
+
#used(tile) {
|
|
359
|
+
let used = this.#usage.get(tile);
|
|
360
|
+
if (!used || used.id !== tile.runtimeId) {
|
|
361
|
+
const { messageBurst, messagesPerSecond } = this.#budget;
|
|
362
|
+
used = {
|
|
363
|
+
id: tile.runtimeId,
|
|
364
|
+
said: new Allowance(messageBurst, messagesPerSecond),
|
|
365
|
+
own: new Map(),
|
|
366
|
+
over: false,
|
|
367
|
+
inFlight: 0,
|
|
368
|
+
waiting: [],
|
|
369
|
+
run: 0,
|
|
370
|
+
paused: false,
|
|
371
|
+
logged: 0,
|
|
372
|
+
};
|
|
373
|
+
this.#usage.set(tile, used);
|
|
374
|
+
}
|
|
375
|
+
return used;
|
|
376
|
+
}
|
|
377
|
+
// Whether a message is within what its tile may send. One that is not is
|
|
378
|
+
// dropped, and the first time that is said.
|
|
379
|
+
#admit(used, action) {
|
|
380
|
+
let allowance = used.said;
|
|
381
|
+
if (OWN.has(action)) {
|
|
382
|
+
allowance = used.own.get(action) ?? new Allowance(OWN_BURST, OWN_PER_SECOND);
|
|
383
|
+
used.own.set(action, allowance);
|
|
384
|
+
}
|
|
385
|
+
if (allowance.spend())
|
|
386
|
+
return true;
|
|
387
|
+
this.#over(used, 'sends more messages');
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
#over(used, how) {
|
|
391
|
+
if (used.over)
|
|
392
|
+
return;
|
|
393
|
+
used.over = true;
|
|
394
|
+
this.#log(used, 'warn', `[M:${used.id}] This tile ${how} than its budget allows: what is over is dropped.`);
|
|
395
|
+
}
|
|
396
|
+
// What a tile has logged is logged a few times and then no more. A tile's
|
|
397
|
+
// runtime can be made to say anything, as often as the tile likes, and so
|
|
398
|
+
// can its requests be made to fail.
|
|
399
|
+
#log(used, level, ...args) {
|
|
400
|
+
const { logLines } = this.#budget;
|
|
401
|
+
if (used.logged >= logLines)
|
|
402
|
+
return;
|
|
403
|
+
used.logged++;
|
|
404
|
+
console[level](...args);
|
|
405
|
+
if (used.logged >= logLines)
|
|
406
|
+
console.warn(`[M:${used.id}] Nothing more of this tile is logged.`);
|
|
407
|
+
}
|
|
408
|
+
// A tile's requests for its resources are resolved a few at a time, in the
|
|
409
|
+
// order they came, and the rest wait: resolving is the loader's work (a
|
|
410
|
+
// fetch, for some), and every answer is copied at each hop on its way to
|
|
411
|
+
// the tile. So that what is in flight is what the page chose, not what the
|
|
412
|
+
// tile asked for.
|
|
413
|
+
#request(tile, used, path, requestId) {
|
|
414
|
+
const { requestsInFlight, requestsQueued } = this.#budget;
|
|
415
|
+
if (path.length > MAX_PATH)
|
|
416
|
+
return this.#turnDown(tile, used, requestId, 404);
|
|
417
|
+
if (used.inFlight < requestsInFlight)
|
|
418
|
+
return void this.#resolve(tile, used, path, requestId);
|
|
419
|
+
if (used.waiting.length < requestsQueued)
|
|
420
|
+
return void used.waiting.push({ path, requestId });
|
|
421
|
+
// More at once than a tile has any use for.
|
|
422
|
+
this.#over(used, 'asks for more of its resources at once');
|
|
423
|
+
this.#turnDown(tile, used, requestId, 503);
|
|
424
|
+
}
|
|
425
|
+
// A request that is not resolved is answered all the same, so that a worker
|
|
426
|
+
// does not wait for nothing: for as long as its tile has messages to
|
|
427
|
+
// spend, as each answer is one more that the tile has us send.
|
|
428
|
+
#turnDown(tile, used, requestId, status) {
|
|
429
|
+
if (!used.said.spend())
|
|
430
|
+
return;
|
|
431
|
+
tile.post(SND_WORKER_RESPONSE, { requestId, response: { status, headers: this.hardenResponse({ 'content-type': 'text/plain' }), body: undefined } });
|
|
432
|
+
}
|
|
433
|
+
// The next of the requests that wait, while there is room for them: a few
|
|
434
|
+
// (RUN), and then the page's thread is given back before any more. Those
|
|
435
|
+
// of a frame the tile has left are for a worker that is gone.
|
|
436
|
+
#next(tile, used) {
|
|
437
|
+
if (tile.runtimeId !== used.id)
|
|
438
|
+
used.waiting.length = 0;
|
|
439
|
+
while (used.waiting.length && used.inFlight < this.#budget.requestsInFlight) {
|
|
440
|
+
if (used.run >= RUN)
|
|
441
|
+
return this.#pause(tile, used);
|
|
442
|
+
used.run++;
|
|
443
|
+
const { path, requestId } = used.waiting.shift();
|
|
444
|
+
void this.#resolve(tile, used, path, requestId);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
// Lets the page get on with whatever else it has to do, then goes on with
|
|
448
|
+
// the requests that wait: in a task of its own, by a message to ourselves.
|
|
449
|
+
// (Not by a timer: a browser holds those back in a page that is not in
|
|
450
|
+
// view, and a tile that loads there would get its resources a few a second.)
|
|
451
|
+
#pause(tile, used) {
|
|
452
|
+
if (used.paused)
|
|
453
|
+
return;
|
|
454
|
+
used.paused = true;
|
|
455
|
+
const { port1, port2 } = new MessageChannel();
|
|
456
|
+
port1.onmessage = () => {
|
|
457
|
+
port1.close();
|
|
458
|
+
used.paused = false;
|
|
459
|
+
used.run = 0;
|
|
460
|
+
this.#next(tile, used);
|
|
461
|
+
};
|
|
462
|
+
port2.postMessage(null);
|
|
463
|
+
}
|
|
464
|
+
async #resolve(tile, used, path, requestId) {
|
|
465
|
+
// In flight until it is answered, or has been long enough at it.
|
|
466
|
+
used.inFlight++;
|
|
467
|
+
let held = true;
|
|
468
|
+
const release = () => {
|
|
469
|
+
if (!held)
|
|
470
|
+
return;
|
|
471
|
+
held = false;
|
|
472
|
+
used.inFlight--;
|
|
473
|
+
this.#next(tile, used);
|
|
474
|
+
};
|
|
475
|
+
const hold = setTimeout(release, HOLD_MS);
|
|
476
|
+
// (Under Node, where the tests run, a timer would keep the process up.)
|
|
477
|
+
hold.unref?.();
|
|
478
|
+
try {
|
|
479
|
+
let response;
|
|
480
|
+
let served = false;
|
|
481
|
+
try {
|
|
482
|
+
const res = await tile.resolvePath(path);
|
|
483
|
+
// Every document the worker serves, 404s included, carries the
|
|
484
|
+
// policy: the tile can script anything on its origin, so nothing
|
|
485
|
+
// on it may be unconfined.
|
|
486
|
+
response = res.ok
|
|
487
|
+
? { status: res.status, headers: this.hardenResponse(res.headers), body: res.body }
|
|
488
|
+
: { status: res.status, headers: this.hardenResponse({ 'content-type': 'text/plain' }), body: undefined };
|
|
489
|
+
served = res.ok;
|
|
490
|
+
}
|
|
491
|
+
catch (err) {
|
|
492
|
+
// Answer all the same, or the worker waits for ever. Resolving the
|
|
493
|
+
// path can throw, and so can hardening what it resolved to: a
|
|
494
|
+
// resource's headers are the tile's to write.
|
|
495
|
+
this.#log(used, 'error', `Could not resolve ${path}:`, err);
|
|
496
|
+
response = { status: 502, headers: this.hardenResponse({ 'content-type': 'text/plain' }), body: undefined };
|
|
497
|
+
}
|
|
498
|
+
tile.post(SND_WORKER_RESPONSE, { requestId, response });
|
|
499
|
+
// The root document is what a tile starts with: once it is on its
|
|
500
|
+
// way the tile is up, and when it is not to be had the host hears.
|
|
501
|
+
// (Unless the tile has left that frame while we were resolving.)
|
|
502
|
+
if (path === '/' && tile.runtimeId === used.id) {
|
|
503
|
+
if (served)
|
|
504
|
+
tile.awaits();
|
|
505
|
+
else
|
|
506
|
+
tile.failed('content');
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
finally {
|
|
510
|
+
clearTimeout(hold);
|
|
511
|
+
release();
|
|
512
|
+
}
|
|
143
513
|
}
|
|
144
514
|
/**
|
|
145
515
|
* The headers a tile document ships with: the resource's own, plus the
|
|
@@ -172,14 +542,37 @@ export class TileMothership {
|
|
|
172
542
|
return {
|
|
173
543
|
id: tile.runtimeId,
|
|
174
544
|
webrtcGuard: this.#conf.webrtcGuard ?? 'exhaust',
|
|
175
|
-
ephemeral:
|
|
545
|
+
ephemeral: tile.ephemeral,
|
|
176
546
|
...(sizing ? { sizing } : {}),
|
|
177
547
|
};
|
|
178
548
|
}
|
|
179
|
-
/**
|
|
549
|
+
/** @internal How long a tile gets to start, in milliseconds; 0 for no deadline. */
|
|
550
|
+
get startDeadline() {
|
|
551
|
+
const ms = this.#conf.startDeadline;
|
|
552
|
+
if (typeof ms !== 'number' || Number.isNaN(ms) || ms < 0)
|
|
553
|
+
return START_DEADLINE_MS;
|
|
554
|
+
return Number.isFinite(ms) ? Math.min(ms, MAX_TIMER_MS) : 0;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Whether tiles get an origin of their own for every load (no
|
|
558
|
+
* `namedOrigin`). With `cidOrigins`, those that have a CID to be named by
|
|
559
|
+
* do not: see `tile.ephemeral`.
|
|
560
|
+
*/
|
|
180
561
|
get ephemeral() {
|
|
181
562
|
return !this.#namedHost();
|
|
182
563
|
}
|
|
564
|
+
/**
|
|
565
|
+
* @internal The host of the origin named by a tile's CID: when tiles run
|
|
566
|
+
* there (`cidOrigins`), the tile has a CID to be named by, and the loading
|
|
567
|
+
* server's tile origins are under its own host (`load.<base>`).
|
|
568
|
+
*/
|
|
569
|
+
cidHost(tile) {
|
|
570
|
+
const cid = tile.manifestBytes?.cid;
|
|
571
|
+
if (this.#conf.cidOrigins !== true || !isCidLabel(cid))
|
|
572
|
+
return undefined;
|
|
573
|
+
const { host } = new URL(this.getLoadOrigin());
|
|
574
|
+
return host.startsWith(LOAD_LABEL) ? `${cid}.${host.slice(LOAD_LABEL.length)}` : undefined;
|
|
575
|
+
}
|
|
183
576
|
// The named origin's host, if the configuration names one.
|
|
184
577
|
#namedHost() {
|
|
185
578
|
const named = this.#conf.namedOrigin ?? this.#conf.tileHost;
|
|
@@ -187,6 +580,93 @@ export class TileMothership {
|
|
|
187
580
|
return undefined;
|
|
188
581
|
return /^https?:\/\//.test(named) ? new URL(named).host : named;
|
|
189
582
|
}
|
|
583
|
+
// The host of a tile origin for one load, as the loading server makes them:
|
|
584
|
+
// twenty random letters under its base host, which is its own host without
|
|
585
|
+
// the `load.` it starts with (and with its port). A loading server that is
|
|
586
|
+
// somewhere else does not say where its tile origins are: it is then left
|
|
587
|
+
// to pick the origin itself, in a redirect, as it always could.
|
|
588
|
+
#freshHost() {
|
|
589
|
+
const { host } = new URL(this.getLoadOrigin());
|
|
590
|
+
return host.startsWith(LOAD_LABEL) ? `${randomLabel()}.${host.slice(LOAD_LABEL.length)}` : undefined;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* @internal The tile origin a frame's address names, when the address is
|
|
594
|
+
* the dock's on our loading server and names one (getLoadSource() does).
|
|
595
|
+
*/
|
|
596
|
+
originNamedIn(src) {
|
|
597
|
+
if (typeof src !== 'string' || !src)
|
|
598
|
+
return undefined;
|
|
599
|
+
let url;
|
|
600
|
+
try {
|
|
601
|
+
url = new URL(src);
|
|
602
|
+
}
|
|
603
|
+
catch {
|
|
604
|
+
return undefined;
|
|
605
|
+
}
|
|
606
|
+
if (url.origin !== this.getLoadOrigin() || url.pathname !== dockPath)
|
|
607
|
+
return undefined;
|
|
608
|
+
const [host, ...more] = url.searchParams.getAll('tile');
|
|
609
|
+
if (!host || more.length || !/^[a-z0-9.-]+(?::\d+)?$/.test(host))
|
|
610
|
+
return undefined;
|
|
611
|
+
return `${url.protocol}//${host}`;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* @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.
|
|
616
|
+
*
|
|
617
|
+
* The wipe answers a page of the origin it empties and no one else, and
|
|
618
|
+
* this page is not one. So a frame is made, out of sight, that loads the
|
|
619
|
+
* dock for that origin again; the dock frames the runtime there, a shuttle
|
|
620
|
+
* that never had a tile in it, and is told to close and wipe, in the words
|
|
621
|
+
* a tile's own frame is told. That shuttle lets the worker go and has the
|
|
622
|
+
* loading server wipe the origin, and the answer comes back up. Nothing in
|
|
623
|
+
* that frame was ever within the tile's reach, so its answer is not the
|
|
624
|
+
* tile's to give; and the shuttle only gets to run once the tile's own
|
|
625
|
+
* pages, which share its origin, are done with whatever they do on their
|
|
626
|
+
* way out, so it does not come too soon.
|
|
627
|
+
*
|
|
628
|
+
* `credentialless` is for a tile that ran in a credentialless frame: the
|
|
629
|
+
* browser keeps what such a frame stores apart, and only another one
|
|
630
|
+
* reaches it.
|
|
631
|
+
*
|
|
632
|
+
* Resolves to whether the origin was wiped: true when the runtime says so,
|
|
633
|
+
* false when it says not, or says nothing for WIPE_MS.
|
|
634
|
+
*/
|
|
635
|
+
wipeOrigin(origin, credentialless = false) {
|
|
636
|
+
const loadOrigin = this.getLoadOrigin();
|
|
637
|
+
const url = new URL(dockPath, loadOrigin);
|
|
638
|
+
url.searchParams.set('tile', new URL(origin).host);
|
|
639
|
+
return new Promise((resolve) => {
|
|
640
|
+
const ifr = el('iframe', {
|
|
641
|
+
src: url.href,
|
|
642
|
+
// The dock and the runtime need no more than this to wipe.
|
|
643
|
+
sandbox: 'allow-scripts allow-same-origin',
|
|
644
|
+
// The attribute is there or it is not; it has no value.
|
|
645
|
+
credentialless: credentialless ? '' : undefined,
|
|
646
|
+
hidden: '',
|
|
647
|
+
style: { display: 'none' },
|
|
648
|
+
});
|
|
649
|
+
const done = (wiped) => {
|
|
650
|
+
clearTimeout(timer);
|
|
651
|
+
window.removeEventListener('message', onMessage);
|
|
652
|
+
ifr.remove();
|
|
653
|
+
resolve(wiped);
|
|
654
|
+
};
|
|
655
|
+
// Only that frame's window, on the loading server, speaks for it.
|
|
656
|
+
const onMessage = (ev) => {
|
|
657
|
+
if (!ev.source || ev.source !== ifr.contentWindow || ev.origin !== loadOrigin)
|
|
658
|
+
return;
|
|
659
|
+
if (ev.data?.action === RCV_SHUTTLE_CLOSED)
|
|
660
|
+
done(ev.data.payload?.wiped !== false);
|
|
661
|
+
};
|
|
662
|
+
const timer = setTimeout(() => done(false), WIPE_MS);
|
|
663
|
+
window.addEventListener('message', onMessage);
|
|
664
|
+
ifr.addEventListener('load', () => {
|
|
665
|
+
ifr.contentWindow?.postMessage({ action: SND_SHUTTLE_CLOSE, payload: { wipe: true } }, loadOrigin);
|
|
666
|
+
}, { once: true });
|
|
667
|
+
(document.body ?? document.documentElement).append(ifr);
|
|
668
|
+
});
|
|
669
|
+
}
|
|
190
670
|
// Adds a loader that will handle matching requests to load a tile.
|
|
191
671
|
// - `loader` is an object that knows how to load a tile for a specific scheme
|
|
192
672
|
// (and types)
|
|
@@ -203,26 +683,79 @@ export class TileMothership {
|
|
|
203
683
|
return new URL(/^https?:\/\//.test(domain) ? domain : `https://${domain}`).origin;
|
|
204
684
|
}
|
|
205
685
|
/**
|
|
206
|
-
* What a tile's frame loads: the dock, on the loading server
|
|
207
|
-
*
|
|
686
|
+
* What a tile's frame loads: the dock, on the loading server, for a tile
|
|
687
|
+
* origin that the address names. Unless the configuration names one
|
|
688
|
+
* (`namedOrigin`), that is a fresh origin at every call, made here as the
|
|
689
|
+
* loading server would make it, so an address is for one frame. Named
|
|
690
|
+
* here, the origin is one the page knows, which is what lets it be wiped
|
|
691
|
+
* once the frame its tile ran in is gone (see Tile.close()); and it saves
|
|
692
|
+
* the redirect in which the loading server would have picked it. With
|
|
693
|
+
* `cidOrigins`, for a `tile` that has a CID to be named by, it is the
|
|
694
|
+
* origin that CID names.
|
|
208
695
|
*/
|
|
209
|
-
getLoadSource() {
|
|
696
|
+
getLoadSource(tile) {
|
|
210
697
|
const url = new URL(dockPath, this.getLoadOrigin());
|
|
211
|
-
const named = this.#namedHost();
|
|
698
|
+
const named = this.#namedHost() ?? (tile && this.cidHost(tile)) ?? this.#freshHost();
|
|
212
699
|
if (named)
|
|
213
700
|
url.searchParams.set('tile', named);
|
|
214
701
|
return url.href;
|
|
215
702
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
703
|
+
/**
|
|
704
|
+
* Loads a tile: the first loader that has one for `url` gives it, and it
|
|
705
|
+
* is `false` when none does.
|
|
706
|
+
*
|
|
707
|
+
* `options` say which tile is expected (see TileLoadOptions), and a tile
|
|
708
|
+
* that is not that one is not returned. With options, even none of them
|
|
709
|
+
* (`{}`), a tile that could not be loaded is a rejection, with the
|
|
710
|
+
* TileLoadError that says why. With the URL alone, which is all there was
|
|
711
|
+
* to call this with before, it is `false` as it is for a URL no loader
|
|
712
|
+
* knows, and the error is logged: a host that only looks for a tile keeps
|
|
713
|
+
* working.
|
|
714
|
+
*/
|
|
715
|
+
async loadTile(url, options) {
|
|
716
|
+
try {
|
|
717
|
+
for (const ldr of this.#loaders) {
|
|
718
|
+
const tile = await ldr.load(url, this, options);
|
|
719
|
+
if (!tile)
|
|
720
|
+
continue;
|
|
721
|
+
// A loader that knows nothing of expected CIDs must not pass for one
|
|
722
|
+
// that checked: whatever made the tile, it has to be one asked for.
|
|
723
|
+
const { cid, manifestCid } = options ?? {};
|
|
724
|
+
const expected = cid !== undefined || manifestCid !== undefined;
|
|
725
|
+
if (expected && !(cid !== undefined && tile.cid === cid) && !(manifestCid !== undefined && tile.manifestCid === manifestCid)) {
|
|
726
|
+
throw new TileLoadError('record-changed', `The tile at ${url} is not the one expected, or has no CID to tell by.`, {
|
|
727
|
+
expected: cid ?? manifestCid,
|
|
728
|
+
actual: cid !== undefined ? tile.cid : tile.manifestCid,
|
|
729
|
+
manifestCid: tile.manifestCid,
|
|
730
|
+
});
|
|
731
|
+
}
|
|
221
732
|
return tile;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
catch (err) {
|
|
736
|
+
if (options || !(err instanceof TileLoadError))
|
|
737
|
+
throw err;
|
|
738
|
+
console.error(`Could not load ${url}:`, err);
|
|
222
739
|
}
|
|
223
740
|
return false;
|
|
224
741
|
}
|
|
225
742
|
}
|
|
743
|
+
// The loading server is at `load.<base>`, and a tile origin at `<label>.<base>`.
|
|
744
|
+
const LOAD_LABEL = 'load.';
|
|
745
|
+
const LETTERS = 'abcdefghijklmnopqrstuvwxyz';
|
|
746
|
+
// Twenty random letters: the name of a tile origin that no one can guess, and
|
|
747
|
+
// that is not made twice.
|
|
748
|
+
function randomLabel() {
|
|
749
|
+
let label = '';
|
|
750
|
+
while (label.length < 20) {
|
|
751
|
+
for (const byte of crypto.getRandomValues(new Uint8Array(32))) {
|
|
752
|
+
// 234 is nine times 26: the bytes from there up would favour the first letters.
|
|
753
|
+
if (byte < 234 && label.length < 20)
|
|
754
|
+
label += LETTERS[byte % 26];
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
return label;
|
|
758
|
+
}
|
|
226
759
|
// Loads a picture once so that it is decoded and cached, then revokes its
|
|
227
760
|
// object URL. For pictures set where no load event says when they are in.
|
|
228
761
|
function releaseWhenLoaded(url) {
|
|
@@ -239,6 +772,11 @@ export class Tile extends EventTarget {
|
|
|
239
772
|
#url;
|
|
240
773
|
#manifest;
|
|
241
774
|
#pathLoader;
|
|
775
|
+
// The address a frame of the host's own making loads for this run (getLoadSource()).
|
|
776
|
+
#source;
|
|
777
|
+
// What the close under way will resolve to, and the frame it is closing.
|
|
778
|
+
#closed;
|
|
779
|
+
#closingFrame;
|
|
242
780
|
// The frame the tile runs in, and the id its runtime knows it by. The frame
|
|
243
781
|
// is held weakly: it is the host's, in the host's document.
|
|
244
782
|
#runtimeId;
|
|
@@ -248,12 +786,14 @@ export class Tile extends EventTarget {
|
|
|
248
786
|
#started = false;
|
|
249
787
|
#closing;
|
|
250
788
|
#runtimeDone;
|
|
251
|
-
|
|
789
|
+
#identity;
|
|
790
|
+
constructor(mothership, url, manifest, pathLoader, identity = {}) {
|
|
252
791
|
super();
|
|
253
792
|
this.#mothership = mothership;
|
|
254
793
|
this.#url = url;
|
|
255
794
|
this.#manifest = manifest;
|
|
256
795
|
this.#pathLoader = pathLoader;
|
|
796
|
+
this.#identity = identity;
|
|
257
797
|
this.addEventListener('load', () => {
|
|
258
798
|
if (this.#manifest?.name) {
|
|
259
799
|
const title = this.#manifest?.name;
|
|
@@ -274,11 +814,68 @@ export class Tile extends EventTarget {
|
|
|
274
814
|
get manifest() {
|
|
275
815
|
return this.#manifest;
|
|
276
816
|
}
|
|
817
|
+
/**
|
|
818
|
+
* The CID of the record or file this tile was made from, computed by its
|
|
819
|
+
* loader from the bytes it was given: what a host keeps to know the tile
|
|
820
|
+
* again (see TileLoadOptions). Undefined where a loader has none to give.
|
|
821
|
+
*/
|
|
822
|
+
get cid() {
|
|
823
|
+
return this.#identity.cid;
|
|
824
|
+
}
|
|
825
|
+
/** The CID of the tile's manifest alone: its content, whatever record carries it. */
|
|
826
|
+
get manifestCid() {
|
|
827
|
+
return this.#identity.manifestCid;
|
|
828
|
+
}
|
|
829
|
+
/** Whether the tile was checked against a CID its host expected. */
|
|
830
|
+
get pinned() {
|
|
831
|
+
return this.#identity.pinned === true;
|
|
832
|
+
}
|
|
833
|
+
/** @internal The bytes of its manifest, and their CID, where its loader has them. */
|
|
834
|
+
get manifestBytes() {
|
|
835
|
+
return this.#identity.manifestBytes;
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Whether the origin this tile runs on is made for one load and wiped when
|
|
839
|
+
* it is closed. It is unless its mothership names an origin for its tiles
|
|
840
|
+
* (`namedOrigin`), or runs them on the origin named by their CID
|
|
841
|
+
* (`cidOrigins`) and this tile has a CID to be named by: such an origin is
|
|
842
|
+
* kept, for the tile to find what it stored there again.
|
|
843
|
+
*/
|
|
844
|
+
get ephemeral() {
|
|
845
|
+
return this.#mothership.ephemeral && !this.#mothership.cidHost(this);
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* @internal The bytes of the manifest that names this tile's origin, when
|
|
849
|
+
* it runs on the origin named by its CID: what that origin's runtime is
|
|
850
|
+
* handed, to check it against its name.
|
|
851
|
+
*/
|
|
852
|
+
get cidOrigin() {
|
|
853
|
+
return this.#mothership.cidHost(this) ? this.#identity.manifestBytes?.bytes : undefined;
|
|
854
|
+
}
|
|
855
|
+
// Whether the dock of the frame the tile is in now said that its runtime
|
|
856
|
+
// holds an origin named by a CID to its tile.
|
|
857
|
+
#bound = false;
|
|
858
|
+
/** @internal What the dock said of itself: whether its runtime holds an origin named by a CID to its tile. */
|
|
859
|
+
dockSaid(bound) {
|
|
860
|
+
this.#bound = bound;
|
|
861
|
+
}
|
|
862
|
+
/** @internal Whether it said so. */
|
|
863
|
+
get bound() {
|
|
864
|
+
return this.#bound;
|
|
865
|
+
}
|
|
277
866
|
async resolvePath(path) {
|
|
278
867
|
const u = new URL(`fake:${path}`);
|
|
279
868
|
return this.#pathLoader.resolvePath(u.pathname);
|
|
280
869
|
}
|
|
870
|
+
/**
|
|
871
|
+
* A card for the tile: what its manifest says of it, and its pictures,
|
|
872
|
+
* which runs the tile when it is clicked. `options.contentHeight` presets
|
|
873
|
+
* the height of the tile's frame, and `options.frame` is what the host
|
|
874
|
+
* asks of that frame, as with `renderContent()`: it is checked now, and
|
|
875
|
+
* the card is refused if the frame could not be made as asked.
|
|
876
|
+
*/
|
|
281
877
|
async renderCard(options) {
|
|
878
|
+
this.#frameAttributes(options?.frame);
|
|
282
879
|
const card = el('div', { style: {
|
|
283
880
|
border: '1px solid lightgrey',
|
|
284
881
|
'border-radius': '3px',
|
|
@@ -287,7 +884,7 @@ export class Tile extends EventTarget {
|
|
|
287
884
|
card.addEventListener('click', async () => {
|
|
288
885
|
const tileRenderer = await this.renderContent(options?.contentHeight ||
|
|
289
886
|
this.#manifest?.sizing?.height ||
|
|
290
|
-
Math.max(card.offsetHeight, 300));
|
|
887
|
+
Math.max(card.offsetHeight, 300), options?.frame);
|
|
291
888
|
card.replaceWith(tileRenderer);
|
|
292
889
|
});
|
|
293
890
|
// XXX we always take the first, we could be smarter with sizes
|
|
@@ -331,8 +928,17 @@ export class Tile extends EventTarget {
|
|
|
331
928
|
}
|
|
332
929
|
return card;
|
|
333
930
|
}
|
|
931
|
+
/**
|
|
932
|
+
* What a frame of the host's own making loads to run this tile (the frame
|
|
933
|
+
* is then handed to `attachIframe()`): the dock, for an origin made for
|
|
934
|
+
* this one run of the tile. It is the same address every time it is asked
|
|
935
|
+
* for, so that it can sit in a template that is rendered again and again,
|
|
936
|
+
* until the tile is closed: after that it is another run, on another
|
|
937
|
+
* origin.
|
|
938
|
+
*/
|
|
334
939
|
getLoadSource() {
|
|
335
|
-
|
|
940
|
+
this.#source ??= this.#mothership.getLoadSource(this);
|
|
941
|
+
return this.#source;
|
|
336
942
|
}
|
|
337
943
|
/**
|
|
338
944
|
* Sends a tiles-protocol message down to the rendered tile. Messages the
|
|
@@ -355,12 +961,133 @@ export class Tile extends EventTarget {
|
|
|
355
961
|
* with it. A tile whose frame is gone says nothing.
|
|
356
962
|
*/
|
|
357
963
|
post(action, payload) {
|
|
964
|
+
this.#track();
|
|
358
965
|
const win = this.#frame?.deref()?.contentWindow;
|
|
359
966
|
win?.postMessage({ id: this.#runtimeId, action, payload }, this.#mothership.getLoadOrigin());
|
|
360
967
|
}
|
|
968
|
+
// The frame's window is how the tile's messages are known for its own. It
|
|
969
|
+
// is there once the frame is in a document, and it is a new one if the
|
|
970
|
+
// frame is taken out and put back, so it is taken down at each load, and
|
|
971
|
+
// whenever something is posted to it: the dock answers the first thing it
|
|
972
|
+
// is told, which can come before its frame's load event (what the host
|
|
973
|
+
// sends the tile meanwhile, its size say), with its word on what its
|
|
974
|
+
// runtime holds (see RCV_DOCK_READY), and that must not go unheard.
|
|
975
|
+
#track() {
|
|
976
|
+
const win = this.#frame?.deref()?.contentWindow;
|
|
977
|
+
if (!win || win === this.#window)
|
|
978
|
+
return;
|
|
979
|
+
if (this.#window)
|
|
980
|
+
this.#mothership.undock(this.#window);
|
|
981
|
+
this.#window = win;
|
|
982
|
+
this.#mothership.dock(win, this);
|
|
983
|
+
}
|
|
984
|
+
// --- Starting
|
|
985
|
+
// What the host hears of a tile that does not start. A start is a frame
|
|
986
|
+
// the tile is attached to, and each time that frame loads (which is when
|
|
987
|
+
// the runtime in it is told to load the tile: see start()). The runtime in
|
|
988
|
+
// it comes up in steps the mothership hears of, and `#waiting` is the one
|
|
989
|
+
// that has not happened yet (see TileErrorEvent); it is nothing before
|
|
990
|
+
// there is a frame, and nothing again once the root document is on its
|
|
991
|
+
// way. A start that fails is reported once: by the runtime, when it knows
|
|
992
|
+
// what stopped it; when the root document is not to be had; or by the
|
|
993
|
+
// deadline, which reports and does nothing else.
|
|
994
|
+
#waiting;
|
|
995
|
+
#reported = false;
|
|
996
|
+
#rootFailed = false;
|
|
997
|
+
#deadline;
|
|
998
|
+
// A start, and the time it is given.
|
|
999
|
+
#watch() {
|
|
1000
|
+
this.#waiting = STAGES[0];
|
|
1001
|
+
this.#reported = this.#rootFailed = false;
|
|
1002
|
+
this.#clock();
|
|
1003
|
+
}
|
|
1004
|
+
// Sets the deadline, for the frame the tile is in now. `out`: that frame
|
|
1005
|
+
// was not in the document when it was last looked at (see #late).
|
|
1006
|
+
#clock(out = false) {
|
|
1007
|
+
clearTimeout(this.#deadline);
|
|
1008
|
+
const ms = this.#mothership.startDeadline;
|
|
1009
|
+
if (!ms)
|
|
1010
|
+
return;
|
|
1011
|
+
// By a weak reference, the way the mothership knows a tile: a deadline
|
|
1012
|
+
// keeps none alive.
|
|
1013
|
+
const weak = new WeakRef(this);
|
|
1014
|
+
const id = this.#runtimeId;
|
|
1015
|
+
this.#deadline = setTimeout(() => {
|
|
1016
|
+
const tile = weak.deref();
|
|
1017
|
+
if (tile)
|
|
1018
|
+
tile.#late(id, out);
|
|
1019
|
+
}, out ? Math.max(ms, OUT_MS) : ms);
|
|
1020
|
+
// (Under Node, where the tests run, a timer would keep the process up.)
|
|
1021
|
+
this.#deadline.unref?.();
|
|
1022
|
+
}
|
|
1023
|
+
// The deadline has passed: for the frame the tile was in when it was set,
|
|
1024
|
+
// which it may have left since. A frame that is not in the document loads
|
|
1025
|
+
// nothing, and a host may make one well before it shows it. That time is
|
|
1026
|
+
// not held against the tile, and it must not use the deadline up either,
|
|
1027
|
+
// or a frame put in the page late would never be watched. So a frame that
|
|
1028
|
+
// is out is looked at again later, and one that is found back in is given
|
|
1029
|
+
// its time from there: what is reported is a whole deadline spent in the
|
|
1030
|
+
// document.
|
|
1031
|
+
#late(id, wasOut) {
|
|
1032
|
+
const ifr = this.#frame?.deref();
|
|
1033
|
+
if (id !== this.#runtimeId || !ifr)
|
|
1034
|
+
return;
|
|
1035
|
+
const out = !ifr.isConnected;
|
|
1036
|
+
if (out || wasOut)
|
|
1037
|
+
return this.#clock(out);
|
|
1038
|
+
this.failed('timeout', { stage: this.#waiting });
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* @internal What this start is waiting for now: a later step than before,
|
|
1042
|
+
* or nothing more, because the tile is up or because it was stopped.
|
|
1043
|
+
*/
|
|
1044
|
+
awaits(stage) {
|
|
1045
|
+
if (!this.#waiting)
|
|
1046
|
+
return;
|
|
1047
|
+
if (stage && STAGES.indexOf(stage) <= STAGES.indexOf(this.#waiting))
|
|
1048
|
+
return;
|
|
1049
|
+
this.#waiting = stage;
|
|
1050
|
+
if (!stage)
|
|
1051
|
+
clearTimeout(this.#deadline);
|
|
1052
|
+
else if (stage === 'content' && this.#rootFailed)
|
|
1053
|
+
this.failed('content');
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* @internal A start, and the time it is given, from now: for a tile that
|
|
1057
|
+
* had more to do before its runtime could be told to load (one from AT
|
|
1058
|
+
* fetches all of itself first), and was waiting for nothing meanwhile
|
|
1059
|
+
* (`awaits()`). What that took is not the runtime being late.
|
|
1060
|
+
*/
|
|
1061
|
+
watch() {
|
|
1062
|
+
this.#watch();
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* @internal This start of the tile has failed, or is late: the host is
|
|
1066
|
+
* told, once, with what there is to say of it (`stage`, `generation`). Not
|
|
1067
|
+
* of a frame it has taken out of the document: nothing is expected of that
|
|
1068
|
+
* one.
|
|
1069
|
+
*/
|
|
1070
|
+
failed(reason, detail = {}) {
|
|
1071
|
+
if (!this.#waiting || this.#reported || !this.#frame?.deref()?.isConnected)
|
|
1072
|
+
return;
|
|
1073
|
+
// A worker asks for the root document as soon as it knows its tile,
|
|
1074
|
+
// which can be before it has said that it is ready. A root document that
|
|
1075
|
+
// fails that early is kept until then: `load` first, as when it is late.
|
|
1076
|
+
if (reason === 'content' && this.#waiting !== 'content') {
|
|
1077
|
+
this.#rootFailed = true;
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
this.#reported = true;
|
|
1081
|
+
clearTimeout(this.#deadline);
|
|
1082
|
+
this.dispatchEvent(Object.assign(new Event('error'), { reason, ...detail }));
|
|
1083
|
+
}
|
|
361
1084
|
/** @internal Tells the runtime in the frame to load the tile. */
|
|
362
1085
|
start() {
|
|
363
1086
|
this.#started = true;
|
|
1087
|
+
// 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) is not the
|
|
1089
|
+
// runtime being late.
|
|
1090
|
+
this.#watch();
|
|
364
1091
|
this.post(SND_SHUTTLE_LOAD, this.#mothership.loadPayload(this));
|
|
365
1092
|
}
|
|
366
1093
|
/** @internal The runtime says it has cleaned up. */
|
|
@@ -376,24 +1103,24 @@ export class Tile extends EventTarget {
|
|
|
376
1103
|
this.#letGo();
|
|
377
1104
|
this.#runtimeId = crypto.randomUUID(); // we might want to make that pluggable
|
|
378
1105
|
this.#frame = new WeakRef(ifr);
|
|
379
|
-
|
|
380
|
-
// It is there once the frame is in a document, and it is a new one if
|
|
381
|
-
// the frame is taken out and put back, so it is taken down at each load.
|
|
382
|
-
const track = () => {
|
|
383
|
-
const win = ifr.contentWindow;
|
|
384
|
-
if (!win || win === this.#window)
|
|
385
|
-
return;
|
|
386
|
-
if (this.#window)
|
|
387
|
-
this.#mothership.undock(this.#window);
|
|
388
|
-
this.#window = win;
|
|
389
|
-
this.#mothership.dock(win, this);
|
|
390
|
-
};
|
|
391
|
-
track();
|
|
1106
|
+
this.#track();
|
|
392
1107
|
this.#onFrameLoad = () => {
|
|
393
|
-
track();
|
|
1108
|
+
this.#track();
|
|
1109
|
+
// The frame has loaded: the time a start is given runs from when the
|
|
1110
|
+
// runtime in it is told to load the tile (see start()).
|
|
1111
|
+
clearTimeout(this.#deadline);
|
|
394
1112
|
this.start();
|
|
395
1113
|
};
|
|
396
1114
|
ifr.addEventListener('load', this.#onFrameLoad);
|
|
1115
|
+
// The frame may never load (a loading server that does not answer), so
|
|
1116
|
+
// the time a start is given runs from here: if the frame is in the
|
|
1117
|
+
// document by the time the host's code that made it has run (it places
|
|
1118
|
+
// what renderContent() hands it), and from when it is put there if not.
|
|
1119
|
+
this.#watch();
|
|
1120
|
+
queueMicrotask(() => {
|
|
1121
|
+
if (this.#frame?.deref() === ifr && this.#waiting && !ifr.isConnected)
|
|
1122
|
+
this.#clock(true);
|
|
1123
|
+
});
|
|
397
1124
|
}
|
|
398
1125
|
// Forgets the frame the tile was in, without touching the frame.
|
|
399
1126
|
#letGo() {
|
|
@@ -404,54 +1131,198 @@ export class Tile extends EventTarget {
|
|
|
404
1131
|
this.#mothership.undock(this.#window);
|
|
405
1132
|
this.#frame = this.#window = this.#onFrameLoad = this.#runtimeId = undefined;
|
|
406
1133
|
this.#started = false;
|
|
1134
|
+
this.#bound = false;
|
|
407
1135
|
}
|
|
408
1136
|
/**
|
|
409
|
-
* Closes the tile: its
|
|
410
|
-
*
|
|
411
|
-
*
|
|
1137
|
+
* Closes the tile: its frame is removed from the document, the tile
|
|
1138
|
+
* forgets it, and the origin it ran on is wiped. Resolves when that is
|
|
1139
|
+
* done, to whether the origin was wiped. The tile can be rendered again
|
|
1140
|
+
* afterwards.
|
|
412
1141
|
*
|
|
413
1142
|
* Removing the frame yourself and dropping the tile is enough for the
|
|
414
1143
|
* page's memory: the mothership keeps nothing alive. What that cannot
|
|
415
1144
|
* reach is what the tile left in the browser. Every tile runs on an origin
|
|
416
1145
|
* made for that one load, and its worker's registration, and anything the
|
|
417
|
-
* tile stored, would stay there for good. `close()`
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
1146
|
+
* tile stored, would stay there for good. `close()` has that origin wiped
|
|
1147
|
+
* (not a named origin, `namedOrigin`, nor the origin named by the tile's
|
|
1148
|
+
* CID, `cidOrigins`: both are kept on purpose, see `tile.ephemeral`).
|
|
1149
|
+
*
|
|
1150
|
+
* It does not matter what became of the frame first, nor what the tile
|
|
1151
|
+
* did. The frame's address names the origin (getLoadSource() does, and so
|
|
1152
|
+
* every frame renderContent() makes), and that is all it takes: the frame
|
|
1153
|
+
* is removed, if the host has not removed it already, and the origin is
|
|
1154
|
+
* wiped from a frame of the loader's own (TileMothership.wipeOrigin). So
|
|
1155
|
+
* `close()` can be called before the frame is taken out of the document,
|
|
1156
|
+
* or after. A few seconds later the origin is wiped a second time, the
|
|
1157
|
+
* same way, without anyone waiting for it: a worker of the tile's can
|
|
1158
|
+
* outlive its pages for a moment, and write.
|
|
1159
|
+
*
|
|
1160
|
+
* Only a frame whose address does not name the origin, which a host that
|
|
1161
|
+
* builds the address itself can make, has to be asked while it is there:
|
|
1162
|
+
* the runtime in it is told to close, as it always was, and given
|
|
1163
|
+
* CLOSE_MS to answer. Out of the document, there is no one to ask.
|
|
1164
|
+
*
|
|
1165
|
+
* 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 and
|
|
1167
|
+
* for the origin named by the tile's CID, which are kept, and when the
|
|
1168
|
+
* wipe could not be done or was not confirmed, which is also logged as a
|
|
1169
|
+
* warning.
|
|
421
1170
|
*/
|
|
422
1171
|
close() {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
//
|
|
426
|
-
|
|
1172
|
+
const ifr = this.#frame?.deref();
|
|
1173
|
+
// Kept until it settles, so that closing twice is closing once. Unless
|
|
1174
|
+
// the tile was rendered again meanwhile: it is in another frame then,
|
|
1175
|
+
// and closing that one is another close.
|
|
1176
|
+
if (this.#closed && (!ifr || ifr === this.#closingFrame))
|
|
1177
|
+
return this.#closed;
|
|
1178
|
+
const outcome = { wiped: false };
|
|
1179
|
+
const closing = this.#close(outcome).finally(() => {
|
|
427
1180
|
if (this.#closing === closing)
|
|
428
|
-
this.#closing = undefined;
|
|
1181
|
+
this.#closing = this.#closed = this.#closingFrame = undefined;
|
|
429
1182
|
});
|
|
430
1183
|
this.#closing = closing;
|
|
431
|
-
|
|
1184
|
+
this.#closingFrame = ifr;
|
|
1185
|
+
this.#closed = closing.then(() => outcome.wiped);
|
|
1186
|
+
return this.#closed;
|
|
432
1187
|
}
|
|
433
|
-
async #close() {
|
|
1188
|
+
async #close(outcome) {
|
|
1189
|
+
// The host is done with this start, however far it got: nothing more is
|
|
1190
|
+
// expected of it, and a deadline that runs out while its runtime is
|
|
1191
|
+
// asked to close is not the tile failing to start.
|
|
1192
|
+
this.awaits();
|
|
434
1193
|
const ifr = this.#frame?.deref();
|
|
1194
|
+
// There is something to wipe once a runtime was started on an origin
|
|
1195
|
+
// made for this load: nothing was put there before that.
|
|
1196
|
+
const started = this.#started;
|
|
1197
|
+
const wipe = this.ephemeral && started;
|
|
1198
|
+
// 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;
|
|
435
1201
|
{
|
|
436
|
-
|
|
1202
|
+
// 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, where
|
|
1204
|
+
// there is nothing to wipe and it just closes.
|
|
1205
|
+
if (ifr?.isConnected && ifr.contentWindow && started && !origin) {
|
|
1206
|
+
const win = ifr.contentWindow;
|
|
1207
|
+
const loadOrigin = this.#mothership.getLoadOrigin();
|
|
1208
|
+
let answered = false;
|
|
1209
|
+
// What the runtime's answer says of the wipe. A runtime whose dock
|
|
1210
|
+
// carries a close out itself says whether the origin was wiped; one
|
|
1211
|
+
// from before that says only that it has closed, and is taken at its
|
|
1212
|
+
// word.
|
|
1213
|
+
let wipedThere = true;
|
|
1214
|
+
const onAnswer = (ev) => {
|
|
1215
|
+
if (ev.source === win && ev.origin === loadOrigin && ev.data?.action === RCV_SHUTTLE_CLOSED)
|
|
1216
|
+
wipedThere = ev.data.payload?.wiped !== false;
|
|
1217
|
+
};
|
|
1218
|
+
window.addEventListener('message', onAnswer);
|
|
437
1219
|
await new Promise((resolve) => {
|
|
438
|
-
this.#runtimeDone =
|
|
1220
|
+
this.#runtimeDone = () => {
|
|
1221
|
+
answered = true;
|
|
1222
|
+
// In a moment: the listener above is handed the same message,
|
|
1223
|
+
// and may be handed it after the one that calls us.
|
|
1224
|
+
setTimeout(resolve);
|
|
1225
|
+
};
|
|
439
1226
|
setTimeout(resolve, CLOSE_MS);
|
|
440
|
-
this.post(SND_SHUTTLE_CLOSE, { wipe: this
|
|
1227
|
+
this.post(SND_SHUTTLE_CLOSE, { wipe: this.ephemeral });
|
|
441
1228
|
});
|
|
1229
|
+
window.removeEventListener('message', onAnswer);
|
|
442
1230
|
this.#runtimeDone = undefined;
|
|
1231
|
+
outcome.wiped = wipe && answered && wipedThere;
|
|
443
1232
|
}
|
|
444
1233
|
// Only if it is still the frame we had: the host may have rendered the
|
|
445
1234
|
// tile again while the old runtime was tidying up.
|
|
446
|
-
if (this.#frame?.deref() === ifr)
|
|
1235
|
+
if (this.#frame?.deref() === ifr) {
|
|
447
1236
|
this.#letGo();
|
|
1237
|
+
// That run is over: the next has an address, and an origin, of its own.
|
|
1238
|
+
this.#source = undefined;
|
|
1239
|
+
}
|
|
448
1240
|
ifr?.remove?.();
|
|
1241
|
+
if (origin) {
|
|
1242
|
+
outcome.wiped = await this.#mothership.wipeOrigin(origin, credentialless);
|
|
1243
|
+
// And once more in a moment, for what a worker of the tile's may
|
|
1244
|
+
// have written since (see WIPE_AGAIN_MS), and for a wipe that did
|
|
1245
|
+
// not go through the first time. No one waits for that one, and
|
|
1246
|
+
// nothing is said of how it went.
|
|
1247
|
+
setTimeout(() => { void this.#mothership.wipeOrigin(origin, credentialless); }, WIPE_AGAIN_MS);
|
|
1248
|
+
}
|
|
1249
|
+
else if (!started)
|
|
1250
|
+
outcome.wiped = this.ephemeral;
|
|
1251
|
+
if (wipe && !outcome.wiped) {
|
|
1252
|
+
console.warn(origin
|
|
1253
|
+
? `[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
|
+
: '[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.');
|
|
1255
|
+
}
|
|
449
1256
|
this.dispatchEvent(new Event('close'));
|
|
450
1257
|
}
|
|
451
1258
|
}
|
|
452
|
-
|
|
1259
|
+
/**
|
|
1260
|
+
* The frame that runs the tile, for the host to put in its document.
|
|
1261
|
+
*
|
|
1262
|
+
* The frame is sandboxed. The runtime in it has a sandbox of its own
|
|
1263
|
+
* (`tileSandbox` in @dasl/tile-lexicon), and a frame's sandbox only ever
|
|
1264
|
+
* takes away: a tile gets what both grant. It needs scripts and its own
|
|
1265
|
+
* origin to run at all (its worker cannot be registered without them), and
|
|
1266
|
+
* forms to submit to itself. The runtime grants dialogs (`allow-modals`:
|
|
1267
|
+
* alert, confirm, prompt, print) and downloads (`allow-downloads`) besides;
|
|
1268
|
+
* a tile in someone else's page has no call for either, so the frame
|
|
1269
|
+
* leaves them out.
|
|
1270
|
+
*
|
|
1271
|
+
* `options.sandbox` is the frame's sandbox attribute in full, for a host
|
|
1272
|
+
* that wants it otherwise: wider where its user runs one tile on purpose,
|
|
1273
|
+
* narrower (no forms) if it likes. `allow-scripts` and `allow-same-origin`
|
|
1274
|
+
* have to be in it, and nothing can be that the runtime does not grant,
|
|
1275
|
+
* since it would be there for nothing. Either mistake throws.
|
|
1276
|
+
*
|
|
1277
|
+
* `options.credentialless` makes the frame credentialless. A tile's origin
|
|
1278
|
+
* is a subdomain of the loading server's site, and cookies go by site: in
|
|
1279
|
+
* a plain frame a tile reads the cookies the site keeps for its whole
|
|
1280
|
+
* domain, and sets some there. In a credentialless frame Chromium gives
|
|
1281
|
+
* everything inside cookies and storage of its own, empty to begin with
|
|
1282
|
+
* and gone with the page, and keeps its password manager out. Other
|
|
1283
|
+
* browsers ignore the attribute. When nothing is said, the frame is
|
|
1284
|
+
* credentialless if the tile's origin was made for this one load
|
|
1285
|
+
* (`tile.ephemeral`): nothing is lost by its storage going with the page.
|
|
1286
|
+
* A named origin, or the origin named by the tile's CID, is there for a
|
|
1287
|
+
* tile to find its storage again, which such a frame loses with every
|
|
1288
|
+
* page, so its frame is a plain one. `true` or
|
|
1289
|
+
* `false` for a host that wants it otherwise, either way, and anything
|
|
1290
|
+
* else throws.
|
|
1291
|
+
*
|
|
1292
|
+
* `options.tabindex` is the frame's tabindex: `-1` has the Tab key pass
|
|
1293
|
+
* over the tile. Whoever has the focus in a tile types into the tile,
|
|
1294
|
+
* every key, Tab and Escape with the rest, and the host hears none of
|
|
1295
|
+
* them: a tile can keep the keyboard it is given, and each key counts as
|
|
1296
|
+
* its user having acted in it. A page with more on it than tiles may want
|
|
1297
|
+
* no one to end up there by tabbing down it, and then gives its user a
|
|
1298
|
+
* way in of its own. The focus guard (`guardFocus` in
|
|
1299
|
+
* @dasl/tile-loader/guard) does both for a frame it guards, whatever this
|
|
1300
|
+
* says: it takes the frame out of the tab order and puts a stop of its own
|
|
1301
|
+
* in its place, unless its host tells it otherwise. This is about the Tab
|
|
1302
|
+
* key and no more: a tile's script can still take the focus itself. A
|
|
1303
|
+
* whole number from -32767 to 32767, or it throws; the frame has no
|
|
1304
|
+
* tabindex, and is in the tab order as any frame is, when nothing is said.
|
|
1305
|
+
*
|
|
1306
|
+
* `options.refuse` names features to take from the tile, among REFUSABLE:
|
|
1307
|
+
* those a browser gives every frame unasked (picture-in-picture, the
|
|
1308
|
+
* gamepad, synchronous requests, the client hints and more), and those the
|
|
1309
|
+
* runtime hands on to a tile when its host's frame allows them. Each is
|
|
1310
|
+
* written into the frame's `allow` attribute as `'none'`, which binds
|
|
1311
|
+
* everything inside the frame, the runtime and every frame the tile makes.
|
|
1312
|
+
* It only ever takes away, and only where the browser knows the name:
|
|
1313
|
+
* Chrome knows them all, Firefox and Safari far fewer, and pass over the
|
|
1314
|
+
* others. A name the loader does not know throws, since it would take
|
|
1315
|
+
* nothing away anywhere. When nothing is said, the frame refuses
|
|
1316
|
+
* synchronous requests (`sync-xhr`), which hold up the tile and its
|
|
1317
|
+
* runtime and never bring a tile its own files. A host's list is the list
|
|
1318
|
+
* in full, as `options.sandbox` is the attribute: it replaces that one, so
|
|
1319
|
+
* a host that refuses more names `sync-xhr` in it to keep that refused,
|
|
1320
|
+
* and `[]` refuses nothing and leaves the frame with no `allow` attribute.
|
|
1321
|
+
*/
|
|
1322
|
+
renderContent(height = 300, options) {
|
|
453
1323
|
const ifr = el('iframe', {
|
|
454
|
-
src: this.#mothership.getLoadSource(),
|
|
1324
|
+
src: this.#mothership.getLoadSource(this),
|
|
1325
|
+
...this.#frameAttributes(options),
|
|
455
1326
|
style: {
|
|
456
1327
|
display: 'block',
|
|
457
1328
|
width: '100%',
|
|
@@ -462,5 +1333,59 @@ export class Tile extends EventTarget {
|
|
|
462
1333
|
this.attachIframe(ifr);
|
|
463
1334
|
return ifr;
|
|
464
1335
|
}
|
|
1336
|
+
// The attributes of the frame renderContent() makes, from what its host
|
|
1337
|
+
// asked, each checked: one that cannot be honoured throws (see
|
|
1338
|
+
// renderContent()).
|
|
1339
|
+
#frameAttributes(options) {
|
|
1340
|
+
// The first two no tile runs without; the rest of what the runtime
|
|
1341
|
+
// grants is the host's to keep or leave out.
|
|
1342
|
+
const needed = ['allow-scripts', 'allow-same-origin'];
|
|
1343
|
+
const granted = tileSandbox.split(' ');
|
|
1344
|
+
const asked = options?.sandbox ?? FRAME_SANDBOX;
|
|
1345
|
+
if (typeof asked !== 'string')
|
|
1346
|
+
throw new Error(`A tile's frame takes its sandbox as the text of the attribute: ${FRAME_SANDBOX} when nothing is said.`);
|
|
1347
|
+
// Read as HTML reads the attribute: any case, any spacing.
|
|
1348
|
+
const sandbox = asked.toLowerCase().split(/\s+/).filter(Boolean);
|
|
1349
|
+
const refused = sandbox.filter(token => !granted.includes(token));
|
|
1350
|
+
if (refused.length)
|
|
1351
|
+
throw new Error(`A tile's frame cannot be given ${refused.join(', ')}: the runtime grants ${granted.join(', ')} and nothing else.`);
|
|
1352
|
+
if (!needed.every(token => sandbox.includes(token)))
|
|
1353
|
+
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 nothing is
|
|
1355
|
+
// lost by the frame's storage going with the page. A host that says
|
|
1356
|
+
// otherwise says true or false, and nothing else: any other value would
|
|
1357
|
+
// decide it unnoticed, and the empty text an HTML attribute has when it
|
|
1358
|
+
// is there would count as false.
|
|
1359
|
+
const credentialless = options?.credentialless ?? this.ephemeral;
|
|
1360
|
+
if (typeof credentialless !== 'boolean')
|
|
1361
|
+
throw new Error(`A tile's frame is credentialless or it is not: the option takes true or false.`);
|
|
1362
|
+
// A tabindex the browser cannot read is no tabindex: the frame would be
|
|
1363
|
+
// in the tab order and its host none the wiser. Only a whole number will
|
|
1364
|
+
// do, and not too large a one: Chromium takes a number it cannot hold for
|
|
1365
|
+
// no tabindex either, and the largest are not even written in digits
|
|
1366
|
+
// ("1e+21", which reads as 1). 32767 is as far as HTML ever said one goes.
|
|
1367
|
+
const tabindex = options?.tabindex ?? undefined;
|
|
1368
|
+
if (tabindex !== undefined && (!Number.isInteger(tabindex) || Math.abs(tabindex) > 32767))
|
|
1369
|
+
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.`);
|
|
1370
|
+
// Names, each one the loader knows, and each refused once. Read with its
|
|
1371
|
+
// holes, which every way of going through a list but this one passes
|
|
1372
|
+
// over: a list with a hole in it is not a list of names, and one that
|
|
1373
|
+
// is all holes would refuse nothing, its host none the wiser.
|
|
1374
|
+
const listed = options?.refuse ?? FRAME_REFUSE;
|
|
1375
|
+
const refuse = Array.isArray(listed) ? Array.from(listed) : listed;
|
|
1376
|
+
if (!Array.isArray(refuse) || refuse.some(name => typeof name !== 'string'))
|
|
1377
|
+
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.`);
|
|
1378
|
+
const unknown = refuse.filter(name => !REFUSABLE.includes(name));
|
|
1379
|
+
if (unknown.length)
|
|
1380
|
+
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(', ')}.`);
|
|
1381
|
+
const allow = [...new Set(refuse)].map(name => `${name} 'none'`).join('; ');
|
|
1382
|
+
return {
|
|
1383
|
+
sandbox: sandbox.join(' '),
|
|
1384
|
+
// The attribute is there or it is not; it has no value.
|
|
1385
|
+
credentialless: credentialless ? '' : undefined,
|
|
1386
|
+
tabindex,
|
|
1387
|
+
allow: allow || undefined,
|
|
1388
|
+
};
|
|
1389
|
+
}
|
|
465
1390
|
}
|
|
466
1391
|
//# sourceMappingURL=index.js.map
|