@kezh/my-custom-js-converter 1.9.0 → 1.11.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.
@@ -97,23 +97,22 @@ function decoding$0(inPath, outPath, config, settingDir, libDirs, conflicts) {
97
97
  mapContext.writeToLocalDir(settingDir);
98
98
  }
99
99
  // 稽核内联工具方法体是否与源文件一致(忽略注释)
100
- // 必须在 importMods 之前:有不一致方法的依赖不生成 require(方案B:自包含)
100
+ // 必须在 importMods/deleteModMethods 之前:若存在任一不一致方法,
101
+ // 则 decoded.js 原样包裹 coded.js(不 require、不删除内联方法、不还原调用)
101
102
  const mismatches = (0, ast_1.auditModMethods)(srcAst, mapContext);
102
- const skipDepNames = new Set();
103
103
  if (mismatches.length) {
104
- // 推导出有不一致方法的依赖名集合:该依赖所有方法都保留内联,不 require
105
- mismatches.forEach((m) => skipDepNames.add(m.modName));
106
- logger.warn(`检测到 ${mismatches.length} 个内联工具方法与源文件不一致,涉及依赖: ${[
107
- ...skipDepNames
108
- ].join(", ")}`);
109
- logger.warn(`以下依赖的所有方法将保留内联形态不还原(不生成 require,decoded.js 自包含):`);
104
+ logger.warn(`检测到 ${mismatches.length} 个内联工具方法与源文件不一致,decoded.js 将原样保留 coded.js(不还原、不 require):`);
110
105
  mismatches.forEach((m) => {
111
106
  logger.warn(` - ${m.methodKey}(源文件: ${m.srcPath})`);
112
107
  });
113
108
  logger.warn(`请检查上述方法,确认 coded.js 与源文件差异后手动处理或重新 encode。`);
109
+ // 跳过 importMods 和 deleteModMethods:保留 coded.js 原样形态
110
+ }
111
+ else {
112
+ // 全部一致:正常还原(生成 require、删除内联方法、还原调用)
113
+ (0, ast_1.importMods)(path_1.default.dirname(outPath), srcAst, mapContext, config.encode.useAlias);
114
+ (0, ast_1.deleteModMethods)(srcAst, mapContext);
114
115
  }
115
- (0, ast_1.importMods)(path_1.default.dirname(outPath), srcAst, mapContext, config.encode.useAlias, skipDepNames);
116
- (0, ast_1.deleteModMethods)(srcAst, mapContext, skipDepNames);
117
116
  // 从onFormReady方法中提取版本日志并移除
118
117
  extractAndRemoveVersionLog(srcAst, settingDir);
119
118
  let { code: dist } = (0, generator_1.default)(srcAst, { compact: true });
@@ -119,16 +119,23 @@ function reorderMethods(methods) {
119
119
  }
120
120
  /**
121
121
  * 防护检测:源文件 mount 对象内若残留 `__mod` 后缀方法,且同时通过 require 引入了
122
- * 工具方法依赖,则禁止编码。
122
+ * 工具方法依赖,则按场景区分处理:
123
123
  *
124
- * 场景:用户把 decode 产物(含未还原的 `validate__util` 等内联方法)复制为 index.js
125
- * 进行二开,同时又保留了 `const util = require(...)`。此时若继续 encode,会导致
126
- * 方法重复定义、mod.map 错乱。应提示用户移除 require 引入,只做无依赖编码。
124
+ * 场景 A(真篡改风险):`__mod` 后缀方法对应的 mod 名能在 libs 目录找到独立模块文件。
125
+ * 说明用户复用了 decode 产物(含未还原的 `validate__util` 等内联 lib 方法)作为二开源文件,
126
+ * 同时又保留了 `const util = require(...)`。此时若继续 encode,会导致方法重复定义、
127
+ * mod.map 错乱。应拒绝编码,提示用户移除 require 引入或抽为独立工具文件。
128
+ *
129
+ * 场景 B(用户自带 `__xxx` 命名,非 lib 模块):所有 `__mod` 后缀方法对应的 mod 名
130
+ * 在 libs 目录均无匹配文件(如 `__getTable`、`__datetimeFormatter` 是用户私有命名,
131
+ * 非 lib 内模块)。此场景不会导致 lib 方法对原方法覆盖,encode 应放行编译,仅给出警告
132
+ * 提示(与 decode 的「未找到依赖文件」提示语义一致)。
127
133
  *
128
134
  * @param inPath - 源文件路径
129
135
  * @param entry - 入口对象名
130
136
  * @param config - 主配置
131
- * @returns 错误提示字符串(检测到连规时返回,未连规返回 null)
137
+ * @returns 错误提示字符串(场景 A 时返回,阻止编码);场景 B 时返回 null 并已打印警告;
138
+ * 无违规返回 null
132
139
  */
133
140
  function guardInlinedModMethods(inPath, entry, config) {
134
141
  const srcAst = (0, ast_1.parseSrcAst)(inPath);
@@ -149,7 +156,38 @@ function guardInlinedModMethods(inPath, entry, config) {
149
156
  return null;
150
157
  }
151
158
  const depNames = [...requireModPaths.keys()];
159
+ // 解析每个内联后缀方法名对应的 mod 名(取 split("__") 最后一段,与
160
+ // getUnknownDepNames / deleteModMethods 的解析逻辑保持一致)。
161
+ const modNames = new Set();
162
+ inlinedNames.forEach((name) => {
163
+ const split = name.split("__");
164
+ if (split.length >= 2) {
165
+ const modName = split[split.length - 1];
166
+ if (modName) {
167
+ modNames.add(modName);
168
+ }
169
+ }
170
+ });
171
+ // 在 libs 目录搜索每个 mod 名是否存在独立模块文件,据此区分场景 A / B。
172
+ // 复用 decode 侧同一套 resolveLibDirs + scanfLibMod,保证两端判定一致。
173
+ const libDirs = (0, scanf_1.resolveLibDirs)(config.libs, config.baseDir, rootDir);
174
+ const libMods = []; // 在 libs 有匹配的 mod 名(场景 A)
175
+ modNames.forEach((modName) => {
176
+ const candidates = (0, scanf_1.scanfLibMod)(libDirs, modName);
177
+ if (candidates.length) {
178
+ libMods.push(modName);
179
+ }
180
+ });
181
+ // 场景 B:所有疑似后缀方法对应的 mod 名在 libs 均无匹配(非 lib 模块,多为用户自带
182
+ // `__xxx` 命名)。不会导致 lib 方法覆盖原方法,放行编译,仅警告提示。
183
+ if (!libMods.length) {
184
+ logger.warn(`源文件 mount 对象内存在疑似 __mod 后缀方法(${inlinedNames.join(", ")}),但对应模块在 libs 目录均无匹配,判定为非 lib 模块命名,放行编码。` +
185
+ `同时存在 require 依赖引入(${depNames.join(", ")})。若上述方法确为 lib 内联方法,请确认 libs 配置或手动处理。`);
186
+ return null;
187
+ }
188
+ // 场景 A:存在能在 libs 找到独立模块文件的内联后缀方法(真篡改风险),拒绝编码。
152
189
  return (`检测到源文件 mount 对象内残留工具方法后缀(${inlinedNames.join(", ")}),` +
190
+ `其中 ${libMods.join(", ")} 在 libs 目录存在对应模块文件,` +
153
191
  `同时存在 require 依赖引入(${depNames.join(", ")})。\n` +
154
192
  `此场景不允许编码:请移除源文件中的 require 工具方法引入(${depNames
155
193
  .map((n) => `const ${n} = require(...)`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kezh/my-custom-js-converter",
3
- "version": "1.9.0",
3
+ "version": "1.11.0",
4
4
  "description": "自定义代码转换器",
5
5
  "main": "index.js",
6
6
  "files": [