@web-auto/camo 0.2.0 → 0.2.2
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,61 +1,61 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 元素注册表
|
|
3
|
-
* 管理浏览器端 ElementHandle 的生命周期和映射
|
|
4
|
-
*/
|
|
5
|
-
export class ElementRegistry {
|
|
6
|
-
elements = new Map();
|
|
7
|
-
pageMap = new Map(); // elementId -> pageId (for cleanup)
|
|
8
|
-
/**
|
|
9
|
-
* 注册元素并返回唯一 ID
|
|
10
|
-
*/
|
|
11
|
-
register(element, pageId) {
|
|
12
|
-
const id = `el_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
13
|
-
this.elements.set(id, element);
|
|
14
|
-
if (pageId) {
|
|
15
|
-
this.pageMap.set(id, pageId);
|
|
16
|
-
}
|
|
17
|
-
return id;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* 获取元素
|
|
21
|
-
*/
|
|
22
|
-
get(id) {
|
|
23
|
-
return this.elements.get(id);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* 释放元素
|
|
27
|
-
*/
|
|
28
|
-
async release(id) {
|
|
29
|
-
const element = this.elements.get(id);
|
|
30
|
-
if (element) {
|
|
31
|
-
try {
|
|
32
|
-
await element.dispose();
|
|
33
|
-
}
|
|
34
|
-
catch (e) {
|
|
35
|
-
// Ignore disposal errors
|
|
36
|
-
}
|
|
37
|
-
this.elements.delete(id);
|
|
38
|
-
this.pageMap.delete(id);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* 清理特定页面的所有元素
|
|
43
|
-
*/
|
|
44
|
-
async clearPage(pageId) {
|
|
45
|
-
const toRemove = [];
|
|
46
|
-
for (const [id, pid] of this.pageMap.entries()) {
|
|
47
|
-
if (pid === pageId) {
|
|
48
|
-
toRemove.push(id);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
await Promise.all(toRemove.map(id => this.release(id)));
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* 清理所有元素
|
|
55
|
-
*/
|
|
56
|
-
async clearAll() {
|
|
57
|
-
const ids = Array.from(this.elements.keys());
|
|
58
|
-
await Promise.all(ids.map(id => this.release(id)));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 元素注册表
|
|
3
|
+
* 管理浏览器端 ElementHandle 的生命周期和映射
|
|
4
|
+
*/
|
|
5
|
+
export class ElementRegistry {
|
|
6
|
+
elements = new Map();
|
|
7
|
+
pageMap = new Map(); // elementId -> pageId (for cleanup)
|
|
8
|
+
/**
|
|
9
|
+
* 注册元素并返回唯一 ID
|
|
10
|
+
*/
|
|
11
|
+
register(element, pageId) {
|
|
12
|
+
const id = `el_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
13
|
+
this.elements.set(id, element);
|
|
14
|
+
if (pageId) {
|
|
15
|
+
this.pageMap.set(id, pageId);
|
|
16
|
+
}
|
|
17
|
+
return id;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 获取元素
|
|
21
|
+
*/
|
|
22
|
+
get(id) {
|
|
23
|
+
return this.elements.get(id);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 释放元素
|
|
27
|
+
*/
|
|
28
|
+
async release(id) {
|
|
29
|
+
const element = this.elements.get(id);
|
|
30
|
+
if (element) {
|
|
31
|
+
try {
|
|
32
|
+
await element.dispose();
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
// Ignore disposal errors
|
|
36
|
+
}
|
|
37
|
+
this.elements.delete(id);
|
|
38
|
+
this.pageMap.delete(id);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 清理特定页面的所有元素
|
|
43
|
+
*/
|
|
44
|
+
async clearPage(pageId) {
|
|
45
|
+
const toRemove = [];
|
|
46
|
+
for (const [id, pid] of this.pageMap.entries()) {
|
|
47
|
+
if (pid === pageId) {
|
|
48
|
+
toRemove.push(id);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
await Promise.all(toRemove.map(id => this.release(id)));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 清理所有元素
|
|
55
|
+
*/
|
|
56
|
+
async clearAll() {
|
|
57
|
+
const ids = Array.from(this.elements.keys());
|
|
58
|
+
await Promise.all(ids.map(id => this.release(id)));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
61
|
//# sourceMappingURL=ElementRegistry.js.map
|
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import os from 'os';
|
|
4
|
-
import { resolveLocksRoot } from './storage-paths.js';
|
|
5
|
-
export class ProfileLock {
|
|
6
|
-
profileId;
|
|
7
|
-
lockDir;
|
|
8
|
-
lockFile;
|
|
9
|
-
constructor(profileId, lockRoot = resolveLocksRoot()) {
|
|
10
|
-
this.profileId = profileId;
|
|
11
|
-
this.lockDir = lockRoot;
|
|
12
|
-
fs.mkdirSync(this.lockDir, { recursive: true });
|
|
13
|
-
this.lockFile = path.join(this.lockDir, `${this.profileId}.lock`);
|
|
14
|
-
}
|
|
15
|
-
isProcessRunning(pid) {
|
|
16
|
-
try {
|
|
17
|
-
process.kill(pid, 0);
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
killProcess(pid) {
|
|
25
|
-
try {
|
|
26
|
-
process.kill(pid, 'SIGTERM');
|
|
27
|
-
}
|
|
28
|
-
catch { }
|
|
29
|
-
const start = Date.now();
|
|
30
|
-
while (Date.now() - start < 5000) {
|
|
31
|
-
if (!this.isProcessRunning(pid))
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
process.kill(pid, 'SIGKILL');
|
|
36
|
-
}
|
|
37
|
-
catch { }
|
|
38
|
-
}
|
|
39
|
-
acquire() {
|
|
40
|
-
if (fs.existsSync(this.lockFile)) {
|
|
41
|
-
try {
|
|
42
|
-
const raw = JSON.parse(fs.readFileSync(this.lockFile, 'utf-8'));
|
|
43
|
-
const pid = Number(raw?.pid);
|
|
44
|
-
if (pid && pid !== process.pid && this.isProcessRunning(pid)) {
|
|
45
|
-
this.killProcess(pid);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
catch {
|
|
49
|
-
// ignore corrupted lock
|
|
50
|
-
}
|
|
51
|
-
try {
|
|
52
|
-
fs.unlinkSync(this.lockFile);
|
|
53
|
-
}
|
|
54
|
-
catch { }
|
|
55
|
-
}
|
|
56
|
-
try {
|
|
57
|
-
const payload = JSON.stringify({
|
|
58
|
-
pid: process.pid,
|
|
59
|
-
profileId: this.profileId,
|
|
60
|
-
createdAt: Date.now(),
|
|
61
|
-
host: os.hostname(),
|
|
62
|
-
}, null, 2);
|
|
63
|
-
fs.writeFileSync(this.lockFile, payload, { encoding: 'utf-8' });
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
catch (err) {
|
|
67
|
-
console.error(`[ProfileLock] failed to acquire lock for ${this.profileId}:`, err);
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
release() {
|
|
72
|
-
try {
|
|
73
|
-
if (!fs.existsSync(this.lockFile))
|
|
74
|
-
return;
|
|
75
|
-
const raw = JSON.parse(fs.readFileSync(this.lockFile, 'utf-8'));
|
|
76
|
-
if (Number(raw?.pid) !== process.pid)
|
|
77
|
-
return;
|
|
78
|
-
fs.unlinkSync(this.lockFile);
|
|
79
|
-
}
|
|
80
|
-
catch (err) {
|
|
81
|
-
console.warn(`[ProfileLock] release failed for ${this.profileId}:`, err);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
import { resolveLocksRoot } from './storage-paths.js';
|
|
5
|
+
export class ProfileLock {
|
|
6
|
+
profileId;
|
|
7
|
+
lockDir;
|
|
8
|
+
lockFile;
|
|
9
|
+
constructor(profileId, lockRoot = resolveLocksRoot()) {
|
|
10
|
+
this.profileId = profileId;
|
|
11
|
+
this.lockDir = lockRoot;
|
|
12
|
+
fs.mkdirSync(this.lockDir, { recursive: true });
|
|
13
|
+
this.lockFile = path.join(this.lockDir, `${this.profileId}.lock`);
|
|
14
|
+
}
|
|
15
|
+
isProcessRunning(pid) {
|
|
16
|
+
try {
|
|
17
|
+
process.kill(pid, 0);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
killProcess(pid) {
|
|
25
|
+
try {
|
|
26
|
+
process.kill(pid, 'SIGTERM');
|
|
27
|
+
}
|
|
28
|
+
catch { }
|
|
29
|
+
const start = Date.now();
|
|
30
|
+
while (Date.now() - start < 5000) {
|
|
31
|
+
if (!this.isProcessRunning(pid))
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
process.kill(pid, 'SIGKILL');
|
|
36
|
+
}
|
|
37
|
+
catch { }
|
|
38
|
+
}
|
|
39
|
+
acquire() {
|
|
40
|
+
if (fs.existsSync(this.lockFile)) {
|
|
41
|
+
try {
|
|
42
|
+
const raw = JSON.parse(fs.readFileSync(this.lockFile, 'utf-8'));
|
|
43
|
+
const pid = Number(raw?.pid);
|
|
44
|
+
if (pid && pid !== process.pid && this.isProcessRunning(pid)) {
|
|
45
|
+
this.killProcess(pid);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// ignore corrupted lock
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
fs.unlinkSync(this.lockFile);
|
|
53
|
+
}
|
|
54
|
+
catch { }
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const payload = JSON.stringify({
|
|
58
|
+
pid: process.pid,
|
|
59
|
+
profileId: this.profileId,
|
|
60
|
+
createdAt: Date.now(),
|
|
61
|
+
host: os.hostname(),
|
|
62
|
+
}, null, 2);
|
|
63
|
+
fs.writeFileSync(this.lockFile, payload, { encoding: 'utf-8' });
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
console.error(`[ProfileLock] failed to acquire lock for ${this.profileId}:`, err);
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
release() {
|
|
72
|
+
try {
|
|
73
|
+
if (!fs.existsSync(this.lockFile))
|
|
74
|
+
return;
|
|
75
|
+
const raw = JSON.parse(fs.readFileSync(this.lockFile, 'utf-8'));
|
|
76
|
+
if (Number(raw?.pid) !== process.pid)
|
|
77
|
+
return;
|
|
78
|
+
fs.unlinkSync(this.lockFile);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
console.warn(`[ProfileLock] release failed for ${this.profileId}:`, err);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
85
|
//# sourceMappingURL=ProfileLock.js.map
|
|
@@ -1,184 +1,184 @@
|
|
|
1
|
-
import { BrowserSession } from './BrowserSession.js';
|
|
2
|
-
export const SESSION_CLOSED_EVENT = 'browser-service:session-closed';
|
|
3
|
-
function isProcessAlive(pid) {
|
|
4
|
-
try {
|
|
5
|
-
process.kill(pid, 0);
|
|
6
|
-
return true;
|
|
7
|
-
}
|
|
8
|
-
catch {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export class SessionManager {
|
|
13
|
-
sessions = new Map();
|
|
14
|
-
// Track owning process (e.g. script pid) and bind browser lifetime to it.
|
|
15
|
-
owners = new Map();
|
|
16
|
-
sessionFactory;
|
|
17
|
-
ownerWatchdog;
|
|
18
|
-
ownerWatchdogBusy = false;
|
|
19
|
-
debugLog(label, data) {
|
|
20
|
-
if (process.env.DEBUG !== '1' && process.env.CAMO_DEBUG !== '1' && process.env.CAMO_DEBUG !== '1')
|
|
21
|
-
return;
|
|
22
|
-
try {
|
|
23
|
-
console.log(`[browser-service:${label}] ${JSON.stringify(data)}`);
|
|
24
|
-
}
|
|
25
|
-
catch {
|
|
26
|
-
console.log(`[browser-service:${label}]`, data);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
// Optional options are currently not used, but allowed for future extensions
|
|
30
|
-
constructor(_options, sessionFactory) {
|
|
31
|
-
this.sessionFactory = sessionFactory || ((opts) => new BrowserSession(opts));
|
|
32
|
-
const watchdogMs = Math.max(1000, Number(_options?.ownerWatchdogMs || 5000));
|
|
33
|
-
this.ownerWatchdog = setInterval(() => {
|
|
34
|
-
void this.reapDeadOwners();
|
|
35
|
-
}, watchdogMs);
|
|
36
|
-
this.ownerWatchdog.unref?.();
|
|
37
|
-
}
|
|
38
|
-
normalizeOwnerPid(value) {
|
|
39
|
-
const pid = Number(value || 0);
|
|
40
|
-
return Number.isFinite(pid) && pid > 0 ? pid : null;
|
|
41
|
-
}
|
|
42
|
-
bindOwner(profileId, pid) {
|
|
43
|
-
if (!pid)
|
|
44
|
-
return;
|
|
45
|
-
this.owners.set(profileId, { pid, startedAt: new Date().toISOString() });
|
|
46
|
-
}
|
|
47
|
-
async reapDeadOwners() {
|
|
48
|
-
if (this.ownerWatchdogBusy)
|
|
49
|
-
return;
|
|
50
|
-
this.ownerWatchdogBusy = true;
|
|
51
|
-
try {
|
|
52
|
-
for (const [profileId, owner] of this.owners.entries()) {
|
|
53
|
-
if (!owner?.pid)
|
|
54
|
-
continue;
|
|
55
|
-
if (isProcessAlive(owner.pid))
|
|
56
|
-
continue;
|
|
57
|
-
this.debugLog('owner:dead_cleanup', { profileId, deadPid: owner.pid });
|
|
58
|
-
await this.deleteSession(profileId).catch((err) => {
|
|
59
|
-
this.debugLog('owner:dead_cleanup_error', {
|
|
60
|
-
profileId,
|
|
61
|
-
deadPid: owner.pid,
|
|
62
|
-
error: err?.message || err,
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
finally {
|
|
68
|
-
this.ownerWatchdogBusy = false;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
async createSession(options) {
|
|
72
|
-
const profileId = options.profileId || options.sessionId || `session_${Date.now().toString(36)}`;
|
|
73
|
-
options.profileId = profileId;
|
|
74
|
-
if (!options.sessionName) {
|
|
75
|
-
options.sessionName = profileId;
|
|
76
|
-
}
|
|
77
|
-
this.debugLog('createSession:start', {
|
|
78
|
-
profileId,
|
|
79
|
-
headless: options.headless,
|
|
80
|
-
hasInitialUrl: Boolean(options.initialUrl),
|
|
81
|
-
ownerPid: Number(options.ownerPid || 0) || null,
|
|
82
|
-
});
|
|
83
|
-
const existing = this.sessions.get(profileId);
|
|
84
|
-
const ownerPid = this.normalizeOwnerPid(options.ownerPid);
|
|
85
|
-
if (existing) {
|
|
86
|
-
const owner = this.owners.get(profileId);
|
|
87
|
-
if (owner?.pid && !isProcessAlive(owner.pid)) {
|
|
88
|
-
// Owner died; treat existing session as stale and replace it.
|
|
89
|
-
this.debugLog('createSession:replace_stale_owner', { profileId, deadPid: owner.pid });
|
|
90
|
-
await this.deleteSession(profileId);
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
if (ownerPid && owner?.pid && owner.pid !== ownerPid && isProcessAlive(owner.pid)) {
|
|
94
|
-
throw new Error(`session_owned_by_another_process profile=${profileId} ownerPid=${owner.pid} requesterPid=${ownerPid}`);
|
|
95
|
-
}
|
|
96
|
-
this.bindOwner(profileId, ownerPid);
|
|
97
|
-
this.debugLog('createSession:reuse', {
|
|
98
|
-
profileId,
|
|
99
|
-
ownerPid: this.owners.get(profileId)?.pid || null,
|
|
100
|
-
requesterPid: ownerPid,
|
|
101
|
-
});
|
|
102
|
-
// Reuse existing session (keepalive). Do not restart the browser if it's already running.
|
|
103
|
-
return { sessionId: profileId };
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
const session = this.sessionFactory(options);
|
|
107
|
-
session.onExit = (id) => {
|
|
108
|
-
const current = this.sessions.get(id);
|
|
109
|
-
if (current === session) {
|
|
110
|
-
this.sessions.delete(id);
|
|
111
|
-
// If the user closed the browser window, also terminate the owning script (if any).
|
|
112
|
-
const owner = this.owners.get(id);
|
|
113
|
-
if (owner?.pid) {
|
|
114
|
-
try {
|
|
115
|
-
process.kill(owner.pid, 'SIGTERM');
|
|
116
|
-
}
|
|
117
|
-
catch {
|
|
118
|
-
// ignore
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
this.owners.delete(id);
|
|
122
|
-
process.emit(SESSION_CLOSED_EVENT, id);
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
try {
|
|
126
|
-
await session.start(options.initialUrl);
|
|
127
|
-
}
|
|
128
|
-
catch (err) {
|
|
129
|
-
this.debugLog('createSession:start_failed', {
|
|
130
|
-
profileId,
|
|
131
|
-
error: err?.message || String(err),
|
|
132
|
-
});
|
|
133
|
-
session.onExit = undefined;
|
|
134
|
-
await session.close().catch(() => { });
|
|
135
|
-
throw err;
|
|
136
|
-
}
|
|
137
|
-
this.sessions.set(profileId, session);
|
|
138
|
-
this.debugLog('createSession:started', { profileId });
|
|
139
|
-
this.bindOwner(profileId, ownerPid);
|
|
140
|
-
return { sessionId: profileId };
|
|
141
|
-
}
|
|
142
|
-
getSession(profileId) {
|
|
143
|
-
this.debugLog('getSession', { profileId, hit: this.sessions.has(profileId) });
|
|
144
|
-
return this.sessions.get(profileId);
|
|
145
|
-
}
|
|
146
|
-
listSessions() {
|
|
147
|
-
return Array.from(this.sessions.values()).map((session) => ({
|
|
148
|
-
profileId: session.id,
|
|
149
|
-
session_id: session.id,
|
|
150
|
-
current_url: session.getCurrentUrl(),
|
|
151
|
-
mode: session.modeName,
|
|
152
|
-
owner_pid: this.owners.get(session.id)?.pid || null,
|
|
153
|
-
recording: session.getRecordingStatus(),
|
|
154
|
-
}));
|
|
155
|
-
}
|
|
156
|
-
async getSessionInfo(profileId) {
|
|
157
|
-
const session = this.getSession(profileId);
|
|
158
|
-
if (!session)
|
|
159
|
-
return null;
|
|
160
|
-
return session.getInfo();
|
|
161
|
-
}
|
|
162
|
-
async deleteSession(profileId) {
|
|
163
|
-
const session = this.sessions.get(profileId);
|
|
164
|
-
if (!session)
|
|
165
|
-
return false;
|
|
166
|
-
this.debugLog('deleteSession:start', { profileId });
|
|
167
|
-
session.onExit = undefined;
|
|
168
|
-
await session.close();
|
|
169
|
-
this.sessions.delete(profileId);
|
|
170
|
-
// Deleting a session from API should NOT kill the owner (Stop=only kill script lives in UI).
|
|
171
|
-
this.owners.delete(profileId);
|
|
172
|
-
process.emit(SESSION_CLOSED_EVENT, profileId);
|
|
173
|
-
this.debugLog('deleteSession:done', { profileId });
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
async shutdown() {
|
|
177
|
-
clearInterval(this.ownerWatchdog);
|
|
178
|
-
const jobs = Array.from(this.sessions.values()).map((session) => session.close().catch(() => { }));
|
|
179
|
-
await Promise.all(jobs);
|
|
180
|
-
this.sessions.clear();
|
|
181
|
-
this.owners.clear();
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
//# sourceMappingURL=SessionManager.js.map
|
|
1
|
+
import { BrowserSession } from './BrowserSession.js';
|
|
2
|
+
export const SESSION_CLOSED_EVENT = 'browser-service:session-closed';
|
|
3
|
+
function isProcessAlive(pid) {
|
|
4
|
+
try {
|
|
5
|
+
process.kill(pid, 0);
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class SessionManager {
|
|
13
|
+
sessions = new Map();
|
|
14
|
+
// Track owning process (e.g. script pid) and bind browser lifetime to it.
|
|
15
|
+
owners = new Map();
|
|
16
|
+
sessionFactory;
|
|
17
|
+
ownerWatchdog;
|
|
18
|
+
ownerWatchdogBusy = false;
|
|
19
|
+
debugLog(label, data) {
|
|
20
|
+
if (process.env.DEBUG !== '1' && process.env.CAMO_DEBUG !== '1' && process.env.CAMO_DEBUG !== '1')
|
|
21
|
+
return;
|
|
22
|
+
try {
|
|
23
|
+
console.log(`[browser-service:${label}] ${JSON.stringify(data)}`);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
console.log(`[browser-service:${label}]`, data);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Optional options are currently not used, but allowed for future extensions
|
|
30
|
+
constructor(_options, sessionFactory) {
|
|
31
|
+
this.sessionFactory = sessionFactory || ((opts) => new BrowserSession(opts));
|
|
32
|
+
const watchdogMs = Math.max(1000, Number(_options?.ownerWatchdogMs || 5000));
|
|
33
|
+
this.ownerWatchdog = setInterval(() => {
|
|
34
|
+
void this.reapDeadOwners();
|
|
35
|
+
}, watchdogMs);
|
|
36
|
+
this.ownerWatchdog.unref?.();
|
|
37
|
+
}
|
|
38
|
+
normalizeOwnerPid(value) {
|
|
39
|
+
const pid = Number(value || 0);
|
|
40
|
+
return Number.isFinite(pid) && pid > 0 ? pid : null;
|
|
41
|
+
}
|
|
42
|
+
bindOwner(profileId, pid) {
|
|
43
|
+
if (!pid)
|
|
44
|
+
return;
|
|
45
|
+
this.owners.set(profileId, { pid, startedAt: new Date().toISOString() });
|
|
46
|
+
}
|
|
47
|
+
async reapDeadOwners() {
|
|
48
|
+
if (this.ownerWatchdogBusy)
|
|
49
|
+
return;
|
|
50
|
+
this.ownerWatchdogBusy = true;
|
|
51
|
+
try {
|
|
52
|
+
for (const [profileId, owner] of this.owners.entries()) {
|
|
53
|
+
if (!owner?.pid)
|
|
54
|
+
continue;
|
|
55
|
+
if (isProcessAlive(owner.pid))
|
|
56
|
+
continue;
|
|
57
|
+
this.debugLog('owner:dead_cleanup', { profileId, deadPid: owner.pid });
|
|
58
|
+
await this.deleteSession(profileId).catch((err) => {
|
|
59
|
+
this.debugLog('owner:dead_cleanup_error', {
|
|
60
|
+
profileId,
|
|
61
|
+
deadPid: owner.pid,
|
|
62
|
+
error: err?.message || err,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
this.ownerWatchdogBusy = false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async createSession(options) {
|
|
72
|
+
const profileId = options.profileId || options.sessionId || `session_${Date.now().toString(36)}`;
|
|
73
|
+
options.profileId = profileId;
|
|
74
|
+
if (!options.sessionName) {
|
|
75
|
+
options.sessionName = profileId;
|
|
76
|
+
}
|
|
77
|
+
this.debugLog('createSession:start', {
|
|
78
|
+
profileId,
|
|
79
|
+
headless: options.headless,
|
|
80
|
+
hasInitialUrl: Boolean(options.initialUrl),
|
|
81
|
+
ownerPid: Number(options.ownerPid || 0) || null,
|
|
82
|
+
});
|
|
83
|
+
const existing = this.sessions.get(profileId);
|
|
84
|
+
const ownerPid = this.normalizeOwnerPid(options.ownerPid);
|
|
85
|
+
if (existing) {
|
|
86
|
+
const owner = this.owners.get(profileId);
|
|
87
|
+
if (owner?.pid && !isProcessAlive(owner.pid)) {
|
|
88
|
+
// Owner died; treat existing session as stale and replace it.
|
|
89
|
+
this.debugLog('createSession:replace_stale_owner', { profileId, deadPid: owner.pid });
|
|
90
|
+
await this.deleteSession(profileId);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
if (ownerPid && owner?.pid && owner.pid !== ownerPid && isProcessAlive(owner.pid)) {
|
|
94
|
+
throw new Error(`session_owned_by_another_process profile=${profileId} ownerPid=${owner.pid} requesterPid=${ownerPid}`);
|
|
95
|
+
}
|
|
96
|
+
this.bindOwner(profileId, ownerPid);
|
|
97
|
+
this.debugLog('createSession:reuse', {
|
|
98
|
+
profileId,
|
|
99
|
+
ownerPid: this.owners.get(profileId)?.pid || null,
|
|
100
|
+
requesterPid: ownerPid,
|
|
101
|
+
});
|
|
102
|
+
// Reuse existing session (keepalive). Do not restart the browser if it's already running.
|
|
103
|
+
return { sessionId: profileId };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const session = this.sessionFactory(options);
|
|
107
|
+
session.onExit = (id) => {
|
|
108
|
+
const current = this.sessions.get(id);
|
|
109
|
+
if (current === session) {
|
|
110
|
+
this.sessions.delete(id);
|
|
111
|
+
// If the user closed the browser window, also terminate the owning script (if any).
|
|
112
|
+
const owner = this.owners.get(id);
|
|
113
|
+
if (owner?.pid) {
|
|
114
|
+
try {
|
|
115
|
+
process.kill(owner.pid, 'SIGTERM');
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// ignore
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
this.owners.delete(id);
|
|
122
|
+
process.emit(SESSION_CLOSED_EVENT, id);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
try {
|
|
126
|
+
await session.start(options.initialUrl);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
this.debugLog('createSession:start_failed', {
|
|
130
|
+
profileId,
|
|
131
|
+
error: err?.message || String(err),
|
|
132
|
+
});
|
|
133
|
+
session.onExit = undefined;
|
|
134
|
+
await session.close().catch(() => { });
|
|
135
|
+
throw err;
|
|
136
|
+
}
|
|
137
|
+
this.sessions.set(profileId, session);
|
|
138
|
+
this.debugLog('createSession:started', { profileId });
|
|
139
|
+
this.bindOwner(profileId, ownerPid);
|
|
140
|
+
return { sessionId: profileId };
|
|
141
|
+
}
|
|
142
|
+
getSession(profileId) {
|
|
143
|
+
this.debugLog('getSession', { profileId, hit: this.sessions.has(profileId) });
|
|
144
|
+
return this.sessions.get(profileId);
|
|
145
|
+
}
|
|
146
|
+
listSessions() {
|
|
147
|
+
return Array.from(this.sessions.values()).map((session) => ({
|
|
148
|
+
profileId: session.id,
|
|
149
|
+
session_id: session.id,
|
|
150
|
+
current_url: session.getCurrentUrl(),
|
|
151
|
+
mode: session.modeName,
|
|
152
|
+
owner_pid: this.owners.get(session.id)?.pid || null,
|
|
153
|
+
recording: session.getRecordingStatus(),
|
|
154
|
+
}));
|
|
155
|
+
}
|
|
156
|
+
async getSessionInfo(profileId) {
|
|
157
|
+
const session = this.getSession(profileId);
|
|
158
|
+
if (!session)
|
|
159
|
+
return null;
|
|
160
|
+
return session.getInfo();
|
|
161
|
+
}
|
|
162
|
+
async deleteSession(profileId) {
|
|
163
|
+
const session = this.sessions.get(profileId);
|
|
164
|
+
if (!session)
|
|
165
|
+
return false;
|
|
166
|
+
this.debugLog('deleteSession:start', { profileId });
|
|
167
|
+
session.onExit = undefined;
|
|
168
|
+
await session.close();
|
|
169
|
+
this.sessions.delete(profileId);
|
|
170
|
+
// Deleting a session from API should NOT kill the owner (Stop=only kill script lives in UI).
|
|
171
|
+
this.owners.delete(profileId);
|
|
172
|
+
process.emit(SESSION_CLOSED_EVENT, profileId);
|
|
173
|
+
this.debugLog('deleteSession:done', { profileId });
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
async shutdown() {
|
|
177
|
+
clearInterval(this.ownerWatchdog);
|
|
178
|
+
const jobs = Array.from(this.sessions.values()).map((session) => session.close().catch(() => { }));
|
|
179
|
+
await Promise.all(jobs);
|
|
180
|
+
this.sessions.clear();
|
|
181
|
+
this.owners.clear();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=SessionManager.js.map
|