@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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Idleware
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,51 @@
1
- # Temporary Holding Version
1
+ # Lockparse
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Read dependency graphs from lockfiles. Pass the file contents and any companion manifests. Each graph includes package identities, recorded dependency edges, native groups, and source line ranges. The graph lists which information the input could not establish.
4
+
5
+ ```js
6
+ import { detect, formats, parse } from '@repometry/lockparse'
7
+
8
+ // Discover supported filenames and capabilities.
9
+ console.log(formats)
10
+
11
+ // Detect a lockfile from its repository-relative path.
12
+ const format = detect('apps/shop/composer.lock', lockfileText)
13
+
14
+ // Decode supplied text and optionally its adjacent manifest.
15
+ const result = parse({
16
+ path: 'apps/shop/composer.lock',
17
+ text: lockfileText,
18
+ manifests: { 'apps/shop/composer.json': manifestText },
19
+ })
20
+ if ('error' in result) throw new Error(result.error.message)
21
+ console.log(result.packages, result.edges, result.losses)
22
+ ```
23
+
24
+ ## Requirements
25
+
26
+ Node.js 24 or newer. Parsing uses the supplied text and accepts up to 64 MiB per input file.
27
+
28
+ ## Supported lockfiles
29
+
30
+ | Ecosystem | Files |
31
+ | --- | --- |
32
+ | PHP | `composer.lock` |
33
+ | Rust | `Cargo.lock` |
34
+ | Go | `go.mod` with companion `go.sum` |
35
+ | Ruby | `Gemfile.lock` |
36
+ | Python | `poetry.lock`, `uv.lock`, `Pipfile.lock`, `pdm.lock` |
37
+ | JVM | `gradle.lockfile`, `buildscript-gradle.lockfile` |
38
+ | .NET | `packages.lock.json` |
39
+ | Swift | `Package.resolved` |
40
+ | Dart | `pubspec.lock` |
41
+ | Elixir | `mix.lock` |
42
+ | Terraform | `.terraform.lock.hcl` |
43
+ | R | `renv.lock` |
44
+ | Perl | `cpanfile.snapshot` |
45
+ | C and C++ | `conan.lock` |
46
+
47
+ A capability in `formats` means that a reader can report it when the source format records it. A specific file can leave that capability unavailable. The `losses` field makes these gaps explicit.
48
+
49
+ ## License
50
+
51
+ [MIT](./LICENSE)
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@repometry/lockparse",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.0",
4
+ "description": "Read dependency graphs from lockfiles",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/idleware/repometry.git"
9
+ },
10
+ "type": "module",
11
+ "engines": {
12
+ "node": ">=24"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "types": "./target/main/ts/index.d.ts",
17
+ "default": "./target/main/ts/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "target/main",
22
+ "!target/main/**/*.map"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "scripts": {
28
+ "build": "node -e \"require('node:fs').rmSync('target/main', { recursive: true, force: true })\" && tsc -p tsconfig.json",
29
+ "build:test": "node -e \"require('node:fs').rmSync('target/verification', { recursive: true, force: true })\" && tsc -p tsconfig.test.json",
30
+ "test": "npm run build:test && node --test 'target/verification/test/ts/*.test.js'",
31
+ "prepack": "npm run build"
32
+ }
33
+ }
@@ -0,0 +1 @@
1
+ export declare function literalGemGroups(text: string): ReadonlyMap<string, ReadonlySet<string>>;
@@ -0,0 +1,191 @@
1
+ import { readLines } from './lines.js';
2
+ const words = /[A-Za-z_][A-Za-z0-9_!?]*/y;
3
+ const options = /[A-Za-z_][A-Za-z0-9_]*:/y;
4
+ const symbol = /:[A-Za-z_][A-Za-z0-9_]*[!?]?/y;
5
+ const strings = /"[^"\\\r\n]*"|'[^'\\\r\n]*'/y;
6
+ const punctuation = new Set(['(', ')', '[', ']', ',']);
7
+ const openBlocks = new Set(['group', 'source', 'platforms', 'platform', 'git', 'path']);
8
+ const leafStatements = new Set(['source', 'ruby', 'gemspec']);
9
+ export function literalGemGroups(text) {
10
+ const blocks = [];
11
+ const groups = new Map();
12
+ for (const row of statements(text)) {
13
+ const first = row[0];
14
+ if (first?.kind !== 'word')
15
+ throw unsupported();
16
+ if (first.value === 'end' && row.length === 1) {
17
+ if (blocks.pop() === undefined)
18
+ throw unsupported();
19
+ continue;
20
+ }
21
+ const opener = row.at(-1);
22
+ const opens = opener?.kind === 'word' && opener.value === 'do';
23
+ const args = argumentsOf(row.slice(1, opens ? -1 : undefined));
24
+ if (opens) {
25
+ if (!openBlocks.has(first.value) || blocks.length === 64)
26
+ throw unsupported();
27
+ blocks.push({ labels: first.value === 'group' ? groupLabels(args.positional) : [],
28
+ platform: first.value === 'platform' || first.value === 'platforms' });
29
+ }
30
+ else if (first.value === 'gem') {
31
+ const name = args.positional[0];
32
+ if (typeof name !== 'string' || !/^[A-Za-z0-9_.-]+$/.test(name))
33
+ throw unsupported();
34
+ if (blocks.some(block => block.platform) || args.options.has('platform') || args.options.has('platforms'))
35
+ continue;
36
+ const originalGroups = args.options.get('groups') || args.options.get('group');
37
+ const literals = groupLabels(originalGroups === undefined || originalGroups === null ? []
38
+ : Array.isArray(originalGroups) ? originalGroups : [originalGroups]);
39
+ const declared = [...blocks.flatMap(block => block.labels), ...literals];
40
+ const known = groups.get(name) ?? new Set();
41
+ for (const label of declared.length ? declared : ['default'])
42
+ known.add(label);
43
+ groups.set(name, known);
44
+ }
45
+ else if (!leafStatements.has(first.value))
46
+ throw unsupported();
47
+ }
48
+ if (blocks.length > 0)
49
+ throw unsupported();
50
+ return groups;
51
+ }
52
+ function groupLabels(values) {
53
+ return values.map(value => {
54
+ if (typeof value !== 'string' || value === '')
55
+ throw unsupported();
56
+ return value;
57
+ });
58
+ }
59
+ function unsupported() {
60
+ return new Error('Gemfile uses nonliteral declarations');
61
+ }
62
+ function* statements(text) {
63
+ const collections = [];
64
+ let tokens = [];
65
+ for (const { text: row } of readLines(text)) {
66
+ for (const token of tokenize(row)) {
67
+ if (token.kind === 'punctuation')
68
+ trackCollections(collections, token.value);
69
+ tokens.push(token);
70
+ }
71
+ if (tokens.length && collections.length === 0 && tokens.at(-1)?.value !== ',') {
72
+ yield tokens;
73
+ tokens = [];
74
+ }
75
+ }
76
+ if (tokens.length > 0)
77
+ throw unsupported();
78
+ }
79
+ function trackCollections(collections, char) {
80
+ if (char === '(' || char === '[') {
81
+ collections.push(char === '(' ? ')' : ']');
82
+ if (collections.length > 64)
83
+ throw unsupported();
84
+ }
85
+ else if ((char === ')' || char === ']') && collections.pop() !== char)
86
+ throw unsupported();
87
+ }
88
+ function* tokenize(row) {
89
+ for (let position = 0; position < row.length;) {
90
+ const char = row[position];
91
+ if (char === ' ' || char === '\t') {
92
+ position++;
93
+ }
94
+ else if (char === '#') {
95
+ return;
96
+ }
97
+ else if (punctuation.has(char)) {
98
+ yield { kind: 'punctuation', value: char };
99
+ position++;
100
+ }
101
+ else {
102
+ const [token, end] = literalToken(row, position, char);
103
+ yield token;
104
+ position = end;
105
+ }
106
+ }
107
+ }
108
+ function literalToken(row, position, character) {
109
+ if (character === '"' || character === "'") {
110
+ const [value, end] = matchAt(strings, row, position);
111
+ const content = value.slice(1, -1);
112
+ if (character === '"' && /#[@{$]/.test(content))
113
+ throw unsupported();
114
+ return [{ kind: 'text', value: content }, end];
115
+ }
116
+ if (character === ':') {
117
+ const [value, end] = matchAt(symbol, row, position);
118
+ return [{ kind: 'symbol', value: value.slice(1) }, end];
119
+ }
120
+ options.lastIndex = position;
121
+ const option = options.exec(row);
122
+ if (option)
123
+ return [{ kind: 'option', value: option[0].slice(0, -1) }, options.lastIndex];
124
+ const [value, end] = matchAt(words, row, position);
125
+ return [{ kind: 'word', value }, end];
126
+ }
127
+ function matchAt(pattern, row, position) {
128
+ pattern.lastIndex = position;
129
+ const result = pattern.exec(row);
130
+ if (result === null)
131
+ throw unsupported();
132
+ return [result[0], pattern.lastIndex];
133
+ }
134
+ function argumentsOf(input) {
135
+ let tokens = input;
136
+ if (input[0]?.value === '(') {
137
+ if (input.at(-1)?.value !== ')')
138
+ throw unsupported();
139
+ tokens = input.slice(1, -1);
140
+ }
141
+ const positional = [];
142
+ const options = new Map();
143
+ for (let index = 0; index < tokens.length;) {
144
+ const token = tokens[index];
145
+ const named = token.kind === 'option';
146
+ const [literal, end] = argument(tokens, named ? index + 1 : index);
147
+ index = end;
148
+ if (named)
149
+ options.set(token.value, literal);
150
+ else
151
+ positional.push(literal);
152
+ if (index < tokens.length) {
153
+ if (tokens[index]?.value !== ',')
154
+ throw unsupported();
155
+ index++;
156
+ }
157
+ }
158
+ return { positional, options };
159
+ }
160
+ function argument(tokens, index) {
161
+ const token = tokens[index];
162
+ if (token?.kind === 'text' || token?.kind === 'symbol')
163
+ return [token.value, index + 1];
164
+ if (token?.kind === 'word') {
165
+ if (token.value === 'true' || token.value === 'false')
166
+ return [token.value === 'true', index + 1];
167
+ if (token.value === 'nil')
168
+ return [null, index + 1];
169
+ }
170
+ if (token?.value === '[')
171
+ return array(tokens, index);
172
+ throw unsupported();
173
+ }
174
+ function array(tokens, start) {
175
+ const result = [];
176
+ for (let index = start + 1; index < tokens.length; index++) {
177
+ const token = tokens[index];
178
+ if (token.kind === 'punctuation' && token.value === ']')
179
+ return [result, index + 1];
180
+ if ((index - start) % 2) {
181
+ if (token.kind !== 'text' && token.kind !== 'symbol')
182
+ throw unsupported();
183
+ result.push(token.value);
184
+ }
185
+ else if (token.kind !== 'punctuation' || token.value !== ',') {
186
+ throw unsupported();
187
+ }
188
+ }
189
+ throw unsupported();
190
+ }
191
+ //# sourceMappingURL=bundler-gemfile.js.map
@@ -0,0 +1,26 @@
1
+ import type { PackageSource } from '../model.js';
2
+ interface Coordinate {
3
+ readonly name: string;
4
+ readonly version: string;
5
+ readonly platform?: string;
6
+ }
7
+ export interface Requirement {
8
+ readonly name: string;
9
+ readonly exactVersion?: string;
10
+ }
11
+ export interface GemSpec extends Coordinate {
12
+ readonly source: PackageSource;
13
+ readonly line: number;
14
+ endLine: number;
15
+ readonly requirements: Requirement[];
16
+ }
17
+ interface Lock {
18
+ readonly specs: GemSpec[];
19
+ readonly checksums: Map<string, string>;
20
+ readonly direct: Requirement[];
21
+ hasDirect: boolean;
22
+ abiChecksums: boolean;
23
+ }
24
+ export declare function readBundlerLock(text: string): Lock;
25
+ export declare function gemKey({ name, version, platform }: Coordinate): string;
26
+ export {};
@@ -0,0 +1,110 @@
1
+ import { readLines } from './lines.js';
2
+ const sources = new Map([
3
+ ['GEM', 'registry'], ['GIT', 'git'], ['PATH', 'path'], ['PLUGIN SOURCE', 'unknown'],
4
+ ]);
5
+ const coordinate = /^([A-Za-z0-9_.-]+) \(([0-9]+(?:\.[A-Za-z0-9]+)*)(?:-([A-Za-z0-9_.-]+))?\)(.*)$/;
6
+ export function readBundlerLock(text) {
7
+ const lock = { specs: [], direct: [], checksums: new Map(), hasDirect: false, abiChecksums: false };
8
+ let section;
9
+ let spec;
10
+ let source;
11
+ let inSpecs = false;
12
+ let sawSpecs = false;
13
+ for (const { text: row, offset } of readLines(text)) {
14
+ if (!row.trim())
15
+ continue;
16
+ if (!row.startsWith(' ')) {
17
+ if (!/^[A-Z][A-Z0-9 ]+$/.test(row.trimEnd()) || (source !== undefined && !inSpecs))
18
+ throw invalid(offset);
19
+ section = row.trimEnd();
20
+ source = sources.get(section);
21
+ spec = undefined;
22
+ inSpecs = false;
23
+ lock.hasDirect ||= section === 'DEPENDENCIES';
24
+ continue;
25
+ }
26
+ if (source !== undefined) {
27
+ if (row.trimEnd() === ' specs:') {
28
+ if (inSpecs)
29
+ throw invalid(offset);
30
+ inSpecs = true;
31
+ sawSpecs = true;
32
+ }
33
+ else if (inSpecs) {
34
+ spec = readSpecRow(lock.specs, spec, row, source, offset);
35
+ }
36
+ else if (!/^ {2}[A-Za-z_-]+: .+$/.test(row)) {
37
+ throw invalid(offset);
38
+ }
39
+ }
40
+ else {
41
+ readSectionRow(lock, section, row, offset);
42
+ }
43
+ }
44
+ if (!sawSpecs || (source !== undefined && !inSpecs))
45
+ throw new Error('Gemfile.lock has no complete specs section');
46
+ return lock;
47
+ }
48
+ function readSpecRow(specs, current, row, source, offset) {
49
+ if (/^ {6}\S/.test(row) && current !== undefined) {
50
+ current.requirements.push(requirement(row.trim(), offset));
51
+ current.endLine = offset + 1;
52
+ return current;
53
+ }
54
+ if (!/^ {4}\S/.test(row))
55
+ throw invalid(offset);
56
+ const match = coordinate.exec(row.trim());
57
+ if (match === null || match[4] !== '')
58
+ throw invalid(offset);
59
+ const [, name, version, platform] = match;
60
+ const spec = { name: name, version: version, platform, source, line: offset + 1, endLine: offset + 1,
61
+ requirements: [] };
62
+ specs.push(spec);
63
+ return spec;
64
+ }
65
+ function readSectionRow(lock, section, row, offset) {
66
+ if (section === undefined)
67
+ throw invalid(offset);
68
+ if (section === 'DEPENDENCIES') {
69
+ if (!/^ {2}\S/.test(row))
70
+ throw invalid(offset);
71
+ lock.direct.push(requirement(row.trim(), offset));
72
+ }
73
+ else if (section === 'CHECKSUMS') {
74
+ rememberChecksum(lock.checksums, row.trim(), offset);
75
+ }
76
+ else if (section === 'CONTENT ADDRESSES') {
77
+ lock.abiChecksums = true;
78
+ }
79
+ }
80
+ function requirement(row, offset) {
81
+ const match = /^([A-Za-z0-9_.-]+)(?: \(([^()]*)\))?!?$/.exec(row);
82
+ if (match === null)
83
+ throw invalid(offset);
84
+ const exactVersion = /^=\s*([0-9]+(?:\.[A-Za-z0-9]+)*)$/.exec(match[2] ?? '')?.[1];
85
+ return { name: match[1], exactVersion };
86
+ }
87
+ function rememberChecksum(checksums, row, offset) {
88
+ const match = coordinate.exec(row);
89
+ if (match === null)
90
+ throw invalid(offset);
91
+ const [, name, version, platform, suffix] = match;
92
+ const digest = /(?:^|\s|,)sha256=([a-fA-F0-9]{64})(?=$|\s|,)/.exec(suffix)?.[1];
93
+ if (digest === undefined) {
94
+ if (suffix.includes('sha256='))
95
+ throw invalid(offset);
96
+ return;
97
+ }
98
+ const key = gemKey({ name: name, version: version, platform });
99
+ const previous = checksums.get(key);
100
+ if (previous !== undefined && previous !== digest.toLowerCase())
101
+ throw invalid(offset);
102
+ checksums.set(key, digest.toLowerCase());
103
+ }
104
+ export function gemKey({ name, version, platform }) {
105
+ return JSON.stringify([name, version, platform ?? 'ruby']);
106
+ }
107
+ function invalid(offset) {
108
+ return new Error('Invalid Gemfile.lock record on line ' + (offset + 1));
109
+ }
110
+ //# sourceMappingURL=bundler-lock.js.map
@@ -0,0 +1,3 @@
1
+ import type { LockFormat, LockGraph, LockInput } from '../model.js';
2
+ export declare const bundler: LockFormat;
3
+ export declare function readBundler(input: LockInput): LockGraph;
@@ -0,0 +1,84 @@
1
+ import { literalGemGroups } from './bundler-gemfile.js';
2
+ import { gemKey, readBundlerLock } from './bundler-lock.js';
3
+ export const bundler = { id: 'bundler', ecosystem: 'rubygems', files: ['Gemfile.lock'],
4
+ edges: true, direct: true, licenses: false };
5
+ function packageUrl({ name, version, platform }) {
6
+ const base = 'pkg:gem/' + encodeURIComponent(name) + '@' + encodeURIComponent(version);
7
+ return platform === undefined || platform === 'ruby' ? base : base + '?platform=' + encodeURIComponent(platform);
8
+ }
9
+ function dependencyIndex(specs) {
10
+ const index = new Map();
11
+ for (const spec of specs) {
12
+ const siblings = index.get(spec.name) ?? [];
13
+ siblings.push(spec);
14
+ index.set(spec.name, siblings);
15
+ }
16
+ return index;
17
+ }
18
+ function matches(requirement, index) {
19
+ const options = index.get(requirement.name) ?? [];
20
+ return requirement.exactVersion === undefined ? options : options.filter(sibling => sibling.version === requirement.exactVersion);
21
+ }
22
+ function internalEdges(specs, index) {
23
+ const edges = [];
24
+ let unresolved = false;
25
+ for (const spec of specs) {
26
+ const from = packageUrl(spec);
27
+ const seen = new Set();
28
+ for (const dependency of spec.requirements) {
29
+ const options = matches(dependency, index);
30
+ if (options.length !== 1)
31
+ unresolved = true;
32
+ else
33
+ seen.add(packageUrl(options[0]));
34
+ }
35
+ for (const to of seen)
36
+ edges.push({ from, to });
37
+ }
38
+ return { edges, unresolved };
39
+ }
40
+ function gemGroups({ path, manifests }) {
41
+ const filename = path.slice(0, path.lastIndexOf('/') + 1) + 'Gemfile';
42
+ if (manifests && Object.hasOwn(manifests, filename)) {
43
+ try {
44
+ return literalGemGroups(manifests[filename]);
45
+ }
46
+ catch {
47
+ // Gemfiles are executable Ruby. Ignore companions whose groups require evaluation.
48
+ }
49
+ }
50
+ return new Map();
51
+ }
52
+ export function readBundler(input) {
53
+ const lock = readBundlerLock(input.text);
54
+ const labels = gemGroups(input);
55
+ if (new Set(lock.specs.map(gemKey)).size !== lock.specs.length)
56
+ throw new Error('Duplicate Bundler gem identity');
57
+ const index = dependencyIndex(lock.specs);
58
+ const { edges, unresolved } = internalEdges(lock.specs, index);
59
+ const requested = new Set(lock.direct.flatMap(row => matches(row, index).map(packageUrl)));
60
+ const missingDirect = lock.direct.some(row => matches(row, index).length === 0);
61
+ const packages = lock.specs.map((gem) => {
62
+ const { name, version, source, line, endLine } = gem;
63
+ const id = packageUrl(gem);
64
+ const checksum = lock.checksums.get(gemKey(gem));
65
+ const direct = requested.has(id) ? true : lock.hasDirect ? false : 'unknown';
66
+ const groups = direct === true ? [...labels.get(name) ?? []].sort() : [];
67
+ return { id, name, version, source, line, endLine, groups, direct,
68
+ ...(checksum === undefined ? {} : { integrity: 'sha256:' + checksum }) };
69
+ });
70
+ const losses = [];
71
+ if (unresolved)
72
+ losses.push('edges');
73
+ if (!lock.hasDirect || missingDirect)
74
+ losses.push('direct');
75
+ losses.push('roots');
76
+ if (packages.some(({ groups }) => groups.length === 0))
77
+ losses.push('groups');
78
+ if (packages.length)
79
+ losses.push('licenses');
80
+ if (lock.abiChecksums)
81
+ losses.push('integrity');
82
+ return { format: 'bundler', ecosystem: 'rubygems', packages, edges, roots: [], losses };
83
+ }
84
+ //# sourceMappingURL=bundler.js.map
@@ -0,0 +1,15 @@
1
+ interface Position {
2
+ readonly line: number;
3
+ readonly endLine: number;
4
+ }
5
+ type Value = number | string | readonly string[];
6
+ interface TomlSection extends Position {
7
+ readonly fields: ReadonlyMap<string, Value>;
8
+ }
9
+ interface CargoLock {
10
+ readonly packages: readonly TomlSection[];
11
+ readonly root?: TomlSection;
12
+ readonly metadata: ReadonlyMap<string, Value>;
13
+ }
14
+ export declare function readCargoLock(text: string): CargoLock;
15
+ export {};
@@ -0,0 +1,127 @@
1
+ import { readLines } from './lines.js';
2
+ const scalar = /(?:"(?:[^"\\\r\n]|\\.)*"|'[^'\r\n]*')/y;
3
+ const blank = /^(?:[ \t]*(?:#.*)?$)/;
4
+ const assignment = /^([A-Za-z0-9_-]+|"(?:[^"\\\r\n]|\\.)*"|'[^'\r\n]*')[ \t]*=[ \t]*/;
5
+ export function readCargoLock(text) {
6
+ const lines = readLines(text);
7
+ const sections = [];
8
+ const top = section(1);
9
+ const metadata = section(1);
10
+ let root;
11
+ let current = top;
12
+ let sawMetadata = false;
13
+ for (const { text: row, offset } of lines) {
14
+ const line = row.trim();
15
+ if (blank.test(line))
16
+ continue;
17
+ const header = /^\[(\[)?([\w.]+)\](\])?[ \t]*(?:#.*)?$/.exec(line);
18
+ if (header) {
19
+ if (Boolean(header[1]) !== Boolean(header[3]))
20
+ throw malformed(offset);
21
+ const kind = header[2];
22
+ if (kind === 'package' && header[1]) {
23
+ current = section(offset + 1);
24
+ sections.push(current);
25
+ }
26
+ else if (kind === 'root' && !header[1] && root === undefined) {
27
+ root = current = section(offset + 1);
28
+ }
29
+ else if (kind === 'metadata' && !header[1] && !sawMetadata) {
30
+ sawMetadata = true;
31
+ current = metadata;
32
+ }
33
+ else if (kind === 'patch.unused' && header[1]) {
34
+ current = section(offset + 1);
35
+ }
36
+ else
37
+ throw malformed(offset);
38
+ continue;
39
+ }
40
+ const field = assignment.exec(line);
41
+ if (!field)
42
+ throw malformed(offset);
43
+ const key = decodeKey(field[1]);
44
+ if (current.fields.has(key))
45
+ throw malformed(offset);
46
+ const rest = line.slice(field[0].length);
47
+ const item = value(lines, rest, offset, current === top && key === 'version');
48
+ current.fields.set(key, item.data);
49
+ current.endLine = item.offset + 1;
50
+ }
51
+ const version = top.fields.get('version');
52
+ if (top.fields.size !== Number(version !== undefined) || (version !== undefined && (typeof version !== 'number' || version < 1 || version > 4))) {
53
+ throw new Error('Unsupported Cargo.lock version');
54
+ }
55
+ if (sections.length === 0 && root === undefined)
56
+ throw new Error('Cargo.lock has no package records');
57
+ return { packages: sections, root, metadata: metadata.fields };
58
+ }
59
+ function section(line) {
60
+ return { line, endLine: line, fields: new Map() };
61
+ }
62
+ function malformed(offset) {
63
+ return new Error('Invalid Cargo.lock entry on line ' + (offset + 1));
64
+ }
65
+ function decodeKey(key) {
66
+ return key.startsWith('"') || key.startsWith("'") ? decodeString(key) : key;
67
+ }
68
+ function decodeString(token) {
69
+ if (token.startsWith("'"))
70
+ return token.slice(1, -1);
71
+ // TOML also permits eight-digit Unicode escapes, unlike JSON.
72
+ const escaped = token.replace(/\\(?:U([\da-fA-F]{8})|.)/g, (match, code) => {
73
+ if (code === undefined)
74
+ return match;
75
+ const point = Number.parseInt(code, 16);
76
+ if (point >= 0xd800 && point <= 0xdfff)
77
+ throw new Error('Invalid Cargo.lock Unicode escape');
78
+ return JSON.stringify(String.fromCodePoint(point)).slice(1, -1);
79
+ });
80
+ return JSON.parse(escaped);
81
+ }
82
+ function value(lines, rest, offset, integer) {
83
+ if (!integer && rest.startsWith('['))
84
+ return stringList(lines, rest.slice(1), offset);
85
+ scalar.lastIndex = 0;
86
+ const match = (integer ? /^\d+/ : scalar).exec(rest);
87
+ if (!match || !blank.test(rest.slice(match[0].length)))
88
+ throw malformed(offset);
89
+ return { data: integer ? Number(match[0]) : decodeString(match[0]), offset };
90
+ }
91
+ function stringList(lines, first, start) {
92
+ const data = [];
93
+ let expectItem = true;
94
+ let current = { done: false, value: { text: first, offset: start } };
95
+ while (!current.done) {
96
+ const { text, offset } = current.value;
97
+ let position = 0;
98
+ while (position < text.length) {
99
+ const token = text[position];
100
+ if (token === ' ' || token === '\t') {
101
+ position++;
102
+ continue;
103
+ }
104
+ if (token === '#')
105
+ break;
106
+ if (token === ']' && blank.test(text.slice(position + 1)))
107
+ return { data, offset };
108
+ if (expectItem) {
109
+ scalar.lastIndex = position;
110
+ const item = scalar.exec(text);
111
+ if (!item)
112
+ throw malformed(offset);
113
+ data.push(decodeString(item[0]));
114
+ position = scalar.lastIndex;
115
+ }
116
+ else {
117
+ if (token !== ',')
118
+ throw malformed(offset);
119
+ position++;
120
+ }
121
+ expectItem = !expectItem;
122
+ }
123
+ current = lines.next();
124
+ }
125
+ throw malformed(start);
126
+ }
127
+ //# sourceMappingURL=cargo-lock.js.map