@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.
- package/README.md +7 -2
- package/bin/stasis.js +263 -446
- package/package.json +38 -13
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +256 -212
- package/src/babel.js +119 -82
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +24 -8
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +211 -117
- package/src/cmd/bundle-options.js +347 -0
- package/src/cmd/bundle.js +1718 -908
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +48 -21
- package/src/cmd/output.js +24 -12
- package/src/cmd/sbom.js +6 -9
- package/src/cmd/tokens.js +60 -0
- package/src/define.js +218 -0
- package/src/diff.js +73 -129
- package/src/esbuild.js +0 -4
- package/src/flow.js +27 -0
- package/src/installed.js +265 -0
- package/src/json-stream.js +4 -9
- package/src/loader-mock.js +3 -8
- package/src/loader.js +1 -5
- package/src/loaders/bash.js +43 -120
- package/src/loaders/cargo.js +301 -808
- package/src/loaders/foundry.js +112 -290
- package/src/loaders/paths.js +15 -26
- package/src/loaders/php.js +221 -475
- package/src/loaders/rust.js +518 -1461
- package/src/loaders/solidity-ownership.js +51 -142
- package/src/loaders/solidity.js +42 -188
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +41 -0
- package/src/metro-resolver.js +58 -133
- package/src/metro-transformer.js +0 -6
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +8 -7
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +170 -235
- package/src/resolve-node.js +33 -46
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +70 -97
- package/src/scan.js +241 -278
- package/src/sixel.js +310 -0
- package/src/table.js +95 -0
- package/src/validate-notes.d.ts +19 -0
- package/src/validate-notes.js +72 -0
- package/src/validate.d.ts +159 -0
- package/src/validate.js +1315 -0
- package/src/vfs-bundle/entries.js +54 -46
- package/src/vfs-bundle/github.js +115 -92
- package/src/vfs-bundle/glob.js +379 -0
- package/src/vfs-bundle/lazy.js +99 -0
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +434 -259
- package/src/vfs-bundle/yarnclean.js +79 -0
- package/src/vfs-bundle.js +25 -51
- package/src/webpack.js +0 -4
- package/src/why.js +79 -231
package/src/vfs-bundle/tree.js
CHANGED
|
@@ -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 {
|
|
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
|
-
//
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
-
//
|
|
76
|
-
|
|
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)
|
|
91
|
-
|
|
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
|
|
85
|
+
return 'unknown'
|
|
96
86
|
}
|
|
87
|
+
let machineLibc
|
|
88
|
+
const currentLibc = () => (machineLibc ??= reportedLibc())
|
|
97
89
|
|
|
98
|
-
|
|
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
|
|
104
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
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
|
|
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
|
-
//
|
|
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
|
-
//
|
|
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) =>
|
|
143
|
+
return nearest(cwd, (dir) => isFile(join(dir, 'package-lock.json'), host) && isFile(join(dir, 'package.json'), host))
|
|
167
144
|
}
|
|
168
145
|
|
|
169
|
-
|
|
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
|
-
|
|
154
|
+
from: 10,
|
|
190
155
|
root: (host, cwd) => nearest(cwd, holding(host, 'pnpm-workspace.yaml')) ?? nearest(cwd, holding(host, 'pnpm-lock.yaml')),
|
|
191
|
-
alone:
|
|
156
|
+
alone: noneAbove('pnpm-workspace.yaml'),
|
|
192
157
|
projects: (view, pnpm) => findPnpmProjects({ project: view, host: { pnpm } }),
|
|
193
158
|
installs: 'node_modules',
|
|
194
|
-
|
|
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
|
-
|
|
203
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
|
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
|
|
215
|
+
const tree = await buildTree(planTree(options))
|
|
248
216
|
return { ...tree, host: treeHost(tree, options.project) }
|
|
249
217
|
}
|
|
250
218
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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.
|
|
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}:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
298
|
+
// Duck-typed: a Vfs of another copy of @preventive/vfs is one too.
|
|
340
299
|
export function checkVfs(name, vfs) {
|
|
341
|
-
|
|
342
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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
|
|
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
|
|
396
|
-
|
|
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
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
|
434
|
-
if (!p.startsWith(prefix)) return hasNodeModulesSegment(p) && !towardRoot(p) ?
|
|
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
|
|
623
|
+
if (installed.has(rel.slice(0, at))) return 'tree'
|
|
438
624
|
}
|
|
439
|
-
if (installed.has(rel)) return
|
|
440
|
-
return hides
|
|
625
|
+
if (installed.has(rel)) return 'tree'
|
|
626
|
+
return hides?.(rel) ? 'none' : 'outside'
|
|
441
627
|
}
|
|
442
|
-
|
|
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
|
-
//
|
|
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 = () => (
|
|
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 ===
|
|
459
|
-
|
|
460
|
-
|
|
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 =
|
|
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
|
|
471
|
-
|
|
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
|
-
|
|
490
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
685
|
+
const manifestError = memo()
|
|
508
686
|
const checkManifest = (path) => {
|
|
509
|
-
|
|
510
|
-
if (error === undefined) {
|
|
511
|
-
error = null
|
|
687
|
+
const error = manifestError(path, () => {
|
|
512
688
|
try {
|
|
513
|
-
|
|
514
|
-
if (!isPlainObject(data)) error = invalidPackageConfig(path)
|
|
689
|
+
return isPlainObject(JSON.parse(packageJSONText(host.readFile(path)))) ? null : invalidPackageConfig(path)
|
|
515
690
|
} catch (cause) {
|
|
516
|
-
|
|
691
|
+
return invalidPackageConfig(path, cause)
|
|
517
692
|
}
|
|
518
|
-
|
|
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) ===
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
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
|
|
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) ===
|
|
738
|
+
if (zoneOf(real) === 'outside') return disk.readFile(real)
|
|
555
739
|
if (nodeOf(real).type !== 'file') throw new VfsError('EISDIR', p)
|
|
556
|
-
|
|
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
|
-
|
|
561
|
-
|
|
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
|
-
|
|
593
|
-
|
|
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
|
-
|
|
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
|