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

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 (46) hide show
  1. package/README.md +5 -1
  2. package/bin/stasis.js +191 -149
  3. package/package.json +31 -7
  4. package/src/audit.js +224 -90
  5. package/src/babel.js +44 -37
  6. package/src/bundle-reader.js +24 -4
  7. package/src/cmd/build.js +218 -8
  8. package/src/cmd/bundle-options.js +31 -25
  9. package/src/cmd/bundle.js +1147 -311
  10. package/src/cmd/github-bundle.js +17 -9
  11. package/src/cmd/output.js +10 -3
  12. package/src/cmd/tokens.js +73 -0
  13. package/src/define.js +5 -0
  14. package/src/diff.js +2 -2
  15. package/src/flow.js +42 -0
  16. package/src/installed-commits.js +4 -2
  17. package/src/loaders/bash.js +2 -27
  18. package/src/loaders/cargo.js +6 -6
  19. package/src/loaders/foundry.js +4 -6
  20. package/src/loaders/paths.js +4 -15
  21. package/src/loaders/php.js +3 -5
  22. package/src/loaders/rust.js +19 -39
  23. package/src/loaders/solidity-ownership.js +4 -15
  24. package/src/loaders/solidity.js +4 -67
  25. package/src/metro-assets.js +65 -0
  26. package/src/metro-resolver.js +76 -123
  27. package/src/parse.js +7 -3
  28. package/src/resolve-fields.js +133 -366
  29. package/src/resolve-node.js +9 -7
  30. package/src/resolve-typescript.js +102 -30
  31. package/src/scan.js +154 -54
  32. package/src/sixel.js +333 -0
  33. package/src/table.js +116 -0
  34. package/src/validate-notes.d.ts +18 -0
  35. package/src/validate-notes.js +66 -0
  36. package/src/validate.d.ts +132 -0
  37. package/src/validate.js +1349 -0
  38. package/src/vfs-bundle/entries.js +5 -2
  39. package/src/vfs-bundle/github.js +68 -19
  40. package/src/vfs-bundle/glob.js +420 -0
  41. package/src/vfs-bundle/lazy.js +128 -0
  42. package/src/vfs-bundle/prisma.js +102 -29
  43. package/src/vfs-bundle/tree.js +238 -75
  44. package/src/vfs-bundle/yarnclean.js +100 -0
  45. package/src/vfs-bundle.js +16 -7
  46. package/src/why.js +9 -7
package/src/audit.js CHANGED
@@ -3,7 +3,11 @@ import { dependencyEcosystem, moduleFileKey, packageRecords, sameGithub } from '
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'
6
+ import { DIM, formatTable, painter, takesColors } from './table.js'
7
+
8
+ export { formatTable }
6
9
  import { parseFile, parseFileWithKind } from './parse.js'
10
+ import { logos } from './sixel.js'
7
11
  import { collectWhy, invertReason } from './why.js'
8
12
 
9
13
  // Where advisories() asks for each ecosystem's advisories: npm's registry; OSV for crates (those
@@ -30,6 +34,15 @@ const versionOf = (ecosystem, version) => (ecosystem === 'github' && version ===
30
34
  // prefixed by the ecosystem for the others, whose names may be npm's too.
31
35
  const keyOf = (ecosystem, name, version) => `${ecosystem === 'npm' ? '' : `${ecosystem}:`}${name}@${version}`
32
36
 
37
+ // A package's key as collectPackages and collectReasons tell packages apart: keyOf it, but a copy a dependency
38
+ // ships with no version, which is one package for each host, by its host's `name@version` too.
39
+ const packageKey = (ecosystem, name, version, host) => (version === undefined ? `${ecosystem}:${name} in ${host}` : keyOf(ecosystem, name, version))
40
+
41
+ // A refused audit, as a usage error: one for the message alone, never a stack.
42
+ export class AuditError extends Error {
43
+ name = 'AuditError'
44
+ }
45
+
33
46
  // Composer's dev versions (`dev-main`, `1.x-dev`), as Composer tells them, a `#ref` dropped: no
34
47
  // advisory database lists those, and advisories() takes releases alone.
35
48
  const isComposerDev = (version) => /^dev-|-dev$/iu.test(version.replace(/#.*$/su, ''))
@@ -116,7 +129,7 @@ export function collectPackages(files) {
116
129
  const repos = new Map() // `ecosystem:name` -> the one repository recorded for it, or null
117
130
  for (const file of files) {
118
131
  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)
132
+ const key = packageKey(pkg.ecosystem, pkg.name, pkg.version, vendoredIn)
120
133
  if (!byKey.has(key)) byKey.set(key, pkg)
121
134
  if (!vendored.has(key)) vendored.set(key, { hosts: new Set(), only: true })
122
135
  if (vendoredIn === undefined) vendored.get(key).only = false
@@ -144,36 +157,72 @@ export function collectPackages(files) {
144
157
  // (see audit-corrections.js): a consumer that recorded nothing of a package but a corrected file or
145
158
  // its manifest carries none of its real code and is NOT a reason -- e.g. webpack shipping only ws's
146
159
  // 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.
160
+ // A file of a copy a dependency ships (packageRecords) attributes to both: the copy and its host; a copy
161
+ // with no version by packageKey.
148
162
  export function collectReasons(files) {
163
+ return reasonIndex(files).byPkg
164
+ }
165
+
166
+ // collectReasons' map (`byPkg`), and every consumer the files' reason maps record (`consumers`), a consumer
167
+ // that recorded first-party code alone too: what --reason may name.
168
+ function reasonIndex(files) {
149
169
  const byPkg = new Map()
170
+ const consumers = new Set()
150
171
  for (const file of files) {
151
172
  const artifact = parseFile(file)
152
173
  const fileReasons = invertReason(artifact.reason)
153
174
  if (!fileReasons) continue
175
+ for (const [consumer, list] of Object.entries(artifact.reason)) if (Array.isArray(list)) consumers.add(consumer)
154
176
  const fileToPkgs = new Map()
155
- for (const [dir, { name, version, ecosystem: tagged, files: modFiles }] of packageRecords(artifact.modules)) {
177
+ for (const [dir, { name, version, ecosystem: tagged, files: modFiles, vendoredIn }] of packageRecords(artifact.modules)) {
156
178
  const ecosystem = dependencyEcosystem(dir, tagged)
157
- if (ecosystem === undefined || !name || !version) continue
179
+ if (ecosystem === undefined || !name || (!version && vendoredIn === undefined)) continue
180
+ const host = vendoredIn === undefined ? undefined : artifact.modules.get(vendoredIn)
181
+ const key = packageKey(ecosystem, name, version === undefined ? undefined : versionOf(ecosystem, version), host && `${host.name}@${host.version}`)
158
182
  for (const rel of Object.keys(modFiles)) {
159
- if (!isEvidenceFile(name, version, rel, ecosystem)) continue
183
+ if (!isEvidenceFile(name, version ?? null, rel, ecosystem)) continue
160
184
  const f = moduleFileKey(dir, rel)
161
185
  if (!fileToPkgs.has(f)) fileToPkgs.set(f, [])
162
- fileToPkgs.get(f).push(keyOf(ecosystem, name, versionOf(ecosystem, version)))
186
+ fileToPkgs.get(f).push(key)
163
187
  }
164
188
  }
165
- for (const [f, consumers] of fileReasons) {
189
+ for (const [f, recorded] of fileReasons) {
166
190
  for (const key of fileToPkgs.get(f) ?? []) {
167
191
  let set = byPkg.get(key)
168
192
  if (set === undefined) byPkg.set(key, (set = new Set()))
169
- for (const consumer of consumers) set.add(consumer)
193
+ for (const consumer of recorded) set.add(consumer)
170
194
  }
171
195
  }
172
196
  }
173
- return byPkg
197
+ return { byPkg, consumers }
198
+ }
199
+
200
+ // The consumer `stasis bundle --metro` records a Metro build's files under, and the one Metro's own
201
+ // plugin records a build Metro ran under.
202
+ const BUNDLE_METRO = 'bundle:metro'
203
+ const METRO = 'metro'
204
+
205
+ // How audit names the consumers the files record (`consumers`, reasonIndex's): each as it is, but
206
+ // `bundle:metro` as `metro` where none records `metro`, which it then can't be taken for.
207
+ // -> consumer -> its name
208
+ function consumerNames(consumers) {
209
+ if (!consumers.has(BUNDLE_METRO) || consumers.has(METRO)) return (consumer) => consumer
210
+ return (consumer) => (consumer === BUNDLE_METRO ? METRO : consumer)
211
+ }
212
+
213
+ // reasonIndex's `byPkg` and `consumers` with each consumer as audit names it (consumerNames), and `name`, that naming.
214
+ function namedIndex({ byPkg, consumers }) {
215
+ const name = consumerNames(consumers)
216
+ return {
217
+ byPkg: new Map([...byPkg].map(([key, set]) => [key, new Set([...set].map(name))])),
218
+ consumers: new Set([...consumers].map(name)),
219
+ name,
220
+ }
174
221
  }
175
222
 
176
- const SEVERITY_ORDER = { critical: 0, high: 1, moderate: 2, low: 3, info: 4, none: 5 }
223
+ // `malware` first: a package that is malware is worse than any vulnerability in one. An advisory
224
+ // advisories() flags as malware (`malware: true`) is rated so, whatever its source rates it.
225
+ const SEVERITY_ORDER = { malware: 0, critical: 1, high: 2, moderate: 3, low: 4, info: 5, none: 6 }
177
226
 
178
227
  // Consumer display order: bundler plugins (metro, webpack, esbuild, ...) first,
179
228
  // then `run`, then `add`, then `extra` (stasis.config.json's). Ties broken alphabetically.
@@ -234,9 +283,10 @@ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = nu
234
283
  package: adv.name,
235
284
  installed: adv.versions.map((v) => installedCell(adv, v, vendoredByKey)).join(', '),
236
285
  vulnerable: adv.range ?? '',
237
- severity: adv.severity ?? '',
286
+ severity: adv.malware ? 'malware' : adv.severity ?? '',
238
287
  title: adv.title ?? '',
239
288
  id: adv.id,
289
+ source: adv.source,
240
290
  reason,
241
291
  })
242
292
  }
@@ -251,117 +301,201 @@ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = nu
251
301
  return rows
252
302
  }
253
303
 
254
- // Strip line breaks (LF/CR/CRLF, U+2028/U+2029) from advisory titles so they can't break the box layout.
255
- const cell = (v) => String(v ?? '').replace(/[\r\n\u2028\u2029]+/gu, ' ')
256
-
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 = [] } = {}) {
265
- if (rows.length === 0) return ''
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
- )
273
- const pad = (s, w) => s.padEnd(w)
274
- const line = (l, m, r, fill) => l + widths.map((w) => fill.repeat(w + 2)).join(m) + r
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
- }
285
- return [
286
- line('┌', '┬', '┐', '─'),
287
- render(header),
288
- line('├', '┼', '┤', '─'),
289
- ...body.map(render),
290
- line('└', '┴', '┘', '─'),
291
- ].join('\n')
292
- }
293
-
304
+ // `files` are the lockfiles' and bundles' paths, or a Bundle or Lockfile in place of one (one built in
305
+ // memory, as `stasis audit` with no file builds stasis.config.json's entries' bundle), taken as the
306
+ // file would read.
294
307
  // `repoAdvisories` and `github` (a @preventive/upstream/github.js client) go to advisories() as they
295
308
  // are; a Soldeer package or a GitHub repo needs the client, their repository being their only source.
296
309
  // 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 } = {}) {
310
+ // `reason` (--reason) audits only the packages that consumer recorded (collectReasons): no other is
311
+ // counted, listed, or asked about as they are. One no file records is refused (AuditError) before anything
312
+ // is, naming those they do; a lockfile records none. `osv` asks OSV about npm packages too, those whose
313
+ // name npm's registry serves to anyone; advisories() never sends it a private one.
314
+ // `npmMalware` is what npm's registry tells of malware in any npm dependency, whatever `reason`: rows as
315
+ // `rows` are, each package's consumers its reason. Under --reason, the npm packages its consumer didn't
316
+ // record are asked of npm's registry alone for it (fast, where OSV and their repositories aren't), as are
317
+ // those `lockfile` (`{ path, packages }`, validate.js installedNpmPackages', a package manager's) locks that
318
+ // the files don't, deduped with the files' before anything is asked; such a package's reason its file name.
319
+ // Consumers are named as consumerNames names them, across all the files: `bundle:metro` as `metro`
320
+ // where none records `metro`, in the rows, the chains and the `reason` returned, and `reason` may
321
+ // name it either way.
322
+ export async function audit(files, { why = false, whyDeep = false, whyFull = false, reason = null, repoAdvisories = false, github, osv = false, lockfile = null } = {}) {
298
323
  // --why-deep and --why-full imply --why. Deep keeps every chain instead of the
299
324
  // default pruning of chains whose full suffix is already a chain (see
300
325
  // collectWhy/dropSuffixed); full spells chains out with no `...` collapse.
301
326
  why = why || whyDeep || whyFull
302
- const packages = collectPackages(files)
327
+ let index // namedIndex(reasonIndex(files)), once it is needed
328
+ const indexed = () => (index ??= namedIndex(reasonIndex(files)))
329
+ if (reason) {
330
+ const { consumers, name } = indexed()
331
+ if (!consumers.has(name(reason))) {
332
+ const known = [...consumers].toSorted(byConsumerOrder)
333
+ throw new AuditError(`--reason=${reason}: ${known.length === 0
334
+ ? 'the files record no consumers (only a bundle does)'
335
+ : `no file records that consumer; they record ${known.join(', ')}`}`)
336
+ }
337
+ reason = name(reason)
338
+ }
339
+ const all = collectPackages(files)
340
+ const packages = reason ? all.filter((pkg) => indexed().byPkg.get(packageKey(pkg.ecosystem, pkg.name, pkg.version, pkg.vendoredIn?.[0]))?.has(reason)) : all
303
341
  const skipped = packages.flatMap((pkg) => {
304
342
  const because = unaudited(pkg)
305
343
  return because === undefined ? [] : [{ ...pkg, because }]
306
344
  })
307
345
  const asked = packages.filter((pkg) => unaudited(pkg) === undefined)
308
- if (asked.length === 0) {
309
- return { packages, skipped, advisories: [], rows: [], why }
346
+ // Under --reason, the npm packages its consumer didn't record, and those `lockfile` locks alone, for npm's
347
+ // malware advisories, each once, by keyOf: none asked about already.
348
+ const own = new Set(packages)
349
+ const keyed = (pkg) => [keyOf(pkg.ecosystem, pkg.name, pkg.version), pkg]
350
+ const byKey = new Map((reason ? all.filter((pkg) => !own.has(pkg) && pkg.ecosystem === 'npm' && unaudited(pkg) === undefined) : []).map(keyed))
351
+ const askedKeys = new Set(asked.map((pkg) => keyed(pkg)[0]))
352
+ const lockedOnly = new Set()
353
+ for (const { name, version } of lockfile?.packages ?? []) {
354
+ const [key, pkg] = keyed({ ecosystem: 'npm', name, version })
355
+ if (askedKeys.has(key) || byKey.has(key)) continue
356
+ byKey.set(key, pkg)
357
+ lockedOnly.add(key)
310
358
  }
359
+ const others = [...byKey.values()]
360
+ if (asked.length === 0 && others.length === 0) {
361
+ return { packages, skipped, advisories: [], rows: [], npmMalware: [], why, whyDeep, whyFull, reason }
362
+ }
363
+ const query = (list) => list.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] }))
311
364
  let result
365
+ let elsewhere
312
366
  try {
313
- result = await advisories(asked.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] })), { repoAdvisories, github })
367
+ ;[result, elsewhere] = await Promise.all([
368
+ asked.length > 0 ? advisories(query(asked), { repoAdvisories, github, osv }) : [],
369
+ others.length > 0 ? advisories(query(others)) : [],
370
+ ])
314
371
  } catch (cause) {
315
372
  // Refused input or a malformed answer is an assertion that says so itself; a transport or
316
373
  // HTTP failure gets the context of which request it was.
317
374
  if (cause?.code === 'ERR_ASSERTION') throw cause
318
- const sources = new Set([...asked.flatMap((pkg) => SOURCES[pkg.ecosystem]), ...(repoAdvisories ? ['GitHub'] : [])])
375
+ const sources = new Set([...asked.flatMap((pkg) => SOURCES[pkg.ecosystem]), ...(others.length > 0 ? ['npm'] : []), ...(osv && asked.some((pkg) => pkg.ecosystem === 'npm') ? ['OSV'] : []), ...(repoAdvisories ? ['GitHub'] : [])])
319
376
  throw new Error(`${[...sources].join('/')} advisories request failed: ${cause.message}`, { cause })
320
377
  }
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.
378
+ const vendoredOf = (list) => new Map(list.filter((pkg) => pkg.vendoredIn !== undefined).map((pkg) => [keyOf(pkg.ecosystem, pkg.name, pkg.version), pkg]))
379
+ // npm's registry's malware advisories (`registry` rows, not OSV's records), of every npm dependency.
380
+ const malware = [...result, ...elsewhere].filter((adv) => adv.malware && adv.source === 'registry')
381
+ // Their consumers, and the lockfile's name for those it alone locks.
382
+ const malwareReasons = () => (lockedOnly.size === 0 ? indexed().byPkg : new Map([...indexed().byPkg, ...[...lockedOnly].map((key) => [key, new Set([lockfile.path.split(/[/\\]/u).at(-1)])])]))
383
+ const npmMalware = malware.length === 0 ? [] : flattenAdvisories(malware, malwareReasons(), null, null, vendoredOf([...asked, ...others]))
384
+ // `--why` REPLACES the consumer list with per-consumer import paths for npm's
385
+ // advisories, the only ones collectWhy has paths for: another ecosystem's keep
386
+ // their consumers. Restrict the (potentially expensive) path search to the
387
+ // packages that actually carry an advisory; its consumers are named as the
388
+ // cells' are (consumerNames), which takes every file's consumers to know.
325
389
  // `reason` (--reason) narrows both the paths (in collectWhy) and the consumer
326
390
  // list (in flattenAdvisories) to a single consumer, dropping advisories
327
391
  // 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
392
+ let whyByPkg = null
330
393
  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)
394
+ const targetKeys = new Set(result.filter((adv) => adv.ecosystem === 'npm').flatMap((adv) => adv.versions.map((v) => keyOf(adv.ecosystem, adv.name, v))))
395
+ whyByPkg = collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull, consumerName: indexed().name })
337
396
  }
338
- return { packages, skipped, advisories: result, rows, why, whyDeep, whyFull, reason }
397
+ const rows = flattenAdvisories(result, indexed().byPkg, whyByPkg, reason, vendoredOf(asked))
398
+ return { packages, skipped, advisories: result, rows, npmMalware, why, whyDeep, whyFull, reason }
339
399
  }
340
400
 
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(', ')
401
+ // Each severity's colour, the worst the loudest (malware white on red); an advisory without one (RustSec's)
402
+ // `unrated`, dimmed.
403
+ const SEVERITY_TONES = { malware: ['bold', 'white', 'bgRed'], critical: ['bold', 'magenta'], high: ['bold', 'red'], moderate: 'yellow', low: 'cyan', info: DIM, none: DIM, unrated: DIM }
404
+ const severityOf = (row) => row.severity || 'unrated'
405
+
406
+ // The columns whose text wraps where the table is wider than the terminal: the long ones, free text first.
407
+ const WRAPPED = ['title', 'reason', 'vulnerable', 'installed']
408
+
409
+ // `5 alerts (1 critical, 2 high, 1 low, 1 unrated)`: the rows, then each severity present, in the rows'
410
+ // own order (most severe first), so the parts add up; each in its colour, and none in green.
411
+ function alertStats(rows, paint, colored) {
412
+ const count = `${rows.length} alert${rows.length === 1 ? '' : 's'}`
413
+ if (rows.length === 0) return paint(['bold', 'green'], count)
414
+ const bySeverity = Map.groupBy(rows, severityOf)
415
+ return `${paint('bold', count)} (${[...bySeverity].map(([severity, list]) => paint(SEVERITY_TONES[severity], badge(severity, `${list.length} ${severity}`, colored))).join(', ')})`
346
416
  }
347
417
 
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'}`
418
+ // Malware as a badge: its colour a space wider on either side, where there is colour.
419
+ const badge = (severity, text, colored) => (severity === 'malware' && colored ? ` ${text} ` : text)
420
+
421
+ // Where an advisory comes from (upstream's `source`), as sixel.js draws it: npm's registry, a GitHub
422
+ // repository's advisories, or OSV.
423
+ const SOURCE_LOGOS = { registry: 'npm', repository: 'github', osv: 'osv' }
424
+ // The cells an icon covers: two, as sixel.js logos() picks icons no wider than two of the terminal's cells.
425
+ const ICON_CELLS = 2
426
+ // An icon, drawn with the cursor saved and put back, as a terminal may leave it below the image, then
427
+ // moved past the cells it covers, as text written over them would erase it.
428
+ const drawIcon = (icon) => `\x1b7${icon}\x1b8\x1b[${ICON_CELLS}C`
429
+ // Written before a line with an icon: two lines below it, then back up. A terminal moving the cursor
430
+ // below the image then never scrolls, which would put the cursor back a line off.
431
+ const ROOM_BELOW = '\n\n\x1b[2A'
432
+
433
+ // The advisories as a table on `out`, fitted to `width` (the terminal's, where `out` is one) by wrapping
434
+ // its long columns, after `notes` on `err` (lines of what the audit leaves out, each in yellow); on
435
+ // `err`, the packages not audited, then the summary, last, so it is what is read after the table. Severities in colour, the header bold and the box dimmed, where each stream takes
436
+ // colours (`colors`, else takesColors). With `icons` (the logos sixel.js logos() picks for a terminal
437
+ // that draws sixel images, true for its 16-pixel ones), each id has its source's icon before it.
438
+ export function printAuditReport({ packages, skipped = [], rows, npmMalware = [], why = false, reason = null }, { out = process.stdout, err = process.stderr, colors, width = out.isTTY ? out.columns : undefined, icons: given = false, notes = [] } = {}) {
439
+ const icons = given === true ? logos() : given || null
440
+ const paintOut = painter(out, colors)
441
+ const paintErr = painter(err, colors)
442
+ const coloredOut = colors ?? takesColors(out)
443
+ const coloredErr = colors ?? takesColors(err)
444
+ for (const note of notes) err.write(`${paintErr('yellow', note)}\n`)
445
+ const scanned = `Scanned ${packages.length} package${packages.length === 1 ? '' : 's'}${reason ? ` recorded by ${reason}` : ''}`
350
446
  if (packages.length === 0) {
351
- err.write(`${scanned}\nNo dependencies found in the input files\n`)
447
+ err.write(`${scanned}\n${paintErr('yellow', reason ? `No dependencies recorded by ${reason}` : 'No dependencies found in the input files')}\n`)
448
+ printMalware(npmMalware, err, paintErr, coloredErr)
352
449
  return
353
450
  }
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`)
451
+ if (rows.length > 0) {
452
+ // The ecosystem column only where one is not npm's, which the others were all once.
453
+ const columns = ['severity', ...(rows.some((r) => r.ecosystem !== 'npm') ? ['ecosystem'] : []), 'package', 'installed', 'vulnerable', 'title', 'id']
454
+ // Surface the reason column only when some advisory has provenance -- bundle
455
+ // consumers, or (with --why) import paths. Under --reason WITHOUT --why every
456
+ // cell is just the filter value repeated, so drop the column then; --why still
457
+ // earns it (the import chains differ per row).
458
+ if (rows.some((r) => r.reason) && !(reason && !why)) columns.splice(columns.indexOf('installed') + 1, 0, 'reason')
459
+ // A chain's arrows and `...` dimmed, so the packages in it stand out. An id's blank cells its icon.
460
+ const iconOf = (row) => icons?.[SOURCE_LOGOS[row.source]]
461
+ const paint = (text, column, row) => {
462
+ if (column === 'id' && iconOf(row)) return drawIcon(iconOf(row)) + text.slice(ICON_CELLS)
463
+ if (column === 'severity') return paintOut(SEVERITY_TONES[row.severity.trim()], text)
464
+ if (column === 'package') return paintOut('bold', text)
465
+ if (column === 'reason' && why) return text.replaceAll(/(?<=^| )(?:->|\.\.\.)(?= |$)/gu, (arrow) => paintOut(DIM, arrow))
466
+ return text
467
+ }
468
+ // Under --why the reason cell is a list of `consumer: path` lines; let it span
469
+ // multiple physical rows instead of collapsing to one.
470
+ const table = formatTable(rows.map((row) => ({ ...row, severity: badge(severityOf(row), severityOf(row), coloredOut), ...(icons && { id: `${' '.repeat(ICON_CELLS)} ${row.id}` }) })), columns, {
471
+ multiline: why ? ['reason'] : [],
472
+ wrap: WRAPPED,
473
+ width,
474
+ paint,
475
+ paintHeader: (text) => paintOut('bold', text),
476
+ paintFrame: (text) => paintOut(DIM, text),
477
+ })
478
+ out.write(`${icons ? table.split('\n').map((line) => (line.includes('\x1bP') ? ROOM_BELOW + line : line)).join('\n') : table}\n`)
479
+ }
480
+ if (skipped.length > 0) {
481
+ const names = skipped.map((pkg) => `${pkg.ecosystem} ${pkg.name}${pkg.version === undefined ? '' : `@${pkg.version}`}`)
482
+ const pad = Math.max(...names.map((name) => name.length))
483
+ const lines = skipped.map((pkg, i) => ` ${names[i].padEnd(pad)} ${paintErr(DIM, pkg.because)}`)
484
+ err.write(`${rows.length > 0 ? '\n' : ''}${paintErr('yellow', 'Not audited:')}\n${lines.join('\n')}\n`)
485
+ }
486
+ err.write(`${rows.length > 0 || skipped.length > 0 ? '\n' : ''}${scanned}: ${alertStats(rows, paintErr, coloredErr)}\n`)
487
+ printMalware(npmMalware, err, paintErr, coloredErr)
488
+ }
489
+
490
+ // What npm's registry tells of malware in any dependency, whatever --reason (audit()'s `npmMalware`), after
491
+ // the summary, apart from the table: each advisory a line, its package and version, id and title, and the
492
+ // consumers that recorded it.
493
+ function printMalware(rows, err, paint, colored) {
356
494
  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')
495
+ const packages = new Set(rows.map((row) => `${row.package}@${row.installed}`)).size
496
+ const names = rows.map((row) => `${row.package} ${row.installed}`)
497
+ const pad = (list) => Math.max(...list.map((text) => text.length))
498
+ const [nameWidth, idWidth, titleWidth] = [pad(names), pad(rows.map((row) => row.id)), pad(rows.map((row) => row.title))]
499
+ const lines = rows.map((row, i) => ` ${names[i].padEnd(nameWidth)} ${row.id.padEnd(idWidth)} ${row.reason ? `${row.title.padEnd(titleWidth)} ${paint(DIM, row.reason)}` : row.title}`)
500
+ err.write(`\n${paint(SEVERITY_TONES.malware, badge('malware', 'malware', colored))} in ${packages} dependenc${packages === 1 ? 'y' : 'ies'}:\n${lines.join('\n')}\n`)
367
501
  }
package/src/babel.js CHANGED
@@ -141,21 +141,38 @@ function findBabelConfig(projectDir, command) {
141
141
  return configFile
142
142
  }
143
143
 
144
+ // --babel for `stasis bundle --metro`: the project's babel.config.* for each file, loaded as Metro's
145
+ // transformer loads it (its `metro` caller, for `platform`) by @babel/core's `method` (loadPartialConfig,
146
+ // loadOptions) -- the config is project code, run here. -> (file) => what it loads, once a file
147
+ function projectBabelConfig({ projectDir, platform }, method) {
148
+ const configFile = findBabelConfig(projectDir, 'stasis bundle')
149
+ const [babelPath] = resolveFromProject(projectDir, ['@babel/core'], '--babel', 'stasis bundle')
150
+ const babel = require(babelPath)
151
+ const caller = { name: 'metro', bundler: 'metro', platform }
152
+ const byFile = new Map()
153
+ return (file) => {
154
+ if (!byFile.has(file)) {
155
+ try {
156
+ byFile.set(file, babel[method]({ cwd: projectDir, root: projectDir, configFile, filename: file, caller }))
157
+ } catch (cause) {
158
+ throw new Error(`stasis bundle: --babel could not load ${configFile} for ${file}`, { cause })
159
+ }
160
+ }
161
+ return byFile.get(file)
162
+ }
163
+ }
164
+
144
165
  // The file a plugin item names babel-plugin-module-resolver by resolves into this.
145
166
  const MODULE_RESOLVER = /[\\/]node_modules[\\/]babel-plugin-module-resolver[\\/]/u
146
167
 
147
168
  // --babel for `stasis bundle --metro`: the import aliases babel-plugin-module-resolver applies to
148
- // each file under the project's babel.config.*, loaded as Metro's transformer loads it (its `metro`
149
- // caller, for `platform`) -- the config is project code, run here. The plugin's own resolvePath (or
150
- // the config's custom one) maps each specifier, its `cwd` the project's where the config sets none,
151
- // as Metro started there has it. Every instance the config lists, by name or as a value (`require`d
152
- // or imported), counts, in order; one a preset adds doesn't.
169
+ // each file under the project's babel.config.* (projectBabelConfig's partial config). The plugin's own
170
+ // resolvePath (or the config's custom one) maps each specifier, its `cwd` the project's where the
171
+ // config sets none, as Metro started there has it. Every instance the config lists, by name or as a
172
+ // value (`require`d or imported), counts, in order; one a preset adds doesn't.
153
173
  // -> (file, specifier) => the specifier the plugin rewrites it to, else null
154
174
  export function createBabelAliases({ projectDir, platform }) {
155
- const configFile = findBabelConfig(projectDir, 'stasis bundle')
156
- const [babelPath] = resolveFromProject(projectDir, ['@babel/core'], '--babel', 'stasis bundle')
157
- const babel = require(babelPath)
158
- const caller = { name: 'metro', bundler: 'metro', platform }
175
+ const partialConfig = projectBabelConfig({ projectDir, platform }, 'loadPartialConfig')
159
176
  // The project's own copy, which a config passing the plugin as a value passes; none where the
160
177
  // project installs none.
161
178
  let projectPlugin
@@ -170,29 +187,13 @@ export function createBabelAliases({ projectDir, platform }) {
170
187
  if (resolved !== undefined) return MODULE_RESOLVER.test(resolved) ? require(resolved) : null
171
188
  return plugin.value === projectPlugin?.default ? projectPlugin : null
172
189
  }
173
- // file -> [{ resolvePath, options }] of the module-resolver instances its config applies.
174
- const byFile = new Map()
175
- const aliasingOf = (file) => {
176
- if (byFile.has(file)) return byFile.get(file)
177
- let partial
178
- try {
179
- partial = babel.loadPartialConfig({ cwd: projectDir, root: projectDir, configFile, filename: file, caller })
180
- } catch (cause) {
181
- throw new Error(`stasis bundle: --babel could not load ${configFile} for ${file}`, { cause })
182
- }
183
- const instances = []
190
+ return (file, specifier) => {
184
191
  // No partial config: the config ignores the file.
185
- for (const plugin of partial?.options.plugins ?? []) {
192
+ for (const plugin of partialConfig(file)?.options.plugins ?? []) {
186
193
  const module = moduleOf(plugin)
187
194
  if (module === null) continue
188
195
  const options = { cwd: projectDir, ...plugin.options }
189
- instances.push({ resolvePath: typeof options.resolvePath === 'function' ? options.resolvePath : module.resolvePath, options })
190
- }
191
- byFile.set(file, instances)
192
- return instances
193
- }
194
- return (file, specifier) => {
195
- for (const { resolvePath, options } of aliasingOf(file)) {
196
+ const resolvePath = typeof options.resolvePath === 'function' ? options.resolvePath : module.resolvePath
196
197
  const mapped = resolvePath(specifier, file, options)
197
198
  // The first instance mapping it wins: the plugin marks the path it writes resolved, which
198
199
  // later instances leave as it is.
@@ -202,6 +203,16 @@ export function createBabelAliases({ projectDir, platform }) {
202
203
  }
203
204
  }
204
205
 
206
+ // --babel for `stasis bundle --metro`: whether Babel keeps a statement listing types alone in `file`
207
+ // (`import { type A } from` as `import {} from`), as its TypeScript transform does under
208
+ // `onlyRemoveTypeImports`, under the project's babel.config.* (projectBabelConfig's options).
209
+ // -> (file) => boolean
210
+ export function createBabelTypeImportsKept({ projectDir, platform }) {
211
+ const options = projectBabelConfig({ projectDir, platform }, 'loadOptions')
212
+ // No options: the config ignores the file.
213
+ return (file) => (options(file)?.plugins ?? []).some((plugin) => plugin.key === 'transform-typescript' && plugin.options?.onlyRemoveTypeImports === true)
214
+ }
215
+
205
216
  // babel-plugin-module-resolver's path tests, as it writes them.
206
217
  const isRelativePath = (path) => /^\.?\.\//u.test(path)
207
218
  const toLocalPath = (path) => {
@@ -229,16 +240,12 @@ export function createAliases(alias, cwd) {
229
240
  for (const [regExp, substitute] of matchers) {
230
241
  const groups = regExp.exec(specifier)
231
242
  if (groups === null) continue
232
- return rewrite(file, specifier, substitute(groups), cwd)
243
+ // None where the mapping is empty.
244
+ const mapped = substitute(groups)
245
+ if (!mapped) return null
246
+ const rewritten = isRelativePath(mapped) ? toLocalPath(relative(dirname(file), resolve(cwd, mapped)).replace(/\\/gu, '/')) : mapped
247
+ return rewritten === specifier ? null : rewritten
233
248
  }
234
249
  return null
235
250
  }
236
251
  }
237
-
238
- // `mapped`, the alias `specifier` from `file` maps to, as the plugin writes it: none where empty, a
239
- // relative one (`./`, `../`) a path from `cwd` written relative to `file`, any other as it is.
240
- function rewrite(file, specifier, mapped, cwd) {
241
- if (!mapped) return null
242
- const rewritten = isRelativePath(mapped) ? toLocalPath(relative(dirname(file), resolve(cwd, mapped)).replace(/\\/gu, '/')) : mapped
243
- return rewritten === specifier ? null : rewritten
244
- }
@@ -2,6 +2,7 @@ import { createReadStream } from 'node:fs'
2
2
  import { pipeline } from 'node:stream/promises'
3
3
  import { createBrotliDecompress } from 'node:zlib'
4
4
 
5
+ import { trailingJunkError } from '@exodus/stasis-core/brotli'
5
6
  import { Bundle } from '@exodus/stasis-core/bundle'
6
7
  import { assert } from '@exodus/stasis-core/util'
7
8
 
@@ -18,7 +19,7 @@ const untilAborted = (promise, signal) => (signal === undefined ? promise : new
18
19
  // Left in place of each streamed file; fromJSON's contents-free mode requires one per file.
19
20
  const STREAMED = Symbol('streamed')
20
21
 
21
- // Bundle.parse(brotliDecompressSync(source)), without holding the decompressed bytes or the JSON text whole.
22
+ // parseBundleBytes(source), without holding the decompressed bytes or the JSON text whole.
22
23
  // onFile gets each file in stream order, wherever the bundle puts its files, and before the bundle is
23
24
  // validated: what it receives (format included) is provisional until readBundle resolves. See doc/file-formats.md.
24
25
  export async function readBundle(source, { onFile, signal } = {}) {
@@ -46,8 +47,26 @@ export async function readBundle(source, { onFile, signal } = {}) {
46
47
 
47
48
  // Big chunks let zlib's thread pool decompress ahead of the parser; zlib's 16 KiB default runs about 2x slower.
48
49
  const decompress = createBrotliDecompress({ chunkSize: 1024 * 1024, readableHighWaterMark: 4 * 1024 * 1024 })
50
+ // The decoder ends its output at the end of the brotli stream and consumes nothing after it, so
51
+ // the bytes it was given and the bytes it consumed (its bytesWritten) differ by exactly those.
52
+ // Counting them here takes `input` out of pipeline's hands, so this destroys a stream on pipeline's
53
+ // signal, raised on any failure or abort, as pipeline would: one waiting for a chunk holds it open.
54
+ let given = 0
55
+ async function* counted({ signal: stop }) {
56
+ const destroy = () => input.destroy?.()
57
+ stop.addEventListener('abort', destroy, { once: true })
58
+ if (stop.aborted) destroy()
59
+ try {
60
+ for await (const chunk of input) {
61
+ given += Buffer.byteLength(chunk)
62
+ yield chunk
63
+ }
64
+ } finally {
65
+ stop.removeEventListener('abort', destroy)
66
+ }
67
+ }
49
68
  let drained = false
50
- await pipeline(input, decompress, async (chunks) => {
69
+ await pipeline(counted, decompress, async (chunks) => {
51
70
  for await (const chunk of chunks) {
52
71
  parser.write(chunk)
53
72
  for (const [file, contents, format] of pending.splice(0)) {
@@ -58,9 +77,10 @@ export async function readBundle(source, { onFile, signal } = {}) {
58
77
  }
59
78
  drained = true
60
79
  }, { signal }).catch((error) => {
61
- // Once drained, the stream is whole: pipeline reports unread bytes after it as an abort.
62
- if (!drained || signal?.aborted) throw error
80
+ // Output that ends before the input does is a premature close to pipeline: refused just below.
81
+ if (!drained || signal?.aborted || decompress.bytesWritten === given) throw error
63
82
  })
83
+ if (decompress.bytesWritten !== given) throw trailingJunkError()
64
84
 
65
85
  const bundle = Bundle.fromJSON(parser.end(), { contents: !onFile })
66
86
  if (onFile) {