hopwire 0.6.0 → 0.8.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/dist/tui/app.js CHANGED
@@ -1,13 +1,14 @@
1
1
  import { curlCommand } from '../curl.js';
2
2
  import { printable } from '../output.js';
3
3
  import { formatDuration, paint } from '../terminal.js';
4
+ import { recogniseWebhook } from '../webhook.js';
4
5
  import { FLASH_MS, headerLine, keysLine, rule, statusLine, tabBar } from './chrome.js';
5
6
  import { parseInput } from './input.js';
6
7
  import { Traffic } from './model.js';
7
8
  import { menuItems, requestsLines } from './requests.js';
8
9
  import { TAB_KEYS, listWindow, sectionsOf, tabsOf, target, } from './state.js';
9
- import { settingValue, unavailable, withSetting } from '../settings.js';
10
- import { changeChoices, settingName, settingRows, settingsLines } from './settings.js';
10
+ import { invalidAuth, invalidValue, isOn, settingKind, settingValue, unavailable, withSetting, } from '../settings.js';
11
+ import { boxChoices, boxFields, settingName, settingRows, settingsLines, shownValue, } from './settings.js';
11
12
  import { helpContent, scrolled, shareItems, shareLines, statusContent, tools, toolsLines } from './tabs.js';
12
13
  /** Rows of the window that are not the tab: two of the header, two rules, two of the footer. */
13
14
  const CHROME_ROWS = 6;
@@ -45,6 +46,7 @@ export class App {
45
46
  atStart: options.settings.current,
46
47
  history: [],
47
48
  cursor: 0,
49
+ top: 0,
48
50
  problem: null,
49
51
  box: null,
50
52
  },
@@ -53,6 +55,9 @@ export class App {
53
55
  flash: null,
54
56
  stopping: false,
55
57
  used: new Set(),
58
+ fixed: new Set(),
59
+ providers: new Set(),
60
+ startedAt: options.ui.now(),
56
61
  extraArgs: options.extraArgs ?? '',
57
62
  };
58
63
  }
@@ -144,7 +149,23 @@ export class App {
144
149
  }
145
150
  }
146
151
  if (original !== null) {
147
- this.flash(replayNote(original.exchange, exchange));
152
+ const failed = original.exchange.response.status >= 400;
153
+ if (failed && exchange.response.status < 400 && !state.fixed.has(original.exchange.id)) {
154
+ // The moment the work was for: the failed request passes
155
+ state.fixed.add(original.exchange.id);
156
+ const tries = state.traffic.entries.filter((entry) => entry.exchange.replayOf === original.exchange.id).length;
157
+ this.flash(fixedNote(original.exchange, exchange, tries));
158
+ }
159
+ else {
160
+ this.flash(replayNote(original.exchange, exchange));
161
+ }
162
+ }
163
+ else if (exchange.source !== 'replay') {
164
+ const webhook = recogniseWebhook(exchange);
165
+ if (webhook !== null && !state.providers.has(webhook.provider)) {
166
+ state.providers.add(webhook.provider);
167
+ this.flash(`First ${providerName(webhook.provider)} webhook of this run${webhook.event === null ? '' : `: ${printable(webhook.event)}`}`);
168
+ }
148
169
  }
149
170
  this.invalidate();
150
171
  }
@@ -168,13 +189,30 @@ export class App {
168
189
  /** What the terminal shows after the view is gone: what ran and what came in. */
169
190
  summary() {
170
191
  const { look } = this.ui;
171
- const { traffic, version } = this.state;
192
+ const { traffic, version, fixed, startedAt } = this.state;
193
+ const muted = (text) => paint(look, text, look.palette.muted);
172
194
  const requests = traffic.requests === 1 ? '1 request' : `${traffic.requests} requests`;
173
195
  const failed = traffic.failed > 0 ? `, ${traffic.failed} failed` : '';
174
- return [
175
- `${paint(look, '●', look.palette.muted)} ${paint(look, 'hopwire', null, true)} ${paint(look, version, look.palette.muted)} stopped after ${requests}${failed}`,
176
- paint(look, ' The traffic is kept: the same command shows it again in the inspector.', look.palette.muted),
196
+ const lines = [
197
+ `${paint(look, '●', look.palette.muted)} ${paint(look, 'hopwire', null, true)} ${muted(version)} stopped after ${requests}${failed}${muted(` · ran ${formatRun(this.ui.now() - startedAt)}`)}`,
177
198
  ];
199
+ const recap = runRecap(traffic.entries.map((entry) => entry.exchange));
200
+ const fact = (label, text) => {
201
+ lines.push(` ${muted(label.padEnd(9))}${text}`);
202
+ };
203
+ if (recap.busiest !== null) {
204
+ fact('Busiest', `${printable(recap.busiest.name)} ${muted(`(${recap.busiest.count} requests)`)}`);
205
+ }
206
+ if (recap.slowest !== null) {
207
+ fact('Slowest', `${printable(recap.slowest.name)} ${muted(formatDuration(recap.slowest.durationMs))}`);
208
+ }
209
+ if (fixed.size > 0) {
210
+ fact('Fixed', fixed.size === 1
211
+ ? '1 failed request passed on replay'
212
+ : `${fixed.size} failed requests passed on replay`);
213
+ }
214
+ lines.push(muted(' The traffic is kept: the same command shows it again in the inspector.'));
215
+ return lines;
178
216
  }
179
217
  /** Input of the terminal: keys and the mouse. */
180
218
  input(chunk) {
@@ -277,10 +315,15 @@ export class App {
277
315
  this.settingsKey('enter');
278
316
  }
279
317
  break;
318
+ case 'field':
319
+ if (state.settings?.box != null) {
320
+ state.settings.box.focus = Number(value);
321
+ }
322
+ break;
280
323
  case 'choice':
281
324
  if (state.settings?.box != null) {
282
- state.settings.box.choice = Number(value);
283
- this.settingsKey('enter');
325
+ state.settings.box.focus = state.settings.box.fields.length + Number(value);
326
+ this.boxKey('enter');
284
327
  }
285
328
  break;
286
329
  default:
@@ -328,12 +371,13 @@ export class App {
328
371
  this.menuKey(key);
329
372
  return;
330
373
  }
331
- if (key === 'q') {
332
- this.quit();
374
+ // A box of the Settings tab takes every key: what is typed into it is text, not a key of the view
375
+ if (state.tab === 'settings' && state.settings?.box != null) {
376
+ this.boxKey(key);
333
377
  return;
334
378
  }
335
- if (state.tab === 'settings' && state.settings?.box != null) {
336
- this.settingsKey(key);
379
+ if (key === 'q') {
380
+ this.quit();
337
381
  return;
338
382
  }
339
383
  const tabs = tabsOf(state);
@@ -374,35 +418,14 @@ export class App {
374
418
  break;
375
419
  }
376
420
  }
377
- /** Keys of the Settings tab and of its change box. */
421
+ /** Keys of the Settings tab while no box is open. */
378
422
  settingsKey(key) {
379
423
  const view = this.state.settings;
380
424
  if (view === null) {
381
425
  return;
382
426
  }
383
427
  const settings = view.access.current;
384
- const rows = settingRows(settings);
385
- if (view.box !== null) {
386
- const { box } = view;
387
- const on = !settingValue(settings, box.id);
388
- const choices = changeChoices(view.access, on);
389
- if (key === 'up') {
390
- box.choice = Math.max(box.choice - 1, 0);
391
- }
392
- else if (key === 'down') {
393
- box.choice = Math.min(box.choice + 1, choices.length - 1);
394
- }
395
- else if (key === 'esc' || key === 'q') {
396
- view.box = null;
397
- }
398
- else if (key === 'enter') {
399
- view.box = null;
400
- if (box.choice < 2) {
401
- this.change(withSetting(settings, box.id, on), box.choice === 1, `${settingName(settings, box.id)}: ${on ? 'on' : 'off'}`);
402
- }
403
- }
404
- return;
405
- }
428
+ const rows = settingRows(view.access);
406
429
  const row = rows[view.cursor];
407
430
  if (key === 'up') {
408
431
  view.cursor = Math.max(view.cursor - 1, 0);
@@ -410,16 +433,23 @@ export class App {
410
433
  else if (key === 'down') {
411
434
  view.cursor = Math.min(view.cursor + 1, rows.length - 1);
412
435
  }
436
+ else if (key === 'pageup' || key === 'home') {
437
+ view.cursor = key === 'home' ? 0 : Math.max(view.cursor - 5, 0);
438
+ }
439
+ else if (key === 'pagedown' || key === 'end') {
440
+ view.cursor = key === 'end' ? rows.length - 1 : Math.min(view.cursor + 5, rows.length - 1);
441
+ }
413
442
  else if ((key === 'enter' || key === 'space') && row !== undefined) {
414
- const reason = settingValue(settings, row.id) ? null : unavailable(settings, row.id);
443
+ // What is on can always be turned off; what needs something else says so
444
+ const reason = isOn(settings, row.id) ? null : unavailable(settings, row.id);
415
445
  if (this.state.restarting) {
416
446
  this.flash('The tunnel is restarting; one change at a time.', true, false);
417
447
  }
418
448
  else if (reason !== null) {
419
- this.flash(`${row.label.trim()} ${reason}.`, true, false);
449
+ this.flash(`${row.label.trim()}: ${reason}.`, true, false);
420
450
  }
421
451
  else {
422
- view.box = { id: row.id, choice: 0 };
452
+ this.openBox(row.id);
423
453
  }
424
454
  }
425
455
  else if (key === 's') {
@@ -435,6 +465,137 @@ export class App {
435
465
  }
436
466
  }
437
467
  }
468
+ openBox(id) {
469
+ const view = this.state.settings;
470
+ if (view === null) {
471
+ return;
472
+ }
473
+ const box = {
474
+ id,
475
+ fields: boxFields(view.access.current, id),
476
+ focus: 0,
477
+ problem: null,
478
+ note: null,
479
+ };
480
+ view.box = box;
481
+ if (settingKind(id) === 'auth') {
482
+ // A saved password stands in the file as text: whether Git would take the file along is said here
483
+ const { shown } = view.access.file;
484
+ void view.access.ignored().then((ignored) => {
485
+ box.note =
486
+ ignored === false
487
+ ? `Saved, the password stands in ${shown} as text, and Git does not ignore ${shown} here: it would be committed with it.`
488
+ : `Saved, the password stands in ${shown} as text${ignored === true ? '; Git ignores that file here' : ''}.`;
489
+ this.invalidate();
490
+ });
491
+ }
492
+ }
493
+ /** Keys while a box of the Settings tab is open: typing into its fields, moving, choosing. */
494
+ boxKey(key) {
495
+ const view = this.state.settings;
496
+ const box = view?.box;
497
+ if (view == null || box == null) {
498
+ return;
499
+ }
500
+ const choices = boxChoices(view.access, box.id);
501
+ const last = box.fields.length + choices.length - 1;
502
+ const field = box.fields[box.focus];
503
+ if (key === 'esc') {
504
+ view.box = null;
505
+ }
506
+ else if (key === 'up' || key === 'shift-tab') {
507
+ box.focus = Math.max(box.focus - 1, 0);
508
+ }
509
+ else if (key === 'down' || key === 'tab') {
510
+ box.focus = Math.min(box.focus + 1, last);
511
+ }
512
+ else if (field !== undefined) {
513
+ // In a field: what is typed is its text, q and the digits included
514
+ if (key === 'enter') {
515
+ box.focus += 1;
516
+ }
517
+ else if (key === 'backspace') {
518
+ field.value = [...field.value].slice(0, -1).join('');
519
+ box.problem = null;
520
+ }
521
+ else if (key === 'space') {
522
+ field.value += ' ';
523
+ }
524
+ else if ([...key].length === 1) {
525
+ field.value += key;
526
+ box.problem = null;
527
+ }
528
+ }
529
+ else if (key === 'enter') {
530
+ const choice = choices[box.focus - box.fields.length];
531
+ if (choice !== undefined) {
532
+ this.choose(choice);
533
+ }
534
+ }
535
+ else if (key === 'q') {
536
+ view.box = null;
537
+ }
538
+ }
539
+ /** Takes a choice of the box: checks what was typed, then changes the setting. */
540
+ choose(choice) {
541
+ const view = this.state.settings;
542
+ const box = view?.box;
543
+ if (view == null || box == null) {
544
+ return;
545
+ }
546
+ const { id } = box;
547
+ const settings = view.access.current;
548
+ if (choice.action === 'cancel') {
549
+ view.box = null;
550
+ return;
551
+ }
552
+ const kind = settingKind(id);
553
+ const typed = (box.fields[0]?.value ?? '').trim();
554
+ let next;
555
+ if (kind === 'switch') {
556
+ next = withSetting(settings, id, !isOn(settings, id));
557
+ }
558
+ else if (choice.action === 'off') {
559
+ next = withSetting(settings, id, null);
560
+ }
561
+ else if (kind === 'list') {
562
+ const list = settingValue(settings, id);
563
+ const entries = Array.isArray(list) ? list : [];
564
+ if (choice.action === 'remove') {
565
+ next = withSetting(settings, id, entries.filter((_, index) => index !== choice.remove));
566
+ }
567
+ else {
568
+ box.problem = invalidValue(settings, id, typed);
569
+ if (box.problem !== null) {
570
+ box.focus = 0;
571
+ return;
572
+ }
573
+ next = withSetting(settings, id, [...entries, typed]);
574
+ }
575
+ }
576
+ else if (kind === 'auth') {
577
+ const user = typed;
578
+ const password = box.fields[1]?.value ?? '';
579
+ box.problem = invalidAuth(user, password);
580
+ if (box.problem !== null) {
581
+ box.focus = user === '' ? 0 : 1;
582
+ return;
583
+ }
584
+ next = withSetting(settings, id, `${user}:${password}`);
585
+ }
586
+ else {
587
+ box.problem = invalidValue(settings, id, typed);
588
+ if (box.problem !== null) {
589
+ box.focus = 0;
590
+ return;
591
+ }
592
+ next = withSetting(settings, id, typed);
593
+ }
594
+ const name = settingName(view.access, id);
595
+ view.box = null;
596
+ // The line about it never holds a password: shownValue names the user only
597
+ this.change(next, choice.save, `${name}: ${shownValue(next, id)}`);
598
+ }
438
599
  /** Restarts the tunnel with changed settings, and saves them when asked. */
439
600
  change(next, save, done, remember = true, forRun = true) {
440
601
  const { state } = this;
@@ -569,6 +730,12 @@ export class App {
569
730
  case '/':
570
731
  state.typing = true;
571
732
  break;
733
+ case 'n':
734
+ this.nextFailed();
735
+ break;
736
+ case 'g':
737
+ this.follow();
738
+ break;
572
739
  case 'e':
573
740
  state.failedOnly = !state.failedOnly;
574
741
  this.follow();
@@ -604,6 +771,9 @@ export class App {
604
771
  break;
605
772
  case 'enter':
606
773
  break;
774
+ case 'n':
775
+ this.nextFailed();
776
+ break;
607
777
  default: {
608
778
  const step = scrollStep(key, this.bodyHeight - 4);
609
779
  if (step !== 0) {
@@ -696,6 +866,23 @@ export class App {
696
866
  state.top = window.top;
697
867
  state.selected = shown[to]?.exchange.id ?? null;
698
868
  }
869
+ /** Picks the next failed request after the picked one, from the top again after the last; opens it in a request. */
870
+ nextFailed() {
871
+ const { state } = this;
872
+ const shown = listWindow(state, this.bodyHeight).shown;
873
+ const at = shown.findIndex((entry) => entry.exchange.id === (state.detail?.id ?? state.selected));
874
+ const failed = (entry) => entry.exchange.response.status >= 400;
875
+ const next = shown.find((entry, index) => index > at && failed(entry)) ?? shown.find(failed);
876
+ if (next === undefined) {
877
+ this.flash('No request has failed.', false, false);
878
+ }
879
+ else if (state.detail !== null) {
880
+ this.open(next.exchange.id);
881
+ }
882
+ else {
883
+ this.pick(next.exchange.id);
884
+ }
885
+ }
699
886
  pick(id) {
700
887
  this.state.top = listWindow(this.state, this.bodyHeight).top;
701
888
  this.state.selected = id;
@@ -839,6 +1026,51 @@ function scrollStep(key, page) {
839
1026
  return 0;
840
1027
  }
841
1028
  }
1029
+ /** The line for a failed request whose replay passed. */
1030
+ export function fixedNote(original, replay, tries) {
1031
+ return `Fixed: ${printable(original.request.method)} ${printable(original.request.path)} ${original.response.status} → ${replay.response.status}${tries > 1 ? ` on try ${tries}` : ''}`;
1032
+ }
1033
+ /** A provider as its name is written. */
1034
+ function providerName(provider) {
1035
+ const names = { github: 'GitHub', gitlab: 'GitLab', paypal: 'PayPal' };
1036
+ return names[provider.toLowerCase()] ?? `${provider.slice(0, 1).toUpperCase()}${provider.slice(1)}`;
1037
+ }
1038
+ /** How long a run took, in the largest unit that says it: 40 s, 12 min, 3 h 5 min. */
1039
+ export function formatRun(ms) {
1040
+ const seconds = Math.max(Math.round(ms / 1000), 0);
1041
+ if (seconds < 60) {
1042
+ return `${seconds} s`;
1043
+ }
1044
+ const minutes = Math.floor(seconds / 60);
1045
+ return minutes < 60 ? `${minutes} min` : `${Math.floor(minutes / 60)} h ${minutes % 60} min`;
1046
+ }
1047
+ /** What a run was mostly about: the endpoint that was called most, and the slowest request. Replays are left out. */
1048
+ export function runRecap(exchanges) {
1049
+ const counts = new Map();
1050
+ let slowest = null;
1051
+ for (const exchange of exchanges) {
1052
+ if (exchange.source === 'replay') {
1053
+ continue;
1054
+ }
1055
+ // The endpoint is the path without its query
1056
+ const name = `${exchange.request.method} ${exchange.request.path.split('?')[0] ?? ''}`;
1057
+ counts.set(name, (counts.get(name) ?? 0) + 1);
1058
+ if (slowest === null || exchange.response.durationMs > slowest.durationMs) {
1059
+ slowest = { name, durationMs: exchange.response.durationMs };
1060
+ }
1061
+ }
1062
+ let busiest = null;
1063
+ for (const [name, count] of counts) {
1064
+ if (busiest === null || count > busiest.count) {
1065
+ busiest = { name, count };
1066
+ }
1067
+ }
1068
+ // One endpoint called once is no finding
1069
+ return {
1070
+ busiest: busiest !== null && busiest.count > 1 ? busiest : null,
1071
+ slowest: exchanges.length > 1 ? slowest : null,
1072
+ };
1073
+ }
842
1074
  /** What changed between a request and its replay, in one line. */
843
1075
  export function replayNote(original, replay) {
844
1076
  const was = original.response;
@@ -198,8 +198,9 @@ export function keysOf(state) {
198
198
  ['space', 'actions', 'space'],
199
199
  ['/', 'filter', '/'],
200
200
  ['e', state.failedOnly ? 'show all' : 'failed only', 'e'],
201
+ ...(state.traffic.failed > 0 ? [['n', 'next failed', 'n']] : []),
201
202
  ...(state.selected !== null || state.top !== null
202
- ? [['end', 'follow newest', 'end']]
203
+ ? [['g', 'newest', 'g']]
203
204
  : []),
204
205
  quit,
205
206
  ];
@@ -28,10 +28,12 @@ export function listLines(state, ui, width, height) {
28
28
  continue;
29
29
  }
30
30
  const text = ui.row(entry, width - 2);
31
+ // A failed request whose replay passed keeps its status and gets a mark: it is done with
32
+ const mark = state.fixed.has(entry.exchange.id) ? paint(look, '✓', look.palette.ok, true) : ' ';
31
33
  lines.push({
32
34
  text: index === picked
33
- ? onBackground(`${paint(look, '▌', look.palette.green)} ${fit(text, width - 2)}`, highlight(look))
34
- : ` ${text}`,
35
+ ? onBackground(`${paint(look, '▌', look.palette.green)}${mark}${fit(text, width - 2)}`, highlight(look))
36
+ : ` ${mark}${text}`,
35
37
  hits: hitLine(`row:${entry.exchange.id}`, width),
36
38
  });
37
39
  }