@skrillex1224/playwright-toolkit 2.1.198 → 2.1.199
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +155 -2
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +155 -2
- package/dist/index.js.map +2 -2
- package/dist/internals/proxy-meter.js +78 -0
- package/package.json +1 -1
|
@@ -6,6 +6,7 @@ import { URL } from 'url';
|
|
|
6
6
|
const HOST = '127.0.0.1';
|
|
7
7
|
const PORT = Number(process.env.PROXY_METER_PORT || 8899);
|
|
8
8
|
const LOG_PATH = String(process.env.PROXY_METER_LOG || '/tmp/proxy-meter.json');
|
|
9
|
+
const STATE_PATH = String(process.env.PROXY_METER_STATE || '').trim();
|
|
9
10
|
const FLUSH_INTERVAL_MS = Number(process.env.PROXY_METER_FLUSH_MS || 5000);
|
|
10
11
|
const UPSTREAM_URL = String(process.env.PROXY_METER_UPSTREAM || '').trim();
|
|
11
12
|
const RUN_ID = String(process.env.PROXY_METER_RUN_ID || process.env.APIFY_ACTOR_RUN_ID || '').trim();
|
|
@@ -32,6 +33,31 @@ const state = {
|
|
|
32
33
|
: null,
|
|
33
34
|
};
|
|
34
35
|
|
|
36
|
+
let lifecycleState = {
|
|
37
|
+
pid: process.pid,
|
|
38
|
+
status: 'starting',
|
|
39
|
+
host: HOST,
|
|
40
|
+
port: PORT,
|
|
41
|
+
startedAt: state.startedAt,
|
|
42
|
+
readyAt: '',
|
|
43
|
+
exitedAt: '',
|
|
44
|
+
exitCode: null,
|
|
45
|
+
error: '',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const writeLifecycleState = (patch = {}) => {
|
|
49
|
+
if (!STATE_PATH) return;
|
|
50
|
+
lifecycleState = {
|
|
51
|
+
...lifecycleState,
|
|
52
|
+
...patch,
|
|
53
|
+
};
|
|
54
|
+
try {
|
|
55
|
+
fs.writeFileSync(STATE_PATH, JSON.stringify(lifecycleState, null, 2));
|
|
56
|
+
} catch {}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
writeLifecycleState();
|
|
60
|
+
|
|
35
61
|
const getHostBucket = (host) => {
|
|
36
62
|
if (!host) return null;
|
|
37
63
|
if (!state.hosts[host]) {
|
|
@@ -292,6 +318,33 @@ const parseUpstream = () => {
|
|
|
292
318
|
};
|
|
293
319
|
};
|
|
294
320
|
|
|
321
|
+
let fatalSignal = '';
|
|
322
|
+
const markFatal = (error) => {
|
|
323
|
+
fatalSignal = safeError(error);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
process.on('uncaughtException', (error) => {
|
|
327
|
+
markFatal(error);
|
|
328
|
+
writeLifecycleState({
|
|
329
|
+
status: 'exited',
|
|
330
|
+
exitedAt: new Date().toISOString(),
|
|
331
|
+
exitCode: 1,
|
|
332
|
+
error: fatalSignal || 'uncaught_exception',
|
|
333
|
+
});
|
|
334
|
+
process.exit(1);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
process.on('unhandledRejection', (error) => {
|
|
338
|
+
markFatal(error);
|
|
339
|
+
writeLifecycleState({
|
|
340
|
+
status: 'exited',
|
|
341
|
+
exitedAt: new Date().toISOString(),
|
|
342
|
+
exitCode: 1,
|
|
343
|
+
error: fatalSignal || 'unhandled_rejection',
|
|
344
|
+
});
|
|
345
|
+
process.exit(1);
|
|
346
|
+
});
|
|
347
|
+
|
|
295
348
|
const upstream = parseUpstream();
|
|
296
349
|
|
|
297
350
|
const resolveTarget = (req) => {
|
|
@@ -533,7 +586,23 @@ const forwardConnect = (req, clientSocket, head) => {
|
|
|
533
586
|
|
|
534
587
|
const server = http.createServer(forwardHttp);
|
|
535
588
|
server.on('connect', forwardConnect);
|
|
589
|
+
server.on('error', (error) => {
|
|
590
|
+
markFatal(error);
|
|
591
|
+
writeLifecycleState({
|
|
592
|
+
status: 'exited',
|
|
593
|
+
exitedAt: new Date().toISOString(),
|
|
594
|
+
exitCode: 1,
|
|
595
|
+
error: fatalSignal || 'server_error',
|
|
596
|
+
});
|
|
597
|
+
flushSnapshot();
|
|
598
|
+
process.exit(1);
|
|
599
|
+
});
|
|
536
600
|
server.listen(PORT, HOST, () => {
|
|
601
|
+
writeLifecycleState({
|
|
602
|
+
status: 'ready',
|
|
603
|
+
readyAt: new Date().toISOString(),
|
|
604
|
+
error: '',
|
|
605
|
+
});
|
|
537
606
|
console.log(`[proxy-meter] listening ${HOST}:${PORT} log=${LOG_PATH}`);
|
|
538
607
|
});
|
|
539
608
|
|
|
@@ -547,3 +616,12 @@ const shutdown = () => {
|
|
|
547
616
|
|
|
548
617
|
process.on('SIGINT', shutdown);
|
|
549
618
|
process.on('SIGTERM', shutdown);
|
|
619
|
+
process.on('exit', (code) => {
|
|
620
|
+
flushSnapshot();
|
|
621
|
+
writeLifecycleState({
|
|
622
|
+
status: 'exited',
|
|
623
|
+
exitedAt: new Date().toISOString(),
|
|
624
|
+
exitCode: Number.isFinite(code) ? code : null,
|
|
625
|
+
error: fatalSignal,
|
|
626
|
+
});
|
|
627
|
+
});
|