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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +1 -1
  2. package/bin/stasis.js +26 -11
  3. package/package.json +10 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +113 -233
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +50 -166
  11. package/src/cmd/bundle-options.js +110 -167
  12. package/src/cmd/bundle.js +861 -1344
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +46 -27
  15. package/src/cmd/output.js +17 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +10 -23
  18. package/src/define.js +34 -97
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +10 -25
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +9 -33
  37. package/src/metro-resolver.js +20 -47
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +2 -5
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +90 -220
  44. package/src/resolve-node.js +27 -45
  45. package/src/resolve-typescript.js +109 -313
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +52 -128
  49. package/src/scan.js +136 -283
  50. package/src/sixel.js +18 -41
  51. package/src/table.js +14 -35
  52. package/src/validate-notes.d.ts +3 -2
  53. package/src/validate-notes.js +25 -19
  54. package/src/validate.d.ts +38 -11
  55. package/src/validate.js +478 -512
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +88 -121
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  60. package/src/vfs-bundle/prisma.js +167 -110
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +75 -231
  67. package/src/installed-commits.js +0 -184
@@ -2,14 +2,16 @@ import { constants } from 'node:fs'
2
2
  import { basename, dirname, join, posix, relative, resolve, sep } from 'node:path'
3
3
 
4
4
  import { isValidRepoField } from '@exodus/stasis-core/bundle'
5
- import { NO_ENTRY, invalidPackageConfig, lexists, packageJSONStat, packageJSONText, parseGithubRepository, readJson } from '@exodus/stasis-core/bundle-util'
5
+ import { NO_ENTRY, invalidPackageConfig, lexists, packageJSONStat, packageJSONText, parseGithubRepository, readJson, readText } from '@exodus/stasis-core/bundle-util'
6
6
  import { byName } from '@exodus/stasis-core/host'
7
- import { hasNodeModulesSegment, isPlainObject } from '@exodus/stasis-core/util'
7
+ import { archiveIntegrity, CONFIG_FILE, hasNodeModulesSegment, isPathWithin, isPlainObject, posixPathEscapes, tarballIntegrityOf, upsert } from '@exodus/stasis-core/util'
8
8
  import { buildNpmTree, findNpmWorkspaces } from '@preventive/deptree/npm.js'
9
9
  import { buildPnpmTree, findPnpmProjects } from '@preventive/deptree/pnpm.js'
10
10
  import { LockfileError, TomlError, buildSoldeerTree } from '@preventive/deptree/soldeer.js'
11
11
  import { buildYarn1Tree, findYarn1Workspaces } from '@preventive/deptree/yarn1.js'
12
+ import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
12
13
  import { VfsError } from '@preventive/vfs'
14
+ import { installedMetadata } from '../installed.js'
13
15
  import { createNodeResolver } from '../resolve-node.js'
14
16
  import { globSync, matchesGlob } from './glob.js'
15
17
  import { lazyView, untouchedMemo } from './lazy.js'
@@ -17,18 +19,6 @@ import { settleSoldeer } from './soldeer.js'
17
19
  import { YARNCLEAN, yarnclean } from './yarnclean.js'
18
20
  import { isDir, isFile } from '../resolve-typescript.js'
19
21
 
20
- // A project's dependencies laid out in memory from its lockfile by @preventive/deptree, as `pnpm
21
- // install --frozen-lockfile --ignore-scripts` lays out its node_modules with pnpm 9, 10, 11 or 12,
22
- // `yarn install --frozen-lockfile --ignore-scripts` with yarn 1.22, `npm ci --ignore-scripts` with
23
- // npm 10.9 or 11, or `soldeer install` its dependencies folder with Soldeer 0.12, and the host that
24
- // reads the project through them; without devDependencies, as `pnpm install --prod`, `yarn install
25
- // --production` and `npm ci --omit=dev` lay it out, where the project's stasis.config.json says so.
26
- // node_modules is then cleaned as `yarn autoclean` would clean it by each .yarnclean in the
27
- // directory installed or above it (yarnclean.js), whatever installs it. The project is read through
28
- // a host it is given, and nothing else. pnpm's and npm's trees may be laid out lazily, each package
29
- // unpacked once asked for (lazy.js).
30
-
31
- // cwd or the nearest of its ancestors that `holds`, or null.
32
22
  export function nearest(cwd, holds) {
33
23
  for (let dir = cwd; ; dir = dirname(dir)) {
34
24
  if (holds(dir)) return dir
@@ -36,31 +26,30 @@ export function nearest(cwd, holds) {
36
26
  }
37
27
  }
38
28
 
39
- // Whether a directory holds a file of one of `names`.
40
29
  export const holding = (host, ...names) => (dir) => names.some((name) => isFile(join(dir, name), host))
41
30
 
42
- // The directory of the package cwd is in that is none of `projects` (their directories from
43
- // `root`), or null: the nearest package.json with a name between cwd and `root`, one without being a
44
- // `type` marker.
31
+ // An unreadable package.json is a package all the same; a nameless one is a `type` marker.
32
+ function holdsPackage(host, dir) {
33
+ if (!isFile(join(dir, 'package.json'), host)) return false
34
+ const json = readJson(join(dir, 'package.json'), host)
35
+ return json === null || json.name !== undefined
36
+ }
37
+
45
38
  function outsider(host, root, cwd, projects) {
46
39
  for (let dir = cwd; dir !== root; dir = dirname(dir)) {
47
- const file = join(dir, 'package.json')
48
- if (!isFile(file, host)) continue
40
+ if (!isFile(join(dir, 'package.json'), host)) continue
49
41
  if (projects.has(relative(root, dir))) return null
50
- const json = readJson(file, host) // unreadable, a package all the same
51
- if (json === null || json.name !== undefined) return dir
42
+ if (holdsPackage(host, dir)) return dir
52
43
  }
53
44
  return null
54
45
  }
55
46
 
56
- // `promise`, a refusal of @preventive/lockfile's naming `file` (deptree names the files it reads but
57
- // for yarn.lock and soldeer.lock).
47
+ // deptree's refusals name no file for yarn.lock and soldeer.lock.
58
48
  const naming = (file, promise) => promise.catch((cause) => {
59
49
  if (!(cause instanceof LockfileError || cause instanceof TomlError)) throw cause
60
50
  throw new Error(`${file}: ${cause.message}`, { cause })
61
51
  })
62
52
 
63
- // The directory `root` as `host` holds it, by paths from `/`, as deptree reads a project.
64
53
  export function projectView(host, root) {
65
54
  const at = (p) => (p === '/' ? root : join(root, p))
66
55
  const typeOf = (st) => (st.isDirectory() ? 'directory' : st.isFile() ? 'file' : 'other')
@@ -81,58 +70,41 @@ export function projectView(host, root) {
81
70
  }
82
71
  }
83
72
 
84
- // What pnpm matches packages' `libc` against (as deptree's host.libc takes it).
85
- const LIBCS = new Set(['glibc', 'musl', 'unknown'])
86
-
87
- // pnpm's libc, detect-libc's familySync, by its check of Node's report alone (it reads
88
- // /usr/bin/ldd first): unknown where that can't tell, which pnpm then installs everything for.
89
- // The report leaves out the network, whose reverse DNS can stall.
90
- let machineLibc
91
- function currentLibc() {
92
- if (machineLibc !== undefined) return machineLibc
93
- machineLibc = 'unknown'
94
- if (process.platform !== 'linux' || !process.report) return machineLibc
73
+ // detect-libc's familySync by the report alone, 'unknown' (pnpm installs all) where it can't tell; no reverse DNS to stall.
74
+ function reportedLibc() {
75
+ if (process.platform !== 'linux' || !process.report) return 'unknown'
95
76
  const { excludeNetwork } = process.report
96
77
  try {
97
78
  process.report.excludeNetwork = true
98
79
  const report = process.report.getReport()
99
- if (report.header?.glibcVersionRuntime) machineLibc = 'glibc'
100
- else if (report.sharedObjects?.some((file) => file.includes('libc.musl-') || file.includes('ld-musl-'))) machineLibc = 'musl'
80
+ if (report.header?.glibcVersionRuntime) return 'glibc'
81
+ if (report.sharedObjects?.some((file) => file.includes('libc.musl-') || file.includes('ld-musl-'))) return 'musl'
101
82
  } catch { /* unknown */ } finally {
102
83
  process.report.excludeNetwork = excludeNetwork
103
84
  }
104
- return machineLibc
85
+ return 'unknown'
105
86
  }
87
+ let machineLibc
88
+ const currentLibc = () => (machineLibc ??= reportedLibc())
106
89
 
107
- const machine = () => ({ node: process.versions.node, os: process.platform, cpu: process.arch })
108
-
109
- // The machine a tree is laid out for: this one, but for the `os`, `cpu` and `libc` given. A libc is
110
- // detected only for this machine's os; for another, unless given, pnpm matches none ('unknown').
90
+ // A libc is detected only for this machine's os; for another, unless given, pnpm matches none ('unknown').
111
91
  const target = ({ os, cpu, libc } = {}) => {
112
- const here = machine()
113
- const forOs = os ?? here.os
114
- return { node: here.node, os: forOs, cpu: cpu ?? here.cpu, libc: libc ?? (forOs === here.os ? currentLibc() : 'unknown') }
92
+ const forOs = os ?? process.platform
93
+ return { node: process.versions.node, os: forOs, cpu: cpu ?? process.arch, libc: libc ?? (forOs === process.platform ? currentLibc() : 'unknown') }
115
94
  }
116
95
 
117
- // `os` and `cpu` as Node names them (process.platform, process.arch); `libc` is pnpm's alone, and
118
- // Soldeer takes `os` alone: what a package manager matches nothing against changes nothing.
119
96
  export function checkTarget(name, { os, cpu, libc }) {
120
- for (const [key, value] of Object.entries({ os, cpu })) {
121
- if (value !== undefined && (typeof value !== 'string' || value === '')) throw new TypeError(`${name}: ${key} must be a non-empty string`)
122
- }
123
- if (libc !== undefined && !LIBCS.has(libc)) throw new TypeError(`${name}: libc must be one of 'glibc', 'musl', 'unknown'`)
97
+ const invalid = Object.entries({ os, cpu }).find(([, value]) => value !== undefined && (typeof value !== 'string' || value === ''))
98
+ if (invalid) throw new TypeError(`${name}: ${invalid[0]} must be a non-empty string`)
99
+ if (libc !== undefined && !['glibc', 'musl', 'unknown'].includes(libc)) throw new TypeError(`${name}: libc must be one of 'glibc', 'musl', 'unknown'`)
124
100
  }
125
101
 
126
- // `cache`, where npm packages' tarballs and version documents are kept, as @preventive/upstream's
127
- // CacheOptions take it: left out, in setCacheDir's cache; a store with `read` and `write`, in place
128
- // of it; or false, nowhere. Checked as deptree checks it, before anything is fetched.
129
102
  export function checkCache(name, cache) {
130
103
  if (cache === undefined || cache === false || (typeof cache?.read === 'function' && typeof cache?.write === 'function')) return
131
104
  throw new TypeError(`${name}: cache must be false, or a store with read and write, or left out`)
132
105
  }
133
106
 
134
- // yarn installs a workspace from the root that declares it, whatever yarn.lock is nearer, and any
135
- // other package from the nearest yarn.lock.
107
+ // yarn installs a workspace from the root declaring it, whatever yarn.lock is nearer; any other package from the nearest.
136
108
  function yarn1Root(host, cwd) {
137
109
  const workspaces = (dir) => {
138
110
  const declared = readJson(join(dir, 'package.json'), host)?.workspaces
@@ -143,17 +115,7 @@ function yarn1Root(host, cwd) {
143
115
  return nearest(cwd, holding(host, 'yarn.lock'))
144
116
  }
145
117
 
146
- // Whether `dir` holds a package: a package.json naming one, or unreadable, which is one all the same;
147
- // one with no name is a `type` marker, its files the package's above.
148
- function holdsPackage(host, dir) {
149
- if (!isFile(join(dir, 'package.json'), host)) return false
150
- const json = readJson(join(dir, 'package.json'), host)
151
- return json === null || json.name !== undefined
152
- }
153
-
154
- // The first directory above `dir` whose workspaces, as npm's glob finds them on `os`, take it, as npm
155
- // finds its local prefix; else null. npm takes a falsy declaration for none, and fails on any other
156
- // but a sequence of globs, or yarn's `{ packages: [...] }` of them.
118
+ // As npm finds its local prefix: the first directory above `dir` whose workspaces (npm's glob on `os`) take it.
157
119
  function npmWorkspaceRoot(host, dir, os) {
158
120
  for (let above = dir; dirname(above) !== above;) {
159
121
  above = dirname(above)
@@ -166,11 +128,8 @@ function npmWorkspaceRoot(host, dir, os) {
166
128
  return null
167
129
  }
168
130
 
169
- // npm installs the package cwd is in from npmWorkspaceRoot's, whatever package-lock.json is nearer,
170
- // and past a workspace declaring workspaces of its own -- and any other package from the nearest
171
- // package-lock.json beside a package.json, npm's prefix being no directory without one. A
172
- // package.json with no name is a package where a workspace root takes it, which npm names by its
173
- // directory, and else a `type` marker, its files the package's above.
131
+ // npm installs the package cwd is in from npmWorkspaceRoot's, whatever package-lock.json is nearer; any other from the
132
+ // nearest package-lock.json beside a package.json. A nameless package.json is a package where a workspace root takes it.
174
133
  function npmRoot(host, cwd, os) {
175
134
  for (let dir = cwd; ; dir = dirname(dir)) {
176
135
  if (isFile(join(dir, 'package.json'), host)) {
@@ -180,38 +139,24 @@ function npmRoot(host, cwd, os) {
180
139
  }
181
140
  if (dirname(dir) === dir) break
182
141
  }
183
- return nearest(cwd, (dir) => holding(host, 'package-lock.json')(dir) && holding(host, 'package.json')(dir))
142
+ return nearest(cwd, (dir) => isFile(join(dir, 'package-lock.json'), host) && isFile(join(dir, 'package.json'), host))
184
143
  }
185
144
 
186
- // npm's host: the machine's, but a libc npm finds only as glibc or musl, and none where it finds
187
- // neither.
188
- const npmHost = (npm, given) => {
189
- const { libc, ...host } = target(given)
190
- return { npm, ...host, ...(libc === 'glibc' || libc === 'musl' ? { libc } : {}) }
191
- }
145
+ const noneAbove = (file) => async (names, above) => !(await above()).some((dir) => dir.includes(file))
192
146
 
193
- // What each package manager reproduced installs from: the kind of bundle it installs for; its
194
- // lockfile; the name a root package.json's packageManager pins it by, where one does; the version
195
- // reproduced where nothing pins one; the directory it installs cwd from, which holds the lockfile
196
- // (for the os given, where that changes it); whether a directory holding the lockfile is installed
197
- // from by itself, by the names in it and in each directory above it (`above()`, as listings); the
198
- // projects it finds in a view of that directory, for the machine given; the directory it installs
199
- // in each, and any it hides, as another package manager's; whether it lays out a tree from the
200
- // lockfile alone, which may then be lazy; and the tree, without what devDependencies alone reach
201
- // where `given.production` (Soldeer has no devDependencies, so it leaves out nothing).
202
147
  const PACKAGE_MANAGERS = {
203
148
  pnpm: {
204
149
  kind: 'js',
205
150
  lockfile: 'pnpm-lock.yaml',
206
151
  pin: 'pnpm',
207
152
  version: '10.33.4',
208
- // pnpm installs from the workspace's root, whatever pnpm-lock.yaml is nearer.
153
+ from: 10,
209
154
  root: (host, cwd) => nearest(cwd, holding(host, 'pnpm-workspace.yaml')) ?? nearest(cwd, holding(host, 'pnpm-lock.yaml')),
210
- alone: async (names, above) => !(await above()).some((dir) => dir.includes('pnpm-workspace.yaml')),
155
+ alone: noneAbove('pnpm-workspace.yaml'),
211
156
  projects: (view, pnpm) => findPnpmProjects({ project: view, host: { pnpm } }),
212
157
  installs: 'node_modules',
213
158
  lazy: true,
214
- build: (view, pnpm, _file, given) => buildPnpmTree({ project: view, host: { pnpm, ...target(given) }, cache: given?.cache, lazy: given?.lazy, production: given?.production }),
159
+ build: (view, pnpm, _file, given) => buildPnpmTree({ project: view, host: { pnpm, ...target(given) }, cache: given.cache, lazy: given.lazy, production: given.production }),
215
160
  },
216
161
  yarn1: {
217
162
  kind: 'js',
@@ -219,13 +164,14 @@ const PACKAGE_MANAGERS = {
219
164
  pin: 'yarn',
220
165
  version: '1.22.22',
221
166
  root: yarn1Root,
222
- // A package.json above may declare it a workspace.
223
- alone: async (names, above) => !(await above()).some((dir) => dir.includes('package.json')),
167
+ alone: noneAbove('package.json'),
168
+ // yarn 1 fetches an entry locking a sha1 alone by the registry's sha512.
169
+ integrities: (text) => new Set(Object.values(parseYarn1Lockfile(text, { checkVersions: false }).packages).map(({ resolution }) => tarballIntegrityOf(resolution?.integrity)).filter(Boolean)),
224
170
  projects: (view) => findYarn1Workspaces({ project: view }),
225
171
  installs: 'node_modules',
226
172
  build: (view, yarn, file, given) => {
227
173
  const { libc: _, ...host } = target({ ...given, libc: 'unknown' }) // yarn 1 matches no libc
228
- return naming(file, buildYarn1Tree({ project: view, host: { yarn, ...host }, cache: given?.cache, production: given?.production }))
174
+ return naming(file, buildYarn1Tree({ project: view, host: { yarn, ...host }, cache: given.cache, production: given.production }))
229
175
  },
230
176
  },
231
177
  npm: {
@@ -234,54 +180,45 @@ const PACKAGE_MANAGERS = {
234
180
  // npm reads no packageManager, so nothing pins it.
235
181
  version: '11.21.0',
236
182
  root: npmRoot,
237
- // A package.json above may declare it a workspace.
238
- alone: async (names, above) => !(await above()).some((dir) => dir.includes('package.json')),
183
+ alone: noneAbove('package.json'),
239
184
  projects: (view, _npm, given) => findNpmWorkspaces({ project: view, os: target(given).os }),
240
185
  installs: 'node_modules',
241
186
  lazy: true,
242
- build: (view, npm, file, given) => naming(file, buildNpmTree({ project: view, host: npmHost(npm, given), cache: given?.cache, lazy: given?.lazy, production: given?.production })),
187
+ build: (view, npm, file, given) => {
188
+ const { libc, ...host } = target(given)
189
+ const found = (libc === 'glibc' || libc === 'musl') && { libc } // npm finds a libc only as glibc or musl
190
+ return naming(file, buildNpmTree({ project: view, host: { npm, ...host, ...found }, cache: given.cache, lazy: given.lazy, production: given.production }))
191
+ },
243
192
  },
244
193
  soldeer: {
245
194
  kind: 'sol',
246
195
  lockfile: 'soldeer.lock',
247
196
  version: '0.12.0',
248
- // Soldeer installs from the nearest directory holding foundry.toml or soldeer.toml, never above
249
- // the git repository's root; without one, from that root, else from cwd.
197
+ // Never above the git repository's root.
250
198
  root: (host, cwd) => nearest(cwd, (dir) => holding(host, 'foundry.toml', 'soldeer.toml')(dir) || isDir(join(dir, '.git'), host)) ?? cwd,
251
199
  alone: async (names, above) => names.includes('foundry.toml') || names.includes('soldeer.toml') || !(await above()).some((dir) => dir.includes('foundry.toml') || dir.includes('soldeer.toml')),
252
200
  projects: () => ['.'],
253
201
  installs: 'dependencies',
254
202
  hides: 'node_modules',
255
203
  async build(view, soldeer, file, given) {
256
- const tree = await naming(file, buildSoldeerTree({ project: view, host: { soldeer, os: given?.os ?? process.platform }, github: given?.client }))
204
+ const tree = await naming(file, buildSoldeerTree({ project: view, host: { soldeer, os: given.os ?? process.platform }, github: given.client }))
257
205
  settleSoldeer(view, tree.vfs, dirname(file))
258
206
  return tree
259
207
  },
260
208
  },
261
209
  }
262
210
 
263
- // Their names.
264
211
  const PACKAGE_MANAGER_NAMES = Object.keys(PACKAGE_MANAGERS)
265
212
 
266
- // layOutTree's tree, with the host reading `project` through it (treeHost), but a lazy one's, whose
267
- // hosts are made for each round that reads it.
268
213
  export async function loadTree(options) {
269
- const tree = await layOutTree(options)
270
- return tree.lazy === undefined ? { ...tree, host: treeHost(tree, options.project) } : tree
214
+ const tree = await buildTree(planTree(options))
215
+ return { ...tree, host: treeHost(tree, options.project) }
271
216
  }
272
217
 
273
- // The host reading `project`, a host of the project's Vfs, through `tree` (layOutTree's): the tree
274
- // serves what the package manager installs, and any file it writes beside it at the root. A lazy
275
- // tree is read as lazyView reads it, into the round's `reads`, caching nothing it reads of a package
276
- // not unpacked yet: each such read reaches it, so that what a file's scan reads tells whether it
277
- // touched one. Its `probing(fn)` is `fn()`, its reads taken as probes of what files a package holds
278
- // (lazyView), which read none of its code.
279
218
  export function treeHost(tree, project, reads) {
280
- const pm = PACKAGE_MANAGERS[tree.packageManager]
281
- const files = tree.vfs.readdir('/').filter((name) => tree.vfs.lstat(`/${name}`).type === 'file')
282
- const installs = [...[...tree.projects].map((dir) => join(dir, pm.installs)), ...files]
283
- if (tree.lazy === undefined) return vfsHost(tree.vfs, { root: tree.root, outside: project, installs, hides: pm.hides })
284
- const host = vfsHost(lazyView(tree.vfs, { ...tree.lazy, reads }), { root: tree.root, outside: project, installs, hides: pm.hides, touches: () => reads.touches, retouch: () => reads.touches++ })
219
+ const { installs, hides } = tree.trees === undefined ? { installs: installsOf(tree), hides: hiding(PACKAGE_MANAGERS[tree.packageManager].hides) } : tree
220
+ if (tree.lazy === undefined) return vfsHost(tree.vfs, { root: tree.root, outside: project, installs, hides })
221
+ const host = vfsHost(lazyView(tree.vfs, { ...tree.lazy, reads }), { root: tree.root, outside: project, installs, hides, touches: () => reads.touches, retouch: () => reads.touches++ })
285
222
  host.probing = (fn) => {
286
223
  reads.probing++
287
224
  try {
@@ -293,83 +230,56 @@ export function treeHost(tree, project, reads) {
293
230
  return host
294
231
  }
295
232
 
296
-
297
- // Refused where `packageManager` lays out no tree from the lockfile alone, which `lazy` would have.
298
233
  export function checkLazy(name, packageManager) {
299
234
  if (!PACKAGE_MANAGERS[packageManager].lazy) throw new Error(`${name}: lazy is taken with pnpm and npm alone, which lay out a tree from the lockfile alone; ${packageManager}'s is not`)
300
235
  }
301
236
 
302
- // Whether `packageManager`'s tree of the project `project` holds at `cwd` can be lazy, as layOutTree
303
- // lays it out: pnpm's and npm's, but where a .yarnclean in cwd or above it keeps paths with `!`.
304
237
  export const lazyTaken = (project, packageManager, cwd) => PACKAGE_MANAGERS[packageManager].lazy === true && !yarnclean(project, cwd)?.keeps
305
238
 
306
- // The lockfile `packageManager` installs from, e.g. 'pnpm-lock.yaml'.
307
239
  export const lockfileOf = (packageManager) => PACKAGE_MANAGERS[packageManager].lockfile
308
240
 
309
- // Whether `packageManager` installs a directory holding its lockfile from that directory itself, by
310
- // the names in it and, from `above()`, the names in each directory above it.
311
241
  export const installedAlone = (packageManager, names, above) => PACKAGE_MANAGERS[packageManager].alone(names, above)
312
242
 
313
- // The real path of the directory `host` holds `cwd` (a real path) installed from by `pm`, for `os`,
314
- // where it holds the lockfile; else null. That directory is cwd or above it, so none is without a
315
- // lockfile there.
243
+ // The root is cwd or above it, so none is without a lockfile there.
316
244
  function rootOf(host, pm, cwd, os) {
317
245
  if (nearest(cwd, holding(host, pm.lockfile)) === null) return null
318
246
  const found = pm.root(host, cwd, os)
319
247
  return found !== null && isFile(join(found, pm.lockfile), host) ? host.realpath(found) : null
320
248
  }
321
249
 
322
- // The real path of the directory the project `host` reads holds at `cwd` is installed from, for
323
- // `os`, which holds the lockfile and which a bundle's paths are relative to; null without one.
324
250
  export const lockfileRoot = (host, packageManager, cwd, os) => rootOf(host, PACKAGE_MANAGERS[packageManager], host.realpath(cwd), os)
325
251
 
326
- // The package managers that install node_modules.
327
252
  export const NODE_MODULES_MANAGERS = PACKAGE_MANAGER_NAMES.filter((name) => PACKAGE_MANAGERS[name].installs === 'node_modules')
328
253
 
329
- // Why the package manager is detected, as a refusal to detect one says: none was given.
330
254
  const NOT_GIVEN = 'no packageManager given'
331
255
 
332
- // The one of the package managers `names` that installs `cwd` in the project `host` reads from a
333
- // directory holding its lockfile, as lockfileRoot finds it for `os`: refused where none or more than
334
- // one does, saying `why` it is detected.
335
256
  export function detectPackageManager(name, host, cwd, { names = PACKAGE_MANAGER_NAMES, os, why = NOT_GIVEN } = {}) {
336
257
  const real = host.realpath(cwd)
337
- const found = names.flatMap((pm) => {
338
- const root = rootOf(host, PACKAGE_MANAGERS[pm], real, os)
339
- return root === null ? [] : [[pm, join(root, PACKAGE_MANAGERS[pm].lockfile)]]
340
- })
258
+ const found = names.map((pm) => [pm, rootOf(host, PACKAGE_MANAGERS[pm], real, os)]).filter(([, root]) => root !== null)
341
259
  if (found.length === 1) return found[0][0]
342
260
  if (found.length === 0) throw noLockfile(name, real, names, why)
343
- throw new Error(`${name}: ${why}, and more than one lockfile installs ${real}: ${found.map(([pm, file]) => `${file} (${pm})`).join(', ')}`)
261
+ throw new Error(`${name}: ${why}, and more than one lockfile installs ${real}: ${found.map(([pm, root]) => `${join(root, lockfileOf(pm))} (${pm})`).join(', ')}`)
344
262
  }
345
263
 
346
- // That none of the lockfiles of `names` installs `place`, saying `why` one is looked for.
347
- export const noLockfile = (name, place, names = PACKAGE_MANAGER_NAMES, why = NOT_GIVEN) => new Error(`${name}: ${why}, and none of ${names.map((pm) => PACKAGE_MANAGERS[pm].lockfile).join(', ')} installs ${place}`)
264
+ export const noLockfile = (name, place, names = PACKAGE_MANAGER_NAMES, why = NOT_GIVEN) => new Error(`${name}: ${why}, and none of ${names.map(lockfileOf).join(', ')} installs ${place}`)
348
265
 
349
- // Whether a directory's listing (listRepoDir's) holds `packageManager`'s lockfile.
350
- const listsLockfile = (listing, packageManager) => listing.some((entry) => entry.path === PACKAGE_MANAGERS[packageManager].lockfile && entry.type === 'blob')
266
+ export const listsFile = (listing, name) => listing.some((entry) => entry.path === name && entry.type === 'blob')
351
267
 
352
- // The package managers whose lockfile `listings` (a directory's and those above it) hold, which are
353
- // the only ones that may install that directory.
354
- export const lockfilesListed = (listings) => PACKAGE_MANAGER_NAMES.filter((pm) => listings.some((listing) => listsLockfile(listing, pm)))
268
+ export const lockfilesListed = (listings) => PACKAGE_MANAGER_NAMES.filter((pm) => listings.some((listing) => listsFile(listing, lockfileOf(pm))))
355
269
 
356
- // A `packageManagerVersion` is one of the package manager given, never of one detected.
357
270
  export function checkVersion(name, { packageManager, packageManagerVersion }) {
358
271
  if (packageManagerVersion !== undefined && packageManager === undefined) throw new TypeError(`${name}: packageManagerVersion is only valid with packageManager`)
359
272
  }
360
273
 
361
- // The package manager of `names` the project `host` reads is built with at `cwd`: `packageManager`
362
- // if given, else the one detected for `os`.
363
- export function packageManagerFor(name, host, cwd, { packageManager, packageManagerVersion, os }, names = PACKAGE_MANAGER_NAMES) {
274
+ export function packageManagerFor(name, host, cwd, { packageManager, packageManagerVersion, os, why }, names = PACKAGE_MANAGER_NAMES) {
364
275
  checkVersion(name, { packageManager, packageManagerVersion })
365
- if (packageManager === undefined) return detectPackageManager(name, host, cwd, { names, os })
276
+ if (packageManager === undefined) return detectPackageManager(name, host, cwd, { names, os, why })
366
277
  packageManagerOf(name, packageManager, names)
367
278
  return packageManager
368
279
  }
369
280
 
370
281
  const KIND_LABELS = { js: 'JS', sol: 'Solidity' }
371
282
 
372
- // Refused where no package manager of `names` builds bundles of `kind`.
373
283
  export function checkKind(name, kind, names = PACKAGE_MANAGER_NAMES) {
374
284
  if (names.some((pm) => PACKAGE_MANAGERS[pm].kind === kind)) return
375
285
  const kinds = [...new Set(names.map((pm) => PACKAGE_MANAGERS[pm].kind))]
@@ -379,101 +289,233 @@ export function checkKind(name, kind, names = PACKAGE_MANAGER_NAMES) {
379
289
  : `${name}: only ${kinds.map((of) => `${KIND_LABELS[of]} bundles (${builders(of)})`).join(' and ')} are built`)
380
290
  }
381
291
 
382
- // The PACKAGE_MANAGERS entry of `packageManager`, which has to be one of `names`.
383
292
  export function packageManagerOf(name, packageManager, names = PACKAGE_MANAGER_NAMES) {
384
293
  if (!names.includes(packageManager)) throw new TypeError(`${name}: packageManager must be one of ${names.map((n) => `'${n}'`).join(', ')}`)
385
294
  return PACKAGE_MANAGERS[packageManager]
386
295
  }
387
296
 
388
- // Anything read as a @preventive/vfs Vfs: a Vfs of another copy of the package is one too.
297
+ // Duck-typed: a Vfs of another copy of @preventive/vfs is one too.
389
298
  export function checkVfs(name, vfs) {
390
- for (const method of ['lstat', 'readdir', 'readFile', 'readlink']) {
391
- if (typeof vfs?.[method] !== 'function') throw new TypeError(`${name}: vfs must be a @preventive/vfs Vfs holding the project`)
392
- }
393
- }
394
-
395
- // -> { root, vfs, projects, stats, installed, packageManager, packageManagerVersion }, of the project
396
- // `project` holds that `cwd` is in, as `packageManager` installs it: the directory it installs from,
397
- // as a real path; a new Vfs holding the tree, rooted there; the directories of the projects it finds,
398
- // from there; deptree's counts, and what it installs, each by its `path` from there; and the version
399
- // reproduced, `packageManagerVersion` if given, else the one the root package.json's packageManager
400
- // pins, else the default. deptree reads the project through a view of `project`, which nothing is
401
- // written through. `client`, a @preventive/upstream/github.js client, is what a Soldeer git
402
- // dependency is fetched through (deptree's `github`); without one, a lockfile holding one is refused
403
- // before anything is fetched. `cache` (checkCache) is where npm packages' tarballs and version
404
- // documents are kept, which deptree fetches for pnpm, yarn1 and npm. Each project's node_modules is
405
- // cleaned by every .yarnclean in cwd and the directories above it (yarnclean.js). With `lazy`, for
406
- // pnpm and npm (checkLazy), the tree is laid out with nothing fetched, and comes with `unpack` and
407
- // `lazy` besides (lazyTree). Without `installDevDependencies`, what devDependencies alone reach is
408
- // left out, as the package manager's production install leaves it out (deptree's `production`).
409
- // With `refuseInstalled`, refused before anything is fetched where `project` holds anything at all
410
- // (a link too) where the tree installs a project's dependencies: its node_modules, or Soldeer's
411
- // dependencies folder, which the tree would serve in place of what is there.
412
- async function layOutTree({ project, packageManager, cwd, packageManagerVersion, os, cpu, libc, client, cache, lazy = false, installDevDependencies = true, refuseInstalled = false }) {
299
+ const missing = ['lstat', 'readdir', 'readFile', 'readlink'].some((method) => typeof vfs?.[method] !== 'function')
300
+ if (missing) throw new TypeError(`${name}: vfs must be a @preventive/vfs Vfs holding the project`)
301
+ }
302
+
303
+ function planTree({ project, packageManager, cwd, packageManagerVersion, os, cpu, libc, lazy = false, refuseInstalled, ...options }) {
413
304
  const pm = PACKAGE_MANAGERS[packageManager]
414
305
  const found = pm.root(project, cwd, os)
415
306
  if (found === null) throw new Error(`no ${pm.lockfile} found in ${cwd} or any parent directory`)
416
307
  const file = join(found, pm.lockfile)
417
308
  if (!isFile(file, project)) throw new Error(`no ${pm.lockfile} found in ${found}, where ${cwd} is installed from`)
418
309
  const pinned = pm.pin === undefined ? undefined : readJson(join(found, 'package.json'), project)?.packageManager
310
+ const pinnedVersion = String(pinned).startsWith(`${pm.pin}@`) ? /^[^@]+@([^+]+)/u.exec(pinned)[1] : undefined
419
311
  // Left out, deptree takes the one packageManager pins, and refuses another package manager.
420
312
  const version = packageManagerVersion ?? (pinned === undefined ? pm.version : undefined)
313
+ const taken = version ?? pinnedVersion
314
+ if (Number(/^(\d+)\./u.exec(taken ?? '')?.[1]) < pm.from) {
315
+ const where = packageManagerVersion === undefined ? `${join(found, 'package.json')}: packageManager` : 'packageManagerVersion'
316
+ throw new Error(`${where}: ${packageManager} "${taken}" is not supported: only ${packageManager} ${pm.from} and later are`)
317
+ }
421
318
  const view = projectView(project, found)
422
319
  const projects = new Set(pm.projects(view, version, { os, cpu, libc }))
423
320
  const other = pm.kind === 'js' ? outsider(project, found, cwd, projects) : null
424
321
  if (other !== null) throw new Error(`${file} does not install ${other}: it is none of the lockfile's projects`)
425
- if (refuseInstalled) {
426
- for (const dir of projects) {
427
- const at = join(found, dir, pm.installs)
428
- if (lexists(at, project)) throw new Error(`${at} is there already, and the tree is laid out from ${file} alone, over nothing installed: remove it first`)
429
- }
430
- }
322
+ const there = refuseInstalled ? [...projects].map((dir) => join(found, dir, pm.installs)).find((at) => lexists(at, project)) : undefined
323
+ if (there !== undefined) throw new Error(`${there} is there already, and the tree is laid out from ${file} alone, over nothing installed: remove it first`)
431
324
  // Read before anything is fetched: matching its patterns needs the optional minimatch.
432
325
  const clean = pm.kind === 'js' ? yarnclean(project, cwd) : null
433
326
  if (lazy && clean?.keeps) throw new Error(`a ${YARNCLEAN} in ${cwd} or above it keeps paths with \`!\`, which lazy does not take: what it keeps of one package turns on what the others hold, which a lazy tree has not fetched`)
327
+ return { ...options, packageManager, pm, os, cpu, libc, lazy, root: project.realpath(found), file, version, taken, view, projects, clean }
328
+ }
329
+
330
+ async function buildTree({ packageManager, pm, os, cpu, libc, client, cache, lazy, installDevDependencies = true, root, file, version, taken, view, projects, clean }) {
434
331
  const built = await pm.build(view, version, file, { os, cpu, libc, client, cache, lazy, production: !installDevDependencies })
435
- // Each project's node_modules, cleaned as `yarn autoclean` would clean it, whatever installs it.
436
332
  const cleanAll = () => {
437
333
  if (clean !== null) for (const dir of projects) clean(built.vfs, posix.join('/', dir, pm.installs))
438
334
  }
439
335
  cleanAll()
440
- return {
441
- root: project.realpath(found),
442
- vfs: built.vfs,
443
- projects,
444
- stats: built.stats,
445
- installed: built.installed,
446
- packageManager,
447
- packageManagerVersion: version ?? (String(pinned).startsWith(`${pm.pin}@`) ? /^[^@]+@([^+]+)/u.exec(pinned)[1] : undefined),
448
- ...lazy ? lazyTree(built, cleanAll) : {},
336
+ const lockText = pm.integrities && readText(view, pm.lockfile)
337
+ const { vfs, stats, installed } = built
338
+ const integrities = pm.integrities && { integrities: () => pm.integrities(lockText) }
339
+ const lazily = lazy ? lazyTree(built, cleanAll) : {}
340
+ return { root, vfs, projects, stats, installed, ...integrities, packageManager, packageManagerVersion: taken, ...lazily }
341
+ }
342
+
343
+ export const lockfilesManagers = (name, lockfiles) => lockfiles.map((path) => {
344
+ const found = PACKAGE_MANAGER_NAMES.find((pm) => lockfileOf(pm) === posix.basename(path))
345
+ if (found === undefined) throw new Error(`${name}: ${CONFIG_FILE}'s lockfiles names ${path}, which is none of ${PACKAGE_MANAGER_NAMES.map(lockfileOf).join(', ')}, the lockfiles a tree is laid out from`)
346
+ return found
347
+ })
348
+
349
+ export function lockfileSpecs(name, project, cwd, lockfiles, os) {
350
+ const managers = lockfilesManagers(name, lockfiles)
351
+ return lockfiles.map((path, i) => {
352
+ // resolve would stop at `/`.
353
+ if (posixPathEscapes(posix.join(cwd.slice(1), path))) throw new Error(`${CONFIG_FILE}'s lockfiles names ${path}, which climbs out of ${cwd}'s root`)
354
+ const file = resolve(cwd, path)
355
+ if (!isFile(file, project)) throw new Error(`${CONFIG_FILE}'s lockfiles names ${path}, which is no file in ${cwd}`)
356
+ const dir = project.realpath(dirname(file))
357
+ const from = isPathWithin(dir, cwd) ? cwd : dir
358
+ const pm = PACKAGE_MANAGERS[managers[i]]
359
+ const found = pm.root(project, from, os)
360
+ if (found !== dir) throw new Error(`${CONFIG_FILE}'s lockfiles names ${path}, but ${managers[i]} installs ${from} from ${found === null ? `no ${pm.lockfile}` : join(found, pm.lockfile)}`)
361
+ return { packageManager: managers[i], cwd: from, path }
362
+ })
363
+ }
364
+
365
+ export const plannedTrees = ({ specs, ...options }) =>
366
+ specs.map((spec) => planTree({ ...options, ...spec })).map(({ root, projects }) => ({ root, projects }))
367
+
368
+ export async function layOutTrees({ specs, ...options }) {
369
+ const plans = specs.map((spec) => planTree({ ...options, ...spec }))
370
+ const claimed = new Map()
371
+ const above = new Map()
372
+ for (const plan of plans) {
373
+ const dirs = [...plan.projects].map((dir) => join(plan.root, dir, plan.pm.installs))
374
+ for (const at of dirs) {
375
+ const outer = nearest(at, (dir) => claimed.has(dir))
376
+ // As [the one claimed, the outer of the two].
377
+ const clash = above.has(at) ? [above.get(at), at] : outer === null ? undefined : [outer, outer]
378
+ if (clash !== undefined) throw new Error(`${CONFIG_FILE}'s lockfiles names ${claimed.get(clash[0]).path} and ${plan.path}, which both install ${clash[1]}`)
379
+ }
380
+ for (const at of dirs) {
381
+ claimed.set(at, plan)
382
+ for (let dir = dirname(at); !above.has(dir); dir = dirname(dir)) {
383
+ above.set(dir, at)
384
+ if (dir === dirname(dir)) break
385
+ }
386
+ }
449
387
  }
388
+ const built = await settled(plans.map(buildTree))
389
+ return built.length === 1 ? built[0] : forestOf(built)
390
+ }
391
+
392
+ // Every one settled before the first refusal is thrown.
393
+ async function settled(promises) {
394
+ const done = await Promise.allSettled(promises)
395
+ const failed = done.find(({ status }) => status === 'rejected')
396
+ if (failed !== undefined) throw failed.reason
397
+ return done.map(({ value }) => value)
398
+ }
399
+
400
+ function installsOf(tree) {
401
+ const files = tree.vfs.readdir('/').filter((name) => tree.vfs.lstat(`/${name}`).type === 'file')
402
+ return [...[...tree.projects].map((dir) => join(dir, PACKAGE_MANAGERS[tree.packageManager].installs)), ...files]
403
+ }
404
+
405
+ const hiding = (name) => (name === undefined ? undefined : (rel) => rel.split('/').includes(name))
406
+
407
+ function mountedVfs(owners) {
408
+ const ownerOf = (p) => {
409
+ for (let at = p.indexOf('/', 1); ; at = p.indexOf('/', at + 1)) {
410
+ const member = owners.get(at === -1 ? p.slice(1) : p.slice(1, at))
411
+ if (member !== undefined) return member
412
+ if (at === -1) throw stackless(() => new VfsError('ENOENT', p))
413
+ }
414
+ }
415
+ const read = (method) => (p) => {
416
+ const member = ownerOf(p)
417
+ return member.tree.vfs[method](p.slice(member.prefix.length))
418
+ }
419
+ return { lstat: read('lstat'), readlink: read('readlink'), readFile: read('readFile'), readdir: read('readdir') }
420
+ }
421
+
422
+ function statsOf(trees) {
423
+ const stats = {}
424
+ const counts = trees.flatMap(({ stats: own }) => Object.entries({ ...own, installed: own.installed ?? own.dependencies, dependencies: undefined }))
425
+ for (const [key, value] of counts) if (typeof value === 'number') stats[key] = (stats[key] ?? 0) + value
426
+ return stats
427
+ }
428
+
429
+ export const unpackedOf = (tree) => (tree.trees === undefined
430
+ ? tree.lazy?.unpacked.size ?? tree.stats.installed ?? tree.stats.dependencies
431
+ : tree.trees.reduce((sum, one) => sum + unpackedOf(one), 0))
432
+
433
+ function forestOf(trees) {
434
+ let root = trees[0].root
435
+ while (!trees.every((tree) => isPathWithin(root, tree.root))) root = dirname(root)
436
+ const members = trees.map((tree) => {
437
+ const at = posix.relative(root, tree.root)
438
+ return { tree, at, prefix: at === '' ? '' : `${at}/`, installs: installsOf(tree), hides: hiding(PACKAGE_MANAGERS[tree.packageManager].hides) }
439
+ })
440
+ // layOutTrees checked the plans' installs apart already, but for the files a tree writes at its root.
441
+ const owners = new Map()
442
+ for (const member of members) {
443
+ for (const path of member.installs) {
444
+ const at = `${member.prefix}${path}`
445
+ if (owners.has(at)) throw new Error(`${CONFIG_FILE}'s lockfiles lay out trees which both install ${posix.join(root, at)}`)
446
+ owners.set(at, member)
447
+ }
448
+ }
449
+ const within = (member, rel) => rel === member.at || rel.startsWith(member.prefix)
450
+ const hiders = members.filter((member) => member.hides !== undefined)
451
+ const rooted = members.some((member) => member.at === '')
452
+ const toward = new Set(members.flatMap(({ at }) => at.split('/').slice(0, -1).map((_, i, parts) => parts.slice(0, i + 1).join('/'))))
453
+ const hides = rooted && hiders.length === 0 ? undefined : (rel) => {
454
+ if (!rooted && !members.some((member) => within(member, rel))) return hasNodeModulesSegment(rel) && !toward.has(rel)
455
+ return hiders.some((member) => within(member, rel) && member.hides(rel.slice(member.prefix.length)))
456
+ }
457
+ const forest = {
458
+ root,
459
+ vfs: mountedVfs(owners),
460
+ installs: [...owners.keys()],
461
+ hides,
462
+ projects: new Set(members.filter(({ tree }) => PACKAGE_MANAGERS[tree.packageManager].kind === 'js').flatMap((member) => [...member.tree.projects].map((dir) => posix.join(member.at, dir)))),
463
+ // Read as they are when read: a lazy tree's counts grow as it unpacks.
464
+ get stats() {
465
+ return statsOf(trees)
466
+ },
467
+ get installed() {
468
+ return members.flatMap((member) => member.tree.installed.map((pkg) => ({ ...pkg, path: `${member.prefix}${pkg.path}` })))
469
+ },
470
+ trees,
471
+ }
472
+ const lazy = members.filter(({ tree }) => tree.lazy !== undefined)
473
+ if (lazy.length === 0) return forest
474
+ const packages = new Map(lazy.flatMap((member) => [...member.tree.lazy.roots].map((dir) => [`/${member.prefix}${dir.slice(1)}`, [member, dir]])))
475
+ const unpacked = {
476
+ has(dir) {
477
+ const [member, own] = packages.get(dir)
478
+ return member.tree.lazy.unpacked.has(own)
479
+ },
480
+ }
481
+ // Onto the forest itself, keeping its getters.
482
+ return Object.assign(forest, {
483
+ lazy: { roots: new Set(packages.keys()), unpacked },
484
+ async unpack(asked, read = asked) {
485
+ const by = new Map()
486
+ for (const dir of asked) {
487
+ const [member, own] = packages.get(dir)
488
+ const sets = upsert(by, member, () => ({ asked: new Set(), read: new Set() }))
489
+ sets.asked.add(own)
490
+ if (read.has(dir)) sets.read.add(own)
491
+ }
492
+ await settled([...by].map(([member, sets]) => member.tree.unpack(sets.asked, sets.read)))
493
+ },
494
+ })
450
495
  }
451
496
 
452
- // A package name, as a directory of node_modules holds it: one name, or a scope and one.
453
497
  const PACKAGE_NAME = /^(?:@[^/@.][^/]*\/)?[^/@.][^/]*$/u
454
498
 
455
- // The directories among `roots` of what the package at `dir` depends on, as its package.json names
456
- // them (dependencies, optionalDependencies and peerDependencies) and Node finds them from `dir` in
457
- // `vfs`: what the build reads next, where it reads that package, but none it is held to.
499
+ // The `roots` the package at `dir` depends on, found as Node finds them from it: what the build likely reads next.
458
500
  function dependenciesOf(vfs, dir, roots) {
459
501
  const manifest = readJson(`${dir}/package.json`, vfs)
460
502
  if (!isPlainObject(manifest)) return []
461
503
  const names = new Set(['dependencies', 'optionalDependencies', 'peerDependencies'].flatMap((field) => (isPlainObject(manifest[field]) ? Object.keys(manifest[field]) : [])))
504
+ const realOrNull = (path) => {
505
+ try {
506
+ return vfs.realpath(path)
507
+ } catch {
508
+ return null
509
+ }
510
+ }
462
511
  const found = []
463
512
  for (const name of names) {
464
513
  if (!PACKAGE_NAME.test(name)) continue
465
514
  for (let at = dir; ; at = posix.dirname(at)) {
466
- if (posix.basename(at) !== 'node_modules') {
467
- let real
468
- try {
469
- real = vfs.realpath(posix.join(at, 'node_modules', name))
470
- } catch {
471
- real = null
472
- }
473
- if (real !== null) {
474
- if (roots.has(real)) found.push(real)
475
- break
476
- }
515
+ const real = posix.basename(at) === 'node_modules' ? null : realOrNull(posix.join(at, 'node_modules', name))
516
+ if (real !== null) {
517
+ if (roots.has(real)) found.push(real)
518
+ break
477
519
  }
478
520
  if (at === '/') break
479
521
  }
@@ -481,17 +523,8 @@ function dependenciesOf(vfs, dir, roots) {
481
523
  return found
482
524
  }
483
525
 
484
- // A lazy tree's `unpack(dirs)`, of its packages' directories from `/` of its Vfs, as lazyView asks
485
- // for them, which deptree unpacks, cleaned as the rest is; and its `lazy`, those directories, as
486
- // lazyView takes them: `roots`, each the layout has (a .yarnclean may take some away), and
487
- // `unpacked`, each unpacked so far. Positive patterns alone clean each path by its own, whatever
488
- // else the tree holds, so cleaning again cleans each package unpacked as the full tree's is.
489
- // `installed` takes in what each one's version document says, once it is unpacked. What each
490
- // package `read` (inRounds') depends on is unpacked with it, as the build would ask for it a round
491
- // later, each round a build; but not what one only probed for its files depends on, nor what those
492
- // depend on in turn, which most builds never read: a package's dependencies are often its tools. One
493
- // deptree refuses is left for the build to ask for, which is then refused, so that one it never
494
- // reads is never refused, as one never fetched is not.
526
+ // Each package unpacked is cleaned as the full tree's (positive patterns clean each path by its own). What a package
527
+ // `read` depends on is unpacked with it, not what a probed one does; one deptree refuses then is left for the build.
495
528
  function lazyTree({ vfs, installed, unpack }, cleanAll) {
496
529
  const roots = new Set(installed.map(({ path }) => `/${path}`).filter((dir) => vfs.isDirectory(dir)))
497
530
  const unpacked = new Set()
@@ -505,13 +538,9 @@ function lazyTree({ vfs, installed, unpack }, cleanAll) {
505
538
  return {
506
539
  lazy: { roots, unpacked },
507
540
  async unpack(asked, read = asked) {
508
- // Each asked for, and once one read is unpacked, what it depends on, then and there rather
509
- // than once every one asked for is: so the fetches of both share the time, not one waiting
510
- // on the last of the other. The first refusal of one asked for is thrown once every one is
511
- // done, as deptree throws one.
512
541
  const seen = new Set(asked)
513
542
  const ahead = []
514
- const done = await Promise.allSettled([...asked].map(async (dir) => {
543
+ await settled([...asked].map(async (dir) => {
515
544
  await unpackOne(dir)
516
545
  if (!read.has(dir)) return
517
546
  for (const dependency of dependenciesOf(vfs, dir, roots)) {
@@ -519,60 +548,45 @@ function lazyTree({ vfs, installed, unpack }, cleanAll) {
519
548
  seen.add(dependency)
520
549
  ahead.push(unpackOne(dependency).catch(() => refused.add(dependency)))
521
550
  }
522
- }))
523
- await Promise.all(ahead)
524
- const failed = done.find(({ status }) => status === 'rejected')
525
- if (failed !== undefined) throw failed.reason
551
+ })).finally(() => Promise.all(ahead))
526
552
  cleanAll()
527
553
  },
528
554
  }
529
555
  }
530
556
 
531
- // The root of the GitHub repository a git URL names, or undefined for another host's.
532
557
  const githubRoot = (url) => {
533
558
  const github = parseGithubRepository(url)
534
559
  return github === null ? undefined : { github, directory: '' }
535
560
  }
536
561
 
537
- // Each dependency of `bundle`, its paths from `root`, at the commit the tree (layOutTree's) installs
538
- // it from, where deptree names a full one: an npm package at its gitHead, which the registry's
539
- // version document names, over the one its package.json may name (packageRepo), a commit of the
540
- // repository its own package.json names, so taken only where the bundle records that one; a
541
- // Soldeer git dependency at its lockfile's `rev`, the root of the repository it is fetched from,
542
- // where that is a GitHub one. Metadata, as `repo` is: the publisher's word, held to no repository.
543
- export function pinCommits(bundle, root, { root: from, installed }) {
562
+ // Each dependency of `bundle` at the full commit the tree installs it from: an npm package at its gitHead, only where
563
+ // the bundle records its repo; a Soldeer git dependency at its `rev`, of its repo's root. -> accounts, by dir
564
+ export function pinFromTree(bundle, root, tree) {
565
+ if (tree.trees !== undefined) return new Map(tree.trees.flatMap((one) => [...pinFromTree(bundle, root, one)]))
566
+ const { root: from, installed, packageManager } = tree
544
567
  const byDir = new Map(installed.map((pkg) => [posix.relative(root, posix.join(from, pkg.path)), pkg]))
568
+ const integrities = tree.integrities?.()
569
+ const locked = ({ integrity, checksum }) => (PACKAGE_MANAGERS[packageManager].kind !== 'js' ? archiveIntegrity('soldeer', checksum) : (integrities?.has(tarballIntegrityOf(integrity)) ?? true) ? integrity : undefined)
570
+ const accounts = new Map()
545
571
  for (const [dir, info] of bundle.modules) {
546
572
  const pkg = byDir.get(dir)
547
- if (!isValidRepoField('commit', pkg?.commit)) continue
548
- const repo = info.repo ?? githubRoot(pkg.git)
549
- if (repo !== undefined) bundle.modules.set(dir, { ...info, repo: { ...repo, commit: pkg.commit } })
573
+ if (pkg === undefined) continue
574
+ const at = isValidRepoField('commit', pkg.commit) ? info.repo ?? githubRoot(pkg.git) : undefined
575
+ const pinned = installedMetadata(dir, info.ecosystem, { repo: at && { ...at, commit: pkg.commit }, integrity: locked(pkg), patchHash: pkg.patch?.hash })
576
+ bundle.modules.set(dir, { ...info, ...pinned })
577
+ accounts.set(dir, { integrity: pinned.integrity, patch: pinned.patch })
550
578
  }
579
+ return accounts
551
580
  }
552
581
 
553
582
  // fs.Stats#mode carries the file type above the permission bits a Vfs stat holds.
554
583
  const TYPE_BITS = { file: constants.S_IFREG, directory: constants.S_IFDIR, symlink: constants.S_IFLNK }
555
584
 
556
- const statsFor = (st) => ({
557
- isFile: () => st.type === 'file',
558
- isDirectory: () => st.type === 'directory',
559
- isSymbolicLink: () => st.type === 'symlink',
560
- mode: TYPE_BITS[st.type] | st.mode,
561
- })
562
-
563
- const dirent = (name, type) => ({
564
- name,
565
- isFile: () => type === 'file',
566
- isDirectory: () => type === 'directory',
567
- isSymbolicLink: () => type === 'symlink',
568
- })
585
+ const typePredicates = (type) => ({ isFile: () => type === 'file', isDirectory: () => type === 'directory', isSymbolicLink: () => type === 'symlink' })
569
586
 
570
- // A view over the Vfs's bytes, never a copy.
571
- const asBuffer = (bytes) => Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
587
+ const dirent = (name, type) => ({ name, ...typePredicates(type) })
572
588
 
573
- // `make()`, an error for a name a lookup does not find, made without a stack: a resolver probes
574
- // many names for each it finds, and its callers take a miss as none, so the stack is never read,
575
- // and building it is most of what a miss costs.
589
+ // A resolver probes many names and takes a miss as none: building the stack is most of what a miss costs.
576
590
  function stackless(make) {
577
591
  const limit = Error.stackTraceLimit
578
592
  Error.stackTraceLimit = 0
@@ -583,8 +597,6 @@ function stackless(make) {
583
597
  }
584
598
  }
585
599
 
586
- // What `vfs` holds at `p` itself (the last link not followed), or null: the Vfs throws for a name
587
- // it does not hold, which is made without a stack (stackless).
588
600
  function lstatOrNull(vfs, p) {
589
601
  try {
590
602
  return stackless(() => vfs.lstat(p))
@@ -594,50 +606,27 @@ function lstatOrNull(vfs, p) {
594
606
  }
595
607
  }
596
608
 
597
- const TREE = 'tree'
598
- const NONE = 'none'
599
- const OUTSIDE = 'outside'
600
-
601
- // A `host` (@exodus/stasis-core/host) over `vfs`. Alone, every path is the Vfs's. With `root`, a
602
- // real path, and `outside`, a host of the rest, the Vfs's `/` stands for `root`, holding the tree
603
- // the package manager lays out there: each of the paths it `installs` (from `root`) is served from
604
- // the Vfs only, whatever `outside` holds there; a directory named `hides` anywhere, and any
605
- // node_modules out of `root`, is none; and everything else comes from `outside`. Symlinks cross
606
- // between the two both ways, so realpaths are walked here one link at a time, and a path is read as
607
- // the OS reads one: a `..` after a link leads up from where the link leads, never textually (Node's
608
- // own resolution, `findPackageJSON` and `resolve`, normalizes its paths first, as Node does). A
609
- // relative path is from `/`. What it reads is cached, as for a tree that holds still, unless `cache`
610
- // is false; with `touches`, a count of the reads that read what the tree does not hold yet (a lazy
611
- // tree's, lazy.js), only what was read with none is cached, so that each of those is read again; but
612
- // with `retouch` too, for a host of one round of a lazy tree, those are cached too, and each taken
613
- // from the cache counts as read again (untouchedMemo's `retouch`).
609
+ // A `host` over `vfs`; with `root` (a real path) and `outside` (a host of the rest), the Vfs's `/` is `root`, serving
610
+ // what it `installs`, a path `hides` takes and a node_modules out of `root` being none. Links are walked as the OS does.
614
611
  export function vfsHost(vfs, { root, outside, installs = [], hides, cache = true, touches = () => 0, retouch } = {}) {
615
612
  if (sep !== '/') throw new Error('The Vfs host is POSIX-only')
616
613
  const disk = outside ?? null
617
614
  const installed = new Set(installs)
618
- // Paths here are absolute and normal.
619
615
  const prefix = disk === null || root === '/' ? '/' : `${root}/`
620
- // The root and the directories above it, whatever their names.
621
616
  const towardRoot = (p) => p === root || prefix.startsWith(`${p}/`)
622
617
  const zoneOf = (p) => {
623
- if (disk === null) return TREE
624
- if (!p.startsWith(prefix)) return hasNodeModulesSegment(p) && !towardRoot(p) ? NONE : OUTSIDE
618
+ if (disk === null) return 'tree'
619
+ if (!p.startsWith(prefix)) return hasNodeModulesSegment(p) && !towardRoot(p) ? 'none' : 'outside'
625
620
  const rel = p.slice(prefix.length)
626
621
  for (let at = rel.indexOf('/'); at !== -1; at = rel.indexOf('/', at + 1)) {
627
- if (installed.has(rel.slice(0, at))) return TREE
622
+ if (installed.has(rel.slice(0, at))) return 'tree'
628
623
  }
629
- if (installed.has(rel)) return TREE
630
- return hides !== undefined && rel.split('/').includes(hides) ? NONE : OUTSIDE
631
- }
632
- // The names of the installed paths in each directory holding one.
633
- const installedIn = new Map()
634
- for (const dir of disk === null ? [] : installed) {
635
- const at = join(root, dirname(dir))
636
- installedIn.set(at, [...(installedIn.get(at) ?? []), basename(dir)])
624
+ if (installed.has(rel)) return 'tree'
625
+ return hides?.(rel) ? 'none' : 'outside'
637
626
  }
638
- // A path in the tree, as the Vfs spells it.
627
+ const installedIn = Map.groupBy(disk === null ? [] : installed, (dir) => join(root, dirname(dir)))
639
628
  const inVfs = disk === null ? (p) => p : (p) => `/${p.slice(prefix.length)}`
640
- // `p` from `/`, as spelled: realpath resolves its `..` after the link before it.
629
+ // As spelled: realpath resolves a `..` after the link before it.
641
630
  const abs = (p) => (p.startsWith('/') ? p : `/${p}`)
642
631
  const normal = (p) => resolve('/', p)
643
632
  const memo = () => untouchedMemo(touches, { cache, retouch })
@@ -645,9 +634,8 @@ export function vfsHost(vfs, { root, outside, installs = [], hides, cache = true
645
634
  // `p` is a real path but for its last name; null when it exists and isn't a link.
646
635
  const readlink = (p) => {
647
636
  const zone = zoneOf(p)
648
- if (zone === OUTSIDE) return disk.readlink(p)
649
- if (zone === NONE) throw stackless(() => new VfsError('ENOENT', p))
650
- const node = lstatOrNull(vfs, inVfs(p))
637
+ if (zone === 'outside') return disk.readlink(p)
638
+ const node = zone === 'none' ? null : lstatOrNull(vfs, inVfs(p))
651
639
  if (node === null) throw stackless(() => new VfsError('ENOENT', p))
652
640
  return node.type === 'symlink' ? vfs.readlink(inVfs(p)) : null
653
641
  }
@@ -673,12 +661,8 @@ export function vfsHost(vfs, { root, outside, installs = [], hides, cache = true
673
661
  } else {
674
662
  const candidate = join(parentReal, name)
675
663
  const link = readlink(candidate)
676
- if (link === null) {
677
- result = candidate
678
- } else {
679
- if (hops >= 40) throw new VfsError('ELOOP', p)
680
- result = realpath(link.startsWith('/') ? link : `${dirname(candidate)}/${link}`, hops + 1)
681
- }
664
+ if (link !== null && hops >= 40) throw new VfsError('ELOOP', p)
665
+ result = link === null ? candidate : realpath(link.startsWith('/') ? link : `${dirname(candidate)}/${link}`, hops + 1)
682
666
  }
683
667
  } catch (err) {
684
668
  const touched = touches() !== before
@@ -709,24 +693,21 @@ export function vfsHost(vfs, { root, outside, installs = [], hides, cache = true
709
693
  if (error !== null) throw error
710
694
  }
711
695
 
712
- // Of `outside` too, whose realpaths and listings are cached here as well.
713
696
  const statCache = memo()
714
697
  const statOf = (real) => statCache(real, () => {
715
- if (zoneOf(real) === OUTSIDE) return disk.stat(real)
698
+ if (zoneOf(real) === 'outside') return disk.stat(real)
716
699
  const node = lstatOrNull(vfs, inVfs(real))
717
- return node !== null && node.type !== 'symlink' ? statsFor(node) : null
700
+ return node !== null && node.type !== 'symlink' ? { ...typePredicates(node.type), mode: TYPE_BITS[node.type] | node.mode } : null
718
701
  })
719
- // The entries of the directory at `real`, `p` as the caller spelled it, sorted by name.
720
702
  const listingOf = (real, p) => {
721
- if (zoneOf(real) === TREE) {
703
+ if (zoneOf(real) === 'tree') {
722
704
  if (nodeOf(real).type !== 'directory') throw new VfsError('ENOTDIR', p)
723
705
  const dir = inVfs(real)
724
706
  return vfs.readdir(dir).map((name) => dirent(name, vfs.lstat(join(dir, name)).type)).toSorted(byName)
725
707
  }
726
- // An outside directory shows the tree's installed paths in place of its own, and nothing that
727
- // is none.
728
- const out = disk.readdir(real).filter((d) => zoneOf(join(real, d.name)) === OUTSIDE)
729
- for (const name of installedIn.get(real) ?? []) {
708
+ // An outside directory shows the tree's installed paths in place of its own, and nothing that is none.
709
+ const out = disk.readdir(real).filter((d) => zoneOf(join(real, d.name)) === 'outside')
710
+ for (const name of (installedIn.get(real) ?? []).map((dir) => basename(dir))) {
730
711
  const node = lstatOrNull(vfs, inVfs(join(real, name)))
731
712
  if (node !== null) out.push(dirent(name, node.type))
732
713
  }
@@ -753,9 +734,10 @@ export function vfsHost(vfs, { root, outside, installs = [], hides, cache = true
753
734
  readFile(p) {
754
735
  const real = realpath(abs(p))
755
736
  checkDir(p, real)
756
- if (zoneOf(real) === OUTSIDE) return disk.readFile(real)
737
+ if (zoneOf(real) === 'outside') return disk.readFile(real)
757
738
  if (nodeOf(real).type !== 'file') throw new VfsError('EISDIR', p)
758
- return asBuffer(vfs.readFile(inVfs(real)))
739
+ const bytes = vfs.readFile(inVfs(real))
740
+ return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
759
741
  },
760
742
  readdir(p) {
761
743
  const real = realpath(abs(p))
@@ -780,11 +762,9 @@ export function vfsHost(vfs, { root, outside, installs = [], hides, cache = true
780
762
  checkDir(p, real)
781
763
  return real
782
764
  },
783
- // Node's fs.globSync and path.matchesGlob, as the disk host's, over this host (glob.js).
784
765
  glob: (pattern, options) => globSync(host, pattern, options),
785
766
  matchesGlob,
786
- // As Node's: the nearest package.json above a file's real path, never out of a node_modules dir;
787
- // one there that can't be read is refused (packageJSONStat).
767
+ // As Node's: never out of a node_modules dir; one there that can't be read is refused (packageJSONStat).
788
768
  findPackageJSON(p) {
789
769
  let from = normal(p)
790
770
  if (host.stat(from)?.isFile()) from = realpath(from)
@@ -798,10 +778,7 @@ export function vfsHost(vfs, { root, outside, installs = [], hides, cache = true
798
778
  }
799
779
  return undefined
800
780
  },
801
- resolve(parentFile, specifier, conditions) {
802
- return (resolver ?? createNodeResolver(host)).resolve(normal(parentFile), specifier, conditions)
803
- },
804
- // What createNodeResolver caches by, as this host does.
781
+ resolve: (parentFile, specifier, conditions) => (resolver ?? createNodeResolver(host)).resolve(normal(parentFile), specifier, conditions),
805
782
  touches,
806
783
  retouch,
807
784
  }