@exodus/stasis 1.0.0-beta.10 → 1.0.0-beta.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/stasis.js CHANGED
@@ -7,7 +7,7 @@ import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
7
7
  import { existsSync, realpathSync } from 'node:fs'
8
8
  import { homedir, constants as osConstants } from 'node:os'
9
9
  import assert from 'node:assert/strict'
10
- import { parseBrotliQuality, parseLeadingOptions, parseResourcesOption } from '@exodus/stasis-core/util'
10
+ import { parseBrotliQuality, parseLeadingOptions } from '@exodus/stasis-core/util'
11
11
  import pkg from '../package.json' with { type: 'json' }
12
12
 
13
13
  const argv = [...process.argv]
@@ -20,10 +20,11 @@ assert(basename(jsname) === 'stasis' || pathsEqual(jsname, fileURLToPath(import.
20
20
 
21
21
  const HELP = `Usage:
22
22
  stasis run --lock=(add|replace|frozen|ignore) [--bundle=(add|replace|load|frozen|ignore)] [--bundle-file=path/to/bundle.br] [--resources-bundle-file=path/to/resources.br] [--dependencies] [--child-process] [--package-json] [--mock] [--import=module ...] [--fs=(sync|async)] [--resources=ext,ext] [--brotli-quality=0..11] path/to/file.js ...
23
- stasis bundle [--scope=(node_modules|full)] [--conditions=cond1,cond2] [--mainFields=field1,field2] [--metro [--metro-resolver] --platforms=ios,android] [--jsx] [--flow] [--typescript [--tsconfig=path/to/tsconfig.json]] [--resources=ext,ext] [--package-json] [--lockfile=path/to/stasis.lock.json] [--brotli-quality=0..11] [--add] [--output=(path|-)] path/to/file.(js|ts|jsx|tsx) ...
23
+ stasis bundle [--scope=(node_modules|full)] [--conditions=cond1,cond2] [--mainFields=field1,field2] [--metro [--metro-resolver] [--babel|--babel-aliases=path/to/aliases.json] --platforms=ios,android] [--jsx] [--flow] [--typescript [--tsconfig=path/to/tsconfig.json]] [--resources=ext,ext] [--define=K=V ...] [--package-json] [--lockfile=path/to/stasis.lock.json] [--brotli-quality=0..11] [--add] [--output=(path|-)] path/to/file.(js|ts|jsx|tsx) ...
24
24
  stasis bundle [--mapping=path/to/remappings(.txt|.toml)] [--manifests] [--add] [--output=(path|-)] path/to/(file.sol|dir) ...
25
25
  stasis bundle [--cargo] [--cargo-features=a,b,pkg/c] [--cargo-no-default-features] [--cargo-all-features] [--cargo-target=(triple|host)] [--cargo-manifests] [--add] [--output=(path|-)] path/to/file.rs ...
26
26
  stasis bundle [--add] [--output=(path|-)] path/to/file.(php|sh|bash) ...
27
+ stasis bundle [--babel] [--metro-resolver] [--flow] [--cargo] [--cargo-target=(triple|host)] [--lockfile=path/to/stasis.lock.json] [--brotli-quality=0..11] [--add] [--output=(path|-)] [entry ...] (the entries' options from stasis.config.json's entries)
27
28
  stasis github-bundle --github=owner/name [--sha=commit|--tag=name] [--directory=path] [--package-manager=(pnpm|yarn1|npm|soldeer) [--package-manager-version=version]] [--generate=prisma] [--lockfile=path/to/stasis.lock.json] [--output=(path|-)] [stasis bundle's options for the entries] [path/in/repo/to/(file.(js|ts|jsx|tsx)|file.sol|dir) ...]
28
29
  stasis add path/to/(file|dir) ...
29
30
  stasis build --output=(dir|file.js) [--format=(esm|cjs|iife)] [--platform=(node|browser|neutral|hermes)] [--babel] [--minify] [--sourcemap] [--define=K=V ...] [--external=pkg ...] [--loader=.ext:name ...] path/to/(stasis.code.br|stasis.lock.json) [entry]
@@ -189,203 +190,22 @@ if (command === '-v' || command === '--version') {
189
190
  // code is null when the child died from a signal; report 128+signo (shell convention) instead of the implicit 0
190
191
  process.exitCode = code ?? 128 + (osConstants.signals[signal] ?? 0)
191
192
  } else if (command === 'bundle') {
192
- const options = {
193
- mapping: { type: 'string' },
194
- manifests: { type: 'boolean' },
195
- output: { type: 'string', short: 'o' },
196
- scope: { type: 'string' },
197
- lockfile: { type: 'string' },
198
- conditions: { type: 'string' },
199
- mainFields: { type: 'string' },
200
- metro: { type: 'boolean' },
201
- 'metro-resolver': { type: 'boolean' },
202
- platforms: { type: 'string', multiple: true },
203
- jsx: { type: 'boolean' },
204
- flow: { type: 'boolean' },
205
- typescript: { type: 'boolean' },
206
- tsconfig: { type: 'string' },
207
- resources: { type: 'string' },
208
- 'package-json': { type: 'boolean' },
209
- cargo: { type: 'boolean' },
210
- 'cargo-features': { type: 'string', multiple: true },
211
- 'cargo-no-default-features': { type: 'boolean' },
212
- 'cargo-all-features': { type: 'boolean' },
213
- 'cargo-target': { type: 'string' },
214
- 'cargo-manifests': { type: 'boolean' },
215
- 'brotli-quality': { type: 'string' },
216
- add: { type: 'boolean' },
217
- }
218
- const values = parseLeadingOptions(argv, options, {
219
- valueFlags: ['--mapping', '--output', '--scope', '--lockfile', '--conditions', '--mainFields', '--platforms', '--resources', '--tsconfig', '--cargo-features', '--cargo-target', '--brotli-quality', '-o'],
220
- onError: fail,
221
- })
222
- if (argv.length === 0) fail('Nothing to bundle: no entry file given')
223
- // A directory entry stands for the .sol files under it (Solidity only); an extensionless path
224
- // that doesn't exist is a missing one (skipped with a warning).
225
- const { directoryEntryError, isSolidityEntry } = await import('../src/cmd/bundle.js')
226
- const dirError = directoryEntryError(argv)
227
- if (dirError !== null) fail(`Error: ${dirError}`)
228
- const allSol = argv.every((f) => isSolidityEntry(f))
229
- const allPhp = argv.every((f) => f.endsWith('.php'))
230
- const allJs = argv.every((f) => /\.(?:js|cjs|mjs|ts|cts|mts|jsx|tsx)$/u.test(f))
231
- const allBash = argv.every((f) => /\.(?:sh|bash)$/u.test(f))
232
- const allRust = argv.every((f) => f.endsWith('.rs'))
233
- if (!allSol && !allPhp && !allJs && !allBash && !allRust) {
234
- fail('Error: bundle entries must all be .sol, all be .php, all be .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx, all be .sh/.bash, or all be .rs')
235
- }
236
- if (values.mapping && !allSol) fail('Error: --mapping is only valid for .sol bundles')
237
- // --manifests: carry the Solidity build's description files (foundry.toml, remappings.txt, ...).
238
- const manifests = Boolean(values.manifests)
239
- if (manifests && !allSol) fail('Error: --manifests is only valid for .sol bundles')
240
- // --cargo: take the Rust feature/dependency resolution from `cargo metadata` (runs cargo; opt-in).
241
- const cargo = Boolean(values.cargo)
242
- if (cargo && !allRust) fail('Error: --cargo is only valid for Rust bundles')
243
- // --cargo-features / --cargo-no-default-features / --cargo-all-features: cargo's own feature flags
244
- // for the entries' packages (`pkg/feat` targets one, or a dependency). --cargo-features is
245
- // repeatable and/or comma-separated; parseFeatureList is the one splitter (the resolver reuses it).
246
- const { parseFeatureList } = await import('../src/loaders/cargo.js')
247
- const cargoFeatures = parseFeatureList(values['cargo-features'] ?? [])
248
- if (values['cargo-features'] !== undefined && cargoFeatures.length === 0) {
249
- fail('Error: --cargo-features must list at least one feature (e.g. --cargo-features=serde,app/tls)')
250
- }
251
- const cargoNoDefaultFeatures = Boolean(values['cargo-no-default-features'])
252
- const cargoAllFeatures = Boolean(values['cargo-all-features'])
253
- // --cargo-target=<triple|host>: the build's target, whose cfgs (from `rustc --print cfg`) decide
254
- // `#[cfg(unix)]`-style code. A triple is letters, digits, `-`, `_` and `.` (`x86_64-unknown-linux-gnu`).
255
- const cargoTarget = values['cargo-target'] ?? null
256
- if (cargoTarget !== null && !/^[\w.-]+$/u.test(cargoTarget)) {
257
- fail('Error: --cargo-target must be a target triple or "host" (e.g. --cargo-target=aarch64-apple-darwin)')
258
- }
259
- // --cargo-manifests: carry each bundled package's Cargo.toml and build script, the workspace
260
- // Cargo.toml, Cargo.lock and .cargo/config.toml too (the Rust counterpart of --package-json).
261
- const cargoManifests = Boolean(values['cargo-manifests'])
262
- if (!allRust) {
263
- const given = { 'cargo-features': cargoFeatures.length > 0, 'cargo-no-default-features': cargoNoDefaultFeatures, 'cargo-all-features': cargoAllFeatures, 'cargo-target': cargoTarget !== null, 'cargo-manifests': cargoManifests }
264
- for (const [flag, on] of Object.entries(given)) if (on) fail(`Error: --${flag} is only valid for Rust bundles`)
265
- }
266
- if (values.scope && !allJs) fail('Error: --scope is only valid for JS bundles')
267
- if (values.scope && !['node_modules', 'full'].includes(values.scope)) {
268
- fail('Error: --scope must be node_modules or full')
269
- }
270
- if (values.lockfile && !allJs) fail('Error: --lockfile is only valid for JS bundles')
271
- // --mainFields/--metro always emit a full-scope bundle, so they'd silently ignore --scope.
272
- if (values.scope !== undefined && (values.mainFields !== undefined || values.metro)) {
273
- fail('Error: --scope is not supported with --mainFields or --metro')
274
- }
275
- // --conditions: extra exports/imports resolution conditions merged onto Node's defaults;
276
- // they don't honour legacy `mainFields` or platform suffixes (see --mainFields).
277
- if (values.conditions !== undefined && !allJs) fail('Error: --conditions is only valid for JS bundles')
278
- const conditions = values.conditions === undefined
279
- ? []
280
- : values.conditions.split(',').map((s) => s.trim()).filter(Boolean)
281
- if (values.conditions !== undefined && conditions.length === 0) {
282
- fail('Error: --conditions must list at least one condition name (e.g. --conditions=react-native,browser)')
283
- }
284
- // --flow: strip Flow type syntax (via the optional flow-remove-types dep) before parsing; JS-only.
285
- if (values.flow && !allJs) fail('Error: --flow is only valid for JS bundles')
286
- const flow = Boolean(values.flow)
287
- // --typescript: resolve like tsc -- a specifier naming a missing JS output (./x.js, .mjs, .cjs,
288
- // extensionless) lands on its on-disk TS source (./x.ts, .mts, .cts); an existing .js always wins.
289
- if (values.typescript && !allJs) fail('Error: --typescript is only valid for JS bundles')
290
- const typescript = Boolean(values.typescript)
291
- // --tsconfig: the config whose compilerOptions.paths aliases --typescript honours (default: the
292
- // tsconfig.json beside each importing file's package.json). Meaningless without --typescript.
293
- if (values.tsconfig !== undefined && !typescript) fail('Error: --tsconfig is only valid with --typescript')
294
- // --mainFields: legacy package entry fields (e.g. react-native,browser,main) for the non-exports resolver.
295
- if (values.mainFields !== undefined && !allJs) fail('Error: --mainFields is only valid for JS bundles')
296
- const mainFields = values.mainFields === undefined
297
- ? undefined
298
- : values.mainFields.split(',').map((s) => s.trim()).filter(Boolean)
299
- if (values.mainFields !== undefined && mainFields.length === 0) {
300
- fail('Error: --mainFields must list at least one field (e.g. --mainFields=react-native,browser,main)')
301
- }
302
- // --metro: resolve like Metro/RN (its own conditions + mainFields, platform suffixes per
303
- // --platforms target). --platforms is repeatable and/or comma-separated; the values union.
304
- const metro = Boolean(values.metro)
305
- // --metro-resolver: resolve through the project's own metro-resolver instead of the built-in
306
- // approximation. Only meaningful alongside --metro (enforced below and in classifyEntries).
307
- const metroResolver = Boolean(values['metro-resolver'])
308
- const platforms = [...new Set((values.platforms ?? []).flatMap((p) => p.split(',')).map((s) => s.trim()).filter(Boolean))]
309
- // A platform name becomes an edge key: reject '/' (parsers reject it) and '*' (reserved placeholder).
310
- for (const p of platforms) {
311
- if (p === '*' || p.includes('/')) fail(`Error: invalid --platforms value '${p}' (a platform name can't contain '/' or be '*')`)
312
- }
313
- if ((metro || platforms.length > 0) && !allJs) fail('Error: --metro is only valid for JS bundles')
314
- // --jsx: parse React Native's JSX-in-.js source in the static scanner (off by default; oxc,
315
- // like tsc, auto-enables JSX for .jsx/.tsx only). Orthogonal to the resolver, so it pairs with
316
- // plain/--mainFields/--metro alike -- JS-only, since no other entry language is scanned.
317
- const jsx = Boolean(values.jsx)
318
- if (jsx && !allJs) fail('Error: --jsx is only valid for JS bundles')
319
- // --package-json: fold each bundled module's package.json into the bundle. JS-only (only JS
320
- // modules carry an npm package.json; the sol/php/bash/rust bucketizers have no such manifest).
321
- const packageJSON = Boolean(values['package-json'])
322
- if (packageJSON && !allJs) fail('Error: --package-json is only valid for JS bundles')
323
- // --resources: comma-separated extension/filename allowlist for assets reached through the graph
324
- // (e.g. --resources=png,svg). They're carried as resources instead of failing "can't carry" --
325
- // for graphs that aren't fully loadable in JS (Metro consumes such assets). JS-only.
326
- if (values.resources !== undefined && !allJs) fail('Error: --resources is only valid for JS bundles')
327
- const resources = values.resources === undefined
328
- ? []
329
- : values.resources.split(',').map((s) => s.trim()).filter(Boolean)
330
- if (values.resources !== undefined && resources.length === 0) {
331
- fail('Error: --resources must list at least one extension or filename (e.g. --resources=png,svg)')
332
- }
193
+ const { BUNDLE_FLAGS, BUNDLE_VALUE_FLAGS, bundlePlan, readConfigEntries } = await import('../src/cmd/bundle-options.js')
194
+ const values = parseLeadingOptions(argv, BUNDLE_FLAGS, { valueFlags: BUNDLE_VALUE_FLAGS, onError: fail })
195
+ const cwd = process.cwd()
196
+ let plan
333
197
  try {
334
- parseResourcesOption('--resources', resources) // validate early for a clean usage error (code exts / bad chars rejected)
198
+ // stasis.config.json's `bundle`, where it is an object: its entries' options, and with no entry
199
+ // given, the entries.
200
+ const config = readConfigEntries(cwd)
201
+ const entries = argv.length > 0 ? argv : [...(config?.values() ?? [])].map(({ entry }) => entry)
202
+ if (entries.length === 0) fail('Nothing to bundle: no entry file given')
203
+ plan = bundlePlan({ cwd, entries, values, config })
335
204
  } catch (cause) {
336
205
  fail(`Error: ${cause.message}`)
337
206
  }
338
- if (metro) {
339
- if (conditions.length > 0) fail("Error: --conditions can't be combined with --metro (it sets its own conditions)")
340
- if (mainFields !== undefined) fail("Error: --mainFields can't be combined with --metro (it sets its own mainFields)")
341
- if (platforms.length === 0) fail('Error: --metro requires --platforms (e.g. --platforms=ios,android)')
342
- } else if (platforms.length > 0) {
343
- fail('Error: --platforms is only valid with --metro')
344
- }
345
- if (metroResolver && !metro) fail('Error: --metro-resolver is only valid with --metro')
346
- // metro-resolver can't substitute .js -> .ts, so --typescript would silently not apply -- reject it.
347
- if (typescript && metroResolver) fail("Error: --typescript is not supported with --metro-resolver (the project's metro-resolver doesn't substitute .js -> .ts)")
348
- // --brotli-quality: valid for every entry language (no allJs gate).
349
- let brotliQuality
350
- if (values['brotli-quality'] !== undefined) {
351
- try {
352
- brotliQuality = parseBrotliQuality('--brotli-quality', values['brotli-quality'])
353
- } catch (cause) {
354
- fail(`Error: ${cause.message}`)
355
- }
356
- }
357
- // --add merges into the existing bundle at --output, so it can't target write-only stdout.
358
- const add = Boolean(values.add)
359
- if (add && values.output === '-') fail('Error: --add cannot be combined with --output=-')
360
207
  const { bundleCommand } = await import('../src/cmd/bundle.js')
361
- await bundleCommand({
362
- cwd: process.cwd(),
363
- entries: argv,
364
- mappingFile: values.mapping,
365
- manifests,
366
- output: values.output,
367
- scope: values.scope,
368
- lockfile: values.lockfile,
369
- conditions,
370
- mainFields,
371
- metro,
372
- metroResolver,
373
- platforms,
374
- jsx,
375
- flow,
376
- typescript,
377
- tsconfig: values.tsconfig,
378
- resources,
379
- packageJSON,
380
- cargo,
381
- cargoFeatures,
382
- cargoNoDefaultFeatures,
383
- cargoAllFeatures,
384
- cargoTarget,
385
- cargoManifests,
386
- brotliQuality,
387
- add,
388
- })
208
+ await bundleCommand({ cwd, ...plan })
389
209
  } else if (command === 'github-bundle') {
390
210
  const values = parseLeadingOptions(argv, {
391
211
  github: { type: 'string' },
@@ -408,11 +228,12 @@ if (command === '-v' || command === '--version') {
408
228
  typescript: { type: 'boolean' },
409
229
  tsconfig: { type: 'string' },
410
230
  resources: { type: 'string' },
231
+ define: { type: 'string', multiple: true },
411
232
  'package-json': { type: 'boolean' },
412
233
  'brotli-quality': { type: 'string' },
413
234
  generate: { type: 'string', multiple: true },
414
235
  }, {
415
- valueFlags: ['--github', '--sha', '--tag', '--directory', '--package-manager', '--package-manager-version', '--mapping', '--output', '--scope', '--lockfile', '--conditions', '--mainFields', '--platforms', '--tsconfig', '--resources', '--brotli-quality', '--generate', '-o'],
236
+ valueFlags: ['--github', '--sha', '--tag', '--directory', '--package-manager', '--package-manager-version', '--mapping', '--output', '--scope', '--lockfile', '--conditions', '--mainFields', '--platforms', '--tsconfig', '--resources', '--define', '--brotli-quality', '--generate', '-o'],
416
237
  onError: fail,
417
238
  })
418
239
  if (values.github === undefined) fail('Error: github-bundle requires --github=owner/name, the repo to bundle')
@@ -427,6 +248,15 @@ if (command === '-v' || command === '--version') {
427
248
  fail(`Error: ${cause.message}`)
428
249
  }
429
250
  }
251
+ let define
252
+ if (values.define !== undefined) {
253
+ const { parseDefineFlags } = await import('../src/define.js')
254
+ try {
255
+ define = parseDefineFlags(null, values.define)
256
+ } catch (cause) {
257
+ fail(`Error: ${cause.message}`)
258
+ }
259
+ }
430
260
  // Tarballs, zips and GitHub trees are cached where `stasis audit` caches.
431
261
  const { setCacheDir } = await import('../src/vfs-bundle.js')
432
262
  setCacheDir(userCacheDir())
@@ -452,7 +282,8 @@ if (command === '-v' || command === '--version') {
452
282
  flow: Boolean(values.flow),
453
283
  typescript: Boolean(values.typescript),
454
284
  tsconfig: values.tsconfig,
455
- resources: list(values.resources),
285
+ resources: values.resources === undefined ? undefined : list(values.resources),
286
+ define,
456
287
  packageJSON: Boolean(values['package-json']),
457
288
  generate: list(values.generate),
458
289
  brotliQuality,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis",
3
- "version": "1.0.0-beta.10",
3
+ "version": "1.0.0-beta.12",
4
4
  "description": "File-level source bundles and lockfiles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -34,11 +34,13 @@
34
34
  "src/bundle-from-lockfile.js",
35
35
  "src/bundle-reader.js",
36
36
  "src/cmd/bundle.js",
37
+ "src/cmd/bundle-options.js",
37
38
  "src/cmd/build.js",
38
39
  "src/cmd/diff.js",
39
40
  "src/cmd/github-bundle.js",
40
41
  "src/cmd/output.js",
41
42
  "src/cmd/sbom.js",
43
+ "src/define.js",
42
44
  "src/diff.js",
43
45
  "src/esbuild.js",
44
46
  "src/installed-commits.js",
@@ -95,12 +97,12 @@
95
97
  "homepage": "https://github.com/PreventiveMeasures/stasis#readme",
96
98
  "dependencies": {
97
99
  "@exodus/bytes": "^1.16.0",
98
- "@exodus/stasis-core": "1.0.0-beta.10",
99
- "@exodus/stasis-plugins": "1.0.0-beta.10",
100
- "@preventive/archive": "1.0.0-beta.4",
101
- "@preventive/deptree": "1.0.0-alpha.11",
102
- "@preventive/lockfile": "1.0.0-alpha.7",
103
- "@preventive/upstream": "1.0.0-alpha.12",
100
+ "@exodus/stasis-core": "1.0.0-beta.12",
101
+ "@exodus/stasis-plugins": "1.0.0-beta.12",
102
+ "@preventive/archive": "^1.2.0",
103
+ "@preventive/deptree": "^1.0.1",
104
+ "@preventive/lockfile": "^1.2.0",
105
+ "@preventive/upstream": "^1.3.0",
104
106
  "@preventive/vfs": "1.0.0-beta.4",
105
107
  "oxc-parser": "^0.152.0"
106
108
  },
package/src/audit.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { packageRepo } from '@exodus/stasis-core/bundle-util'
2
- import { dependencyEcosystem, moduleFileKey, sameGithub } from '@exodus/stasis-core/util'
2
+ import { dependencyEcosystem, moduleFileKey, packageRecords, sameGithub } from '@exodus/stasis-core/util'
3
3
  import { advisories } from '@preventive/upstream/advisories.js'
4
4
  import { compareVersions, valid } from '@preventive/upstream/semver.js'
5
5
  import { isEvidenceFile } from './audit-corrections.js'
@@ -35,7 +35,8 @@ const keyOf = (ecosystem, name, version) => `${ecosystem === 'npm' ? '' : `${eco
35
35
  const isComposerDev = (version) => /^dev-|-dev$/iu.test(version.replace(/#.*$/su, ''))
36
36
 
37
37
  // Why `pkg` cannot be audited, or undefined where it can.
38
- function unaudited({ ecosystem, version }) {
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`
39
40
  if (Object.hasOwn(UNREGISTERED_CRATES, ecosystem)) return UNREGISTERED_CRATES[ecosystem]
40
41
  if (!Object.hasOwn(SOURCES, ecosystem)) return `no advisories are looked up for ${ecosystem}`
41
42
  if (ecosystem === 'composer' && isComposerDev(version)) return 'a Composer dev version, which no advisory database lists'
@@ -61,15 +62,25 @@ function unaudited({ ecosystem, version }) {
61
62
  // one does; else, for an npm package a bundle carries the package.json of (one built before builds
62
63
  // recorded `repo`), the one that names, read by the rule builds record it by. A GitHub repo is its
63
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.
64
70
  export function collectPackagesFromFile(file) {
65
71
  const out = []
66
72
  const { kind, artifact } = parseFileWithKind(file)
67
- for (const [dir, { name, version, ecosystem: tagged, repo, files }] of artifact.modules) {
73
+ for (const [dir, { name, version, ecosystem: tagged, repo, files, vendoredIn }] of packageRecords(artifact.modules)) {
68
74
  const ecosystem = dependencyEcosystem(dir, tagged)
69
75
  // First-party code is never sent to a public registry (leaks names, adds noise).
70
76
  if (ecosystem === undefined) continue
71
- if (!name || !version) continue
72
- if (!Object.keys(files).some((rel) => isEvidenceFile(name, version, rel, ecosystem))) 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
+ }
73
84
  let github = ecosystem === 'github' ? undefined : repo?.github
74
85
  if (github === undefined && ecosystem === 'npm' && kind === 'bundle') github = manifestRepo(files['package.json'])
75
86
  out.push({ ecosystem, name, version: versionOf(ecosystem, version), ...(github === undefined ? {} : { github }) })
@@ -96,14 +107,20 @@ const byVersion = (a, b) => (valid(a) && valid(b) ? compareVersions(a, b) : byNu
96
107
  // Each package once, with the GitHub repository its artifacts record for its name, which
97
108
  // advisories() asks instead of looking one up. It takes one repository a name, so a name recorded
98
109
  // with two, by two of its versions (a package that moved) or by two artifacts, has none, and is
99
- // looked up as before.
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.
100
113
  export function collectPackages(files) {
101
114
  const byKey = new Map()
115
+ const vendored = new Map() // key -> { hosts, only }
102
116
  const repos = new Map() // `ecosystem:name` -> the one repository recorded for it, or null
103
117
  for (const file of files) {
104
- for (const { github, ...pkg } of collectPackagesFromFile(file)) {
105
- const key = keyOf(pkg.ecosystem, pkg.name, pkg.version)
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)
106
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)
107
124
  if (github === undefined) continue
108
125
  const name = `${pkg.ecosystem}:${pkg.name}`
109
126
  const known = repos.get(name)
@@ -112,10 +129,11 @@ export function collectPackages(files) {
112
129
  else if (known !== null && !sameGithub(known, github)) repos.set(name, null)
113
130
  }
114
131
  }
115
- return [...byKey.values()]
116
- .map((pkg) => {
132
+ return [...byKey]
133
+ .map(([key, pkg]) => {
117
134
  const github = repos.get(`${pkg.ecosystem}:${pkg.name}`)
118
- return github ? { ...pkg, github } : pkg
135
+ const { hosts, only } = vendored.get(key)
136
+ return { ...pkg, ...(github ? { github } : {}), ...(hosts.size === 0 ? {} : { vendoredIn: [...hosts].toSorted(), vendoredOnly: only }) }
119
137
  })
120
138
  .toSorted((a, b) => a.ecosystem.localeCompare(b.ecosystem) || a.name.localeCompare(b.name) || byVersion(a.version, b.version))
121
139
  }
@@ -126,27 +144,30 @@ export function collectPackages(files) {
126
144
  // (see audit-corrections.js): a consumer that recorded nothing of a package but a corrected file or
127
145
  // its manifest carries none of its real code and is NOT a reason -- e.g. webpack shipping only ws's
128
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.
129
148
  export function collectReasons(files) {
130
149
  const byPkg = new Map()
131
150
  for (const file of files) {
132
151
  const artifact = parseFile(file)
133
152
  const fileReasons = invertReason(artifact.reason)
134
153
  if (!fileReasons) continue
135
- const fileToPkg = new Map()
136
- for (const [dir, { name, version, ecosystem: tagged, files: modFiles }] of artifact.modules) {
154
+ const fileToPkgs = new Map()
155
+ for (const [dir, { name, version, ecosystem: tagged, files: modFiles }] of packageRecords(artifact.modules)) {
137
156
  const ecosystem = dependencyEcosystem(dir, tagged)
138
157
  if (ecosystem === undefined || !name || !version) continue
139
158
  for (const rel of Object.keys(modFiles)) {
140
159
  if (!isEvidenceFile(name, version, rel, ecosystem)) continue
141
- fileToPkg.set(moduleFileKey(dir, rel), keyOf(ecosystem, name, versionOf(ecosystem, version)))
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)))
142
163
  }
143
164
  }
144
165
  for (const [f, consumers] of fileReasons) {
145
- const key = fileToPkg.get(f)
146
- if (key === undefined) continue
147
- let set = byPkg.get(key)
148
- if (set === undefined) byPkg.set(key, (set = new Set()))
149
- for (const consumer of consumers) set.add(consumer)
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
+ }
150
171
  }
151
172
  }
152
173
  return byPkg
@@ -155,15 +176,15 @@ export function collectReasons(files) {
155
176
  const SEVERITY_ORDER = { critical: 0, high: 1, moderate: 2, low: 3, info: 4, none: 5 }
156
177
 
157
178
  // Consumer display order: bundler plugins (metro, webpack, esbuild, ...) first,
158
- // then `run`, then `add`. Ties broken alphabetically.
159
- const CONSUMER_RANK = { run: 1, add: 2 }
179
+ // then `run`, then `add`, then `extra` (stasis.config.json's). Ties broken alphabetically.
180
+ const CONSUMER_RANK = { run: 1, add: 2, extra: 3 }
160
181
  const consumerRank = (c) => CONSUMER_RANK[c] ?? 0
161
182
  const byConsumerOrder = (a, b) => consumerRank(a) - consumerRank(b) || a.localeCompare(b)
162
183
 
163
184
  // Build a row's `reason` cell by unioning over exactly the affected versions it
164
185
  // covers. Without `--why` that's the `, `-joined set of bundle consumers; with
165
186
  // `--why` it's the newline-joined `consumer: a -> b -> c` import-path lines
166
- // (which REPLACE the consumer list). Consumers are ordered plugins -> run -> add.
187
+ // (which REPLACE the consumer list). Consumers are ordered plugins -> run -> add -> extra.
167
188
  // `reasonFilter`, when set, narrows the cell to a single consumer: the `--why`
168
189
  // chains are already filtered upstream (see collectWhy), so only the consumer
169
190
  // list needs pruning here. collectWhy follows npm's import graph alone, so an
@@ -176,7 +197,7 @@ function reasonCell({ ecosystem, name, versions: affected }, reasonsByPkg, whyBy
176
197
  for (const p of source.get(keyOf(ecosystem, name, v)) ?? []) parts.add(p)
177
198
  }
178
199
  // --why: group `consumer: path` lines by consumer and order the groups
179
- // plugins -> run -> add (this also re-unites a consumer's lines when they were
200
+ // plugins -> run -> add -> extra (this also re-unites a consumer's lines when they were
180
201
  // split across affected versions); within a group collectWhy's compressed order
181
202
  // is preserved. Otherwise: the `, `-joined consumer set in the same order.
182
203
  if (chains) {
@@ -190,10 +211,18 @@ function reasonCell({ ecosystem, name, versions: affected }, reasonsByPkg, whyBy
190
211
  return consumers.toSorted(byConsumerOrder).join(', ')
191
212
  }
192
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
+
193
222
  // `found` is what @preventive/upstream's advisories() answers: one row per advisory range, whose
194
223
  // `versions` are the audited versions it covers (never empty -- a range covering none is dropped
195
224
  // there, so the table and exit code reflect only real hits).
196
- export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = null, reasonFilter = null) {
225
+ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = null, reasonFilter = null, vendoredByKey = new Map()) {
197
226
  const rows = []
198
227
  for (const adv of found) {
199
228
  const reason = reasonCell(adv, reasonsByPkg, whyByPkg, reasonFilter)
@@ -203,7 +232,7 @@ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = nu
203
232
  rows.push({
204
233
  ecosystem: adv.ecosystem,
205
234
  package: adv.name,
206
- installed: adv.versions.join(', '),
235
+ installed: adv.versions.map((v) => installedCell(adv, v, vendoredByKey)).join(', '),
207
236
  vulnerable: adv.range ?? '',
208
237
  severity: adv.severity ?? '',
209
238
  title: adv.title ?? '',
@@ -281,7 +310,7 @@ export async function audit(files, { why = false, whyDeep = false, whyFull = fal
281
310
  }
282
311
  let result
283
312
  try {
284
- result = await advisories(asked.map(({ version, ...pkg }) => ({ ...pkg, versions: [version] })), { repoAdvisories, github })
313
+ result = await advisories(asked.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] })), { repoAdvisories, github })
285
314
  } catch (cause) {
286
315
  // Refused input or a malformed answer is an assertion that says so itself; a transport or
287
316
  // HTTP failure gets the context of which request it was.
@@ -296,14 +325,15 @@ export async function audit(files, { why = false, whyDeep = false, whyFull = fal
296
325
  // `reason` (--reason) narrows both the paths (in collectWhy) and the consumer
297
326
  // list (in flattenAdvisories) to a single consumer, dropping advisories
298
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]))
299
329
  let rows
300
330
  if (why) {
301
331
  const npm = result.filter((adv) => adv.ecosystem === 'npm')
302
332
  const targetKeys = new Set(npm.flatMap((adv) => adv.versions.map((v) => keyOf(adv.ecosystem, adv.name, v))))
303
333
  const reasons = npm.length < result.length ? collectReasons(files) : undefined
304
- rows = flattenAdvisories(result, reasons, collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull }), reason)
334
+ rows = flattenAdvisories(result, reasons, collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull }), reason, vendoredByKey)
305
335
  } else {
306
- rows = flattenAdvisories(result, collectReasons(files), null, reason)
336
+ rows = flattenAdvisories(result, collectReasons(files), null, reason, vendoredByKey)
307
337
  }
308
338
  return { packages, skipped, advisories: result, rows, why, whyDeep, whyFull, reason }
309
339
  }
@@ -322,7 +352,7 @@ export function printAuditReport({ packages, skipped = [], rows, why = false, re
322
352
  return
323
353
  }
324
354
  err.write(`${scanned}: ${alertStats(rows)}\n`)
325
- for (const pkg of skipped) err.write(`Not audited: ${pkg.ecosystem} ${pkg.name}@${pkg.version}, ${pkg.because}\n`)
355
+ for (const pkg of skipped) err.write(`Not audited: ${pkg.ecosystem} ${pkg.name}${pkg.version === undefined ? '' : `@${pkg.version}`}, ${pkg.because}\n`)
326
356
  if (rows.length === 0) return
327
357
  // The ecosystem column only where one is not npm's, which the others were all once.
328
358
  const columns = ['severity', ...(rows.some((r) => r.ecosystem !== 'npm') ? ['ecosystem'] : []), 'package', 'installed', 'vulnerable', 'title', 'id']