@basaltkit/deploy-ui 0.0.0-stage → 0.1.1
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 +167 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +3 -0
- package/dist/cli.d.ts +34 -0
- package/dist/cli.js +124 -0
- package/dist/client/api.js +203 -0
- package/dist/client/app.js +252 -0
- package/dist/client/dialog.js +48 -0
- package/dist/client/dom.js +83 -0
- package/dist/client/icons.js +47 -0
- package/dist/client/main.js +81 -0
- package/dist/client/messages.d.ts +58 -0
- package/dist/client/messages.js +1 -0
- package/dist/client/reply.js +41 -0
- package/dist/client/sanitize.js +16 -0
- package/dist/client/view-history.js +57 -0
- package/dist/client/view-plan.js +171 -0
- package/dist/client/view-run.js +338 -0
- package/dist/client/view-status.js +60 -0
- package/dist/client/view-targets.js +136 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +14 -0
- package/dist/open.d.ts +13 -0
- package/dist/open.js +31 -0
- package/dist/page.d.ts +7 -0
- package/dist/page.js +470 -0
- package/dist/remote.d.ts +68 -0
- package/dist/remote.js +220 -0
- package/dist/security.d.ts +38 -0
- package/dist/security.js +64 -0
- package/dist/server.d.ts +39 -0
- package/dist/server.js +513 -0
- package/dist/session.d.ts +66 -0
- package/dist/session.js +123 -0
- package/dist/ssh.d.ts +71 -0
- package/dist/ssh.js +186 -0
- package/dist/stream.d.ts +70 -0
- package/dist/stream.js +155 -0
- package/dist/targets.d.ts +80 -0
- package/dist/targets.js +242 -0
- package/package.json +51 -3
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { ApiError } from './api.js';
|
|
2
|
+
import { confirmDialog } from './dialog.js';
|
|
3
|
+
import { button, el, emptyState, quiet, replace } from './dom.js';
|
|
4
|
+
import { icon } from './icons.js';
|
|
5
|
+
import { describe } from './reply.js';
|
|
6
|
+
import { renderHistory } from './view-history.js';
|
|
7
|
+
import { renderPlan } from './view-plan.js';
|
|
8
|
+
import { RunView } from './view-run.js';
|
|
9
|
+
import { renderStatus } from './view-status.js';
|
|
10
|
+
import { renderTargets } from './view-targets.js';
|
|
11
|
+
/** This tab's session (sessionStorage): the bearer token, and the key the server proves itself with. */
|
|
12
|
+
export const TOKEN_KEY = 'basalt-deploy-ui:token';
|
|
13
|
+
export const SERVER_KEY = 'basalt-deploy-ui:server-key';
|
|
14
|
+
/** Forgets this tab's session: after a stop it is worth nothing, and nothing should look like it still works. */
|
|
15
|
+
export function forgetSession(storage) {
|
|
16
|
+
try {
|
|
17
|
+
storage?.removeItem(TOKEN_KEY);
|
|
18
|
+
storage?.removeItem(SERVER_KEY);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// No storage: nothing kept.
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const SCREENS = [['status', 'Status', 'status'], ['plan', 'Plan & deploy', 'plan'], ['history', 'History', 'history'], ['run', 'Run', 'run'], ['targets', 'Targets', 'targets']];
|
|
25
|
+
/** Per-target state the screens render; every server string in it is shown as text only. */
|
|
26
|
+
export class App {
|
|
27
|
+
api;
|
|
28
|
+
shell;
|
|
29
|
+
storage;
|
|
30
|
+
targets = { path: '', exists: false, targets: [] };
|
|
31
|
+
current;
|
|
32
|
+
hello;
|
|
33
|
+
statusReply;
|
|
34
|
+
planReply;
|
|
35
|
+
run;
|
|
36
|
+
screen = 'targets';
|
|
37
|
+
statusBusy = false;
|
|
38
|
+
planBusy = false;
|
|
39
|
+
stopped = false;
|
|
40
|
+
constructor(api, shell, storage) {
|
|
41
|
+
this.api = api;
|
|
42
|
+
this.shell = shell;
|
|
43
|
+
this.storage = storage;
|
|
44
|
+
api.onGone = (error) => this.serverGone(error);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The local server stopped, or something else answers on its port: every
|
|
48
|
+
* form goes (typed answers with it), the session is forgotten, and the page
|
|
49
|
+
* says what happened. Nothing is sent from here on.
|
|
50
|
+
*/
|
|
51
|
+
serverGone(error) {
|
|
52
|
+
if (this.stopped)
|
|
53
|
+
return;
|
|
54
|
+
this.stopped = true;
|
|
55
|
+
this.run?.stop();
|
|
56
|
+
forgetSession(this.storage);
|
|
57
|
+
replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', 'Disconnected', 'This page sends nothing from here on.'), el('div', { class: 'banner err', role: 'alert' }, icon('error', 'icon banner-icon'), el('div', { class: 'banner-body' }, el('strong', { class: 'banner-title' }, error.why === 'unverified' ? 'This page no longer talks to your deploy UI.' : 'The deploy UI has stopped.'), el('p', {}, error.message), el('p', {}, 'Nothing was sent. Detached runs go on on the server. Start basalt-deploy-ui again and open the URL it prints.')))));
|
|
58
|
+
replace(this.shell.nav);
|
|
59
|
+
this.clearBar();
|
|
60
|
+
replace(this.shell.session);
|
|
61
|
+
}
|
|
62
|
+
get status() {
|
|
63
|
+
return this.statusReply?.result;
|
|
64
|
+
}
|
|
65
|
+
get plan() {
|
|
66
|
+
return this.planReply?.result;
|
|
67
|
+
}
|
|
68
|
+
async start() {
|
|
69
|
+
replace(this.shell.session, el('button', { type: 'button', class: 'ghost danger', on: { click: () => { void this.quit(); } } }, icon('power'), 'Quit'));
|
|
70
|
+
await this.loadTargets();
|
|
71
|
+
const first = this.targets.targets[0];
|
|
72
|
+
if (first) {
|
|
73
|
+
this.select(first.name);
|
|
74
|
+
this.show('status');
|
|
75
|
+
await this.refreshStatus();
|
|
76
|
+
}
|
|
77
|
+
else
|
|
78
|
+
this.show('targets');
|
|
79
|
+
}
|
|
80
|
+
async loadTargets() {
|
|
81
|
+
this.targets = await this.api.post('targets');
|
|
82
|
+
if (this.current && !this.targets.targets.some((t) => t.name === this.current?.name))
|
|
83
|
+
this.current = undefined;
|
|
84
|
+
if (this.current)
|
|
85
|
+
this.current = this.targets.targets.find((t) => t.name === this.current?.name);
|
|
86
|
+
}
|
|
87
|
+
/** The server refused because the target changed after the confirmation: show the file's current values. */
|
|
88
|
+
async noticeChange(error) {
|
|
89
|
+
if (!(error instanceof ApiError) || error.body['changed'] !== true)
|
|
90
|
+
return;
|
|
91
|
+
try {
|
|
92
|
+
await this.loadTargets();
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.renderNav();
|
|
98
|
+
this.renderBar();
|
|
99
|
+
}
|
|
100
|
+
select(name) {
|
|
101
|
+
const found = this.targets.targets.find((t) => t.name === name);
|
|
102
|
+
if (!found || found.name === this.current?.name)
|
|
103
|
+
return;
|
|
104
|
+
this.current = found;
|
|
105
|
+
this.hello = undefined;
|
|
106
|
+
this.statusReply = undefined;
|
|
107
|
+
this.planReply = undefined;
|
|
108
|
+
}
|
|
109
|
+
show(screen) {
|
|
110
|
+
this.screen = screen;
|
|
111
|
+
this.renderNav();
|
|
112
|
+
this.renderBar();
|
|
113
|
+
const main = this.shell.main;
|
|
114
|
+
if (screen === 'targets')
|
|
115
|
+
return replace(main, renderTargets(this));
|
|
116
|
+
if (!this.current)
|
|
117
|
+
return replace(main, emptyState('targets', 'No target selected', 'Add a target first.', el('span', { class: 'row' }, button('Targets', () => this.show('targets'), { class: 'primary' }))));
|
|
118
|
+
if (screen === 'status')
|
|
119
|
+
return replace(main, renderStatus(this));
|
|
120
|
+
if (screen === 'plan')
|
|
121
|
+
return replace(main, renderPlan(this));
|
|
122
|
+
if (screen === 'history')
|
|
123
|
+
return replace(main, renderHistory(this));
|
|
124
|
+
replace(main, this.run ? this.run.element : el('section', { aria: { label: 'Run' } }, emptyState('run', 'Nothing running here', 'No run in this tab yet: start one from Plan & deploy, or attach from Status.')));
|
|
125
|
+
}
|
|
126
|
+
/** Re-renders the current screen (after a state change). */
|
|
127
|
+
refresh() {
|
|
128
|
+
this.show(this.screen);
|
|
129
|
+
}
|
|
130
|
+
renderNav() {
|
|
131
|
+
const select = el('select', { id: 'target-select', aria: { label: 'Target' }, on: { change: (event) => { this.select(event.target.value); this.refresh(); if (this.current)
|
|
132
|
+
void this.refreshStatus(); } } }, ...this.targets.targets.map((t) => {
|
|
133
|
+
const option = el('option', { value: t.name }, t.name);
|
|
134
|
+
option.selected = t.name === this.current?.name;
|
|
135
|
+
return option;
|
|
136
|
+
}));
|
|
137
|
+
replace(this.shell.nav, this.targets.targets.length > 0 ? el('div', { class: 'nav-target' }, el('label', { class: 'section-label', for: 'target-select' }, 'Target'), select) : null, el('div', { class: 'nav-list' }, ...SCREENS.map(([screen, label, name]) => el('button', {
|
|
138
|
+
type: 'button', class: 'nav-item', ...(screen === this.screen ? { aria: { current: 'page' } } : {}),
|
|
139
|
+
on: { click: () => this.show(screen) },
|
|
140
|
+
}, icon(name), label))));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* "Am I about to deploy production?": name, destination, the server's host
|
|
144
|
+
* name and the app — on every screen. The dividers are text (" · ") for
|
|
145
|
+
* screen readers and copy, drawn as rules.
|
|
146
|
+
*/
|
|
147
|
+
renderBar() {
|
|
148
|
+
const t = this.current;
|
|
149
|
+
if (!t)
|
|
150
|
+
return this.clearBar();
|
|
151
|
+
const status = this.status;
|
|
152
|
+
const sep = () => el('span', { class: 'ctx-sep', aria: { hidden: 'true' } }, ' · ');
|
|
153
|
+
const item = (name, value, label) => [sep(),
|
|
154
|
+
el('span', { class: 'ctx-item' }, icon(name), label ? el('span', { class: 'ctx-label' }, label) : null, label ? ' ' : null, el('span', { class: 'ctx-val' }, value))];
|
|
155
|
+
this.shell.bar.classList.toggle('is-protected', t.protected === true);
|
|
156
|
+
replace(this.shell.bar, el('span', { class: 'ctx-target' }, el('strong', { class: 'ctx-name' }, t.name), t.protected ? el('span', { class: 'badge badge-protected', title: 'Deploy and rollback ask for the name to be typed' }, icon('shield'), quiet(' '), 'PROTECTED') : null), ...item('terminal', `${t.ssh}${t.port ? `:${t.port}` : ''}`), ...item('folder', t.dir), ...(this.hello ? [...item('server', this.hello.host, 'host'), ...item('tag', this.hello.version, 'deploy')] : []), ...(status ? item('app', status.app, 'app') : []));
|
|
157
|
+
}
|
|
158
|
+
clearBar() {
|
|
159
|
+
this.shell.bar.classList.remove('is-protected');
|
|
160
|
+
replace(this.shell.bar);
|
|
161
|
+
}
|
|
162
|
+
async refreshStatus() {
|
|
163
|
+
const t = this.current;
|
|
164
|
+
if (!t)
|
|
165
|
+
return;
|
|
166
|
+
this.statusBusy = true;
|
|
167
|
+
if (this.screen === 'status')
|
|
168
|
+
this.refresh();
|
|
169
|
+
try {
|
|
170
|
+
this.statusReply = await this.api.post('status', { target: t.name });
|
|
171
|
+
if (this.statusReply.hello)
|
|
172
|
+
this.hello = this.statusReply.hello;
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
this.statusReply = { ok: false, failure: { code: 'local', message: describe(error) }, diagnostics: [] };
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
this.statusBusy = false;
|
|
179
|
+
}
|
|
180
|
+
if (this.current?.name === t.name)
|
|
181
|
+
this.refresh();
|
|
182
|
+
}
|
|
183
|
+
async runPlan() {
|
|
184
|
+
const t = this.current;
|
|
185
|
+
if (!t)
|
|
186
|
+
return;
|
|
187
|
+
this.planBusy = true;
|
|
188
|
+
this.planReply = undefined;
|
|
189
|
+
this.refresh();
|
|
190
|
+
try {
|
|
191
|
+
this.planReply = await this.api.post('plan', { target: t.name });
|
|
192
|
+
if (this.planReply.hello)
|
|
193
|
+
this.hello = this.planReply.hello;
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
this.planReply = { ok: false, failure: { code: 'local', message: describe(error) }, diagnostics: [] };
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
this.planBusy = false;
|
|
200
|
+
}
|
|
201
|
+
if (this.current?.name === t.name)
|
|
202
|
+
this.refresh();
|
|
203
|
+
}
|
|
204
|
+
/** A run to watch: a new view, attached from the seq this tab last saw (or 0). */
|
|
205
|
+
watch(runId, title, fromStart = false) {
|
|
206
|
+
if (this.run?.runId === runId && !this.run.ended && this.run.following) {
|
|
207
|
+
this.show('run');
|
|
208
|
+
return this.run;
|
|
209
|
+
}
|
|
210
|
+
this.run?.stop();
|
|
211
|
+
const t = this.current;
|
|
212
|
+
this.run = new RunView(this, t, title, runId);
|
|
213
|
+
this.show('run');
|
|
214
|
+
void this.run.attach(fromStart ? 0 : this.run.resumeSeq());
|
|
215
|
+
return this.run;
|
|
216
|
+
}
|
|
217
|
+
/** An attended stream (env, an attended run): it stops if this page closes. */
|
|
218
|
+
attended(title, route, body) {
|
|
219
|
+
this.run?.stop();
|
|
220
|
+
const t = this.current;
|
|
221
|
+
this.run = new RunView(this, t, title, undefined);
|
|
222
|
+
this.show('run');
|
|
223
|
+
void this.run.streamAttended(route, body);
|
|
224
|
+
return this.run;
|
|
225
|
+
}
|
|
226
|
+
async quit() {
|
|
227
|
+
const watching = this.run?.following === true;
|
|
228
|
+
const ok = await confirmDialog({
|
|
229
|
+
title: 'Quit the deploy UI?',
|
|
230
|
+
lines: [
|
|
231
|
+
'The local server stops and this page stops working.',
|
|
232
|
+
watching ? 'A run is being watched: detached runs go on on the server (re-attach later); an attended one stops at its next step.' : 'Detached runs go on on the server.',
|
|
233
|
+
],
|
|
234
|
+
confirmLabel: 'Quit',
|
|
235
|
+
});
|
|
236
|
+
if (!ok)
|
|
237
|
+
return;
|
|
238
|
+
this.run?.stop();
|
|
239
|
+
try {
|
|
240
|
+
await this.api.post('quit');
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
// Already gone.
|
|
244
|
+
}
|
|
245
|
+
this.stopped = true;
|
|
246
|
+
forgetSession(this.storage);
|
|
247
|
+
replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', 'Stopped', 'The deploy UI has stopped. You can close this tab.', 'Detached runs go on on the server; start basalt-deploy-ui again to re-attach.')));
|
|
248
|
+
replace(this.shell.nav);
|
|
249
|
+
this.clearBar();
|
|
250
|
+
replace(this.shell.session);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { append, button, el } from './dom.js';
|
|
2
|
+
import { icon } from './icons.js';
|
|
3
|
+
/**
|
|
4
|
+
* A native modal `<dialog>` (focus stays inside, Escape cancels). Resolves
|
|
5
|
+
* true only on the confirm button.
|
|
6
|
+
*/
|
|
7
|
+
export function confirmDialog(options, host = document.body) {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
const dialog = el('dialog', { class: options.danger ? 'is-danger' : '', aria: { labelledby: 'confirm-title' } });
|
|
10
|
+
let settled = false;
|
|
11
|
+
const finish = (answer) => {
|
|
12
|
+
if (settled)
|
|
13
|
+
return;
|
|
14
|
+
settled = true;
|
|
15
|
+
if (typeof dialog.close === 'function' && dialog.open)
|
|
16
|
+
dialog.close();
|
|
17
|
+
dialog.remove();
|
|
18
|
+
resolve(answer);
|
|
19
|
+
};
|
|
20
|
+
const confirm = button(options.confirmLabel, () => finish(true), { class: options.danger ? 'danger' : 'primary' });
|
|
21
|
+
const dismiss = button('Cancel', () => finish(false));
|
|
22
|
+
let typed;
|
|
23
|
+
if (options.typeToConfirm !== undefined) {
|
|
24
|
+
const expected = options.typeToConfirm;
|
|
25
|
+
confirm.disabled = true;
|
|
26
|
+
typed = el('input', {
|
|
27
|
+
type: 'text', id: 'confirm-name', autocomplete: 'off', spellcheck: false,
|
|
28
|
+
on: { input: () => { confirm.disabled = typed.value !== expected; } },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
append(dialog, el('div', { class: 'dialog-head' }, el('span', { class: 'dialog-icon' }, icon(options.danger ? 'alert' : 'info')), el('h2', { id: 'confirm-title' }, options.title)), el('div', { class: 'dialog-body' }, options.context ? el('p', { class: 'dialog-context' }, options.context) : null, options.typeToConfirm !== undefined ? el('p', { class: 'dialog-protected' }, el('span', { class: 'badge badge-protected' }, icon('shield'), 'PROTECTED'), ' This target is protected.') : null, ...options.lines.filter((line) => line !== null && line !== undefined && line !== false && line !== '').map((line) => el('p', {}, line)), typed ? el('div', { class: 'field' }, el('label', { for: 'confirm-name' }, 'Type ', el('code', {}, options.typeToConfirm), ' to confirm'), typed) : null), el('div', { class: 'dialog-actions' }, dismiss, confirm));
|
|
32
|
+
dialog.addEventListener('cancel', (event) => { event.preventDefault(); finish(false); });
|
|
33
|
+
dialog.addEventListener('keydown', (event) => { if (event.key === 'Escape')
|
|
34
|
+
finish(false); });
|
|
35
|
+
host.appendChild(dialog);
|
|
36
|
+
if (typeof dialog.showModal === 'function') {
|
|
37
|
+
try {
|
|
38
|
+
dialog.showModal();
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
dialog.setAttribute('open', '');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else
|
|
45
|
+
dialog.setAttribute('open', '');
|
|
46
|
+
(typed ?? (options.danger ? dismiss : confirm)).focus();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { icon } from './icons.js';
|
|
2
|
+
import { clean } from './sanitize.js';
|
|
3
|
+
export function text(value) {
|
|
4
|
+
return document.createTextNode(clean(value));
|
|
5
|
+
}
|
|
6
|
+
export function el(tag, props = {}, ...children) {
|
|
7
|
+
const node = document.createElement(tag);
|
|
8
|
+
if (props.class)
|
|
9
|
+
node.className = props.class;
|
|
10
|
+
if (props.id)
|
|
11
|
+
node.id = props.id;
|
|
12
|
+
for (const key of ['type', 'name', 'placeholder', 'title', 'role', 'autocomplete', 'min', 'max', 'for', 'scope']) {
|
|
13
|
+
const value = props[key];
|
|
14
|
+
if (value !== undefined)
|
|
15
|
+
node.setAttribute(key, value);
|
|
16
|
+
}
|
|
17
|
+
if (props.value !== undefined)
|
|
18
|
+
node.value = props.value;
|
|
19
|
+
if (props.disabled)
|
|
20
|
+
node.disabled = true;
|
|
21
|
+
if (props.checked)
|
|
22
|
+
node.checked = true;
|
|
23
|
+
if (props.required)
|
|
24
|
+
node.required = true;
|
|
25
|
+
if (props.hidden)
|
|
26
|
+
node.hidden = true;
|
|
27
|
+
if (props.spellcheck !== undefined)
|
|
28
|
+
node.spellcheck = props.spellcheck;
|
|
29
|
+
for (const [key, value] of Object.entries(props.aria ?? {}))
|
|
30
|
+
node.setAttribute(`aria-${key}`, value);
|
|
31
|
+
for (const [event, listener] of Object.entries(props.on ?? {}))
|
|
32
|
+
node.addEventListener(event, listener);
|
|
33
|
+
append(node, ...children);
|
|
34
|
+
return node;
|
|
35
|
+
}
|
|
36
|
+
export function append(node, ...children) {
|
|
37
|
+
for (const child of children) {
|
|
38
|
+
if (child === false || child === null || child === undefined)
|
|
39
|
+
continue;
|
|
40
|
+
node.appendChild(typeof child === 'string' || typeof child === 'number' ? text(child) : child);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function replace(node, ...children) {
|
|
44
|
+
while (node.firstChild)
|
|
45
|
+
node.removeChild(node.firstChild);
|
|
46
|
+
append(node, ...children);
|
|
47
|
+
}
|
|
48
|
+
export function button(label, onClick, props = {}) {
|
|
49
|
+
return el('button', { type: 'button', ...props, on: { click: () => onClick() } }, label);
|
|
50
|
+
}
|
|
51
|
+
/** A short when: the ISO time as the server wrote it, without milliseconds. */
|
|
52
|
+
export const when = (iso) => clean(iso).replace('T', ' ').replace(/\.\d+Z$/, 'Z');
|
|
53
|
+
/** A status pill: a dot and a label (the label carries the meaning, never the colour alone). */
|
|
54
|
+
export function pill(tone, label) {
|
|
55
|
+
return el('span', { class: `pill pill-${tone}` }, el('span', { class: 'dot', aria: { hidden: 'true' } }), label);
|
|
56
|
+
}
|
|
57
|
+
/** Text that screen readers and `textContent` keep, but the layout replaces with spacing or a divider. */
|
|
58
|
+
export function quiet(value) {
|
|
59
|
+
return el('span', { class: 'sr-only' }, value);
|
|
60
|
+
}
|
|
61
|
+
/** The screen's header: its title (the id a section is labelled by), an optional line under it, and its actions. */
|
|
62
|
+
export function pageHead(id, title, sub, ...actions) {
|
|
63
|
+
const subs = Array.isArray(sub) ? sub : [sub];
|
|
64
|
+
return el('header', { class: 'page-head' }, el('div', { class: 'page-title' }, el('h2', { id }, title), subs.some((s) => s !== null && s !== undefined && s !== false && s !== '') ? el('p', { class: 'page-sub' }, ...subs) : null), actions.some(Boolean) ? el('div', { class: 'page-actions' }, ...actions) : null);
|
|
65
|
+
}
|
|
66
|
+
/** A card: a header row (title, then actions) and a body. */
|
|
67
|
+
export function card(title, body, options = {}) {
|
|
68
|
+
return el('div', { class: `card${options.class ? ` ${options.class}` : ''}` }, el('div', { class: 'card-head' }, el('h3', {}, title), options.actions && options.actions.some(Boolean) ? el('div', { class: 'card-actions' }, ...options.actions) : null), el('div', { class: `card-body${options.bodyClass ? ` ${options.bodyClass}` : ''}` }, ...body), options.foot && options.foot.some(Boolean) ? el('div', { class: 'card-foot' }, ...options.foot) : null);
|
|
69
|
+
}
|
|
70
|
+
/** A key/value grid. */
|
|
71
|
+
export function kv(rows) {
|
|
72
|
+
const list = el('dl', { class: 'kv' });
|
|
73
|
+
for (const row of rows) {
|
|
74
|
+
if (!row)
|
|
75
|
+
continue;
|
|
76
|
+
append(list, el('div', { class: 'kv-row' }, el('dt', {}, row[0]), el('dd', {}, row[1])));
|
|
77
|
+
}
|
|
78
|
+
return list;
|
|
79
|
+
}
|
|
80
|
+
/** An empty or final state: an icon, a title and a line. */
|
|
81
|
+
export function emptyState(name, title, ...lines) {
|
|
82
|
+
return el('div', { class: 'empty' }, el('div', { class: 'empty-icon' }, icon(name)), el('p', { class: 'empty-title' }, title), ...lines.map((line) => el('p', { class: 'empty-text' }, line)));
|
|
83
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's icons: fixed path data drawn with `createElementNS`, stroked in
|
|
3
|
+
* `currentColor`. No markup is parsed and nothing is loaded from elsewhere;
|
|
4
|
+
* every icon is decorative (`aria-hidden`), the text next to it carries the meaning.
|
|
5
|
+
*/
|
|
6
|
+
const PATHS = {
|
|
7
|
+
status: 'M3 12h4l3-8 4 16 3-8h4',
|
|
8
|
+
plan: 'M12 15V3M7 8l5-5 5 5M5 21h14',
|
|
9
|
+
history: 'M3 12a9 9 0 1 0 2.6-6.4L3 8M3 3v5h5M12 7v5l3 2',
|
|
10
|
+
run: 'M4 17l6-5-6-5M12 19h8',
|
|
11
|
+
targets: 'M4 4h16v6H4zM4 14h16v6H4zM8 7h.01M8 17h.01',
|
|
12
|
+
lock: 'M6 11h12v10H6zM8 11V7a4 4 0 0 1 8 0v4',
|
|
13
|
+
terminal: 'M4 17l6-5-6-5M12 19h8',
|
|
14
|
+
folder: 'M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z',
|
|
15
|
+
server: 'M5 4h14v6H5zM5 14h14v6H5zM8 7h.01M8 17h.01',
|
|
16
|
+
app: 'M12 3l8 4.5v9L12 21l-8-4.5v-9zM4 7.5l8 4.5 8-4.5M12 12v9',
|
|
17
|
+
check: 'M5 12l5 5L20 7',
|
|
18
|
+
cross: 'M6 6l12 12M18 6L6 18',
|
|
19
|
+
alert: 'M12 3.5l9.5 16.5h-19zM12 10v4.5M12 17.5h.01',
|
|
20
|
+
info: 'M12 3a9 9 0 1 1 0 18 9 9 0 1 1 0-18zM12 11v6M12 7.5h.01',
|
|
21
|
+
ok: 'M12 3a9 9 0 1 1 0 18 9 9 0 1 1 0-18zM8 12.5l3 3 5-6',
|
|
22
|
+
error: 'M12 3a9 9 0 1 1 0 18 9 9 0 1 1 0-18zM12 7.5v5.5M12 16.5h.01',
|
|
23
|
+
shield: 'M12 3l8 3v6c0 4.5-3.4 7.8-8 9-4.6-1.2-8-4.5-8-9V6z',
|
|
24
|
+
power: 'M12 3v8M6.3 6.6a8 8 0 1 0 11.4 0',
|
|
25
|
+
refresh: 'M20 11a8 8 0 0 0-14.6-4.5M4 3v4h4M4 13a8 8 0 0 0 14.6 4.5M20 21v-4h-4',
|
|
26
|
+
inbox: 'M3 13l3-8h12l3 8v6H3zM3 13h5l1.5 2h5L16 13h5',
|
|
27
|
+
tag: 'M3 12V4h8l10 10-8 8zM7.5 8h.01',
|
|
28
|
+
};
|
|
29
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
30
|
+
export function icon(name, className = 'icon') {
|
|
31
|
+
const svg = document.createElementNS(SVG, 'svg');
|
|
32
|
+
svg.setAttribute('class', className);
|
|
33
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
34
|
+
svg.setAttribute('width', '16');
|
|
35
|
+
svg.setAttribute('height', '16');
|
|
36
|
+
svg.setAttribute('fill', 'none');
|
|
37
|
+
svg.setAttribute('stroke', 'currentColor');
|
|
38
|
+
svg.setAttribute('stroke-width', '1.75');
|
|
39
|
+
svg.setAttribute('stroke-linecap', 'round');
|
|
40
|
+
svg.setAttribute('stroke-linejoin', 'round');
|
|
41
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
42
|
+
svg.setAttribute('focusable', 'false');
|
|
43
|
+
const path = document.createElementNS(SVG, 'path');
|
|
44
|
+
path.setAttribute('d', PATHS[name]);
|
|
45
|
+
svg.appendChild(path);
|
|
46
|
+
return svg;
|
|
47
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Api, ApiError, exchange, ServerGone } from './api.js';
|
|
2
|
+
import { App, forgetSession, SERVER_KEY, TOKEN_KEY } from './app.js';
|
|
3
|
+
import { el, replace } from './dom.js';
|
|
4
|
+
import { describe } from './reply.js';
|
|
5
|
+
function storageOf(win) {
|
|
6
|
+
try {
|
|
7
|
+
return win.sessionStorage;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The page's start: the one-time launch code from the URL fragment (removed
|
|
15
|
+
* from the address bar at once), exchanged for the session token and the
|
|
16
|
+
* server key, kept in this tab's sessionStorage — never persistent storage,
|
|
17
|
+
* never a cookie. Then the server proves it is the process that printed the
|
|
18
|
+
* URL, before anything else.
|
|
19
|
+
*/
|
|
20
|
+
export async function boot(env) {
|
|
21
|
+
const doc = env.document;
|
|
22
|
+
const shell = {
|
|
23
|
+
nav: doc.getElementById('nav'),
|
|
24
|
+
bar: doc.getElementById('target-bar'),
|
|
25
|
+
main: doc.getElementById('app'),
|
|
26
|
+
session: doc.getElementById('session'),
|
|
27
|
+
};
|
|
28
|
+
const match = /^#launch=([A-Za-z0-9_-]{1,128})$/.exec(env.location.hash);
|
|
29
|
+
if (env.location.hash)
|
|
30
|
+
env.history.replaceState(null, '', env.location.pathname);
|
|
31
|
+
let token;
|
|
32
|
+
let serverKey;
|
|
33
|
+
try {
|
|
34
|
+
token = env.storage?.getItem(TOKEN_KEY) ?? undefined;
|
|
35
|
+
serverKey = env.storage?.getItem(SERVER_KEY) ?? undefined;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
token = undefined;
|
|
39
|
+
}
|
|
40
|
+
if (match) {
|
|
41
|
+
try {
|
|
42
|
+
;
|
|
43
|
+
({ token, serverKey } = await exchange(match[1], env.fetch));
|
|
44
|
+
try {
|
|
45
|
+
env.storage?.setItem(TOKEN_KEY, token);
|
|
46
|
+
env.storage?.setItem(SERVER_KEY, serverKey);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// No storage: this tab works until it reloads.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
const taken = error instanceof ApiError && error.status === 409;
|
|
54
|
+
replace(shell.main, el('div', { class: 'banner err', role: 'alert' }, el('strong', {}, taken ? 'This UI already has its browser session.' : 'The launch code was refused.'), el('p', {}, taken
|
|
55
|
+
? 'Another tab or browser claimed it first. If that was not you, stop basalt-deploy-ui in your terminal now (Ctrl-C) and start it again.'
|
|
56
|
+
: `${describe(error)}. Start basalt-deploy-ui again and open the URL it prints.`)));
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!token || !serverKey) {
|
|
61
|
+
forgetSession(env.storage);
|
|
62
|
+
replace(shell.main, el('p', {}, 'No session: open the URL basalt-deploy-ui printed in your terminal (it works once, for 2 minutes).'));
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const app = new App(new Api(token, env.fetch, { serverKey, ...(env.crypto ? { crypto: env.crypto } : {}) }), shell, env.storage);
|
|
66
|
+
try {
|
|
67
|
+
await app.api.verifyServer();
|
|
68
|
+
await app.start();
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
// The server is gone or not ours: the page already says so.
|
|
72
|
+
if (error instanceof ServerGone)
|
|
73
|
+
return undefined;
|
|
74
|
+
replace(shell.main, el('div', { class: 'banner err', role: 'alert' }, el('strong', {}, 'The UI could not start.'), el('p', {}, describe(error))));
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return app;
|
|
78
|
+
}
|
|
79
|
+
if (typeof window !== 'undefined' && document.getElementById('app')) {
|
|
80
|
+
void boot({ location: window.location, history: window.history, storage: storageOf(window), fetch: (input, init) => window.fetch(input, init), document, crypto: window.crypto });
|
|
81
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { DeployEvent, ErrorEvent, HelloEvent } from '@basaltkit/deploy/protocol';
|
|
2
|
+
/**
|
|
3
|
+
* What the local server sends the page (RFC 0009 §7). Types only: shared by
|
|
4
|
+
* the server and the browser code, erased at compile time.
|
|
5
|
+
*/
|
|
6
|
+
/** Why a remote command produced no usable answer, as the UI shows it. */
|
|
7
|
+
export interface Failure {
|
|
8
|
+
code: string;
|
|
9
|
+
message: string;
|
|
10
|
+
hint?: string;
|
|
11
|
+
}
|
|
12
|
+
/** The answer of a collected command (status, plan, run --detach, rollback --detach, cancel). */
|
|
13
|
+
export interface CommandReply {
|
|
14
|
+
ok: boolean;
|
|
15
|
+
hello?: HelloEvent;
|
|
16
|
+
result?: unknown;
|
|
17
|
+
error?: ErrorEvent;
|
|
18
|
+
exitCode?: number;
|
|
19
|
+
failure?: Failure;
|
|
20
|
+
/** Foreign text before `hello` and ssh's stderr, sanitised. */
|
|
21
|
+
diagnostics: string[];
|
|
22
|
+
}
|
|
23
|
+
/** One NDJSON line of a stream (env, an attended run, attach): local state is never confused with a server event. */
|
|
24
|
+
export type StreamMessage = {
|
|
25
|
+
kind: 'hello';
|
|
26
|
+
hello: HelloEvent;
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'event';
|
|
29
|
+
event: DeployEvent;
|
|
30
|
+
known: boolean;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'diagnostic';
|
|
33
|
+
text: string;
|
|
34
|
+
} | {
|
|
35
|
+
kind: 'state';
|
|
36
|
+
state: 'connecting' | 'reconnecting' | 'ended';
|
|
37
|
+
exitCode?: number;
|
|
38
|
+
code?: string;
|
|
39
|
+
reason?: string;
|
|
40
|
+
hint?: string;
|
|
41
|
+
retryInMs?: number;
|
|
42
|
+
};
|
|
43
|
+
export interface TargetView {
|
|
44
|
+
name: string;
|
|
45
|
+
ssh: string;
|
|
46
|
+
port?: number;
|
|
47
|
+
dir: string;
|
|
48
|
+
node?: string;
|
|
49
|
+
config?: string;
|
|
50
|
+
protected?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export interface TargetsReply {
|
|
53
|
+
path: string;
|
|
54
|
+
exists: boolean;
|
|
55
|
+
actor?: string;
|
|
56
|
+
targets: TargetView[];
|
|
57
|
+
error?: string;
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { el, quiet } from './dom.js';
|
|
2
|
+
import { icon } from './icons.js';
|
|
3
|
+
import { ApiError } from './api.js';
|
|
4
|
+
/** What went wrong, as a banner: a protocol error (code, message, hint), a failure of the connection, or the local server's refusal. */
|
|
5
|
+
export function problem(reply, error) {
|
|
6
|
+
if (error !== undefined)
|
|
7
|
+
return banner('err', 'The UI refused the request', [describe(error)]);
|
|
8
|
+
if (!reply)
|
|
9
|
+
return null;
|
|
10
|
+
if (reply.failure)
|
|
11
|
+
return failureBanner(reply.failure, reply.diagnostics);
|
|
12
|
+
if (reply.error)
|
|
13
|
+
return errorBanner(reply.error, reply.diagnostics);
|
|
14
|
+
if (!reply.ok)
|
|
15
|
+
return banner('err', 'The command failed', [`exit code ${reply.exitCode ?? 'unknown'}`], reply.diagnostics);
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
export function describe(error) {
|
|
19
|
+
if (error instanceof ApiError)
|
|
20
|
+
return `${error.message} (HTTP ${error.status})`;
|
|
21
|
+
return error instanceof Error ? error.message : String(error);
|
|
22
|
+
}
|
|
23
|
+
export function failureBanner(failure, diagnostics = []) {
|
|
24
|
+
return banner('err', failure.message, failure.hint ? [failure.hint] : [], diagnostics, failure.code);
|
|
25
|
+
}
|
|
26
|
+
export function errorBanner(error, diagnostics = []) {
|
|
27
|
+
const lines = [];
|
|
28
|
+
if (error.hint)
|
|
29
|
+
lines.push(error.hint);
|
|
30
|
+
if (error.holder)
|
|
31
|
+
lines.push(`held by pid ${error.holder.pid} on ${error.holder.host}, ${error.holder.actor}, ${error.holder.command}${error.holder.runId ? ` (run ${error.holder.runId})` : ''}`);
|
|
32
|
+
for (const finding of error.findings ?? [])
|
|
33
|
+
lines.push(`${finding.level}: ${finding.message}${finding.fix ? ` — ${finding.fix}` : ''}`);
|
|
34
|
+
return banner('err', error.message, lines, diagnostics, error.code);
|
|
35
|
+
}
|
|
36
|
+
export function banner(level, title, lines = [], diagnostics = [], code) {
|
|
37
|
+
return el('div', { class: `banner ${level}`, role: level === 'err' ? 'alert' : 'status' }, icon(level === 'ok' ? 'ok' : level === 'warn' ? 'alert' : 'error', 'icon banner-icon'), el('div', { class: 'banner-body' }, el('strong', { class: 'banner-title' }, quiet(level === 'ok' ? 'OK: ' : level === 'warn' ? 'Warning: ' : 'Error: '), title), code ? el('span', { class: 'banner-code' }, ` [${code}]`) : null, ...lines.filter((line) => line !== '' && line !== null && line !== undefined && line !== false).map((line) => el('p', {}, line)), diagnostics.length > 0 ? diagnosticsBlock(diagnostics) : null));
|
|
38
|
+
}
|
|
39
|
+
export function diagnosticsBlock(lines) {
|
|
40
|
+
return el('details', { class: 'disclosure' }, el('summary', {}, `Diagnostics (${lines.length})`), el('pre', { class: 'diagnostics' }, lines.join('\n')));
|
|
41
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every string from the server is data (RFC 0009 §9). The deploy already
|
|
3
|
+
* sanitised it; the page does it again before it reaches a text node: ANSI/VT
|
|
4
|
+
* sequences, C0/C1 controls but tab and newline, and bidi controls (a
|
|
5
|
+
* "Trojan Source" line must not reorder what the operator reads).
|
|
6
|
+
*/
|
|
7
|
+
export function clean(value) {
|
|
8
|
+
const text = typeof value === 'string' ? value : value === undefined || value === null ? '' : String(value);
|
|
9
|
+
return text
|
|
10
|
+
.replace(/\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)?/g, '')
|
|
11
|
+
.replace(/(?:\u001b\[|\u009b)[0-?]*[ -/]*[@-~]/g, '')
|
|
12
|
+
.replace(/\u001b[@-_]?/g, '')
|
|
13
|
+
.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f--]/g, '');
|
|
14
|
+
}
|
|
15
|
+
/** A commit as the UI shows it. */
|
|
16
|
+
export const short = (sha) => clean(sha).slice(0, 12);
|