@trustnext/ztam-analyzer 1.0.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.
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * detectors/session/express-session.js
5
+ * Detects express-session usage and its backing store.
6
+ */
7
+
8
+ const STORES = [
9
+ { name: 'Redis', depSignals: ['connect-redis', 'ioredis'], codeSignals: [/RedisStore/, /connect-redis/] },
10
+ { name: 'PostgreSQL', depSignals: ['connect-pg-simple'], codeSignals: [/PgSession/, /connect-pg-simple/] },
11
+ { name: 'MongoDB', depSignals: ['connect-mongo', 'connect-mongodb-session'], codeSignals: [/MongoStore/, /connect-mongo/] },
12
+ { name: 'MySQL', depSignals: ['express-mysql-session'], codeSignals: [/MySQLStore/, /express-mysql-session/] },
13
+ ];
14
+
15
+ module.exports = {
16
+ name: 'express-session',
17
+ category: 'session',
18
+ depSignals: ['express-session'],
19
+ codeSignals: [/require\(['"]express-session['"]\)/, /app\.use\(session\(/],
20
+
21
+ scan(packageJson, files, allDeps, allCode) {
22
+ // Detect store
23
+ let store = 'In-memory (default)';
24
+ for (const s of STORES) {
25
+ if (s.depSignals.some(d => allDeps.has(d)) || s.codeSignals.some(re => re.test(allCode))) {
26
+ store = s.name;
27
+ break;
28
+ }
29
+ }
30
+ // Detect secure cookie
31
+ let secureCookie = null;
32
+ const secureMatch = allCode.match(/cookie\s*:\s*\{[^}]*secure\s*:\s*(true|false)/);
33
+ if (secureMatch) secureCookie = secureMatch[1] === 'true';
34
+
35
+ return { approach: 'express-session', store, cookieBased: true, secureCookie };
36
+ },
37
+ };
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+ module.exports = {
3
+ name: 'JWT stateless',
4
+ category: 'session',
5
+ depSignals: ['jsonwebtoken'],
6
+ codeSignals: [/jwt\.verify\(/, /jwt\.sign\(/],
7
+ scan() {
8
+ return { approach: 'JWT stateless', store: 'N/A (stateless)', cookieBased: false, secureCookie: null };
9
+ },
10
+ };
package/index.js ADDED
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * index.js (v2) — Public programmatic API for @trustnext/ztam-analyzer
5
+ *
6
+ * READ-ONLY. Never modifies the client project.
7
+ */
8
+
9
+ const path = require('path');
10
+ const { scanProject } = require('./lib/file-scanner');
11
+ const { runAll } = require('./detectors/index');
12
+ const { generateReport } = require('./lib/report-generator');
13
+
14
+ /**
15
+ * Analyzes a Node.js project and returns an integration report.
16
+ *
17
+ * @param {string} projectPath Absolute or relative path to the client project root.
18
+ * @returns {{
19
+ * terminal: string,
20
+ * markdown: string,
21
+ * json: string,
22
+ * conflicts: Array,
23
+ * strategy: Object,
24
+ * complexity: string
25
+ * }}
26
+ */
27
+ function analyze(projectPath) {
28
+ const absolutePath = path.resolve(projectPath);
29
+
30
+ // 1. Scan — read all files once into memory
31
+ const { files, packageJson } = scanProject(absolutePath);
32
+
33
+ // 2. Detect — run registry against FileMap
34
+ const detectionResult = runAll(packageJson, files);
35
+ detectionResult.projectPath = absolutePath;
36
+
37
+ // 3. Report — assemble and format
38
+ return generateReport(detectionResult);
39
+ }
40
+
41
+ module.exports = { analyze };
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * file-scanner.js
5
+ *
6
+ * Walks a project directory recursively and returns a FileMap:
7
+ * { 'relative/path/to/file.js': '<file contents>' }
8
+ *
9
+ * READ-ONLY. Never writes anything.
10
+ */
11
+
12
+ const fs = require('fs');
13
+ const path = require('path');
14
+
15
+ const SKIP_DIRS = new Set([
16
+ 'node_modules', '.git', 'dist', 'build', '.next',
17
+ 'coverage', '.cache', 'out', '.turbo', '.vercel',
18
+ 'test', 'tests', 'spec', '__tests__', '__mocks__'
19
+ ]);
20
+
21
+ const ALLOWED_EXTENSIONS = new Set([
22
+ '.js', '.ts', '.mjs', '.cjs', '.jsx', '.tsx',
23
+ ]);
24
+
25
+ /**
26
+ * @param {string} projectRoot Absolute path to the client project root.
27
+ * @returns {{ files: Object.<string,string>, packageJson: Object|null }}
28
+ */
29
+ function scanProject(projectRoot) {
30
+ const files = {};
31
+ let packageJson = null;
32
+
33
+ // Read package.json at the root if it exists
34
+ const pkgPath = path.join(projectRoot, 'package.json');
35
+ if (fs.existsSync(pkgPath)) {
36
+ try {
37
+ packageJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
38
+ } catch (_) {
39
+ packageJson = null;
40
+ }
41
+ }
42
+
43
+ _walk(projectRoot, projectRoot, files);
44
+
45
+ return { files, packageJson };
46
+ }
47
+
48
+ function _walk(root, dir, files) {
49
+ let entries;
50
+ try {
51
+ entries = fs.readdirSync(dir, { withFileTypes: true });
52
+ } catch (_) {
53
+ return; // permission error or unreadable dir — skip silently
54
+ }
55
+
56
+ for (const entry of entries) {
57
+ if (entry.isDirectory()) {
58
+ if (SKIP_DIRS.has(entry.name)) continue;
59
+ _walk(root, path.join(dir, entry.name), files);
60
+ } else if (entry.isFile()) {
61
+ const ext = path.extname(entry.name).toLowerCase();
62
+ if (!ALLOWED_EXTENSIONS.has(ext)) continue;
63
+ const fullPath = path.join(dir, entry.name);
64
+ const relPath = path.relative(root, fullPath);
65
+ try {
66
+ files[relPath] = fs.readFileSync(fullPath, 'utf8');
67
+ } catch (_) {
68
+ // unreadable file — skip silently
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ module.exports = { scanProject };