@exodus/stasis 1.0.0-beta.1 → 1.0.0-beta.11

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 (48) hide show
  1. package/README.md +32 -8
  2. package/bin/stasis.js +386 -213
  3. package/package.json +52 -12
  4. package/src/audit-corrections.js +96 -0
  5. package/src/audit.js +319 -64
  6. package/src/babel-worker.cjs +65 -0
  7. package/src/babel.js +131 -0
  8. package/src/bundle-from-lockfile.js +14 -22
  9. package/src/bundle-reader.js +77 -0
  10. package/src/cmd/build.js +96 -128
  11. package/src/cmd/bundle.js +1121 -654
  12. package/src/cmd/diff.js +2 -19
  13. package/src/cmd/github-bundle.js +39 -0
  14. package/src/cmd/output.js +36 -0
  15. package/src/cmd/sbom.js +7 -19
  16. package/src/diff.js +80 -129
  17. package/src/installed-commits.js +182 -0
  18. package/src/json-stream.js +283 -0
  19. package/src/loader-mock.js +8 -15
  20. package/src/loaders/bash.js +49 -116
  21. package/src/loaders/cargo.js +1716 -0
  22. package/src/loaders/foundry.js +923 -0
  23. package/src/loaders/paths.js +33 -0
  24. package/src/loaders/php.js +375 -402
  25. package/src/loaders/rust.js +3254 -242
  26. package/src/loaders/solidity-ownership.js +531 -0
  27. package/src/loaders/solidity.js +412 -162
  28. package/src/loaders/toml.js +68 -0
  29. package/src/metro-resolver.js +150 -0
  30. package/src/metro-transformer.js +3 -2
  31. package/src/mock.js +57 -165
  32. package/src/parse.js +4 -16
  33. package/src/resolve-fields.js +370 -0
  34. package/src/resolve-node.js +419 -0
  35. package/src/resolve-typescript.js +541 -0
  36. package/src/rollup.js +5 -0
  37. package/src/sbom.js +120 -97
  38. package/src/scan.js +348 -180
  39. package/src/vfs-bundle/entries.js +144 -0
  40. package/src/vfs-bundle/github.js +204 -0
  41. package/src/vfs-bundle/prisma.js +439 -0
  42. package/src/vfs-bundle/soldeer.js +125 -0
  43. package/src/vfs-bundle/tree.js +647 -0
  44. package/src/vfs-bundle.js +80 -0
  45. package/src/why.js +436 -0
  46. package/src/apis/npm/index.js +0 -36
  47. package/src/apis/npm/semver.cjs +0 -44
  48. package/src/cmd/extract.js +0 -193
package/src/audit.js CHANGED
@@ -1,112 +1,367 @@
1
- import { advisories } from './apis/npm/index.js'
2
- import semver from './apis/npm/semver.cjs'
3
- import { parseFile } from './parse.js'
1
+ import { packageRepo } from '@exodus/stasis-core/bundle-util'
2
+ import { dependencyEcosystem, moduleFileKey, packageRecords, sameGithub } from '@exodus/stasis-core/util'
3
+ import { advisories } from '@preventive/upstream/advisories.js'
4
+ import { compareVersions, valid } from '@preventive/upstream/semver.js'
5
+ import { isEvidenceFile } from './audit-corrections.js'
6
+ import { parseFile, parseFileWithKind } from './parse.js'
7
+ import { collectWhy, invertReason } from './why.js'
4
8
 
5
- // Only audit installed dependencies; workspace/first-party packages live under
6
- // non-`node_modules` keys in .modules (Lockfile/Bundle merge sources in) and
7
- // shouldn't be sent to the public registry — they leak names and produce noise.
9
+ // Where advisories() asks for each ecosystem's advisories: npm's registry; OSV for crates (those
10
+ // vendored from a registry, which OSV takes for crates.io's) and Composer packages; for Soldeer
11
+ // packages and GitHub repos (Foundry's lib/ submodules), the advisories their GitHub repository
12
+ // publishes, a Soldeer package's as Soldeer names it.
13
+ const SOURCES = { npm: ['npm'], cargo: ['OSV'], composer: ['OSV'], soldeer: ['Soldeer', 'GitHub'], github: ['GitHub'] }
14
+
15
+ // Why a vendored crate that may not be crates.io's is asked nowhere: by its name, crates.io's
16
+ // advisories would be another crate's, and a private git dependency's name must not reach a public
17
+ // database at all.
18
+ const UNREGISTERED_CRATES = {
19
+ 'cargo-git': 'a crate vendored from a git repository, not crates.io',
20
+ 'cargo-unknown': 'a vendored crate with no .cargo-checksum.json, which may be a git checkout, not crates.io\'s',
21
+ }
22
+
23
+ // A dependency's version as it is audited: a GitHub repo's `.gitmodules` branch `.`, git's for the
24
+ // superproject's own branch, which the bundle does not know and advisories() takes for no branch, is
25
+ // the 0.0.0 stasis versions a repo with no version of its own by. Every advisory range covers both,
26
+ // as it does a branch name.
27
+ const versionOf = (ecosystem, version) => (ecosystem === 'github' && version === '.' ? '0.0.0' : version)
28
+
29
+ // A package's key across the audit: `name@version` for npm, as collectWhy keys its chains, and
30
+ // prefixed by the ecosystem for the others, whose names may be npm's too.
31
+ const keyOf = (ecosystem, name, version) => `${ecosystem === 'npm' ? '' : `${ecosystem}:`}${name}@${version}`
32
+
33
+ // Composer's dev versions (`dev-main`, `1.x-dev`), as Composer tells them, a `#ref` dropped: no
34
+ // advisory database lists those, and advisories() takes releases alone.
35
+ const isComposerDev = (version) => /^dev-|-dev$/iu.test(version.replace(/#.*$/su, ''))
36
+
37
+ // Why `pkg` cannot be audited, or undefined where it can.
38
+ function unaudited({ ecosystem, version, vendoredIn }) {
39
+ if (version === undefined) return `a copy vendored in ${vendoredIn.join(', ')} whose package.json gives no version, so no advisory can be matched to it`
40
+ if (Object.hasOwn(UNREGISTERED_CRATES, ecosystem)) return UNREGISTERED_CRATES[ecosystem]
41
+ if (!Object.hasOwn(SOURCES, ecosystem)) return `no advisories are looked up for ${ecosystem}`
42
+ if (ecosystem === 'composer' && isComposerDev(version)) return 'a Composer dev version, which no advisory database lists'
43
+ return undefined
44
+ }
45
+
46
+ // Only audit installed dependencies, `{ ecosystem, name, version }`: an npm package, a vendored
47
+ // crate, a Composer package, a Soldeer package or a GitHub repo, as the bundle tags it (a crate
48
+ // vendored from git, or from where no `.cargo-checksum.json` tells, is listed but never asked about).
49
+ //
50
+ // A package counts as present only when its REAL code is, and recorded = present:
51
+ // an artifact records exactly the files it ships or attested -- imported, entry,
52
+ // or `add`-ed (the last two have no in-edge in the resolution graph, so presence
53
+ // must NOT be derived from edges). The package is audited only if some recorded
54
+ // file is code evidence (see audit-corrections.js) -- not a corrected file (ws's
55
+ // noop browser.js stub), not a manifest (package.json, recorded for resolver /
56
+ // metadata reads; Cargo.toml). So a ws shipped only as browser.js (+ manifest) is
57
+ // skipped, while one whose real code was bundled stays. Which consumers and import
58
+ // EDGES reach that code is the reason column's concern -- collectReasons and
59
+ // why.js apply the same evidence rule per file/edge there.
60
+ //
61
+ // A package's `github` is the repository its artifact records for it (its package.json's), where
62
+ // one does; else, for an npm package a bundle carries the package.json of (one built before builds
63
+ // recorded `repo`), the one that names, read by the rule builds record it by. A GitHub repo is its
64
+ // own, and has none.
65
+ //
66
+ // A copy of a package a dependency ships (its record's `vendored`, as packageRecords splits it out) is
67
+ // audited too, as the package it is, by the same evidence rule over the files it holds, with
68
+ // `vendoredIn` its host's `name@version`; its host, all of whose files the copy's are, as before. A copy
69
+ // whose package.json gives no version is listed with none, as not audited.
8
70
  export function collectPackagesFromFile(file) {
9
71
  const out = []
10
- for (const [dir, { name, version }] of parseFile(file).modules) {
11
- if (!dir.includes('node_modules')) continue
12
- if (name && version) out.push({ name, version })
72
+ const { kind, artifact } = parseFileWithKind(file)
73
+ for (const [dir, { name, version, ecosystem: tagged, repo, files, vendoredIn }] of packageRecords(artifact.modules)) {
74
+ const ecosystem = dependencyEcosystem(dir, tagged)
75
+ // First-party code is never sent to a public registry (leaks names, adds noise).
76
+ if (ecosystem === undefined) continue
77
+ if (!name || (!version && vendoredIn === undefined)) continue
78
+ if (!Object.keys(files).some((rel) => isEvidenceFile(name, version ?? null, rel, ecosystem))) continue
79
+ if (vendoredIn !== undefined) {
80
+ const host = artifact.modules.get(vendoredIn)
81
+ out.push({ ecosystem, name, version: version === undefined ? undefined : versionOf(ecosystem, version), vendoredIn: `${host.name}@${host.version}` })
82
+ continue
83
+ }
84
+ let github = ecosystem === 'github' ? undefined : repo?.github
85
+ if (github === undefined && ecosystem === 'npm' && kind === 'bundle') github = manifestRepo(files['package.json'])
86
+ out.push({ ecosystem, name, version: versionOf(ecosystem, version), ...(github === undefined ? {} : { github }) })
13
87
  }
14
88
  return out
15
89
  }
16
90
 
91
+ // The GitHub repository a bundled package.json's text names (packageRepo), or undefined where it
92
+ // names none or doesn't parse. Read as Node reads one, past a byte order mark (packageJSONText),
93
+ // which a bundle keeps in the text. A lockfile holds the file's digest alone, never asked here.
94
+ function manifestRepo(text) {
95
+ if (typeof text !== 'string') return undefined
96
+ try {
97
+ return packageRepo(JSON.parse(text.replace(/^\uFEFF/u, '')))?.github
98
+ } catch {
99
+ return undefined
100
+ }
101
+ }
102
+
103
+ // Versions in semver's order where both are semver's, else as numbers in text compare.
104
+ const byNumbers = new Intl.Collator('en', { numeric: true }).compare
105
+ const byVersion = (a, b) => (valid(a) && valid(b) ? compareVersions(a, b) : byNumbers(a, b))
106
+
107
+ // Each package once, with the GitHub repository its artifacts record for its name, which
108
+ // advisories() asks instead of looking one up. It takes one repository a name, so a name recorded
109
+ // with two, by two of its versions (a package that moved) or by two artifacts, has none, and is
110
+ // looked up as before. One some dependency ships a copy of carries `vendoredIn`, the hosts' sorted
111
+ // `name@version`s, and `vendoredOnly`, whether that copy is all there is of it; a copy without a version
112
+ // is one package per host.
17
113
  export function collectPackages(files) {
18
- const seen = new Set()
19
- const out = []
114
+ const byKey = new Map()
115
+ const vendored = new Map() // key -> { hosts, only }
116
+ const repos = new Map() // `ecosystem:name` -> the one repository recorded for it, or null
117
+ for (const file of files) {
118
+ for (const { github, vendoredIn, ...pkg } of collectPackagesFromFile(file)) {
119
+ const key = pkg.version === undefined ? `${pkg.ecosystem}:${pkg.name} in ${vendoredIn}` : keyOf(pkg.ecosystem, pkg.name, pkg.version)
120
+ if (!byKey.has(key)) byKey.set(key, pkg)
121
+ if (!vendored.has(key)) vendored.set(key, { hosts: new Set(), only: true })
122
+ if (vendoredIn === undefined) vendored.get(key).only = false
123
+ else vendored.get(key).hosts.add(vendoredIn)
124
+ if (github === undefined) continue
125
+ const name = `${pkg.ecosystem}:${pkg.name}`
126
+ const known = repos.get(name)
127
+ // The first spelling stands.
128
+ if (known === undefined) repos.set(name, github)
129
+ else if (known !== null && !sameGithub(known, github)) repos.set(name, null)
130
+ }
131
+ }
132
+ return [...byKey]
133
+ .map(([key, pkg]) => {
134
+ const github = repos.get(`${pkg.ecosystem}:${pkg.name}`)
135
+ const { hosts, only } = vendored.get(key)
136
+ return { ...pkg, ...(github ? { github } : {}), ...(hosts.size === 0 ? {} : { vendoredIn: [...hosts].toSorted(), vendoredOnly: only }) }
137
+ })
138
+ .toSorted((a, b) => a.ecosystem.localeCompare(b.ecosystem) || a.name.localeCompare(b.name) || byVersion(a.version, b.version))
139
+ }
140
+
141
+ // Map each audited package (keyOf it) to the bundle consumers ("reasons") that recorded its files.
142
+ // Only bundles carry a reason map ({ consumer: [file, ...] }); lockfiles omit it. Each file is
143
+ // resolved back to its owning package via the module file listing. Only evidence files attribute
144
+ // (see audit-corrections.js): a consumer that recorded nothing of a package but a corrected file or
145
+ // its manifest carries none of its real code and is NOT a reason -- e.g. webpack shipping only ws's
146
+ // noop browser.js stub, while `run` bundles the real ws.
147
+ // A file of a copy a dependency ships (packageRecords) attributes to both: the copy and its host.
148
+ export function collectReasons(files) {
149
+ const byPkg = new Map()
20
150
  for (const file of files) {
21
- for (const { name, version } of collectPackagesFromFile(file)) {
22
- const key = `${name}@${version}`
23
- if (seen.has(key)) continue
24
- seen.add(key)
25
- out.push({ name, version })
151
+ const artifact = parseFile(file)
152
+ const fileReasons = invertReason(artifact.reason)
153
+ if (!fileReasons) continue
154
+ const fileToPkgs = new Map()
155
+ for (const [dir, { name, version, ecosystem: tagged, files: modFiles }] of packageRecords(artifact.modules)) {
156
+ const ecosystem = dependencyEcosystem(dir, tagged)
157
+ if (ecosystem === undefined || !name || !version) continue
158
+ for (const rel of Object.keys(modFiles)) {
159
+ if (!isEvidenceFile(name, version, rel, ecosystem)) continue
160
+ const f = moduleFileKey(dir, rel)
161
+ if (!fileToPkgs.has(f)) fileToPkgs.set(f, [])
162
+ fileToPkgs.get(f).push(keyOf(ecosystem, name, versionOf(ecosystem, version)))
163
+ }
164
+ }
165
+ for (const [f, consumers] of fileReasons) {
166
+ for (const key of fileToPkgs.get(f) ?? []) {
167
+ let set = byPkg.get(key)
168
+ if (set === undefined) byPkg.set(key, (set = new Set()))
169
+ for (const consumer of consumers) set.add(consumer)
170
+ }
26
171
  }
27
172
  }
28
- return out.toSorted((a, b) => a.name.localeCompare(b.name) || semver.compare(a.version, b.version))
173
+ return byPkg
29
174
  }
30
175
 
31
176
  const SEVERITY_ORDER = { critical: 0, high: 1, moderate: 2, low: 3, info: 4, none: 5 }
32
177
 
33
- export function flattenAdvisories(result, packages = []) {
34
- const installed = new Map()
35
- for (const { name, version } of packages) {
36
- if (!installed.has(name)) installed.set(name, [])
37
- installed.get(name).push(version)
178
+ // Consumer display order: bundler plugins (metro, webpack, esbuild, ...) first,
179
+ // then `run`, then `add`. Ties broken alphabetically.
180
+ const CONSUMER_RANK = { run: 1, add: 2 }
181
+ const consumerRank = (c) => CONSUMER_RANK[c] ?? 0
182
+ const byConsumerOrder = (a, b) => consumerRank(a) - consumerRank(b) || a.localeCompare(b)
183
+
184
+ // Build a row's `reason` cell by unioning over exactly the affected versions it
185
+ // covers. Without `--why` that's the `, `-joined set of bundle consumers; with
186
+ // `--why` it's the newline-joined `consumer: a -> b -> c` import-path lines
187
+ // (which REPLACE the consumer list). Consumers are ordered plugins -> run -> add.
188
+ // `reasonFilter`, when set, narrows the cell to a single consumer: the `--why`
189
+ // chains are already filtered upstream (see collectWhy), so only the consumer
190
+ // list needs pruning here. collectWhy follows npm's import graph alone, so an
191
+ // advisory of another ecosystem keeps its consumer list under `--why` too.
192
+ function reasonCell({ ecosystem, name, versions: affected }, reasonsByPkg, whyByPkg, reasonFilter) {
193
+ const chains = whyByPkg !== null && ecosystem === 'npm'
194
+ const parts = new Set()
195
+ const source = chains ? whyByPkg : reasonsByPkg
196
+ for (const v of affected) {
197
+ for (const p of source.get(keyOf(ecosystem, name, v)) ?? []) parts.add(p)
198
+ }
199
+ // --why: group `consumer: path` lines by consumer and order the groups
200
+ // plugins -> run -> add (this also re-unites a consumer's lines when they were
201
+ // split across affected versions); within a group collectWhy's compressed order
202
+ // is preserved. Otherwise: the `, `-joined consumer set in the same order.
203
+ if (chains) {
204
+ const byConsumer = Map.groupBy(parts, (line) => {
205
+ const i = line.indexOf(': ')
206
+ return i === -1 ? '' : line.slice(0, i)
207
+ })
208
+ return [...byConsumer.keys()].toSorted(byConsumerOrder).flatMap((c) => byConsumer.get(c)).join('\n')
38
209
  }
210
+ const consumers = reasonFilter ? [...parts].filter((c) => c === reasonFilter) : [...parts]
211
+ return consumers.toSorted(byConsumerOrder).join(', ')
212
+ }
213
+
214
+ // An installed version as the table shows it: with the dependencies that ship a copy of it, where one does
215
+ // (`vendoredByKey`, collectPackages' packages by keyOf).
216
+ function installedCell({ ecosystem, name }, version, vendoredByKey) {
217
+ const pkg = vendoredByKey.get(keyOf(ecosystem, name, version))
218
+ if (pkg?.vendoredIn === undefined) return version
219
+ return `${version} (${pkg.vendoredOnly ? '' : 'also '}vendored in ${pkg.vendoredIn.join(', ')})`
220
+ }
221
+
222
+ // `found` is what @preventive/upstream's advisories() answers: one row per advisory range, whose
223
+ // `versions` are the audited versions it covers (never empty -- a range covering none is dropped
224
+ // there, so the table and exit code reflect only real hits).
225
+ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = null, reasonFilter = null, vendoredByKey = new Map()) {
39
226
  const rows = []
40
- for (const [pkg, list] of Object.entries(result)) {
41
- if (!Array.isArray(list)) continue
42
- const installedVersions = installed.get(pkg) ?? []
43
- for (const adv of list) {
44
- const range = adv.vulnerable_versions ?? ''
45
- const affected = range
46
- ? installedVersions.filter((v) => semver.satisfies(v, range))
47
- : installedVersions
48
- // npm occasionally returns advisories whose vulnerable range matches none
49
- // of the versions we submitted; drop them so the table (and the CLI exit
50
- // code) reflect only real hits.
51
- if (installedVersions.length > 0 && affected.length === 0) continue
52
- rows.push({
53
- package: pkg,
54
- installed: affected.join(', '),
55
- vulnerable: range,
56
- severity: adv.severity ?? '',
57
- title: adv.title ?? '',
58
- url: adv.url ?? '',
59
- })
60
- }
227
+ for (const adv of found) {
228
+ const reason = reasonCell(adv, reasonsByPkg, whyByPkg, reasonFilter)
229
+ // --reason keeps only advisories tied to that consumer: once the cell is
230
+ // narrowed to it, an empty cell means this package isn't related to it.
231
+ if (reasonFilter && reason === '') continue
232
+ rows.push({
233
+ ecosystem: adv.ecosystem,
234
+ package: adv.name,
235
+ installed: adv.versions.map((v) => installedCell(adv, v, vendoredByKey)).join(', '),
236
+ vulnerable: adv.range ?? '',
237
+ severity: adv.severity ?? '',
238
+ title: adv.title ?? '',
239
+ id: adv.id,
240
+ reason,
241
+ })
61
242
  }
62
243
  rows.sort((a, b) => {
63
244
  const sa = SEVERITY_ORDER[a.severity] ?? 99
64
245
  const sb = SEVERITY_ORDER[b.severity] ?? 99
65
246
  if (sa !== sb) return sa - sb
66
247
  if (a.package !== b.package) return a.package < b.package ? -1 : 1
248
+ if (a.ecosystem !== b.ecosystem) return a.ecosystem < b.ecosystem ? -1 : 1
67
249
  return a.title < b.title ? -1 : 1
68
250
  })
69
251
  return rows
70
252
  }
71
253
 
72
- // Line breaks in npm advisory titles would otherwise break the box layout.
73
- // Covers LF, bare CR (and CRLF), and U+2028 / U+2029 line/paragraph separators.
254
+ // Strip line breaks (LF/CR/CRLF, U+2028/U+2029) from advisory titles so they can't break the box layout.
74
255
  const cell = (v) => String(v ?? '').replace(/[\r\n\u2028\u2029]+/gu, ' ')
75
256
 
76
- export function formatTable(rows, columns) {
257
+ // Split a value into the physical lines a cell occupies. Non-multiline columns
258
+ // collapse every line break to a space via `cell` (one line). A column named in
259
+ // `multiline` (the `--why` reason column) keeps intentional `\n` breaks -- one
260
+ // physical line per entry -- while still flattening stray CR/separators in each.
261
+ const cellLines = (v, multiline) =>
262
+ multiline ? String(v ?? '').split('\n').map((s) => cell(s)) : [cell(v)]
263
+
264
+ export function formatTable(rows, columns, { multiline = [] } = {}) {
77
265
  if (rows.length === 0) return ''
78
- const widths = columns.map((c) => Math.max(c.length, ...rows.map((r) => cell(r[c]).length)))
266
+ const ml = new Set(multiline)
267
+ const linesOf = (obj) => columns.map((c) => cellLines(obj[c], ml.has(c)))
268
+ const header = columns.map((c) => [c])
269
+ const body = rows.map((r) => linesOf(r))
270
+ const widths = columns.map((c, i) =>
271
+ Math.max(c.length, ...body.map((cells) => Math.max(0, ...cells[i].map((l) => l.length))))
272
+ )
79
273
  const pad = (s, w) => s.padEnd(w)
80
274
  const line = (l, m, r, fill) => l + widths.map((w) => fill.repeat(w + 2)).join(m) + r
81
- const row = (vals) => '│ ' + vals.map((v, i) => pad(cell(v), widths[i])).join(' │ ') + ' │'
275
+ // A row spans as many physical lines as its tallest cell; shorter cells pad
276
+ // out with blanks so the borders stay aligned.
277
+ const render = (cells) => {
278
+ const height = Math.max(...cells.map((c) => c.length))
279
+ const out = []
280
+ for (let i = 0; i < height; i++) {
281
+ out.push('│ ' + cells.map((c, j) => pad(c[i] ?? '', widths[j])).join(' │ ') + ' │')
282
+ }
283
+ return out.join('\n')
284
+ }
82
285
  return [
83
286
  line('┌', '┬', '┐', '─'),
84
- row(columns),
287
+ render(header),
85
288
  line('├', '┼', '┤', '─'),
86
- ...rows.map((r) => row(columns.map((c) => r[c]))),
289
+ ...body.map(render),
87
290
  line('└', '┴', '┘', '─'),
88
291
  ].join('\n')
89
292
  }
90
293
 
91
- export async function audit(files) {
294
+ // `repoAdvisories` and `github` (a @preventive/upstream/github.js client) go to advisories() as they
295
+ // are; a Soldeer package or a GitHub repo needs the client, their repository being their only source.
296
+ // A package that cannot be audited is `skipped`, with why (`because`), and asked for nothing.
297
+ export async function audit(files, { why = false, whyDeep = false, whyFull = false, reason = null, repoAdvisories = false, github } = {}) {
298
+ // --why-deep and --why-full imply --why. Deep keeps every chain instead of the
299
+ // default pruning of chains whose full suffix is already a chain (see
300
+ // collectWhy/dropSuffixed); full spells chains out with no `...` collapse.
301
+ why = why || whyDeep || whyFull
92
302
  const packages = collectPackages(files)
93
- if (packages.length === 0) {
94
- return { packages, advisories: {}, rows: [] }
303
+ const skipped = packages.flatMap((pkg) => {
304
+ const because = unaudited(pkg)
305
+ return because === undefined ? [] : [{ ...pkg, because }]
306
+ })
307
+ const asked = packages.filter((pkg) => unaudited(pkg) === undefined)
308
+ if (asked.length === 0) {
309
+ return { packages, skipped, advisories: [], rows: [], why }
310
+ }
311
+ let result
312
+ try {
313
+ result = await advisories(asked.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] })), { repoAdvisories, github })
314
+ } catch (cause) {
315
+ // Refused input or a malformed answer is an assertion that says so itself; a transport or
316
+ // HTTP failure gets the context of which request it was.
317
+ if (cause?.code === 'ERR_ASSERTION') throw cause
318
+ const sources = new Set([...asked.flatMap((pkg) => SOURCES[pkg.ecosystem]), ...(repoAdvisories ? ['GitHub'] : [])])
319
+ throw new Error(`${[...sources].join('/')} advisories request failed: ${cause.message}`, { cause })
320
+ }
321
+ // `--why` REPLACES the consumer list with per-consumer import paths, so only
322
+ // one of the two is computed for npm's advisories, the only ones collectWhy has
323
+ // paths for: another ecosystem's keep their consumers. Restrict the (potentially
324
+ // expensive) path search to the packages that actually carry an advisory.
325
+ // `reason` (--reason) narrows both the paths (in collectWhy) and the consumer
326
+ // list (in flattenAdvisories) to a single consumer, dropping advisories
327
+ // unrelated to it.
328
+ const vendoredByKey = new Map(asked.filter((pkg) => pkg.vendoredIn !== undefined).map((pkg) => [keyOf(pkg.ecosystem, pkg.name, pkg.version), pkg]))
329
+ let rows
330
+ if (why) {
331
+ const npm = result.filter((adv) => adv.ecosystem === 'npm')
332
+ const targetKeys = new Set(npm.flatMap((adv) => adv.versions.map((v) => keyOf(adv.ecosystem, adv.name, v))))
333
+ const reasons = npm.length < result.length ? collectReasons(files) : undefined
334
+ rows = flattenAdvisories(result, reasons, collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull }), reason, vendoredByKey)
335
+ } else {
336
+ rows = flattenAdvisories(result, collectReasons(files), null, reason, vendoredByKey)
95
337
  }
96
- const result = await advisories(packages)
97
- const rows = flattenAdvisories(result, packages)
98
- return { packages, advisories: result, rows }
338
+ return { packages, skipped, advisories: result, rows, why, whyDeep, whyFull, reason }
99
339
  }
100
340
 
101
- export function printAuditReport({ packages, rows }, { out = process.stdout, err = process.stderr } = {}) {
102
- err.write(`Scanned ${packages.length} package${packages.length === 1 ? '' : 's'}\n`)
341
+ // `10 alerts, 1 critical, 5 high, 3 moderate, 1 low`: the rows, then each severity present, in the
342
+ // rows' own order (most severe first); a row without one is `unrated`, so the parts add up.
343
+ function alertStats(rows) {
344
+ const bySeverity = Map.groupBy(rows, (r) => r.severity || 'unrated')
345
+ return [`${rows.length} alert${rows.length === 1 ? '' : 's'}`, ...[...bySeverity].map(([severity, list]) => `${list.length} ${severity}`)].join(', ')
346
+ }
347
+
348
+ export function printAuditReport({ packages, skipped = [], rows, why = false, reason = null }, { out = process.stdout, err = process.stderr } = {}) {
349
+ const scanned = `Scanned ${packages.length} package${packages.length === 1 ? '' : 's'}`
103
350
  if (packages.length === 0) {
104
- err.write('No node_modules entries found in the input files\n')
105
- return
106
- }
107
- if (rows.length === 0) {
108
- err.write('No advisories found\n')
351
+ err.write(`${scanned}\nNo dependencies found in the input files\n`)
109
352
  return
110
353
  }
111
- out.write(formatTable(rows, ['severity', 'package', 'installed', 'vulnerable', 'title', 'url']) + '\n')
354
+ err.write(`${scanned}: ${alertStats(rows)}\n`)
355
+ for (const pkg of skipped) err.write(`Not audited: ${pkg.ecosystem} ${pkg.name}${pkg.version === undefined ? '' : `@${pkg.version}`}, ${pkg.because}\n`)
356
+ if (rows.length === 0) return
357
+ // The ecosystem column only where one is not npm's, which the others were all once.
358
+ const columns = ['severity', ...(rows.some((r) => r.ecosystem !== 'npm') ? ['ecosystem'] : []), 'package', 'installed', 'vulnerable', 'title', 'id']
359
+ // Surface the reason column only when some advisory has provenance -- bundle
360
+ // consumers, or (with --why) import paths. Under --reason WITHOUT --why every
361
+ // cell is just the filter value repeated, so drop the column then; --why still
362
+ // earns it (the import chains differ per row).
363
+ if (rows.some((r) => r.reason) && !(reason && !why)) columns.splice(columns.indexOf('installed') + 1, 0, 'reason')
364
+ // Under --why the reason cell is a list of `consumer: path` lines; let it span
365
+ // multiple physical rows instead of collapsing to one.
366
+ out.write(formatTable(rows, columns, { multiline: why ? ['reason'] : [] }) + '\n')
112
367
  }
@@ -0,0 +1,65 @@
1
+ 'use strict'
2
+
3
+ const { Worker, MessageChannel, isMainThread, parentPort, workerData } = require('node:worker_threads')
4
+ const { once } = require('node:events')
5
+ const { availableParallelism } = require('node:os')
6
+
7
+ // Babel transforms are CPU-bound and `stasis build` fires many concurrently (esbuild loads
8
+ // files in parallel), so they run on a small worker-thread pool off the main thread. Workers
9
+ // are unref()ed: a pending transform keeps the process alive through its open MessagePort,
10
+ // but an idle pool never holds the process open after the build finishes.
11
+ //
12
+ // Babel is the project's dependency, not stasis's, so each pool is keyed by the resolved
13
+ // @babel/core entry path and the worker loads that exact copy (passed via workerData).
14
+ if (isMainThread) {
15
+ const maxWorkers = availableParallelism() >= 4 ? 2 : 1
16
+ const pools = new Map() // resolved @babel/core path -> [{ worker, busy }]
17
+
18
+ const getWorker = (babelPath) => {
19
+ if (!pools.has(babelPath)) pools.set(babelPath, [])
20
+ const workers = pools.get(babelPath)
21
+
22
+ const idle = workers.find((info) => info.busy === 0)
23
+ if (idle) return idle
24
+
25
+ if (workers.length < maxWorkers) {
26
+ const worker = new Worker(__filename, { workerData: { babelPath } })
27
+ worker.unref()
28
+ // unhandled top-level errors crash the process automatically, which is the desired
29
+ // behavior -- no need to listen to 'error'
30
+ workers.unshift({ worker, busy: 0 })
31
+ } else if (workers.length > 1) {
32
+ workers.sort((a, b) => a.busy - b.busy)
33
+ }
34
+
35
+ return workers[0]
36
+ }
37
+
38
+ const transformAsync = async (babelPath, code, options) => {
39
+ const info = getWorker(babelPath)
40
+ info.busy++
41
+ const channel = new MessageChannel()
42
+ info.worker.postMessage({ port: channel.port1, code, options }, [channel.port1])
43
+ const [{ result, error }] = await once(channel.port2, 'message')
44
+ info.busy--
45
+ if (error) throw error
46
+ return result
47
+ }
48
+
49
+ module.exports = { transformAsync }
50
+ } else {
51
+ const babel = require(workerData.babelPath)
52
+
53
+ parentPort.on('message', ({ port, code: input, options }) => {
54
+ try {
55
+ // transformSync: the worker is already off the main thread, async only adds overhead
56
+ const { code, map } = babel.transformSync(input, options)
57
+ // only plain data crosses the port -- a full Babel result carries non-cloneable state
58
+ port.postMessage({ result: { code, map } })
59
+ } catch (error) {
60
+ port.postMessage({ error })
61
+ }
62
+
63
+ port.close()
64
+ })
65
+ }
package/src/babel.js ADDED
@@ -0,0 +1,131 @@
1
+ // Per-file Babel pre-transforms for `stasis build`, serving two flags:
2
+ //
3
+ // --platform=hermes downlevels every code file to the dialect Hermes (React Native's
4
+ // engine) executes -- no classes, class fields, let/const, arrows, async generators or
5
+ // for-await. Two steps per file, then a constrained final bundle:
6
+ // 1. esbuild.transform lowers class fields / private members / static blocks / `using`
7
+ // into plain class syntax (stripping TS types and compiling JSX on the way), the
8
+ // shape Babel's class transform can consume.
9
+ // 2. A Babel worker pool applies transform-block-scoping + transform-classes -- the two
10
+ // lowerings esbuild refuses to do itself.
11
+ // 3. The final esbuild bundle runs with HERMES_UNSUPPORTED (arrow/async-generator/
12
+ // for-await lowered by esbuild; class and const-and-let declared unsupported both so
13
+ // esbuild's own helpers stay Hermes-safe and as a fail-closed check that none
14
+ // survived Babel).
15
+ //
16
+ // --babel instead runs each code file through the project's own babel.config.* verbatim --
17
+ // no esbuild pre-transform, Babel sees the original source exactly as the project's own
18
+ // toolchain (e.g. Metro) would hand it over.
19
+ //
20
+ // Babel is the PROJECT's dev dependency, not stasis's: @babel/core (and, for hermes, the
21
+ // two transform plugins) resolve from the project directory, so the project pins its own
22
+ // Babel toolchain and config semantics.
23
+ import { existsSync } from 'node:fs'
24
+ import { createRequire } from 'node:module'
25
+ import { join } from 'node:path'
26
+
27
+ const require = createRequire(import.meta.url)
28
+
29
+ // esbuild `supported` map for the FINAL hermes bundle (spread into esbuild.build options).
30
+ export const HERMES_UNSUPPORTED = {
31
+ arrow: false,
32
+ class: false, // already lowered by Babel; unsupporting it doubles as a safeguard check
33
+ 'async-generator': false,
34
+ 'const-and-let': false, // ditto, and keeps esbuild-emitted helpers off let/const
35
+ 'for-await': false,
36
+ }
37
+
38
+ // Class-syntax extensions the hermes pre-transform lowers into plain classes for Babel,
39
+ // which handles none of them without extra plugins (and Hermes supports none of them either).
40
+ const PRE_TRANSFORM_UNSUPPORTED = {
41
+ 'class-field': false,
42
+ 'class-private-accessor': false,
43
+ 'class-private-brand-check': false,
44
+ 'class-private-field': false,
45
+ 'class-private-method': false,
46
+ 'class-private-static-accessor': false,
47
+ 'class-private-static-field': false,
48
+ 'class-private-static-method': false,
49
+ 'class-static-blocks': false,
50
+ 'class-static-field': false,
51
+ 'import-attributes': false,
52
+ using: false,
53
+ }
54
+
55
+ const HERMES_BABEL_PLUGINS = ['@babel/plugin-transform-block-scoping', '@babel/plugin-transform-classes']
56
+
57
+ // Only real JS-family sources are transformed; anything else the plugin serves (json, or a
58
+ // --loader override like .svg:text) passes through untouched.
59
+ const TRANSFORMABLE_LOADERS = new Set(['js', 'ts', 'jsx', 'tsx'])
60
+
61
+ // Resolved up front on the main thread so a missing install fails with instructions, not a
62
+ // worker crash mid-build. Returns absolute paths: the worker loads @babel/core by path, and
63
+ // plugin paths bypass Babel's own name resolution (which would look in the wrong dir).
64
+ function resolveFromProject(projectDir, deps, flag) {
65
+ const projectRequire = createRequire(join(projectDir, 'noop.js'))
66
+ try {
67
+ return deps.map((dep) => projectRequire.resolve(dep))
68
+ } catch (cause) {
69
+ throw new Error(
70
+ `stasis build: ${flag} needs ${deps.join(', ')} installed in the project ` +
71
+ `(e.g. \`npm i -D ${deps.join(' ')}\`); resolution failed from ${projectDir}`,
72
+ { cause }
73
+ )
74
+ }
75
+ }
76
+
77
+ // The hermes per-file pre-transform, in StasisEsbuild's load-mode `transform` hook shape:
78
+ // (source, { path, loader }) => { contents, loader } | undefined.
79
+ export function createHermesTransform({ projectDir, esbuild }) {
80
+ const [babelPath, ...plugins] = resolveFromProject(
81
+ projectDir, ['@babel/core', ...HERMES_BABEL_PLUGINS], '--platform=hermes'
82
+ )
83
+ const { transformAsync } = require('./babel-worker.cjs')
84
+ return async (source, { path, loader }) => {
85
+ if (!TRANSFORMABLE_LOADERS.has(loader)) return undefined
86
+ const { code, warnings } = await esbuild.transform(source, {
87
+ sourcemap: 'inline',
88
+ sourcefile: path,
89
+ loader,
90
+ supported: PRE_TRANSFORM_UNSUPPORTED,
91
+ })
92
+ if (warnings.length > 0) {
93
+ const formatted = await esbuild.formatMessages(warnings, { kind: 'warning', color: false })
94
+ for (const line of formatted) process.stderr.write(line)
95
+ }
96
+ const result = await transformAsync(babelPath, code, {
97
+ compact: false,
98
+ babelrc: false,
99
+ configFile: false,
100
+ plugins,
101
+ })
102
+ // TS is stripped and JSX compiled by the pre-transform, so the result is always plain js.
103
+ return { contents: result.code, loader: 'js' }
104
+ }
105
+ }
106
+
107
+ // The --babel per-file transform (same hook shape): the project's own babel.config.* over
108
+ // the original source. Its output must be esbuild-parseable js -- a config that leaves TS/JSX
109
+ // in place fails the build in Babel or esbuild, which is the project's config to fix.
110
+ export function createBabelConfigTransform({ projectDir }) {
111
+ const configFile = ['babel.config.js', 'babel.config.cjs', 'babel.config.mjs', 'babel.config.json']
112
+ .map((name) => join(projectDir, name))
113
+ .find((file) => existsSync(file))
114
+ if (!configFile) {
115
+ throw new Error(`stasis build: --babel found no babel.config.(js|cjs|mjs|json) in ${projectDir}`)
116
+ }
117
+ const [babelPath] = resolveFromProject(projectDir, ['@babel/core'], '--babel')
118
+ const { transformAsync } = require('./babel-worker.cjs')
119
+ return async (source, { path, loader }) => {
120
+ if (!TRANSFORMABLE_LOADERS.has(loader)) return undefined
121
+ // filename makes per-file config (overrides, .babelrc lookup) behave as in a real run.
122
+ const result = await transformAsync(babelPath, source, {
123
+ cwd: projectDir,
124
+ root: projectDir,
125
+ configFile,
126
+ filename: path,
127
+ compact: false,
128
+ })
129
+ return { contents: result.code, loader: 'js' }
130
+ }
131
+ }