dsh-git-push 2.20.19 → 3.1.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/README.md +76 -47
- package/cli-tool.mjs +2 -4
- package/cli.mjs +11 -4
- package/lib/app/apply.js +15 -13
- package/lib/app/command-registry.js +26 -19
- package/lib/app/handlers/http-constants.js +8 -0
- package/lib/app/handlers/meta.js +2 -2
- package/lib/app/handlers/preprocess.js +104 -0
- package/lib/app/handlers/repo-actions.js +2 -3
- package/lib/app/handlers/repos.js +137 -10
- package/lib/app/handlers/settings.js +34 -15
- package/lib/app/http-handlers.js +13 -42
- package/lib/app/index.js +3 -2
- package/lib/app/inject-text.js +6 -21
- package/lib/app/preprocess-client.js +136 -0
- package/lib/app/repo-ownership.js +1 -1
- package/lib/app/schema.js +20 -10
- package/lib/app/settings-bridge.js +60 -8
- package/lib/app/severity.js +14 -0
- package/lib/app/slash-commands.js +5 -164
- package/lib/app/tool-call.js +96 -163
- package/lib/cli/commands-preprocess.mjs +74 -0
- package/lib/cli/commands-tool.mjs +3 -2
- package/lib/cli/commands-vcs.mjs +39 -5
- package/lib/cli/shared.mjs +36 -10
- package/lib/client/index.js +11 -16
- package/lib/client-parts/git/account.js +21 -12
- package/lib/client-parts/git/actions.js +23 -77
- package/lib/client-parts/git/apply.js +8 -2
- package/lib/client-parts/git/browser.js +1 -1
- package/lib/client-parts/git/const.js +33 -1
- package/lib/client-parts/git/controller.js +175 -176
- package/lib/client-parts/git/icons.js +1 -1
- package/lib/client-parts/git/loader.js +1 -1
- package/lib/client-parts/git/npm.js +56 -37
- package/lib/client-parts/git/page.js +53 -1
- package/lib/client-parts/git/repo.js +8 -1
- package/lib/client-parts/git/settings.js +13 -1
- package/lib/client-parts/git/state.js +237 -0
- package/lib/client-parts/git/utils.js +1 -1
- package/lib/client.js +631 -312
- package/lib/commit-push/pre-commit-engine.js +629 -30
- package/lib/commit-push.js +127 -67
- package/lib/context/index.js +7 -5
- package/lib/core/config.js +1 -27
- package/lib/core/exec.js +2 -11
- package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
- package/lib/git/identity-rewrite.js +1 -1
- package/lib/git/ignore.js +1 -1
- package/lib/git/index.js +1 -1
- package/lib/git/push.js +2 -17
- package/lib/git/repo-index.js +8 -3
- package/lib/git/repos.js +2 -2
- package/lib/git/sensitive.js +1 -1
- package/lib/npm/install.js +1 -1
- package/lib/npm/publish.js +20 -5
- package/lib/npm/registry.js +2 -2
- package/lib/plugin/auto-push.js +5 -8
- package/lib/preprocess/providers.js +195 -0
- package/lib/self/index.js +1 -1
- package/lib/skip-dirs.js +14 -55
- package/package.json +31 -42
- package/skills/dev/dsh-git-push.md +9 -9
- package/skills/guide/guide-commit-push.md +11 -13
- package/skills/workflow/README.md +0 -1
- package/skills/workflow/task-completion-report.md +2 -2
- package/.auditignore +0 -30
- package/.gitignore +0 -43
- package/cli-audit.mjs +0 -141
- package/lib/BUGFIX-NOTES-2026-09-14.md +0 -278
- package/lib/app/audit-api.js +0 -173
- package/lib/app/handlers/audit.js +0 -150
- package/lib/app/host-doc-checks.js +0 -25
- package/lib/app/schema-audit.js +0 -42
- package/lib/app/scope-check.js +0 -69
- package/lib/ast/brace.js +0 -120
- package/lib/ast/callgraph.js +0 -135
- package/lib/ast/code-lines.js +0 -175
- package/lib/ast/consts.js +0 -12
- package/lib/ast/control-flow.js +0 -470
- package/lib/ast/credential.js +0 -315
- package/lib/ast/dataflow.js +0 -391
- package/lib/ast/dup-code.js +0 -165
- package/lib/ast/fs-hotspots.js +0 -154
- package/lib/ast/index.js +0 -38
- package/lib/ast/io-risk-const.js +0 -95
- package/lib/ast/io-risk-fn.js +0 -208
- package/lib/ast/io-risk-loop.js +0 -308
- package/lib/ast/io-risk.js +0 -314
- package/lib/ast/lang.js +0 -156
- package/lib/ast/line-count.js +0 -52
- package/lib/ast/magic-number.js +0 -272
- package/lib/ast/naming.js +0 -292
- package/lib/ast/perf.js +0 -488
- package/lib/ast/scope-collisions.js +0 -228
- package/lib/ast/scope.js +0 -241
- package/lib/ast/shell.js +0 -131
- package/lib/ast/size.js +0 -448
- package/lib/ast/symbol-index.js +0 -399
- package/lib/ast/tokenizer.js +0 -421
- package/lib/ast/treesitter.js +0 -413
- package/lib/audit/analysis-coverage.js +0 -98
- package/lib/audit/audit-file.js +0 -205
- package/lib/audit/auditignore-rules.js +0 -148
- package/lib/audit/checks.js +0 -17
- package/lib/audit/client-entry.js +0 -93
- package/lib/audit/collector.js +0 -610
- package/lib/audit/ext-runner.js +0 -80
- package/lib/audit/file-context.js +0 -391
- package/lib/audit/finding.js +0 -101
- package/lib/audit/gitignore-match.js +0 -113
- package/lib/audit/glob.js +0 -63
- package/lib/audit/history-report.js +0 -100
- package/lib/audit/history.js +0 -126
- package/lib/audit/ignore-blind.js +0 -102
- package/lib/audit/index.js +0 -33
- package/lib/audit/orchestrate.js +0 -672
- package/lib/audit/public.js +0 -24
- package/lib/audit/repo-level.js +0 -30
- package/lib/audit/report-yaml.js +0 -90
- package/lib/audit/scope-policy.js +0 -110
- package/lib/audit/slot.js +0 -66
- package/lib/audit-rules/audit-rules-comment.yml +0 -199
- package/lib/audit-rules/audit-rules-cpp.yml +0 -114
- package/lib/audit-rules/audit-rules-csharp.yml +0 -82
- package/lib/audit-rules/audit-rules-docs.yml +0 -32
- package/lib/audit-rules/audit-rules-dsh.yml +0 -177
- package/lib/audit-rules/audit-rules-filehealth.yml +0 -42
- package/lib/audit-rules/audit-rules-folder.yml +0 -99
- package/lib/audit-rules/audit-rules-frontend.yml +0 -335
- package/lib/audit-rules/audit-rules-go.yml +0 -103
- package/lib/audit-rules/audit-rules-i18n.yml +0 -45
- package/lib/audit-rules/audit-rules-java.yml +0 -105
- package/lib/audit-rules/audit-rules-nodejs.yml +0 -662
- package/lib/audit-rules/audit-rules-npm.yml +0 -187
- package/lib/audit-rules/audit-rules-paths.yml +0 -98
- package/lib/audit-rules/audit-rules-performance.yml +0 -245
- package/lib/audit-rules/audit-rules-php.yml +0 -101
- package/lib/audit-rules/audit-rules-private.yml +0 -24
- package/lib/audit-rules/audit-rules-python.yml +0 -106
- package/lib/audit-rules/audit-rules-robustness.yml +0 -31
- package/lib/audit-rules/audit-rules-rust.yml +0 -109
- package/lib/audit-rules/audit-rules-structure.yml +0 -158
- package/lib/audit-rules/audit-rules-swift.yml +0 -99
- package/lib/audit-rules/audit-rules-template.yml +0 -108
- package/lib/audit-ui-sdk/contract.js +0 -117
- package/lib/audit-ui-sdk/index.js +0 -17
- package/lib/checks/button-bind.js +0 -347
- package/lib/checks/common.js +0 -170
- package/lib/checks/credential-file.js +0 -43
- package/lib/checks/dataflow.js +0 -40
- package/lib/checks/dispatch.js +0 -117
- package/lib/checks/dup-code.js +0 -69
- package/lib/checks/dup-const.js +0 -232
- package/lib/checks/file-health.js +0 -150
- package/lib/checks/filter.js +0 -137
- package/lib/checks/folder.js +0 -173
- package/lib/checks/fs-scanner.js +0 -57
- package/lib/checks/hardcoded-path.js +0 -98
- package/lib/checks/index.js +0 -49
- package/lib/checks/io.js +0 -66
- package/lib/checks/magic-number.js +0 -42
- package/lib/checks/npm-json.js +0 -273
- package/lib/checks/private.js +0 -64
- package/lib/checks/regex.js +0 -298
- package/lib/checks/scope-collision.js +0 -68
- package/lib/checks/semantic.js +0 -134
- package/lib/checks/structural.js +0 -281
- package/lib/cli/commands-audit.mjs +0 -142
- package/lib/cli/commands-doc.mjs +0 -127
- package/lib/client-parts/audit/audit.js +0 -301
- package/lib/exempt/tool.js +0 -198
- package/lib/git/module-splitter.js +0 -47
- package/lib/link-check/index.js +0 -187
- package/lib/lint-adapter/detect.js +0 -161
- package/lib/lint-adapter/index.js +0 -50
- package/lib/lint-adapter/runner.js +0 -133
- package/lib/preprocess/audit.js +0 -98
- package/lib/preprocess/index.js +0 -78
- package/lib/rule/compilers/credential.js +0 -62
- package/lib/rule/compilers/dataflow.js +0 -38
- package/lib/rule/compilers/file-health.js +0 -75
- package/lib/rule/compilers/frontend.js +0 -86
- package/lib/rule/compilers/func.js +0 -35
- package/lib/rule/compilers/helpers.js +0 -71
- package/lib/rule/compilers/index.js +0 -29
- package/lib/rule/compilers/link-semantic.js +0 -45
- package/lib/rule/compilers/list-kinds.js +0 -12
- package/lib/rule/compilers/numeric-kinds.js +0 -73
- package/lib/rule/compilers/regex.js +0 -78
- package/lib/rule/compilers/structure.js +0 -107
- package/lib/rule/compilers.js +0 -17
- package/lib/rule/homoglyph.js +0 -45
- package/lib/rule/loader.js +0 -274
- package/lib/rule/registry.js +0 -122
- package/lib/rule/scope.js +0 -67
- package/lib/score/docs-score.js +0 -166
- package/lib/score/index.js +0 -234
- package/lib/user-requirements.json +0 -15
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +0 -34
- package/scripts/audit-ext/dsh-inject-declarations.mjs +0 -209
- package/scripts/audit-ext/variable-min-length.mjs +0 -102
- package/scripts/audit-gate.mjs +0 -235
- package/scripts/audit-runner.mjs +0 -35
- package/scripts/audit-runtime-check.mjs +0 -129
- package/scripts/browser-page-probe.mjs +0 -271
- package/scripts/build-client.mjs +0 -131
- package/scripts/bump-version.mjs +0 -178
- package/scripts/check-js-scope.mjs +0 -87
- package/scripts/check.mjs +0 -203
- package/scripts/clean-date-comments.py +0 -102
- package/scripts/cpu-sample.mjs +0 -188
- package/scripts/doc-drift.mjs +0 -200
- package/scripts/doc-func.mjs +0 -212
- package/scripts/doc-tree.mjs +0 -458
- package/scripts/doc-version.mjs +0 -149
- package/scripts/hot-path-probe-cjs.cjs +0 -71
- package/scripts/hot-path-probe-fs-shim.mjs +0 -28
- package/scripts/hot-path-probe-fsp-shim.mjs +0 -31
- package/scripts/hot-path-probe.mjs +0 -153
- package/scripts/local-registry.mjs +0 -37
- package/scripts/module-splitter.py +0 -800
- package/scripts/picker-repro.mjs +0 -115
- package/scripts/platform-scan.mjs +0 -168
- package/scripts/probe-recheck.mjs +0 -47
- package/scripts/rename-locator.mjs +0 -97
- package/scripts/rule-switch.mjs +0 -97
- package/scripts/rules-solo-audit.mjs +0 -173
- package/scripts/scan-file-io.mjs +0 -717
- package/scripts/scan-fs-hotspots.mjs +0 -107
- package/scripts/scan-version.mjs +0 -125
- package/scripts/symbol-index.mjs +0 -85
- package/scripts/verify-prestep.mjs +0 -100
- package/scripts/version-source.mjs +0 -173
- package/skills/dev/audit-rule-authoring.md +0 -58
- package/skills/guide/guide-audit.md +0 -270
- /package/lib/{audit → app}/ownership.js +0 -0
- /package/lib/{audit-defaults.js → git/defaults.js} +0 -0
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CJS 侧探针通道(配合 scripts/hot-path-probe.mjs 的 ESM loader 钩子;由它 createRequire 载入)。
|
|
3
|
-
*
|
|
4
|
-
* 【为什么需要单独一条通道】Node 对 `format: 'commonjs'` **不采用** loader 钩子返回的源码
|
|
5
|
-
* (实测:同一探针下 ESM 目标计数正常,CJS 目标计数为 0),故 CJS 必须走自己的路:
|
|
6
|
-
* patch `Module._extensions['.js'/'.cjs']`。
|
|
7
|
-
*
|
|
8
|
-
* 【为什么用 _compile 重编译,而不是只包 module.exports】只包 `module.exports` 只能统计
|
|
9
|
-
* 「外部调用」;模块内部自调用(helper 调 helper)统计不到。重编译时在源码**末尾追加**
|
|
10
|
-
* 与 ESM 侧同形的包装,即可同时覆盖两种调用——与 ESM 侧行为对称。
|
|
11
|
-
*
|
|
12
|
-
* 【计数容器】与 ESM 侧共用 `globalThis.__HOT_PATH_PROBE__`(同进程同主线程),
|
|
13
|
-
* 故报告由 ESM 侧的退出钩子统一打印,本文件不重复打印。
|
|
14
|
-
*
|
|
15
|
-
* 【失败兜底】包装失败(源码不可读 / 语法异常等)一律**回退原始加载**并 emitWarning——
|
|
16
|
-
* 探针绝不能让被测程序跑不起来。
|
|
17
|
-
*/
|
|
18
|
-
const Module = require('node:module');
|
|
19
|
-
const fs = require('node:fs');
|
|
20
|
-
const { pathToFileURL } = require('node:url');
|
|
21
|
-
|
|
22
|
-
const cfg = (() => {
|
|
23
|
-
try { return JSON.parse(process.env.HOT_PATH_PROBE || '{}'); } catch { return {}; }
|
|
24
|
-
})();
|
|
25
|
-
const targets = cfg.modules || {};
|
|
26
|
-
|
|
27
|
-
const HEADER = '\nconst __HP = (globalThis.__HOT_PATH_PROBE__ ||= { fn: {}, fs: {} });\n'
|
|
28
|
-
+ 'function __hpRecord(__n, __ms) { const __e = (__HP.fn[__n] ||= { calls: 0, ms: 0 }); __e.calls += 1; __e.ms += __ms; }\n';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 生成与 ESM 侧同形的包装代码。
|
|
32
|
-
* @param {string[]} names 目标函数名
|
|
33
|
-
* @returns {string} 追加到源码末尾的包装片段
|
|
34
|
-
*/
|
|
35
|
-
function suffixFor(names) {
|
|
36
|
-
return names.map((n) => `
|
|
37
|
-
;(() => {
|
|
38
|
-
const __orig = typeof ${n} === 'function' ? ${n} : (module.exports && module.exports.${n});
|
|
39
|
-
if (typeof __orig !== 'function') return;
|
|
40
|
-
const __wrapped = function (...__a) {
|
|
41
|
-
const __t0 = performance.now();
|
|
42
|
-
try { return __orig.apply(this, __a); }
|
|
43
|
-
finally { __hpRecord(${JSON.stringify(n)}, performance.now() - __t0); }
|
|
44
|
-
};
|
|
45
|
-
try { ${n} = __wrapped; } catch { /* 非可变绑定(const 箭头函数等):只兜 exports */ }
|
|
46
|
-
try {
|
|
47
|
-
if (module.exports && module.exports.${n} === __orig) module.exports.${n} = __wrapped;
|
|
48
|
-
} catch { /* 只读导出(getter/冻结)忽略:内部调用已计数 */ }
|
|
49
|
-
})();`).join('');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
for (const ext of ['.js', '.cjs']) {
|
|
53
|
-
const orig = Module._extensions[ext];
|
|
54
|
-
if (typeof orig !== 'function') continue;
|
|
55
|
-
Module._extensions[ext] = function patchedExtension(mod, filename) {
|
|
56
|
-
const names = targets[pathToFileURL(filename).href];
|
|
57
|
-
if (!Array.isArray(names) || !names.length) return orig.call(this, mod, filename);
|
|
58
|
-
let src;
|
|
59
|
-
try {
|
|
60
|
-
src = fs.readFileSync(filename, 'utf8');
|
|
61
|
-
} catch {
|
|
62
|
-
return orig.call(this, mod, filename);
|
|
63
|
-
}
|
|
64
|
-
try {
|
|
65
|
-
mod._compile(src + HEADER + suffixFor(names), filename);
|
|
66
|
-
} catch (e) {
|
|
67
|
-
process.emitWarning(`hot-path-probe-cjs: 包装失败,已回退原始加载(${filename}):${e && e.message}`);
|
|
68
|
-
return orig.call(this, mod, filename);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* node:fs 计数 shim(配合 scripts/hot-path-probe.mjs 的 loader 钩子)。
|
|
3
|
-
*
|
|
4
|
-
* ⚠️ 本文件**只负责 `node:fs`**:`node:fs/promises` 由 hot-path-probe-fsp-shim.mjs 负责。
|
|
5
|
-
* 两者必须分开——两个 builtin 的导出面重叠但不同(如 `access`),
|
|
6
|
-
* 若把两个 `export *` 塞进同一模块会触发 **conflicting star exports** 语法错误
|
|
7
|
-
* (实测:真实审计因此崩掉,本注释即该事故的记录)。
|
|
8
|
-
*
|
|
9
|
-
* 做法:`export *` 透传全部导出(避免漏导出导致运行时报错),
|
|
10
|
-
* 再对需计数的少数函数用显式导出**遮蔽**(ESM 里显式导出优先于 star 导出)。
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import * as fs from 'node:fs';
|
|
14
|
-
|
|
15
|
-
const HP = (globalThis.__HOT_PATH_PROBE__ ||= { fn: {}, fs: {} });
|
|
16
|
-
const hit = (name) => { HP.fs[name] = (HP.fs[name] || 0) + 1; };
|
|
17
|
-
/** 包一层:先计数再转发(保留 this 与返回值语义)。 */
|
|
18
|
-
const wrap = (mod, name) => (...a) => { hit(name); return mod[name](...a); };
|
|
19
|
-
|
|
20
|
-
export * from 'node:fs';
|
|
21
|
-
export const existsSync = wrap(fs, 'existsSync');
|
|
22
|
-
export const readFileSync = wrap(fs, 'readFileSync');
|
|
23
|
-
export const statSync = wrap(fs, 'statSync');
|
|
24
|
-
export const lstatSync = wrap(fs, 'lstatSync');
|
|
25
|
-
export const readdirSync = wrap(fs, 'readdirSync');
|
|
26
|
-
export const writeFileSync = wrap(fs, 'writeFileSync');
|
|
27
|
-
export const mkdirSync = wrap(fs, 'mkdirSync');
|
|
28
|
-
export const realpathSync = wrap(fs, 'realpathSync');
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* node:fs/promises 计数 shim(与 hot-path-probe-fs-shim.mjs 成对;两者必须分开)。
|
|
3
|
-
*
|
|
4
|
-
* 【为什么不能合成一个】`node:fs` 与 `node:fs/promises` 的导出面**重叠但不同**
|
|
5
|
-
* (如 `access` 在两处都存在)⇒ 若把两个 `export *` 放进同一模块,
|
|
6
|
-
* 会出现 **conflicting star exports** 直接报语法错误(实测:真实审计因此崩掉)。
|
|
7
|
-
*
|
|
8
|
-
* 做法同 fs shim:`export *` 透传全部导出(避免漏导出导致运行时报错),
|
|
9
|
-
* 再对需计数的少数函数用显式导出**遮蔽**。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import * as fsp from 'node:fs/promises';
|
|
13
|
-
|
|
14
|
-
const HP = (globalThis.__HOT_PATH_PROBE__ ||= { fn: {}, fs: {} });
|
|
15
|
-
const hit = (name) => { HP.fs[name] = (HP.fs[name] || 0) + 1; };
|
|
16
|
-
/** 包一层:先计数再转发(保留 this 与返回值语义)。 */
|
|
17
|
-
const wrap = (mod, name) => (...a) => { hit(name); return mod[name](...a); };
|
|
18
|
-
|
|
19
|
-
export * from 'node:fs/promises';
|
|
20
|
-
export const readFile = wrap(fsp, 'readFile');
|
|
21
|
-
export const writeFile = wrap(fsp, 'writeFile');
|
|
22
|
-
export const readdir = wrap(fsp, 'readdir');
|
|
23
|
-
export const stat = wrap(fsp, 'stat');
|
|
24
|
-
export const mkdir = wrap(fsp, 'mkdir');
|
|
25
|
-
export const rename = wrap(fsp, 'rename');
|
|
26
|
-
export const unlink = wrap(fsp, 'unlink');
|
|
27
|
-
export const rm = wrap(fsp, 'rm');
|
|
28
|
-
export const access = wrap(fsp, 'access');
|
|
29
|
-
export const symlink = wrap(fsp, 'symlink');
|
|
30
|
-
export const chmod = wrap(fsp, 'chmod');
|
|
31
|
-
export const rmdir = wrap(fsp, 'rmdir');
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 热路径探针(C1):统计指定模块里关键函数的**耗时 + 调用次数**,并统计 fs 调用次数。
|
|
3
|
-
*
|
|
4
|
-
* 【为什么用 loader 钩子】ESM 的导出绑定与模块命名空间对象都是**只读**的,
|
|
5
|
-
* `mod.fn = wrapper` 这类 monkey-patch 对 import 方**无效**(只对 CJS 有效)。
|
|
6
|
-
* 想在 ESM 里量函数耗时/调用次数,只能在**模块加载时**改源码——本文件即该钩子。
|
|
7
|
-
* 注入方式:给目标模块源码**追加**包装代码。ESM 里模块**自己的函数声明绑定是可变的**
|
|
8
|
-
* (只有 `import` 进来的绑定只读),故模块内部 `fn = wrapper` 会被 import 方看到 ✓。
|
|
9
|
-
*
|
|
10
|
-
* 【一个文件两种角色】(Node 的 register 允许钩子与注册器同文件,用线程区分)
|
|
11
|
-
* · 主线程:把自己注册为 loader 钩子,并在退出时打印统计;
|
|
12
|
-
* · 钩子线程:导出 resolve/load/initialize,负责改源码与重定向 node:fs。
|
|
13
|
-
*
|
|
14
|
-
* 【用法】
|
|
15
|
-
* node --import ./scripts/hot-path-probe.mjs <目标脚本或 -e 代码>
|
|
16
|
-
* 目标由环境变量 HOT_PATH_PROBE 指定(JSON):
|
|
17
|
-
* {"modules":{"<模块绝对URL>":["fn1","fn2"]},"fs":true}
|
|
18
|
-
* 便捷入口:`node scripts/hot-path-probe.mjs run -- <命令...>`(本文件自己起子进程并透传环境)。
|
|
19
|
-
*
|
|
20
|
-
* 【计数落在哪】被改写的代码跑在**主线程**,故 `globalThis.__HOT_PATH_PROBE__` 可直接读。
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { isMainThread } from 'node:worker_threads';
|
|
24
|
-
import { register } from 'node:module';
|
|
25
|
-
import { createRequire } from 'node:module';
|
|
26
|
-
|
|
27
|
-
/** 统计容器(主线程可读;钩子线程里也初始化一份,互不干扰)。 */
|
|
28
|
-
const HP = (globalThis.__HOT_PATH_PROBE__ ||= { fn: {}, fs: {} });
|
|
29
|
-
|
|
30
|
-
/** 从环境读取配置(钩子线程同样能读 process.env)。 */
|
|
31
|
-
function readCfg() {
|
|
32
|
-
try {
|
|
33
|
-
return JSON.parse(process.env.HOT_PATH_PROBE || '{}');
|
|
34
|
-
} catch {
|
|
35
|
-
return {};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// ─────────────────────────── 主线程:注册 + 报告 ───────────────────────────
|
|
40
|
-
if (isMainThread) {
|
|
41
|
-
const cfg = readCfg();
|
|
42
|
-
if (!process.env.HOT_PATH_PROBE_ACTIVE) {
|
|
43
|
-
// 作为 --import 预载时:注册钩子(把配置通过 data 传给钩子线程),并在退出时报告。
|
|
44
|
-
process.env.HOT_PATH_PROBE_ACTIVE = '1';
|
|
45
|
-
register(import.meta.url, { data: cfg });
|
|
46
|
-
// 顺带装上 **CJS 通道**:Node 对 format:'commonjs' 不采用 loader 钩子返回的源码(实测计数为 0),
|
|
47
|
-
// 故 CJS 必须 patch Module._extensions;而该 patch 必须在任何 CJS 模块被加载**之前**完成,
|
|
48
|
-
// `--import` 预载正是最早的时机 ⇒ 一个 --import 即可同时覆盖 ESM 与 CJS。
|
|
49
|
-
try {
|
|
50
|
-
createRequire(import.meta.url)('./hot-path-probe-cjs.cjs');
|
|
51
|
-
} catch (e) {
|
|
52
|
-
process.emitWarning(`hot-path-probe: CJS 通道未装上(${e && e.message})——CJS 目标将不会被计数`);
|
|
53
|
-
}
|
|
54
|
-
process.on('exit', () => report());
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 打印统计:函数按累计耗时降序(耗时 + 次数 + 均值),另列 fs 调用次数。
|
|
59
|
-
* 用途:区分「CPU 重复计算」(某函数耗时高、次数多)与「I/O 等待」(fs 次数高)。
|
|
60
|
-
*/
|
|
61
|
-
function report() {
|
|
62
|
-
const fn = Object.entries(HP.fn || {});
|
|
63
|
-
const fs = Object.entries(HP.fs || {});
|
|
64
|
-
if (!fn.length && !fs.length) return;
|
|
65
|
-
const lines = ['', '── 热路径探针 ──'];
|
|
66
|
-
if (fn.length) {
|
|
67
|
-
lines.push('函数(按累计耗时降序):');
|
|
68
|
-
for (const [name, s] of fn.sort((a, b) => b[1].ms - a[1].ms)) {
|
|
69
|
-
lines.push(` ${name.padEnd(28)} 调用 ${String(s.calls).padStart(7)} 次|累计 ${s.ms.toFixed(1).padStart(9)} ms|均值 ${(s.ms / s.calls).toFixed(3)} ms`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (fs.length) {
|
|
73
|
-
lines.push('fs 调用(按次数降序):');
|
|
74
|
-
for (const [name, n] of fs.sort((a, b) => b[1] - a[1])) lines.push(` ${name.padEnd(28)} ${String(n).padStart(7)} 次`);
|
|
75
|
-
}
|
|
76
|
-
process.stderr.write(`${lines.join('\n')}\n`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ─────────────────────────── 钩子线程:resolve / load ───────────────────────────
|
|
81
|
-
/** fs shim 的 URL:`node:fs` 与 `node:fs/promises` **必须分开两个 shim** —— 两者导出面重叠但不同
|
|
82
|
-
* (如 access),塞进同一模块会触发 conflicting star exports 语法错误(实测真实审计因此崩掉)。 */
|
|
83
|
-
const SHIM_FS = new URL('./hot-path-probe-fs-shim.mjs', import.meta.url).href;
|
|
84
|
-
const SHIM_FSP = new URL('./hot-path-probe-fsp-shim.mjs', import.meta.url).href;
|
|
85
|
-
|
|
86
|
-
let hookCfg = {};
|
|
87
|
-
/** Node 在钩子线程启动时调用,data 即主线程 register 传的配置。 */
|
|
88
|
-
export function initialize(data) {
|
|
89
|
-
hookCfg = data || {};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** 重定向 fs 到对应 shim(用于统计 I/O 次数)。 */
|
|
93
|
-
export async function resolve(specifier, context, next) {
|
|
94
|
-
// ⚠️ 必须放行 **shim 自己**对 builtin 的 import:否则会把 shim 重定向到 shim,成环且静默埋雷
|
|
95
|
-
// (实测:负载不碰 fs 时看不出问题,一旦真用 fs 就会炸)。
|
|
96
|
-
const fromShim = context?.parentURL === SHIM_FS || context?.parentURL === SHIM_FSP;
|
|
97
|
-
if (!fromShim && hookCfg.fs !== false) {
|
|
98
|
-
if (specifier === 'node:fs') return { url: SHIM_FS, format: 'module', shortCircuit: true };
|
|
99
|
-
if (specifier === 'node:fs/promises') return { url: SHIM_FSP, format: 'module', shortCircuit: true };
|
|
100
|
-
}
|
|
101
|
-
return next(specifier, context);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** 给目标模块源码追加「耗时 + 次数」包装(**ESM 与 CJS 自动判断**,用 loader 给的 format,不猜)。 */
|
|
105
|
-
export async function load(url, context, next) {
|
|
106
|
-
const r = await next(url, context);
|
|
107
|
-
const names = (hookCfg.modules || {})[url];
|
|
108
|
-
if (!Array.isArray(names) || !names.length) return r;
|
|
109
|
-
// format 由 Node 给出:'module' = ESM;'commonjs'(含 commonjs-typescript)= CJS;
|
|
110
|
-
// 其余(builtin / json / wasm 等)不处理——既不该改,也不适用下面两种包装。
|
|
111
|
-
const isEsm = r.format === 'module';
|
|
112
|
-
const isCjs = r.format === 'commonjs' || r.format === 'commonjs-typescript';
|
|
113
|
-
if (!isEsm && !isCjs) return r;
|
|
114
|
-
let src = r.source;
|
|
115
|
-
if (src && typeof src !== 'string') src = Buffer.from(src).toString('utf8');
|
|
116
|
-
if (typeof src !== 'string') return r;
|
|
117
|
-
|
|
118
|
-
// 计数容器 + 上报函数(两种格式共用;代码最终跑在主线程,故 globalThis 直接可读)
|
|
119
|
-
const header = '\nconst __HP = (globalThis.__HOT_PATH_PROBE__ ||= { fn: {}, fs: {} });\n'
|
|
120
|
-
+ 'function __hpRecord(__n, __ms) { const __e = (__HP.fn[__n] ||= { calls: 0, ms: 0 }); __e.calls += 1; __e.ms += __ms; }\n';
|
|
121
|
-
|
|
122
|
-
// ESM:模块**自己的函数声明绑定可变**(只有 import 进来的绑定只读)⇒ 模块内重赋值 import 方可见。
|
|
123
|
-
const esmSuffix = names.map((n) => `
|
|
124
|
-
;(() => {
|
|
125
|
-
const __orig = ${n};
|
|
126
|
-
if (typeof __orig !== 'function') return;
|
|
127
|
-
${n} = function (...__a) {
|
|
128
|
-
const __t0 = performance.now();
|
|
129
|
-
try { return __orig.apply(this, __a); }
|
|
130
|
-
finally { __hpRecord(${JSON.stringify(n)}, performance.now() - __t0); }
|
|
131
|
-
};
|
|
132
|
-
})();`).join('');
|
|
133
|
-
|
|
134
|
-
// CJS:同样重赋值本地函数声明(覆盖**内部调用**),并额外替换 `module.exports` 上**已导出**的引用
|
|
135
|
-
// —— CJS 常见 `module.exports = { foo }` 写在文件末尾,而本段代码追加在最后,
|
|
136
|
-
// 此时导出对象已捕获旧引用,故两处都要换。
|
|
137
|
-
const cjsSuffix = names.map((n) => `
|
|
138
|
-
;(() => {
|
|
139
|
-
const __orig = ${n};
|
|
140
|
-
if (typeof __orig !== 'function') return;
|
|
141
|
-
const __wrapped = function (...__a) {
|
|
142
|
-
const __t0 = performance.now();
|
|
143
|
-
try { return __orig.apply(this, __a); }
|
|
144
|
-
finally { __hpRecord(${JSON.stringify(n)}, performance.now() - __t0); }
|
|
145
|
-
};
|
|
146
|
-
${n} = __wrapped;
|
|
147
|
-
try {
|
|
148
|
-
if (typeof module !== 'undefined' && module.exports && module.exports.${n} === __orig) module.exports.${n} = __wrapped;
|
|
149
|
-
} catch { /* 只读导出(getter/冻结)忽略:内部调用已计数 */ }
|
|
150
|
-
})();`).join('');
|
|
151
|
-
|
|
152
|
-
return { ...r, source: src + header + (isEsm ? esmSuffix : cjsSuffix) };
|
|
153
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* 本地 npm registry 的命令行入口(实现在 `lib/npm/local-registry.js`)。
|
|
4
|
-
*
|
|
5
|
-
* 用途:把本机当"npm 网站"做**发布彩排**——publish → install → 加载公开面,全程不写公共 registry。
|
|
6
|
-
*
|
|
7
|
-
* 用法:
|
|
8
|
-
* node scripts/local-registry.mjs --selftest # 一条命令跑完整彩排(推荐)
|
|
9
|
-
* node scripts/local-registry.mjs --port=4873 # 前台起服务(Ctrl-C 结束)
|
|
10
|
-
*
|
|
11
|
-
* 要真发到公网请用 `npm_publish`(含发布前冒烟);这里只做彩排,结束即清理临时目录。
|
|
12
|
-
*/
|
|
13
|
-
import { fileURLToPath } from 'node:url';
|
|
14
|
-
import { startLocalRegistry, runLocalPreview } from '../lib/npm/local-registry.js';
|
|
15
|
-
|
|
16
|
-
const repo = fileURLToPath(new URL('..', import.meta.url));
|
|
17
|
-
const args = process.argv.slice(2);
|
|
18
|
-
|
|
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'],
|
|
24
|
-
});
|
|
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);
|
|
37
|
-
}
|