hopwire 0.7.0 → 0.8.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.
@@ -1,65 +1,118 @@
1
- import { changedSettings, settingValue, unavailable, } from '../settings.js';
1
+ import { shortKey } from '../output.js';
2
+ import { authUser, changedSettings, isOn, settingKind, settingValue, unavailable, } from '../settings.js';
2
3
  import { paint, wrap } from '../terminal.js';
3
4
  import { bold, muted } from './chrome.js';
4
5
  import { hitLine } from './state.js';
5
6
  import { after, clip, fit, onBackground, sides } from './text.js';
6
- /** The rows of the tab, in the order they are shown. */
7
- export function settingRows(settings) {
8
- const one = settings.services.length === 1;
9
- return [
10
- ...settings.services.map((service) => ({
11
- id: `public:${service.name}`,
12
- group: 'Reach',
13
- label: one ? 'Public URL' : `Public URL of ${service.name}`,
14
- what: 'a public HTTPS address through the edge',
15
- })),
16
- {
17
- id: 'share',
18
- group: 'Reach',
19
- label: 'Share with teammates',
20
- what: 'direct P2P: connect and watch with your keys',
21
- },
22
- { id: 'team', group: 'Reach', label: ' Team only', what: 'only machines of your organisation (Team plan)' },
23
- {
24
- id: 'relay',
25
- group: 'Reach',
26
- label: ' Relay',
27
- what: 'through the edge when no direct connection comes about',
28
- },
29
- {
30
- id: 'websocket',
31
- group: 'Recording',
32
- label: 'WebSocket messages',
33
- what: 'both directions, up to 64 KiB per message',
34
- },
35
- {
36
- id: 'agent',
37
- group: 'Recording',
38
- label: 'Coding agents',
39
- what: 'an agent on this machine can read and replay',
40
- },
41
- ];
7
+ const SERVICE_ROWS = [
8
+ ['name', 'Name', 'the label in the log, and what a teammate passes to --service'],
9
+ ['public', 'Public URL', 'a public HTTPS address through the edge'],
10
+ ['subdomain', ' Subdomain', 'a name your organisation reserved; empty takes its fixed URL'],
11
+ ['auth', ' Basic Auth', 'the edge asks public requests for a user and a password'],
12
+ ['logs', 'Log files', 'new entries are shown next to the requests'],
13
+ ];
14
+ const SHARED_ROWS = [
15
+ ['share', 'Share with teammates', 'direct P2P: connect and watch with your keys'],
16
+ ['team', ' Team only', 'only machines of your organisation (Team plan)'],
17
+ ['relay', ' Relay', 'through the edge when no direct connection comes about'],
18
+ ['allow', ' Allowed keys', 'the only keys that get in; none lets in everyone with your keys'],
19
+ ];
20
+ const RECORDING_ROWS = [
21
+ ['websocket', 'WebSocket messages', 'both directions, up to 64 KiB per message'],
22
+ ['agent', 'Coding agents', 'an agent on this machine can read and replay'],
23
+ ];
24
+ /** Everything the tab shows, in order: titles, the rows that can be picked, and lines of facts between them. */
25
+ function items(access) {
26
+ const settings = access.current;
27
+ const list = [];
28
+ const several = settings.services.length > 1;
29
+ settings.services.forEach((service, index) => {
30
+ list.push({ title: several ? `Service ${service.name}` : 'Service' });
31
+ for (const [field, label, what] of SERVICE_ROWS) {
32
+ list.push({ row: { id: `${index}:${field}`, label, what } });
33
+ }
34
+ list.push({ info: `${'Forwards'.padEnd(16)}${access.facts.entries[index] ?? ''} → ${service.target}` });
35
+ const domains = access.facts.customDomains[index] ?? [];
36
+ list.push({
37
+ info: `${'Custom domains'.padEnd(16)}${domains.length === 0 ? 'none' : domains.join(', ')} (managed in your account, on the website)`,
38
+ });
39
+ });
40
+ list.push({ title: 'Teammates' });
41
+ for (const [id, label, what] of SHARED_ROWS) {
42
+ list.push({ row: { id, label, what } });
43
+ }
44
+ list.push({ title: 'Recording' });
45
+ for (const [id, label, what] of RECORDING_ROWS) {
46
+ list.push({ row: { id, label, what } });
47
+ }
48
+ list.push({
49
+ info: `${'Inspector'.padEnd(16)}${access.facts.inspector ?? 'off (--no-inspect)'}`,
50
+ });
51
+ list.push({ info: 'Ports, the target and the inspector are set when the tunnel is started.' });
52
+ return list;
53
+ }
54
+ /** The rows that can be picked, in the order they are shown. */
55
+ export function settingRows(access) {
56
+ return items(access).flatMap((item) => ('row' in item ? [item.row] : []));
57
+ }
58
+ /** The label of a setting without its indent, for sentences; of a service among several with its name. */
59
+ export function settingName(access, id) {
60
+ const label = settingRows(access)
61
+ .find((row) => row.id === id)
62
+ ?.label.trim() ?? id;
63
+ const index = /^(\d+):/.exec(id)?.[1];
64
+ const service = index === undefined ? undefined : access.current.services[Number(index)];
65
+ return access.current.services.length > 1 && service !== undefined ? `${label} of ${service.name}` : label;
66
+ }
67
+ /** The value of a setting as the row shows it. A password is never part of it. */
68
+ export function shownValue(settings, id) {
69
+ const value = settingValue(settings, id);
70
+ switch (settingKind(id)) {
71
+ case 'switch':
72
+ return value === true ? 'on' : 'off';
73
+ case 'auth':
74
+ return typeof value === 'string' ? `on (user ${authUser(value)})` : 'off';
75
+ case 'text':
76
+ return typeof value === 'string' ? value : 'fixed URL';
77
+ default: {
78
+ const list = Array.isArray(value) ? value : [];
79
+ if (id === 'allow') {
80
+ return list.length === 0 ? 'everyone' : list.length === 1 ? '1 key' : `${list.length} keys`;
81
+ }
82
+ return list.length === 0 ? 'none' : list.length === 1 ? '1 file' : `${list.length} files`;
83
+ }
84
+ }
42
85
  }
43
86
  /** What a change does, in the words the user confirms. Turning something on that opens the service says to whom. */
44
87
  export function changeSentence(id, on) {
45
- if (id.startsWith('public:')) {
46
- return on
47
- ? 'Anyone who knows the address can call your service through the Hopwire edge. It needs an account (hopwire login), and the requests pass through the edge.'
48
- : 'The public address stops working. Requests to it are no longer answered by your service.';
49
- }
50
- switch (id) {
88
+ const field = /^\d+:(\w+)$/.exec(id)?.[1];
89
+ switch (field ?? id) {
90
+ case 'public':
91
+ return on
92
+ ? 'Anyone who knows the address can call your service through the Hopwire edge. It needs an account (hopwire login), and the requests pass through the edge.'
93
+ : 'The public address stops working. Requests to it are no longer answered by your service.';
94
+ case 'name':
95
+ return 'The name is the label of the service in the traffic log and the inspector, and what a teammate passes to hopwire connect --service.';
96
+ case 'subdomain':
97
+ return 'The public URL is served under this name. It must be one your organisation reserved in its account; leave the field empty for the fixed URL of your organisation.';
98
+ case 'auth':
99
+ return 'The edge asks every public request for this user and password before it reaches your machine. It protects the public URL only: the entry point on this machine and teammates over P2P are not asked.';
100
+ case 'logs':
101
+ return 'New entries of a log file of your service are shown next to the requests they were written during. Nothing written before is read, and the entries stay on this machine unless you share them.';
51
102
  case 'share':
52
103
  return on
53
104
  ? 'Teammates with your keys can call your service and read the traffic log: headers and bodies as captured, credentials included, earlier runs too. Direct P2P, end-to-end encrypted, no servers of ours.'
54
105
  : 'Teammates lose the connection: nobody can call your service or follow the log any more. Team only and Relay go off with it.';
55
106
  case 'team':
56
107
  return on
57
- ? 'Only machines of the members of your organisation get in (Team plan). The control plane of your account names their keys and gets the key of this machine; your traffic still passes none.'
108
+ ? 'Only machines of the members of your organisation get in (Team plan). The control plane of your account names their keys and gets the key of this machine; your traffic still passes none. The list of allowed keys is emptied: the team is the list.'
58
109
  : 'Anyone with your keys gets in again, not only your organisation. Relay goes off with it.';
59
110
  case 'relay':
60
111
  return on
61
112
  ? 'Teammates whose network allows no direct connection come in through the edge of your account. End-to-end encrypted: the edge passes on bytes it cannot read, and sees addresses, your organisation, keys and byte counts. Turns on Team only.'
62
113
  : 'Teammates connect directly only. One whose network allows no direct connection no longer gets in.';
114
+ case 'allow':
115
+ return "Only the keys on this list may connect and watch. A key is what hopwire id prints on the teammate's machine. With no key on the list, everyone who has your keys gets in.";
63
116
  case 'websocket':
64
117
  return on
65
118
  ? 'The messages of WebSockets are recorded in both directions, up to 64 KiB per message.'
@@ -70,24 +123,108 @@ export function changeSentence(id, on) {
70
123
  : 'hopwire mcp can no longer read or replay the traffic of this tunnel.';
71
124
  }
72
125
  }
73
- /** The choices of the change box. */
74
- export function changeChoices(access, on) {
75
- const turn = on ? 'Turn on' : 'Turn off';
76
- return [
77
- `${turn} for this run`,
78
- access.file.exists ? `${turn} and save to ${access.file.shown}` : `${turn} and create ${access.file.shown}`,
79
- 'Cancel',
126
+ /** The fields a box opens with: what is typed for this setting. */
127
+ export function boxFields(settings, id) {
128
+ const value = settingValue(settings, id);
129
+ switch (settingKind(id)) {
130
+ case 'text':
131
+ return [
132
+ {
133
+ label: id.endsWith(':name') ? 'Name' : 'Subdomain',
134
+ value: typeof value === 'string' ? value : '',
135
+ masked: false,
136
+ },
137
+ ];
138
+ case 'auth':
139
+ return [
140
+ { label: 'User', value: typeof value === 'string' ? authUser(value) : '', masked: false },
141
+ { label: 'Password', value: '', masked: true },
142
+ ];
143
+ case 'list':
144
+ return [{ label: id === 'allow' ? 'Add a key' : 'Add a file', value: '', masked: false }];
145
+ default:
146
+ return [];
147
+ }
148
+ }
149
+ /** The choices of the box for a setting. */
150
+ export function boxChoices(access, id) {
151
+ const settings = access.current;
152
+ const file = access.file.exists ? `save to ${access.file.shown}` : `create ${access.file.shown}`;
153
+ const pair = (label, action, remove) => [
154
+ { label: `${label} for this run`, action, save: false, remove },
155
+ { label: `${label} and ${file}`, action, save: true, remove },
80
156
  ];
157
+ const cancel = { label: 'Cancel', action: 'cancel', save: false };
158
+ const value = settingValue(settings, id);
159
+ switch (settingKind(id)) {
160
+ case 'switch':
161
+ return [...pair(isOn(settings, id) ? 'Turn off' : 'Turn on', 'apply'), cancel];
162
+ case 'text':
163
+ return [...pair('Change', 'apply'), cancel];
164
+ case 'auth':
165
+ return [
166
+ ...pair(value === null ? 'Turn on' : 'Change', 'apply'),
167
+ ...(value === null ? [] : pair('Turn off', 'off')),
168
+ cancel,
169
+ ];
170
+ default: {
171
+ const list = Array.isArray(value) ? value : [];
172
+ return [
173
+ ...pair('Add', 'apply'),
174
+ // A long list is shortened here; the file has all of it
175
+ ...list
176
+ .slice(0, 4)
177
+ .flatMap((entry, index) => pair(`Remove ${id === 'allow' ? shortKey(entry) : clip(entry, 24)}`, 'remove', index)),
178
+ cancel,
179
+ ];
180
+ }
181
+ }
81
182
  }
82
183
  function highlight(look) {
83
184
  const light = look.palette.mark === null;
84
185
  return look.truecolor ? (light ? '48;2;226;232;240' : '48;2;30;41;59') : light ? '48;5;254' : '48;5;237';
85
186
  }
86
- /** The label of a setting without its indent, for sentences. */
87
- export function settingName(settings, id) {
88
- return (settingRows(settings)
89
- .find((row) => row.id === id)
90
- ?.label.trim() ?? id);
187
+ /** The rows of the tab that are not the two lines at its bottom. */
188
+ function rowLines(ui, view, width) {
189
+ const { look } = ui;
190
+ const { palette } = look;
191
+ const settings = view.access.current;
192
+ const list = items(view.access);
193
+ const labels = Math.max(...list.flatMap((item) => ('row' in item ? [item.row.label.length] : []))) + 2;
194
+ const lines = [];
195
+ let index = -1;
196
+ let cursorLine = 0;
197
+ for (const item of list) {
198
+ if ('title' in item) {
199
+ lines.push({ text: '' }, { text: ` ${bold(look, item.title)}` });
200
+ }
201
+ else if ('info' in item) {
202
+ lines.push({ text: ` ${muted(look, item.info)}` });
203
+ }
204
+ else {
205
+ index += 1;
206
+ const { row } = item;
207
+ const reason = unavailable(settings, row.id);
208
+ const shown = shownValue(settings, row.id);
209
+ const on = settingKind(row.id) === 'switch' ? isOn(settings, row.id) : shown !== 'off' && shown !== 'none';
210
+ const off = reason !== null && !isOn(settings, row.id) && settingKind(row.id) !== 'text';
211
+ const value = off
212
+ ? muted(look, '-')
213
+ : on && shown !== 'everyone' && shown !== 'fixed URL'
214
+ ? paint(look, shown, palette.ok, settingKind(row.id) === 'switch')
215
+ : muted(look, shown);
216
+ const current = index === view.cursor;
217
+ if (current) {
218
+ cursorLine = lines.length;
219
+ }
220
+ const text = `${current ? paint(look, '▌', palette.green) : ' '} ${fit(row.label, labels)}${fit(value, 18)}${muted(look, reason ?? row.what)}`;
221
+ lines.push({
222
+ text: current ? onBackground(fit(text, width), highlight(look)) : text,
223
+ hits: hitLine(`setting:${index}`, width),
224
+ });
225
+ }
226
+ }
227
+ return { lines, cursorLine };
91
228
  }
92
229
  export function settingsLines(state, ui, width, height) {
93
230
  const { look } = ui;
@@ -97,44 +234,33 @@ export function settingsLines(state, ui, width, height) {
97
234
  return [];
98
235
  }
99
236
  const settings = view.access.current;
100
- const rows = settingRows(settings);
101
- const labels = Math.max(...rows.map((row) => row.label.length)) + 2;
102
- const lines = [];
103
- let group = '';
104
- rows.forEach((row, index) => {
105
- if (row.group !== group) {
106
- group = row.group;
107
- lines.push({ text: '' }, { text: ` ${bold(look, group)}` });
108
- }
109
- const reason = unavailable(settings, row.id);
110
- const on = settingValue(settings, row.id);
111
- const value = reason !== null && !on ? muted(look, '-') : on ? paint(look, 'on', palette.ok, true) : muted(look, 'off');
112
- const current = index === view.cursor;
113
- const text = `${current ? paint(look, '▌', palette.green) : ' '} ${fit(row.label, labels)}${fit(value, 6)}${muted(look, reason !== null && !on ? reason : row.what)}`;
114
- lines.push({
115
- text: current ? onBackground(fit(text, width), highlight(look)) : text,
116
- hits: hitLine(`setting:${index}`, width),
117
- });
118
- });
119
- const changed = changedSettings(view.atStart, settings).map((id) => `${settingName(settings, id)} ${settingValue(settings, id) ? 'on' : 'off'}`);
237
+ const { lines, cursorLine } = rowLines(ui, view, width);
238
+ // The rows scroll so that the picked one is in view; the two lines at the bottom stand still
239
+ const room = Math.max(height - 2, 1);
240
+ view.top = Math.min(Math.max(Math.min(view.top, cursorLine - 1), cursorLine - room + 2, 0), Math.max(lines.length - room, 0));
241
+ const shown = lines.slice(view.top, view.top + room);
242
+ const changed = changedSettings(view.atStart, settings).map((id) => settingKind(id) === 'switch'
243
+ ? `${settingName(view.access, id)} ${isOn(settings, id) ? 'on' : 'off'}`
244
+ : settingName(view.access, id));
120
245
  const left = ` ${muted(look, 'Changed in this run:')} ${changed.length === 0 ? muted(look, 'nothing') : changed.join(', ')}`;
121
246
  const file = view.access.file.exists
122
247
  ? `${muted(look, 'file:')} ${view.access.file.shown} `
123
248
  : muted(look, `no ${view.access.file.shown} here yet; saving creates it `);
124
- lines.push({ text: '' }, { text: sides(left, file, width) });
125
- if (view.problem !== null && !state.restarting) {
126
- lines.push({ text: '' });
127
- for (const part of wrap(`Not changed: ${view.problem}`, Math.max(width - 2, 20))) {
128
- lines.push({ text: ` ${paint(look, part, palette.err)}` });
129
- }
130
- }
131
- if (state.restarting) {
132
- lines.push({ text: '' }, { text: ` ${paint(look, 'The tunnel is restarting with the new settings…', palette.info)}` });
133
- }
134
- const filled = Array.from({ length: height }, (_, row) => lines[row] ?? { text: '' });
249
+ const status = state.restarting
250
+ ? ` ${paint(look, 'The tunnel is restarting with the new settings…', palette.info)}`
251
+ : view.problem !== null
252
+ ? ` ${paint(look, `Not changed: ${view.problem}`, palette.err)}`
253
+ : lines.length > room
254
+ ? muted(look, ` rows ${view.top + 1} to ${Math.min(view.top + room, lines.length)} of ${lines.length}`)
255
+ : '';
256
+ const filled = [
257
+ ...Array.from({ length: room }, (_, row) => shown[row] ?? { text: '' }),
258
+ { text: status },
259
+ { text: sides(left, file, width) },
260
+ ].slice(0, height);
135
261
  return view.box === null ? filled : withBox(ui, filled, view, width);
136
262
  }
137
- /** The change box over the rows: what the change does, and the choices. */
263
+ /** The change box over the rows: what the change does, what is typed, and the choices. */
138
264
  function withBox(ui, lines, view, width) {
139
265
  const { look } = ui;
140
266
  const { box } = view;
@@ -142,30 +268,55 @@ function withBox(ui, lines, view, width) {
142
268
  return lines;
143
269
  }
144
270
  const settings = view.access.current;
145
- const on = !settingValue(settings, box.id);
146
- const inner = Math.min(Math.max(width - 8, 30), 64);
147
- const title = settingName(settings, box.id);
271
+ const inner = Math.min(Math.max(width - 8, 30), 70);
272
+ const title = settingName(view.access, box.id);
273
+ const kind = settingKind(box.id);
148
274
  const text = [
149
- ...wrap(changeSentence(box.id, on), inner - 2),
275
+ ...wrap(changeSentence(box.id, kind === 'switch' ? !isOn(settings, box.id) : true), inner - 2),
276
+ ...(box.note === null ? [] : wrap(box.note, inner - 2)),
150
277
  ...wrap('The tunnel restarts for it; a request that arrives in that moment fails.', inner - 2),
151
278
  ];
152
279
  const bar = muted(look, '│');
280
+ const blank = { text: `${bar}${' '.repeat(inner)}${bar}` };
281
+ const fields = box.fields.map((field, index) => {
282
+ const focused = index === box.focus;
283
+ const shown = field.masked ? '•'.repeat([...field.value].length) : field.value;
284
+ const cursor = focused ? paint(look, '▏', look.palette.green) : '';
285
+ const label = fit(`${field.label}:`, 12);
286
+ const room = inner - 16;
287
+ // The end of a long text is what is being typed
288
+ const tail = [...shown].slice(-room).join('');
289
+ const line = ` ${focused ? paint(look, '▸', look.palette.green, true) : ' '} ${focused ? bold(look, label) : label}${fit(`${tail}${cursor}`, room)} `;
290
+ return {
291
+ text: `${bar}${focused ? onBackground(line, highlight(look)) : line}${bar}`,
292
+ hits: [{ from: 0, to: inner + 2, id: `field:${index}` }],
293
+ };
294
+ });
295
+ const problem = box.problem === null
296
+ ? []
297
+ : wrap(box.problem, inner - 2).map((part) => ({
298
+ text: `${bar} ${fit(paint(look, part, look.palette.err), inner - 2)} ${bar}`,
299
+ }));
300
+ const choices = boxChoices(view.access, box.id).map((choice, index) => {
301
+ const current = box.fields.length + index === box.focus;
302
+ const label = ` ${current ? paint(look, '▸', look.palette.green, true) : ' '} ${fit(choice.label, inner - 4)} `;
303
+ return {
304
+ text: `${bar}${current ? onBackground(label, highlight(look)) : label}${bar}`,
305
+ hits: [{ from: 0, to: inner + 2, id: `choice:${index}` }],
306
+ };
307
+ });
153
308
  const content = [
154
309
  { text: muted(look, `┌ ${title} ${'─'.repeat(Math.max(inner - title.length - 2, 0))}┐`) },
155
310
  ...text.map((part) => ({ text: `${bar} ${fit(part, inner - 2)} ${bar}` })),
156
- { text: `${bar}${' '.repeat(inner)}${bar}` },
157
- ...changeChoices(view.access, on).map((choice, index) => {
158
- const current = index === box.choice;
159
- const label = ` ${current ? paint(look, '▸', look.palette.green, true) : ' '} ${fit(choice, inner - 4)} `;
160
- return {
161
- text: `${bar}${current ? onBackground(label, highlight(look)) : label}${bar}`,
162
- hits: [{ from: 0, to: inner + 2, id: `choice:${index}` }],
163
- };
164
- }),
311
+ ...(fields.length > 0 ? [blank, ...fields] : []),
312
+ ...problem,
313
+ blank,
314
+ ...choices,
165
315
  { text: muted(look, `└${'─'.repeat(inner)}┘`) },
166
316
  ];
167
317
  const x = Math.max(Math.min(4, width - inner - 2), 0);
168
- const y = Math.max(Math.min(2, lines.length - content.length), 0);
318
+ // A box taller than the tab keeps its end in view: the choices are what is acted on
319
+ const y = Math.min(1, lines.length - content.length);
169
320
  return lines.map((line, row) => {
170
321
  const part = content[row - y];
171
322
  if (part === undefined) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hopwire",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "mcpName": "to.hopwire/hopwire",
5
5
  "description": "Record, share and replay the HTTP traffic of your local services: private P2P sharing with teammates, and public HTTPS URLs.",
6
6
  "license": "Apache-2.0",