1
1
# 🚀 开启包同步服务
2
+
2
3
> 我们将以 ` 127.0.0.1:7001 ` 为例,演示如何开启包同步服务,在实际部署中请替换对应的 registry 站点,更多部署相关内容可以参考 [ deploy-in-docker] ( ./deploy-in-docker.md )
3
4
4
5
## 🔌 确保 registry 服务已开启
5
6
6
7
cnpmcore 实现了所有 registry 相关 api 这意味着我们可以直接使用 npm 客户端进行相关验证工作。
8
+
7
9
``` bash
8
10
npm ping --registry=http://127.0.0.1:7001
9
11
```
10
12
11
13
预期输出
14
+
12
15
``` bash
13
16
npm notice PING http://127.0.0.1:7001/
14
17
npm notice PONG 19ms
@@ -22,22 +25,26 @@ npm notice PONG }
22
25
23
26
在默认配置中,我们内置了 ` cnpmcore_admin` 作为管理员账号,你可以通过修改不同环境对应的配置文件来定义对应管理员账号。
24
27
此外,我们默认开启了 webAuth,只需执行 npm login 即可在命令行进行登录相关操作
28
+
25
29
` ` ` shell
26
30
npm login --registry=http://127.0.0.1:7001
27
31
` ` `
28
32
29
33
预期将在浏览器唤起登录相关页面,输入账号 ` cnpmcore_admin` 及密码即可(未创建时会自动注册)
34
+
30
35
` ` ` bash
31
36
Login at:
32
37
http://localhost:7001/-/v1/login/request/session/ada5af3e-773d-4e64-b8bb-e98ffe25d1c0
33
38
Press ENTER to open in the browser...
34
39
` ` `
35
40
36
41
查看本地对应授权信息,注意服务端将不会存储 token 值,请妥善保存,或者进行对应 revoke 操作
42
+
37
43
` ` ` shell
38
44
cat ~ /.npmrc
39
45
# //127.0.0.1:7001/:_authToken=cnpm_1byTg6qJuZZm3ZnMpFl43fz6DsbhwN2rH_373PXC
40
46
` ` `
47
+
41
48
其中,` cnpm_1byTg6qJuZZm3ZnMpFl43fz6DsbhwN2rH_373PXC` 就是我们的管理员 token,后续我们将用这个 token 来进行同步任务初始化,我们可以通过 ` whoami` 来验证 token 是否有效
42
49
43
50
` ` ` bash
@@ -75,7 +82,7 @@ npm registry 将通过服务实时广播相关包变更,也就是 `changesStre
75
82
changesStream 内包含了包所有版本变更信息,在存量同步场景存在大量冗余,我们可以通过一个脚本来快速创建同步任务。
76
83
77
84
` ` ` typescript
78
- import { load } from " all-package-names" ;
85
+ import { load } from ' all-package-names' ;
79
86
import urllib from ' urllib' ;
80
87
import { setTimeout } from ' timers/promises' ;
81
88
@@ -93,7 +100,7 @@ async function main() {
93
100
const total = data.packageNames.length;
94
101
console.log(' Total %d packages' , total);
95
102
const lastIndex = 0;
96
- for (const [ index, fullname ] of data.packageNames.entries ()) {
103
+ for (const [index, fullname] of data.packageNames.entries ()) {
97
104
if (index < lastIndex) continue ;
98
105
let success = false ;
99
106
@@ -104,13 +111,33 @@ async function main() {
104
111
const data = result.data;
105
112
if (data && data.id) {
106
113
const logUrl = ` ${url} /${data.id} /log` ;
107
- console.log(' [%s/%s] %s, status: %s, log: %s' , index, total, fullname, result.status, logUrl);
114
+ console.log(
115
+ ' [%s/%s] %s, status: %s, log: %s' ,
116
+ index,
117
+ total,
118
+ fullname,
119
+ result.status,
120
+ logUrl
121
+ );
108
122
} else {
109
- console.log(' [%s/%s] %s, status: %s, data: %j' , index, total, fullname, result.status, data);
123
+ console.log(
124
+ ' [%s/%s] %s, status: %s, data: %j' ,
125
+ index,
126
+ total,
127
+ fullname,
128
+ result.status,
129
+ data
130
+ );
110
131
}
111
132
success = true ;
112
133
} catch (err: any) {
113
- console.error(' [%s/%s] %s, error: %s' , index, total, fullname, err.message);
134
+ console.error(
135
+ ' [%s/%s] %s, error: %s' ,
136
+ index,
137
+ total,
138
+ fullname,
139
+ err.message
140
+ );
114
141
await setTimeout(1000);
115
142
}
116
143
}
@@ -133,3 +160,53 @@ sourceRegistryIsCNpm: true,
133
160
changesStreamRegistry: ' https://registry.npmmirror.com/_changes' ,
134
161
changesStreamRegistryMode: ChangesStreamMode.json,
135
162
` ` `
163
+
164
+ # # 🎯 为 scope 指定单独同步源
165
+
166
+ 在某些场景下,我们可能需要为特定的 scope 指定单独的同步源。例如,公司内部的一些私有包需要从特定的 registry 同步。以下是具体操作步骤:
167
+
168
+ # ## 1. 创建 Registry
169
+
170
+ 首先需要创建一个新的 registry,并指定其 changesStream 信息:
171
+
172
+ ` ` ` bash
173
+ # 创建新的 registry
174
+ curl -H " Authorization: Bearer cnpm_1byTg6qJuZZm3ZnMpFl43fz6DsbhwN2rH_373PXC" \
175
+ -X POST http://127.0.0.1:7001/-/registry \
176
+ -H " Content-Type: application/json" \
177
+ -d ' {
178
+ "name": "custom-registry",
179
+ "host": "https://custom.registry.com/",
180
+ "changeStream": "https://custom.registry.com/_changes",
181
+ "type": "cnpmcore"
182
+ }'
183
+ ` ` `
184
+
185
+ # ## 2. 创建 Scope 并关联 Registry
186
+
187
+ 创建完 registry 后,我们可以为特定的 scope 指定这个 registry:
188
+
189
+ ` ` ` bash
190
+ # 创建 scope 并关联到指定的 registry
191
+ curl -H " Authorization: Bearer cnpm_1byTg6qJuZZm3ZnMpFl43fz6DsbhwN2rH_373PXC" \
192
+ -X POST http://127.0.0.1:7001/-/scope \
193
+ -H " Content-Type: application/json" \
194
+ -d ' {
195
+ "name": "@custom",
196
+ "registryId": "REGISTRY_ID" # 替换为上一步创建的 registry ID
197
+ }'
198
+ ` ` `
199
+
200
+ # ## 3. 开启自动同步
201
+
202
+ 创建完 registry 和 scope 后,需要开启自动同步功能:
203
+
204
+ ` ` ` bash
205
+ # 为指定的 registry 开启同步任务
206
+ curl -H " Authorization: Bearer cnpm_1byTg6qJuZZm3ZnMpFl43fz6DsbhwN2rH_373PXC" \
207
+ -X POST http://127.0.0.1:7001/-/registry/REGISTRY_ID/sync \
208
+ -H " Content-Type: application/json" \
209
+ -d ' {
210
+ "since": "1" # 可选参数,指定从哪个序列号开始同步
211
+ }'
212
+ ` ` `
0 commit comments