@exodus/stasis-core 1.0.0-beta.1 → 1.0.0-beta.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.
package/src/prune.js CHANGED
@@ -1,55 +1,22 @@
1
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'
2
+ import { existsSync, readFileSync, readdirSync, realpathSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs'
3
+ import { basename, dirname, join, posix, relative, resolve, sep } from 'node:path'
13
4
 
14
5
  import { Lockfile } from './lockfile.js'
15
6
  import { sha512integrity } from './state-util.js' // also runs the posix-sep assertion
16
- import { isPlainObject, moduleFileKey, posixPathEscapes, splitNodeModulesPath } from './util.js'
7
+ import { hasNodeModulesSegment, isPathWithin, isPlainObject, moduleFileKey, posixPathEscapes, splitNodeModulesPath } from './util.js'
17
8
 
18
9
  const LOCKFILE = 'stasis.lock.json'
19
10
 
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
- ]
11
+ // Fields copied (sanitised) from a dep's on-disk package.json; everything else is dropped as
12
+ // attack surface (scripts especially).
13
+ const ROOT_FIELD_ORDER = ['license', 'type', 'main', 'module', 'jsnext:main', 'jsnext', 'source', 'browser', 'react-native', 'bin', 'sideEffects', 'exports', 'imports']
41
14
 
42
15
  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.
16
+ // Global virtual store fills node_modules with symlinks, which prune skips -- lockfile-vs-disk goes blind.
48
17
  const env = process.env.npm_config_enable_global_virtual_store
49
18
  if (env !== undefined && env !== 'false' && env !== '') {
50
- throw new Error(
51
- `stasis prune: enableGlobalVirtualStore must be false, got ${JSON.stringify(env)}`,
52
- )
19
+ throw new Error(`stasis prune: enableGlobalVirtualStore must be false, got ${JSON.stringify(env)}`)
53
20
  }
54
21
  }
55
22
 
@@ -59,21 +26,18 @@ function loadLockfile(root) {
59
26
  return Lockfile.parse(readFileSync(path, 'utf8'))
60
27
  }
61
28
 
29
+ // The file -> hash the lockfile expects under node_modules (workspace sources aren't pnpm-managed),
30
+ // and the bucket dirs it knows.
62
31
  function buildExpected(lockfile) {
63
32
  const expected = new Map()
64
33
  const knownDirs = new Set()
65
34
  for (const [dir, { files }] of lockfile.modules) {
66
- if (!dir.includes('node_modules')) continue // workspace sources, not pnpm-managed
35
+ if (!hasNodeModulesSegment(dir)) continue
67
36
  knownDirs.add(dir)
68
37
  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>/`.
38
+ // moduleFileKey, not `${dir}/${rel}`: a root capture's rel === '' would demand a file at `<pkg>/`.
72
39
  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").
40
+ // A `directory` entry is an attested listing, not a file on disk; demanding one would fail "missing on disk".
77
41
  if (lockfile.formats?.get(file) === 'directory') continue
78
42
  expected.set(file, hash)
79
43
  }
@@ -81,61 +45,36 @@ function buildExpected(lockfile) {
81
45
  return { expected, knownDirs }
82
46
  }
83
47
 
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`).
48
+ // A rewritten manifest must never point resolution at a path prune is about to delete.
95
49
  const EXTENSIONS = ['.js', '.cjs', '.mjs', '.json', '.node']
96
50
 
97
51
  function normalizeRef(ref) {
98
- if (typeof ref !== 'string' || ref === '') return null
99
- if (posixPathEscapes(ref)) return null // leading/embedded `..` above root, or absolute
52
+ if (typeof ref !== 'string' || ref === '' || posixPathEscapes(ref)) return null
100
53
  return posix.normalize(ref).replace(/^\.\//u, '')
101
54
  }
102
55
 
103
56
  function referencesPresentFile(ref, present, { exact = false } = {}) {
104
57
  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)
58
+ if (base === null) return false
59
+ if (base.includes('*')) return true // subpath pattern: unverifiable, kept
107
60
  if (present.has(base)) return true
108
61
  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
62
+ return EXTENSIONS.some((ext) => present.has(base + ext) || present.has(`${base}/index${ext}`))
112
63
  }
113
64
 
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.
65
+ // Cap the recursion; real exports/imports trees nest a few deep and a deep one must not overflow.
118
66
  const MAX_TARGET_DEPTH = 64
119
67
 
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`.
68
+ // Filter a target tree to attested files. An object/array filtering to empty is dropped:
69
+ // `exports: {}` would block every subpath instead of `main`.
129
70
  function filterTargets(value, present, opts, depth = 0) {
130
71
  if (depth > MAX_TARGET_DEPTH) return { keep: false }
131
72
  const { allowBare, allowFalse, exact } = opts
132
73
  if (value === null) return { keep: true, value: null }
133
74
  if (value === false) return allowFalse ? { keep: true, value: false } : { keep: false }
134
75
  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 }
76
+ const keep = value.startsWith('.') ? referencesPresentFile(value, present, { exact }) : allowBare
77
+ return keep ? { keep: true, value } : { keep: false }
139
78
  }
140
79
  if (Array.isArray(value)) {
141
80
  const out = []
@@ -160,78 +99,45 @@ function filterTargets(value, present, opts, depth = 0) {
160
99
  return { keep: false }
161
100
  }
162
101
 
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.
102
+ // Printable ASCII only, so a copied `license` can't smuggle control or escape characters.
165
103
  const isAscii = (s) => /^[\x20-\x7e]*$/u.test(s)
166
104
 
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.
105
+ // `browser`/`react-native`: a string, or a map to replacements/`false`. undefined means drop the field.
170
106
  function browserField(value, present) {
171
107
  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
108
+ if (!isPlainObject(value)) return undefined
109
+ const r = filterTargets(value, present, { allowBare: true, allowFalse: true, exact: false })
110
+ return r.keep ? r.value : undefined
177
111
  }
178
112
 
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.
113
+ // Resolution fields (`type`, entry points, exports/imports), every file reference filtered to an attested file.
185
114
  function resolutionFields(pkg, present) {
186
115
  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.
116
+ // Only the values Node honours; a garbage `type` is dropped rather than recategorising the package's .js files.
190
117
  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']) {
118
+ // Bundler `mainFields` entries too: dropping jsnext/jsnext:main downgrades an older dep ESM->CJS under Vite.
119
+ for (const field of ['main', 'module', 'jsnext:main', 'jsnext', 'source']) {
202
120
  if (referencesPresentFile(pkg[field], present)) out[field] = pkg[field]
203
121
  }
204
-
205
122
  for (const field of ['browser', 'react-native']) {
206
123
  const v = browserField(pkg[field], present)
207
124
  if (v !== undefined) out[field] = v
208
125
  }
209
-
210
126
  const exp = filterTargets(pkg.exports, present, { allowBare: false, allowFalse: false, exact: true })
211
127
  if (exp.keep) out.exports = exp.value
212
128
  const imp = filterTargets(pkg.imports, present, { allowBare: true, allowFalse: false, exact: true })
213
129
  if (imp.keep) out.imports = imp.value
214
-
215
130
  return out
216
131
  }
217
132
 
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`.
133
+ // On-disk `pkg` is untrusted: every copied field needs its own guard. name/version come from the lockfile.
224
134
  function minimalRootFields(pkg, { name, version, present }) {
225
135
  const fields = resolutionFields(pkg, present)
226
136
 
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
137
  if (typeof pkg.license === 'string' && isAscii(pkg.license) && Buffer.byteLength(pkg.license) < 64) {
230
138
  fields.license = pkg.license
231
139
  }
232
140
 
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
141
  if (typeof pkg.bin === 'string') {
236
142
  if (referencesPresentFile(pkg.bin, present)) fields.bin = pkg.bin
237
143
  } else if (isPlainObject(pkg.bin)) {
@@ -246,10 +152,7 @@ function minimalRootFields(pkg, { name, version, present }) {
246
152
  if (any) fields.bin = bin
247
153
  }
248
154
 
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.
155
+ // A sideEffects list filtering to empty is dropped: `[]` would assert "no side effects" and over-tree-shake.
253
156
  if (typeof pkg.sideEffects === 'boolean') {
254
157
  fields.sideEffects = pkg.sideEffects
255
158
  } else if (Array.isArray(pkg.sideEffects)) {
@@ -257,41 +160,34 @@ function minimalRootFields(pkg, { name, version, present }) {
257
160
  if (se.length > 0) fields.sideEffects = se
258
161
  }
259
162
 
260
- // name/version first, then the copied fields in a stable order.
261
163
  const out = { name, version }
262
164
  for (const key of ROOT_FIELD_ORDER) if (key in fields) out[key] = fields[key]
263
165
  return out
264
166
  }
265
167
 
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) {
168
+ // The minimal manifest for a known module's package.json at `rel`, root or nested: a nested one's refs
169
+ // are relative to its own dir, so the recorded files are re-based first, and it survives because it
170
+ // flips its subtree's module system (dropping it would revert those .js files to the root's).
171
+ function minimalManifest(lockfile, owner, rel, raw) {
172
+ let parsed
173
+ try { parsed = JSON.parse(raw) } catch { parsed = null }
174
+ const pkg = isPlainObject(parsed) ? parsed : {}
175
+ const { name, version, files } = lockfile.modules.get(owner.dir)
176
+ // The package.json itself survives prune, so it's always a legal self-reference target.
177
+ const present = new Set(['package.json'])
178
+ if (dirname(rel) === owner.dir) {
179
+ for (const f of Object.keys(files)) present.add(f)
180
+ return minimalRootFields(pkg, { name, version, present })
181
+ }
182
+ const prefix = `${relative(owner.dir, dirname(rel))}/`
183
+ for (const f of Object.keys(files)) if (f.startsWith(prefix)) present.add(f.slice(prefix.length))
275
184
  return resolutionFields(pkg, present)
276
185
  }
277
186
 
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
- }
187
+ // In a workspace the symlink-containment boundary widens from node_modules to the whole workspace,
188
+ // since a dep legitimately links to a sibling package's source dir.
189
+ const isPnpmWorkspaceRoot = (root) => existsSync(join(root, 'pnpm-workspace.yaml'))
290
190
 
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
191
  function discoverNodeModulesDirs(root) {
296
192
  const found = []
297
193
  const stack = [root]
@@ -304,71 +200,49 @@ function discoverNodeModulesDirs(root) {
304
200
  continue
305
201
  }
306
202
  for (const entry of entries) {
307
- if (entry.name.startsWith('.')) continue // .git, .pnpm-* state, other dotdirs
203
+ if (entry.name.startsWith('.')) continue
308
204
  const full = join(dir, entry.name)
309
205
  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.
206
+ // A symlinked node_modules is skipped; if the lockfile records files under it, prune fails closed.
314
207
  if (entry.isDirectory()) found.push(full)
315
208
  continue // don't descend; prune walks node_modules internals itself
316
209
  }
317
- if (entry.isDirectory()) stack.push(full) // a workspace source dir: keep looking
210
+ if (entry.isDirectory()) stack.push(full)
318
211
  }
319
212
  }
320
213
  return found
321
214
  }
322
215
 
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.)
216
+ // A symlink survives prune only if its real target stays inside `boundary`. A dangling link can't leak,
217
+ // so ENOENT returns instead of throwing -- deliberately NOT the loaders' assertRealPathWithinBase.
337
218
  function assertSymlinkInternal({ root, realBoundary, label }, linkPath) {
338
219
  let real
339
220
  try {
340
221
  real = realpathSync(linkPath)
341
222
  } catch (err) {
342
- if (err.code === 'ENOENT') return // dangling link
223
+ if (err.code === 'ENOENT') return
343
224
  throw err
344
225
  }
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
- }
226
+ if (!isPathWithin(realBoundary, real)) {
227
+ throw new Error(`stasis prune: symlink escapes ${label}: ${relative(root, linkPath)} -> ${real}`)
364
228
  }
365
229
  }
366
230
 
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) {
231
+ // Every regular file under the node_modules roots, tagged with its root so deletion and empty-dir
232
+ // pruning stay scoped to it. Symlinks are checked, never followed.
233
+ function* walkFiles(nmRoots, ctx) {
370
234
  for (const nmRoot of nmRoots) {
371
- for (const full of walkFiles(nmRoot, ctx)) yield { full, nmRoot }
235
+ if (!existsSync(nmRoot)) continue
236
+ const stack = [nmRoot]
237
+ while (stack.length > 0) {
238
+ const dir = stack.pop()
239
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
240
+ const full = join(dir, entry.name)
241
+ if (entry.isSymbolicLink()) assertSymlinkInternal(ctx, full)
242
+ else if (entry.isDirectory()) stack.push(full)
243
+ else if (entry.isFile()) yield { full, nmRoot }
244
+ }
245
+ }
372
246
  }
373
247
  }
374
248
 
@@ -393,9 +267,6 @@ function pruneEmptyDirs(dir, stopAt) {
393
267
  export function prune({ root = process.cwd() } = {}) {
394
268
  assertGlobalVirtualStoreDisabled()
395
269
  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
270
  const workspace = isPnpmWorkspaceRoot(root)
400
271
  const nmRoots = workspace ? discoverNodeModulesDirs(root) : [join(root, 'node_modules')]
401
272
  const boundary = workspace ? root : join(root, 'node_modules')
@@ -407,10 +278,7 @@ export function prune({ root = process.cwd() } = {}) {
407
278
  const lockfile = loadLockfile(root)
408
279
  const { expected, knownDirs } = buildExpected(lockfile)
409
280
 
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.
281
+ // Plan first, mutate later: any failure throws before anything on disk is touched.
414
282
  const toRemove = []
415
283
  const toMinimize = []
416
284
  const validated = []
@@ -418,7 +286,7 @@ export function prune({ root = process.cwd() } = {}) {
418
286
  const seen = new Set()
419
287
  const mismatches = []
420
288
 
421
- for (const { full, nmRoot } of walkAll(nmRoots, ctx)) {
289
+ for (const { full, nmRoot } of walkFiles(nmRoots, ctx)) {
422
290
  const rel = relative(root, full)
423
291
  const expectedHash = expected.get(rel)
424
292
  if (expectedHash !== undefined) {
@@ -433,65 +301,22 @@ export function prune({ root = process.cwd() } = {}) {
433
301
  continue
434
302
  }
435
303
 
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.
304
+ // package.json isn't lockfile-enumerated but drives resolution, so rewrite (not delete) it for a known module.
305
+ const owner = basename(rel) === 'package.json' ? splitNodeModulesPath(rel) : null
306
+ if (owner && knownDirs.has(owner.dir)) {
307
+ const raw = readFileSync(full, 'utf8')
308
+ const text = `${JSON.stringify(minimalManifest(lockfile, owner, rel, raw), undefined, 2)}\n`
309
+ kept.push(rel)
310
+ if (text !== raw) toMinimize.push({ full, rel, text })
311
+ continue
478
312
  }
479
313
 
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
- )
314
+ // Defense-in-depth: the walk should already guarantee this.
315
+ assert.ok(full.startsWith(`${nmRoot}${sep}`), `refusing to remove path outside node_modules: ${full}`)
487
316
  toRemove.push({ full, nmRoot })
488
317
  }
489
318
 
490
- const missing = []
491
- for (const [rel] of expected) {
492
- if (!seen.has(rel)) missing.push(rel)
493
- }
494
-
319
+ const missing = [...expected.keys()].filter((rel) => !seen.has(rel))
495
320
  if (mismatches.length > 0) {
496
321
  const lines = mismatches.map(({ rel, expected: exp, actual }) =>
497
322
  ` ${rel}: expected ${exp}, got ${actual}`).join('\n')
@@ -505,12 +330,7 @@ export function prune({ root = process.cwd() } = {}) {
505
330
 
506
331
  const minimized = []
507
332
  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.
333
+ // Write+rename, never in place: a dep's package.json is often a hardlink into pnpm's store (shared inode).
514
334
  const tmp = `${full}.stasis-${process.pid}-${i}.tmp`
515
335
  writeFileSync(tmp, text)
516
336
  renameSync(tmp, full)
@@ -524,9 +344,7 @@ export function prune({ root = process.cwd() } = {}) {
524
344
  removed.push(relative(root, full))
525
345
  touchedDirs.set(dirname(full), nmRoot)
526
346
  }
527
-
528
- const sortedDirs = [...touchedDirs.keys()].toSorted((a, b) => b.length - a.length)
529
- for (const d of sortedDirs) pruneEmptyDirs(d, touchedDirs.get(d))
347
+ for (const d of [...touchedDirs.keys()].toSorted((a, b) => b.length - a.length)) pruneEmptyDirs(d, touchedDirs.get(d))
530
348
 
531
349
  return { removed, validated, kept, minimized }
532
350
  }
package/src/shard.js ADDED
@@ -0,0 +1,68 @@
1
+ // Wire format for child->root capture forwarding (--child-process). A shard carries only keys, formats and
2
+ // resolution edges; content, listings, stat kinds and exec bits are re-derived from the root's OWN disk, so
3
+ // a shard cannot inject content or a forged bit. `files` and `formats` are independent -- don't collapse them.
4
+
5
+ import { KNOWN_FORMATS, assert, fileMapToObject, isPlainObject, isRefusedPath, objectToMaps } from './artifact-util.js'
6
+
7
+ export const SHARD_VERSION = 1
8
+
9
+ // `imports` nests (conditions -> parent -> specifier -> file, or -> platform -> file under --metro),
10
+ // so the conversion recurses rather than flattening one level.
11
+ export function serializeShard({ scope, files, formats, imports }) {
12
+ // Never send a shard the root's parseShard would reject.
13
+ assertShard(files, formats, imports)
14
+ return JSON.stringify({
15
+ version: SHARD_VERSION,
16
+ scope,
17
+ files,
18
+ formats: fileMapToObject(formats, { sorted: false }),
19
+ imports: fileMapToObject(imports, { sorted: false }),
20
+ })
21
+ }
22
+
23
+ // The upstream signature is what makes a shard trustworthy; this only guards a malformed message,
24
+ // applying the same schema rules Lockfile.parse will. Maps keep lookups off Object.prototype.
25
+ export function parseShard(text) {
26
+ const json = JSON.parse(text)
27
+ assert(isPlainObject(json), 'shard must be an object')
28
+ assert(json.version === SHARD_VERSION, `shard version ${json.version} != ${SHARD_VERSION}`)
29
+ assert(['node_modules', 'full'].includes(json.scope), `shard scope: ${json.scope}`)
30
+ assert(Array.isArray(json.files), 'shard files must be an array')
31
+ assert(isPlainObject(json.formats) && isPlainObject(json.imports), 'shard formats/imports must be objects')
32
+
33
+ const formats = objectToMaps(json.formats)
34
+ const imports = objectToMaps(json.imports)
35
+ assertShard(json.files, formats, imports)
36
+ return { scope: json.scope, files: json.files, formats, imports }
37
+ }
38
+
39
+ // THE rules a shard's keys, formats and import edges satisfy, on write (serializeShard) and on read
40
+ // (parseShard).
41
+ function assertShard(files, formats, imports) {
42
+ for (const file of files) assertKey(file, 'shard file')
43
+ for (const [file, format] of formats) {
44
+ assertKey(file, 'shard format')
45
+ assert(KNOWN_FORMATS.has(format), `shard format for ${file}: ${format}`)
46
+ }
47
+ for (const [, byParent] of imports) {
48
+ assert(byParent instanceof Map, 'shard imports: expected a parent map')
49
+ for (const [parent, specs] of byParent) {
50
+ assertKey(parent, 'shard import parent')
51
+ assert(specs instanceof Map, `shard imports for ${parent}: expected a specifier map`)
52
+ for (const [, target] of specs) assertTarget(target, parent)
53
+ }
54
+ }
55
+ }
56
+
57
+ function assertKey(key, what) {
58
+ assert(typeof key === 'string' && key !== '' && !isRefusedPath(key), `${what}: ${key}`)
59
+ }
60
+
61
+ // A target is a file, or (--metro) a platform -> file map; nothing deeper.
62
+ function assertTarget(target, parent) {
63
+ if (target instanceof Map) {
64
+ for (const [, file] of target) assertKey(file, `shard import target from ${parent}`)
65
+ return
66
+ }
67
+ assertKey(target, `shard import target from ${parent}`)
68
+ }