cornfield 0.48.0 → 0.49.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/core/strings.js +14 -0
- package/explorer/check.js +7 -227
- package/explorer/connection.js +3 -3
- package/explorer/engine/checks.js +252 -0
- package/explorer/engine/connection.js +36 -0
- package/explorer/engine/layers.js +1 -0
- package/explorer/engine/player.js +489 -0
- package/explorer/engine/proposal.js +373 -0
- package/explorer/engine/render.js +1 -0
- package/explorer/engine/source.js +315 -0
- package/explorer/engine/theme-sheet.js +444 -0
- package/explorer/index.html +6 -59
- package/explorer/instance.js +82 -6
- package/explorer/page-address.js +59 -0
- package/explorer/page-pieces.js +33 -5
- package/explorer/page.js +20 -20
- package/explorer/panels.js +1 -3
- package/explorer/pieces/changes.js +502 -0
- package/explorer/pieces/decision.js +657 -0
- package/explorer/pieces/layers.js +4 -0
- package/explorer/pieces/map.js +113 -2
- package/explorer/pieces/node-panel.js +34 -1
- package/explorer/pieces/proposal.js +488 -0
- package/explorer/pieces/scope-link.js +50 -0
- package/explorer/pieces/scope.js +247 -0
- package/explorer/pieces/status.css +24 -0
- package/explorer/pieces/status.js +144 -0
- package/explorer/plugins.js +0 -10
- package/explorer/proposal.js +64 -697
- package/explorer/review-column.js +24 -439
- package/explorer/review.js +27 -673
- package/explorer/scenario-ui.js +32 -478
- package/explorer/sheets.js +8 -7
- package/explorer/view.js +6 -5
- package/explorer/waiting-line.js +7 -50
- package/package.json +1 -1
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/engine/source
|
|
3
|
+
*
|
|
4
|
+
* Where the map's data comes from (spec 0011 D6): `/cornfield/config.json` and `/topology.json` of a cornfield server, read through an
|
|
5
|
+
* instance and applied to it. The same module serves cornfield's own page (an instance with no base: the paths and the fetch options are what
|
|
6
|
+
* they were, `{ cache: 'no-store' }` for the config and nothing for the topology) and a scope (pieces/scope.js) or a map with a `src`
|
|
7
|
+
* (an instance with a base: the same two routes of another origin, with `credentials: 'include'`, because they are behind the maintainer's session).
|
|
8
|
+
*
|
|
9
|
+
* parseSource(src) an address ('https://map.example.com', with an optional path) -> { ok, base } or { ok: false, problem }
|
|
10
|
+
* compareVersions(a, b) 'same' | 'patch' | 'minor' | 'major' | 'unknown' (what differs between two cornfield versions)
|
|
11
|
+
* signInUrl(base, pageUrl) the server's sign-in, which returns to `pageUrl`
|
|
12
|
+
* fetchConfig(inst, opts) the config object, or a SourceError (kind 'auth' | 'server' | 'network')
|
|
13
|
+
* fetchTopology(inst, path) { nodes, edges }, or a SourceError
|
|
14
|
+
* applyConfig(inst, config) what the instance takes from a config: its words, the project's layers, the map's theme (icons)
|
|
15
|
+
* createSource(inst, opts) the load as a small state machine that tells the instance where it is (below)
|
|
16
|
+
*
|
|
17
|
+
* It never `import()`s, evaluates or follows anything a config names (spec 0011 D10): `sources` (the live layer, the House view, the checker)
|
|
18
|
+
* are plugin modules that only cornfield's own page loads (explorer/plugins.js), and a config's `links` and `brand` are not read here at all.
|
|
19
|
+
*
|
|
20
|
+
* The instance event `source` (name: detail) says where the load is. Every detail has a `state`:
|
|
21
|
+
*
|
|
22
|
+
* loading a request is out
|
|
23
|
+
* ready the topology is there (read from the server, or handed in); `source.topology` and `source.config` hold what came
|
|
24
|
+
* sign-in { status, signIn } the server answered 401 or 403 to a gated read; `signIn` is its sign-in, which returns to the page
|
|
25
|
+
* not-allowed { origin } the server gave no CORS answer: the page's origin is not in the server's `embed.origins`
|
|
26
|
+
* offline the server cannot be reached (or the browser is offline)
|
|
27
|
+
* server { status, message } the server answered, and what it said is not a config or a topology
|
|
28
|
+
* address { problem } `src` is not an http(s) address of a server
|
|
29
|
+
* version { server, package, drift } the package and the server differ by a minor version or more; the load goes on (said once)
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import { setStrings } from './strings.js';
|
|
33
|
+
import { setProjectLayers } from './layers.js';
|
|
34
|
+
import { mapStyleOf } from './map-style.js';
|
|
35
|
+
import { sessionInit } from './connection.js';
|
|
36
|
+
|
|
37
|
+
export const CONFIG_PATH = '/cornfield/config.json';
|
|
38
|
+
export const TOPOLOGY_PATH = '/topology.json';
|
|
39
|
+
/** A route that answers a listed origin without a session: the reach test for a fetch that got no answer */
|
|
40
|
+
export const REACH_PATH = '/cornfield/theme.css';
|
|
41
|
+
export const SIGN_IN_PATH = '/auth/start';
|
|
42
|
+
export const SOURCE_STATES = Object.freeze(['loading', 'ready', 'sign-in', 'not-allowed', 'offline', 'server', 'address', 'version']);
|
|
43
|
+
|
|
44
|
+
/** A control character, a space or a backslash (a browser reads `\` as `/`): never in an address */
|
|
45
|
+
const BAD_CHAR = /[\u0000- \u007f\\]/;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* An address of a cornfield server, checked: an http(s) origin with an optional path, and nothing else (no user name, no query, no fragment).
|
|
49
|
+
* `base` is what the instance is made with: the origin and the path without a trailing slash.
|
|
50
|
+
* @param {unknown} src
|
|
51
|
+
* @returns {{ ok: true, base: string, origin: string } | { ok: false, problem: string }}
|
|
52
|
+
*/
|
|
53
|
+
export function parseSource(src) {
|
|
54
|
+
if (typeof src !== 'string' || !src.trim()) return { ok: false, problem: 'is empty' };
|
|
55
|
+
const text = src.trim();
|
|
56
|
+
if (BAD_CHAR.test(text)) return { ok: false, problem: 'has a space, a control character or a backslash' };
|
|
57
|
+
let u;
|
|
58
|
+
try { u = new URL(text); } catch { return { ok: false, problem: 'is not an address (an http or https address, as the server is opened in a browser)' }; }
|
|
59
|
+
if (u.protocol !== 'https:' && u.protocol !== 'http:') return { ok: false, problem: 'is not http or https' };
|
|
60
|
+
if (!/^https?:\/\/[^/?#]/i.test(text)) return { ok: false, problem: 'has no host' };
|
|
61
|
+
if (u.username || u.password) return { ok: false, problem: 'has a user name or password' };
|
|
62
|
+
if (text.includes('?') || text.includes('#')) return { ok: false, problem: 'has a query or a fragment' };
|
|
63
|
+
return { ok: true, base: u.origin + u.pathname.replace(/\/+$/, ''), origin: u.origin };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The numbers of a version, or null */
|
|
67
|
+
const parts = (v) => {
|
|
68
|
+
const m = /^\s*v?(\d{1,9})\.(\d{1,9})\.(\d{1,9})(?:[-+][0-9A-Za-z.-]{1,64})?\s*$/.exec(String(v == null ? '' : v));
|
|
69
|
+
return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* What differs between two cornfield versions: the major, the minor (a different minor of the same major) or only the patch. A version that is
|
|
74
|
+
* not `x.y.z` (with an optional `-pre` or `+build`; a dev build, none at all, or anything else after it) cannot be compared: 'unknown', and is never
|
|
75
|
+
* shown, so what the version notice repeats of the server is only ever a version.
|
|
76
|
+
* @returns {'same' | 'patch' | 'minor' | 'major' | 'unknown'}
|
|
77
|
+
*/
|
|
78
|
+
export function compareVersions(a, b) {
|
|
79
|
+
const x = parts(a);
|
|
80
|
+
const y = parts(b);
|
|
81
|
+
if (!x || !y) return 'unknown';
|
|
82
|
+
if (x[0] !== y[0]) return 'major';
|
|
83
|
+
if (x[1] !== y[1]) return 'minor';
|
|
84
|
+
return x[2] !== y[2] ? 'patch' : 'same';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The server's sign-in, which ends on the page it was asked from: `<base>/auth/start?returnTo=<page URL>` (docs/sign-in.md). The server follows
|
|
89
|
+
* `returnTo` only for an origin its `embed.origins` lists. A page that is not http(s) (a file, about:blank) has nothing to return to. A user name
|
|
90
|
+
* or password in the page's address (`https://user:pass@tasks.example.com/`) is never sent to the server: it is taken out (the server would refuse
|
|
91
|
+
* such a `returnTo` anyway).
|
|
92
|
+
* @param {string} base
|
|
93
|
+
* @param {string} pageUrl
|
|
94
|
+
*/
|
|
95
|
+
export function signInUrl(base, pageUrl) {
|
|
96
|
+
let back = '';
|
|
97
|
+
if (typeof pageUrl === 'string' && /^https?:\/\//i.test(pageUrl)) {
|
|
98
|
+
try {
|
|
99
|
+
const u = new URL(pageUrl);
|
|
100
|
+
u.username = '';
|
|
101
|
+
u.password = '';
|
|
102
|
+
back = `?returnTo=${encodeURIComponent(u.href)}`;
|
|
103
|
+
} catch { back = ''; }
|
|
104
|
+
}
|
|
105
|
+
return `${base}${SIGN_IN_PATH}${back}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Why a read failed: `auth` (401 or 403: sign in), `server` (it answered, and not with what was asked for) or `network` (no answer the page may read) */
|
|
109
|
+
export class SourceError extends Error {
|
|
110
|
+
/** @param {'auth' | 'server' | 'network'} kind @param {string} message @param {{ status?: number, cause?: unknown }} [extra] */
|
|
111
|
+
constructor(kind, message, { status = 0, cause } = {}) {
|
|
112
|
+
super(message);
|
|
113
|
+
this.name = 'SourceError';
|
|
114
|
+
this.kind = kind;
|
|
115
|
+
this.status = status;
|
|
116
|
+
if (cause !== undefined) this.cause = cause;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The fetch options of a read: the maintainer's session goes only to the base's own origin, by engine/connection.js `sessionInit` (one rule for
|
|
122
|
+
* every read and write of a piece); the page's own request is read as `fetch` reads it. Nothing is added where there is nothing to add.
|
|
123
|
+
*/
|
|
124
|
+
function optionsOf(inst, path, init, signal) {
|
|
125
|
+
const out = { ...sessionInit(inst, path, init) };
|
|
126
|
+
if (signal) out.signal = signal;
|
|
127
|
+
return Object.keys(out).length ? out : undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Fetches `url` through the instance and returns the response, or throws a SourceError for a network failure, 401/403 and any other non-OK status.
|
|
132
|
+
* `label` is the address a message names (the page's has always said `GET /topology.json answered 500`, without the cache-buster).
|
|
133
|
+
*/
|
|
134
|
+
async function read(inst, url, init, signal, label = url) {
|
|
135
|
+
let res;
|
|
136
|
+
try {
|
|
137
|
+
const options = optionsOf(inst, url, init, signal);
|
|
138
|
+
res = options ? await inst.fetch(url, options) : await inst.fetch(url);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
throw new SourceError('network', err && err.message ? err.message : String(err), { cause: err });
|
|
141
|
+
}
|
|
142
|
+
if (res.status === 401 || res.status === 403) throw new SourceError('auth', `GET ${label} answered ${res.status} ${res.statusText}`.trim(), { status: res.status });
|
|
143
|
+
if (!res.ok) throw new SourceError('server', `GET ${label} answered ${res.status} ${res.statusText}`.trim(), { status: res.status });
|
|
144
|
+
return res;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The config the server hands the browser (`/cornfield/config.json`). Throws a SourceError; cornfield's own page, which works without one,
|
|
149
|
+
* catches it and goes on with `{}`.
|
|
150
|
+
* @param {ReturnType<typeof import('../instance.js').createInstance>} inst
|
|
151
|
+
* @param {{ signal?: AbortSignal }} [opts]
|
|
152
|
+
*/
|
|
153
|
+
export async function fetchConfig(inst, { signal } = {}) {
|
|
154
|
+
const res = await read(inst, CONFIG_PATH, { cache: 'no-store' }, signal);
|
|
155
|
+
let config;
|
|
156
|
+
try { config = await res.json(); } catch (err) { throw new SourceError('server', `${CONFIG_PATH} is not JSON`, { status: res.status, cause: err }); }
|
|
157
|
+
if (!config || typeof config !== 'object' || Array.isArray(config)) throw new SourceError('server', `${CONFIG_PATH} is not a config`, { status: res.status });
|
|
158
|
+
return config;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The map (`/topology.json`, or `path`): `{ nodes, edges }`, with at least one node. The caller keeps the object as it is (the map works on copies).
|
|
163
|
+
* The address carries a cache-buster, as the page's always has.
|
|
164
|
+
* @param {ReturnType<typeof import('../instance.js').createInstance>} inst
|
|
165
|
+
* @param {string} [path]
|
|
166
|
+
* @param {{ signal?: AbortSignal }} [opts]
|
|
167
|
+
*/
|
|
168
|
+
export async function fetchTopology(inst, path = TOPOLOGY_PATH, { signal } = {}) {
|
|
169
|
+
const res = await read(inst, path + (path.includes('?') ? '&v=' : '?v=') + Date.now(), undefined, signal, path);
|
|
170
|
+
let data;
|
|
171
|
+
try { data = await res.json(); } catch (err) { throw new SourceError('server', `${path} is not JSON`, { status: res.status, cause: err }); }
|
|
172
|
+
if (!data || !Array.isArray(data.nodes) || data.nodes.length === 0) throw new SourceError('server', `${path} has no nodes`, { status: res.status });
|
|
173
|
+
return data;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* What an instance takes from a config, as cornfield's page does: the repo's words (`strings`), the project's layers, and the map's theme
|
|
178
|
+
* (the icons, fetched through `inst.url()` with `crossOrigin` when the instance has a base, and the pixelated flag). The brand, the title and the
|
|
179
|
+
* links are the page's header; the `sources` are plugin modules, which only the page loads. Nothing in a config becomes script here.
|
|
180
|
+
* @param {ReturnType<typeof import('../instance.js').createInstance>} inst
|
|
181
|
+
* @param {object} config
|
|
182
|
+
*/
|
|
183
|
+
export function applyConfig(inst, config) {
|
|
184
|
+
if (!config || typeof config !== 'object') return;
|
|
185
|
+
setStrings(inst, config.strings);
|
|
186
|
+
mapStyleOf(inst).applyTheme(config.theme);
|
|
187
|
+
setProjectLayers(inst, config.layers);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The load as a state machine on an instance. It makes the requests (when the instance has a base), keeps what came, and says where it is with the
|
|
192
|
+
* instance's `source` event (above); a piece that joins later reads `state()`. `destroy()` aborts what is in flight and says nothing more.
|
|
193
|
+
*
|
|
194
|
+
* @param {ReturnType<typeof import('../instance.js').createInstance>} inst
|
|
195
|
+
* @param {object} [opts]
|
|
196
|
+
* @param {string} [opts.version] the package's version, compared with the server's (`config.version`)
|
|
197
|
+
* @param {() => string} [opts.pageUrl] the page's address, for the sign-in's way back
|
|
198
|
+
* @param {(config: object) => void} [opts.onConfig] called with each config taken, the host's or the server's (a scope starts the server's theme on it)
|
|
199
|
+
*/
|
|
200
|
+
export function createSource(inst, { version = '', pageUrl = () => '', onConfig = () => {} } = {}) {
|
|
201
|
+
let destroyed = false;
|
|
202
|
+
let current = null;
|
|
203
|
+
let controller = null;
|
|
204
|
+
let run = 0;
|
|
205
|
+
const own = { topology: null, config: null, strings: null };
|
|
206
|
+
const source = {
|
|
207
|
+
/** The topology in force: the host's, else the server's */
|
|
208
|
+
topology: null,
|
|
209
|
+
/** The config in force: the host's, else the server's */
|
|
210
|
+
config: null,
|
|
211
|
+
/** The last thing said about the load (`{ state, … }`, never the version notice), or null before the first */
|
|
212
|
+
state: () => current,
|
|
213
|
+
/** The version notice, once the server's config said a different minor or major; null until then */
|
|
214
|
+
notice: null,
|
|
215
|
+
say(detail) {
|
|
216
|
+
if (destroyed) return;
|
|
217
|
+
if (detail.state === 'version') source.notice = detail; else current = detail;
|
|
218
|
+
inst.emit('source', detail);
|
|
219
|
+
},
|
|
220
|
+
/**
|
|
221
|
+
* Takes what the host fetched for itself. A key that is `undefined` is left as it was, `null` takes the host's back. With a topology there is
|
|
222
|
+
* nothing to load: no request is made for it. The host's `strings` win over the server's.
|
|
223
|
+
*/
|
|
224
|
+
provide({ topology, config, strings } = {}) {
|
|
225
|
+
if (destroyed) return;
|
|
226
|
+
if (strings !== undefined) { own.strings = strings; useStrings(source.config); }
|
|
227
|
+
if (config !== undefined) { own.config = config; if (config) takeConfig(config); else if (source.fetchedConfig) takeConfig(source.fetchedConfig); }
|
|
228
|
+
if (topology !== undefined) {
|
|
229
|
+
own.topology = topology;
|
|
230
|
+
source.topology = topology || source.fetchedTopology || null;
|
|
231
|
+
if (source.topology) source.say({ state: 'ready' });
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
fetchedConfig: null,
|
|
235
|
+
fetchedTopology: null,
|
|
236
|
+
/** Reads the server's config and topology (what the host did not hand in). Does nothing for an instance with no base. */
|
|
237
|
+
async load() {
|
|
238
|
+
if (destroyed || !inst.base) return;
|
|
239
|
+
if (own.config && own.topology) { source.topology = own.topology; source.say({ state: 'ready' }); return; } // the host fetched for itself: nothing to ask
|
|
240
|
+
if (controller) controller.abort();
|
|
241
|
+
controller = new AbortController();
|
|
242
|
+
const { signal } = controller;
|
|
243
|
+
const mine = ++run;
|
|
244
|
+
const live = () => !destroyed && mine === run && !signal.aborted;
|
|
245
|
+
source.say({ state: 'loading' });
|
|
246
|
+
try {
|
|
247
|
+
if (!own.config) {
|
|
248
|
+
const config = await fetchConfig(inst, { signal });
|
|
249
|
+
if (!live()) return;
|
|
250
|
+
source.fetchedConfig = config;
|
|
251
|
+
takeConfig(config);
|
|
252
|
+
}
|
|
253
|
+
if (own.topology) {
|
|
254
|
+
source.topology = own.topology;
|
|
255
|
+
} else {
|
|
256
|
+
const topology = await fetchTopology(inst, TOPOLOGY_PATH, { signal });
|
|
257
|
+
if (!live()) return;
|
|
258
|
+
source.fetchedTopology = topology;
|
|
259
|
+
source.topology = topology;
|
|
260
|
+
}
|
|
261
|
+
source.say({ state: 'ready' });
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (!live()) return;
|
|
264
|
+
const detail = await failure(err, signal);
|
|
265
|
+
if (live()) source.say(detail);
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/** The words: the host's, else the config's */
|
|
271
|
+
function useStrings(config) {
|
|
272
|
+
setStrings(inst, own.strings || (config && config.strings));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function takeConfig(config) {
|
|
276
|
+
source.config = config;
|
|
277
|
+
applyConfig(inst, config);
|
|
278
|
+
if (own.strings) useStrings(config);
|
|
279
|
+
const drift = compareVersions(version, config.version);
|
|
280
|
+
if (version && (drift === 'minor' || drift === 'major')) source.say({ state: 'version', server: String(config.version).trim(), package: version, drift });
|
|
281
|
+
onConfig(config);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** What a failed read says. A fetch that was refused outright says nothing about why: the server is told apart from "not listed" by asking a route that answers anyone */
|
|
285
|
+
async function failure(err, signal) {
|
|
286
|
+
if (!(err instanceof SourceError)) return { state: 'server', status: 0, message: err && err.message ? err.message : String(err) };
|
|
287
|
+
if (err.kind === 'auth') return { state: 'sign-in', status: err.status, signIn: signInUrl(inst.base, pageUrl()) };
|
|
288
|
+
if (err.kind === 'server') return { state: 'server', status: err.status, message: err.message };
|
|
289
|
+
const offline = inst.win && inst.win.navigator && inst.win.navigator.onLine === false;
|
|
290
|
+
if (offline) return { state: 'offline' };
|
|
291
|
+
try {
|
|
292
|
+
await inst.fetch(REACH_PATH, { method: 'HEAD', mode: 'no-cors', credentials: 'omit', signal });
|
|
293
|
+
} catch {
|
|
294
|
+
return { state: 'offline' };
|
|
295
|
+
}
|
|
296
|
+
return { state: 'not-allowed', origin: originOf(pageUrl()) };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
holderOf(inst).source = source;
|
|
300
|
+
inst.own(() => {
|
|
301
|
+
destroyed = true;
|
|
302
|
+
if (controller) controller.abort();
|
|
303
|
+
holderOf(inst).source = null;
|
|
304
|
+
});
|
|
305
|
+
return source;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The origin of a page address, or '' */
|
|
309
|
+
export function originOf(url) {
|
|
310
|
+
try { return /^https?:/i.test(url) ? new URL(url).origin : ''; } catch { return ''; }
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const holderOf = (inst) => inst.slot('source', () => ({ source: null }));
|
|
314
|
+
/** The instance's source, if a scope or a map with a `src` made one (a piece that joins late reads what it has said); else null */
|
|
315
|
+
export const sourceOf = (inst) => holderOf(inst).source;
|