@preventive/deptree 1.0.0-alpha.8 → 1.0.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/bin/compare.js +123 -0
- package/bin/deptree.js +217 -0
- package/bin/npmrc.js +21 -0
- package/cargo.d.ts +221 -0
- package/cargo.js +6 -0
- package/npm.d.ts +87 -17
- package/package.json +26 -6
- package/pnpm.d.ts +102 -28
- package/soldeer.d.ts +36 -33
- package/src/cargo/crate.js +183 -0
- package/src/cargo/inputs.js +81 -0
- package/src/cargo/members.js +90 -0
- package/src/cargo/registry.js +65 -0
- package/src/cargo/tree.js +125 -0
- package/src/cargo/workspace.js +177 -0
- package/src/concurrent.js +9 -0
- package/src/error.js +4 -2
- package/src/hash.js +14 -30
- package/src/manifest.js +1 -1
- package/src/matcher.js +1 -1
- package/src/mount.js +10 -0
- package/src/npm/package.js +44 -5
- package/src/npm/tree.js +25 -10
- package/src/patch.js +2 -3
- package/src/pnpm/gitignore.js +2 -3
- package/src/pnpm/hoisted.js +15 -0
- package/src/pnpm/package.js +11 -5
- package/src/pnpm/packlist.js +2 -2
- package/src/pnpm/tree.js +53 -27
- package/src/project.js +4 -5
- package/src/soldeer/config.js +6 -1
- package/src/soldeer/git.js +16 -1
- package/src/soldeer/tree.js +6 -12
- package/src/soldeer/zip.js +3 -4
- package/src/tarball.js +53 -38
- package/src/yarn1/package.js +51 -6
- package/src/yarn1/resolve.js +58 -11
- package/src/yarn1/tree.js +27 -15
- package/yarn1.d.ts +69 -26
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The vendor directory `cargo vendor` makes from a Cargo.lock.
|
|
2
|
+
|
|
3
|
+
import { linkCargo, parseCargoConfig, parseCargoLock, parseCargoManifest, readCargoVendor } from '@preventive/lockfile/cargo.js'
|
|
4
|
+
import { parseToml } from '@preventive/lockfile/toml.js'
|
|
5
|
+
import { Vfs } from '@preventive/vfs'
|
|
6
|
+
import { DeptreeError, quote } from '../error.js'
|
|
7
|
+
import { own } from '../manifest.js'
|
|
8
|
+
import { checkNoDir, mount, writeFiles } from '../mount.js'
|
|
9
|
+
import { decodeUtf8 } from '../project.js'
|
|
10
|
+
import { checkHost, inputsOf } from './inputs.js'
|
|
11
|
+
import { about, fetchCrates, vendoredPackages } from './registry.js'
|
|
12
|
+
import { fileOf, findWorkspace, parseAs } from './workspace.js'
|
|
13
|
+
|
|
14
|
+
const checkNoVendor = checkNoDir('vendor', 'a vendor directory')
|
|
15
|
+
|
|
16
|
+
// Of the config, [patch] is read, as cargo resolves with it; `paths`, which
|
|
17
|
+
// overrides where packages come from, and `include`, which reads more
|
|
18
|
+
// files, are refused; the rest bears on no package vendored.
|
|
19
|
+
function configOf({ text, file }) {
|
|
20
|
+
if (text === undefined) return { patch: parseCargoConfig([]).patch, file }
|
|
21
|
+
const doc = parseAs(file, () => parseToml(text))
|
|
22
|
+
for (const key of ['paths', 'include']) if (doc[key] !== undefined) throw new DeptreeError('not supported', `${file}: ${key}`)
|
|
23
|
+
return { patch: parseAs(file, () => parseCargoConfig([text])).patch, file }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Each path package by the key the lockfile gives it, `name version`: every
|
|
27
|
+
// one of the lockfile's has to be one cargo reads, and every member there.
|
|
28
|
+
function pathKeys(lock, packages) {
|
|
29
|
+
const keys = new Map()
|
|
30
|
+
const dirs = new Map()
|
|
31
|
+
for (const [dir, { manifest, member }] of packages) {
|
|
32
|
+
if (manifest.package === undefined) continue
|
|
33
|
+
const key = `${manifest.package.name} ${manifest.package.version}`
|
|
34
|
+
if (dirs.has(key)) throw new DeptreeError(`it and ${quote(fileOf(dirs.get(key)))} are both ${quote(key)}, which the lockfile names one package`, fileOf(dir))
|
|
35
|
+
if (member && lock.packages[key] === undefined) throw new DeptreeError(`the lockfile has no member ${quote(key)}: is it out of date?`, fileOf(dir))
|
|
36
|
+
keys.set(dir, key)
|
|
37
|
+
dirs.set(key, dir)
|
|
38
|
+
}
|
|
39
|
+
for (const [key, pkg] of Object.entries(lock.packages)) {
|
|
40
|
+
if (pkg.source === undefined && !dirs.has(key)) throw new DeptreeError('a path package that no manifest cargo reads here is: is the lockfile out of date?', about(key))
|
|
41
|
+
}
|
|
42
|
+
return keys
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// linkCargo ties a path dependency to a path package by name and version;
|
|
46
|
+
// it has to be the one in the directory the path leads to.
|
|
47
|
+
function checkPaths(graph, packages, keys) {
|
|
48
|
+
for (const [dir, { links }] of packages) {
|
|
49
|
+
const linked = graph.packages[keys.get(dir)]
|
|
50
|
+
if (linked === undefined) continue
|
|
51
|
+
for (const { index, dir: to, where } of links) {
|
|
52
|
+
const { resolved } = linked.dependencies[index]
|
|
53
|
+
if (resolved !== undefined && resolved !== keys.get(to)) throw new DeptreeError(`the lockfile resolves it to ${quote(resolved)}, not ${quote(keys.get(to))}, which its path leads to`, where)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// `links` are the repository and homepage the Cargo.toml cargo packed gives
|
|
59
|
+
// in [package], or [project] as cargo reads it too: strings, as
|
|
60
|
+
// parseCargoManifest holds them to, or undefined.
|
|
61
|
+
function manifestOf(crate, vendored) {
|
|
62
|
+
const where = `${about(crate.key)}: Cargo.toml`
|
|
63
|
+
const file = vendored.files.get('Cargo.toml')
|
|
64
|
+
if (file === undefined) throw new DeptreeError('its .crate has no Cargo.toml', about(crate.key))
|
|
65
|
+
const text = decodeUtf8(file.data, 'not UTF-8, which cargo fails on', where)
|
|
66
|
+
const manifest = parseAs(where, () => parseCargoManifest(text))
|
|
67
|
+
const doc = parseToml(text)
|
|
68
|
+
const pkg = own(doc, 'package') ?? own(doc, 'project')
|
|
69
|
+
return { text, manifest, links: { repository: own(pkg, 'repository'), homepage: own(pkg, 'homepage') } }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The lockfile laid over every manifest, as linkCargo holds it to them; then
|
|
73
|
+
// the vendored copies read back as cargo's directory source reads them.
|
|
74
|
+
// Gives back each crate's links, by its key.
|
|
75
|
+
function checkLock(lock, inputs, crates, vendored) {
|
|
76
|
+
const { keys } = inputs
|
|
77
|
+
const manifests = Object.create(null)
|
|
78
|
+
for (const [dir, key] of keys) if (key in lock.packages) manifests[key] = inputs.packages.get(dir).manifest
|
|
79
|
+
const texts = new Map(crates.map((crate) => [crate.key, manifestOf(crate, vendored.get(crate.key))]))
|
|
80
|
+
for (const [key, { manifest }] of texts) manifests[key] = manifest
|
|
81
|
+
const members = [...inputs.packages].filter(([dir, { member }]) => member && keys.has(dir)).map(([dir]) => keys.get(dir))
|
|
82
|
+
checkPaths(linkCargo(lock, manifests, { workspace: inputs.root, members, config: inputs.config }), inputs.packages, keys)
|
|
83
|
+
const read = readCargoVendor(lock, Object.fromEntries(crates.map((crate) => [crate.directory, { manifest: texts.get(crate.key).text, checksum: vendored.get(crate.key).checksumText }])))
|
|
84
|
+
for (const crate of crates) if (read[crate.key].directory !== crate.directory) throw new Error(`unreachable: ${crate.key} read back from ${read[crate.key].directory}`)
|
|
85
|
+
return new Map([...texts].map(([key, { links }]) => [key, links]))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Each crate's bytes are let go once its files are written, which copies them.
|
|
89
|
+
function writeTree(crates, vendored, stats) {
|
|
90
|
+
const vfs = new Vfs()
|
|
91
|
+
for (const { key, directory } of crates) {
|
|
92
|
+
const { root, dirs, files } = vendored.get(key)
|
|
93
|
+
const path = `vendor/${directory}`
|
|
94
|
+
writeFiles(vfs, path, { dirs: dirs.keys(), files }, stats)
|
|
95
|
+
for (const [dir, mode] of dirs) vfs.chmod(`/${path}/${dir}`, mode)
|
|
96
|
+
vfs.chmod(`/${path}`, root)
|
|
97
|
+
vendored.delete(key)
|
|
98
|
+
}
|
|
99
|
+
return vfs
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function buildCargoTree(options) {
|
|
103
|
+
const { host: given, vfs: into } = options ?? {}
|
|
104
|
+
if (into !== undefined && !(into instanceof Vfs)) throw new TypeError('vfs must be a Vfs, or left out')
|
|
105
|
+
const inputs = inputsOf(options ?? {})
|
|
106
|
+
const host = checkHost(given)
|
|
107
|
+
const folded = host.os === 'darwin'
|
|
108
|
+
// Refused before anything is fetched; mount checks again.
|
|
109
|
+
if (into !== undefined) checkNoVendor(into, folded)
|
|
110
|
+
const lock = parseCargoLock(inputs.lockfile)
|
|
111
|
+
const config = configOf(inputs.config)
|
|
112
|
+
const { root, packages, read } = findWorkspace(inputs.project, config, folded)
|
|
113
|
+
for (const dir of inputs.given ?? []) {
|
|
114
|
+
if (read.get(dir) === undefined) throw new DeptreeError('given, but no manifest cargo reads', `manifests[${quote(dir)}]`)
|
|
115
|
+
}
|
|
116
|
+
const crates = vendoredPackages(lock)
|
|
117
|
+
const keys = pathKeys(lock, packages)
|
|
118
|
+
const vendored = await fetchCrates(crates, host.comment)
|
|
119
|
+
const links = checkLock(lock, { root, packages, config, keys }, crates, vendored)
|
|
120
|
+
const stats = { packages: Object.keys(lock.packages).length, vendored: crates.length, files: 0, bytes: 0 }
|
|
121
|
+
// Before writeTree lets each crate go.
|
|
122
|
+
const installed = crates.map(({ key, directory, name, version, checksum }) => ({ path: `vendor/${directory}`, name, version, source: lock.packages[key].source, checksum, commit: vendored.get(key).commit, ...links.get(key) }))
|
|
123
|
+
const vfs = writeTree(crates, vendored, stats)
|
|
124
|
+
return { vfs: mount(vfs, into, folded, checkNoVendor), stats, installed }
|
|
125
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// The workspace as cargo finds it from the project's root, which is taken to
|
|
2
|
+
// be its root: the members, each path dependency of one that is in the root
|
|
3
|
+
// and not excluded a member too, and every other path package cargo reads,
|
|
4
|
+
// each manifest parsed as cargo reads it, inheriting from the root's
|
|
5
|
+
// [workspace] where cargo has it inherit.
|
|
6
|
+
|
|
7
|
+
import { LockfileError, parseCargoManifest } from '@preventive/lockfile/cargo.js'
|
|
8
|
+
import { TomlError, parseToml } from '@preventive/lockfile/toml.js'
|
|
9
|
+
import { dirname } from '@preventive/vfs/path.js'
|
|
10
|
+
import { DeptreeError, quote } from '../error.js'
|
|
11
|
+
import { UNSAFE } from '../mount.js'
|
|
12
|
+
import { readUnlinked } from './inputs.js'
|
|
13
|
+
import { checkDefaultMembers, checkLists, explicitMembers, isExcluded, parts } from './members.js'
|
|
14
|
+
|
|
15
|
+
export const fileOf = (dir) => (dir === '.' ? 'Cargo.toml' : `${dir}/Cargo.toml`)
|
|
16
|
+
const manifestPath = (dir) => [...parts(dir), 'Cargo.toml']
|
|
17
|
+
// Whether the root's [workspace] takes `dir` in: there is one, and it does
|
|
18
|
+
// not exclude it.
|
|
19
|
+
const inRoot = (workspace, dir) => workspace !== undefined && !isExcluded(workspace, manifestPath(dir))
|
|
20
|
+
|
|
21
|
+
// A path as a manifest writes it, from the directory `base`: `..` only ahead
|
|
22
|
+
// of every name, which the filesystem would otherwise walk through a name
|
|
23
|
+
// that cargo's normalize_path drops unread.
|
|
24
|
+
function resolvePath(base, path, where) {
|
|
25
|
+
if (path.startsWith('/') || UNSAFE.test(path) || /^[A-Za-z]:/u.test(path)) throw new DeptreeError(`${quote(path)} is not a relative path within the project, which is all that is read`, where)
|
|
26
|
+
const segments = parts(base)
|
|
27
|
+
let named = false
|
|
28
|
+
for (const segment of parts(path)) {
|
|
29
|
+
if (segment !== '..') {
|
|
30
|
+
named = true
|
|
31
|
+
segments.push(segment)
|
|
32
|
+
} else if (named) {
|
|
33
|
+
throw new DeptreeError(`${quote(path)} has a \`..\` after a name, which is not supported`, where)
|
|
34
|
+
} else if (segments.pop() === undefined) {
|
|
35
|
+
throw new DeptreeError(`${quote(path)} leads out of the project, which is not read`, where)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return segments.length === 0 ? '.' : segments.join('/')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function parseAs(file, parse) {
|
|
42
|
+
try {
|
|
43
|
+
return parse()
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error instanceof LockfileError || error instanceof TomlError) throw new DeptreeError(error.message, file, { cause: error })
|
|
46
|
+
throw error
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A Cargo.toml's text and TOML, or undefined where there is none.
|
|
51
|
+
function readRaw(project, dir) {
|
|
52
|
+
const file = fileOf(dir)
|
|
53
|
+
const text = readUnlinked(project, file, 'a manifest')
|
|
54
|
+
return text === undefined ? undefined : { text, doc: parseAs(file, () => parseToml(text)) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const pointerOf = (doc) => (doc.package ?? doc.project)?.workspace
|
|
58
|
+
|
|
59
|
+
const DEP_KEYS = { normal: 'dependencies', dev: 'dev-dependencies', build: 'build-dependencies' }
|
|
60
|
+
const depWhere = (dir, dep) => `${fileOf(dir)}: ${dep.target === undefined ? '' : `target.${quote(dep.target)}.`}${DEP_KEYS[dep.kind]}.${dep.name}`
|
|
61
|
+
|
|
62
|
+
// The context the steps below share: the root's manifest and [workspace],
|
|
63
|
+
// each Cargo.toml read, and each path package loaded.
|
|
64
|
+
function contextOf(project, folded) {
|
|
65
|
+
const read = new Map()
|
|
66
|
+
const raw = (dir) => {
|
|
67
|
+
if (!read.has(dir)) read.set(dir, readRaw(project, dir))
|
|
68
|
+
return read.get(dir)
|
|
69
|
+
}
|
|
70
|
+
const top = raw('.')
|
|
71
|
+
if (top === undefined) throw new DeptreeError('the project has no Cargo.toml')
|
|
72
|
+
if (pointerOf(top.doc) !== undefined) throw new DeptreeError('a root that points to a workspace above it, which is not read, is not supported', 'Cargo.toml: package.workspace')
|
|
73
|
+
const root = parseAs('Cargo.toml', () => parseCargoManifest(top.text))
|
|
74
|
+
if (root.workspace !== undefined) checkLists(root.workspace)
|
|
75
|
+
return { project, folded, read, raw, root, workspace: root.workspace, packages: new Map() }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Where cargo finds the workspace of the package in `dir`, below the root:
|
|
79
|
+
// its own [workspace], the root's, or none. A package.workspace pointing
|
|
80
|
+
// elsewhere than the root, and a [workspace] between the two, are refused.
|
|
81
|
+
function workspaceOf({ raw, workspace }, dir) {
|
|
82
|
+
if (raw(dir).doc.workspace !== undefined) return 'own'
|
|
83
|
+
for (let here = dir; here !== '.'; here = dirname(here)) {
|
|
84
|
+
const doc = raw(here)?.doc
|
|
85
|
+
if (doc === undefined) continue
|
|
86
|
+
if (doc.workspace !== undefined) throw new DeptreeError('a [workspace] between a package and the root, one workspace in another, is not supported', fileOf(here))
|
|
87
|
+
const pointer = pointerOf(doc)
|
|
88
|
+
if (pointer === undefined) continue
|
|
89
|
+
const where = `${fileOf(here)}: package.workspace`
|
|
90
|
+
if (typeof pointer !== 'string' || resolvePath(here, pointer, where) !== '.' || workspace === undefined) throw new DeptreeError('a package.workspace pointing elsewhere than the root is not supported', where)
|
|
91
|
+
return 'root'
|
|
92
|
+
}
|
|
93
|
+
return inRoot(workspace, dir) ? 'root' : 'none'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// A path package's manifest, and where its path dependencies lead: of a
|
|
97
|
+
// member every kind, of another package all but dev-dependencies, which
|
|
98
|
+
// cargo resolves for members alone. An inherited dependency's path is from
|
|
99
|
+
// the root of the workspace it is inherited from.
|
|
100
|
+
function load(context, dir, member, where) {
|
|
101
|
+
const { raw, root, packages } = context
|
|
102
|
+
if (raw(dir) === undefined) throw new DeptreeError(`${quote(fileOf(dir))} is not there, which cargo fails on`, where)
|
|
103
|
+
const inherits = dir === '.' ? 'own' : workspaceOf(context, dir)
|
|
104
|
+
if (member && dir !== '.' && inherits !== 'root') throw new DeptreeError('a member with a [workspace] of its own, which cargo refuses', fileOf(dir))
|
|
105
|
+
const manifest = dir === '.' ? root : parseAs(fileOf(dir), () => parseCargoManifest(raw(dir).text, inherits === 'root' ? root : undefined))
|
|
106
|
+
if (manifest.package === undefined && dir !== '.') throw new DeptreeError('a manifest with no [package], which cargo fails on as a path dependency', fileOf(dir))
|
|
107
|
+
const links = []
|
|
108
|
+
for (const [index, dep] of (manifest.package?.dependencies ?? []).entries()) {
|
|
109
|
+
if (dep.source.type !== 'path' || (!member && dep.kind === 'dev')) continue
|
|
110
|
+
const here = depWhere(dir, dep)
|
|
111
|
+
links.push({ index, dir: resolvePath(dep.inherited && inherits === 'root' ? '.' : dir, dep.source.path, here), where: here })
|
|
112
|
+
}
|
|
113
|
+
packages.set(dir, { manifest, member, links })
|
|
114
|
+
return links
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Those `members` takes and the root, then each path dependency of one that
|
|
118
|
+
// is in the root and not excluded, of any kind. What else they lead to is
|
|
119
|
+
// given back, for the packages that are not members.
|
|
120
|
+
function loadMembers(context) {
|
|
121
|
+
const { project, folded, workspace } = context
|
|
122
|
+
const members = new Set()
|
|
123
|
+
const pending = []
|
|
124
|
+
const others = []
|
|
125
|
+
const join = ({ dir, where }) => {
|
|
126
|
+
if (members.has(dir)) return
|
|
127
|
+
members.add(dir)
|
|
128
|
+
pending.push({ dir, where })
|
|
129
|
+
}
|
|
130
|
+
const listed = workspace === undefined ? [] : explicitMembers(project, workspace, folded)
|
|
131
|
+
if (workspace !== undefined) {
|
|
132
|
+
for (const dir of listed) if (inRoot(workspace, dir)) join({ dir, where: 'Cargo.toml: workspace.members' })
|
|
133
|
+
if (isExcluded(workspace, ['Cargo.toml'])) throw new DeptreeError('the root is excluded from its own workspace, which cargo refuses', 'Cargo.toml: workspace.exclude')
|
|
134
|
+
}
|
|
135
|
+
join({ dir: '.', where: 'Cargo.toml' })
|
|
136
|
+
while (pending.length > 0) {
|
|
137
|
+
const { dir, where } = pending.shift()
|
|
138
|
+
for (const link of load(context, dir, true, where)) {
|
|
139
|
+
if (inRoot(workspace, link.dir)) join(link)
|
|
140
|
+
else others.push(link)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (workspace !== undefined) checkDefaultMembers(project, workspace, members, new Set(listed), folded)
|
|
144
|
+
return others
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Each [patch] from a path, the root's from the root and the config's from
|
|
148
|
+
// the directory its .cargo is in, which is the root too.
|
|
149
|
+
function patchesOf(root, config) {
|
|
150
|
+
const tables = [
|
|
151
|
+
...Object.entries(root.patch).map(([table, specs]) => ({ table, specs, file: 'Cargo.toml' })),
|
|
152
|
+
...Object.entries(config.patch).map(([table, specs]) => ({ table, specs, file: config.file })),
|
|
153
|
+
]
|
|
154
|
+
return tables.flatMap(({ table, specs, file }) => Object.entries(specs).filter(([, spec]) => spec.source.type === 'path').map(([name, spec]) => {
|
|
155
|
+
const where = `${file}: patch.${quote(table)}.${name}`
|
|
156
|
+
return { dir: resolvePath('.', spec.source.path, where), name: spec.package, where }
|
|
157
|
+
}))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// `config` is what parseCargoConfig gives, with the `file` it was read from.
|
|
161
|
+
// `read` is each Cargo.toml read, by its directory: undefined where there is
|
|
162
|
+
// none.
|
|
163
|
+
export function findWorkspace(project, config, folded) {
|
|
164
|
+
const context = contextOf(project, folded)
|
|
165
|
+
const others = loadMembers(context)
|
|
166
|
+
const patches = patchesOf(context.root, config)
|
|
167
|
+
others.push(...patches)
|
|
168
|
+
while (others.length > 0) {
|
|
169
|
+
const { dir, where } = others.shift()
|
|
170
|
+
if (!context.packages.has(dir)) others.push(...load(context, dir, false, where))
|
|
171
|
+
}
|
|
172
|
+
for (const { dir, name, where } of patches) {
|
|
173
|
+
const found = context.packages.get(dir).manifest.package.name
|
|
174
|
+
if (found !== name) throw new DeptreeError(`${quote(fileOf(dir))} is of ${quote(found)}, not ${quote(name)}, which cargo fails on`, where)
|
|
175
|
+
}
|
|
176
|
+
return { root: context.root, packages: context.packages, read: context.read }
|
|
177
|
+
}
|
package/src/concurrent.js
CHANGED
|
@@ -21,3 +21,12 @@ export async function eachConcurrently(items, job, whereOf) {
|
|
|
21
21
|
}))
|
|
22
22
|
if (failure !== undefined) throw failure.error
|
|
23
23
|
}
|
|
24
|
+
|
|
25
|
+
// Each of `values` awaited at once, the first failure by position thrown once
|
|
26
|
+
// all have ended, as eachConcurrently throws one.
|
|
27
|
+
export async function settled(values) {
|
|
28
|
+
const results = await Promise.allSettled(values)
|
|
29
|
+
const failed = results.find(({ status }) => status === 'rejected')
|
|
30
|
+
if (failed !== undefined) throw failed.reason
|
|
31
|
+
return results.map(({ value }) => value)
|
|
32
|
+
}
|
package/src/error.js
CHANGED
|
@@ -12,9 +12,11 @@ export const refusalOf = (error, where) => (error instanceof DeptreeError ? erro
|
|
|
12
12
|
|
|
13
13
|
// Escaped so that a name cannot act on a terminal or reorder what is shown.
|
|
14
14
|
const UNSHOWN = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}]/gu
|
|
15
|
+
export const escaped = (text) => text.replaceAll(UNSHOWN, (char) => `\\u${char.codePointAt(0).toString(16).padStart(4, '0')}`)
|
|
16
|
+
|
|
15
17
|
export function quote(text) {
|
|
16
|
-
const cut = text.length > 200 ? `${text.slice(0, text.codePointAt(199) > 0xFFFF ? 199 : 200)}
|
|
17
|
-
return JSON.stringify(cut)
|
|
18
|
+
const cut = text.length > 200 ? `${text.slice(0, text.codePointAt(199) > 0xFFFF ? 199 : 200)}\u2026` : text
|
|
19
|
+
return escaped(JSON.stringify(cut))
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
export function difference(a, b, [aSide, bSide], same = (x, y) => x === y) {
|
package/src/hash.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
+
import { toBase32 } from '@exodus/bytes/base32.js'
|
|
2
|
+
import { toBase64 } from '@exodus/bytes/base64.js'
|
|
3
|
+
import { toHex } from '@exodus/bytes/hex.js'
|
|
4
|
+
import { utf8fromString } from '@exodus/bytes/utf8.js'
|
|
1
5
|
import { DeptreeError } from './error.js'
|
|
2
6
|
|
|
3
|
-
const encoder = new TextEncoder()
|
|
4
|
-
|
|
5
7
|
const digest = async (algorithm, bytes) => new Uint8Array(await crypto.subtle.digest(algorithm, bytes))
|
|
6
|
-
const hex = (bytes) => Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
7
|
-
const base64 = (bytes) => btoa(String.fromCodePoint(...bytes))
|
|
8
8
|
|
|
9
|
-
// Only well-formed text is hashed:
|
|
10
|
-
//
|
|
9
|
+
// Only well-formed text is hashed: a lone surrogate written as U+FFFD would
|
|
10
|
+
// hash two different strings the same.
|
|
11
11
|
function utf8(text, where) {
|
|
12
12
|
if (typeof text !== 'string' || !text.isWellFormed()) throw new DeptreeError('expected well-formed text to hash', where)
|
|
13
|
-
return
|
|
13
|
+
return utf8fromString(text)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export const sha256Hex = async (text, where) =>
|
|
16
|
+
export const sha256Hex = async (text, where) => toHex(await digest('SHA-256', utf8(text, where)))
|
|
17
17
|
|
|
18
18
|
// MD5, which Web Crypto has not, as RFC 1321 has it.
|
|
19
19
|
const SHIFTS = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21]
|
|
@@ -45,30 +45,14 @@ function md5(input) {
|
|
|
45
45
|
return bytes
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
let text = ''
|
|
52
|
-
let bits = 0
|
|
53
|
-
let value = 0
|
|
54
|
-
for (const byte of bytes) {
|
|
55
|
-
value = (value << 8) | byte
|
|
56
|
-
bits += 8
|
|
57
|
-
while (bits >= 5) {
|
|
58
|
-
bits -= 5
|
|
59
|
-
text += BASE32[(value >>> bits) & 31]
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return bits > 0 ? text + BASE32[(value << (5 - bits)) & 31] : text
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// pnpm 9's createBase32Hash: the MD5 of the text's UTF-8, in base32.
|
|
66
|
-
export const md5Base32 = (text, where) => base32(md5(utf8(text, where)))
|
|
48
|
+
// pnpm 9's createBase32Hash: the MD5 of the text's UTF-8, in RFC 4648's
|
|
49
|
+
// base32, lowercased and unpadded.
|
|
50
|
+
export const md5Base32 = (text, where) => toBase32(md5(utf8(text, where)), { padding: false }).toLowerCase()
|
|
67
51
|
|
|
68
|
-
export const matchesIntegrity = async (bytes, integrity) => integrity === `sha512-${
|
|
52
|
+
export const matchesIntegrity = async (bytes, integrity) => integrity === `sha512-${toBase64(await digest('SHA-512', bytes))}`
|
|
69
53
|
|
|
70
54
|
// As yarn 1 records a tarball's after the `#` of its URL.
|
|
71
|
-
export const sha1Hex = async (bytes) =>
|
|
55
|
+
export const sha1Hex = async (bytes) => toHex(await digest('SHA-1', bytes))
|
|
72
56
|
|
|
73
57
|
// As soldeer.lock records a zip's checksum.
|
|
74
|
-
export const bytesSha256Hex = async (bytes) =>
|
|
58
|
+
export const bytesSha256Hex = async (bytes) => toHex(await digest('SHA-256', bytes))
|
package/src/manifest.js
CHANGED
|
@@ -28,7 +28,7 @@ export function readManifest(text, where) {
|
|
|
28
28
|
if (typeof text !== 'string') throw new TypeError(`${where} must be the text of a package.json`)
|
|
29
29
|
let manifest
|
|
30
30
|
try {
|
|
31
|
-
manifest = JSON.parse(text.replace(
|
|
31
|
+
manifest = JSON.parse(text.replace(/^\uFEFF/u, ''))
|
|
32
32
|
} catch (error) {
|
|
33
33
|
throw new DeptreeError(`not JSON: ${error.message}`, where, { cause: error })
|
|
34
34
|
}
|
package/src/matcher.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
// Whether a text matches `pattern`, `*` any run of what `any` takes and,
|
|
6
6
|
// with `one`, `?` any one code point; anything else itself. The places of
|
|
7
|
-
// `pattern` a text reaches are kept a code point at a time, in O(n
|
|
7
|
+
// `pattern` a text reaches are kept a code point at a time, in O(n*m) for
|
|
8
8
|
// any pattern, where a regexp of k `*`s backtracks in O(n^k): a glob from a
|
|
9
9
|
// project's own files is no one's to stall.
|
|
10
10
|
export function wildcard(pattern, { one = false, any = () => true } = {}) {
|
package/src/mount.js
CHANGED
|
@@ -21,6 +21,16 @@ export function checkNoModules(vfs, folded) {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
// The same for a directory of `name` at the root alone, as Soldeer's
|
|
25
|
+
// dependencies and cargo's vendor: `what` it is called.
|
|
26
|
+
export const checkNoDir = (name, what) => (vfs, folded) => {
|
|
27
|
+
for (const there of vfs.readdir('/')) {
|
|
28
|
+
if (there === name || (folded && fold(there) === name)) {
|
|
29
|
+
throw new DeptreeError(`${what} is there already, which is neither kept beside the tree nor removed`, where(`/${there}`))
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
function checkCollisions(vfs) {
|
|
25
35
|
const [clash] = vfs.collisions(fold)
|
|
26
36
|
if (clash !== undefined) throw new DeptreeError(`${quote(clash.names[0])} and ${quote(clash.names[1])} are one name on macOS`, quote(clash.path))
|
package/src/npm/package.js
CHANGED
|
@@ -22,16 +22,54 @@ export function registryTarball({ name, version, resolution }, where) {
|
|
|
22
22
|
|
|
23
23
|
const FILES = new Set(['file', 'contiguous-file'])
|
|
24
24
|
|
|
25
|
+
// The headers that stand ahead of an entry: pax, global, long name, long link.
|
|
26
|
+
const EXTENDED = new Set([0x78, 0x67, 0x4c, 0x4b])
|
|
27
|
+
const PAX = 0x78
|
|
28
|
+
const BLOCK = 512
|
|
29
|
+
const blocks = (size) => BLOCK * Math.ceil(size / BLOCK)
|
|
30
|
+
|
|
31
|
+
// An extended header's size as the archive reader read it, which held it to
|
|
32
|
+
// 1 MiB: base 256, or octal digits to a NUL, a blank field 0.
|
|
33
|
+
function extendedSize(tar, at) {
|
|
34
|
+
const field = tar.subarray(at + 124, at + 136)
|
|
35
|
+
if (field[0] === 0x80 || field[0] === 0xff) return field.subarray(1).reduce((size, byte) => size * 256 + byte, 0)
|
|
36
|
+
const digits = /^ *([0-7]*)/u.exec(String.fromCodePoint(...field).split('\0')[0])[1]
|
|
37
|
+
return digits === '' ? 0 : Number.parseInt(digits, 8)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Every tar release takes a pax size over the header's. Up to 7.5.15, as
|
|
41
|
+
// npm 10.9.8 and 11.16 and before bundle it, tar reads it into each header
|
|
42
|
+
// after the pax one up to its entry too, so a long name, a long link or a
|
|
43
|
+
// global header between them is read by it, and all that follows otherwise
|
|
44
|
+
// than later releases read it. The archive reader has checked the layout
|
|
45
|
+
// whole, so each header is where the sizes it took say.
|
|
46
|
+
function checkPaxSizes(tar, entries, where) {
|
|
47
|
+
if (!entries.some(({ pax }) => pax.has('size'))) return
|
|
48
|
+
let at = 0
|
|
49
|
+
for (const { storedName, data, pax } of entries) {
|
|
50
|
+
let afterPax = false
|
|
51
|
+
let between = false
|
|
52
|
+
for (let flag = tar[at + 156]; EXTENDED.has(flag); flag = tar[at + 156]) {
|
|
53
|
+
between ||= afterPax
|
|
54
|
+
afterPax ||= flag === PAX
|
|
55
|
+
at += BLOCK + blocks(extendedSize(tar, at))
|
|
56
|
+
}
|
|
57
|
+
if (between && pax.has('size')) throw new DeptreeError(`${quote(storedName)} has a pax size ahead of another extended header, which tar's releases read otherwise`, where)
|
|
58
|
+
at += BLOCK + blocks(data.length)
|
|
59
|
+
}
|
|
60
|
+
if (tar.length < at + 2 * BLOCK || tar.subarray(at, at + 2 * BLOCK).some((byte) => byte !== 0)) throw new Error('unreachable: no end of the archive where the archive reader found one')
|
|
61
|
+
}
|
|
62
|
+
|
|
25
63
|
function unpack(entries, where) {
|
|
26
64
|
const files = new Map()
|
|
27
65
|
const ignores = new Set()
|
|
28
66
|
let top
|
|
29
|
-
for (const { storedName, storedLinkname, type, mode, data,
|
|
67
|
+
for (const { storedName, storedLinkname, type, mode, data, globalPax } of entries) {
|
|
30
68
|
const segments = storedName.replace(/(?<=.)\/$/u, '').split('/')
|
|
31
69
|
if (segments.length > 1024 || /[\\\0]/u.test(storedName) || segments.some((segment) => segment === '' || segment === '.' || segment === '..')) {
|
|
32
70
|
throw new DeptreeError(`${quote(storedName)} is a name tar reads otherwise`, where)
|
|
33
71
|
}
|
|
34
|
-
if (globalPax.size > 0
|
|
72
|
+
if (globalPax.size > 0) throw new DeptreeError(`${quote(storedName)} has a pax header tar's releases read otherwise`, where)
|
|
35
73
|
if (top !== undefined && segments[0] !== top) throw new DeptreeError('the tarball has entries under more than one directory', where)
|
|
36
74
|
top = segments[0]
|
|
37
75
|
if (!FILES.has(type)) continue
|
|
@@ -50,8 +88,9 @@ function unpack(entries, where) {
|
|
|
50
88
|
return withDirs(files, where)
|
|
51
89
|
}
|
|
52
90
|
|
|
53
|
-
export async function fetchNpmPackage({ name, version, integrity }, where, check) {
|
|
54
|
-
const { bytes, entries, inflated } = await fetchTarball(name, version, integrity, where)
|
|
91
|
+
export async function fetchNpmPackage({ name, version, integrity }, where, check, fetching) {
|
|
92
|
+
const { bytes, tar, entries, inflated, about } = await fetchTarball(name, version, integrity, where, fetching)
|
|
55
93
|
await check?.(bytes, inflated, where)
|
|
56
|
-
|
|
94
|
+
checkPaxSizes(tar, entries, where)
|
|
95
|
+
return { ...unpack(entries, where), about }
|
|
57
96
|
}
|
package/src/npm/tree.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// The tree `npm ci --ignore-scripts` makes, but node_modules/.bin and npm's
|
|
2
|
-
// node_modules/.package-lock.json.
|
|
2
|
+
// node_modules/.package-lock.json. With `sizeEstimateOnly`, its directories
|
|
3
|
+
// and links alone, each package's sized by its version document, of which
|
|
4
|
+
// nothing is fetched but those documents.
|
|
3
5
|
|
|
4
6
|
import { parseNpmLockfile } from '@preventive/lockfile/npm.js'
|
|
5
7
|
import { Vfs } from '@preventive/vfs'
|
|
@@ -8,6 +10,7 @@ import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
|
|
|
8
10
|
import { eachConcurrently } from '../concurrent.js'
|
|
9
11
|
import { DeptreeError, quote } from '../error.js'
|
|
10
12
|
import { checkNoModules, isInside, makeDirs, mount, writeFiles, writeLink } from '../mount.js'
|
|
13
|
+
import { fetchSize, fetchingOf } from '../tarball.js'
|
|
11
14
|
import { fixBins } from './bins.js'
|
|
12
15
|
import { skippedOf } from './compat.js'
|
|
13
16
|
import { graphOf } from './graph.js'
|
|
@@ -47,8 +50,9 @@ function checkSpecs(lockfile, host) {
|
|
|
47
50
|
}
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
// Every URL is checked before any is fetched.
|
|
51
|
-
|
|
53
|
+
// Every URL is checked before any is fetched. With `sizeEstimateOnly`, a
|
|
54
|
+
// package's size alone, with no files.
|
|
55
|
+
async function fetchAll(nodes, host, fetching) {
|
|
52
56
|
const tarballs = new Map()
|
|
53
57
|
for (const node of nodes) {
|
|
54
58
|
const tarball = registryTarball(node.pkg, whereOf(node))
|
|
@@ -57,8 +61,9 @@ async function fetchAll(nodes, host) {
|
|
|
57
61
|
tarballs.get(key).nodes.push(node)
|
|
58
62
|
}
|
|
59
63
|
const fetched = new Map()
|
|
64
|
+
const fetch = (tarball, where) => (fetching.sizeEstimateOnly ? fetchSize(tarball.name, tarball.version, tarball.integrity, where, fetching) : fetchNpmPackage(tarball, where, host.ratio ? checkRatio : undefined, fetching))
|
|
60
65
|
await eachConcurrently(tarballs.values(), async ({ tarball, where, nodes: copies }) => {
|
|
61
|
-
const pkg = { ...await
|
|
66
|
+
const pkg = { ...await fetch(tarball, where), integrity: tarball.integrity }
|
|
62
67
|
for (const node of copies) fetched.set(node, pkg)
|
|
63
68
|
}, ({ where }) => where)
|
|
64
69
|
return { fetched, tarballs: tarballs.size }
|
|
@@ -76,11 +81,18 @@ function residueOf(nodes, skipped) {
|
|
|
76
81
|
return dirs
|
|
77
82
|
}
|
|
78
83
|
|
|
79
|
-
|
|
84
|
+
// With `sizeEstimateOnly`, each package's directory alone is made, and its
|
|
85
|
+
// size counted.
|
|
86
|
+
function writeTree({ lockfile, kept, fetched, changed, residue, sizeEstimateOnly }) {
|
|
80
87
|
const vfs = new Vfs()
|
|
81
88
|
const stats = { files: 0, bytes: 0, links: 0 }
|
|
82
89
|
for (const node of kept) {
|
|
83
|
-
const { files, dirs } = fetched.get(node)
|
|
90
|
+
const { files, dirs, size } = fetched.get(node)
|
|
91
|
+
if (sizeEstimateOnly) {
|
|
92
|
+
makeDirs(vfs, node.location)
|
|
93
|
+
stats.bytes += size ?? 0
|
|
94
|
+
continue
|
|
95
|
+
}
|
|
84
96
|
const fixed = changed.get(node.location)
|
|
85
97
|
writeFiles(vfs, node.location, { dirs, files: fixed === undefined ? files : new Map([...files].map(([path, file]) => [path, fixed.get(path) ?? file])) }, stats)
|
|
86
98
|
}
|
|
@@ -106,6 +118,8 @@ function rootListsOf(text) {
|
|
|
106
118
|
export async function buildNpmTree(options) {
|
|
107
119
|
const { host: given, vfs: into } = options ?? {}
|
|
108
120
|
if (into !== undefined && !(into instanceof Vfs)) throw new TypeError('vfs must be a Vfs, or left out')
|
|
121
|
+
const fetching = fetchingOf(options ?? {})
|
|
122
|
+
if (fetching.sizeEstimateOnly && into !== undefined) throw new TypeError('vfs is not taken with sizeEstimateOnly, whose tree has no files to mount')
|
|
109
123
|
const host = checkHost(given)
|
|
110
124
|
const folded = host.os === 'darwin'
|
|
111
125
|
const inputs = inputsOf(options ?? {}, folded)
|
|
@@ -122,15 +136,16 @@ export async function buildNpmTree(options) {
|
|
|
122
136
|
const nodes = graphOf(lockfile, manifests)
|
|
123
137
|
const skipped = skippedOf(nodes, host, settings)
|
|
124
138
|
const kept = [...nodes.values()].filter((node) => node.kind === 'package' && !skipped.has(node))
|
|
125
|
-
const { fetched, tarballs } = await fetchAll(kept, host)
|
|
126
|
-
const changed = settings.binLinks ? fixBins(kept, fetched) : new Map()
|
|
139
|
+
const { fetched, tarballs } = await fetchAll(kept, host, fetching)
|
|
140
|
+
const changed = settings.binLinks && !fetching.sizeEstimateOnly ? fixBins(kept, fetched) : new Map()
|
|
127
141
|
const residue = host.reuse ? residueOf(nodes, skipped) : new Set()
|
|
128
|
-
const { vfs, stats: written } = writeTree({ lockfile, kept, fetched, changed, residue })
|
|
142
|
+
const { vfs, stats: written } = writeTree({ lockfile, kept, fetched, changed, residue, sizeEstimateOnly: fetching.sizeEstimateOnly })
|
|
129
143
|
const packages = Object.keys(lockfile.packages).length
|
|
130
144
|
const stats = { packages, installed: kept.length, skipped: packages - kept.length, tarballs, ...written }
|
|
131
145
|
const installed = kept.map((node) => {
|
|
132
146
|
const { name, version, dev, optional, devOptional, peer } = node.pkg
|
|
133
|
-
|
|
147
|
+
const { integrity, about, size } = fetched.get(node)
|
|
148
|
+
return { path: node.location, name, version, integrity, ...about, dev, optional, devOptional, peer, ...fetching.sizeEstimateOnly ? { size } : {} }
|
|
134
149
|
})
|
|
135
150
|
return { vfs: mount(vfs, into, folded), stats, installed }
|
|
136
151
|
}
|
package/src/patch.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// inserts after a line with no context a line early; a patch that needs any
|
|
6
6
|
// of that is refused, so one applied here leaves the file as pnpm would.
|
|
7
7
|
|
|
8
|
+
import { utf8fromString } from '@exodus/bytes/utf8.js'
|
|
8
9
|
import { applyChangeSet, parseDiff } from '@preventive/diff'
|
|
9
10
|
import { DeptreeError, quote } from './error.js'
|
|
10
11
|
import { UNSAFE, isInside } from './mount.js'
|
|
@@ -96,11 +97,9 @@ function applyTo(text, { hunks, blocks, where }) {
|
|
|
96
97
|
return applyChangeSet(text, blocks)
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
const encoder = new TextEncoder()
|
|
100
|
-
|
|
101
100
|
function bytesOf(text, where) {
|
|
102
101
|
if (!text.isWellFormed()) throw new DeptreeError('makes a file that is not well-formed text', where)
|
|
103
|
-
return
|
|
102
|
+
return utf8fromString(text)
|
|
104
103
|
}
|
|
105
104
|
|
|
106
105
|
const parentsOf = (path) => path.split('/').slice(0, -1).map((_, index, names) => names.slice(0, index + 1).join('/'))
|
package/src/pnpm/gitignore.js
CHANGED
|
@@ -3,15 +3,14 @@
|
|
|
3
3
|
// and all, from the directory of the file it is in. Only `*`, `?` and `**`
|
|
4
4
|
// are followed of glob syntax, as in minimatch.js.
|
|
5
5
|
|
|
6
|
+
import { utf8fromStringLoose } from '@exodus/bytes/utf8.js'
|
|
6
7
|
import { checkGlob, checkLength } from './minimatch.js'
|
|
7
8
|
|
|
8
9
|
// What Rust's trim_end takes off: White_Space.
|
|
9
10
|
const TRAILING = /[\t-\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]+$/u
|
|
10
11
|
|
|
11
|
-
const encoder = new TextEncoder()
|
|
12
|
-
|
|
13
12
|
// A path as its UTF-8 bytes, as globset matches it.
|
|
14
|
-
export const bytesOf = (path) =>
|
|
13
|
+
export const bytesOf = (path) => utf8fromStringLoose(path)
|
|
15
14
|
|
|
16
15
|
const SLASH = 0x2F
|
|
17
16
|
const NEWLINE = 0x0A
|