@exodus/stasis 1.0.0-beta.9 → 1.0.0-rc.1

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