@enspirit/opspicy 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +325 -2
- package/bin/entry.js +48 -0
- package/bin/help.js +115 -0
- package/bin/opspicy +27 -0
- package/dist/addresses.js +27 -0
- package/dist/behind.js +64 -0
- package/dist/citizens.js +71 -0
- package/dist/configuration.d.ts +56 -0
- package/dist/configuration.js +66 -0
- package/dist/explorer/afresh.js +43 -0
- package/dist/explorer/api.js +101 -0
- package/dist/explorer/asking.js +59 -0
- package/dist/explorer/build.js +11 -0
- package/dist/explorer/calls.js +42 -0
- package/dist/explorer/document.js +117 -0
- package/dist/explorer/elo-refs.js +153 -0
- package/dist/explorer/elo.js +95 -0
- package/dist/explorer/fonts/OFL.txt +93 -0
- package/dist/explorer/fonts/dm-sans-latin.woff2 +0 -0
- package/dist/explorer/html.js +104 -0
- package/dist/explorer/index.js +62 -0
- package/dist/explorer/json.js +33 -0
- package/dist/explorer/machine.js +34 -0
- package/dist/explorer/masking.js +57 -0
- package/dist/explorer/operation.js +97 -0
- package/dist/explorer/pages.js +159 -0
- package/dist/explorer/script/base.js +103 -0
- package/dist/explorer/script/call.js +161 -0
- package/dist/explorer/script/calling.js +103 -0
- package/dist/explorer/script/connection.js +83 -0
- package/dist/explorer/script/copying.js +75 -0
- package/dist/explorer/script/filter.js +56 -0
- package/dist/explorer/script/formaldef.js +37 -0
- package/dist/explorer/script/main.js +82 -0
- package/dist/explorer/script/markdown.js +143 -0
- package/dist/explorer/script/notes.js +184 -0
- package/dist/explorer/script/noting.js +38 -0
- package/dist/explorer/script/peek.js +62 -0
- package/dist/explorer/script/peeking.js +19 -0
- package/dist/explorer/script/ranking.js +91 -0
- package/dist/explorer/script/review.js +126 -0
- package/dist/explorer/script/rows.js +58 -0
- package/dist/explorer/script/run.js +168 -0
- package/dist/explorer/script/running.js +66 -0
- package/dist/explorer/script/search.js +122 -0
- package/dist/explorer/script/sending.js +64 -0
- package/dist/explorer/script/served.js +51 -0
- package/dist/explorer/serve.js +41 -0
- package/dist/explorer/server.js +143 -0
- package/dist/explorer/site.js +78 -0
- package/dist/explorer/style.css +247 -0
- package/dist/explorer/telling.js +30 -0
- package/dist/explorer/tests.js +78 -0
- package/dist/explorer/wiki.js +90 -0
- package/dist/files.js +178 -0
- package/dist/finding.js +17 -0
- package/dist/judging.js +99 -0
- package/dist/lines.js +30 -0
- package/dist/main.js +128 -0
- package/dist/meaning.js +100 -0
- package/dist/modelling.js +173 -0
- package/dist/naming.js +84 -0
- package/dist/notation.js +119 -0
- package/dist/planning.js +41 -0
- package/dist/reach.js +177 -0
- package/dist/reading.js +135 -0
- package/dist/report-file.js +42 -0
- package/dist/report.js +63 -0
- package/dist/running.js +105 -0
- package/dist/selecting.js +71 -0
- package/dist/starting.js +41 -0
- package/dist/suite.js +32 -0
- package/dist/upgrade.js +27 -0
- package/dist/upgrading.js +76 -0
- package/dist/words.js +103 -0
- package/dist/world.js +129 -0
- package/package.json +57 -4
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// The page of an operation: what it is, its input and its output, its clauses, its examples and
|
|
2
|
+
// its counterexamples. What the suite says, and nothing of a run: that is in tests.ts.
|
|
3
|
+
// Pure: the wiki is given, markup is what comes out.
|
|
4
|
+
import { PRE, SAID, isForType, sameSentence } from '../suite.js';
|
|
5
|
+
import { requestOf } from './calls.js';
|
|
6
|
+
import { eloHtml } from './elo.js';
|
|
7
|
+
import { jsonHtml } from './json.js';
|
|
8
|
+
import { citizens, compact, elo, esc, link, list, markOf, mono, said } from './html.js';
|
|
9
|
+
import { idOf, violating } from './wiki.js';
|
|
10
|
+
/** What each group of clauses says: what is, and what is required, before and after the call. */
|
|
11
|
+
const GROUP = {
|
|
12
|
+
DomPre: 'Before the call, of the domain',
|
|
13
|
+
ReqPre: 'Before the call, required of it',
|
|
14
|
+
DomPost: 'After the call, of the domain',
|
|
15
|
+
ReqPost: 'After the call, required of it',
|
|
16
|
+
ReqTrig: 'When it is performed without being asked',
|
|
17
|
+
and: 'What one example alone also shows',
|
|
18
|
+
};
|
|
19
|
+
export const crumbs = (...parts) => `<p class="crumb">${parts.join('<span aria-hidden="true">›</span>')}</p>`;
|
|
20
|
+
export const areaTitle = (name) => (name === '_shared' ? 'Shared' : name.charAt(0).toUpperCase() + name.slice(1));
|
|
21
|
+
const row = (label, value) => `<dt>${esc(label)}</dt><dd>${value}</dd>`;
|
|
22
|
+
/** States, each leading to its row of the State page; a list its file does not say is derived, and says so. */
|
|
23
|
+
const statesOf = (names, said) => `${names.map((name) => link(`#${idOf('state', name)}`, name)).join(', ') || '<span class="m">none</span>'}${said ? '' : ' <span class="m s" title="Not said by its file: derived from its formaldefs">· derived</span>'}`;
|
|
24
|
+
const typeLink = (name) => (name ? link(`#${idOf('type', name)}`, name) : '<span class="m">nothing</span>');
|
|
25
|
+
/** Who calls, and from which Background: a name for a table cell, each leading to who and what it is. */
|
|
26
|
+
export const callerOf = (kase) => {
|
|
27
|
+
const who = kase.given.actor ? link(`#${idOf('actor', kase.given.actor)}`, kase.given.actor) : '<span class="m">nobody</span>';
|
|
28
|
+
return kase.given.seed ? `${who}<br><span class="m s">${link(`#${idOf('background', kase.given.seed)}`, kase.given.seed)}</span>` : who;
|
|
29
|
+
};
|
|
30
|
+
/** What a case sends, as a cell: its data, or the Elo expression that gives it. */
|
|
31
|
+
export const sentOf = (kase, refs) => typeof kase.input === 'string' ? `<code class="elo">${eloHtml(kase.input, refs)}</code>` : `<span class="mono">${kase.input === null || kase.input === undefined ? 'nothing' : jsonHtml(compact(kase.input))}</span>`;
|
|
32
|
+
/** What a case is answered, as a cell: its status, and the code of a refusal. */
|
|
33
|
+
export const answeredOf = (kase) => mono(`${kase.then.status}${kase.then.code ? ` ${kase.then.code}` : ''}`);
|
|
34
|
+
/** A clause, as a citizen: its mark for a name, its sentence, its formaldef, and where a condition gives it, the way there. */
|
|
35
|
+
function clauseShown(operation, clause, { conditions, refs }) {
|
|
36
|
+
const shared = clause.from === 'shared' ? conditions.find((one) => sameSentence(one.description, clause.description)) : undefined;
|
|
37
|
+
const origin = shared ? ` <a class="m s" href="#${esc(idOf('condition', shared.name))}">condition</a>` : '';
|
|
38
|
+
const against = PRE.includes(clause.kind) ? violating(operation, clause.description) : [];
|
|
39
|
+
const refused = against.length ? against.map((kase) => said(kase.description)).join('<br>') : '<span class="m">none</span>';
|
|
40
|
+
return { id: idOf('clause', clause.name), source: clause.source, name: markOf(clause), description: `${said(clause.description)}${origin}`, formaldef: clause.formaldef ? elo(clause.formaldef, refs) : undefined, more: PRE.includes(clause.kind) ? [refused] : [] };
|
|
41
|
+
}
|
|
42
|
+
/** One group of clauses, as citizens: before the call, with the counterexamples that refuse each. */
|
|
43
|
+
function clauseGroup(operation, kind, around) {
|
|
44
|
+
const clauses = operation.clauses.filter((one) => one.kind === kind);
|
|
45
|
+
if (!clauses.length)
|
|
46
|
+
return '';
|
|
47
|
+
const pre = PRE.includes(kind);
|
|
48
|
+
return citizens(kind, clauses.map((clause) => clauseShown(operation, clause, around)), { level: 'h3', note: GROUP[kind], columns: pre ? ['', '', 'Refused when'] : [], widths: pre ? ['3%', '67%', '30%'] : ['3%', '97%'] });
|
|
49
|
+
}
|
|
50
|
+
/** A main section of a page: its title, and its content, which the toggle at the top of the page collapses. */
|
|
51
|
+
const fold = (title, content) => `<details class="fold" open><summary class="fold-title">${esc(title)}</summary>${content}</details>`;
|
|
52
|
+
/** What one example alone also shows: each sentence with its mark, at an address of its own as any clause has, then with what decides it. */
|
|
53
|
+
const alsoShown = (kase) => kase.and.map((clause) => `<span class="also" id="${esc(idOf('clause', clause.name))}"${clause.source ? ` data-source="${esc(clause.source)}"` : ''}>${markOf(clause)} ${said(clause.description)}</span>`).join('<br>');
|
|
54
|
+
const alsoDecided = (kase, refs) => kase.and.filter((clause) => clause.formaldef).map((clause) => `<p class="m s">${said(clause.description)}</p>${elo(clause.formaldef ?? '', refs)}`).join('');
|
|
55
|
+
/** The columns of the examples and of the counterexamples: the same widths, so the two tables line up. */
|
|
56
|
+
const CASE_WIDTHS = ['24%', '14%', '26%', '14%', '22%'];
|
|
57
|
+
function casesShown(operation, kind, refs) {
|
|
58
|
+
const cases = operation.cases.filter((kase) => kase.kind === kind);
|
|
59
|
+
const example = kind === 'example';
|
|
60
|
+
if (!cases.length)
|
|
61
|
+
return `<p class="sub">No ${kind}.</p>`;
|
|
62
|
+
const shown = cases.map((kase) => ({
|
|
63
|
+
id: idOf('case', kase.name),
|
|
64
|
+
source: kase.source,
|
|
65
|
+
request: JSON.stringify(requestOf(operation, kase)),
|
|
66
|
+
name: `${said(kase.description)}${kase.generated ? '<br><span class="m s">from a condition</span>' : ''}`,
|
|
67
|
+
description: callerOf(kase),
|
|
68
|
+
formaldef: alsoDecided(kase, refs) || undefined,
|
|
69
|
+
more: [sentOf(kase, refs), answeredOf(kase), example ? alsoShown(kase) : isForType(operation, kase) ? `what is not ${typeLink(operation.input)}` : said(kase.for ?? '')],
|
|
70
|
+
}));
|
|
71
|
+
return citizens(example ? 'Examples' : 'Counterexamples', shown, { level: 'h3', columns: ['When', 'Caller', 'Sends', 'Answers', example ? 'Also shows' : 'Refuses'], widths: CASE_WIDTHS, note: `${cases.length} of them` });
|
|
72
|
+
}
|
|
73
|
+
/** One side of the input or the output: what the type is, and its declaration, highlighted as Elo. */
|
|
74
|
+
function typeSide(label, name, { types, refs }) {
|
|
75
|
+
const type = name ? types.get(name) : undefined;
|
|
76
|
+
if (!name)
|
|
77
|
+
return `<section aria-label="${label}"><p class="m">nothing</p></section>`;
|
|
78
|
+
if (!type)
|
|
79
|
+
return `<section aria-label="${label}"><p class="sub">${esc(name)}: not declared in a types.elo.</p></section>`;
|
|
80
|
+
return `<section aria-label="${label}"><p class="sub">${said(type.description)}</p>${elo(`${type.name} = ${type.formaldef}`, refs, { self: idOf('type', type.name) })}</section>`;
|
|
81
|
+
}
|
|
82
|
+
export function operationPage(operation, around) {
|
|
83
|
+
const gaps = operation.gaps.length ? fold('Gaps', `<p class="sub">The transition the operation does not say, what no case shows, or no counterexample violates.</p>${list(operation.gaps.map((gap) => `<span class="m">· ${esc(gap)}</span>`))}`) : '';
|
|
84
|
+
const kind = operation.observes ? 'Observation: it only reads, so it is checked to change nothing' : 'Operation';
|
|
85
|
+
const { reach } = operation;
|
|
86
|
+
const meta = [row('Performed by', link(`#${idOf('agent', operation.agent)}`, operation.agent)), row('Route', mono(`${operation.entry.method} ${operation.entry.path}`)), row('Kind', esc(kind)), row('Input', typeLink(operation.input)), row('Output', typeLink(operation.output)), row('Monitors', statesOf(reach.monitors, reach.said.monitors)), row('Controls', statesOf(reach.controls, reach.said.controls)), row('Specification', mono(operation.file)), row('Tests', link(`#${idOf('test', operation.name)}`, 'Results of the last run'))];
|
|
87
|
+
const io = `<div class="io">${typeSide('Input', operation.input, around)}<span class="arrow" aria-hidden="true">→</span>${typeSide('Output', operation.output, around)}</div>`;
|
|
88
|
+
const body = `<div class="page-top">${crumbs(link('#home', 'Specification'), link(`#${idOf('area', operation.area)}`, areaTitle(operation.area)))}<button type="button" class="fold-all" aria-expanded="true">Collapse all</button></div>
|
|
89
|
+
<h1 class="code">${esc(operation.name)}</h1>
|
|
90
|
+
<p class="lead">${said(operation.description)}</p>
|
|
91
|
+
<dl class="meta">${meta.join('')}</dl>
|
|
92
|
+
${fold('Input and output', io)}
|
|
93
|
+
${fold('Clauses', SAID.map((group) => clauseGroup(operation, group, around)).join(''))}
|
|
94
|
+
${fold('Cases', `${casesShown(operation, 'example', around.refs)}${casesShown(operation, 'counterexample', around.refs)}`)}
|
|
95
|
+
${gaps}`;
|
|
96
|
+
return { id: idOf('op', operation.name), kind: operation.observes ? 'Observation' : 'Operation', title: operation.name, nav: idOf('area', operation.area), source: operation.source, said: `${operation.description} ${operation.entry.method} ${operation.entry.path}`, area: operation.area, quote: operation.description, body };
|
|
97
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// The pages of the specification, as HTML sections: one per page, each with its own id, and
|
|
2
|
+
// every citizen at home on one of them, under an anchor of its own. What the suite says, and
|
|
3
|
+
// nothing of a run: that is in tests.ts. Pure: the wiki is given, markup is what comes out.
|
|
4
|
+
import { isA } from '../suite.js';
|
|
5
|
+
import { citizens, described, elo, esc, link, list, mono, plural, said } from './html.js';
|
|
6
|
+
import { areaTitle, crumbs, operationPage } from './operation.js';
|
|
7
|
+
import { idOf } from './wiki.js';
|
|
8
|
+
/**
|
|
9
|
+
* What a section says first: what its file says of it, in the words of the domain, when it says
|
|
10
|
+
* something; then how opspicy reads it, quieter. Without the first, the second leads.
|
|
11
|
+
*/
|
|
12
|
+
const introOf = (description, how) => (description ? `${described(description)}<p class="sub how">${esc(how)}</p>` : `<p class="lead">${esc(how)}</p>`);
|
|
13
|
+
const tile = (value, label) => `<div class="tile"><b>${esc(value)}</b><span>${esc(label)}</span></div>`;
|
|
14
|
+
const WIDTHS = ['30%', '70%'];
|
|
15
|
+
/** The problems of the specification, when it cannot be read: what follows may be incomplete. */
|
|
16
|
+
function problemsOf(wiki) {
|
|
17
|
+
if (!wiki.problems.length)
|
|
18
|
+
return '';
|
|
19
|
+
return `<div class="prob"><p><b>The specification cannot be read</b>, so what follows may be incomplete.</p>${list(wiki.problems.map(esc), 'code')}</div>`;
|
|
20
|
+
}
|
|
21
|
+
const count = (operations) => {
|
|
22
|
+
const cases = operations.flatMap((one) => one.cases);
|
|
23
|
+
return { clauses: operations.reduce((sum, one) => sum + one.clauses.length, 0), examples: cases.filter((one) => one.kind === 'example').length, counterexamples: cases.filter((one) => one.kind === 'counterexample').length };
|
|
24
|
+
};
|
|
25
|
+
function overviewPage(wiki) {
|
|
26
|
+
const all = count(wiki.operations);
|
|
27
|
+
const tiles = [tile(wiki.operations.length, 'operations'), tile(all.clauses, 'clauses'), tile(all.examples, 'examples'), tile(all.counterexamples, 'counterexamples')];
|
|
28
|
+
const areas = wiki.areas.map((area) => {
|
|
29
|
+
const of = count(area.operations);
|
|
30
|
+
return listed('home', idOf('area', area.name), { source: area.source, name: '', description: said(area.description) }, link(`#${idOf('area', area.name)}`, areaTitle(area.name)), [String(area.operations.length), String(of.clauses), String(of.examples), String(of.counterexamples)]);
|
|
31
|
+
});
|
|
32
|
+
const { system, descriptions } = wiki;
|
|
33
|
+
const how = 'What each operation of the system promises, area by area. Every name is a page or leads to one: follow the links. The results of the last run are under Testing.';
|
|
34
|
+
const body = `<h1>${esc(system.name ?? 'Specification')}</h1>
|
|
35
|
+
${introOf(descriptions.system, how)}
|
|
36
|
+
${problemsOf(wiki)}
|
|
37
|
+
<div class="tiles">${tiles.join('')}</div>
|
|
38
|
+
${citizens('Areas', areas, { columns: ['Area', '', 'Operations', 'Clauses', 'Examples', 'Counterexamples'], widths: ['14%', '46%', '10%', '10%', '10%', '10%'], intro: descriptions.areas ? `<div class="prose intro">${described(descriptions.areas)}</div>` : '' })}`;
|
|
39
|
+
return { id: 'home', kind: 'Specification', title: system.name ?? 'Specification', said: firstOf(descriptions.system) ?? how, source: 'system.yml', body };
|
|
40
|
+
}
|
|
41
|
+
/** The first paragraph of a description, as the search tells a page by it. */
|
|
42
|
+
const firstOf = (description) => description?.trim().split(/\n\s*\n/)[0]?.replace(/\s+/g, ' ');
|
|
43
|
+
/** A page that is the home of a file at the root: its description first, then how opspicy reads it, then what it lists. */
|
|
44
|
+
function homePage(wiki, home, id, title, how, sections) {
|
|
45
|
+
const description = wiki.descriptions[home];
|
|
46
|
+
const body = `<h1>${esc(title)}</h1>
|
|
47
|
+
${introOf(description, how)}
|
|
48
|
+
${sections}`;
|
|
49
|
+
return { id, kind: title, title, said: firstOf(description) ?? how, source: `${home}.yml`, body };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A row that lists a citizen at home elsewhere, on the page of that id: it says where its home is,
|
|
53
|
+
* leads there by its name, and shows its formaldef here too, so that reading it never depends on
|
|
54
|
+
* following the link.
|
|
55
|
+
*/
|
|
56
|
+
const listed = (page, home, shown, name, more) => ({ ...shown, id: `${page}-${home}`, home, name, more });
|
|
57
|
+
/** An operation, where a page lists it: its name leads to its own page. */
|
|
58
|
+
const operationListed = (page, one, more) => listed(page, idOf('op', one.name), { source: one.source, name: '', description: said(one.description) }, link(`#${idOf('op', one.name)}`, one.name), more);
|
|
59
|
+
/** A predicate or a function, as a citizen: its name with what it is given, its description, its formaldef in Elo. */
|
|
60
|
+
const definitionShown = (one, refs) => {
|
|
61
|
+
const id = idOf(one.kind, one.name);
|
|
62
|
+
return { id, source: one.source, name: mono(`${one.name}${one.parameters.length ? `(${one.parameters.join(', ')})` : ''}`), description: said(one.description), formaldef: elo(one.formaldef, refs, { self: id, given: one.parameters }) };
|
|
63
|
+
};
|
|
64
|
+
/** A condition, as a citizen: the sentence is its description, and its name is where it stands. */
|
|
65
|
+
function conditionShown(one, refs) {
|
|
66
|
+
const violated = one.violated ? `<br><span class="m s">Violated as ${link(`#${idOf('actor', one.violated.actor)}`, one.violated.actor)}: ${mono(`${one.violated.then.status}${one.violated.then.code ? ` ${one.violated.then.code}` : ''}`)}</span>` : '';
|
|
67
|
+
return { id: idOf('condition', one.name), source: one.source, name: mono(one.name), description: `<span class="sentence">${said(one.description)}</span>${violated}`, formaldef: elo(one.formaldef, refs) };
|
|
68
|
+
}
|
|
69
|
+
/** A type, as a citizen: the comment above it is its description, and its declaration its formaldef. */
|
|
70
|
+
const typeShown = (one, refs) => {
|
|
71
|
+
const id = idOf('type', one.name);
|
|
72
|
+
return { id, source: one.source, name: mono(one.name), description: said(one.description), formaldef: elo(`${one.name} = ${one.formaldef}`, refs, { self: id }) };
|
|
73
|
+
};
|
|
74
|
+
function areaPage(area, refs) {
|
|
75
|
+
const id = idOf('area', area.name);
|
|
76
|
+
const operations = area.operations.map((one) => operationListed(id, one, [mono(`${one.entry.method} ${one.entry.path}`), esc(one.observes ? 'Observation' : 'Operation'), link(`#${idOf('agent', one.agent)}`, one.agent)]));
|
|
77
|
+
const body = `${crumbs(link('#home', 'Specification'))}<h1>${esc(areaTitle(area.name))}</h1>
|
|
78
|
+
<p class="lead">${said(area.description)}</p>
|
|
79
|
+
<p class="sub">${esc(plural(area.operations.length, 'operation'))}, from ${mono(`${area.name}/`)}.</p>
|
|
80
|
+
${citizens('Operations', operations, { columns: ['Operation', '', 'Route', 'Kind', 'Performed by'], widths: ['20%', '40%', '22%', '9%', '9%'] })}
|
|
81
|
+
${citizens('Predicates', area.predicates.map((one) => definitionShown(one, refs)), { widths: WIDTHS })}
|
|
82
|
+
${citizens('Functions', area.functions.map((one) => definitionShown(one, refs)), { widths: WIDTHS })}
|
|
83
|
+
${citizens('Conditions', area.conditions.map((one) => conditionShown(one, refs)), { widths: WIDTHS })}
|
|
84
|
+
${citizens('Types', area.types.map((one) => typeShown(one, refs)), { widths: WIDTHS })}`;
|
|
85
|
+
return { id, kind: 'Area', title: areaTitle(area.name), source: area.source, said: area.description, area: area.name, quote: area.description, body };
|
|
86
|
+
}
|
|
87
|
+
const statesLinked = (names) => names.map((name) => link(`#${idOf('state', name)}`, name)).join(', ') || '<span class="m">none</span>';
|
|
88
|
+
const operationsLinked = (all) => all.map((one) => link(`#${idOf('op', one.name)}`, one.name)).join(', ') || '<span class="m">none</span>';
|
|
89
|
+
/** What an agent monitors, or controls: what the operations it performs do, in the order of the state. */
|
|
90
|
+
const reachOfAgent = (agent, wiki, of) => {
|
|
91
|
+
const all = new Set(performedBy(agent, wiki).flatMap((one) => one.reach[of]));
|
|
92
|
+
return wiki.state.map((one) => one.name).filter((name) => all.has(name));
|
|
93
|
+
};
|
|
94
|
+
/** What an agent performs: the operations that name it, and those that name an agent it also is. */
|
|
95
|
+
const performedBy = (agent, { agents, operations }) => operations.filter((one) => isA(agents, agent.name, one.agent));
|
|
96
|
+
/** An agent, on a page of its own: what it is, who of the actors is one, and what it performs, itself or as an agent it also is. */
|
|
97
|
+
function agentPage(agent, wiki) {
|
|
98
|
+
const actors = wiki.actors.filter((one) => isA(wiki.agents, one.is, agent.name)).map((one) => link(`#${idOf('actor', one.name)}`, one.name));
|
|
99
|
+
const id = idOf('agent', agent.name);
|
|
100
|
+
const as = (one) => (one.agent === agent.name ? '' : link(`#${idOf('agent', one.agent)}`, one.agent));
|
|
101
|
+
const performed = performedBy(agent, wiki).map((one) => operationListed(id, one, [link(`#${idOf('area', one.area)}`, areaTitle(one.area)), as(one)]));
|
|
102
|
+
const body = `${crumbs(link('#home', 'Specification'), link('#agents', 'Agents'))}<h1>${esc(agent.name)}</h1>
|
|
103
|
+
<p class="lead">${said(agent.description)}</p>
|
|
104
|
+
<dl class="meta"><dt>Is also</dt><dd>${agent.is ? link(`#${idOf('agent', agent.is)}`, agent.is) : '<span class="m">nothing else</span>'}</dd><dt>Actors</dt><dd>${actors.join(', ') || '<span class="m">none</span>'}</dd><dt>Monitors</dt><dd>${statesLinked(reachOfAgent(agent, wiki, 'monitors'))}</dd><dt>Controls</dt><dd>${statesLinked(reachOfAgent(agent, wiki, 'controls'))}</dd></dl>
|
|
105
|
+
${citizens('Performs', performed, { columns: ['Operation', '', 'Area', 'As'], widths: ['22%', '52%', '13%', '13%'] })}`;
|
|
106
|
+
return { id, kind: 'Agent', title: agent.name, nav: 'agents', source: agent.source, said: agent.description, quote: agent.description, body };
|
|
107
|
+
}
|
|
108
|
+
function agentsPage(wiki) {
|
|
109
|
+
const agents = wiki.agents.map((one) => listed('agents', idOf('agent', one.name), { source: one.source, name: '', description: said(one.description) }, link(`#${idOf('agent', one.name)}`, one.name), [one.is ? link(`#${idOf('agent', one.is)}`, one.is) : '', String(performedBy(one, wiki).length)]));
|
|
110
|
+
const how = 'Who performs the operations: each agent leads to a page of its own, with what it performs. Who of them calls in a case is under World.';
|
|
111
|
+
return homePage(wiki, 'agents', 'agents', 'Agents', how, citizens('Agents', agents, { columns: ['Agent', '', 'Is also', 'Operations'], widths: ['16%', '60%', '12%', '12%'] }));
|
|
112
|
+
}
|
|
113
|
+
/** A word or a type, where an index names it: its name leads to where it is at home, in its area, and its formaldef is here too. */
|
|
114
|
+
const indexed = (page, shown, one) => listed(page, shown.id ?? '', shown, `<a class="mono" href="#${esc(shown.id ?? '')}">${esc(one.name)}</a>`, [link(`#${idOf('area', one.area)}`, areaTitle(one.area))]);
|
|
115
|
+
const INDEX = { columns: ['', '', 'Area'], widths: ['28%', '58%', '14%'] };
|
|
116
|
+
function definitionsPage(wiki) {
|
|
117
|
+
const of = (pick) => wiki.areas.flatMap(pick);
|
|
118
|
+
const definitions = (all) => all.map((one) => indexed('definitions', definitionShown(one, wiki.refs), one));
|
|
119
|
+
const lead = 'An index of what the sentences are written with: the predicates, true or false of the state, the functions, which give something of it, and the conditions, which several operations say in the same words. Each is at home in its area: its name leads there.';
|
|
120
|
+
const body = `<h1>Formal definitions</h1>
|
|
121
|
+
<p class="lead">${esc(lead)}</p>
|
|
122
|
+
${citizens('Predicates', definitions(of((area) => area.predicates)), INDEX)}
|
|
123
|
+
${citizens('Functions', definitions(of((area) => area.functions)), INDEX)}
|
|
124
|
+
${citizens('Conditions', of((area) => area.conditions).map((one) => indexed('definitions', conditionShown(one, wiki.refs), one)), INDEX)}`;
|
|
125
|
+
return { id: 'definitions', kind: 'Formal definitions', title: 'Formal definitions', said: lead, body };
|
|
126
|
+
}
|
|
127
|
+
function typesPage(wiki) {
|
|
128
|
+
const types = wiki.areas.flatMap((area) => area.types).sort((a, b) => a.name.localeCompare(b.name));
|
|
129
|
+
const of = (kind) => types.filter((one) => (wiki.typeKinds.get(one.name) ?? 'data') === kind).map((one) => indexed('types', typeShown(one, wiki.refs), one));
|
|
130
|
+
const lead = 'An index of the data, as the areas declare it in their types.elo, each with what it means: what a state tells, what an operation exchanges, and other data. Each is at home in its area: its name leads there.';
|
|
131
|
+
const body = `<h1>Types</h1><p class="lead">${esc(lead)}</p>
|
|
132
|
+
${citizens('State types', of('state'), INDEX)}
|
|
133
|
+
${citizens('Message types', of('message'), INDEX)}
|
|
134
|
+
${citizens('Other data', of('data'), INDEX)}`;
|
|
135
|
+
return { id: 'types', kind: 'Types', title: 'Types', said: lead, body };
|
|
136
|
+
}
|
|
137
|
+
function statePage(wiki) {
|
|
138
|
+
const by = (name, of) => operationsLinked(wiki.operations.filter((one) => one.reach[of].includes(name)));
|
|
139
|
+
const state = wiki.state.map((one) => ({ id: idOf('state', one.name), source: one.source, name: mono(one.name), description: said(one.description), more: [mono(`GET ${one.path}`), one.type ? link(`#${idOf('type', one.type)}`, one.type) : '', by(one.name, 'monitors'), by(one.name, 'controls')] }));
|
|
140
|
+
const relations = wiki.relations.map((one) => ({ id: idOf('relation', one.name), source: one.source, name: mono(`db.${one.name}`), description: said(one.description), more: [mono(one.table), esc(one.key.join(', ')), esc(one.attributes.join(', '))] }));
|
|
141
|
+
const how = 'What is observed: by the routes that tell it, and behind them, in the database. Each state says the operations that must read it to meet their clauses (monitored by) and those that may change it (controlled by).';
|
|
142
|
+
return homePage(wiki, 'state', 'state', 'State', how, `${citizens('State', state, { columns: ['State', '', 'Route', 'Type', 'Monitored by', 'Controlled by'], widths: ['11%', '27%', '18%', '10%', '17%', '17%'] })}
|
|
143
|
+
${citizens('Relations', relations, { columns: ['Relation', '', 'Table', 'Key', 'Attributes'], widths: ['14%', '40%', '12%', '8%', '26%'] })}`);
|
|
144
|
+
}
|
|
145
|
+
function worldPage(wiki) {
|
|
146
|
+
const actors = wiki.actors.map((one) => ({ id: idOf('actor', one.name), source: one.source, name: mono(one.name), description: said(one.description), more: [link(`#${idOf('agent', one.is)}`, one.is), one.signsIn ? 'signs in' : '<span class="m">not signed in</span>'] }));
|
|
147
|
+
const how = 'What the runner knows: who calls in a case, each the agent they are, and what else the world holds. What an actor signs in with, and the values of the world, are not shown: some of them are secrets.';
|
|
148
|
+
return homePage(wiki, 'world', 'world', 'World', how, `${citizens('Actors', actors, { columns: ['Actor', '', 'Is', ''], widths: ['16%', '60%', '12%', '12%'] })}
|
|
149
|
+
<h2>What else it holds</h2>${list(wiki.worldKeys.map(mono)) || '<p class="sub">Nothing but its actors.</p>'}`);
|
|
150
|
+
}
|
|
151
|
+
function backgroundsPage(wiki) {
|
|
152
|
+
const backgrounds = wiki.backgrounds.map((one) => ({ id: idOf('background', one.name), source: one.source, name: mono(one.name), description: said(one.description) }));
|
|
153
|
+
return homePage(wiki, 'backgrounds', 'backgrounds', 'Backgrounds', 'Where a case starts: each the state the configuration puts the system in, by its name.', citizens('Backgrounds', backgrounds, { columns: ['Background', ''], widths: ['16%', '84%'] }));
|
|
154
|
+
}
|
|
155
|
+
/** The pages of the specification, in the order the navigation shows them. */
|
|
156
|
+
export function specPagesOf(wiki) {
|
|
157
|
+
const around = { types: new Map(wiki.areas.flatMap((area) => area.types).map((one) => [one.name, one])), conditions: wiki.areas.flatMap((area) => area.conditions), refs: wiki.refs };
|
|
158
|
+
return [overviewPage(wiki), agentsPage(wiki), statePage(wiki), ...wiki.areas.map((area) => areaPage(area, wiki.refs)), ...wiki.operations.map((one) => operationPage(one, around)), ...wiki.agents.map((one) => agentPage(one, wiki)), definitionsPage(wiki), typesPage(wiki), worldPage(wiki), backgroundsPage(wiki)];
|
|
159
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// The script of the explorer's page, in parts: site.ts reads them one after the other into one
|
|
2
|
+
// function, so nothing here is global, and a part may name what another declares. The page reads
|
|
3
|
+
// without it: it adds the search, the filter, the formaldef at one click, the notes, the theme
|
|
4
|
+
// and the keys.
|
|
5
|
+
//
|
|
6
|
+
// This part is what every other uses: finding elements, text made safe, what the browser keeps.
|
|
7
|
+
|
|
8
|
+
const $ = (selector, root = document) => root.querySelector(selector);
|
|
9
|
+
const $$ = (selector, root = document) => [...root.querySelectorAll(selector)];
|
|
10
|
+
|
|
11
|
+
const ENTITIES = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
12
|
+
/** Any value, as text of the page. */
|
|
13
|
+
const esc = (value) => String(value).replace(/[&<>"']/g, (character) => ENTITIES[character]);
|
|
14
|
+
/** What an element says, on one line. */
|
|
15
|
+
const txt = (element) => (element ? element.textContent.replace(/\s+/g, ' ').trim() : '');
|
|
16
|
+
|
|
17
|
+
/** Whether a value is a record: an object with names, and not a list. */
|
|
18
|
+
const isRecord = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
19
|
+
|
|
20
|
+
/** A value read back from the browser, when it is a text; nothing otherwise. What a store gives back was written by a page, and not always by this one. */
|
|
21
|
+
const asText = (value) => (typeof value === 'string' ? value : '');
|
|
22
|
+
|
|
23
|
+
/** A store of the browser, read and written as JSON. One that refuses (a private window, a store that is full) is not an error: the page forgets, and `set` says whether it kept. */
|
|
24
|
+
const keep = (area) => ({
|
|
25
|
+
get(key, fallback) {
|
|
26
|
+
try {
|
|
27
|
+
const value = area.getItem(key);
|
|
28
|
+
return value == null ? fallback : JSON.parse(value);
|
|
29
|
+
} catch {
|
|
30
|
+
return fallback;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
set(key, value) {
|
|
34
|
+
try {
|
|
35
|
+
area.setItem(key, JSON.stringify(value));
|
|
36
|
+
return true;
|
|
37
|
+
} catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function storeOf(name) {
|
|
44
|
+
try {
|
|
45
|
+
return keep(window[name]);
|
|
46
|
+
} catch {
|
|
47
|
+
return keep({
|
|
48
|
+
getItem: () => null,
|
|
49
|
+
setItem() {
|
|
50
|
+
throw new Error('this browser has no such store for this page');
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What outlives the tab: choices of how to read, never anything secret. */
|
|
57
|
+
const local = storeOf('localStorage');
|
|
58
|
+
|
|
59
|
+
/** Says something for a moment, at the foot of the window: a longer one for what must not be missed. */
|
|
60
|
+
function toast(message, moment = 1800) {
|
|
61
|
+
let said = $('#toast');
|
|
62
|
+
if (!said) {
|
|
63
|
+
said = document.createElement('div');
|
|
64
|
+
said.id = 'toast';
|
|
65
|
+
said.setAttribute('role', 'status');
|
|
66
|
+
document.body.append(said);
|
|
67
|
+
}
|
|
68
|
+
said.textContent = message;
|
|
69
|
+
said.hidden = false;
|
|
70
|
+
clearTimeout(toast.timer);
|
|
71
|
+
toast.timer = setTimeout(() => {
|
|
72
|
+
said.hidden = true;
|
|
73
|
+
}, moment);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Copies a text, and says so; where the browser refuses, says what to do instead. */
|
|
77
|
+
async function copy(text, said = 'Copied') {
|
|
78
|
+
try {
|
|
79
|
+
await navigator.clipboard.writeText(text);
|
|
80
|
+
toast(said);
|
|
81
|
+
} catch {
|
|
82
|
+
toast('Copy was refused: select the text and copy it');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** What an address names (`#type-Board`): a page, or a row of one. One that cannot be read (`#%`) names nothing, as one nothing has. */
|
|
87
|
+
const elementAt = (hash) => {
|
|
88
|
+
try {
|
|
89
|
+
const id = decodeURIComponent(hash.slice(1));
|
|
90
|
+
return id ? document.getElementById(id) : null;
|
|
91
|
+
} catch {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** What the address of the page names. */
|
|
97
|
+
const target = () => elementAt(location.hash);
|
|
98
|
+
|
|
99
|
+
/** The page shown: the one the address names or holds, the overview when it names none. */
|
|
100
|
+
const currentPage = () => {
|
|
101
|
+
const named = target();
|
|
102
|
+
return (named && named.closest('.page')) || $('#home');
|
|
103
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// "Call it yourself": on the page of an operation, a fold, closed, built the first time it opens.
|
|
2
|
+
// A form for the input, made by elo-form from the type of the input, beside the same input as
|
|
3
|
+
// JSON, each following the other; a case to start from; the call sent to the target of the
|
|
4
|
+
// connection, and the same call as a command to copy. What a call is made of is calling.js.
|
|
5
|
+
|
|
6
|
+
const CALLS = JSON.parse($('#calls').textContent);
|
|
7
|
+
|
|
8
|
+
let eloFormKit = null;
|
|
9
|
+
/** elo-form, read the first time a form is asked for: the page holds it as a text, and most readers never call anything. */
|
|
10
|
+
function formKit() {
|
|
11
|
+
if (!eloFormKit) eloFormKit = new Function(`${$('#elo-form').textContent}\nreturn EloForm;`)();
|
|
12
|
+
return eloFormKit;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The fold of an operation's page, added once, after what the page says of its input and its output. */
|
|
16
|
+
function addCall(page) {
|
|
17
|
+
if (!CALLS[page.id] || $(':scope > details.try', page)) return;
|
|
18
|
+
const fold = document.createElement('details');
|
|
19
|
+
fold.className = 'fold try';
|
|
20
|
+
fold.innerHTML = '<summary class="fold-title">Call it yourself</summary><div class="body"></div>';
|
|
21
|
+
$(':scope > details.fold', page).after(fold);
|
|
22
|
+
fold.addEventListener('toggle', () => {
|
|
23
|
+
if (!fold.open || fold.dataset.built) return;
|
|
24
|
+
fold.dataset.built = '1';
|
|
25
|
+
buildCall(page, $('.body', fold));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The cases of a page that send data: one whose input is an expression is known only to a run. */
|
|
30
|
+
const casesOf = (page) =>
|
|
31
|
+
$$('tr.citizen[data-request]', page)
|
|
32
|
+
.map((row) => ({ name: (byAnchor.get(row.id) || { n: row.id }).n, request: JSON.parse(row.dataset.request) }))
|
|
33
|
+
.filter((one) => 'input' in one.request);
|
|
34
|
+
|
|
35
|
+
function callHtml(call, cases) {
|
|
36
|
+
const from = cases.length ? `<select aria-label="Start from a case"><option value="">Start from a case…</option>${cases.map((one, at) => `<option value="${at}">${esc(one.name)}</option>`).join('')}</select>` : '';
|
|
37
|
+
return `<p class="conf"></p>
|
|
38
|
+
<div class="grid"><div class="fields"></div><label class="json">The input, as JSON<textarea spellcheck="false">{}</textarea></label></div>
|
|
39
|
+
<div class="bar">${from}<span class="actor"></span><button type="button" class="tb primary" data-send>Send ${esc(call.method)}</button><button type="button" class="tb" data-curl>Copy as curl</button><span class="err s" role="alert"></span></div>
|
|
40
|
+
<div class="result" aria-live="polite"></div>`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The form of an input, by elo-form; none for an operation that is given nothing, or for a type it cannot show. */
|
|
44
|
+
function formOf(call, fields, onChange) {
|
|
45
|
+
if (!call.input) {
|
|
46
|
+
fields.innerHTML = '<p class="m s">It is given nothing.</p>';
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const form = formKit().eloForm(call.input.source, { type: call.input.type, onChange });
|
|
51
|
+
fields.append(form.element);
|
|
52
|
+
return form;
|
|
53
|
+
} catch {
|
|
54
|
+
fields.innerHTML = '<p class="m s">This input cannot be shown as a form: write it as JSON.</p>';
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** What the JSON side says, as `{ value }`; nothing when it does not parse. */
|
|
60
|
+
function saidAsJson(panel) {
|
|
61
|
+
try {
|
|
62
|
+
return { value: JSON.parse(panel.json.value || 'null') };
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Whatever was about to be sent is not any more: the input, or where it goes, changed. */
|
|
69
|
+
function disarm(panel) {
|
|
70
|
+
panel.armed = false;
|
|
71
|
+
panel.button.textContent = `Send ${panel.call.method}`;
|
|
72
|
+
panel.problem.textContent = '';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The form changed: the JSON follows it, unless it already says the same. A form with a secret in it hides the JSON for good. */
|
|
76
|
+
function followForm(panel, form) {
|
|
77
|
+
const raw = form.getRaw();
|
|
78
|
+
const now = saidAsJson(panel);
|
|
79
|
+
if (!now || JSON.stringify(now.value) !== JSON.stringify(raw)) panel.json.value = JSON.stringify(raw === null ? {} : raw, null, 2);
|
|
80
|
+
if ($('input[type=password]', form.element)) panel.side.hidden = true;
|
|
81
|
+
disarm(panel);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The JSON changed: the form follows it, when it parses. */
|
|
85
|
+
function followJson(panel) {
|
|
86
|
+
disarm(panel);
|
|
87
|
+
const now = saidAsJson(panel);
|
|
88
|
+
if (!now) panel.problem.textContent = 'The JSON does not parse.';
|
|
89
|
+
else if (panel.form) panel.form.setValue(now.value);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The input as it is now, as `{ value }`: what the form holds when the JSON is not shown, what the JSON says otherwise; nothing when it does not parse. */
|
|
93
|
+
function inputOf(panel) {
|
|
94
|
+
if (!panel.call.input) return { value: null };
|
|
95
|
+
if (panel.side.hidden && panel.form) return { value: panel.form.getRaw() };
|
|
96
|
+
const now = saidAsJson(panel);
|
|
97
|
+
if (!now) panel.problem.textContent = 'The JSON does not parse: nothing is sent.';
|
|
98
|
+
return now;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Fills the form and the JSON with what a case sends. */
|
|
102
|
+
function startFrom(panel, kase) {
|
|
103
|
+
panel.json.value = JSON.stringify(kase.request.input, null, 2);
|
|
104
|
+
if (panel.form) panel.form.setValue(kase.request.input);
|
|
105
|
+
disarm(panel);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Copies the call as a command, unless it is no call at all: the reason is then said, as it is to who sends. */
|
|
109
|
+
function copyCurl(panel) {
|
|
110
|
+
const now = inputOf(panel);
|
|
111
|
+
if (!now) return;
|
|
112
|
+
const problem = whyNoCall(requestOf(panel.call, now.value));
|
|
113
|
+
panel.problem.textContent = problem;
|
|
114
|
+
if (problem) return;
|
|
115
|
+
const { target, token, headers } = connection();
|
|
116
|
+
copy(curlOf(panel.call, now.value, { target, token: Boolean(token), headers: Object.keys(headers) }), 'Copied, with a variable in place of what is secret');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Sends the call, unless something keeps it: a part of the route not given, no target, or data changed on another machine, which is asked twice. */
|
|
120
|
+
function trySend(panel) {
|
|
121
|
+
const now = inputOf(panel);
|
|
122
|
+
if (!now) return;
|
|
123
|
+
const request = requestOf(panel.call, now.value);
|
|
124
|
+
const target = callTarget();
|
|
125
|
+
const problem = whyNotSent(request, target);
|
|
126
|
+
if (problem) panel.problem.textContent = problem;
|
|
127
|
+
else if (asksTwice(panel.call.method, target) && !panel.armed) {
|
|
128
|
+
panel.armed = true;
|
|
129
|
+
panel.button.textContent = `It changes data on ${new URL(target).host}: send anyway`;
|
|
130
|
+
} else {
|
|
131
|
+
disarm(panel);
|
|
132
|
+
const result = $('.result', panel.body);
|
|
133
|
+
if (throughServer()) sendThrough(panel, now.value, result);
|
|
134
|
+
else sendCall(request, result);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The panel of a call, built in the fold of an operation's page. */
|
|
139
|
+
function buildCall(page, body) {
|
|
140
|
+
const call = CALLS[page.id];
|
|
141
|
+
const cases = casesOf(page);
|
|
142
|
+
body.innerHTML = callHtml(call, cases);
|
|
143
|
+
const panel = { call, page: page.id, body, side: $('.json', body), json: $('.json textarea', body), problem: $('.err', body), button: $('[data-send]', body), armed: false, form: null };
|
|
144
|
+
panel.form = formOf(call, $('.fields', body), (form) => followForm(panel, form));
|
|
145
|
+
panel.side.hidden = !call.input;
|
|
146
|
+
if (panel.form) followForm(panel, panel.form);
|
|
147
|
+
const say = () => {
|
|
148
|
+
$('.conf', body).innerHTML = connectionLine(call);
|
|
149
|
+
$('.actor', body).innerHTML = actorHtml(call);
|
|
150
|
+
disarm(panel);
|
|
151
|
+
};
|
|
152
|
+
say();
|
|
153
|
+
document.addEventListener('opspicy:connection', say);
|
|
154
|
+
document.addEventListener('opspicy:served', say);
|
|
155
|
+
panel.json.addEventListener('input', () => followJson(panel));
|
|
156
|
+
body.addEventListener('change', (event) => {
|
|
157
|
+
if (event.target.matches('.bar select:not([data-actor])') && event.target.value !== '') startFrom(panel, cases[event.target.value]);
|
|
158
|
+
});
|
|
159
|
+
$('[data-curl]', body).addEventListener('click', () => copyCurl(panel));
|
|
160
|
+
panel.button.addEventListener('click', () => trySend(panel));
|
|
161
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// How a call is made of an input: the parts of the route it fills, what is left to send, and
|
|
2
|
+
// what keeps it from going. Pure: nothing of the page is read, so the same text is held by a
|
|
3
|
+
// table of cases outside a browser, where it is also checked to address a call as the runner
|
|
4
|
+
// does (notation.ts). The same call as a command to copy is copying.js.
|
|
5
|
+
|
|
6
|
+
/** A host that is this machine: a call that changes data anywhere else asks twice. */
|
|
7
|
+
const isLocal = (host) => /^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(host) || host.endsWith('.localhost');
|
|
8
|
+
|
|
9
|
+
/** What a part of a route cannot be: the address would then lead elsewhere, since nothing encodes a dot (`/api/nothing/../boards` is `/api/boards`). */
|
|
10
|
+
const LEADS_ELSEWHERE = ['.', '..'];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The path with its parts filled from the input, and what is left of the input. A part is one
|
|
14
|
+
* the input itself gives, by its own name (`Object.hasOwn`): one it does not give is `missing`,
|
|
15
|
+
* one it gives as `.` or `..` is `refused`, and both stay as written. An input that is no record
|
|
16
|
+
* (a list, a text, a number) fills no part, and is left whole.
|
|
17
|
+
*/
|
|
18
|
+
function addressedBy(path, input) {
|
|
19
|
+
const given = isRecord(input) ? { ...input } : {};
|
|
20
|
+
const missing = [];
|
|
21
|
+
const refused = [];
|
|
22
|
+
const filled = path.replace(/:(\w+)/g, (all, name) => {
|
|
23
|
+
const part = Object.hasOwn(given, name) ? given[name] : null;
|
|
24
|
+
const told = part === undefined || part === null ? missing : LEADS_ELSEWHERE.includes(String(part)) ? refused : null;
|
|
25
|
+
if (told) return told.push(name) && all;
|
|
26
|
+
delete given[name];
|
|
27
|
+
return encodeURIComponent(String(part));
|
|
28
|
+
});
|
|
29
|
+
const rest = input === null || input === undefined ? null : isRecord(input) ? given : input;
|
|
30
|
+
return { path: filled, rest, missing, refused };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What is left of the input as a query, for a route that is only read: a name and a value each. What is no record has none, and says nothing there. */
|
|
34
|
+
function queryOf(rest) {
|
|
35
|
+
const pairs = Object.entries(isRecord(rest) ? rest : {}).filter(([, value]) => value !== undefined && value !== null);
|
|
36
|
+
return pairs.length ? `?${pairs.map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(String(value))}`).join('&')}` : '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What a call sends: the rest of the input is the query of a GET, and the body, as JSON, of any other; nothing left is an empty record there. */
|
|
40
|
+
function requestOf(call, input) {
|
|
41
|
+
const { path, rest, missing, refused } = addressedBy(call.path, input);
|
|
42
|
+
if (call.method === 'GET') return { method: 'GET', path: `${path}${queryOf(rest)}`, missing, refused };
|
|
43
|
+
return { method: call.method, path, body: rest === null ? {} : rest, missing, refused };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Whether a call has a body: one that is `0`, `false` or an empty text is one. */
|
|
47
|
+
const hasBody = (request) => request.body !== undefined;
|
|
48
|
+
|
|
49
|
+
/** The headers of a connection, from what was typed: a line each, `Name: value`. Lines that are not are told. */
|
|
50
|
+
function headersOf(text) {
|
|
51
|
+
const headers = {};
|
|
52
|
+
const problems = [];
|
|
53
|
+
for (const line of text.split('\n').map((one) => one.trim()).filter(Boolean)) {
|
|
54
|
+
const [, name, value] = line.match(/^([A-Za-z0-9!#$%&'*+.^_`|~-]+)\s*:\s*(.*)$/) || [];
|
|
55
|
+
if (name) headers[name] = value;
|
|
56
|
+
else problems.push(line);
|
|
57
|
+
}
|
|
58
|
+
return { headers, problems };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The headers read back from the browser: a value by its name, each a text; what is not is left out. */
|
|
62
|
+
const headersKept = (value) => Object.fromEntries(Object.entries(isRecord(value) ? value : {}).filter(([, one]) => typeof one === 'string'));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* What a call carries to a target, of what the tab keeps: the token and the headers, when they
|
|
66
|
+
* were saved for that target; nothing of them when they were saved for another, which `withheld`
|
|
67
|
+
* then says. The target is kept where another page of the same origin can rewrite it: what is
|
|
68
|
+
* secret goes where its reader sent it, and nowhere else.
|
|
69
|
+
*/
|
|
70
|
+
function carriedTo(target, kept) {
|
|
71
|
+
const token = asText(kept.token);
|
|
72
|
+
const headers = headersKept(kept.headers);
|
|
73
|
+
if (kept.target === target) return { token, headers, withheld: false };
|
|
74
|
+
return { token: '', headers: {}, withheld: Boolean(token) || Object.keys(headers).length > 0 };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Whether a call asks twice before it is sent: it changes data (it is not a GET) on a host that is not this machine. */
|
|
78
|
+
function asksTwice(method, target) {
|
|
79
|
+
try {
|
|
80
|
+
return method !== 'GET' && !isLocal(new URL(target).hostname);
|
|
81
|
+
} catch {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** What keeps a call from being a call, wherever it would go, as a sentence: a part of the route given as `.` or `..`, which the runner refuses too; nothing otherwise. */
|
|
87
|
+
const whyNoCall = (request) => (request.refused.length ? `The route cannot take "." or ".." for ${request.refused.join(', ')}: the call would go elsewhere.` : '');
|
|
88
|
+
|
|
89
|
+
/** What keeps a call from being sent, as a sentence: a part of the route not given or refused, no target, a target that is none; nothing when it may go. */
|
|
90
|
+
function whyNotSent(request, target) {
|
|
91
|
+
if (request.missing.length) return `The route takes ${request.missing.join(', ')} from the input: give it.`;
|
|
92
|
+
if (whyNoCall(request)) return whyNoCall(request);
|
|
93
|
+
return target ? whyNoTarget(target) : 'Say where calls go first: Connection.';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Why a text is no target, as a sentence: it is no address, or one a page cannot call (`localhost:3000` is an address whose scheme is `localhost:`); nothing when it is one. */
|
|
97
|
+
function whyNoTarget(text) {
|
|
98
|
+
try {
|
|
99
|
+
return ['http:', 'https:'].includes(new URL(text).protocol) ? '' : 'A target begins with http:// or https://.';
|
|
100
|
+
} catch {
|
|
101
|
+
return 'The target is not an address.';
|
|
102
|
+
}
|
|
103
|
+
}
|