@starci/eslint-canon-be 3.8.20 → 3.9.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/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/runtime/bundle.json +192 -0
- package/runtime/engine/by-code-unit.mjs +6 -6
- package/runtime/engine/model-config.mjs +98 -76
- package/runtime/engine/orca-config.mjs +22 -17
- package/runtime/engine/release-config.mjs +22 -18
- package/runtime/engine/removed-vocabulary.mjs +21 -20
- package/runtime/engine/resources-config.mjs +42 -35
- package/runtime/engine/runtime-root.mjs +11 -13
- package/runtime/engine/secrets.mjs +170 -102
- package/runtime/engine/sonar-config.mjs +16 -13
- package/runtime/engine/temp-root.mjs +3 -9
- package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
- package/runtime/knowledge/hfs/rules.yaml +6 -6
- package/runtime/knowledge/hfs/slots.yaml +2 -2
- package/runtime/knowledge/sonar-gate.yaml +1 -1
- package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
- package/runtime/scripts/api/git/cat-file.mjs +0 -1
- package/runtime/scripts/api/git/config-get.mjs +1 -2
- package/runtime/scripts/api/git/diff.mjs +0 -1
- package/runtime/scripts/api/git/lib.mjs +32 -32
- package/runtime/scripts/api/git/ls-files.mjs +0 -1
- package/runtime/scripts/api/git/ls-tree.mjs +0 -1
- package/runtime/scripts/api/git/merge-base.mjs +2 -3
- package/runtime/scripts/api/git/remote.mjs +0 -1
- package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
- package/runtime/scripts/api/git/show.mjs +0 -1
- package/runtime/scripts/hfs/allows.mjs +37 -38
- package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
- package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
- package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
- package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
- package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
- package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
- package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
- package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
- package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
- package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
- package/runtime/scripts/hfs/architecture/config.mjs +302 -268
- package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
- package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
- package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
- package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
- package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
- package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
- package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
- package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
- package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
- package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
- package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
- package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
- package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
- package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
- package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
- package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
- package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
- package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
- package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
- package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
- package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
- package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
- package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
- package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
- package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
- package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
- package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
- package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
- package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
- package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
- package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
- package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
- package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
- package/runtime/scripts/hfs/architecture/index.mjs +272 -272
- package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
- package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
- package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
- package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
- package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
- package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
- package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
- package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
- package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
- package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
- package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
- package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
- package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
- package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
- package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
- package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
- package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
- package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
- package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
- package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
- package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
- package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
- package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
- package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
- package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
- package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
- package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
- package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
- package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
- package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
- package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
- package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
- package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
- package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
- package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
- package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
- package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
- package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
- package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
- package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
- package/runtime/scripts/hfs/edition-slots.mjs +43 -40
- package/runtime/scripts/hfs/linear-text.mjs +20 -20
- package/runtime/scripts/hfs/param-names.mjs +6 -11
- package/runtime/scripts/hfs/path-findings.mjs +120 -100
- package/runtime/scripts/hfs/repo-identity.mjs +55 -42
- package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
- package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
- package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
- package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
- package/runtime/scripts/hfs/rules/read.mjs +19 -15
- package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
- package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
- package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
- package/runtime/scripts/hfs/slot-classify.mjs +42 -38
- package/runtime/scripts/hfs/slot-errors.mjs +8 -7
- package/runtime/scripts/hfs/slot-imports.mjs +70 -63
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
- package/runtime/scripts/hfs/slot-match.mjs +122 -111
- package/runtime/scripts/hfs/slot-path.mjs +0 -2
- package/runtime/scripts/hfs/slot-required.mjs +65 -59
- package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
- package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
- package/runtime/scripts/hfs/slots.mjs +175 -152
- package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
- package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
- package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
- package/runtime/scripts/hfs/view.mjs +38 -40
- package/runtime/scripts/lib/dockerfile.mjs +131 -114
- package/runtime/scripts/lib/env.mjs +3 -6
- package/runtime/scripts/lib/event-contract.mjs +73 -78
- package/runtime/scripts/lib/git.mjs +15 -16
- package/runtime/scripts/lib/glob.mjs +23 -14
- package/runtime/scripts/lib/grammar-lint.mjs +33 -37
- package/runtime/scripts/lib/i18n.mjs +32 -35
- package/runtime/scripts/lib/in-order.mjs +57 -38
- package/runtime/scripts/lib/json-clone.mjs +45 -36
- package/runtime/scripts/lib/json.mjs +26 -14
- package/runtime/scripts/lib/language.mjs +133 -129
- package/runtime/scripts/lib/list.mjs +9 -14
- package/runtime/scripts/lib/mutation-fence.mjs +10 -5
- package/runtime/scripts/lib/path-key.mjs +53 -38
- package/runtime/scripts/lib/read-text.mjs +30 -17
- package/runtime/scripts/lib/regex.mjs +1 -2
- package/runtime/scripts/lib/same-text.mjs +0 -1
- package/runtime/scripts/lib/sops-envelope.mjs +148 -120
- package/runtime/scripts/lib/stack-services.mjs +128 -129
- package/runtime/scripts/lib/ts-analysis.mjs +28 -26
- package/runtime/scripts/lib/ts-ast.mjs +36 -42
- package/runtime/scripts/lib/walk.mjs +33 -27
- package/runtime/scripts/lib/yaml-cached.mjs +4 -4
|
@@ -3,7 +3,6 @@ import path from 'node:path';
|
|
|
3
3
|
import { machineKit, pascal, upperSnake } from './machine-ast.mjs';
|
|
4
4
|
import { locateDeclaration } from '../slots.mjs';
|
|
5
5
|
import { byCodeUnit } from '../../lib/list.mjs';
|
|
6
|
-
|
|
7
6
|
/**
|
|
8
7
|
* R84 `connection-map` (BE_CONNECTION_DUPLICATE), folding RED01 and RED02. One physical database is one connection, one
|
|
9
8
|
* `Inject<Conn>EntityManager()` injector, one config; the `hfs.json` connections, the connection files, the injectors, the
|
|
@@ -25,7 +24,6 @@ import { byCodeUnit } from '../../lib/list.mjs';
|
|
|
25
24
|
* `isolation: schema` with different `<PREFIX>_SCHEMA` values (two contexts as schemas of one database).
|
|
26
25
|
*/
|
|
27
26
|
export const CONNECTION_RULE_IDS = ['BE_CONNECTION_DUPLICATE'];
|
|
28
|
-
|
|
29
27
|
const RULE = 'BE_CONNECTION_DUPLICATE';
|
|
30
28
|
export const DATABASE_DIR = 'src/modules/platform/database';
|
|
31
29
|
const TYPEORM = '@nestjs/typeorm';
|
|
@@ -33,285 +31,320 @@ const MANAGER_CALLS = new Set(['getEntityManagerToken', 'InjectEntityManager']);
|
|
|
33
31
|
const SOURCE_CALLS = new Set(['getDataSourceToken', 'InjectDataSource']);
|
|
34
32
|
const ENV_KEY = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$/u;
|
|
35
33
|
const INJECTOR_NAME = /^Inject[A-Za-z0-9]*EntityManager$/u;
|
|
36
|
-
|
|
37
34
|
const exportedInjectorName = (ts, kit, node) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
const statement = node.parent?.parent;
|
|
36
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && statement && ts.isVariableStatement(statement) && kit.isExported(statement))
|
|
37
|
+
return node.name.text;
|
|
38
|
+
if (ts.isFunctionDeclaration(node) && kit.isExported(node) && node.name)
|
|
39
|
+
return node.name.text;
|
|
40
|
+
return null;
|
|
41
41
|
};
|
|
42
|
-
|
|
43
42
|
const ENV_ASSIGNMENT = /^(?:export\s+)?([A-Za-z_]\w*)\s*=/u;
|
|
44
43
|
const LINE_BREAK = /[\n\r\u2028\u2029]/u;
|
|
45
|
-
|
|
46
44
|
// One `KEY=value` line: a value holding a line break after its first character is not an assignment.
|
|
47
45
|
function parseEnvLine(raw) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
46
|
+
const line = raw.trim();
|
|
47
|
+
if (!line || line.startsWith('#'))
|
|
48
|
+
return null;
|
|
49
|
+
const match = ENV_ASSIGNMENT.exec(line);
|
|
50
|
+
if (!match)
|
|
51
|
+
return null;
|
|
52
|
+
const key = match[1];
|
|
53
|
+
if (key === undefined)
|
|
54
|
+
return null;
|
|
55
|
+
let value = line.slice(match[0].length).trim();
|
|
56
|
+
if (LINE_BREAK.test(value))
|
|
57
|
+
return null;
|
|
58
|
+
if (/^(['"]).*\1$/u.test(value))
|
|
59
|
+
value = value.slice(1, -1);
|
|
60
|
+
return [key, value.trim()];
|
|
56
61
|
}
|
|
57
|
-
|
|
58
62
|
function readEnvFile(file, merged) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
let text;
|
|
64
|
+
try {
|
|
65
|
+
text = fs.readFileSync(file, 'utf8');
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
for (const raw of text.split(/\r?\n/u)) {
|
|
71
|
+
const parsed = parseEnvLine(raw);
|
|
72
|
+
if (parsed)
|
|
73
|
+
merged.set(parsed[0], parsed[1]);
|
|
74
|
+
}
|
|
65
75
|
}
|
|
66
|
-
|
|
67
76
|
function readEnvFiles(target) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
const merged = new Map();
|
|
78
|
+
let stat;
|
|
79
|
+
try {
|
|
80
|
+
stat = fs.statSync(target);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (stat.isFile())
|
|
86
|
+
readEnvFile(target, merged);
|
|
87
|
+
else
|
|
88
|
+
for (const entry of fs.readdirSync(target, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
89
|
+
if (entry.isFile() && !entry.name.toLowerCase().endsWith('.md'))
|
|
90
|
+
readEnvFile(path.join(target, entry.name), merged);
|
|
91
|
+
}
|
|
92
|
+
return merged;
|
|
76
93
|
}
|
|
77
|
-
|
|
78
94
|
const exportsVariable = (ts, kit, item, name) => ts.isVariableStatement(item) && kit.isExported(item)
|
|
79
|
-
|
|
80
|
-
|
|
95
|
+
&& item.declarationList.declarations.some(declaration => ts.isIdentifier(declaration.name) && declaration.name.text === name);
|
|
81
96
|
const checkConnectionFile = (connection, kit, ts, report) => {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
97
|
+
const connectionRel = `${DATABASE_DIR}/${connection.name}.connection.ts`;
|
|
98
|
+
const constant = `${upperSnake(connection.name)}_CONNECTION`;
|
|
99
|
+
const connectionFile = kit.graphFile(connectionRel);
|
|
100
|
+
if (!connectionFile) {
|
|
101
|
+
report(connectionRel, `hfs.json declares connection ${connection.name} but ${connectionRel} does not exist; declare \`export const ${constant} = "${connection.name}"\` there.`, { connection: connection.name });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const checker = kit.checkerOf(connectionFile.sourceFile);
|
|
105
|
+
const statement = connectionFile.sourceFile.statements.find(item => exportsVariable(ts, kit, item, constant));
|
|
106
|
+
const initializer = statement?.declarationList.declarations.find(declaration => ts.isIdentifier(declaration.name) && declaration.name.text === constant)?.initializer;
|
|
107
|
+
if (!statement || kit.stringValue(checker, initializer) !== connection.name) {
|
|
108
|
+
report(connectionRel, `${connectionRel} must export \`${constant} = "${connection.name}"\` (the connection name declared in hfs.json).`, { connection: connection.name });
|
|
109
|
+
}
|
|
95
110
|
};
|
|
96
|
-
|
|
97
111
|
const checkInjectorFile = (connection, kit, ts, report, decoratorsFile) => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
112
|
+
const decorators = kit.graphFile(decoratorsFile(connection.name));
|
|
113
|
+
const injector = `Inject${pascal(connection.name)}EntityManager`;
|
|
114
|
+
const exported = decorators?.sourceFile.statements.some(item => exportsVariable(ts, kit, item, injector)
|
|
115
|
+
|| (ts.isFunctionDeclaration(item) && kit.isExported(item) && item.name?.text === injector));
|
|
116
|
+
if (!exported)
|
|
117
|
+
report(decoratorsFile(connection.name), `Connection ${connection.name} needs ${decoratorsFile(connection.name)} exporting ${injector}, the one injector of its shared EntityManager.`, { connection: connection.name });
|
|
103
118
|
};
|
|
104
|
-
|
|
105
119
|
const isProcessEnvAccess = (ts, node) => ts.isPropertyAccessExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'env'
|
|
106
|
-
|
|
107
|
-
|
|
120
|
+
&& ts.isIdentifier(node.expression.expression) && node.expression.expression.text === 'process';
|
|
108
121
|
const configEnvKey = (ts, node) => {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
122
|
+
if (ts.isStringLiteralLike(node) && ENV_KEY.test(node.text))
|
|
123
|
+
return node.text;
|
|
124
|
+
if (isProcessEnvAccess(ts, node) && ENV_KEY.test(node.name.text))
|
|
125
|
+
return node.name.text;
|
|
126
|
+
return null;
|
|
112
127
|
};
|
|
113
|
-
|
|
114
128
|
const checkConfigFile = (connection, declared, kit, ts, report, configRel, configFile) => {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
129
|
+
let readsSchema = false;
|
|
130
|
+
kit.walk(configFile.sourceFile, node => {
|
|
131
|
+
const key = configEnvKey(ts, node);
|
|
132
|
+
if (key === null)
|
|
133
|
+
return true;
|
|
134
|
+
if (key === `${connection.envPrefix}_SCHEMA`)
|
|
135
|
+
readsSchema = true;
|
|
136
|
+
const own = key.startsWith(`${connection.envPrefix}_`);
|
|
137
|
+
const foreign = declared.some(other => other !== connection && key.startsWith(`${other.envPrefix}_`));
|
|
138
|
+
if (!own || foreign) {
|
|
139
|
+
report(configRel, `${configRel} reads ${key}; the config of connection ${connection.name} reads only ${connection.envPrefix}_* keys.`, { ...kit.at(configRel, configFile.sourceFile, node), connection: connection.name });
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
});
|
|
143
|
+
if (connection.isolation === 'schema' && !readsSchema)
|
|
144
|
+
report(configRel, `Connection ${connection.name} is isolated as a schema of a shared database (hfs.json isolation), so ${configRel} must read ${connection.envPrefix}_SCHEMA and hand it to the data source; splitting the context out into its own database is then an env change only.`, { connection: connection.name });
|
|
128
145
|
};
|
|
129
|
-
|
|
130
146
|
const checkDeclaredConnections = (declared, kit, ts, report, decoratorsFile) => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
147
|
+
for (const connection of declared) {
|
|
148
|
+
checkConnectionFile(connection, kit, ts, report);
|
|
149
|
+
checkInjectorFile(connection, kit, ts, report, decoratorsFile);
|
|
150
|
+
const configRel = `${DATABASE_DIR}/${connection.name}.config.ts`;
|
|
151
|
+
const configFile = kit.graphFile(configRel);
|
|
152
|
+
if (!configFile) {
|
|
153
|
+
report(configRel, `Connection ${connection.name} needs ${configRel}, the one place its ${connection.envPrefix}_* keys are read.`, { connection: connection.name });
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
checkConfigFile(connection, declared, kit, ts, report, configRel, configFile);
|
|
139
157
|
}
|
|
140
|
-
checkConfigFile(connection, declared, kit, ts, report, configRel, configFile);
|
|
141
|
-
}
|
|
142
158
|
};
|
|
143
|
-
|
|
144
159
|
const reportUndeclaredDatabaseFiles = (databaseFiles, byName, report) => {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
160
|
+
for (const file of databaseFiles) {
|
|
161
|
+
const match = /^(.+)\.(connection|decorators|config)\.ts$/u.exec(path.posix.basename(file.rel));
|
|
162
|
+
const name = match?.[1];
|
|
163
|
+
if (match && name !== undefined && path.posix.dirname(file.rel) === DATABASE_DIR && !byName.has(name)) {
|
|
164
|
+
report(file.rel, `${file.rel} belongs to connection ${name}, which hfs.json does not declare; declare the database in hfs.json connections or delete the file.`, { connection: name });
|
|
165
|
+
}
|
|
149
166
|
}
|
|
150
|
-
}
|
|
151
167
|
};
|
|
152
|
-
|
|
153
168
|
const inspectManagerToken = (file, checker, node, binding, state) => {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
169
|
+
const { kit, byName, decoratorsFile, report, perDecorators } = state;
|
|
170
|
+
const value = kit.stringValue(checker, node.arguments[0]);
|
|
171
|
+
const home = value === null ? null : decoratorsFile(value);
|
|
172
|
+
if (value === null || !byName.has(value)) {
|
|
173
|
+
const target = value === null ? 'a connection that cannot be resolved to a constant' : `connection ${value}, which hfs.json does not declare`;
|
|
174
|
+
report(file.rel, `${binding.name}() names ${target}; only the declared connections have an entity manager token.`, kit.at(file.rel, file.sourceFile, node));
|
|
175
|
+
}
|
|
176
|
+
else if (file.rel !== home) {
|
|
177
|
+
report(file.rel, `${binding.name}(${value}) belongs in ${home} only; inject the shared manager with Inject${pascal(value)}EntityManager() instead of a second path to connection ${value}.`, { ...kit.at(file.rel, file.sourceFile, node), connection: value });
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
perDecorators.set(value, (perDecorators.get(value) ?? 0) + 1);
|
|
181
|
+
if ((perDecorators.get(value) ?? 0) > 1)
|
|
182
|
+
report(file.rel, `${home} resolves connection ${value} more than once; one connection has one injector.`, { ...kit.at(file.rel, file.sourceFile, node), connection: value });
|
|
183
|
+
}
|
|
166
184
|
};
|
|
167
|
-
|
|
168
185
|
const inspectDataSourceCall = (file, node, binding, state) => {
|
|
169
|
-
|
|
170
|
-
|
|
186
|
+
const allowed = file.rel.startsWith(`${DATABASE_DIR}/`) || state.slotIs(file, 'be.app.cli') || state.slotIs(file, 'be.cli') || state.slotIs(file, 'be.tests.fixtures');
|
|
187
|
+
if (!allowed)
|
|
188
|
+
state.report(file.rel, `${binding.name}() reaches the DataSource outside platform/database and the cli; inject the shared EntityManager of the connection instead.`, state.kit.at(file.rel, file.sourceFile, node));
|
|
171
189
|
};
|
|
172
|
-
|
|
173
190
|
const inspectTypeormCall = (file, checker, node, state) => {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
191
|
+
const binding = state.kit.importBinding(checker, node.expression);
|
|
192
|
+
if (binding?.module === TYPEORM && MANAGER_CALLS.has(binding.name))
|
|
193
|
+
inspectManagerToken(file, checker, node, binding, state);
|
|
194
|
+
else if (binding?.module === TYPEORM && SOURCE_CALLS.has(binding.name))
|
|
195
|
+
inspectDataSourceCall(file, node, binding, state);
|
|
177
196
|
};
|
|
178
|
-
|
|
179
197
|
const inspectInjectorExport = (file, node, state) => {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
198
|
+
const { ts, kit, byName, decoratorsFile, report } = state;
|
|
199
|
+
const exportedName = exportedInjectorName(ts, kit, node);
|
|
200
|
+
if (!exportedName || !INJECTOR_NAME.test(exportedName))
|
|
201
|
+
return;
|
|
202
|
+
const home = [...byName.keys()].find(name => `Inject${pascal(name)}EntityManager` === exportedName);
|
|
203
|
+
if (!home || file.rel !== decoratorsFile(home)) {
|
|
204
|
+
report(file.rel, `${exportedName} is exported here, but the only injector of an entity manager is Inject<Conn>EntityManager in src/modules/platform/database/<conn>.decorators.ts, one per declared connection.`, kit.at(file.rel, file.sourceFile, node));
|
|
205
|
+
}
|
|
187
206
|
};
|
|
188
|
-
|
|
189
207
|
const inspectManagerNode = (file, checker, node, state) => {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
208
|
+
if (state.ts.isCallExpression(node))
|
|
209
|
+
inspectTypeormCall(file, checker, node, state);
|
|
210
|
+
inspectInjectorExport(file, node, state);
|
|
211
|
+
return true;
|
|
193
212
|
};
|
|
194
|
-
|
|
195
213
|
const checkManagerCalls = (graph, state) => {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
214
|
+
const perDecorators = new Map();
|
|
215
|
+
const walkState = { ...state, perDecorators, slotIs: (file, id) => file.slot === id };
|
|
216
|
+
for (const file of graph.files.values()) {
|
|
217
|
+
const checker = state.kit.checkerOf(file.sourceFile);
|
|
218
|
+
state.kit.walk(file.sourceFile, node => inspectManagerNode(file, checker, node, walkState));
|
|
219
|
+
}
|
|
202
220
|
};
|
|
203
|
-
|
|
204
221
|
/** The receiver of a `<receiver>.register(...)` call that `node` is, or null. */
|
|
205
222
|
const registerReceiver = (ts, node) => {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
223
|
+
if (!ts.isCallExpression(node))
|
|
224
|
+
return null;
|
|
225
|
+
const callee = node.expression;
|
|
226
|
+
return ts.isPropertyAccessExpression(callee) && callee.name.text === 'register' ? callee.expression : null;
|
|
227
|
+
};
|
|
228
|
+
const isPropertyName = (ts, node) => {
|
|
229
|
+
const parent = node.parent;
|
|
230
|
+
if (!parent)
|
|
231
|
+
return false;
|
|
232
|
+
return (ts.isPropertyAssignment(parent) || ts.isPropertyAccessExpression(parent)) && parent.name === node;
|
|
209
233
|
};
|
|
210
|
-
|
|
211
|
-
const isPropertyName = (ts, node) => (ts.isPropertyAssignment(node.parent ?? {}) || ts.isPropertyAccessExpression(node.parent ?? {})) && node.parent.name === node;
|
|
212
|
-
|
|
213
234
|
const passConnections = (argument, scope) => {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
235
|
+
const { ts, kit, checker, byName, passed, root, app, report } = scope;
|
|
236
|
+
kit.walk(argument, inner => {
|
|
237
|
+
if (isPropertyName(ts, inner))
|
|
238
|
+
return true;
|
|
239
|
+
if (!(ts.isIdentifier(inner) || ts.isStringLiteralLike(inner) || ts.isPropertyAccessExpression(inner)))
|
|
240
|
+
return true;
|
|
241
|
+
const value = kit.stringValue(checker, inner);
|
|
242
|
+
if (value === null || !byName.has(value))
|
|
243
|
+
return true;
|
|
244
|
+
passed.set(value, (passed.get(value) ?? 0) + 1);
|
|
245
|
+
if ((passed.get(value) ?? 0) > 1)
|
|
246
|
+
report(root.rel, `App ${app.name} passes connection ${value} to the database module registration more than once; one connection is registered once per app.`, { ...kit.at(root.rel, root.sourceFile, inner), connection: value, app: app.name });
|
|
247
|
+
return false;
|
|
248
|
+
});
|
|
224
249
|
};
|
|
225
|
-
|
|
226
250
|
const checkAppRegistrations = (config, kit, ts, byName, report) => {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
251
|
+
let registrations = 0;
|
|
252
|
+
for (const app of config.apps) {
|
|
253
|
+
// The root and its composition groups (compose/<area>.ts) are one composition: a connection is passed once across them.
|
|
254
|
+
const passed = new Map();
|
|
255
|
+
for (const root of kit.appCompositionFiles(app.name)) {
|
|
256
|
+
const checker = kit.checkerOf(root.sourceFile);
|
|
257
|
+
const scope = { ts, kit, checker, byName, passed, root, app, report };
|
|
258
|
+
kit.walk(root.sourceFile, node => {
|
|
259
|
+
const receiver = registerReceiver(ts, node);
|
|
260
|
+
if (!receiver || !ts.isCallExpression(node))
|
|
261
|
+
return;
|
|
262
|
+
const owner = kit.declarationsOf(checker, receiver).map(kit.ownerOfDeclaration).find(Boolean);
|
|
263
|
+
if (owner?.tier !== 'platform' || owner.name !== 'database')
|
|
264
|
+
return;
|
|
265
|
+
registrations += 1;
|
|
266
|
+
for (const argument of node.arguments)
|
|
267
|
+
passConnections(argument, scope);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
242
270
|
}
|
|
243
|
-
|
|
244
|
-
return registrations;
|
|
271
|
+
return registrations;
|
|
245
272
|
};
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
273
|
+
const listStackEnvs = (stacksRoot) => {
|
|
274
|
+
try {
|
|
275
|
+
return fs.readdirSync(stacksRoot, { withFileTypes: true }).filter(entry => entry.isDirectory()).map(entry => entry.name).sort(byCodeUnit);
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
return [];
|
|
279
|
+
}
|
|
249
280
|
};
|
|
250
|
-
|
|
251
281
|
// The resolved database of one connection in one stack env, or null while its host, port or name is unset.
|
|
252
282
|
const resolveStackConnection = (kit, connection, values) => {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
283
|
+
const configFile = kit.graphFile(`${DATABASE_DIR}/${connection.name}.config.ts`);
|
|
284
|
+
const nameKey = configFile?.sourceFile.text.includes(`${connection.envPrefix}_DATABASE`) ? `${connection.envPrefix}_DATABASE` : `${connection.envPrefix}_NAME`;
|
|
285
|
+
const host = values.get(`${connection.envPrefix}_HOST`);
|
|
286
|
+
const port = values.get(`${connection.envPrefix}_PORT`);
|
|
287
|
+
const database = values.get(nameKey);
|
|
288
|
+
if (!host || !port || !database)
|
|
289
|
+
return null;
|
|
290
|
+
const schema = connection.isolation === 'schema' ? (values.get(`${connection.envPrefix}_SCHEMA`) ?? '') : null;
|
|
291
|
+
return { host, port, database, triple: `${host.toLowerCase()}|${port}|${database}`, schema };
|
|
261
292
|
};
|
|
262
|
-
|
|
263
293
|
// True when the env resolves at least one connection; false when it is skipped.
|
|
264
294
|
const checkStackEnv = (config, kit, declared, report, stacksRoot, env) => {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
295
|
+
// Relative to the side folder the machine judges (`../.starcistacks/...` for a side): the app-relative path once the side is prefixed.
|
|
296
|
+
const rel = path.relative(config.root, path.join(stacksRoot, env, 'runtime', 'env')).split(path.sep).join('/');
|
|
297
|
+
const values = readEnvFiles(path.join(stacksRoot, env, 'runtime', 'env'));
|
|
298
|
+
if (!values || values.size === 0)
|
|
299
|
+
return false;
|
|
300
|
+
const seen = new Map(); // host|port|database -> [{name, isolation, schema}]
|
|
301
|
+
let resolved = 0;
|
|
302
|
+
for (const connection of declared) {
|
|
303
|
+
const found = resolveStackConnection(kit, connection, values);
|
|
304
|
+
if (!found)
|
|
305
|
+
continue;
|
|
306
|
+
resolved += 1;
|
|
307
|
+
const { host, port, database, triple, schema } = found;
|
|
308
|
+
const sharing = seen.get(triple) ?? [];
|
|
309
|
+
// Two contexts may share one database only as distinct, named schemas; a context that is its own database shares it with nobody.
|
|
310
|
+
const clash = sharing.find(other => schema === null || other.schema === null || other.schema === schema);
|
|
311
|
+
if (clash) {
|
|
312
|
+
const schemaDetail = schema !== null && clash.schema === schema ? `, schema ${schema || '(none)'}` : '';
|
|
313
|
+
report(rel, `Connections ${clash.name} and ${connection.name} resolve to the same database (${host}:${port}/${database}${schemaDetail}) in stack ${env}; contexts share a database only as distinct schemas (isolation schema, a different ${connection.envPrefix}_SCHEMA), otherwise one physical database is one connection.`, { connection: connection.name, env });
|
|
314
|
+
}
|
|
315
|
+
sharing.push({ name: connection.name, schema });
|
|
316
|
+
seen.set(triple, sharing);
|
|
282
317
|
}
|
|
283
|
-
|
|
284
|
-
seen.set(triple, sharing);
|
|
285
|
-
}
|
|
286
|
-
return resolved > 0;
|
|
318
|
+
return resolved > 0;
|
|
287
319
|
};
|
|
288
|
-
|
|
289
320
|
const checkStackConnections = (config, kit, declared, report) => {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
321
|
+
let stacksChecked = 0;
|
|
322
|
+
let stacksSkipped = 0;
|
|
323
|
+
// .starcistacks sits at the app root: the side folder the machine judges reads its app's tree (locateDeclaration).
|
|
324
|
+
const stacksRoot = path.join(locateDeclaration(config.root).appRoot, '.starcistacks');
|
|
325
|
+
for (const env of listStackEnvs(stacksRoot)) {
|
|
326
|
+
if (checkStackEnv(config, kit, declared, report, stacksRoot, env))
|
|
327
|
+
stacksChecked += 1;
|
|
328
|
+
else
|
|
329
|
+
stacksSkipped += 1;
|
|
330
|
+
}
|
|
331
|
+
return { stacksChecked, stacksSkipped };
|
|
298
332
|
};
|
|
299
|
-
|
|
300
333
|
export function checkConnectionMap(input) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
334
|
+
const { config, graph } = input;
|
|
335
|
+
const kit = machineKit(input);
|
|
336
|
+
const { ts } = kit;
|
|
337
|
+
const declared = config.hfs.repo.connections ?? [];
|
|
338
|
+
const byName = new Map(declared.map((connection) => [connection.name, connection]));
|
|
339
|
+
const violations = [];
|
|
340
|
+
const report = (rel, message, extra = {}) => { violations.push({ ruleId: RULE, path: rel, message, ...extra }); };
|
|
341
|
+
const decoratorsFile = name => `${DATABASE_DIR}/${name}.decorators.ts`;
|
|
342
|
+
const databaseFiles = [...graph.files.values()].filter(file => file.rel.startsWith(`${DATABASE_DIR}/`));
|
|
343
|
+
const state = { config, graph, kit, ts, declared, byName, violations, report, decoratorsFile };
|
|
344
|
+
checkDeclaredConnections(declared, kit, ts, report, decoratorsFile);
|
|
345
|
+
reportUndeclaredDatabaseFiles(databaseFiles, byName, report);
|
|
346
|
+
checkManagerCalls(graph, state);
|
|
347
|
+
const registrations = checkAppRegistrations(config, kit, ts, byName, report);
|
|
348
|
+
const { stacksChecked, stacksSkipped } = checkStackConnections(config, kit, declared, report);
|
|
349
|
+
return { violations, coverage: { status: 'checked', connections: declared.length, registrations, stacksChecked, stacksSkipped } };
|
|
317
350
|
}
|