@exodus/stasis 1.0.0-beta.15 → 1.0.0-beta.17

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 (67) hide show
  1. package/README.md +1 -1
  2. package/bin/stasis.js +26 -11
  3. package/package.json +10 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +113 -233
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +50 -166
  11. package/src/cmd/bundle-options.js +110 -167
  12. package/src/cmd/bundle.js +861 -1344
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +46 -27
  15. package/src/cmd/output.js +17 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +10 -23
  18. package/src/define.js +34 -97
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +10 -25
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +9 -33
  37. package/src/metro-resolver.js +20 -47
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +2 -5
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +90 -220
  44. package/src/resolve-node.js +27 -45
  45. package/src/resolve-typescript.js +109 -313
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +52 -128
  49. package/src/scan.js +136 -283
  50. package/src/sixel.js +18 -41
  51. package/src/table.js +14 -35
  52. package/src/validate-notes.d.ts +3 -2
  53. package/src/validate-notes.js +25 -19
  54. package/src/validate.d.ts +38 -11
  55. package/src/validate.js +478 -512
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +88 -121
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  60. package/src/vfs-bundle/prisma.js +167 -110
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +75 -231
  67. package/src/installed-commits.js +0 -184
package/src/rollup.js CHANGED
@@ -1,5 +1 @@
1
- // Public-export adapter: backs `@exodus/stasis/rollup`. The plugin lives in
2
- // @exodus/stasis-plugins (which depends on @exodus/stasis-core for State /
3
- // resolvePluginState). Internal code uses `@exodus/stasis-plugins/rollup` directly;
4
- // this file exists only to back the package's `exports` map.
5
1
  export * from '@exodus/stasis-plugins/rollup'
@@ -0,0 +1,10 @@
1
+ import { INSTALLED } from '@exodus/stasis-core/util'
2
+ import { installedRecords } from './installed.js'
3
+
4
+ // `stasis run` preloads this before the loader; what it reads is kept across a run's calls.
5
+ const cache = { stores: new Map(), layouts: new Map() }
6
+ globalThis[INSTALLED] = (modules, root) => {
7
+ const accounts = installedRecords(modules, root, undefined, cache)
8
+ for (const [dir, { repo: _repo, ...account }] of accounts) accounts.set(dir, account)
9
+ return accounts
10
+ }
package/src/sbom.js CHANGED
@@ -1,59 +1,38 @@
1
1
  import { compareVersions, valid } from '@preventive/upstream/semver.js'
2
2
  import pkg from '../package.json' with { type: 'json' }
3
3
 
4
- // Derive an SPDX 2.3 or CycloneDX 1.5 SBOM from already-parsed stasis artifacts (`Bundle`/`Lockfile`).
5
- // Deliberately free of file/transport concerns (no disk, no brotli); the disk-reading CLI glue is src/cmd/sbom.js.
4
+ // An SPDX 2.3 or CycloneDX 1.5 SBOM of parsed artifacts, free of disk and brotli (the CLI's glue is src/cmd/sbom.js).
6
5
 
7
- // Tool/creator identity stamped into every generated SBOM.
8
6
  const DEFAULT_TOOL = { name: '@exodus/stasis', version: pkg.version, vendor: 'Exodus Movement, Inc.' }
9
7
 
10
- // Web Crypto (global) instead of node:crypto, keeping this module dependency-light.
11
8
  const newUuid = () => globalThis.crypto.randomUUID()
12
9
 
13
- // purl types we can mint; an ecosystem with no registered type (`soldeer`) gets no purl rather than a fabricated one,
14
- // and so does a crate vendored from git or from a source not known (`cargo-git`, `cargo-unknown`): `pkg:cargo` names
15
- // crates.io's crate of that name, which it may not be.
10
+ // purl types we can mint: `soldeer` has no registered type, and `pkg:cargo` names crates.io's crate, which a
11
+ // crate vendored from git or from a source not known (`cargo-git`, `cargo-unknown`) may not be.
16
12
  const PURL_TYPES = new Set(['npm', 'composer', 'cargo', 'github'])
17
13
 
18
- // Infer an artifact's ecosystem from its loader formats: PHP bundles map to Composer, everything
19
- // else to npm. Used for untagged (first-party) buckets and as the fallback for legacy artifacts
20
- // predating the per-dependency `ecosystem` field.
21
- function detectEcosystem(artifact) {
22
- const formats = artifact.formats
23
- if (formats && typeof formats.values === 'function') {
24
- for (const format of formats.values()) {
25
- if (format === 'php') return 'composer'
26
- }
27
- }
28
- return 'npm'
29
- }
14
+ // An artifact's ecosystem by its formats, PHP's Composer, any other npm: its untagged buckets', and every
15
+ // bucket's in an artifact predating the per-dependency `ecosystem` field.
16
+ const detectEcosystem = (artifact) => ([...(artifact.formats?.values?.() ?? [])].includes('php') ? 'composer' : 'npm')
30
17
 
31
- // Legacy scope classification (pre-`ecosystem` artifacts): non-`node_modules` dirs are first-party,
32
- // except in a Composer bundle where vendor packages live outside `node_modules`. `node_modules` is
33
- // a path segment, as stasis-core's hasNodeModulesSegment has it: `foo_node_modules/x` is first-party.
18
+ // For a pre-`ecosystem` artifact: a dir is first-party outside a `node_modules` segment (`foo_node_modules/x` is),
19
+ // but in a Composer bundle, whose vendor packages live outside one.
34
20
  function classifyScope(ecosystem, dir) {
35
21
  if (dir === '.') return 'workspace'
36
22
  if (ecosystem === 'composer') return 'dependency'
37
23
  return dir.split('/').includes('node_modules') ? 'dependency' : 'workspace'
38
24
  }
39
25
 
40
- // Split a package name into purl/CycloneDX namespace (group) + bare name: npm scopes, Composer
41
- // vendors and GitHub owners use the segment before the first slash; cargo and bare names have none.
26
+ // The purl/CycloneDX namespace (group): an npm scope, a Composer vendor or a GitHub owner.
42
27
  function splitName(ecosystem, name) {
43
- const namespaced = ecosystem === 'composer' || ecosystem === 'github' ||
44
- (ecosystem === 'npm' && name.startsWith('@'))
45
- if (namespaced && name.includes('/')) {
46
- const slash = name.indexOf('/')
47
- return { group: name.slice(0, slash), name: name.slice(slash + 1) }
48
- }
49
- return { group: undefined, name }
28
+ const namespaced = ecosystem === 'composer' || ecosystem === 'github' || (ecosystem === 'npm' && name.startsWith('@'))
29
+ const slash = name.indexOf('/')
30
+ if (!namespaced || slash === -1) return { group: undefined, name }
31
+ return { group: name.slice(0, slash), name: name.slice(slash + 1) }
50
32
  }
51
33
 
52
- // Build a Package URL (purl) for a known ecosystem, or null when it has no purl type. Each path
53
- // segment is percent-encoded (npm scope's `@` -> `%40`) while the `/` namespace separator and the
54
- // `@` before the version stay literal; Composer and GitHub names are lowercased, per the purl spec.
55
- // The version qualifier is optional in the purl spec, so a versionless workspace package mints a
56
- // bare-name purl rather than a fabricated version.
34
+ // A Package URL (purl), or null for an ecosystem with no purl type: each segment percent-encoded (npm's `@`
35
+ // scope -> `%40`), Composer and GitHub names lowercased, per the purl spec; with no version, a bare-name purl.
57
36
  export function buildPurl(ecosystem, rawName, version) {
58
37
  if (!PURL_TYPES.has(ecosystem)) return null
59
38
  let { group, name } = splitName(ecosystem, rawName)
@@ -68,7 +47,7 @@ export function buildPurl(ecosystem, rawName, version) {
68
47
 
69
48
  function compareComponents(a, b) {
70
49
  if (a.name !== b.name) return a.name < b.name ? -1 : 1
71
- // A missing version sorts first, as '' (comparing raw undefined would be order-unstable).
50
+ // As '': comparing raw undefined would be order-unstable.
72
51
  const [av, bv] = [a.version ?? '', b.version ?? '']
73
52
  if (av !== bv) {
74
53
  if (valid(av) && valid(bv)) return compareVersions(av, bv)
@@ -77,40 +56,20 @@ function compareComponents(a, b) {
77
56
  return a.ecosystem < b.ecosystem ? -1 : a.ecosystem > b.ecosystem ? 1 : 0
78
57
  }
79
58
 
80
- // Collect the deduplicated, sorted set of packages across parsed artifacts. Unlike `stasis audit`
81
- // (installed deps only), an SBOM is the *full* bill of materials, so workspace/first-party packages
82
- // are included too, tagged scope 'workspace' vs 'dependency'. Buckets without a name are skipped,
83
- // and so are DEPENDENCY buckets without a version (v0 partial metadata: unverifiable identity) --
84
- // but a workspace package may legitimately omit version (private/unpublished) and is kept, since
85
- // dropping it would silently lose the document's own first-party subject. Dedup is by
86
- // ecosystem+name+version; a package seen as workspace in any artifact stays workspace regardless
87
- // of argument order, so output doesn't depend on how artifacts are listed.
88
- // A dependency's `vendored` packages (those copied into it whose files a bundle carries) ride on its
89
- // component as `vendored`, sorted by `dir`: each its `name`, the `version` its package.json gives if
90
- // any, and a purl only beside one -- a vendored package.json without a version names no release, and
91
- // may not name a registry package at all (Next.js calls its React copy `react-builtin`).
59
+ // The artifacts' named packages, deduped by ecosystem+name+version and sorted: a dependency only with a version,
60
+ // a first-party one ('workspace' where any artifact has it so) with or without. A dependency's `vendored` copies
61
+ // ride on it, by `dir`, a purl only beside a version (Next.js's React copy, `react-builtin`, names no release).
92
62
  export function collectComponents(artifacts) {
93
63
  const seen = new Map()
94
64
  for (const artifact of artifacts) {
95
65
  const records = [...artifact.modules]
96
- // The per-dependency `ecosystem` field separates deps (tagged) from first-party buckets (untagged);
97
- // if no bucket carries it, the artifact predates the field and we use the heuristic for everything.
66
+ // An artifact where no bucket carries `ecosystem` predates the field: classifyScope tells deps apart.
98
67
  const tagged = records.some(([, m]) => m.ecosystem !== undefined)
99
68
  const fileEcosystem = detectEcosystem(artifact)
100
69
  for (const [dir, { name, version, ecosystem, vendored }] of records) {
101
70
  if (!name) continue
102
- let scope, eco
103
- if (ecosystem !== undefined) {
104
- scope = 'dependency'
105
- eco = ecosystem
106
- } else if (tagged) {
107
- // Untagged bucket in a modern artifact: first-party workspace code; ecosystem inferred.
108
- scope = 'workspace'
109
- eco = fileEcosystem
110
- } else {
111
- scope = classifyScope(fileEcosystem, dir)
112
- eco = fileEcosystem
113
- }
71
+ const eco = ecosystem ?? fileEcosystem
72
+ const scope = ecosystem !== undefined ? 'dependency' : tagged ? 'workspace' : classifyScope(fileEcosystem, dir)
114
73
  if (!version && scope !== 'workspace') continue
115
74
  const key = `${eco} ${name} ${version ?? ''}`
116
75
  let component = seen.get(key)
@@ -133,76 +92,53 @@ export function collectComponents(artifacts) {
133
92
  return [...seen.values()].toSorted(compareComponents)
134
93
  }
135
94
 
136
- // Pick the single component the SBOM is *about* (its primary/root): the lone workspace package when
137
- // there is exactly one; with zero or several workspace roots there's no single subject, so every
138
- // package is a plain component. `rest` is everything that is not the primary.
95
+ // The component the SBOM is *about*: the lone workspace package, none with zero or several.
139
96
  function selectPrimary(components) {
140
97
  const workspace = components.filter((c) => c.scope === 'workspace')
141
98
  const primary = workspace.length === 1 ? workspace[0] : null
142
99
  return { primary, rest: primary ? components.filter((c) => c !== primary) : components }
143
100
  }
144
101
 
145
- // SBOM timestamps are RFC 3339 to whole seconds; drop the milliseconds Date.toISOString includes.
102
+ // RFC 3339 to whole seconds.
146
103
  const isoSeconds = (date) => date.toISOString().replace(/\.\d{3}Z$/u, 'Z')
147
104
 
148
- // A CycloneDX bom-ref must uniquely identify a component; use the purl when present, else
149
- // ecosystem+name(+version) (unique by construction -- it's the dedup key).
105
+ // Unique by construction: the purl, else the dedup key.
150
106
  const bomRef = (c) => c.purl ?? `${c.ecosystem}:${c.name}${c.version == null ? '' : `@${c.version}`}`
151
107
 
152
- // Render an SPDX 2.3 document. It DESCRIBES its primary component, which DEPENDS_ON each installed
153
- // dependency (flat graph). With no single primary, the document DESCRIBES the workspace packages,
154
- // or every package when there are none. A package vendored into a dependency is a package of its own,
155
- // after the rest, which that dependency CONTAINS, its directory there its `packageFileName`.
108
+ // An SPDX 2.3 document: it DESCRIBES its primary, which DEPENDS_ON each dependency (flat), else the workspace
109
+ // packages, else every package. A package vendored into a dependency comes after the rest, CONTAINED by that
110
+ // dependency, its directory there its `packageFileName`.
156
111
  export function toSpdx(components, { tool = DEFAULT_TOOL, now = new Date(), uuid = newUuid() } = {}) {
157
112
  const { primary, rest } = selectPrimary(components)
158
113
  const ordered = primary ? [primary, ...rest] : components
159
114
  const ids = new Map(ordered.map((c, i) => [c, `SPDXRef-Package-${i}`]))
160
115
 
161
- const packages = ordered.map((c) => ({
162
- SPDXID: ids.get(c),
116
+ const spdxPackage = (id, c, purpose, extra = {}) => ({
117
+ SPDXID: id,
163
118
  name: c.name,
164
- // SPDX versionInfo is optional; omit it for a versionless workspace package.
165
119
  ...(c.version == null ? {} : { versionInfo: c.version }),
120
+ ...extra,
166
121
  downloadLocation: 'NOASSERTION',
167
122
  filesAnalyzed: false,
168
123
  ...(c.purl && {
169
124
  externalRefs: [{ referenceCategory: 'PACKAGE-MANAGER', referenceType: 'purl', referenceLocator: c.purl }],
170
125
  }),
171
- primaryPackagePurpose: c.scope === 'workspace' ? 'APPLICATION' : 'LIBRARY',
172
- }))
126
+ primaryPackagePurpose: purpose,
127
+ })
128
+ const packages = ordered.map((c) => spdxPackage(ids.get(c), c, c.scope === 'workspace' ? 'APPLICATION' : 'LIBRARY'))
173
129
  const vendored = ordered.flatMap((c) => (c.vendored ?? []).map((v, i) => ({ host: c, v, id: `${ids.get(c)}-Vendored-${i}` })))
174
- for (const { v, id } of vendored) {
175
- packages.push({
176
- SPDXID: id,
177
- name: v.name,
178
- ...(v.version == null ? {} : { versionInfo: v.version }),
179
- packageFileName: v.dir,
180
- downloadLocation: 'NOASSERTION',
181
- filesAnalyzed: false,
182
- ...(v.purl && {
183
- externalRefs: [{ referenceCategory: 'PACKAGE-MANAGER', referenceType: 'purl', referenceLocator: v.purl }],
184
- }),
185
- primaryPackagePurpose: 'LIBRARY',
186
- })
187
- }
130
+ for (const { v, id } of vendored) packages.push(spdxPackage(id, v, 'LIBRARY', { packageFileName: v.dir }))
188
131
 
189
132
  const relationships = []
133
+ const relate = (spdxElementId, relatedSpdxElement, relationshipType) => relationships.push({ spdxElementId, relatedSpdxElement, relationshipType })
190
134
  if (primary) {
191
- relationships.push({ spdxElementId: 'SPDXRef-DOCUMENT', relatedSpdxElement: ids.get(primary), relationshipType: 'DESCRIBES' })
192
- for (const c of rest) {
193
- if (c.scope === 'dependency') {
194
- relationships.push({ spdxElementId: ids.get(primary), relatedSpdxElement: ids.get(c), relationshipType: 'DEPENDS_ON' })
195
- }
196
- }
135
+ relate('SPDXRef-DOCUMENT', ids.get(primary), 'DESCRIBES')
136
+ for (const c of rest) if (c.scope === 'dependency') relate(ids.get(primary), ids.get(c), 'DEPENDS_ON')
197
137
  } else {
198
138
  const workspace = ordered.filter((c) => c.scope === 'workspace')
199
- for (const c of workspace.length > 0 ? workspace : ordered) {
200
- relationships.push({ spdxElementId: 'SPDXRef-DOCUMENT', relatedSpdxElement: ids.get(c), relationshipType: 'DESCRIBES' })
201
- }
202
- }
203
- for (const { host, id } of vendored) {
204
- relationships.push({ spdxElementId: ids.get(host), relatedSpdxElement: id, relationshipType: 'CONTAINS' })
139
+ for (const c of workspace.length > 0 ? workspace : ordered) relate('SPDXRef-DOCUMENT', ids.get(c), 'DESCRIBES')
205
140
  }
141
+ for (const { host, id } of vendored) relate(ids.get(host), id, 'CONTAINS')
206
142
 
207
143
  const docName = primary
208
144
  ? (primary.version == null ? primary.name : `${primary.name}@${primary.version}`)
@@ -222,39 +158,28 @@ export function toSpdx(components, { tool = DEFAULT_TOOL, now = new Date(), uuid
222
158
  }
223
159
  }
224
160
 
225
- // Render a CycloneDX 1.5 document. The primary (workspace root) becomes metadata.component; the
226
- // rest go in `components`, with a flat `dependencies` edge from primary to each installed dependency.
227
- // A package vendored into a dependency is nested in that dependency's `components`, its directory
228
- // there the location of its one occurrence.
161
+ // A CycloneDX 1.5 document: the primary (workspace root) its metadata.component, the rest `components`, with a flat
162
+ // `dependencies` edge from it to each dependency. A package vendored into a dependency is nested in its `components`,
163
+ // its directory there the location of its one occurrence.
229
164
  export function toCyclonedx(components, { tool = DEFAULT_TOOL, now = new Date(), uuid = newUuid() } = {}) {
230
165
  const { primary, rest } = selectPrimary(components)
231
- const toComponent = (c) => {
166
+ const component = (c, type, ref, tail) => {
232
167
  const { group, name } = splitName(c.ecosystem, c.name)
233
168
  return {
234
- type: c.scope === 'workspace' ? 'application' : 'library',
235
- 'bom-ref': bomRef(c),
169
+ type,
170
+ 'bom-ref': ref,
236
171
  ...(group && { group }),
237
172
  name,
238
- // CycloneDX 1.5 `version` is optional; omit it for a versionless workspace package.
239
173
  ...(c.version == null ? {} : { version: c.version }),
240
174
  ...(c.purl && { purl: c.purl }),
241
- ...(c.vendored && { components: c.vendored.map((v) => toVendoredComponent(c, v)) }),
175
+ ...tail,
242
176
  }
243
177
  }
178
+ const toComponent = (c) => component(c, c.scope === 'workspace' ? 'application' : 'library', bomRef(c),
179
+ c.vendored && { components: c.vendored.map((v) => toVendoredComponent(c, v)) })
244
180
  // Its bom-ref the host's with the directory as a purl subpath: two copies of one package (Next.js
245
181
  // has loader-utils2 and loader-utils3, both `loader-utils`) stay apart.
246
- const toVendoredComponent = (host, v) => {
247
- const { group, name } = splitName(v.ecosystem, v.name)
248
- return {
249
- type: 'library',
250
- 'bom-ref': `${bomRef(host)}#${v.dir}`,
251
- ...(group && { group }),
252
- name,
253
- ...(v.version == null ? {} : { version: v.version }),
254
- ...(v.purl && { purl: v.purl }),
255
- evidence: { occurrences: [{ location: v.dir }] },
256
- }
257
- }
182
+ const toVendoredComponent = (host, v) => component(v, 'library', `${bomRef(host)}#${v.dir}`, { evidence: { occurrences: [{ location: v.dir }] } })
258
183
 
259
184
  const doc = {
260
185
  bomFormat: 'CycloneDX',
@@ -263,7 +188,7 @@ export function toCyclonedx(components, { tool = DEFAULT_TOOL, now = new Date(),
263
188
  version: 1,
264
189
  metadata: {
265
190
  timestamp: isoSeconds(now),
266
- // CycloneDX 1.5 `tools.components` shape: the flat tools array is deprecated in 1.5 (removed in 1.6).
191
+ // Not the flat tools array, deprecated in 1.5 and removed in 1.6.
267
192
  tools: { components: [{ type: 'application', publisher: tool.vendor, name: tool.name, version: tool.version }] },
268
193
  ...(primary && { component: toComponent(primary) }),
269
194
  },
@@ -276,14 +201,13 @@ export function toCyclonedx(components, { tool = DEFAULT_TOOL, now = new Date(),
276
201
  return doc
277
202
  }
278
203
 
279
- // Dispatch to the requested format; options: { tool, now, uuid } for custom identity / deterministic output.
204
+ // `options` { tool, now, uuid } for a custom identity or deterministic output.
280
205
  export function generateSbom(format, components, options) {
281
206
  if (format === 'spdx') return toSpdx(components, options)
282
207
  if (format === 'cyclonedx') return toCyclonedx(components, options)
283
208
  throw new Error(`sbom: format must be spdx or cyclonedx, got ${format}`)
284
209
  }
285
210
 
286
- // Convenience one-shot: collectComponents then generateSbom.
287
211
  export function sbom(format, artifacts, options) {
288
212
  return generateSbom(format, collectComponents(artifacts), options)
289
213
  }