Skip to content

Commit a17aed8

Browse files
authored
chore: custom registry (#793)
1 parent 770fc7f commit a17aed8

File tree

1 file changed

+82
-5
lines changed

1 file changed

+82
-5
lines changed

docs/sync-setup.md

+82-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# 🚀 开启包同步服务
2+
23
> 我们将以 `127.0.0.1:7001` 为例,演示如何开启包同步服务,在实际部署中请替换对应的 registry 站点,更多部署相关内容可以参考 [deploy-in-docker](./deploy-in-docker.md)
34
45
## 🔌 确保 registry 服务已开启
56

67
cnpmcore 实现了所有 registry 相关 api 这意味着我们可以直接使用 npm 客户端进行相关验证工作。
8+
79
```bash
810
npm ping --registry=http://127.0.0.1:7001
911
```
1012

1113
预期输出
14+
1215
```bash
1316
npm notice PING http://127.0.0.1:7001/
1417
npm notice PONG 19ms
@@ -22,22 +25,26 @@ npm notice PONG }
2225
2326
在默认配置中,我们内置了 `cnpmcore_admin` 作为管理员账号,你可以通过修改不同环境对应的配置文件来定义对应管理员账号。
2427
此外,我们默认开启了 webAuth,只需执行 npm login 即可在命令行进行登录相关操作
28+
2529
```shell
2630
npm login --registry=http://127.0.0.1:7001
2731
```
2832
2933
预期将在浏览器唤起登录相关页面,输入账号 `cnpmcore_admin` 及密码即可(未创建时会自动注册)
34+
3035
```bash
3136
Login at:
3237
http://localhost:7001/-/v1/login/request/session/ada5af3e-773d-4e64-b8bb-e98ffe25d1c0
3338
Press ENTER to open in the browser...
3439
```
3540
3641
查看本地对应授权信息,注意服务端将不会存储 token 值,请妥善保存,或者进行对应 revoke 操作
42+
3743
```shell
3844
cat ~/.npmrc
3945
# //127.0.0.1:7001/:_authToken=cnpm_1byTg6qJuZZm3ZnMpFl43fz6DsbhwN2rH_373PXC
4046
```
47+
4148
其中,`cnpm_1byTg6qJuZZm3ZnMpFl43fz6DsbhwN2rH_373PXC` 就是我们的管理员 token,后续我们将用这个 token 来进行同步任务初始化,我们可以通过 `whoami` 来验证 token 是否有效
4249
4350
```bash
@@ -75,7 +82,7 @@ npm registry 将通过服务实时广播相关包变更,也就是 `changesStre
7582
changesStream 内包含了包所有版本变更信息,在存量同步场景存在大量冗余,我们可以通过一个脚本来快速创建同步任务。
7683
7784
```typescript
78-
import { load } from "all-package-names";
85+
import { load } from 'all-package-names';
7986
import urllib from 'urllib';
8087
import { setTimeout } from 'timers/promises';
8188

@@ -93,7 +100,7 @@ async function main() {
93100
const total = data.packageNames.length;
94101
console.log('Total %d packages', total);
95102
const lastIndex = 0;
96-
for (const [ index, fullname ] of data.packageNames.entries()) {
103+
for (const [index, fullname] of data.packageNames.entries()) {
97104
if (index < lastIndex) continue;
98105
let success = false;
99106

@@ -104,13 +111,33 @@ async function main() {
104111
const data = result.data;
105112
if (data && data.id) {
106113
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+
);
108122
} 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+
);
110131
}
111132
success = true;
112133
} 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+
);
114141
await setTimeout(1000);
115142
}
116143
}
@@ -133,3 +160,53 @@ sourceRegistryIsCNpm: true,
133160
changesStreamRegistry: 'https://registry.npmmirror.com/_changes',
134161
changesStreamRegistryMode: ChangesStreamMode.json,
135162
```
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

Comments
 (0)