mm_validator 1.0.1 → 1.0.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.
@@ -1,73 +0,0 @@
1
- /**
2
- * Basic验证器
3
- * 验证HTTP Basic认证
4
- */
5
-
6
- async function main(params, ctx, options) {
7
- const action = params.action;
8
- const username = params.username || '';
9
- const password = params.password || '';
10
- const encoded = params.encoded || '';
11
-
12
- switch (action) {
13
- case 'encode': {
14
- const combined = username + ':' + password;
15
- const base64 = Buffer.from(combined).toString('base64');
16
- ctx.output = { valid: true, tip: '参数校验通过', errors: [] };
17
- return ctx.output;
18
- }
19
- case 'decode': {
20
- const decoded = Buffer.from(encoded, 'base64').toString();
21
- ctx.output = { valid: true, tip: '参数校验通过', errors: [] };
22
- return ctx.output;
23
- }
24
- case 'verify': {
25
- let verified = false;
26
- const base64 = params.base64 || '';
27
-
28
- // 如果提供了base64,解码后比对
29
- if (base64) {
30
- let auth_value = base64;
31
- // 去掉 'Basic ' 前缀
32
- if (auth_value.indexOf('Basic ') === 0) {
33
- auth_value = auth_value.substring(6);
34
- }
35
- const decoded = Buffer.from(auth_value, 'base64').toString();
36
- const parts = decoded.split(':');
37
- const decoded_username = parts[0] || '';
38
- const decoded_password = parts.slice(1).join(':');
39
-
40
- if (decoded_username === username && decoded_password === password) {
41
- verified = true;
42
- }
43
- } else {
44
- // 没有base64时,比对expected_password或直接比对password
45
- const expected_password = params.expected_password;
46
- if (expected_password !== undefined) {
47
- verified = password === expected_password;
48
- } else {
49
- // 至少有一个凭证就被认为是有效的
50
- verified = !!(username && password);
51
- }
52
- }
53
-
54
- var tip = verified ? '参数校验通过' : '1个参数校验失败: 用户名或密码不匹配';
55
- ctx.output = {
56
- valid: verified,
57
- tip: tip,
58
- errors: verified ? [] : ['用户名或密码不匹配']
59
- };
60
- return ctx.output;
61
- }
62
- default: {
63
- ctx.output = {
64
- valid: false,
65
- tip: '1个参数校验失败: 不支持的操作类型: ' + action,
66
- errors: ['不支持的操作类型: ' + action]
67
- };
68
- return ctx.output;
69
- }
70
- }
71
- }
72
-
73
- module.exports = { main };
@@ -1,462 +0,0 @@
1
- /**
2
- * 代码格式验证器
3
- * 验证代码语法和格式规范,返回问题列表
4
- * @param {object} ctx 上下文对象
5
- * @param {object} params 步骤参数
6
- */
7
- async function main(params, ctx, options) {
8
- var code = params.code || '';
9
- var language = params.language || 'javascript';
10
- var rules =
11
- params.rules ||
12
- [
13
- 'naming',
14
- 'semicolon',
15
- 'jsdoc',
16
- 'boolean_name',
17
- 'long_function',
18
- 'event_handler',
19
- 'try_catch',
20
- 'tab_indent',
21
- 'double_quote',
22
- 'var_decl',
23
- 'line_length'
24
- ];
25
-
26
- if (!code) {
27
- ctx.output = {
28
- error: { code: 'VAL_CODE_PARAM', message: '缺少代码内容(code)' }
29
- };
30
- return ctx.output;
31
- }
32
-
33
- var errors = [];
34
- var lines = code.split(/\r?\n/);
35
-
36
- // 语法检查
37
- if (language === 'javascript' || language === 'js') {
38
- try {
39
- new Function(code);
40
- } catch (e) {
41
- errors.push({
42
- rule: 'syntax',
43
- severity: 'error',
44
- line: 0,
45
- message: '语法错误: ' + e.message
46
- });
47
- }
48
- }
49
-
50
- // 检查 Tab 缩进
51
- if (rules.indexOf('tab_indent') !== -1) {
52
- for (var i = 0; i < lines.length; i++) {
53
- if (lines[i].indexOf('\t') !== -1) {
54
- errors.push({
55
- rule: 'tab_indent',
56
- severity: 'warning',
57
- line: i + 1,
58
- message: '使用了 Tab 缩进,应使用 2 个空格'
59
- });
60
- }
61
- }
62
- }
63
-
64
- // 检查双引号字符串
65
- if (rules.indexOf('double_quote') !== -1) {
66
- var dq_re = /"([^"\\\n]|\\.)*"/g;
67
- var dq_match;
68
- while ((dq_match = dq_re.exec(code)) !== null) {
69
- errors.push({
70
- rule: 'double_quote',
71
- severity: 'warning',
72
- line: _findLine(lines, dq_match.index),
73
- message: '使用了双引号字符串,应使用单引号'
74
- });
75
- }
76
- }
77
-
78
- // 检查 var 声明
79
- if (rules.indexOf('var_decl') !== -1) {
80
- var var_re = /\bvar\s+[a-zA-Z_$]/g;
81
- var var_match;
82
- while ((var_match = var_re.exec(code)) !== null) {
83
- errors.push({
84
- rule: 'var_decl',
85
- severity: 'warning',
86
- line: _findLine(lines, var_match.index),
87
- message: '使用了 var 声明,应使用 let 或 const'
88
- });
89
- }
90
- }
91
-
92
- // 检查命名规范
93
- if (rules.indexOf('naming') !== -1) {
94
- _checkNaming(code, lines, errors);
95
- }
96
-
97
- // 检查分号
98
- if (rules.indexOf('semicolon') !== -1) {
99
- _checkSemicolon(lines, errors);
100
- }
101
-
102
- // 检查 JSDoc 注释
103
- if (rules.indexOf('jsdoc') !== -1) {
104
- _checkJsdoc(lines, errors);
105
- }
106
-
107
- // 检查布尔变量命名
108
- if (rules.indexOf('boolean_name') !== -1) {
109
- _checkBooleanName(code, lines, errors);
110
- }
111
-
112
- // 检查函数长度
113
- if (rules.indexOf('long_function') !== -1) {
114
- _checkLongFunction(lines, errors);
115
- }
116
-
117
- // 检查事件处理函数命名
118
- if (rules.indexOf('event_handler') !== -1) {
119
- _checkEventHandler(code, lines, errors);
120
- }
121
-
122
- // 检查错误处理
123
- if (rules.indexOf('try_catch') !== -1) {
124
- _checkTryCatch(lines, errors);
125
- }
126
-
127
- // 检查行长度
128
- if (rules.indexOf('line_length') !== -1) {
129
- for (var j = 0; j < lines.length; j++) {
130
- if (lines[j].length > 120) {
131
- errors.push({
132
- rule: 'line_length',
133
- severity: 'warning',
134
- line: j + 1,
135
- message: '行长度超过 120 个字符'
136
- });
137
- }
138
- }
139
- }
140
-
141
- var valid = errors.length === 0;
142
- ctx.output = {
143
- valid: valid,
144
- tip: valid ? '代码验证通过' : '发现 ' + errors.length + ' 个问题',
145
- detail: _stringifyErrors(errors),
146
- errors: errors,
147
- error_count: errors.length,
148
- line_count: lines.length
149
- };
150
- return ctx.output;
151
- }
152
-
153
- /**
154
- * 将问题列表字符串化,供 LLM 精准修复时反馈
155
- * @param {Array} errors 问题列表
156
- * @returns {string} 字符串化的问题详情,无问题时返回空字符串
157
- */
158
- function _stringifyErrors(errors) {
159
- if (!errors || errors.length === 0) return '';
160
- var list = [];
161
- for (var i = 0; i < errors.length; i++) {
162
- var e = errors[i];
163
- var loc = e.line > 0 ? '第' + e.line + '行 ' : '';
164
- list.push(loc + '[' + e.rule + '] ' + e.message);
165
- }
166
- return list.join('\n');
167
- }
168
-
169
- /**
170
- * 检查命名规范
171
- * @param {string} code 代码内容
172
- * @param {Array} lines 行数组
173
- * @param {Array} errors 问题列表
174
- */
175
- function _checkNaming(code, lines, errors) {
176
- // 函数名:小驼峰(检测蛇形或大写开头)
177
- var fn_re = /(?:async\s+)?function\s+([a-zA-Z_$][\w$]*)\s*\(/g;
178
- var fn_match;
179
- while ((fn_match = fn_re.exec(code)) !== null) {
180
- var fn_name = fn_match[1];
181
- if (fn_name.indexOf('_') !== -1 || /^[A-Z]/.test(fn_name)) {
182
- errors.push({
183
- rule: 'naming',
184
- severity: 'warning',
185
- line: _findLine(lines, fn_match.index),
186
- message: '函数名 "' + fn_name + '" 应使用小驼峰命名'
187
- });
188
- }
189
- }
190
-
191
- // 变量名:小写蛇形(检测含大写字母)
192
- var lv_re = /\b(?:let|var)\s+([a-zA-Z_$][\w$]*)\s*=/g;
193
- var lv_match;
194
- while ((lv_match = lv_re.exec(code)) !== null) {
195
- var lv_name = lv_match[1];
196
- if (/[A-Z]/.test(lv_name)) {
197
- errors.push({
198
- rule: 'naming',
199
- severity: 'warning',
200
- line: _findLine(lines, lv_match.index),
201
- message: '变量名 "' + lv_name + '" 应使用小写蛇形命名'
202
- });
203
- }
204
- }
205
-
206
- // 类名:大驼峰(检测小写开头或蛇形)
207
- var cls_re = /class\s+([a-zA-Z_$][\w$]*)/g;
208
- var cls_match;
209
- while ((cls_match = cls_re.exec(code)) !== null) {
210
- var cls_name = cls_match[1];
211
- if (/^[a-z]/.test(cls_name) || cls_name.indexOf('_') !== -1) {
212
- errors.push({
213
- rule: 'naming',
214
- severity: 'warning',
215
- line: _findLine(lines, cls_match.index),
216
- message: '类名 "' + cls_name + '" 应使用大驼峰命名'
217
- });
218
- }
219
- }
220
-
221
- // 常量名:大写蛇形(检测 const 小写开头且赋值为基本字面量)
222
- var const_re = /const\s+([a-z][\w$]*)\s*=\s*(?:"[^"]*"|'[^']*'|\d+\.?\d*|true|false|null)\b/g;
223
- var const_match;
224
- while ((const_match = const_re.exec(code)) !== null) {
225
- var const_name = const_match[1];
226
- errors.push({
227
- rule: 'naming',
228
- severity: 'warning',
229
- line: _findLine(lines, const_match.index),
230
- message: '常量名 "' + const_name + '" 应使用大写蛇形命名'
231
- });
232
- }
233
- }
234
-
235
- /**
236
- * 检查分号
237
- * @param {Array} lines 行数组
238
- * @param {Array} errors 问题列表
239
- */
240
- function _checkSemicolon(lines, errors) {
241
- var cont_re = /[\(\)\,\[\]\.\:\+\-\*\/\=\<\>\!\?\&\|\}]$/;
242
- var stmt_re = /^(let|const|var)\s+[a-zA-Z_$][\w$]*\s*=|^[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*\s*[=(]/;
243
-
244
- for (var i = 0; i < lines.length; i++) {
245
- var line = lines[i].trim();
246
- if (!line) continue;
247
- if (line.startsWith('//') || line.startsWith('/*') || line.startsWith('*')) continue;
248
- if (line.endsWith(';') || line.endsWith('{') || line.endsWith('}')) continue;
249
- if (cont_re.test(line)) continue;
250
-
251
- if (stmt_re.test(line) || /^return\s+\S/.test(line) || /^throw\s+\S/.test(line)) {
252
- errors.push({
253
- rule: 'semicolon',
254
- severity: 'warning',
255
- line: i + 1,
256
- message: '语句末尾缺少分号'
257
- });
258
- }
259
- }
260
- }
261
-
262
- /**
263
- * 检查 JSDoc 注释
264
- * @param {Array} lines 行数组
265
- * @param {Array} errors 问题列表
266
- */
267
- function _checkJsdoc(lines, errors) {
268
- var fn_re = /(?:async\s+)?function\s+[a-zA-Z_$][\w$]*\s*\(/;
269
- for (var i = 0; i < lines.length; i++) {
270
- var line = lines[i].trim();
271
- if (!fn_re.test(line)) continue;
272
-
273
- var has_jsdoc = false;
274
- for (var j = i - 1; j >= 0; j--) {
275
- var prev = lines[j].trim();
276
- if (!prev) continue;
277
- if (prev.startsWith('/**')) {
278
- has_jsdoc = true;
279
- break;
280
- }
281
- if (prev.startsWith('*')) continue;
282
- break;
283
- }
284
-
285
- if (!has_jsdoc) {
286
- errors.push({
287
- rule: 'jsdoc',
288
- severity: 'warning',
289
- line: i + 1,
290
- message: '函数缺少 JSDoc 注释'
291
- });
292
- }
293
- }
294
- }
295
-
296
- /**
297
- * 检查布尔变量命名
298
- * @param {string} code 代码内容
299
- * @param {Array} lines 行数组
300
- * @param {Array} errors 问题列表
301
- */
302
- function _checkBooleanName(code, lines, errors) {
303
- var bool_re = /\b(?:let|const|var)\s+([a-zA-Z_$][\w$]*)\s*=\s*(?:true|false)\b/g;
304
- var bool_match;
305
- while ((bool_match = bool_re.exec(code)) !== null) {
306
- var bool_name = bool_match[1];
307
- if (!/^(is|has|can)([A-Z_]|$)/.test(bool_name)) {
308
- errors.push({
309
- rule: 'boolean_name',
310
- severity: 'warning',
311
- line: _findLine(lines, bool_match.index),
312
- message: '布尔变量名 "' + bool_name + '" 应以 is/has/can 开头'
313
- });
314
- }
315
- }
316
- }
317
-
318
- /**
319
- * 检查函数长度
320
- * @param {Array} lines 行数组
321
- * @param {Array} errors 问题列表
322
- */
323
- function _checkLongFunction(lines, errors) {
324
- var fn_re = /(?:async\s+)?function\s+[a-zA-Z_$][\w$]*\s*\([^)]*\)\s*\{/;
325
- for (var i = 0; i < lines.length; i++) {
326
- var line = lines[i].trim();
327
- if (!fn_re.test(line)) continue;
328
-
329
- var depth = _countBraces(line);
330
- var end_idx = i;
331
- if (depth > 0) {
332
- for (var j = i + 1; j < lines.length; j++) {
333
- depth += _countBraces(lines[j]);
334
- end_idx = j;
335
- if (depth <= 0) break;
336
- }
337
- }
338
-
339
- var fn_lines = end_idx - i + 1;
340
- if (fn_lines > 50) {
341
- errors.push({
342
- rule: 'long_function',
343
- severity: 'warning',
344
- line: i + 1,
345
- message: '函数体 ' + fn_lines + ' 行,超过 50 行限制'
346
- });
347
- }
348
- }
349
- }
350
-
351
- /**
352
- * 检查事件处理函数命名
353
- * @param {string} code 代码内容
354
- * @param {Array} lines 行数组
355
- * @param {Array} errors 问题列表
356
- */
357
- function _checkEventHandler(code, lines, errors) {
358
- var add_re = /addEventListener\s*\(\s*['"][^'"]*['"]\s*,\s*([a-zA-Z_$][\w$]*)/g;
359
- var add_match;
360
- while ((add_match = add_re.exec(code)) !== null) {
361
- _checkHandlerName(add_match[1], add_match.index, lines, errors);
362
- }
363
-
364
- var on_re = /\.on[a-zA-Z]+\s*=\s*([a-zA-Z_$][\w$]*)/g;
365
- var on_match;
366
- while ((on_match = on_re.exec(code)) !== null) {
367
- _checkHandlerName(on_match[1], on_match.index, lines, errors);
368
- }
369
- }
370
-
371
- /**
372
- * 检查单个事件处理函数名是否以 handle 开头
373
- * @param {string} handler 处理函数名
374
- * @param {number} offset 字符偏移
375
- * @param {Array} lines 行数组
376
- * @param {Array} errors 问题列表
377
- */
378
- function _checkHandlerName(handler, offset, lines, errors) {
379
- if (handler === 'function' || handler === 'async') return;
380
- if (!/^handle/.test(handler)) {
381
- errors.push({
382
- rule: 'event_handler',
383
- severity: 'warning',
384
- line: _findLine(lines, offset),
385
- message: '事件处理函数 "' + handler + '" 应以 handle 开头'
386
- });
387
- }
388
- }
389
-
390
- /**
391
- * 检查错误处理
392
- * @param {Array} lines 行数组
393
- * @param {Array} errors 问题列表
394
- */
395
- function _checkTryCatch(lines, errors) {
396
- var await_re = /await\s+(?:fetch|axios)/;
397
- for (var i = 0; i < lines.length; i++) {
398
- var line = lines[i].trim();
399
- if (!await_re.test(line)) continue;
400
-
401
- var in_try = false;
402
- for (var j = i - 1; j >= 0; j--) {
403
- var prev = lines[j].trim();
404
- if (!prev || prev.startsWith('//')) continue;
405
- if (prev.endsWith('{')) {
406
- if (prev.startsWith('try')) in_try = true;
407
- break;
408
- }
409
- if (prev.endsWith('}')) break;
410
- }
411
-
412
- if (!in_try) {
413
- errors.push({
414
- rule: 'try_catch',
415
- severity: 'warning',
416
- line: i + 1,
417
- message: '异步 API 调用未使用 try...catch 包裹'
418
- });
419
- }
420
- }
421
- }
422
-
423
- /**
424
- * 计算一行中大括号的净增量,跳过字符串内容
425
- * @param {string} line 行内容
426
- * @returns {number} 大括号净增量
427
- */
428
- function _countBraces(line) {
429
- var depth = 0;
430
- var in_str = null;
431
- for (var i = 0; i < line.length; i++) {
432
- var ch = line[i];
433
- if (in_str) {
434
- if (ch === in_str && line[i - 1] !== '\\') in_str = null;
435
- continue;
436
- }
437
- if (ch === "'" || ch === '"' || ch === '`') {
438
- in_str = ch;
439
- continue;
440
- }
441
- if (ch === '{') depth++;
442
- else if (ch === '}') depth--;
443
- }
444
- return depth;
445
- }
446
-
447
- /**
448
- * 根据字符偏移计算所在行号
449
- * @param {Array} lines 行数组
450
- * @param {number} offset 字符偏移
451
- * @returns {number} 行号(从1开始)
452
- */
453
- function _findLine(lines, offset) {
454
- var pos = 0;
455
- for (var i = 0; i < lines.length; i++) {
456
- pos += lines[i].length + 1;
457
- if (pos > offset) return i + 1;
458
- }
459
- return 1;
460
- }
461
-
462
- module.exports = { main };
@@ -1,22 +0,0 @@
1
- /**
2
- * 枚举验证器
3
- * 验证值是否在允许的枚举列表中
4
- */
5
-
6
- async function main(params, ctx, options) {
7
- var errors = [];
8
- var value = params.value;
9
- var options = params.options || [];
10
-
11
- if (!Array.isArray(options)) {
12
- errors.push('options 必须是数组');
13
- } else if (options.indexOf(value) === -1) {
14
- errors.push(params.field + ' 的值不在允许范围内: ' + options.join(', '));
15
- }
16
- var valid = errors.length === 0;
17
- var tip = valid ? '参数校验通过' : errors.length + '个参数校验失败: ' + errors.join('; ');
18
- ctx.output = { valid: valid, tip: tip, errors: errors, allowed_values: options };
19
- return ctx.output;
20
- }
21
-
22
- module.exports = { main };
@@ -1,16 +0,0 @@
1
- 'use strict';
2
- const fs = require('fs');
3
- const path = require('path');
4
- /** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
5
- module.exports = new Proxy({}, {
6
- get(_t, key) {
7
- if (typeof key !== 'string') return undefined;
8
- if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
9
- return require('./' + key);
10
- },
11
- has(_t, key) {
12
- return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
13
- },
14
- ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
15
- getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
16
- });