@preventive/deptree 1.0.0-alpha.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/LICENSE +21 -0
- package/package.json +71 -0
- package/pnpm.d.ts +227 -0
- package/pnpm.js +12 -0
- package/src/error.js +32 -0
- package/src/hash.js +23 -0
- package/src/matcher.js +34 -0
- package/src/mount.js +63 -0
- package/src/patch.js +152 -0
- package/src/pnpm/bins.js +436 -0
- package/src/pnpm/checks.js +59 -0
- package/src/pnpm/filename.js +26 -0
- package/src/pnpm/frozen.js +213 -0
- package/src/pnpm/graph.js +54 -0
- package/src/pnpm/hoist.js +109 -0
- package/src/pnpm/hook.js +106 -0
- package/src/pnpm/install.js +229 -0
- package/src/pnpm/local.js +49 -0
- package/src/pnpm/npmrc.js +31 -0
- package/src/pnpm/overrides.js +128 -0
- package/src/pnpm/patches.js +142 -0
- package/src/pnpm/projects.js +240 -0
- package/src/pnpm/readers.js +270 -0
- package/src/pnpm/settings.js +228 -0
- package/src/pnpm/tree.js +288 -0
- package/src/pnpm/uptodate.js +87 -0
- package/src/pnpm/workspace.js +73 -0
- package/src/tarball.js +152 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Which directories pnpm 10 takes for a workspace's projects, all of them
|
|
2
|
+
// under one lockfile: the root, always, and each directory whose
|
|
3
|
+
// package.json a glob of pnpm-workspace.yaml's `packages` takes, as
|
|
4
|
+
// @pnpm/fs.find-packages globs for them with tinyglobby — `<glob>/
|
|
5
|
+
// package.json`, less anything under node_modules or bower_components.
|
|
6
|
+
// Without `packages`, or without pnpm-workspace.yaml, the root is the only
|
|
7
|
+
// project. pnpm finds them on disk; here they are what is given, and each
|
|
8
|
+
// has to be one pnpm would find. Those it would find and are not given
|
|
9
|
+
// cannot be seen here, and have to be given: pnpm refuses a frozen install
|
|
10
|
+
// for one the lockfile has no importer for, and installs every one.
|
|
11
|
+
//
|
|
12
|
+
// A glob is read as tinyglobby reads it, normalized as a path first, as
|
|
13
|
+
// far as `*` and `**` go: `*` any run of characters in one directory's
|
|
14
|
+
// name, `**` a whole name for any number of directories, neither taking a
|
|
15
|
+
// name that starts with a dot, which only a glob that spells it takes; `!`
|
|
16
|
+
// in front leaves out what it takes, wherever it is in the list. Anything
|
|
17
|
+
// else a glob can say — `?`, a class, a brace, an extglob, an escape — is
|
|
18
|
+
// refused, and so is a glob reaching outside the workspace's directory.
|
|
19
|
+
// pnpm 11 leaves out what a `!` glob takes with micromatch too, a name
|
|
20
|
+
// starting with a dot and all.
|
|
21
|
+
|
|
22
|
+
import { DeptreeError, quote } from '../error.js'
|
|
23
|
+
import { escape } from '../matcher.js'
|
|
24
|
+
|
|
25
|
+
const UNSUPPORTED = /[?[\]{}()\\]/u
|
|
26
|
+
|
|
27
|
+
// tinyglobby's path.posix.normalize of a glob: empty and `.` names
|
|
28
|
+
// dropped, and `..` taking the name before it with it.
|
|
29
|
+
function normalizeGlob(glob, where) {
|
|
30
|
+
const names = []
|
|
31
|
+
for (const name of glob.split('/')) {
|
|
32
|
+
if (name === '' || name === '.') continue
|
|
33
|
+
if (name !== '..') names.push(name)
|
|
34
|
+
else if (names.length === 0) throw new DeptreeError(`${quote(glob)} reaches outside the workspace's directory, which is not supported`, where)
|
|
35
|
+
else names.pop()
|
|
36
|
+
}
|
|
37
|
+
return names
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// A name's pattern: `*` any run of characters, but not a leading dot
|
|
41
|
+
// unless `dot`.
|
|
42
|
+
const NAME = (name, dot) => `${name.startsWith('*') && !dot ? '(?!\\.)' : ''}${name.split('*').map(escape).join('[^/]*')}`
|
|
43
|
+
// `**`: any number of names, none with a leading dot unless `dot`, each
|
|
44
|
+
// with its `/`.
|
|
45
|
+
const GLOBSTAR = (dot) => `(?:${dot ? '' : '(?!\\.)'}[^/]+/)*`
|
|
46
|
+
|
|
47
|
+
function compile(glob, where, dot) {
|
|
48
|
+
const names = normalizeGlob(`${glob}/package.json`, where)
|
|
49
|
+
if (glob === '' || glob.startsWith('/') || UNSUPPORTED.test(glob) || glob.includes('!') || names.some((name) => name !== '**' && name.includes('**'))) {
|
|
50
|
+
throw new DeptreeError(`${quote(glob)} is not supported: only \`*\`, \`**\` as a whole name and a leading \`!\` are`, where)
|
|
51
|
+
}
|
|
52
|
+
// The last name is package.json, which a `/` never follows.
|
|
53
|
+
const source = names.map((name, index) => (name === '**' ? GLOBSTAR(dot) : `${NAME(name, dot)}${index === names.length - 1 ? '' : '/'}`)).join('')
|
|
54
|
+
return new RegExp(`^${source}$`, 'u')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// `ids` are the projects' directories, `.` the root; `packages` the globs,
|
|
58
|
+
// or undefined; `major` pnpm's major version.
|
|
59
|
+
export function checkWorkspace(ids, packages, major = 10) {
|
|
60
|
+
const where = 'pnpm-workspace.yaml: packages'
|
|
61
|
+
const globs = (packages ?? []).map((glob) => {
|
|
62
|
+
const exclude = glob.startsWith('!')
|
|
63
|
+
return { exclude, regexp: compile(exclude ? glob.slice(1) : glob, where, exclude && major >= 11) }
|
|
64
|
+
})
|
|
65
|
+
for (const id of ids) {
|
|
66
|
+
if (id === '.') continue
|
|
67
|
+
const path = `${id}/package.json`
|
|
68
|
+
const taken = globs.some(({ exclude, regexp }) => !exclude && regexp.test(path)) && !globs.some(({ exclude, regexp }) => exclude && regexp.test(path))
|
|
69
|
+
if (!taken || id.split('/').includes('bower_components')) {
|
|
70
|
+
throw new DeptreeError(`pnpm-workspace.yaml's packages ${packages === undefined ? 'are not set' : 'do not take this directory'}, so pnpm would not install it as a project`, `importers[${quote(id)}]`)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/tarball.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// A registry package's files, from its tarball: fetched through
|
|
2
|
+
// @preventive/upstream, which reads a cache of its own and npm's before
|
|
3
|
+
// the registry and holds what it gets to the integrity it is given, then
|
|
4
|
+
// held to that integrity again here, since it is the lockfile's; gunzipped
|
|
5
|
+
// and unpacked by @preventive/archive, with every rule of a name that
|
|
6
|
+
// holds; and read into files the way pnpm reads them out of a tarball
|
|
7
|
+
// (@pnpm/store.cafs): the first segment of each name dropped, whatever it
|
|
8
|
+
// is, only files kept, as ones not executable or executable by anyone.
|
|
9
|
+
//
|
|
10
|
+
// Here it is held to more than pnpm holds it to, to what npm packs: a
|
|
11
|
+
// gzipped tarball, every file under one directory, no link of either kind
|
|
12
|
+
// or device, which pnpm would pass over or refuse, and no name twice as
|
|
13
|
+
// two different files, of which pnpm would keep the later. It has to have
|
|
14
|
+
// a package.json, for exactly the name and version the lockfile says, and
|
|
15
|
+
// that package.json has to agree with what the lockfile recorded of the
|
|
16
|
+
// package's: its os, cpu and libc, whether it has bins, what it bundles.
|
|
17
|
+
// And every dependency it asks for, read through pnpm's read-package hook
|
|
18
|
+
// as pnpm reads it (hook.js), has to be among each of its snapshots', bundled
|
|
19
|
+
// or optional ones aside: a lockfile that leaves one out would leave the
|
|
20
|
+
// package to find it wherever it is hoisted, if anywhere. A registry whose
|
|
21
|
+
// metadata says other than its tarball does is caught at the same time.
|
|
22
|
+
//
|
|
23
|
+
// A package is fetched and read once however many snapshots it has, one
|
|
24
|
+
// for each set of peers it is resolved with.
|
|
25
|
+
|
|
26
|
+
import { decompress } from '@preventive/archive/compression.js'
|
|
27
|
+
import { unpack } from '@preventive/archive/tar.js'
|
|
28
|
+
import { normalize } from '@preventive/vfs/path.js'
|
|
29
|
+
import { getTarball } from '@preventive/upstream/npm.js'
|
|
30
|
+
import { DeptreeError, quote } from './error.js'
|
|
31
|
+
import { localOf } from './pnpm/overrides.js'
|
|
32
|
+
import { matchesIntegrity } from './hash.js'
|
|
33
|
+
|
|
34
|
+
// What a tarball may unpack to, as upstream bounds what it downloads.
|
|
35
|
+
const MAX_BYTES = 512 * 1024 * 1024
|
|
36
|
+
export const REGISTRY = 'https://registry.npmjs.org/'
|
|
37
|
+
|
|
38
|
+
// The registry's own URL for a version's tarball, as npm and pnpm spell it.
|
|
39
|
+
export const tarballUrl = (name, version) => `${REGISTRY}${name}/-/${name.split('/').at(-1)}-${version}.tgz`
|
|
40
|
+
|
|
41
|
+
// pnpm's name for an entry: past the first `/` of the name as stored,
|
|
42
|
+
// folded where it has a `./` in it, a `//` made one.
|
|
43
|
+
function nameOf(stored) {
|
|
44
|
+
const slash = stored.indexOf('/')
|
|
45
|
+
let name = slash === -1 ? stored : stored.slice(slash + 1)
|
|
46
|
+
if (name.includes('./')) name = normalize(`/${name}`).slice(1)
|
|
47
|
+
return name.replaceAll('//', '/')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const sameFile = (a, b) => a.mode === b.mode && a.data.length === b.data.length && a.data.every((byte, i) => byte === b.data[i])
|
|
51
|
+
|
|
52
|
+
// A Map of each file's path in the package to its bytes and mode.
|
|
53
|
+
export function filesOf(entries, where) {
|
|
54
|
+
const files = new Map()
|
|
55
|
+
const tops = new Set()
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
if (entry.type === 'directory') continue
|
|
58
|
+
if (entry.type !== 'file') throw new DeptreeError(`${quote(entry.name)} is a ${entry.type}, which is not supported`, where)
|
|
59
|
+
tops.add(entry.storedName.slice(0, Math.max(entry.storedName.indexOf('/'), 0)))
|
|
60
|
+
if (tops.size > 1 || tops.has('')) throw new DeptreeError('the tarball has files under more than one directory, or at its top', where)
|
|
61
|
+
const name = nameOf(entry.storedName)
|
|
62
|
+
if (name === '' || name.split('/').some((segment) => segment === '' || segment === '.' || segment === '..')) {
|
|
63
|
+
throw new DeptreeError(`${quote(entry.storedName)} names no file in the package`, where)
|
|
64
|
+
}
|
|
65
|
+
const file = { data: entry.data, mode: (entry.mode & 0o111) === 0 ? 0o644 : 0o755 }
|
|
66
|
+
const earlier = files.get(name)
|
|
67
|
+
if (earlier !== undefined && !sameFile(earlier, file)) throw new DeptreeError(`${quote(name)} is in the tarball twice`, where)
|
|
68
|
+
files.set(name, file)
|
|
69
|
+
}
|
|
70
|
+
return files
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const decoder = new TextDecoder('utf-8', { fatal: true })
|
|
74
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
|
75
|
+
const names = (value) => Object.keys(value ?? {})
|
|
76
|
+
|
|
77
|
+
// Whether a package.json has bins, as the lockfile's hasBin records it:
|
|
78
|
+
// by a `bin` that names any, or with none, by a directories.bin. Where
|
|
79
|
+
// `bin` is there and names none, and there is a directories.bin, pnpm
|
|
80
|
+
// resolves it as having none and pnpm 11 rewrites a snapshot as having
|
|
81
|
+
// some, so that is undefined: bins are linked as the lockfile has it.
|
|
82
|
+
function hasBin({ bin, directories }) {
|
|
83
|
+
if (bin === undefined || bin === null) return Boolean(directories?.bin)
|
|
84
|
+
if (bin && Object.keys(bin).length > 0) return true
|
|
85
|
+
return directories?.bin ? undefined : false
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function readManifest(files, pkg, where) {
|
|
89
|
+
const file = files.get('package.json')
|
|
90
|
+
if (file === undefined) throw new DeptreeError('the tarball has no package.json', where)
|
|
91
|
+
let manifest
|
|
92
|
+
try {
|
|
93
|
+
manifest = JSON.parse(decoder.decode(file.data))
|
|
94
|
+
} catch {
|
|
95
|
+
throw new DeptreeError('package.json is not JSON', where)
|
|
96
|
+
}
|
|
97
|
+
if (manifest === null || typeof manifest !== 'object' || Array.isArray(manifest)) throw new DeptreeError('package.json is not an object', where)
|
|
98
|
+
if (manifest.name !== pkg.name || manifest.version !== pkg.version) throw new DeptreeError(`package.json is for ${quote(`${manifest.name}@${manifest.version}`)}`, where)
|
|
99
|
+
return manifest
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// What the lockfile recorded of the package against the package.json.
|
|
103
|
+
function checkManifest(manifest, pkg, where) {
|
|
104
|
+
for (const field of ['os', 'cpu', 'libc']) {
|
|
105
|
+
if (!same(manifest[field], pkg[field])) throw new DeptreeError(`package.json's ${field} is not the lockfile's`, where)
|
|
106
|
+
}
|
|
107
|
+
const has = hasBin(manifest)
|
|
108
|
+
if (has !== undefined && has !== pkg.hasBin) throw new DeptreeError(`package.json ${pkg.hasBin ? 'has no bins, and the lockfile says it has' : 'has bins, and the lockfile says it has none'}`, where)
|
|
109
|
+
if (!same(bundledOf(manifest), pkg.bundledDependencies)) throw new DeptreeError('package.json bundles other than the lockfile says', where)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const bundledOf = (manifest) => manifest.bundleDependencies ?? manifest.bundledDependencies
|
|
113
|
+
|
|
114
|
+
// A snapshot's dependencies against the package.json of its package, as
|
|
115
|
+
// fetchPackage read it and `read` as hook.js's hook has it. One the
|
|
116
|
+
// package.json, as overridden, names a directory for is linked there,
|
|
117
|
+
// from the lockfile's directory, as pnpm writes it.
|
|
118
|
+
export function checkDependencies(manifest, read, pkg, where) {
|
|
119
|
+
const bundled = bundledOf(manifest)
|
|
120
|
+
const given = new Set([...names(pkg.dependencies), ...names(pkg.optionalDependencies)])
|
|
121
|
+
for (const name of [...names(read.dependencies), ...names(read.optionalDependencies)]) {
|
|
122
|
+
if (bundled === true || (Array.isArray(bundled) && bundled.includes(name)) || given.has(name)) continue
|
|
123
|
+
throw new DeptreeError(`package.json asks for ${quote(name)}, which the lockfile does not give it`, where)
|
|
124
|
+
}
|
|
125
|
+
for (const [name, spec] of [...Object.entries(read.dependencies ?? {}), ...Object.entries(read.optionalDependencies ?? {})]) {
|
|
126
|
+
const target = pkg.dependencies[name] ?? pkg.optionalDependencies[name]
|
|
127
|
+
if (target === undefined) continue
|
|
128
|
+
const local = localOf(spec, `${where}: package.json`)
|
|
129
|
+
if (local === undefined || target === `link:${local.dir}`) continue
|
|
130
|
+
throw new DeptreeError(`the lockfile gives it ${quote(name)} as ${quote(target)}, and its package.json, overridden, names ${quote(local.dir)}`, where)
|
|
131
|
+
}
|
|
132
|
+
// A peer resolved is filed as optional where it is optional; pnpm's
|
|
133
|
+
// compatibility database adds peers to a few packages, which the
|
|
134
|
+
// lockfile records among their peers and the package.json does not.
|
|
135
|
+
const optional = new Set([...names(read.optionalDependencies), ...names(manifest.peerDependencies), ...names(manifest.peerDependenciesMeta), ...names(pkg.peerDependencies), ...names(pkg.peerDependenciesMeta)])
|
|
136
|
+
for (const name of names(pkg.optionalDependencies)) {
|
|
137
|
+
if (!optional.has(name)) throw new DeptreeError(`the lockfile gives it ${quote(name)} as optional, which its package.json does not`, where)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// A package's files, and its package.json as parsed; the package has to be
|
|
142
|
+
// from the registry.
|
|
143
|
+
export async function fetchPackage(pkg, where) {
|
|
144
|
+
const { integrity } = pkg.resolution
|
|
145
|
+
const bytes = await getTarball(pkg.name, pkg.version, { tarball: tarballUrl(pkg.name, pkg.version), integrity })
|
|
146
|
+
if (!await matchesIntegrity(bytes, integrity)) throw new DeptreeError(`the tarball is not ${integrity}`, where)
|
|
147
|
+
if (bytes[0] !== 0x1f || bytes[1] !== 0x8b || bytes[2] !== 0x08) throw new DeptreeError('the tarball is not gzipped', where)
|
|
148
|
+
const files = filesOf(unpack(await decompress(bytes, 'gzip', { limit: MAX_BYTES })), where)
|
|
149
|
+
const manifest = readManifest(files, pkg, where)
|
|
150
|
+
checkManifest(manifest, pkg, where)
|
|
151
|
+
return { files, manifest }
|
|
152
|
+
}
|