dsh-git-push 2.15.2 → 2.16.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/lib/app/command-registry.js +2 -0
- package/lib/app/tool-call.js +20 -2
- package/lib/git/index.js +2 -2
- package/lib/git/init-compat.js +45 -1
- package/lib/git/remote.js +51 -16
- package/lib/git/transport.js +36 -0
- package/lib/npm/local-registry.js +218 -0
- package/lib/npm/publish.js +25 -1
- package/lib/self/index.js +1 -1
- package/package.json +1 -1
- package/scripts/local-registry.mjs +25 -236
|
@@ -93,6 +93,7 @@ export const TOOL_REGISTRY = [
|
|
|
93
93
|
params: [
|
|
94
94
|
{ flag: 'repo', type: 'string', positional: 0, required: true, desc: '本地 git 仓库绝对路径' },
|
|
95
95
|
{ flag: 'visibility', type: 'string', enum: ['public', 'private'], default: 'private', desc: '可见性' },
|
|
96
|
+
{ flag: 'pushInitial', type: 'boolean', desc: '建仓后做首次推送(本地无提交时先本地提交,再走 Git Data API 上传;CIFS 上不需 git remote add)' },
|
|
96
97
|
{ flag: 'dryRun', type: 'boolean', desc: '只探测预演' },
|
|
97
98
|
],
|
|
98
99
|
},
|
|
@@ -132,6 +133,7 @@ export const TOOL_REGISTRY = [
|
|
|
132
133
|
{ flag: 'waitVisible', type: 'boolean', desc: '发布后等待 registry 可见(默认 false:上传成功即为成功,可见延迟是已知现象;true 时每分钟查一次、最多 5 分钟)' },
|
|
133
134
|
{ flag: 'json', type: 'boolean', desc: 'JSON 输出(CLI 用;与其它工具同口径)' },
|
|
134
135
|
{ flag: 'skipSmoke', type: 'boolean', desc: '跳过发布前冒烟(默认跑:pack→干净目录安装→import 公开面→解析依赖;关掉会失去“装得上”这层门禁)' },
|
|
136
|
+
{ flag: 'localPreview', type: 'boolean', desc: '本地 registry 彩排(默认关):真实 publish 到本机 registry 再从它安装并加载公开面,不写公网;分钟级,发版前建议开一次' },
|
|
135
137
|
],
|
|
136
138
|
},
|
|
137
139
|
{
|
package/lib/app/tool-call.js
CHANGED
|
@@ -18,6 +18,7 @@ import { auditWithScope, auditFull, summarize, isBlockerFinding } from '../audit
|
|
|
18
18
|
import { formatAnalysisCoverageLine } from '../audit/analysis-coverage.js';
|
|
19
19
|
import { scoreQuality } from '../score/index.js';
|
|
20
20
|
import {
|
|
21
|
+
pushInitialContent,
|
|
21
22
|
cloneViaApi, previewClone, resolveMaxCloneFileMB, ensureRemoteRepo, setVisibility, renameRepo, maintainRepoIndex, updateRepoIndex, resolveToken, commitAndPush,
|
|
22
23
|
scanReposAsync, scanRepos, checkGithubAccount, formatGithubAccountBlock, generateSshKey, persistSshPub,
|
|
23
24
|
refreshAccountStatus,
|
|
@@ -294,6 +295,8 @@ async function callNpmPublishJob(args = {}, jobs, exec, log, env = {}, cfg = {})
|
|
|
294
295
|
otp: args.otp || '',
|
|
295
296
|
// 发布前冒烟默认开;慢/离线环境可关(关掉就等于放弃「装得上」这一层门禁)
|
|
296
297
|
skipSmoke: args.skipSmoke === true || args.noSmoke === true,
|
|
298
|
+
// 本地 registry 彩排:走真 publish/install 协议但不写公网(分钟级,发版前建议开一次)
|
|
299
|
+
localPreview: args.localPreview === true,
|
|
297
300
|
confirm: args.confirm === true,
|
|
298
301
|
// 可选:发布后等待 registry 可见(默认不等——上传成功即为成功,可见延迟是已知现象)
|
|
299
302
|
waitVisible: args.waitVisible === true,
|
|
@@ -595,9 +598,24 @@ export async function callTool(toolName, args = {}, env = {}, cfg = defaultConfi
|
|
|
595
598
|
}
|
|
596
599
|
case 'npm_publish':
|
|
597
600
|
return callNpmPublishJob(args, jobs, exec, log, env, cfg);
|
|
598
|
-
case 'git_remote_create':
|
|
601
|
+
case 'git_remote_create': {
|
|
599
602
|
if (!args.repo) return { ok: false, error: MSG_REPO_REQUIRED };
|
|
600
|
-
|
|
603
|
+
const created = await ensureRemoteRepo({ repoPath: args.repo, visibility: args.visibility || 'private', dryRun: args.dryRun === true });
|
|
604
|
+
// 可选:建仓后做「首次推送」——本地还没提交就先提交,再走 Git Data API 上传。
|
|
605
|
+
// CIFS 上这一整条链不依赖 `git remote add/config`(那些会 chmod 失败),
|
|
606
|
+
// 只有 add/commit 两次本地写,实测在 smb2 挂载上可用。
|
|
607
|
+
if (args.pushInitial === true && created.ok && created.dryRun !== true) {
|
|
608
|
+
const push = await pushInitialContent({ repoPath: args.repo, branch: args.branch || '', token: '' });
|
|
609
|
+
return {
|
|
610
|
+
...created,
|
|
611
|
+
initialPush: {
|
|
612
|
+
ok: push.ok === true, pushed: push.pushed === true, committed: push.committed === true,
|
|
613
|
+
reason: push.reason || '', remoteSha: push.remoteSha || push.commitSha || '',
|
|
614
|
+
},
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
return created;
|
|
618
|
+
}
|
|
601
619
|
case 'git_identity_rewrite':
|
|
602
620
|
// 提交身份历史改写:默认 dryRun 只报告,真改写要先 dryRun:false/write,推送要 push:true
|
|
603
621
|
return callIdentityRewrite(args, env, cfg);
|
package/lib/git/index.js
CHANGED
|
@@ -28,10 +28,10 @@ export { GH_API, githubFetch, parseGithubOwnerRepo, isBadCredentials, detectRepo
|
|
|
28
28
|
export { resolveRepoVisibility } from './visibility.js';
|
|
29
29
|
export { scanSensitiveFiles } from './sensitive.js';
|
|
30
30
|
export { DEFAULT_IGNORE_PATTERNS, ensureGitignore } from './ignore.js';
|
|
31
|
-
export { fetchRemoteHeads, dispatchPush, pushViaSsh, pushViaApi, sshReason, isNonFastForward, liveRemoteHead, liveRemoteHeadAsync } from './transport.js';
|
|
31
|
+
export { fetchRemoteHeads, dispatchPush, pushViaSsh, pushViaApi, pushInitialContent, sshReason, isNonFastForward, liveRemoteHead, liveRemoteHeadAsync } from './transport.js';
|
|
32
32
|
export { readmeCheckHint, commitAndPush, pushCurrentBranch } from './push.js';
|
|
33
33
|
export { cloneViaApi, previewClone, resolveMaxCloneFileMB, isPartialCloneDir } from './clone.js';
|
|
34
|
-
export { ensureRemoteRepo, setVisibility } from './remote.js';
|
|
34
|
+
export { ensureRemoteRepo, setVisibility, prepareLocalRepoForRemote } from './remote.js';
|
|
35
35
|
export { renameRepo } from './rename.js';
|
|
36
36
|
export { scanRepos, scanReposAsync, collectRepoPaths, describeRepo, describeRepoAsync, maskRemoteUrl } from './repos.js';
|
|
37
37
|
export { browseDir } from './browse.js';
|
package/lib/git/init-compat.js
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* 因此降级路径 = 手写 `.git/{HEAD,config}` + 空 `objects/refs/info` 目录,绕开 git 的
|
|
23
23
|
* config 写入流程;config 里预先写死 `filemode = false`。
|
|
24
24
|
*/
|
|
25
|
-
import { existsSync, mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
|
|
25
|
+
import { existsSync, mkdirSync, writeFileSync, chmodSync, rmSync, realpathSync } from 'node:fs';
|
|
26
26
|
import { join } from 'node:path';
|
|
27
27
|
import { runGit } from './exec.js';
|
|
28
28
|
|
|
@@ -112,6 +112,50 @@ export function initRepoCompat(dir, { branch = 'master', remoteUrl = '', chmodOk
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
/**
|
|
116
|
+
* 现有 .git 是否**真的可用**(不是"目录存在"就算)。
|
|
117
|
+
*
|
|
118
|
+
* 为什么必须实测:CIFS/SMB 上 `git init` 会在写 config 时 chmod 失败而**半途退出**——
|
|
119
|
+
* 留下 `.git/{config,description,hooks,info}` 却**没有 HEAD/objects/refs**。
|
|
120
|
+
* 此时 `existsSync('.git')` 为真,但 `git remote add` 直接报「not a git repository」。
|
|
121
|
+
* 历史实现正是拿 existsSync 当判据,于是「建仓成功、origin 没设上」还报成功。
|
|
122
|
+
*/
|
|
123
|
+
export function isUsableRepo(dir) {
|
|
124
|
+
const r = runGit(['rev-parse', '--git-dir'], { cwd: dir });
|
|
125
|
+
if (!r || !r.ok) return false;
|
|
126
|
+
// 必须确认「这个目录本身」就是仓库根:git 会向上层目录找仓库,
|
|
127
|
+
// 若目标目录位于某个父仓库内部(如插件里的临时目录),
|
|
128
|
+
// `rev-parse --git-dir` 同样成功 —— 会把它误判成"已有仓库"从而跳过初始化(实测踩过)。
|
|
129
|
+
const top = runGit(['rev-parse', '--show-toplevel'], { cwd: dir });
|
|
130
|
+
if (!top || !top.ok) return false;
|
|
131
|
+
const real = (p) => { try { return realpathSync.native(p); } catch { return p; } };
|
|
132
|
+
return real(String(top.stdout || '').trim() || dir) === real(dir);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 确保目录里有一个**可用**的本地 git 仓库(CIFS 安全)。
|
|
137
|
+
*
|
|
138
|
+
* 三种情形:
|
|
139
|
+
* · 已可用 → action:'keep'(不动它,避免覆盖用户既有配置)
|
|
140
|
+
* · 有残缺 .git(半途而废)→ action:'repair'(走 initRepoCompat 补全/重写)
|
|
141
|
+
* · 没有 .git → action:'create'
|
|
142
|
+
*
|
|
143
|
+
* @param {string} dir 工作树目录
|
|
144
|
+
* @param {{branch?:string, remoteUrl?:string, remoteName?:string, chmodOk?:boolean,
|
|
145
|
+
* gitInit?:Function}} [opts]
|
|
146
|
+
* remoteUrl 给出时,降级路径会把 remote 段一并写进 config —— 因为 `git remote add`
|
|
147
|
+
* 同样是写 config,在 CIFS 上一样会 chmod 失败(实测)。
|
|
148
|
+
* @returns {{ok:boolean, action:'keep'|'repair'|'create'|'failed', via:string, filemode:?boolean, stderr?:string}}
|
|
149
|
+
*/
|
|
150
|
+
export function ensureUsableLocalRepo(dir, { branch = 'master', remoteUrl = '', remoteName = 'origin', chmodOk, gitInit } = {}) {
|
|
151
|
+
const exec = typeof gitInit === 'function' ? gitInit : (args, opts) => runGit(args, opts);
|
|
152
|
+
if (isUsableRepo(dir)) return { ok: true, action: 'keep', via: 'existing', filemode: null };
|
|
153
|
+
const hadPartial = hasGitDir(dir);
|
|
154
|
+
const r = initRepoCompat(dir, { branch, remoteUrl, remoteName, chmodOk, gitInit: exec });
|
|
155
|
+
if (!r.ok) return { ok: false, action: 'failed', via: r.via, filemode: r.filemode, stderr: r.stderr };
|
|
156
|
+
return { ok: true, action: hadPartial ? 'repair' : 'create', via: r.via, filemode: r.filemode };
|
|
157
|
+
}
|
|
158
|
+
|
|
115
159
|
/** .git 目录是否已存在(续跑判断:已 init 过就不再重复写)。 */
|
|
116
160
|
export function hasGitDir(dir) {
|
|
117
161
|
return existsSync(join(dir, '.git'));
|
package/lib/git/remote.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { existsSync } from 'node:fs';
|
|
8
8
|
import { basename, join } from 'node:path';
|
|
9
9
|
import { githubFetch } from './api.js';
|
|
10
|
+
import { ensureUsableLocalRepo } from './init-compat.js';
|
|
10
11
|
import { resolveToken } from './credentials.js';
|
|
11
12
|
import { runGit } from './exec.js';
|
|
12
13
|
|
|
@@ -34,19 +35,43 @@ function isApiFormOrigin(url) {
|
|
|
34
35
|
* —— 这样用旧版建过仓的仓库不用手工修。
|
|
35
36
|
* @returns {{action:'add'|'repair'|'keep', url:string, from?:string}}
|
|
36
37
|
*/
|
|
37
|
-
|
|
38
|
+
/**
|
|
39
|
+
* 确保「本地仓库可用 + origin 指向该 GitHub 仓库」,两者都按实际情况回报。
|
|
40
|
+
*
|
|
41
|
+
* 历史缺陷(CIFS 上必现):老实现只判 `existsSync('.git')` 就调 `git remote add`。
|
|
42
|
+
* CIFS/SMB 上 `git init` 会在写 config 时 chmod 失败而半途退出 —— 留下没有 HEAD/objects
|
|
43
|
+
* 的残缺 `.git`;于是 `remote add` 报「not a git repository」,而函数**仍返回 action:'add'**,
|
|
44
|
+
* 调用方据此报「建仓成功」,用户以为好了、其实本地根本不能用。
|
|
45
|
+
*
|
|
46
|
+
* 现在:先 ensureUsableLocalRepo(CIFS 安全 init,与 clone 用的同一套),再校验 origin;
|
|
47
|
+
* 任何一步失败都带 error 返回,不再谎报。
|
|
48
|
+
* @returns {{ok:boolean, action:'create'|'repair'|'keep'|'add'|'failed',
|
|
49
|
+
* url:string, localRepo:object, error?:string}}
|
|
50
|
+
*/
|
|
51
|
+
export function prepareLocalRepoForRemote(repoPath, ownerName, name, { branch = 'main' } = {}) {
|
|
38
52
|
const gitUrl = `${GH_WEB}/${ownerName}/${name}`;
|
|
53
|
+
const local = ensureUsableLocalRepo(repoPath, { branch, remoteUrl: gitUrl });
|
|
54
|
+
if (!local.ok) {
|
|
55
|
+
return { ok: false, action: 'failed', url: gitUrl, localRepo: local, error: `本地仓库不可用:${local.stderr || '初始化失败'}` };
|
|
56
|
+
}
|
|
39
57
|
const cur = runGit(['remote', 'get-url', 'origin'], { cwd: repoPath });
|
|
40
58
|
if (!cur.ok) {
|
|
41
|
-
runGit(['remote', 'add', 'origin', gitUrl], { cwd: repoPath });
|
|
42
|
-
return {
|
|
59
|
+
const add = runGit(['remote', 'add', 'origin', gitUrl], { cwd: repoPath });
|
|
60
|
+
return {
|
|
61
|
+
ok: !!add.ok, action: add.ok ? 'add' : 'failed', url: gitUrl, localRepo: local,
|
|
62
|
+
...(add.ok ? {} : { error: `设置 origin 失败:${String(add.stderr || '').slice(0, 200)}` }),
|
|
63
|
+
};
|
|
43
64
|
}
|
|
44
65
|
const existing = String(cur.stdout || cur.stderr || '').trim();
|
|
45
66
|
if (isApiFormOrigin(existing)) {
|
|
46
|
-
runGit(['remote', 'set-url', 'origin', gitUrl], { cwd: repoPath });
|
|
47
|
-
return {
|
|
67
|
+
const set = runGit(['remote', 'set-url', 'origin', gitUrl], { cwd: repoPath });
|
|
68
|
+
return {
|
|
69
|
+
// 动作名沿用既有语义 'repair'(把 API 形态 origin 修回 git 形态)——测试与外部消费方都按这个名字判断
|
|
70
|
+
ok: !!set.ok, action: set.ok ? 'repair' : 'failed', url: gitUrl, localRepo: local, from: existing,
|
|
71
|
+
...(set.ok ? {} : { error: `修正 origin 失败:${String(set.stderr || '').slice(0, 200)}` }),
|
|
72
|
+
};
|
|
48
73
|
}
|
|
49
|
-
return { action: 'keep', url: existing };
|
|
74
|
+
return { ok: true, action: 'keep', url: existing, localRepo: local };
|
|
50
75
|
}
|
|
51
76
|
|
|
52
77
|
/** 建仓(POST /user/repos)并设置 origin。dryRun 只探测不创建。 */
|
|
@@ -82,20 +107,30 @@ export async function ensureRemoteRepo({ repoPath = '', owner = '', visibility =
|
|
|
82
107
|
if (exists.status === 200) {
|
|
83
108
|
// 仓库已存在也要**确保 origin 可用**(原来这里直接 return,不碰 origin ⇒ 本地缺 origin 或
|
|
84
109
|
// 是旧版写下的 API 形态时会一直坏着)。
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
110
|
+
// 已存在也要确保本地可用 + origin 正确(原实现只要 existsSync('.git') 就往下走,CIFS 上会静默失败)
|
|
111
|
+
const prep = repoPath ? prepareLocalRepoForRemote(repoPath, ownerName, name) : null;
|
|
112
|
+
return {
|
|
113
|
+
ok: true, exists: true, owner: ownerName, name, visibility: vis, reason: '已存在同名仓库',
|
|
114
|
+
origin: `${GH_WEB}/${ownerName}/${name}`,
|
|
115
|
+
originAction: prep ? prep.action : 'skip',
|
|
116
|
+
originOk: prep ? prep.ok : false,
|
|
117
|
+
localRepo: prep ? prep.localRepo : null,
|
|
118
|
+
...(prep && !prep.ok ? { warning: prep.error } : {}),
|
|
119
|
+
};
|
|
90
120
|
}
|
|
91
121
|
if (dryRun) return { ok: true, dryRun: true, wouldCreate: true, owner: ownerName, name, visibility: vis };
|
|
92
122
|
const created = await githubFetch('/user/repos', { token: tok, method: 'POST', body: { name, private: vis === 'private' } });
|
|
93
123
|
if (created.status !== 201) return { ok: false, error: created.json?.message || created.error || `创建失败: HTTP ${created.status}` };
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
124
|
+
// 建仓后:确保本地仓库可用(CIFS 安全 init)+ origin 指向它;失败如实回报
|
|
125
|
+
const prep = repoPath ? prepareLocalRepoForRemote(repoPath, ownerName, name) : null;
|
|
126
|
+
return {
|
|
127
|
+
ok: true, created: true, owner: ownerName, name, visibility: vis,
|
|
128
|
+
origin: prep ? prep.url : `${GH_WEB}/${ownerName}/${name}`,
|
|
129
|
+
originAction: prep ? prep.action : 'skip',
|
|
130
|
+
originOk: prep ? prep.ok : false,
|
|
131
|
+
localRepo: prep ? prep.localRepo : null,
|
|
132
|
+
...(prep && !prep.ok ? { warning: prep.error } : {}),
|
|
133
|
+
};
|
|
99
134
|
}
|
|
100
135
|
|
|
101
136
|
/** 从本地仓库 origin remote 解析 owner/repo(支持 api.github.com/repos/、github.com、ssh:// 三格式)。 */
|
package/lib/git/transport.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import { join } from 'node:path';
|
|
15
15
|
import { githubFetch, isBadCredentials, parseGithubOwnerRepo } from './api.js';
|
|
16
16
|
import { resolveSshKey, resolveSshKeys, resolveToken } from './credentials.js';
|
|
17
|
+
import { resolveCommitIdentity } from './identity.js';
|
|
17
18
|
import { gitRaw, runGit, runGitAsync } from './exec.js';
|
|
18
19
|
// 「推送到 GitHub 时自动指定正确的 remote」:归属 + 本机可用形态(ssh:443)+ 名字优先级统一判定
|
|
19
20
|
import { resolveGithubRemote, parseGithubUrl } from './remote-resolve.js';
|
|
@@ -299,6 +300,41 @@ async function apiOrFallback(api, repoPath, branch, res) {
|
|
|
299
300
|
* token 401 → pushViaSsh 回退。
|
|
300
301
|
* @returns {{ok, pushed, method?, commitSha?, reason?, branchAdjusted?}}
|
|
301
302
|
*/
|
|
303
|
+
/**
|
|
304
|
+
* 首次推送:**本地还没有提交时**先做一次本地提交,再走 Git Data API 上传。
|
|
305
|
+
*
|
|
306
|
+
* 为什么需要:`pushViaApi` 以本地提交为输入(rev-parse HEAD / ls-tree / cat-file),
|
|
307
|
+
* 新建的仓库一个提交都没有 ⇒ 直接调它只会得到「本地无提交」。
|
|
308
|
+
* 而 CIFS/SMB 上恰好「本地提交」这件事需要 CIFS 安全 init 才能成立(见 init-compat.js)。
|
|
309
|
+
*
|
|
310
|
+
* 身份:优先用仓库已配置的 user.name/email;缺失时按 `-c` 临时给出(**不写 config**,
|
|
311
|
+
* 因为写 config 在 CIFS 上会 chmod 失败)。
|
|
312
|
+
* @returns {Promise<object>} pushViaApi 的返回,附加 committed 字段说明本次是否新做了提交
|
|
313
|
+
*/
|
|
314
|
+
export async function pushInitialContent({
|
|
315
|
+
repoPath = '', branch = '', token = '', message = 'init: 初始提交(dsh-git-push)', remote = '',
|
|
316
|
+
} = {}) {
|
|
317
|
+
const head = runGit(['rev-parse', 'HEAD'], { cwd: repoPath });
|
|
318
|
+
let committed = false;
|
|
319
|
+
if (!head.ok || !String(head.stdout || '').trim()) {
|
|
320
|
+
const readCfg = (key) => {
|
|
321
|
+
const r = runGit(['config', key], { cwd: repoPath });
|
|
322
|
+
return r && r.ok ? String(r.stdout || '').trim() : '';
|
|
323
|
+
};
|
|
324
|
+
const ident = resolveCommitIdentity({ configName: readCfg('user.name'), configEmail: readCfg('user.email') });
|
|
325
|
+
const idArgs = ident.complete ? [] : ['-c', `user.name=${ident.name}`, '-c', `user.email=${ident.email}`];
|
|
326
|
+
const add = runGit(['add', '-A'], { cwd: repoPath });
|
|
327
|
+
if (!add.ok) return { ok: false, pushed: false, committed: false, reason: `git add 失败:${String(add.stderr || '').slice(0, 300)}` };
|
|
328
|
+
const commit = runGit([...idArgs, 'commit', '-m', message], { cwd: repoPath });
|
|
329
|
+
if (!commit.ok && !/nothing to commit|no changes added/i.test(String(commit.stderr || ''))) {
|
|
330
|
+
return { ok: false, pushed: false, committed: false, reason: `git commit 失败:${String(commit.stderr || '').slice(0, 300)}` };
|
|
331
|
+
}
|
|
332
|
+
committed = !!commit.ok;
|
|
333
|
+
}
|
|
334
|
+
const r = await pushViaApi({ repoPath, branch, token, remote });
|
|
335
|
+
return { ...r, committed };
|
|
336
|
+
}
|
|
337
|
+
|
|
302
338
|
export async function pushViaApi({ repoPath = '', branch = '', token = '', force = false, remote = '' } = {}) {
|
|
303
339
|
const rr = resolveGithubRemote({ repoPath });
|
|
304
340
|
const origin = runGit(['remote', 'get-url', 'origin'], { cwd: repoPath }).stdout;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 零依赖本地 npm registry —— 把本机当"npm 网站",用于**发布前彩排**:
|
|
3
|
+
* publish → install → 加载 全链路真跑一遍,公网零痕迹(不写 registry.npmjs.org)。
|
|
4
|
+
*
|
|
5
|
+
* 为什么自己实现而不装 verdaccio:registry 的发布/安装协议面很小(三条路由),
|
|
6
|
+
* 而 verdaccio 是一个带配置与存储布局的独立服务。这里只要能回答三件事:
|
|
7
|
+
* ① `npm publish` 能不能成功(含版本冲突 403、dist-tags 写入)
|
|
8
|
+
* ② `npm install <pkg>` 能不能装上(packument + tarball 两个请求)
|
|
9
|
+
* ③ 装完能不能加载(由调用方继续 import 公开面)
|
|
10
|
+
* 零依赖 → 随仓库走、不污染 node_modules。
|
|
11
|
+
*
|
|
12
|
+
* 与「发布前冒烟(lib/npm/smoke.js)」的分工:
|
|
13
|
+
* · 冒烟 = pack → 本地装 tarball → import(4 秒级,验**包内容**);
|
|
14
|
+
* · 本地 registry = publish → 从 registry 装 → import(分钟级,验**真实发布/安装语义**:
|
|
15
|
+
* 版本冲突、dist-tags、exports、依赖解析全走真路径)。
|
|
16
|
+
*
|
|
17
|
+
* 两个实现坑(实测踩过,勿改回去):
|
|
18
|
+
* ① 调用方与 server 同进程时**必须用异步 spawn**,不能 spawnSync —— 后者阻塞事件循环,
|
|
19
|
+
* npm 发的 PUT 没人应答、这边又在等 npm 退出 ⇒ 死锁(实测 publish 卡 10 分钟)。
|
|
20
|
+
* ② npm publish 需要在 `--userconfig` 指定的 npmrc 里写该 registry 的认证令牌;
|
|
21
|
+
* 只在环境变量里给令牌,客户端仍报 ENEEDAUTH。
|
|
22
|
+
*/
|
|
23
|
+
import { createServer } from 'node:http';
|
|
24
|
+
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
25
|
+
import { join } from 'node:path';
|
|
26
|
+
import { tmpdir } from 'node:os';
|
|
27
|
+
import { randomBytes } from 'node:crypto';
|
|
28
|
+
import { gunzipSync } from 'node:zlib';
|
|
29
|
+
import { spawn } from 'node:child_process';
|
|
30
|
+
|
|
31
|
+
const UPSTREAM = 'https://registry.npmjs.org';
|
|
32
|
+
|
|
33
|
+
/** 异步执行命令(见文件头坑①:绝不能用 spawnSync)。 */
|
|
34
|
+
function run(cmd, args, { cwd, env, timeoutMs = 300000 } = {}) {
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
const child = spawn(cmd, args, { cwd, env, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
37
|
+
let out = '';
|
|
38
|
+
let err = '';
|
|
39
|
+
child.stdout.on('data', (d) => { out += String(d); });
|
|
40
|
+
child.stderr.on('data', (d) => { err += String(d); });
|
|
41
|
+
const timer = setTimeout(() => { try { child.kill('SIGTERM'); } catch { /* 已退出 */ } }, timeoutMs);
|
|
42
|
+
child.on('close', (code, signal) => { clearTimeout(timer); resolve({ code, signal, out, err }); });
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 起一个本地 registry。
|
|
48
|
+
* @param {{storeDir?:string, port?:number}} o
|
|
49
|
+
* @returns {Promise<{url:string, port:number, storeDir:string, packuments:Map, tarballs:Map, close:Function}>}
|
|
50
|
+
*/
|
|
51
|
+
export async function startLocalRegistry({ storeDir, port = 0 } = {}) {
|
|
52
|
+
const dir = storeDir || mkdtempSync(join(tmpdir(), 'dshgp-registry-'));
|
|
53
|
+
mkdirSync(dir, { recursive: true });
|
|
54
|
+
const packuments = new Map(); // name → { 'dist-tags', versions }
|
|
55
|
+
const tarballs = new Map(); // `${name}/${file}` → Buffer
|
|
56
|
+
|
|
57
|
+
const json = (res, code, obj) => {
|
|
58
|
+
const body = JSON.stringify(obj);
|
|
59
|
+
res.writeHead(code, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) });
|
|
60
|
+
res.end(body);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const readBody = (req) => new Promise((resolve, reject) => {
|
|
64
|
+
const chunks = [];
|
|
65
|
+
req.on('data', (c) => chunks.push(c));
|
|
66
|
+
req.on('end', () => {
|
|
67
|
+
let buf = Buffer.concat(chunks);
|
|
68
|
+
if (req.headers['content-encoding'] === 'gzip') { try { buf = gunzipSync(buf); } catch { /* 原样解析 */ } }
|
|
69
|
+
resolve(buf);
|
|
70
|
+
});
|
|
71
|
+
req.on('error', reject);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/** 未知包透传公有 registry(当镜像用;否则装依赖时会 404)。 */
|
|
75
|
+
async function proxyToUpstream(req, res) {
|
|
76
|
+
const ctrl = new AbortController();
|
|
77
|
+
const timer = setTimeout(() => ctrl.abort(), 30000);
|
|
78
|
+
try {
|
|
79
|
+
const upstream = await fetch(`${UPSTREAM}${req.url}`, {
|
|
80
|
+
headers: { accept: String(req.headers.accept || '*/*'), 'user-agent': 'dsh-git-push-local-registry' },
|
|
81
|
+
signal: ctrl.signal,
|
|
82
|
+
});
|
|
83
|
+
const buf = Buffer.from(await upstream.arrayBuffer());
|
|
84
|
+
res.writeHead(upstream.status, {
|
|
85
|
+
'content-type': upstream.headers.get('content-type') || 'application/octet-stream',
|
|
86
|
+
'content-length': buf.length,
|
|
87
|
+
});
|
|
88
|
+
return res.end(buf);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
return json(res, 502, { error: `upstream proxy failed: ${String(e?.message || e).slice(0, 200)}` });
|
|
91
|
+
} finally { clearTimeout(timer); }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const server = createServer(async (req, res) => {
|
|
95
|
+
const path = decodeURIComponent(new URL(req.url, 'http://127.0.0.1').pathname);
|
|
96
|
+
if (path === '/-/ping') return json(res, 200, { ok: true });
|
|
97
|
+
|
|
98
|
+
const tarMatch = /^\/(.+)\/-\/([^/]+)$/.exec(path);
|
|
99
|
+
if (tarMatch && req.method === 'GET') {
|
|
100
|
+
const buf = tarballs.get(`${tarMatch[1]}/${tarMatch[2]}`);
|
|
101
|
+
if (!buf) { res.writeHead(404); return res.end('not found'); }
|
|
102
|
+
res.writeHead(200, { 'content-type': 'application/octet-stream', 'content-length': buf.length });
|
|
103
|
+
return res.end(buf);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const name = path.replace(/^\//, '');
|
|
107
|
+
if (!name) { res.writeHead(404); return res.end('not found'); }
|
|
108
|
+
|
|
109
|
+
if (req.method === 'GET') {
|
|
110
|
+
const doc = packuments.get(name);
|
|
111
|
+
if (doc) return json(res, 200, doc);
|
|
112
|
+
return proxyToUpstream(req, res);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (req.method === 'PUT') {
|
|
116
|
+
let body;
|
|
117
|
+
try { body = JSON.parse((await readBody(req)).toString('utf8')); } catch (e) {
|
|
118
|
+
return json(res, 400, { error: `bad body: ${e.message}` });
|
|
119
|
+
}
|
|
120
|
+
const versions = body.versions || {};
|
|
121
|
+
const vids = Object.keys(versions);
|
|
122
|
+
if (!vids.length) return json(res, 400, { error: 'no versions' });
|
|
123
|
+
const existing = packuments.get(name) || { 'dist-tags': {}, versions: {} };
|
|
124
|
+
for (const v of vids) {
|
|
125
|
+
// 与真 registry 语义一致:同版本号不允许重复发布
|
|
126
|
+
if (existing.versions[v]) return json(res, 403, { error: `cannot publish over existing version ${v}` });
|
|
127
|
+
}
|
|
128
|
+
const atts = Object.entries(body._attachments || {});
|
|
129
|
+
for (const [file, att] of atts) {
|
|
130
|
+
tarballs.set(`${name}/${file}`, Buffer.from(String(att?.data || ''), 'base64'));
|
|
131
|
+
}
|
|
132
|
+
const merged = {
|
|
133
|
+
name,
|
|
134
|
+
'dist-tags': { ...(existing['dist-tags'] || {}), ...(body['dist-tags'] || {}) },
|
|
135
|
+
versions: { ...existing.versions, ...versions },
|
|
136
|
+
};
|
|
137
|
+
for (const v of Object.keys(merged.versions)) {
|
|
138
|
+
const file = `${name}-${v}.tgz`;
|
|
139
|
+
if (!tarballs.has(`${name}/${file}`) && atts.length) tarballs.set(`${name}/${file}`, tarballs.get(`${name}/${atts[0][0]}`));
|
|
140
|
+
merged.versions[v] = {
|
|
141
|
+
...merged.versions[v],
|
|
142
|
+
dist: { ...(merged.versions[v].dist || {}), tarball: `http://127.0.0.1:${server.address().port}/${name}/-/${file}` },
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
packuments.set(name, merged);
|
|
146
|
+
return json(res, 201, { ok: true, name, versions: Object.keys(merged.versions) });
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
res.writeHead(405); res.end('method not allowed');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
await new Promise((resolve, reject) => {
|
|
153
|
+
server.once('error', reject);
|
|
154
|
+
server.listen(port, '127.0.0.1', resolve);
|
|
155
|
+
});
|
|
156
|
+
const actualPort = server.address().port;
|
|
157
|
+
return {
|
|
158
|
+
url: `http://127.0.0.1:${actualPort}`,
|
|
159
|
+
port: actualPort,
|
|
160
|
+
storeDir: dir,
|
|
161
|
+
packuments,
|
|
162
|
+
tarballs,
|
|
163
|
+
close: () => new Promise((resolve) => {
|
|
164
|
+
server.close(() => { try { rmSync(dir, { recursive: true, force: true }); } catch { /* 清理失败不影响结论 */ } resolve(); });
|
|
165
|
+
}),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 发布彩排:publish → install → 加载公开面。
|
|
171
|
+
* @param {{dir:string, tag?:string, packageName?:string, entrySpecs?:string[], timeoutMs?:number}} o
|
|
172
|
+
* @returns {Promise<{ok:boolean, steps:object[], failures:string[], packument?:object}>}
|
|
173
|
+
*/
|
|
174
|
+
export async function runLocalPreview({ dir, tag = 'beta', packageName = '', entrySpecs = [], timeoutMs = 300000 } = {}) {
|
|
175
|
+
const steps = [];
|
|
176
|
+
const failures = [];
|
|
177
|
+
const reg = await startLocalRegistry();
|
|
178
|
+
let work = '';
|
|
179
|
+
try {
|
|
180
|
+
// 令牌在运行期随机生成:不在仓库里出现任何"像凭据"的字面量
|
|
181
|
+
const token = `local-${randomBytes(8).toString('hex')}`;
|
|
182
|
+
work = mkdtempSync(join(tmpdir(), 'dshgp-preview-'));
|
|
183
|
+
const npmrc = join(work, '.npmrc');
|
|
184
|
+
writeFileSync(npmrc, `registry=${reg.url}/\n//127.0.0.1:${reg.port}/:_authToken=${token}\n`, 'utf8');
|
|
185
|
+
const common = ['--registry', `${reg.url}/`, '--userconfig', npmrc];
|
|
186
|
+
const env = { ...process.env, npm_config_userconfig: npmrc, npm_config_registry: `${reg.url}/` };
|
|
187
|
+
|
|
188
|
+
const pub = await run('npm', ['publish', ...common, '--tag', tag], { cwd: dir, env, timeoutMs });
|
|
189
|
+
steps.push({ step: 'publish', ok: pub.code === 0 });
|
|
190
|
+
if (pub.code !== 0) { failures.push(`npm publish(本地)失败:${(pub.err || pub.out).slice(-400)}`); return { ok: false, steps, failures }; }
|
|
191
|
+
|
|
192
|
+
const name = packageName || (reg.packuments.keys().next().value || '');
|
|
193
|
+
const doc = reg.packuments.get(name);
|
|
194
|
+
steps.push({ step: 'packument', ok: !!doc, distTags: doc && doc['dist-tags'] });
|
|
195
|
+
if (!doc) { failures.push('本地 registry 没有拿到 packument'); return { ok: false, steps, failures }; }
|
|
196
|
+
|
|
197
|
+
const consumer = join(work, 'consumer');
|
|
198
|
+
mkdirSync(consumer, { recursive: true });
|
|
199
|
+
writeFileSync(join(consumer, 'package.json'), JSON.stringify({ name: 'preview-consumer', private: true, version: '0.0.0' }, null, 2), 'utf8');
|
|
200
|
+
const inst = await run('npm', ['install', `${name}@${tag}`, ...common, '--no-audit', '--no-fund'], { cwd: consumer, env, timeoutMs });
|
|
201
|
+
steps.push({ step: 'install', ok: inst.code === 0 });
|
|
202
|
+
if (inst.code !== 0) { failures.push(`npm install(本地 registry)失败:${(inst.err || inst.out).slice(-400)}`); return { ok: false, steps, failures }; }
|
|
203
|
+
|
|
204
|
+
if (entrySpecs.length) {
|
|
205
|
+
const script = `for (const s of ${JSON.stringify(entrySpecs)}) { await import(s); }\nconsole.log('ok');\n`;
|
|
206
|
+
const probe = await run(process.execPath, ['--input-type=module', '-e', script], { cwd: consumer, env, timeoutMs });
|
|
207
|
+
steps.push({ step: 'import', ok: probe.code === 0, specs: entrySpecs });
|
|
208
|
+
if (probe.code !== 0) failures.push(`从本地 registry 安装后加载失败:${(probe.err || '').slice(0, 400)}`);
|
|
209
|
+
}
|
|
210
|
+
return { ok: failures.length === 0, steps, failures, packument: { name, distTags: doc['dist-tags'], versions: Object.keys(doc.versions || {}) } };
|
|
211
|
+
} catch (e) {
|
|
212
|
+
failures.push(`本地彩排异常:${String(e?.message || e).slice(0, 300)}`);
|
|
213
|
+
return { ok: false, steps, failures };
|
|
214
|
+
} finally {
|
|
215
|
+
await reg.close();
|
|
216
|
+
if (work) { try { rmSync(work, { recursive: true, force: true }); } catch { /* 忽略 */ } }
|
|
217
|
+
}
|
|
218
|
+
}
|
package/lib/npm/publish.js
CHANGED
|
@@ -13,7 +13,8 @@ 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
|
+
import { smokeTestPackage, entrySpecsOf, isBrowserEntry } from './smoke.js';
|
|
17
|
+
import { runLocalPreview } from './local-registry.js';
|
|
17
18
|
|
|
18
19
|
export const PUBLISH_LOG_FILE = 'npm-publish.log';
|
|
19
20
|
export const PUBLISH_TIMEOUT_MS = 300_000;
|
|
@@ -121,6 +122,9 @@ export async function publishPackage({
|
|
|
121
122
|
// 发布前冒烟(默认开):pack → 干净目录装 → import 公开面 → 解析依赖。
|
|
122
123
|
// 干跑只给「会打进哪些文件」,不证明「装上能不能用」——这类问题只能在真装一次时暴露。
|
|
123
124
|
skipSmoke = false,
|
|
125
|
+
// 本地 registry 彩排(默认关):publish → install → 加载 全走真 registry 协议,但不写公网。
|
|
126
|
+
// 比冒烟更接近真实,代价是分钟级;适合发版前手动开一次。
|
|
127
|
+
localPreview = false,
|
|
124
128
|
// 可选:发布成功后等待 registry 可见。默认 false —— 上传成功即为成功(可见延迟是已知现象)
|
|
125
129
|
waitVisible = false,
|
|
126
130
|
} = {}) {
|
|
@@ -164,6 +168,24 @@ export async function publishPackage({
|
|
|
164
168
|
}
|
|
165
169
|
}
|
|
166
170
|
|
|
171
|
+
// 体检⑤:本地 registry 彩排(可选)——真 publish 到本地 registry 再装回来加载。
|
|
172
|
+
let preview = null;
|
|
173
|
+
if (localPreview) {
|
|
174
|
+
const pkgForPreview = JSON.parse(readFileSync(join(info.dir, 'package.json'), 'utf8'));
|
|
175
|
+
const specs = entrySpecsOf(pkgForPreview).filter((sp) => {
|
|
176
|
+
const key = sp === pkgForPreview.name ? '.' : `./${sp.slice(String(pkgForPreview.name).length + 1)}`;
|
|
177
|
+
return !isBrowserEntry(key);
|
|
178
|
+
});
|
|
179
|
+
preview = await runLocalPreview({ dir: info.dir, tag: tg, packageName: info.name, entrySpecs: specs, timeoutMs });
|
|
180
|
+
if (!preview.ok) {
|
|
181
|
+
return {
|
|
182
|
+
ok: false, blocked: 'local-preview-failed', name: info.name, version: info.version,
|
|
183
|
+
error: `本地 registry 彩排不通过:${(preview.failures || []).slice(0, 3).join(';')}`,
|
|
184
|
+
localPreview: preview,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
167
189
|
const rc = writeNpmrc({ workspaceRoot, registry: reg, token });
|
|
168
190
|
if (!rc.ok) return { ok: false, error: rc.error || 'npmrc 写入失败' };
|
|
169
191
|
const args = ['publish', ...npmArgs({ npmrcFile: rc.file, registry: reg }), '--tag', tg];
|
|
@@ -201,6 +223,8 @@ export async function publishPackage({
|
|
|
201
223
|
tarball: pack.filename,
|
|
202
224
|
// 发布前冒烟结果(装了没、公开面能不能 import、依赖闭包全不全)
|
|
203
225
|
smoke: smoke ? { ok: smoke.ok, tgz: smoke.tgz, bytes: smoke.bytes, entries: smoke.entries, deps: smoke.deps, failures: smoke.failures } : null,
|
|
226
|
+
// 本地 registry 彩排结果(publish→install→import 全走真协议,不写公网)
|
|
227
|
+
localPreview: preview ? { ok: preview.ok, steps: preview.steps, packument: preview.packument } : null,
|
|
204
228
|
published: !dryRun && !!r.ok, // 成功判定:npm 上传成功即为成功(不要求 registry 已可见)
|
|
205
229
|
// 附加信息(不影响 published):registry 是否已能看到新版本;发布延迟(几分钟)是已知现象
|
|
206
230
|
visible,
|
package/lib/self/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
export const VERSION = '2.
|
|
12
|
+
export const VERSION = '2.16.0'; // 2.16.0:建仓路径改用 CIFS 安全初始化 + 可选首推。① 抽公共:init-compat 新增 ensureUsableLocalRepo/isUsableRepo —— 判「.git 是否真可用」(含仓库根校验,避免把父仓库误判成本目录的仓库),不可用且残缺则 repair、没有则 create,复用 clone 已在用的 CIFS 降级(手写 .git 绕 chmod);② 建仓:ensureRemoteRepo 用它替代 existsSync('.git') 判据,origin 设置失败**如实报错**(原来会谎报成功);③ 首推:git_remote_create 新增 pushInitial —— 本地无提交时先本地提交(身份用 -c 传入,不写 config),再走已有 Git Data API 上传(transport.pushInitialContent → pushViaApi),CIFS 上全程不需 git remote add/config。历史:2.15.3 本地 registry 彩排。
|
|
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
|
@@ -1,248 +1,37 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* 公网零痕迹(不写 registry.npmjs.org)。
|
|
3
|
+
* 本地 npm registry 的命令行入口(实现在 `lib/npm/local-registry.js`)。
|
|
5
4
|
*
|
|
6
|
-
*
|
|
7
|
-
* 而 verdaccio 是一个需要安装、有自己配置与存储布局的服务。这里只要能回答三件事就够了:
|
|
8
|
-
* ① `npm publish` 能不能成功(含版本冲突检测、dist-tags 写入)
|
|
9
|
-
* ② `npm install <pkg>` 能不能装上(packument + tarball 两个请求)
|
|
10
|
-
* ③ 装完能不能加载(由调用方继续做 import 验证)
|
|
11
|
-
* 零依赖 → 不污染工作区、不需要 node_modules、随仓库走。
|
|
5
|
+
* 用途:把本机当"npm 网站"做**发布彩排**——publish → install → 加载公开面,全程不写公共 registry。
|
|
12
6
|
*
|
|
13
7
|
* 用法:
|
|
14
|
-
* node scripts/local-registry.mjs --
|
|
15
|
-
* node scripts/local-registry.mjs --
|
|
8
|
+
* node scripts/local-registry.mjs --selftest # 一条命令跑完整彩排(推荐)
|
|
9
|
+
* node scripts/local-registry.mjs --port=4873 # 前台起服务(Ctrl-C 结束)
|
|
16
10
|
*
|
|
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 用)
|
|
11
|
+
* 要真发到公网请用 `npm_publish`(含发布前冒烟);这里只做彩排,结束即清理临时目录。
|
|
22
12
|
*/
|
|
23
|
-
import { createServer } from 'node:http';
|
|
24
|
-
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync, existsSync, readdirSync } from 'node:fs';
|
|
25
13
|
import { fileURLToPath } from 'node:url';
|
|
26
|
-
import {
|
|
27
|
-
import { tmpdir } from 'node:os';
|
|
28
|
-
import { gunzipSync } from 'node:zlib';
|
|
29
|
-
import { spawn } from 'node:child_process';
|
|
14
|
+
import { startLocalRegistry, runLocalPreview } from '../lib/npm/local-registry.js';
|
|
30
15
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const packuments = new Map(); // name → { 'dist-tags', versions }
|
|
34
|
-
const tarballs = new Map(); // `${name}/${file}` → Buffer
|
|
35
|
-
mkdirSync(storeDir, { recursive: true });
|
|
16
|
+
const repo = fileURLToPath(new URL('..', import.meta.url));
|
|
17
|
+
const args = process.argv.slice(2);
|
|
36
18
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// npm 可能按配置 gzip 上传
|
|
43
|
-
if (req.headers['content-encoding'] === 'gzip') { try { buf = gunzipSync(buf); } catch { /* 原样尝试 */ } }
|
|
44
|
-
resolve(buf);
|
|
45
|
-
});
|
|
46
|
-
req.on('error', reject);
|
|
19
|
+
if (args.includes('--selftest')) {
|
|
20
|
+
const r = await runLocalPreview({
|
|
21
|
+
dir: repo,
|
|
22
|
+
tag: 'beta',
|
|
23
|
+
entrySpecs: ['dsh-git-push', 'dsh-git-push/audit'],
|
|
47
24
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
}
|
|
25
|
+
for (const s of r.steps) console.log(` ${s.ok ? 'OK ' : 'FAIL'} ${s.step}${s.distTags ? ` → dist-tags ${JSON.stringify(s.distTags)}` : ''}`);
|
|
26
|
+
if (r.failures.length) console.log('失败:\n' + r.failures.map((f) => ` - ${f}`).join('\n'));
|
|
27
|
+
else console.log(`彩排通过(${r.packument?.name}@${r.packument?.distTags?.beta || '?'});临时目录已清理,公网零痕迹`);
|
|
28
|
+
process.exit(r.ok ? 0 : 1);
|
|
29
|
+
} else {
|
|
30
|
+
const portArg = args.find((a) => a.startsWith('--port='));
|
|
31
|
+
const port = Number(portArg ? portArg.split('=')[1] : 4873);
|
|
32
|
+
const reg = await startLocalRegistry({ port });
|
|
33
|
+
console.log(`本地 npm registry: ${reg.url}(Ctrl-C 结束)`);
|
|
34
|
+
const stop = async () => { await reg.close(); process.exit(0); };
|
|
35
|
+
process.on('SIGINT', stop);
|
|
36
|
+
process.on('SIGTERM', stop);
|
|
248
37
|
}
|