@preventive/deptree 1.0.0-alpha.0 → 1.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/bin/compare.js +132 -0
  3. package/bin/deptree.js +211 -0
  4. package/bin/npmrc.js +21 -0
  5. package/cargo.d.ts +223 -0
  6. package/cargo.js +6 -0
  7. package/npm.d.ts +321 -0
  8. package/npm.js +7 -0
  9. package/package.json +93 -11
  10. package/pnpm.d.ts +422 -60
  11. package/pnpm.js +3 -8
  12. package/soldeer.d.ts +235 -0
  13. package/soldeer.js +6 -0
  14. package/src/cargo/crate.js +183 -0
  15. package/src/cargo/inputs.js +81 -0
  16. package/src/cargo/members.js +90 -0
  17. package/src/cargo/registry.js +65 -0
  18. package/src/cargo/tree.js +125 -0
  19. package/src/cargo/workspace.js +177 -0
  20. package/src/concurrent.js +32 -0
  21. package/src/error.js +8 -10
  22. package/src/glob.js +55 -0
  23. package/src/hash.js +51 -16
  24. package/src/manifest.js +52 -0
  25. package/src/matcher.js +60 -10
  26. package/src/mount.js +128 -39
  27. package/src/npm/bins.js +54 -0
  28. package/src/npm/compat.js +97 -0
  29. package/src/npm/dev-engines.js +52 -0
  30. package/src/npm/graph.js +68 -0
  31. package/src/npm/inputs.js +75 -0
  32. package/src/npm/manifests.js +130 -0
  33. package/src/npm/package.js +57 -0
  34. package/src/npm/ratio.js +34 -0
  35. package/src/npm/settings.js +67 -0
  36. package/src/npm/tree.js +139 -0
  37. package/src/npm/versions.js +20 -0
  38. package/src/npmrc.js +24 -0
  39. package/src/patch.js +26 -45
  40. package/src/pnpm/bins.js +207 -327
  41. package/src/pnpm/checks.js +8 -35
  42. package/src/pnpm/commands.js +230 -0
  43. package/src/pnpm/filename.js +19 -16
  44. package/src/pnpm/frozen.js +47 -81
  45. package/src/pnpm/gitignore.js +138 -0
  46. package/src/pnpm/glob.js +65 -0
  47. package/src/pnpm/graph.js +26 -34
  48. package/src/pnpm/hoist.js +97 -59
  49. package/src/pnpm/hoisted.js +234 -0
  50. package/src/pnpm/hook.js +37 -51
  51. package/src/pnpm/inputs.js +177 -0
  52. package/src/pnpm/install.js +140 -130
  53. package/src/pnpm/local.js +105 -34
  54. package/src/pnpm/minimatch.js +319 -0
  55. package/src/pnpm/nm-hoist-graph.js +299 -0
  56. package/src/pnpm/nm-hoist.js +149 -0
  57. package/src/pnpm/npm-packlist.js +217 -0
  58. package/src/pnpm/overrides.js +20 -53
  59. package/src/pnpm/package.js +123 -0
  60. package/src/pnpm/packlist.js +194 -0
  61. package/src/pnpm/patches.js +21 -28
  62. package/src/pnpm/projects.js +150 -101
  63. package/src/pnpm/readers.js +122 -77
  64. package/src/pnpm/settings.js +85 -93
  65. package/src/pnpm/tree.js +234 -188
  66. package/src/pnpm/uptodate.js +17 -29
  67. package/src/pnpm/workspace.js +210 -53
  68. package/src/project.js +56 -0
  69. package/src/soldeer/config.js +63 -0
  70. package/src/soldeer/git.js +52 -0
  71. package/src/soldeer/inputs.js +30 -0
  72. package/src/soldeer/tree.js +83 -0
  73. package/src/soldeer/zip.js +121 -0
  74. package/src/tarball.js +123 -125
  75. package/src/yarn1/bins.js +28 -0
  76. package/src/yarn1/compat.js +67 -0
  77. package/src/yarn1/hoist.js +249 -0
  78. package/src/yarn1/inputs.js +83 -0
  79. package/src/yarn1/manifest.js +64 -0
  80. package/src/yarn1/package.js +206 -0
  81. package/src/yarn1/peers.js +51 -0
  82. package/src/yarn1/requests.js +74 -0
  83. package/src/yarn1/resolve.js +289 -0
  84. package/src/yarn1/settings.js +95 -0
  85. package/src/yarn1/tree.js +219 -0
  86. package/yarn1.d.ts +368 -0
  87. package/yarn1.js +7 -0
  88. package/src/pnpm/npmrc.js +0 -31
  89. /package/{LICENSE → LICENSE-MIT} +0 -0
@@ -0,0 +1,194 @@
1
+ // The files pnpm's directory fetcher installs of a `file:` dependency, as its
2
+ // packlist picks them: npm-packlist 5.1.3 for pnpm 9 and 10, 10.0.4 for pnpm
3
+ // 11, and pnpm 12's own port of it, by package.json's `files`, `main` and
4
+ // `bin`, the .npmignore and .gitignore files where they read them, and their
5
+ // own rules. Of glob syntax only `*`, `?` and `**` are followed, which they
6
+ // all read alike. Refused are a link the walk comes on, which they treat
7
+ // differently, and a mode a checkout does not have under umask 022 or 002.
8
+
9
+ import { utf8toString } from '@exodus/bytes/utf8.js'
10
+ import { compareVersions, valid } from '@preventive/upstream/semver.js'
11
+ import { normalize } from '@preventive/vfs/path.js'
12
+ import { DeptreeError, quote } from '../error.js'
13
+ import { readBytes } from '../project.js'
14
+ import { bytesOf, gitignoreGlob, matched, matchedOrParents } from './gitignore.js'
15
+ import { pack10, pack11 } from './npm-packlist.js'
16
+
17
+
18
+ const MODES = new Set([0o644, 0o664, 0o755, 0o775])
19
+
20
+ // How deep a directory is walked: each entry is matched against the rules
21
+ // of every directory above it, which takes time growing with the cube.
22
+ const MAX_DEPTH = 100
23
+
24
+ // The directory by paths from it, `.` parts and all, which refuses a link
25
+ // on the way to anything it is asked of.
26
+ function viewOf(project, dir, where) {
27
+ const types = new Map()
28
+ const absolute = (rel) => `/${[dir, ...rel.split('/').filter((part) => part !== '' && part !== '.')].filter(Boolean).join('/')}`
29
+ const type = (rel) => {
30
+ const path = absolute(rel)
31
+ if (!types.has(path)) {
32
+ let found
33
+ try {
34
+ found = project.lstat(path).type
35
+ } catch (error) {
36
+ if (!['ENOENT', 'ENOTDIR'].includes(error.code)) throw error
37
+ }
38
+ if (found === 'symlink') throw new DeptreeError('a link in a directory pnpm installs a copy of is not supported', `${where}: ${quote(normalize(rel))}`)
39
+ types.set(path, found)
40
+ }
41
+ return types.get(path)
42
+ }
43
+ const view = {
44
+ // What `rel` is once normalized, as path.join would; nothing outside.
45
+ type(rel) {
46
+ const normal = normalize(rel)
47
+ if (normal === '..' || normal.startsWith('../')) return undefined
48
+ const parts = normal.split('/').filter((part) => part !== '' && part !== '.')
49
+ for (let i = 1; i < parts.length; i++) if (type(parts.slice(0, i).join('/')) !== 'directory') return undefined
50
+ const found = parts.length === 0 ? 'directory' : type(parts.join('/'))
51
+ return rel.endsWith('/') && found !== 'directory' ? undefined : found
52
+ },
53
+ entries(rel) {
54
+ if (view.type(rel) !== 'directory') return undefined
55
+ if (normalize(rel).split('/').length > MAX_DEPTH) throw new DeptreeError(`a directory nested more than ${MAX_DEPTH} deep, each of whose entries the packlists match against the rules of every directory above it, is not supported`, `${where}: ${quote(normalize(rel))}`)
56
+ return project.readdir(absolute(rel))
57
+ },
58
+ text(rel, here) {
59
+ if (view.type(rel) !== 'file') throw new DeptreeError('it is not a file, which pnpm fails to read', here)
60
+ try {
61
+ return utf8toString(readBytes(project, absolute(rel)))
62
+ } catch {
63
+ throw new DeptreeError('it is not UTF-8, which is not supported', here)
64
+ }
65
+ },
66
+ bytes: (rel) => readBytes(project, absolute(rel)),
67
+ lstat: (rel) => project.lstat(absolute(rel)),
68
+ }
69
+ return view
70
+ }
71
+
72
+ // pnpm 12's lists of names.
73
+ const VCS = new Set(['.git', '.svn', '.hg', 'CVS'])
74
+ const CRUFT = new Set(['.npmrc', 'npm-debug.log', '.DS_Store', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'])
75
+ const alwaysExcluded = (rel) => CRUFT.has(rel.split('/').at(-1)) || rel.split('/').some((part) => VCS.has(part)) || rel.endsWith('.orig')
76
+ const asciiLower = (name) => name.replaceAll(/[A-Z]/gu, (char) => char.toLowerCase())
77
+ const alwaysIncluded = (name) => asciiLower(name) === 'package.json' || ['readme', 'license', 'licence'].some((start) => asciiLower(name).startsWith(start))
78
+ const ALTERNATES = new Set(['package.yaml', 'package.json5'])
79
+ const fieldPath = (path) => path.replace(/^(?:\.\/)+/u, '').replace(/^\/+/u, '')
80
+
81
+ // pnpm 12 makes one regexp of the globs of an ignore file, or of `files`,
82
+ // and drops them all where it grows too large, which none does below twice
83
+ // this many bytes of them.
84
+ const MAX_GLOBS_12 = 16 * 1024
85
+ const checkGlobs12 = (lines, what, where) => {
86
+ if (lines.reduce((sum, line) => sum + bytesOf(line).length + 1, 0) > MAX_GLOBS_12) throw new DeptreeError(`${what} of more than ${MAX_GLOBS_12 / 1024} KiB, all of which pnpm 12 may drop as too large a regexp, is not supported`, where)
87
+ }
88
+
89
+ // pnpm 12's `files` allowlist: its matcher, or undefined where no entry is
90
+ // one, and the files it names outright.
91
+ function filesOf12(view, files, where) {
92
+ checkGlobs12(files ?? [], 'its package.json has `files`', where)
93
+ const globs = []
94
+ for (const entry of files ?? []) {
95
+ const path = fieldPath(entry)
96
+ if (path !== '') globs.push(gitignoreGlob(path.startsWith('!') ? path : `/${path}`, `${where}: files[${quote(entry)}]`))
97
+ }
98
+ const named = new Set((files ?? []).filter((entry) => !entry.startsWith('!') && !entry.endsWith('/') && !/[*?[\]{}]/u.test(entry)).map(fieldPath).filter((path) => path !== '' && !path.split('/').includes('..') && view.type(path) === 'file'))
99
+ return { globs: globs.length > 0 ? globs : undefined, named }
100
+ }
101
+
102
+ function includedBy12(globs, rel, named) {
103
+ if (globs.some((glob) => glob.whitelist) && !named.has(rel)) {
104
+ const parts = rel.split('/')
105
+ for (let i = parts.length - 1; i >= 0; i--) if (matched(globs, parts.slice(0, i).join('/'), true) === 'whitelist') return false
106
+ }
107
+ return matchedOrParents(globs, rel, false) === 'ignore'
108
+ }
109
+
110
+ // The ignore crate's walk: each directory's .npmignore and, unless the top
111
+ // has one, .gitignore, the nearest to match a path deciding, an .npmignore's
112
+ // before any .gitignore's. Neither is read where `files` has a matcher.
113
+ function walk12(view, { globs }, where) {
114
+ const useGit = view.type('.npmignore') !== 'file'
115
+ const found = []
116
+ const visit = (rel, stack) => {
117
+ const names = view.entries(rel)
118
+ const own = { dir: rel }
119
+ for (const [key, name] of [['custom', '.npmignore'], ['git', '.gitignore']]) {
120
+ if (globs === undefined && (key === 'custom' || useGit) && names.includes(name)) {
121
+ const path = rel === '' ? name : `${rel}/${name}`
122
+ const here = `${where}: ${quote(path)}`
123
+ const lines = view.text(path, here).split('\n')
124
+ checkGlobs12(lines, 'an ignore file', here)
125
+ own[key] = lines.map((line, i) => (i === 0 ? line.replace(/^\uFEFF/u, '') : line).replace(/\r$/u, '')).map((line) => gitignoreGlob(line, here)).filter(Boolean)
126
+ }
127
+ }
128
+ const chain = [own, ...stack]
129
+ for (const name of names) {
130
+ const path = rel === '' ? name : `${rel}/${name}`
131
+ if ((rel === '' && name === 'node_modules') || VCS.has(name)) continue
132
+ const isDir = view.type(path) === 'directory'
133
+ const verdict = (key) => chain.values().map((level) => level[key] === undefined ? undefined : matched(level[key], level.dir === '' ? path : path.slice(level.dir.length + 1), isDir)).find(Boolean)
134
+ if ((verdict('custom') ?? verdict('git')) === 'ignore') continue
135
+ if (isDir) visit(path, chain)
136
+ else found.push(path)
137
+ }
138
+ }
139
+ visit('', [])
140
+ return found
141
+ }
142
+
143
+ function pack12(view, manifest, where) {
144
+ const files = Array.isArray(manifest.files) ? filesOf12(view, manifest.files, where) : { named: new Set() }
145
+ const main = typeof manifest.main === 'string' ? [manifest.main] : []
146
+ const bins = typeof manifest.bin === 'string' ? [manifest.bin] : manifest.bin !== null && typeof manifest.bin === 'object' && !Array.isArray(manifest.bin) ? Object.values(manifest.bin).filter((bin) => typeof bin === 'string') : []
147
+ const fields = [...main, ...bins].map(fieldPath)
148
+ const kept = new Set(walk12(view, files, where).filter((rel) => !alwaysExcluded(rel) && !rel.startsWith('node_modules/')
149
+ && (files.globs === undefined || includedBy12(files.globs, rel, files.named) || (!rel.includes('/') && alwaysIncluded(rel)) || fields.includes(rel))))
150
+ for (const name of view.entries('')) {
151
+ if (!alwaysExcluded(name) && (alwaysIncluded(name) || ALTERNATES.has(asciiLower(name))) && view.type(name) === 'file') kept.add(name)
152
+ }
153
+ // A main or bin path that stays inside, kept as it is spelled.
154
+ for (const path of fields) {
155
+ const parts = path.split('/')
156
+ if (path === '' || path.includes('\\') || parts.includes('..') || parts[0] === '.' || alwaysExcluded(path) || view.type(path) !== 'file') continue
157
+ if (parts.some((part) => part === '' || part === '.') || parts[0] === 'node_modules') throw new DeptreeError(`main or bin names ${quote(path)}, which pnpm 12 keeps as it is spelled, and which is not supported`, where)
158
+ kept.add(path)
159
+ }
160
+ return kept
161
+ }
162
+
163
+ const bundles = (list) => Boolean(list) && !(Array.isArray(list) && list.length === 0)
164
+
165
+ function checkManifest(manifest, pnpm, view, where) {
166
+ const { files, bin } = manifest
167
+ if (bundles(manifest.bundleDependencies) || bundles(manifest.bundledDependencies)) throw new DeptreeError('a directory with bundled dependencies is not supported', where)
168
+ if (files !== undefined && files !== null && !(Array.isArray(files) && files.every((entry) => typeof entry === 'string'))) throw new DeptreeError('its package.json has `files` that is not a list of strings, which is not supported', where)
169
+ if (Array.isArray(bin) && bin.some((path) => typeof path !== 'string') && !pnpm.startsWith('11.') && !pnpm.startsWith('12.')) throw new DeptreeError('its package.json has a bin list with other than strings, which npm-packlist then reads unnormalized, and which is not supported', where)
170
+ if (!pnpm.startsWith('11.')) return
171
+ if (bin !== null && typeof bin === 'object' && Object.values(bin).some((path) => typeof path !== 'string')) throw new DeptreeError('its package.json has a bin that is not a string, which pnpm 11 fails on', where)
172
+ if (compareVersions(pnpm, '11.28.0') < 0 && view.text('package.json', where).startsWith('\uFEFF')) throw new DeptreeError('its package.json starts with a byte order mark, which pnpm 11 fails on before 11.28', where)
173
+ }
174
+
175
+ // `version` is the version that installs, as host.pnpm gives it: `v11.28.2`
176
+ // is read as 11.28.2.
177
+ export function packDirectory(project, dir, manifest, version, where) {
178
+ const pnpm = valid(version)
179
+ const view = viewOf(project, dir, where)
180
+ checkManifest(manifest, pnpm, view, where)
181
+ const major = Number(pnpm.split('.')[0])
182
+ const kept = major >= 12 ? pack12(view, manifest, where)
183
+ : major === 11 ? pack11(view, manifest, where, compareVersions(pnpm, '11.28.0') >= 0)
184
+ : pack10(view, view.text('package.json', where).startsWith('\uFEFF') ? undefined : manifest, where, major === 10 && compareVersions(pnpm, '10.31.0') >= 0 ? '5.1.9' : '5.1.6')
185
+ const files = new Map()
186
+ for (const rel of [...new Set([...kept].map((path) => path.replace(/^\.\//u, '')))].sort()) {
187
+ const here = `${where}: ${quote(rel)}`
188
+ if (rel.split('/').some((part) => part === '' || part === '.')) throw new DeptreeError('pnpm keeps it as a pattern spells it, which is not supported', here)
189
+ const { mode } = view.lstat(rel)
190
+ if (!MODES.has(mode)) throw new DeptreeError(`its mode, ${mode.toString(8)}, is not 644, 664, 755 or 775, which is not supported`, here)
191
+ files.set(rel, { data: view.bytes(rel), mode })
192
+ }
193
+ return files
194
+ }
@@ -1,29 +1,23 @@
1
- // Which patch pnpm 10 applies to a package: not the one its snapshot key
2
- // names, but the one the settings' patchedDependencies pick for its name
3
- // and version (@pnpm/patching.config's getPatchInfo): `name@version`
4
- // exactly, else the one `name@range` whose range takes the version — two
5
- // that do are refused — else `name@*` or `name` alone. Every snapshot is
6
- // held to have the patch hash that pick has, so the patch applied here by
7
- // the key is the one pnpm applies; and, stricter than pnpm, which checks
8
- // this only when it resolves, every patch configured has to be the pick of
9
- // some snapshot.
1
+ // pnpm 10 applies the patch patchedDependencies picks for a package's name and
2
+ // version (@pnpm/patching.config's getPatchInfo), not the one its snapshot key
3
+ // names, so every snapshot must name that pick's hash. Every patch must be some
4
+ // snapshot's, which pnpm checks only when resolving. pnpm 9 picks by the
5
+ // selector `name@version` as spelled, else `name`, and takes no range.
10
6
 
11
7
  import { packageKeyOf } from '@preventive/lockfile/pnpm.js'
12
- import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
8
+ import { isExactVersion, satisfies, valid, validRange } from '@preventive/upstream/semver.js'
13
9
  import { DeptreeError, quote } from '../error.js'
14
10
 
15
- // dependency-path's parse of a selector: a name and an exact version, a
16
- // name and anything else after its `@`, or neither.
11
+ // dependency-path's parse of a selector.
17
12
  function parseSelector(selector) {
18
13
  const at = selector.indexOf('@', 1)
19
14
  const rest = at === -1 ? '' : packageKeyOf(selector).slice(at + 1)
20
15
  if (rest === '') return {}
21
16
  const name = selector.slice(0, at)
22
- return valid(rest) === rest ? { name, version: rest } : { name, range: rest }
17
+ return isExactVersion(rest) ? { name, version: rest } : { name, range: rest }
23
18
  }
24
19
 
25
- // groupPatchedDependencies: by package name, the selectors that can pick
26
- // it; a later one of a kind over an earlier, as pnpm assigns them.
20
+ // groupPatchedDependencies: a later selector of a kind wins, as pnpm assigns.
27
21
  function group(selectors) {
28
22
  const groups = new Map()
29
23
  const of = (name) => {
@@ -50,12 +44,14 @@ function pick(groups, name, version, where) {
50
44
  return taking[0]?.selector ?? found.all
51
45
  }
52
46
 
53
- // `hashes` is the configured patches' hashes and paths, by selector.
54
- export function checkPatchUse(lockfile, hashes) {
55
- const groups = group(Object.keys(hashes))
47
+ // pnpm 9's getPatchInfo.
48
+ const pick9 = (hashes, name, version) => [`${name}@${version}`, name].find((selector) => Object.hasOwn(hashes, selector))
49
+
50
+ export function checkPatchUse(lockfile, hashes, major = 10) {
51
+ const groups = major < 10 ? undefined : group(Object.keys(hashes))
56
52
  const used = new Set()
57
53
  for (const [key, pkg] of Object.entries(lockfile.packages)) {
58
- const selector = pick(groups, pkg.name, pkg.version, quote(key))
54
+ const selector = major < 10 ? pick9(hashes, pkg.name, pkg.version) : pick(groups, pkg.name, pkg.version, quote(key))
59
55
  const wanted = selector === undefined ? undefined : hashes[selector].hash
60
56
  if (wanted !== pkg.patchHash) {
61
57
  throw new DeptreeError(`pnpm applies ${selector === undefined ? 'no patch' : `the patch of ${quote(selector)}`} to it, and the lockfile names ${pkg.patchHash === undefined ? 'none' : `the patch ${pkg.patchHash}`}`, quote(key))
@@ -69,7 +65,7 @@ export function checkPatchUse(lockfile, hashes) {
69
65
 
70
66
  const PATCH = '(patch_hash='
71
67
 
72
- // A key's run of balanced groups at its end, each on its own, and what is
68
+ // A key's trailing parenthesized groups, each on its own, and what is
73
69
  // before them; undefined where they do not balance.
74
70
  function splitSuffix(key) {
75
71
  const segments = []
@@ -88,8 +84,7 @@ function splitSuffix(key) {
88
84
  return { locator: key.slice(0, end), segments }
89
85
  }
90
86
 
91
- // dependency-path's parse, of what is read here: the name, the version
92
- // where it is SemVer, and the patch hash.
87
+ // dependency-path's parse, of the name, a SemVer version and the patch hash.
93
88
  function parseKey(key) {
94
89
  const at = key.indexOf('@', 1)
95
90
  if (at === -1 || at === key.length - 1) return {}
@@ -99,11 +94,9 @@ function parseKey(key) {
99
94
  return { name: key.slice(0, at), version: valid(base) === null ? undefined : base, hash }
100
95
  }
101
96
 
102
- // pnpm 11 holds what a key names to their patches down through its peers
103
- // (@pnpm/lockfile.fs's checkPatchedDepPaths), where pnpm 10 holds only the
104
- // snapshot: a peer that names a patch hash, or, unless dedupePeers leaves
105
- // them out, one of a version of a patched package, has to name the hash of
106
- // the patch picked for it. One whose hash cannot be told is refused too.
97
+ // pnpm 11's checkPatchedDepPaths (@pnpm/lockfile.fs) checks hashes down through
98
+ // a key's peers too: a peer that names one, or, unless dedupePeers leaves
99
+ // versions out, a version of a patched package.
107
100
  export function checkPeerPatches(lockfile, hashes) {
108
101
  const groups = group(Object.keys(hashes))
109
102
  const carried = lockfile.settings.dedupePeers !== true
@@ -117,7 +110,7 @@ export function checkPeerPatches(lockfile, hashes) {
117
110
  const known = lockfile.packages[key]?.version ?? version
118
111
  const found = groups.get(name)
119
112
  if (known === undefined && found !== undefined && (found.exact.size > 0 || found.ranges.length > 0)) throw uncheckable(key, where)
120
- const selector = found === undefined ? undefined : pick(groups, name, known ?? '', where)
113
+ const selector = pick(groups, name, known ?? '', where)
121
114
  if ((selector === undefined ? undefined : hashes[selector].hash) !== hash) {
122
115
  throw new DeptreeError(`the patch hash ${quote(key)} names is not the one of the patch pnpm picks for it, which pnpm 11 refuses a frozen install for`, where)
123
116
  }