@web-auto/camo 0.2.0 → 0.2.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/LICENSE +21 -21
- package/README.md +586 -586
- package/bin/browser-service.mjs +11 -11
- package/bin/camo.mjs +22 -22
- package/package.json +48 -48
- package/scripts/build.mjs +19 -19
- package/scripts/bump-version.mjs +34 -34
- package/scripts/check-file-size.mjs +80 -80
- package/scripts/file-size-policy.json +12 -2
- package/scripts/install.mjs +76 -76
- package/scripts/release.sh +54 -54
- package/src/autoscript/action-providers/index.mjs +6 -6
- package/src/autoscript/impact-engine.mjs +78 -78
- package/src/autoscript/runtime.mjs +1017 -1017
- package/src/autoscript/schema.mjs +376 -376
- package/src/cli.mjs +405 -405
- package/src/commands/attach.mjs +141 -141
- package/src/commands/autoscript.mjs +1011 -1011
- package/src/commands/browser.mjs +1255 -1255
- package/src/commands/container.mjs +401 -401
- package/src/commands/cookies.mjs +69 -69
- package/src/commands/create.mjs +98 -98
- package/src/commands/devtools.mjs +349 -349
- package/src/commands/events.mjs +152 -152
- package/src/commands/highlight-mode.mjs +24 -24
- package/src/commands/init.mjs +68 -68
- package/src/commands/lifecycle.mjs +275 -275
- package/src/commands/mouse.mjs +45 -45
- package/src/commands/profile.mjs +46 -46
- package/src/commands/record.mjs +115 -115
- package/src/commands/system.mjs +14 -14
- package/src/commands/window.mjs +123 -123
- package/src/container/change-notifier.mjs +362 -362
- package/src/container/element-filter.mjs +143 -143
- package/src/container/index.mjs +3 -3
- package/src/container/runtime-core/checkpoint.mjs +209 -209
- package/src/container/runtime-core/index.mjs +21 -21
- package/src/container/runtime-core/operations/index.mjs +774 -774
- package/src/container/runtime-core/operations/selector-scripts.mjs +277 -277
- package/src/container/runtime-core/operations/tab-pool.mjs +746 -746
- package/src/container/runtime-core/operations/viewport.mjs +189 -189
- package/src/container/runtime-core/search.mjs +190 -190
- package/src/container/runtime-core/subscription.mjs +224 -224
- package/src/container/runtime-core/utils.mjs +94 -94
- package/src/container/runtime-core/validation.mjs +127 -127
- package/src/container/runtime-core.mjs +1 -1
- package/src/container/subscription-registry.mjs +459 -459
- package/src/core/actions.mjs +561 -561
- package/src/core/browser.mjs +266 -266
- package/src/core/index.mjs +52 -52
- package/src/core/utils.mjs +91 -91
- package/src/events/daemon-entry.mjs +33 -33
- package/src/events/daemon.mjs +80 -80
- package/src/events/progress-log.mjs +109 -109
- package/src/events/ws-server.mjs +239 -239
- package/src/lib/client.mjs +200 -200
- package/src/lifecycle/cleanup.mjs +83 -83
- package/src/lifecycle/lock.mjs +126 -126
- package/src/lifecycle/session-registry.mjs +279 -279
- package/src/lifecycle/session-view.mjs +76 -76
- package/src/lifecycle/session-watchdog.mjs +281 -281
- package/src/services/browser-service/index.js +671 -671
- package/src/services/browser-service/internal/BrowserSession.input.test.js +389 -389
- package/src/services/browser-service/internal/BrowserSession.js +325 -304
- package/src/services/browser-service/internal/ElementRegistry.js +60 -60
- package/src/services/browser-service/internal/ProfileLock.js +84 -84
- package/src/services/browser-service/internal/SessionManager.js +184 -184
- package/src/services/browser-service/internal/SessionManager.test.js +39 -39
- package/src/services/browser-service/internal/browser-session/cookies.js +144 -144
- package/src/services/browser-service/internal/browser-session/input-ops.js +222 -222
- package/src/services/browser-service/internal/browser-session/input-pipeline.js +144 -144
- package/src/services/browser-service/internal/browser-session/logging.js +46 -46
- package/src/services/browser-service/internal/browser-session/navigation.js +38 -38
- package/src/services/browser-service/internal/browser-session/page-hooks.js +442 -442
- package/src/services/browser-service/internal/browser-session/page-management.js +302 -302
- package/src/services/browser-service/internal/browser-session/page-management.test.js +148 -148
- package/src/services/browser-service/internal/browser-session/recording.js +198 -198
- package/src/services/browser-service/internal/browser-session/runtime-events.js +61 -61
- package/src/services/browser-service/internal/browser-session/session-core.js +84 -84
- package/src/services/browser-service/internal/browser-session/session-state.js +38 -38
- package/src/services/browser-service/internal/browser-session/types.js +14 -14
- package/src/services/browser-service/internal/browser-session/utils.js +95 -95
- package/src/services/browser-service/internal/browser-session/viewport-manager.js +46 -46
- package/src/services/browser-service/internal/browser-session/viewport.js +215 -215
- package/src/services/browser-service/internal/container-matcher.js +851 -851
- package/src/services/browser-service/internal/container-registry.js +182 -182
- package/src/services/browser-service/internal/engine-manager.js +259 -259
- package/src/services/browser-service/internal/fingerprint.js +203 -203
- package/src/services/browser-service/internal/heartbeat.js +137 -137
- package/src/services/browser-service/internal/logging.js +46 -46
- package/src/services/browser-service/internal/page-runtime/runtime.js +1317 -1317
- package/src/services/browser-service/internal/pageRuntime.js +28 -28
- package/src/services/browser-service/internal/runtimeInjector.js +31 -31
- package/src/services/browser-service/internal/service-process-logger.js +140 -140
- package/src/services/browser-service/internal/state-bus.js +45 -45
- package/src/services/browser-service/internal/storage-paths.js +42 -42
- package/src/services/browser-service/internal/ws-server.js +1194 -1194
- package/src/services/browser-service/internal/ws-server.test.js +58 -58
- package/src/services/browser-service/server.mjs +6 -6
- package/src/services/controller/cli-bridge.js +93 -93
- package/src/services/controller/container-index.js +50 -50
- package/src/services/controller/container-storage.js +36 -36
- package/src/services/controller/controller-actions.js +207 -207
- package/src/services/controller/controller.js +1138 -1138
- package/src/services/controller/selectors.js +54 -54
- package/src/services/controller/transport.js +125 -125
- package/src/utils/args.mjs +26 -26
- package/src/utils/browser-service.mjs +544 -544
- package/src/utils/command-log.mjs +64 -64
- package/src/utils/config.mjs +214 -214
- package/src/utils/fingerprint.mjs +181 -181
- package/src/utils/help.mjs +216 -216
- package/src/utils/js-policy.mjs +13 -13
- package/src/utils/ws-client.mjs +30 -30
|
@@ -1,281 +1,281 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { spawn } from 'node:child_process';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
6
|
-
import { callAPI } from '../utils/browser-service.mjs';
|
|
7
|
-
import { CONFIG_DIR } from '../utils/config.mjs';
|
|
8
|
-
import { releaseLock } from './lock.mjs';
|
|
9
|
-
import { getSessionInfo, markSessionClosed } from './session-registry.mjs';
|
|
10
|
-
|
|
11
|
-
const WATCHDOG_DIR = path.join(CONFIG_DIR, 'run', 'camo-watchdogs');
|
|
12
|
-
const DEFAULT_HEADLESS_IDLE_TIMEOUT_MS = 30 * 60 * 1000;
|
|
13
|
-
|
|
14
|
-
function ensureWatchdogDir() {
|
|
15
|
-
if (!fs.existsSync(WATCHDOG_DIR)) {
|
|
16
|
-
fs.mkdirSync(WATCHDOG_DIR, { recursive: true });
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function getWatchdogFile(profileId) {
|
|
21
|
-
ensureWatchdogDir();
|
|
22
|
-
return path.join(WATCHDOG_DIR, `${profileId}.json`);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function readWatchdogRecord(profileId) {
|
|
26
|
-
const file = getWatchdogFile(profileId);
|
|
27
|
-
if (!fs.existsSync(file)) return null;
|
|
28
|
-
try {
|
|
29
|
-
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
30
|
-
} catch {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function writeWatchdogRecord(profileId, record) {
|
|
36
|
-
const file = getWatchdogFile(profileId);
|
|
37
|
-
fs.writeFileSync(file, JSON.stringify(record, null, 2));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function removeWatchdogRecord(profileId) {
|
|
41
|
-
const file = getWatchdogFile(profileId);
|
|
42
|
-
if (fs.existsSync(file)) {
|
|
43
|
-
fs.unlinkSync(file);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function isProcessAlive(pid) {
|
|
48
|
-
const target = Number(pid);
|
|
49
|
-
if (!Number.isFinite(target) || target <= 0) return false;
|
|
50
|
-
try {
|
|
51
|
-
process.kill(target, 0);
|
|
52
|
-
return true;
|
|
53
|
-
} catch {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function sleep(ms) {
|
|
59
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function isBlankUrl(url) {
|
|
63
|
-
const text = String(url || '').trim().toLowerCase();
|
|
64
|
-
return text === '' || text === 'about:blank' || text === 'about:blank#blocked';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function computeTargetViewportFromWindow(measured) {
|
|
68
|
-
const innerWidth = Math.max(320, Number(measured?.innerWidth || 0) || 0);
|
|
69
|
-
const innerHeight = Math.max(240, Number(measured?.innerHeight || 0) || 0);
|
|
70
|
-
const outerWidth = Math.max(320, Number(measured?.outerWidth || 0) || innerWidth);
|
|
71
|
-
const outerHeight = Math.max(240, Number(measured?.outerHeight || 0) || innerHeight);
|
|
72
|
-
const rawDeltaW = Math.max(0, outerWidth - innerWidth);
|
|
73
|
-
const rawDeltaH = Math.max(0, outerHeight - innerHeight);
|
|
74
|
-
const frameW = rawDeltaW > 400 ? 16 : Math.min(rawDeltaW, 120);
|
|
75
|
-
const frameH = rawDeltaH > 400 ? 88 : Math.min(rawDeltaH, 180);
|
|
76
|
-
return {
|
|
77
|
-
width: Math.max(320, outerWidth - frameW),
|
|
78
|
-
height: Math.max(240, outerHeight - frameH),
|
|
79
|
-
innerWidth,
|
|
80
|
-
innerHeight,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async function probeWindowMetrics(profileId) {
|
|
85
|
-
const measured = await callAPI('evaluate', {
|
|
86
|
-
profileId,
|
|
87
|
-
script: '({ innerWidth: window.innerWidth, innerHeight: window.innerHeight, outerWidth: window.outerWidth, outerHeight: window.outerHeight })',
|
|
88
|
-
});
|
|
89
|
-
return measured?.result || {};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function syncViewportWithWindow(profileId, tolerancePx = 3) {
|
|
93
|
-
const measured = await probeWindowMetrics(profileId);
|
|
94
|
-
const target = computeTargetViewportFromWindow(measured);
|
|
95
|
-
const dw = Math.abs(target.innerWidth - target.width);
|
|
96
|
-
const dh = Math.abs(target.innerHeight - target.height);
|
|
97
|
-
if (dw <= tolerancePx && dh <= tolerancePx) return false;
|
|
98
|
-
await callAPI('page:setViewport', {
|
|
99
|
-
profileId,
|
|
100
|
-
width: target.width,
|
|
101
|
-
height: target.height,
|
|
102
|
-
});
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function resolveIdleTimeoutMs(sessionInfo) {
|
|
107
|
-
if (!sessionInfo || sessionInfo.headless !== true) return 0;
|
|
108
|
-
const raw = Number(sessionInfo.idleTimeoutMs);
|
|
109
|
-
if (Number.isFinite(raw) && raw >= 0) return Math.floor(raw);
|
|
110
|
-
return DEFAULT_HEADLESS_IDLE_TIMEOUT_MS;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async function cleanupSession(profileId) {
|
|
114
|
-
await callAPI('stop', { profileId }).catch(() => {});
|
|
115
|
-
releaseLock(profileId);
|
|
116
|
-
markSessionClosed(profileId);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function runMonitor(profileId, options = {}) {
|
|
120
|
-
const intervalMs = Math.max(500, Number(options.intervalMs) || 1200);
|
|
121
|
-
const emptyThreshold = Math.max(1, Number(options.emptyThreshold) || 2);
|
|
122
|
-
const blankThreshold = Math.max(1, Number(options.blankThreshold) || 3);
|
|
123
|
-
const viewportSyncIntervalMs = Math.max(500, Number(options.viewportSyncIntervalMs) || 1500);
|
|
124
|
-
let lastViewportSyncAt = 0;
|
|
125
|
-
|
|
126
|
-
let seenAnyPage = false;
|
|
127
|
-
let seenNonBlankPage = false;
|
|
128
|
-
let emptyStreak = 0;
|
|
129
|
-
let blankOnlyStreak = 0;
|
|
130
|
-
|
|
131
|
-
while (true) {
|
|
132
|
-
const localInfo = getSessionInfo(profileId);
|
|
133
|
-
if (!localInfo || localInfo.status !== 'active') return;
|
|
134
|
-
|
|
135
|
-
const idleTimeoutMs = resolveIdleTimeoutMs(localInfo);
|
|
136
|
-
if (idleTimeoutMs > 0) {
|
|
137
|
-
const lastActivityAt = Number(localInfo.lastActivityAt || localInfo.lastSeen || localInfo.startTime || Date.now());
|
|
138
|
-
const idleMs = Date.now() - lastActivityAt;
|
|
139
|
-
if (idleMs >= idleTimeoutMs) {
|
|
140
|
-
await cleanupSession(profileId);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
let sessions = [];
|
|
146
|
-
try {
|
|
147
|
-
const status = await callAPI('getStatus', {});
|
|
148
|
-
sessions = Array.isArray(status?.sessions) ? status.sessions : [];
|
|
149
|
-
} catch {
|
|
150
|
-
// Service unavailable; exit monitor silently.
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const liveSession = sessions.find((item) => item?.profileId === profileId);
|
|
155
|
-
if (!liveSession) {
|
|
156
|
-
releaseLock(profileId);
|
|
157
|
-
markSessionClosed(profileId);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
let pages = [];
|
|
162
|
-
try {
|
|
163
|
-
const listed = await callAPI('page:list', { profileId });
|
|
164
|
-
pages = Array.isArray(listed?.pages) ? listed.pages : [];
|
|
165
|
-
} catch {
|
|
166
|
-
// Session lookup failed in page:list: treat as closed.
|
|
167
|
-
releaseLock(profileId);
|
|
168
|
-
markSessionClosed(profileId);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (pages.length > 0) {
|
|
173
|
-
seenAnyPage = true;
|
|
174
|
-
if (pages.some((item) => !isBlankUrl(item?.url))) {
|
|
175
|
-
seenNonBlankPage = true;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const blankOnly = pages.length > 0 && pages.every((item) => isBlankUrl(item?.url));
|
|
180
|
-
|
|
181
|
-
if (seenAnyPage && pages.length === 0) {
|
|
182
|
-
emptyStreak += 1;
|
|
183
|
-
} else {
|
|
184
|
-
emptyStreak = 0;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (seenNonBlankPage && blankOnly) {
|
|
188
|
-
blankOnlyStreak += 1;
|
|
189
|
-
} else {
|
|
190
|
-
blankOnlyStreak = 0;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (emptyStreak >= emptyThreshold || blankOnlyStreak >= blankThreshold) {
|
|
194
|
-
await cleanupSession(profileId);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const now = Date.now();
|
|
199
|
-
if (pages.length > 0 && now - lastViewportSyncAt >= viewportSyncIntervalMs) {
|
|
200
|
-
lastViewportSyncAt = now;
|
|
201
|
-
await syncViewportWithWindow(profileId).catch(() => {});
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
await sleep(intervalMs);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export function startSessionWatchdog(profileId) {
|
|
209
|
-
const normalized = String(profileId || '').trim();
|
|
210
|
-
if (!normalized) return { ok: false, reason: 'profile_required' };
|
|
211
|
-
|
|
212
|
-
const existing = readWatchdogRecord(normalized);
|
|
213
|
-
if (existing?.pid && isProcessAlive(existing.pid)) {
|
|
214
|
-
return { ok: true, started: false, pid: existing.pid };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const commandPath = fileURLToPath(new URL('../cli.mjs', import.meta.url));
|
|
218
|
-
const child = spawn(process.execPath, [commandPath, '__session-watchdog', normalized], {
|
|
219
|
-
detached: true,
|
|
220
|
-
stdio: 'ignore',
|
|
221
|
-
env: {
|
|
222
|
-
...process.env,
|
|
223
|
-
CAMO_WATCHDOG_CHILD: '1',
|
|
224
|
-
},
|
|
225
|
-
});
|
|
226
|
-
child.unref();
|
|
227
|
-
|
|
228
|
-
const childPid = Number(child.pid);
|
|
229
|
-
if (!Number.isFinite(childPid) || childPid <= 0) {
|
|
230
|
-
return { ok: false, reason: 'spawn_failed' };
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
writeWatchdogRecord(normalized, {
|
|
234
|
-
profileId: normalized,
|
|
235
|
-
pid: childPid,
|
|
236
|
-
startedAt: Date.now(),
|
|
237
|
-
});
|
|
238
|
-
return { ok: true, started: true, pid: childPid };
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
export function stopSessionWatchdog(profileId) {
|
|
242
|
-
const normalized = String(profileId || '').trim();
|
|
243
|
-
if (!normalized) return false;
|
|
244
|
-
const record = readWatchdogRecord(normalized);
|
|
245
|
-
if (record?.pid && isProcessAlive(record.pid)) {
|
|
246
|
-
try {
|
|
247
|
-
process.kill(record.pid, 'SIGTERM');
|
|
248
|
-
} catch {
|
|
249
|
-
// Ignore kill failure and still cleanup record.
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
removeWatchdogRecord(normalized);
|
|
253
|
-
return true;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export function stopAllSessionWatchdogs() {
|
|
257
|
-
ensureWatchdogDir();
|
|
258
|
-
const files = fs.readdirSync(WATCHDOG_DIR).filter((name) => name.endsWith('.json'));
|
|
259
|
-
for (const file of files) {
|
|
260
|
-
const profileId = file.slice(0, -'.json'.length);
|
|
261
|
-
stopSessionWatchdog(profileId);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export async function handleSessionWatchdogCommand(args) {
|
|
266
|
-
const profileId = String(args[1] || '').trim();
|
|
267
|
-
if (!profileId) {
|
|
268
|
-
throw new Error('Usage: camo __session-watchdog <profileId>');
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const cleanup = () => removeWatchdogRecord(profileId);
|
|
272
|
-
process.on('SIGINT', cleanup);
|
|
273
|
-
process.on('SIGTERM', cleanup);
|
|
274
|
-
process.on('exit', cleanup);
|
|
275
|
-
|
|
276
|
-
try {
|
|
277
|
-
await runMonitor(profileId);
|
|
278
|
-
} finally {
|
|
279
|
-
cleanup();
|
|
280
|
-
}
|
|
281
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { callAPI } from '../utils/browser-service.mjs';
|
|
7
|
+
import { CONFIG_DIR } from '../utils/config.mjs';
|
|
8
|
+
import { releaseLock } from './lock.mjs';
|
|
9
|
+
import { getSessionInfo, markSessionClosed } from './session-registry.mjs';
|
|
10
|
+
|
|
11
|
+
const WATCHDOG_DIR = path.join(CONFIG_DIR, 'run', 'camo-watchdogs');
|
|
12
|
+
const DEFAULT_HEADLESS_IDLE_TIMEOUT_MS = 30 * 60 * 1000;
|
|
13
|
+
|
|
14
|
+
function ensureWatchdogDir() {
|
|
15
|
+
if (!fs.existsSync(WATCHDOG_DIR)) {
|
|
16
|
+
fs.mkdirSync(WATCHDOG_DIR, { recursive: true });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getWatchdogFile(profileId) {
|
|
21
|
+
ensureWatchdogDir();
|
|
22
|
+
return path.join(WATCHDOG_DIR, `${profileId}.json`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function readWatchdogRecord(profileId) {
|
|
26
|
+
const file = getWatchdogFile(profileId);
|
|
27
|
+
if (!fs.existsSync(file)) return null;
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function writeWatchdogRecord(profileId, record) {
|
|
36
|
+
const file = getWatchdogFile(profileId);
|
|
37
|
+
fs.writeFileSync(file, JSON.stringify(record, null, 2));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function removeWatchdogRecord(profileId) {
|
|
41
|
+
const file = getWatchdogFile(profileId);
|
|
42
|
+
if (fs.existsSync(file)) {
|
|
43
|
+
fs.unlinkSync(file);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isProcessAlive(pid) {
|
|
48
|
+
const target = Number(pid);
|
|
49
|
+
if (!Number.isFinite(target) || target <= 0) return false;
|
|
50
|
+
try {
|
|
51
|
+
process.kill(target, 0);
|
|
52
|
+
return true;
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function sleep(ms) {
|
|
59
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isBlankUrl(url) {
|
|
63
|
+
const text = String(url || '').trim().toLowerCase();
|
|
64
|
+
return text === '' || text === 'about:blank' || text === 'about:blank#blocked';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function computeTargetViewportFromWindow(measured) {
|
|
68
|
+
const innerWidth = Math.max(320, Number(measured?.innerWidth || 0) || 0);
|
|
69
|
+
const innerHeight = Math.max(240, Number(measured?.innerHeight || 0) || 0);
|
|
70
|
+
const outerWidth = Math.max(320, Number(measured?.outerWidth || 0) || innerWidth);
|
|
71
|
+
const outerHeight = Math.max(240, Number(measured?.outerHeight || 0) || innerHeight);
|
|
72
|
+
const rawDeltaW = Math.max(0, outerWidth - innerWidth);
|
|
73
|
+
const rawDeltaH = Math.max(0, outerHeight - innerHeight);
|
|
74
|
+
const frameW = rawDeltaW > 400 ? 16 : Math.min(rawDeltaW, 120);
|
|
75
|
+
const frameH = rawDeltaH > 400 ? 88 : Math.min(rawDeltaH, 180);
|
|
76
|
+
return {
|
|
77
|
+
width: Math.max(320, outerWidth - frameW),
|
|
78
|
+
height: Math.max(240, outerHeight - frameH),
|
|
79
|
+
innerWidth,
|
|
80
|
+
innerHeight,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function probeWindowMetrics(profileId) {
|
|
85
|
+
const measured = await callAPI('evaluate', {
|
|
86
|
+
profileId,
|
|
87
|
+
script: '({ innerWidth: window.innerWidth, innerHeight: window.innerHeight, outerWidth: window.outerWidth, outerHeight: window.outerHeight })',
|
|
88
|
+
});
|
|
89
|
+
return measured?.result || {};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function syncViewportWithWindow(profileId, tolerancePx = 3) {
|
|
93
|
+
const measured = await probeWindowMetrics(profileId);
|
|
94
|
+
const target = computeTargetViewportFromWindow(measured);
|
|
95
|
+
const dw = Math.abs(target.innerWidth - target.width);
|
|
96
|
+
const dh = Math.abs(target.innerHeight - target.height);
|
|
97
|
+
if (dw <= tolerancePx && dh <= tolerancePx) return false;
|
|
98
|
+
await callAPI('page:setViewport', {
|
|
99
|
+
profileId,
|
|
100
|
+
width: target.width,
|
|
101
|
+
height: target.height,
|
|
102
|
+
});
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function resolveIdleTimeoutMs(sessionInfo) {
|
|
107
|
+
if (!sessionInfo || sessionInfo.headless !== true) return 0;
|
|
108
|
+
const raw = Number(sessionInfo.idleTimeoutMs);
|
|
109
|
+
if (Number.isFinite(raw) && raw >= 0) return Math.floor(raw);
|
|
110
|
+
return DEFAULT_HEADLESS_IDLE_TIMEOUT_MS;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function cleanupSession(profileId) {
|
|
114
|
+
await callAPI('stop', { profileId }).catch(() => {});
|
|
115
|
+
releaseLock(profileId);
|
|
116
|
+
markSessionClosed(profileId);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function runMonitor(profileId, options = {}) {
|
|
120
|
+
const intervalMs = Math.max(500, Number(options.intervalMs) || 1200);
|
|
121
|
+
const emptyThreshold = Math.max(1, Number(options.emptyThreshold) || 2);
|
|
122
|
+
const blankThreshold = Math.max(1, Number(options.blankThreshold) || 3);
|
|
123
|
+
const viewportSyncIntervalMs = Math.max(500, Number(options.viewportSyncIntervalMs) || 1500);
|
|
124
|
+
let lastViewportSyncAt = 0;
|
|
125
|
+
|
|
126
|
+
let seenAnyPage = false;
|
|
127
|
+
let seenNonBlankPage = false;
|
|
128
|
+
let emptyStreak = 0;
|
|
129
|
+
let blankOnlyStreak = 0;
|
|
130
|
+
|
|
131
|
+
while (true) {
|
|
132
|
+
const localInfo = getSessionInfo(profileId);
|
|
133
|
+
if (!localInfo || localInfo.status !== 'active') return;
|
|
134
|
+
|
|
135
|
+
const idleTimeoutMs = resolveIdleTimeoutMs(localInfo);
|
|
136
|
+
if (idleTimeoutMs > 0) {
|
|
137
|
+
const lastActivityAt = Number(localInfo.lastActivityAt || localInfo.lastSeen || localInfo.startTime || Date.now());
|
|
138
|
+
const idleMs = Date.now() - lastActivityAt;
|
|
139
|
+
if (idleMs >= idleTimeoutMs) {
|
|
140
|
+
await cleanupSession(profileId);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let sessions = [];
|
|
146
|
+
try {
|
|
147
|
+
const status = await callAPI('getStatus', {});
|
|
148
|
+
sessions = Array.isArray(status?.sessions) ? status.sessions : [];
|
|
149
|
+
} catch {
|
|
150
|
+
// Service unavailable; exit monitor silently.
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const liveSession = sessions.find((item) => item?.profileId === profileId);
|
|
155
|
+
if (!liveSession) {
|
|
156
|
+
releaseLock(profileId);
|
|
157
|
+
markSessionClosed(profileId);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let pages = [];
|
|
162
|
+
try {
|
|
163
|
+
const listed = await callAPI('page:list', { profileId });
|
|
164
|
+
pages = Array.isArray(listed?.pages) ? listed.pages : [];
|
|
165
|
+
} catch {
|
|
166
|
+
// Session lookup failed in page:list: treat as closed.
|
|
167
|
+
releaseLock(profileId);
|
|
168
|
+
markSessionClosed(profileId);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (pages.length > 0) {
|
|
173
|
+
seenAnyPage = true;
|
|
174
|
+
if (pages.some((item) => !isBlankUrl(item?.url))) {
|
|
175
|
+
seenNonBlankPage = true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const blankOnly = pages.length > 0 && pages.every((item) => isBlankUrl(item?.url));
|
|
180
|
+
|
|
181
|
+
if (seenAnyPage && pages.length === 0) {
|
|
182
|
+
emptyStreak += 1;
|
|
183
|
+
} else {
|
|
184
|
+
emptyStreak = 0;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (seenNonBlankPage && blankOnly) {
|
|
188
|
+
blankOnlyStreak += 1;
|
|
189
|
+
} else {
|
|
190
|
+
blankOnlyStreak = 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (emptyStreak >= emptyThreshold || blankOnlyStreak >= blankThreshold) {
|
|
194
|
+
await cleanupSession(profileId);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const now = Date.now();
|
|
199
|
+
if (pages.length > 0 && now - lastViewportSyncAt >= viewportSyncIntervalMs) {
|
|
200
|
+
lastViewportSyncAt = now;
|
|
201
|
+
await syncViewportWithWindow(profileId).catch(() => {});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
await sleep(intervalMs);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function startSessionWatchdog(profileId) {
|
|
209
|
+
const normalized = String(profileId || '').trim();
|
|
210
|
+
if (!normalized) return { ok: false, reason: 'profile_required' };
|
|
211
|
+
|
|
212
|
+
const existing = readWatchdogRecord(normalized);
|
|
213
|
+
if (existing?.pid && isProcessAlive(existing.pid)) {
|
|
214
|
+
return { ok: true, started: false, pid: existing.pid };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const commandPath = fileURLToPath(new URL('../cli.mjs', import.meta.url));
|
|
218
|
+
const child = spawn(process.execPath, [commandPath, '__session-watchdog', normalized], {
|
|
219
|
+
detached: true,
|
|
220
|
+
stdio: 'ignore',
|
|
221
|
+
env: {
|
|
222
|
+
...process.env,
|
|
223
|
+
CAMO_WATCHDOG_CHILD: '1',
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
child.unref();
|
|
227
|
+
|
|
228
|
+
const childPid = Number(child.pid);
|
|
229
|
+
if (!Number.isFinite(childPid) || childPid <= 0) {
|
|
230
|
+
return { ok: false, reason: 'spawn_failed' };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
writeWatchdogRecord(normalized, {
|
|
234
|
+
profileId: normalized,
|
|
235
|
+
pid: childPid,
|
|
236
|
+
startedAt: Date.now(),
|
|
237
|
+
});
|
|
238
|
+
return { ok: true, started: true, pid: childPid };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function stopSessionWatchdog(profileId) {
|
|
242
|
+
const normalized = String(profileId || '').trim();
|
|
243
|
+
if (!normalized) return false;
|
|
244
|
+
const record = readWatchdogRecord(normalized);
|
|
245
|
+
if (record?.pid && isProcessAlive(record.pid)) {
|
|
246
|
+
try {
|
|
247
|
+
process.kill(record.pid, 'SIGTERM');
|
|
248
|
+
} catch {
|
|
249
|
+
// Ignore kill failure and still cleanup record.
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
removeWatchdogRecord(normalized);
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function stopAllSessionWatchdogs() {
|
|
257
|
+
ensureWatchdogDir();
|
|
258
|
+
const files = fs.readdirSync(WATCHDOG_DIR).filter((name) => name.endsWith('.json'));
|
|
259
|
+
for (const file of files) {
|
|
260
|
+
const profileId = file.slice(0, -'.json'.length);
|
|
261
|
+
stopSessionWatchdog(profileId);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export async function handleSessionWatchdogCommand(args) {
|
|
266
|
+
const profileId = String(args[1] || '').trim();
|
|
267
|
+
if (!profileId) {
|
|
268
|
+
throw new Error('Usage: camo __session-watchdog <profileId>');
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const cleanup = () => removeWatchdogRecord(profileId);
|
|
272
|
+
process.on('SIGINT', cleanup);
|
|
273
|
+
process.on('SIGTERM', cleanup);
|
|
274
|
+
process.on('exit', cleanup);
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
await runMonitor(profileId);
|
|
278
|
+
} finally {
|
|
279
|
+
cleanup();
|
|
280
|
+
}
|
|
281
|
+
}
|