@dasl/tile-loader 2.6.0 → 3.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/dist/link.js ADDED
@@ -0,0 +1,142 @@
1
+ // The context side of the link protocol (https://dasl.ing/tp-link.html): a
2
+ // tile asks for a link to be opened, the context that frames it decides.
3
+ //
4
+ // A tile cannot open a window, and its frame only ever shows the tile's own
5
+ // documents. When the user follows a link out of a tile, or the tile asks
6
+ // from script, the request arrives here, and nothing happens unless the host
7
+ // says so:
8
+ //
9
+ // import { tileLinks, linkPrompt } from '@dasl/tile-loader/link';
10
+ // tileLinks(tile); // links out of the tile do nothing
11
+ // tileLinks(tile, { open: linkPrompt(container) }); // the user is shown where, and opens it
12
+ // tileLinks(tile, { open: (url) => myApp.confirmAndOpen(url) });
13
+ //
14
+ // The URL is the tile's to choose, and whatever it wants to say can be in it:
15
+ // a link opened without the user seeing where it goes is the tile sending
16
+ // data out. So the default opens nothing, `linkPrompt` has the user follow a
17
+ // link of the host's own making with the destination in plain sight, and a
18
+ // host with a handler of its own should do no less. Whatever the host does,
19
+ // it does somewhere of its own: the tile's frame is never navigated.
20
+ /** The tiles protocol this module speaks. */
21
+ export const LINK_PROTOCOL = 'link';
22
+ // Longer than any link a person follows; a tile has no call to send more.
23
+ const MAX_URL_LENGTH = 8192;
24
+ /** Hears a rendered tile's requests to open links, and answers them. */
25
+ export function tileLinks(tile, options = {}) {
26
+ const schemes = options.schemes ?? ['http:', 'https:'];
27
+ // One at a time: while the host (or its user) makes up its mind about a
28
+ // link, the tile does not get to queue up others.
29
+ let deciding = false;
30
+ const answer = (id, opened) => {
31
+ if (typeof id !== 'string' || !id)
32
+ return; // a followed link carries no id, and gets no answer
33
+ try {
34
+ tile.sendProtocol(LINK_PROTOCOL, 'result', { id, opened });
35
+ }
36
+ catch { /* gone */ }
37
+ };
38
+ const onProtocol = async (ev) => {
39
+ const { protocol, kind, payload } = ev;
40
+ if (protocol !== LINK_PROTOCOL || kind !== 'open')
41
+ return;
42
+ const { id, url: asked } = (payload && typeof payload === 'object' ? payload : {});
43
+ let url;
44
+ if (typeof asked === 'string' && asked.length <= MAX_URL_LENGTH) {
45
+ try {
46
+ url = new URL(asked);
47
+ }
48
+ catch { /* not a URL */ }
49
+ }
50
+ if (!url || !schemes.includes(url.protocol) || !options.open || deciding)
51
+ return answer(id, false);
52
+ deciding = true;
53
+ let opened;
54
+ try {
55
+ opened = await options.open(url, {
56
+ activated: globalThis.navigator?.userActivation?.isActive ?? false,
57
+ ...(typeof id === 'string' && id ? { id } : {}),
58
+ });
59
+ }
60
+ catch (err) {
61
+ console.error('Could not open a link for a tile:', err);
62
+ opened = false;
63
+ }
64
+ finally {
65
+ deciding = false;
66
+ }
67
+ if (typeof opened === 'boolean')
68
+ answer(id, opened);
69
+ };
70
+ tile.addEventListener('protocol', onProtocol);
71
+ return {
72
+ stop() {
73
+ tile.removeEventListener('protocol', onProtocol);
74
+ },
75
+ };
76
+ }
77
+ /**
78
+ * Opens `url` in a new tab or window, cut off from the page that opens it (no
79
+ * opener, no referrer). Only `http:` and `https:`. Browsers only allow it
80
+ * right after the user did something, so call it from a click of theirs.
81
+ */
82
+ export function openInNewTab(url) {
83
+ if (url.protocol !== 'http:' && url.protocol !== 'https:')
84
+ return false;
85
+ window.open(url.href, '_blank', 'noopener,noreferrer');
86
+ return true;
87
+ }
88
+ /**
89
+ * A link handler that leaves it to the user: it shows, in `container`, where
90
+ * the tile wants to go, as a link of the host's own that the user can follow,
91
+ * with a button to dismiss it. The whole address is shown, since the address
92
+ * is where a tile would hide what it wants to send out. Nothing opens unless
93
+ * the user follows the link, and it opens in a new tab, never in the tile.
94
+ *
95
+ * A request that arrives when the user has not touched the page is refused
96
+ * outright, and a new request replaces one still showing.
97
+ *
98
+ * The prompt is plain elements with a few class names (`tile-link-prompt`,
99
+ * `tile-link-url`, `tile-link-dismiss`) for a host to style.
100
+ */
101
+ export function linkPrompt(container, options = {}) {
102
+ let settle;
103
+ return (url, { activated }) => {
104
+ if (!activated)
105
+ return false;
106
+ settle?.(false);
107
+ const doc = container.ownerDocument;
108
+ const box = doc.createElement('div');
109
+ box.className = 'tile-link-prompt';
110
+ box.setAttribute('role', 'group');
111
+ box.style.cssText = 'display: flex; gap: 0.5em; align-items: baseline; padding: 0.5em 0.75em; font: inherit; overflow-wrap: anywhere;';
112
+ const label = doc.createElement('span');
113
+ label.textContent = options.label ?? 'This tile wants to open';
114
+ const link = doc.createElement('a');
115
+ link.className = 'tile-link-url';
116
+ link.href = url.href;
117
+ link.target = '_blank';
118
+ link.rel = 'noopener noreferrer';
119
+ link.textContent = url.href;
120
+ link.style.cssText = 'flex: 1; min-width: 0;';
121
+ const dismiss = doc.createElement('button');
122
+ dismiss.type = 'button';
123
+ dismiss.className = 'tile-link-dismiss';
124
+ dismiss.textContent = options.dismiss ?? 'Dismiss';
125
+ box.append(label, link, dismiss);
126
+ return new Promise((resolve) => {
127
+ const done = (opened) => {
128
+ if (settle !== done)
129
+ return;
130
+ settle = undefined;
131
+ box.remove();
132
+ resolve(opened);
133
+ };
134
+ settle = done;
135
+ // The link is a real one: following it is the user's own navigation.
136
+ link.addEventListener('click', () => setTimeout(() => done(true)));
137
+ dismiss.addEventListener('click', () => done(false));
138
+ container.append(box);
139
+ });
140
+ };
141
+ }
142
+ //# sourceMappingURL=link.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link.js","sourceRoot":"","sources":["../src/link.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,yEAAyE;AACzE,EAAE;AACF,4EAA4E;AAC5E,0EAA0E;AAC1E,6EAA6E;AAC7E,WAAW;AACX,EAAE;AACF,oEAAoE;AACpE,2FAA2F;AAC3F,gGAAgG;AAChG,mEAAmE;AACnE,EAAE;AACF,8EAA8E;AAC9E,0EAA0E;AAC1E,6EAA6E;AAC7E,2EAA2E;AAC3E,4EAA4E;AAC5E,qEAAqE;AAIrE,6CAA6C;AAC7C,MAAM,CAA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CAAC,EAAE,OAAO,CAAC;IAChB,8FAA8F;IAC9F,KAAK,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;IAC3B,+EAA+E;IAC/E,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IAClE,uDAAuD;IACvD,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,CAAC;CACnC;AAED,MAAM,WAAW,eAAe;IAC9B,iCAAiC;IACjC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,2CAA2C;IAC3C,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,8EAA8E;IAC9E,SAAS,CAAE,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC;IACrC,gEAAgE;IAChE,OAAO,CAAE,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;IAC5C,+DAA+D;IAC/D,IAAI,IAAK,IAAI,CAAC;CACf;AAID,oEAAoE;AACpE,wBAAgB,QAAQ,CAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,GAAE,eAAoB,GAAG,eAAe,CAsIxG"}
package/dist/size.js CHANGED
@@ -42,7 +42,9 @@ export function tileSize(tile, frame, options = {}) {
42
42
  let reported = ''; // the last size the host was told of
43
43
  let timer;
44
44
  let lastApplied = 0;
45
- const waiting = []; // request ids to answer once the box has settled
45
+ let stopped = false;
46
+ const waiting = []; // request ids whose move is still to come
47
+ const moved = []; // request ids whose move the host is still to apply
46
48
  // The frame's real box, when it has one.
47
49
  const box = () => {
48
50
  const r = frame.getBoundingClientRect?.();
@@ -55,6 +57,8 @@ export function tileSize(tile, frame, options = {}) {
55
57
  fallback: box() ?? current,
56
58
  });
57
59
  const grant = (id) => {
60
+ if (stopped)
61
+ return; // a move's timer can still fire after stop()
58
62
  const size = box() ?? current;
59
63
  current = size;
60
64
  const dims = `${size.width}x${size.height}`;
@@ -71,13 +75,19 @@ export function tileSize(tile, frame, options = {}) {
71
75
  }
72
76
  catch { /* not rendered yet: it asks when it is */ }
73
77
  };
74
- const answer = () => {
75
- const ids = waiting.splice(0);
78
+ const answer = (ids) => {
76
79
  if (ids.length)
77
80
  ids.forEach(id => grant(id));
78
81
  else
79
82
  grant();
80
83
  };
84
+ // The ids of `ids` that are still in `moved`, taken out of it.
85
+ const take = (ids) => ids.filter(id => {
86
+ const i = moved.indexOf(id);
87
+ if (i >= 0)
88
+ moved.splice(i, 1);
89
+ return i >= 0;
90
+ });
81
91
  const put = (size) => {
82
92
  current = size;
83
93
  if (!apply)
@@ -96,12 +106,15 @@ export function tileSize(tile, frame, options = {}) {
96
106
  timer = undefined;
97
107
  lastApplied = Date.now();
98
108
  put(settle());
109
+ const ids = waiting.splice(0);
99
110
  if (apply)
100
- answer();
101
- else
102
- setTimeout(answer, HOST_APPLIES_MS); // the host resizes its box; the observer may answer sooner
111
+ return answer(ids);
112
+ // The host resizes its box. Each move's requests get their own time
113
+ // for that; the observer may answer them sooner.
114
+ moved.push(...ids);
115
+ setTimeout(() => answer(take(ids)), HOST_APPLIES_MS);
103
116
  };
104
- if (timer)
117
+ if (timer || stopped)
105
118
  return;
106
119
  const since = Date.now() - lastApplied;
107
120
  if (since >= SETTLE_MS)
@@ -133,8 +146,9 @@ export function tileSize(tile, frame, options = {}) {
133
146
  };
134
147
  tile.addEventListener('protocol', onProtocol);
135
148
  // The context's layout changes the box too (a narrower column, a rotated
136
- // phone): the tile is told its new size.
137
- const observer = typeof ResizeObserver === 'function' ? new ResizeObserver(() => answer()) : undefined;
149
+ // phone): the tile is told its new size. Requests still waiting for their
150
+ // move are left to it.
151
+ const observer = typeof ResizeObserver === 'function' ? new ResizeObserver(() => answer(moved.splice(0))) : undefined;
138
152
  observer?.observe(frame);
139
153
  put(settle());
140
154
  reported = `${current.width}x${current.height}`; // the host knows where it starts
@@ -153,6 +167,7 @@ export function tileSize(tile, frame, options = {}) {
153
167
  return resolved;
154
168
  },
155
169
  stop() {
170
+ stopped = true;
156
171
  tile.removeEventListener('protocol', onProtocol);
157
172
  observer?.disconnect();
158
173
  if (timer)
package/dist/size.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"size.js","sourceRoot":"","sources":["../src/size.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,mEAAmE;AACnE,EAAE;AACF,uDAAuD;AACvD,wCAAwC;AACxC,0EAA0E;AAC1E,EAAE;AACF,wEAAwE;AACxE,4EAA4E;AAC5E,8EAA8E;AAC9E,+EAA+E;AAC/E,6EAA6E;AAC7E,qCAAqC;AACrC,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,wEAAwE;AACxE,6EAA6E;AAE7E,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAMlE,6CAA6C;AAC7C,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAEpC,kEAAkE;AAClE,MAAM,YAAY,GAAa,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AAC3D,8EAA8E;AAC9E,0CAA0C;AAC1C,MAAM,SAAS,GAAG,GAAG,CAAC;AACtB,0EAA0E;AAC1E,+BAA+B;AAC/B,MAAM,eAAe,GAAG,GAAG,CAAC;AA4B5B,MAAM,QAAQ,GAAG,CAAC,CAAU,EAAsB,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAE5H,oEAAoE;AACpE,MAAM,UAAU,QAAQ,CAAE,IAAU,EAAE,KAAkB,EAAE,UAA2B,EAAE;IACrF,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC;IACtC,MAAM,SAA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MAAM,KAAM,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACjD,SAAS,CAAE,IAAI;YACb,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACpB,IAAI,EAAE,CAAC;QACT,CAAC;QACD,OAAO,CAAE,IAAI;YACX,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAC1B,MAAM,QAAQ,GAAG,MAAM,EAAE,CAAC;YAC1B,IAAI,EAAE,CAAC;YACP,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,IAAI;YACF,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,CAAC,mBAAmB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;YACjD,QAAQ,EAAE,UAAU,EAAE,CAAC;YACvB,IAAI,KAAK;gBAAE,YAAY,CAAC,KAAK,CAAC,CAAC;QACjC,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dasl/tile-loader",
3
3
  "type": "module",
4
- "version": "2.6.0",
4
+ "version": "3.0.0",
5
5
  "description": "Browser architecture for safely loading DASL tiles (mothership, shuttle, worker)",
6
6
  "author": "Robin Berjon <robin@berjon.com>",
7
7
  "license": "Apache-2.0",
@@ -36,6 +36,11 @@
36
36
  "import": "./dist/size.js",
37
37
  "default": "./dist/size.js"
38
38
  },
39
+ "./link": {
40
+ "types": "./dist/link.d.ts",
41
+ "import": "./dist/link.js",
42
+ "default": "./dist/link.js"
43
+ },
39
44
  "./memory": {
40
45
  "types": "./dist/memory.d.ts",
41
46
  "import": "./dist/memory.js",
@@ -74,7 +79,7 @@
74
79
  "dependencies": {
75
80
  "@atcute/car": "^6.0.0",
76
81
  "@atcute/cid": "^2.4.1",
77
- "@dasl/tile-lexicon": "^2.2.0",
82
+ "@dasl/tile-lexicon": "^3.0.0",
78
83
  "jszip": "^3.10.1",
79
84
  "mime": "^4.1.0",
80
85
  "smol-toml": "^1.6.1"
package/src/data.ts CHANGED
@@ -12,8 +12,8 @@
12
12
  // data.onReady(() => data.send({ name: 'world' }));
13
13
  // const stop = data.onData((payload) => console.log(payload));
14
14
  //
15
- // It replaces `Tile.sendData()` and the `data` / `data-ready` events, which
16
- // the loader still has but deprecates.
15
+ // (Before 3.0 the loader had `Tile.sendData()` and `data` / `data-ready`
16
+ // events for this. They are gone: this module is the way.)
17
17
 
18
18
  import type { ProtocolEvent, Tile } from './index.js';
19
19
 
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝
9
9
  •--~~~## MOTHERSHIP ##~~~--•
10
10
 
11
- The tile-loading architecture has three levels that all communicate together:
11
+ The tile-loading architecture has four levels that all communicate together:
12
12
 
13
13
  - At the top, the MOTHERSHIP. This has access to things in the real world like
14
14
  fetching from the internet or reading from the file system. It's the interface
@@ -18,7 +18,14 @@ The tile-loading architecture has three levels that all communicate together:
18
18
  the configurability so that the other components can be deployed in entirely
19
19
  generic ways.
20
20
  - The mothership instantiates tiles by creating insulated contexts (a sandboxed
21
- iframe, an incognito window…) and loading a SHUTTLE in it. The role of the
21
+ iframe, an incognito window…) and loading the DOCK in it: a page on the
22
+ loading server's own origin, which frames the shuttle on the tile's origin
23
+ and keeps it there. The tile can script its shuttle but not the dock, so
24
+ the dock is what holds the frame in place (it can be navigated nowhere),
25
+ keeps everything below it from opening windows, and passes messages only
26
+ between the mothership and that one shuttle. The mothership only ever
27
+ talks to the dock.
28
+ - In the dock, the SHUTTLE. The role of the
22
29
  shuttle is to set up a service worker and an iframe to load the root of the
23
30
  tile into. It only exists because you need something to carry a service worker
24
31
  in. The only other thing that it does is (*drumroll*) shuttle messages back
@@ -29,12 +36,20 @@ The tile-loading architecture has three levels that all communicate together:
29
36
  */
30
37
 
31
38
  import { el } from "./lib/el.js";
32
- import { tileResponseHeaders } from "@dasl/tile-lexicon";
39
+ import { tileResponseHeaders, dockPath } from "@dasl/tile-lexicon";
33
40
  import type { AnyMasl, MaslHeaders, MaslResponse } from "@dasl/tile-lexicon";
34
41
 
35
42
  export interface MothershipConfig {
36
43
  /** Host of the loading server (`load.webtil.es`), with a scheme and port when not https://. */
37
44
  loadDomain?: string;
45
+ /**
46
+ * The tile origin to run tiles on, as a host (`notes.tiles.example`, with
47
+ * the port if there is one), instead of a fresh random one for every load.
48
+ * For a deployment that means tiles to share an origin or to find their
49
+ * storage again; the loading server has to know the name as one of its tile
50
+ * origins. Leave it out and every tile gets an origin of its own.
51
+ */
52
+ tileHost?: string;
38
53
  /**
39
54
  * Whether the shuttle spends the renderer's WebRTC budget before the tile
40
55
  * loads (the default). A host that blocks WebRTC itself (an Electron app with
@@ -62,12 +77,25 @@ const RCV_WORKER_READY = `${WORKER_PFX}ready`; // worker ready
62
77
  const RCV_WORKER_REQUEST = `${WORKER_PFX}request`; // worker requested something
63
78
  const SND_WORKER_RESPONSE = `${WORKER_PFX}response`; // respond to a worker
64
79
  const TILES_WARNING = `${TILES_PFX}warn`; // worker warnings
65
- const TILES_ERROR = `${TILES_PFX}error`; // shuttle errors
80
+ const TILES_ERROR = `${SHUTTLE_PFX}error`; // shuttle errors
81
+ const RCV_DOCK_STOPPED = 'tiles-dock-stopped'; // the dock stopped the tile
66
82
  // Tiles protocols (https://dasl.ing/tp-data.html and friends): the shuttle
67
83
  // relays `tiles-protocol-up-<protocol>-<kind>` from the tile to us, and
68
84
  // `tiles-protocol-down-<protocol>-<kind>` from us to the tile.
69
85
  const PROTOCOL_UP = 'tiles-protocol-up-';
70
86
  const PROTOCOL_DOWN = 'tiles-protocol-down-';
87
+ // Warnings and errors come as a list; anything else is logged as it is.
88
+ const asList = (msg: unknown): unknown[] => (Array.isArray(msg) ? msg : [msg]);
89
+
90
+ /**
91
+ * The dock stopped a tile, because its runtime tried to leave the frame it
92
+ * runs in: `blocked` when the navigation was refused, `navigated` when the
93
+ * frame loaded something else all the same. The tile is gone either way; a
94
+ * host that wants it back renders it again.
95
+ */
96
+ export interface StoppedEvent extends Event {
97
+ reason: 'blocked' | 'navigated';
98
+ }
71
99
 
72
100
  /** A tiles-protocol message from a tile: `protocol` ("data"), `kind` ("payload", "ready"), and its `payload`. The mothership attaches no meaning to any of them. */
73
101
  export interface ProtocolEvent extends Event {
@@ -75,14 +103,6 @@ export interface ProtocolEvent extends Event {
75
103
  kind: string;
76
104
  payload: unknown;
77
105
  }
78
- /**
79
- * A tp-data message from a tile; `payload` is what the tile sent.
80
- * @deprecated The `data` and `data-ready` events go in the next major
81
- * version. Use `tileData()` from `@dasl/tile-loader/data`, or the `protocol` event.
82
- */
83
- export interface DataEvent extends Event {
84
- payload: unknown;
85
- }
86
106
 
87
107
  export class TileMothership {
88
108
  #loaders: TileLoader[] = [];
@@ -95,31 +115,45 @@ export class TileMothership {
95
115
  init () {
96
116
  window.addEventListener('message', async (ev: MessageEvent) => {
97
117
  const { action } = ev.data || {};
118
+ if (typeof action !== 'string') return;
119
+ // Any frame can post a message, and say anything in it: the window
120
+ // it comes from says which tile is talking. That window is the tile's
121
+ // dock, on the loading server's origin, and it stays there; a message
122
+ // from one of our frames that is somewhere else is not from a dock.
123
+ // Messages still carry an id; we don't read it.
124
+ const id = this.#idForWindow(ev.source);
125
+ if (!id || ev.origin !== this.getLoadOrigin()) return;
126
+ const tile = this.#id2tile.get(id)!;
98
127
  if (action === TILES_WARNING) {
99
- const { msg, id } = ev.data;
100
- console.warn(`[W:${id}]`, ...msg);
128
+ console.warn(`[W:${id}]`, ...asList(ev.data.msg));
101
129
  }
102
- if (action === TILES_ERROR) {
103
- const { msg, id } = ev.data;
104
- console.error(`[S:${id}]`, ...msg);
130
+ else if (action === TILES_ERROR) {
131
+ console.error(`[S:${id}]`, ...asList(ev.data.msg));
132
+ }
133
+ else if (action === RCV_DOCK_STOPPED) {
134
+ const reason = ev.data.payload?.reason === 'blocked' ? 'blocked' : 'navigated';
135
+ console.error(`[D:${id}] The tile's runtime tried to navigate (${reason}); the tile was stopped.`);
136
+ tile.dispatchEvent(Object.assign(new Event('stopped'), { reason }));
105
137
  }
106
138
  else if (action === RCV_SHUTTLE_READY) {
107
- const { id } = ev.data;
108
139
  this.sendToShuttle(id, SND_WORKER_LOAD, { id });
109
140
  }
110
141
  else if (action === RCV_WORKER_READY) {
111
- const { id } = ev.data;
112
- const tile = this.#id2tile.get(id);
113
- if (!tile) throw new Error(`No tile shuttle with ID ${id}`);
114
142
  tile.dispatchEvent(new Event('load'));
115
143
  }
116
144
  else if (action === RCV_WORKER_REQUEST) {
117
- const { type, id, payload } = ev.data;
145
+ const { type, payload } = ev.data;
118
146
  if (type === 'resolve-path') {
119
147
  const { path, requestId } = payload;
120
- const tile = this.#id2tile.get(id);
121
- if (!tile) throw new Error(`No tile shuttle with ID ${id}`);
122
- const res = await tile.resolvePath(path);
148
+ let res: MaslResponse;
149
+ try {
150
+ res = await tile.resolvePath(path);
151
+ }
152
+ catch (err) {
153
+ // Answer all the same, or the worker waits for ever.
154
+ console.error(`Could not resolve ${path}:`, err);
155
+ res = { ok: false, status: 502, statusText: 'Bad Gateway' };
156
+ }
123
157
  // Every document the worker serves, 404s included, carries the
124
158
  // policy: the tile can script anything on its origin, so nothing
125
159
  // on it may be unconfined.
@@ -129,11 +163,7 @@ export class TileMothership {
129
163
  this.sendToShuttle(id, SND_WORKER_RESPONSE, { requestId, response });
130
164
  }
131
165
  }
132
- else if (typeof action === 'string' && action.startsWith(PROTOCOL_UP)) {
133
- // Protocol messages carry no id: the shuttle window they come from
134
- // says which tile is talking, and nothing else gets to pretend.
135
- const tile = this.tileForWindow(ev.source);
136
- if (!tile) return;
166
+ else if (action.startsWith(PROTOCOL_UP)) {
137
167
  const rest = action.slice(PROTOCOL_UP.length);
138
168
  const dash = rest.indexOf('-');
139
169
  if (dash < 1) return;
@@ -141,11 +171,6 @@ export class TileMothership {
141
171
  const kind = rest.slice(dash + 1);
142
172
  const { payload } = ev.data;
143
173
  tile.dispatchEvent(Object.assign(new Event('protocol'), { protocol, kind, payload }));
144
- // Deprecated: tp-data also raised as events of its own, until the
145
- // next major version (see @dasl/tile-loader/data).
146
- if (protocol === 'data') {
147
- tile.dispatchEvent(Object.assign(new Event(kind === 'payload' ? 'data' : `data-${kind}`), { payload }));
148
- }
149
174
  }
150
175
  });
151
176
  }
@@ -158,15 +183,22 @@ export class TileMothership {
158
183
  return tileResponseHeaders(headers);
159
184
  }
160
185
  tileForWindow (source: MessageEventSource | null): Tile | undefined {
186
+ const id = this.#idForWindow(source);
187
+ if (id) return this.#id2tile.get(id);
188
+ }
189
+ #idForWindow (source: MessageEventSource | null): string | undefined {
161
190
  if (!source) return;
162
191
  for (const [id, ifr] of this.#id2shuttle) {
163
- if (ifr.contentWindow === source) return this.#id2tile.get(id);
192
+ if (ifr.contentWindow === source) return id;
164
193
  }
165
194
  }
166
195
  sendToShuttle (id: string, action: string, payload: unknown) {
167
196
  const ifr = this.#id2shuttle.get(id);
168
197
  if (!ifr) return console.error(`No shuttle for ID ${id}`);
169
- ifr.contentWindow?.postMessage({ id, action, payload }, '*');
198
+ // To the dock and no one else: should the frame ever be anywhere but on
199
+ // the loading server, what we say (the tile's documents, the host's
200
+ // data) does not go with it.
201
+ ifr.contentWindow?.postMessage({ id, action, payload }, this.getLoadOrigin());
170
202
  }
171
203
  registerShuttleFrame (ifr: HTMLIFrameElement, tile: Tile) {
172
204
  const id = crypto.randomUUID(); // we might want to make that pluggable
@@ -190,10 +222,19 @@ export class TileMothership {
190
222
  removeLoader (loader: TileLoader) {
191
223
  this.#loaders = this.#loaders.filter(ldr => ldr !== loader);
192
224
  }
193
- getLoadSource () {
225
+ /** The origin of the loading server: where the dock is, and so who we talk to. */
226
+ getLoadOrigin (): string {
194
227
  const domain = this.#conf.loadDomain || 'load.webtil.es';
195
- const base = /^https?:\/\//.test(domain) ? domain : `https://${domain}`;
196
- return new URL('/.well-known/web-tiles/', base).href;
228
+ return new URL(/^https?:\/\//.test(domain) ? domain : `https://${domain}`).origin;
229
+ }
230
+ /**
231
+ * What a tile's frame loads: the dock, on the loading server. Asked like
232
+ * this it picks a fresh origin for the tile and frames the runtime there.
233
+ */
234
+ getLoadSource (): string {
235
+ const url = new URL(dockPath, this.getLoadOrigin());
236
+ if (this.#conf.tileHost) url.searchParams.set('tile', this.#conf.tileHost);
237
+ return url.href;
197
238
  }
198
239
  // Load a tile.
199
240
  async loadTile (url: string): Promise<Tile | false> {
@@ -301,14 +342,6 @@ export class Tile extends EventTarget {
301
342
  if (!this.#shuttleId) throw new Error('The tile is not rendered yet.');
302
343
  this.#mothership.sendToShuttle(this.#shuttleId, `${PROTOCOL_DOWN}${protocol}-${kind}`, payload);
303
344
  }
304
- /**
305
- * Sends a tp-data payload to the tile (https://dasl.ing/tp-data.html).
306
- * @deprecated Goes in the next major version. Use `tileData(tile).send()`
307
- * from `@dasl/tile-loader/data`, or `sendProtocol('data', 'payload', payload)`.
308
- */
309
- sendData (payload: unknown) {
310
- this.sendProtocol('data', 'payload', payload);
311
- }
312
345
  attachIframe (ifr: HTMLIFrameElement) {
313
346
  this.#shuttleId = this.#mothership.registerShuttleFrame(ifr, this);
314
347
  ifr.addEventListener('load', () => this.#mothership.startShuttle(this.#shuttleId!));