hopwire 0.6.0 → 0.7.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,6 +1,7 @@
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';
@@ -53,6 +54,9 @@ export class App {
53
54
  flash: null,
54
55
  stopping: false,
55
56
  used: new Set(),
57
+ fixed: new Set(),
58
+ providers: new Set(),
59
+ startedAt: options.ui.now(),
56
60
  extraArgs: options.extraArgs ?? '',
57
61
  };
58
62
  }
@@ -144,7 +148,23 @@ export class App {
144
148
  }
145
149
  }
146
150
  if (original !== null) {
147
- this.flash(replayNote(original.exchange, exchange));
151
+ const failed = original.exchange.response.status >= 400;
152
+ if (failed && exchange.response.status < 400 && !state.fixed.has(original.exchange.id)) {
153
+ // The moment the work was for: the failed request passes
154
+ state.fixed.add(original.exchange.id);
155
+ const tries = state.traffic.entries.filter((entry) => entry.exchange.replayOf === original.exchange.id).length;
156
+ this.flash(fixedNote(original.exchange, exchange, tries));
157
+ }
158
+ else {
159
+ this.flash(replayNote(original.exchange, exchange));
160
+ }
161
+ }
162
+ else if (exchange.source !== 'replay') {
163
+ const webhook = recogniseWebhook(exchange);
164
+ if (webhook !== null && !state.providers.has(webhook.provider)) {
165
+ state.providers.add(webhook.provider);
166
+ this.flash(`First ${providerName(webhook.provider)} webhook of this run${webhook.event === null ? '' : `: ${printable(webhook.event)}`}`);
167
+ }
148
168
  }
149
169
  this.invalidate();
150
170
  }
@@ -168,13 +188,30 @@ export class App {
168
188
  /** What the terminal shows after the view is gone: what ran and what came in. */
169
189
  summary() {
170
190
  const { look } = this.ui;
171
- const { traffic, version } = this.state;
191
+ const { traffic, version, fixed, startedAt } = this.state;
192
+ const muted = (text) => paint(look, text, look.palette.muted);
172
193
  const requests = traffic.requests === 1 ? '1 request' : `${traffic.requests} requests`;
173
194
  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),
195
+ const lines = [
196
+ `${paint(look, '●', look.palette.muted)} ${paint(look, 'hopwire', null, true)} ${muted(version)} stopped after ${requests}${failed}${muted(` · ran ${formatRun(this.ui.now() - startedAt)}`)}`,
177
197
  ];
198
+ const recap = runRecap(traffic.entries.map((entry) => entry.exchange));
199
+ const fact = (label, text) => {
200
+ lines.push(` ${muted(label.padEnd(9))}${text}`);
201
+ };
202
+ if (recap.busiest !== null) {
203
+ fact('Busiest', `${printable(recap.busiest.name)} ${muted(`(${recap.busiest.count} requests)`)}`);
204
+ }
205
+ if (recap.slowest !== null) {
206
+ fact('Slowest', `${printable(recap.slowest.name)} ${muted(formatDuration(recap.slowest.durationMs))}`);
207
+ }
208
+ if (fixed.size > 0) {
209
+ fact('Fixed', fixed.size === 1
210
+ ? '1 failed request passed on replay'
211
+ : `${fixed.size} failed requests passed on replay`);
212
+ }
213
+ lines.push(muted(' The traffic is kept: the same command shows it again in the inspector.'));
214
+ return lines;
178
215
  }
179
216
  /** Input of the terminal: keys and the mouse. */
180
217
  input(chunk) {
@@ -569,6 +606,12 @@ export class App {
569
606
  case '/':
570
607
  state.typing = true;
571
608
  break;
609
+ case 'n':
610
+ this.nextFailed();
611
+ break;
612
+ case 'g':
613
+ this.follow();
614
+ break;
572
615
  case 'e':
573
616
  state.failedOnly = !state.failedOnly;
574
617
  this.follow();
@@ -604,6 +647,9 @@ export class App {
604
647
  break;
605
648
  case 'enter':
606
649
  break;
650
+ case 'n':
651
+ this.nextFailed();
652
+ break;
607
653
  default: {
608
654
  const step = scrollStep(key, this.bodyHeight - 4);
609
655
  if (step !== 0) {
@@ -696,6 +742,23 @@ export class App {
696
742
  state.top = window.top;
697
743
  state.selected = shown[to]?.exchange.id ?? null;
698
744
  }
745
+ /** Picks the next failed request after the picked one, from the top again after the last; opens it in a request. */
746
+ nextFailed() {
747
+ const { state } = this;
748
+ const shown = listWindow(state, this.bodyHeight).shown;
749
+ const at = shown.findIndex((entry) => entry.exchange.id === (state.detail?.id ?? state.selected));
750
+ const failed = (entry) => entry.exchange.response.status >= 400;
751
+ const next = shown.find((entry, index) => index > at && failed(entry)) ?? shown.find(failed);
752
+ if (next === undefined) {
753
+ this.flash('No request has failed.', false, false);
754
+ }
755
+ else if (state.detail !== null) {
756
+ this.open(next.exchange.id);
757
+ }
758
+ else {
759
+ this.pick(next.exchange.id);
760
+ }
761
+ }
699
762
  pick(id) {
700
763
  this.state.top = listWindow(this.state, this.bodyHeight).top;
701
764
  this.state.selected = id;
@@ -839,6 +902,51 @@ function scrollStep(key, page) {
839
902
  return 0;
840
903
  }
841
904
  }
905
+ /** The line for a failed request whose replay passed. */
906
+ export function fixedNote(original, replay, tries) {
907
+ return `Fixed: ${printable(original.request.method)} ${printable(original.request.path)} ${original.response.status} → ${replay.response.status}${tries > 1 ? ` on try ${tries}` : ''}`;
908
+ }
909
+ /** A provider as its name is written. */
910
+ function providerName(provider) {
911
+ const names = { github: 'GitHub', gitlab: 'GitLab', paypal: 'PayPal' };
912
+ return names[provider.toLowerCase()] ?? `${provider.slice(0, 1).toUpperCase()}${provider.slice(1)}`;
913
+ }
914
+ /** How long a run took, in the largest unit that says it: 40 s, 12 min, 3 h 5 min. */
915
+ export function formatRun(ms) {
916
+ const seconds = Math.max(Math.round(ms / 1000), 0);
917
+ if (seconds < 60) {
918
+ return `${seconds} s`;
919
+ }
920
+ const minutes = Math.floor(seconds / 60);
921
+ return minutes < 60 ? `${minutes} min` : `${Math.floor(minutes / 60)} h ${minutes % 60} min`;
922
+ }
923
+ /** What a run was mostly about: the endpoint that was called most, and the slowest request. Replays are left out. */
924
+ export function runRecap(exchanges) {
925
+ const counts = new Map();
926
+ let slowest = null;
927
+ for (const exchange of exchanges) {
928
+ if (exchange.source === 'replay') {
929
+ continue;
930
+ }
931
+ // The endpoint is the path without its query
932
+ const name = `${exchange.request.method} ${exchange.request.path.split('?')[0] ?? ''}`;
933
+ counts.set(name, (counts.get(name) ?? 0) + 1);
934
+ if (slowest === null || exchange.response.durationMs > slowest.durationMs) {
935
+ slowest = { name, durationMs: exchange.response.durationMs };
936
+ }
937
+ }
938
+ let busiest = null;
939
+ for (const [name, count] of counts) {
940
+ if (busiest === null || count > busiest.count) {
941
+ busiest = { name, count };
942
+ }
943
+ }
944
+ // One endpoint called once is no finding
945
+ return {
946
+ busiest: busiest !== null && busiest.count > 1 ? busiest : null,
947
+ slowest: exchanges.length > 1 ? slowest : null,
948
+ };
949
+ }
842
950
  /** What changed between a request and its replay, in one line. */
843
951
  export function replayNote(original, replay) {
844
952
  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
  }
package/dist/tui/tabs.js CHANGED
@@ -213,7 +213,8 @@ export function helpContent(ui) {
213
213
  key('space', 'what can be done with it: replay, fix, copy as curl, inspector'),
214
214
  key('/', 'filter by text: path, status, provider'),
215
215
  key('e', 'failed requests only'),
216
- key('end, esc', 'follow the newest request again'),
216
+ key('n', 'the next failed request, from the picked one on'),
217
+ key('g, end, esc', 'follow the newest request again'),
217
218
  key('r y i f', 'replay, copy as curl, open in the inspector, fix'),
218
219
  key('o c', 'open the inspector, copy the public URL'),
219
220
  '',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hopwire",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
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",