hopwire 0.4.0 → 0.5.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/dist/bin.js +143 -5
- package/dist/cli.js +20 -6
- package/dist/commands/chaos.js +18 -18
- package/dist/help.js +279 -0
- package/dist/signature.js +83 -34
- package/dist/terminal.js +37 -9
- package/dist/tui/app.js +748 -0
- package/dist/tui/chrome.js +222 -0
- package/dist/tui/input.js +82 -0
- package/dist/tui/model.js +174 -0
- package/dist/tui/requests.js +271 -0
- package/dist/tui/screen.js +91 -0
- package/dist/tui/state.js +88 -0
- package/dist/tui/tabs.js +239 -0
- package/dist/tui/text.js +182 -0
- package/package.json +1 -1
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { paint } from '../terminal.js';
|
|
2
|
+
import { shortKey } from '../output.js';
|
|
3
|
+
import { TABS, TAB_KEYS, TAB_TITLES, listWindow, target } from './state.js';
|
|
4
|
+
import { clip, sides, width as widthOf } from './text.js';
|
|
5
|
+
/**
|
|
6
|
+
* What stands around every tab: the two lines of the header (what runs, and the navigation), and the two lines of
|
|
7
|
+
* the footer (what is going on, and the keys of the view that is shown).
|
|
8
|
+
*/
|
|
9
|
+
const DOT = '●';
|
|
10
|
+
export const muted = (look, text) => paint(look, text, look.palette.muted);
|
|
11
|
+
export const bold = (look, text) => paint(look, text, null, true);
|
|
12
|
+
/** A line across the window. */
|
|
13
|
+
export function rule(look, width) {
|
|
14
|
+
return { text: muted(look, '─'.repeat(width)) };
|
|
15
|
+
}
|
|
16
|
+
/** Pieces of a line one after the other, each with what a click on it does. */
|
|
17
|
+
export class LineBuilder {
|
|
18
|
+
text = '';
|
|
19
|
+
columns = 0;
|
|
20
|
+
hits = [];
|
|
21
|
+
add(text, hit) {
|
|
22
|
+
const columns = widthOf(text);
|
|
23
|
+
if (hit !== undefined && columns > 0) {
|
|
24
|
+
this.hits.push({ from: this.columns, to: this.columns + columns, id: hit });
|
|
25
|
+
}
|
|
26
|
+
this.text += text;
|
|
27
|
+
this.columns += columns;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
get width() {
|
|
31
|
+
return this.columns;
|
|
32
|
+
}
|
|
33
|
+
line() {
|
|
34
|
+
return { text: this.text, hits: this.hits };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** What runs, in a few words: the entry point and where it leads, or the log that is watched. */
|
|
38
|
+
function running(state, look) {
|
|
39
|
+
const { info } = state;
|
|
40
|
+
const line = new LineBuilder();
|
|
41
|
+
line.add(`${paint(look, DOT, look.palette.green)} ${bold(look, 'hopwire')} `);
|
|
42
|
+
if (info.kind === 'watch') {
|
|
43
|
+
line.add(`watching ${shortKey(info.logKey)}`);
|
|
44
|
+
line.add(muted(look, info.replayTarget === null ? ' no replay target' : ` replays go to ${info.replayTarget}`));
|
|
45
|
+
}
|
|
46
|
+
else if (info.services.length === 1) {
|
|
47
|
+
const [service] = info.services;
|
|
48
|
+
const target = service?.target ?? state.facts.fact('Forwarding')?.split(' -> ')[1] ?? '';
|
|
49
|
+
line.add(service?.url ?? '', `copy:${service?.url ?? ''}`);
|
|
50
|
+
line.add(muted(look, ` → ${target.replace(/^https?:\/\//, '')}`));
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
line.add(`${info.services.length} services`);
|
|
54
|
+
}
|
|
55
|
+
if (info.publicUrl !== null) {
|
|
56
|
+
line.add(' ').add(paint(look, info.publicUrl, look.palette.cyan), `open:${info.publicUrl}`);
|
|
57
|
+
}
|
|
58
|
+
return line;
|
|
59
|
+
}
|
|
60
|
+
/** The states of the tunnel as dots with a word each. */
|
|
61
|
+
function states(state, look) {
|
|
62
|
+
const { palette } = look;
|
|
63
|
+
const parts = [];
|
|
64
|
+
const dot = (tone, word) => {
|
|
65
|
+
parts.push(`${paint(look, DOT, tone)} ${word}`);
|
|
66
|
+
};
|
|
67
|
+
if (state.facts.edge !== null) {
|
|
68
|
+
if (state.facts.edge) {
|
|
69
|
+
dot(palette.blue, 'public');
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
dot(palette.err, paint(look, 'edge offline', palette.err));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (state.info.kind !== 'watch' && state.info.peerKey !== null) {
|
|
76
|
+
const count = state.facts.teammateCount;
|
|
77
|
+
dot(palette.cyan, count === 1 ? '1 teammate' : `${count} teammates`);
|
|
78
|
+
}
|
|
79
|
+
if (state.info.agent) {
|
|
80
|
+
dot(palette.green, 'agent');
|
|
81
|
+
}
|
|
82
|
+
return parts.join(' ');
|
|
83
|
+
}
|
|
84
|
+
/** The first line: what runs on the left, its states on the right. */
|
|
85
|
+
export function headerLine(state, ui, width) {
|
|
86
|
+
const left = running(state, ui.look).line();
|
|
87
|
+
return { text: sides(left.text, states(state, ui.look), width), hits: left.hits };
|
|
88
|
+
}
|
|
89
|
+
/** The navigation: every tab by its key and name, the current one set apart. */
|
|
90
|
+
export function tabBar(state, ui) {
|
|
91
|
+
const { look } = ui;
|
|
92
|
+
const line = new LineBuilder().add(' ');
|
|
93
|
+
for (const tab of TABS) {
|
|
94
|
+
const current = tab === state.tab;
|
|
95
|
+
const problem = tab === 'status' && state.facts.unseenProblems > 0;
|
|
96
|
+
const label = `${TAB_KEYS[tab]} ${TAB_TITLES[tab]}`;
|
|
97
|
+
line.add(current
|
|
98
|
+
? `\u001b[4m${paint(look, label, look.palette.green, true)}\u001b[0m`
|
|
99
|
+
: `${muted(look, TAB_KEYS[tab])} ${TAB_TITLES[tab]}`, `tab:${tab}`);
|
|
100
|
+
line.add(problem ? ` ${paint(look, DOT, look.palette.warn)} ` : ' ');
|
|
101
|
+
}
|
|
102
|
+
return line.line();
|
|
103
|
+
}
|
|
104
|
+
/** How long a line about something that just happened stays in the footer. */
|
|
105
|
+
export const FLASH_MS = 6000;
|
|
106
|
+
/** At most one hint, chosen by what just happened. */
|
|
107
|
+
function hint(state, ui) {
|
|
108
|
+
const { look } = ui;
|
|
109
|
+
if (state.facts.edge === false) {
|
|
110
|
+
return paint(look, 'The edge connection is lost; reconnecting', look.palette.warn);
|
|
111
|
+
}
|
|
112
|
+
const newest = state.traffic.entries.findLast((entry) => entry.exchange.source !== 'replay');
|
|
113
|
+
if (newest !== undefined && state.tab === 'requests') {
|
|
114
|
+
const check = ui.signature(newest);
|
|
115
|
+
if (check?.result === 'invalid') {
|
|
116
|
+
return paint(look, `Wrong ${check.provider} signature: check ${check.variable}`, look.palette.warn);
|
|
117
|
+
}
|
|
118
|
+
if (newest.exchange.response.status >= 500 && !state.used.has('actions')) {
|
|
119
|
+
return `${bold(look, 'space')} ${muted(look, 'replay or fix the failed request')}`;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (state.info.kind !== 'watch' && state.info.peerKey !== null && !state.used.has('share')) {
|
|
123
|
+
return `${bold(look, '2')} ${muted(look, 'Share has the keys for your teammate')}`;
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
/** The line above the keys: what came in on the left; on the right what just happened, or one hint. */
|
|
128
|
+
export function statusLine(state, ui, width, bodyHeight) {
|
|
129
|
+
const { look } = ui;
|
|
130
|
+
const { palette } = look;
|
|
131
|
+
if (state.typing) {
|
|
132
|
+
return {
|
|
133
|
+
text: ` ${bold(look, '/')} ${state.filter}${paint(look, '▏', palette.green)} ${muted(look, 'enter keeps the filter · esc clears it')}`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const { traffic } = state;
|
|
137
|
+
const parts = [traffic.requests === 1 ? '1 request' : `${traffic.requests} requests`];
|
|
138
|
+
if (traffic.failed > 0) {
|
|
139
|
+
parts.push(paint(look, `${traffic.failed} failed`, palette.warn));
|
|
140
|
+
}
|
|
141
|
+
if (state.tab === 'requests' && state.detail === null) {
|
|
142
|
+
const { shown, below } = listWindow(state, bodyHeight);
|
|
143
|
+
if (state.filter.trim() !== '' || state.failedOnly) {
|
|
144
|
+
const what = [
|
|
145
|
+
...(state.failedOnly ? ['failed only'] : []),
|
|
146
|
+
...(state.filter.trim() !== '' ? [`"${state.filter.trim()}"`] : []),
|
|
147
|
+
].join(', ');
|
|
148
|
+
parts.push(paint(look, `filter ${what}: ${shown.length} shown`, palette.info));
|
|
149
|
+
}
|
|
150
|
+
if (below > 0) {
|
|
151
|
+
parts.push(paint(look, `${below} newer below, end follows them`, palette.green));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const left = ` ${parts.join(muted(look, ' · '))}`;
|
|
155
|
+
const flash = state.flash !== null && ui.now() - state.flash.at < FLASH_MS
|
|
156
|
+
? `${paint(look, '›', state.flash.problem ? palette.err : palette.green, true)} ${state.flash.problem ? paint(look, state.flash.text, palette.err) : state.flash.text}`
|
|
157
|
+
: null;
|
|
158
|
+
const right = state.stopping ? muted(look, 'Stopping…') : (flash ?? hint(state, ui) ?? '');
|
|
159
|
+
// What just happened matters more than the counts: it gets the room it needs
|
|
160
|
+
const rightWidth = Math.min(widthOf(right), Math.max(width - 12, 0));
|
|
161
|
+
return { text: sides(left, `${clip(right, rightWidth)} `, width) };
|
|
162
|
+
}
|
|
163
|
+
/** The keys of the view that is shown: [key, what it does, the key a click on it presses]. */
|
|
164
|
+
export function keysOf(state) {
|
|
165
|
+
if (state.typing) {
|
|
166
|
+
return [
|
|
167
|
+
['enter', 'keep', 'enter'],
|
|
168
|
+
['esc', 'clear', 'esc'],
|
|
169
|
+
];
|
|
170
|
+
}
|
|
171
|
+
if (state.menu !== null) {
|
|
172
|
+
return [
|
|
173
|
+
['↑↓', 'select', 'down'],
|
|
174
|
+
['enter', 'run', 'enter'],
|
|
175
|
+
['esc', 'close', 'esc'],
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
const quit = ['q', 'quit', 'q'];
|
|
179
|
+
switch (state.tab) {
|
|
180
|
+
case 'requests':
|
|
181
|
+
if (state.detail !== null) {
|
|
182
|
+
return [
|
|
183
|
+
['tab', 'next section', 'tab'],
|
|
184
|
+
['↑↓', 'scroll', 'down'],
|
|
185
|
+
['space', 'actions', 'space'],
|
|
186
|
+
['r', 'replay', 'r'],
|
|
187
|
+
['esc', 'back', 'esc'],
|
|
188
|
+
quit,
|
|
189
|
+
];
|
|
190
|
+
}
|
|
191
|
+
return [
|
|
192
|
+
['↑↓', 'select', 'up'],
|
|
193
|
+
['enter', 'open', 'enter'],
|
|
194
|
+
['space', 'actions', 'space'],
|
|
195
|
+
['/', 'filter', '/'],
|
|
196
|
+
['e', state.failedOnly ? 'show all' : 'failed only', 'e'],
|
|
197
|
+
...(state.selected !== null || state.top !== null
|
|
198
|
+
? [['end', 'follow newest', 'end']]
|
|
199
|
+
: []),
|
|
200
|
+
quit,
|
|
201
|
+
];
|
|
202
|
+
case 'share':
|
|
203
|
+
case 'tools':
|
|
204
|
+
return [['↑↓', 'select', 'down'], ['enter', 'copy', 'enter'], quit];
|
|
205
|
+
default:
|
|
206
|
+
return [['↑↓', 'scroll', 'down'], quit];
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/** The last line: the keys, each of them clickable. */
|
|
210
|
+
export function keysLine(state, ui) {
|
|
211
|
+
const { look } = ui;
|
|
212
|
+
const line = new LineBuilder().add(' ');
|
|
213
|
+
for (const [key, what, press] of keysOf(state)) {
|
|
214
|
+
line.add(`${bold(look, key)} ${muted(look, what)}`, `key:${press}`).add(' ');
|
|
215
|
+
}
|
|
216
|
+
return line.line();
|
|
217
|
+
}
|
|
218
|
+
/** Whether a request is the target of the keys, for the views that name it. */
|
|
219
|
+
export function targetTitle(state) {
|
|
220
|
+
const entry = target(state);
|
|
221
|
+
return entry === null ? null : `${entry.exchange.request.method} ${entry.exchange.request.path}`;
|
|
222
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the terminal sends while the full-screen view runs, as events: a key by its name, a mouse button pressed at a
|
|
3
|
+
* cell, the wheel turned. The terminal is in raw mode and reports the mouse in the SGR format (1006).
|
|
4
|
+
*/
|
|
5
|
+
/** Keys that arrive as an escape sequence, by what follows ESC [ or ESC O. */
|
|
6
|
+
const SEQUENCES = {
|
|
7
|
+
A: 'up',
|
|
8
|
+
B: 'down',
|
|
9
|
+
C: 'right',
|
|
10
|
+
D: 'left',
|
|
11
|
+
H: 'home',
|
|
12
|
+
F: 'end',
|
|
13
|
+
Z: 'shift-tab',
|
|
14
|
+
'1~': 'home',
|
|
15
|
+
'4~': 'end',
|
|
16
|
+
'5~': 'pageup',
|
|
17
|
+
'6~': 'pagedown',
|
|
18
|
+
'7~': 'home',
|
|
19
|
+
'8~': 'end',
|
|
20
|
+
'3~': 'delete',
|
|
21
|
+
};
|
|
22
|
+
const CONTROL = {
|
|
23
|
+
'\r': 'enter',
|
|
24
|
+
'\n': 'enter',
|
|
25
|
+
'\t': 'tab',
|
|
26
|
+
'\u007f': 'backspace',
|
|
27
|
+
'\b': 'backspace',
|
|
28
|
+
'\u0003': 'ctrl-c',
|
|
29
|
+
'\u0004': 'ctrl-d',
|
|
30
|
+
'\u001b': 'esc',
|
|
31
|
+
' ': 'space',
|
|
32
|
+
};
|
|
33
|
+
// eslint-disable-next-line no-control-regex -- escape sequences start with ESC
|
|
34
|
+
const MOUSE = /^\u001b\[<(\d+);(\d+);(\d+)([Mm])/;
|
|
35
|
+
// eslint-disable-next-line no-control-regex
|
|
36
|
+
const CSI = /^\u001b(?:\[|O)([0-9;]*[A-Za-z~])/;
|
|
37
|
+
/** The events in one chunk of input. Cells count from 0. */
|
|
38
|
+
export function parseInput(chunk) {
|
|
39
|
+
const events = [];
|
|
40
|
+
let rest = chunk;
|
|
41
|
+
while (rest.length > 0) {
|
|
42
|
+
const mouse = MOUSE.exec(rest);
|
|
43
|
+
if (mouse !== null) {
|
|
44
|
+
const button = Number(mouse[1]);
|
|
45
|
+
const x = Number(mouse[2]) - 1;
|
|
46
|
+
const y = Number(mouse[3]) - 1;
|
|
47
|
+
if ((button & 64) !== 0) {
|
|
48
|
+
events.push({ type: 'wheel', step: (button & 1) === 0 ? -1 : 1, x, y });
|
|
49
|
+
}
|
|
50
|
+
else if (mouse[4] === 'M' && (button & 3) === 0 && (button & 32) === 0) {
|
|
51
|
+
// The left button going down; releases, other buttons and drags are not used
|
|
52
|
+
events.push({ type: 'click', x, y });
|
|
53
|
+
}
|
|
54
|
+
rest = rest.slice(mouse[0].length);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const sequence = CSI.exec(rest);
|
|
58
|
+
if (sequence !== null) {
|
|
59
|
+
const key = SEQUENCES[sequence[1] ?? ''];
|
|
60
|
+
if (key !== undefined) {
|
|
61
|
+
events.push({ type: 'key', key });
|
|
62
|
+
}
|
|
63
|
+
rest = rest.slice(sequence[0].length);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
// Alt or Option with a key arrives as ESC and the key: it is no Esc and no key of the view
|
|
67
|
+
if (rest.charCodeAt(0) === 0x1b && rest.length > 1) {
|
|
68
|
+
rest = rest.slice(1 + String.fromCodePoint(rest.codePointAt(1) ?? 0).length);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const character = String.fromCodePoint(rest.codePointAt(0) ?? 0);
|
|
72
|
+
const control = CONTROL[character];
|
|
73
|
+
if (control !== undefined) {
|
|
74
|
+
events.push({ type: 'key', key: control });
|
|
75
|
+
}
|
|
76
|
+
else if ((character.codePointAt(0) ?? 0) >= 0x20) {
|
|
77
|
+
events.push({ type: 'key', key: character });
|
|
78
|
+
}
|
|
79
|
+
rest = rest.slice(character.length);
|
|
80
|
+
}
|
|
81
|
+
return events;
|
|
82
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { mainPublicUrl, PEER_LABEL } from '../output.js';
|
|
2
|
+
/** What the view needs of a running tunnel; null for commands that show no exchanges. */
|
|
3
|
+
export function tunnelInfo(running) {
|
|
4
|
+
switch (running.kind) {
|
|
5
|
+
case 'http':
|
|
6
|
+
return {
|
|
7
|
+
kind: 'http',
|
|
8
|
+
services: [{ name: '', url: running.url, target: null, publicUrl: running.publicUrl }],
|
|
9
|
+
inspectorUrl: running.inspectorUrl,
|
|
10
|
+
publicUrl: running.publicUrl,
|
|
11
|
+
logKey: running.logKey,
|
|
12
|
+
peerKey: running.peerKey,
|
|
13
|
+
agent: running.agentSocket !== null,
|
|
14
|
+
replayTarget: null,
|
|
15
|
+
};
|
|
16
|
+
case 'up': {
|
|
17
|
+
const published = running.services.find((service) => service.publicUrl !== null);
|
|
18
|
+
return {
|
|
19
|
+
kind: 'up',
|
|
20
|
+
services: running.services.map((service) => ({
|
|
21
|
+
name: service.name,
|
|
22
|
+
url: service.url,
|
|
23
|
+
target: service.target,
|
|
24
|
+
publicUrl: mainPublicUrl(service.publicUrl, service.customDomains),
|
|
25
|
+
})),
|
|
26
|
+
inspectorUrl: running.inspectorUrl,
|
|
27
|
+
publicUrl: published === undefined ? null : mainPublicUrl(published.publicUrl, published.customDomains),
|
|
28
|
+
logKey: running.logKey,
|
|
29
|
+
peerKey: running.peerKey,
|
|
30
|
+
agent: running.agentSocket !== null,
|
|
31
|
+
replayTarget: null,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
case 'watch':
|
|
35
|
+
return {
|
|
36
|
+
kind: 'watch',
|
|
37
|
+
services: [],
|
|
38
|
+
inspectorUrl: running.inspectorUrl,
|
|
39
|
+
publicUrl: null,
|
|
40
|
+
logKey: running.logKey,
|
|
41
|
+
peerKey: running.peerKey,
|
|
42
|
+
agent: running.agentSocket !== null,
|
|
43
|
+
replayTarget: running.replayTarget,
|
|
44
|
+
};
|
|
45
|
+
default:
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** Width of a label and its padding: every line of facts starts with one. */
|
|
50
|
+
const LABEL_WIDTH = 13;
|
|
51
|
+
/** Labels whose line is a warning. */
|
|
52
|
+
const WARNING_LABELS = new Set(['Notice', 'Limit', 'Drift']);
|
|
53
|
+
/** Things that happened are kept up to this many; older ones are let go. */
|
|
54
|
+
const HAPPENINGS = 300;
|
|
55
|
+
// eslint-disable-next-line no-control-regex -- colour and link sequences start with ESC
|
|
56
|
+
const ANSI = /\u001b\[[\d;?]*[A-Za-z]|\u001b\]8;;[^\u001b]*\u001b\\/g;
|
|
57
|
+
/** The facts of the start and what happened since, read from the lines the command prints. */
|
|
58
|
+
export class Facts {
|
|
59
|
+
now;
|
|
60
|
+
facts = [];
|
|
61
|
+
happenings = [];
|
|
62
|
+
/** Whether the connection to the edge stands; null for a tunnel without a public URL */
|
|
63
|
+
edge = null;
|
|
64
|
+
started = false;
|
|
65
|
+
/** Per teammate (the short form of their key): replications of the log and streams of hopwire connect */
|
|
66
|
+
teammates = new Map();
|
|
67
|
+
/** Problems since the Status tab was last looked at */
|
|
68
|
+
unseenProblems = 0;
|
|
69
|
+
constructor(now = Date.now) {
|
|
70
|
+
this.now = now;
|
|
71
|
+
}
|
|
72
|
+
/** The tunnel runs: from now on a line is something that happened, not a fact of the start. */
|
|
73
|
+
start() {
|
|
74
|
+
this.started = true;
|
|
75
|
+
}
|
|
76
|
+
/** One printed line, or several joined by newlines. Returns what happened, for the view to show at once. */
|
|
77
|
+
line(text) {
|
|
78
|
+
const happened = [];
|
|
79
|
+
for (const raw of text.split('\n')) {
|
|
80
|
+
const line = raw.replace(ANSI, '');
|
|
81
|
+
if (line.trim() === '') {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const label = /^[A-Z][A-Za-z]*(?: [A-Za-z]+)? +(?=\S)/.exec(line)?.[0];
|
|
85
|
+
const name = label !== undefined && label.length === LABEL_WIDTH ? label.trimEnd() : '';
|
|
86
|
+
const value = name === '' ? line.trim() : line.slice(LABEL_WIDTH);
|
|
87
|
+
this.observe(line);
|
|
88
|
+
if (!this.started) {
|
|
89
|
+
// The next steps of the start are the Share tab; the facts are kept
|
|
90
|
+
if (name !== '') {
|
|
91
|
+
this.facts.push({ label: name, value, warning: WARNING_LABELS.has(name) });
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const problem = WARNING_LABELS.has(name) || /connection lost|could not|failed|no longer|refused/i.test(value);
|
|
96
|
+
happened.push(this.happen(name, value, problem));
|
|
97
|
+
}
|
|
98
|
+
return happened;
|
|
99
|
+
}
|
|
100
|
+
/** A line of standard error while the tunnel runs. */
|
|
101
|
+
problem(text) {
|
|
102
|
+
return this.happen('', text.replace(ANSI, '').trim(), true);
|
|
103
|
+
}
|
|
104
|
+
/** Something the view itself wants on record (a replay and what changed). */
|
|
105
|
+
note(text) {
|
|
106
|
+
return this.happen('', text, false);
|
|
107
|
+
}
|
|
108
|
+
/** Teammates who watch or are connected now. */
|
|
109
|
+
get teammateCount() {
|
|
110
|
+
return this.teammatesNow.length;
|
|
111
|
+
}
|
|
112
|
+
/** Who is here now, by the start and end of their machine's key, and what they do. */
|
|
113
|
+
get teammatesNow() {
|
|
114
|
+
return [...this.teammates]
|
|
115
|
+
.filter(([, here]) => here.watching > 0 || here.connected > 0)
|
|
116
|
+
.map(([key, here]) => ({ key, watching: here.watching > 0, connected: here.connected > 0 }));
|
|
117
|
+
}
|
|
118
|
+
/** The value of the first fact with this label. */
|
|
119
|
+
fact(label) {
|
|
120
|
+
return this.facts.find((fact) => fact.label === label)?.value ?? null;
|
|
121
|
+
}
|
|
122
|
+
happen(label, text, problem) {
|
|
123
|
+
const happening = { at: this.now(), label, text, problem };
|
|
124
|
+
this.happenings.push(happening);
|
|
125
|
+
if (this.happenings.length > HAPPENINGS) {
|
|
126
|
+
this.happenings.shift();
|
|
127
|
+
}
|
|
128
|
+
if (problem) {
|
|
129
|
+
this.unseenProblems += 1;
|
|
130
|
+
}
|
|
131
|
+
return happening;
|
|
132
|
+
}
|
|
133
|
+
observe(line) {
|
|
134
|
+
if (line.startsWith('Public URL ')) {
|
|
135
|
+
this.edge = true;
|
|
136
|
+
}
|
|
137
|
+
else if (line.startsWith('Edge connection lost')) {
|
|
138
|
+
this.edge = false;
|
|
139
|
+
}
|
|
140
|
+
else if (line.startsWith('Edge connected again')) {
|
|
141
|
+
this.edge = true;
|
|
142
|
+
}
|
|
143
|
+
else if (line.startsWith(PEER_LABEL)) {
|
|
144
|
+
const match = /^\S+ +(\S+) (is watching|stopped watching|connected|disconnected)/.exec(line);
|
|
145
|
+
if (match !== null) {
|
|
146
|
+
const here = this.teammates.get(match[1] ?? '') ?? { watching: 0, connected: 0 };
|
|
147
|
+
const kind = match[2] === 'is watching' || match[2] === 'stopped watching' ? 'watching' : 'connected';
|
|
148
|
+
const step = match[2] === 'is watching' || match[2] === 'connected' ? 1 : -1;
|
|
149
|
+
here[kind] = Math.max(here[kind] + step, 0);
|
|
150
|
+
this.teammates.set(match[1] ?? '', here);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** Exchanges kept for the list; when the limit is reached the oldest fifth is let go at once. */
|
|
156
|
+
const KEPT = 5000;
|
|
157
|
+
export class Traffic {
|
|
158
|
+
entries = [];
|
|
159
|
+
requests = 0;
|
|
160
|
+
failed = 0;
|
|
161
|
+
/** Adds an exchange; returns how many of the oldest entries were let go to make room. */
|
|
162
|
+
add(exchange, label) {
|
|
163
|
+
this.entries.push({ exchange, label });
|
|
164
|
+
// A replay is no request that came in
|
|
165
|
+
if (exchange.source !== 'replay') {
|
|
166
|
+
this.requests += 1;
|
|
167
|
+
this.failed += exchange.response.status >= 400 ? 1 : 0;
|
|
168
|
+
}
|
|
169
|
+
return this.entries.length > KEPT ? this.entries.splice(0, KEPT / 5) : [];
|
|
170
|
+
}
|
|
171
|
+
find(id) {
|
|
172
|
+
return this.entries.find((entry) => entry.exchange.id === id) ?? null;
|
|
173
|
+
}
|
|
174
|
+
}
|