com.jimuwd.xian.registry-proxy 1.0.1 → 1.0.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.
Files changed (2) hide show
  1. package/README.MD +92 -38
  2. package/package.json +1 -1
package/README.MD CHANGED
@@ -3,14 +3,14 @@
3
3
 
4
4
  ## 项目简介
5
5
 
6
- `com.jimuwd.xian.registry-proxy` 是一个轻量级的 npm 代理服务器,旨在为 Node.js 项目提供多 registry 的代理和 fallback 机制。它通过读取 Yarn 的配置文件(`.yarnrc.yml`),将多个 npm registry(如公共 registry、私有仓库等)代理到一个本地端口(默认 `4873`),并支持在本地 token 缺失时从全局配置文件回退获取认证信息。
6
+ `com.jimuwd.xian.registry-proxy` 是一个轻量级的 npm 代理服务器,旨在为 Node.js 项目提供多 registry 的代理和 fallback 机制。它通过读取独立的配置文件 `.registry-proxy.yml`,将多个 npm registry(如公共 registry、私有仓库等)代理到一个本地端口(默认 `4873`),并支持在 `.registry-proxy.yml` 中 token 缺失时从 Yarn 配置文件(本地 `.yarnrc.yml` 和全局 `~/.yarnrc.yml`)回退获取认证信息。
7
7
  主要功能:
8
8
  - **多 registry 代理**:将多个 npm registry 统一代理到本地端口,提供单一访问入口。
9
9
  - **Fallback 机制**:按配置顺序尝试多个 registry,直到找到可用包。
10
- - **灵活的 token 管理**:优先使用本地配置文件中的 `npmAuthToken`,缺失时从全局配置文件读取。
10
+ - **灵活的 token 管理**:优先使用 `.registry-proxy.yml` 中的 `npmAuthToken`,缺失时从 Yarn 配置文件读取。
11
11
  - **优雅关闭**:支持通过 SIGTERM 信号优雅停止服务。
12
12
 
13
- 这个工具特别适合需要同时访问多个 npm 源(例如公司私有仓库和公共 npm registry)的开发场景,尤其在 CI/CD 或本地开发中,能显著提高依赖安装的稳定性和效率。
13
+ 这个工具特别适合需要同时访问多个 npm 源的开发场景,尤其在 CI/CD 或本地开发中,能提高依赖安装的稳定性和效率。
14
14
 
15
15
  ---
16
16
 
@@ -24,25 +24,29 @@ yarn add --dev com.jimuwd.xian.registry-proxy --registry https://your-private-re
24
24
  ```
25
25
 
26
26
  ### 配置
27
- 1. **本地 `.yarnrc.yml`**
28
- 在业务项目根目录创建或编辑 `.yarnrc.yml`,指定需要代理的 registry 和本地代理地址。对于需要认证的 registry,建议添加 `npmAlwaysAuth: true`:
27
+ 1. **代理配置文件 `.registry-proxy.yml`**
28
+ 在业务项目根目录创建 `.registry-proxy.yml`,指定需要代理的 registry 列表:
29
29
  ```yaml
30
- npmRegistries:
30
+ registries:
31
31
  "http://localhost:4873/":
32
32
  npmAuthToken: "local-token" # 可选
33
33
  "https://registry.npmjs.org/":
34
- # token 可省略,从全局读取
34
+ # token 可省略,从 Yarn 配置读取
35
35
  "https://your-private-registry.example.com/":
36
36
  npmAuthToken: "private-token" # 可选
37
37
  npmAlwaysAuth: true # 强制要求认证
38
+ ```
38
39
 
40
+ 2. **本地 `.yarnrc.yml`**
41
+ 在项目根目录创建或编辑 `.yarnrc.yml`,指定 Yarn 使用本地代理地址:
42
+ ```yaml
39
43
  npmRegistryServer: "http://localhost:4873/"
40
44
  unsafeHttpWhitelist:
41
45
  - "localhost"
42
46
  ```
43
47
 
44
- 2. **全局 `~/.yarnrc.yml`(可选)**
45
- 如果本地未提供某些 registry 的 token,可以在用户主目录下的 `.yarnrc.yml` 配置回退 token:
48
+ 3. **全局 `~/.yarnrc.yml`(可选)**
49
+ 如果 `.registry-proxy.yml` 未提供某些 registry 的 token,可以在用户主目录下的 `.yarnrc.yml` 配置回退 token:
46
50
  ```yaml
47
51
  npmRegistries:
48
52
  "https://registry.npmjs.org/":
@@ -59,7 +63,7 @@ yarn add --dev com.jimuwd.xian.registry-proxy --registry https://your-private-re
59
63
  #!/bin/bash
60
64
 
61
65
  # 启动代理服务器并记录 PID
62
- yarn run registry-proxy .yarnrc.yml ~/.yarnrc.yml &
66
+ yarn run registry-proxy .registry-proxy.yml .yarnrc.yml ~/.yarnrc.yml &
63
67
  PROXY_PID=$!
64
68
 
65
69
  # 等待代理服务器启动,最多 10 秒
@@ -94,7 +98,7 @@ yarn add --dev com.jimuwd.xian.registry-proxy --registry https://your-private-re
94
98
  ```json
95
99
  {
96
100
  "scripts": {
97
- "start-proxy": "registry-proxy .yarnrc.yml ~/.yarnrc.yml",
101
+ "start-proxy": "registry-proxy .registry-proxy.yml .yarnrc.yml ~/.yarnrc.yml",
98
102
  "install": "bash start-proxy.sh"
99
103
  }
100
104
  }
@@ -105,7 +109,7 @@ yarn add --dev com.jimuwd.xian.registry-proxy --registry https://your-private-re
105
109
  ```bash
106
110
  yarn install
107
111
  ```
108
- - 代理会在安装完成后自动停止。
112
+ - 代理会在安装完成后自动停止。
109
113
 
110
114
  ### 输出示例
111
115
  运行后,你会看到类似以下输出:
@@ -134,60 +138,71 @@ com.jimuwd.xian.registry-proxy/
134
138
 
135
139
  ### 功能实现
136
140
  1. **配置加载(`loadRegistries`)**:
137
- - **本地配置文件**:从指定路径(默认 `./.yarnrc.yml`)读取 `npmRegistries`,提取 `registryUrl` 和 `npmAuthToken`。
138
- - **全局配置文件**:如果本地 token 缺失,从指定路径(默认 `~/.yarnrc.yml`)读取对应 `registryUrl` 的 token。
139
- - **安全设计**:本地 token 缺失时回退到全局 token 的设计,是为了避免将敏感的 `npmAuthToken` 写入项目配置文件并提交到代码仓库,从而降低安全隐患。全局配置文件(如 `~/.yarnrc.yml`)通常存储在用户主目录,不会被版本控制系统追踪。
140
- - **优先级**:本地 token > 全局 token > 无 token。
141
- - **错误处理**:本地配置文件必须存在且包含 `npmRegistries`,否则退出。
141
+ - **代理配置文件**:从指定路径(默认 `./.registry-proxy.yml`)读取 `registries`,提取 `registryUrl` 和 `npmAuthToken`。
142
+ - **Yarn 配置文件回退**:如果 `.registry-proxy.yml` 中 token 缺失,依次从本地 `.yarnrc.yml`(默认 `./.yarnrc.yml`)和全局 `~/.yarnrc.yml`(默认 `~/.yarnrc.yml`)读取对应 `registryUrl` 的 `npmAuthToken`。
143
+ - **安全设计**:将 `registryUrl` token 配置独立于 `.registry-proxy.yml`,避免敏感信息直接写入 Yarn 配置文件并提交到代码仓库。回退到 Yarn 配置的 token(尤其是全局配置)进一步降低安全隐患。
144
+ - **优先级**:`.registry-proxy.yml` token > 本地 `.yarnrc.yml` token > 全局 `~/.yarnrc.yml` token > 无 token。
145
+ - **错误处理**:`.registry-proxy.yml` 必须存在且包含 `registries`,否则退出。
142
146
 
143
147
  2. **代理逻辑**:
144
- - **服务器**:使用 Node.js 的 `http.createServer` 创建本地 HTTP 服务,默认监听 `4873` 端口。
145
- - **请求转发**:将所有请求按配置顺序转发到目标 registry,附带对应的 `Authorization: Bearer <token>`(如果存在)。
146
- - **Fallback**:依次尝试每个 registry,直到返回成功响应(`response.ok`)或全部失败(返回 404)。
148
+ - **服务器**:使用 Node.js 的 `http.createServer` 创建本地 HTTP 服务,默认监听 `4873` 端口。
149
+ - **请求转发**:将所有请求按配置顺序转发到目标 registry,附带对应的 `Authorization: Bearer <token>`(如果存在)。
150
+ - **Fallback**:依次尝试每个 registry,直到返回成功响应(`response.ok`)或全部失败(返回 404)。
147
151
 
148
152
  3. **进程管理**:
149
- - **优雅关闭**:监听 `SIGTERM` 信号,关闭服务器并退出进程。
150
- - **脚本集成**:通过 shell 脚本记录 PID,安装完成后发送 SIGTERM 停止服务。
153
+ - **优雅关闭**:监听 `SIGTERM` 信号,关闭服务器并退出进程。
154
+ - **脚本集成**:通过 shell 脚本记录 PID,安装完成后发送 SIGTERM 停止服务。
151
155
 
152
156
  ### 技术栈
153
157
  - **语言**:TypeScript(ES Modules)。
154
158
  - **模块系统**:`"module": "nodenext"`,兼容 Node.js v20+。
155
159
  - **依赖**:
156
- - `node-fetch@^3.3.2`:发起 HTTP 请求。
157
- - `js-yaml@^4.1.0`:解析 `.yarnrc.yml` 文件。
160
+ - `node-fetch@^3.3.2`:发起 HTTP 请求。
161
+ - `js-yaml@^4.1.0`:解析 `.registry-proxy.yml` 和 `.yarnrc.yml` 文件。
158
162
  - **Node.js 版本**:推荐 v14+,测试于 v20.17.0。
159
163
 
160
164
  ### CLI 参数
161
165
  ```bash
162
- registry-proxy [localConfigPath] [globalConfigPath] [port]
166
+ registry-proxy [proxyConfigPath] [localYarnConfigPath] [globalYarnConfigPath] [port]
163
167
  ```
164
- - `localConfigPath`:本地 `.yarnrc.yml` 路径,默认 `./.yarnrc.yml`。
165
- - `globalConfigPath`:全局 `.yarnrc.yml` 路径,默认 `~/.yarnrc.yml`。
168
+ - `proxyConfigPath`:代理配置文件路径,默认 `./.registry-proxy.yml`。
169
+ - `localYarnConfigPath`:本地 Yarn 配置文件路径,默认 `./.yarnrc.yml`。
170
+ - `globalYarnConfigPath`:全局 Yarn 配置文件路径,默认 `~/.yarnrc.yml`。
166
171
  - `port`:代理服务器端口,默认 `4873`。
167
172
 
168
173
  示例:
169
174
  ```bash
170
- yarn run registry-proxy ./custom.yml ~/.custom.yml 54321
175
+ yarn run registry-proxy ./custom-registry.yml ./custom-yarn.yml ~/.custom-yarn.yml 54321
171
176
  ```
172
177
 
173
178
  ### 配置说明
179
+ - **`.registry-proxy.yml`**:
180
+ - 使用 `registries` 字段定义代理的 registry 列表,与 Yarn 的 `npmRegistries` 区分。
181
+ - 示例:
182
+ ```yaml
183
+ registries:
184
+ "https://your-private-registry.example.com/":
185
+ npmAuthToken: "private-token"
186
+ npmAlwaysAuth: true
187
+ ```
174
188
  - **`npmAlwaysAuth: true`**:
175
- - `.yarnrc.yml` 中为需要认证的 registry 添加此配置,表示对该 registry 的所有请求都必须携带 `npmAuthToken`。
176
- - **原因**:某些私有 npm 仓库(例如 Nexus 或 Artifactory)要求即使访问公共包,也需要认证。设置 `npmAlwaysAuth: true` 确保代理在转发请求时始终附带 token,避免因缺少认证导致的 401 错误。
177
- - 如果不设置此项,Yarn 可能只在下载受限包时发送 token,而对公开包跳过认证,可能导致请求失败。
189
+ - 在需要认证的 registry 下添加此配置,表示所有请求必须携带 `npmAuthToken`。
190
+ - **原因**:某些私有仓库要求即使访问公共包也需要认证,设置此项避免因缺少 token 导致的 401 错误。
191
+ - **Yarn 配置**:
192
+ - `.yarnrc.yml` 仅用于设置 `npmRegistryServer` 和回退 token,不定义代理的 registry 列表。
178
193
 
179
194
  ### 注意事项
180
195
  1. **端口冲突**:
181
- - 默认端口 `4873` 是 Verdaccio 的惯用端口,可能与其他工具冲突。
182
- - 检查端口占用:`lsof -i :4873`。
183
- - 可通过参数指定其他端口(如 `54321`)。
196
+ - 默认端口 `4873` 是 Verdaccio 的惯用端口,可能与其他工具冲突。
197
+ - 检查端口占用:`lsof -i :4873`。
198
+ - 可通过参数指定其他端口(如 `54321`)。
184
199
  2. **配置文件格式**:
185
- - 确保 `.yarnrc.yml` 遵循 Yarn 的格式,`npmRegistries` 为必填项。
200
+ - 确保 `.registry-proxy.yml` 包含 `registries` 字段,`.yarnrc.yml` 包含 `npmRegistryServer`。
186
201
  3. **日志**:
187
- - 当前通过 `console.log` 输出启动信息,可扩展为文件日志。
202
+ - 当前通过 `console.log` 输出启动信息,可扩展为文件日志。
188
203
  4. **安全性**:
189
- - 代理运行于本地,未开放外部访问,确保 `unsafeHttpWhitelist` 配置正确。
190
- - 避免将 token 写入本地 `.yarnrc.yml`,优先使用全局配置或环境变量。
204
+ - 代理运行于本地,未开放外部访问,确保 `unsafeHttpWhitelist` 配置正确。
205
+ - 优先将 token 放入 `.registry-proxy.yml` 或全局 `.yarnrc.yml`,避免提交到代码仓库。
191
206
 
192
207
  ### 开发与发布
193
208
  1. **构建**:
@@ -200,3 +215,42 @@ yarn run registry-proxy ./custom.yml ~/.custom.yml 54321
200
215
  ```
201
216
 
202
217
  ---
218
+
219
+ ### 测试流程
220
+ 1. **构建并发布**:
221
+ ```bash
222
+ cd registry-proxy
223
+ yarn install
224
+ yarn build
225
+ yarn publish --registry https://your-private-registry.example.com/
226
+ ```
227
+ 2. **业务项目配置**:
228
+ - `.registry-proxy.yml`:
229
+ ```yaml
230
+ registries:
231
+ "http://localhost:4873/":
232
+ npmAuthToken: "local-token"
233
+ "https://registry.npmjs.org/":
234
+ # 无 token,回退到 Yarn 配置
235
+ "https://your-private-registry.example.com/":
236
+ npmAuthToken: "private-token"
237
+ npmAlwaysAuth: true
238
+ ```
239
+ - `.yarnrc.yml`:
240
+ ```yaml
241
+ npmRegistryServer: "http://localhost:4873/"
242
+ unsafeHttpWhitelist:
243
+ - "localhost"
244
+ ```
245
+ - `~/.yarnrc.yml`:
246
+ ```yaml
247
+ npmRegistries:
248
+ "https://registry.npmjs.org/":
249
+ npmAuthToken: "global-npm-token"
250
+ ```
251
+ 3. **运行**:
252
+ ```bash
253
+ yarn install
254
+ ```
255
+
256
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "description": "A lightweight npm registry proxy with fallback support",
6
6
  "main": "dist/index.js",