@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nikita Skovoroda
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@preventive/deptree",
|
|
3
|
+
"version": "1.0.0-alpha.0",
|
|
4
|
+
"description": "A node_modules tree in memory, as a package manager would install it: pnpm 10 or 11 from its lockfile, tarballs fetched and checked, patches applied",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./pnpm.js": {
|
|
8
|
+
"types": "./pnpm.d.ts",
|
|
9
|
+
"default": "./pnpm.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"pnpm.d.ts",
|
|
14
|
+
"pnpm.js",
|
|
15
|
+
"src/error.js",
|
|
16
|
+
"src/hash.js",
|
|
17
|
+
"src/matcher.js",
|
|
18
|
+
"src/mount.js",
|
|
19
|
+
"src/patch.js",
|
|
20
|
+
"src/pnpm/bins.js",
|
|
21
|
+
"src/pnpm/checks.js",
|
|
22
|
+
"src/pnpm/filename.js",
|
|
23
|
+
"src/pnpm/frozen.js",
|
|
24
|
+
"src/pnpm/graph.js",
|
|
25
|
+
"src/pnpm/hook.js",
|
|
26
|
+
"src/pnpm/hoist.js",
|
|
27
|
+
"src/pnpm/install.js",
|
|
28
|
+
"src/pnpm/local.js",
|
|
29
|
+
"src/pnpm/npmrc.js",
|
|
30
|
+
"src/pnpm/overrides.js",
|
|
31
|
+
"src/pnpm/patches.js",
|
|
32
|
+
"src/pnpm/projects.js",
|
|
33
|
+
"src/pnpm/readers.js",
|
|
34
|
+
"src/pnpm/settings.js",
|
|
35
|
+
"src/pnpm/tree.js",
|
|
36
|
+
"src/pnpm/uptodate.js",
|
|
37
|
+
"src/pnpm/workspace.js",
|
|
38
|
+
"src/tarball.js"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=24.14.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "node --test ./tests/*.test.js"
|
|
45
|
+
},
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"author": {
|
|
48
|
+
"name": "Nikita Skovoroda",
|
|
49
|
+
"email": "chalkerx@gmail.com",
|
|
50
|
+
"url": "https://github.com/ChALkeR"
|
|
51
|
+
},
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/PreventiveMeasures/libraries.git",
|
|
55
|
+
"directory": "deptree"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/PreventiveMeasures/libraries/issues"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://github.com/PreventiveMeasures/libraries/tree/main/deptree",
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@preventive/archive": "1.0.0-beta.3",
|
|
66
|
+
"@preventive/diff": "^1.0.0",
|
|
67
|
+
"@preventive/lockfile": "1.0.0-alpha.1",
|
|
68
|
+
"@preventive/upstream": "1.0.0-alpha.1",
|
|
69
|
+
"@preventive/vfs": "1.0.0-beta.1"
|
|
70
|
+
}
|
|
71
|
+
}
|
package/pnpm.d.ts
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// Hand-written against pnpm.js; a change to either belongs with the other.
|
|
2
|
+
|
|
3
|
+
import type { Vfs } from '@preventive/vfs'
|
|
4
|
+
|
|
5
|
+
export { LockfileError, YamlError } from '@preventive/lockfile/pnpm.js'
|
|
6
|
+
|
|
7
|
+
// Where @preventive/upstream caches what it fetches, tarballs among them.
|
|
8
|
+
// Unset, which it is by default, nothing is cached and nothing is written:
|
|
9
|
+
// tarballs are fetched every time, each once for however many snapshots it
|
|
10
|
+
// has. It is the one place anything here touches a filesystem.
|
|
11
|
+
export { setCacheDir } from '@preventive/upstream/npm.js'
|
|
12
|
+
|
|
13
|
+
// The machine pnpm would install on, which a tree depends on: `pnpm` is
|
|
14
|
+
// the version that installs, and has to be a 10.x or an 11.x, the two
|
|
15
|
+
// built for, each as it differs from the other; `node` the Node it runs
|
|
16
|
+
// on, unless the settings name a nodeVersion or, for pnpm 11, the root
|
|
17
|
+
// package.json's engines.runtime pins one;
|
|
18
|
+
// `os`, `cpu` and `libc` as Node and pnpm name them — `linux`, `x64`,
|
|
19
|
+
// `glibc` — with `unknown` for a libc outside Linux, as pnpm has it.
|
|
20
|
+
// Windows is refused: pnpm links there with junctions to absolute paths.
|
|
21
|
+
// An optional package the machine cannot run is left out, as pnpm leaves
|
|
22
|
+
// it out; supportedArchitectures in the settings widens what is taken.
|
|
23
|
+
export interface PnpmHost {
|
|
24
|
+
pnpm: string
|
|
25
|
+
node: string
|
|
26
|
+
os: string
|
|
27
|
+
cpu: string
|
|
28
|
+
libc: 'glibc' | 'musl' | 'unknown'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The files an install reads, as text: pnpm-lock.yaml; the package.json
|
|
32
|
+
// of every project it installs, by the project's directory relative to the
|
|
33
|
+
// lockfile's (`.` for the root, then as its importers are keyed); pnpm-
|
|
34
|
+
// workspace.yaml and the .npmrc, where there are any; and every patch file
|
|
35
|
+
// the settings' patchedDependencies name, by the path from the lockfile's
|
|
36
|
+
// directory they name it by. Settings from anywhere else — another
|
|
37
|
+
// .npmrc, the environment, the command line — are not read, and are taken
|
|
38
|
+
// to be at their defaults.
|
|
39
|
+
//
|
|
40
|
+
// A workspace is one lockfile for several projects, each a package.json:
|
|
41
|
+
// the root, and every directory pnpm-workspace.yaml's `packages` globs
|
|
42
|
+
// take, as pnpm globs for them — `*` and `**`, a leading `!` to leave out,
|
|
43
|
+
// and no other glob syntax. Without `packages` the root is the only
|
|
44
|
+
// project. Every importer has to have its package.json given, and be a
|
|
45
|
+
// project those globs take; and every project pnpm would find has to be
|
|
46
|
+
// given, which cannot be checked here, as the directories are not.
|
|
47
|
+
//
|
|
48
|
+
// Each project is held to its importer as --frozen-lockfile holds it: its
|
|
49
|
+
// dependencies, devDependencies, optionalDependencies and, with
|
|
50
|
+
// autoInstallPeers, the peers it lists nowhere else, have to be what the
|
|
51
|
+
// importer records, and in the ranges they ask for. The projects' names
|
|
52
|
+
// are what hoistWorkspacePackages hoists them by.
|
|
53
|
+
//
|
|
54
|
+
// Settings are read as `pnpm install` 10 reads them, each source over the
|
|
55
|
+
// one before: the .npmrc, then pnpm-workspace.yaml, then what the
|
|
56
|
+
// package.json's `pnpm` field sets, which pnpm's install spreads over the
|
|
57
|
+
// rest. Overrides are the package.json's Yarn-style `resolutions` and
|
|
58
|
+
// `pnpm.overrides`, the second winning a selector both name; only where
|
|
59
|
+
// those name none are pnpm-workspace.yaml's `overrides` read. `$name` in
|
|
60
|
+
// one is the root package.json's own specifier for `name`, and
|
|
61
|
+
// `catalog:` what the workspace's catalog gives the package. One to a
|
|
62
|
+
// directory — a path alone, such as `./vendor/foo`, or by `link:` — is
|
|
63
|
+
// read only with `vfs` given, and only where the directory is under the
|
|
64
|
+
// lockfile's and holds a package.json there; pnpm links it. One to a copy
|
|
65
|
+
// of a directory, by `file:`, is refused: pnpm picks the files it copies
|
|
66
|
+
// with npm-packlist.
|
|
67
|
+
//
|
|
68
|
+
// Of the .npmrc, only what pnpm reads for an install is read: its settings
|
|
69
|
+
// by their kebab-case names, those that can change the tree held to what
|
|
70
|
+
// is built here, and all else — npm's own settings, publishing's,
|
|
71
|
+
// credentials, other names — passed over. A `${VAR}` pnpm would fill in is
|
|
72
|
+
// taken in a line passed over, such as `//registry.npmjs.org/:_authToken`,
|
|
73
|
+
// where the rest of the file would not change the tree: pnpm drops the
|
|
74
|
+
// whole file where the variable is unset.
|
|
75
|
+
//
|
|
76
|
+
// pnpm 11 reads its settings from pnpm-workspace.yaml alone, as this does
|
|
77
|
+
// for it: of the .npmrc only its registries, and of the package.json no
|
|
78
|
+
// setting, `resolutions` and the `pnpm` field none. It passes over a
|
|
79
|
+
// pnpm-workspace.yaml key not in camelCase; its own settings are read as
|
|
80
|
+
// the rest are, pmOnFail and runtimeOnFail among them, which decide what
|
|
81
|
+
// its packageManager, devEngines.packageManager and engines.runtime
|
|
82
|
+
// checks do. The tree follows the lockfile, as pnpm 11's does with
|
|
83
|
+
// trustLockfile: minimumReleaseAge, and the rest of what pnpm 11 checks the
|
|
84
|
+
// lockfile against the registry by before it installs — each package's
|
|
85
|
+
// publish time, its tarball URL, its trust — are passed over.
|
|
86
|
+
//
|
|
87
|
+
// `vfs` is a Vfs to mount the tree into, at its root, which is taken to
|
|
88
|
+
// be the lockfile's directory, beside whatever it holds, such as the
|
|
89
|
+
// projects themselves; without one, a new Vfs holds the tree alone. A Vfs
|
|
90
|
+
// that holds a node_modules anywhere, or on macOS a name that is one
|
|
91
|
+
// there, is refused before anything is fetched: kept beside the tree, Node
|
|
92
|
+
// would read it as the tree's, and removed, it would be the caller's lost.
|
|
93
|
+
// Nothing there is written over: each directory the tree has is one there
|
|
94
|
+
// or is made, and every file and link is written where nothing is. The
|
|
95
|
+
// tree is built, and held to every check below, before any of it is
|
|
96
|
+
// written, so a refusal leaves the Vfs as it was. With it given, a
|
|
97
|
+
// directory the tree links to that is no project, and is under the
|
|
98
|
+
// lockfile's, has to be there with a package.json, whose bins are read as
|
|
99
|
+
// pnpm reads them; nothing outside node_modules is written, though pnpm
|
|
100
|
+
// 10 makes the files a linked directory's bins run executable too.
|
|
101
|
+
// Without it, or outside the lockfile's directory, such a directory's
|
|
102
|
+
// bins are not known, and neither are those of a project that has them
|
|
103
|
+
// by the files of its directories.bin: where one could win a command a
|
|
104
|
+
// package's file is linked by, and so decide whether pnpm fixes that
|
|
105
|
+
// file, and fixing it would change it, the tree is refused.
|
|
106
|
+
export interface PnpmTreeOptions {
|
|
107
|
+
lockfile: string
|
|
108
|
+
manifests: Record<string, string> | Map<string, string>
|
|
109
|
+
workspace?: string
|
|
110
|
+
npmrc?: string
|
|
111
|
+
patches?: Record<string, string> | Map<string, string>
|
|
112
|
+
host: PnpmHost
|
|
113
|
+
vfs?: Vfs
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// What buildPnpmTree counts, all of it plain numbers: `projects` the
|
|
117
|
+
// importers; `snapshots` the lockfile's; `installed` those in the tree;
|
|
118
|
+
// `skipped` the optional ones left out, as the host cannot run them or
|
|
119
|
+
// only such reach them; `incompatible` those installed although the host
|
|
120
|
+
// does not take their platform or engines, which pnpm warns of; `tarballs`
|
|
121
|
+
// the packages fetched, each once; `patched` the snapshots a patch is
|
|
122
|
+
// applied to; `files` and `bytes` what is written; `links` the symlinks.
|
|
123
|
+
export interface PnpmTreeStats {
|
|
124
|
+
projects: number
|
|
125
|
+
snapshots: number
|
|
126
|
+
installed: number
|
|
127
|
+
skipped: number
|
|
128
|
+
incompatible: number
|
|
129
|
+
tarballs: number
|
|
130
|
+
patched: number
|
|
131
|
+
files: number
|
|
132
|
+
bytes: number
|
|
133
|
+
links: number
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// `vfs` is the one given, the tree mounted into it, or a new one.
|
|
137
|
+
export interface PnpmTree {
|
|
138
|
+
vfs: Vfs
|
|
139
|
+
stats: PnpmTreeStats
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// The node_modules tree `pnpm install --frozen-lockfile --ignore-scripts`
|
|
143
|
+
// makes with the isolated linker of host.pnpm, 10 or 11, and no other
|
|
144
|
+
// install: whatever the
|
|
145
|
+
// settings say of frozen lockfiles, the install is frozen, which is also
|
|
146
|
+
// the only one that hoists by the lockfile's graph alone. It is rooted at
|
|
147
|
+
// the lockfile's directory: each package's
|
|
148
|
+
// files at node_modules/.pnpm/<dir>/node_modules/<name>, its dependencies
|
|
149
|
+
// linked beside it, the hoisted aliases, and each project's own
|
|
150
|
+
// node_modules of links. Links are relative; a `link:` dependency leads
|
|
151
|
+
// where the lockfile says, which the tree does not hold. Scripts are
|
|
152
|
+
// always ignored, as `--ignore-scripts` has pnpm ignore them, whatever the
|
|
153
|
+
// settings would allow to build. No .bin is written, nor pnpm's own state
|
|
154
|
+
// files; but each file a bin pnpm links runs is left as linking leaves it:
|
|
155
|
+
// executable, and with a CRLF ending its `#!` line made LF. Every file is
|
|
156
|
+
// its own, as pnpm's files are where it clones or copies them from its
|
|
157
|
+
// store; where it hardlinks them instead, making one executable makes
|
|
158
|
+
// every file of the same content executable, and the store's, for later
|
|
159
|
+
// installs too, which is not followed here. Every file is written once,
|
|
160
|
+
// and a write that would replace anything with something else is refused;
|
|
161
|
+
// into a Vfs given, one that would replace anything at all.
|
|
162
|
+
// pnpm 11 links bins otherwise in places — npm owns `npx` and pnpm its
|
|
163
|
+
// aliases, a project's .bin takes the bins of the peers its dependencies
|
|
164
|
+
// require — and each is built as the one given links them.
|
|
165
|
+
//
|
|
166
|
+
// The lockfile is held to what a frozen install holds it to, and refused
|
|
167
|
+
// where pnpm would refuse it: the settings that shaped its resolution —
|
|
168
|
+
// catalogs, overrides, package extensions, optional dependencies left out,
|
|
169
|
+
// patches, autoInstallPeers, dedupePeers, peersSuffixMaxLength — and each
|
|
170
|
+
// project's package.json, read through pnpm's read-package hook as pnpm
|
|
171
|
+
// reads it. The root package.json's packageManager, where it has one, has
|
|
172
|
+
// to be `pnpm@` host.pnpm exactly, as pnpm would switch to the one it
|
|
173
|
+
// names; a project's engines.pnpm has to take host.pnpm, and with
|
|
174
|
+
// engineStrict its engines.node host.node. With engineStrict, pnpm 11
|
|
175
|
+
// holds a patched package's engines.node to host.node as its package.json
|
|
176
|
+
// has it once patched, not as the lockfile records it, and fails on, or
|
|
177
|
+
// takes out, one that does not take it, which is refused. pnpm 11 holds a
|
|
178
|
+
// lockfile to more before it installs, and so does this for it: a catalog
|
|
179
|
+
// dependency to the catalog's version, a workspace package linked exactly
|
|
180
|
+
// where its version is in range, and a patch hash in a snapshot's peers to
|
|
181
|
+
// the patch; it lets an optional dependency the importer has no specifier
|
|
182
|
+
// for go unresolved, and takes two spellings of one git commit alike. A
|
|
183
|
+
// devEngines.packageManager is refused unless its onFail, or pmOnFail, is
|
|
184
|
+
// to warn or ignore, as pnpm 11 installs with the pnpm the lockfile pins
|
|
185
|
+
// for it; and a root engines.runtime whose onFail is `error` has to take
|
|
186
|
+
// host.node.
|
|
187
|
+
//
|
|
188
|
+
// And to more than pnpm holds it to, where a lockfile pnpm writes, or a
|
|
189
|
+
// package the registry serves, always holds: each dependency linked where
|
|
190
|
+
// the package.json names a directory for it, a path alone among them; each
|
|
191
|
+
// snapshot marked optional exactly where only optional dependencies reach
|
|
192
|
+
// it; each patch
|
|
193
|
+
// on the package the settings pick it for, and each used; each tarball
|
|
194
|
+
// gzipped, every file under one directory, no link or device in it, and
|
|
195
|
+
// a package.json for exactly its name and version, whose os, cpu, libc,
|
|
196
|
+
// bins and bundled dependencies are what the lockfile recorded and whose
|
|
197
|
+
// every dependency the snapshot has. On macOS, two names in one directory
|
|
198
|
+
// that differ only in case or normalization are refused, as they would be
|
|
199
|
+
// one name there.
|
|
200
|
+
//
|
|
201
|
+
// Packages come from https://registry.npmjs.org/ alone, fetched through
|
|
202
|
+
// @preventive/upstream, and each tarball is held to the lockfile's
|
|
203
|
+
// integrity; each patch to the lockfile's hash of it, and applied only
|
|
204
|
+
// where every hunk matches exactly where it says.
|
|
205
|
+
//
|
|
206
|
+
// Nothing is left to a guess: a lockfile the lockfile reader refuses, a
|
|
207
|
+
// setting this does not know or does not build for, overrides pnpm cannot
|
|
208
|
+
// read, a lockfile not resolved with these settings, a package from
|
|
209
|
+
// anywhere but the registry, a pnpmfile, package extensions, an injected
|
|
210
|
+
// dependency, a lockfile not up to date with a package.json, another
|
|
211
|
+
// package manager, a runtime to download, two projects of one name, a
|
|
212
|
+
// patch that does not hash or apply, a check above that fails, text
|
|
213
|
+
// that is not well-formed where it is hashed, a patch that would change a
|
|
214
|
+
// bin's file between two times pnpm links it, a file whose mode would turn
|
|
215
|
+
// on the order a directory is read in — each is refused with a
|
|
216
|
+
// DeptreeError, a LockfileError or a YamlError that says where. A
|
|
217
|
+
// TypeError is thrown for options of the wrong type.
|
|
218
|
+
export function buildPnpmTree(options: PnpmTreeOptions): Promise<PnpmTree>
|
|
219
|
+
|
|
220
|
+
// `where` is what a refusal is about — `pnpm-workspace.yaml: nodeLinker`,
|
|
221
|
+
// `.npmrc:3: node-linker`, a package's key — or undefined for the call as
|
|
222
|
+
// a whole; the message leads with it. `cause` is what a package beneath
|
|
223
|
+
// refused with, where one did.
|
|
224
|
+
export class DeptreeError extends Error {
|
|
225
|
+
constructor(detail: string, where?: string, options?: { cause?: unknown })
|
|
226
|
+
where: string | undefined
|
|
227
|
+
}
|
package/pnpm.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// A node_modules tree built in memory from a pnpm lockfile, as pnpm 10 or
|
|
2
|
+
// 11 installs it with the isolated linker: pnpm.d.ts says what it takes and
|
|
3
|
+
// what comes back. Nothing here touches a filesystem; tarballs come
|
|
4
|
+
// through @preventive/upstream, and whatever cache it keeps is its own.
|
|
5
|
+
export { buildPnpmTree } from './src/pnpm/tree.js'
|
|
6
|
+
export { DeptreeError } from './src/error.js'
|
|
7
|
+
// Where @preventive/upstream caches what it fetches, tarballs among it;
|
|
8
|
+
// unset, nothing is cached and nothing is written.
|
|
9
|
+
export { setCacheDir } from '@preventive/upstream/npm.js'
|
|
10
|
+
// What the inputs beneath are refused with: the lockfile, and the YAML of
|
|
11
|
+
// it and of pnpm-workspace.yaml.
|
|
12
|
+
export { LockfileError, YamlError } from '@preventive/lockfile/pnpm.js'
|
package/src/error.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// `where` is what a refusal is about — a file and the setting or key in
|
|
2
|
+
// it, `pnpm-workspace.yaml: nodeLinker`, or a package and the file in it —
|
|
3
|
+
// or undefined for the call as a whole; the message leads with it. What a
|
|
4
|
+
// package below refused with is kept as the `cause`.
|
|
5
|
+
export class DeptreeError extends Error {
|
|
6
|
+
constructor(detail, where, options) {
|
|
7
|
+
super(where === undefined ? detail : `${where}: ${detail}`, options)
|
|
8
|
+
this.name = 'DeptreeError'
|
|
9
|
+
this.where = where
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// A piece of an input for a message: quoted, cut short where it runs long,
|
|
14
|
+
// and with every control, line separator and bidirectional control
|
|
15
|
+
// escaped, so a name cannot act on a terminal or reorder what is shown.
|
|
16
|
+
const UNSHOWN = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}]/gu
|
|
17
|
+
export function quote(text) {
|
|
18
|
+
const cut = text.length > 200 ? `${text.slice(0, text.codePointAt(199) > 0xFFFF ? 199 : 200)}…` : text
|
|
19
|
+
return JSON.stringify(cut).replaceAll(UNSHOWN, (char) => `\\u${char.codePointAt(0).toString(16).padStart(4, '0')}`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// The first key two mappings of strings differ at, for a message: `sides`
|
|
23
|
+
// names where each is from, and `same` tells two values alike.
|
|
24
|
+
export function difference(a, b, [aSide, bSide], same = (x, y) => x === y) {
|
|
25
|
+
for (const key of new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
26
|
+
const inA = Object.hasOwn(a, key)
|
|
27
|
+
const inB = Object.hasOwn(b, key)
|
|
28
|
+
if (inA && inB && same(a[key], b[key])) continue
|
|
29
|
+
return `${quote(key)} is ${inA ? quote(a[key]) : 'nothing'} in ${aSide} and ${inB ? quote(b[key]) : 'nothing'} in ${bSide}`
|
|
30
|
+
}
|
|
31
|
+
return undefined
|
|
32
|
+
}
|
package/src/hash.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Digests on the platform's Web Crypto, which every runtime this runs on
|
|
2
|
+
// has: the integrity a lockfile pins a tarball to, and the hex digests
|
|
3
|
+
// pnpm names a patch and a long directory by.
|
|
4
|
+
|
|
5
|
+
import { DeptreeError } from './error.js'
|
|
6
|
+
|
|
7
|
+
const encoder = new TextEncoder()
|
|
8
|
+
|
|
9
|
+
const digest = async (algorithm, bytes) => new Uint8Array(await crypto.subtle.digest(algorithm, bytes))
|
|
10
|
+
const hex = (bytes) => Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
11
|
+
const base64 = (bytes) => btoa(String.fromCodePoint(...bytes))
|
|
12
|
+
|
|
13
|
+
// Text is hashed as its UTF-8, and only text that has one: TextEncoder
|
|
14
|
+
// writes a lone surrogate as U+FFFD, so two different strings would hash
|
|
15
|
+
// the same. `where` is what the text is, for the refusal.
|
|
16
|
+
export async function sha256Hex(text, where) {
|
|
17
|
+
if (typeof text !== 'string' || !text.isWellFormed()) throw new DeptreeError('expected well-formed text to hash', where)
|
|
18
|
+
return hex(await digest('SHA-256', encoder.encode(text)))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Whether `bytes` are what a subresource integrity names: a sha512, the
|
|
22
|
+
// only kind taken, in padded base64.
|
|
23
|
+
export const matchesIntegrity = async (bytes, integrity) => integrity === `sha512-${base64(await digest('SHA-512', bytes))}`
|
package/src/matcher.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// pnpm's name patterns (@pnpm/matcher), as `hoist-pattern` and
|
|
2
|
+
// `public-hoist-pattern` read them: `*` for any run of characters and
|
|
3
|
+
// nothing else special, `!` in front to exclude. One pattern is that
|
|
4
|
+
// pattern or its negation; several are read in order, where an exclusion
|
|
5
|
+
// cancels what matched before it and nothing after it matches again but a
|
|
6
|
+
// later inclusion, and a list of exclusions alone matches what none of them
|
|
7
|
+
// does.
|
|
8
|
+
|
|
9
|
+
// Text as a regular expression that matches it alone.
|
|
10
|
+
export const escape = (text) => text.replace(/[$()+.?[\\\]^{|}]/gu, '\\$&')
|
|
11
|
+
|
|
12
|
+
function matcherOf(pattern) {
|
|
13
|
+
if (pattern === '*') return () => true
|
|
14
|
+
if (!pattern.includes('*')) return (input) => input === pattern
|
|
15
|
+
const regexp = new RegExp(`^${pattern.split('*').map(escape).join('.*')}$`, 'u')
|
|
16
|
+
return (input) => regexp.test(input)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function createMatcher(patterns) {
|
|
20
|
+
if (patterns.length === 0) return () => false
|
|
21
|
+
const matchers = patterns.map((pattern) => {
|
|
22
|
+
const ignore = pattern.startsWith('!')
|
|
23
|
+
return { ignore, match: matcherOf(ignore ? pattern.slice(1) : pattern) }
|
|
24
|
+
})
|
|
25
|
+
if (matchers.every(({ ignore }) => ignore)) return (input) => !matchers.some(({ match }) => match(input))
|
|
26
|
+
return (input) => {
|
|
27
|
+
let matched = false
|
|
28
|
+
for (const { ignore, match } of matchers) {
|
|
29
|
+
if (ignore) { if (match(input)) matched = false }
|
|
30
|
+
else if (!matched && match(input)) matched = true
|
|
31
|
+
}
|
|
32
|
+
return matched
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/mount.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// A tree built on its own, mounted into a Vfs the caller already has, at
|
|
2
|
+
// its root: the lockfile's directory. Nothing there is written over or
|
|
3
|
+
// removed. A node_modules there already, anywhere, is refused: kept beside
|
|
4
|
+
// the tree, Node would read it as the tree's, and removed, it would be the
|
|
5
|
+
// caller's lost; neither is safe. Everything is checked before anything
|
|
6
|
+
// is written, so a refusal leaves the Vfs as it was.
|
|
7
|
+
|
|
8
|
+
import { basename, dirname } from '@preventive/vfs/path.js'
|
|
9
|
+
import { DeptreeError, quote } from './error.js'
|
|
10
|
+
|
|
11
|
+
// As macOS takes a name, whatever its case and normalization.
|
|
12
|
+
export const fold = (name) => name.normalize('NFD').toLowerCase()
|
|
13
|
+
|
|
14
|
+
const where = (path) => `vfs[${quote(path)}]`
|
|
15
|
+
|
|
16
|
+
// Refuses a Vfs that holds a node_modules, or, where names are `folded`, a
|
|
17
|
+
// name that is one there.
|
|
18
|
+
export function checkNoModules(vfs, folded) {
|
|
19
|
+
for (const { path } of vfs.walk('/')) {
|
|
20
|
+
const name = basename(path)
|
|
21
|
+
if (name === 'node_modules' || (folded && fold(name) === 'node_modules')) {
|
|
22
|
+
throw new DeptreeError('a node_modules is there already, which is neither kept beside the tree nor removed', where(path))
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function typeAt(vfs, path) {
|
|
28
|
+
try {
|
|
29
|
+
return vfs.lstat(path).type
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error.code === 'ENOENT') return undefined
|
|
32
|
+
throw error
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Moves `tree` into `target`: each directory of the tree is one there
|
|
37
|
+
// already or is made, and each file and link is written where nothing is,
|
|
38
|
+
// and where names are `folded`, none beside a name it would be one with.
|
|
39
|
+
// Each file is taken out of `tree` as it is written.
|
|
40
|
+
export function mount(tree, target, folded) {
|
|
41
|
+
checkNoModules(target, folded)
|
|
42
|
+
const entries = [...tree.walk('/')].filter(({ path }) => path !== '/')
|
|
43
|
+
for (const { path, type } of entries) {
|
|
44
|
+
const existing = typeAt(target, path)
|
|
45
|
+
if (existing !== undefined && (type !== 'directory' || existing !== 'directory')) {
|
|
46
|
+
throw new DeptreeError(`a ${existing} is there already, where the tree has a ${type}`, where(path))
|
|
47
|
+
}
|
|
48
|
+
if (!folded || existing !== undefined || typeAt(target, dirname(path)) !== 'directory') continue
|
|
49
|
+
const name = basename(path)
|
|
50
|
+
const clash = target.readdir(dirname(path)).find((other) => fold(other) === fold(name))
|
|
51
|
+
if (clash !== undefined) throw new DeptreeError(`${quote(clash)} is there already, which is one name with ${quote(name)} on macOS`, where(path))
|
|
52
|
+
}
|
|
53
|
+
for (const { path, type } of entries) {
|
|
54
|
+
if (type === 'directory') {
|
|
55
|
+
if (typeAt(target, path) === undefined) target.mkdir(path)
|
|
56
|
+
} else if (type === 'symlink') {
|
|
57
|
+
target.symlink(tree.readlink(path), path)
|
|
58
|
+
} else {
|
|
59
|
+
target.writeFile(path, tree.readFile(path), { mode: tree.lstat(path).mode })
|
|
60
|
+
tree.unlink(path)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/patch.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// A patch applied to a package's files, as `pnpm patch-commit`
|
|
2
|
+
// writes one: a git diff, a `diff --git a/<path> b/<path>` header for each
|
|
3
|
+
// file, and unified hunks, a path relative to the package's directory. A
|
|
4
|
+
// file is changed, created from /dev/null or deleted; a rename, a copy, a
|
|
5
|
+
// change of mode and a binary patch are refused, as is anything else
|
|
6
|
+
// between one file's hunks and the next header.
|
|
7
|
+
//
|
|
8
|
+
// A hunk applies where it says and nowhere else, every line of context and
|
|
9
|
+
// every line it removes the same, byte for byte, as the file has there.
|
|
10
|
+
// pnpm (@pnpm/patch-package) would also look up to twenty lines away and
|
|
11
|
+
// compare lines with their trailing whitespace dropped; a patch that needs
|
|
12
|
+
// either is refused here, so a patch applied here leaves the file as pnpm
|
|
13
|
+
// would. So is a hunk that inserts after a line and keeps no context,
|
|
14
|
+
// which pnpm puts a line early.
|
|
15
|
+
|
|
16
|
+
import { applyChangeSet, parseDiff } from '@preventive/diff'
|
|
17
|
+
import { DeptreeError, quote } from './error.js'
|
|
18
|
+
|
|
19
|
+
const HEADER = 'diff --git a/'
|
|
20
|
+
const MODE = /^(?:new|deleted) file mode (100644|100755)$/u
|
|
21
|
+
const INDEX = /^index [\da-f]+\.\.[\da-f]+(?: 100644| 100755)?$/u
|
|
22
|
+
const HUNK_LINE = /^(?:@@ |[ +\\-]|$)/u
|
|
23
|
+
|
|
24
|
+
// The path a header names on both sides, or undefined where the two differ.
|
|
25
|
+
function pathOf(header) {
|
|
26
|
+
const rest = header.slice(HEADER.length)
|
|
27
|
+
const path = rest.slice(0, (rest.length - 3) / 2)
|
|
28
|
+
return rest === `${path} b/${path}` ? path : undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function checkPath(path, where) {
|
|
32
|
+
if (path === undefined || path.startsWith('"') || path.split('/').some((segment) => segment === '' || segment === '.' || segment === '..')) {
|
|
33
|
+
throw new DeptreeError('expected a header naming one relative path on both sides', where)
|
|
34
|
+
}
|
|
35
|
+
return path
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// One file's header: what happens to the file, and the `---`/`+++` pair
|
|
39
|
+
// naming it, which a patch without hunks may leave out.
|
|
40
|
+
function readHeader(lines, path, where) {
|
|
41
|
+
let change = 'modify'
|
|
42
|
+
let mode = 0o644
|
|
43
|
+
let i = 1
|
|
44
|
+
for (; i < lines.length && !lines[i].startsWith('--- ') && !lines[i].startsWith('@@ '); i++) {
|
|
45
|
+
const m = MODE.exec(lines[i])
|
|
46
|
+
if (m !== null && change === 'modify') {
|
|
47
|
+
change = lines[i].startsWith('new') ? 'create' : 'delete'
|
|
48
|
+
mode = m[1] === '100755' ? 0o755 : 0o644
|
|
49
|
+
} else if (!INDEX.test(lines[i])) throw new DeptreeError(`${quote(lines[i])} is not supported`, where)
|
|
50
|
+
}
|
|
51
|
+
if (lines[i]?.startsWith('--- ')) {
|
|
52
|
+
const old = change === 'create' ? '/dev/null' : `a/${path}`
|
|
53
|
+
const fresh = change === 'delete' ? '/dev/null' : `b/${path}`
|
|
54
|
+
if (lines[i] !== `--- ${old}` || lines[i + 1] !== `+++ ${fresh}`) throw new DeptreeError(`expected "--- ${old}" and "+++ ${fresh}"`, where)
|
|
55
|
+
i += 2
|
|
56
|
+
}
|
|
57
|
+
return { change, mode, body: i }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The hunks of one file, each line of them read: parseDiff passes over
|
|
61
|
+
// what it does not read, so every line has to be one a hunk holds, and
|
|
62
|
+
// every one of those in a hunk.
|
|
63
|
+
function readHunks(lines, where) {
|
|
64
|
+
if (lines.length === 0) return { hunks: [], blocks: [] }
|
|
65
|
+
if (!lines.every((line) => HUNK_LINE.test(line))) throw new DeptreeError('expected only hunks after the header', where)
|
|
66
|
+
const files = parseDiff(`${lines.join('\n')}\n`)
|
|
67
|
+
const hunks = files.flatMap((file) => file.hunks)
|
|
68
|
+
const heads = lines.filter((line) => line.startsWith('@@ ')).length
|
|
69
|
+
const held = lines.filter((line) => !line.startsWith('@@ ') && !line.startsWith('\\')).length
|
|
70
|
+
if (files.length !== 1 || files[0].format !== 'unified' || hunks.length !== heads || hunks.reduce((sum, hunk) => sum + hunk.lines.length, 0) !== held) {
|
|
71
|
+
throw new DeptreeError('expected unified hunks and nothing between them', where)
|
|
72
|
+
}
|
|
73
|
+
return { hunks, blocks: files[0].blocks }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function parsePatch(text, where) {
|
|
77
|
+
if (text.includes('\r')) throw new DeptreeError('a carriage return is not supported', where)
|
|
78
|
+
if (!text.startsWith(HEADER)) throw new DeptreeError(`expected ${quote(HEADER)} first`, where)
|
|
79
|
+
const sections = text.split(/\n(?=diff --git )/u)
|
|
80
|
+
return sections.map((section) => {
|
|
81
|
+
const lines = section.split('\n')
|
|
82
|
+
if (lines.at(-1) === '') lines.pop()
|
|
83
|
+
const path = checkPath(pathOf(lines[0]), `${where}: ${quote(lines[0])}`)
|
|
84
|
+
const here = `${where}: ${quote(path)}`
|
|
85
|
+
const { change, mode, body } = readHeader(lines, path, here)
|
|
86
|
+
return { path, change, mode, ...readHunks(lines.slice(body), here), where: here }
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Holds each hunk to the lines it names in `text`, and hands back what
|
|
91
|
+
// applying them makes of it.
|
|
92
|
+
function applyTo(text, { hunks, blocks, where }) {
|
|
93
|
+
const records = text.split(/(?<=\n)/u)
|
|
94
|
+
let end = 0
|
|
95
|
+
for (const hunk of hunks) {
|
|
96
|
+
const kept = hunk.lines.filter((line) => line.tag !== '+')
|
|
97
|
+
if (hunk.oldStart < end || (kept.length === 0 && hunk.oldStart !== 0)) throw new DeptreeError('hunks out of order, or one with no context', where)
|
|
98
|
+
kept.forEach((line, index) => {
|
|
99
|
+
if (records[hunk.oldStart + index] !== line.text) throw new DeptreeError(`the hunk at line ${hunk.oldStart + 1} does not apply`, where)
|
|
100
|
+
})
|
|
101
|
+
end = hunk.oldStart + kept.length
|
|
102
|
+
}
|
|
103
|
+
return applyChangeSet(text, blocks)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
|
|
107
|
+
const encoder = new TextEncoder()
|
|
108
|
+
|
|
109
|
+
function textOf(file, where) {
|
|
110
|
+
try {
|
|
111
|
+
return decoder.decode(file.data)
|
|
112
|
+
} catch {
|
|
113
|
+
throw new DeptreeError('changes a file that is not UTF-8', where)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function bytesOf(text, where) {
|
|
118
|
+
if (!text.isWellFormed()) throw new DeptreeError('makes a file that is not well-formed text', where)
|
|
119
|
+
return encoder.encode(text)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const parentsOf = (path) => path.split('/').slice(0, -1).map((_, index, names) => names.slice(0, index + 1).join('/'))
|
|
123
|
+
|
|
124
|
+
// Applies a parsed patch to a package's files: a Map of each path in the
|
|
125
|
+
// package to a file, `{ data, mode }`, or to `{ directory: true }` for a
|
|
126
|
+
// directory. A changed file keeps its mode, and a deleted one leaves its
|
|
127
|
+
// directory, empty or not, as pnpm leaves them. Hands back a new Map; the
|
|
128
|
+
// one given is left as it is.
|
|
129
|
+
export function applyPatch(files, patch) {
|
|
130
|
+
const next = new Map(files)
|
|
131
|
+
for (const file of patch) {
|
|
132
|
+
const current = next.get(file.path)
|
|
133
|
+
if (file.change === 'create') {
|
|
134
|
+
const taken = current !== undefined || [...next.keys()].some((path) => path.startsWith(`${file.path}/`)) || parentsOf(file.path).some((dir) => next.get(dir)?.data !== undefined)
|
|
135
|
+
if (taken) throw new DeptreeError('creates a file that is there', file.where)
|
|
136
|
+
next.set(file.path, { data: bytesOf(applyTo('', file), file.where), mode: file.mode })
|
|
137
|
+
continue
|
|
138
|
+
}
|
|
139
|
+
if (current?.data === undefined) throw new DeptreeError('changes a file that is not there', file.where)
|
|
140
|
+
// A deletion git writes with --irreversible-delete has no hunks, and
|
|
141
|
+
// says nothing of what it deletes.
|
|
142
|
+
const result = file.change === 'delete' && file.hunks.length === 0 ? '' : applyTo(textOf(current, file.where), file)
|
|
143
|
+
if (file.change === 'delete') {
|
|
144
|
+
if (result !== '') throw new DeptreeError('deletes a file it does not remove all of', file.where)
|
|
145
|
+
next.delete(file.path)
|
|
146
|
+
for (const dir of parentsOf(file.path)) if (!next.has(dir)) next.set(dir, { directory: true })
|
|
147
|
+
} else {
|
|
148
|
+
next.set(file.path, { data: bytesOf(result, file.where), mode: current.mode })
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return next
|
|
152
|
+
}
|