@exodus/stasis 1.0.0-beta.8 → 1.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +7 -2
  2. package/bin/stasis.js +263 -446
  3. package/package.json +38 -13
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +256 -212
  6. package/src/babel.js +119 -82
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +24 -8
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +211 -117
  11. package/src/cmd/bundle-options.js +347 -0
  12. package/src/cmd/bundle.js +1718 -908
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +48 -21
  15. package/src/cmd/output.js +24 -12
  16. package/src/cmd/sbom.js +6 -9
  17. package/src/cmd/tokens.js +60 -0
  18. package/src/define.js +218 -0
  19. package/src/diff.js +73 -129
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +27 -0
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +43 -120
  27. package/src/loaders/cargo.js +301 -808
  28. package/src/loaders/foundry.js +112 -290
  29. package/src/loaders/paths.js +15 -26
  30. package/src/loaders/php.js +221 -475
  31. package/src/loaders/rust.js +518 -1461
  32. package/src/loaders/solidity-ownership.js +51 -142
  33. package/src/loaders/solidity.js +42 -188
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +41 -0
  37. package/src/metro-resolver.js +58 -133
  38. package/src/metro-transformer.js +0 -6
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +8 -7
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +170 -235
  44. package/src/resolve-node.js +33 -46
  45. package/src/resolve-typescript.js +166 -298
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +70 -97
  49. package/src/scan.js +241 -278
  50. package/src/sixel.js +310 -0
  51. package/src/table.js +95 -0
  52. package/src/validate-notes.d.ts +19 -0
  53. package/src/validate-notes.js +72 -0
  54. package/src/validate.d.ts +159 -0
  55. package/src/validate.js +1315 -0
  56. package/src/vfs-bundle/entries.js +54 -46
  57. package/src/vfs-bundle/github.js +115 -92
  58. package/src/vfs-bundle/glob.js +379 -0
  59. package/src/vfs-bundle/lazy.js +99 -0
  60. package/src/vfs-bundle/prisma.js +235 -105
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +434 -259
  63. package/src/vfs-bundle/yarnclean.js +79 -0
  64. package/src/vfs-bundle.js +25 -51
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +79 -231
package/src/diff.js CHANGED
@@ -1,53 +1,40 @@
1
- import { reposAgree, sortPaths } from '@exodus/stasis-core/util'
1
+ import { ATTESTED_METADATA, extSetsEqual, moduleFileKey, REPO_FIELDS, sameGithub, sortPaths, upsert } from '@exodus/stasis-core/util'
2
2
 
3
- // `@exodus/stasis/diff` — compare two parsed stasis artifacts (Bundle/Lockfile) at the module and
4
- // file level. Never reads disk. Comparing across kinds works by reducing every file to the same
5
- // digest space: a lockfile already holds SRI digests; a bundle holds bytes, re-hashed via the
6
- // injected `hash` — so a `hash` function is required whenever a bundle is an operand. Per-file
7
- // encoding (resource:base64 vs text) is read from the bundle's `formats` map.
3
+ // Two parsed artifacts compared by module and file, never reading disk: each file as an SRI digest, a bundle's bytes
4
+ // re-hashed by the injected `hash`.
8
5
 
9
6
  const KINDS = new Set(['lockfile', 'bundle'])
10
7
 
11
- // Reduce one file's recorded value to an SRI digest: lockfile values already are digests; a
12
- // bundle's are re-hashed, with a `resource:base64` file decoded to bytes first (others hash as text).
8
+ // One file's recorded value as an SRI digest: a bundle's re-hashed, a `resource:base64` file decoded first.
13
9
  function digestOf(kind, format, value, hash) {
14
10
  if (kind === 'lockfile') return value
15
11
  if (typeof hash !== 'function') {
16
12
  throw new Error('diff: a `hash` function is required to compare a bundle (its bytes are re-hashed into a lockfile-style digest)')
17
13
  }
18
- if (format === 'resource:base64') return hash(Buffer.from(value, 'base64'))
19
- return hash(value)
14
+ return hash(format === 'resource:base64' ? Buffer.from(value, 'base64') : value)
20
15
  }
21
16
 
22
- const projectPath = (dir, rel) => (dir === '.' ? rel : `${dir}/${rel}`)
17
+ // An artifact from before the `repo` field records none; `github` compares in any case.
18
+ const reposAgree = (a, b) => a === undefined || b === undefined ||
19
+ Object.keys(REPO_FIELDS).every((key) => (key === 'github' ? sameGithub(a.github, b.github) : a[key] === b[key]))
23
20
 
24
- // Project an artifact onto a uniform shape: scope + Map<dir, { name, version, ecosystem, repo,
25
- // files: Map<rel, digest> }>. `input` is { artifact, kind }; `hash` re-hashes bundle bytes only.
21
+ // `{ artifact, kind }` as scope + Map<dir, { name, version, ecosystem, repo, integrity, patch, files: Map<rel, digest> }>.
26
22
  export function normalizeArtifact(input, { hash } = {}) {
27
23
  const { artifact, kind } = input ?? {}
28
- if (!artifact || !KINDS.has(kind)) {
29
- throw new Error("diff: each input must be { artifact, kind } with kind 'lockfile' | 'bundle'")
30
- }
24
+ if (!artifact || !KINDS.has(kind)) throw new Error("diff: each input must be { artifact, kind } with kind 'lockfile' | 'bundle'")
31
25
  const formats = artifact.formats ?? new Map()
32
26
  const modules = new Map()
33
- for (const [dir, { name, version, ecosystem, repo, files }] of artifact.modules) {
34
- const digests = new Map()
35
- for (const [rel, value] of Object.entries(files)) {
36
- digests.set(rel, digestOf(kind, formats.get(projectPath(dir, rel)), value, hash))
37
- }
38
- // v0 bundles record no name/version; normalize undefined -> null for a single "unknown" sentinel.
39
- modules.set(dir, { name: name ?? null, version: version ?? null, ecosystem: ecosystem ?? null, repo, files: digests })
27
+ for (const [dir, { name, version, ecosystem, repo, integrity, patch, files }] of artifact.modules) {
28
+ const digests = new Map(Object.entries(files).map(([rel, value]) => [rel, digestOf(kind, formats.get(moduleFileKey(dir, rel)), value, hash)]))
29
+ // v0 bundles record no name/version: null is the one "unknown".
30
+ modules.set(dir, { name: name ?? null, version: version ?? null, ecosystem: ecosystem ?? null, repo, integrity, patch, files: digests })
40
31
  }
41
- // `executable` decides which files `stasis extract` chmods +x, so a change to it changes the tree
42
- // an artifact produces even when every digest matches -- it has to be part of "did these differ?".
32
+ // `executable` decides what `stasis extract` chmods +x: the tree differs even where every digest matches.
43
33
  return { scope: artifact.config?.scope ?? 'full', modules, executable: artifact.executable ?? new Set() }
44
34
  }
45
35
 
46
- // Diff two `{ artifact, kind }` operands (`left` = baseline/"from", `right` = "to"). Modules are
47
- // compared whole-package; files only within packages present on BOTH sides (a one-sided package is
48
- // reported once as an added/removed module, not per-file). A version/name/repo change is reported
49
- // only when both sides record a non-null value (an artifact from before `repo` records none). `{ imports: true }` also diffs the resolution graphs
50
- // (opt-in; verbose). `hash` is required only when a bundle is an operand.
36
+ // `left` the baseline. Files compare only within modules on BOTH sides (a one-sided module is reported once); a
37
+ // name/version/repo change only where both sides record one. `imports: true` also diffs the resolution graphs.
51
38
  export function diffArtifacts(left, right, { imports = false, hash } = {}) {
52
39
  const L = normalizeArtifact(left, { hash })
53
40
  const R = normalizeArtifact(right, { hash })
@@ -60,37 +47,29 @@ export function diffArtifacts(left, right, { imports = false, hash } = {}) {
60
47
  const filesDiffering = []
61
48
 
62
49
  const summary = (dir, m) => ({ dir, name: m.name, version: m.version, ecosystem: m.ecosystem, files: m.files.size })
63
- const dirs = new Set([...L.modules.keys(), ...R.modules.keys()])
64
- for (const dir of dirs) {
50
+ for (const dir of new Set([...L.modules.keys(), ...R.modules.keys()])) {
65
51
  const l = L.modules.get(dir)
66
52
  const r = R.modules.get(dir)
67
- if (l && !r) {
68
- modulesRemoved.push(summary(dir, l))
69
- continue
70
- }
71
- if (!l && r) {
72
- modulesAdded.push(summary(dir, r))
53
+ if (!l || !r) {
54
+ (l ? modulesRemoved : modulesAdded).push(summary(dir, l ?? r))
73
55
  continue
74
56
  }
75
57
 
76
- const change = { dir, name: r.name ?? l.name }
77
- if (l.version !== null && r.version !== null && l.version !== r.version) {
78
- change.versionChange = { from: l.version, to: r.version }
79
- }
80
- if (l.name !== null && r.name !== null && l.name !== r.name) {
81
- change.nameChange = { from: l.name, to: r.name }
58
+ const changes = {}
59
+ for (const field of ['version', 'name']) {
60
+ if (l[field] !== null && r[field] !== null && l[field] !== r[field]) changes[`${field}Change`] = { from: l[field], to: r[field] }
82
61
  }
83
62
  // A dependency's repo picks the GitHub advisories audit asks, so a change to it is one to review.
84
- if (!reposAgree(l.repo, r.repo)) {
85
- change.repoChange = { from: l.repo, to: r.repo }
63
+ if (!reposAgree(l.repo, r.repo)) changes.repoChange = { from: l.repo, to: r.repo }
64
+ for (const field of ATTESTED_METADATA) {
65
+ if (JSON.stringify(l[field]) !== JSON.stringify(r[field])) changes[`${field}Change`] = { from: l[field], to: r[field] }
86
66
  }
87
- if (change.versionChange || change.nameChange || change.repoChange) modulesChanged.push(change)
67
+ if (Object.keys(changes).length > 0) modulesChanged.push({ dir, name: r.name ?? l.name, ...changes })
88
68
 
89
- const rels = new Set([...l.files.keys(), ...r.files.keys()])
90
- for (const rel of rels) {
69
+ for (const rel of new Set([...l.files.keys(), ...r.files.keys()])) {
91
70
  const lh = l.files.get(rel)
92
71
  const rh = r.files.get(rel)
93
- const path = projectPath(dir, rel)
72
+ const path = moduleFileKey(dir, rel)
94
73
  if (lh !== undefined && rh === undefined) filesRemoved.push(path)
95
74
  else if (lh === undefined && rh !== undefined) filesAdded.push(path)
96
75
  else if (lh !== rh) filesDiffering.push(path)
@@ -98,50 +77,28 @@ export function diffArtifacts(left, right, { imports = false, hash } = {}) {
98
77
  }
99
78
 
100
79
  const byDir = (a, b) => sortPaths(a.dir, b.dir)
101
- modulesAdded.sort(byDir)
102
- modulesRemoved.sort(byDir)
103
- modulesChanged.sort(byDir)
104
- filesAdded.sort(sortPaths)
105
- filesRemoved.sort(sortPaths)
106
- filesDiffering.sort(sortPaths)
107
-
108
- // Execute-bit changes, reported for every path either side marks (not just files in shared
109
- // modules): gaining or losing +x is a real change to the extracted tree, whatever bucket it sits in.
110
- const onlyIn = (a, b) => [...a].filter((file) => !b.has(file)).toSorted(sortPaths)
111
- const executableAdded = onlyIn(R.executable, L.executable)
112
- const executableRemoved = onlyIn(L.executable, R.executable)
113
-
80
+ // Of every path either side marks, not just files in shared modules.
81
+ const onlyIn = (a, b) => [...a.difference(b)].toSorted(sortPaths)
114
82
  const result = {
115
83
  scope: { left: L.scope, right: R.scope },
116
- modules: { added: modulesAdded, removed: modulesRemoved, changed: modulesChanged },
117
- files: { added: filesAdded, removed: filesRemoved, differing: filesDiffering },
118
- executable: { added: executableAdded, removed: executableRemoved },
84
+ modules: { added: modulesAdded.toSorted(byDir), removed: modulesRemoved.toSorted(byDir), changed: modulesChanged.toSorted(byDir) },
85
+ files: { added: filesAdded.toSorted(sortPaths), removed: filesRemoved.toSorted(sortPaths), differing: filesDiffering.toSorted(sortPaths) },
86
+ executable: { added: onlyIn(R.executable, L.executable), removed: onlyIn(L.executable, R.executable) },
119
87
  }
120
88
  if (imports) result.imports = diffImports(left, right)
121
89
  return result
122
90
  }
123
91
 
124
- // The resolution graph an artifact attests, or null when it attests none (a legacy lockfile
125
- // predating attestation); a bundle always carries a possibly-empty map.
126
- function attestedImports({ artifact }) {
127
- return artifact.imports ?? null
128
- }
129
-
130
- // Collapse an imports map (conditions -> parent -> specifier -> target) to (parent, specifier) ->
131
- // Set<target>, unioning over conditions: a static bundle keys every edge under "*" while a runtime
132
- // lockfile uses precise sets, so keying on conditions would report identical resolutions as
133
- // removed+added. Folding conditions away makes them compare equal.
92
+ // (parent, specifier) -> Set<target>, unioned over conditions: a static bundle keys every edge under "*", a runtime
93
+ // lockfile by precise sets, and the two must agree. A `--metro` target may be a { platform: file } map.
134
94
  function resolutionsOf(imports) {
135
95
  const byPair = new Map()
136
96
  for (const [, byParent] of imports) {
137
97
  for (const [parent, specifiers] of byParent) {
138
98
  for (const [specifier, target] of specifiers) {
139
99
  const key = JSON.stringify([parent, specifier])
140
- let entry = byPair.get(key)
141
- if (!entry) byPair.set(key, (entry = { parent, specifier, targets: new Set() }))
142
- // A `--metro` edge target may be a { platform: file } map; fold each platform's file into the set.
143
- if (target instanceof Map) for (const f of target.values()) entry.targets.add(f)
144
- else entry.targets.add(target)
100
+ const { targets } = upsert(byPair, key, () => ({ parent, specifier, targets: new Set() }))
101
+ for (const f of target instanceof Map ? target.values() : [target]) targets.add(f)
145
102
  }
146
103
  }
147
104
  }
@@ -151,15 +108,12 @@ function resolutionsOf(imports) {
151
108
  const cmpStr = (a, b) => (a < b ? -1 : a > b ? 1 : 0)
152
109
  const cmpPair = (a, b) => sortPaths(a.parent, b.parent) || cmpStr(a.specifier, b.specifier)
153
110
  const sortedTargets = (set) => [...set].toSorted(sortPaths)
154
- const sameTargets = (a, b) => a.size === b.size && [...a].every((t) => b.has(t))
155
111
 
156
- // Diff resolution graphs at the (parent, specifier) level (conditions folded away — see
157
- // resolutionsOf). Reports added/removed and changed (resolved on both sides to different targets —
158
- // a redirect a byte diff can't see). When either side doesn't attest imports, the diff is skipped
159
- // and `attested` flags why, rather than reporting one whole graph as added/removed.
160
- export function diffImports(left, right) {
161
- const li = attestedImports(left)
162
- const ri = attestedImports(right)
112
+ // Changed catches a redirect a byte diff can't see. A side attesting no imports (a lockfile predating attestation)
113
+ // skips the diff, `attested` saying which, rather than reporting the other whole graph as added/removed.
114
+ export function diffImports({ artifact: left }, { artifact: right }) {
115
+ const li = left.imports ?? null
116
+ const ri = right.imports ?? null
163
117
  const attested = { left: li !== null, right: ri !== null }
164
118
  if (li === null || ri === null) return { attested, added: [], removed: [], changed: [] }
165
119
 
@@ -173,22 +127,17 @@ export function diffImports(left, right) {
173
127
  const r = R.get(key)
174
128
  if (l && !r) removed.push({ parent: l.parent, specifier: l.specifier, from: sortedTargets(l.targets) })
175
129
  else if (!l && r) added.push({ parent: r.parent, specifier: r.specifier, to: sortedTargets(r.targets) })
176
- else if (!sameTargets(l.targets, r.targets)) {
130
+ else if (!extSetsEqual(l.targets, r.targets)) {
177
131
  changed.push({ parent: l.parent, specifier: l.specifier, from: sortedTargets(l.targets), to: sortedTargets(r.targets) })
178
132
  }
179
133
  }
180
- added.sort(cmpPair)
181
- removed.sort(cmpPair)
182
- changed.sort(cmpPair)
134
+ for (const list of [added, removed, changed]) list.sort(cmpPair)
183
135
  return { attested, added, removed, changed }
184
136
  }
185
137
 
186
- // True when the diff records any module-, file-, or import-level change. Scope alone is
187
- // informational and doesn't count (unattested imports contribute empty lists).
138
+ // Scope alone doesn't count. Only the opt-in `imports` is guarded: a guard on the others would make a renamed facet
139
+ // read as "no differences" and quietly pass a review gate.
188
140
  export function hasDifferences(diff) {
189
- // `executable` is unguarded like modules/files -- diffArtifacts always sets it, and a shape guard
190
- // here would make a renamed facet read as "no differences" and quietly pass a review gate.
191
- // `imports` is genuinely opt-in (the `imports` option), so it keeps its guard.
192
141
  const { modules, files, executable, imports } = diff
193
142
  let n =
194
143
  modules.added.length + modules.removed.length + modules.changed.length +
@@ -198,7 +147,6 @@ export function hasDifferences(diff) {
198
147
  return n > 0
199
148
  }
200
149
 
201
- const KIND_LABEL = { lockfile: 'lockfile', bundle: 'bundle' }
202
150
  const plural = (n) => (n === 1 ? '' : 's')
203
151
  const ident = ({ name, version }) => (name ? (version ? `${name}@${version}` : name) : '')
204
152
 
@@ -210,11 +158,13 @@ const CHANGED = '*'
210
158
  // `owner/name`, its commit and directory where recorded: `owner/name@0123abc (packages/x)`.
211
159
  const repoLabel = ({ github, commit, directory }) => `${github ?? '?'}${commit ? `@${commit.slice(0, 7)}` : ''}${directory ? ` (${directory})` : ''}`
212
160
 
213
- function describeModuleChange({ name, versionChange, nameChange, repoChange }) {
161
+ function describeModuleChange({ name, versionChange, nameChange, repoChange, integrityChange, patchChange }) {
214
162
  const segs = []
215
163
  if (versionChange) segs.push(`${versionChange.from} -> ${versionChange.to}`)
216
164
  if (nameChange) segs.push(`name ${nameChange.from} -> ${nameChange.to}`)
217
165
  if (repoChange) segs.push(`repo ${repoLabel(repoChange.from)} -> ${repoLabel(repoChange.to)}`)
166
+ if (integrityChange) segs.push(`integrity ${integrityChange.from ?? 'none'} -> ${integrityChange.to ?? 'none'}`)
167
+ if (patchChange) segs.push(`patch ${patchChange.from?.integrity ?? 'none'} -> ${patchChange.to?.integrity ?? 'none'}`)
218
168
  return `${name ? `${name} ` : ''}${segs.join(', ')}`
219
169
  }
220
170
 
@@ -223,44 +173,39 @@ const moduleLine = (mark, x) => {
223
173
  return ` ${mark} ${x.dir}${id ? ` ${id}` : ''} (${x.files} file${plural(x.files)})`
224
174
  }
225
175
 
226
- // `parent 'specifier'` — an edge is identified by importer + specifier (conditions folded away).
227
176
  const edgeLabel = (e) => `${e.parent} '${e.specifier}'`
228
- const targetList = (files) => files.join(', ')
229
177
 
230
- // Render the `--stat` summary (module + file change lists with counts, plus import-edge changes
231
- // when present); reports what changed, not the text. `labels` is { left, right, leftKind, rightKind }.
232
- // Returns a string ending in a newline.
178
+ // The `--stat` summary: what changed, not the text.
233
179
  export function formatDiffStat(diff, { left = '(left)', right = '(right)', leftKind, rightKind } = {}) {
234
180
  const { modules: m, files: f, executable: ex, imports: im, scope } = diff
235
- const lines = ['stasis diff --stat']
236
- const label = (kind) => (kind ? `${KIND_LABEL[kind]}, ` : '')
237
- lines.push(` ${REMOVED} ${left} (${label(leftKind)}scope=${scope.left})`)
238
- lines.push(` ${ADDED} ${right} (${label(rightKind)}scope=${scope.right})`)
181
+ const label = (kind) => (kind ? `${kind}, ` : '')
182
+ const lines = [
183
+ 'stasis diff --stat',
184
+ ` ${REMOVED} ${left} (${label(leftKind)}scope=${scope.left})`,
185
+ ` ${ADDED} ${right} (${label(rightKind)}scope=${scope.right})`,
186
+ ]
187
+ const paths = (mark, list) => {
188
+ for (const p of list) lines.push(` ${mark} ${p}`)
189
+ }
239
190
  if (scope.left !== scope.right) {
240
191
  lines.push(` note: scopes differ (${scope.left} vs ${scope.right}); files only the full-scope artifact records show as removed/added`)
241
192
  }
242
193
 
243
- lines.push('')
244
- lines.push(`Modules: ${m.added.length} added, ${m.removed.length} removed, ${m.changed.length} changed`)
194
+ lines.push('', `Modules: ${m.added.length} added, ${m.removed.length} removed, ${m.changed.length} changed`)
245
195
  for (const x of m.removed) lines.push(moduleLine(REMOVED, x))
246
196
  for (const x of m.added) lines.push(moduleLine(ADDED, x))
247
197
  for (const x of m.changed) lines.push(` ${CHANGED} ${x.dir} ${describeModuleChange(x)}`)
248
198
 
249
- lines.push('')
250
- // Counts cover only files within modules present on both sides; a wholly added/removed module's
251
- // files are summarized by its module entry above — hence the "within shared modules" label.
252
- lines.push(`Files (within shared modules): ${f.added.length} added, ${f.removed.length} removed, ${f.differing.length} differing`)
253
- for (const p of f.removed) lines.push(` ${REMOVED} ${p}`)
254
- for (const p of f.added) lines.push(` ${ADDED} ${p}`)
255
- for (const p of f.differing) lines.push(` ${CHANGED} ${p}`)
199
+ lines.push('', `Files (within shared modules): ${f.added.length} added, ${f.removed.length} removed, ${f.differing.length} differing`)
200
+ paths(REMOVED, f.removed)
201
+ paths(ADDED, f.added)
202
+ paths(CHANGED, f.differing)
256
203
 
257
- // Only rendered when something changed: an unchanged execute-bit set is the overwhelming norm,
258
- // and a permanent "0 added, 0 removed" line would be noise in every diff.
204
+ // A permanent "0 added, 0 removed" line would be noise in every diff.
259
205
  if (ex.added.length > 0 || ex.removed.length > 0) {
260
- lines.push('')
261
- lines.push(`Executable: ${ex.added.length} added, ${ex.removed.length} removed`)
262
- for (const p of ex.removed) lines.push(` ${REMOVED} ${p}`)
263
- for (const p of ex.added) lines.push(` ${ADDED} ${p}`)
206
+ lines.push('', `Executable: ${ex.added.length} added, ${ex.removed.length} removed`)
207
+ paths(REMOVED, ex.removed)
208
+ paths(ADDED, ex.added)
264
209
  }
265
210
 
266
211
  if (im) {
@@ -272,13 +217,12 @@ export function formatDiffStat(diff, { left = '(left)', right = '(right)', leftK
272
217
  lines.push(`Imports: ${which} resolutions; skipping import diff`)
273
218
  } else {
274
219
  lines.push(`Imports: ${im.added.length} added, ${im.removed.length} removed, ${im.changed.length} changed`)
275
- for (const e of im.removed) lines.push(` ${REMOVED} ${edgeLabel(e)} -> ${targetList(e.from)}`)
276
- for (const e of im.added) lines.push(` ${ADDED} ${edgeLabel(e)} -> ${targetList(e.to)}`)
277
- for (const e of im.changed) lines.push(` ${CHANGED} ${edgeLabel(e)} ${targetList(e.from)} -> ${targetList(e.to)}`)
220
+ for (const e of im.removed) lines.push(` ${REMOVED} ${edgeLabel(e)} -> ${e.from.join(', ')}`)
221
+ for (const e of im.added) lines.push(` ${ADDED} ${edgeLabel(e)} -> ${e.to.join(', ')}`)
222
+ for (const e of im.changed) lines.push(` ${CHANGED} ${edgeLabel(e)} ${e.from.join(', ')} -> ${e.to.join(', ')}`)
278
223
  }
279
224
  }
280
225
 
281
- lines.push('')
282
- lines.push(hasDifferences(diff) ? 'Artifacts differ' : 'No differences')
226
+ lines.push('', hasDifferences(diff) ? 'Artifacts differ' : 'No differences')
283
227
  return lines.join('\n') + '\n'
284
228
  }
package/src/esbuild.js CHANGED
@@ -1,5 +1 @@
1
- // Public-export adapter: backs `@exodus/stasis/esbuild`. The plugin lives in
2
- // @exodus/stasis-plugins (which depends on @exodus/stasis-core for State /
3
- // resolvePluginState). Internal code uses `@exodus/stasis-plugins/esbuild` directly;
4
- // this file exists only to back the package's `exports` map.
5
1
  export * from '@exodus/stasis-plugins/esbuild'
package/src/flow.js ADDED
@@ -0,0 +1,27 @@
1
+ // --flow (which --metro turns on): Flow type syntax stripped as Metro's Babel (@react-native/babel-preset) strips it,
2
+ // by flow-remove-types -- stasis's own dependency, never the project's, loaded lazily.
3
+ import { createRequire } from 'node:module'
4
+
5
+ // The JS family alone: flow-remove-types would corrupt TS-only constructs. It preserves JSX.
6
+ export const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
7
+
8
+ // Anywhere in the file, as React Native's Babel preset finds it (/@flow/), to parse the file as Flow throughout.
9
+ export const hasFlowPragma = (source) => source.includes('@flow')
10
+
11
+ // In a file marked `@noflow`, flow-remove-types reads syntax both Flow and JS can read (`f<T>(x)`, `a < b > (c)`) as
12
+ // JS (hermes-parser's `flow: 'detect'`). NOFLOW_STRIPPED is what it leaves of the comment.
13
+ const NOFLOW = '\n/* @noflow */'
14
+ const NOFLOW_STRIPPED = `\n/* ${' '.repeat('@noflow'.length)} */`
15
+
16
+ let flowRemoveTypes
17
+
18
+ // `source` with its Flow types blanked in place (offsets kept): one marked `@flow` as Flow throughout (`f<T>(x)` a
19
+ // call), any other as a `@noflow` file, marked so past its end. Flow code (`enum`s) stays. Throws where it can't parse.
20
+ export function stripFlowTypes(source, { removeEmptyImports = false } = {}) {
21
+ flowRemoveTypes ??= createRequire(import.meta.url)('flow-remove-types')
22
+ if (hasFlowPragma(source)) return flowRemoveTypes(source, { all: true, removeEmptyImports }).toString()
23
+ if (source.includes('@noflow')) return flowRemoveTypes(source, { removeEmptyImports }).toString()
24
+ const stripped = flowRemoveTypes(source + NOFLOW, { removeEmptyImports }).toString()
25
+ if (!stripped.endsWith(NOFLOW_STRIPPED)) throw new Error('flow-remove-types left the @noflow comment stasis appended')
26
+ return stripped.slice(0, -NOFLOW_STRIPPED.length)
27
+ }
@@ -0,0 +1,265 @@
1
+ import { hash } from 'node:crypto'
2
+ import { basename, dirname, join, relative, resolve } from 'node:path'
3
+
4
+ import { isValidRepoField } from '@exodus/stasis-core/bundle'
5
+ import { parseGithubRepository, readJson, readText } from '@exodus/stasis-core/bundle-util'
6
+ import { diskHost } from '@exodus/stasis-core/host'
7
+ import { hexIntegrity, isNpmDependency, isPlainObject, relativeEscapes, tarballIntegrityOf, toPosix, upsert, withoutBom } from '@exodus/stasis-core/util'
8
+ import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
9
+ import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
10
+
11
+ // What the package manager that laid out a node_modules recorded there; one missing or unreadable names nothing.
12
+
13
+ function githubCommit(repo, commit) {
14
+ const github = parseGithubRepository(repo)
15
+ return github !== null && isValidRepoField('commit', commit) ? { github, commit } : undefined
16
+ }
17
+
18
+ const CODELOAD = /^https:\/\/codeload\.github\.com\/([^/]+\/[^/]+)\/tar\.gz\/([^/]+)$/u
19
+ const githubTarball = (url) => {
20
+ const [, github, commit] = CODELOAD.exec(url) ?? []
21
+ return isValidRepoField('github', github) ? githubCommit(github, commit) : undefined
22
+ }
23
+
24
+ export const resolutionCommit = (resolution) => {
25
+ if (resolution?.type === 'git') return githubCommit(resolution.repo, resolution.commit)
26
+ return resolution?.type === 'tarball' ? githubTarball(resolution.tarball ?? '') : undefined
27
+ }
28
+
29
+ export const patchIntegrity = (patchHash) => (/^[\da-f]{64}$/u.test(patchHash ?? '') ? hexIntegrity('sha256', patchHash) : undefined)
30
+
31
+ const repoAt = (commit, path = '') => {
32
+ const directory = path.replace(/^\/+|\/+$/gu, '')
33
+ return commit && { github: commit.github, ...(isValidRepoField('directory', directory) && { directory }), commit: commit.commit }
34
+ }
35
+
36
+ const NONE = () => undefined
37
+
38
+ // A git dependency's `resolved` is the URL npm clones, `#` and the commit.
39
+ const npmCommit = (resolved) => {
40
+ if (typeof resolved !== 'string') return undefined
41
+ if (!/^git(?:\+[\w.-]+)?:/u.test(resolved)) return githubTarball(resolved)
42
+ const at = resolved.lastIndexOf('#')
43
+ return at === -1 ? undefined : githubCommit(resolved.slice(0, at), resolved.slice(at + 1))
44
+ }
45
+
46
+ const unreadable = (file, cause) => new Error(`${file}: ${cause?.message ?? 'not a lockfile'}`, { cause })
47
+
48
+ function parseLock(file, host, strict, parse) {
49
+ const text = readText(host, file)
50
+ if (text === null && strict && host.stat(file) !== null) throw new Error(`${file}: can't be read`)
51
+ if (text === null) return undefined
52
+ try {
53
+ return parse(withoutBom(text))
54
+ } catch (cause) {
55
+ if (strict) throw unreadable(file, cause)
56
+ return undefined
57
+ }
58
+ }
59
+
60
+ export const nameAt = (location) => location.slice(location.lastIndexOf('node_modules/') + 'node_modules/'.length)
61
+
62
+ // By name and version, or `name@` (a directory's): of two that differ, either may be the one installed, so its version
63
+ // alone, `ambiguous`. Of `location`'s package by its own `name` (an alias's), else the name it is installed as.
64
+ function byId(entries) {
65
+ const records = new Map()
66
+ for (const [id, record] of entries) {
67
+ const known = records.get(id)
68
+ records.set(id, known === undefined || JSON.stringify(known) === JSON.stringify(record) ? record : { version: record.version, ambiguous: true })
69
+ }
70
+ const find = (name, version) => records.get(`${name}@${version}`) ?? records.get(`${name}@`)
71
+ return (location, version, name) => (name === undefined ? undefined : find(name, version)) ?? find(nameAt(location), version)
72
+ }
73
+
74
+ function npmLocks(file, host, strict) {
75
+ const parsed = parseLock(file, host, strict, (text) => ({ packages: JSON.parse(text).packages }))
76
+ if (parsed === undefined) return undefined
77
+ const { packages } = parsed
78
+ if (!isPlainObject(packages)) {
79
+ if (strict) throw unreadable(file)
80
+ return undefined
81
+ }
82
+ return (location) => {
83
+ const entry = Object.hasOwn(packages, location) ? packages[location] : undefined
84
+ return isPlainObject(entry) ? { version: entry.version, repo: repoAt(npmCommit(entry.resolved)), integrity: entry.integrity } : undefined
85
+ }
86
+ }
87
+
88
+ const resolutionOf = (resolution) => (resolution?.type === 'git' ? `${resolution.repo}#${resolution.commit}` : resolution?.sha1 === undefined ? resolution?.tarball : `${resolution.tarball}#${resolution.sha1}`)
89
+
90
+ const yarnEntries = (file, host, strict) => parseLock(file, host, strict, (text) => {
91
+ const entries = new Set(Object.values(parseYarn1Lockfile(text, { checkVersions: false }).packages))
92
+ return [...entries].map(({ patterns, name, version, resolution }) => ({
93
+ id: `${name}@${version}`,
94
+ patterns,
95
+ record: { version, resolved: resolutionOf(resolution), repo: repoAt(resolutionCommit(resolution)), integrity: resolution?.integrity },
96
+ }))
97
+ })
98
+
99
+ // As pnpm 10 to 12's depPathToFilename names `key`'s store directory, and the `_<hash>` one cut short ends with.
100
+ const storeNames = (key) => {
101
+ let name = key.replace(/[\\/:*?"<>|#]/gu, '+')
102
+ if (name.includes('(')) name = name.replace(/\)$/u, '').replace(/\)\(|\(|\)/gu, '_')
103
+ return [name, `_${hash('sha256', name).slice(0, 32)}`]
104
+ }
105
+ export const STORE_HASH = /_[\da-f]{32}$/u
106
+
107
+ function pnpmLocks(file, host, strict) {
108
+ const parsed = parseLock(file, host, strict, (text) => ({ lockfile: parsePnpmLockfile(text).lockfile }))
109
+ if (parsed === undefined) return undefined
110
+ const { lockfile } = parsed
111
+ const stored = new Map()
112
+ const keyed = new Map()
113
+ const ids = []
114
+ for (const [key, { name, version, resolution, patchHash }] of Object.entries(lockfile?.packages ?? {})) {
115
+ const record = { version, repo: repoAt(resolutionCommit(resolution), resolution?.path), integrity: resolution?.integrity, patchHash }
116
+ for (const store of storeNames(key)) stored.set(store, record)
117
+ keyed.set(key, record)
118
+ ids.push([`${name}@${version ?? ''}`, record])
119
+ }
120
+ // By the name it is installed as at a top-level location (an alias's or an override's too); none where two differ.
121
+ const top = new Map()
122
+ for (const importer of Object.values(lockfile?.importers ?? {})) {
123
+ for (const [as, key] of [importer.dependencies, importer.devDependencies, importer.optionalDependencies].flatMap((deps) => Object.entries(deps ?? {}))) {
124
+ const record = keyed.get(key)
125
+ if (record) top.set(as, top.has(as) && top.get(as) !== record ? undefined : record)
126
+ }
127
+ }
128
+ const named = byId(ids)
129
+ return {
130
+ stored: (name) => stored.get(name) ?? stored.get(STORE_HASH.exec(name)?.[0]),
131
+ named: (location, ...of) => (location === `node_modules/${nameAt(location)}` ? top.get(nameAt(location)) : undefined) ?? named(location, ...of),
132
+ }
133
+ }
134
+
135
+ const MANAGERS = [
136
+ {
137
+ marker: '.package-lock.json',
138
+ lockfiles: ['npm-shrinkwrap.json', 'package-lock.json'],
139
+ installed: (dir, host) => {
140
+ const locks = npmLocks(join(dir, 'node_modules', '.package-lock.json'), host)
141
+ return locks && ((location, version) => {
142
+ const record = locks(location)
143
+ return record?.version === version ? record : undefined
144
+ })
145
+ },
146
+ locks: (file, host) => npmLocks(file, host, true),
147
+ none: () => 'it locks none there',
148
+ },
149
+ {
150
+ marker: '.yarn-integrity',
151
+ lockfiles: ['yarn.lock'],
152
+ installed: (dir, host) => {
153
+ const integrity = readJson(join(dir, 'node_modules', '.yarn-integrity'), host)?.lockfileEntries
154
+ const entries = isPlainObject(integrity) && yarnEntries(join(dir, 'yarn.lock'), host)
155
+ if (!entries) return undefined
156
+ // What .yarn-integrity has of an entry installed otherwise: a changed yarn.lock often moves a git dependency's commit.
157
+ const installedAs = ({ patterns, record }) => patterns.map((pattern) => (Object.hasOwn(integrity, pattern) ? integrity[pattern] : 'none')).find((resolved) => resolved !== record.resolved)
158
+ return byId(entries.map((entry) => {
159
+ const resolved = installedAs(entry)
160
+ return [entry.id, resolved === undefined ? entry.record : { version: entry.record.version, resolved }]
161
+ }))
162
+ },
163
+ locks: (file, host) => byId(yarnEntries(file, host, true).map(({ id, record }) => [id, record])),
164
+ none: (version) => `${version}; it locks none`,
165
+ },
166
+ {
167
+ marker: '.modules.yaml',
168
+ lockfiles: ['pnpm-lock.yaml'],
169
+ installed: (dir, host) => pnpmLocks(join(dir, 'node_modules', '.pnpm', 'lock.yaml'), host)?.named,
170
+ locks: (file, host) => pnpmLocks(file, host, true).named,
171
+ none: (version) => `${version}; it locks none`,
172
+ },
173
+ ]
174
+
175
+ // Null where there is neither record nor lockfile. Of several lockfiles, the one whose package manager alone left its
176
+ // record, else refused; of several records, none is read, as which was last is not known.
177
+ function layoutOf(dir, host) {
178
+ const marked = MANAGERS.filter(({ marker }) => host.stat(join(dir, 'node_modules', marker)) !== null)
179
+ const locking = MANAGERS.flatMap((manager) => manager.lockfiles.map((name) => join(dir, name)).filter((file) => host.stat(file) !== null).slice(0, 1).map((file) => [manager, file]))
180
+ if (marked.length === 0 && locking.length === 0) return null
181
+ const held = locking.length > 1 ? locking.filter(([manager]) => marked.length === 1 && marked[0] === manager) : locking
182
+ if (locking.length > 1 && held.length === 0) throw new Error(`${dir}: ${locking.map(([, file]) => basename(file)).join(' and ')} are both there, and which installed its node_modules isn't known`)
183
+ const [manager = marked.length === 1 ? marked[0] : undefined, lockfile] = held[0] ?? []
184
+ const installed = (marked.length === 1 && marked[0] === manager && manager.installed(dir, host)) || NONE
185
+ return { installed, lockfile, locks: lockfile && manager.locks(lockfile, host), none: manager?.none }
186
+ }
187
+
188
+ // pnpm's, where it isolates: `store`'s lock.yaml held to the pnpm-lock.yaml of the directory the store is in.
189
+ function storeOf(store, host) {
190
+ const lockfile = join(dirname(dirname(store)), 'pnpm-lock.yaml')
191
+ return { installed: pnpmLocks(join(store, 'lock.yaml'), host)?.stored ?? NONE, lockfile, locks: pnpmLocks(lockfile, host, true)?.stored, none: () => 'it locks none there' }
192
+ }
193
+
194
+ const commitLabel = (repo) => (repo ? `${repo.github}${repo.directory ? `/${repo.directory}` : ''}@${repo.commit}` : 'none')
195
+
196
+ function lockDiffers(version, installed, locked, none) {
197
+ if (locked === undefined) return none(version)
198
+ if (locked.version !== undefined && locked.version !== version) return `${version}; it locks ${locked.version}`
199
+ if (installed === undefined) return undefined
200
+ if (installed.resolved !== undefined && installed.resolved !== locked.resolved) return `installed from ${installed.resolved}; it locks ${locked.resolved ?? 'none'}`
201
+ if (commitLabel(installed.repo) !== commitLabel(locked.repo)) return `at ${commitLabel(installed.repo)}; it locks ${commitLabel(locked.repo)}`
202
+ if (tarballIntegrityOf(installed.integrity) !== tarballIntegrityOf(locked.integrity)) return `installed with ${installed.integrity ?? 'no integrity'}; it locks ${locked.integrity ?? 'none'}`
203
+ if (installed.patchHash !== locked.patchHash) return `installed with the patch ${installed.patchHash ?? 'none'}; it locks ${locked.patchHash ?? 'none'}`
204
+ return undefined
205
+ }
206
+
207
+ const PNPM_PACKAGE = /^(.*\/node_modules\/\.pnpm)\/([^/]+)\/node_modules\/(?:@[^/]+\/)?[^/]+$/u
208
+
209
+ // An npm tarball's integrity is its sha512 alone; only an npm dependency has a patch.
210
+ export function installedMetadata(dir, ecosystem, { repo, integrity, patchHash } = {}) {
211
+ const npm = isNpmDependency(dir, ecosystem)
212
+ const tarball = npm ? tarballIntegrityOf(integrity) : integrity
213
+ const patch = npm ? patchIntegrity(patchHash) : undefined
214
+ return { ...(repo && { repo }), ...(tarball && { integrity: tarball }), ...(patch && { patch: { integrity: patch } }) }
215
+ }
216
+
217
+ export function installedRecords(modules, root, host = diskHost, { stores = new Map(), layouts = new Map() } = {}) {
218
+ const accounts = new Map()
219
+ let top // the bundle root's real path, as each package's is compared with it
220
+ const layoutAt = (dir) => upsert(layouts, dir, () => layoutOf(dir, host))
221
+ for (const [dir, info] of modules) {
222
+ // An untagged bucket under node_modules is npm's too (an artifact from before the tag).
223
+ if (!isNpmDependency(dir, info.ecosystem)) continue
224
+ let real
225
+ try {
226
+ real = toPosix(host.realpath(resolve(root, dir)))
227
+ } catch {
228
+ continue
229
+ }
230
+ let layout
231
+ let at
232
+ const pnpm = PNPM_PACKAGE.exec(real)
233
+ if (pnpm !== null) {
234
+ const [, store, name] = pnpm
235
+ ;[layout, at] = [upsert(stores, store, () => storeOf(store, host)), [name]]
236
+ } else {
237
+ try {
238
+ top ??= resolve(host.realpath(resolve(root)))
239
+ } catch {
240
+ return accounts
241
+ }
242
+ const rel = relative(top, real)
243
+ const parts = toPosix(rel).split('/')
244
+ const segment = parts.indexOf('node_modules')
245
+ if (relativeEscapes(rel) || segment === -1) continue
246
+ let from = resolve(top, ...parts.slice(0, segment))
247
+ while (layoutAt(from) === null && from !== top) from = dirname(from)
248
+ ;[layout, at] = [layoutAt(from) ?? { installed: NONE }, [toPosix(relative(from, real)), info.version, info.name]]
249
+ }
250
+ const { installed, lockfile, locks, none } = layout
251
+ const record = installed(...at)
252
+ const locked = locks?.(...at)
253
+ const differs = locks && lockDiffers(info.version, record, locked, none)
254
+ if (differs) throw new Error(`${lockfile} doesn't lock ${dir} as installed (${differs}): install from it again`)
255
+ const told = record ?? locked
256
+ if (told !== undefined && !told.ambiguous) accounts.set(dir, { ...installedMetadata(dir, info.ecosystem, told), ...(record === undefined && { locked: true }) })
257
+ }
258
+ return accounts
259
+ }
260
+
261
+ export function pinInstalled(bundle, root, host = diskHost) {
262
+ const accounts = installedRecords(bundle.modules, root, host)
263
+ for (const [dir, { locked, ...metadata }] of accounts) if (!locked) bundle.modules.set(dir, { ...bundle.modules.get(dir), ...metadata })
264
+ return accounts
265
+ }