@basaltkit/deploy-ui 0.0.0-stage → 0.1.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 +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 +235 -0
- package/dist/client/dialog.js +47 -0
- package/dist/client/dom.js +51 -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 +40 -0
- package/dist/client/sanitize.js +16 -0
- package/dist/client/view-history.js +52 -0
- package/dist/client/view-plan.js +154 -0
- package/dist/client/view-run.js +311 -0
- package/dist/client/view-status.js +38 -0
- package/dist/client/view-targets.js +132 -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 +101 -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,235 @@
|
|
|
1
|
+
import { ApiError } from './api.js';
|
|
2
|
+
import { confirmDialog } from './dialog.js';
|
|
3
|
+
import { button, el, replace } from './dom.js';
|
|
4
|
+
import { describe } from './reply.js';
|
|
5
|
+
import { renderHistory } from './view-history.js';
|
|
6
|
+
import { renderPlan } from './view-plan.js';
|
|
7
|
+
import { RunView } from './view-run.js';
|
|
8
|
+
import { renderStatus } from './view-status.js';
|
|
9
|
+
import { renderTargets } from './view-targets.js';
|
|
10
|
+
/** This tab's session (sessionStorage): the bearer token, and the key the server proves itself with. */
|
|
11
|
+
export const TOKEN_KEY = 'basalt-deploy-ui:token';
|
|
12
|
+
export const SERVER_KEY = 'basalt-deploy-ui:server-key';
|
|
13
|
+
/** Forgets this tab's session: after a stop it is worth nothing, and nothing should look like it still works. */
|
|
14
|
+
export function forgetSession(storage) {
|
|
15
|
+
try {
|
|
16
|
+
storage?.removeItem(TOKEN_KEY);
|
|
17
|
+
storage?.removeItem(SERVER_KEY);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// No storage: nothing kept.
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const SCREENS = [['status', 'Status'], ['plan', 'Plan & deploy'], ['history', 'History'], ['run', 'Run'], ['targets', 'Targets']];
|
|
24
|
+
/** Per-target state the screens render; every server string in it is shown as text only. */
|
|
25
|
+
export class App {
|
|
26
|
+
api;
|
|
27
|
+
shell;
|
|
28
|
+
storage;
|
|
29
|
+
targets = { path: '', exists: false, targets: [] };
|
|
30
|
+
current;
|
|
31
|
+
hello;
|
|
32
|
+
statusReply;
|
|
33
|
+
planReply;
|
|
34
|
+
run;
|
|
35
|
+
screen = 'targets';
|
|
36
|
+
statusBusy = false;
|
|
37
|
+
planBusy = false;
|
|
38
|
+
stopped = false;
|
|
39
|
+
constructor(api, shell, storage) {
|
|
40
|
+
this.api = api;
|
|
41
|
+
this.shell = shell;
|
|
42
|
+
this.storage = storage;
|
|
43
|
+
api.onGone = (error) => this.serverGone(error);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The local server stopped, or something else answers on its port: every
|
|
47
|
+
* form goes (typed answers with it), the session is forgotten, and the page
|
|
48
|
+
* says what happened. Nothing is sent from here on.
|
|
49
|
+
*/
|
|
50
|
+
serverGone(error) {
|
|
51
|
+
if (this.stopped)
|
|
52
|
+
return;
|
|
53
|
+
this.stopped = true;
|
|
54
|
+
this.run?.stop();
|
|
55
|
+
forgetSession(this.storage);
|
|
56
|
+
replace(this.shell.main, el('div', { class: 'banner err', role: 'alert' }, el('strong', {}, 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.')));
|
|
57
|
+
replace(this.shell.nav);
|
|
58
|
+
replace(this.shell.bar);
|
|
59
|
+
replace(this.shell.session);
|
|
60
|
+
}
|
|
61
|
+
get status() {
|
|
62
|
+
return this.statusReply?.result;
|
|
63
|
+
}
|
|
64
|
+
get plan() {
|
|
65
|
+
return this.planReply?.result;
|
|
66
|
+
}
|
|
67
|
+
async start() {
|
|
68
|
+
replace(this.shell.session, button('Quit', () => { void this.quit(); }, { class: 'danger' }));
|
|
69
|
+
await this.loadTargets();
|
|
70
|
+
const first = this.targets.targets[0];
|
|
71
|
+
if (first) {
|
|
72
|
+
this.select(first.name);
|
|
73
|
+
this.show('status');
|
|
74
|
+
await this.refreshStatus();
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
this.show('targets');
|
|
78
|
+
}
|
|
79
|
+
async loadTargets() {
|
|
80
|
+
this.targets = await this.api.post('targets');
|
|
81
|
+
if (this.current && !this.targets.targets.some((t) => t.name === this.current?.name))
|
|
82
|
+
this.current = undefined;
|
|
83
|
+
if (this.current)
|
|
84
|
+
this.current = this.targets.targets.find((t) => t.name === this.current?.name);
|
|
85
|
+
}
|
|
86
|
+
/** The server refused because the target changed after the confirmation: show the file's current values. */
|
|
87
|
+
async noticeChange(error) {
|
|
88
|
+
if (!(error instanceof ApiError) || error.body['changed'] !== true)
|
|
89
|
+
return;
|
|
90
|
+
try {
|
|
91
|
+
await this.loadTargets();
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this.renderNav();
|
|
97
|
+
this.renderBar();
|
|
98
|
+
}
|
|
99
|
+
select(name) {
|
|
100
|
+
const found = this.targets.targets.find((t) => t.name === name);
|
|
101
|
+
if (!found || found.name === this.current?.name)
|
|
102
|
+
return;
|
|
103
|
+
this.current = found;
|
|
104
|
+
this.hello = undefined;
|
|
105
|
+
this.statusReply = undefined;
|
|
106
|
+
this.planReply = undefined;
|
|
107
|
+
}
|
|
108
|
+
show(screen) {
|
|
109
|
+
this.screen = screen;
|
|
110
|
+
this.renderNav();
|
|
111
|
+
this.renderBar();
|
|
112
|
+
const main = this.shell.main;
|
|
113
|
+
if (screen === 'targets')
|
|
114
|
+
return replace(main, renderTargets(this));
|
|
115
|
+
if (!this.current)
|
|
116
|
+
return replace(main, el('p', {}, 'Add a target first.'), button('Targets', () => this.show('targets')));
|
|
117
|
+
if (screen === 'status')
|
|
118
|
+
return replace(main, renderStatus(this));
|
|
119
|
+
if (screen === 'plan')
|
|
120
|
+
return replace(main, renderPlan(this));
|
|
121
|
+
if (screen === 'history')
|
|
122
|
+
return replace(main, renderHistory(this));
|
|
123
|
+
replace(main, this.run ? this.run.element : el('p', { class: 'muted' }, 'No run in this tab yet: start one from Plan & deploy, or attach from Status.'));
|
|
124
|
+
}
|
|
125
|
+
/** Re-renders the current screen (after a state change). */
|
|
126
|
+
refresh() {
|
|
127
|
+
this.show(this.screen);
|
|
128
|
+
}
|
|
129
|
+
renderNav() {
|
|
130
|
+
const select = el('select', { aria: { label: 'Target' }, on: { change: (event) => { this.select(event.target.value); this.refresh(); if (this.current)
|
|
131
|
+
void this.refreshStatus(); } } }, ...this.targets.targets.map((t) => {
|
|
132
|
+
const option = el('option', { value: t.name }, t.name);
|
|
133
|
+
option.selected = t.name === this.current?.name;
|
|
134
|
+
return option;
|
|
135
|
+
}));
|
|
136
|
+
replace(this.shell.nav, this.targets.targets.length > 0 ? select : null, ...SCREENS.map(([screen, label]) => button(label, () => this.show(screen), screen === this.screen ? { aria: { current: 'page' } } : {})));
|
|
137
|
+
}
|
|
138
|
+
/** "Am I about to deploy production?": name, destination, the server's host name and the app — on every screen. */
|
|
139
|
+
renderBar() {
|
|
140
|
+
const t = this.current;
|
|
141
|
+
if (!t)
|
|
142
|
+
return replace(this.shell.bar);
|
|
143
|
+
const status = this.status;
|
|
144
|
+
replace(this.shell.bar, el('strong', {}, t.name), ` · ${t.ssh}${t.port ? `:${t.port}` : ''} · ${t.dir}`, this.hello ? ` · host ${this.hello.host} · deploy ${this.hello.version}` : '', status ? ` · app ${status.app}` : '', t.protected ? el('strong', { class: 'status-err' }, ' · PROTECTED') : '');
|
|
145
|
+
}
|
|
146
|
+
async refreshStatus() {
|
|
147
|
+
const t = this.current;
|
|
148
|
+
if (!t)
|
|
149
|
+
return;
|
|
150
|
+
this.statusBusy = true;
|
|
151
|
+
if (this.screen === 'status')
|
|
152
|
+
this.refresh();
|
|
153
|
+
try {
|
|
154
|
+
this.statusReply = await this.api.post('status', { target: t.name });
|
|
155
|
+
if (this.statusReply.hello)
|
|
156
|
+
this.hello = this.statusReply.hello;
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
this.statusReply = { ok: false, failure: { code: 'local', message: describe(error) }, diagnostics: [] };
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
this.statusBusy = false;
|
|
163
|
+
}
|
|
164
|
+
if (this.current?.name === t.name)
|
|
165
|
+
this.refresh();
|
|
166
|
+
}
|
|
167
|
+
async runPlan() {
|
|
168
|
+
const t = this.current;
|
|
169
|
+
if (!t)
|
|
170
|
+
return;
|
|
171
|
+
this.planBusy = true;
|
|
172
|
+
this.planReply = undefined;
|
|
173
|
+
this.refresh();
|
|
174
|
+
try {
|
|
175
|
+
this.planReply = await this.api.post('plan', { target: t.name });
|
|
176
|
+
if (this.planReply.hello)
|
|
177
|
+
this.hello = this.planReply.hello;
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
this.planReply = { ok: false, failure: { code: 'local', message: describe(error) }, diagnostics: [] };
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
this.planBusy = false;
|
|
184
|
+
}
|
|
185
|
+
if (this.current?.name === t.name)
|
|
186
|
+
this.refresh();
|
|
187
|
+
}
|
|
188
|
+
/** A run to watch: a new view, attached from the seq this tab last saw (or 0). */
|
|
189
|
+
watch(runId, title, fromStart = false) {
|
|
190
|
+
if (this.run?.runId === runId && !this.run.ended && this.run.following) {
|
|
191
|
+
this.show('run');
|
|
192
|
+
return this.run;
|
|
193
|
+
}
|
|
194
|
+
this.run?.stop();
|
|
195
|
+
const t = this.current;
|
|
196
|
+
this.run = new RunView(this, t, title, runId);
|
|
197
|
+
this.show('run');
|
|
198
|
+
void this.run.attach(fromStart ? 0 : this.run.resumeSeq());
|
|
199
|
+
return this.run;
|
|
200
|
+
}
|
|
201
|
+
/** An attended stream (env, an attended run): it stops if this page closes. */
|
|
202
|
+
attended(title, route, body) {
|
|
203
|
+
this.run?.stop();
|
|
204
|
+
const t = this.current;
|
|
205
|
+
this.run = new RunView(this, t, title, undefined);
|
|
206
|
+
this.show('run');
|
|
207
|
+
void this.run.streamAttended(route, body);
|
|
208
|
+
return this.run;
|
|
209
|
+
}
|
|
210
|
+
async quit() {
|
|
211
|
+
const watching = this.run?.following === true;
|
|
212
|
+
const ok = await confirmDialog({
|
|
213
|
+
title: 'Quit the deploy UI?',
|
|
214
|
+
lines: [
|
|
215
|
+
'The local server stops and this page stops working.',
|
|
216
|
+
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.',
|
|
217
|
+
],
|
|
218
|
+
confirmLabel: 'Quit',
|
|
219
|
+
});
|
|
220
|
+
if (!ok)
|
|
221
|
+
return;
|
|
222
|
+
this.run?.stop();
|
|
223
|
+
try {
|
|
224
|
+
await this.api.post('quit');
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// Already gone.
|
|
228
|
+
}
|
|
229
|
+
this.stopped = true;
|
|
230
|
+
forgetSession(this.storage);
|
|
231
|
+
replace(this.shell.main, el('p', {}, 'The deploy UI has stopped. You can close this tab.'));
|
|
232
|
+
replace(this.shell.nav);
|
|
233
|
+
replace(this.shell.session);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { append, button, el } from './dom.js';
|
|
2
|
+
/**
|
|
3
|
+
* A native modal `<dialog>` (focus stays inside, Escape cancels). Resolves
|
|
4
|
+
* true only on the confirm button.
|
|
5
|
+
*/
|
|
6
|
+
export function confirmDialog(options, host = document.body) {
|
|
7
|
+
return new Promise((resolve) => {
|
|
8
|
+
const dialog = el('dialog', { aria: { labelledby: 'confirm-title' } });
|
|
9
|
+
let settled = false;
|
|
10
|
+
const finish = (answer) => {
|
|
11
|
+
if (settled)
|
|
12
|
+
return;
|
|
13
|
+
settled = true;
|
|
14
|
+
if (typeof dialog.close === 'function' && dialog.open)
|
|
15
|
+
dialog.close();
|
|
16
|
+
dialog.remove();
|
|
17
|
+
resolve(answer);
|
|
18
|
+
};
|
|
19
|
+
const confirm = button(options.confirmLabel, () => finish(true), { class: options.danger ? 'danger' : 'primary' });
|
|
20
|
+
const dismiss = button('Cancel', () => finish(false));
|
|
21
|
+
let typed;
|
|
22
|
+
if (options.typeToConfirm !== undefined) {
|
|
23
|
+
const expected = options.typeToConfirm;
|
|
24
|
+
confirm.disabled = true;
|
|
25
|
+
typed = el('input', {
|
|
26
|
+
type: 'text', id: 'confirm-name', autocomplete: 'off', spellcheck: false,
|
|
27
|
+
on: { input: () => { confirm.disabled = typed.value !== expected; } },
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
append(dialog, el('h2', { id: 'confirm-title' }, options.title), ...options.lines.map((line) => el('p', {}, line)), typed ? el('div', { class: 'field' }, el('label', { for: 'confirm-name' }, `Type ${options.typeToConfirm} to confirm`), typed) : null, el('div', { class: 'row' }, dismiss, confirm));
|
|
31
|
+
dialog.addEventListener('cancel', (event) => { event.preventDefault(); finish(false); });
|
|
32
|
+
dialog.addEventListener('keydown', (event) => { if (event.key === 'Escape')
|
|
33
|
+
finish(false); });
|
|
34
|
+
host.appendChild(dialog);
|
|
35
|
+
if (typeof dialog.showModal === 'function') {
|
|
36
|
+
try {
|
|
37
|
+
dialog.showModal();
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
dialog.setAttribute('open', '');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else
|
|
44
|
+
dialog.setAttribute('open', '');
|
|
45
|
+
(typed ?? (options.danger ? dismiss : confirm)).focus();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { clean } from './sanitize.js';
|
|
2
|
+
export function text(value) {
|
|
3
|
+
return document.createTextNode(clean(value));
|
|
4
|
+
}
|
|
5
|
+
export function el(tag, props = {}, ...children) {
|
|
6
|
+
const node = document.createElement(tag);
|
|
7
|
+
if (props.class)
|
|
8
|
+
node.className = props.class;
|
|
9
|
+
if (props.id)
|
|
10
|
+
node.id = props.id;
|
|
11
|
+
for (const key of ['type', 'name', 'placeholder', 'title', 'role', 'autocomplete', 'min', 'max', 'for']) {
|
|
12
|
+
const value = props[key];
|
|
13
|
+
if (value !== undefined)
|
|
14
|
+
node.setAttribute(key, value);
|
|
15
|
+
}
|
|
16
|
+
if (props.value !== undefined)
|
|
17
|
+
node.value = props.value;
|
|
18
|
+
if (props.disabled)
|
|
19
|
+
node.disabled = true;
|
|
20
|
+
if (props.checked)
|
|
21
|
+
node.checked = true;
|
|
22
|
+
if (props.required)
|
|
23
|
+
node.required = true;
|
|
24
|
+
if (props.hidden)
|
|
25
|
+
node.hidden = true;
|
|
26
|
+
if (props.spellcheck !== undefined)
|
|
27
|
+
node.spellcheck = props.spellcheck;
|
|
28
|
+
for (const [key, value] of Object.entries(props.aria ?? {}))
|
|
29
|
+
node.setAttribute(`aria-${key}`, value);
|
|
30
|
+
for (const [event, listener] of Object.entries(props.on ?? {}))
|
|
31
|
+
node.addEventListener(event, listener);
|
|
32
|
+
append(node, ...children);
|
|
33
|
+
return node;
|
|
34
|
+
}
|
|
35
|
+
export function append(node, ...children) {
|
|
36
|
+
for (const child of children) {
|
|
37
|
+
if (child === false || child === null || child === undefined)
|
|
38
|
+
continue;
|
|
39
|
+
node.appendChild(typeof child === 'string' || typeof child === 'number' ? text(child) : child);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function replace(node, ...children) {
|
|
43
|
+
while (node.firstChild)
|
|
44
|
+
node.removeChild(node.firstChild);
|
|
45
|
+
append(node, ...children);
|
|
46
|
+
}
|
|
47
|
+
export function button(label, onClick, props = {}) {
|
|
48
|
+
return el('button', { type: 'button', ...props, on: { click: () => onClick() } }, label);
|
|
49
|
+
}
|
|
50
|
+
/** A short when: the ISO time as the server wrote it, without milliseconds. */
|
|
51
|
+
export const when = (iso) => clean(iso).replace('T', ' ').replace(/\.\d+Z$/, 'Z');
|
|
@@ -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,40 @@
|
|
|
1
|
+
import { el } from './dom.js';
|
|
2
|
+
import { ApiError } from './api.js';
|
|
3
|
+
/** What went wrong, as a banner: a protocol error (code, message, hint), a failure of the connection, or the local server's refusal. */
|
|
4
|
+
export function problem(reply, error) {
|
|
5
|
+
if (error !== undefined)
|
|
6
|
+
return banner('err', 'The UI refused the request', [describe(error)]);
|
|
7
|
+
if (!reply)
|
|
8
|
+
return null;
|
|
9
|
+
if (reply.failure)
|
|
10
|
+
return failureBanner(reply.failure, reply.diagnostics);
|
|
11
|
+
if (reply.error)
|
|
12
|
+
return errorBanner(reply.error, reply.diagnostics);
|
|
13
|
+
if (!reply.ok)
|
|
14
|
+
return banner('err', 'The command failed', [`exit code ${reply.exitCode ?? 'unknown'}`], reply.diagnostics);
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
export function describe(error) {
|
|
18
|
+
if (error instanceof ApiError)
|
|
19
|
+
return `${error.message} (HTTP ${error.status})`;
|
|
20
|
+
return error instanceof Error ? error.message : String(error);
|
|
21
|
+
}
|
|
22
|
+
export function failureBanner(failure, diagnostics = []) {
|
|
23
|
+
return banner('err', failure.message, failure.hint ? [failure.hint] : [], diagnostics, failure.code);
|
|
24
|
+
}
|
|
25
|
+
export function errorBanner(error, diagnostics = []) {
|
|
26
|
+
const lines = [];
|
|
27
|
+
if (error.hint)
|
|
28
|
+
lines.push(error.hint);
|
|
29
|
+
if (error.holder)
|
|
30
|
+
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})` : ''}`);
|
|
31
|
+
for (const finding of error.findings ?? [])
|
|
32
|
+
lines.push(`${finding.level}: ${finding.message}${finding.fix ? ` — ${finding.fix}` : ''}`);
|
|
33
|
+
return banner('err', error.message, lines, diagnostics, error.code);
|
|
34
|
+
}
|
|
35
|
+
export function banner(level, title, lines = [], diagnostics = [], code) {
|
|
36
|
+
return el('div', { class: `banner ${level}`, role: level === 'err' ? 'alert' : 'status' }, el('strong', {}, level === 'ok' ? 'OK: ' : level === 'warn' ? 'Warning: ' : 'Error: ', title), code ? el('span', { class: 'muted' }, ` [${code}]`) : null, ...lines.map((line) => el('p', {}, line)), diagnostics.length > 0 ? diagnosticsBlock(diagnostics) : null);
|
|
37
|
+
}
|
|
38
|
+
export function diagnosticsBlock(lines) {
|
|
39
|
+
return el('details', {}, el('summary', {}, `Diagnostics (${lines.length})`), el('pre', { class: 'diagnostics' }, lines.join('\n')));
|
|
40
|
+
}
|
|
@@ -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);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { confirmDialog } from './dialog.js';
|
|
2
|
+
import { button, el, replace, when } from './dom.js';
|
|
3
|
+
import { problem } from './reply.js';
|
|
4
|
+
import { short } from './sanitize.js';
|
|
5
|
+
/** History: the last 10 releases, and rollback (to the last healthy release, or one picked here). */
|
|
6
|
+
export function renderHistory(app) {
|
|
7
|
+
const status = app.status;
|
|
8
|
+
const releases = status?.releases ?? [];
|
|
9
|
+
const message = el('div', { aria: { live: 'polite' } });
|
|
10
|
+
const current = releases.find((r) => r.status === 'ok');
|
|
11
|
+
const choices = releases.filter((r) => r.status === 'ok' && r !== current);
|
|
12
|
+
const select = el('select', { id: 'rollback-to' }, el('option', { value: '' }, 'the last healthy release (the deploy decides)'), ...choices.map((r) => el('option', { value: r.sha }, `${short(r.sha)} — ${when(r.at)}`)));
|
|
13
|
+
return el('section', { aria: { labelledby: 'history-title' } }, el('div', { class: 'row' }, el('h2', { id: 'history-title' }, 'History'), button('Refresh', () => { void app.refreshStatus(); })), !status ? el('p', { class: 'muted' }, 'Load the status first.') : null, releases.length > 0 ? table(releases) : status ? el('p', { class: 'muted' }, 'No release recorded yet.') : null, status ? el('div', { class: 'panel' }, el('h3', {}, 'Roll back'), el('div', { class: 'field' }, el('label', { for: 'rollback-to' }, 'Back to'), select), el('p', { class: 'muted' }, 'Code only: the database is not rolled back. The commit it leaves is held: a scheduled run will not redeploy it until the branch moves on.'), button('Roll back…', () => { void rollback(app, select.value, message); }, { class: 'danger' }), message) : null);
|
|
14
|
+
}
|
|
15
|
+
function table(releases) {
|
|
16
|
+
return el('div', { class: 'table-wrap' }, el('table', {}, el('thead', {}, el('tr', {}, ...['Commit', 'When', 'Status', 'Actor', 'Via', 'Client', 'Migrations', 'Backup', 'Note'].map((h) => el('th', {}, h)))), el('tbody', {}, ...releases.map((r) => el('tr', {}, el('td', {}, el('code', {}, short(r.sha))), el('td', {}, when(r.at)), el('td', {}, el('span', { class: r.status === 'ok' ? 'status-ok' : 'status-err' }, r.status)), el('td', {}, r.actor ?? r.user), el('td', {}, r.via ?? ''), el('td', {}, r.client ?? ''), el('td', {}, r.migrations.join(', ')), el('td', {}, r.backup ?? ''), el('td', {}, r.note ?? ''))))));
|
|
17
|
+
}
|
|
18
|
+
async function rollback(app, to, message) {
|
|
19
|
+
const t = app.current;
|
|
20
|
+
if (!t)
|
|
21
|
+
return;
|
|
22
|
+
const ok = await confirmDialog({
|
|
23
|
+
title: `Roll back ${t.name}?`,
|
|
24
|
+
lines: [
|
|
25
|
+
`${t.ssh} · ${t.dir}${app.hello ? ` · host ${app.hello.host}` : ''}`,
|
|
26
|
+
to ? `Back to ${short(to)}.` : 'Back to the last healthy release.',
|
|
27
|
+
'The commit it leaves is held until the branch moves on.',
|
|
28
|
+
],
|
|
29
|
+
confirmLabel: 'Roll back',
|
|
30
|
+
danger: true,
|
|
31
|
+
...(t.protected ? { typeToConfirm: t.name } : {}),
|
|
32
|
+
});
|
|
33
|
+
if (!ok)
|
|
34
|
+
return;
|
|
35
|
+
replace(message, el('p', { class: 'muted' }, 'Starting the rollback…'));
|
|
36
|
+
let reply;
|
|
37
|
+
try {
|
|
38
|
+
reply = await app.api.post('rollback', { target: t.name, confirm: true, confirmedTarget: t, ...(t.protected ? { confirmName: t.name } : {}), ...(to ? { to } : {}) });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
replace(message, problem(undefined, error));
|
|
42
|
+
void app.noticeChange(error);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const result = reply.result;
|
|
46
|
+
if (reply.ok && result?.runId) {
|
|
47
|
+
replace(message);
|
|
48
|
+
app.watch(result.runId, `rollback ${result.runId}`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
replace(message, problem(reply));
|
|
52
|
+
}
|