portpreview 0.2.1 → 0.3.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/README.md +128 -1
- package/lib/cli-main.mjs +11 -4
- package/lib/config.mjs +59 -6
- package/lib/index.mjs +2 -0
- package/lib/inspector-server.mjs +577 -0
- package/lib/inspector-store.mjs +295 -0
- package/lib/inspector-ui.mjs +837 -0
- package/lib/tcp-client.mjs +70 -0
- package/lib/tunnel-client.mjs +63 -13
- package/package.json +5 -1
|
@@ -0,0 +1,837 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single-page inspector UI: inline CSS and JS, no framework, no external assets.
|
|
3
|
+
* Captured data is rendered with textContent only. The inline script avoids backticks and
|
|
4
|
+
* dollar-brace sequences so it can live inside this template literal (String.raw keeps its
|
|
5
|
+
* backslash escapes intact).
|
|
6
|
+
* @param {{ nonce: string }} options
|
|
7
|
+
*/
|
|
8
|
+
export function renderInspectorPage({ nonce }) {
|
|
9
|
+
const n = String(nonce).replace(/[^A-Za-z0-9+/=]/g, '')
|
|
10
|
+
return String.raw`<!doctype html>
|
|
11
|
+
<html lang="en">
|
|
12
|
+
<head>
|
|
13
|
+
<meta charset="utf-8">
|
|
14
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
15
|
+
<meta name="referrer" content="no-referrer">
|
|
16
|
+
<title>PortPreview Inspector</title>
|
|
17
|
+
<link rel="icon" href="data:,">
|
|
18
|
+
<style nonce="${n}">
|
|
19
|
+
:root { --bg: #f7f7f8; --panel: #fff; --line: #e3e3e8; --text: #1d1d22; --muted: #6b6b76; --accent: #3a5bd9; --ok: #1f8a4c; --warn: #b26a00; --bad: #c0362c; --chip: #eef0f6; --sel: #e8edff; --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
|
20
|
+
@media (prefers-color-scheme: dark) { :root { --bg: #141417; --panel: #1c1c21; --line: #2e2e36; --text: #e8e8ee; --muted: #9a9aa6; --accent: #7d97ff; --ok: #4cc37f; --warn: #e0a043; --bad: #ff6b5e; --chip: #2a2a33; --sel: #262d48; } }
|
|
21
|
+
* { box-sizing: border-box; }
|
|
22
|
+
html, body { height: 100%; }
|
|
23
|
+
body { margin: 0; font: 13px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); display: flex; flex-direction: column; }
|
|
24
|
+
button, select, input, textarea { font: inherit; color: inherit; }
|
|
25
|
+
button { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 4px 10px; cursor: pointer; }
|
|
26
|
+
button:hover:not(:disabled) { border-color: var(--accent); }
|
|
27
|
+
button:disabled { opacity: .45; cursor: not-allowed; }
|
|
28
|
+
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
|
|
29
|
+
select, input[type=search], input[type=text] { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 4px 8px; }
|
|
30
|
+
header.top { display: flex; align-items: center; gap: 14px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
|
|
31
|
+
.brand { font-weight: 650; font-size: 14px; }
|
|
32
|
+
.brand span { color: var(--muted); font-weight: 500; }
|
|
33
|
+
.meta { color: var(--muted); font-family: var(--mono); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
|
|
34
|
+
.meta b { color: var(--text); font-weight: 500; }
|
|
35
|
+
.conn { font-size: 12px; color: var(--muted); }
|
|
36
|
+
.conn::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--warn); vertical-align: 0; }
|
|
37
|
+
.conn.live::before { background: var(--ok); }
|
|
38
|
+
.filters { display: flex; gap: 8px; align-items: center; padding: 8px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
|
|
39
|
+
.filters input[type=search] { width: 220px; }
|
|
40
|
+
.filters label { display: flex; align-items: center; gap: 5px; color: var(--muted); }
|
|
41
|
+
.filters .count { margin-left: auto; color: var(--muted); }
|
|
42
|
+
main { flex: 1; display: flex; min-height: 0; }
|
|
43
|
+
.list { width: 48%; min-width: 340px; overflow: auto; border-right: 1px solid var(--line); background: var(--panel); }
|
|
44
|
+
table { width: 100%; border-collapse: collapse; }
|
|
45
|
+
th { position: sticky; top: 0; background: var(--panel); text-align: left; font-weight: 600; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .03em; padding: 6px 8px; border-bottom: 1px solid var(--line); }
|
|
46
|
+
td { padding: 5px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; font-family: var(--mono); font-size: 12px; }
|
|
47
|
+
td.path { max-width: 1px; width: 100%; overflow: hidden; text-overflow: ellipsis; }
|
|
48
|
+
tr.row { cursor: pointer; }
|
|
49
|
+
tr.row:hover td { background: var(--bg); }
|
|
50
|
+
tr.row.selected td { background: var(--sel); }
|
|
51
|
+
.s2 { color: var(--ok); } .s3 { color: var(--accent); } .s4 { color: var(--warn); } .s5, .serr { color: var(--bad); } .spend { color: var(--muted); }
|
|
52
|
+
.badge { display: inline-block; font-family: system-ui, sans-serif; font-size: 10px; padding: 0 5px; border-radius: 4px; background: var(--chip); color: var(--muted); margin-left: 5px; vertical-align: 1px; }
|
|
53
|
+
.badge.replay { color: var(--accent); }
|
|
54
|
+
.badge.trunc { color: var(--warn); }
|
|
55
|
+
.badge.edited { color: var(--accent); }
|
|
56
|
+
.empty, .placeholder { color: var(--muted); padding: 24px 16px; margin: 0; }
|
|
57
|
+
.detail { flex: 1; overflow: auto; padding: 14px 18px 40px; min-width: 0; }
|
|
58
|
+
.d-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
|
|
59
|
+
.d-title { font-family: var(--mono); font-size: 14px; font-weight: 600; word-break: break-all; flex: 1; min-width: 200px; }
|
|
60
|
+
.d-actions { display: flex; gap: 6px; flex-wrap: wrap; }
|
|
61
|
+
.d-meta { color: var(--muted); margin: 8px 0 4px; display: flex; flex-wrap: wrap; gap: 4px 14px; }
|
|
62
|
+
.d-meta a { color: var(--accent); cursor: pointer; text-decoration: underline; }
|
|
63
|
+
.note { margin: 8px 0; padding: 6px 10px; border-radius: 6px; background: var(--chip); color: var(--text); }
|
|
64
|
+
.note.bad { color: var(--bad); }
|
|
65
|
+
.note.warn { color: var(--warn); }
|
|
66
|
+
.tabs { display: flex; gap: 2px; margin: 14px 0 0; border-bottom: 1px solid var(--line); }
|
|
67
|
+
.tabs button, .views button { border: 0; border-radius: 0; background: none; padding: 6px 12px; color: var(--muted); border-bottom: 2px solid transparent; }
|
|
68
|
+
.tabs button.on, .views button.on { color: var(--text); border-bottom-color: var(--accent); }
|
|
69
|
+
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); margin: 16px 0 6px; font-weight: 600; }
|
|
70
|
+
table.headers td { white-space: normal; word-break: break-all; vertical-align: top; }
|
|
71
|
+
table.headers td.name { width: 30%; color: var(--muted); }
|
|
72
|
+
.mask { font-family: var(--mono); font-size: 12px; padding: 0 6px; }
|
|
73
|
+
.bodybar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 16px 0 6px; }
|
|
74
|
+
.bodybar h3 { margin: 0; }
|
|
75
|
+
.views { display: flex; }
|
|
76
|
+
.bodyinfo { color: var(--muted); font-size: 12px; }
|
|
77
|
+
.bodybar a { color: var(--accent); font-size: 12px; }
|
|
78
|
+
pre { margin: 0; padding: 10px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; font: 12px/1.5 var(--mono); white-space: pre-wrap; word-break: break-all; max-height: 60vh; overflow: auto; }
|
|
79
|
+
pre.hex { white-space: pre; word-break: normal; }
|
|
80
|
+
footer { padding: 6px 16px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11.5px; background: var(--panel); display: flex; gap: 12px; flex-wrap: wrap; }
|
|
81
|
+
footer .stats { margin-left: auto; }
|
|
82
|
+
.toast { position: fixed; right: 16px; bottom: 40px; max-width: 420px; background: var(--text); color: var(--bg); padding: 8px 12px; border-radius: 8px; box-shadow: 0 4px 18px rgba(0,0,0,.2); }
|
|
83
|
+
.copybox { margin-top: 10px; }
|
|
84
|
+
.copybox textarea { width: 100%; min-height: 120px; font: 12px var(--mono); background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 8px; }
|
|
85
|
+
[hidden] { display: none !important; }
|
|
86
|
+
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
|
|
87
|
+
dialog.editor { width: min(940px, 94vw); max-height: 92vh; padding: 0; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); box-shadow: 0 14px 44px rgba(0,0,0,.3); }
|
|
88
|
+
dialog.editor::backdrop { background: rgba(12,12,16,.45); }
|
|
89
|
+
.ed { display: flex; flex-direction: column; gap: 6px; padding: 16px 18px 14px; }
|
|
90
|
+
.ed h2 { font-size: 15px; margin: 0; font-weight: 650; }
|
|
91
|
+
.ed-sub { color: var(--muted); margin: 0 0 6px; }
|
|
92
|
+
.ed-line { display: flex; gap: 8px; }
|
|
93
|
+
.ed-line input { flex: 1; min-width: 0; font-family: var(--mono); font-size: 12px; }
|
|
94
|
+
.ed-label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; margin: 10px 0 0; }
|
|
95
|
+
.ed-label span { text-transform: none; letter-spacing: 0; font-weight: 400; }
|
|
96
|
+
.ed textarea { width: 100%; font: 12px/1.5 var(--mono); background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 8px; resize: vertical; }
|
|
97
|
+
.ed-hint { color: var(--muted); font-size: 12px; }
|
|
98
|
+
.ed-bodybar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
|
|
99
|
+
.ed-bodybar .ed-label { margin: 0; }
|
|
100
|
+
.ed-bodybar .bodyinfo { margin-right: auto; }
|
|
101
|
+
.ed .note { margin: 2px 0; }
|
|
102
|
+
.ed-confirm { display: flex; align-items: center; gap: 6px; color: var(--warn); }
|
|
103
|
+
.ed-actions { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 8px; }
|
|
104
|
+
.ed-actions .ed-hint { margin-right: auto; }
|
|
105
|
+
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
|
106
|
+
@media (max-width: 860px) { main { flex-direction: column; } .list { width: auto; min-width: 0; max-height: 45vh; border-right: 0; border-bottom: 1px solid var(--line); } }
|
|
107
|
+
</style>
|
|
108
|
+
</head>
|
|
109
|
+
<body>
|
|
110
|
+
<header class="top">
|
|
111
|
+
<div class="brand">PortPreview <span>Inspector</span></div>
|
|
112
|
+
<div class="meta" id="meta">Loading…</div>
|
|
113
|
+
<div class="conn" id="conn">Connecting…</div>
|
|
114
|
+
<button id="clear" type="button" title="Remove every captured request from memory">Clear</button>
|
|
115
|
+
</header>
|
|
116
|
+
<div class="filters">
|
|
117
|
+
<select id="f-method" aria-label="Method">
|
|
118
|
+
<option value="">All methods</option><option>GET</option><option>POST</option><option>PUT</option><option>PATCH</option><option>DELETE</option><option>HEAD</option><option>OPTIONS</option><option value="WS">WebSocket</option>
|
|
119
|
+
</select>
|
|
120
|
+
<select id="f-status" aria-label="Status">
|
|
121
|
+
<option value="">All statuses</option><option value="2">2xx</option><option value="3">3xx</option><option value="4">4xx</option><option value="5">5xx</option><option value="err">Errors</option><option value="pending">In flight</option>
|
|
122
|
+
</select>
|
|
123
|
+
<input id="f-path" type="search" placeholder="Filter by path" aria-label="Path contains">
|
|
124
|
+
<label><input id="f-static" type="checkbox"> Hide static assets</label>
|
|
125
|
+
<span class="count" id="count"></span>
|
|
126
|
+
</div>
|
|
127
|
+
<main>
|
|
128
|
+
<section class="list">
|
|
129
|
+
<table><thead><tr><th>Time</th><th>Method</th><th>Path</th><th>Status</th><th>Duration</th><th>Size</th></tr></thead><tbody id="rows"></tbody></table>
|
|
130
|
+
<p class="empty" id="empty">No requests yet. Requests that reach this CLI through the tunnel appear here as they arrive.</p>
|
|
131
|
+
</section>
|
|
132
|
+
<section class="detail" id="detail"><p class="placeholder">Select a request to see its headers and body.</p></section>
|
|
133
|
+
</main>
|
|
134
|
+
<footer>
|
|
135
|
+
<span>Only requests that reached this CLI are listed. Requests the gateway rejected (body too large, preview password, IP allowlist, tunnel offline) never arrive here.</span>
|
|
136
|
+
<span>Captured data stays in this process's memory and is gone when the CLI exits.</span>
|
|
137
|
+
<span class="stats" id="stats"></span>
|
|
138
|
+
</footer>
|
|
139
|
+
<div class="toast" id="toast" hidden></div>
|
|
140
|
+
<dialog class="editor" id="editor" aria-labelledby="ed-title">
|
|
141
|
+
<div class="ed">
|
|
142
|
+
<h2 id="ed-title">Edit & replay</h2>
|
|
143
|
+
<p class="ed-sub" id="ed-sub"></p>
|
|
144
|
+
<div class="ed-line">
|
|
145
|
+
<label class="sr" for="ed-method">Method</label>
|
|
146
|
+
<select id="ed-method"><option>GET</option><option>POST</option><option>PUT</option><option>PATCH</option><option>DELETE</option><option>HEAD</option><option>OPTIONS</option></select>
|
|
147
|
+
<label class="sr" for="ed-path">Path and query</label>
|
|
148
|
+
<input id="ed-path" type="text" spellcheck="false" autocomplete="off">
|
|
149
|
+
</div>
|
|
150
|
+
<div id="ed-method-note"></div>
|
|
151
|
+
<label class="ed-label" for="ed-headers">Headers <span>(one "name: value" per line)</span></label>
|
|
152
|
+
<textarea id="ed-headers" rows="9" spellcheck="false" wrap="off"></textarea>
|
|
153
|
+
<div class="ed-hint">Host, Connection, Content-Length and Transfer-Encoding are always set by the inspector; lines for them are ignored.</div>
|
|
154
|
+
<div class="ed-bodybar">
|
|
155
|
+
<label class="ed-label" for="ed-body">Body</label>
|
|
156
|
+
<span class="bodyinfo" id="ed-bodyinfo"></span>
|
|
157
|
+
<select id="ed-eol" aria-label="Line endings" hidden><option value="crlf">CRLF line endings</option><option value="lf">LF line endings</option></select>
|
|
158
|
+
<button type="button" id="ed-astext" hidden>Edit as text</button>
|
|
159
|
+
<button type="button" id="ed-restore" hidden>Restore captured body</button>
|
|
160
|
+
<button type="button" id="ed-empty">Empty body</button>
|
|
161
|
+
</div>
|
|
162
|
+
<div id="ed-notes"></div>
|
|
163
|
+
<textarea id="ed-body" rows="12" spellcheck="false"></textarea>
|
|
164
|
+
<label class="ed-confirm" id="ed-confirm-row" hidden><input type="checkbox" id="ed-confirm"> Send this partial body as shown</label>
|
|
165
|
+
<div class="note bad" id="ed-error" role="alert" hidden></div>
|
|
166
|
+
<div class="ed-actions">
|
|
167
|
+
<span class="ed-hint">Ctrl+Enter sends · Esc cancels</span>
|
|
168
|
+
<button type="button" id="ed-cancel">Cancel</button>
|
|
169
|
+
<button type="button" class="primary" id="ed-send">Send</button>
|
|
170
|
+
</div>
|
|
171
|
+
</div>
|
|
172
|
+
</dialog>
|
|
173
|
+
<script nonce="${n}">
|
|
174
|
+
(function () {
|
|
175
|
+
'use strict';
|
|
176
|
+
var MUTATE = { 'X-PortPreview-Inspector': '1', 'Content-Type': 'application/json' };
|
|
177
|
+
var SECRET = { 'authorization': 1, 'proxy-authorization': 1, 'cookie': 1, 'set-cookie': 1, 'x-api-key': 1 };
|
|
178
|
+
var CURL_OMIT = { 'host': 1, 'content-length': 1, 'connection': 1, 'keep-alive': 1, 'proxy-authenticate': 1, 'proxy-authorization': 1, 'te': 1, 'trailer': 1, 'transfer-encoding': 1, 'upgrade': 1, 'accept-encoding': 1 };
|
|
179
|
+
var STATIC_EXT = /\.(?:m?js|css|map|png|jpe?g|gif|svg|ico|webp|avif|bmp|woff2?|ttf|otf|eot|mp4|webm|mp3|wav|wasm)$/i;
|
|
180
|
+
var STATIC_PREFIX = /^\/(?:_next\/static\/|_next\/image|@vite\/|@fs\/|@id\/|@react-refresh|node_modules\/|__vite_ping|assets\/|static\/|favicon)/i;
|
|
181
|
+
var HEX_LIMIT = 4096;
|
|
182
|
+
var EDIT_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];
|
|
183
|
+
var REGENERATED = { 'host': 1, 'connection': 1, 'content-length': 1, 'transfer-encoding': 1 };
|
|
184
|
+
|
|
185
|
+
var entries = new Map();
|
|
186
|
+
var selected = null;
|
|
187
|
+
var detail = null;
|
|
188
|
+
var tab = 'request';
|
|
189
|
+
var view = 'pretty';
|
|
190
|
+
var status = null;
|
|
191
|
+
var revealed = new Set();
|
|
192
|
+
var renderQueued = false;
|
|
193
|
+
var detailTimer = null;
|
|
194
|
+
var detailLoading = false;
|
|
195
|
+
var detailDirty = false;
|
|
196
|
+
|
|
197
|
+
function $(id) { return document.getElementById(id); }
|
|
198
|
+
function el(tag, cls, text) {
|
|
199
|
+
var node = document.createElement(tag);
|
|
200
|
+
if (cls) node.className = cls;
|
|
201
|
+
if (text !== undefined && text !== null) node.textContent = String(text);
|
|
202
|
+
return node;
|
|
203
|
+
}
|
|
204
|
+
function clear(node) { while (node.firstChild) node.removeChild(node.firstChild); }
|
|
205
|
+
function api(path, options) {
|
|
206
|
+
return fetch(path, options).then(function (response) {
|
|
207
|
+
return response.json().catch(function () { return {}; }).then(function (body) {
|
|
208
|
+
if (!response.ok) throw new Error((body && body.error) || ('HTTP ' + response.status));
|
|
209
|
+
return body;
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
function toast(text, ms) {
|
|
214
|
+
var node = $('toast');
|
|
215
|
+
node.textContent = text;
|
|
216
|
+
node.hidden = false;
|
|
217
|
+
clearTimeout(toast.timer);
|
|
218
|
+
toast.timer = setTimeout(function () { node.hidden = true; }, ms || 3500);
|
|
219
|
+
}
|
|
220
|
+
function fmtBytes(n) {
|
|
221
|
+
if (n === null || n === undefined) return '';
|
|
222
|
+
if (n < 1024) return n + ' B';
|
|
223
|
+
if (n < 1048576) return (n / 1024).toFixed(n < 10240 ? 1 : 0) + ' KB';
|
|
224
|
+
return (n / 1048576).toFixed(1) + ' MB';
|
|
225
|
+
}
|
|
226
|
+
function pad(value, width) { value = String(value); while (value.length < width) value = '0' + value; return value; }
|
|
227
|
+
function fmtTime(ms) {
|
|
228
|
+
var d = new Date(ms);
|
|
229
|
+
return pad(d.getHours(), 2) + ':' + pad(d.getMinutes(), 2) + ':' + pad(d.getSeconds(), 2) + '.' + pad(d.getMilliseconds(), 3);
|
|
230
|
+
}
|
|
231
|
+
function fmtDuration(entry) {
|
|
232
|
+
if (!entry.done) return '…';
|
|
233
|
+
var ms = entry.durationMs;
|
|
234
|
+
return ms < 1000 ? ms + ' ms' : (ms / 1000).toFixed(ms < 10000 ? 2 : 1) + ' s';
|
|
235
|
+
}
|
|
236
|
+
function statusText(entry) {
|
|
237
|
+
if (entry.status) return String(entry.status);
|
|
238
|
+
if (entry.error) return 'ERR';
|
|
239
|
+
return '…';
|
|
240
|
+
}
|
|
241
|
+
function statusClass(entry) {
|
|
242
|
+
if (entry.status) return 's' + String(entry.status).charAt(0);
|
|
243
|
+
return entry.error ? 'serr' : 'spend';
|
|
244
|
+
}
|
|
245
|
+
function bytesFrom64(b64) {
|
|
246
|
+
var bin = atob(b64 || '');
|
|
247
|
+
var out = new Uint8Array(bin.length);
|
|
248
|
+
for (var i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
249
|
+
return out;
|
|
250
|
+
}
|
|
251
|
+
function utf8(bytes, fatal) {
|
|
252
|
+
try { return new TextDecoder('utf-8', { fatal: !!fatal }).decode(bytes); } catch (e) { return null; }
|
|
253
|
+
}
|
|
254
|
+
function looksBinary(text) { return /[\u0000-\u0008\u000e-\u001f]/.test(text); }
|
|
255
|
+
|
|
256
|
+
// ---------- list ----------
|
|
257
|
+
function matches(entry) {
|
|
258
|
+
var method = $('f-method').value;
|
|
259
|
+
if (method === 'WS' ? entry.kind !== 'websocket' : (method && (entry.method !== method || entry.kind === 'websocket'))) return false;
|
|
260
|
+
var cls = $('f-status').value;
|
|
261
|
+
if (cls === 'err' && !(entry.error || entry.status >= 500)) return false;
|
|
262
|
+
if (cls === 'pending' && entry.done) return false;
|
|
263
|
+
if (/^[2-5]$/.test(cls) && String(entry.status || '').charAt(0) !== cls) return false;
|
|
264
|
+
var needle = $('f-path').value.trim().toLowerCase();
|
|
265
|
+
if (needle && entry.path.toLowerCase().indexOf(needle) === -1) return false;
|
|
266
|
+
if ($('f-static').checked) {
|
|
267
|
+
var path = entry.path.split('?')[0];
|
|
268
|
+
if (STATIC_EXT.test(path) || STATIC_PREFIX.test(path)) return false;
|
|
269
|
+
}
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
function queueRender() {
|
|
273
|
+
if (renderQueued) return;
|
|
274
|
+
renderQueued = true;
|
|
275
|
+
requestAnimationFrame(function () { renderQueued = false; renderList(); });
|
|
276
|
+
}
|
|
277
|
+
function renderList() {
|
|
278
|
+
var rows = $('rows');
|
|
279
|
+
var fragment = document.createDocumentFragment();
|
|
280
|
+
var ids = Array.from(entries.keys()).sort(function (a, b) { return b - a; });
|
|
281
|
+
var shown = 0;
|
|
282
|
+
ids.forEach(function (id) {
|
|
283
|
+
var entry = entries.get(id);
|
|
284
|
+
if (!matches(entry)) return;
|
|
285
|
+
shown++;
|
|
286
|
+
var tr = el('tr', 'row' + (id === selected ? ' selected' : ''));
|
|
287
|
+
tr.dataset.id = String(id);
|
|
288
|
+
tr.appendChild(el('td', '', fmtTime(entry.startedAt)));
|
|
289
|
+
tr.appendChild(el('td', '', entry.kind === 'websocket' ? 'WS' : entry.method));
|
|
290
|
+
var path = el('td', 'path', entry.path);
|
|
291
|
+
path.title = entry.path;
|
|
292
|
+
if (entry.source === 'replay') path.appendChild(el('span', 'badge replay', 'replay'));
|
|
293
|
+
if (entry.edited) path.appendChild(el('span', 'badge edited', 'edited'));
|
|
294
|
+
if (entry.request.truncated || entry.response.truncated) path.appendChild(el('span', 'badge trunc', 'truncated'));
|
|
295
|
+
tr.appendChild(path);
|
|
296
|
+
tr.appendChild(el('td', statusClass(entry), statusText(entry)));
|
|
297
|
+
tr.appendChild(el('td', '', fmtDuration(entry)));
|
|
298
|
+
tr.appendChild(el('td', '', entry.kind === 'websocket'
|
|
299
|
+
? (entry.websocket.framesIn + entry.websocket.framesOut) + ' frames'
|
|
300
|
+
: fmtBytes(entry.response.bytes)));
|
|
301
|
+
fragment.appendChild(tr);
|
|
302
|
+
});
|
|
303
|
+
clear(rows);
|
|
304
|
+
rows.appendChild(fragment);
|
|
305
|
+
$('empty').hidden = entries.size > 0;
|
|
306
|
+
$('count').textContent = shown === entries.size ? entries.size + ' requests' : shown + ' of ' + entries.size + ' requests';
|
|
307
|
+
}
|
|
308
|
+
$('rows').addEventListener('click', function (event) {
|
|
309
|
+
var tr = event.target.closest('tr.row');
|
|
310
|
+
if (tr) select(Number(tr.dataset.id));
|
|
311
|
+
});
|
|
312
|
+
['f-method', 'f-status', 'f-static'].forEach(function (id) { $(id).addEventListener('change', queueRender); });
|
|
313
|
+
$('f-path').addEventListener('input', queueRender);
|
|
314
|
+
|
|
315
|
+
// ---------- detail ----------
|
|
316
|
+
function select(id) {
|
|
317
|
+
if (selected !== id) { selected = id; detail = null; revealed.clear(); }
|
|
318
|
+
queueRender();
|
|
319
|
+
loadDetail();
|
|
320
|
+
}
|
|
321
|
+
function loadDetail() {
|
|
322
|
+
if (selected === null) return;
|
|
323
|
+
if (detailLoading) { detailDirty = true; return; }
|
|
324
|
+
detailLoading = true;
|
|
325
|
+
var id = selected;
|
|
326
|
+
api('/api/requests/' + id).then(function (body) {
|
|
327
|
+
if (id === selected) { detail = body; renderDetail(); }
|
|
328
|
+
}).catch(function (error) {
|
|
329
|
+
if (id === selected) { detail = null; renderMissing(error.message); }
|
|
330
|
+
}).then(function () {
|
|
331
|
+
detailLoading = false;
|
|
332
|
+
if (detailDirty) { detailDirty = false; scheduleDetail(); }
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
function scheduleDetail() {
|
|
336
|
+
if (detailTimer) return;
|
|
337
|
+
detailTimer = setTimeout(function () { detailTimer = null; loadDetail(); }, 400);
|
|
338
|
+
}
|
|
339
|
+
function renderMissing(message) {
|
|
340
|
+
var root = $('detail');
|
|
341
|
+
clear(root);
|
|
342
|
+
root.appendChild(el('p', 'placeholder', message || 'Select a request to see its headers and body.'));
|
|
343
|
+
}
|
|
344
|
+
function link(text, id) {
|
|
345
|
+
var a = el('a', '', text);
|
|
346
|
+
a.addEventListener('click', function () { select(id); });
|
|
347
|
+
return a;
|
|
348
|
+
}
|
|
349
|
+
function renderDetail() {
|
|
350
|
+
var d = detail;
|
|
351
|
+
var root = $('detail');
|
|
352
|
+
var scroll = root.scrollTop;
|
|
353
|
+
clear(root);
|
|
354
|
+
var head = el('div', 'd-head');
|
|
355
|
+
var title = el('div', 'd-title', (d.kind === 'websocket' ? 'WS' : d.method) + ' ' + d.path);
|
|
356
|
+
head.appendChild(title);
|
|
357
|
+
var actions = el('div', 'd-actions');
|
|
358
|
+
if (d.kind !== 'websocket') {
|
|
359
|
+
var replay = el('button', 'primary', 'Replay');
|
|
360
|
+
replay.type = 'button';
|
|
361
|
+
replay.disabled = !d.replayable;
|
|
362
|
+
replay.title = d.replayable ? 'Send this exact request to the local app again' : d.replayBlockedReason;
|
|
363
|
+
replay.addEventListener('click', function () { doReplay(d.id, replay); });
|
|
364
|
+
actions.appendChild(replay);
|
|
365
|
+
var edit = el('button', '', 'Edit & replay');
|
|
366
|
+
edit.type = 'button';
|
|
367
|
+
edit.id = 'd-edit';
|
|
368
|
+
edit.title = 'Change the method, path, headers or body, then send it to the local app';
|
|
369
|
+
edit.addEventListener('click', function () { openEditor(d, edit); });
|
|
370
|
+
actions.appendChild(edit);
|
|
371
|
+
var curlLocal = el('button', '', 'Copy cURL (local)');
|
|
372
|
+
var curlPublic = el('button', '', 'Copy cURL (public URL)');
|
|
373
|
+
var curlBlocked = d.request.truncated || !d.request.complete;
|
|
374
|
+
[curlLocal, curlPublic].forEach(function (button, index) {
|
|
375
|
+
button.type = 'button';
|
|
376
|
+
button.disabled = curlBlocked || (index === 1 && !publicBase(d));
|
|
377
|
+
button.title = curlBlocked ? 'The request body was not fully captured' : 'The command contains captured header values, which may include secrets';
|
|
378
|
+
button.addEventListener('click', function () { copyCurl(d, index === 1 ? 'public' : 'local'); });
|
|
379
|
+
actions.appendChild(button);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
head.appendChild(actions);
|
|
383
|
+
root.appendChild(head);
|
|
384
|
+
|
|
385
|
+
var meta = el('div', 'd-meta');
|
|
386
|
+
meta.appendChild(el('span', '', '#' + d.id + ' · ' + new Date(d.startedAt).toLocaleString()));
|
|
387
|
+
meta.appendChild(el('span', statusClass(d), 'Status ' + statusText(d)));
|
|
388
|
+
meta.appendChild(el('span', '', 'Duration ' + fmtDuration(d)));
|
|
389
|
+
if (d.protocolVersion) meta.appendChild(el('span', '', 'Protocol v' + d.protocolVersion));
|
|
390
|
+
if (d.replayOf !== null) {
|
|
391
|
+
var of = el('span', '', d.edited ? 'Edited replay of ' : 'Replay of ');
|
|
392
|
+
of.appendChild(entries.has(d.replayOf) ? link('#' + d.replayOf, d.replayOf) : el('span', '', '#' + d.replayOf + ' (evicted)'));
|
|
393
|
+
meta.appendChild(of);
|
|
394
|
+
}
|
|
395
|
+
if (d.replays.length) {
|
|
396
|
+
var reps = el('span', '', 'Replays: ');
|
|
397
|
+
d.replays.forEach(function (id, i) {
|
|
398
|
+
if (i) reps.appendChild(document.createTextNode(', '));
|
|
399
|
+
reps.appendChild(entries.has(id) ? link('#' + id, id) : el('span', '', '#' + id));
|
|
400
|
+
});
|
|
401
|
+
meta.appendChild(reps);
|
|
402
|
+
}
|
|
403
|
+
root.appendChild(meta);
|
|
404
|
+
if (d.error) root.appendChild(el('div', 'note bad', 'Error: ' + d.error));
|
|
405
|
+
if (d.synthetic) root.appendChild(el('div', 'note warn', 'The CLI generated this 502 because the local app could not be reached.'));
|
|
406
|
+
|
|
407
|
+
if (d.kind === 'websocket') { renderWebSocket(root, d); root.scrollTop = scroll; return; }
|
|
408
|
+
|
|
409
|
+
var tabs = el('div', 'tabs');
|
|
410
|
+
['request', 'response'].forEach(function (name) {
|
|
411
|
+
var button = el('button', tab === name ? 'on' : '', name === 'request' ? 'Request' : 'Response');
|
|
412
|
+
button.type = 'button';
|
|
413
|
+
button.addEventListener('click', function () { tab = name; renderDetail(); });
|
|
414
|
+
tabs.appendChild(button);
|
|
415
|
+
});
|
|
416
|
+
root.appendChild(tabs);
|
|
417
|
+
var side = d[tab];
|
|
418
|
+
if (tab === 'response' && !d.status) {
|
|
419
|
+
root.appendChild(el('p', 'placeholder', d.done ? 'No response was received.' : 'Waiting for the response…'));
|
|
420
|
+
} else {
|
|
421
|
+
root.appendChild(el('h3', '', tab === 'request' ? 'Headers sent to the local app' : 'Headers from the local app'));
|
|
422
|
+
root.appendChild(headerTable(side.headers, tab));
|
|
423
|
+
renderBody(root, d, side, tab);
|
|
424
|
+
}
|
|
425
|
+
root.scrollTop = scroll;
|
|
426
|
+
}
|
|
427
|
+
function headerTable(pairs, sideName) {
|
|
428
|
+
var table = el('table', 'headers');
|
|
429
|
+
var body = el('tbody');
|
|
430
|
+
if (!pairs.length) {
|
|
431
|
+
var tr0 = el('tr');
|
|
432
|
+
tr0.appendChild(el('td', 'name', '(none)'));
|
|
433
|
+
body.appendChild(tr0);
|
|
434
|
+
}
|
|
435
|
+
pairs.forEach(function (pair, index) {
|
|
436
|
+
var tr = el('tr');
|
|
437
|
+
tr.appendChild(el('td', 'name', pair[0]));
|
|
438
|
+
var value = el('td');
|
|
439
|
+
var key = detail.id + ':' + sideName + ':' + index;
|
|
440
|
+
if (SECRET[pair[0].toLowerCase()] && !revealed.has(key)) {
|
|
441
|
+
var mask = el('button', 'mask', '•••••••• click to reveal');
|
|
442
|
+
mask.type = 'button';
|
|
443
|
+
mask.title = 'Hidden on screen; stored unmodified for replay';
|
|
444
|
+
mask.addEventListener('click', function () { revealed.add(key); clear(value); value.textContent = pair[1]; });
|
|
445
|
+
value.appendChild(mask);
|
|
446
|
+
} else value.textContent = pair[1];
|
|
447
|
+
tr.appendChild(value);
|
|
448
|
+
body.appendChild(tr);
|
|
449
|
+
});
|
|
450
|
+
table.appendChild(body);
|
|
451
|
+
return table;
|
|
452
|
+
}
|
|
453
|
+
function renderBody(root, d, side, sideName) {
|
|
454
|
+
var bar = el('div', 'bodybar');
|
|
455
|
+
bar.appendChild(el('h3', '', 'Body'));
|
|
456
|
+
var views = el('div', 'views');
|
|
457
|
+
['pretty', 'raw', 'hex'].forEach(function (name) {
|
|
458
|
+
var button = el('button', view === name ? 'on' : '', name === 'pretty' ? 'Pretty' : name === 'raw' ? 'Raw' : 'Hex');
|
|
459
|
+
button.type = 'button';
|
|
460
|
+
button.addEventListener('click', function () { view = name; renderDetail(); });
|
|
461
|
+
views.appendChild(button);
|
|
462
|
+
});
|
|
463
|
+
bar.appendChild(views);
|
|
464
|
+
var info = fmtBytes(side.bytes);
|
|
465
|
+
if (side.retained !== side.bytes) info += ', ' + fmtBytes(side.retained) + ' captured';
|
|
466
|
+
bar.appendChild(el('span', 'bodyinfo', info));
|
|
467
|
+
if (side.retained) {
|
|
468
|
+
var download = el('a', '', 'Download');
|
|
469
|
+
download.href = '/api/requests/' + d.id + '/body/' + sideName;
|
|
470
|
+
download.setAttribute('download', 'request-' + d.id + '-' + sideName + '.bin');
|
|
471
|
+
bar.appendChild(download);
|
|
472
|
+
}
|
|
473
|
+
root.appendChild(bar);
|
|
474
|
+
if (side.truncated === 'limit') root.appendChild(el('div', 'note warn', 'Only the first ' + fmtBytes(side.retained) + ' of this body were captured (per-body limit). The app and the visitor still received all of it.'));
|
|
475
|
+
else if (side.truncated === 'budget') root.appendChild(el('div', 'note warn', 'Capture stopped early because the inspector memory budget was full. The app and the visitor still received all of it.'));
|
|
476
|
+
else if (side.truncated === 'tunnel') root.appendChild(el('div', 'note warn', 'The legacy v2 tunnel truncated this response to 1 MiB for the visitor.'));
|
|
477
|
+
if (!side.complete && d.done && side.bytes) root.appendChild(el('div', 'note warn', 'This body did not finish (the transfer was cancelled or failed).'));
|
|
478
|
+
if (!side.complete && !d.done && sideName === 'response' && d.status) root.appendChild(el('div', 'note', 'Still streaming…'));
|
|
479
|
+
|
|
480
|
+
if (!side.retained) { root.appendChild(el('pre', '', side.bytes ? '(not captured)' : '(empty body)')); return; }
|
|
481
|
+
var raw = bytesFrom64(side.base64);
|
|
482
|
+
var shownBytes = raw;
|
|
483
|
+
if (side.decodedBase64 && view !== 'hex') {
|
|
484
|
+
shownBytes = bytesFrom64(side.decodedBase64);
|
|
485
|
+
root.appendChild(el('div', 'note', 'Decoded from Content-Encoding: ' + side.contentEncoding + (side.decodedPartial ? ' (partial body)' : '') + '. Hex and Download show the raw bytes.'));
|
|
486
|
+
} else if (side.decodeError && view !== 'hex') root.appendChild(el('div', 'note warn', side.decodeError));
|
|
487
|
+
if (view === 'hex') { root.appendChild(el('pre', 'hex', hexDump(raw))); return; }
|
|
488
|
+
if (view === 'raw') {
|
|
489
|
+
var text = utf8(shownBytes, false);
|
|
490
|
+
root.appendChild(el('pre', '', text));
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
root.appendChild(el('pre', '', pretty(shownBytes, side.contentType || '')));
|
|
494
|
+
}
|
|
495
|
+
function pretty(bytes, contentType) {
|
|
496
|
+
var text = utf8(bytes, true);
|
|
497
|
+
if (text === null || looksBinary(text)) return 'Binary body (' + fmtBytes(bytes.length) + '). Use Hex or Download.';
|
|
498
|
+
var trimmed = text.trim();
|
|
499
|
+
if (/json/i.test(contentType) || /^[\[{]/.test(trimmed)) {
|
|
500
|
+
try { return JSON.stringify(JSON.parse(trimmed), null, 2); } catch (e) { }
|
|
501
|
+
}
|
|
502
|
+
if (/x-www-form-urlencoded/i.test(contentType)) {
|
|
503
|
+
try {
|
|
504
|
+
return trimmed.split('&').filter(Boolean).map(function (part) {
|
|
505
|
+
var eq = part.indexOf('=');
|
|
506
|
+
var key = eq === -1 ? part : part.slice(0, eq);
|
|
507
|
+
var value = eq === -1 ? '' : part.slice(eq + 1);
|
|
508
|
+
return decodeURIComponent(key.replace(/\+/g, ' ')) + ' = ' + decodeURIComponent(value.replace(/\+/g, ' '));
|
|
509
|
+
}).join('\n');
|
|
510
|
+
} catch (e) { }
|
|
511
|
+
}
|
|
512
|
+
return text;
|
|
513
|
+
}
|
|
514
|
+
function hexDump(bytes) {
|
|
515
|
+
var lines = [];
|
|
516
|
+
var n = Math.min(bytes.length, HEX_LIMIT);
|
|
517
|
+
for (var i = 0; i < n; i += 16) {
|
|
518
|
+
var hex = [];
|
|
519
|
+
var ascii = '';
|
|
520
|
+
for (var j = i; j < i + 16; j++) {
|
|
521
|
+
if (j < n) {
|
|
522
|
+
var b = bytes[j];
|
|
523
|
+
hex.push((b < 16 ? '0' : '') + b.toString(16));
|
|
524
|
+
ascii += b >= 32 && b < 127 ? String.fromCharCode(b) : '.';
|
|
525
|
+
} else hex.push(' ');
|
|
526
|
+
}
|
|
527
|
+
lines.push(pad(i.toString(16), 8) + ' ' + hex.slice(0, 8).join(' ') + ' ' + hex.slice(8).join(' ') + ' |' + ascii + '|');
|
|
528
|
+
}
|
|
529
|
+
if (bytes.length > HEX_LIMIT) lines.push('… first 4 KiB of ' + bytes.length + ' bytes shown. Use Download for the rest.');
|
|
530
|
+
return lines.join('\n');
|
|
531
|
+
}
|
|
532
|
+
function renderWebSocket(root, d) {
|
|
533
|
+
var ws = d.websocket;
|
|
534
|
+
root.appendChild(el('h3', '', 'WebSocket'));
|
|
535
|
+
var rows = [
|
|
536
|
+
['Frames visitor → app', ws.framesIn + ' (' + fmtBytes(ws.bytesIn) + ')'],
|
|
537
|
+
['Frames app → visitor', ws.framesOut + ' (' + fmtBytes(ws.bytesOut) + ')'],
|
|
538
|
+
['State', d.done ? 'closed' : (d.status === 101 ? 'open' : 'connecting')],
|
|
539
|
+
];
|
|
540
|
+
if (ws.closeCode !== null) rows.push(['Close code', String(ws.closeCode) + (ws.closeReason ? ' ' + ws.closeReason : '')]);
|
|
541
|
+
root.appendChild(headerTableStatic(rows));
|
|
542
|
+
root.appendChild(el('div', 'note', 'Frame contents are not recorded yet. WebSocket connections cannot be replayed.'));
|
|
543
|
+
root.appendChild(el('h3', '', 'Handshake headers sent to the local app'));
|
|
544
|
+
root.appendChild(headerTable(d.request.headers, 'request'));
|
|
545
|
+
if (d.response.headers.length) {
|
|
546
|
+
root.appendChild(el('h3', '', 'Handshake response headers'));
|
|
547
|
+
root.appendChild(headerTable(d.response.headers, 'response'));
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function headerTableStatic(rows) {
|
|
551
|
+
var table = el('table', 'headers');
|
|
552
|
+
var body = el('tbody');
|
|
553
|
+
rows.forEach(function (row) {
|
|
554
|
+
var tr = el('tr');
|
|
555
|
+
tr.appendChild(el('td', 'name', row[0]));
|
|
556
|
+
tr.appendChild(el('td', '', row[1]));
|
|
557
|
+
body.appendChild(tr);
|
|
558
|
+
});
|
|
559
|
+
table.appendChild(body);
|
|
560
|
+
return table;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// ---------- actions ----------
|
|
564
|
+
function doReplay(id, button) {
|
|
565
|
+
button.disabled = true;
|
|
566
|
+
api('/api/requests/' + id + '/replay', { method: 'POST', headers: MUTATE, body: '{}' }).then(function (body) {
|
|
567
|
+
toast('Replayed as #' + body.id);
|
|
568
|
+
if (body.id) select(body.id);
|
|
569
|
+
}).catch(function (error) {
|
|
570
|
+
toast('Replay failed: ' + error.message, 5000);
|
|
571
|
+
button.disabled = false;
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// ---------- edit & replay ----------
|
|
576
|
+
// Unchanged bodies are sent as the captured bytes (base64), so CRLF line endings, a BOM or
|
|
577
|
+
// binary data survive; the textarea only becomes the body once the user edits it.
|
|
578
|
+
var ed = null;
|
|
579
|
+
function textOrNull(bytes) {
|
|
580
|
+
var text;
|
|
581
|
+
try { text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes); } catch (e) { return null; }
|
|
582
|
+
return looksBinary(text) ? null : text;
|
|
583
|
+
}
|
|
584
|
+
function openEditor(d, opener) {
|
|
585
|
+
var req = d.request;
|
|
586
|
+
var raw = bytesFrom64(req.base64);
|
|
587
|
+
var text = textOrNull(raw);
|
|
588
|
+
ed = { id: d.id, opener: opener, base64: req.base64 || '', raw: raw, text: text, encoding: req.contentEncoding || '',
|
|
589
|
+
decoded: req.decodedBase64 || '', partial: !!req.truncated || !req.complete, truncated: req.truncated, bytes: req.bytes,
|
|
590
|
+
mode: text === null ? 'bytes' : 'text', touched: false, converted: '', sending: false };
|
|
591
|
+
$('ed-title').textContent = 'Edit & replay #' + d.id;
|
|
592
|
+
$('ed-sub').textContent = 'Sends a new request straight to ' + (localBase() || 'the local app') + ' (not through the tunnel). Request #' + d.id + ' stays as captured.';
|
|
593
|
+
var methodNote = $('ed-method-note');
|
|
594
|
+
clear(methodNote);
|
|
595
|
+
if (EDIT_METHODS.indexOf(d.method) === -1) {
|
|
596
|
+
$('ed-method').value = 'GET';
|
|
597
|
+
methodNote.appendChild(el('div', 'note warn', 'The captured method ' + d.method + ' cannot be sent from the editor; pick one of ' + EDIT_METHODS.join(', ') + '.'));
|
|
598
|
+
} else $('ed-method').value = d.method;
|
|
599
|
+
$('ed-path').value = d.path;
|
|
600
|
+
$('ed-headers').value = req.headers.filter(function (pair) { return !REGENERATED[pair[0].toLowerCase()]; })
|
|
601
|
+
.map(function (pair) { return pair[0] + ': ' + pair[1]; }).join('\n');
|
|
602
|
+
$('ed-body').value = text === null ? '' : text;
|
|
603
|
+
var crlf = text !== null && text.indexOf('\r\n') !== -1;
|
|
604
|
+
$('ed-eol').hidden = !(text !== null && text.indexOf('\r') !== -1);
|
|
605
|
+
$('ed-eol').value = crlf ? 'crlf' : 'lf';
|
|
606
|
+
$('ed-confirm').checked = false;
|
|
607
|
+
editorError('');
|
|
608
|
+
renderEditorBody();
|
|
609
|
+
$('editor').showModal();
|
|
610
|
+
$('ed-path').focus();
|
|
611
|
+
}
|
|
612
|
+
function renderEditorBody() {
|
|
613
|
+
var notes = $('ed-notes');
|
|
614
|
+
clear(notes);
|
|
615
|
+
var asIs = ed.mode === 'bytes' || !ed.touched;
|
|
616
|
+
if (ed.partial && asIs && ed.raw.length) {
|
|
617
|
+
notes.appendChild(el('div', 'note warn', ed.truncated
|
|
618
|
+
? 'Partial body: only the first ' + fmtBytes(ed.raw.length) + ' of ' + fmtBytes(ed.bytes) + ' were captured. Confirm below to send it as shown, or replace it.'
|
|
619
|
+
: 'Partial body: the captured body never finished arriving (' + fmtBytes(ed.raw.length) + ' captured). Confirm below to send it as shown, or replace it.'));
|
|
620
|
+
} else if (ed.partial && ed.raw.length && ed.converted !== 'empty') {
|
|
621
|
+
notes.appendChild(el('div', 'note warn', 'This body started from a partial capture; check that it is complete before sending.'));
|
|
622
|
+
}
|
|
623
|
+
if (ed.mode === 'bytes') {
|
|
624
|
+
notes.appendChild(el('div', 'note', 'The captured body is not UTF-8 text' + (ed.encoding ? ' (Content-Encoding: ' + ed.encoding + ')' : '') + '. Its ' + fmtBytes(ed.raw.length) + ' of original bytes are sent unchanged unless you switch to editing it as text.'));
|
|
625
|
+
} else if (ed.converted === 'decoded' || ed.converted === 'lossy') {
|
|
626
|
+
notes.appendChild(el('div', 'note warn', ed.converted === 'decoded'
|
|
627
|
+
? 'Shown decoded from ' + ed.encoding + '. The text is sent as is, so remove the Content-Encoding header or the app will try to decompress it.'
|
|
628
|
+
: 'Shown as text: bytes that are not valid UTF-8 became U+FFFD. The text below is sent, not the original bytes.'));
|
|
629
|
+
}
|
|
630
|
+
$('ed-body').hidden = ed.mode === 'bytes';
|
|
631
|
+
$('ed-astext').hidden = ed.mode !== 'bytes';
|
|
632
|
+
$('ed-restore').hidden = asIs && ed.mode === (ed.text === null ? 'bytes' : 'text');
|
|
633
|
+
$('ed-confirm-row').hidden = !(ed.partial && asIs && ed.raw.length);
|
|
634
|
+
var size = ed.mode === 'bytes' ? ed.raw.length : new TextEncoder().encode($('ed-body').value).length;
|
|
635
|
+
$('ed-bodyinfo').textContent = fmtBytes(size) + (asIs ? ', as captured' : ', edited');
|
|
636
|
+
}
|
|
637
|
+
function editorError(message) {
|
|
638
|
+
var node = $('ed-error');
|
|
639
|
+
node.textContent = message;
|
|
640
|
+
node.hidden = !message;
|
|
641
|
+
}
|
|
642
|
+
function editorPayload() {
|
|
643
|
+
var pairs = [];
|
|
644
|
+
var lines = $('ed-headers').value.split('\n');
|
|
645
|
+
for (var i = 0; i < lines.length; i++) {
|
|
646
|
+
if (!lines[i].trim()) continue;
|
|
647
|
+
var colon = lines[i].indexOf(':');
|
|
648
|
+
if (colon <= 0) return { error: 'Header line ' + (i + 1) + ' must look like "name: value".' };
|
|
649
|
+
pairs.push([lines[i].slice(0, colon).trim(), lines[i].slice(colon + 1).trim()]);
|
|
650
|
+
}
|
|
651
|
+
var payload = { replayOf: ed.id, method: $('ed-method').value, path: $('ed-path').value, headers: pairs };
|
|
652
|
+
if (ed.mode === 'bytes' || !ed.touched) {
|
|
653
|
+
if (ed.partial && ed.raw.length && !$('ed-confirm').checked) return { error: 'The body shown is partial. Tick "Send this partial body as shown", or replace or empty the body.', focus: 'ed-confirm' };
|
|
654
|
+
payload.body = ed.base64;
|
|
655
|
+
payload.bodyEncoding = 'base64';
|
|
656
|
+
} else {
|
|
657
|
+
var text = $('ed-body').value;
|
|
658
|
+
if (!$('ed-eol').hidden && $('ed-eol').value === 'crlf') text = text.replace(/\r?\n/g, '\r\n');
|
|
659
|
+
payload.body = text;
|
|
660
|
+
payload.bodyEncoding = 'text';
|
|
661
|
+
}
|
|
662
|
+
return { payload: payload };
|
|
663
|
+
}
|
|
664
|
+
function sendEditor() {
|
|
665
|
+
if (!ed || ed.sending) return;
|
|
666
|
+
var built = editorPayload();
|
|
667
|
+
if (built.error) { editorError(built.error); if (built.focus) $(built.focus).focus(); return; }
|
|
668
|
+
editorError('');
|
|
669
|
+
ed.sending = true;
|
|
670
|
+
$('ed-send').disabled = true;
|
|
671
|
+
var current = ed;
|
|
672
|
+
api('/api/replay', { method: 'POST', headers: MUTATE, body: JSON.stringify(built.payload) }).then(function (body) {
|
|
673
|
+
if (ed === current) $('editor').close();
|
|
674
|
+
toast('Sent edited request as #' + body.id);
|
|
675
|
+
if (body.id) select(body.id);
|
|
676
|
+
}).catch(function (error) {
|
|
677
|
+
if (ed === current) editorError('Not sent: ' + error.message);
|
|
678
|
+
}).then(function () {
|
|
679
|
+
current.sending = false;
|
|
680
|
+
$('ed-send').disabled = false;
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
function touchBody() { ed.touched = true; renderEditorBody(); }
|
|
684
|
+
$('ed-body').addEventListener('input', function () { if (ed && !ed.touched) touchBody(); else if (ed) renderEditorBody(); });
|
|
685
|
+
$('ed-eol').addEventListener('change', function () { if (ed) touchBody(); });
|
|
686
|
+
$('ed-confirm').addEventListener('change', function () { if ($('ed-confirm').checked) editorError(''); });
|
|
687
|
+
$('ed-astext').addEventListener('click', function () {
|
|
688
|
+
var decoded = ed.decoded ? textOrNull(bytesFrom64(ed.decoded)) : null;
|
|
689
|
+
ed.converted = decoded !== null ? 'decoded' : 'lossy';
|
|
690
|
+
$('ed-body').value = decoded !== null ? decoded : utf8(ed.raw, false);
|
|
691
|
+
ed.mode = 'text';
|
|
692
|
+
touchBody();
|
|
693
|
+
$('ed-body').focus();
|
|
694
|
+
});
|
|
695
|
+
$('ed-restore').addEventListener('click', function () {
|
|
696
|
+
ed.mode = ed.text === null ? 'bytes' : 'text';
|
|
697
|
+
ed.touched = false;
|
|
698
|
+
ed.converted = '';
|
|
699
|
+
$('ed-body').value = ed.text === null ? '' : ed.text;
|
|
700
|
+
$('ed-eol').value = ed.text !== null && ed.text.indexOf('\r\n') !== -1 ? 'crlf' : 'lf';
|
|
701
|
+
renderEditorBody();
|
|
702
|
+
});
|
|
703
|
+
$('ed-empty').addEventListener('click', function () {
|
|
704
|
+
ed.mode = 'text';
|
|
705
|
+
ed.converted = 'empty';
|
|
706
|
+
$('ed-body').value = '';
|
|
707
|
+
touchBody();
|
|
708
|
+
$('ed-body').focus();
|
|
709
|
+
});
|
|
710
|
+
$('ed-send').addEventListener('click', sendEditor);
|
|
711
|
+
$('ed-cancel').addEventListener('click', function () { $('editor').close(); });
|
|
712
|
+
$('editor').addEventListener('keydown', function (event) {
|
|
713
|
+
if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) { event.preventDefault(); sendEditor(); }
|
|
714
|
+
});
|
|
715
|
+
$('editor').addEventListener('close', function () {
|
|
716
|
+
var opener = ed && ed.opener;
|
|
717
|
+
ed = null;
|
|
718
|
+
// Live updates re-render the detail pane, so the button that opened the editor may be gone.
|
|
719
|
+
var target = opener && opener.isConnected ? opener : $('d-edit');
|
|
720
|
+
if (target) target.focus();
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
function shellQuote(value) { return "'" + String(value).replace(/'/g, "'\\''") + "'"; }
|
|
724
|
+
function publicBase(d) {
|
|
725
|
+
var url = (status && status.publicUrl) || d.publicUrl;
|
|
726
|
+
return url ? String(url).replace(/\/+$/, '') : '';
|
|
727
|
+
}
|
|
728
|
+
function localBase() { return status && status.target ? status.target : ''; }
|
|
729
|
+
function curlCommand(d, where) {
|
|
730
|
+
var omit = Object.assign({}, CURL_OMIT);
|
|
731
|
+
d.request.headers.forEach(function (pair) {
|
|
732
|
+
if (pair[0].toLowerCase() === 'connection') pair[1].split(',').forEach(function (name) { omit[name.trim().toLowerCase()] = 1; });
|
|
733
|
+
});
|
|
734
|
+
var args = ['curl --globoff --path-as-is', '--request ' + shellQuote(d.method)];
|
|
735
|
+
d.request.headers.forEach(function (pair) {
|
|
736
|
+
if (!omit[pair[0].toLowerCase()]) args.push('--header ' + shellQuote(pair[0] + ': ' + pair[1]));
|
|
737
|
+
});
|
|
738
|
+
args.push('--url ' + shellQuote((where === 'public' ? publicBase(d) : localBase()) + d.path));
|
|
739
|
+
if (d.request.retained) {
|
|
740
|
+
args.push('--data-binary @-');
|
|
741
|
+
return 'printf ' + shellQuote('%s') + ' ' + shellQuote(d.request.base64) + ' | base64 -d | ' + args.join(' \\\n ');
|
|
742
|
+
}
|
|
743
|
+
return args.join(' \\\n ');
|
|
744
|
+
}
|
|
745
|
+
function copyCurl(d, where) {
|
|
746
|
+
var command = curlCommand(d, where);
|
|
747
|
+
var warning = 'Copied. The command contains captured header values (for example Authorization or Cookie); treat it as a secret.';
|
|
748
|
+
var fallback = function () {
|
|
749
|
+
var root = $('detail');
|
|
750
|
+
var box = el('div', 'copybox');
|
|
751
|
+
box.appendChild(el('div', 'note warn', 'Clipboard access is unavailable. Copy the command below; it may contain secrets.'));
|
|
752
|
+
var area = el('textarea');
|
|
753
|
+
area.value = command;
|
|
754
|
+
area.readOnly = true;
|
|
755
|
+
box.appendChild(area);
|
|
756
|
+
root.appendChild(box);
|
|
757
|
+
area.focus();
|
|
758
|
+
area.select();
|
|
759
|
+
};
|
|
760
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
761
|
+
navigator.clipboard.writeText(command).then(function () { toast(warning, 5000); }, fallback);
|
|
762
|
+
} else fallback();
|
|
763
|
+
}
|
|
764
|
+
$('clear').addEventListener('click', function () {
|
|
765
|
+
api('/api/requests', { method: 'DELETE', headers: MUTATE, body: '{}' }).catch(function (error) { toast('Clear failed: ' + error.message); });
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
// ---------- data ----------
|
|
769
|
+
function loadStatus() {
|
|
770
|
+
return api('/api/status').then(function (body) {
|
|
771
|
+
status = body;
|
|
772
|
+
var meta = $('meta');
|
|
773
|
+
clear(meta);
|
|
774
|
+
meta.appendChild(el('b', '', body.publicUrl || 'Tunnel not connected'));
|
|
775
|
+
meta.appendChild(document.createTextNode(' → ' + body.target));
|
|
776
|
+
var stats = body.inspector || {};
|
|
777
|
+
$('stats').textContent = (stats.entries || 0) + ' / ' + stats.maxEntries + ' entries · ' + fmtBytes(stats.bytes || 0) + ' of ' + fmtBytes(stats.maxBytes) + ' memory';
|
|
778
|
+
}).catch(function () { });
|
|
779
|
+
}
|
|
780
|
+
function loadAll() {
|
|
781
|
+
return api('/api/requests').then(function (body) {
|
|
782
|
+
entries.clear();
|
|
783
|
+
body.entries.forEach(function (entry) { entries.set(entry.id, entry); });
|
|
784
|
+
if (selected !== null && !entries.has(selected)) { selected = null; detail = null; renderMissing(); }
|
|
785
|
+
queueRender();
|
|
786
|
+
if (selected !== null) loadDetail();
|
|
787
|
+
}).catch(function () { });
|
|
788
|
+
}
|
|
789
|
+
function scheduleStatus() {
|
|
790
|
+
if (scheduleStatus.timer) return;
|
|
791
|
+
scheduleStatus.timer = setTimeout(function () { scheduleStatus.timer = null; loadStatus(); }, 1000);
|
|
792
|
+
}
|
|
793
|
+
function upsert(entry) {
|
|
794
|
+
var isNew = !entries.has(entry.id);
|
|
795
|
+
entries.set(entry.id, entry);
|
|
796
|
+
if (isNew || entry.done) scheduleStatus();
|
|
797
|
+
queueRender();
|
|
798
|
+
if (entry.id === selected) scheduleDetail();
|
|
799
|
+
if (detail && detail.replays && detail.replays.indexOf(entry.id) !== -1 && entry.id !== selected) scheduleDetail();
|
|
800
|
+
}
|
|
801
|
+
function connect() {
|
|
802
|
+
var source = new EventSource('/api/events');
|
|
803
|
+
source.addEventListener('open', function () {
|
|
804
|
+
$('conn').textContent = 'Live';
|
|
805
|
+
$('conn').className = 'conn live';
|
|
806
|
+
loadAll();
|
|
807
|
+
loadStatus();
|
|
808
|
+
});
|
|
809
|
+
source.addEventListener('error', function () {
|
|
810
|
+
$('conn').textContent = 'Reconnecting…';
|
|
811
|
+
$('conn').className = 'conn';
|
|
812
|
+
});
|
|
813
|
+
source.addEventListener('created', function (event) { upsert(JSON.parse(event.data)); });
|
|
814
|
+
source.addEventListener('updated', function (event) { upsert(JSON.parse(event.data)); });
|
|
815
|
+
source.addEventListener('evicted', function (event) {
|
|
816
|
+
JSON.parse(event.data).ids.forEach(function (id) { entries.delete(id); });
|
|
817
|
+
scheduleStatus();
|
|
818
|
+
if (selected !== null && !entries.has(selected)) { selected = null; detail = null; renderMissing('That request was evicted to stay within the memory limit.'); }
|
|
819
|
+
queueRender();
|
|
820
|
+
});
|
|
821
|
+
source.addEventListener('cleared', function () {
|
|
822
|
+
entries.clear();
|
|
823
|
+
selected = null;
|
|
824
|
+
detail = null;
|
|
825
|
+
renderMissing();
|
|
826
|
+
queueRender();
|
|
827
|
+
loadStatus();
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
setInterval(loadStatus, 10000);
|
|
831
|
+
connect();
|
|
832
|
+
})();
|
|
833
|
+
</script>
|
|
834
|
+
</body>
|
|
835
|
+
</html>
|
|
836
|
+
`
|
|
837
|
+
}
|