@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.
Files changed (114) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +586 -586
  3. package/bin/browser-service.mjs +11 -11
  4. package/bin/camo.mjs +22 -22
  5. package/package.json +48 -48
  6. package/scripts/build.mjs +19 -19
  7. package/scripts/bump-version.mjs +34 -34
  8. package/scripts/check-file-size.mjs +80 -80
  9. package/scripts/file-size-policy.json +12 -2
  10. package/scripts/install.mjs +76 -76
  11. package/scripts/release.sh +54 -54
  12. package/src/autoscript/action-providers/index.mjs +6 -6
  13. package/src/autoscript/impact-engine.mjs +78 -78
  14. package/src/autoscript/runtime.mjs +1017 -1017
  15. package/src/autoscript/schema.mjs +376 -376
  16. package/src/cli.mjs +405 -405
  17. package/src/commands/attach.mjs +141 -141
  18. package/src/commands/autoscript.mjs +1011 -1011
  19. package/src/commands/browser.mjs +1255 -1255
  20. package/src/commands/container.mjs +401 -401
  21. package/src/commands/cookies.mjs +69 -69
  22. package/src/commands/create.mjs +98 -98
  23. package/src/commands/devtools.mjs +349 -349
  24. package/src/commands/events.mjs +152 -152
  25. package/src/commands/highlight-mode.mjs +24 -24
  26. package/src/commands/init.mjs +68 -68
  27. package/src/commands/lifecycle.mjs +275 -275
  28. package/src/commands/mouse.mjs +45 -45
  29. package/src/commands/profile.mjs +46 -46
  30. package/src/commands/record.mjs +115 -115
  31. package/src/commands/system.mjs +14 -14
  32. package/src/commands/window.mjs +123 -123
  33. package/src/container/change-notifier.mjs +362 -362
  34. package/src/container/element-filter.mjs +143 -143
  35. package/src/container/index.mjs +3 -3
  36. package/src/container/runtime-core/checkpoint.mjs +209 -209
  37. package/src/container/runtime-core/index.mjs +21 -21
  38. package/src/container/runtime-core/operations/index.mjs +774 -774
  39. package/src/container/runtime-core/operations/selector-scripts.mjs +277 -277
  40. package/src/container/runtime-core/operations/tab-pool.mjs +746 -746
  41. package/src/container/runtime-core/operations/viewport.mjs +189 -189
  42. package/src/container/runtime-core/search.mjs +190 -190
  43. package/src/container/runtime-core/subscription.mjs +224 -224
  44. package/src/container/runtime-core/utils.mjs +94 -94
  45. package/src/container/runtime-core/validation.mjs +127 -127
  46. package/src/container/runtime-core.mjs +1 -1
  47. package/src/container/subscription-registry.mjs +459 -459
  48. package/src/core/actions.mjs +561 -561
  49. package/src/core/browser.mjs +266 -266
  50. package/src/core/index.mjs +52 -52
  51. package/src/core/utils.mjs +91 -91
  52. package/src/events/daemon-entry.mjs +33 -33
  53. package/src/events/daemon.mjs +80 -80
  54. package/src/events/progress-log.mjs +109 -109
  55. package/src/events/ws-server.mjs +239 -239
  56. package/src/lib/client.mjs +200 -200
  57. package/src/lifecycle/cleanup.mjs +83 -83
  58. package/src/lifecycle/lock.mjs +126 -126
  59. package/src/lifecycle/session-registry.mjs +279 -279
  60. package/src/lifecycle/session-view.mjs +76 -76
  61. package/src/lifecycle/session-watchdog.mjs +281 -281
  62. package/src/services/browser-service/index.js +671 -671
  63. package/src/services/browser-service/internal/BrowserSession.input.test.js +389 -389
  64. package/src/services/browser-service/internal/BrowserSession.js +325 -304
  65. package/src/services/browser-service/internal/ElementRegistry.js +60 -60
  66. package/src/services/browser-service/internal/ProfileLock.js +84 -84
  67. package/src/services/browser-service/internal/SessionManager.js +184 -184
  68. package/src/services/browser-service/internal/SessionManager.test.js +39 -39
  69. package/src/services/browser-service/internal/browser-session/cookies.js +144 -144
  70. package/src/services/browser-service/internal/browser-session/input-ops.js +222 -222
  71. package/src/services/browser-service/internal/browser-session/input-pipeline.js +144 -144
  72. package/src/services/browser-service/internal/browser-session/logging.js +46 -46
  73. package/src/services/browser-service/internal/browser-session/navigation.js +38 -38
  74. package/src/services/browser-service/internal/browser-session/page-hooks.js +442 -442
  75. package/src/services/browser-service/internal/browser-session/page-management.js +302 -302
  76. package/src/services/browser-service/internal/browser-session/page-management.test.js +148 -148
  77. package/src/services/browser-service/internal/browser-session/recording.js +198 -198
  78. package/src/services/browser-service/internal/browser-session/runtime-events.js +61 -61
  79. package/src/services/browser-service/internal/browser-session/session-core.js +84 -84
  80. package/src/services/browser-service/internal/browser-session/session-state.js +38 -38
  81. package/src/services/browser-service/internal/browser-session/types.js +14 -14
  82. package/src/services/browser-service/internal/browser-session/utils.js +95 -95
  83. package/src/services/browser-service/internal/browser-session/viewport-manager.js +46 -46
  84. package/src/services/browser-service/internal/browser-session/viewport.js +215 -215
  85. package/src/services/browser-service/internal/container-matcher.js +851 -851
  86. package/src/services/browser-service/internal/container-registry.js +182 -182
  87. package/src/services/browser-service/internal/engine-manager.js +259 -259
  88. package/src/services/browser-service/internal/fingerprint.js +203 -203
  89. package/src/services/browser-service/internal/heartbeat.js +137 -137
  90. package/src/services/browser-service/internal/logging.js +46 -46
  91. package/src/services/browser-service/internal/page-runtime/runtime.js +1317 -1317
  92. package/src/services/browser-service/internal/pageRuntime.js +28 -28
  93. package/src/services/browser-service/internal/runtimeInjector.js +31 -31
  94. package/src/services/browser-service/internal/service-process-logger.js +140 -140
  95. package/src/services/browser-service/internal/state-bus.js +45 -45
  96. package/src/services/browser-service/internal/storage-paths.js +42 -42
  97. package/src/services/browser-service/internal/ws-server.js +1194 -1194
  98. package/src/services/browser-service/internal/ws-server.test.js +58 -58
  99. package/src/services/browser-service/server.mjs +6 -6
  100. package/src/services/controller/cli-bridge.js +93 -93
  101. package/src/services/controller/container-index.js +50 -50
  102. package/src/services/controller/container-storage.js +36 -36
  103. package/src/services/controller/controller-actions.js +207 -207
  104. package/src/services/controller/controller.js +1138 -1138
  105. package/src/services/controller/selectors.js +54 -54
  106. package/src/services/controller/transport.js +125 -125
  107. package/src/utils/args.mjs +26 -26
  108. package/src/utils/browser-service.mjs +544 -544
  109. package/src/utils/command-log.mjs +64 -64
  110. package/src/utils/config.mjs +214 -214
  111. package/src/utils/fingerprint.mjs +181 -181
  112. package/src/utils/help.mjs +216 -216
  113. package/src/utils/js-policy.mjs +13 -13
  114. package/src/utils/ws-client.mjs +30 -30
@@ -1,259 +1,259 @@
1
- import os from 'node:os';
2
- import { spawnSync } from 'node:child_process';
3
- function readNumber(v) {
4
- const n = Number(v);
5
- return Number.isFinite(n) && n > 0 ? n : null;
6
- }
7
- function getDisplayMetrics() {
8
- const envWidth = readNumber(process.env.CAMO_SCREEN_WIDTH || process.env.CAMO_SCREEN_WIDTH);
9
- const envHeight = readNumber(process.env.CAMO_SCREEN_HEIGHT || process.env.CAMO_SCREEN_HEIGHT);
10
- if (envWidth && envHeight) {
11
- return { width: envWidth, height: envHeight, source: 'env' };
12
- }
13
- if (os.platform() === 'darwin') {
14
- try {
15
- const sp = spawnSync('system_profiler', ['SPDisplaysDataType', '-json'], { encoding: 'utf8' });
16
- const spJson = sp.status === 0 && sp.stdout ? JSON.parse(sp.stdout) : null;
17
- let width = null;
18
- let height = null;
19
- const displays = spJson?.SPDisplaysDataType;
20
- if (Array.isArray(displays) && displays.length > 0) {
21
- const first = displays[0];
22
- const gpus = first?._items;
23
- const maybe = Array.isArray(gpus) && gpus.length > 0 ? gpus[0] : first;
24
- const resStr = maybe?.spdisplays_ndrvs?.[0]?._spdisplays_resolution || maybe?._spdisplays_resolution;
25
- if (typeof resStr === 'string') {
26
- const m = resStr.match(/(\d+)\s*x\s*(\d+)/i);
27
- if (m) {
28
- width = readNumber(m[1]);
29
- height = readNumber(m[2]);
30
- }
31
- }
32
- }
33
- const osascript = spawnSync('osascript', ['-l', 'JavaScript', '-e',
34
- `ObjC.import('AppKit');
35
- const s = $.NSScreen.mainScreen;
36
- const f = s.frame;
37
- const v = s.visibleFrame;
38
- JSON.stringify({
39
- width: Number(f.size.width),
40
- height: Number(f.size.height),
41
- workWidth: Number(v.size.width),
42
- workHeight: Number(v.size.height)
43
- });`
44
- ], { encoding: 'utf8' });
45
- const vf = osascript.status === 0 && osascript.stdout ? JSON.parse(osascript.stdout.trim()) : null;
46
- const finalW = readNumber(vf?.width) || width;
47
- const finalH = readNumber(vf?.height) || height;
48
- const workWidth = readNumber(vf?.workWidth);
49
- const workHeight = readNumber(vf?.workHeight);
50
- if (!finalW || !finalH)
51
- return null;
52
- return {
53
- width: finalW,
54
- height: finalH,
55
- ...(workWidth ? { workWidth } : {}),
56
- ...(workHeight ? { workHeight } : {}),
57
- source: 'darwin',
58
- };
59
- }
60
- catch {
61
- return null;
62
- }
63
- }
64
- if (os.platform() !== 'win32')
65
- return null;
66
- try {
67
- const script = [
68
- 'Add-Type -AssemblyName System.Windows.Forms;',
69
- '$screen=[System.Windows.Forms.Screen]::PrimaryScreen;',
70
- '$b=$screen.Bounds;',
71
- '$w=$screen.WorkingArea;',
72
- '$video=Get-CimInstance Win32_VideoController | Select-Object -First 1;',
73
- '$nw=$null;$nh=$null;',
74
- 'if ($video) { $nw=$video.CurrentHorizontalResolution; $nh=$video.CurrentVerticalResolution }',
75
- '$o=[pscustomobject]@{width=$b.Width;height=$b.Height;workWidth=$w.Width;workHeight=$w.Height;nativeWidth=$nw;nativeHeight=$nh};',
76
- '$o | ConvertTo-Json -Compress',
77
- ].join(' ');
78
- const res = spawnSync('powershell', ['-NoProfile', '-Command', script], {
79
- encoding: 'utf8',
80
- windowsHide: true,
81
- });
82
- if (res.status !== 0 || !res.stdout)
83
- return null;
84
- const payload = JSON.parse(res.stdout.trim());
85
- const nativeWidth = readNumber(payload?.nativeWidth);
86
- const nativeHeight = readNumber(payload?.nativeHeight);
87
- const width = readNumber(payload?.width) || nativeWidth || null;
88
- const height = readNumber(payload?.height) || nativeHeight || null;
89
- const workWidth = readNumber(payload?.workWidth);
90
- const workHeight = readNumber(payload?.workHeight);
91
- if (!width || !height)
92
- return null;
93
- return {
94
- width,
95
- height,
96
- ...(workWidth ? { workWidth } : {}),
97
- ...(workHeight ? { workHeight } : {}),
98
- ...(nativeWidth ? { nativeWidth } : {}),
99
- ...(nativeHeight ? { nativeHeight } : {}),
100
- source: 'win32',
101
- };
102
- }
103
- catch {
104
- return null;
105
- }
106
- }
107
- function getDisplayMetricsWithDPR() {
108
- const dm = getDisplayMetrics();
109
- const base = dm ? { ...dm } : {};
110
- const width = Number(base.width) || 0;
111
- const height = Number(base.height) || 0;
112
- const workWidth = Number(base.workWidth) || width;
113
- const workHeight = Number(base.workHeight) || height;
114
- let dpr = 1;
115
- if (os.platform() === 'darwin') {
116
- try {
117
- const sp = spawnSync('system_profiler', ['SPDisplaysDataType', '-json'], { encoding: 'utf8' });
118
- const spJson = sp.status === 0 && sp.stdout ? JSON.parse(sp.stdout) : null;
119
- const displays = spJson?.SPDisplaysDataType;
120
- if (Array.isArray(displays) && displays.length > 0) {
121
- const first = displays[0];
122
- const gpus = first?._items;
123
- const maybe = Array.isArray(gpus) && gpus.length > 0 ? gpus[0] : first;
124
- const isRetina = maybe?.spdisplays_retina === 'spdisplays_yes' || maybe?.spdisplays_retina === true;
125
- if (isRetina)
126
- dpr = 2;
127
- }
128
- }
129
- catch {
130
- if (width >= 2560 && height >= 1440)
131
- dpr = 2;
132
- }
133
- }
134
- return { width, height, workWidth, workHeight, source: base.source || 'unknown', dpr };
135
- }
136
- async function loadCamoufox() {
137
- // Prefer dynamic import; fall back to require for NodeNext interop edge cases.
138
- try {
139
- return await import('camoufox');
140
- }
141
- catch (err) {
142
- const mod = await import('node:module');
143
- const require = mod.createRequire(import.meta.url);
144
- return require('camoufox');
145
- }
146
- }
147
- export async function launchEngineContext(opts) {
148
- if (opts.engine === 'camoufox') {
149
- // Camoufox/Firefox is sensitive to non-integer window placement/size values.
150
- // In practice, when the underlying launch path attempts to derive screenY/screenX from floats,
151
- // Firefox can crash with: "Invalid type for property window.screenY. Expected int, got number".
152
- // Also, we want the window size to reflect the *real* OS work area so later system clicks
153
- // have enough usable viewport.
154
- // Prefer OS work area (real screen resolution) for window size; fall back to viewport.
155
- const dm = getDisplayMetricsWithDPR();
156
- // Physical display size from OS
157
- const physicalW = Number(dm?.width || 4096);
158
- const physicalH = Number(dm?.height || 2304);
159
- const workW = Number(dm?.workWidth || physicalW || 4096);
160
- const workH = Number(dm?.workHeight || physicalH || 2190);
161
- // Base viewport target:
162
- // - prefer explicit/fingerprint viewport if provided
163
- // - otherwise use a deterministic default for stable first-run UX
164
- const requestedW = Number(opts.viewport?.width || process.env.CAMO_VIEWPORT_WIDTH || process.env.CAMO_VIEWPORT_WIDTH || 1440);
165
- const requestedH = Number(opts.viewport?.height || process.env.CAMO_VIEWPORT_HEIGHT || process.env.CAMO_VIEWPORT_HEIGHT || 1100);
166
- const viewportW = Math.max(900, Math.floor(Number(requestedW) || 1440));
167
- const viewportH = Math.max(700, Math.floor(Number(requestedH) || 1100));
168
- const envHeadlessW = Number(process.env.CAMO_HEADLESS_WIDTH || process.env.CAMO_HEADLESS_WIDTH || 0);
169
- const envHeadlessH = Number(process.env.CAMO_HEADLESS_HEIGHT || process.env.CAMO_HEADLESS_HEIGHT || 0);
170
- const headlessW = envHeadlessW > 0 ? envHeadlessW : viewportW;
171
- const headlessH = envHeadlessH > 0 ? envHeadlessH : viewportH;
172
- if (!Number.isFinite(headlessW) || !Number.isFinite(headlessH)) {
173
- throw new Error('headless viewport invalid');
174
- }
175
- const headless = Boolean(opts.headless);
176
- // Final headful window size:
177
- // align window with viewport by default and keep it inside available work area.
178
- const maxHeadfulW = workW > 0 ? Math.max(900, workW - 40) : 1920;
179
- const maxHeadfulH = workH > 0 ? Math.max(700, workH - 80) : 1200;
180
- const winW = headless ? headlessW : Math.min(viewportW, maxHeadfulW);
181
- const winH = headless ? headlessH : Math.min(viewportH, maxHeadfulH);
182
- // Ensure integer types for Camoufox
183
- const screenW = Math.floor(physicalW) | 0;
184
- const screenH = Math.floor(physicalH) | 0;
185
- const intWinW = Math.floor(winW) | 0;
186
- const intWinH = Math.floor(winH) | 0;
187
- // Firefox/Camoufox requires screenX/screenY to be integers; default to 0 to avoid float errors.
188
- const camoufox = await loadCamoufox();
189
- const Camoufox = camoufox.Camoufox;
190
- if (!Camoufox)
191
- throw new Error('camoufox_invalid_api');
192
- const targetOS = process.platform === 'win32'
193
- ? 'windows'
194
- : process.platform === 'darwin'
195
- ? 'macos'
196
- : 'linux';
197
- // Build config to force actual display dimensions (without screen constraints to avoid position calc issues)
198
- const config = {
199
- 'screen.width': Math.floor(physicalW),
200
- 'screen.height': Math.floor(physicalH),
201
- 'screen.availWidth': Math.floor(workW),
202
- 'screen.availHeight': Math.floor(workH),
203
- 'window.screenX': 0,
204
- 'window.screenY': 0,
205
- };
206
- // Force tabs to open in same window (not new windows)
207
- const firefox_user_prefs = {
208
- // Force all new windows (including script popups) to open as tabs
209
- 'browser.link.open_newwindow': 3,
210
- 'browser.link.open_newwindow.restriction': 0,
211
- 'browser.link.open_newwindow.override.external': -1,
212
- // Make sure tab strip is visible and no single-window mode
213
- 'browser.tabs.loadInBackground': false,
214
- 'browser.tabs.loadDivertedInBackground': false,
215
- 'browser.tabs.closeWindowWithLastTab': false,
216
- 'browser.tabs.warnOnClose': false,
217
- 'browser.tabs.tabMinWidth': 50,
218
- };
219
- const result = await Camoufox({
220
- headless,
221
- os: targetOS,
222
- window: [intWinW, intWinH],
223
- viewport: headless ? { width: headlessW, height: headlessH } : null,
224
- firefox_user_prefs,
225
- config,
226
- data_dir: opts.profileDir,
227
- humanize: true,
228
- iKnowWhatImDoing: true,
229
- locale: 'zh-CN',
230
- fonts: [
231
- 'PingFang SC',
232
- 'Hiragino Sans GB',
233
- 'STHeiti',
234
- 'Microsoft YaHei',
235
- 'SimHei',
236
- 'SimSun',
237
- 'Microsoft JhengHei',
238
- 'Noto Sans CJK SC',
239
- 'Source Han Sans SC',
240
- 'Arial Unicode MS',
241
- 'Helvetica',
242
- 'Arial',
243
- 'Sans-Serif',
244
- ],
245
- custom_fonts_only: false,
246
- });
247
- if (result && typeof result.pages === 'function') {
248
- // Already a BrowserContext
249
- return result;
250
- }
251
- if (result && typeof result.newContext === 'function') {
252
- // Browser -> create a new context
253
- return (await result.newContext());
254
- }
255
- throw new Error('camoufox_invalid_response');
256
- }
257
- throw new Error('chromium_removed');
258
- }
259
- //# sourceMappingURL=engine-manager.js.map
1
+ import os from 'node:os';
2
+ import { spawnSync } from 'node:child_process';
3
+ function readNumber(v) {
4
+ const n = Number(v);
5
+ return Number.isFinite(n) && n > 0 ? n : null;
6
+ }
7
+ function getDisplayMetrics() {
8
+ const envWidth = readNumber(process.env.CAMO_SCREEN_WIDTH || process.env.CAMO_SCREEN_WIDTH);
9
+ const envHeight = readNumber(process.env.CAMO_SCREEN_HEIGHT || process.env.CAMO_SCREEN_HEIGHT);
10
+ if (envWidth && envHeight) {
11
+ return { width: envWidth, height: envHeight, source: 'env' };
12
+ }
13
+ if (os.platform() === 'darwin') {
14
+ try {
15
+ const sp = spawnSync('system_profiler', ['SPDisplaysDataType', '-json'], { encoding: 'utf8' });
16
+ const spJson = sp.status === 0 && sp.stdout ? JSON.parse(sp.stdout) : null;
17
+ let width = null;
18
+ let height = null;
19
+ const displays = spJson?.SPDisplaysDataType;
20
+ if (Array.isArray(displays) && displays.length > 0) {
21
+ const first = displays[0];
22
+ const gpus = first?._items;
23
+ const maybe = Array.isArray(gpus) && gpus.length > 0 ? gpus[0] : first;
24
+ const resStr = maybe?.spdisplays_ndrvs?.[0]?._spdisplays_resolution || maybe?._spdisplays_resolution;
25
+ if (typeof resStr === 'string') {
26
+ const m = resStr.match(/(\d+)\s*x\s*(\d+)/i);
27
+ if (m) {
28
+ width = readNumber(m[1]);
29
+ height = readNumber(m[2]);
30
+ }
31
+ }
32
+ }
33
+ const osascript = spawnSync('osascript', ['-l', 'JavaScript', '-e',
34
+ `ObjC.import('AppKit');
35
+ const s = $.NSScreen.mainScreen;
36
+ const f = s.frame;
37
+ const v = s.visibleFrame;
38
+ JSON.stringify({
39
+ width: Number(f.size.width),
40
+ height: Number(f.size.height),
41
+ workWidth: Number(v.size.width),
42
+ workHeight: Number(v.size.height)
43
+ });`
44
+ ], { encoding: 'utf8' });
45
+ const vf = osascript.status === 0 && osascript.stdout ? JSON.parse(osascript.stdout.trim()) : null;
46
+ const finalW = readNumber(vf?.width) || width;
47
+ const finalH = readNumber(vf?.height) || height;
48
+ const workWidth = readNumber(vf?.workWidth);
49
+ const workHeight = readNumber(vf?.workHeight);
50
+ if (!finalW || !finalH)
51
+ return null;
52
+ return {
53
+ width: finalW,
54
+ height: finalH,
55
+ ...(workWidth ? { workWidth } : {}),
56
+ ...(workHeight ? { workHeight } : {}),
57
+ source: 'darwin',
58
+ };
59
+ }
60
+ catch {
61
+ return null;
62
+ }
63
+ }
64
+ if (os.platform() !== 'win32')
65
+ return null;
66
+ try {
67
+ const script = [
68
+ 'Add-Type -AssemblyName System.Windows.Forms;',
69
+ '$screen=[System.Windows.Forms.Screen]::PrimaryScreen;',
70
+ '$b=$screen.Bounds;',
71
+ '$w=$screen.WorkingArea;',
72
+ '$video=Get-CimInstance Win32_VideoController | Select-Object -First 1;',
73
+ '$nw=$null;$nh=$null;',
74
+ 'if ($video) { $nw=$video.CurrentHorizontalResolution; $nh=$video.CurrentVerticalResolution }',
75
+ '$o=[pscustomobject]@{width=$b.Width;height=$b.Height;workWidth=$w.Width;workHeight=$w.Height;nativeWidth=$nw;nativeHeight=$nh};',
76
+ '$o | ConvertTo-Json -Compress',
77
+ ].join(' ');
78
+ const res = spawnSync('powershell', ['-NoProfile', '-Command', script], {
79
+ encoding: 'utf8',
80
+ windowsHide: true,
81
+ });
82
+ if (res.status !== 0 || !res.stdout)
83
+ return null;
84
+ const payload = JSON.parse(res.stdout.trim());
85
+ const nativeWidth = readNumber(payload?.nativeWidth);
86
+ const nativeHeight = readNumber(payload?.nativeHeight);
87
+ const width = readNumber(payload?.width) || nativeWidth || null;
88
+ const height = readNumber(payload?.height) || nativeHeight || null;
89
+ const workWidth = readNumber(payload?.workWidth);
90
+ const workHeight = readNumber(payload?.workHeight);
91
+ if (!width || !height)
92
+ return null;
93
+ return {
94
+ width,
95
+ height,
96
+ ...(workWidth ? { workWidth } : {}),
97
+ ...(workHeight ? { workHeight } : {}),
98
+ ...(nativeWidth ? { nativeWidth } : {}),
99
+ ...(nativeHeight ? { nativeHeight } : {}),
100
+ source: 'win32',
101
+ };
102
+ }
103
+ catch {
104
+ return null;
105
+ }
106
+ }
107
+ function getDisplayMetricsWithDPR() {
108
+ const dm = getDisplayMetrics();
109
+ const base = dm ? { ...dm } : {};
110
+ const width = Number(base.width) || 0;
111
+ const height = Number(base.height) || 0;
112
+ const workWidth = Number(base.workWidth) || width;
113
+ const workHeight = Number(base.workHeight) || height;
114
+ let dpr = 1;
115
+ if (os.platform() === 'darwin') {
116
+ try {
117
+ const sp = spawnSync('system_profiler', ['SPDisplaysDataType', '-json'], { encoding: 'utf8' });
118
+ const spJson = sp.status === 0 && sp.stdout ? JSON.parse(sp.stdout) : null;
119
+ const displays = spJson?.SPDisplaysDataType;
120
+ if (Array.isArray(displays) && displays.length > 0) {
121
+ const first = displays[0];
122
+ const gpus = first?._items;
123
+ const maybe = Array.isArray(gpus) && gpus.length > 0 ? gpus[0] : first;
124
+ const isRetina = maybe?.spdisplays_retina === 'spdisplays_yes' || maybe?.spdisplays_retina === true;
125
+ if (isRetina)
126
+ dpr = 2;
127
+ }
128
+ }
129
+ catch {
130
+ if (width >= 2560 && height >= 1440)
131
+ dpr = 2;
132
+ }
133
+ }
134
+ return { width, height, workWidth, workHeight, source: base.source || 'unknown', dpr };
135
+ }
136
+ async function loadCamoufox() {
137
+ // Prefer dynamic import; fall back to require for NodeNext interop edge cases.
138
+ try {
139
+ return await import('camoufox');
140
+ }
141
+ catch (err) {
142
+ const mod = await import('node:module');
143
+ const require = mod.createRequire(import.meta.url);
144
+ return require('camoufox');
145
+ }
146
+ }
147
+ export async function launchEngineContext(opts) {
148
+ if (opts.engine === 'camoufox') {
149
+ // Camoufox/Firefox is sensitive to non-integer window placement/size values.
150
+ // In practice, when the underlying launch path attempts to derive screenY/screenX from floats,
151
+ // Firefox can crash with: "Invalid type for property window.screenY. Expected int, got number".
152
+ // Also, we want the window size to reflect the *real* OS work area so later system clicks
153
+ // have enough usable viewport.
154
+ // Prefer OS work area (real screen resolution) for window size; fall back to viewport.
155
+ const dm = getDisplayMetricsWithDPR();
156
+ // Physical display size from OS
157
+ const physicalW = Number(dm?.width || 4096);
158
+ const physicalH = Number(dm?.height || 2304);
159
+ const workW = Number(dm?.workWidth || physicalW || 4096);
160
+ const workH = Number(dm?.workHeight || physicalH || 2190);
161
+ // Base viewport target:
162
+ // - prefer explicit/fingerprint viewport if provided
163
+ // - otherwise use a deterministic default for stable first-run UX
164
+ const requestedW = Number(opts.viewport?.width || process.env.CAMO_VIEWPORT_WIDTH || process.env.CAMO_VIEWPORT_WIDTH || 1440);
165
+ const requestedH = Number(opts.viewport?.height || process.env.CAMO_VIEWPORT_HEIGHT || process.env.CAMO_VIEWPORT_HEIGHT || 1100);
166
+ const viewportW = Math.max(900, Math.floor(Number(requestedW) || 1440));
167
+ const viewportH = Math.max(700, Math.floor(Number(requestedH) || 1100));
168
+ const envHeadlessW = Number(process.env.CAMO_HEADLESS_WIDTH || process.env.CAMO_HEADLESS_WIDTH || 0);
169
+ const envHeadlessH = Number(process.env.CAMO_HEADLESS_HEIGHT || process.env.CAMO_HEADLESS_HEIGHT || 0);
170
+ const headlessW = envHeadlessW > 0 ? envHeadlessW : viewportW;
171
+ const headlessH = envHeadlessH > 0 ? envHeadlessH : viewportH;
172
+ if (!Number.isFinite(headlessW) || !Number.isFinite(headlessH)) {
173
+ throw new Error('headless viewport invalid');
174
+ }
175
+ const headless = Boolean(opts.headless);
176
+ // Final headful window size:
177
+ // align window with viewport by default and keep it inside available work area.
178
+ const maxHeadfulW = workW > 0 ? Math.max(900, workW - 40) : 1920;
179
+ const maxHeadfulH = workH > 0 ? Math.max(700, workH - 80) : 1200;
180
+ const winW = headless ? headlessW : Math.min(viewportW, maxHeadfulW);
181
+ const winH = headless ? headlessH : Math.min(viewportH, maxHeadfulH);
182
+ // Ensure integer types for Camoufox
183
+ const screenW = Math.floor(physicalW) | 0;
184
+ const screenH = Math.floor(physicalH) | 0;
185
+ const intWinW = Math.floor(winW) | 0;
186
+ const intWinH = Math.floor(winH) | 0;
187
+ // Firefox/Camoufox requires screenX/screenY to be integers; default to 0 to avoid float errors.
188
+ const camoufox = await loadCamoufox();
189
+ const Camoufox = camoufox.Camoufox;
190
+ if (!Camoufox)
191
+ throw new Error('camoufox_invalid_api');
192
+ const targetOS = process.platform === 'win32'
193
+ ? 'windows'
194
+ : process.platform === 'darwin'
195
+ ? 'macos'
196
+ : 'linux';
197
+ // Build config to force actual display dimensions (without screen constraints to avoid position calc issues)
198
+ const config = {
199
+ 'screen.width': Math.floor(physicalW),
200
+ 'screen.height': Math.floor(physicalH),
201
+ 'screen.availWidth': Math.floor(workW),
202
+ 'screen.availHeight': Math.floor(workH),
203
+ 'window.screenX': 0,
204
+ 'window.screenY': 0,
205
+ };
206
+ // Force tabs to open in same window (not new windows)
207
+ const firefox_user_prefs = {
208
+ // Force all new windows (including script popups) to open as tabs
209
+ 'browser.link.open_newwindow': 3,
210
+ 'browser.link.open_newwindow.restriction': 0,
211
+ 'browser.link.open_newwindow.override.external': -1,
212
+ // Make sure tab strip is visible and no single-window mode
213
+ 'browser.tabs.loadInBackground': false,
214
+ 'browser.tabs.loadDivertedInBackground': false,
215
+ 'browser.tabs.closeWindowWithLastTab': false,
216
+ 'browser.tabs.warnOnClose': false,
217
+ 'browser.tabs.tabMinWidth': 50,
218
+ };
219
+ const result = await Camoufox({
220
+ headless,
221
+ os: targetOS,
222
+ window: [intWinW, intWinH],
223
+ viewport: headless ? { width: headlessW, height: headlessH } : null,
224
+ firefox_user_prefs,
225
+ config,
226
+ data_dir: opts.profileDir,
227
+ humanize: true,
228
+ iKnowWhatImDoing: true,
229
+ locale: 'zh-CN',
230
+ fonts: [
231
+ 'PingFang SC',
232
+ 'Hiragino Sans GB',
233
+ 'STHeiti',
234
+ 'Microsoft YaHei',
235
+ 'SimHei',
236
+ 'SimSun',
237
+ 'Microsoft JhengHei',
238
+ 'Noto Sans CJK SC',
239
+ 'Source Han Sans SC',
240
+ 'Arial Unicode MS',
241
+ 'Helvetica',
242
+ 'Arial',
243
+ 'Sans-Serif',
244
+ ],
245
+ custom_fonts_only: false,
246
+ });
247
+ if (result && typeof result.pages === 'function') {
248
+ // Already a BrowserContext
249
+ return result;
250
+ }
251
+ if (result && typeof result.newContext === 'function') {
252
+ // Browser -> create a new context
253
+ return (await result.newContext());
254
+ }
255
+ throw new Error('camoufox_invalid_response');
256
+ }
257
+ throw new Error('chromium_removed');
258
+ }
259
+ //# sourceMappingURL=engine-manager.js.map