@repometry/lockparse 0.0.0-stage → 0.0.1
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/package.json +31 -4
- package/target/main/ts/formats/bundler-gemfile.d.ts +1 -0
- package/target/main/ts/formats/bundler-gemfile.js +191 -0
- package/target/main/ts/formats/bundler-lock.d.ts +26 -0
- package/target/main/ts/formats/bundler-lock.js +110 -0
- package/target/main/ts/formats/bundler.d.ts +3 -0
- package/target/main/ts/formats/bundler.js +84 -0
- package/target/main/ts/formats/cargo-lock.d.ts +15 -0
- package/target/main/ts/formats/cargo-lock.js +127 -0
- package/target/main/ts/formats/cargo-manifest.d.ts +25 -0
- package/target/main/ts/formats/cargo-manifest.js +146 -0
- package/target/main/ts/formats/cargo-roots.d.ts +10 -0
- package/target/main/ts/formats/cargo-roots.js +116 -0
- package/target/main/ts/formats/cargo.d.ts +3 -0
- package/target/main/ts/formats/cargo.js +108 -0
- package/target/main/ts/formats/carton.d.ts +3 -0
- package/target/main/ts/formats/carton.js +127 -0
- package/target/main/ts/formats/composer.d.ts +5 -0
- package/target/main/ts/formats/composer.js +126 -0
- package/target/main/ts/formats/conan-reference.d.ts +11 -0
- package/target/main/ts/formats/conan-reference.js +32 -0
- package/target/main/ts/formats/conan.d.ts +3 -0
- package/target/main/ts/formats/conan.js +138 -0
- package/target/main/ts/formats/dart.d.ts +3 -0
- package/target/main/ts/formats/dart.js +122 -0
- package/target/main/ts/formats/go-mod.d.ts +28 -0
- package/target/main/ts/formats/go-mod.js +148 -0
- package/target/main/ts/formats/go.d.ts +3 -0
- package/target/main/ts/formats/go.js +74 -0
- package/target/main/ts/formats/gradle.d.ts +3 -0
- package/target/main/ts/formats/gradle.js +42 -0
- package/target/main/ts/formats/lines.d.ts +5 -0
- package/target/main/ts/formats/lines.js +12 -0
- package/target/main/ts/formats/mix-literals.d.ts +22 -0
- package/target/main/ts/formats/mix-literals.js +145 -0
- package/target/main/ts/formats/mix.d.ts +3 -0
- package/target/main/ts/formats/mix.js +90 -0
- package/target/main/ts/formats/nuget.d.ts +3 -0
- package/target/main/ts/formats/nuget.js +112 -0
- package/target/main/ts/formats/pdm.d.ts +3 -0
- package/target/main/ts/formats/pdm.js +52 -0
- package/target/main/ts/formats/pipenv.d.ts +3 -0
- package/target/main/ts/formats/pipenv.js +151 -0
- package/target/main/ts/formats/poetry-lock.d.ts +15 -0
- package/target/main/ts/formats/poetry-lock.js +68 -0
- package/target/main/ts/formats/poetry.d.ts +3 -0
- package/target/main/ts/formats/poetry.js +9 -0
- package/target/main/ts/formats/python-graph.d.ts +16 -0
- package/target/main/ts/formats/python-graph.js +91 -0
- package/target/main/ts/formats/python-package.d.ts +15 -0
- package/target/main/ts/formats/python-package.js +63 -0
- package/target/main/ts/formats/python-project.d.ts +12 -0
- package/target/main/ts/formats/python-project.js +76 -0
- package/target/main/ts/formats/renv.d.ts +3 -0
- package/target/main/ts/formats/renv.js +93 -0
- package/target/main/ts/formats/swift.d.ts +3 -0
- package/target/main/ts/formats/swift.js +87 -0
- package/target/main/ts/formats/terraform.d.ts +3 -0
- package/target/main/ts/formats/terraform.js +85 -0
- package/target/main/ts/formats/toml-document.d.ts +11 -0
- package/target/main/ts/formats/toml-document.js +79 -0
- package/target/main/ts/formats/toml-statements.d.ts +13 -0
- package/target/main/ts/formats/toml-statements.js +144 -0
- package/target/main/ts/formats/toml-values.d.ts +12 -0
- package/target/main/ts/formats/toml-values.js +111 -0
- package/target/main/ts/formats/uv-lock.d.ts +27 -0
- package/target/main/ts/formats/uv-lock.js +80 -0
- package/target/main/ts/formats/uv.d.ts +3 -0
- package/target/main/ts/formats/uv.js +114 -0
- package/target/main/ts/index.d.ts +14 -0
- package/target/main/ts/index.js +66 -0
- package/target/main/ts/json-spans.d.ts +11 -0
- package/target/main/ts/json-spans.js +86 -0
- package/target/main/ts/json.d.ts +2 -0
- package/target/main/ts/json.js +29 -0
- package/target/main/ts/model.d.ts +48 -0
- package/target/main/ts/model.js +2 -0
- package/README.md +0 -3
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { isSymbol, tomlKey, tomlStatements } from './toml-statements.js';
|
|
2
|
+
const dependencyGroups = new Set(['dependencies', 'dev-dependencies', 'build-dependencies']);
|
|
3
|
+
function nativeGroup(name) {
|
|
4
|
+
// Cargo still accepts the old underscore spelling, which remains observable in evidence.
|
|
5
|
+
return name.replaceAll('_', '-');
|
|
6
|
+
}
|
|
7
|
+
export function readCargoManifest(text, workspace) {
|
|
8
|
+
const fields = { workspace: false, members: [], exclude: [] };
|
|
9
|
+
const dependencies = new Map();
|
|
10
|
+
const workspaceDependencies = new Map();
|
|
11
|
+
let section = [];
|
|
12
|
+
for (const tokens of tomlStatements(text)) {
|
|
13
|
+
if (isSymbol(tokens[0], '[')) {
|
|
14
|
+
section = sectionKey(tokens);
|
|
15
|
+
if (section?.[0] === 'workspace')
|
|
16
|
+
fields.workspace = true;
|
|
17
|
+
}
|
|
18
|
+
else if (section !== undefined) {
|
|
19
|
+
const [key, value] = assignment(tokens);
|
|
20
|
+
const path = [...section, ...key];
|
|
21
|
+
recordFields(fields, path, value);
|
|
22
|
+
recordDeclaration(dependencies, workspaceDependencies, path, value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const parents = workspace?.workspaceDependencies ?? workspaceDependencies;
|
|
26
|
+
const version = fields.inheritedVersion ? (workspace?.workspaceVersion ?? fields.workspaceVersion) : fields.version;
|
|
27
|
+
return { ...fields, version, workspaceDependencies,
|
|
28
|
+
dependencies: [...dependencies.values()].flatMap(entry => resolvedDependency(entry, parents) ?? []) };
|
|
29
|
+
}
|
|
30
|
+
function sectionKey(tokens) {
|
|
31
|
+
if (!isSymbol(tokens.at(-1), ']'))
|
|
32
|
+
throw new Error('Invalid TOML section');
|
|
33
|
+
return isSymbol(tokens[1], '[') ? undefined : tomlKey(tokens.slice(1, -1));
|
|
34
|
+
}
|
|
35
|
+
function assignment(tokens) {
|
|
36
|
+
const separator = tokens.findIndex(token => isSymbol(token, '='));
|
|
37
|
+
if (separator < 1 || separator === tokens.length - 1)
|
|
38
|
+
throw new Error('Invalid TOML assignment');
|
|
39
|
+
return [tomlKey(tokens.slice(0, separator)), tokens.slice(separator + 1)];
|
|
40
|
+
}
|
|
41
|
+
function textValue(token) {
|
|
42
|
+
return token?.kind === 'text' ? token.value : undefined;
|
|
43
|
+
}
|
|
44
|
+
function isTrue(token) {
|
|
45
|
+
return token?.kind === 'bare' && token.value === 'true';
|
|
46
|
+
}
|
|
47
|
+
function fieldName(path) {
|
|
48
|
+
return path.join('\0');
|
|
49
|
+
}
|
|
50
|
+
function recordFields(fields, path, value) {
|
|
51
|
+
switch (fieldName(path)) {
|
|
52
|
+
case 'package\0name':
|
|
53
|
+
fields.name = textValue(value[0]);
|
|
54
|
+
break;
|
|
55
|
+
case 'package\0version':
|
|
56
|
+
fields.version = textValue(value[0]);
|
|
57
|
+
fields.inheritedVersion = isTrue(inlineFields(value).get('workspace'));
|
|
58
|
+
break;
|
|
59
|
+
case 'package\0version\0workspace':
|
|
60
|
+
fields.inheritedVersion = isTrue(value[0]);
|
|
61
|
+
break;
|
|
62
|
+
case 'workspace\0package\0version':
|
|
63
|
+
fields.workspaceVersion = textValue(value[0]);
|
|
64
|
+
break;
|
|
65
|
+
case 'workspace\0members':
|
|
66
|
+
fields.workspace = true;
|
|
67
|
+
fields.members = stringArray(value);
|
|
68
|
+
break;
|
|
69
|
+
case 'workspace\0exclude':
|
|
70
|
+
fields.workspace = true;
|
|
71
|
+
fields.exclude = stringArray(value);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function stringArray(value) {
|
|
76
|
+
if (!isSymbol(value[0], '[') || !isSymbol(value.at(-1), ']'))
|
|
77
|
+
throw new Error('Invalid Cargo workspace list');
|
|
78
|
+
const items = [];
|
|
79
|
+
for (let index = 1; index < value.length - 1; index++) {
|
|
80
|
+
const token = value[index];
|
|
81
|
+
if (index % 2 === 1 && token.kind === 'text')
|
|
82
|
+
items.push(token.value);
|
|
83
|
+
else if (index % 2 === 1 || !isSymbol(token, ','))
|
|
84
|
+
throw new Error('Invalid Cargo workspace list');
|
|
85
|
+
}
|
|
86
|
+
return items;
|
|
87
|
+
}
|
|
88
|
+
function recordDeclaration(dependencies, workspace, path, value) {
|
|
89
|
+
const isWorkspace = path[0] === 'workspace';
|
|
90
|
+
const declarations = isWorkspace ? workspace : dependencies;
|
|
91
|
+
const start = path[0] === 'target' ? 2 : isWorkspace ? 1 : 0;
|
|
92
|
+
const group = path[start];
|
|
93
|
+
const alias = path[start + 1];
|
|
94
|
+
if (group === undefined || alias === undefined || !dependencyGroups.has(nativeGroup(group)))
|
|
95
|
+
return;
|
|
96
|
+
const id = JSON.stringify(path.slice(0, start + 2));
|
|
97
|
+
let stored = declarations.get(isWorkspace ? alias : id);
|
|
98
|
+
if (stored === undefined) {
|
|
99
|
+
stored = { alias, group, fields: new Map() };
|
|
100
|
+
declarations.set(isWorkspace ? alias : id, stored);
|
|
101
|
+
}
|
|
102
|
+
const attribute = path[start + 2];
|
|
103
|
+
if (attribute !== undefined) {
|
|
104
|
+
if (path.length === start + 3 && value[0] !== undefined)
|
|
105
|
+
stored.fields.set(attribute, value[0]);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
if (value[0]?.kind === 'text')
|
|
109
|
+
stored.fields.set('version', value[0]);
|
|
110
|
+
for (const [key, token] of inlineFields(value))
|
|
111
|
+
stored.fields.set(key, token);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function inlineFields(value) {
|
|
115
|
+
const fields = new Map();
|
|
116
|
+
if (!isSymbol(value[0], '{') || !isSymbol(value.at(-1), '}'))
|
|
117
|
+
return fields;
|
|
118
|
+
let nesting = 0;
|
|
119
|
+
let start = 1;
|
|
120
|
+
for (let index = 1; index < value.length; index++) {
|
|
121
|
+
const token = value[index];
|
|
122
|
+
if ((index === value.length - 1 || isSymbol(token, ',')) && nesting === 0) {
|
|
123
|
+
const row = value.slice(start, index);
|
|
124
|
+
if (row.length > 0) {
|
|
125
|
+
const [key, assigned] = assignment(row);
|
|
126
|
+
if (key.length === 1 && assigned[0] !== undefined)
|
|
127
|
+
fields.set(key[0], assigned[0]);
|
|
128
|
+
}
|
|
129
|
+
start = index + 1;
|
|
130
|
+
}
|
|
131
|
+
else if (isSymbol(token, '[') || isSymbol(token, '{'))
|
|
132
|
+
nesting++;
|
|
133
|
+
else if (isSymbol(token, ']') || isSymbol(token, '}'))
|
|
134
|
+
nesting--;
|
|
135
|
+
}
|
|
136
|
+
return fields;
|
|
137
|
+
}
|
|
138
|
+
function resolvedDependency(declaration, workspace) {
|
|
139
|
+
const { alias, group, fields } = declaration;
|
|
140
|
+
const source = isTrue(fields.get('workspace')) ? workspace.get(alias)?.fields : fields;
|
|
141
|
+
if (source === undefined)
|
|
142
|
+
return undefined;
|
|
143
|
+
return { group, package: textValue(source.get('package')) ?? alias, git: textValue(source.get('git')),
|
|
144
|
+
path: textValue(source.get('path')), version: textValue(source.get('version')) };
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=cargo-manifest.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { LockEdge, LockInput, LockPackage } from '../model.js';
|
|
2
|
+
type LockedPackage = Omit<LockPackage, 'direct' | 'groups'>;
|
|
3
|
+
interface Evidence {
|
|
4
|
+
readonly roots: readonly string[];
|
|
5
|
+
readonly completeRoots: boolean;
|
|
6
|
+
readonly direct: ReadonlySet<string>;
|
|
7
|
+
readonly groups: ReadonlyMap<string, ReadonlySet<string>>;
|
|
8
|
+
}
|
|
9
|
+
export declare function cargoEvidence({ path, manifests }: LockInput, packages: readonly LockedPackage[], edges: readonly LockEdge[], legacyRoot?: string): Evidence;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { readCargoManifest } from './cargo-manifest.js';
|
|
2
|
+
export function cargoEvidence({ path, manifests }, packages, edges, legacyRoot) {
|
|
3
|
+
const folder = path.slice(0, path.lastIndexOf('/') + 1);
|
|
4
|
+
const workspace = optionalManifest(manifests, folder + 'Cargo.toml');
|
|
5
|
+
const { roots, missing } = identifyRoots(packages, legacyRoot, workspace, manifests, folder);
|
|
6
|
+
const ids = new Set(roots.map(({ id }) => id));
|
|
7
|
+
const byId = new Map(packages.map(row => [row.id, row]));
|
|
8
|
+
const dependencies = new Map();
|
|
9
|
+
const direct = new Set();
|
|
10
|
+
for (const { from, to } of edges) {
|
|
11
|
+
if (!ids.has(from))
|
|
12
|
+
continue;
|
|
13
|
+
direct.add(to);
|
|
14
|
+
const row = byId.get(to);
|
|
15
|
+
if (row !== undefined) {
|
|
16
|
+
const targets = dependencies.get(from) ?? [];
|
|
17
|
+
targets.push(row);
|
|
18
|
+
dependencies.set(from, targets);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const completeRoots = roots.length > 0 && !missing && (!workspace?.workspace
|
|
22
|
+
|| packages.every(row => row.source !== 'path' || ids.has(row.id)));
|
|
23
|
+
return { roots: [...ids], completeRoots, direct, groups: collectGroups(roots, dependencies) };
|
|
24
|
+
}
|
|
25
|
+
function identifyRoots(packages, legacyRoot, workspace, manifests, folder) {
|
|
26
|
+
const rootPackage = matchingRoot(packages, workspace);
|
|
27
|
+
const roots = [];
|
|
28
|
+
if (legacyRoot !== undefined)
|
|
29
|
+
roots.push({ id: legacyRoot, manifest: rootPackage === legacyRoot ? workspace : undefined });
|
|
30
|
+
else if (rootPackage !== undefined)
|
|
31
|
+
roots.push({ id: rootPackage, manifest: workspace });
|
|
32
|
+
if (!workspace?.workspace)
|
|
33
|
+
return { roots, missing: false };
|
|
34
|
+
let missing = workspace.name !== undefined && rootPackage === undefined;
|
|
35
|
+
const excluded = new Set(workspace.exclude.map(memberPath));
|
|
36
|
+
for (const member of workspace.members) {
|
|
37
|
+
if (member === '.')
|
|
38
|
+
continue;
|
|
39
|
+
const name = memberPath(member);
|
|
40
|
+
if (name === undefined || excluded.has(undefined)) {
|
|
41
|
+
missing = true;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (excluded.has(name))
|
|
45
|
+
continue;
|
|
46
|
+
const manifest = optionalManifest(manifests, folder + name + '/Cargo.toml', workspace);
|
|
47
|
+
const id = matchingRoot(packages, manifest);
|
|
48
|
+
if (id === undefined)
|
|
49
|
+
missing = true;
|
|
50
|
+
else
|
|
51
|
+
roots.push({ id, manifest });
|
|
52
|
+
}
|
|
53
|
+
return { roots, missing };
|
|
54
|
+
}
|
|
55
|
+
/** Read only explicitly named workspace companions. Globs can leave root provenance incomplete. */
|
|
56
|
+
function memberPath(path) {
|
|
57
|
+
const parts = path.split('/').filter(part => part !== '' && part !== '.');
|
|
58
|
+
if (path.startsWith('/') || !parts.length || parts.some(part => part === '..' || /[\\*?\[\]]/.test(part)))
|
|
59
|
+
return undefined;
|
|
60
|
+
return parts.join('/');
|
|
61
|
+
}
|
|
62
|
+
function optionalManifest(manifests, filename, workspace) {
|
|
63
|
+
if (!manifests || !Object.hasOwn(manifests, filename))
|
|
64
|
+
return undefined;
|
|
65
|
+
try {
|
|
66
|
+
return readCargoManifest(manifests[filename], workspace);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// A companion outside the supported manifest syntax cannot prove a root or its categories.
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function matchingRoot(packages, manifest) {
|
|
74
|
+
if (!manifest?.name)
|
|
75
|
+
return undefined;
|
|
76
|
+
const version = manifest.version ?? '0.0.0';
|
|
77
|
+
const rows = packages.filter(row => row.source === 'path' && row.name === manifest.name && row.version === version);
|
|
78
|
+
return rows.length === 1 ? rows[0].id : undefined;
|
|
79
|
+
}
|
|
80
|
+
function collectGroups(roots, dependencies) {
|
|
81
|
+
const groups = new Map();
|
|
82
|
+
for (const { id, manifest } of roots) {
|
|
83
|
+
const targets = dependencies.get(id) ?? [];
|
|
84
|
+
const byName = new Map();
|
|
85
|
+
for (const row of targets) {
|
|
86
|
+
const candidates = byName.get(row.name) ?? [];
|
|
87
|
+
candidates.push(row);
|
|
88
|
+
byName.set(row.name, candidates);
|
|
89
|
+
}
|
|
90
|
+
for (const declaration of manifest?.dependencies ?? []) {
|
|
91
|
+
const choices = byName.get(declaration.package)?.filter(row => matchesSource(row, declaration)) ?? [];
|
|
92
|
+
if (choices.length !== 1)
|
|
93
|
+
continue;
|
|
94
|
+
const target = choices[0].id;
|
|
95
|
+
const labels = groups.get(target) ?? new Set();
|
|
96
|
+
labels.add(declaration.group);
|
|
97
|
+
groups.set(target, labels);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return groups;
|
|
101
|
+
}
|
|
102
|
+
function matchesSource(row, { git, path, version }) {
|
|
103
|
+
const exact = version?.match(/^=\s*(\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?)$/)?.[1];
|
|
104
|
+
if (exact !== undefined && row.version !== exact)
|
|
105
|
+
return false;
|
|
106
|
+
if (path !== undefined)
|
|
107
|
+
return row.source === 'path';
|
|
108
|
+
if (git === undefined)
|
|
109
|
+
return true;
|
|
110
|
+
if (row.source !== 'git')
|
|
111
|
+
return false;
|
|
112
|
+
const stored = new URL(row.id).searchParams.get('vcs_url');
|
|
113
|
+
const recorded = stored?.slice('git+'.length).split(/[?#]/, 1)[0];
|
|
114
|
+
return recorded === git;
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=cargo-roots.js.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { readCargoLock } from './cargo-lock.js';
|
|
2
|
+
import { cargoEvidence } from './cargo-roots.js';
|
|
3
|
+
export const cargo = { id: 'cargo', ecosystem: 'cargo', files: ['Cargo.lock'],
|
|
4
|
+
edges: true, direct: true, licenses: false, integrity: true };
|
|
5
|
+
const cratesIo = new Set(['registry+https://github.com/rust-lang/crates.io-index', 'sparse+https://index.crates.io/']);
|
|
6
|
+
const namePattern = /^[\p{L}\p{N}_-]+$/u;
|
|
7
|
+
const versionPattern = /^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/;
|
|
8
|
+
const dependencyPattern = /^([^\s]+)(?: ([^\s]+)(?: \(([^)]+)\))?)?$/;
|
|
9
|
+
function knownSource(source) {
|
|
10
|
+
if (source === undefined || source.startsWith('path+'))
|
|
11
|
+
return 'path';
|
|
12
|
+
if (source.startsWith('git+'))
|
|
13
|
+
return 'git';
|
|
14
|
+
if (source.startsWith('registry+') || source.startsWith('sparse+'))
|
|
15
|
+
return 'registry';
|
|
16
|
+
return 'unknown';
|
|
17
|
+
}
|
|
18
|
+
function identity(name, version, sourceId, source) {
|
|
19
|
+
const id = 'pkg:cargo/' + encodeURIComponent(name) + '@' + encodeURIComponent(version);
|
|
20
|
+
if (sourceId === undefined || cratesIo.has(sourceId))
|
|
21
|
+
return id;
|
|
22
|
+
const qualifier = source === 'git' ? 'vcs_url' : 'repository_url';
|
|
23
|
+
return id + '?' + qualifier + '=' + encodeURIComponent(sourceId);
|
|
24
|
+
}
|
|
25
|
+
function readEntry({ fields, line, endLine }, metadata) {
|
|
26
|
+
const name = fields.get('name');
|
|
27
|
+
const version = fields.get('version');
|
|
28
|
+
if (typeof name !== 'string' || !namePattern.test(name) || typeof version !== 'string' || !versionPattern.test(version)) {
|
|
29
|
+
throw new Error('Missing or invalid Cargo package at line ' + line);
|
|
30
|
+
}
|
|
31
|
+
const sourceId = fields.get('source');
|
|
32
|
+
if (sourceId !== undefined && (typeof sourceId !== 'string' || sourceId === ''))
|
|
33
|
+
throw new Error('Invalid Cargo source at line ' + line);
|
|
34
|
+
const source = knownSource(sourceId);
|
|
35
|
+
const checksum = fields.get('checksum') ?? metadata.get('checksum ' + name + ' ' + version + ' (' + sourceId + ')');
|
|
36
|
+
const integrity = typeof checksum === 'string' && /^[\da-f]{64}$/i.test(checksum) ? 'sha256:' + checksum : undefined;
|
|
37
|
+
const deps = fields.get('dependencies') ?? [];
|
|
38
|
+
const replacement = fields.get('replace');
|
|
39
|
+
if (!Array.isArray(deps) || (replacement !== undefined && typeof replacement !== 'string')) {
|
|
40
|
+
throw new Error('Invalid Cargo dependency list at line ' + line);
|
|
41
|
+
}
|
|
42
|
+
const dependencies = replacement === undefined ? deps : [...deps, replacement];
|
|
43
|
+
return { sourceId, dependencies, locked: { name, version, source, id: identity(name, version, sourceId, source),
|
|
44
|
+
line, endLine, ...(integrity === undefined ? {} : { integrity }) } };
|
|
45
|
+
}
|
|
46
|
+
function dependencyIndex(entries) {
|
|
47
|
+
const keys = new Map();
|
|
48
|
+
const record = (key, id) => {
|
|
49
|
+
keys.set(key, keys.has(key) ? null : id);
|
|
50
|
+
};
|
|
51
|
+
for (const { locked: { id, name, version }, sourceId } of entries) {
|
|
52
|
+
record(name, id);
|
|
53
|
+
record(name + ' ' + version, id);
|
|
54
|
+
if (sourceId !== undefined)
|
|
55
|
+
record(name + ' ' + version + ' (' + sourceId + ')', id);
|
|
56
|
+
}
|
|
57
|
+
return keys;
|
|
58
|
+
}
|
|
59
|
+
function readEdges(entries) {
|
|
60
|
+
const index = dependencyIndex(entries);
|
|
61
|
+
const edges = [];
|
|
62
|
+
let incomplete = false;
|
|
63
|
+
for (const { locked: { id: from }, dependencies } of entries) {
|
|
64
|
+
const found = new Set();
|
|
65
|
+
for (const reference of dependencies) {
|
|
66
|
+
if (!dependencyPattern.test(reference))
|
|
67
|
+
throw new Error('Invalid Cargo dependency reference: ' + reference);
|
|
68
|
+
const to = index.get(reference);
|
|
69
|
+
if (to)
|
|
70
|
+
found.add(to);
|
|
71
|
+
else
|
|
72
|
+
incomplete = true;
|
|
73
|
+
}
|
|
74
|
+
for (const to of found)
|
|
75
|
+
edges.push({ from, to });
|
|
76
|
+
}
|
|
77
|
+
return { edges, incomplete };
|
|
78
|
+
}
|
|
79
|
+
export function readCargo(input) {
|
|
80
|
+
const lock = readCargoLock(input.text);
|
|
81
|
+
const root = lock.root === undefined ? undefined : readEntry(lock.root, lock.metadata);
|
|
82
|
+
const entries = lock.packages.map(entry => readEntry(entry, lock.metadata));
|
|
83
|
+
if (root !== undefined)
|
|
84
|
+
entries.unshift(root);
|
|
85
|
+
if (new Set(entries.map(({ locked }) => locked.id)).size !== entries.length)
|
|
86
|
+
throw new Error('Duplicate Cargo package identity');
|
|
87
|
+
const { edges, incomplete } = readEdges(entries);
|
|
88
|
+
const lockedPackages = entries.map(entry => entry.locked);
|
|
89
|
+
const { roots, completeRoots, direct, groups } = cargoEvidence(input, lockedPackages, edges, root?.locked.id);
|
|
90
|
+
const completeDirect = completeRoots && !incomplete;
|
|
91
|
+
const packages = lockedPackages.map((locked) => ({
|
|
92
|
+
...locked, groups: [...groups.get(locked.id) ?? []].sort(),
|
|
93
|
+
direct: direct.has(locked.id) ? true : completeDirect ? false : 'unknown',
|
|
94
|
+
}));
|
|
95
|
+
const losses = [];
|
|
96
|
+
if (incomplete)
|
|
97
|
+
losses.push('edges');
|
|
98
|
+
if (!completeRoots)
|
|
99
|
+
losses.push('roots');
|
|
100
|
+
if (!completeDirect)
|
|
101
|
+
losses.push('direct');
|
|
102
|
+
const rootIds = new Set(roots);
|
|
103
|
+
if (packages.some(({ id, groups }) => !rootIds.has(id) && groups.length === 0))
|
|
104
|
+
losses.push('groups');
|
|
105
|
+
losses.push('licenses');
|
|
106
|
+
return { format: 'cargo', ecosystem: 'cargo', packages, edges, roots, losses };
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=cargo.js.map
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { readLines } from './lines.js';
|
|
2
|
+
export const carton = { id: 'carton', ecosystem: 'cpan', files: ['cpanfile.snapshot'],
|
|
3
|
+
edges: true, direct: false, licenses: false };
|
|
4
|
+
const distributionName = /^[A-Za-z0-9_+.][A-Za-z0-9_.+-]*$/;
|
|
5
|
+
const versionedHeading = /^(.+?)-(v?\d+(?:[._]\d+)*(?:-TRIAL\d*)?)$/i;
|
|
6
|
+
const cpanArchive = /^(?:authors\/id\/)?([A-Z])\/([A-Z][A-Z0-9])\/([A-Z][A-Z0-9-]*)\/([A-Za-z0-9_.+-]+)\.(tar\.gz|tar\.bz2|tar\.xz|tgz|zip)$/;
|
|
7
|
+
function* distributions(text) {
|
|
8
|
+
let current;
|
|
9
|
+
let section;
|
|
10
|
+
let header = false;
|
|
11
|
+
let body = false;
|
|
12
|
+
for (const { text: row, offset } of readLines(text)) {
|
|
13
|
+
const line = offset + 1;
|
|
14
|
+
if (line === 1) {
|
|
15
|
+
header = /^# carton snapshot format: version 1\.0[ \t]*$/i.test(row);
|
|
16
|
+
if (!header)
|
|
17
|
+
throw new Error('Invalid or unsupported Carton snapshot header');
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (!row.trim())
|
|
21
|
+
continue;
|
|
22
|
+
if (!body && row === 'DISTRIBUTIONS') {
|
|
23
|
+
body = true;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const heading = /^ {2}(\S+)$/.exec(row)?.[1];
|
|
27
|
+
if (body && heading && distributionName.test(heading)) {
|
|
28
|
+
if (current)
|
|
29
|
+
yield current;
|
|
30
|
+
current = { heading, line, endLine: line };
|
|
31
|
+
section = undefined;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (!current)
|
|
35
|
+
throw new Error('Missing Carton distribution heading');
|
|
36
|
+
const pathname = /^ {4}pathname: (.*)$/.exec(row)?.[1];
|
|
37
|
+
if (pathname !== undefined && current.pathname === undefined) {
|
|
38
|
+
current.pathname = pathname;
|
|
39
|
+
section = undefined;
|
|
40
|
+
}
|
|
41
|
+
else if (row === ' provides:' && current.provides === undefined) {
|
|
42
|
+
section = current.provides = [];
|
|
43
|
+
}
|
|
44
|
+
else if (row === ' requirements:' && current.requirements === undefined) {
|
|
45
|
+
section = current.requirements = [];
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const module = /^ {6}([A-Za-z0-9_:]+) +(?:[v0-9._,=!<> \t]+|undef)$/.exec(row)?.[1];
|
|
49
|
+
if (!section || !module)
|
|
50
|
+
throw new Error('Invalid Carton distribution field');
|
|
51
|
+
section.push(module);
|
|
52
|
+
}
|
|
53
|
+
current.endLine = line;
|
|
54
|
+
}
|
|
55
|
+
if (!header || !body)
|
|
56
|
+
throw new Error('Missing Carton distributions section');
|
|
57
|
+
if (current)
|
|
58
|
+
yield current;
|
|
59
|
+
}
|
|
60
|
+
function coordinate(dist) {
|
|
61
|
+
const { heading, line, endLine, pathname } = dist;
|
|
62
|
+
if (pathname === undefined || dist.provides === undefined || dist.requirements === undefined) {
|
|
63
|
+
throw new Error('Incomplete Carton distribution');
|
|
64
|
+
}
|
|
65
|
+
const archive = cpanArchive.exec(pathname);
|
|
66
|
+
const cpan = archive !== null && archive[3].startsWith(archive[2]) && archive[2].startsWith(archive[1])
|
|
67
|
+
&& archive[4] === heading;
|
|
68
|
+
const versioned = cpan ? versionedHeading.exec(heading) : null;
|
|
69
|
+
const name = versioned?.[1] ?? heading;
|
|
70
|
+
const version = versioned?.[2];
|
|
71
|
+
const source = cpan ? 'registry' : /^https?:\/\//i.test(pathname) ? 'url' : 'unknown';
|
|
72
|
+
let id = 'pkg:cpan/' + encodeURIComponent(name);
|
|
73
|
+
if (version !== undefined)
|
|
74
|
+
id += '@' + encodeURIComponent(version);
|
|
75
|
+
if (cpan) {
|
|
76
|
+
id += '?author=' + archive[3];
|
|
77
|
+
if (archive[5] !== 'tar.gz')
|
|
78
|
+
id += '&ext=' + archive[5];
|
|
79
|
+
}
|
|
80
|
+
return { pkg: { id, name, ...(version !== undefined ? { version } : {}), source,
|
|
81
|
+
groups: [], direct: 'unknown', line, endLine }, complete: cpan && version !== undefined };
|
|
82
|
+
}
|
|
83
|
+
export function readCarton({ text }) {
|
|
84
|
+
const packages = [];
|
|
85
|
+
const requirements = [];
|
|
86
|
+
const providers = new Map();
|
|
87
|
+
const seen = new Set();
|
|
88
|
+
let resolutionLoss = false;
|
|
89
|
+
for (const dist of distributions(text)) {
|
|
90
|
+
const { pkg, complete } = coordinate(dist);
|
|
91
|
+
if (seen.has(pkg.id))
|
|
92
|
+
throw new Error('Duplicate Carton distribution');
|
|
93
|
+
seen.add(pkg.id);
|
|
94
|
+
packages.push(pkg);
|
|
95
|
+
requirements.push(dist.requirements);
|
|
96
|
+
resolutionLoss ||= !complete;
|
|
97
|
+
for (const module of dist.provides) {
|
|
98
|
+
if (!providers.has(module))
|
|
99
|
+
providers.set(module, pkg.id);
|
|
100
|
+
else if (providers.get(module) !== pkg.id)
|
|
101
|
+
providers.set(module, undefined);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const edges = [];
|
|
105
|
+
let edgeLoss = false;
|
|
106
|
+
for (const [index, pkg] of packages.entries()) {
|
|
107
|
+
const targets = new Set();
|
|
108
|
+
for (const module of requirements[index]) {
|
|
109
|
+
if (module === 'perl')
|
|
110
|
+
continue;
|
|
111
|
+
const target = providers.get(module);
|
|
112
|
+
if (target === undefined)
|
|
113
|
+
edgeLoss = true;
|
|
114
|
+
else if (target !== pkg.id)
|
|
115
|
+
targets.add(target);
|
|
116
|
+
}
|
|
117
|
+
for (const to of targets)
|
|
118
|
+
edges.push({ from: pkg.id, to });
|
|
119
|
+
}
|
|
120
|
+
const losses = ['roots', 'direct', 'groups', 'licenses'];
|
|
121
|
+
if (edgeLoss)
|
|
122
|
+
losses.push('edges');
|
|
123
|
+
if (resolutionLoss)
|
|
124
|
+
losses.push('resolution');
|
|
125
|
+
return { format: 'carton', ecosystem: 'cpan', packages, edges, roots: [], losses };
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=carton.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { LockFormat, LockGraph, LockInput, LockPackage } from '../model.js';
|
|
2
|
+
export declare const composer: LockFormat;
|
|
3
|
+
/** Composer evidence shared with the legacy reader for noncanonical package coordinates. */
|
|
4
|
+
export declare function composerEvidence(entry: Record<string, unknown>): Pick<LockPackage, 'license' | 'source' | 'integrity'>;
|
|
5
|
+
export declare function readComposer(input: LockInput): LockGraph;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { locateJsonSpans, pointer } from '../json-spans.js';
|
|
2
|
+
import { isRecord, readJson } from '../json.js';
|
|
3
|
+
export const composer = { id: 'composer', ecosystem: 'composer', files: ['composer.lock'],
|
|
4
|
+
edges: true, direct: true, licenses: true, integrity: true };
|
|
5
|
+
const packageName = /^[a-z0-9][a-z0-9_.-]*\/[a-z0-9][a-z0-9_.-]*$/i;
|
|
6
|
+
function packageUrl(name, version) {
|
|
7
|
+
return 'pkg:composer/' + name.toLowerCase() + (version === undefined ? '' : '@' + encodeURIComponent(version));
|
|
8
|
+
}
|
|
9
|
+
function requirements(value) {
|
|
10
|
+
return isRecord(value) ? Object.keys(value).filter(key => packageName.test(key)).map(key => key.toLowerCase()) : [];
|
|
11
|
+
}
|
|
12
|
+
function sourceOf(entry) {
|
|
13
|
+
const source = entry.source;
|
|
14
|
+
const dist = entry.dist;
|
|
15
|
+
if (isRecord(dist)) {
|
|
16
|
+
if (dist.type === 'path')
|
|
17
|
+
return 'path';
|
|
18
|
+
if (typeof dist.url === 'string' && dist.url.trim()) {
|
|
19
|
+
if (/^file:/i.test(dist.url))
|
|
20
|
+
return 'path';
|
|
21
|
+
// The notification URL is attached by Composer repositories, including Packagist.
|
|
22
|
+
const notify = entry['notification-url'];
|
|
23
|
+
const repository = typeof notify === 'string' && /^https?:\/\//i.test(notify)
|
|
24
|
+
|| /^https?:\/\/(?:[^/?#:]+\.)?packagist\.org(?=[:/?#]|$)/i.test(dist.url);
|
|
25
|
+
return repository && /^https?:\/\//i.test(dist.url) ? 'registry' : 'url';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (isRecord(source) && source.type === 'git')
|
|
29
|
+
return 'git';
|
|
30
|
+
return 'unknown';
|
|
31
|
+
}
|
|
32
|
+
function licenseOf(value) {
|
|
33
|
+
if (typeof value === 'string') {
|
|
34
|
+
const text = value.trim();
|
|
35
|
+
return /^\(([A-Za-z0-9.+-]+)\)$/.exec(text)?.[1] ?? (text || undefined);
|
|
36
|
+
}
|
|
37
|
+
if (isRecord(value) && typeof value.type === 'string')
|
|
38
|
+
return licenseOf(value.type);
|
|
39
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
40
|
+
return undefined;
|
|
41
|
+
const choices = value.map(item => typeof item === 'string' || isRecord(item) ? licenseOf(item) : undefined);
|
|
42
|
+
return choices.every(choice => choice !== undefined) ? choices.join(' OR ') : undefined;
|
|
43
|
+
}
|
|
44
|
+
/** Composer evidence shared with the legacy reader for noncanonical package coordinates. */
|
|
45
|
+
export function composerEvidence(entry) {
|
|
46
|
+
const license = licenseOf(entry.license);
|
|
47
|
+
const integrity = isRecord(entry.dist) && typeof entry.dist.shasum === 'string' && /^[a-f0-9]{40}$/i.test(entry.dist.shasum)
|
|
48
|
+
? 'sha1:' + entry.dist.shasum : undefined;
|
|
49
|
+
return { source: sourceOf(entry), ...(license === undefined ? {} : { license }), ...(integrity === undefined ? {} : { integrity }) };
|
|
50
|
+
}
|
|
51
|
+
function manifestFor({ path, manifests }) {
|
|
52
|
+
const name = path.slice(0, path.lastIndexOf('/') + 1) + 'composer.json';
|
|
53
|
+
if (!manifests || !Object.hasOwn(manifests, name))
|
|
54
|
+
return undefined;
|
|
55
|
+
const manifest = readJson(manifests[name]);
|
|
56
|
+
if (!isRecord(manifest))
|
|
57
|
+
throw new Error('composer.json must declare an object');
|
|
58
|
+
return manifest;
|
|
59
|
+
}
|
|
60
|
+
function entries(section, value) {
|
|
61
|
+
if (!Array.isArray(value))
|
|
62
|
+
throw new Error(`Composer lockfile must contain a ${section} array`);
|
|
63
|
+
return value.map((entry, index) => {
|
|
64
|
+
if (!isRecord(entry) || typeof entry.name !== 'string' || !packageName.test(entry.name)
|
|
65
|
+
|| typeof entry.version !== 'string' || !entry.version.trim()) {
|
|
66
|
+
throw new Error(`Invalid Composer package at ${section}[${index}]`);
|
|
67
|
+
}
|
|
68
|
+
const { name, version } = entry;
|
|
69
|
+
return { path: pointer([section, index]), requires: requirements(entry.require), locked: {
|
|
70
|
+
id: packageUrl(name, version), name, version, groups: [section], ...composerEvidence(entry),
|
|
71
|
+
} };
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
export function readComposer(input) {
|
|
75
|
+
const lock = readJson(input.text);
|
|
76
|
+
if (!isRecord(lock))
|
|
77
|
+
throw new Error('Composer lockfile must declare an object');
|
|
78
|
+
const manifest = manifestFor(input);
|
|
79
|
+
const root = typeof manifest?.name === 'string' && packageName.test(manifest.name) ? packageUrl(manifest.name) : undefined;
|
|
80
|
+
const direct = new Set([...requirements(manifest?.require), ...requirements(manifest?.['require-dev'])]);
|
|
81
|
+
const raw = [...entries('packages', lock.packages), ...entries('packages-dev', lock['packages-dev'] ?? [])];
|
|
82
|
+
const identical = new Set();
|
|
83
|
+
const byName = new Map();
|
|
84
|
+
for (const { locked } of raw) {
|
|
85
|
+
if (identical.has(locked.id))
|
|
86
|
+
throw new Error('Duplicate Composer package identity');
|
|
87
|
+
identical.add(locked.id);
|
|
88
|
+
const name = locked.name.toLowerCase();
|
|
89
|
+
byName.set(name, byName.has(name) ? null : locked.id);
|
|
90
|
+
}
|
|
91
|
+
const directUnknown = manifest === undefined || [...direct].some(name => !byName.get(name));
|
|
92
|
+
const spans = locateJsonSpans(input.text, new Set(raw.map(({ path }) => path)), 2);
|
|
93
|
+
const packages = raw.map(({ path, locked }) => {
|
|
94
|
+
const range = spans.get(path);
|
|
95
|
+
if (range === undefined)
|
|
96
|
+
throw new Error('Missing Composer package source span');
|
|
97
|
+
return { ...locked, ...range,
|
|
98
|
+
direct: direct.has(locked.name.toLowerCase()) ? true : directUnknown ? 'unknown' : false };
|
|
99
|
+
});
|
|
100
|
+
const edges = [];
|
|
101
|
+
let missingEdges = false;
|
|
102
|
+
const link = (from, name) => {
|
|
103
|
+
const to = byName.get(name);
|
|
104
|
+
if (to)
|
|
105
|
+
edges.push({ from, to });
|
|
106
|
+
else
|
|
107
|
+
missingEdges = true;
|
|
108
|
+
};
|
|
109
|
+
for (const { locked, requires } of raw) {
|
|
110
|
+
for (const dependency of requires)
|
|
111
|
+
link(locked.id, dependency);
|
|
112
|
+
}
|
|
113
|
+
if (root !== undefined) {
|
|
114
|
+
for (const dependency of direct)
|
|
115
|
+
link(root, dependency);
|
|
116
|
+
}
|
|
117
|
+
const losses = [];
|
|
118
|
+
if (missingEdges || root === undefined)
|
|
119
|
+
losses.push('edges');
|
|
120
|
+
if (root === undefined)
|
|
121
|
+
losses.push('roots');
|
|
122
|
+
if (directUnknown)
|
|
123
|
+
losses.push('direct');
|
|
124
|
+
return { format: 'composer', ecosystem: 'composer', packages, edges, roots: root === undefined ? [] : [root], losses };
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=composer.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { LockPackage } from '../model.js';
|
|
2
|
+
export interface ConanReference {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly version: string;
|
|
5
|
+
readonly user?: string;
|
|
6
|
+
readonly channel?: string;
|
|
7
|
+
readonly revision?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function recipe(value: unknown): ConanReference;
|
|
10
|
+
export declare function binaryField(value: unknown): string | undefined;
|
|
11
|
+
export declare function coordinate({ name, version, user, channel, revision }: ConanReference, packageId?: string, packageRevision?: string): Pick<LockPackage, 'id' | 'name' | 'version'>;
|