dsh-devdock 0.0.0-stage → 2.0.0
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/CHANGELOG.md +166 -0
- package/CONTRACT.md +519 -0
- package/LICENSE +21 -0
- package/PUBLISHING.md +175 -0
- package/README.md +244 -2
- package/assets/devdock-256.png +0 -0
- package/assets/devdock-32.png +0 -0
- package/assets/devdock-512.png +0 -0
- package/assets/devdock-64.png +0 -0
- package/client.js +11242 -0
- package/cordis.patch.yml +7 -0
- package/icon.svg +6 -0
- package/index.js +95 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +180 -4
- package/src/host/git.js +1664 -0
- package/src/host/github.js +1922 -0
- package/src/host/index.js +3969 -0
- package/src/host/npm.js +424 -0
- package/src/host/npmcli.js +283 -0
- package/src/host/review.js +627 -0
- package/src/host/store.js +874 -0
package/src/host/npm.js
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* npm registry 客户端(DevDock 套件的 npm 模块 · Host 半边)。
|
|
3
|
+
*
|
|
4
|
+
* 只做**只读**的事:认账号、列包、看版本与 dist-tags、查废弃状态。
|
|
5
|
+
* 刻意不做发布:npm 的发布强制 2FA,非交互场景拿不到 OTP,硬做只会做出
|
|
6
|
+
* 一个"点了没反应"的按钮。发布请用 `npm publish`。
|
|
7
|
+
*
|
|
8
|
+
* 错误类型复用 `GithubError`:它是这套插件共享的错误载体(名字是历史遗留),
|
|
9
|
+
* 已有 code → HTTP 状态的映射,另起一个类只会多一套映射。
|
|
10
|
+
*/
|
|
11
|
+
import http from 'node:http';
|
|
12
|
+
import https from 'node:https';
|
|
13
|
+
import { GithubError } from './github.js';
|
|
14
|
+
|
|
15
|
+
/** 默认 registry;可用环境变量指向镜像或测试服务器。 */
|
|
16
|
+
const DEFAULT_REGISTRY = 'https://registry.npmjs.org';
|
|
17
|
+
|
|
18
|
+
const USER_AGENT = 'dsh-devdock (+https://github.com/QingJing5906/dsh-DevDock)';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 当前 registry 基地址。
|
|
22
|
+
*
|
|
23
|
+
* 每次调用时读取而不是模块加载时固化:测试要能在同一进程里指向本地假
|
|
24
|
+
* registry,固化会让它读到旧值。
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
function registryBase() {
|
|
28
|
+
const raw = process.env.DSH_PUSH_NPM_REGISTRY;
|
|
29
|
+
const value = typeof raw === 'string' && raw.trim() !== '' ? raw.trim() : DEFAULT_REGISTRY;
|
|
30
|
+
return value.replace(/\/+$/, '');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 发一个 registry 请求。
|
|
35
|
+
*
|
|
36
|
+
* @param {object} options
|
|
37
|
+
* @param {string} [options.method]
|
|
38
|
+
* @param {string} options.path 相对 registry 基地址的路径,可含查询串。
|
|
39
|
+
* @param {string} [options.token] npm granular access token。
|
|
40
|
+
* @param {number} [options.timeoutMs]
|
|
41
|
+
* @param {object} [options.body]
|
|
42
|
+
* @returns {Promise<{status: number, headers: object, json: any, text: string}>}
|
|
43
|
+
*/
|
|
44
|
+
function request(options) {
|
|
45
|
+
const base = registryBase();
|
|
46
|
+
const url = `${base}${options.path}`;
|
|
47
|
+
const timeoutMs = Number.isFinite(options.timeoutMs) ? options.timeoutMs : 15_000;
|
|
48
|
+
const headers = {
|
|
49
|
+
'user-agent': USER_AGENT,
|
|
50
|
+
accept: 'application/json',
|
|
51
|
+
};
|
|
52
|
+
if (typeof options.token === 'string' && options.token !== '') {
|
|
53
|
+
headers.authorization = `Bearer ${options.token}`;
|
|
54
|
+
}
|
|
55
|
+
let payload;
|
|
56
|
+
if (options.body !== undefined) {
|
|
57
|
+
payload = Buffer.from(JSON.stringify(options.body), 'utf8');
|
|
58
|
+
headers['content-type'] = 'application/json';
|
|
59
|
+
headers['content-length'] = String(payload.length);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return new Promise((resolvePromise, reject) => {
|
|
63
|
+
const controller = new AbortController();
|
|
64
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
65
|
+
let settled = false;
|
|
66
|
+
const done = (fn, value) => {
|
|
67
|
+
if (settled) return;
|
|
68
|
+
settled = true;
|
|
69
|
+
clearTimeout(timer);
|
|
70
|
+
fn(value);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
let req;
|
|
74
|
+
try {
|
|
75
|
+
const transport = url.startsWith('http://') ? http.request : https.request;
|
|
76
|
+
req = transport(url, { method: options.method ?? 'GET', headers, signal: controller.signal });
|
|
77
|
+
} catch (error) {
|
|
78
|
+
done(reject, new GithubError('NETWORK', `无法连接 npm registry:${error.message}`, { cause: error }));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
req.on('error', (error) => {
|
|
83
|
+
const reason = error && error.name === 'AbortError'
|
|
84
|
+
? `请求超时(${timeoutMs} ms)`
|
|
85
|
+
: (error && error.message) || '未知错误';
|
|
86
|
+
done(reject, new GithubError('NETWORK', `无法连接 npm registry:${reason}`, { cause: error }));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
req.on('response', (response) => {
|
|
90
|
+
const chunks = [];
|
|
91
|
+
response.on('data', (chunk) => chunks.push(chunk));
|
|
92
|
+
response.on('end', () => {
|
|
93
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
94
|
+
let json = null;
|
|
95
|
+
try {
|
|
96
|
+
json = text === '' ? null : JSON.parse(text);
|
|
97
|
+
} catch {
|
|
98
|
+
json = null;
|
|
99
|
+
}
|
|
100
|
+
done(resolvePromise, {
|
|
101
|
+
status: response.statusCode ?? 0,
|
|
102
|
+
headers: response.headers,
|
|
103
|
+
json,
|
|
104
|
+
text,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (payload) req.write(payload);
|
|
110
|
+
req.end();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 把 registry 的响应翻译成插件统一的错误。
|
|
116
|
+
*
|
|
117
|
+
* @param {{status: number, json: any, text: string}} response
|
|
118
|
+
* @param {string} action 中文动作名,用于组织提示。
|
|
119
|
+
* @returns {GithubError}
|
|
120
|
+
*/
|
|
121
|
+
function registryError(response, action) {
|
|
122
|
+
const status = response.status;
|
|
123
|
+
const detail = response.json && typeof response.json === 'object'
|
|
124
|
+
? String(response.json.error ?? response.json.reason ?? '')
|
|
125
|
+
: '';
|
|
126
|
+
const suffix = detail === '' ? '' : `:${detail}`;
|
|
127
|
+
if (status === 401 || status === 403) {
|
|
128
|
+
return new GithubError(
|
|
129
|
+
'UNAUTHORIZED',
|
|
130
|
+
`${action}被 npm 拒绝(${status})${suffix}。请确认令牌有效,且类型为 Granular Access Token(只读即可)。`,
|
|
131
|
+
{ status },
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
if (status === 404) {
|
|
135
|
+
return new GithubError('NOT_FOUND', `${action}:npm 上找不到对应资源(404)${suffix}`, { status });
|
|
136
|
+
}
|
|
137
|
+
if (status === 429) {
|
|
138
|
+
return new GithubError('RATE_LIMITED', `${action}过于频繁(429),请稍后再试${suffix}`, { status });
|
|
139
|
+
}
|
|
140
|
+
return new GithubError(
|
|
141
|
+
'INTERNAL',
|
|
142
|
+
`${action}失败(${status || '无响应'})${suffix || `:${String(response.text || '').slice(0, 200)}`}`,
|
|
143
|
+
{ status },
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 用一个令牌确认身份。
|
|
149
|
+
*
|
|
150
|
+
* `/-/whoami` 是唯一一个"只验令牌、不要包名"的端点;它对 granular 令牌同样
|
|
151
|
+
* 有效,且不会像 `/-/user/org.couchdb.user:<name>` 那样在账号名上出歧义。
|
|
152
|
+
*
|
|
153
|
+
* @param {{token: string, timeoutMs?: number}} options
|
|
154
|
+
* @returns {Promise<{login: string}>}
|
|
155
|
+
*/
|
|
156
|
+
export async function npmWhoami(options) {
|
|
157
|
+
const token = String(options.token ?? '');
|
|
158
|
+
if (token === '') throw new GithubError('BAD_REQUEST', '缺少 npm 令牌');
|
|
159
|
+
const response = await request({ path: '/-/whoami', token, timeoutMs: options.timeoutMs });
|
|
160
|
+
if (response.status !== 200) throw registryError(response, '校验 npm 令牌');
|
|
161
|
+
const login = response.json && typeof response.json.username === 'string'
|
|
162
|
+
? response.json.username
|
|
163
|
+
: '';
|
|
164
|
+
if (login === '') throw new GithubError('UNAUTHORIZED', 'npm 没有返回用户名,令牌可能无效');
|
|
165
|
+
return { login };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 按关键词搜索 npm 上的包。
|
|
170
|
+
*
|
|
171
|
+
* 与 `npmSearchByMaintainer` 的区别:这个面向「找包」,用户输入什么就搜
|
|
172
|
+
* 什么;那个面向「我的包」,限定维护者。两者共用同一个搜索端点,但
|
|
173
|
+
* 用途不同,界面上也分开放。
|
|
174
|
+
*
|
|
175
|
+
* @param {{text: string, token?: string, size?: number, from?: number, timeoutMs?: number}} options
|
|
176
|
+
* @returns {Promise<{items: object[], total: number}>}
|
|
177
|
+
*/
|
|
178
|
+
export async function npmSearchText(options) {
|
|
179
|
+
const text = String(options.text ?? '').trim();
|
|
180
|
+
if (text === '') throw new GithubError('BAD_REQUEST', '搜索关键词不能为空');
|
|
181
|
+
const size = Number.isFinite(options.size) ? Math.min(100, Math.max(1, Math.trunc(options.size))) : 20;
|
|
182
|
+
const from = Number.isFinite(options.from) ? Math.max(0, Math.trunc(options.from)) : 0;
|
|
183
|
+
const query = new URLSearchParams({ text, size: String(size), from: String(from) });
|
|
184
|
+
const response = await request({
|
|
185
|
+
path: `/-/v1/search?${query.toString()}`
|
|
186
|
+
, token: options.token,
|
|
187
|
+
timeoutMs: options.timeoutMs,
|
|
188
|
+
});
|
|
189
|
+
if (response.status !== 200) throw registryError(response, '搜索 npm 包');
|
|
190
|
+
const objects = response.json && Array.isArray(response.json.objects) ? response.json.objects : [];
|
|
191
|
+
const total = response.json && Number.isFinite(response.json.total) ? response.json.total : objects.length;
|
|
192
|
+
return { items: objects.map((entry) => mapSearchItem(entry, undefined)), total };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 搜索某用户维护的包。/**
|
|
197
|
+
* 搜索某用户维护的包。
|
|
198
|
+
*
|
|
199
|
+
* registry 的搜索接口支持 `maintainer:` 限定符,这是唯一不依赖"先知道包名"
|
|
200
|
+
* 的枚举方式;结果会再按维护者过滤一遍,因为限定符对 scope 包有时不精确。
|
|
201
|
+
*
|
|
202
|
+
* @param {{token?: string, login: string, size?: number, from?: number, timeoutMs?: number}} options
|
|
203
|
+
* @returns {Promise<{items: object[], total: number}>}
|
|
204
|
+
*/
|
|
205
|
+
export async function npmSearchByMaintainer(options) {
|
|
206
|
+
const login = String(options.login ?? '').trim();
|
|
207
|
+
if (login === '') throw new GithubError('BAD_REQUEST', '缺少 npm 用户名');
|
|
208
|
+
const size = Number.isFinite(options.size) ? Math.min(250, Math.max(1, Math.trunc(options.size))) : 20;
|
|
209
|
+
const from = Number.isFinite(options.from) ? Math.max(0, Math.trunc(options.from)) : 0;
|
|
210
|
+
const query = new URLSearchParams({
|
|
211
|
+
text: `maintainer:${login}`,
|
|
212
|
+
size: String(size),
|
|
213
|
+
from: String(from),
|
|
214
|
+
});
|
|
215
|
+
const response = await request({
|
|
216
|
+
path: `/-/v1/search?${query.toString()}`,
|
|
217
|
+
token: options.token,
|
|
218
|
+
timeoutMs: options.timeoutMs,
|
|
219
|
+
});
|
|
220
|
+
if (response.status !== 200) throw registryError(response, '搜索 npm 包');
|
|
221
|
+
const objects = response.json && Array.isArray(response.json.objects) ? response.json.objects : [];
|
|
222
|
+
const total = response.json && Number.isFinite(response.json.total) ? response.json.total : objects.length;
|
|
223
|
+
return {
|
|
224
|
+
items: objects.map((entry) => mapSearchItem(entry, login)),
|
|
225
|
+
total,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 搜索结果的精简投影。
|
|
231
|
+
*
|
|
232
|
+
* 只保留界面要显示的字段:原始条目有三层嵌套,直接透传会让客户端契约
|
|
233
|
+
* 随着 registry 的字段增删而漂移。
|
|
234
|
+
*
|
|
235
|
+
* @param {object} entry
|
|
236
|
+
* @param {string} [login] 用于判断是否本人维护。
|
|
237
|
+
* @returns {object}
|
|
238
|
+
*/
|
|
239
|
+
export function mapSearchItem(entry, login) {
|
|
240
|
+
const pkg = (entry && entry.package) || {};
|
|
241
|
+
const score = (entry && entry.score) || {};
|
|
242
|
+
const maintainers = Array.isArray(pkg.maintainers)
|
|
243
|
+
? pkg.maintainers.map((m) => (typeof m === 'string' ? m : m && m.username)).filter(Boolean)
|
|
244
|
+
: [];
|
|
245
|
+
return {
|
|
246
|
+
name: typeof pkg.name === 'string' ? pkg.name : '',
|
|
247
|
+
version: typeof pkg.version === 'string' ? pkg.version : '',
|
|
248
|
+
description: typeof pkg.description === 'string' ? pkg.description : '',
|
|
249
|
+
date: typeof pkg.date === 'string' ? pkg.date : '',
|
|
250
|
+
links: pkg.links && typeof pkg.links === 'object' ? pkg.links : {},
|
|
251
|
+
maintainers,
|
|
252
|
+
owned: typeof login === 'string' && login !== ''
|
|
253
|
+
? maintainers.some((name) => name === login)
|
|
254
|
+
: null,
|
|
255
|
+
score: Number.isFinite(score.final) ? Math.round(score.final * 1000) / 1000 : null,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 取一个包的 packument(全部版本信息)。
|
|
261
|
+
*
|
|
262
|
+
* registry 对单个包返回整份文档,可能很大(几十上百个版本)。这里把它投影成
|
|
263
|
+
* 界面需要的最小结构,并按发布时间倒序,避免客户端自己排序时出错。
|
|
264
|
+
*
|
|
265
|
+
* @param {{name: string, token?: string, timeoutMs?: number}} options
|
|
266
|
+
* @returns {Promise<object>}
|
|
267
|
+
*/
|
|
268
|
+
export async function npmPackument(options) {
|
|
269
|
+
const name = String(options.name ?? '').trim();
|
|
270
|
+
if (name === '') throw new GithubError('BAD_REQUEST', '缺少包名');
|
|
271
|
+
const response = await request({
|
|
272
|
+
path: `/${encodeURIComponent(name).replace('%40', '@').replace('%2F', '/')}`,
|
|
273
|
+
token: options.token,
|
|
274
|
+
timeoutMs: options.timeoutMs,
|
|
275
|
+
});
|
|
276
|
+
if (response.status !== 200) throw registryError(response, `读取包 ${name}`);
|
|
277
|
+
return { pkg: mapPackument(response.json, name) };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* packument → 精简结构。
|
|
282
|
+
*
|
|
283
|
+
* @param {object} raw
|
|
284
|
+
* @param {string} [fallbackName]
|
|
285
|
+
* @returns {object}
|
|
286
|
+
*/
|
|
287
|
+
export function mapPackument(raw, fallbackName = '') {
|
|
288
|
+
const doc = raw && typeof raw === 'object' ? raw : {};
|
|
289
|
+
const versions = doc.versions && typeof doc.versions === 'object' ? doc.versions : {};
|
|
290
|
+
const times = doc.time && typeof doc.time === 'object' ? doc.time : {};
|
|
291
|
+
const distTags = doc['dist-tags'] && typeof doc['dist-tags'] === 'object' ? doc['dist-tags'] : {};
|
|
292
|
+
const latestVersion = typeof distTags.latest === 'string' ? distTags.latest : '';
|
|
293
|
+
|
|
294
|
+
const rows = Object.keys(versions).map((version) => {
|
|
295
|
+
const entry = versions[version] || {};
|
|
296
|
+
const deprecated = typeof entry.deprecated === 'string' ? entry.deprecated : '';
|
|
297
|
+
return {
|
|
298
|
+
version,
|
|
299
|
+
publishedAt: typeof times[version] === 'string' ? times[version] : '',
|
|
300
|
+
deprecated,
|
|
301
|
+
// 一个版本是否被 superceded 由 dist-tags 决定,客户端不该自己算
|
|
302
|
+
isLatest: version === latestVersion,
|
|
303
|
+
license: typeof entry.license === 'string' ? entry.license : '',
|
|
304
|
+
};
|
|
305
|
+
}).sort((a, b) => {
|
|
306
|
+
// 发布时间倒序;没有时间戳的排最后
|
|
307
|
+
const ta = a.publishedAt === '' ? 0 : Date.parse(a.publishedAt);
|
|
308
|
+
const tb = b.publishedAt === '' ? 0 : Date.parse(b.publishedAt);
|
|
309
|
+
return (Number.isFinite(tb) ? tb : 0) - (Number.isFinite(ta) ? ta : 0);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
const latestEntry = latestVersion !== '' ? (versions[latestVersion] || {}) : {};
|
|
313
|
+
// 发布目标的体积与文件数在 dist 上,是"这个包有多大"唯一可靠的来源。
|
|
314
|
+
const latestDist = (latestEntry.dist && typeof latestEntry.dist === 'object') ? latestEntry.dist : {};
|
|
315
|
+
const repository = latestEntry.repository;
|
|
316
|
+
const repositoryUrl = typeof repository === 'string'
|
|
317
|
+
? repository
|
|
318
|
+
: (repository && typeof repository.url === 'string' ? repository.url : '');
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
name: typeof doc.name === 'string' ? doc.name : fallbackName,
|
|
322
|
+
description: typeof doc.description === 'string' ? doc.description : '',
|
|
323
|
+
distTags: Object.fromEntries(
|
|
324
|
+
Object.entries(distTags).filter(([, value]) => typeof value === 'string'),
|
|
325
|
+
),
|
|
326
|
+
latestVersion,
|
|
327
|
+
latestPublishedAt: latestVersion !== '' && typeof times[latestVersion] === 'string'
|
|
328
|
+
? times[latestVersion]
|
|
329
|
+
: '',
|
|
330
|
+
created: typeof times.created === 'string' ? times.created : '',
|
|
331
|
+
modified: typeof times.modified === 'string' ? times.modified : '',
|
|
332
|
+
license: typeof latestEntry.license === 'string' ? latestEntry.license : '',
|
|
333
|
+
homepage: typeof latestEntry.homepage === 'string' ? latestEntry.homepage : '',
|
|
334
|
+
repository: repositoryUrl,
|
|
335
|
+
keywords: Array.isArray(latestEntry.keywords)
|
|
336
|
+
? latestEntry.keywords.filter((k) => typeof k === 'string').slice(0, 12)
|
|
337
|
+
: [],
|
|
338
|
+
maintainers: Array.isArray(doc.maintainers)
|
|
339
|
+
? doc.maintainers
|
|
340
|
+
.map((m) => (typeof m === 'string' ? m : m && m.username))
|
|
341
|
+
.filter((m) => typeof m === 'string')
|
|
342
|
+
: [],
|
|
343
|
+
versions: rows,
|
|
344
|
+
versionCount: rows.length,
|
|
345
|
+
deprecatedVersions: rows.filter((row) => row.deprecated !== '').length,
|
|
346
|
+
// 下面这些让包详情在本地就能看全,不必跳去 npmjs.com。
|
|
347
|
+
dependencies: latestEntry.dependencies && typeof latestEntry.dependencies === 'object'
|
|
348
|
+
? latestEntry.dependencies : {},
|
|
349
|
+
peerDependencies: latestEntry.peerDependencies && typeof latestEntry.peerDependencies === 'object'
|
|
350
|
+
? latestEntry.peerDependencies : {},
|
|
351
|
+
engines: latestEntry.engines && typeof latestEntry.engines === 'object'
|
|
352
|
+
? latestEntry.engines : {},
|
|
353
|
+
unpackedSize: Number.isFinite(latestDist.unpackedSize) ? latestDist.unpackedSize : null,
|
|
354
|
+
fileCount: Number.isFinite(latestDist.fileCount) ? latestDist.fileCount : null,
|
|
355
|
+
tarball: typeof latestDist.tarball === 'string' ? latestDist.tarball : '',
|
|
356
|
+
integrity: typeof latestDist.integrity === 'string' ? latestDist.integrity : '',
|
|
357
|
+
htmlUrl: `https://www.npmjs.com/package/${encodeURIComponent(
|
|
358
|
+
typeof doc.name === 'string' ? doc.name : fallbackName,
|
|
359
|
+
)}`,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* 只取一个包的最新版本号。
|
|
365
|
+
*
|
|
366
|
+
* `/-/package/<name>/dist-tags` 返回的是一个极小的对象,比整份 packument
|
|
367
|
+
* 小两个数量级——检查一个项目的全部依赖时,这个差别决定了能不能秒回。
|
|
368
|
+
*
|
|
369
|
+
* @param {{name: string, token?: string, timeoutMs?: number}} options
|
|
370
|
+
* @returns {Promise<{name: string, latest: string|null, error?: string}>}
|
|
371
|
+
*/
|
|
372
|
+
export async function npmLatestVersion(options) {
|
|
373
|
+
const name = String(options.name ?? '').trim();
|
|
374
|
+
if (name === '') return { name, latest: null, error: 'BAD_NAME' };
|
|
375
|
+
try {
|
|
376
|
+
const path = `/-/package/${encodeURIComponent(name).replace('%40', '@').replace('%2F', '/')}/dist-tags`;
|
|
377
|
+
const response = await request({ path, token: options.token, timeoutMs: options.timeoutMs ?? 12_000 });
|
|
378
|
+
if (response.status !== 200) {
|
|
379
|
+
return { name, latest: null, error: response.status === 404 ? 'NOT_FOUND' : 'HTTP_' + response.status };
|
|
380
|
+
}
|
|
381
|
+
const latest = response.json && typeof response.json.latest === 'string' ? response.json.latest : null;
|
|
382
|
+
return { name, latest };
|
|
383
|
+
} catch (error) {
|
|
384
|
+
return { name, latest: null, error: error && error.code ? error.code : 'NETWORK' };
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* 查一个包名在 registry 上的占用情况。
|
|
390
|
+
*
|
|
391
|
+
* 发布前自检需要知道两件不同的事:**名字**有没有人用,以及**这个具体
|
|
392
|
+
* 版本**是不是已经发过。名字被占用不等于不能发(可能就是自己的包),
|
|
393
|
+
* 但版本重复一定发不上去。
|
|
394
|
+
*
|
|
395
|
+
* @param {{name: string, token?: string, timeoutMs?: number}} options
|
|
396
|
+
* @returns {Promise<{exists: boolean, versions: string[], latest: string|null, error?: string}>}
|
|
397
|
+
*/
|
|
398
|
+
export async function npmPackageStatus(options) {
|
|
399
|
+
const name = String(options.name ?? '').trim();
|
|
400
|
+
if (name === '') return { exists: false, versions: [], latest: null, error: 'BAD_NAME' };
|
|
401
|
+
try {
|
|
402
|
+
const path = `/${encodeURIComponent(name).replace('%40', '@').replace('%2F', '/')}`;
|
|
403
|
+
const response = await request({ path, token: options.token, timeoutMs: options.timeoutMs ?? 15_000 });
|
|
404
|
+
if (response.status === 404) return { exists: false, versions: [], latest: null };
|
|
405
|
+
if (response.status !== 200) {
|
|
406
|
+
return { exists: false, versions: [], latest: null, error: 'HTTP_' + response.status };
|
|
407
|
+
}
|
|
408
|
+
const doc = response.json || {};
|
|
409
|
+
const versions = doc.versions && typeof doc.versions === 'object'
|
|
410
|
+
? Object.keys(doc.versions)
|
|
411
|
+
: [];
|
|
412
|
+
const latest = doc['dist-tags'] && typeof doc['dist-tags'].latest === 'string'
|
|
413
|
+
? doc['dist-tags'].latest
|
|
414
|
+
: null;
|
|
415
|
+
return { exists: true, versions, latest };
|
|
416
|
+
} catch (error) {
|
|
417
|
+
return { exists: false, versions: [], latest: null, error: error && error.code ? error.code : 'NETWORK' };
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** 包在 npmjs.com 上的页面地址。 *//** 包在 npmjs.com 上的页面地址。 *//** 包在 npmjs.com 上的页面地址。 */
|
|
422
|
+
export function packageHtmlUrl(name) {
|
|
423
|
+
return `https://www.npmjs.com/package/${encodeURIComponent(String(name ?? ''))}`;
|
|
424
|
+
}
|