dsh-git-push 2.15.0 → 2.15.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.
- package/lib/app/command-registry.js +1 -0
- package/lib/app/tool-call.js +2 -0
- package/lib/npm/publish.js +19 -0
- package/lib/npm/smoke.js +162 -0
- package/lib/self/index.js +1 -1
- package/package.json +1 -1
- package/scripts/local-registry.mjs +248 -0
|
@@ -131,6 +131,7 @@ export const TOOL_REGISTRY = [
|
|
|
131
131
|
{ flag: 'no-dry-run', type: 'boolean', desc: '关闭 dry-run(= dryRun:false;真发还需 --confirm)' },
|
|
132
132
|
{ flag: 'waitVisible', type: 'boolean', desc: '发布后等待 registry 可见(默认 false:上传成功即为成功,可见延迟是已知现象;true 时每分钟查一次、最多 5 分钟)' },
|
|
133
133
|
{ flag: 'json', type: 'boolean', desc: 'JSON 输出(CLI 用;与其它工具同口径)' },
|
|
134
|
+
{ flag: 'skipSmoke', type: 'boolean', desc: '跳过发布前冒烟(默认跑:pack→干净目录安装→import 公开面→解析依赖;关掉会失去“装得上”这层门禁)' },
|
|
134
135
|
],
|
|
135
136
|
},
|
|
136
137
|
{
|
package/lib/app/tool-call.js
CHANGED
|
@@ -292,6 +292,8 @@ async function callNpmPublishJob(args = {}, jobs, exec, log, env = {}, cfg = {})
|
|
|
292
292
|
dryRun,
|
|
293
293
|
tag: args.tag || 'latest',
|
|
294
294
|
otp: args.otp || '',
|
|
295
|
+
// 发布前冒烟默认开;慢/离线环境可关(关掉就等于放弃「装得上」这一层门禁)
|
|
296
|
+
skipSmoke: args.skipSmoke === true || args.noSmoke === true,
|
|
295
297
|
confirm: args.confirm === true,
|
|
296
298
|
// 可选:发布后等待 registry 可见(默认不等——上传成功即为成功,可见延迟是已知现象)
|
|
297
299
|
waitVisible: args.waitVisible === true,
|
package/lib/npm/publish.js
CHANGED
|
@@ -13,6 +13,7 @@ import { join } from 'node:path';
|
|
|
13
13
|
import { credentialsDir, readNpmToken } from '../core/credentials.js';
|
|
14
14
|
import { DEFAULT_REGISTRY, NPM_TIMEOUT_MS, normalizeRegistry, runNpm } from './registry.js';
|
|
15
15
|
import { npmArgs, writeNpmrc } from './npmrc.js';
|
|
16
|
+
import { smokeTestPackage } from './smoke.js';
|
|
16
17
|
|
|
17
18
|
export const PUBLISH_LOG_FILE = 'npm-publish.log';
|
|
18
19
|
export const PUBLISH_TIMEOUT_MS = 300_000;
|
|
@@ -117,6 +118,9 @@ function appendPublishLog(line, { workspaceRoot = '' } = {}) {
|
|
|
117
118
|
export async function publishPackage({
|
|
118
119
|
dir = '', registry = DEFAULT_REGISTRY, workspaceRoot = '',
|
|
119
120
|
dryRun = true, tag = 'latest', otp = '', confirm = false, timeoutMs = PUBLISH_TIMEOUT_MS,
|
|
121
|
+
// 发布前冒烟(默认开):pack → 干净目录装 → import 公开面 → 解析依赖。
|
|
122
|
+
// 干跑只给「会打进哪些文件」,不证明「装上能不能用」——这类问题只能在真装一次时暴露。
|
|
123
|
+
skipSmoke = false,
|
|
120
124
|
// 可选:发布成功后等待 registry 可见。默认 false —— 上传成功即为成功(可见延迟是已知现象)
|
|
121
125
|
waitVisible = false,
|
|
122
126
|
} = {}) {
|
|
@@ -147,6 +151,19 @@ export async function publishPackage({
|
|
|
147
151
|
if (!pack.ok) return { ok: false, blocked: 'pack-failed', name: info.name, version: info.version, error: pack.error, packStdout: String(pack.stdout || '').slice(-2000) };
|
|
148
152
|
if (!pack.files.length) return { ok: false, blocked: 'empty-pack', name: info.name, version: info.version, error: 'npm pack 清单为空(检查 files 字段)' };
|
|
149
153
|
|
|
154
|
+
// 体检④:**发布前冒烟**——真装一次并加载公开面。失败直接拦,不写 registry。
|
|
155
|
+
let smoke = null;
|
|
156
|
+
if (!skipSmoke) {
|
|
157
|
+
smoke = await smokeTestPackage({ dir: info.dir, workspaceRoot, registry: reg, timeoutMs });
|
|
158
|
+
if (!smoke.ok) {
|
|
159
|
+
return {
|
|
160
|
+
ok: false, blocked: 'smoke-failed', name: info.name, version: info.version,
|
|
161
|
+
error: `发布前冒烟不通过:${(smoke.failures || []).slice(0, 3).join(';')}`,
|
|
162
|
+
smoke,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
150
167
|
const rc = writeNpmrc({ workspaceRoot, registry: reg, token });
|
|
151
168
|
if (!rc.ok) return { ok: false, error: rc.error || 'npmrc 写入失败' };
|
|
152
169
|
const args = ['publish', ...npmArgs({ npmrcFile: rc.file, registry: reg }), '--tag', tg];
|
|
@@ -182,6 +199,8 @@ export async function publishPackage({
|
|
|
182
199
|
packList: pack.files,
|
|
183
200
|
packSize: pack.size,
|
|
184
201
|
tarball: pack.filename,
|
|
202
|
+
// 发布前冒烟结果(装了没、公开面能不能 import、依赖闭包全不全)
|
|
203
|
+
smoke: smoke ? { ok: smoke.ok, tgz: smoke.tgz, bytes: smoke.bytes, entries: smoke.entries, deps: smoke.deps, failures: smoke.failures } : null,
|
|
185
204
|
published: !dryRun && !!r.ok, // 成功判定:npm 上传成功即为成功(不要求 registry 已可见)
|
|
186
205
|
// 附加信息(不影响 published):registry 是否已能看到新版本;发布延迟(几分钟)是已知现象
|
|
187
206
|
visible,
|
package/lib/npm/smoke.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 发布前冒烟:证明「这个包**装得上、加载得了、跑得起来**」——而不是只看 `npm publish --dry-run`
|
|
3
|
+
* 的文件清单。
|
|
4
|
+
*
|
|
5
|
+
* 为什么需要(真实教训):干跑只告诉你「哪些文件会进包」,不告诉你「装上后能不能 import」。
|
|
6
|
+
* 我们踩过的两类坑都在干跑里看不见:
|
|
7
|
+
* · 依赖漏声明 / 子路径依赖缺失(`js-yaml`、`tree-sitter-wasms/out/*.wasm`)——装完才 ERR_MODULE_NOT_FOUND;
|
|
8
|
+
* · `exports` 收窄后内部子路径 `import('pkg/lib/x.js')` 直接 ERR_PACKAGE_PATH_NOT_EXPORTED,
|
|
9
|
+
* 而公开面(`.` / `./audit` / `./client`)其实需要逐个 have-to-import 才算验过。
|
|
10
|
+
*
|
|
11
|
+
* 做法(全部在临时目录里,不污染仓库):
|
|
12
|
+
* ① `npm pack` 出真实 tarball;② 建一个干净 consumer 工程并 `npm i <tgz>`(走真实依赖解析);
|
|
13
|
+
* ③ 按 package.json 的 `exports` 公开面逐个 import;④ 逐个解析声明的 dependencies;
|
|
14
|
+
* ⑤ 清理临时目录,回执里给出每步结果与失败原因。
|
|
15
|
+
*
|
|
16
|
+
* 不做的事:不跑单测、不启宿主。它只回答「包本身能不能被正常消费」;
|
|
17
|
+
* 「宿主能不能加载插件」属于隔离实例验证(test_env)的范畴,成本高得多,不做进默认门禁。
|
|
18
|
+
*/
|
|
19
|
+
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
20
|
+
import { join } from 'node:path';
|
|
21
|
+
import { tmpdir } from 'node:os';
|
|
22
|
+
|
|
23
|
+
import { runNpm } from './registry.js';
|
|
24
|
+
import { npmArgs } from './npmrc.js';
|
|
25
|
+
|
|
26
|
+
const SMOKE_TIMEOUT_MS = 240000;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 由 package.json 推导「必须能 import 的公开面」。
|
|
30
|
+
* exports 存对象时取所有 key(跳过 './package.json'——它不是模块,只能被读取);
|
|
31
|
+
* 没有 exports 时退化为包名根入口。
|
|
32
|
+
*/
|
|
33
|
+
/** 浏览器端入口(如 `./client`):只能在宿主前端环境加载,Node 里 import 必然失败,冒烟时跳过。 */
|
|
34
|
+
export function isBrowserEntry(key) {
|
|
35
|
+
// `./client`、`./client/index`、`./lib/client.js` 都算浏览器端入口
|
|
36
|
+
return /(^|\/)client(\/|\.js)?(\/|$)|browser/i.test(String(key).replace(/^\.\//, ''));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function entrySpecsOf(pkg) {
|
|
40
|
+
const name = String(pkg?.name || '');
|
|
41
|
+
if (!name) return [];
|
|
42
|
+
const exp = pkg.exports;
|
|
43
|
+
if (typeof exp === 'string') return [name];
|
|
44
|
+
if (exp && typeof exp === 'object') {
|
|
45
|
+
const subs = Object.keys(exp)
|
|
46
|
+
.filter((k) => k === '.' || k.startsWith('./'))
|
|
47
|
+
.filter((k) => k !== './package.json')
|
|
48
|
+
.map((k) => (k === '.' ? name : `${name}/${k.slice(2)}`));
|
|
49
|
+
return subs.length ? subs : [name];
|
|
50
|
+
}
|
|
51
|
+
return [name];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 冒烟主流程。 */
|
|
55
|
+
export async function smokeTestPackage({ dir = '', workspaceRoot = '', registry = '', timeoutMs = SMOKE_TIMEOUT_MS } = {}) {
|
|
56
|
+
const steps = [];
|
|
57
|
+
const failures = [];
|
|
58
|
+
let tmp = '';
|
|
59
|
+
try {
|
|
60
|
+
const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
61
|
+
const name = String(pkg.name || '');
|
|
62
|
+
const version = String(pkg.version || '');
|
|
63
|
+
if (!name) return { ok: false, steps, failures: ['package.json 缺 name'] };
|
|
64
|
+
|
|
65
|
+
tmp = mkdtempSync(join(tmpdir(), 'dshgp-smoke-'));
|
|
66
|
+
// ① 打真实 tarball
|
|
67
|
+
const packDest = join(tmp, 'pack');
|
|
68
|
+
const consumer = join(tmp, 'consumer');
|
|
69
|
+
mkdirSync(packDest, { recursive: true });
|
|
70
|
+
mkdirSync(consumer, { recursive: true });
|
|
71
|
+
const packArgs = ['pack', '--pack-destination', packDest, ...npmArgs({ registry })];
|
|
72
|
+
const packed = runNpm(packArgs, { cwd: dir, timeoutMs, env: { NO_UPDATE_NOTIFIER: '1' } });
|
|
73
|
+
if (!packed.ok) {
|
|
74
|
+
failures.push(`npm pack 失败:${String(packed.error || packed.stderr || '').slice(0, 300)}`);
|
|
75
|
+
return { ok: false, steps, failures };
|
|
76
|
+
}
|
|
77
|
+
const tgzName = readdirSync(packDest).find((f) => f.endsWith('.tgz'));
|
|
78
|
+
if (!tgzName) { failures.push('npm pack 没产出 tarball'); return { ok: false, steps, failures }; }
|
|
79
|
+
const tgz = join(packDest, tgzName);
|
|
80
|
+
const bytes = statSync(tgz).size;
|
|
81
|
+
steps.push({ step: 'pack', ok: true, tgz: tgzName, bytes });
|
|
82
|
+
|
|
83
|
+
// ② 干净 consumer 里真实安装
|
|
84
|
+
writeFileSync(join(consumer, 'package.json'), JSON.stringify({ name: 'smoke-consumer', private: true, version: '0.0.0' }, null, 2), 'utf8');
|
|
85
|
+
const installed = runNpm(['install', '--no-audit', '--no-fund', tgz], { cwd: consumer, timeoutMs, env: { NO_UPDATE_NOTIFIER: '1' } });
|
|
86
|
+
if (!installed.ok) {
|
|
87
|
+
failures.push(`npm install <tgz> 失败:${String(installed.error || installed.stderr || '').slice(0, 400)}`);
|
|
88
|
+
return { ok: false, steps, failures, tgz: tgzName, bytes };
|
|
89
|
+
}
|
|
90
|
+
steps.push({ step: 'install', ok: true });
|
|
91
|
+
|
|
92
|
+
// ③ 公开面逐个 import + ④ 依赖逐个解析(同一进程里做,省一次启动)
|
|
93
|
+
const probe = join(consumer, 'probe.mjs');
|
|
94
|
+
const deps = Object.keys(pkg.dependencies || {});
|
|
95
|
+
// nodeEntries = 可在 Node 里加载的公开面;browserEntries = 仅前端可加载(跳过但如实列出)
|
|
96
|
+
const nodeEntries = [];
|
|
97
|
+
const browserEntries = [];
|
|
98
|
+
for (const key of Object.keys(pkg.exports && typeof pkg.exports === 'object' ? pkg.exports : { '.': null })) {
|
|
99
|
+
const spec = key === '.' ? name : `${name}/${key.slice(2)}`;
|
|
100
|
+
if (key !== '.' && isBrowserEntry(key)) browserEntries.push(spec);
|
|
101
|
+
else if (!key.startsWith('./') || key === './package.json') continue;
|
|
102
|
+
else nodeEntries.push(spec);
|
|
103
|
+
}
|
|
104
|
+
writeFileSync(probe, [
|
|
105
|
+
'import { createRequire } from "node:module";',
|
|
106
|
+
'import { existsSync } from "node:fs";',
|
|
107
|
+
'import { join } from "node:path";',
|
|
108
|
+
`const specs = ${JSON.stringify(nodeEntries)};`,
|
|
109
|
+
`const deps = ${JSON.stringify(deps)};`,
|
|
110
|
+
'const out = { entries: [], deps: [] };',
|
|
111
|
+
'for (const s of specs) {',
|
|
112
|
+
' try { await import(s); out.entries.push({ spec: s, ok: true }); }',
|
|
113
|
+
' catch (e) { out.entries.push({ spec: s, ok: false, error: String(e.code || "") + " " + String(e.message || e).slice(0, 200) }); }',
|
|
114
|
+
'}',
|
|
115
|
+
'const req = createRequire(import.meta.url);',
|
|
116
|
+
'for (const d of deps) {',
|
|
117
|
+
' // 判据是「**依赖真的装进来了**」,不是「它的 main 能解析」:',
|
|
118
|
+
' // 有些包只发布子路径(如 tree-sitter-wasms 的 main 指向未发布目录,但 out/*.wasm 可用),',
|
|
119
|
+
' // 用 require.resolve(包名) 判会把正常包判成缺依赖。',
|
|
120
|
+
' const installed = existsSync(join(process.cwd(), "node_modules", d, "package.json"));',
|
|
121
|
+
' if (!installed) { out.deps.push({ dep: d, ok: false, error: "未安装(依赖闭包不全)" }); continue; }',
|
|
122
|
+
' if (d.startsWith("node:")) { out.deps.push({ dep: d, ok: true }); continue; }',
|
|
123
|
+
' try { req.resolve(d); out.deps.push({ dep: d, ok: true }); }',
|
|
124
|
+
' catch (e) { out.deps.push({ dep: d, ok: true, warn: `已安装,但 bare 名不可解析(可能只导出子路径):${e.code || ""}` }); }',
|
|
125
|
+
'}',
|
|
126
|
+
'console.log(JSON.stringify(out));',
|
|
127
|
+
].join('\n'), 'utf8');
|
|
128
|
+
// 直接用 node 跑探针脚本:比 `npm exec` 稳(后者在部分版本会给 stdout 加包装/额外行)
|
|
129
|
+
let parsed = null;
|
|
130
|
+
try {
|
|
131
|
+
const { execFileSync } = await import('node:child_process');
|
|
132
|
+
const stdout = execFileSync(process.execPath, [probe], { cwd: consumer, encoding: 'utf8', timeout: timeoutMs });
|
|
133
|
+
parsed = JSON.parse(String(stdout).trim().split('\n').pop());
|
|
134
|
+
} catch (e) {
|
|
135
|
+
failures.push(`加载探针执行失败:${String(e.message || e).slice(0, 300)}`);
|
|
136
|
+
return { ok: false, steps, failures, tgz: tgzName, bytes };
|
|
137
|
+
}
|
|
138
|
+
for (const it of parsed.entries || []) {
|
|
139
|
+
if (it.ok) steps.push({ step: 'import', spec: it.spec, ok: true });
|
|
140
|
+
else { steps.push({ step: 'import', spec: it.spec, ok: false }); failures.push(`公开面 import 失败:${it.spec} → ${it.error}`); }
|
|
141
|
+
}
|
|
142
|
+
for (const it of parsed.deps || []) {
|
|
143
|
+
if (it.ok) steps.push({ step: 'dep', dep: it.dep, ok: true });
|
|
144
|
+
else { steps.push({ step: 'dep', dep: it.dep, ok: false }); failures.push(`依赖解析失败:${it.dep} → ${it.error}`); }
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
ok: failures.length === 0,
|
|
148
|
+
tgz: tgzName, bytes,
|
|
149
|
+
entries: nodeEntries.length + browserEntries.length,
|
|
150
|
+
deps: deps.length,
|
|
151
|
+
// 浏览器端入口(如 ./client)不在 Node 里 import,如实列出而不是静默当通过
|
|
152
|
+
browserEntries,
|
|
153
|
+
steps, failures,
|
|
154
|
+
hint: failures.length ? '包本身消费不了,先修再发' : '装得上、公开面能加载、依赖闭包完整',
|
|
155
|
+
};
|
|
156
|
+
} catch (e) {
|
|
157
|
+
failures.push(`冒烟异常:${String(e?.message || e).slice(0, 300)}`);
|
|
158
|
+
return { ok: false, steps, failures };
|
|
159
|
+
} finally {
|
|
160
|
+
if (tmp) { try { rmSync(tmp, { recursive: true, force: true }); } catch { /* 临时目录清理失败不影响结论 */ } }
|
|
161
|
+
}
|
|
162
|
+
}
|
package/lib/self/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
export const VERSION = '2.15.
|
|
12
|
+
export const VERSION = '2.15.2'; // 2.15.2:新增本地 npm registry(scripts/local-registry.mjs)——把本机当 npm 网站做发布彩排。零依赖实现 publish/install 两条协议(PUT 收 tarball、GET 发 packument、GET 下 tarball),未知包透传公网当镜像;`--selftest` 一条命令跑完 publish→install→加载公开面,全程不写公网 registry、结束即清临时目录。历史:2.15.1 发布前冒烟。
|
|
13
13
|
// 实测后果:该目录有 package.json ⇒ npm 去打包整棵 DSH 树、输出 2.1MB > spawnSync 默认 maxBuffer 1MB ⇒ ENOBUFS(npm_publish 的体检与真发全部失败);
|
|
14
14
|
// 该目录没有 package.json 时报 ENOENT。现 publish 的两个调用点显式传 cwd=目标包目录,并给 runNpm 加 16MB maxBuffer。
|
|
15
15
|
// 2.8.3:文案修正——「零改动 / 无新提交」明确为**正常成功路径**(注入用法、工具描述、README、skill 同批改),并修 job 输出自相矛盾(cleanSkipCommit 路径补 committed:false,不再显示「已提交,未推送」)
|
package/package.json
CHANGED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 极简本地 npm registry —— 把本机当成"npm 网站",用于**发布前彩排**:publish → install → 加载 全链路真跑一遍,
|
|
4
|
+
* 公网零痕迹(不写 registry.npmjs.org)。
|
|
5
|
+
*
|
|
6
|
+
* 为什么自己写而不装 verdaccio:registry 的 publish/install 协议面很小(两个路由 + 一个 tarball 下载),
|
|
7
|
+
* 而 verdaccio 是一个需要安装、有自己配置与存储布局的服务。这里只要能回答三件事就够了:
|
|
8
|
+
* ① `npm publish` 能不能成功(含版本冲突检测、dist-tags 写入)
|
|
9
|
+
* ② `npm install <pkg>` 能不能装上(packument + tarball 两个请求)
|
|
10
|
+
* ③ 装完能不能加载(由调用方继续做 import 验证)
|
|
11
|
+
* 零依赖 → 不污染工作区、不需要 node_modules、随仓库走。
|
|
12
|
+
*
|
|
13
|
+
* 用法:
|
|
14
|
+
* node scripts/local-registry.mjs --port 4873 [--store /tmp/xxx] # 前台起服务
|
|
15
|
+
* node scripts/local-registry.mjs --selftest # 起服务→publish→install→退出 全自动验证
|
|
16
|
+
*
|
|
17
|
+
* 路由:
|
|
18
|
+
* PUT /:name npm publish(body 含 _attachments[].data = base64 tarball)
|
|
19
|
+
* GET /:name packument(versions + dist-tags + dist.tarball)
|
|
20
|
+
* GET /:name/-/:file tarball 下载
|
|
21
|
+
* GET /-/ping 健康检查(npm ping 用)
|
|
22
|
+
*/
|
|
23
|
+
import { createServer } from 'node:http';
|
|
24
|
+
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync, existsSync, readdirSync } from 'node:fs';
|
|
25
|
+
import { fileURLToPath } from 'node:url';
|
|
26
|
+
import { join } from 'node:path';
|
|
27
|
+
import { tmpdir } from 'node:os';
|
|
28
|
+
import { gunzipSync } from 'node:zlib';
|
|
29
|
+
import { spawn } from 'node:child_process';
|
|
30
|
+
|
|
31
|
+
/** 内存 + 磁盘双写:packument 放内存,tarball 落盘(便于安装时按路径读)。 */
|
|
32
|
+
function createRegistry({ storeDir }) {
|
|
33
|
+
const packuments = new Map(); // name → { 'dist-tags', versions }
|
|
34
|
+
const tarballs = new Map(); // `${name}/${file}` → Buffer
|
|
35
|
+
mkdirSync(storeDir, { recursive: true });
|
|
36
|
+
|
|
37
|
+
const readBody = (req) => new Promise((resolve, reject) => {
|
|
38
|
+
const chunks = [];
|
|
39
|
+
req.on('data', (c) => chunks.push(c));
|
|
40
|
+
req.on('end', () => {
|
|
41
|
+
let buf = Buffer.concat(chunks);
|
|
42
|
+
// npm 可能按配置 gzip 上传
|
|
43
|
+
if (req.headers['content-encoding'] === 'gzip') { try { buf = gunzipSync(buf); } catch { /* 原样尝试 */ } }
|
|
44
|
+
resolve(buf);
|
|
45
|
+
});
|
|
46
|
+
req.on('error', reject);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const json = (res, code, obj) => {
|
|
50
|
+
const body = JSON.stringify(obj);
|
|
51
|
+
res.writeHead(code, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) });
|
|
52
|
+
res.end(body);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const UPSTREAM = 'https://registry.npmjs.org';
|
|
56
|
+
|
|
57
|
+
/** 未知包的透传代理(GET 只读)。失败原样返回上游状态码,便于排查。 */
|
|
58
|
+
async function proxyToUpstream(req, res) {
|
|
59
|
+
const ctrl = new AbortController();
|
|
60
|
+
const timer = setTimeout(() => ctrl.abort(), 30000);
|
|
61
|
+
try {
|
|
62
|
+
const upstream = await fetch(`${UPSTREAM}${req.url}`, {
|
|
63
|
+
headers: { accept: String(req.headers.accept || '*/*'), 'user-agent': 'dsh-git-push-local-registry' },
|
|
64
|
+
signal: ctrl.signal,
|
|
65
|
+
});
|
|
66
|
+
const buf = Buffer.from(await upstream.arrayBuffer());
|
|
67
|
+
res.writeHead(upstream.status, {
|
|
68
|
+
'content-type': upstream.headers.get('content-type') || 'application/octet-stream',
|
|
69
|
+
'content-length': buf.length,
|
|
70
|
+
});
|
|
71
|
+
return res.end(buf);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
return json(res, 502, { error: `upstream proxy failed: ${String(e?.message || e).slice(0, 200)}` });
|
|
74
|
+
} finally { clearTimeout(timer); }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const server = createServer(async (req, res) => {
|
|
78
|
+
const url = new URL(req.url, 'http://127.0.0.1');
|
|
79
|
+
const path = decodeURIComponent(url.pathname);
|
|
80
|
+
|
|
81
|
+
if (path === '/-/ping') return json(res, 200, { ok: true });
|
|
82
|
+
|
|
83
|
+
// tarball 下载:/<name>/-/<file>
|
|
84
|
+
const tarMatch = /^\/(.+)\/-\/([^/]+)$/.exec(path);
|
|
85
|
+
if (tarMatch && req.method === 'GET') {
|
|
86
|
+
const key = `${tarMatch[1]}/${tarMatch[2]}`;
|
|
87
|
+
const buf = tarballs.get(key);
|
|
88
|
+
if (!buf) { res.writeHead(404); return res.end('not found'); }
|
|
89
|
+
res.writeHead(200, { 'content-type': 'application/octet-stream', 'content-length': buf.length });
|
|
90
|
+
return res.end(buf);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// packument / publish:/<name>(支持 @scope/name 两段)
|
|
94
|
+
const name = path.replace(/^\//, '');
|
|
95
|
+
if (!name) { res.writeHead(404); return res.end('not found'); }
|
|
96
|
+
|
|
97
|
+
if (req.method === 'GET') {
|
|
98
|
+
const doc = packuments.get(name);
|
|
99
|
+
if (doc) return json(res, 200, doc);
|
|
100
|
+
// 未知包**透传到公网 registry**(本服务因此等价于「本地镜像站」):
|
|
101
|
+
// 只存自己发布的包,其余(依赖树)照常从 npmjs 拉——否则 `npm install <pkg>`
|
|
102
|
+
// 会在解析 js-yaml / web-tree-sitter 这些依赖时 404(实测踩过)。
|
|
103
|
+
return proxyToUpstream(req, res);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (req.method === 'PUT') {
|
|
107
|
+
let body;
|
|
108
|
+
try { body = JSON.parse((await readBody(req)).toString('utf8')); } catch (e) {
|
|
109
|
+
return json(res, 400, { error: `bad body: ${e.message}` });
|
|
110
|
+
}
|
|
111
|
+
const versions = body.versions || {};
|
|
112
|
+
const versionIds = Object.keys(versions);
|
|
113
|
+
if (!versionIds.length) return json(res, 400, { error: 'no versions' });
|
|
114
|
+
// 版本冲突检测:已存在的版本号不允许重复发布(与真 registry 语义一致)
|
|
115
|
+
const existing = packuments.get(name) || { 'dist-tags': {}, versions: {} };
|
|
116
|
+
for (const v of versionIds) {
|
|
117
|
+
if (existing.versions[v]) return json(res, 403, { error: `cannot publish over existing version ${v}` });
|
|
118
|
+
}
|
|
119
|
+
// 收 tarball
|
|
120
|
+
for (const [file, att] of Object.entries(body._attachments || {})) {
|
|
121
|
+
const buf = Buffer.from(String(att && att.data ? att.data : ''), 'base64');
|
|
122
|
+
tarballs.set(`${name}/${file}`, buf);
|
|
123
|
+
writeFileSync(join(storeDir, file.replace(/[\\/]/g, '_')), buf);
|
|
124
|
+
}
|
|
125
|
+
// 组装 packument(dist.tarball 指向本服务)
|
|
126
|
+
const merged = {
|
|
127
|
+
name,
|
|
128
|
+
'dist-tags': { ...(existing['dist-tags'] || {}), ...(body['dist-tags'] || {}) },
|
|
129
|
+
versions: { ...existing.versions, ...versions },
|
|
130
|
+
};
|
|
131
|
+
for (const v of Object.keys(merged.versions)) {
|
|
132
|
+
const file = `${name}-${v}.tgz`;
|
|
133
|
+
merged.versions[v] = {
|
|
134
|
+
...merged.versions[v],
|
|
135
|
+
dist: { ...(merged.versions[v].dist || {}), tarball: `http://127.0.0.1:${server.address().port}/${name}/-/${file}` },
|
|
136
|
+
};
|
|
137
|
+
if (!tarballs.has(`${name}/${file}`) && tarballs.has(`${name}/${Object.keys(body._attachments || {})[0]}`)) {
|
|
138
|
+
tarballs.set(`${name}/${file}`, tarballs.get(`${name}/${Object.keys(body._attachments || {})[0]}`));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
packuments.set(name, merged);
|
|
142
|
+
return json(res, 201, { ok: true, name, versions: Object.keys(merged.versions) });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
res.writeHead(405); res.end('method not allowed');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return { server, packuments, tarballs };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 起服务并在随机端口上监听。 */
|
|
152
|
+
export async function startLocalRegistry({ storeDir, port = 0 } = {}) {
|
|
153
|
+
const dir = storeDir || mkdtempSync(join(tmpdir(), 'dshgp-registry-'));
|
|
154
|
+
const { server, packuments, tarballs } = createRegistry({ storeDir: dir });
|
|
155
|
+
await new Promise((resolve, reject) => {
|
|
156
|
+
server.once('error', reject);
|
|
157
|
+
server.listen(port, '127.0.0.1', resolve);
|
|
158
|
+
});
|
|
159
|
+
const actualPort = server.address().port;
|
|
160
|
+
return {
|
|
161
|
+
url: `http://127.0.0.1:${actualPort}`,
|
|
162
|
+
port: actualPort,
|
|
163
|
+
storeDir: dir,
|
|
164
|
+
packuments,
|
|
165
|
+
tarballs,
|
|
166
|
+
close: () => new Promise((resolve) => { server.close(() => { try { rmSync(dir, { recursive: true, force: true }); } catch { /* 清理失败不影响结论 */ } resolve(); }); }),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 异步执行命令(**不能用 spawnSync**)。
|
|
172
|
+
* 为什么:本脚本的 registry 服务与调用方在**同一个进程**里;spawnSync 会阻塞事件循环,
|
|
173
|
+
* 于是 npm 发的 PUT 请求没人处理、而这边又在等 npm 退出 —— 直接死锁(实测 publish 卡住 10 分钟)。
|
|
174
|
+
* 用异步 spawn 让事件循环继续跑,registry 才能应答。
|
|
175
|
+
*/
|
|
176
|
+
function run(cmd, args, { cwd, env, timeoutMs = 300000 } = {}) {
|
|
177
|
+
return new Promise((resolve) => {
|
|
178
|
+
const child = spawn(cmd, args, { cwd, env, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
179
|
+
let out = '';
|
|
180
|
+
let err = '';
|
|
181
|
+
child.stdout.on('data', (d) => { out += String(d); });
|
|
182
|
+
child.stderr.on('data', (d) => { err += String(d); });
|
|
183
|
+
const timer = setTimeout(() => { try { child.kill('SIGTERM'); } catch { /* 已退出 */ } }, timeoutMs);
|
|
184
|
+
child.on('close', (code, signal) => { clearTimeout(timer); resolve({ code, signal, out, err }); });
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** 自测:起服务 → publish 本仓库包 → 从本地 registry 安装 → 加载公开面 → 收尾。 */
|
|
189
|
+
async function selftest() {
|
|
190
|
+
// 必须用 fileURLToPath:本仓库路径含中文,URL.pathname 会给出 %E5.. 转义路径,spawn 的 cwd 就不存在
|
|
191
|
+
const repo = fileURLToPath(new URL('..', import.meta.url));
|
|
192
|
+
const reg = await startLocalRegistry();
|
|
193
|
+
console.log(`[registry] ${reg.url}(存储 ${reg.storeDir})`);
|
|
194
|
+
try {
|
|
195
|
+
const work = mkdtempSync(join(tmpdir(), 'dshgp-regtest-'));
|
|
196
|
+
// 用一个临时 npmrc,把 registry 与假 token 都指到本地(绝对不碰公网)
|
|
197
|
+
writeFileSync(join(work, '.npmrc'), `registry=${reg.url}/\n//127.0.0.1:${reg.port}/:_authToken=local-dummy\n`, 'utf8');
|
|
198
|
+
const env = { ...process.env, npm_config_userconfig: join(work, '.npmrc'), npm_config_registry: `${reg.url}/` };
|
|
199
|
+
|
|
200
|
+
console.log('[1/3] npm publish → 本地 registry');
|
|
201
|
+
const npmrc = join(work, '.npmrc');
|
|
202
|
+
writeFileSync(npmrc, `registry=${reg.url}/\n//127.0.0.1:${reg.port}/:_authToken=local-dummy\n`, 'utf8');
|
|
203
|
+
// 必须用 --userconfig 显式指定:只在 env 里给 token 时 npm 仍报 ENEEDAUTH(客户端侧校验)
|
|
204
|
+
const common = ['--registry', `${reg.url}/`, '--userconfig', npmrc];
|
|
205
|
+
const pub = await run('npm', ['publish', ...common, '--tag', 'beta'], { cwd: repo, env, timeoutMs: 180000 });
|
|
206
|
+
console.log(' ', pub.code === 0 ? 'OK' : `FAILED(code=${pub.code} signal=${pub.signal}): ${pub.out.slice(-600)} | ${pub.err.slice(-600)}`);
|
|
207
|
+
if (pub.code !== 0) return 1;
|
|
208
|
+
|
|
209
|
+
const consumer = join(work, 'consumer');
|
|
210
|
+
mkdirSync(consumer, { recursive: true });
|
|
211
|
+
writeFileSync(join(consumer, 'package.json'), JSON.stringify({ name: 'consumer', private: true, version: '0.0.0' }, null, 2));
|
|
212
|
+
console.log('[2/3] npm install <pkg> --registry 本地');
|
|
213
|
+
const inst = await run('npm', ['install', 'dsh-git-push@beta', ...common, '--no-audit', '--no-fund'], { cwd: consumer, env, timeoutMs: 300000 });
|
|
214
|
+
console.log(' ', inst.code === 0 ? 'OK' : `FAILED(code=${inst.code}): ${inst.out.slice(-600)} | ${inst.err.slice(-600)}`);
|
|
215
|
+
if (inst.code !== 0) return 1;
|
|
216
|
+
|
|
217
|
+
// 顺带校验 packument(相当于「本地 npm 网站上真的能查到这版」)
|
|
218
|
+
const doc = reg.packuments.get('dsh-git-push');
|
|
219
|
+
console.log(' packument: dist-tags =', JSON.stringify(doc && doc['dist-tags']), '| versions =', Object.keys((doc && doc.versions) || {}));
|
|
220
|
+
console.log('[3/3] 加载公开面 + 解析依赖');
|
|
221
|
+
const probe = await run(process.execPath, ['-e', `
|
|
222
|
+
const m = await import('dsh-git-push');
|
|
223
|
+
console.log(' root apply:', typeof m.apply);
|
|
224
|
+
const a = await import('dsh-git-push/audit');
|
|
225
|
+
console.log(' ./audit 导出数:', Object.keys(a).length);
|
|
226
|
+
`, '--input-type=module'], { cwd: consumer, env, timeoutMs: 120000 });
|
|
227
|
+
console.log(probe.out.trim() || `FAILED: ${probe.err.slice(0, 400)}`);
|
|
228
|
+
return probe.code === 0 ? 0 : 1;
|
|
229
|
+
} finally {
|
|
230
|
+
await reg.close();
|
|
231
|
+
console.log('[cleanup] 本地 registry 已停止,临时目录已删除(公网零痕迹)');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// 直接运行时:--selftest 跑全链路,否则前台起服务
|
|
236
|
+
if (process.argv[1] && process.argv[1].endsWith('local-registry.mjs')) {
|
|
237
|
+
if (process.argv.includes('--selftest')) {
|
|
238
|
+
selftest().then((code) => process.exit(code));
|
|
239
|
+
} else {
|
|
240
|
+
const portArg = process.argv.find((a) => a.startsWith('--port='));
|
|
241
|
+
const port = Number(portArg ? portArg.split('=')[1] : 4873);
|
|
242
|
+
const reg = await startLocalRegistry({ port });
|
|
243
|
+
console.log(`本地 npm registry: ${reg.url}(Ctrl-C 结束)`);
|
|
244
|
+
console.log('用法:npm publish --registry ' + reg.url + '/ --userconfig <含 //127.0.0.1:' + reg.port + '/:_authToken=xxx 的 npmrc>');
|
|
245
|
+
const stop = async () => { await reg.close(); process.exit(0); };
|
|
246
|
+
process.on('SIGINT', stop); process.on('SIGTERM', stop);
|
|
247
|
+
}
|
|
248
|
+
}
|