hopwire 0.4.0 → 0.5.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.
@@ -0,0 +1,748 @@
1
+ import { curlCommand } from '../curl.js';
2
+ import { printable } from '../output.js';
3
+ import { formatDuration, paint } from '../terminal.js';
4
+ import { FLASH_MS, headerLine, keysLine, rule, statusLine, tabBar } from './chrome.js';
5
+ import { parseInput } from './input.js';
6
+ import { Traffic } from './model.js';
7
+ import { menuItems, requestsLines } from './requests.js';
8
+ import { TABS, TAB_KEYS, listWindow, sectionsOf, target } from './state.js';
9
+ import { helpContent, scrolled, shareItems, shareLines, statusContent, tools, toolsLines } from './tabs.js';
10
+ /** Rows of the window that are not the tab: two of the header, two rules, two of the footer. */
11
+ const CHROME_ROWS = 6;
12
+ /** Rows the wheel moves. */
13
+ const WHEEL = 3;
14
+ export class App {
15
+ screen;
16
+ state;
17
+ ui;
18
+ actions;
19
+ pending = null;
20
+ flashTimer = null;
21
+ constructor(screen, options) {
22
+ this.screen = screen;
23
+ this.ui = options.ui;
24
+ this.actions = options.actions;
25
+ this.state = {
26
+ version: options.version,
27
+ info: options.info,
28
+ facts: options.facts,
29
+ traffic: new Traffic(),
30
+ tab: 'requests',
31
+ filter: '',
32
+ typing: false,
33
+ failedOnly: false,
34
+ selected: null,
35
+ top: null,
36
+ detail: null,
37
+ menu: null,
38
+ cursor: { share: 0, tools: 0 },
39
+ scroll: { status: 0, help: 0 },
40
+ flash: null,
41
+ stopping: false,
42
+ used: new Set(),
43
+ extraArgs: options.extraArgs ?? '',
44
+ };
45
+ }
46
+ /** Rows the tab has. */
47
+ get bodyHeight() {
48
+ return Math.max(this.screen.height - CHROME_ROWS, 1);
49
+ }
50
+ /** The whole window as lines. */
51
+ frame() {
52
+ const { state, ui } = this;
53
+ const { width } = this.screen;
54
+ const height = this.bodyHeight;
55
+ // While a row is picked or the list was scrolled, the list stands where it is
56
+ if (state.selected !== null || state.top !== null) {
57
+ state.top = listWindow(state, height).top;
58
+ }
59
+ let body;
60
+ switch (state.tab) {
61
+ case 'requests':
62
+ body = requestsLines(state, ui, width, height);
63
+ break;
64
+ case 'share':
65
+ body = shareLines(state, ui, width, height);
66
+ break;
67
+ case 'tools':
68
+ body = toolsLines(state, ui, width, height);
69
+ break;
70
+ case 'status': {
71
+ const shown = scrolled(statusContent(state, ui, width), state.scroll.status, height);
72
+ state.scroll.status = shown.scroll;
73
+ body = shown.lines;
74
+ break;
75
+ }
76
+ case 'help': {
77
+ const shown = scrolled(helpContent(ui), state.scroll.help, height);
78
+ state.scroll.help = shown.scroll;
79
+ body = shown.lines;
80
+ break;
81
+ }
82
+ }
83
+ const filled = Array.from({ length: height }, (_, row) => body[row] ?? { text: '' });
84
+ return [
85
+ headerLine(state, ui, width),
86
+ tabBar(state, ui),
87
+ rule(ui.look, width),
88
+ ...filled,
89
+ rule(ui.look, width),
90
+ statusLine(state, ui, width, height),
91
+ keysLine(state, ui),
92
+ ];
93
+ }
94
+ /** Draws now. */
95
+ draw() {
96
+ if (this.pending !== null) {
97
+ clearTimeout(this.pending);
98
+ this.pending = null;
99
+ }
100
+ this.screen.draw(this.frame());
101
+ }
102
+ /** Draws soon: many changes in one moment become one frame. */
103
+ invalidate() {
104
+ if (this.pending === null) {
105
+ this.pending = setTimeout(() => {
106
+ this.pending = null;
107
+ this.screen.draw(this.frame());
108
+ }, 16);
109
+ this.pending.unref();
110
+ }
111
+ }
112
+ /** One exchange of the tunnel. */
113
+ exchange(exchange, label) {
114
+ const { state } = this;
115
+ const original = exchange.source === 'replay' && exchange.replayOf !== undefined
116
+ ? state.traffic.find(exchange.replayOf)
117
+ : null;
118
+ const removed = state.traffic.add(exchange, label);
119
+ if (removed.length > 0) {
120
+ if (state.top !== null) {
121
+ state.top = Math.max(state.top - removed.length, 0);
122
+ }
123
+ if (removed.some((entry) => entry.exchange.id === state.selected)) {
124
+ state.selected = null;
125
+ }
126
+ if (removed.some((entry) => entry.exchange.id === state.detail?.id)) {
127
+ state.detail = null;
128
+ }
129
+ }
130
+ if (original !== null) {
131
+ this.flash(replayNote(original.exchange, exchange));
132
+ }
133
+ this.invalidate();
134
+ }
135
+ /** A line the command printed while the tunnel runs. */
136
+ line(text) {
137
+ for (const happening of this.state.facts.line(text)) {
138
+ this.flash(`${happening.label === '' ? '' : `${happening.label}: `}${happening.text}`, happening.problem, false);
139
+ }
140
+ this.invalidate();
141
+ }
142
+ /** A line of standard error while the tunnel runs. */
143
+ problem(text) {
144
+ const happening = this.state.facts.problem(text);
145
+ this.flash(happening.text, true, false);
146
+ }
147
+ /** The process is about to stop. */
148
+ stopping() {
149
+ this.state.stopping = true;
150
+ this.draw();
151
+ }
152
+ /** What the terminal shows after the view is gone: what ran and what came in. */
153
+ summary() {
154
+ const { look } = this.ui;
155
+ const { traffic, version } = this.state;
156
+ const requests = traffic.requests === 1 ? '1 request' : `${traffic.requests} requests`;
157
+ const failed = traffic.failed > 0 ? `, ${traffic.failed} failed` : '';
158
+ return [
159
+ `${paint(look, '●', look.palette.muted)} ${paint(look, 'hopwire', null, true)} ${paint(look, version, look.palette.muted)} stopped after ${requests}${failed}`,
160
+ paint(look, ' The traffic is kept: the same command shows it again in the inspector.', look.palette.muted),
161
+ ];
162
+ }
163
+ /** Input of the terminal: keys and the mouse. */
164
+ input(chunk) {
165
+ for (const event of parseInput(chunk)) {
166
+ this.handle(event);
167
+ }
168
+ this.invalidate();
169
+ }
170
+ handle(event) {
171
+ if (event.type === 'key') {
172
+ this.press(event.key);
173
+ }
174
+ else if (event.type === 'click') {
175
+ this.click(this.screen.hitAt(event.x, event.y));
176
+ }
177
+ else {
178
+ this.wheel(event.step);
179
+ }
180
+ }
181
+ flash(text, problem = false, record = true) {
182
+ this.state.flash = { text, problem, at: this.ui.now() };
183
+ if (record && problem) {
184
+ this.state.facts.problem(text);
185
+ }
186
+ else if (record) {
187
+ this.state.facts.note(text);
188
+ }
189
+ if (this.flashTimer !== null) {
190
+ clearTimeout(this.flashTimer);
191
+ }
192
+ this.flashTimer = setTimeout(() => this.invalidate(), FLASH_MS + 50);
193
+ this.flashTimer.unref();
194
+ this.invalidate();
195
+ }
196
+ show(tab) {
197
+ const { state } = this;
198
+ state.tab = tab;
199
+ state.menu = null;
200
+ state.typing = false;
201
+ if (tab === 'status') {
202
+ state.facts.unseenProblems = 0;
203
+ }
204
+ if (tab === 'share') {
205
+ state.used.add('share');
206
+ }
207
+ }
208
+ click(hit) {
209
+ const { state } = this;
210
+ if (hit === null) {
211
+ state.menu = null;
212
+ return;
213
+ }
214
+ const [kind = '', ...rest] = hit.split(':');
215
+ const value = rest.join(':');
216
+ if (kind !== 'menu' && kind !== 'key') {
217
+ state.menu = null;
218
+ }
219
+ switch (kind) {
220
+ case 'tab':
221
+ this.show(value);
222
+ break;
223
+ case 'key':
224
+ this.press(value);
225
+ break;
226
+ case 'row':
227
+ // A click picks the request; a click on the picked one opens it
228
+ if (state.selected === value || state.detail !== null) {
229
+ this.open(value);
230
+ }
231
+ else {
232
+ this.pick(value);
233
+ }
234
+ break;
235
+ case 'section':
236
+ this.section(value);
237
+ break;
238
+ case 'menu':
239
+ state.menu = Number(value);
240
+ this.runMenu();
241
+ break;
242
+ case 'open':
243
+ if (!this.actions.openBrowser(value)) {
244
+ this.flash(`Open ${value} in your browser.`);
245
+ }
246
+ break;
247
+ case 'copy':
248
+ this.copy(value, value);
249
+ break;
250
+ case 'share':
251
+ state.cursor.share = Number(value);
252
+ this.copyShare();
253
+ break;
254
+ case 'tools':
255
+ state.cursor.tools = Number(value);
256
+ this.copyTool();
257
+ break;
258
+ default:
259
+ break;
260
+ }
261
+ }
262
+ wheel(step) {
263
+ const { state } = this;
264
+ if (state.tab === 'status') {
265
+ state.scroll.status += step * WHEEL;
266
+ }
267
+ else if (state.tab === 'help') {
268
+ state.scroll.help += step * WHEEL;
269
+ }
270
+ else if (state.tab === 'requests' && state.detail !== null) {
271
+ state.detail.scroll += step * WHEEL;
272
+ }
273
+ else if (state.tab === 'requests') {
274
+ this.scrollList(step * WHEEL);
275
+ }
276
+ }
277
+ /** Moves the list without picking a row; at the newest row it follows again. */
278
+ scrollList(rows) {
279
+ const { state } = this;
280
+ const height = this.bodyHeight;
281
+ const window = listWindow(state, height);
282
+ const last = Math.max(window.shown.length - height, 0);
283
+ const top = Math.min(Math.max(window.top + rows, 0), last);
284
+ if (window.picked >= 0 && (window.picked < top || window.picked >= top + height)) {
285
+ state.selected = null;
286
+ }
287
+ state.top = top >= last && state.selected === null ? null : top;
288
+ }
289
+ press(key) {
290
+ const { state } = this;
291
+ if (key === 'ctrl-c') {
292
+ this.quit();
293
+ return;
294
+ }
295
+ if (state.typing) {
296
+ this.type(key);
297
+ return;
298
+ }
299
+ if (state.menu !== null) {
300
+ this.menuKey(key);
301
+ return;
302
+ }
303
+ if (key === 'q') {
304
+ this.quit();
305
+ return;
306
+ }
307
+ const tab = TABS.find((name) => TAB_KEYS[name] === key);
308
+ if (tab !== undefined) {
309
+ this.show(tab);
310
+ return;
311
+ }
312
+ const inDetail = state.tab === 'requests' && state.detail !== null;
313
+ if (!inDetail && (key === 'left' || key === 'right')) {
314
+ const at = TABS.indexOf(state.tab) + (key === 'right' ? 1 : -1);
315
+ this.show(TABS[(at + TABS.length) % TABS.length] ?? 'requests');
316
+ return;
317
+ }
318
+ switch (state.tab) {
319
+ case 'requests':
320
+ if (inDetail) {
321
+ this.detailKey(key);
322
+ }
323
+ else {
324
+ this.listKey(key);
325
+ }
326
+ break;
327
+ case 'share':
328
+ this.cursorKey(key, 'share', shareItems(state).length, () => this.copyShare());
329
+ break;
330
+ case 'tools':
331
+ this.cursorKey(key, 'tools', tools(state).length, () => this.copyTool());
332
+ break;
333
+ case 'status':
334
+ state.scroll.status += scrollStep(key, this.bodyHeight);
335
+ break;
336
+ case 'help':
337
+ state.scroll.help += scrollStep(key, this.bodyHeight);
338
+ break;
339
+ }
340
+ }
341
+ quit() {
342
+ this.state.typing = false;
343
+ this.state.menu = null;
344
+ this.state.stopping = true;
345
+ this.actions.interrupt();
346
+ }
347
+ /** Keys while the filter is typed. */
348
+ type(key) {
349
+ const { state } = this;
350
+ if (key === 'enter') {
351
+ state.typing = false;
352
+ }
353
+ else if (key === 'esc') {
354
+ state.typing = false;
355
+ state.filter = '';
356
+ }
357
+ else if (key === 'backspace') {
358
+ state.filter = state.filter.slice(0, -1);
359
+ }
360
+ else if (key === 'space') {
361
+ state.filter += ' ';
362
+ }
363
+ else if ([...key].length === 1) {
364
+ state.filter += key;
365
+ }
366
+ // The filter changes what the list holds: it shows the newest again
367
+ state.selected = null;
368
+ state.top = null;
369
+ }
370
+ cursorKey(key, list, length, enter) {
371
+ const { cursor } = this.state;
372
+ if (key === 'up') {
373
+ cursor[list] = Math.max(cursor[list] - 1, 0);
374
+ }
375
+ else if (key === 'down') {
376
+ cursor[list] = Math.min(cursor[list] + 1, Math.max(length - 1, 0));
377
+ }
378
+ else if (key === 'enter' || key === 'space') {
379
+ enter();
380
+ }
381
+ }
382
+ listKey(key) {
383
+ const { state } = this;
384
+ const height = this.bodyHeight;
385
+ switch (key) {
386
+ case 'up':
387
+ this.move(-1);
388
+ break;
389
+ case 'down':
390
+ this.move(1);
391
+ break;
392
+ case 'pageup':
393
+ this.move(-height);
394
+ break;
395
+ case 'pagedown':
396
+ this.move(height);
397
+ break;
398
+ case 'home':
399
+ this.move(-Infinity);
400
+ break;
401
+ case 'end':
402
+ this.follow();
403
+ break;
404
+ case 'esc':
405
+ // First back to the newest row, then away with the filter
406
+ if (state.selected !== null || state.top !== null) {
407
+ this.follow();
408
+ }
409
+ else {
410
+ state.filter = '';
411
+ state.failedOnly = false;
412
+ }
413
+ break;
414
+ case 'enter': {
415
+ const entry = target(state);
416
+ if (entry === null) {
417
+ this.flash('No request has come in yet.', true, false);
418
+ }
419
+ else {
420
+ this.open(entry.exchange.id);
421
+ }
422
+ break;
423
+ }
424
+ case '/':
425
+ state.typing = true;
426
+ break;
427
+ case 'e':
428
+ state.failedOnly = !state.failedOnly;
429
+ this.follow();
430
+ break;
431
+ default:
432
+ this.requestKey(key);
433
+ }
434
+ }
435
+ detailKey(key) {
436
+ const { state } = this;
437
+ const { detail } = state;
438
+ if (detail === null) {
439
+ return;
440
+ }
441
+ const entry = state.traffic.find(detail.id);
442
+ const sections = entry === null ? [] : sectionsOf(state, entry);
443
+ const turn = (step) => {
444
+ const at = sections.indexOf(detail.section) + step;
445
+ this.section(sections[(at + sections.length) % sections.length] ?? 'request');
446
+ };
447
+ switch (key) {
448
+ case 'esc':
449
+ case 'backspace':
450
+ state.detail = null;
451
+ break;
452
+ case 'tab':
453
+ case 'right':
454
+ turn(1);
455
+ break;
456
+ case 'shift-tab':
457
+ case 'left':
458
+ turn(-1);
459
+ break;
460
+ case 'enter':
461
+ break;
462
+ default: {
463
+ const step = scrollStep(key, this.bodyHeight - 4);
464
+ if (step !== 0) {
465
+ detail.scroll += step;
466
+ }
467
+ else {
468
+ this.requestKey(key);
469
+ }
470
+ }
471
+ }
472
+ }
473
+ /** Keys that act on the request the list or the opened view points at. */
474
+ requestKey(key) {
475
+ const { state } = this;
476
+ if (key === 'space') {
477
+ if (target(state) === null) {
478
+ this.flash('No request has come in yet.', true, false);
479
+ }
480
+ else {
481
+ state.menu = 0;
482
+ state.used.add('actions');
483
+ }
484
+ return;
485
+ }
486
+ const item = menuItems(state).find((entry) => entry.key === key);
487
+ if (item !== undefined) {
488
+ this.run(item);
489
+ }
490
+ else if (key === 'o') {
491
+ this.openInspector('');
492
+ }
493
+ else if (key === 'c') {
494
+ const url = state.info.publicUrl;
495
+ if (url === null) {
496
+ this.flash('This tunnel has no public URL.', true, false);
497
+ }
498
+ else {
499
+ this.copy(url, url);
500
+ }
501
+ }
502
+ else if ((key === 'r' || key === 'i') && state.info.inspectorUrl === null) {
503
+ this.flash('That needs the inspector; this run has none (--no-inspect).', true, false);
504
+ }
505
+ }
506
+ menuKey(key) {
507
+ const { state } = this;
508
+ const items = menuItems(state);
509
+ if (key === 'up') {
510
+ state.menu = Math.max((state.menu ?? 0) - 1, 0);
511
+ }
512
+ else if (key === 'down') {
513
+ state.menu = Math.min((state.menu ?? 0) + 1, items.length - 1);
514
+ }
515
+ else if (key === 'enter') {
516
+ this.runMenu();
517
+ }
518
+ else if (key === 'esc' || key === 'space' || key === 'q') {
519
+ state.menu = null;
520
+ }
521
+ else {
522
+ const item = items.find((entry) => entry.key === key);
523
+ if (item !== undefined) {
524
+ state.menu = null;
525
+ this.run(item);
526
+ }
527
+ }
528
+ }
529
+ runMenu() {
530
+ const item = menuItems(this.state)[this.state.menu ?? 0];
531
+ this.state.menu = null;
532
+ if (item !== undefined) {
533
+ this.run(item);
534
+ }
535
+ }
536
+ /** Moves the pick by rows; below the newest row the list follows again. */
537
+ move(rows) {
538
+ const { state } = this;
539
+ const window = listWindow(state, this.bodyHeight);
540
+ const { shown } = window;
541
+ if (shown.length === 0) {
542
+ return;
543
+ }
544
+ const from = window.picked >= 0 ? window.picked : rows < 0 ? shown.length : window.top - 1;
545
+ const to = Math.max(from + rows, 0);
546
+ if (to >= shown.length) {
547
+ this.follow();
548
+ return;
549
+ }
550
+ // From here on the list stands still
551
+ state.top = window.top;
552
+ state.selected = shown[to]?.exchange.id ?? null;
553
+ }
554
+ pick(id) {
555
+ this.state.top = listWindow(this.state, this.bodyHeight).top;
556
+ this.state.selected = id;
557
+ }
558
+ follow() {
559
+ this.state.selected = null;
560
+ this.state.top = null;
561
+ }
562
+ open(id) {
563
+ const { state } = this;
564
+ const entry = state.traffic.find(id);
565
+ if (entry === null) {
566
+ return;
567
+ }
568
+ state.top = listWindow(state, this.bodyHeight).top;
569
+ state.selected = id;
570
+ state.detail = { id, section: 'request', scroll: 0, messages: null, logs: null, content: null };
571
+ }
572
+ section(section) {
573
+ const { detail } = this.state;
574
+ if (detail === null) {
575
+ return;
576
+ }
577
+ detail.section = section;
578
+ detail.scroll = 0;
579
+ if (section === 'messages' && detail.messages === null) {
580
+ this.load(detail.id, 'messages');
581
+ }
582
+ else if (section === 'logs' && detail.logs === null) {
583
+ this.load(detail.id, 'logs');
584
+ }
585
+ }
586
+ /** Reads the messages of a WebSocket or the log entries of a request from the inspector of this run. */
587
+ load(id, what) {
588
+ const { state } = this;
589
+ const set = (lines) => {
590
+ if (state.detail?.id === id) {
591
+ state.detail[what] = lines;
592
+ this.invalidate();
593
+ }
594
+ };
595
+ const inspector = state.info.inspectorUrl;
596
+ if (inspector === null) {
597
+ set(['This needs the inspector; this run has none (--no-inspect).']);
598
+ return;
599
+ }
600
+ const url = what === 'messages'
601
+ ? `${inspector}/api/ws-messages/${encodeURIComponent(id)}`
602
+ : `${inspector}/api/log-entries?exchangeId=${encodeURIComponent(id)}`;
603
+ this.actions.fetchJson(url).then((answer) => set(what === 'messages' ? messageLines(answer) : logLines(answer)), () => set(['The inspector did not answer.']));
604
+ }
605
+ run(item) {
606
+ const { state } = this;
607
+ const entry = target(state);
608
+ state.used.add('actions');
609
+ if (entry === null) {
610
+ this.flash('No request has come in yet.', true, false);
611
+ return;
612
+ }
613
+ const { exchange } = entry;
614
+ const name = `${printable(exchange.request.method)} ${printable(exchange.request.path)}`;
615
+ switch (item.id) {
616
+ case 'replay': {
617
+ const inspector = state.info.inspectorUrl;
618
+ if (inspector !== null) {
619
+ this.flash(`Replaying ${name}`, false, false);
620
+ void this.actions.replay(inspector, exchange).then((problem) => {
621
+ if (problem !== null) {
622
+ this.flash(`Replay failed: ${problem}`, true);
623
+ }
624
+ });
625
+ }
626
+ break;
627
+ }
628
+ case 'fix':
629
+ this.copy(`hopwire fix ${exchange.id}${state.extraArgs}`, 'the fix command; run it in a second terminal');
630
+ break;
631
+ case 'curl': {
632
+ const service = state.info.services.find((entry2) => entry2.name === entry.label.trim());
633
+ const base = state.info.kind === 'watch' ? state.info.replayTarget : (service ?? state.info.services[0])?.url;
634
+ if (base === null || base === undefined) {
635
+ this.flash('There is nowhere on this machine to send it: start with --replay-target.', true, false);
636
+ break;
637
+ }
638
+ const copy = curlCommand(exchange, base);
639
+ const notes = [
640
+ ...(copy.credentials.length > 0 ? [`it carries ${copy.credentials.join(', ')}`] : []),
641
+ ...(copy.bodyLeftOut === null ? [] : [`no body: ${copy.bodyLeftOut}`]),
642
+ ];
643
+ this.copy(copy.command, `curl for ${name}${notes.length > 0 ? ` (${notes.join('; ')})` : ''}`);
644
+ break;
645
+ }
646
+ case 'inspector':
647
+ this.openInspector(`#exchange=${encodeURIComponent(exchange.id)}`);
648
+ break;
649
+ }
650
+ }
651
+ openInspector(fragment) {
652
+ const inspector = this.state.info.inspectorUrl;
653
+ if (inspector === null) {
654
+ this.flash('There is no inspector in this run (--no-inspect).', true, false);
655
+ return;
656
+ }
657
+ const url = `${inspector}/${fragment}`;
658
+ this.flash(this.actions.openBrowser(url) ? 'Opened the inspector in your browser.' : `Open ${url} in your browser.`, false, false);
659
+ }
660
+ copy(text, what) {
661
+ void this.actions.copy(text).then((how) => {
662
+ this.flash(`${how === 'copied' ? 'Copied' : 'Sent to the clipboard of the terminal:'} ${what}`, false, false);
663
+ });
664
+ }
665
+ copyShare() {
666
+ const item = shareItems(this.state)[this.state.cursor.share];
667
+ if (item !== undefined) {
668
+ this.copy(item.copy, item.title.toLowerCase());
669
+ }
670
+ }
671
+ copyTool() {
672
+ const tool = tools(this.state)[this.state.cursor.tools];
673
+ if (tool !== undefined) {
674
+ this.copy(tool.command, 'the command; run it in a second terminal');
675
+ }
676
+ }
677
+ }
678
+ /** Rows a key scrolls a text by; 0 for a key that does not scroll. */
679
+ function scrollStep(key, page) {
680
+ switch (key) {
681
+ case 'up':
682
+ return -1;
683
+ case 'down':
684
+ return 1;
685
+ case 'pageup':
686
+ return -Math.max(page - 1, 1);
687
+ case 'pagedown':
688
+ return Math.max(page - 1, 1);
689
+ case 'home':
690
+ return -Infinity;
691
+ case 'end':
692
+ return Infinity;
693
+ default:
694
+ return 0;
695
+ }
696
+ }
697
+ /** What changed between a request and its replay, in one line. */
698
+ export function replayNote(original, replay) {
699
+ const was = original.response;
700
+ const now = replay.response;
701
+ const status = was.status === now.status ? `same status ${now.status}` : `${was.status} → ${now.status}`;
702
+ const body = was.bodyTruncated || now.bodyTruncated
703
+ ? 'body not compared'
704
+ : was.bodyBase64 === now.bodyBase64
705
+ ? 'same body'
706
+ : 'body changed';
707
+ return `Replay of ${printable(original.request.method)} ${printable(original.request.path)}: ${status}, ${body}, ${formatDuration(was.durationMs)} → ${formatDuration(now.durationMs)}`;
708
+ }
709
+ /** The messages of a WebSocket as lines: when, which way, and the text or the size. */
710
+ export function messageLines(page) {
711
+ const lines = [];
712
+ for (const block of page.blocks ?? []) {
713
+ for (const message of block.messages) {
714
+ const time = `+${(message.atMs / 1000).toFixed(3)} s`.padStart(11);
715
+ const way = message.direction === 'in' ? '→ service' : '← client ';
716
+ const data = Buffer.from(message.dataBase64, 'base64');
717
+ const text = message.binary
718
+ ? `binary, ${message.length} bytes`
719
+ : printable(data.toString('utf8').replaceAll('\n', ' ').replaceAll('\t', ' '));
720
+ lines.push(`${time} ${way} ${text}${message.truncated ? ' … (cut by the capture)' : ''}`);
721
+ }
722
+ if (block.close !== undefined) {
723
+ const { close } = block;
724
+ lines.push('', `closed by ${close.by === 'hopwire' ? 'Hopwire' : `the ${close.by}`} with ${close.code}${close.reason === '' ? '' : ` (${printable(close.reason)})`}${close.omittedMessages > 0
725
+ ? `; ${close.omittedMessages} messages past the limit were not recorded`
726
+ : ''}`);
727
+ }
728
+ }
729
+ if (page.more) {
730
+ lines.push('', 'More messages are in the inspector.');
731
+ }
732
+ return lines.length === 0 ? ['No message was recorded.'] : lines;
733
+ }
734
+ /** The log entries written during a request, as lines. */
735
+ export function logLines(page) {
736
+ const lines = [];
737
+ for (const { block } of page.blocks ?? []) {
738
+ for (const entry of block.entries) {
739
+ const [first = '', ...rest] = entry.text.split(/\r?\n/);
740
+ lines.push(`${printable(block.source)} ${printable(first)}`);
741
+ lines.push(...rest.map((line) => ` ${printable(line.replaceAll('\t', ' '))}`));
742
+ if (entry.truncated) {
743
+ lines.push(' … (cut by the capture)');
744
+ }
745
+ }
746
+ }
747
+ return lines.length === 0 ? ['No log entry was written during this request.'] : lines;
748
+ }