@exodus/stasis-core 1.0.0-beta.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/README.md +9 -0
- package/bin/stasis-core.js +116 -0
- package/package.json +50 -0
- package/src/brotli.js +21 -0
- package/src/bundle.js +256 -0
- package/src/config.js +426 -0
- package/src/fs.js +712 -0
- package/src/hooks.js +826 -0
- package/src/loader.js +6 -0
- package/src/lockfile.js +152 -0
- package/src/package.cjs +1 -0
- package/src/prune.js +532 -0
- package/src/state-util.js +64 -0
- package/src/state.js +2375 -0
- package/src/util.js +210 -0
package/src/prune.js
ADDED
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
realpathSync,
|
|
7
|
+
renameSync,
|
|
8
|
+
rmdirSync,
|
|
9
|
+
unlinkSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from 'node:fs'
|
|
12
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from 'node:path'
|
|
13
|
+
|
|
14
|
+
import { Lockfile } from './lockfile.js'
|
|
15
|
+
import { sha512integrity } from './state-util.js' // also runs the posix-sep assertion
|
|
16
|
+
import { isPlainObject, moduleFileKey, posixPathEscapes, splitNodeModulesPath } from './util.js'
|
|
17
|
+
|
|
18
|
+
const LOCKFILE = 'stasis.lock.json'
|
|
19
|
+
|
|
20
|
+
// Fields we copy (sanitised) from a dependency's on-disk package.json into the
|
|
21
|
+
// minimised rewrite. Everything else -- scripts, dependencies, engines, config,
|
|
22
|
+
// ... -- is dropped: it isn't recorded in the lockfile, isn't needed to resolve
|
|
23
|
+
// or load the package, and (scripts especially) is pure attack surface in an
|
|
24
|
+
// installed tree. `name`/`version` are NOT read from disk; they come from the
|
|
25
|
+
// lockfile, which is the attested source of the package's identity.
|
|
26
|
+
const ROOT_FIELD_ORDER = [
|
|
27
|
+
'license',
|
|
28
|
+
'type',
|
|
29
|
+
'main',
|
|
30
|
+
'module',
|
|
31
|
+
'jsnext:main',
|
|
32
|
+
'jsnext',
|
|
33
|
+
'source',
|
|
34
|
+
'browser',
|
|
35
|
+
'react-native',
|
|
36
|
+
'bin',
|
|
37
|
+
'sideEffects',
|
|
38
|
+
'exports',
|
|
39
|
+
'imports',
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
function assertGlobalVirtualStoreDisabled() {
|
|
43
|
+
// pnpm exports its settings as `npm_config_*` env vars to hooks and
|
|
44
|
+
// lifecycle scripts. When the global virtual store is enabled, node_modules
|
|
45
|
+
// is dominated by symlinks into a shared store; pruning would walk past
|
|
46
|
+
// most files (we skip symlinks) and the lockfile-vs-disk comparison stops
|
|
47
|
+
// being meaningful. Reject before touching anything.
|
|
48
|
+
const env = process.env.npm_config_enable_global_virtual_store
|
|
49
|
+
if (env !== undefined && env !== 'false' && env !== '') {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`stasis prune: enableGlobalVirtualStore must be false, got ${JSON.stringify(env)}`,
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function loadLockfile(root) {
|
|
57
|
+
const path = join(root, LOCKFILE)
|
|
58
|
+
assert.ok(existsSync(path), `stasis prune: ${LOCKFILE} not found at ${path}`)
|
|
59
|
+
return Lockfile.parse(readFileSync(path, 'utf8'))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function buildExpected(lockfile) {
|
|
63
|
+
const expected = new Map()
|
|
64
|
+
const knownDirs = new Set()
|
|
65
|
+
for (const [dir, { files }] of lockfile.modules) {
|
|
66
|
+
if (!dir.includes('node_modules')) continue // workspace sources, not pnpm-managed
|
|
67
|
+
knownDirs.add(dir)
|
|
68
|
+
for (const [rel, hash] of Object.entries(files)) {
|
|
69
|
+
// moduleFileKey (not `${dir}/${rel}`): a readdir of a package ROOT records
|
|
70
|
+
// rel === '', whose trailing-slash join would miss the `directory` format
|
|
71
|
+
// lookup below and wrongly demand a file at `node_modules/<pkg>/`.
|
|
72
|
+
const file = moduleFileKey(dir, rel)
|
|
73
|
+
// A `directory` entry (`stasis run --fs` readdir capture) is a JSON listing
|
|
74
|
+
// attested by hash, NOT a file on disk -- the on-disk path is a real directory
|
|
75
|
+
// whose contents prune walks separately. Excluding it keeps prune from
|
|
76
|
+
// demanding a regular file at the directory's path ("missing on disk").
|
|
77
|
+
if (lockfile.formats?.get(file) === 'directory') continue
|
|
78
|
+
expected.set(file, hash)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { expected, knownDirs }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// True when `ref` (a package.json file reference like "./a.js", "a/b", "./dir")
|
|
85
|
+
// names a file the lockfile records for this module, so a rewritten manifest
|
|
86
|
+
// never points resolution at a path prune is about to delete. A ref that
|
|
87
|
+
// escapes the module (`..`/absolute) is rejected outright -- it can never name a
|
|
88
|
+
// recorded file. A subpath pattern ("./*") can't be enumerated against a fixed
|
|
89
|
+
// file list, but the only files left after prune are the attested ones -- so a
|
|
90
|
+
// (non-escaping) pattern can only ever resolve onto an attested file -- and it's
|
|
91
|
+
// kept as-is. `exact` selects the resolution discipline for a concrete path:
|
|
92
|
+
// exports/imports targets are matched verbatim (Node applies no CommonJS
|
|
93
|
+
// extension/index resolution to them), whereas main/module/browser/bin get the
|
|
94
|
+
// legacy expansion (`main: "lib"` -> `lib/index.js`).
|
|
95
|
+
const EXTENSIONS = ['.js', '.cjs', '.mjs', '.json', '.node']
|
|
96
|
+
|
|
97
|
+
function normalizeRef(ref) {
|
|
98
|
+
if (typeof ref !== 'string' || ref === '') return null
|
|
99
|
+
if (posixPathEscapes(ref)) return null // leading/embedded `..` above root, or absolute
|
|
100
|
+
return posix.normalize(ref).replace(/^\.\//u, '')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function referencesPresentFile(ref, present, { exact = false } = {}) {
|
|
104
|
+
const base = normalizeRef(ref)
|
|
105
|
+
if (base === null) return false // non-string, empty, escaping, or absolute
|
|
106
|
+
if (base.includes('*')) return true // subpath pattern: unverifiable, kept (see above)
|
|
107
|
+
if (present.has(base)) return true
|
|
108
|
+
if (exact) return false // exports/imports targets resolve verbatim -- no extension/index fallback
|
|
109
|
+
for (const ext of EXTENSIONS) if (present.has(base + ext)) return true
|
|
110
|
+
for (const ext of EXTENSIONS) if (present.has(`${base}/index${ext}`)) return true
|
|
111
|
+
return false
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Real exports/imports trees nest only a few conditions deep; anything past this
|
|
115
|
+
// is a malformed or hostile manifest. We cap the recursion (rather than risk a
|
|
116
|
+
// stack overflow that would abort the whole prune and leave the offending
|
|
117
|
+
// package's unattested files in place) and drop the over-deep subtree.
|
|
118
|
+
const MAX_TARGET_DEPTH = 64
|
|
119
|
+
|
|
120
|
+
// Recursively filter an exports/imports/browser subtree down to the mappings
|
|
121
|
+
// whose targets are still present. `allowBare` keeps bare specifiers (imports
|
|
122
|
+
// may map "#x" to another package; browser may stub/redirect a bare module);
|
|
123
|
+
// `allowFalse` keeps `false` (browser/react-native module stubbing); `exact` is
|
|
124
|
+
// passed through to referencesPresentFile (exports/imports match verbatim). A
|
|
125
|
+
// `null` target (exports/imports blocking a subpath) is always kept. An object
|
|
126
|
+
// or array that filters down to nothing is dropped, so we never emit an empty
|
|
127
|
+
// `exports: {}` -- which would block every subpath rather than fall back to
|
|
128
|
+
// `main`.
|
|
129
|
+
function filterTargets(value, present, opts, depth = 0) {
|
|
130
|
+
if (depth > MAX_TARGET_DEPTH) return { keep: false }
|
|
131
|
+
const { allowBare, allowFalse, exact } = opts
|
|
132
|
+
if (value === null) return { keep: true, value: null }
|
|
133
|
+
if (value === false) return allowFalse ? { keep: true, value: false } : { keep: false }
|
|
134
|
+
if (typeof value === 'string') {
|
|
135
|
+
if (value.startsWith('.')) {
|
|
136
|
+
return referencesPresentFile(value, present, { exact }) ? { keep: true, value } : { keep: false }
|
|
137
|
+
}
|
|
138
|
+
return allowBare ? { keep: true, value } : { keep: false }
|
|
139
|
+
}
|
|
140
|
+
if (Array.isArray(value)) {
|
|
141
|
+
const out = []
|
|
142
|
+
for (const item of value) {
|
|
143
|
+
const r = filterTargets(item, present, opts, depth + 1)
|
|
144
|
+
if (r.keep) out.push(r.value)
|
|
145
|
+
}
|
|
146
|
+
return out.length > 0 ? { keep: true, value: out } : { keep: false }
|
|
147
|
+
}
|
|
148
|
+
if (isPlainObject(value)) {
|
|
149
|
+
const out = {}
|
|
150
|
+
let any = false
|
|
151
|
+
for (const [k, v] of Object.entries(value)) {
|
|
152
|
+
const r = filterTargets(v, present, opts, depth + 1)
|
|
153
|
+
if (r.keep) {
|
|
154
|
+
out[k] = r.value
|
|
155
|
+
any = true
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return any ? { keep: true, value: out } : { keep: false }
|
|
159
|
+
}
|
|
160
|
+
return { keep: false }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Printable ASCII only: rejects the C0 (incl. DEL) and C1 control ranges and all
|
|
164
|
+
// non-ASCII, so a copied `license` can't smuggle control or escape characters.
|
|
165
|
+
const isAscii = (s) => /^[\x20-\x7e]*$/u.test(s)
|
|
166
|
+
|
|
167
|
+
// `browser`/`react-native`: a string (an alternate entry, like `main`) or a map
|
|
168
|
+
// of source path / bare module -> replacement file or `false` (stub). Returns
|
|
169
|
+
// the filtered value, or undefined to drop the field.
|
|
170
|
+
function browserField(value, present) {
|
|
171
|
+
if (typeof value === 'string') return referencesPresentFile(value, present) ? value : undefined
|
|
172
|
+
if (isPlainObject(value)) {
|
|
173
|
+
const r = filterTargets(value, present, { allowBare: true, allowFalse: true, exact: false })
|
|
174
|
+
if (r.keep) return r.value
|
|
175
|
+
}
|
|
176
|
+
return undefined
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// The fields that govern how a package's own files resolve and load: the module
|
|
180
|
+
// system flag (`type`), the entry points (`main`/`module`/`browser`/
|
|
181
|
+
// `react-native`), and the subpath maps (`exports`/`imports`). Shared by the
|
|
182
|
+
// package root and by nested markers -- a nested marker is exactly this subset.
|
|
183
|
+
// Every file reference is filtered to a present (attested) file: exports/imports
|
|
184
|
+
// match verbatim (`exact`), the entry fields get Node's extension/index expansion.
|
|
185
|
+
function resolutionFields(pkg, present) {
|
|
186
|
+
const out = { __proto__: null }
|
|
187
|
+
|
|
188
|
+
// Only the two values Node honours; a flipped/garbage `type` is dropped so it
|
|
189
|
+
// falls back to the default rather than recategorising the package's .js files.
|
|
190
|
+
if (pkg.type === 'module' || pkg.type === 'commonjs') out.type = pkg.type
|
|
191
|
+
|
|
192
|
+
if (referencesPresentFile(pkg.main, present)) out.main = pkg.main
|
|
193
|
+
// `module`: non-standard ESM entry that bundlers (webpack/rollup/esbuild)
|
|
194
|
+
// prefer over `main`; kept on the same "points at a present file" basis.
|
|
195
|
+
if (referencesPresentFile(pkg.module, present)) out.module = pkg.module
|
|
196
|
+
|
|
197
|
+
// Legacy / framework entry points read via bundler `mainFields`: `jsnext:main`
|
|
198
|
+
// and `jsnext` (in Vite's default list -- dropping them downgrades an older
|
|
199
|
+
// dep ESM->CJS under Vite) and `source` (Parcel, Metro/React Native). String
|
|
200
|
+
// entries, kept like main/module; inert for resolvers that don't read them.
|
|
201
|
+
for (const field of ['jsnext:main', 'jsnext', 'source']) {
|
|
202
|
+
if (referencesPresentFile(pkg[field], present)) out[field] = pkg[field]
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (const field of ['browser', 'react-native']) {
|
|
206
|
+
const v = browserField(pkg[field], present)
|
|
207
|
+
if (v !== undefined) out[field] = v
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const exp = filterTargets(pkg.exports, present, { allowBare: false, allowFalse: false, exact: true })
|
|
211
|
+
if (exp.keep) out.exports = exp.value
|
|
212
|
+
const imp = filterTargets(pkg.imports, present, { allowBare: true, allowFalse: false, exact: true })
|
|
213
|
+
if (imp.keep) out.imports = imp.value
|
|
214
|
+
|
|
215
|
+
return out
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Compute the sanitised field set for a package *root* package.json (the dir is
|
|
219
|
+
// a module the lockfile records). `name`/`version` are authoritative from the
|
|
220
|
+
// lockfile; the rest are copied from `pkg` (the untrusted on-disk manifest)
|
|
221
|
+
// only after passing their field-specific guard. On top of the shared
|
|
222
|
+
// resolution fields, a root also carries `license` and the package-level
|
|
223
|
+
// `bin`/`sideEffects`.
|
|
224
|
+
function minimalRootFields(pkg, { name, version, present }) {
|
|
225
|
+
const fields = resolutionFields(pkg, present)
|
|
226
|
+
|
|
227
|
+
// A short, plain-ASCII license string is harmless metadata; anything else
|
|
228
|
+
// (object/array SPDX forms, long or non-ASCII blobs) is dropped.
|
|
229
|
+
if (typeof pkg.license === 'string' && isAscii(pkg.license) && Buffer.byteLength(pkg.license) < 64) {
|
|
230
|
+
fields.license = pkg.license
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// bin: a string (one command, named after the package) or a map of
|
|
234
|
+
// command -> file. Keep only entries that point at a present file.
|
|
235
|
+
if (typeof pkg.bin === 'string') {
|
|
236
|
+
if (referencesPresentFile(pkg.bin, present)) fields.bin = pkg.bin
|
|
237
|
+
} else if (isPlainObject(pkg.bin)) {
|
|
238
|
+
const bin = {}
|
|
239
|
+
let any = false
|
|
240
|
+
for (const [cmd, target] of Object.entries(pkg.bin)) {
|
|
241
|
+
if (typeof target === 'string' && referencesPresentFile(target, present)) {
|
|
242
|
+
bin[cmd] = target
|
|
243
|
+
any = true
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (any) fields.bin = bin
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// sideEffects: a boolean is kept verbatim; an array is filtered to entries
|
|
250
|
+
// that are present files (glob patterns are kept, see referencesPresentFile).
|
|
251
|
+
// A list that filters to empty is dropped rather than emitted as `[]`, which
|
|
252
|
+
// would assert "no side effects" and over-eagerly tree-shake.
|
|
253
|
+
if (typeof pkg.sideEffects === 'boolean') {
|
|
254
|
+
fields.sideEffects = pkg.sideEffects
|
|
255
|
+
} else if (Array.isArray(pkg.sideEffects)) {
|
|
256
|
+
const se = pkg.sideEffects.filter((e) => referencesPresentFile(e, present))
|
|
257
|
+
if (se.length > 0) fields.sideEffects = se
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// name/version first, then the copied fields in a stable order.
|
|
261
|
+
const out = { name, version }
|
|
262
|
+
for (const key of ROOT_FIELD_ORDER) if (key in fields) out[key] = fields[key]
|
|
263
|
+
return out
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// A nested package.json (e.g. a `dist/cjs/package.json` that flips the module
|
|
267
|
+
// system for that subtree) carries no package identity, so we drop name/version
|
|
268
|
+
// and the package-level fields (license/bin/sideEffects) and keep exactly the
|
|
269
|
+
// resolution fields, filtered relative to this dir. Dropping them outright -- as
|
|
270
|
+
// the old prune did, since their dir isn't a recorded module and they aren't in
|
|
271
|
+
// the lockfile file list -- would silently revert this subtree's .js files to
|
|
272
|
+
// the package root's module system and break `require('pkg/subdir')` and
|
|
273
|
+
// `#imports` resolution within it.
|
|
274
|
+
function minimalNestedFields(pkg, present) {
|
|
275
|
+
return resolutionFields(pkg, present)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const PNPM_WORKSPACE = 'pnpm-workspace.yaml'
|
|
279
|
+
|
|
280
|
+
// A pnpm workspace root is marked by a pnpm-workspace.yaml. In a workspace,
|
|
281
|
+
// prune covers every package's node_modules (not just the root's) and widens
|
|
282
|
+
// the symlink-containment boundary from node_modules to the whole workspace --
|
|
283
|
+
// a workspace dependency legitimately links to a sibling package's source dir,
|
|
284
|
+
// which lives outside any node_modules but inside the workspace. Outside a
|
|
285
|
+
// workspace the boundary stays at node_modules (a self-contained install must
|
|
286
|
+
// not reach beyond it).
|
|
287
|
+
function isPnpmWorkspaceRoot(root) {
|
|
288
|
+
return existsSync(join(root, PNPM_WORKSPACE))
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Every node_modules directory in the workspace (the root's and each package's).
|
|
292
|
+
// We don't descend into a node_modules once found -- prune walks its internals
|
|
293
|
+
// itself -- nor into dotdirs (.git, ...), and we never leave `root` (symlinked
|
|
294
|
+
// dirs that aren't node_modules aren't followed).
|
|
295
|
+
function discoverNodeModulesDirs(root) {
|
|
296
|
+
const found = []
|
|
297
|
+
const stack = [root]
|
|
298
|
+
while (stack.length > 0) {
|
|
299
|
+
const dir = stack.pop()
|
|
300
|
+
let entries
|
|
301
|
+
try {
|
|
302
|
+
entries = readdirSync(dir, { withFileTypes: true })
|
|
303
|
+
} catch {
|
|
304
|
+
continue
|
|
305
|
+
}
|
|
306
|
+
for (const entry of entries) {
|
|
307
|
+
if (entry.name.startsWith('.')) continue // .git, .pnpm-* state, other dotdirs
|
|
308
|
+
const full = join(dir, entry.name)
|
|
309
|
+
if (entry.name === 'node_modules') {
|
|
310
|
+
// Only a real node_modules dir is a prune target. A package whose
|
|
311
|
+
// node_modules is itself a symlink is skipped here (not a real pnpm
|
|
312
|
+
// layout); if the lockfile records files under it, prune fails closed
|
|
313
|
+
// ("missing on disk") rather than walking through the link.
|
|
314
|
+
if (entry.isDirectory()) found.push(full)
|
|
315
|
+
continue // don't descend; prune walks node_modules internals itself
|
|
316
|
+
}
|
|
317
|
+
if (entry.isDirectory()) stack.push(full) // a workspace source dir: keep looking
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return found
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// A symlink is left in place only if its real target stays inside `boundary`
|
|
324
|
+
// (`label` names it in the error): node_modules for a self-contained install,
|
|
325
|
+
// or the whole workspace when pruning a pnpm workspace. That boundary is prune's
|
|
326
|
+
// containment perimeter -- a link reaching outside it would keep bytes prune
|
|
327
|
+
// never walks reachable from the pruned tree. pnpm's public `node_modules/<pkg>`
|
|
328
|
+
// links into `.pnpm`, whose real bytes are walked directly, so the link needs no
|
|
329
|
+
// validation or deletion; in a workspace a dep also links to a sibling source
|
|
330
|
+
// dir (inside the workspace, outside any node_modules), which is left as-is and
|
|
331
|
+
// not validated. An escaping link fails closed; because this runs during the
|
|
332
|
+
// planning walk, before any unlink/rewrite, it aborts prune without touching
|
|
333
|
+
// disk. A dangling link points at nothing and can't leak, so it's left as-is.
|
|
334
|
+
//
|
|
335
|
+
// (Deliberately NOT the loaders' assertRealPathWithinBase: it rethrows ENOENT,
|
|
336
|
+
// whereas here a dangling link is tolerated.)
|
|
337
|
+
function assertSymlinkInternal({ root, realBoundary, label }, linkPath) {
|
|
338
|
+
let real
|
|
339
|
+
try {
|
|
340
|
+
real = realpathSync(linkPath)
|
|
341
|
+
} catch (err) {
|
|
342
|
+
if (err.code === 'ENOENT') return // dangling link
|
|
343
|
+
throw err
|
|
344
|
+
}
|
|
345
|
+
const rel = relative(realBoundary, real)
|
|
346
|
+
if (rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))) return // inside the boundary
|
|
347
|
+
throw new Error(`stasis prune: symlink escapes ${label}: ${relative(root, linkPath)} -> ${real}`)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function* walkFiles(nmRoot, ctx) {
|
|
351
|
+
if (!existsSync(nmRoot)) return
|
|
352
|
+
const stack = [nmRoot]
|
|
353
|
+
while (stack.length > 0) {
|
|
354
|
+
const dir = stack.pop()
|
|
355
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
356
|
+
const full = join(dir, entry.name)
|
|
357
|
+
if (entry.isSymbolicLink()) {
|
|
358
|
+
assertSymlinkInternal(ctx, full)
|
|
359
|
+
continue
|
|
360
|
+
}
|
|
361
|
+
if (entry.isDirectory()) stack.push(full)
|
|
362
|
+
else if (entry.isFile()) yield full
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Walk every node_modules root, tagging each file with the root it came from so
|
|
368
|
+
// deletion and empty-dir pruning stay scoped to that node_modules.
|
|
369
|
+
function* walkAll(nmRoots, ctx) {
|
|
370
|
+
for (const nmRoot of nmRoots) {
|
|
371
|
+
for (const full of walkFiles(nmRoot, ctx)) yield { full, nmRoot }
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function pruneEmptyDirs(dir, stopAt) {
|
|
376
|
+
while (dir.startsWith(stopAt) && dir !== stopAt && existsSync(dir)) {
|
|
377
|
+
let entries
|
|
378
|
+
try {
|
|
379
|
+
entries = readdirSync(dir)
|
|
380
|
+
} catch {
|
|
381
|
+
return
|
|
382
|
+
}
|
|
383
|
+
if (entries.length > 0) return
|
|
384
|
+
try {
|
|
385
|
+
rmdirSync(dir)
|
|
386
|
+
} catch {
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
dir = dirname(dir)
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function prune({ root = process.cwd() } = {}) {
|
|
394
|
+
assertGlobalVirtualStoreDisabled()
|
|
395
|
+
root = resolve(root)
|
|
396
|
+
// In a pnpm workspace, prune every package's node_modules (not just the
|
|
397
|
+
// root's) and bound symlinks by the whole workspace; otherwise just the root's
|
|
398
|
+
// node_modules, bounded by node_modules itself.
|
|
399
|
+
const workspace = isPnpmWorkspaceRoot(root)
|
|
400
|
+
const nmRoots = workspace ? discoverNodeModulesDirs(root) : [join(root, 'node_modules')]
|
|
401
|
+
const boundary = workspace ? root : join(root, 'node_modules')
|
|
402
|
+
const ctx = {
|
|
403
|
+
root,
|
|
404
|
+
realBoundary: existsSync(boundary) ? realpathSync(boundary) : boundary,
|
|
405
|
+
label: workspace ? 'the workspace' : 'node_modules',
|
|
406
|
+
}
|
|
407
|
+
const lockfile = loadLockfile(root)
|
|
408
|
+
const { expected, knownDirs } = buildExpected(lockfile)
|
|
409
|
+
|
|
410
|
+
// Plan first, mutate later: walk the tree, validate every tracked file,
|
|
411
|
+
// collect the deletion and package.json-rewrite lists, and verify nothing in
|
|
412
|
+
// the lockfile is missing. Bail with a thrown error before touching disk on
|
|
413
|
+
// any failure.
|
|
414
|
+
const toRemove = []
|
|
415
|
+
const toMinimize = []
|
|
416
|
+
const validated = []
|
|
417
|
+
const kept = []
|
|
418
|
+
const seen = new Set()
|
|
419
|
+
const mismatches = []
|
|
420
|
+
|
|
421
|
+
for (const { full, nmRoot } of walkAll(nmRoots, ctx)) {
|
|
422
|
+
const rel = relative(root, full)
|
|
423
|
+
const expectedHash = expected.get(rel)
|
|
424
|
+
if (expectedHash !== undefined) {
|
|
425
|
+
seen.add(rel)
|
|
426
|
+
const actual = sha512integrity(readFileSync(full))
|
|
427
|
+
if (actual === expectedHash) {
|
|
428
|
+
validated.push(rel)
|
|
429
|
+
kept.push(rel)
|
|
430
|
+
} else {
|
|
431
|
+
mismatches.push({ rel, expected: expectedHash, actual })
|
|
432
|
+
}
|
|
433
|
+
continue
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// package.json files aren't enumerated in the lockfile (their contents
|
|
437
|
+
// aren't recorded), but they're load-bearing for resolution, so rather than
|
|
438
|
+
// keep them verbatim we rewrite them down to the minimal, lockfile-derived
|
|
439
|
+
// field set when they belong to a module the lockfile recognises. A
|
|
440
|
+
// package.json that IS in the lockfile (imported directly, so its bytes are
|
|
441
|
+
// attested) took the hash-validated branch above and is kept in full.
|
|
442
|
+
if (basename(rel) === 'package.json') {
|
|
443
|
+
const owner = splitNodeModulesPath(rel)
|
|
444
|
+
if (owner && knownDirs.has(owner.dir)) {
|
|
445
|
+
const raw = readFileSync(full, 'utf8')
|
|
446
|
+
let parsed
|
|
447
|
+
try {
|
|
448
|
+
parsed = JSON.parse(raw)
|
|
449
|
+
} catch {
|
|
450
|
+
parsed = null
|
|
451
|
+
}
|
|
452
|
+
const pkg = isPlainObject(parsed) ? parsed : {}
|
|
453
|
+
let next
|
|
454
|
+
if (dirname(rel) === owner.dir) {
|
|
455
|
+
const { name, version, files } = lockfile.modules.get(owner.dir)
|
|
456
|
+
// The package.json itself survives prune, so it's always a legal
|
|
457
|
+
// self-reference target (e.g. `exports: { "./package.json": ... }`).
|
|
458
|
+
const present = new Set([...Object.keys(files), 'package.json'])
|
|
459
|
+
next = minimalRootFields(pkg, { name, version, present })
|
|
460
|
+
} else {
|
|
461
|
+
// A nested package.json's file references are relative to its own
|
|
462
|
+
// dir, so re-base the module's recorded files onto it before filtering.
|
|
463
|
+
const { files } = lockfile.modules.get(owner.dir)
|
|
464
|
+
const prefix = `${relative(owner.dir, dirname(rel))}/`
|
|
465
|
+
const present = new Set(['package.json'])
|
|
466
|
+
for (const f of Object.keys(files)) {
|
|
467
|
+
if (f.startsWith(prefix)) present.add(f.slice(prefix.length))
|
|
468
|
+
}
|
|
469
|
+
next = minimalNestedFields(pkg, present)
|
|
470
|
+
}
|
|
471
|
+
const newText = `${JSON.stringify(next, undefined, 2)}\n`
|
|
472
|
+
kept.push(rel)
|
|
473
|
+
if (newText !== raw) toMinimize.push({ full, rel, text: newText })
|
|
474
|
+
continue
|
|
475
|
+
}
|
|
476
|
+
// package.json under a dir the lockfile doesn't own falls through and is
|
|
477
|
+
// pruned like any other untracked file.
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// Defense-in-depth: the path must resolve inside the node_modules we're
|
|
481
|
+
// walking (no symlink escape, no `..` traversal) before we queue it for
|
|
482
|
+
// deletion -- so prune never deletes anything outside a node_modules.
|
|
483
|
+
assert.ok(
|
|
484
|
+
full.startsWith(`${nmRoot}${sep}`),
|
|
485
|
+
`refusing to remove path outside node_modules: ${full}`,
|
|
486
|
+
)
|
|
487
|
+
toRemove.push({ full, nmRoot })
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const missing = []
|
|
491
|
+
for (const [rel] of expected) {
|
|
492
|
+
if (!seen.has(rel)) missing.push(rel)
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (mismatches.length > 0) {
|
|
496
|
+
const lines = mismatches.map(({ rel, expected: exp, actual }) =>
|
|
497
|
+
` ${rel}: expected ${exp}, got ${actual}`).join('\n')
|
|
498
|
+
throw new Error(`stasis prune: hash mismatch for ${mismatches.length} file(s):\n${lines}`)
|
|
499
|
+
}
|
|
500
|
+
if (missing.length > 0) {
|
|
501
|
+
const sample = missing.slice(0, 5).join(', ')
|
|
502
|
+
const suffix = missing.length > 5 ? `, ... (${missing.length} total)` : ''
|
|
503
|
+
throw new Error(`stasis prune: files listed in lockfile are missing on disk: ${sample}${suffix}`)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const minimized = []
|
|
507
|
+
for (const [i, { full, rel, text }] of toMinimize.entries()) {
|
|
508
|
+
// Replace, never modify in place: a dependency's package.json is commonly a
|
|
509
|
+
// hardlink into pnpm's content-addressable store, so it shares an inode with
|
|
510
|
+
// every other project (and other versions) linked from that store. An
|
|
511
|
+
// in-place write would mutate all of them. Write a fresh file and rename it
|
|
512
|
+
// over the entry -- atomic, and it breaks the hardlink, leaving the store
|
|
513
|
+
// copy untouched.
|
|
514
|
+
const tmp = `${full}.stasis-${process.pid}-${i}.tmp`
|
|
515
|
+
writeFileSync(tmp, text)
|
|
516
|
+
renameSync(tmp, full)
|
|
517
|
+
minimized.push(rel)
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const removed = []
|
|
521
|
+
const touchedDirs = new Map() // dir -> the node_modules root under it (prune stops there)
|
|
522
|
+
for (const { full, nmRoot } of toRemove) {
|
|
523
|
+
unlinkSync(full)
|
|
524
|
+
removed.push(relative(root, full))
|
|
525
|
+
touchedDirs.set(dirname(full), nmRoot)
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const sortedDirs = [...touchedDirs.keys()].toSorted((a, b) => b.length - a.length)
|
|
529
|
+
for (const d of sortedDirs) pruneEmptyDirs(d, touchedDirs.get(d))
|
|
530
|
+
|
|
531
|
+
return { removed, validated, kept, minimized }
|
|
532
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { hash } from 'node:crypto'
|
|
3
|
+
import * as fs from 'node:fs'
|
|
4
|
+
import { join, resolve, sep } from 'node:path'
|
|
5
|
+
|
|
6
|
+
// The genuine fs readers, snapshotted off the namespace before any --fs patch. This
|
|
7
|
+
// module is the single source of truth for "the real reader" -- imported by stasis's
|
|
8
|
+
// own internals (here; and fs.js's --fs hook imports realReadFileSync so its capture
|
|
9
|
+
// path sees real bytes and never recurses) AND by every bundler plugin (webpack.js /
|
|
10
|
+
// esbuild.js / metro.js; esbuild uses the async realReadFile for its async onLoad). A
|
|
11
|
+
// plugin's capture read is stasis-internal bookkeeping, NOT a program fs
|
|
12
|
+
// read, so routing it through the patched fs would re-record the bundler's module graph
|
|
13
|
+
// into the preload/main bundle -- duplicating into main what the plugin captured into
|
|
14
|
+
// its own sidecar. `import { readFileSync } from 'node:fs'` would NOT do: `stasis run
|
|
15
|
+
// --fs` monkey-patches fs.readFileSync then calls module.syncBuiltinESMExports(), which
|
|
16
|
+
// rebinds that named import to the patched fn -- and a plugin loads AFTER the patch.
|
|
17
|
+
// Snapshotting off the namespace dodges that. state-util.js is the lowest-level module
|
|
18
|
+
// (a leaf with no internal imports), evaluated during the `--import` preload before any
|
|
19
|
+
// patch, so these are always the real builtins -- and the snapshot is taken as early as
|
|
20
|
+
// possible. (`fs.promises` IS node:fs/promises, the same object, so realReadFile also
|
|
21
|
+
// covers `import { readFile } from 'node:fs/promises'`.)
|
|
22
|
+
export const realReadFileSync = fs.readFileSync
|
|
23
|
+
export const realReadFile = fs.promises.readFile
|
|
24
|
+
const { realpathSync } = fs
|
|
25
|
+
|
|
26
|
+
assert.equal(sep, '/', 'Not tested on Windows')
|
|
27
|
+
|
|
28
|
+
export const sha512integrity = (x) => `sha512-${hash('sha512', x, 'base64')}`
|
|
29
|
+
|
|
30
|
+
export function readFileSyncMaybe(dir, file, encoding) {
|
|
31
|
+
try {
|
|
32
|
+
return realReadFileSync(join(dir, file), encoding)
|
|
33
|
+
} catch (err) {
|
|
34
|
+
if (err.code === 'ENOENT') return null
|
|
35
|
+
throw err
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function noupsert(map, key, value) {
|
|
40
|
+
if (map.has(key)) {
|
|
41
|
+
assert.deepStrictEqual(map.get(key), value, `Conflict for ${JSON.stringify(key)}`)
|
|
42
|
+
} else {
|
|
43
|
+
map.set(key, value)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Canonicalize a filesystem path for write-target collision detection.
|
|
48
|
+
// `resolve()` normalizes `./` and `../` but NOT symlinks -- two paths through
|
|
49
|
+
// different symlink chains that ultimately name the same inode would compare as
|
|
50
|
+
// distinct strings, letting a "is this the same target?" check silently fork
|
|
51
|
+
// (the resourcesBundleFile/lockFile/bundleFile claim sets). `realpathSync`
|
|
52
|
+
// closes that hole when the file already exists;
|
|
53
|
+
// for not-yet-written targets (the common case for a fresh capture's write
|
|
54
|
+
// path), fall back to the lexical `resolve()` -- there's no symlink to follow,
|
|
55
|
+
// and a later writer claiming the realpath of the same target will canonicalize
|
|
56
|
+
// to the same string the first writer's resolve() produced.
|
|
57
|
+
export function canonicalizePath(p) {
|
|
58
|
+
const abs = resolve(p)
|
|
59
|
+
try {
|
|
60
|
+
return realpathSync(abs)
|
|
61
|
+
} catch {
|
|
62
|
+
return abs
|
|
63
|
+
}
|
|
64
|
+
}
|