aws-runtime-bridge 1.9.106 → 1.9.108

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aws-runtime-bridge",
3
- "version": "1.9.106",
3
+ "version": "1.9.108",
4
4
  "description": "AgentsWorkStudio runtime bridge service for machine-level agent runtime integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -11,11 +11,14 @@
11
11
  * 浏览器内核与系统依赖(libnss3 等)一步到位;--with-deps 失败时回退为仅下载内核。
12
12
  * - Linux 非 root:仅下载内核,缺系统库时输出可操作的 sudo 提示。
13
13
  * - 其他平台(Windows/macOS):`install chromium`(系统库由系统自带)。
14
+ * - 下载源:动态读取当前 npm registry 配置,若为 npmmirror/taobao 系镜像则自动
15
+ * 推导对应的 Playwright 二进制镜像加速下载;官方源或其他源走 playwright 默认 CDN。
16
+ * 用户显式设置 PLAYWRIGHT_DOWNLOAD_HOST 时优先尊重用户选择。
14
17
  * - 幂等:playwright 检测到浏览器已安装时直接跳过,重复安装无副作用。
15
18
  * - 容错:任何失败仅警告并给出手动命令,绝不阻断 npm 安装。
16
19
  * - 跳过:设置环境变量 AWS_BRIDGE_SKIP_PLAYWRIGHT_INSTALL=1。
17
20
  */
18
- import { execFile } from "node:child_process";
21
+ import { execFile, spawnSync } from "node:child_process";
19
22
  import { createRequire } from "node:module";
20
23
  import { dirname, join } from "node:path";
21
24
  import { promisify } from "node:util";
@@ -26,9 +29,6 @@ const require = createRequire(import.meta.url);
26
29
  /** 下载超时:Chromium 约 150MB,慢网络下允许 15 分钟。 */
27
30
  const INSTALL_TIMEOUT_MS = 15 * 60 * 1000;
28
31
 
29
- /** npmmirror 镜像,加速国内下载;用户已显式设置 PLAYWRIGHT_DOWNLOAD_HOST 时尊重用户选择。 */
30
- const DEFAULT_PLAYWRIGHT_DOWNLOAD_HOST = "https://npmmirror.com/mirrors/playwright";
31
-
32
32
  /** 定位 playwright 包的 CLI 入口(兼容 npm hoist 与嵌套安装两种布局)。 */
33
33
  function resolvePlaywrightCliPath() {
34
34
  try {
@@ -39,12 +39,47 @@ function resolvePlaywrightCliPath() {
39
39
  }
40
40
  }
41
41
 
42
- async function runInstall(cliPath, args) {
43
- // 未显式设置 PLAYWRIGHT_DOWNLOAD_HOST 时,默认使用 npmmirror 镜像加速国内下载。
44
- const env = { ...process.env };
45
- if (!env.PLAYWRIGHT_DOWNLOAD_HOST) {
46
- env.PLAYWRIGHT_DOWNLOAD_HOST = DEFAULT_PLAYWRIGHT_DOWNLOAD_HOST;
42
+ /**
43
+ * 读取当前 npm 配置中的 registry(综合 npm config get registry 与 .npmrc)。
44
+ * 失败时返回 null,调用方按官方源默认处理。
45
+ */
46
+ function resolveNpmRegistry() {
47
+ try {
48
+ const result = spawnSync("npm", ["config", "get", "registry"], {
49
+ encoding: "utf-8",
50
+ shell: process.platform === "win32",
51
+ stdio: ["ignore", "pipe", "ignore"],
52
+ });
53
+ if (result.status === 0 && result.stdout) {
54
+ const url = result.stdout.trim();
55
+ if (url && !/^undefined$/i.test(url)) return url;
56
+ }
57
+ } catch {
58
+ // 忽略,返回 null
59
+ }
60
+ return null;
61
+ }
62
+
63
+ /**
64
+ * 根据 npm registry 推导 Playwright 二进制镜像下载源。
65
+ *
66
+ * 映射规则:
67
+ * - npmmirror.com / npm.taobao.org 系:返回 https://npmmirror.com/mirrors/playwright
68
+ * (npmmirror 的 binary 镜像路径,已验证可用,与 playwright 的 ${host}/${downloadPath} 拼接格式匹配)
69
+ * - 官方源 registry.npmjs.org 或 null:返回 null,走 playwright 默认 CDN
70
+ * - 其他未知源:返回 null,避免错误覆盖
71
+ */
72
+ function resolvePlaywrightDownloadHost(registry) {
73
+ if (!registry) return null;
74
+ const r = registry.toLowerCase();
75
+ if (r.includes("npmmirror.com") || r.includes("npm.taobao.org")) {
76
+ return "https://npmmirror.com/mirrors/playwright";
47
77
  }
78
+ // 其他镜像源不自动推导,避免路径错误
79
+ return null;
80
+ }
81
+
82
+ async function runInstall(cliPath, args, env) {
48
83
  const { stdout, stderr } = await execFileAsync(
49
84
  process.execPath,
50
85
  [cliPath, ...args],
@@ -81,23 +116,33 @@ async function main() {
81
116
  return;
82
117
  }
83
118
 
84
- const isLinux = process.platform === "linux";
85
- const isRoot =
86
- typeof process.getuid === "function" && process.getuid() === 0;
119
+ // 优先级:用户显式 PLAYWRIGHT_DOWNLOAD_HOST > 动态推导(npm registry 镜像) > 走 playwright 默认 CDN
120
+ const userHost = process.env.PLAYWRIGHT_DOWNLOAD_HOST;
121
+ const registry = resolveNpmRegistry();
122
+ const derivedHost = resolvePlaywrightDownloadHost(registry);
123
+ const downloadHost = userHost || derivedHost;
124
+ const downloadSource = downloadHost || "playwright 默认 CDN";
125
+
126
+ const env = { ...process.env };
127
+ if (downloadHost) {
128
+ env.PLAYWRIGHT_DOWNLOAD_HOST = downloadHost;
129
+ }
87
130
 
88
- const downloadHost = process.env.PLAYWRIGHT_DOWNLOAD_HOST || DEFAULT_PLAYWRIGHT_DOWNLOAD_HOST;
89
- const usingDefaultMirror = downloadHost === DEFAULT_PLAYWRIGHT_DOWNLOAD_HOST;
131
+ const sourceDesc =
132
+ (userHost ? "用户设置" : derivedHost ? `镜像加速(${registry})` : "playwright 默认 CDN");
90
133
  console.log(
91
134
  `[aws-runtime-bridge] 正在准备 Playwright Chromium(首次安装需下载约 150MB,请耐心等待)...` +
92
- (usingDefaultMirror
93
- ? `\n[aws-runtime-bridge] 下载源:${downloadHost}(npmmirror 镜像加速;如需切换可设 PLAYWRIGHT_DOWNLOAD_HOST)`
94
- : ""),
135
+ `\n[aws-runtime-bridge] 下载源:${downloadSource}(${sourceDesc};如需切换可设 PLAYWRIGHT_DOWNLOAD_HOST)`,
95
136
  );
96
137
 
138
+ const isLinux = process.platform === "linux";
139
+ const isRoot =
140
+ typeof process.getuid === "function" && process.getuid() === 0;
141
+
97
142
  // Linux + root:连系统依赖一起装(sudo npm install -g 场景一步到位)
98
143
  if (isLinux && isRoot) {
99
144
  try {
100
- await runInstall(cliPath, ["install", "--with-deps", "chromium"]);
145
+ await runInstall(cliPath, ["install", "--with-deps", "chromium"], env);
101
146
  console.log("[aws-runtime-bridge] Playwright Chromium 与系统依赖安装完成");
102
147
  return;
103
148
  } catch (error) {
@@ -112,7 +157,7 @@ async function main() {
112
157
  }
113
158
 
114
159
  try {
115
- await runInstall(cliPath, ["install", "chromium"]);
160
+ await runInstall(cliPath, ["install", "chromium"], env);
116
161
  console.log("[aws-runtime-bridge] Playwright Chromium 安装完成");
117
162
  } catch (error) {
118
163
  console.warn(