@exodus/stasis-core 1.0.0-beta.4 → 1.0.0-beta.6
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/bin/stasis-core.js +17 -3
- package/package.json +6 -4
- package/src/add.js +36 -46
- package/src/artifact-util.js +263 -34
- package/src/bundle-util.js +244 -33
- package/src/bundle.js +154 -110
- package/src/config.js +163 -223
- package/src/extract.js +8 -16
- package/src/fs.js +180 -296
- package/src/hooks.js +228 -108
- package/src/host.js +45 -0
- package/src/lockfile.js +22 -75
- package/src/prune.js +63 -130
- package/src/shard.js +13 -5
- package/src/state-util.js +10 -6
- package/src/state.js +452 -479
- package/src/util.js +35 -48
package/src/lockfile.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assert, assertArtifactPath, duplicateKeyError, serializeExecutable, flatFileKeys, groupModules, hasNodeModulesSegment, mergeExecutableSets, mergeFormatMaps, mergeImportMaps, mergeModuleMaps, normalizeModule, parseEntries, parseExecutable, parseFormats, parseImports, serializeEntries, serializeFormats, serializeImports } from './artifact-util.js'
|
|
2
2
|
|
|
3
3
|
const VERSION = 0
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
assert(ecosystem === undefined || typeof ecosystem === 'string')
|
|
7
|
-
return { name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files: fromEntries(Object.entries(files)) }
|
|
8
|
-
}
|
|
5
|
+
const duplicateKey = duplicateKeyError('lockfile', 'lockfile (lock=replace)')
|
|
9
6
|
|
|
10
7
|
export class Lockfile {
|
|
11
8
|
static VERSION = VERSION
|
|
@@ -46,97 +43,47 @@ export class Lockfile {
|
|
|
46
43
|
for (const [dir, info] of Object.entries(json.modules)) {
|
|
47
44
|
assert(hasNodeModulesSegment(dir))
|
|
48
45
|
assert(info?.name && info.version && info.files)
|
|
49
|
-
modules.set(dir,
|
|
46
|
+
modules.set(dir, normalizeModule(info, dir, 'lockfile'))
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
let entries = new Set()
|
|
53
50
|
if (full) {
|
|
54
|
-
|
|
55
|
-
entries = new Set(json.entries)
|
|
51
|
+
entries = parseEntries(json.entries, 'lockfile')
|
|
56
52
|
for (const [dir, info] of Object.entries(json.sources)) {
|
|
57
53
|
assert(!hasNodeModulesSegment(dir))
|
|
58
|
-
|
|
54
|
+
// A workspace bucket may omit version (a private/unpublished package.json can lack one).
|
|
55
|
+
assert(info?.name && info.files)
|
|
56
|
+
modules.set(dir, normalizeModule(info, dir, 'lockfile'))
|
|
59
57
|
}
|
|
60
58
|
}
|
|
61
59
|
|
|
60
|
+
// Flat keys must be unique across buckets (mirrors Bundle.parse): two bucket splits can flatten
|
|
61
|
+
// to one path, and hashes/attestation lookups key on the flat path.
|
|
62
62
|
for (const [dir, { files }] of modules) {
|
|
63
|
-
|
|
63
|
+
assertArtifactPath(dir, 'lockfile')
|
|
64
64
|
assert(files)
|
|
65
|
-
for (const name of Object.keys(files)) {
|
|
66
|
-
assert(!posixPathEscapes(name))
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
assert(isPlainObject(json.imports))
|
|
71
|
-
const imports = new Map()
|
|
72
|
-
for (const [conditions, byParent] of Object.entries(json.imports)) {
|
|
73
|
-
assert(isPlainObject(byParent))
|
|
74
|
-
const parents = new Map()
|
|
75
|
-
for (const [parent, specifiers] of Object.entries(byParent)) {
|
|
76
|
-
assert(!posixPathEscapes(parent))
|
|
77
|
-
assert(isPlainObject(specifiers))
|
|
78
|
-
const specs = new Map()
|
|
79
|
-
for (const [specifier, target] of Object.entries(specifiers)) {
|
|
80
|
-
if (typeof target === 'string') {
|
|
81
|
-
assert(!posixPathEscapes(target))
|
|
82
|
-
specs.set(specifier, target)
|
|
83
|
-
} else {
|
|
84
|
-
assert(isPlainObject(target) && Object.keys(target).length > 0,
|
|
85
|
-
'import target must be a file or a non-empty {platform: file} map')
|
|
86
|
-
const byPlatform = new Map()
|
|
87
|
-
for (const [platform, file] of Object.entries(target)) {
|
|
88
|
-
assert(typeof platform === 'string' && platform.length > 0 && !platform.includes('/'), `invalid platform key '${platform}'`)
|
|
89
|
-
assert(typeof file === 'string')
|
|
90
|
-
assert(!posixPathEscapes(file))
|
|
91
|
-
byPlatform.set(platform, file)
|
|
92
|
-
}
|
|
93
|
-
specs.set(specifier, byPlatform)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
parents.set(parent, specs)
|
|
97
|
-
}
|
|
98
|
-
imports.set(conditions, parents)
|
|
99
65
|
}
|
|
66
|
+
const flatKeys = flatFileKeys(modules, 'lockfile', duplicateKey)
|
|
100
67
|
|
|
101
|
-
|
|
102
|
-
const formats =
|
|
103
|
-
|
|
104
|
-
assert(!posixPathEscapes(file))
|
|
105
|
-
// Reject unknown formats at the schema boundary (not downstream) so a tampered lockfile fails closed.
|
|
106
|
-
assert(KNOWN_FORMATS.has(format), `unknown format '${format}' for ${file}`)
|
|
107
|
-
// '' and '.' alias to the same key (older lockfiles keyed the root listing ''); normalize, fail closed on dupes.
|
|
108
|
-
const key = file === '' ? '.' : file
|
|
109
|
-
assert(!formats.has(key), `duplicate format key '${key}'`)
|
|
110
|
-
formats.set(key, format)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Every executable must be a file this lockfile attests; the key index is built only when the (usually absent) key is present.
|
|
68
|
+
const imports = parseImports(json.imports)
|
|
69
|
+
const formats = parseFormats(json.formats)
|
|
70
|
+
// Every executable must be a file this lockfile attests.
|
|
114
71
|
const executable = json.executable === undefined
|
|
115
72
|
? new Set()
|
|
116
|
-
: parseExecutable(json.executable, { what: 'lockfile', files:
|
|
73
|
+
: parseExecutable(json.executable, { what: 'lockfile', files: flatKeys, formats, scope: json.config.scope })
|
|
117
74
|
|
|
118
75
|
return new Lockfile({ config: json.config, entries, modules, imports, formats, executable })
|
|
119
76
|
}
|
|
120
77
|
|
|
121
78
|
serialize() {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const sources =
|
|
125
|
-
for (const [dir, { name, version, ecosystem, files }] of this.modules) {
|
|
126
|
-
const inNodeModules = hasNodeModulesSegment(dir)
|
|
127
|
-
if (inNodeModules) assert(name && version && files)
|
|
128
|
-
const type = inNodeModules ? modules : sources
|
|
129
|
-
const sorted = fromEntries(Object.entries(files).toSorted((a, b) => sortPaths(a[0], b[0])))
|
|
130
|
-
type.push([dir, { name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files: sorted }])
|
|
131
|
-
}
|
|
132
|
-
modules.sort((a, b) => sortPaths(a[0], b[0]))
|
|
133
|
-
sources.sort((a, b) => sortPaths(a[0], b[0]))
|
|
134
|
-
|
|
79
|
+
// Never write an artifact that parse would reject.
|
|
80
|
+
flatFileKeys(this.modules, 'lockfile', duplicateKey)
|
|
81
|
+
const { modules, sources } = groupModules(this.modules, { what: 'lockfile' })
|
|
135
82
|
const store = { version: this.version, config: this.config }
|
|
136
|
-
if (this.config.scope === 'full') Object.assign(store, { entries
|
|
137
|
-
|
|
138
|
-
if (this.imports !== null)
|
|
139
|
-
if (this.formats !== null)
|
|
83
|
+
if (this.config.scope === 'full') Object.assign(store, { entries: serializeEntries(this.entries, 'lockfile'), sources })
|
|
84
|
+
store.modules = modules
|
|
85
|
+
if (this.imports !== null) store.imports = serializeImports(this.imports)
|
|
86
|
+
if (this.formats !== null) store.formats = serializeFormats(this.formats)
|
|
140
87
|
const executable = serializeExecutable(this.executable, {
|
|
141
88
|
what: 'lockfile',
|
|
142
89
|
modules: this.modules,
|
package/src/prune.js
CHANGED
|
@@ -1,47 +1,22 @@
|
|
|
1
1
|
import assert from 'node:assert/strict'
|
|
2
|
-
import {
|
|
3
|
-
|
|
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 { hasNodeModulesSegment, 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
11
|
// Fields copied (sanitised) from a dep's on-disk package.json; everything else is dropped as
|
|
21
12
|
// attack surface (scripts especially).
|
|
22
|
-
const ROOT_FIELD_ORDER = [
|
|
23
|
-
'license',
|
|
24
|
-
'type',
|
|
25
|
-
'main',
|
|
26
|
-
'module',
|
|
27
|
-
'jsnext:main',
|
|
28
|
-
'jsnext',
|
|
29
|
-
'source',
|
|
30
|
-
'browser',
|
|
31
|
-
'react-native',
|
|
32
|
-
'bin',
|
|
33
|
-
'sideEffects',
|
|
34
|
-
'exports',
|
|
35
|
-
'imports',
|
|
36
|
-
]
|
|
13
|
+
const ROOT_FIELD_ORDER = ['license', 'type', 'main', 'module', 'jsnext:main', 'jsnext', 'source', 'browser', 'react-native', 'bin', 'sideEffects', 'exports', 'imports']
|
|
37
14
|
|
|
38
15
|
function assertGlobalVirtualStoreDisabled() {
|
|
39
16
|
// Global virtual store fills node_modules with symlinks, which prune skips -- lockfile-vs-disk goes blind.
|
|
40
17
|
const env = process.env.npm_config_enable_global_virtual_store
|
|
41
18
|
if (env !== undefined && env !== 'false' && env !== '') {
|
|
42
|
-
throw new Error(
|
|
43
|
-
`stasis prune: enableGlobalVirtualStore must be false, got ${JSON.stringify(env)}`,
|
|
44
|
-
)
|
|
19
|
+
throw new Error(`stasis prune: enableGlobalVirtualStore must be false, got ${JSON.stringify(env)}`)
|
|
45
20
|
}
|
|
46
21
|
}
|
|
47
22
|
|
|
@@ -51,11 +26,13 @@ function loadLockfile(root) {
|
|
|
51
26
|
return Lockfile.parse(readFileSync(path, 'utf8'))
|
|
52
27
|
}
|
|
53
28
|
|
|
29
|
+
// The file -> hash the lockfile expects under node_modules (workspace sources aren't pnpm-managed),
|
|
30
|
+
// and the bucket dirs it knows.
|
|
54
31
|
function buildExpected(lockfile) {
|
|
55
32
|
const expected = new Map()
|
|
56
33
|
const knownDirs = new Set()
|
|
57
34
|
for (const [dir, { files }] of lockfile.modules) {
|
|
58
|
-
if (!hasNodeModulesSegment(dir)) continue
|
|
35
|
+
if (!hasNodeModulesSegment(dir)) continue
|
|
59
36
|
knownDirs.add(dir)
|
|
60
37
|
for (const [rel, hash] of Object.entries(files)) {
|
|
61
38
|
// moduleFileKey, not `${dir}/${rel}`: a root capture's rel === '' would demand a file at `<pkg>/`.
|
|
@@ -72,8 +49,7 @@ function buildExpected(lockfile) {
|
|
|
72
49
|
const EXTENSIONS = ['.js', '.cjs', '.mjs', '.json', '.node']
|
|
73
50
|
|
|
74
51
|
function normalizeRef(ref) {
|
|
75
|
-
if (typeof ref !== 'string' || ref === '') return null
|
|
76
|
-
if (posixPathEscapes(ref)) return null
|
|
52
|
+
if (typeof ref !== 'string' || ref === '' || posixPathEscapes(ref)) return null
|
|
77
53
|
return posix.normalize(ref).replace(/^\.\//u, '')
|
|
78
54
|
}
|
|
79
55
|
|
|
@@ -83,25 +59,22 @@ function referencesPresentFile(ref, present, { exact = false } = {}) {
|
|
|
83
59
|
if (base.includes('*')) return true // subpath pattern: unverifiable, kept
|
|
84
60
|
if (present.has(base)) return true
|
|
85
61
|
if (exact) return false // exports/imports targets resolve verbatim -- no extension/index fallback
|
|
86
|
-
|
|
87
|
-
for (const ext of EXTENSIONS) if (present.has(`${base}/index${ext}`)) return true
|
|
88
|
-
return false
|
|
62
|
+
return EXTENSIONS.some((ext) => present.has(base + ext) || present.has(`${base}/index${ext}`))
|
|
89
63
|
}
|
|
90
64
|
|
|
91
65
|
// Cap the recursion; real exports/imports trees nest a few deep and a deep one must not overflow.
|
|
92
66
|
const MAX_TARGET_DEPTH = 64
|
|
93
67
|
|
|
94
|
-
// An object/array filtering to empty is dropped:
|
|
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`.
|
|
95
70
|
function filterTargets(value, present, opts, depth = 0) {
|
|
96
71
|
if (depth > MAX_TARGET_DEPTH) return { keep: false }
|
|
97
72
|
const { allowBare, allowFalse, exact } = opts
|
|
98
73
|
if (value === null) return { keep: true, value: null }
|
|
99
74
|
if (value === false) return allowFalse ? { keep: true, value: false } : { keep: false }
|
|
100
75
|
if (typeof value === 'string') {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
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 }
|
|
105
78
|
}
|
|
106
79
|
if (Array.isArray(value)) {
|
|
107
80
|
const out = []
|
|
@@ -132,38 +105,28 @@ const isAscii = (s) => /^[\x20-\x7e]*$/u.test(s)
|
|
|
132
105
|
// `browser`/`react-native`: a string, or a map to replacements/`false`. undefined means drop the field.
|
|
133
106
|
function browserField(value, present) {
|
|
134
107
|
if (typeof value === 'string') return referencesPresentFile(value, present) ? value : undefined
|
|
135
|
-
if (isPlainObject(value))
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
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
|
|
140
111
|
}
|
|
141
112
|
|
|
142
113
|
// Resolution fields (`type`, entry points, exports/imports), every file reference filtered to an attested file.
|
|
143
114
|
function resolutionFields(pkg, present) {
|
|
144
115
|
const out = { __proto__: null }
|
|
145
|
-
|
|
146
116
|
// Only the values Node honours; a garbage `type` is dropped rather than recategorising the package's .js files.
|
|
147
117
|
if (pkg.type === 'module' || pkg.type === 'commonjs') out.type = pkg.type
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (referencesPresentFile(pkg.module, present)) out.module = pkg.module
|
|
151
|
-
|
|
152
|
-
// Bundler `mainFields` entries: dropping jsnext/jsnext:main downgrades an older dep ESM->CJS under Vite.
|
|
153
|
-
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']) {
|
|
154
120
|
if (referencesPresentFile(pkg[field], present)) out[field] = pkg[field]
|
|
155
121
|
}
|
|
156
|
-
|
|
157
122
|
for (const field of ['browser', 'react-native']) {
|
|
158
123
|
const v = browserField(pkg[field], present)
|
|
159
124
|
if (v !== undefined) out[field] = v
|
|
160
125
|
}
|
|
161
|
-
|
|
162
126
|
const exp = filterTargets(pkg.exports, present, { allowBare: false, allowFalse: false, exact: true })
|
|
163
127
|
if (exp.keep) out.exports = exp.value
|
|
164
128
|
const imp = filterTargets(pkg.imports, present, { allowBare: true, allowFalse: false, exact: true })
|
|
165
129
|
if (imp.keep) out.imports = imp.value
|
|
166
|
-
|
|
167
130
|
return out
|
|
168
131
|
}
|
|
169
132
|
|
|
@@ -202,18 +165,28 @@ function minimalRootFields(pkg, { name, version, present }) {
|
|
|
202
165
|
return out
|
|
203
166
|
}
|
|
204
167
|
|
|
205
|
-
//
|
|
206
|
-
|
|
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))
|
|
207
184
|
return resolutionFields(pkg, present)
|
|
208
185
|
}
|
|
209
186
|
|
|
210
|
-
const PNPM_WORKSPACE = 'pnpm-workspace.yaml'
|
|
211
|
-
|
|
212
187
|
// In a workspace the symlink-containment boundary widens from node_modules to the whole workspace,
|
|
213
188
|
// since a dep legitimately links to a sibling package's source dir.
|
|
214
|
-
|
|
215
|
-
return existsSync(join(root, PNPM_WORKSPACE))
|
|
216
|
-
}
|
|
189
|
+
const isPnpmWorkspaceRoot = (root) => existsSync(join(root, 'pnpm-workspace.yaml'))
|
|
217
190
|
|
|
218
191
|
function discoverNodeModulesDirs(root) {
|
|
219
192
|
const found = []
|
|
@@ -250,32 +223,26 @@ function assertSymlinkInternal({ root, realBoundary, label }, linkPath) {
|
|
|
250
223
|
if (err.code === 'ENOENT') return
|
|
251
224
|
throw err
|
|
252
225
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
throw new Error(`stasis prune: symlink escapes ${label}: ${relative(root, linkPath)} -> ${real}`)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function* walkFiles(nmRoot, ctx) {
|
|
259
|
-
if (!existsSync(nmRoot)) return
|
|
260
|
-
const stack = [nmRoot]
|
|
261
|
-
while (stack.length > 0) {
|
|
262
|
-
const dir = stack.pop()
|
|
263
|
-
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
264
|
-
const full = join(dir, entry.name)
|
|
265
|
-
if (entry.isSymbolicLink()) {
|
|
266
|
-
assertSymlinkInternal(ctx, full)
|
|
267
|
-
continue
|
|
268
|
-
}
|
|
269
|
-
if (entry.isDirectory()) stack.push(full)
|
|
270
|
-
else if (entry.isFile()) yield full
|
|
271
|
-
}
|
|
226
|
+
if (!isPathWithin(realBoundary, real)) {
|
|
227
|
+
throw new Error(`stasis prune: symlink escapes ${label}: ${relative(root, linkPath)} -> ${real}`)
|
|
272
228
|
}
|
|
273
229
|
}
|
|
274
230
|
|
|
275
|
-
//
|
|
276
|
-
|
|
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) {
|
|
277
234
|
for (const nmRoot of nmRoots) {
|
|
278
|
-
|
|
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
|
+
}
|
|
279
246
|
}
|
|
280
247
|
}
|
|
281
248
|
|
|
@@ -319,7 +286,7 @@ export function prune({ root = process.cwd() } = {}) {
|
|
|
319
286
|
const seen = new Set()
|
|
320
287
|
const mismatches = []
|
|
321
288
|
|
|
322
|
-
for (const { full, nmRoot } of
|
|
289
|
+
for (const { full, nmRoot } of walkFiles(nmRoots, ctx)) {
|
|
323
290
|
const rel = relative(root, full)
|
|
324
291
|
const expectedHash = expected.get(rel)
|
|
325
292
|
if (expectedHash !== undefined) {
|
|
@@ -335,53 +302,21 @@ export function prune({ root = process.cwd() } = {}) {
|
|
|
335
302
|
}
|
|
336
303
|
|
|
337
304
|
// package.json isn't lockfile-enumerated but drives resolution, so rewrite (not delete) it for a known module.
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
} catch {
|
|
346
|
-
parsed = null
|
|
347
|
-
}
|
|
348
|
-
const pkg = isPlainObject(parsed) ? parsed : {}
|
|
349
|
-
let next
|
|
350
|
-
if (dirname(rel) === owner.dir) {
|
|
351
|
-
const { name, version, files } = lockfile.modules.get(owner.dir)
|
|
352
|
-
// The package.json itself survives prune, so it's always a legal self-reference target.
|
|
353
|
-
const present = new Set([...Object.keys(files), 'package.json'])
|
|
354
|
-
next = minimalRootFields(pkg, { name, version, present })
|
|
355
|
-
} else {
|
|
356
|
-
// A nested package.json's refs are relative to its own dir, so re-base the recorded files first.
|
|
357
|
-
const { files } = lockfile.modules.get(owner.dir)
|
|
358
|
-
const prefix = `${relative(owner.dir, dirname(rel))}/`
|
|
359
|
-
const present = new Set(['package.json'])
|
|
360
|
-
for (const f of Object.keys(files)) {
|
|
361
|
-
if (f.startsWith(prefix)) present.add(f.slice(prefix.length))
|
|
362
|
-
}
|
|
363
|
-
next = minimalNestedFields(pkg, present)
|
|
364
|
-
}
|
|
365
|
-
const newText = `${JSON.stringify(next, undefined, 2)}\n`
|
|
366
|
-
kept.push(rel)
|
|
367
|
-
if (newText !== raw) toMinimize.push({ full, rel, text: newText })
|
|
368
|
-
continue
|
|
369
|
-
}
|
|
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
|
|
370
312
|
}
|
|
371
313
|
|
|
372
314
|
// Defense-in-depth: the walk should already guarantee this.
|
|
373
|
-
assert.ok(
|
|
374
|
-
full.startsWith(`${nmRoot}${sep}`),
|
|
375
|
-
`refusing to remove path outside node_modules: ${full}`,
|
|
376
|
-
)
|
|
315
|
+
assert.ok(full.startsWith(`${nmRoot}${sep}`), `refusing to remove path outside node_modules: ${full}`)
|
|
377
316
|
toRemove.push({ full, nmRoot })
|
|
378
317
|
}
|
|
379
318
|
|
|
380
|
-
const missing = []
|
|
381
|
-
for (const [rel] of expected) {
|
|
382
|
-
if (!seen.has(rel)) missing.push(rel)
|
|
383
|
-
}
|
|
384
|
-
|
|
319
|
+
const missing = [...expected.keys()].filter((rel) => !seen.has(rel))
|
|
385
320
|
if (mismatches.length > 0) {
|
|
386
321
|
const lines = mismatches.map(({ rel, expected: exp, actual }) =>
|
|
387
322
|
` ${rel}: expected ${exp}, got ${actual}`).join('\n')
|
|
@@ -409,9 +344,7 @@ export function prune({ root = process.cwd() } = {}) {
|
|
|
409
344
|
removed.push(relative(root, full))
|
|
410
345
|
touchedDirs.set(dirname(full), nmRoot)
|
|
411
346
|
}
|
|
412
|
-
|
|
413
|
-
const sortedDirs = [...touchedDirs.keys()].toSorted((a, b) => b.length - a.length)
|
|
414
|
-
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))
|
|
415
348
|
|
|
416
349
|
return { removed, validated, kept, minimized }
|
|
417
350
|
}
|
package/src/shard.js
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
// resolution edges; content, listings, stat kinds and exec bits are re-derived from the root's OWN disk, so
|
|
3
3
|
// a shard cannot inject content or a forged bit. `files` and `formats` are independent -- don't collapse them.
|
|
4
4
|
|
|
5
|
-
import { KNOWN_FORMATS, assert, fileMapToObject, isPlainObject,
|
|
5
|
+
import { KNOWN_FORMATS, assert, fileMapToObject, isPlainObject, isRefusedPath, objectToMaps } from './artifact-util.js'
|
|
6
6
|
|
|
7
7
|
export const SHARD_VERSION = 1
|
|
8
8
|
|
|
9
9
|
// `imports` nests (conditions -> parent -> specifier -> file, or -> platform -> file under --metro),
|
|
10
10
|
// so the conversion recurses rather than flattening one level.
|
|
11
11
|
export function serializeShard({ scope, files, formats, imports }) {
|
|
12
|
+
// Never send a shard the root's parseShard would reject.
|
|
13
|
+
assertShard(files, formats, imports)
|
|
12
14
|
return JSON.stringify({
|
|
13
15
|
version: SHARD_VERSION,
|
|
14
16
|
scope,
|
|
@@ -28,13 +30,20 @@ export function parseShard(text) {
|
|
|
28
30
|
assert(Array.isArray(json.files), 'shard files must be an array')
|
|
29
31
|
assert(isPlainObject(json.formats) && isPlainObject(json.imports), 'shard formats/imports must be objects')
|
|
30
32
|
|
|
31
|
-
for (const file of json.files) assertKey(file, 'shard file')
|
|
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')
|
|
33
43
|
for (const [file, format] of formats) {
|
|
34
44
|
assertKey(file, 'shard format')
|
|
35
45
|
assert(KNOWN_FORMATS.has(format), `shard format for ${file}: ${format}`)
|
|
36
46
|
}
|
|
37
|
-
const imports = objectToMaps(json.imports)
|
|
38
47
|
for (const [, byParent] of imports) {
|
|
39
48
|
assert(byParent instanceof Map, 'shard imports: expected a parent map')
|
|
40
49
|
for (const [parent, specs] of byParent) {
|
|
@@ -43,11 +52,10 @@ export function parseShard(text) {
|
|
|
43
52
|
for (const [, target] of specs) assertTarget(target, parent)
|
|
44
53
|
}
|
|
45
54
|
}
|
|
46
|
-
return { scope: json.scope, files: json.files, formats, imports }
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
function assertKey(key, what) {
|
|
50
|
-
assert(typeof key === 'string' && key !== '' && !
|
|
58
|
+
assert(typeof key === 'string' && key !== '' && !isRefusedPath(key), `${what}: ${key}`)
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
// A target is a file, or (--metro) a platform -> file map; nothing deeper.
|
package/src/state-util.js
CHANGED
|
@@ -3,24 +3,28 @@ import { hash } from 'node:crypto'
|
|
|
3
3
|
import * as fs from 'node:fs'
|
|
4
4
|
import { join, resolve, sep } from 'node:path'
|
|
5
5
|
|
|
6
|
+
import { diskHost } from './host.js'
|
|
7
|
+
|
|
6
8
|
// Genuine fs readers snapshotted off the namespace before any --fs patch. A plain `import { readFileSync }`
|
|
7
9
|
// won't do: --fs monkey-patches fs then syncBuiltinESMExports() rebinds named imports, so snapshotting dodges that.
|
|
8
10
|
export const realReadFileSync = fs.readFileSync
|
|
9
11
|
export const realReadFile = fs.promises.readFile
|
|
10
12
|
export const realReaddirSync = fs.readdirSync
|
|
11
|
-
const
|
|
13
|
+
export const realExistsSync = fs.existsSync
|
|
12
14
|
|
|
13
15
|
assert.equal(sep, '/', 'Not tested on Windows')
|
|
14
16
|
|
|
15
17
|
export const sha512integrity = (x) => `sha512-${hash('sha512', x, 'base64')}`
|
|
16
18
|
|
|
17
|
-
export function readFileSyncMaybe(dir, file, encoding) {
|
|
19
|
+
export function readFileSyncMaybe(dir, file, encoding, host = diskHost) {
|
|
20
|
+
let buf
|
|
18
21
|
try {
|
|
19
|
-
|
|
22
|
+
buf = host.readFile(join(dir, file))
|
|
20
23
|
} catch (err) {
|
|
21
24
|
if (err.code === 'ENOENT') return null
|
|
22
25
|
throw err
|
|
23
26
|
}
|
|
27
|
+
return encoding === undefined ? buf : buf.toString(encoding)
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
export function noupsert(map, key, value) {
|
|
@@ -31,11 +35,11 @@ export function noupsert(map, key, value) {
|
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
37
|
|
|
34
|
-
// Canonicalize for write-target collision detection:
|
|
35
|
-
export function canonicalizePath(p) {
|
|
38
|
+
// Canonicalize for write-target collision detection: the host's realpath catches symlink aliases resolve() would miss; lexical fallback for not-yet-written targets.
|
|
39
|
+
export function canonicalizePath(p, host = diskHost) {
|
|
36
40
|
const abs = resolve(p)
|
|
37
41
|
try {
|
|
38
|
-
return
|
|
42
|
+
return host.realpath(abs)
|
|
39
43
|
} catch {
|
|
40
44
|
return abs
|
|
41
45
|
}
|