mandrel 2.75.0 → 2.76.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/.agents/audit-checklists/exceptions.md +24 -0
- package/.agents/docs/workflows.md +2 -1
- package/.agents/schemas/audit-exceptions-envelope.schema.json +268 -0
- package/.agents/schemas/audit-rules.json +43 -0
- package/.agents/scripts/audit-exceptions.js +145 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/ci-gates.js +65 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/code-allowlists.js +143 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-audit.js +160 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-patches.js +124 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-pins.js +177 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-policy.js +243 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/index.js +30 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/inline-suppressions.js +107 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/sca-exceptions.js +55 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/test-skips.js +96 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/tool-config.js +273 -0
- package/.agents/scripts/lib/audit-exceptions/classify.js +64 -0
- package/.agents/scripts/lib/audit-exceptions/dependency-index.js +266 -0
- package/.agents/scripts/lib/audit-exceptions/engine.js +249 -0
- package/.agents/scripts/lib/audit-exceptions/globs.js +106 -0
- package/.agents/scripts/lib/audit-exceptions/history.js +126 -0
- package/.agents/scripts/lib/audit-exceptions/locate.js +59 -0
- package/.agents/scripts/lib/audit-exceptions/probe.js +196 -0
- package/.agents/scripts/lib/audit-exceptions/range-subset.js +153 -0
- package/.agents/scripts/lib/audit-exceptions/read.js +101 -0
- package/.agents/scripts/lib/audit-exceptions/record.js +102 -0
- package/.agents/scripts/lib/audit-exceptions/scope.js +131 -0
- package/.agents/scripts/lib/audit-exceptions/tickets.js +98 -0
- package/.agents/scripts/lib/audit-to-stories/audit-lenses.js +1 -0
- package/.agents/scripts/lib/observability/source-classifier.js +1 -0
- package/.agents/workflows/audit-exceptions.md +235 -0
- package/docs/CHANGELOG.md +12 -0
- package/package.json +1 -1
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exemptions declared in tool configuration: biome / eslint / knip / coverage /
|
|
3
|
+
* markdownlint / prettier ignore globs and rule-disabling overrides, and
|
|
4
|
+
* tsconfig excludes plus loosened compiler strictness. JS-format configs are
|
|
5
|
+
* never evaluated; their presence is recorded as a degradation.
|
|
6
|
+
*
|
|
7
|
+
* @module lib/audit-exceptions/adapters/tool-config
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { globsProbe, ignoreLineGlobs } from '../globs.js';
|
|
12
|
+
import { lineOf } from '../locate.js';
|
|
13
|
+
import { readJsonc, readText } from '../read.js';
|
|
14
|
+
import { makeRecord } from '../record.js';
|
|
15
|
+
|
|
16
|
+
const JS_CONFIG_RE =
|
|
17
|
+
/(?:^|\/)(?:eslint\.config|\.eslintrc|\.c8rc|\.nycrc|vitest\.config|jest\.config)\.(?:[cm]?[jt]s)$/;
|
|
18
|
+
|
|
19
|
+
const strings = (list) =>
|
|
20
|
+
Array.isArray(list) ? list.filter((g) => typeof g === 'string') : [];
|
|
21
|
+
|
|
22
|
+
/** Does an override object switch anything off? */
|
|
23
|
+
const disablesSomething = (override) =>
|
|
24
|
+
/"off"|"enabled":\s*false/.test(JSON.stringify(override));
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One JSON-config source: which files, and how to pick its glob exemptions.
|
|
28
|
+
* `pick(config)` → `[{ rule, globs, needle }]`.
|
|
29
|
+
*/
|
|
30
|
+
const JSON_SOURCES = Object.freeze([
|
|
31
|
+
{
|
|
32
|
+
surface: 'biome',
|
|
33
|
+
files: ['biome.json', 'biome.jsonc'],
|
|
34
|
+
pick: (c) => [
|
|
35
|
+
...['files', 'linter', 'formatter'].flatMap((k) =>
|
|
36
|
+
strings(c[k]?.ignore).map((g) => ({
|
|
37
|
+
rule: `${k}.ignore`,
|
|
38
|
+
globs: [g],
|
|
39
|
+
needle: g,
|
|
40
|
+
})),
|
|
41
|
+
),
|
|
42
|
+
...strings(c.files?.includes)
|
|
43
|
+
.filter((g) => g.startsWith('!'))
|
|
44
|
+
.map((g) => ({
|
|
45
|
+
rule: 'files.includes-negation',
|
|
46
|
+
globs: [g.slice(1)],
|
|
47
|
+
needle: g,
|
|
48
|
+
})),
|
|
49
|
+
...(c.overrides ?? []).filter(disablesSomething).map((o) => {
|
|
50
|
+
const globs = strings(o.includes ?? o.include);
|
|
51
|
+
return { rule: 'overrides', globs, needle: globs[0] ?? 'overrides' };
|
|
52
|
+
}),
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
surface: 'eslint',
|
|
57
|
+
files: ['.eslintrc', '.eslintrc.json'],
|
|
58
|
+
pick: (c) => [
|
|
59
|
+
...strings(c.ignorePatterns).map((g) => ({
|
|
60
|
+
rule: 'ignorePatterns',
|
|
61
|
+
globs: [g],
|
|
62
|
+
needle: g,
|
|
63
|
+
})),
|
|
64
|
+
...(c.overrides ?? []).filter(disablesSomething).map((o) => {
|
|
65
|
+
const globs = strings([o.files].flat());
|
|
66
|
+
return { rule: 'overrides', globs, needle: globs[0] ?? 'overrides' };
|
|
67
|
+
}),
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
surface: 'knip',
|
|
72
|
+
files: ['knip.json', 'knip.jsonc', '.knip.json', '.knip.jsonc'],
|
|
73
|
+
pick: (c) =>
|
|
74
|
+
['ignore', 'ignoreFiles'].flatMap((k) =>
|
|
75
|
+
strings([c[k]].flat()).map((g) => ({ rule: k, globs: [g], needle: g })),
|
|
76
|
+
),
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
surface: 'coverage',
|
|
80
|
+
files: ['.c8rc', '.c8rc.json', '.nycrc', '.nycrc.json'],
|
|
81
|
+
pick: (c) =>
|
|
82
|
+
strings(c.exclude).map((g) => ({
|
|
83
|
+
rule: 'exclude',
|
|
84
|
+
globs: [g],
|
|
85
|
+
needle: g,
|
|
86
|
+
})),
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
surface: 'markdownlint',
|
|
90
|
+
files: ['.markdownlint-cli2.jsonc', '.markdownlint-cli2.json'],
|
|
91
|
+
pick: (c) =>
|
|
92
|
+
strings(c.ignores).map((g) => ({
|
|
93
|
+
rule: 'ignores',
|
|
94
|
+
globs: [g],
|
|
95
|
+
needle: g,
|
|
96
|
+
})),
|
|
97
|
+
},
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
const IGNORE_FILES = Object.freeze({
|
|
101
|
+
'.eslintignore': 'eslint',
|
|
102
|
+
'.prettierignore': 'prettier',
|
|
103
|
+
'.markdownlintignore': 'markdownlint',
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const TS_LOOSENED = Object.freeze({
|
|
107
|
+
strict: false,
|
|
108
|
+
noImplicitAny: false,
|
|
109
|
+
strictNullChecks: false,
|
|
110
|
+
skipLibCheck: true,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
function configRecord(
|
|
114
|
+
ctx,
|
|
115
|
+
{
|
|
116
|
+
surface,
|
|
117
|
+
file,
|
|
118
|
+
text,
|
|
119
|
+
rule,
|
|
120
|
+
target,
|
|
121
|
+
globs,
|
|
122
|
+
justification,
|
|
123
|
+
probe,
|
|
124
|
+
permanentHint,
|
|
125
|
+
anchors,
|
|
126
|
+
},
|
|
127
|
+
) {
|
|
128
|
+
return makeRecord({
|
|
129
|
+
adapter: 'tool-config',
|
|
130
|
+
category: 'config',
|
|
131
|
+
surface,
|
|
132
|
+
file,
|
|
133
|
+
// Every target is a JSON string or key, so match it quoted.
|
|
134
|
+
line: lineOf(text, `"${target}"`, anchors),
|
|
135
|
+
target,
|
|
136
|
+
rule: `${surface}:${rule}`,
|
|
137
|
+
justification,
|
|
138
|
+
probe: probe ?? globsProbe(ctx, globs),
|
|
139
|
+
permanentHint,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function jsonSourceRecords(ctx, source, file, config) {
|
|
144
|
+
const text = readText(ctx.root, file);
|
|
145
|
+
return source.pick(config).map((p) =>
|
|
146
|
+
configRecord(ctx, {
|
|
147
|
+
surface: source.surface,
|
|
148
|
+
file,
|
|
149
|
+
text,
|
|
150
|
+
rule: p.rule,
|
|
151
|
+
target: p.needle,
|
|
152
|
+
globs: p.globs,
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function ignoreFileRecords(ctx, file, surface) {
|
|
158
|
+
const lines = (readText(ctx.root, file) ?? '').split('\n');
|
|
159
|
+
const dir = path.posix.dirname(file) === '.' ? '' : path.posix.dirname(file);
|
|
160
|
+
const records = [];
|
|
161
|
+
lines.forEach((raw, i) => {
|
|
162
|
+
const line = raw.trim();
|
|
163
|
+
if (line === '' || line.startsWith('#') || line.startsWith('!')) return;
|
|
164
|
+
const prev = lines[i - 1]?.trim() ?? '';
|
|
165
|
+
records.push(
|
|
166
|
+
makeRecord({
|
|
167
|
+
adapter: 'tool-config',
|
|
168
|
+
category: 'config',
|
|
169
|
+
surface,
|
|
170
|
+
file,
|
|
171
|
+
line: i + 1,
|
|
172
|
+
target: line,
|
|
173
|
+
rule: `${surface}:ignore-file`,
|
|
174
|
+
justification: prev.startsWith('#') ? prev.slice(1) : null,
|
|
175
|
+
probe: globsProbe(ctx, ignoreLineGlobs(line, dir)),
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
return records;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function tsconfigRecords(ctx, file) {
|
|
183
|
+
const config = readJsonc(ctx.root, file);
|
|
184
|
+
if (config === null) return [];
|
|
185
|
+
const text = readText(ctx.root, file);
|
|
186
|
+
const dir =
|
|
187
|
+
path.posix.dirname(file) === '.' ? '' : `${path.posix.dirname(file)}/`;
|
|
188
|
+
const base = { surface: 'typescript', file, text };
|
|
189
|
+
const records = strings(config.exclude).map((g) =>
|
|
190
|
+
configRecord(ctx, {
|
|
191
|
+
...base,
|
|
192
|
+
rule: 'exclude',
|
|
193
|
+
target: g,
|
|
194
|
+
globs: [`${dir}${g}`],
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
for (const [option, loosened] of Object.entries(TS_LOOSENED)) {
|
|
198
|
+
if (config.compilerOptions?.[option] !== loosened) continue;
|
|
199
|
+
records.push(
|
|
200
|
+
configRecord(ctx, {
|
|
201
|
+
...base,
|
|
202
|
+
rule: `compilerOptions.${option}`,
|
|
203
|
+
target: option,
|
|
204
|
+
probe: { verdict: 'live', basis: 'setting-in-force' },
|
|
205
|
+
permanentHint:
|
|
206
|
+
option === 'skipLibCheck' ? 'conventional-setting' : null,
|
|
207
|
+
}),
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
return records;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function manifestKeyRecords(ctx) {
|
|
214
|
+
const records = [];
|
|
215
|
+
for (const m of ctx.scope.manifests) {
|
|
216
|
+
const text = readText(ctx.root, m.rel);
|
|
217
|
+
for (const [key, surface] of [
|
|
218
|
+
['c8', 'coverage'],
|
|
219
|
+
['nyc', 'coverage'],
|
|
220
|
+
['knip', 'knip'],
|
|
221
|
+
]) {
|
|
222
|
+
const block = m.pkg[key];
|
|
223
|
+
if (!block) continue;
|
|
224
|
+
const source = JSON_SOURCES.find((s) => s.surface === surface);
|
|
225
|
+
for (const p of source.pick(block)) {
|
|
226
|
+
records.push(
|
|
227
|
+
configRecord(ctx, {
|
|
228
|
+
surface,
|
|
229
|
+
file: m.rel,
|
|
230
|
+
text,
|
|
231
|
+
rule: p.rule,
|
|
232
|
+
target: p.needle,
|
|
233
|
+
globs: p.globs,
|
|
234
|
+
anchors: [`"${key}"`],
|
|
235
|
+
}),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return records;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function extract(ctx) {
|
|
244
|
+
const { files, fileSet } = ctx.scope;
|
|
245
|
+
const records = [];
|
|
246
|
+
for (const source of JSON_SOURCES) {
|
|
247
|
+
for (const file of source.files.filter((f) => fileSet.has(f))) {
|
|
248
|
+
const config = readJsonc(ctx.root, file);
|
|
249
|
+
if (config === null) ctx.degrade('config-not-statically-readable', file);
|
|
250
|
+
else records.push(...jsonSourceRecords(ctx, source, file, config));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
for (const file of files) {
|
|
254
|
+
const base = path.posix.basename(file);
|
|
255
|
+
if (IGNORE_FILES[base])
|
|
256
|
+
records.push(...ignoreFileRecords(ctx, file, IGNORE_FILES[base]));
|
|
257
|
+
if (/^tsconfig.*\.json$/.test(base))
|
|
258
|
+
records.push(...tsconfigRecords(ctx, file));
|
|
259
|
+
if (JS_CONFIG_RE.test(file))
|
|
260
|
+
ctx.degrade('config-not-statically-readable', file);
|
|
261
|
+
}
|
|
262
|
+
return [...records, ...manifestKeyRecords(ctx)];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export const toolConfig = Object.freeze({
|
|
266
|
+
id: 'tool-config',
|
|
267
|
+
category: 'config',
|
|
268
|
+
applies: (ctx) =>
|
|
269
|
+
ctx.scope.files.length > 0
|
|
270
|
+
? { applies: true, reason: 'tracked files present' }
|
|
271
|
+
: { applies: false, reason: 'no tracked files' },
|
|
272
|
+
extract,
|
|
273
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settle each record's mechanical verdict. First match wins:
|
|
3
|
+
* `dead` → `expired` → `orphaned` → `unjustified` → `unknown` → `live`.
|
|
4
|
+
* Only a probe that PROVED the exception exempts nothing may say `dead`; an
|
|
5
|
+
* undecided probe says `unknown`, because a false `dead` invites deleting a
|
|
6
|
+
* load-bearing exception.
|
|
7
|
+
*
|
|
8
|
+
* @module lib/audit-exceptions/classify
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export const VERDICTS = Object.freeze([
|
|
12
|
+
'dead',
|
|
13
|
+
'expired',
|
|
14
|
+
'orphaned',
|
|
15
|
+
'unjustified',
|
|
16
|
+
'live',
|
|
17
|
+
'unknown',
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {object} record
|
|
22
|
+
* @param {Map<string, string>} ticketStates - ref → `open` | `closed`.
|
|
23
|
+
* @param {string} today - ISO date.
|
|
24
|
+
* @returns {{ verdict: string, verdictBasis: string }}
|
|
25
|
+
*/
|
|
26
|
+
function decide(record, ticketStates, today) {
|
|
27
|
+
if (record.probe?.verdict === 'dead') {
|
|
28
|
+
return { verdict: 'dead', verdictBasis: record.probe.basis };
|
|
29
|
+
}
|
|
30
|
+
if (record.expires && record.expires < today) {
|
|
31
|
+
return { verdict: 'expired', verdictBasis: 'expiry-date' };
|
|
32
|
+
}
|
|
33
|
+
const states = record.ticketRefs.map((ref) => ticketStates.get(ref));
|
|
34
|
+
if (states.length > 0 && states.every((s) => s === 'closed')) {
|
|
35
|
+
return { verdict: 'orphaned', verdictBasis: 'ticket-closed' };
|
|
36
|
+
}
|
|
37
|
+
// A permanent shape (an untracked build dir, a platform skip) needs no written reason.
|
|
38
|
+
if (!record.justification && !record.permanentHint) {
|
|
39
|
+
return { verdict: 'unjustified', verdictBasis: 'no-justification' };
|
|
40
|
+
}
|
|
41
|
+
if (record.probe?.verdict === 'unknown') {
|
|
42
|
+
return { verdict: 'unknown', verdictBasis: record.probe.basis };
|
|
43
|
+
}
|
|
44
|
+
return { verdict: 'live', verdictBasis: record.probe?.basis ?? 'none' };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Mutates and returns `records` with `verdict` / `verdictBasis` set, the
|
|
49
|
+
* probe's own finding kept as `probeBasis` (an `unjustified` pin a dependent
|
|
50
|
+
* still needs is a different finding from an unjustified redundant one), and
|
|
51
|
+
* the transient `probe` field removed.
|
|
52
|
+
*
|
|
53
|
+
* @param {object[]} records
|
|
54
|
+
* @param {{ ticketStates?: Map<string, string>, today: string }} opts
|
|
55
|
+
* @returns {object[]}
|
|
56
|
+
*/
|
|
57
|
+
export function classifyAll(records, { ticketStates = new Map(), today }) {
|
|
58
|
+
for (const record of records) {
|
|
59
|
+
Object.assign(record, decide(record, ticketStates, today));
|
|
60
|
+
record.probeBasis = record.probe?.basis ?? null;
|
|
61
|
+
delete record.probe;
|
|
62
|
+
}
|
|
63
|
+
return records;
|
|
64
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the dependency probes compare a pin against: which packages are in the
|
|
3
|
+
* tree at which versions (`present`), and which declared range every dependent
|
|
4
|
+
* asks for (`dependents`). Both come from offline sources only — the lockfile
|
|
5
|
+
* for presence; the npm lockfile or the installed manifests under
|
|
6
|
+
* `node_modules` (including the `.pnpm` store) for dependents' ranges. Either
|
|
7
|
+
* half is `null` when its source is missing, and the probe then says
|
|
8
|
+
* `unknown` rather than guessing.
|
|
9
|
+
*
|
|
10
|
+
* @module lib/audit-exceptions/dependency-index
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { detectPackageManager } from '../detect-package-manager.js';
|
|
16
|
+
import { readJsonc, readText, readYaml } from './read.js';
|
|
17
|
+
|
|
18
|
+
const DEP_FIELDS = Object.freeze([
|
|
19
|
+
'dependencies',
|
|
20
|
+
'devDependencies',
|
|
21
|
+
'optionalDependencies',
|
|
22
|
+
'peerDependencies',
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
/** Bounds the installed-manifest walk on a very large tree. */
|
|
26
|
+
const MAX_INSTALLED_MANIFESTS = 20000;
|
|
27
|
+
const MAX_NESTING = 4;
|
|
28
|
+
|
|
29
|
+
/** `@scope/name@1.2.3` / `name@npm:^1` → `@scope/name` / `name`. */
|
|
30
|
+
export function nameOfSpec(spec) {
|
|
31
|
+
const at = spec.indexOf('@', 1);
|
|
32
|
+
return at === -1 ? spec : spec.slice(0, at);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function addTo(map, key, value) {
|
|
36
|
+
if (!map.has(key)) map.set(key, []);
|
|
37
|
+
map.get(key).push(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function addPresent(present, name, version) {
|
|
41
|
+
if (!present.has(name)) present.set(name, new Set());
|
|
42
|
+
if (typeof version === 'string') present.get(name).add(version);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function addDependents(dependents, from, pkg) {
|
|
46
|
+
for (const field of DEP_FIELDS) {
|
|
47
|
+
for (const [name, range] of Object.entries(pkg?.[field] ?? {})) {
|
|
48
|
+
if (typeof range === 'string') addTo(dependents, name, { from, range });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function fromPackageLock(lock, present, dependents) {
|
|
54
|
+
for (const [key, entry] of Object.entries(lock?.packages ?? {})) {
|
|
55
|
+
const at = key.lastIndexOf('node_modules/');
|
|
56
|
+
const name =
|
|
57
|
+
at === -1 ? entry?.name : key.slice(at + 'node_modules/'.length);
|
|
58
|
+
if (name) addPresent(present, name, entry?.version);
|
|
59
|
+
addDependents(dependents, key || '(root)', entry);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** pnpm lockfile keys across v5 (`/a/1.0.0`), v6 (`/a@1.0.0`) and v9 (`a@1.0.0(peer)`). */
|
|
64
|
+
function parsePnpmKey(key) {
|
|
65
|
+
const bare = key.replace(/^\//, '').replace(/\(.*$/, '');
|
|
66
|
+
const at = bare.lastIndexOf('@');
|
|
67
|
+
if (at > 0) return { name: bare.slice(0, at), version: bare.slice(at + 1) };
|
|
68
|
+
const slash = bare.lastIndexOf('/');
|
|
69
|
+
return slash > 0
|
|
70
|
+
? { name: bare.slice(0, slash), version: bare.slice(slash + 1) }
|
|
71
|
+
: null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function fromPnpmLock(lock, present) {
|
|
75
|
+
for (const section of ['packages', 'snapshots']) {
|
|
76
|
+
for (const key of Object.keys(lock?.[section] ?? {})) {
|
|
77
|
+
const parsed = parsePnpmKey(key);
|
|
78
|
+
if (parsed) addPresent(present, parsed.name, parsed.version);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function fromYarnLock(text, present) {
|
|
84
|
+
let names = [];
|
|
85
|
+
for (const line of text.split('\n')) {
|
|
86
|
+
if (/^[^\s#].*:\s*$/.test(line)) {
|
|
87
|
+
names = line
|
|
88
|
+
.replace(/:\s*$/, '')
|
|
89
|
+
.split(/,\s*/)
|
|
90
|
+
.map((s) => nameOfSpec(s.replace(/^"|"$/g, '')));
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const version = /^\s+version:?\s+"?([^"\s]+)"?/.exec(line)?.[1];
|
|
94
|
+
if (version) for (const name of names) addPresent(present, name, version);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @param {string} root
|
|
100
|
+
* @param {string} pm
|
|
101
|
+
* @returns {Map<string, Set<string>>|null}
|
|
102
|
+
*/
|
|
103
|
+
/** npm lockfile v1 has no `packages` map: walk its nested `dependencies` tree. */
|
|
104
|
+
function fromPackageLockV1(deps, present, dependents) {
|
|
105
|
+
for (const [name, entry] of Object.entries(deps ?? {})) {
|
|
106
|
+
addPresent(present, name, entry?.version);
|
|
107
|
+
addDependents(dependents, `${name}@${entry?.version}`, {
|
|
108
|
+
dependencies: entry?.requires,
|
|
109
|
+
});
|
|
110
|
+
fromPackageLockV1(entry?.dependencies, present, dependents);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function fromNpmLock(lock, present, dependents) {
|
|
115
|
+
if (lock?.packages) fromPackageLock(lock, present, dependents);
|
|
116
|
+
else fromPackageLockV1(lock?.dependencies, present, dependents);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Lockfile per package manager, and how to read presence from it. */
|
|
120
|
+
const LOCK_READERS = Object.freeze({
|
|
121
|
+
npm: (root, present, dependents) =>
|
|
122
|
+
fromNpmLock(readJsonc(root, 'package-lock.json'), present, dependents),
|
|
123
|
+
pnpm: (root, present) =>
|
|
124
|
+
fromPnpmLock(readYaml(root, 'pnpm-lock.yaml'), present),
|
|
125
|
+
yarn: (root, present) =>
|
|
126
|
+
fromYarnLock(readText(root, 'yarn.lock') ?? '', present),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* `null` — "presence unknown" — when there is no lockfile, or when the one
|
|
131
|
+
* there yields no package at all (unparseable, an unrecognised format, empty).
|
|
132
|
+
* An empty map would read as "nothing is in the tree" and prove every pin
|
|
133
|
+
* dead, the one verdict the engine may never guess.
|
|
134
|
+
*
|
|
135
|
+
* @param {string} root
|
|
136
|
+
* @param {string|null} pm
|
|
137
|
+
* @param {Map} dependents
|
|
138
|
+
* @returns {Map<string, Set<string>>|null}
|
|
139
|
+
*/
|
|
140
|
+
function readPresence(root, pm, dependents) {
|
|
141
|
+
const read = LOCK_READERS[pm];
|
|
142
|
+
if (!read) return null;
|
|
143
|
+
if (pm === 'npm' && !fs.existsSync(path.join(root, 'package-lock.json')))
|
|
144
|
+
return null;
|
|
145
|
+
const present = new Map();
|
|
146
|
+
read(root, present, dependents);
|
|
147
|
+
return present.size > 0 ? present : null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function childPackageDirs(nodeModules) {
|
|
151
|
+
let entries;
|
|
152
|
+
try {
|
|
153
|
+
entries = fs.readdirSync(nodeModules, { withFileTypes: true });
|
|
154
|
+
} catch {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
const dirs = [];
|
|
158
|
+
for (const e of entries) {
|
|
159
|
+
if (e.name.startsWith('.')) continue;
|
|
160
|
+
const abs = path.join(nodeModules, e.name);
|
|
161
|
+
if (e.name.startsWith('@')) dirs.push(...childPackageDirs(abs));
|
|
162
|
+
else dirs.push(abs);
|
|
163
|
+
}
|
|
164
|
+
return dirs;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function pnpmStoreDirs(root) {
|
|
168
|
+
const store = path.join(root, 'node_modules', '.pnpm');
|
|
169
|
+
let entries;
|
|
170
|
+
try {
|
|
171
|
+
entries = fs.readdirSync(store);
|
|
172
|
+
} catch {
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
return entries
|
|
176
|
+
.filter(
|
|
177
|
+
(e) =>
|
|
178
|
+
e !== 'node_modules' && !e.startsWith('.') && !e.startsWith('lock'),
|
|
179
|
+
)
|
|
180
|
+
.flatMap((e) => childPackageDirs(path.join(store, e, 'node_modules')));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function walkInstalled(dir, depth, seen, visit) {
|
|
184
|
+
if (depth > MAX_NESTING || seen.size >= MAX_INSTALLED_MANIFESTS) return;
|
|
185
|
+
let real;
|
|
186
|
+
try {
|
|
187
|
+
real = fs.realpathSync(dir);
|
|
188
|
+
} catch {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (seen.has(real)) return;
|
|
192
|
+
seen.add(real);
|
|
193
|
+
const pkg = readJsonc(real, 'package.json');
|
|
194
|
+
if (pkg) visit(pkg);
|
|
195
|
+
for (const child of childPackageDirs(path.join(real, 'node_modules'))) {
|
|
196
|
+
walkInstalled(child, depth + 1, seen, visit);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @param {string} root
|
|
202
|
+
* @param {string[]} manifestDirs
|
|
203
|
+
* @param {Map} dependents
|
|
204
|
+
* @returns {boolean} whether any `node_modules` was readable.
|
|
205
|
+
*/
|
|
206
|
+
function readInstalled(root, manifestDirs, dependents) {
|
|
207
|
+
const starts = [
|
|
208
|
+
...manifestDirs.flatMap((d) =>
|
|
209
|
+
childPackageDirs(path.join(root, d, 'node_modules')),
|
|
210
|
+
),
|
|
211
|
+
...pnpmStoreDirs(root),
|
|
212
|
+
];
|
|
213
|
+
const seen = new Set();
|
|
214
|
+
for (const dir of starts) {
|
|
215
|
+
walkInstalled(dir, 0, seen, (pkg) =>
|
|
216
|
+
addDependents(dependents, `${pkg.name}@${pkg.version}`, pkg),
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return starts.length > 0;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @param {object} scope - from `buildScope`.
|
|
224
|
+
* @param {string} root
|
|
225
|
+
* @returns {{ pm: string|null, present: Map<string, Set<string>>|null,
|
|
226
|
+
* dependents: Map<string, Array<{from: string, range: string}>>|null,
|
|
227
|
+
* declared: Set<string>, degradations: object[] }}
|
|
228
|
+
*/
|
|
229
|
+
export function buildDependencyIndex(scope, root) {
|
|
230
|
+
const degradations = [];
|
|
231
|
+
const pm = detectPackageManager(root);
|
|
232
|
+
const dependents = new Map();
|
|
233
|
+
for (const m of scope.manifests) addDependents(dependents, m.rel, m.pkg);
|
|
234
|
+
const declared = new Set(dependents.keys());
|
|
235
|
+
const present = readPresence(root, pm, dependents);
|
|
236
|
+
if (present === null) {
|
|
237
|
+
degradations.push({
|
|
238
|
+
input: 'lockfile',
|
|
239
|
+
reason:
|
|
240
|
+
'no readable lockfile; presence probes fell back to declared manifests',
|
|
241
|
+
detail: '',
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
const fromLock = pm === 'npm' && present !== null;
|
|
245
|
+
const installed =
|
|
246
|
+
fromLock ||
|
|
247
|
+
readInstalled(
|
|
248
|
+
root,
|
|
249
|
+
scope.manifests.map((m) => m.dir),
|
|
250
|
+
dependents,
|
|
251
|
+
);
|
|
252
|
+
if (!installed) {
|
|
253
|
+
degradations.push({
|
|
254
|
+
input: 'dependency-manifests-unavailable',
|
|
255
|
+
reason: 'no installed manifests; redundancy probes were skipped',
|
|
256
|
+
detail: '',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
pm,
|
|
261
|
+
present,
|
|
262
|
+
dependents: installed ? dependents : null,
|
|
263
|
+
declared,
|
|
264
|
+
degradations,
|
|
265
|
+
};
|
|
266
|
+
}
|