@repometry/lockparse 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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -2
  3. package/package.json +31 -4
  4. package/target/main/ts/formats/bundler-gemfile.d.ts +1 -0
  5. package/target/main/ts/formats/bundler-gemfile.js +191 -0
  6. package/target/main/ts/formats/bundler-lock.d.ts +26 -0
  7. package/target/main/ts/formats/bundler-lock.js +110 -0
  8. package/target/main/ts/formats/bundler.d.ts +3 -0
  9. package/target/main/ts/formats/bundler.js +84 -0
  10. package/target/main/ts/formats/cargo-lock.d.ts +15 -0
  11. package/target/main/ts/formats/cargo-lock.js +127 -0
  12. package/target/main/ts/formats/cargo-manifest.d.ts +25 -0
  13. package/target/main/ts/formats/cargo-manifest.js +146 -0
  14. package/target/main/ts/formats/cargo-roots.d.ts +10 -0
  15. package/target/main/ts/formats/cargo-roots.js +116 -0
  16. package/target/main/ts/formats/cargo.d.ts +3 -0
  17. package/target/main/ts/formats/cargo.js +108 -0
  18. package/target/main/ts/formats/carton.d.ts +3 -0
  19. package/target/main/ts/formats/carton.js +127 -0
  20. package/target/main/ts/formats/composer.d.ts +3 -0
  21. package/target/main/ts/formats/composer.js +112 -0
  22. package/target/main/ts/formats/conan-reference.d.ts +11 -0
  23. package/target/main/ts/formats/conan-reference.js +32 -0
  24. package/target/main/ts/formats/conan.d.ts +3 -0
  25. package/target/main/ts/formats/conan.js +138 -0
  26. package/target/main/ts/formats/dart.d.ts +3 -0
  27. package/target/main/ts/formats/dart.js +122 -0
  28. package/target/main/ts/formats/go-mod.d.ts +28 -0
  29. package/target/main/ts/formats/go-mod.js +148 -0
  30. package/target/main/ts/formats/go.d.ts +3 -0
  31. package/target/main/ts/formats/go.js +74 -0
  32. package/target/main/ts/formats/gradle.d.ts +3 -0
  33. package/target/main/ts/formats/gradle.js +42 -0
  34. package/target/main/ts/formats/lines.d.ts +5 -0
  35. package/target/main/ts/formats/lines.js +12 -0
  36. package/target/main/ts/formats/mix-literals.d.ts +22 -0
  37. package/target/main/ts/formats/mix-literals.js +145 -0
  38. package/target/main/ts/formats/mix.d.ts +3 -0
  39. package/target/main/ts/formats/mix.js +90 -0
  40. package/target/main/ts/formats/nuget.d.ts +3 -0
  41. package/target/main/ts/formats/nuget.js +112 -0
  42. package/target/main/ts/formats/pdm.d.ts +3 -0
  43. package/target/main/ts/formats/pdm.js +52 -0
  44. package/target/main/ts/formats/pipenv.d.ts +3 -0
  45. package/target/main/ts/formats/pipenv.js +151 -0
  46. package/target/main/ts/formats/poetry-lock.d.ts +15 -0
  47. package/target/main/ts/formats/poetry-lock.js +68 -0
  48. package/target/main/ts/formats/poetry.d.ts +3 -0
  49. package/target/main/ts/formats/poetry.js +9 -0
  50. package/target/main/ts/formats/python-graph.d.ts +16 -0
  51. package/target/main/ts/formats/python-graph.js +91 -0
  52. package/target/main/ts/formats/python-package.d.ts +15 -0
  53. package/target/main/ts/formats/python-package.js +63 -0
  54. package/target/main/ts/formats/python-project.d.ts +12 -0
  55. package/target/main/ts/formats/python-project.js +76 -0
  56. package/target/main/ts/formats/renv.d.ts +3 -0
  57. package/target/main/ts/formats/renv.js +93 -0
  58. package/target/main/ts/formats/swift.d.ts +3 -0
  59. package/target/main/ts/formats/swift.js +87 -0
  60. package/target/main/ts/formats/terraform.d.ts +3 -0
  61. package/target/main/ts/formats/terraform.js +85 -0
  62. package/target/main/ts/formats/toml-document.d.ts +11 -0
  63. package/target/main/ts/formats/toml-document.js +79 -0
  64. package/target/main/ts/formats/toml-statements.d.ts +13 -0
  65. package/target/main/ts/formats/toml-statements.js +144 -0
  66. package/target/main/ts/formats/toml-values.d.ts +12 -0
  67. package/target/main/ts/formats/toml-values.js +111 -0
  68. package/target/main/ts/formats/uv-lock.d.ts +27 -0
  69. package/target/main/ts/formats/uv-lock.js +80 -0
  70. package/target/main/ts/formats/uv.d.ts +3 -0
  71. package/target/main/ts/formats/uv.js +114 -0
  72. package/target/main/ts/index.d.ts +12 -0
  73. package/target/main/ts/index.js +65 -0
  74. package/target/main/ts/json-spans.d.ts +11 -0
  75. package/target/main/ts/json-spans.js +86 -0
  76. package/target/main/ts/json.d.ts +2 -0
  77. package/target/main/ts/json.js +29 -0
  78. package/target/main/ts/model.d.ts +47 -0
  79. package/target/main/ts/model.js +2 -0
@@ -0,0 +1,145 @@
1
+ const operator = new Set(['{', '}', '[', ']', ':', ',']);
2
+ const word = /(?:[A-Za-z_][A-Za-z0-9_!?@]*|-?\d+(?:\.\d+)?)/y;
3
+ const quoted = /"(?:[^"\\\r\n]|\\[^\r\n])*"/y;
4
+ export function readMixLiterals(text) {
5
+ const parser = new Reader(text);
6
+ return parser.entries();
7
+ }
8
+ class Reader {
9
+ text;
10
+ offset = 0;
11
+ line = 1;
12
+ token;
13
+ constructor(text) {
14
+ this.text = text;
15
+ this.token = this.read();
16
+ }
17
+ entries() {
18
+ this.expect('%{');
19
+ const entries = [];
20
+ while (!this.take('}')) {
21
+ const line = this.token.line;
22
+ const label = this.term(0);
23
+ if (label.type !== 'pair' || label.value.type !== 'tuple')
24
+ throw this.invalid();
25
+ entries.push({ key: label.key.value, value: label.value, line, endLine: this.lastLine });
26
+ if (!this.take(',')) {
27
+ this.expect('}');
28
+ break;
29
+ }
30
+ }
31
+ if (this.token.type !== 'eof')
32
+ throw this.invalid();
33
+ return entries;
34
+ }
35
+ lastLine = 1;
36
+ term(depth) {
37
+ if (depth > 64)
38
+ throw new Error('Mix lock nesting is too deep');
39
+ const label = this.token;
40
+ const repeated = label.type === 'symbol' ? label.value : undefined;
41
+ if (repeated === '{' || repeated === '[' || repeated === '%{')
42
+ return this.collection(repeated, depth);
43
+ const scalar = this.scalar();
44
+ if (this.take(':') || this.take('=>')) {
45
+ if (scalar.type === 'literal')
46
+ throw this.invalid();
47
+ return { type: 'pair', key: scalar, value: this.term(depth + 1) };
48
+ }
49
+ if (label.type === 'word' && scalar.type === 'atom')
50
+ throw this.invalid();
51
+ return scalar;
52
+ }
53
+ scalar() {
54
+ const before = this.token;
55
+ if (this.take(':')) {
56
+ const next = this.advance();
57
+ if (next.type !== 'word' && next.type !== 'string')
58
+ throw this.invalid();
59
+ return { type: 'atom', value: next.value };
60
+ }
61
+ this.advance();
62
+ if (before.type === 'string')
63
+ return { type: 'string', value: before.value };
64
+ if (before.type === 'word') {
65
+ const literal = /^(?:true|false|nil|-?\d+(?:\.\d+)?)$/.test(before.value);
66
+ return { type: literal ? 'literal' : 'atom', value: before.value };
67
+ }
68
+ throw this.invalid();
69
+ }
70
+ collection(opening, depth) {
71
+ this.expect(opening);
72
+ const end = opening === '[' ? ']' : '}';
73
+ const type = opening === '{' ? 'tuple' : opening === '[' ? 'list' : 'map';
74
+ const values = [];
75
+ while (!this.take(end)) {
76
+ values.push(this.term(depth + 1));
77
+ if (!this.take(',')) {
78
+ this.expect(end);
79
+ break;
80
+ }
81
+ }
82
+ return { type, values };
83
+ }
84
+ take(value) {
85
+ if (this.token.type !== 'symbol' || this.token.value !== value)
86
+ return false;
87
+ this.advance();
88
+ return true;
89
+ }
90
+ expect(value) {
91
+ if (!this.take(value))
92
+ throw this.invalid();
93
+ }
94
+ advance() {
95
+ const before = this.token;
96
+ this.lastLine = before.line;
97
+ this.token = this.read();
98
+ return before;
99
+ }
100
+ invalid() {
101
+ return new Error('Invalid literal mix.lock near line ' + this.token.line);
102
+ }
103
+ read() {
104
+ this.skip();
105
+ const line = this.line;
106
+ if (this.offset === this.text.length)
107
+ return { type: 'eof', value: '', line };
108
+ const char = this.text[this.offset];
109
+ const pair = this.text.slice(this.offset, this.offset + 2);
110
+ if (pair === '%{' || pair === '=>') {
111
+ this.offset += 2;
112
+ return { type: 'symbol', value: pair, line };
113
+ }
114
+ if (operator.has(char)) {
115
+ this.offset++;
116
+ return { type: 'symbol', value: char, line };
117
+ }
118
+ const pattern = char === '"' ? quoted : word;
119
+ pattern.lastIndex = this.offset;
120
+ const found = pattern.exec(this.text);
121
+ if (found === null)
122
+ throw new Error('Unsupported mix.lock token on line ' + line);
123
+ this.offset = pattern.lastIndex;
124
+ const raw = found[0];
125
+ return char === '"'
126
+ ? { type: 'string', value: JSON.parse(raw.replace(/\\(?:#|.)/g, escape => escape === '\\#' ? '#' : escape)), line }
127
+ : { type: 'word', value: raw, line };
128
+ }
129
+ skip() {
130
+ while (this.offset < this.text.length) {
131
+ const char = this.text[this.offset];
132
+ if (char === '\n')
133
+ this.line++;
134
+ if (char === '#') {
135
+ const next = this.text.indexOf('\n', this.offset);
136
+ this.offset = next < 0 ? this.text.length : next;
137
+ }
138
+ else if (/\s/.test(char))
139
+ this.offset++;
140
+ else
141
+ return;
142
+ }
143
+ }
144
+ }
145
+ //# sourceMappingURL=mix-literals.js.map
@@ -0,0 +1,3 @@
1
+ import type { LockFormat, LockGraph, LockInput } from '../model.js';
2
+ export declare const mix: LockFormat;
3
+ export declare function readMix({ text }: LockInput): LockGraph;
@@ -0,0 +1,90 @@
1
+ import { readMixLiterals } from './mix-literals.js';
2
+ export const mix = { id: 'mix', ecosystem: 'hex', files: ['mix.lock'],
3
+ edges: true, direct: false, licenses: false };
4
+ function text(value, type) {
5
+ return value?.type === type ? value.value : undefined;
6
+ }
7
+ function packageUrl(name, version, repo) {
8
+ return 'pkg:hex/' + encodeURIComponent(name) + '@' + encodeURIComponent(version)
9
+ + (repo === undefined || repo === 'hexpm' ? '' : '?repository=' + encodeURIComponent(repo));
10
+ }
11
+ function dependencies(value) {
12
+ if (value?.type !== 'list')
13
+ return undefined;
14
+ return value.values.map(entry => {
15
+ if (entry.type !== 'tuple')
16
+ throw new Error('Invalid Hex dependency tuple');
17
+ const alias = text(entry.values[0], 'atom');
18
+ if (alias === undefined)
19
+ throw new Error('Missing Hex dependency name');
20
+ const fields = entry.values[2];
21
+ const optional = fields?.type === 'list' && fields.values.some(field => field.type === 'pair' && field.key.value === 'optional' && text(field.value, 'literal') === 'true');
22
+ return { alias, optional };
23
+ });
24
+ }
25
+ function hash(value) {
26
+ const recorded = text(value, 'string');
27
+ return recorded && /^[a-fA-F0-9]{64}$/.test(recorded) ? 'sha256:' + recorded : undefined;
28
+ }
29
+ function readPackage(entry) {
30
+ const { key, line, endLine, value: { values } } = entry;
31
+ const source = text(values[0], 'atom');
32
+ const common = { groups: [], direct: 'unknown', line, endLine };
33
+ if (source === 'hex') {
34
+ const name = text(values[1], 'atom');
35
+ const version = text(values[2], 'string');
36
+ if (!name || !version)
37
+ throw new Error('Invalid Hex package coordinate');
38
+ const integrity = hash(values[7]);
39
+ const repo = text(values[6], 'string');
40
+ return { locked: { ...common, id: packageUrl(name, version, repo), name, version, source: 'registry',
41
+ ...(integrity === undefined ? {} : { integrity }) }, dependencies: dependencies(values[5]),
42
+ legacyHash: integrity === undefined && hash(values[3]) !== undefined };
43
+ }
44
+ if (source === 'git') {
45
+ const revision = text(values[2], 'string');
46
+ if (!text(values[1], 'string') || !revision)
47
+ throw new Error('Incomplete Git lock entry');
48
+ return { locked: { ...common, id: packageUrl(key, revision), name: key, source: 'git' } };
49
+ }
50
+ throw new Error('Unsupported Mix dependency source');
51
+ }
52
+ export function readMix({ text }) {
53
+ const parsed = new Map();
54
+ for (const row of readMixLiterals(text)) {
55
+ if (parsed.has(row.key))
56
+ throw new Error('Duplicate Mix dependency alias');
57
+ parsed.set(row.key, readPackage(row));
58
+ }
59
+ const unique = new Map();
60
+ const edges = [];
61
+ const seen = new Set();
62
+ let incomplete = false;
63
+ for (const { locked, dependencies } of parsed.values()) {
64
+ if (!unique.has(locked.id))
65
+ unique.set(locked.id, locked);
66
+ if (dependencies === undefined)
67
+ incomplete = true;
68
+ for (const { alias, optional } of dependencies ?? []) {
69
+ const target = parsed.get(alias);
70
+ if (target === undefined) {
71
+ if (!optional)
72
+ incomplete = true;
73
+ }
74
+ else {
75
+ const key = JSON.stringify([locked.id, target.locked.id]);
76
+ if (!seen.has(key))
77
+ edges.push({ from: locked.id, to: target.locked.id });
78
+ seen.add(key);
79
+ }
80
+ }
81
+ }
82
+ const losses = [];
83
+ if (incomplete)
84
+ losses.push('edges');
85
+ losses.push('roots', 'direct', 'groups', 'licenses');
86
+ if ([...parsed.values()].some(pkg => pkg.legacyHash))
87
+ losses.push('integrity');
88
+ return { format: 'mix', ecosystem: 'hex', packages: [...unique.values()], edges, roots: [], losses };
89
+ }
90
+ //# sourceMappingURL=mix.js.map
@@ -0,0 +1,3 @@
1
+ import type { LockFormat, LockGraph, LockInput } from '../model.js';
2
+ export declare const nuget: LockFormat;
3
+ export declare function readNuget({ text }: LockInput): LockGraph;
@@ -0,0 +1,112 @@
1
+ import { isRecord, readJson } from '../json.js';
2
+ import { locateJsonSpans, pointer } from '../json-spans.js';
3
+ export const nuget = { id: 'nuget', ecosystem: 'nuget', files: ['packages.lock.json'],
4
+ edges: true, direct: true, licenses: false };
5
+ function packageId(name, version) {
6
+ const base = 'pkg:nuget/' + encodeURIComponent(name);
7
+ return version === undefined ? base : base + '@' + encodeURIComponent(version);
8
+ }
9
+ function readPackage(name, canonicalName, row, path) {
10
+ if (!isRecord(row) || typeof row.type !== 'string' || row.type === '' || !/^[A-Za-z0-9_.-]+$/.test(name)) {
11
+ throw new Error('Invalid NuGet lock entry');
12
+ }
13
+ const version = typeof row.resolved === 'string' && row.resolved !== '' ? row.resolved : undefined;
14
+ if (version === undefined && row.type !== 'Project')
15
+ throw new Error('Missing NuGet resolved version');
16
+ if (row.dependencies !== undefined && !isRecord(row.dependencies))
17
+ throw new Error('Invalid NuGet dependencies');
18
+ const hash = typeof row.contentHash === 'string' && /^[A-Za-z0-9+/]+={0,2}$/.test(row.contentHash) ? row.contentHash : undefined;
19
+ return { id: packageId(canonicalName, version), name, version, type: row.type, at: pointer([...path, name]),
20
+ hash, requires: Object.keys(row.dependencies ?? {}).map(dependency => dependency.toLowerCase()) };
21
+ }
22
+ function readFramework(label, value, schema, names) {
23
+ if (!isRecord(value))
24
+ throw new Error('Invalid NuGet framework');
25
+ const path = ['dependencies', label];
26
+ let items = value;
27
+ if (schema === 3) {
28
+ if (typeof value.framework !== 'string' || !value.framework || !isRecord(value.dependencies))
29
+ throw new Error('Invalid NuGet framework alias');
30
+ path.push('dependencies');
31
+ items = value.dependencies;
32
+ }
33
+ const packages = new Map();
34
+ for (const [name, row] of Object.entries(items)) {
35
+ const key = name.toLowerCase();
36
+ if (packages.has(key))
37
+ throw new Error('Duplicate NuGet package in framework');
38
+ const canonicalName = names.get(key) ?? name;
39
+ names.set(key, canonicalName);
40
+ packages.set(key, readPackage(name, canonicalName, row, path));
41
+ }
42
+ return { label, packages };
43
+ }
44
+ function directType(type) {
45
+ if (type === 'Direct')
46
+ return true;
47
+ if (type === 'Transitive' || type === 'CentralTransitive')
48
+ return false;
49
+ return 'unknown';
50
+ }
51
+ function aggregate(frameworks) {
52
+ const edges = [];
53
+ const seen = new Map();
54
+ const packages = new Map();
55
+ let incomplete = false;
56
+ for (const { label, packages: scope } of frameworks) {
57
+ for (const row of scope.values()) {
58
+ const { id, type, requires, hash } = row;
59
+ const entry = packages.get(id) ?? { row, groups: new Set(), hashes: new Set(), direct: new Set() };
60
+ entry.groups.add(label);
61
+ entry.groups.add(type);
62
+ if (hash !== undefined)
63
+ entry.hashes.add(hash);
64
+ entry.direct.add(directType(type));
65
+ packages.set(id, entry);
66
+ const links = seen.get(id) ?? new Set();
67
+ for (const name of requires) {
68
+ const target = scope.get(name);
69
+ if (target === undefined)
70
+ incomplete = true;
71
+ else if (!links.has(target.id)) {
72
+ links.add(target.id);
73
+ edges.push({ from: id, to: target.id });
74
+ }
75
+ }
76
+ seen.set(id, links);
77
+ }
78
+ }
79
+ return { packages: [...packages.values()], edges, incomplete };
80
+ }
81
+ function toPackage({ row, groups, hashes, direct }, span) {
82
+ if (span === undefined)
83
+ throw new Error('Missing NuGet package provenance');
84
+ const { id, name, version, type } = row;
85
+ return { id, name, ...(version === undefined ? {} : { version }), ...span,
86
+ source: type === 'Project' ? 'path' : 'unknown', groups: [...groups],
87
+ direct: direct.has(true) ? true : direct.has('unknown') ? 'unknown' : false,
88
+ ...(hashes.size === 1 ? { integrity: 'sha512:' + hashes.values().next().value } : {}) };
89
+ }
90
+ export function readNuget({ text }) {
91
+ const raw = readJson(text);
92
+ if (!isRecord(raw) || (raw.version !== 1 && raw.version !== 2 && raw.version !== 3) || !isRecord(raw.dependencies)) {
93
+ throw new Error('Unsupported NuGet lockfile');
94
+ }
95
+ const schema = raw.version;
96
+ const names = new Map();
97
+ const frameworks = Object.entries(raw.dependencies).map(([key, value]) => readFramework(key, value, schema, names));
98
+ const { packages: rawPackages, edges, incomplete } = aggregate(frameworks);
99
+ const spans = locateJsonSpans(text, new Set(rawPackages.map(({ row }) => row.at)), schema === 3 ? 4 : 3);
100
+ const packages = rawPackages.map(entry => toPackage(entry, spans.get(entry.row.at)));
101
+ const losses = ['roots'];
102
+ if (incomplete)
103
+ losses.push('edges');
104
+ if (packages.some(row => row.direct === 'unknown'))
105
+ losses.push('direct');
106
+ if (packages.length)
107
+ losses.push('licenses');
108
+ if (rawPackages.some(pkg => pkg.hashes.size > 1))
109
+ losses.push('integrity');
110
+ return { format: 'nuget', ecosystem: 'nuget', packages, edges, roots: [], losses };
111
+ }
112
+ //# sourceMappingURL=nuget.js.map
@@ -0,0 +1,3 @@
1
+ import type { LockFormat, LockGraph, LockInput } from '../model.js';
2
+ export declare const pdm: LockFormat;
3
+ export declare function readPdm(input: LockInput): LockGraph;
@@ -0,0 +1,52 @@
1
+ import { pythonGraph } from './python-graph.js';
2
+ import { fileDigests, pep508, pypi, pypiIds, pythonName, tableField } from './python-package.js';
3
+ import { projectFor } from './python-project.js';
4
+ import { readTomlDocument } from './toml-document.js';
5
+ import { textField } from './toml-values.js';
6
+ export const pdm = { id: 'pdm', ecosystem: 'pypi', files: ['pdm.lock'],
7
+ edges: true, direct: true, licenses: false };
8
+ function texts(values) {
9
+ if (values === undefined)
10
+ return undefined;
11
+ if (!Array.isArray(values) || !values.every((item) => typeof item === 'string' && item !== '')) {
12
+ throw new Error('Invalid PDM text list');
13
+ }
14
+ return [...new Set(values)];
15
+ }
16
+ function packageRow(pkg, document) {
17
+ if (!(pkg instanceof Map))
18
+ throw new Error('Invalid PDM package');
19
+ const name = textField(pkg, 'name');
20
+ const version = textField(pkg, 'version');
21
+ const ranges = document.spans.get(pkg);
22
+ if (!name || !ranges)
23
+ throw new Error('Incomplete PDM package');
24
+ const vcs = ['git', 'hg', 'svn', 'bzr'].find(key => textField(pkg, key) !== undefined);
25
+ const path = textField(pkg, 'path');
26
+ const url = textField(pkg, 'url');
27
+ const source = vcs === 'git' ? 'git' : vcs !== undefined ? 'url'
28
+ : path !== undefined ? 'path' : url !== undefined ? 'url' : 'registry';
29
+ if (source === 'registry' && !version)
30
+ throw new Error('Missing PDM registry package version');
31
+ const extras = texts(pkg.get('extras'))?.map(pythonName).sort();
32
+ const sourceKey = [vcs, vcs !== undefined ? textField(pkg, vcs) : undefined, path, url, textField(pkg, 'ref'),
33
+ textField(pkg, 'revision'), textField(pkg, 'subdirectory'), textField(pkg, 'marker'),
34
+ textField(pkg, 'requires_python'), extras];
35
+ return { name, version, ranges, source, extras, sourceKey,
36
+ groups: texts(pkg.get('groups')), hashes: fileDigests(pkg.get('files')),
37
+ dependencies: (texts(pkg.get('dependencies')) ?? []).map(pep508) };
38
+ }
39
+ export function readPdm(input) {
40
+ const document = readTomlDocument(input.text);
41
+ const metadata = tableField(document.root, 'metadata');
42
+ if (!/^4\.\d+(?:\.\d+)?$/.test(textField(metadata, 'lock_version') ?? '')) {
43
+ throw new Error('Unsupported PDM lock version');
44
+ }
45
+ const rows = document.root.get('package') ?? [];
46
+ if (!Array.isArray(rows))
47
+ throw new Error('Invalid PDM package list');
48
+ const packages = rows.map(pkg => packageRow(pkg, document));
49
+ const ids = pypiIds(packages, pkg => pypi(pkg.name, pkg.version), pkg => pkg.sourceKey);
50
+ return pythonGraph('pdm', packages.map(({ sourceKey: _sourceKey, ...pkg }, index) => ({ ...pkg, id: ids[index] })), projectFor(input, 'pdm', texts(metadata?.get('groups'))));
51
+ }
52
+ //# sourceMappingURL=pdm.js.map
@@ -0,0 +1,3 @@
1
+ import type { LockFormat, LockGraph, LockInput } from '../model.js';
2
+ export declare const pipenv: LockFormat;
3
+ export declare function readPipenv(input: LockInput): LockGraph;
@@ -0,0 +1,151 @@
1
+ import { isRecord, readJson } from '../json.js';
2
+ import { locateJsonSpans, pointer } from '../json-spans.js';
3
+ import { exactPythonVersion, pypi, pypiIds, pythonName, tableField } from './python-package.js';
4
+ import { readTomlDocument } from './toml-document.js';
5
+ import { textField } from './toml-values.js';
6
+ export const pipenv = { id: 'pipenv', ecosystem: 'pypi', files: ['Pipfile.lock'],
7
+ edges: false, direct: true, licenses: false };
8
+ const nativeSections = new Map([['default', 'packages'], ['develop', 'dev-packages']]);
9
+ const locatorKinds = new Map([
10
+ ['git', 'git'], ['hg', 'unknown'], ['svn', 'unknown'], ['bzr', 'unknown'], ['path', 'path'], ['file', 'url'],
11
+ ]);
12
+ function optionalString(value) {
13
+ return typeof value === 'string' && value !== '' ? value : undefined;
14
+ }
15
+ function identity(value, defaultIndex) {
16
+ const kinds = [...locatorKinds.keys()].filter(key => optionalString(value[key]) !== undefined);
17
+ if (kinds.length > 1)
18
+ throw new Error('Conflicting Pipenv package origins');
19
+ const [locator] = kinds;
20
+ if (locator === undefined)
21
+ return { kind: 'registry', key: ['index', optionalString(value.index) ?? defaultIndex] };
22
+ const address = optionalString(value[locator]);
23
+ const localFile = locator === 'file' && address?.startsWith('file:');
24
+ return { kind: localFile ? 'path' : locatorKinds.get(locator),
25
+ key: [locator, address, optionalString(value.ref), optionalString(value.subdirectory)] };
26
+ }
27
+ function versionFromLock(value) {
28
+ const text = optionalString(value);
29
+ if (text === undefined)
30
+ return undefined;
31
+ const version = /^={2,3}([^=\s]+)$/.exec(text)?.[1];
32
+ if (!version)
33
+ throw new Error('Pipenv lock version must be exact');
34
+ return version;
35
+ }
36
+ function categories(lock, defaultIndex) {
37
+ const byGroup = new Map();
38
+ const byCoordinate = new Map();
39
+ for (const [group, value] of Object.entries(lock)) {
40
+ if (group === '_meta')
41
+ continue;
42
+ if (!isRecord(value))
43
+ throw new Error('Invalid Pipenv category');
44
+ const section = new Map();
45
+ byGroup.set(group, section);
46
+ for (const [name, row] of Object.entries(value)) {
47
+ if (!isRecord(row) || section.has(pythonName(name)))
48
+ throw new Error('Invalid or duplicate Pipenv package');
49
+ const coordinate = identity(row, defaultIndex);
50
+ const version = versionFromLock(row.version);
51
+ if (coordinate.kind === 'registry' && version === undefined)
52
+ throw new Error('Missing Pipenv package version');
53
+ const key = JSON.stringify([pythonName(name), version, coordinate.key]);
54
+ const entry = byCoordinate.get(key) ?? { name, coordinate, version, at: pointer([group, name]),
55
+ hashes: new Set(), groups: new Set() };
56
+ recordHashes(entry.hashes, row.hashes);
57
+ entry.groups.add(group);
58
+ byCoordinate.set(key, entry);
59
+ section.set(pythonName(name), entry);
60
+ }
61
+ }
62
+ return { byGroup, packages: [...byCoordinate.values()] };
63
+ }
64
+ function recordHashes(hashes, value) {
65
+ if (value === undefined)
66
+ return;
67
+ if (!Array.isArray(value))
68
+ throw new Error('Invalid Pipenv artifact hashes');
69
+ for (const hash of value) {
70
+ if (typeof hash !== 'string' || !/^[a-z][a-z0-9-]+:[a-f0-9]+$/i.test(hash))
71
+ throw new Error('Invalid Pipenv artifact hash');
72
+ hashes.add(hash);
73
+ }
74
+ }
75
+ function getManifest({ path, manifests }) {
76
+ const file = path.slice(0, path.lastIndexOf('/') + 1) + 'Pipfile';
77
+ if (manifests && Object.hasOwn(manifests, file)) {
78
+ try {
79
+ return readTomlDocument(manifests[file]).root;
80
+ }
81
+ catch {
82
+ // Unreadable companion data cannot establish directness.
83
+ }
84
+ }
85
+ return undefined;
86
+ }
87
+ function directPackages(input, categories) {
88
+ const direct = new Set();
89
+ const manifest = getManifest(input);
90
+ if (manifest === undefined)
91
+ return { direct, complete: false };
92
+ let complete = true;
93
+ for (const [group, packages] of categories) {
94
+ const declarations = tableField(manifest, nativeSections.get(group) ?? group);
95
+ if (declarations === undefined && packages.size)
96
+ complete = false;
97
+ for (const [name, spec] of declarations ?? []) {
98
+ const locked = packages.get(pythonName(name));
99
+ if (locked === undefined || !matchesManifest(locked, spec))
100
+ complete = false;
101
+ else
102
+ direct.add(locked);
103
+ }
104
+ }
105
+ return { direct, complete };
106
+ }
107
+ function matchesManifest(locked, spec) {
108
+ const exact = exactPythonVersion(typeof spec === 'string' ? spec : textField(spec, 'version'));
109
+ if (exact !== undefined && exact !== locked.version)
110
+ return false;
111
+ const [locator, address, revision] = locked.coordinate.key;
112
+ for (const field of [...locatorKinds.keys(), 'index']) {
113
+ const expected = textField(spec, field);
114
+ if (expected !== undefined && (locator !== field || address !== expected))
115
+ return false;
116
+ }
117
+ const reference = textField(spec, 'ref');
118
+ return reference === undefined || !/^[a-f0-9]{40,64}$/i.test(reference) || revision === reference;
119
+ }
120
+ export function readPipenv(input) {
121
+ const lock = readJson(input.text);
122
+ const metadata = isRecord(lock) ? lock._meta : undefined;
123
+ if (!isRecord(lock) || !isRecord(metadata) || typeof metadata['pipfile-spec'] !== 'number'
124
+ || !Number.isInteger(metadata['pipfile-spec']) || metadata['pipfile-spec'] < 1 || metadata['pipfile-spec'] > 6) {
125
+ throw new Error('Unsupported Pipenv lockfile metadata');
126
+ }
127
+ const firstIndex = Array.isArray(metadata.sources) ? metadata.sources[0] : undefined;
128
+ const defaultIndex = isRecord(firstIndex) ? optionalString(firstIndex.name) : undefined;
129
+ const { byGroup, packages: raw } = categories(lock, defaultIndex);
130
+ const spans = locateJsonSpans(input.text, new Set(raw.map(pkg => pkg.at)), 2);
131
+ const ids = pypiIds(raw, pkg => pypi(pkg.name, pkg.version), pkg => pkg.coordinate.key);
132
+ const { direct, complete } = directPackages(input, byGroup);
133
+ const packages = raw.map((pkg, index) => {
134
+ const span = spans.get(pkg.at);
135
+ if (span === undefined)
136
+ throw new Error('Missing Pipenv package provenance');
137
+ const { name, version, coordinate, groups, hashes } = pkg;
138
+ return { id: ids[index], name, ...(version === undefined ? {} : { version }), source: coordinate.kind, ...span,
139
+ groups: [...groups], direct: direct.has(pkg) ? true : complete ? false : 'unknown',
140
+ ...(hashes.size === 1 ? { integrity: hashes.values().next().value } : {}) };
141
+ });
142
+ const losses = ['edges', 'roots'];
143
+ if (!complete)
144
+ losses.push('direct');
145
+ if (packages.length)
146
+ losses.push('licenses');
147
+ if (raw.some(pkg => pkg.hashes.size > 1))
148
+ losses.push('integrity');
149
+ return { format: 'pipenv', ecosystem: 'pypi', packages, edges: [], roots: [], losses };
150
+ }
151
+ //# sourceMappingURL=pipenv.js.map
@@ -0,0 +1,15 @@
1
+ import type { PythonRequirement } from './python-package.js';
2
+ import type { LockPackage, PackageSource } from '../model.js';
3
+ interface Entry {
4
+ readonly id: string;
5
+ readonly name: string;
6
+ readonly version: string;
7
+ readonly source: PackageSource;
8
+ readonly sourceReference?: string;
9
+ readonly ranges: Pick<LockPackage, 'line' | 'endLine'>;
10
+ readonly groups?: string[];
11
+ readonly hashes: string[];
12
+ readonly dependencies: PythonRequirement[];
13
+ }
14
+ export declare function readPoetryLock(text: string): Entry[];
15
+ export {};
@@ -0,0 +1,68 @@
1
+ import { fileDigests, pypi, pypiIds, pythonName, tableField, tableRequirement } from './python-package.js';
2
+ import { readTomlDocument } from './toml-document.js';
3
+ import { textField } from './toml-values.js';
4
+ function packageSource(type) {
5
+ switch (type) {
6
+ case undefined:
7
+ case 'legacy':
8
+ case 'index': return 'registry';
9
+ case 'git': return 'git';
10
+ case 'directory':
11
+ case 'file': return 'path';
12
+ case 'url': return 'url';
13
+ default: return 'unknown';
14
+ }
15
+ }
16
+ function groupNames(pkg) {
17
+ const groups = pkg.get('groups');
18
+ if (groups === undefined) {
19
+ const category = textField(pkg, 'category');
20
+ return category === undefined ? undefined : [category];
21
+ }
22
+ if (!Array.isArray(groups) || !groups.every((group) => typeof group === 'string' && group !== '')) {
23
+ throw new Error('Invalid Poetry dependency groups');
24
+ }
25
+ return [...new Set(groups)];
26
+ }
27
+ function dependencyRows(pkg) {
28
+ const items = tableField(pkg, 'dependencies');
29
+ return items === undefined ? [] : [...items].flatMap(([name, spec]) => tableRequirement(name, spec));
30
+ }
31
+ function readPackage(pkg, document) {
32
+ if (!(pkg instanceof Map))
33
+ throw new Error('Invalid Poetry package');
34
+ const name = textField(pkg, 'name');
35
+ const version = textField(pkg, 'version');
36
+ const ranges = document.spans.get(pkg);
37
+ if (!name || !version || !ranges)
38
+ throw new Error('Incomplete Poetry package');
39
+ const source = tableField(pkg, 'source');
40
+ const type = textField(source, 'type');
41
+ const sourceReference = textField(source, 'reference');
42
+ const sourceKey = [type, textField(source, 'url'), sourceReference, textField(source, 'resolved_reference'),
43
+ textField(source, 'subdirectory'), pkg.get('markers') instanceof Map ? [...pkg.get('markers')] : pkg.get('markers')];
44
+ return { name, version, normalized: pythonName(name), ranges, source: packageSource(type), sourceReference, sourceKey,
45
+ groups: groupNames(pkg), hashes: fileDigests(pkg.get('files')), dependencies: dependencyRows(pkg) };
46
+ }
47
+ export function readPoetryLock(text) {
48
+ const document = readTomlDocument(text);
49
+ const metadata = tableField(document.root, 'metadata');
50
+ const version = textField(metadata, 'lock-version');
51
+ if (!version || !/^[12]\.\d+$/.test(version))
52
+ throw new Error('Unsupported Poetry lock version');
53
+ const rows = document.root.get('package') ?? [];
54
+ if (!Array.isArray(rows))
55
+ throw new Error('Invalid Poetry package list');
56
+ const packages = rows.map(pkg => readPackage(pkg, document));
57
+ const ids = pypiIds(packages, pkg => pypi(pkg.name, pkg.version), pkg => pkg.sourceKey);
58
+ const names = new Map();
59
+ for (const { normalized } of packages)
60
+ names.set(normalized, (names.get(normalized) ?? 0) + 1);
61
+ const legacy = tableField(metadata, 'files') ?? tableField(metadata, 'hashes');
62
+ const legacyHashes = new Map([...legacy ?? []].map(([name, hashes]) => [pythonName(name), fileDigests(hashes)]));
63
+ return packages.map(({ sourceKey: _sourceKey, normalized, ...pkg }, index) => ({
64
+ ...pkg, id: ids[index],
65
+ hashes: pkg.hashes.length || names.get(normalized) !== 1 ? pkg.hashes : legacyHashes.get(normalized) ?? [],
66
+ }));
67
+ }
68
+ //# sourceMappingURL=poetry-lock.js.map
@@ -0,0 +1,3 @@
1
+ import type { LockFormat, LockGraph, LockInput } from '../model.js';
2
+ export declare const poetry: LockFormat;
3
+ export declare function readPoetry(input: LockInput): LockGraph;