@repometry/core 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/package.json +47 -4
- package/target/main/ts/engine/execute.d.ts +15 -0
- package/target/main/ts/engine/execute.js +63 -0
- package/target/main/ts/engine/manifest.d.ts +3 -0
- package/target/main/ts/engine/manifest.js +129 -0
- package/target/main/ts/engine/operation-cache.d.ts +14 -0
- package/target/main/ts/engine/operation-cache.js +66 -0
- package/target/main/ts/engine/plan.d.ts +21 -0
- package/target/main/ts/engine/plan.js +123 -0
- package/target/main/ts/engine/plugin.d.ts +71 -0
- package/target/main/ts/engine/plugin.js +3 -0
- package/target/main/ts/engine/report-products.d.ts +16 -0
- package/target/main/ts/engine/report-products.js +76 -0
- package/target/main/ts/index.d.ts +20 -0
- package/target/main/ts/index.js +8 -0
- package/target/main/ts/model/changes.d.ts +39 -0
- package/target/main/ts/model/changes.js +6 -0
- package/target/main/ts/model/churn.d.ts +11 -0
- package/target/main/ts/model/churn.js +6 -0
- package/target/main/ts/model/clone-source.d.ts +19 -0
- package/target/main/ts/model/clone-source.js +20 -0
- package/target/main/ts/model/code-structure.d.ts +45 -0
- package/target/main/ts/model/code-structure.js +63 -0
- package/target/main/ts/model/comparison.d.ts +24 -0
- package/target/main/ts/model/comparison.js +2 -0
- package/target/main/ts/model/coverage-input.d.ts +12 -0
- package/target/main/ts/model/coverage-input.js +11 -0
- package/target/main/ts/model/diff-coverage.d.ts +25 -0
- package/target/main/ts/model/diff-coverage.js +10 -0
- package/target/main/ts/model/file-types.d.ts +20 -0
- package/target/main/ts/model/file-types.js +50 -0
- package/target/main/ts/model/finding-ignore.d.ts +8 -0
- package/target/main/ts/model/finding-ignore.js +36 -0
- package/target/main/ts/model/finding.d.ts +34 -0
- package/target/main/ts/model/finding.js +45 -0
- package/target/main/ts/model/ignore-rule.d.ts +11 -0
- package/target/main/ts/model/ignore-rule.js +2 -0
- package/target/main/ts/model/language.d.ts +44 -0
- package/target/main/ts/model/language.js +31 -0
- package/target/main/ts/model/line-coverage.d.ts +15 -0
- package/target/main/ts/model/line-coverage.js +43 -0
- package/target/main/ts/model/measurement.d.ts +34 -0
- package/target/main/ts/model/measurement.js +42 -0
- package/target/main/ts/model/metric-descriptor.d.ts +17 -0
- package/target/main/ts/model/metric-descriptor.js +2 -0
- package/target/main/ts/model/plugin-data.d.ts +32 -0
- package/target/main/ts/model/plugin-data.js +16 -0
- package/target/main/ts/model/pull-review.d.ts +70 -0
- package/target/main/ts/model/pull-review.js +4 -0
- package/target/main/ts/model/quality-summary.d.ts +46 -0
- package/target/main/ts/model/quality-summary.js +92 -0
- package/target/main/ts/model/report.d.ts +45 -0
- package/target/main/ts/model/report.js +5 -0
- package/target/main/ts/model/scanner-import.d.ts +39 -0
- package/target/main/ts/model/scanner-import.js +29 -0
- package/target/main/ts/model/snapshot.d.ts +14 -0
- package/target/main/ts/model/snapshot.js +49 -0
- package/target/main/ts/model/source-locator.d.ts +7 -0
- package/target/main/ts/model/source-locator.js +2 -0
- package/target/main/ts/model/source.d.ts +31 -0
- package/target/main/ts/model/source.js +20 -0
- package/target/main/ts/ports/report.d.ts +9 -0
- package/target/main/ts/ports/report.js +2 -0
- package/target/main/ts/ports/source-provider.d.ts +14 -0
- package/target/main/ts/ports/source-provider.js +5 -0
- package/target/main/ts/ports/tool-runner.d.ts +18 -0
- package/target/main/ts/ports/tool-runner.js +2 -0
- package/target/main/ts/profile/exclusions.d.ts +11 -0
- package/target/main/ts/profile/exclusions.js +58 -0
- package/target/main/ts/profile/maintainability.d.ts +31 -0
- package/target/main/ts/profile/maintainability.js +10 -0
- package/target/main/ts/profile/profile.d.ts +4 -0
- package/target/main/ts/profile/profile.js +11 -0
- package/target/main/ts/usecases/diff-coverage-policy.d.ts +6 -0
- package/target/main/ts/usecases/diff-coverage-policy.js +11 -0
- package/target/main/ts/usecases/diff-coverage.d.ts +40 -0
- package/target/main/ts/usecases/diff-coverage.js +16 -0
- package/target/main/ts/usecases/inspect.d.ts +37 -0
- package/target/main/ts/usecases/inspect.js +42 -0
- package/target/main/ts/usecases/review-effect.d.ts +18 -0
- package/target/main/ts/usecases/review-effect.js +37 -0
- package/target/main/ts/usecases/review-match.d.ts +14 -0
- package/target/main/ts/usecases/review-match.js +72 -0
- package/target/main/ts/usecases/review-position.d.ts +14 -0
- package/target/main/ts/usecases/review-position.js +53 -0
- package/target/main/ts/usecases/review-scope.d.ts +21 -0
- package/target/main/ts/usecases/review-scope.js +41 -0
- package/target/main/ts/usecases/review.d.ts +42 -0
- package/target/main/ts/usecases/review.js +112 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Idleware
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@repometry/core",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "The Repometry plugin API and inspection engine",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/idleware/repometry.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/idleware/repometry#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/idleware/repometry/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"repometry",
|
|
16
|
+
"plugin",
|
|
17
|
+
"metrics"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=24"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./target/main/ts/index.d.ts",
|
|
26
|
+
"default": "./target/main/ts/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./internal/*.js": {
|
|
29
|
+
"types": "./target/main/ts/*.d.ts",
|
|
30
|
+
"default": "./target/main/ts/*.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"target/main",
|
|
35
|
+
"!target/main/**/*.map"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "node -e \"require('node:fs').rmSync('target/main', { recursive: true, force: true })\" && tsc -p tsconfig.json && node -e \"require('node:fs').cpSync('../../LICENSE', 'LICENSE')\"",
|
|
42
|
+
"build:test": "npm run build && node -e \"require('node:fs').rmSync('target/verification', { recursive: true, force: true })\" && tsc -p tsconfig.test.json",
|
|
43
|
+
"test": "npm run build:test && node --test 'target/verification/test/ts/*.test.js'",
|
|
44
|
+
"prepack": "npm run build"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"ajv": "8.20.0"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { OperationCacheOptions } from './operation-cache.js';
|
|
2
|
+
import type { Product } from './plugin.js';
|
|
3
|
+
import type { PlannedOperation } from './plan.js';
|
|
4
|
+
export type Outcome = {
|
|
5
|
+
readonly state: 'completed';
|
|
6
|
+
readonly product: Product;
|
|
7
|
+
readonly diagnostics?: readonly {
|
|
8
|
+
message: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
}[];
|
|
11
|
+
} | {
|
|
12
|
+
readonly state: 'failed' | 'blocked';
|
|
13
|
+
readonly reason: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function execute(plan: readonly PlannedOperation[], external: ReadonlyMap<string, Product>, cache?: OperationCacheOptions): Promise<ReadonlyMap<string, Outcome>>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { operationCacheKey, readCachedOperation, writeCachedOperation } from './operation-cache.js';
|
|
2
|
+
export async function execute(plan, external, cache) {
|
|
3
|
+
const results = new Map();
|
|
4
|
+
for (const task of plan) {
|
|
5
|
+
const inputs = new Map();
|
|
6
|
+
const missing = [];
|
|
7
|
+
for (const [name, binding] of task.inputs) {
|
|
8
|
+
const producer = 'operation' in binding ? results.get(binding.operation) : undefined;
|
|
9
|
+
const product = 'external' in binding ? external.get(binding.external)
|
|
10
|
+
: producer?.state === 'completed' ? producer.product : undefined;
|
|
11
|
+
if (product)
|
|
12
|
+
inputs.set(name, product);
|
|
13
|
+
else
|
|
14
|
+
missing.push(name);
|
|
15
|
+
}
|
|
16
|
+
results.set(task.key, missing.length
|
|
17
|
+
? { state: 'blocked', reason: `Unavailable inputs: ${missing.join(', ')}` }
|
|
18
|
+
: await runTask(task, inputs, cache));
|
|
19
|
+
}
|
|
20
|
+
return results;
|
|
21
|
+
}
|
|
22
|
+
async function runTask(task, inputs, cache) {
|
|
23
|
+
try {
|
|
24
|
+
const { operation, validateOutput } = task;
|
|
25
|
+
for (const [name, contract] of Object.entries(operation.inputs))
|
|
26
|
+
readInput(inputs, name, contract);
|
|
27
|
+
const key = cache && operationCacheKey(task, inputs, cache.configurationDigest);
|
|
28
|
+
const hit = key && cache ? await readCachedOperation(cache.store, key, task) : undefined;
|
|
29
|
+
if (hit)
|
|
30
|
+
return { state: 'completed', product: hit };
|
|
31
|
+
const diagnostics = [];
|
|
32
|
+
const context = {
|
|
33
|
+
read: (port, contract) => {
|
|
34
|
+
if (!Object.hasOwn(operation.inputs, port) || operation.inputs[port]?.id !== contract.id) {
|
|
35
|
+
throw new Error(`Undeclared input contract: ${port} (${contract.id})`);
|
|
36
|
+
}
|
|
37
|
+
return readInput(inputs, port, contract);
|
|
38
|
+
},
|
|
39
|
+
emitDiagnostic: (message, path) => diagnostics.push(path === undefined ? { message } : { message, path }),
|
|
40
|
+
};
|
|
41
|
+
const value = await operation.execute(context);
|
|
42
|
+
const { output } = operation;
|
|
43
|
+
if (!output.accepts(value))
|
|
44
|
+
throw new Error(`Invalid output for ${output.id}`);
|
|
45
|
+
if (validateOutput && !validateOutput(value))
|
|
46
|
+
throw new Error(`Output violates the schema for ${output.id}`);
|
|
47
|
+
const product = { contract: output.id, value };
|
|
48
|
+
if (key && cache && diagnostics.length === 0)
|
|
49
|
+
await writeCachedOperation(cache.store, key, product);
|
|
50
|
+
return { state: 'completed', product, ...(diagnostics.length ? { diagnostics } : {}) };
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return { state: 'failed', reason: error instanceof Error ? error.message : String(error) };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function readInput(inputs, port, contract) {
|
|
57
|
+
const product = inputs.get(port);
|
|
58
|
+
if (!product || product.contract !== contract.id || !contract.accepts(product.value)) {
|
|
59
|
+
throw new Error(`Invalid input: ${port} (${contract.id})`);
|
|
60
|
+
}
|
|
61
|
+
return product.value;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=execute.js.map
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { isRecord } from './plugin.js';
|
|
2
|
+
import { findingResult } from '../model/finding.js';
|
|
3
|
+
import { definitions, measurements } from '../model/measurement.js';
|
|
4
|
+
import { structureMeasures } from '../model/code-structure.js';
|
|
5
|
+
import { isLanguageDisplay, isLanguageSyntax } from '../model/language.js';
|
|
6
|
+
const kinds = new Set(['language', 'analysis', 'coverage', 'security', 'import', 'source']);
|
|
7
|
+
const persistence = new Set(['report', 'cache', 'transient']);
|
|
8
|
+
const units = new Set(['count', 'ratio', 'minutes']);
|
|
9
|
+
const scopes = new Set(['repository', 'file', 'function']);
|
|
10
|
+
const aggregates = new Set(['sum', 'max', 'none']);
|
|
11
|
+
const directions = new Set(['lower', 'higher', 'none']);
|
|
12
|
+
const coreMetrics = new Set(Object.values(definitions));
|
|
13
|
+
export function manifestProducts(plugin) {
|
|
14
|
+
const { id, produces, rules } = manifestDeclarations(plugin);
|
|
15
|
+
const products = new Map();
|
|
16
|
+
const catalog = new Map();
|
|
17
|
+
for (const product of produces) {
|
|
18
|
+
if (!isRecord(product) || !text(product.contract) || !text(product.persistence) || !persistence.has(product.persistence)
|
|
19
|
+
|| products.has(product.contract))
|
|
20
|
+
throw new Error(`Invalid or duplicate product for plugin ${id}`);
|
|
21
|
+
const { contract, schema, metrics } = product;
|
|
22
|
+
// Only rows need no schema: the core row contracts, or a report product that declares its metrics.
|
|
23
|
+
const rows = [measurements.id, findingResult.id].includes(contract) || (product.persistence === 'report' && metrics !== undefined);
|
|
24
|
+
if (schema === undefined ? product.persistence !== 'transient' && !rows : typeof schema !== 'boolean' && !isRecord(schema)) {
|
|
25
|
+
throw new Error(`Missing or invalid product schema for ${contract} in plugin ${id}`);
|
|
26
|
+
}
|
|
27
|
+
if (metrics !== undefined && (!Array.isArray(metrics) || !metrics.every(isMetricDescriptor))) {
|
|
28
|
+
throw new Error(`Invalid metric descriptors for ${contract} in plugin ${id}`);
|
|
29
|
+
}
|
|
30
|
+
const declaration = product;
|
|
31
|
+
for (const metric of declaration.metrics ?? []) {
|
|
32
|
+
if (catalog.has(metric.id))
|
|
33
|
+
throw new Error(`Duplicate metric ${metric.id} in plugin ${id}`);
|
|
34
|
+
catalog.set(metric.id, metric);
|
|
35
|
+
}
|
|
36
|
+
products.set(contract, declaration);
|
|
37
|
+
}
|
|
38
|
+
const undeclared = rules?.find(rule => rule.measure !== undefined && !catalog.has(rule.measure));
|
|
39
|
+
if (undeclared)
|
|
40
|
+
throw new Error(`Rule ${undeclared.id} in plugin ${id} references undeclared metric ${undeclared.measure}`);
|
|
41
|
+
for (const metric of catalog.values()) {
|
|
42
|
+
if (!metric.ratio)
|
|
43
|
+
continue;
|
|
44
|
+
const invalid = [metric.ratio.of, metric.ratio.over].find(component => component === metric.id || (!catalog.has(component) && !coreMetrics.has(component)));
|
|
45
|
+
if (invalid !== undefined)
|
|
46
|
+
throw new Error(`Ratio ${metric.id} in plugin ${id} references invalid component ${invalid}`);
|
|
47
|
+
}
|
|
48
|
+
return products;
|
|
49
|
+
}
|
|
50
|
+
function manifestDeclarations(plugin) {
|
|
51
|
+
const { id, kinds: declaredKinds, claims, providesLanguages, produces, rules, config } = plugin;
|
|
52
|
+
if (declaredKinds === undefined && claims === undefined && providesLanguages === undefined && produces === undefined) {
|
|
53
|
+
throw new Error(`Missing plugin manifest for ${id}. Declare kinds and produces.`);
|
|
54
|
+
}
|
|
55
|
+
if (Object.hasOwn(plugin, 'languages'))
|
|
56
|
+
throw new Error(`Plugin ${id} must declare languages in claims.languages`);
|
|
57
|
+
if (!strings(declaredKinds) || declaredKinds.length === 0 || declaredKinds.some(kind => !kinds.has(kind))) {
|
|
58
|
+
throw new Error(`Invalid kinds for plugin ${id}`);
|
|
59
|
+
}
|
|
60
|
+
if (claims !== undefined && (!isRecord(claims) || Object.entries(claims).some(([key, values]) => key === 'scanOnly' ? typeof values !== 'boolean'
|
|
61
|
+
: !['extensions', 'globs', 'languages'].includes(key) || !strings(values)
|
|
62
|
+
|| (key === 'extensions' && !values.every(isExtension))))) {
|
|
63
|
+
throw new Error(`Invalid file claims for plugin ${id}`);
|
|
64
|
+
}
|
|
65
|
+
if (providesLanguages !== undefined && (!declaredKinds.includes('language') || !validLanguages(providesLanguages))) {
|
|
66
|
+
throw new Error(`Invalid provided languages for plugin ${id}`);
|
|
67
|
+
}
|
|
68
|
+
if (!Array.isArray(produces))
|
|
69
|
+
throw new Error(`Plugin ${id} must declare its products`);
|
|
70
|
+
if (rules !== undefined && (!Array.isArray(rules) || !rules.every(isRuleDescriptor)
|
|
71
|
+
|| new Set(rules.map(rule => rule.id)).size !== rules.length))
|
|
72
|
+
throw new Error(`Invalid or duplicate rules for plugin ${id}`);
|
|
73
|
+
if (config !== undefined && typeof config !== 'boolean' && !isRecord(config))
|
|
74
|
+
throw new Error(`Invalid configuration schema for plugin ${id}`);
|
|
75
|
+
return { id, produces, rules };
|
|
76
|
+
}
|
|
77
|
+
function validLanguages(values) {
|
|
78
|
+
if (!Array.isArray(values) || values.length === 0)
|
|
79
|
+
return false;
|
|
80
|
+
const ids = new Set();
|
|
81
|
+
for (const value of values) {
|
|
82
|
+
if (!isRecord(value) || !isLanguageDisplay(value) || ids.has(value.id))
|
|
83
|
+
return false;
|
|
84
|
+
const { extensions, globs, measures, syntax } = value;
|
|
85
|
+
if (syntax !== undefined && !isLanguageSyntax(syntax))
|
|
86
|
+
return false;
|
|
87
|
+
if (extensions !== undefined && (!strings(extensions) || !extensions.every(isExtension)))
|
|
88
|
+
return false;
|
|
89
|
+
if (globs !== undefined && !strings(globs))
|
|
90
|
+
return false;
|
|
91
|
+
if (measures !== undefined && (!strings(measures) || measures.some(measure => !structureMeasures.includes(measure))
|
|
92
|
+
|| measures.includes('duplication') && !measures.includes('codeLines')))
|
|
93
|
+
return false;
|
|
94
|
+
if (!(Array.isArray(extensions) && extensions.length || Array.isArray(globs) && globs.length))
|
|
95
|
+
return false;
|
|
96
|
+
ids.add(value.id);
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
function isRuleDescriptor(value) {
|
|
101
|
+
return isRecord(value) && text(value.id) && text(value.category) && optionalText(value.description)
|
|
102
|
+
&& optionalText(value.measure) && (value.per === undefined || (Array.isArray(value.per) && value.per.length === 2 && value.per.every(text)))
|
|
103
|
+
&& (value.defaults === undefined || (isRecord(value.defaults)
|
|
104
|
+
&& Object.entries(value.defaults).every(([key, amount]) => (key === 'limit' || key === 'cost')
|
|
105
|
+
&& (amount === undefined || (typeof amount === 'number' && Number.isFinite(amount) && amount >= 0)))));
|
|
106
|
+
}
|
|
107
|
+
export function isMetricDescriptor(value) {
|
|
108
|
+
return isRecord(value) && text(value.id) && text(value.label)
|
|
109
|
+
&& text(value.unit) && units.has(value.unit) && text(value.scope) && scopes.has(value.scope)
|
|
110
|
+
&& text(value.aggregate) && aggregates.has(value.aggregate) && text(value.better) && directions.has(value.better)
|
|
111
|
+
&& optionalText(value.short) && optionalText(value.description) && optionalText(value.group)
|
|
112
|
+
&& (value.ratio === undefined || (value.unit === 'ratio' && isRecord(value.ratio) && text(value.ratio.of) && text(value.ratio.over)))
|
|
113
|
+
&& (value.bands === undefined || (Array.isArray(value.bands) && value.bands.length === 4
|
|
114
|
+
&& value.bands.every((limit, index, array) => typeof limit === 'number' && Number.isFinite(limit)
|
|
115
|
+
&& (index === 0 || limit > array[index - 1]))));
|
|
116
|
+
}
|
|
117
|
+
function isExtension(value) {
|
|
118
|
+
return /^\.[\w-]+$/.test(value);
|
|
119
|
+
}
|
|
120
|
+
function strings(value) {
|
|
121
|
+
return Array.isArray(value) && value.every(text) && new Set(value).size === value.length;
|
|
122
|
+
}
|
|
123
|
+
function text(value) {
|
|
124
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
125
|
+
}
|
|
126
|
+
function optionalText(value) {
|
|
127
|
+
return value === undefined || text(value);
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=manifest.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PlannedOperation } from './plan.js';
|
|
2
|
+
import type { Product } from './plugin.js';
|
|
3
|
+
export interface OperationCache {
|
|
4
|
+
read(key: string): Promise<Uint8Array | undefined>;
|
|
5
|
+
write(key: string, bytes: Uint8Array): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export interface OperationCacheOptions {
|
|
8
|
+
readonly store: OperationCache;
|
|
9
|
+
/** Digest of effective settings that the input products do not carry. */
|
|
10
|
+
readonly configurationDigest: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function operationCacheKey(task: PlannedOperation, inputs: ReadonlyMap<string, Product>, configurationDigest: string): string | undefined;
|
|
13
|
+
export declare function readCachedOperation(store: OperationCache, key: string, task: PlannedOperation): Promise<Product | undefined>;
|
|
14
|
+
export declare function writeCachedOperation(store: OperationCache, key: string, product: Product): Promise<void>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { deserialize, serialize } from 'node:v8';
|
|
2
|
+
import { digest } from '../model/snapshot.js';
|
|
3
|
+
import { isRecord } from './plugin.js';
|
|
4
|
+
export function operationCacheKey(task, inputs, configurationDigest) {
|
|
5
|
+
if (task.cacheIdentity === undefined)
|
|
6
|
+
return undefined;
|
|
7
|
+
const argumentsByPort = [...inputs];
|
|
8
|
+
try {
|
|
9
|
+
if (argumentsByPort.some(([, { value }]) => !cacheData(value)))
|
|
10
|
+
return undefined;
|
|
11
|
+
return digest(serialize([
|
|
12
|
+
'repometry.operation-cache/v1', process.version, task.key, task.cacheIdentity, configurationDigest,
|
|
13
|
+
[...task.inputs], argumentsByPort,
|
|
14
|
+
]));
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export async function readCachedOperation(store, key, task) {
|
|
21
|
+
try {
|
|
22
|
+
const bytes = await store.read(key);
|
|
23
|
+
if (!bytes)
|
|
24
|
+
return undefined;
|
|
25
|
+
const product = deserialize(bytes);
|
|
26
|
+
const output = task.operation.output;
|
|
27
|
+
if (isRecord(product) && product.contract === output.id && Object.hasOwn(product, 'value') && cacheData(product.value)
|
|
28
|
+
&& output.accepts(product.value) && (!task.validateOutput || task.validateOutput(product.value))) {
|
|
29
|
+
return { contract: output.id, value: product.value };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// Unreadable cache entries are misses.
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
export async function writeCachedOperation(store, key, product) {
|
|
38
|
+
try {
|
|
39
|
+
if (cacheData(product.value))
|
|
40
|
+
await store.write(key, serialize(product));
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// Cache publication is optional once an inspection has a valid result.
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function cacheData(value, seen = new Set()) {
|
|
47
|
+
if (value === null)
|
|
48
|
+
return true;
|
|
49
|
+
if (typeof value !== 'object')
|
|
50
|
+
return ['undefined', 'boolean', 'number', 'bigint', 'string'].includes(typeof value);
|
|
51
|
+
const prototype = Object.getPrototypeOf(value);
|
|
52
|
+
if (ArrayBuffer.isView(value))
|
|
53
|
+
return prototype === Buffer.prototype || prototype === Uint8Array.prototype;
|
|
54
|
+
const array = Array.isArray(value);
|
|
55
|
+
if (prototype !== (array ? Array.prototype : Object.prototype))
|
|
56
|
+
return false;
|
|
57
|
+
if (seen.has(value))
|
|
58
|
+
return true;
|
|
59
|
+
seen.add(value);
|
|
60
|
+
return Reflect.ownKeys(value).every(key => {
|
|
61
|
+
const property = Object.getOwnPropertyDescriptor(value, key);
|
|
62
|
+
return typeof key === 'string' && property !== undefined && 'value' in property
|
|
63
|
+
&& (property.enumerable || (array && key === 'length')) && cacheData(property.value, seen);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=operation-cache.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Operation, Plugin, ProductDeclaration } from './plugin.js';
|
|
2
|
+
export type InputBinding = {
|
|
3
|
+
readonly external: string;
|
|
4
|
+
} | {
|
|
5
|
+
readonly operation: string;
|
|
6
|
+
};
|
|
7
|
+
export interface PlanRequest {
|
|
8
|
+
readonly plugins: readonly Plugin[];
|
|
9
|
+
readonly select: readonly string[];
|
|
10
|
+
readonly inputs: ReadonlyMap<string, string>;
|
|
11
|
+
readonly bindings?: ReadonlyMap<string, Readonly<Record<string, InputBinding>>>;
|
|
12
|
+
}
|
|
13
|
+
export interface PlannedOperation {
|
|
14
|
+
readonly key: string;
|
|
15
|
+
readonly operation: Operation;
|
|
16
|
+
readonly inputs: ReadonlyMap<string, InputBinding>;
|
|
17
|
+
readonly declaration?: ProductDeclaration;
|
|
18
|
+
readonly validateOutput?: (value: unknown) => boolean;
|
|
19
|
+
readonly cacheIdentity?: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function plan(request: PlanRequest): readonly PlannedOperation[];
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Ajv2020 } from 'ajv/dist/2020.js';
|
|
2
|
+
import { isJsonValue } from '../model/plugin-data.js';
|
|
3
|
+
import { pluginApi } from './plugin.js';
|
|
4
|
+
import { manifestProducts } from './manifest.js';
|
|
5
|
+
export function plan(request) {
|
|
6
|
+
const { operations, declarations, cacheIdentities } = indexOperations(request.plugins);
|
|
7
|
+
const resolved = new Map();
|
|
8
|
+
const path = [];
|
|
9
|
+
function visit(key) {
|
|
10
|
+
const cycle = path.indexOf(key);
|
|
11
|
+
if (cycle >= 0)
|
|
12
|
+
throw new Error(`Plugin dependency cycle: ${[...path.slice(cycle), key].join(' -> ')}`);
|
|
13
|
+
if (resolved.has(key))
|
|
14
|
+
return;
|
|
15
|
+
const operation = operations.get(key);
|
|
16
|
+
if (!operation)
|
|
17
|
+
throw new Error(`Unknown operation: ${key}`);
|
|
18
|
+
path.push(key);
|
|
19
|
+
const inputs = new Map();
|
|
20
|
+
const declared = request.bindings?.get(key);
|
|
21
|
+
for (const name of Object.keys(declared ?? {})) {
|
|
22
|
+
if (!Object.hasOwn(operation.inputs, name))
|
|
23
|
+
throw new Error(`Unknown input port: ${key}.${name}`);
|
|
24
|
+
}
|
|
25
|
+
for (const [name, contract] of Object.entries(operation.inputs)) {
|
|
26
|
+
const explicit = declared && Object.hasOwn(declared, name) ? declared[name] : undefined;
|
|
27
|
+
const candidates = explicit ? [explicit] : candidatesFor(contract.id, request.inputs, operations);
|
|
28
|
+
const binding = candidates[0];
|
|
29
|
+
if (!binding)
|
|
30
|
+
throw new Error(`Missing input ${key}.${name} (${contract.id})`);
|
|
31
|
+
if (candidates.length > 1)
|
|
32
|
+
throw new Error(`Ambiguous input ${key}.${name} (${contract.id})`);
|
|
33
|
+
if (('external' in binding) === ('operation' in binding)) {
|
|
34
|
+
throw new Error(`Expected one binding for ${key}.${name}`);
|
|
35
|
+
}
|
|
36
|
+
const actual = 'external' in binding
|
|
37
|
+
? request.inputs.get(binding.external)
|
|
38
|
+
: operations.get(binding.operation)?.output.id;
|
|
39
|
+
if (actual !== contract.id)
|
|
40
|
+
throw new Error(`Incompatible input ${key}.${name}: expected ${contract.id}, got ${actual ?? 'missing'}`);
|
|
41
|
+
if ('operation' in binding)
|
|
42
|
+
visit(binding.operation);
|
|
43
|
+
inputs.set(name, binding);
|
|
44
|
+
}
|
|
45
|
+
path.pop();
|
|
46
|
+
const declaration = declarations.get(key);
|
|
47
|
+
const validateOutput = declaration?.schema === undefined ? undefined : validateSchema(declaration.schema, key);
|
|
48
|
+
const cacheIdentity = cacheIdentities.get(key);
|
|
49
|
+
resolved.set(key, { key, operation, inputs, ...(declaration === undefined ? {} : { declaration }),
|
|
50
|
+
...(validateOutput === undefined ? {} : { validateOutput }), ...(cacheIdentity === undefined ? {} : { cacheIdentity }) });
|
|
51
|
+
}
|
|
52
|
+
for (const key of request.select)
|
|
53
|
+
visit(key);
|
|
54
|
+
return [...resolved.values()];
|
|
55
|
+
}
|
|
56
|
+
function validateSchema(schema, key) {
|
|
57
|
+
try {
|
|
58
|
+
return new Ajv2020().compile(schema);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
throw new Error(`Invalid product schema for ${key}`, { cause: error });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function indexOperations(plugins) {
|
|
65
|
+
const ids = new Set();
|
|
66
|
+
const operations = new Map();
|
|
67
|
+
const declarations = new Map();
|
|
68
|
+
const cacheIdentities = new Map();
|
|
69
|
+
const metricOwners = new Map();
|
|
70
|
+
for (const plugin of plugins) {
|
|
71
|
+
if (plugin.api !== pluginApi)
|
|
72
|
+
throw new Error(`Unsupported plugin API for ${plugin.id}: ${plugin.api}`);
|
|
73
|
+
if (ids.has(plugin.id) || !plugin.id || plugin.id.includes('/'))
|
|
74
|
+
throw new Error(`Invalid or duplicate plugin ID: ${plugin.id}`);
|
|
75
|
+
ids.add(plugin.id);
|
|
76
|
+
const products = manifestProducts(plugin);
|
|
77
|
+
const hasSettings = plugin.settings !== undefined;
|
|
78
|
+
if (hasSettings && !isJsonValue(plugin.settings))
|
|
79
|
+
throw new Error(`Plugin settings must be JSON data: ${plugin.id}`);
|
|
80
|
+
if (hasSettings && plugin.config !== undefined && !new Ajv2020().validate(plugin.config, plugin.settings)) {
|
|
81
|
+
throw new Error(`Plugin settings violate the configuration schema: ${plugin.id}`);
|
|
82
|
+
}
|
|
83
|
+
for (const declaration of products.values()) {
|
|
84
|
+
for (const metric of declaration.metrics ?? []) {
|
|
85
|
+
const owner = metricOwners.get(metric.id);
|
|
86
|
+
if (owner !== undefined)
|
|
87
|
+
throw new Error(`Duplicate metric ${metric.id} in plugins ${owner} and ${plugin.id}`);
|
|
88
|
+
metricOwners.set(metric.id, plugin.id);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (const operation of plugin.operations) {
|
|
92
|
+
const key = `${plugin.id}/${operation.id}`;
|
|
93
|
+
if (operations.has(key) || !operation.id || operation.id.includes('/'))
|
|
94
|
+
throw new Error(`Invalid or duplicate operation: ${key}`);
|
|
95
|
+
if (operation.scope !== undefined && operation.scope !== 'file' && operation.scope !== 'repository') {
|
|
96
|
+
throw new Error(`Invalid operation scope: ${key}`);
|
|
97
|
+
}
|
|
98
|
+
const declaration = products.get(operation.output.id);
|
|
99
|
+
if (!declaration)
|
|
100
|
+
throw new Error(`Undeclared output for ${key}: ${operation.output.id}`);
|
|
101
|
+
declarations.set(key, declaration);
|
|
102
|
+
if (declaration.persistence === 'cache') {
|
|
103
|
+
cacheIdentities.set(key, JSON.stringify([
|
|
104
|
+
plugin.api, plugin.version, Object.entries(plugin.tools ?? {}).sort(), plugin.settings, operation.scope, operation.implementation,
|
|
105
|
+
declaration.contract, declaration.schema,
|
|
106
|
+
]));
|
|
107
|
+
}
|
|
108
|
+
operations.set(key, operation);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return { operations, declarations, cacheIdentities };
|
|
112
|
+
}
|
|
113
|
+
function candidatesFor(contract, inputs, operations) {
|
|
114
|
+
const candidates = [];
|
|
115
|
+
for (const [external, id] of inputs)
|
|
116
|
+
if (id === contract)
|
|
117
|
+
candidates.push({ external });
|
|
118
|
+
for (const [operation, candidate] of operations)
|
|
119
|
+
if (candidate.output.id === contract)
|
|
120
|
+
candidates.push({ operation });
|
|
121
|
+
return candidates;
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=plan.js.map
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { MetricDescriptor } from '../model/metric-descriptor.js';
|
|
2
|
+
import type { ProvidedLanguage } from '../model/language.js';
|
|
3
|
+
import type { JsonValue } from '../model/plugin-data.js';
|
|
4
|
+
export type { MetricDescriptor, MetricScope } from '../model/metric-descriptor.js';
|
|
5
|
+
export { isRecord } from '../model/plugin-data.js';
|
|
6
|
+
export declare const pluginApi = "repometry.plugin/v1";
|
|
7
|
+
export interface DataContract<T = unknown> {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly accepts: (value: unknown) => value is T;
|
|
10
|
+
}
|
|
11
|
+
export interface Product {
|
|
12
|
+
readonly contract: string;
|
|
13
|
+
readonly value: unknown;
|
|
14
|
+
}
|
|
15
|
+
export interface InspectionInputs {
|
|
16
|
+
read<T>(port: string, contract: DataContract<T>): T;
|
|
17
|
+
emitDiagnostic?(message: string, path?: string): void;
|
|
18
|
+
}
|
|
19
|
+
export interface Operation {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly role: 'provider' | 'inspection';
|
|
22
|
+
readonly scope?: 'file' | 'repository';
|
|
23
|
+
/** A repository provider produces the same results and diagnostics for each file regardless of which siblings it receives. */
|
|
24
|
+
readonly fileIndependent?: boolean;
|
|
25
|
+
readonly inputs: Readonly<Record<string, DataContract>>;
|
|
26
|
+
readonly output: DataContract;
|
|
27
|
+
/** Fingerprint for executable details that the declared versions and inputs do not carry. */
|
|
28
|
+
readonly implementation?: string;
|
|
29
|
+
execute(inputs: InspectionInputs): unknown | Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
export interface RuleDescriptor {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly category: string;
|
|
34
|
+
readonly description?: string;
|
|
35
|
+
readonly defaults?: {
|
|
36
|
+
readonly limit?: number;
|
|
37
|
+
readonly cost?: number;
|
|
38
|
+
};
|
|
39
|
+
readonly measure?: string;
|
|
40
|
+
readonly per?: readonly [singular: string, plural: string];
|
|
41
|
+
}
|
|
42
|
+
export type PluginConfigSchema = boolean | Readonly<Record<string, unknown>>;
|
|
43
|
+
export type PluginKind = 'language' | 'analysis' | 'coverage' | 'security' | 'import' | 'source';
|
|
44
|
+
export interface FileClaims {
|
|
45
|
+
readonly extensions?: readonly string[];
|
|
46
|
+
readonly globs?: readonly string[];
|
|
47
|
+
readonly languages?: readonly string[];
|
|
48
|
+
/** Run inspections for matching files without counting them as analysed in the inventory. */
|
|
49
|
+
readonly scanOnly?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface ProductDeclaration {
|
|
52
|
+
readonly contract: string;
|
|
53
|
+
readonly persistence: 'report' | 'cache' | 'transient';
|
|
54
|
+
readonly schema?: PluginConfigSchema;
|
|
55
|
+
readonly metrics?: readonly MetricDescriptor[];
|
|
56
|
+
}
|
|
57
|
+
export interface Plugin {
|
|
58
|
+
readonly id: string;
|
|
59
|
+
readonly version: string;
|
|
60
|
+
readonly api: string;
|
|
61
|
+
readonly tools?: Readonly<Record<string, string>>;
|
|
62
|
+
readonly operations: readonly Operation[];
|
|
63
|
+
readonly rules?: readonly RuleDescriptor[];
|
|
64
|
+
readonly config?: PluginConfigSchema;
|
|
65
|
+
/** Effective non-secret settings saved with inspection provenance. */
|
|
66
|
+
readonly settings?: JsonValue;
|
|
67
|
+
readonly kinds: readonly PluginKind[];
|
|
68
|
+
readonly claims?: FileClaims;
|
|
69
|
+
readonly providesLanguages?: readonly ProvidedLanguage[];
|
|
70
|
+
readonly produces: readonly ProductDeclaration[];
|
|
71
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FindingResult } from '../model/finding.js';
|
|
2
|
+
import type { PluginData } from '../model/plugin-data.js';
|
|
3
|
+
import type { Plugin, Product } from './plugin.js';
|
|
4
|
+
import type { PlannedOperation } from './plan.js';
|
|
5
|
+
export declare class ReportProducts {
|
|
6
|
+
private readonly metrics;
|
|
7
|
+
private readonly measured;
|
|
8
|
+
private readonly privateMetrics;
|
|
9
|
+
private readonly products;
|
|
10
|
+
private readonly contracts;
|
|
11
|
+
private readonly rules;
|
|
12
|
+
private readonly declared;
|
|
13
|
+
constructor(plugins: readonly Plugin[]);
|
|
14
|
+
record(task: PlannedOperation, { contract, value }: Product, path?: string): FindingResult | undefined;
|
|
15
|
+
result(): PluginData | undefined;
|
|
16
|
+
}
|