@enspirit/opspicy 0.0.0-stage → 0.2.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 +281 -2
- package/bin/entry.js +27 -0
- package/bin/opspicy +19 -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 +109 -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 +59 -0
- package/dist/explorer/index.js +61 -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 +94 -0
- package/dist/explorer/pages.js +132 -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 +77 -0
- package/dist/explorer/style.css +241 -0
- package/dist/explorer/telling.js +30 -0
- package/dist/explorer/tests.js +78 -0
- package/dist/explorer/wiki.js +76 -0
- package/dist/files.js +176 -0
- package/dist/finding.js +17 -0
- package/dist/judging.js +75 -0
- package/dist/lines.js +30 -0
- package/dist/main.js +128 -0
- package/dist/meaning.js +100 -0
- package/dist/modelling.js +115 -0
- package/dist/naming.js +84 -0
- package/dist/notation.js +119 -0
- package/dist/planning.js +32 -0
- package/dist/reading.js +120 -0
- package/dist/report-file.js +42 -0
- package/dist/report.js +62 -0
- package/dist/running.js +102 -0
- package/dist/selecting.js +71 -0
- package/dist/starting.js +41 -0
- package/dist/suite.js +29 -0
- package/dist/words.js +103 -0
- package/dist/world.js +128 -0
- package/package.json +57 -4
|
@@ -0,0 +1,132 @@
|
|
|
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, 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
|
+
const tile = (value, label) => `<div class="tile"><b>${esc(value)}</b><span>${esc(label)}</span></div>`;
|
|
9
|
+
const WIDTHS = ['30%', '70%'];
|
|
10
|
+
/** The problems of the specification, when it cannot be read: what follows may be incomplete. */
|
|
11
|
+
function problemsOf(wiki) {
|
|
12
|
+
if (!wiki.problems.length)
|
|
13
|
+
return '';
|
|
14
|
+
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>`;
|
|
15
|
+
}
|
|
16
|
+
const count = (operations) => {
|
|
17
|
+
const cases = operations.flatMap((one) => one.cases);
|
|
18
|
+
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 };
|
|
19
|
+
};
|
|
20
|
+
function overviewPage(wiki) {
|
|
21
|
+
const all = count(wiki.operations);
|
|
22
|
+
const tiles = [tile(wiki.operations.length, 'operations'), tile(all.clauses, 'clauses'), tile(all.examples, 'examples'), tile(all.counterexamples, 'counterexamples')];
|
|
23
|
+
const areas = wiki.areas.map((area) => {
|
|
24
|
+
const of = count(area.operations);
|
|
25
|
+
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)]);
|
|
26
|
+
});
|
|
27
|
+
const lead = '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 Tests.';
|
|
28
|
+
const body = `<h1>Specification</h1>
|
|
29
|
+
<p class="lead">${lead}</p>
|
|
30
|
+
${problemsOf(wiki)}
|
|
31
|
+
<div class="tiles">${tiles.join('')}</div>
|
|
32
|
+
${citizens('Areas', areas, { columns: ['Area', '', 'Operations', 'Clauses', 'Examples', 'Counterexamples'], widths: ['14%', '46%', '10%', '10%', '10%', '10%'] })}`;
|
|
33
|
+
return { id: 'home', kind: 'Specification', title: 'Specification', said: lead, body };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A row that lists a citizen at home elsewhere, on the page of that id: it says where its home is,
|
|
37
|
+
* leads there by its name, and shows its formaldef here too, so that reading it never depends on
|
|
38
|
+
* following the link.
|
|
39
|
+
*/
|
|
40
|
+
const listed = (page, home, shown, name, more) => ({ ...shown, id: `${page}-${home}`, home, name, more });
|
|
41
|
+
/** An operation, where a page lists it: its name leads to its own page. */
|
|
42
|
+
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);
|
|
43
|
+
/** A predicate or a function, as a citizen: its name with what it is given, its description, its formaldef in Elo. */
|
|
44
|
+
const definitionShown = (one, refs) => {
|
|
45
|
+
const id = idOf(one.kind, one.name);
|
|
46
|
+
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 }) };
|
|
47
|
+
};
|
|
48
|
+
/** A condition, as a citizen: the sentence is its description, and its name is where it stands. */
|
|
49
|
+
function conditionShown(one, refs) {
|
|
50
|
+
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>` : '';
|
|
51
|
+
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) };
|
|
52
|
+
}
|
|
53
|
+
/** A type, as a citizen: the comment above it is its description, and its declaration its formaldef. */
|
|
54
|
+
const typeShown = (one, refs) => {
|
|
55
|
+
const id = idOf('type', one.name);
|
|
56
|
+
return { id, source: one.source, name: mono(one.name), description: said(one.description), formaldef: elo(`${one.name} = ${one.formaldef}`, refs, { self: id }) };
|
|
57
|
+
};
|
|
58
|
+
function areaPage(area, refs) {
|
|
59
|
+
const id = idOf('area', area.name);
|
|
60
|
+
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)]));
|
|
61
|
+
const body = `${crumbs(link('#home', 'Specification'))}<h1>${esc(areaTitle(area.name))}</h1>
|
|
62
|
+
<p class="lead">${said(area.description)}</p>
|
|
63
|
+
<p class="sub">${esc(plural(area.operations.length, 'operation'))}, from ${mono(`${area.name}/`)}.</p>
|
|
64
|
+
${citizens('Operations', operations, { columns: ['Operation', '', 'Route', 'Kind', 'Performed by'], widths: ['20%', '40%', '22%', '9%', '9%'] })}
|
|
65
|
+
${citizens('Predicates', area.predicates.map((one) => definitionShown(one, refs)), { widths: WIDTHS })}
|
|
66
|
+
${citizens('Functions', area.functions.map((one) => definitionShown(one, refs)), { widths: WIDTHS })}
|
|
67
|
+
${citizens('Conditions', area.conditions.map((one) => conditionShown(one, refs)), { widths: WIDTHS })}
|
|
68
|
+
${citizens('Types', area.types.map((one) => typeShown(one, refs)), { widths: WIDTHS })}`;
|
|
69
|
+
return { id, kind: 'Area', title: areaTitle(area.name), source: area.source, said: area.description, area: area.name, quote: area.description, body };
|
|
70
|
+
}
|
|
71
|
+
/** What an agent performs: the operations that name it, and those that name an agent it also is. */
|
|
72
|
+
const performedBy = (agent, { agents, operations }) => operations.filter((one) => isA(agents, agent.name, one.agent));
|
|
73
|
+
/** 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. */
|
|
74
|
+
function agentPage(agent, wiki) {
|
|
75
|
+
const actors = wiki.actors.filter((one) => isA(wiki.agents, one.is, agent.name)).map((one) => link(`#${idOf('actor', one.name)}`, one.name));
|
|
76
|
+
const id = idOf('agent', agent.name);
|
|
77
|
+
const as = (one) => (one.agent === agent.name ? '' : link(`#${idOf('agent', one.agent)}`, one.agent));
|
|
78
|
+
const performed = performedBy(agent, wiki).map((one) => operationListed(id, one, [link(`#${idOf('area', one.area)}`, areaTitle(one.area)), as(one)]));
|
|
79
|
+
const body = `${crumbs(link('#home', 'Specification'), link('#agents', 'Agents'))}<h1>${esc(agent.name)}</h1>
|
|
80
|
+
<p class="lead">${said(agent.description)}</p>
|
|
81
|
+
<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></dl>
|
|
82
|
+
${citizens('Performs', performed, { columns: ['Operation', '', 'Area', 'As'], widths: ['22%', '52%', '13%', '13%'] })}`;
|
|
83
|
+
return { id, kind: 'Agent', title: agent.name, nav: 'agents', source: agent.source, said: agent.description, quote: agent.description, body };
|
|
84
|
+
}
|
|
85
|
+
function agentsPage(wiki) {
|
|
86
|
+
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)]));
|
|
87
|
+
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>'] }));
|
|
88
|
+
const lead = 'Who performs the operations, and who of the Backgrounds calls in a case. What an actor signs in with is not shown.';
|
|
89
|
+
const body = `<h1>Agents and actors</h1>
|
|
90
|
+
<p class="lead">${lead}</p>
|
|
91
|
+
${citizens('Agents', agents, { columns: ['Agent', '', 'Is also', 'Operations'], widths: ['16%', '60%', '12%', '12%'] })}
|
|
92
|
+
${citizens('Actors', actors, { columns: ['Actor', '', 'Is', ''], widths: ['16%', '60%', '12%', '12%'] })}`;
|
|
93
|
+
return { id: 'agents', kind: 'Agents', title: 'Agents and actors', said: lead, body };
|
|
94
|
+
}
|
|
95
|
+
/** 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. */
|
|
96
|
+
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))]);
|
|
97
|
+
const INDEX = { columns: ['', '', 'Area'], widths: ['28%', '58%', '14%'] };
|
|
98
|
+
function wordsPage(wiki) {
|
|
99
|
+
const of = (pick) => wiki.areas.flatMap(pick);
|
|
100
|
+
const definitions = (all) => all.map((one) => indexed('words', definitionShown(one, wiki.refs), one));
|
|
101
|
+
const lead = '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.';
|
|
102
|
+
const body = `<h1>Words</h1>
|
|
103
|
+
<p class="lead">${lead}</p>
|
|
104
|
+
${citizens('Predicates', definitions(of((area) => area.predicates)), INDEX)}
|
|
105
|
+
${citizens('Functions', definitions(of((area) => area.functions)), INDEX)}
|
|
106
|
+
${citizens('Conditions', of((area) => area.conditions).map((one) => indexed('words', conditionShown(one, wiki.refs), one)), INDEX)}`;
|
|
107
|
+
return { id: 'words', kind: 'Words', title: 'Words', said: lead, body };
|
|
108
|
+
}
|
|
109
|
+
function typesPage(wiki) {
|
|
110
|
+
const types = wiki.areas.flatMap((area) => area.types).sort((a, b) => a.name.localeCompare(b.name));
|
|
111
|
+
const body = `<h1>Types</h1><p class="lead">The data exchanged, as the areas declare it in their ${mono('types.elo')}. Each is at home in its area: its name leads there.</p>
|
|
112
|
+
${citizens('Types', types.map((one) => indexed('types', typeShown(one, wiki.refs), one)), INDEX)}`;
|
|
113
|
+
return { id: 'types', kind: 'Types', title: 'Types', said: 'The data exchanged, as the areas declare it in their types.elo.', body };
|
|
114
|
+
}
|
|
115
|
+
function statePage(wiki) {
|
|
116
|
+
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) : ''] }));
|
|
117
|
+
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(', '))] }));
|
|
118
|
+
const backgrounds = wiki.backgrounds.map((one) => ({ id: idOf('background', one.name), source: one.source, name: mono(one.name), description: said(one.description) }));
|
|
119
|
+
const lead = 'What is observed, by the routes that tell it and behind them; where a case starts; and what the runner knows. Values of the world are not shown: some of them are secrets.';
|
|
120
|
+
const body = `<h1>State and world</h1>
|
|
121
|
+
<p class="lead">${lead}</p>
|
|
122
|
+
${citizens('State', state, { columns: ['State', '', 'Route', 'Type'], widths: ['12%', '48%', '26%', '14%'] })}
|
|
123
|
+
${citizens('Relations', relations, { columns: ['Relation', '', 'Table', 'Key', 'Attributes'], widths: ['14%', '40%', '12%', '8%', '26%'] })}
|
|
124
|
+
${citizens('Backgrounds', backgrounds, { columns: ['Background', ''], widths: ['16%', '84%'] })}
|
|
125
|
+
<h2>World</h2>${list(wiki.worldKeys.map(mono)) || '<p class="sub">Nothing but its actors.</p>'}`;
|
|
126
|
+
return { id: 'state', kind: 'State', title: 'State and world', said: lead, body };
|
|
127
|
+
}
|
|
128
|
+
/** The pages of the specification, in the order the navigation shows them. */
|
|
129
|
+
export function specPagesOf(wiki) {
|
|
130
|
+
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 };
|
|
131
|
+
return [overviewPage(wiki), ...wiki.areas.map((area) => areaPage(area, wiki.refs)), ...wiki.operations.map((one) => operationPage(one, around)), agentsPage(wiki), ...wiki.agents.map((one) => agentPage(one, wiki)), wordsPage(wiki), typesPage(wiki), statePage(wiki)];
|
|
132
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// The connection: where a call made from this page goes, and what it carries. Asked in a dialog
|
|
2
|
+
// opened from the sidebar, which says the host calls go to.
|
|
3
|
+
//
|
|
4
|
+
// The target is kept in this browser, apart for each specification. The token and the headers
|
|
5
|
+
// are kept until the tab closes and no longer (`sessionStorage`), and are written nowhere else:
|
|
6
|
+
// not in a note, not in the review, not in a command that is copied. The target they were saved
|
|
7
|
+
// for is kept beside them: they are sent to that one only, since the target itself is kept where
|
|
8
|
+
// another page of the same origin can change it (every file opened in Chrome is one origin).
|
|
9
|
+
|
|
10
|
+
const TARGET = `opspicy.target:${META.name}`;
|
|
11
|
+
const TOKEN = `opspicy.token:${META.name}`;
|
|
12
|
+
const HEADERS = `opspicy.headers:${META.name}`;
|
|
13
|
+
const SAVED_FOR = `opspicy.for:${META.name}`;
|
|
14
|
+
const session = storeOf('sessionStorage');
|
|
15
|
+
const dialog = $('#conf');
|
|
16
|
+
|
|
17
|
+
/** A target as it is kept and compared: with no slash at its end. */
|
|
18
|
+
const targetOf = (text) => asText(text).trim().replace(/\/+$/, '');
|
|
19
|
+
|
|
20
|
+
/** Where calls go, and what they carry there: a target, a token, headers by name, and whether some were saved for another target and are `withheld`. */
|
|
21
|
+
function connection() {
|
|
22
|
+
const target = targetOf(local.get(TARGET, ''));
|
|
23
|
+
return { target, ...carriedTo(target, { target: session.get(SAVED_FOR, null), token: session.get(TOKEN, ''), headers: session.get(HEADERS, {}) }) };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The host of an address, with its port; nothing of what is not one. */
|
|
27
|
+
const hostOf = (url) => {
|
|
28
|
+
try {
|
|
29
|
+
return new URL(url).host;
|
|
30
|
+
} catch {
|
|
31
|
+
return '';
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** The sidebar says the host calls go to: the Connection's target, or else the one the server of the page runs against; and whoever shows the connection is told it changed. */
|
|
36
|
+
function sayConnection() {
|
|
37
|
+
$('#conf-state').textContent = hostOf(connection().target) || servedHost();
|
|
38
|
+
document.dispatchEvent(new Event('opspicy:connection'));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function openConnection() {
|
|
42
|
+
const { target, token, headers } = connection();
|
|
43
|
+
$('#c-target').value = target;
|
|
44
|
+
$('#c-token').value = token;
|
|
45
|
+
$('#c-headers').value = Object.entries(headers).map(([name, value]) => `${name}: ${value}`).join('\n');
|
|
46
|
+
$('#c-problem').textContent = '';
|
|
47
|
+
dialog.showModal();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Keeps what the dialog says, unless a line of its headers is none or its target is: then it says
|
|
52
|
+
* which, and stays open. Save is the one button that submits, so Enter in a field saves; Cancel and Esc close and keep nothing.
|
|
53
|
+
*/
|
|
54
|
+
function saveConnection(event) {
|
|
55
|
+
const { headers, problems } = headersOf($('#c-headers').value);
|
|
56
|
+
const target = $('#c-target').value.trim();
|
|
57
|
+
const problem = problems.length ? `Not a header, which is written "Name: value": ${problems[0]}` : target ? whyNoTarget(target) : '';
|
|
58
|
+
if (problem) {
|
|
59
|
+
event.preventDefault();
|
|
60
|
+
$('#c-problem').textContent = problem;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
local.set(TARGET, target);
|
|
64
|
+
session.set(SAVED_FOR, targetOf(target));
|
|
65
|
+
session.set(TOKEN, $('#c-token').value);
|
|
66
|
+
session.set(HEADERS, headers);
|
|
67
|
+
sayConnection();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
$('form', dialog).addEventListener('submit', saveConnection);
|
|
71
|
+
|
|
72
|
+
document.addEventListener('click', (event) => {
|
|
73
|
+
if (event.target.closest('button[data-conf]')) openConnection();
|
|
74
|
+
else if (event.target.closest('#conf button[value=cancel]')) dialog.close();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
sayConnection();
|
|
78
|
+
document.addEventListener('opspicy:served', sayConnection);
|
|
79
|
+
|
|
80
|
+
// Another page changed what this browser keeps: the connection is said again, so that nothing here names a target calls no longer go to.
|
|
81
|
+
addEventListener('storage', (event) => {
|
|
82
|
+
if (event.key === null || event.key === TARGET) sayConnection();
|
|
83
|
+
});
|