repoburg 1.3.290 → 1.3.291
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/daemon/dist/index.js +1 -0
- package/daemon/dist/tabtape/api/router.js +36 -1
- package/daemon/dist/tabtape/api/router.spec.js +47 -0
- package/daemon/dist/tabtape/e2e-server.js +1 -0
- package/daemon/dist/tabtape/index.js +10 -0
- package/daemon/dist/tabtape/index.spec.js +35 -2
- package/daemon/dist/tabtape/mcp/definitions.js +14 -3
- package/daemon/dist/tabtape/mcp/server.js +2 -1
- package/daemon/dist/tabtape/mcp/server.spec.js +50 -2
- package/daemon/dist/tabtape/mcp/tools.js +19 -0
- package/daemon/dist/tabtape/proxy/proxy.js +361 -0
- package/daemon/dist/tabtape/proxy/proxy.spec.js +497 -0
- package/daemon/dist/tabtape/proxy/rewrite.js +176 -0
- package/daemon/dist/tabtape/proxy/rewrite.spec.js +140 -0
- package/package.json +1 -1
- package/tabtape/extension/dist/background.js +4 -4
- package/tabtape/extension/dist/background.js.map +4 -4
- package/tabtape/extension/dist/bridge.js +2 -2
- package/tabtape/extension/dist/bridge.js.map +2 -2
- package/tabtape/extension/dist/build-info.json +4 -4
- package/tabtape/extension/dist/manifest.json +1 -1
- package/tabtape/extension/dist/popup.js +1 -1
- package/tabtape/extension/dist/popup.js.map +2 -2
- package/tabtape/extension/dist/proxy-bridge.js +3 -0
- package/tabtape/extension/dist/proxy-bridge.js.map +1 -0
- package/tabtape/extension/dist/recorder.js +20 -20
- package/tabtape/extension/dist/recorder.js.map +1 -1
- package/tabtape/extension/dist/state.js +1 -1
- package/tabtape/extension/dist/state.js.map +1 -1
- package/tabtape/playwright/tabtape.d.mts +17 -0
- package/tabtape/playwright/tabtape.mjs +40 -2
- package/tabtape/ui/dist/build-info.json +4 -4
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Proxies = exports.ProxyInputError = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Recording without the extension: a proxy in front of an app, on a loopback
|
|
9
|
+
* port of its own, one per recording. It forwards everything to the app and
|
|
10
|
+
* puts the recorder into the pages it serves, so any browser that opens the
|
|
11
|
+
* proxy's URL is recorded, in any Playwright context, and always with the
|
|
12
|
+
* recorder this daemon ships.
|
|
13
|
+
*
|
|
14
|
+
* recorder.js in the page --> POST /__tabtape/events on the page's origin
|
|
15
|
+
* --> the proxy, in this process --> the store
|
|
16
|
+
*
|
|
17
|
+
* The lines take this way because the page cannot write to tabtape's API:
|
|
18
|
+
* that stays closed to web pages (api/router.ts). The events route has the
|
|
19
|
+
* same protection, the client header a foreign page cannot send, and like the
|
|
20
|
+
* API the proxy serves loopback Host names only (DNS rebinding).
|
|
21
|
+
*
|
|
22
|
+
* The session starts with the first page, so a proxy nobody opened leaves
|
|
23
|
+
* nothing behind. There is no deep mode: it needs the debugger.
|
|
24
|
+
*/
|
|
25
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
26
|
+
const fs_1 = __importDefault(require("fs"));
|
|
27
|
+
const http_1 = __importDefault(require("http"));
|
|
28
|
+
const https_1 = __importDefault(require("https"));
|
|
29
|
+
const path_1 = __importDefault(require("path"));
|
|
30
|
+
const stream_1 = require("stream");
|
|
31
|
+
const http_2 = require("../api/http");
|
|
32
|
+
const errors_1 = require("../errors");
|
|
33
|
+
const record_1 = require("../store/record");
|
|
34
|
+
const rewrite_1 = require("./rewrite");
|
|
35
|
+
/** Must accompany the lines of a page: cross-origin it needs a preflight, which nothing answers. */
|
|
36
|
+
const CLIENT_HEADER = 'x-tabtape-client';
|
|
37
|
+
const EVENTS_PATH = `${rewrite_1.PROXY_PATH}events`;
|
|
38
|
+
const SCRIPT_FILES = new Set(rewrite_1.PAGE_SCRIPTS.flatMap((s) => [s, `${s}.map`]));
|
|
39
|
+
const DEFAULT_MAX_BATCH_BYTES = 64 << 20;
|
|
40
|
+
/** A proxy nobody stopped goes away after this long without a request. */
|
|
41
|
+
const DEFAULT_IDLE_MS = 30 * 60000;
|
|
42
|
+
/** Pages send their lines every 250 ms: what they still hold gets here within this time. */
|
|
43
|
+
const DEFAULT_STOP_GRACE_MS = 600;
|
|
44
|
+
const MAX_PROXIES = 16;
|
|
45
|
+
/** The caller's mistake, as opposed to a failure of the proxy. */
|
|
46
|
+
class ProxyInputError extends Error {
|
|
47
|
+
constructor(message) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.name = 'ProxyInputError';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.ProxyInputError = ProxyInputError;
|
|
53
|
+
/** The recordings in progress. */
|
|
54
|
+
class Proxies {
|
|
55
|
+
constructor(opts) {
|
|
56
|
+
this.opts = opts;
|
|
57
|
+
this.active = new Map();
|
|
58
|
+
}
|
|
59
|
+
async start(input) {
|
|
60
|
+
const target = parseTarget(input.url);
|
|
61
|
+
const scriptsDir = this.opts.scriptsDir;
|
|
62
|
+
if (!scriptsDir || !fs_1.default.existsSync(path_1.default.join(scriptsDir, rewrite_1.PAGE_SCRIPTS[0]))) {
|
|
63
|
+
throw new Error('the recorder is not built in this Repoburg install (tabtape/extension/dist)');
|
|
64
|
+
}
|
|
65
|
+
if (this.active.size >= MAX_PROXIES)
|
|
66
|
+
throw new ProxyInputError(`too many proxy recordings at once (at most ${MAX_PROXIES}); stop one first`);
|
|
67
|
+
const id = newSessionId();
|
|
68
|
+
const proxy = new RecordingProxy(id, target, input.name?.trim() ?? '', scriptsDir, this.opts, () => this.active.delete(id));
|
|
69
|
+
const port = await proxy.listen();
|
|
70
|
+
this.active.set(id, proxy);
|
|
71
|
+
return { id, url: `http://127.0.0.1:${port}${target.pathname}${target.search}${target.hash}`, target: target.href, replay: this.replay(id) };
|
|
72
|
+
}
|
|
73
|
+
/** Stops a recording; one that stopped on its own already is reported like any other. */
|
|
74
|
+
async stop(id) {
|
|
75
|
+
const proxy = this.active.get(id);
|
|
76
|
+
if (proxy)
|
|
77
|
+
return { id, recorded: await proxy.stop(this.opts.stopGraceMs ?? DEFAULT_STOP_GRACE_MS), replay: this.replay(id) };
|
|
78
|
+
if (this.opts.st.has(id))
|
|
79
|
+
return { id, recorded: true, replay: this.replay(id) };
|
|
80
|
+
throw new ProxyInputError(`no proxy recording ${id}`);
|
|
81
|
+
}
|
|
82
|
+
/** The daemon is shutting down. */
|
|
83
|
+
async close() {
|
|
84
|
+
await Promise.all([...this.active.values()].map((p) => p.stop(0)));
|
|
85
|
+
}
|
|
86
|
+
replay(id) {
|
|
87
|
+
return `${this.opts.baseURL}/#/s/${id}`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.Proxies = Proxies;
|
|
91
|
+
function parseTarget(raw) {
|
|
92
|
+
let target;
|
|
93
|
+
try {
|
|
94
|
+
target = new URL(raw.trim());
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
throw new ProxyInputError(`url ${JSON.stringify(raw)}: not a URL`);
|
|
98
|
+
}
|
|
99
|
+
if (target.protocol !== 'http:' && target.protocol !== 'https:') {
|
|
100
|
+
throw new ProxyInputError(`url ${JSON.stringify(raw)}: only http and https pages can go through the proxy`);
|
|
101
|
+
}
|
|
102
|
+
return target;
|
|
103
|
+
}
|
|
104
|
+
/** Same form as the extension's (background.ts). */
|
|
105
|
+
function newSessionId() {
|
|
106
|
+
const d = new Date();
|
|
107
|
+
const p = (n) => String(n).padStart(2, '0');
|
|
108
|
+
const day = `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}`;
|
|
109
|
+
return `${day}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}-${crypto_1.default.randomBytes(3).toString('hex')}`;
|
|
110
|
+
}
|
|
111
|
+
class RecordingProxy {
|
|
112
|
+
constructor(id, target, name, scriptsDir, opts, onStopped) {
|
|
113
|
+
this.id = id;
|
|
114
|
+
this.target = target;
|
|
115
|
+
this.name = name;
|
|
116
|
+
this.scriptsDir = scriptsDir;
|
|
117
|
+
this.opts = opts;
|
|
118
|
+
this.onStopped = onStopped;
|
|
119
|
+
/** Upgraded connections: the server no longer knows them. */
|
|
120
|
+
this.sockets = new Set();
|
|
121
|
+
this.recording = false;
|
|
122
|
+
this.stopping = null;
|
|
123
|
+
this.client = target.protocol === 'https:' ? https_1.default : http_1.default;
|
|
124
|
+
this.server = http_1.default.createServer((req, res) => {
|
|
125
|
+
this.handle(req, res).catch((err) => {
|
|
126
|
+
opts.logger.error('proxy request failed', { session: id, err });
|
|
127
|
+
(0, http_2.sendText)(res, 500, 'internal error');
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
this.server.on('error', (err) => opts.logger.warn('proxy: server error', { session: id, err }));
|
|
131
|
+
this.server.on('upgrade', (req, socket, head) => {
|
|
132
|
+
try {
|
|
133
|
+
this.upgrade(req, socket, head);
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
opts.logger.warn('proxy: upgrade failed', { session: id, err });
|
|
137
|
+
socket.destroy();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
listen() {
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
this.server.once('error', reject);
|
|
144
|
+
this.server.listen(0, '127.0.0.1', () => {
|
|
145
|
+
this.server.off('error', reject);
|
|
146
|
+
this.touch();
|
|
147
|
+
resolve(this.server.address().port);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/** Resolves to whether anything was recorded. */
|
|
152
|
+
stop(graceMs) {
|
|
153
|
+
this.stopping ?? (this.stopping = (async () => {
|
|
154
|
+
clearTimeout(this.idle);
|
|
155
|
+
if (graceMs > 0)
|
|
156
|
+
await new Promise((r) => setTimeout(r, graceMs));
|
|
157
|
+
this.onStopped();
|
|
158
|
+
this.server.close();
|
|
159
|
+
this.server.closeAllConnections();
|
|
160
|
+
for (const s of this.sockets)
|
|
161
|
+
s.destroy();
|
|
162
|
+
if (this.recording)
|
|
163
|
+
this.opts.st.end(this.id, Date.now());
|
|
164
|
+
return this.recording;
|
|
165
|
+
})());
|
|
166
|
+
return this.stopping;
|
|
167
|
+
}
|
|
168
|
+
touch() {
|
|
169
|
+
if (this.stopping)
|
|
170
|
+
return;
|
|
171
|
+
clearTimeout(this.idle);
|
|
172
|
+
this.idle = setTimeout(() => void this.stop(0), this.opts.idleMs ?? DEFAULT_IDLE_MS);
|
|
173
|
+
this.idle.unref();
|
|
174
|
+
}
|
|
175
|
+
/** The origin the browser sees; both localhost and 127.0.0.1 reach this port. */
|
|
176
|
+
proxyOrigin(req) {
|
|
177
|
+
return `http://${req.headers.host}`;
|
|
178
|
+
}
|
|
179
|
+
async handle(req, res) {
|
|
180
|
+
if (!(0, http_2.loopbackHost)(req.headers.host)) {
|
|
181
|
+
(0, http_2.sendText)(res, 403, 'forbidden host');
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
this.touch();
|
|
185
|
+
const url = req.url ?? '';
|
|
186
|
+
if (!url.startsWith('/')) {
|
|
187
|
+
(0, http_2.sendText)(res, 400, 'bad request target');
|
|
188
|
+
}
|
|
189
|
+
else if (url === EVENTS_PATH) {
|
|
190
|
+
await this.events(req, res);
|
|
191
|
+
}
|
|
192
|
+
else if (url.startsWith(rewrite_1.PROXY_PATH)) {
|
|
193
|
+
await this.script(url.slice(rewrite_1.PROXY_PATH.length), res);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
this.forward(req, res);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
async events(req, res) {
|
|
200
|
+
if (req.method !== 'POST' || !req.headers[CLIENT_HEADER]) {
|
|
201
|
+
(0, http_2.sendText)(res, 403, 'forbidden');
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
let body;
|
|
205
|
+
try {
|
|
206
|
+
body = await (0, http_2.readBody)(req, this.opts.maxBatchBytes ?? DEFAULT_MAX_BATCH_BYTES);
|
|
207
|
+
}
|
|
208
|
+
catch (err) {
|
|
209
|
+
if (err instanceof http_2.TooLargeError)
|
|
210
|
+
(0, http_2.sendText)(res, 413, 'batch too large');
|
|
211
|
+
else
|
|
212
|
+
(0, http_2.sendText)(res, 400, (0, errors_1.errorMessage)(err));
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
// Recordings name the app, not this port. It closes with the recording,
|
|
216
|
+
// while replays keep loading images and fonts from the app.
|
|
217
|
+
const lines = body.toString('utf8').split(this.proxyOrigin(req)).join(this.target.origin);
|
|
218
|
+
try {
|
|
219
|
+
this.opts.st.append(this.id, (0, record_1.readRecords)(lines));
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
if (err instanceof errors_1.RecordError)
|
|
223
|
+
(0, http_2.sendText)(res, 400, err.message);
|
|
224
|
+
else
|
|
225
|
+
throw err;
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
(0, http_2.sendStatus)(res, 204);
|
|
229
|
+
}
|
|
230
|
+
async script(name, res) {
|
|
231
|
+
if (!SCRIPT_FILES.has(name)) {
|
|
232
|
+
(0, http_2.sendText)(res, 404, '404 page not found');
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const body = await fs_1.default.promises.readFile(path_1.default.join(this.scriptsDir, name));
|
|
236
|
+
res.writeHead(200, {
|
|
237
|
+
'Content-Type': name.endsWith('.map') ? 'application/json' : 'text/javascript; charset=utf-8',
|
|
238
|
+
'Content-Length': body.length,
|
|
239
|
+
'Cache-Control': 'no-store',
|
|
240
|
+
'X-Content-Type-Options': 'nosniff',
|
|
241
|
+
});
|
|
242
|
+
res.end(body);
|
|
243
|
+
}
|
|
244
|
+
requestOptions(req) {
|
|
245
|
+
return {
|
|
246
|
+
protocol: this.target.protocol,
|
|
247
|
+
hostname: this.target.hostname.replace(/^\[|\]$/g, ''),
|
|
248
|
+
port: this.target.port,
|
|
249
|
+
method: req.method,
|
|
250
|
+
path: req.url,
|
|
251
|
+
headers: (0, rewrite_1.upstreamHeaders)(req.headers, this.proxyOrigin(req), this.target),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
forward(req, res) {
|
|
255
|
+
const fail = (err) => {
|
|
256
|
+
if (res.headersSent)
|
|
257
|
+
res.destroy();
|
|
258
|
+
else
|
|
259
|
+
(0, http_2.sendText)(res, 502, `tabtape proxy: ${this.target.origin} did not answer: ${err.message}`);
|
|
260
|
+
};
|
|
261
|
+
const upstream = this.client.request(this.requestOptions(req), (up) => {
|
|
262
|
+
try {
|
|
263
|
+
this.respond(req, res, up);
|
|
264
|
+
}
|
|
265
|
+
catch (err) {
|
|
266
|
+
// Say, a status Node refuses to pass on. Thrown from here it would take the daemon down.
|
|
267
|
+
upstream.destroy();
|
|
268
|
+
fail(err);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
upstream.on('error', fail);
|
|
272
|
+
res.on('close', () => {
|
|
273
|
+
if (!res.writableFinished)
|
|
274
|
+
upstream.destroy();
|
|
275
|
+
});
|
|
276
|
+
req.pipe(upstream);
|
|
277
|
+
}
|
|
278
|
+
respond(req, res, up) {
|
|
279
|
+
const status = up.statusCode ?? 502;
|
|
280
|
+
const headers = (0, rewrite_1.downstreamHeaders)(up.headers, this.target);
|
|
281
|
+
const page = (0, rewrite_1.opensPage)(req.headers) && (0, rewrite_1.isPage)(req.method, status, up.headers);
|
|
282
|
+
const decoder = page ? (0, rewrite_1.decoderFor)(up.headers['content-encoding']) : undefined;
|
|
283
|
+
const steps = [];
|
|
284
|
+
if (decoder !== undefined) {
|
|
285
|
+
this.startSession(req);
|
|
286
|
+
delete headers['content-encoding'];
|
|
287
|
+
delete headers['content-length'];
|
|
288
|
+
if (decoder)
|
|
289
|
+
steps.push(decoder);
|
|
290
|
+
steps.push(new rewrite_1.PageRewriter());
|
|
291
|
+
}
|
|
292
|
+
else if (page) {
|
|
293
|
+
this.opts.logger.warn('proxy: page left without the recorder', { session: this.id, encoding: up.headers['content-encoding'] });
|
|
294
|
+
}
|
|
295
|
+
res.writeHead(status, up.statusMessage, headers);
|
|
296
|
+
(0, stream_1.pipeline)([up, ...steps, res], (err) => {
|
|
297
|
+
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE')
|
|
298
|
+
this.opts.logger.warn('proxy: response failed', { session: this.id, err });
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
startSession(req) {
|
|
302
|
+
if (this.recording)
|
|
303
|
+
return;
|
|
304
|
+
this.recording = true;
|
|
305
|
+
this.opts.st.start({
|
|
306
|
+
id: this.id,
|
|
307
|
+
startedAt: Date.now(),
|
|
308
|
+
url: this.target.origin + (req.url ?? ''),
|
|
309
|
+
title: this.name,
|
|
310
|
+
userAgent: req.headers['user-agent'],
|
|
311
|
+
source: 'automation',
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
/** WebSockets: a dev server's hot reload, the app's own. */
|
|
315
|
+
upgrade(req, socket, head) {
|
|
316
|
+
socket.on('error', () => socket.destroy());
|
|
317
|
+
if (!(0, http_2.loopbackHost)(req.headers.host)) {
|
|
318
|
+
socket.end('HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n');
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
this.touch();
|
|
322
|
+
const options = this.requestOptions(req);
|
|
323
|
+
const upstream = this.client.request({
|
|
324
|
+
...options,
|
|
325
|
+
agent: false,
|
|
326
|
+
headers: { ...options.headers, connection: 'Upgrade', upgrade: req.headers.upgrade },
|
|
327
|
+
});
|
|
328
|
+
upstream.on('upgrade', (res, upSocket, upHead) => {
|
|
329
|
+
const lines = [`HTTP/1.1 101 ${res.statusMessage ?? 'Switching Protocols'}`];
|
|
330
|
+
for (let i = 0; i < res.rawHeaders.length; i += 2)
|
|
331
|
+
lines.push(`${res.rawHeaders[i]}: ${res.rawHeaders[i + 1]}`);
|
|
332
|
+
socket.write(`${lines.join('\r\n')}\r\n\r\n`);
|
|
333
|
+
if (upHead.length > 0)
|
|
334
|
+
socket.write(upHead);
|
|
335
|
+
if (head.length > 0)
|
|
336
|
+
upSocket.write(head);
|
|
337
|
+
this.join(socket, upSocket);
|
|
338
|
+
});
|
|
339
|
+
// The target refused: the browser hears the same.
|
|
340
|
+
upstream.on('response', (res) => {
|
|
341
|
+
res.resume();
|
|
342
|
+
socket.end(`HTTP/1.1 ${res.statusCode} ${res.statusMessage ?? ''}\r\nConnection: close\r\n\r\n`);
|
|
343
|
+
});
|
|
344
|
+
upstream.on('error', () => socket.destroy());
|
|
345
|
+
upstream.end();
|
|
346
|
+
}
|
|
347
|
+
join(a, b) {
|
|
348
|
+
for (const [from, to] of [
|
|
349
|
+
[a, b],
|
|
350
|
+
[b, a],
|
|
351
|
+
]) {
|
|
352
|
+
this.sockets.add(from);
|
|
353
|
+
from.on('error', () => from.destroy());
|
|
354
|
+
from.on('close', () => {
|
|
355
|
+
this.sockets.delete(from);
|
|
356
|
+
to.destroy();
|
|
357
|
+
});
|
|
358
|
+
from.pipe(to);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|