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

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