@exodus/stasis-core 1.0.0-beta.8 → 1.0.0-rc.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/bin/stasis-core.js +28 -109
- package/package.json +6 -2
- package/src/add.js +161 -157
- package/src/artifact-util.js +322 -217
- package/src/brotli.js +20 -6
- package/src/bundle-util.js +148 -98
- package/src/bundle.js +65 -128
- package/src/cli.js +123 -0
- package/src/config.js +74 -87
- package/src/extract.js +140 -67
- package/src/fs.js +57 -106
- package/src/hooks.js +126 -238
- package/src/host.js +9 -1
- package/src/loader.js +1 -1
- package/src/lockfile.js +58 -74
- package/src/policy.js +222 -0
- package/src/prune.js +60 -114
- package/src/shard.js +10 -25
- package/src/state-util.js +24 -9
- package/src/state.js +737 -862
- package/src/util.js +123 -172
package/src/state.js
CHANGED
|
@@ -3,71 +3,65 @@ import { isUtf8 } from 'node:buffer'
|
|
|
3
3
|
import * as fs from 'node:fs'
|
|
4
4
|
import { join, resolve, relative, basename, dirname, extname, isAbsolute } from 'node:path'
|
|
5
5
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
6
|
-
import { brotliCompressSync, brotliDecompressSync } from 'node:zlib'
|
|
7
6
|
|
|
8
7
|
import { Config } from './config.js'
|
|
9
8
|
import { Bundle, mergeRepo } from './bundle.js'
|
|
10
9
|
import { Lockfile } from './lockfile.js'
|
|
11
10
|
import { parseShard, serializeShard } from './shard.js'
|
|
12
|
-
import { canonicalizePath, sha512integrity, readFileSyncMaybe, noupsert } from './state-util.js'
|
|
13
|
-
import {
|
|
14
|
-
import { CODE_EXTENSIONS, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat,
|
|
15
|
-
import { detectRepo, packageJSONStat, packageJSONText, packageRepo, readModuleManifest } from './bundle-util.js'
|
|
11
|
+
import { bundledIntegrity, canonicalizePath, inScope, sha512integrity, readFileSyncMaybe, noupsert, realContained } from './state-util.js'
|
|
12
|
+
import { compressBundleText, parseBundleBytes } from './brotli.js'
|
|
13
|
+
import { ATTESTED_METADATA, attestsAny, BUNDLE_FILE, CODE_EXTENSIONS, CONFIG_FILE, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, hasNodeModulesSegment, heldToAccounts, importTargetsEqual, installedAccounts, LOCK_FILE, memoized, mergeModuleMaps, inResources, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, isPlainObject, isStatFormat, isSubpackage, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, nodeExtFormat, observeExecutable, relativeEscapes, sortedByPath, splitNodeModulesPath, toPosix, upsert, upsertFormat, withMetadataOf } from './util.js'
|
|
14
|
+
import { detectRepo, invalidPackageConfig, namedManifestOf, packageJSONStat, packageJSONText, packageRepo, readModuleManifest, scopeManifestOf, vendorDirOf, vendoredPackageOf } from './bundle-util.js'
|
|
15
|
+
import { expandExtra, validationError } from './add.js'
|
|
16
|
+
import { linksOn } from './policy.js'
|
|
16
17
|
import { diskHost } from './host.js'
|
|
17
18
|
import corePackage from './package.cjs'
|
|
18
19
|
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
const { existsSync, mkdirSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } = fs
|
|
20
|
+
// Snapshotted, so writes survive --mock's syncBuiltinESMExports() remock of node:fs.
|
|
21
|
+
const { mkdirSync, rmSync, writeFileSync } = fs
|
|
22
22
|
|
|
23
|
-
const FILE_CONFIG = 'stasis.config.json'
|
|
24
|
-
const FILE_LOCK = 'stasis.lock.json'
|
|
25
|
-
const FILE_CODE = 'stasis.code.br'
|
|
26
|
-
|
|
27
|
-
// Via src/package.cjs: a bundler-safe re-export of package.json.
|
|
28
23
|
const VERSION = corePackage.version
|
|
29
|
-
|
|
30
24
|
const CAN_OBSERVE_EXECUTE_BITS = canObserveExecuteBits()
|
|
25
|
+
const REBUNDLE = 'Re-bundle with the current stasis or `bundle=replace` against a v0-free starting point to upgrade.'
|
|
31
26
|
|
|
32
|
-
const invalidPackageConfig = (pkgAbsolute, cause) => Object.assign(
|
|
33
|
-
cause === undefined ? new Error(`Invalid package config ${pkgAbsolute}.`) : new Error(`Invalid package config ${pkgAbsolute}.`, { cause }),
|
|
34
|
-
{ code: 'ERR_INVALID_PACKAGE_CONFIG' })
|
|
35
|
-
|
|
36
|
-
// A package.json's fields; one that is no JSON object is refused as Node refuses it.
|
|
37
27
|
function readPackageJSON(host, pkgAbsolute) {
|
|
38
28
|
const buf = host.readFile(pkgAbsolute)
|
|
39
29
|
assert.ok(isUtf8(buf))
|
|
40
30
|
let json
|
|
41
|
-
try {
|
|
42
|
-
|
|
43
|
-
} catch (cause) {
|
|
44
|
-
throw invalidPackageConfig(pkgAbsolute, cause)
|
|
45
|
-
}
|
|
46
|
-
if (json === null || typeof json !== 'object' || Array.isArray(json)) throw invalidPackageConfig(pkgAbsolute)
|
|
31
|
+
try { json = JSON.parse(packageJSONText(buf)) } catch (cause) { throw invalidPackageConfig(pkgAbsolute, cause) }
|
|
32
|
+
if (!isPlainObject(json)) throw invalidPackageConfig(pkgAbsolute)
|
|
47
33
|
return json
|
|
48
34
|
}
|
|
49
35
|
|
|
50
|
-
|
|
51
|
-
function upsertFormat(map, file, format) {
|
|
52
|
-
const currentFormat = map.get(file)
|
|
53
|
-
map.set(file, currentFormat === undefined ? format : reconcileFormat(format, currentFormat, file))
|
|
54
|
-
}
|
|
36
|
+
const cloneMaps = (map) => new Map(Array.from(map, ([key, value]) => [key, value instanceof Map ? cloneMaps(value) : value]))
|
|
55
37
|
|
|
56
|
-
// Loader formats the extension alone decides; `.js`/`.ts` follow the nearest package `type`.
|
|
57
|
-
const EXT_FORMATS = { __proto__: null, '.json': 'json', '.mjs': 'module', '.cjs': 'commonjs', '.mts': 'module-typescript', '.cts': 'commonjs-typescript' }
|
|
58
38
|
// With no `type`, Node decides .js/.ts by syntax detection: either variant is possible, the first is the default.
|
|
59
39
|
const UNTYPED_VARIANTS = { __proto__: null, '.js': ['commonjs', 'module'], '.ts': ['commonjs-typescript', 'module-typescript'] }
|
|
60
40
|
|
|
61
|
-
// Nested package.json files
|
|
62
|
-
// upstream packaging, exempt from #locateModule's consistency check. Matched as a segment-aligned
|
|
63
|
-
// suffix of the project-relative path, so hoisted, nested and .pnpm-store copies all match.
|
|
64
|
-
// Maintained list: add only a confirmed upstream false positive, with its reason.
|
|
41
|
+
// Nested package.json files upstream leaves disagreeing with their root's. Add only a confirmed false positive.
|
|
65
42
|
const INCONSISTENT_PACKAGE_JSON_EXCEPTIONS = [
|
|
66
43
|
'node_modules/@redis/client/dist/package.json', // stale build-time copy
|
|
44
|
+
'node_modules/hashids/esm/package.json', // a `type` marker named `hashids-esm`, in every 2.x release
|
|
67
45
|
]
|
|
68
46
|
const isInconsistentPackageJsonException = (file) =>
|
|
69
47
|
INCONSISTENT_PACKAGE_JSON_EXCEPTIONS.some((suffix) => file === suffix || file.endsWith(`/${suffix}`))
|
|
70
48
|
|
|
49
|
+
// 'import' or 'require', as a condition list or #conditionsKey asserts; null for '*' or a set naming both or neither.
|
|
50
|
+
function resolutionContext(conditions) {
|
|
51
|
+
const list = typeof conditions === 'string' ? conditions.split(' (with: ')[0].split(', ') : conditions
|
|
52
|
+
const asImport = list.includes('import')
|
|
53
|
+
if (asImport === list.includes('require')) return null
|
|
54
|
+
return asImport ? 'import' : 'require'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A bare specifier reads `exports` conditions (its own name's included), a `#` one `imports`'.
|
|
58
|
+
function readsConditions(specifier) {
|
|
59
|
+
if (typeof specifier !== 'string') return false
|
|
60
|
+
if (specifier.startsWith('#')) return true
|
|
61
|
+
return !(specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../') ||
|
|
62
|
+
specifier.startsWith('/') || /^[a-z][a-z\d+.-]*:/i.test(specifier))
|
|
63
|
+
}
|
|
64
|
+
|
|
71
65
|
// On globalThis via `Symbol.for` so the registry is shared across duplicate stasis-core copies.
|
|
72
66
|
const STATES_KEY = Symbol.for('@exodus/stasis-core/states')
|
|
73
67
|
const liveStates = () => (globalThis[STATES_KEY] ??= new Set())
|
|
@@ -80,95 +74,59 @@ export class State {
|
|
|
80
74
|
formats = new Map()
|
|
81
75
|
modules = new Map()
|
|
82
76
|
imports = new Map()
|
|
83
|
-
//
|
|
84
|
-
// the bit drops out). Emitted as the artifacts' `executable` list.
|
|
77
|
+
// Disk is authoritative: a file that lost its execute bit drops out.
|
|
85
78
|
executable = new Set()
|
|
86
79
|
config
|
|
87
80
|
root
|
|
88
81
|
#parent
|
|
82
|
+
// Shared with sidecars: every record's `dirs`, the `files` of each absorbed with ATTESTED_METADATA, and the
|
|
83
|
+
// `bundles`' own record of each package they attest something of, which the run's record may not take.
|
|
84
|
+
#absorbed = { dirs: new Set(), files: new Map(), bundles: new Map() }
|
|
85
|
+
#attestedByBundle = new Set()
|
|
89
86
|
#isPreload = false
|
|
90
87
|
#host
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
// State built with `claim: false`, which never writes (a static build), claims none.
|
|
88
|
+
#policy = null
|
|
89
|
+
#linksOf = null
|
|
94
90
|
#claim
|
|
95
91
|
#claims = new Map()
|
|
96
|
-
|
|
97
|
-
// WRITE-mode sidecars: lockfile CONTRIBUTORS (#mergedImports/#mergedFormats union theirs in).
|
|
92
|
+
// Write-mode sidecars contribute to the lockfile; load-mode ones are only served from (getFs*Family).
|
|
98
93
|
#sidecars = new Set()
|
|
99
|
-
// LOAD-mode sidecars: read-only, NOT lockfile contributors (getFs*Family serves bytes from them).
|
|
100
94
|
#readSidecars = new Set()
|
|
101
|
-
|
|
102
|
-
// Lockfile-attested resolutions (conditions -> parent -> specifier -> file) and loader formats
|
|
103
|
-
// (file -> format), or null; may be supersets of what a run observes, so kept apart from the live maps.
|
|
95
|
+
// May be supersets of what a run observes, so kept apart from the live maps.
|
|
104
96
|
#lockImports = null
|
|
105
97
|
#lockFormats = null
|
|
106
|
-
// Loaded a lockfile at construction: #mergeBundleMetadata cross-checks bundle metadata against
|
|
107
|
-
// it instead of absorbing it as source of truth.
|
|
108
98
|
#lockfileLoaded = false
|
|
109
|
-
|
|
110
|
-
// Keys THIS process recorded this run (not the seeded baseline); shardSnapshot forwards only these.
|
|
99
|
+
// What THIS process recorded, not the seeded baseline: shardSnapshot forwards only these.
|
|
111
100
|
#observed = new Set()
|
|
112
|
-
// file -> consumers that recorded it, for the bundle's informational `reason` field.
|
|
113
101
|
#reasonFiles = new Map()
|
|
114
|
-
|
|
102
|
+
// 'run' files it loaded itself, and ones it only fs-read after a plugin attached (#reasonClaims).
|
|
103
|
+
#runOwn = new Set()
|
|
115
104
|
#fsReadPostPlugin = new Set()
|
|
116
105
|
#pluginAttached = false
|
|
117
|
-
|
|
118
|
-
// Attested by a frozen bundle (bundle=frozen), or null: closes the attested set, so anything the
|
|
119
|
-
// bundle never recorded is rejected. Separate from the live maps.
|
|
106
|
+
// A frozen bundle's attestation, apart from the live maps: anything it never recorded is rejected.
|
|
120
107
|
#bundleSources = null
|
|
121
108
|
#bundleResources = null
|
|
122
109
|
#bundleImports = null
|
|
123
110
|
#bundleFormats = null
|
|
124
|
-
|
|
125
|
-
// Lazily-built dirs IMPLIED by recorded files, so getFsStat answers isDirectory() at bundle=load.
|
|
126
111
|
#impliedDirIndex = null
|
|
127
|
-
|
|
128
|
-
// Native resolutions (parent URL -> specifier -> resolved URL) from the Module._resolveFilename
|
|
129
|
-
// shim: require.resolve()/CJS require()s bypassing the resolve hook.
|
|
130
112
|
#observedResolutions = new Map()
|
|
131
|
-
|
|
132
|
-
// Files a require() actually EXECUTED (from the Module._load shim) -- unlike a resolve-only
|
|
133
|
-
// edge, an executed file with no captured bytes is a hole in the attestation, so write()
|
|
134
|
-
// backfills it from disk (a transplanted require.extensions handler, e.g. a transforming
|
|
135
|
-
// preload's CJS TypeScript pipeline, loads files without ever hitting the load hook).
|
|
136
113
|
#executedFiles = new Set()
|
|
137
|
-
|
|
138
|
-
// Files whose BYTES this run captured via addFile. Distinct from this.hashes, which also
|
|
139
|
-
// absorbs lockfile-seeded entries: #backfillExecutedFiles must key on "observed this run", or
|
|
140
|
-
// a frozen/add run would skip verifying an executed file merely because a hash is on record.
|
|
114
|
+
// Unlike this.hashes, never lockfile-seeded: a seeded hash must not exempt an executed file from verification.
|
|
141
115
|
#filesCapturedThisRun = new Set()
|
|
142
|
-
|
|
143
|
-
// Node's require-condition set, from the first require()-context addImport;
|
|
144
|
-
// #backfillObservedResolutions keys native edges under it so the lockfile is Node-version-stable.
|
|
116
|
+
// Keys backfilled native edges, so the lockfile is Node-version-stable.
|
|
145
117
|
#requireConditions = null
|
|
146
|
-
|
|
147
|
-
// Root of a package Node loaded BEFORE our hooks (stasis-core's own); write()'s backfill
|
|
148
|
-
// statically parses files under it to recover edges the resolve hook never observed.
|
|
149
118
|
#preloadRoot = null
|
|
150
|
-
|
|
151
|
-
// Per-artifact "last serialized" caches: write() skips brotli + writeFileSync on unchanged text.
|
|
152
119
|
#lastLockData = null
|
|
153
|
-
#
|
|
154
|
-
#lastCodeBundle = null
|
|
155
|
-
#lastResourcesBundle = null
|
|
156
|
-
// Lazy `{ value: detectRepo(this.root, this.#host) }`.
|
|
120
|
+
#lastBundleText = new Map()
|
|
157
121
|
#detectedRepoCache
|
|
158
|
-
// Half ('code' | 'resources') -> `repo` of the bundle absorbed there for adding to.
|
|
159
122
|
#absorbedRepos = new Map()
|
|
160
|
-
|
|
161
|
-
// Memoized #canonicalUncached (per URL, State-lifetime): a realpathSync per node_modules URL, and
|
|
162
|
-
// hot callers repeat relentlessly. Sound only because the tree holds still mid-run; the cached
|
|
163
|
-
// result is shared -- never mutate it.
|
|
123
|
+
// Sound only because the tree holds still mid-run; the cached result is shared: never mutate it.
|
|
164
124
|
#canonicalCache = new Map()
|
|
165
125
|
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
// `skipDiscovery` (a caller-populated in-memory State, NO filesystem discovery; read-only modes
|
|
169
|
-
// only). All other keys forward to Config.
|
|
126
|
+
// `preload`: the unique preload State. `parent`: a sidecar sharing the parent's hashes/entries/modules.
|
|
127
|
+
// `skipDiscovery`: caller-populated, read-only, NO filesystem discovery. The rest go to Config.
|
|
170
128
|
constructor(root, options = {}) {
|
|
171
|
-
const { preload: isPreload = false, parent: parentState, preloadRoot, skipDiscovery = false, host = diskHost, env = process.env, claim = true, ...configOptions } = options
|
|
129
|
+
const { preload: isPreload = false, parent: parentState, preloadRoot, skipDiscovery = false, host = diskHost, env = process.env, claim = true, checkPolicy = true, ...configOptions } = options
|
|
172
130
|
this.config = new Config({ ...configOptions, env, host })
|
|
173
131
|
this.#host = host
|
|
174
132
|
this.#claim = claim
|
|
@@ -177,73 +135,58 @@ export class State {
|
|
|
177
135
|
this.#isPreload = isPreload
|
|
178
136
|
|
|
179
137
|
if (parentState) {
|
|
180
|
-
// A sidecar shares the parent's maps and
|
|
181
|
-
|
|
182
|
-
assert.equal(parentState.version, VERSION,
|
|
183
|
-
`child State version '${VERSION}' must exactly match parent version '${parentState.version}'`)
|
|
138
|
+
// A sidecar shares the parent's maps and attestation: a version skew is unsound (another copy is fine).
|
|
139
|
+
assert.equal(parentState.version, VERSION, `child State version '${VERSION}' must exactly match parent version '${parentState.version}'`)
|
|
184
140
|
this.#parent = parentState
|
|
185
141
|
this.root = parentState.root
|
|
186
142
|
this.hashes = parentState.hashes
|
|
187
143
|
this.entries = parentState.entries
|
|
188
144
|
this.modules = parentState.modules
|
|
189
|
-
//
|
|
190
|
-
// sidecar, else a tampered bundle could redirect a resolution on hash-valid bytes.
|
|
145
|
+
// The lockfile attestation must reach the sidecar, else a tampered bundle could redirect hash-valid bytes.
|
|
191
146
|
const inherited = parentState.sidecarInheritance()
|
|
192
147
|
this.#preloadRoot = inherited.preloadRoot
|
|
193
148
|
this.#lockImports = inherited.lockImports
|
|
194
149
|
this.#lockFormats = inherited.lockFormats
|
|
195
150
|
this.#lockfileLoaded = inherited.lockfileLoaded
|
|
151
|
+
this.#policy = inherited.policy
|
|
152
|
+
this.#absorbed = inherited.absorbed
|
|
196
153
|
assert.ok(this.config.bundleFile, 'sidecar State requires bundleFile')
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
this.#claimWritePath('bundleFile', 'sidecar ')
|
|
200
|
-
this.#claimWritePath('resourcesBundleFile', 'sidecar ')
|
|
201
|
-
}
|
|
202
|
-
// frozenBundle MUST go through this branch too -- it has its own attestation snapshot to build.
|
|
154
|
+
if (this.config.writeBundle) for (const key of ['bundleFile', 'resourcesBundleFile']) this.#claimWritePath(key, 'sidecar ')
|
|
155
|
+
// frozenBundle too: it has its own attestation snapshot to build. What it absorbs reaches the parent.
|
|
203
156
|
if (this.config.bundle) {
|
|
204
|
-
const
|
|
205
|
-
const sources = this.#readMaybe(
|
|
206
|
-
if (sources && !this.config.replaceBundle)
|
|
207
|
-
|
|
208
|
-
// v0 bundle has no per-file formats or import map -- serving one via a sidecar widens trust.
|
|
209
|
-
assert.equal(bundle.version, Bundle.VERSION,
|
|
210
|
-
`stasis sidecar requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
|
|
211
|
-
`Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point.`)
|
|
212
|
-
assert.equal(bundle.config.scope, this.config.scope)
|
|
213
|
-
// Cross-checked against the parent's lockfile, or absorbed if none (shared hashes/entries/
|
|
214
|
-
// modules mean those writes propagate to the parent).
|
|
215
|
-
this.#absorbCodeBundle(bundle, this.#lockfileLoaded)
|
|
216
|
-
}
|
|
217
|
-
this.#absorbResourcesBundleFile(this.#lockfileLoaded)
|
|
218
|
-
}
|
|
219
|
-
if (this.config.frozenBundle) {
|
|
220
|
-
assert.ok(this.#bundleSources !== null, 'No bundle, but attempting to run in frozen bundle mode')
|
|
157
|
+
const path = this.config.bundleFile
|
|
158
|
+
const sources = this.#readMaybe(path)
|
|
159
|
+
if (sources && !this.config.replaceBundle) this.#absorbCodeBundle(this.#parseServedBundle(sources, path, 'stasis sidecar'))
|
|
160
|
+
this.#absorbResourcesBundleFile()
|
|
221
161
|
}
|
|
222
|
-
|
|
223
|
-
|
|
162
|
+
if (this.config.frozenBundle) this.#freezeBundle()
|
|
163
|
+
if (this.#attestedByBundle.size > 0) this.#holdToInstalled(relative(this.root, this.#bundlePath))
|
|
164
|
+
// AFTER every fallible step, so a throw leaves no dead reference. A frozen sidecar joins neither registry.
|
|
224
165
|
if (this.config.writeBundle) parentState.registerSidecar(this)
|
|
225
166
|
else if (this.config.loadBundle) parentState.registerReadSidecar(this)
|
|
226
167
|
if (this.#claims.size > 0) liveStates().add(this)
|
|
227
168
|
return
|
|
228
169
|
}
|
|
229
170
|
|
|
171
|
+
if (preloadRoot !== undefined) {
|
|
172
|
+
assert.equal(typeof preloadRoot, 'string', 'preloadRoot must be a string')
|
|
173
|
+
this.#preloadRoot = preloadRoot
|
|
174
|
+
}
|
|
230
175
|
if (skipDiscovery) {
|
|
231
176
|
assert.ok(!this.config.frozen && !this.config.frozenBundle && !this.config.writeBundle && !this.config.writeLockfile,
|
|
232
177
|
'skipDiscovery is read-only: incompatible with frozen and write (add/replace) lock/bundle modes')
|
|
233
178
|
this.root = resolve(root)
|
|
234
|
-
this.#setPreloadRoot(preloadRoot)
|
|
235
179
|
if (isPreload) liveStates().add(this)
|
|
236
180
|
return
|
|
237
181
|
}
|
|
238
182
|
|
|
239
|
-
// Capture and load MUST agree on where the upward walk stops
|
|
240
|
-
// pnpm-workspace.yaml), else they commit different roots and fail "outside the project root".
|
|
183
|
+
// Capture and load MUST agree on where the upward walk stops, else they commit different roots.
|
|
241
184
|
const potentialRoots = []
|
|
242
185
|
for (let cursor = root; cursor; cursor = dirname(cursor)) {
|
|
243
|
-
// One there that can't be read is refused, not walked past
|
|
186
|
+
// One there that can't be read is refused, not walked past (packageJSONStat).
|
|
244
187
|
if (packageJSONStat(this.#host, join(cursor, 'package.json')) !== null) {
|
|
245
188
|
potentialRoots.push(cursor)
|
|
246
|
-
} else if ([
|
|
189
|
+
} else if ([CONFIG_FILE, LOCK_FILE, BUNDLE_FILE].some((file) => this.#exists(join(cursor, file)))) {
|
|
247
190
|
throw new Error('Unexpected stasis config without package.json')
|
|
248
191
|
}
|
|
249
192
|
if (cursor === env.PROJECT_CWD) break // e.g. yarn sets this
|
|
@@ -251,75 +194,56 @@ export class State {
|
|
|
251
194
|
if (dirname(cursor) === cursor) break
|
|
252
195
|
}
|
|
253
196
|
|
|
254
|
-
//
|
|
197
|
+
// The top-level package.json by default; a stasis.config.json opts a dir in.
|
|
255
198
|
this.root = potentialRoots.at(-1)
|
|
256
199
|
assert.ok(this.root, `stasis: no package.json found at or above ${resolve(root)}; run stasis from within a project`)
|
|
257
200
|
|
|
258
201
|
let loaded = false
|
|
259
|
-
|
|
260
|
-
// Explicit lockFile: suppress the per-rootDir probe so a stale project-root lockfile can't
|
|
261
|
-
// shadow it; substituted below.
|
|
202
|
+
// An explicit lockFile suppresses the per-rootDir probe, so a stale project-root lockfile can't shadow it.
|
|
262
203
|
const explicitLockPath = this.config.lockFile
|
|
263
|
-
// A
|
|
264
|
-
// root-detection signal: picking the innermost dir would diverge load's root from the capture
|
|
265
|
-
// root. A config-only bundleFile is NOT suppressed (its dir already carries the config signal).
|
|
204
|
+
// A flag/env `bundleFile` is rootDir-INDEPENDENT, so NOT a root signal: else load's root could diverge.
|
|
266
205
|
const explicitBundlePath = this.config.bundleFile
|
|
267
206
|
for (const rootDir of potentialRoots) {
|
|
268
|
-
const config = readFileSyncMaybe(rootDir,
|
|
269
|
-
const lockProbe = explicitLockPath ? null : readFileSyncMaybe(rootDir,
|
|
270
|
-
let sourcesPath = explicitBundlePath || join(rootDir,
|
|
207
|
+
const config = readFileSyncMaybe(rootDir, CONFIG_FILE, 'utf-8', this.#host)
|
|
208
|
+
const lockProbe = explicitLockPath ? null : readFileSyncMaybe(rootDir, LOCK_FILE, 'utf-8', this.#host)
|
|
209
|
+
let sourcesPath = explicitBundlePath || join(rootDir, BUNDLE_FILE)
|
|
271
210
|
let sources = this.#readMaybe(sourcesPath)
|
|
272
|
-
// Root
|
|
273
|
-
// (see explicitBundlePath; an explicit bundleFile is still loaded via `sources`).
|
|
211
|
+
// Root signals: a config, a lockfile, or the DEFAULT bundle path (an explicit one still loads via `sources`).
|
|
274
212
|
if (config === null && lockProbe === null && (explicitBundlePath || sources === null)) continue
|
|
275
|
-
if (loaded) throw new Error('Stasis config already loaded')
|
|
276
213
|
loaded = true
|
|
277
214
|
this.root = rootDir
|
|
278
215
|
|
|
279
216
|
if (config) {
|
|
280
217
|
this.config.loadConfig(config)
|
|
281
|
-
//
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
218
|
+
// false: a static build checks the whole bundle itself, every offender at once.
|
|
219
|
+
if (checkPolicy) this.#policy = this.config.policy
|
|
220
|
+
// stasis.config.json may set a `bundleFile` the probe couldn't see.
|
|
221
|
+
if (this.#bundlePath !== sourcesPath) {
|
|
222
|
+
sourcesPath = this.#bundlePath
|
|
285
223
|
sources = this.#readMaybe(sourcesPath)
|
|
286
224
|
}
|
|
287
225
|
}
|
|
288
226
|
|
|
289
227
|
const lock = explicitLockPath ? this.#readMaybe(explicitLockPath, 'utf-8') : lockProbe
|
|
290
|
-
|
|
291
|
-
this.#loadBundleArtifacts(sources, sourcesPath
|
|
228
|
+
this.#absorbLockfile(lock, this.#lockPath)
|
|
229
|
+
this.#loadBundleArtifacts(sources, sourcesPath)
|
|
292
230
|
|
|
293
|
-
//
|
|
294
|
-
// re-detects a rootDir-independent bundleFile and trips `loaded`.
|
|
231
|
+
// The innermost wins: else an outer root re-detects a rootDir-independent bundleFile.
|
|
295
232
|
break
|
|
296
233
|
}
|
|
297
234
|
|
|
298
|
-
//
|
|
299
|
-
|
|
300
|
-
if (!loaded && explicitLockPath) {
|
|
301
|
-
lockfileLoaded = this.#absorbLockfile(this.#readMaybe(explicitLockPath, 'utf-8'), explicitLockPath)
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// Explicit bundleFile suppressed as a root signal above: load against `this.root` (outermost).
|
|
235
|
+
// With no root signal, explicit artifacts load against the outermost root.
|
|
236
|
+
if (!loaded && explicitLockPath) this.#absorbLockfile(this.#readMaybe(explicitLockPath, 'utf-8'), explicitLockPath)
|
|
305
237
|
if (!loaded && (explicitBundlePath || this.config.resourcesBundleFile)) {
|
|
306
|
-
this.#loadBundleArtifacts(explicitBundlePath ? this.#readMaybe(explicitBundlePath) : null, explicitBundlePath
|
|
238
|
+
this.#loadBundleArtifacts(explicitBundlePath ? this.#readMaybe(explicitBundlePath) : null, explicitBundlePath)
|
|
307
239
|
}
|
|
308
240
|
|
|
309
|
-
|
|
310
|
-
if (this.config.
|
|
311
|
-
|
|
241
|
+
if (this.config.frozen) assert.ok(this.#lockfileLoaded, 'No lockfile, but attempting to run in frozen mode')
|
|
242
|
+
if (this.config.frozenBundle) this.#freezeBundle()
|
|
243
|
+
this.#holdToInstalled(relative(this.root, this.#lockfileLoaded ? this.#lockPath : this.#bundlePath))
|
|
312
244
|
|
|
313
|
-
|
|
314
|
-
if (this.config.writeBundle) {
|
|
315
|
-
this.#claimWritePath('bundleFile')
|
|
316
|
-
this.#claimWritePath('resourcesBundleFile')
|
|
317
|
-
}
|
|
245
|
+
if (this.config.writeBundle) for (const key of ['bundleFile', 'resourcesBundleFile']) this.#claimWritePath(key)
|
|
318
246
|
if (this.config.writeLockfile) this.#claimWritePath('lockFile')
|
|
319
|
-
|
|
320
|
-
this.#setPreloadRoot(preloadRoot)
|
|
321
|
-
|
|
322
|
-
// The registry answers for the preload and for write claims; any other State stays out of it.
|
|
323
247
|
if (isPreload || this.#claims.size > 0) liveStates().add(this)
|
|
324
248
|
}
|
|
325
249
|
|
|
@@ -327,116 +251,145 @@ export class State {
|
|
|
327
251
|
return this.#host.stat(p) !== null
|
|
328
252
|
}
|
|
329
253
|
|
|
330
|
-
// The
|
|
331
|
-
|
|
332
|
-
|
|
254
|
+
// The ATTESTED_METADATA absorbed of the artifact at `label`, and what each bundle attests, held to how each
|
|
255
|
+
// package was installed; kept as recorded where no account tells (nothing installed, as where a bundle runs).
|
|
256
|
+
#holdToInstalled(label) {
|
|
257
|
+
const { files, bundles } = this.#absorbed
|
|
258
|
+
// What only its bundles attest of a package is its record's too where they hold every file of it; else
|
|
259
|
+
// it stays theirs, which a run writing its bundle again would drop: refused.
|
|
260
|
+
for (const dir of this.#attestedByBundle) {
|
|
261
|
+
const [module, own] = [this.modules.get(dir), bundles.get(dir)]
|
|
262
|
+
const alone = Object.fromEntries(ATTESTED_METADATA.filter((field) => module[field] === undefined && own[field] !== undefined).map((field) => [field, own[field]]))
|
|
263
|
+
const beyond = Object.keys(module.files).find((rel) => !Object.hasOwn(own.files, rel))
|
|
264
|
+
if (beyond === undefined) this.modules.set(dir, moduleInfo({ ...module, ...alone }))
|
|
265
|
+
else assert.ok(!this.config.writeBundle || Object.keys(alone).length === 0, `bundle module ${dir} ${Object.keys(alone)[0]} is recorded of its files, not of '${moduleFileKey(dir, beyond)}' its record holds: record it anew (lock = replace)`)
|
|
266
|
+
}
|
|
267
|
+
const attested = new Map([...this.modules].filter(([, module]) => attestsAny(module)))
|
|
268
|
+
for (const [dir, module] of attested) if (!files.has(dir)) files.set(dir, new Set(Object.keys(module.files)))
|
|
269
|
+
const accounts = installedAccounts(new Map([...bundles, ...attested]), this.root)
|
|
270
|
+
heldToAccounts(attested, accounts, `${label}: `)
|
|
271
|
+
heldToAccounts(bundles, accounts, `${relative(this.root, this.#bundlePath)}: `)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// A bundle's record of a package merged into the State's (`record`, if any). What it alone attests agrees
|
|
275
|
+
// with every other bundle's of the files it covers (mergeModuleMaps), and is taken once all are absorbed.
|
|
276
|
+
#absorbBundleRecord(dir, info, record, what) {
|
|
277
|
+
const { dirs, files, bundles } = this.#absorbed
|
|
278
|
+
dirs.add(dir)
|
|
279
|
+
if (attestsAny(info)) {
|
|
280
|
+
this.#attestedByBundle.add(dir)
|
|
281
|
+
const known = bundles.get(dir)
|
|
282
|
+
bundles.set(dir, known === undefined ? info : mergeModuleMaps(new Map([[dir, known]]), new Map([[dir, info]]), 'bundle').get(dir))
|
|
283
|
+
// A later bundle's files (a sidecar's) of what it attests alike are absorbed, not this run's.
|
|
284
|
+
const baseline = files.get(dir)
|
|
285
|
+
if (baseline && ATTESTED_METADATA.every((field) => record?.[field] === undefined || JSON.stringify(record[field]) === JSON.stringify(info[field]))) for (const rel of Object.keys(info.files)) baseline.add(rel)
|
|
286
|
+
}
|
|
287
|
+
// One attesting none of what the record took already (a sidecar's, after its parent's) adds no
|
|
288
|
+
// file to it where its files are the record's too: the value was recorded without them.
|
|
289
|
+
const lacked = this.config.writeBundle && record ? ATTESTED_METADATA.find((field) => record[field] !== undefined && info[field] === undefined) : undefined
|
|
290
|
+
const added = lacked === undefined ? undefined : Object.keys(info.files).find((rel) => !(files.get(dir)?.has(rel) ?? Object.hasOwn(record.files, rel)))
|
|
291
|
+
if (added !== undefined) assert.fail(`bundle: module '${dir}' ${lacked} is recorded of one side's files, not of '${moduleFileKey(dir, added)}' the other adds`)
|
|
292
|
+
// What a bundle alone attests is the record's once all are absorbed, where they cover its files (#holdToInstalled).
|
|
293
|
+
const alone = Object.fromEntries(ATTESTED_METADATA.filter((field) => record?.[field] === undefined).map((field) => [field, undefined]))
|
|
294
|
+
return moduleInfo(record === undefined ? { ...info, ...alone, files: Object.create(null) } : { ...withMetadataOf(record, info, what), ...alone })
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Not what a lockfile alone locks (what an install would lay out, not what one did), nor onto a record
|
|
298
|
+
// absorbed, whose other files this run may not have read: of a file this run adds, an install record tells.
|
|
299
|
+
#recordInstalled() {
|
|
300
|
+
const accounts = installedAccounts(this.modules, this.root)
|
|
301
|
+
heldToAccounts(this.modules, accounts)
|
|
302
|
+
for (const [dir, account] of accounts) if (!account.locked && !this.#absorbed.dirs.has(dir)) this.modules.set(dir, withMetadataOf(this.modules.get(dir), account))
|
|
303
|
+
for (const [dir, files] of this.#absorbed.files) {
|
|
304
|
+
const [module, account] = [this.modules.get(dir), accounts.get(dir)]
|
|
305
|
+
const added = account && !account.locked ? undefined : Object.keys(module.files).find((rel) => !files.has(rel))
|
|
306
|
+
if (added === undefined) continue
|
|
307
|
+
const fields = ATTESTED_METADATA.filter((field) => module[field] !== undefined).join(' and ')
|
|
308
|
+
assert.fail(`module '${dir}': its ${fields} is recorded of the files it was read with, not of ${moduleFileKey(dir, added)}, which this run adds and no install record tells of: record it anew (lock = replace)`)
|
|
309
|
+
}
|
|
333
310
|
}
|
|
334
311
|
|
|
335
|
-
#
|
|
336
|
-
|
|
337
|
-
assert.equal(typeof preloadRoot, 'string', 'preloadRoot must be a string')
|
|
338
|
-
this.#preloadRoot = preloadRoot
|
|
312
|
+
#readMaybe(path, encoding) {
|
|
313
|
+
return readFileSyncMaybe(dirname(path), basename(path), encoding, this.#host)
|
|
339
314
|
}
|
|
340
315
|
|
|
341
|
-
//
|
|
342
|
-
// requires both imports and formats, so an absorbed lockfile always attests them.
|
|
316
|
+
// Lockfile.parse requires both imports and formats, so an absorbed lockfile always attests them.
|
|
343
317
|
#absorbLockfile(lock, lockPath) {
|
|
344
|
-
if (lock && !this.config.useLockfile && !this.config.ignoreLockfile) {
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
if (!lock || !this.config.useLockfile || this.config.replaceLockfile) return false
|
|
318
|
+
if (lock && !this.config.useLockfile && !this.config.ignoreLockfile) throw new Error(`Unexpected ${lockPath} with config.lock = 'none'`)
|
|
319
|
+
if (!lock || !this.config.useLockfile || this.config.replaceLockfile) return
|
|
348
320
|
const lockfile = Lockfile.parse(lock)
|
|
349
321
|
if (this.config.frozen) assert.equal(lockfile.config.scope, this.config.scope)
|
|
322
|
+
this.#assertLoaded(lockfile, lockPath)
|
|
350
323
|
|
|
351
324
|
const includeSources = lockfile.config.scope === 'full' && this.config.full
|
|
352
325
|
for (const [dir, info] of lockfile.modules) {
|
|
353
326
|
if (!hasNodeModulesSegment(dir) && !includeSources) continue
|
|
354
327
|
this.modules.set(dir, info)
|
|
328
|
+
this.#absorbed.dirs.add(dir)
|
|
329
|
+
for (const [name, hash] of Object.entries(info.files)) noupsert(this.hashes, moduleFileKey(dir, name), hash)
|
|
355
330
|
}
|
|
356
331
|
if (includeSources) this.entries = lockfile.entries
|
|
357
|
-
|
|
358
|
-
for (const [dir, { files }] of this.modules) {
|
|
359
|
-
for (const [name, hash] of Object.entries(files)) noupsert(this.hashes, join(dir, name), hash)
|
|
360
|
-
}
|
|
361
332
|
this.#lockImports = lockfile.imports
|
|
362
333
|
this.#lockFormats = lockfile.formats
|
|
363
|
-
//
|
|
364
|
-
|
|
365
|
-
this.#absorbExecutable(lockfile)
|
|
334
|
+
// Into the live set, not a baseline, so re-reading a file this run can CLEAR a stale exec bit.
|
|
335
|
+
for (const file of lockfile.executable) this.executable.add(file)
|
|
366
336
|
this.#lockfileLoaded = true
|
|
367
|
-
return true
|
|
368
337
|
}
|
|
369
338
|
|
|
370
|
-
// An on-disk bundle is read in under add/load/frozen; replace starts over.
|
|
371
339
|
get #absorbsBundles() {
|
|
372
340
|
return this.config.bundle && !this.config.replaceBundle
|
|
373
341
|
}
|
|
374
342
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
if (sources && this.config.
|
|
379
|
-
throw new Error(`Unexpected ${sourcesPath} with config.bundle = 'none'`)
|
|
380
|
-
}
|
|
381
|
-
// A frozen bundle is self-attesting, so it needs no sibling lockfile and is exempt here.
|
|
382
|
-
if (sources && !lockfileLoaded && this.config.useLockfile && !this.config.replaceLockfile && !this.config.frozenBundle) {
|
|
343
|
+
#loadBundleArtifacts(sources, sourcesPath) {
|
|
344
|
+
if (sources && this.config.bundleMode === 'none') throw new Error(`Unexpected ${sourcesPath} with config.bundle = 'none'`)
|
|
345
|
+
// A frozen bundle is self-attesting, so it needs no sibling lockfile.
|
|
346
|
+
if (sources && !this.#lockfileLoaded && this.config.useLockfile && !this.config.replaceLockfile && !this.config.frozenBundle) {
|
|
383
347
|
throw new Error('stasis.lock.json missing, can not use sources')
|
|
384
348
|
}
|
|
385
|
-
if (sources && this.#absorbsBundles)
|
|
386
|
-
|
|
349
|
+
if (sources && this.#absorbsBundles) {
|
|
350
|
+
const bundle = this.#parseServedBundle(sources, sourcesPath, 'stasis run',
|
|
351
|
+
'Re-bundle with the current stasis (`stasis bundle`) or `stasis run --bundle=replace` against a v0-free starting point to upgrade.')
|
|
352
|
+
// assertEntry passes anything on an empty set: else any bundled file could run as the root.
|
|
353
|
+
assert.ok(!this.config.full || bundle.entries.size > 0 || !bundle.hasCode,
|
|
354
|
+
`${sourcesPath}: a full-scope bundle carrying code must declare an entry to run ` +
|
|
355
|
+
`(an add-only attestation has none; run a bundle built by \`stasis bundle\`/\`stasis run\`)`)
|
|
356
|
+
this.#absorbCodeBundle(bundle)
|
|
357
|
+
}
|
|
358
|
+
this.#absorbResourcesBundleFile()
|
|
387
359
|
}
|
|
388
360
|
|
|
389
|
-
//
|
|
390
|
-
#
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
361
|
+
// v0 has no per-file formats or import map: serving one widens the trust boundary.
|
|
362
|
+
#parseServedBundle(bytes, path, runner = 'stasis run', remedy = REBUNDLE) {
|
|
363
|
+
const bundle = parseBundleBytes(bytes)
|
|
364
|
+
assert.equal(bundle.version, Bundle.VERSION, `${runner} requires a v1 bundle; ${path} is v${bundle.version}. ${remedy}`)
|
|
365
|
+
assert.equal(bundle.config.scope, this.config.scope)
|
|
366
|
+
this.#assertLoaded(bundle, path)
|
|
367
|
+
return bundle
|
|
394
368
|
}
|
|
395
369
|
|
|
396
|
-
#
|
|
397
|
-
|
|
398
|
-
// Bundle.parse accepts v0 for offline tooling; the runtime refuses it -- v0 has no per-file
|
|
399
|
-
// formats or import map, so serving one widens the trust boundary.
|
|
400
|
-
assert.equal(bundle.version, Bundle.VERSION,
|
|
401
|
-
`stasis run requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
|
|
402
|
-
`Re-bundle with the current stasis (\`stasis bundle\`) or \`stasis run --bundle=replace\` ` +
|
|
403
|
-
`against a v0-free starting point to upgrade.`)
|
|
404
|
-
assert.equal(bundle.config.scope, this.config.scope)
|
|
405
|
-
// assertEntry short-circuits on an empty set, so a runnable full-scope code bundle must declare
|
|
406
|
-
// an entry here -- else any bundled file could run as the root. Add-only bundles have none.
|
|
407
|
-
assert.ok(!this.config.full || bundle.entries.size > 0 || !bundle.hasCode,
|
|
408
|
-
`${sourcesPath}: a full-scope bundle carrying code must declare an entry to run ` +
|
|
409
|
-
`(an add-only attestation has none; run a bundle built by \`stasis bundle\`/\`stasis run\`)`)
|
|
410
|
-
this.#absorbCodeBundle(bundle, lockfileLoaded)
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
// Take a parsed code bundle (unified, or the code half) as the live maps, cross-checked against
|
|
414
|
-
// the loaded lockfile, and snapshot it when it is the frozen attestation.
|
|
415
|
-
#absorbCodeBundle(bundle, lockfileLoaded) {
|
|
416
|
-
this.#mergeBundleMetadata(bundle, { lockfileLoaded })
|
|
370
|
+
#absorbCodeBundle(bundle) {
|
|
371
|
+
this.#mergeBundleMetadata(bundle)
|
|
417
372
|
for (const [file, content] of bundle.sources) {
|
|
418
373
|
if (Bundle.isResourceFormat(bundle.formats.get(file))) this.resources.set(file, content)
|
|
419
374
|
else this.sources.set(file, content)
|
|
420
375
|
}
|
|
421
376
|
this.formats = bundle.formats
|
|
422
377
|
this.imports = bundle.imports
|
|
423
|
-
this.#
|
|
424
|
-
this.#seedReasonFromBundle(bundle)
|
|
425
|
-
this.#absorbRepo('code', bundle)
|
|
426
|
-
if (this.config.frozenBundle) {
|
|
427
|
-
// Snapshot before addFile/addImport mutate the live maps; imports is deep-cloned because
|
|
428
|
-
// this.imports shares bundle.imports's nested Maps.
|
|
429
|
-
this.#bundleSources = new Set(this.sources.keys())
|
|
430
|
-
this.#bundleResources = new Set(this.resources.keys())
|
|
431
|
-
this.#bundleImports = objectToMaps(fileMapToObject(bundle.imports))
|
|
432
|
-
this.#bundleFormats = new Map(bundle.formats)
|
|
433
|
-
}
|
|
378
|
+
this.#absorbBundleExtras('code', bundle)
|
|
434
379
|
}
|
|
435
380
|
|
|
436
|
-
//
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
381
|
+
// Before addFile/addImport mutate the live maps; imports deep, as this.imports shares the bundle's Maps.
|
|
382
|
+
#freezeBundle() {
|
|
383
|
+
assert.ok(this.#absorbedRepos.size > 0, 'No bundle, but attempting to run in frozen bundle mode')
|
|
384
|
+
this.#bundleSources = new Set(this.sources.keys())
|
|
385
|
+
this.#bundleResources = new Set(this.resources.keys())
|
|
386
|
+
this.#bundleImports = cloneMaps(this.imports)
|
|
387
|
+
this.#bundleFormats = new Map(this.formats)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Cross-checked against the loaded lockfile, else absorbed as the source of truth.
|
|
391
|
+
#mergeBundleMetadata(bundle) {
|
|
392
|
+
if (this.#lockfileLoaded) {
|
|
440
393
|
if (bundle.entries.size > 0) {
|
|
441
394
|
assert.deepStrictEqual([...bundle.entries].toSorted(), [...this.entries].toSorted(), 'bundle/lockfile entries mismatch')
|
|
442
395
|
}
|
|
@@ -445,119 +398,99 @@ export class State {
|
|
|
445
398
|
assert.ok(this.modules.has(dir), `bundle module ${dir} missing in lockfile`)
|
|
446
399
|
const lockModule = this.modules.get(dir)
|
|
447
400
|
assert.equal(info.name, lockModule.name)
|
|
448
|
-
// Strict, absent included: every absorb site requires
|
|
449
|
-
// a real workspace identity (or a stripped field), never v0 partial metadata -- a
|
|
450
|
-
// version-less bundle bucket must not dodge the lockfile consistency check.
|
|
401
|
+
// Strict, absent included: every absorb site requires v1, so a missing version is a real identity.
|
|
451
402
|
assert.equal(info.version, lockModule.version, `bundle module ${dir} version mismatch with lockfile`)
|
|
452
|
-
//
|
|
453
|
-
|
|
454
|
-
for (const rel of Object.keys(info.files)) {
|
|
455
|
-
assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
|
|
456
|
-
}
|
|
403
|
+
// The bundle's metadata is taken where the lockfile records none, to be written again.
|
|
404
|
+
this.modules.set(dir, this.#absorbBundleRecord(dir, info, lockModule, `bundle module ${dir} (against the lockfile)`))
|
|
405
|
+
for (const rel of Object.keys(info.files)) assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
|
|
457
406
|
}
|
|
458
|
-
// Hashes alone don't stop a redirect to
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
for (const [specifier, file] of specifiers) {
|
|
464
|
-
this.#assertAttestedResolution(this.#lockImports, conditions, parent, specifier, file, { what: 'bundle', source: 'lockfile' })
|
|
465
|
-
}
|
|
407
|
+
// Hashes alone don't stop a redirect to another hash-valid file: every edge must match the lockfile's.
|
|
408
|
+
for (const [conditions, byParent] of bundle.imports) {
|
|
409
|
+
for (const [parent, specifiers] of byParent) {
|
|
410
|
+
for (const [specifier, file] of specifiers) {
|
|
411
|
+
this.#assertAttestedResolution(this.#lockImports, conditions, parent, specifier, file, { what: 'bundle', source: 'lockfile' })
|
|
466
412
|
}
|
|
467
413
|
}
|
|
468
414
|
}
|
|
469
|
-
// A format flip changes how hash-valid bytes parse (module<->commonjs) without touching a
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
for (const rel of Object.keys(files)) {
|
|
479
|
-
const file = moduleFileKey(dir, rel)
|
|
480
|
-
const lockFormat = this.#lockFormats.get(file)
|
|
481
|
-
if (!Bundle.isResourceFormat(lockFormat)) continue
|
|
482
|
-
assert.equal(bundle.formats.get(file), lockFormat,
|
|
483
|
-
`bundle file ${file} must declare format='${lockFormat}' to match the lockfile`)
|
|
484
|
-
}
|
|
415
|
+
// A format flip changes how hash-valid bytes parse (module<->commonjs) without touching a hash.
|
|
416
|
+
for (const [file, format] of bundle.formats) this.#assertAttestedFormat(this.#lockFormats, file, format, 'bundle', 'lockfile')
|
|
417
|
+
// Inverse: omitting a resource tag would route the base64 payload through this.sources to the loader.
|
|
418
|
+
for (const [dir, { files }] of bundle.modules) {
|
|
419
|
+
for (const rel of Object.keys(files)) {
|
|
420
|
+
const file = moduleFileKey(dir, rel)
|
|
421
|
+
const lockFormat = this.#lockFormats.get(file)
|
|
422
|
+
if (!Bundle.isResourceFormat(lockFormat)) continue
|
|
423
|
+
assert.equal(bundle.formats.get(file), lockFormat, `bundle file ${file} must declare format='${lockFormat}' to match the lockfile`)
|
|
485
424
|
}
|
|
486
425
|
}
|
|
487
426
|
} else {
|
|
488
|
-
//
|
|
427
|
+
// In place: this.entries may be a sidecar's parent's.
|
|
489
428
|
for (const e of bundle.entries) this.entries.add(e)
|
|
490
429
|
for (const [dir, info] of bundle.modules) {
|
|
491
|
-
//
|
|
492
|
-
// buckets); a v1 workspace bucket may omit version, so its identity is absorbed anyway.
|
|
430
|
+
// v0 partial metadata; a v1 workspace bucket may omit version.
|
|
493
431
|
if (!info.name || (!info.version && hasNodeModulesSegment(dir))) continue
|
|
494
432
|
const existing = this.modules.get(dir)
|
|
495
|
-
if (existing
|
|
496
|
-
|
|
497
|
-
} else {
|
|
498
|
-
// A dir may be added twice (code + resource entries), and both must agree.
|
|
433
|
+
if (existing !== undefined) {
|
|
434
|
+
// A dir may be added twice (code + resource halves).
|
|
499
435
|
assert.equal(info.name, existing.name, `bundle ${dir} name mismatch`)
|
|
500
436
|
assert.equal(info.version, existing.version, `bundle ${dir} version mismatch`)
|
|
501
|
-
|
|
502
|
-
|
|
437
|
+
}
|
|
438
|
+
this.modules.set(dir, this.#absorbBundleRecord(dir, info, existing, `bundle ${dir}`))
|
|
439
|
+
// A bundle=add run writes these back with only the files they list: else an unloaded bundled file drops out.
|
|
440
|
+
if (!this.config.writeBundle) continue
|
|
441
|
+
const { files } = this.modules.get(dir)
|
|
442
|
+
for (const [rel, content] of Object.entries(info.files)) {
|
|
443
|
+
const file = moduleFileKey(dir, rel)
|
|
444
|
+
const integrity = bundledIntegrity(content, bundle.formats.get(file))
|
|
445
|
+
noupsert(this.hashes, file, integrity)
|
|
446
|
+
if (!Object.hasOwn(files, rel)) files[rel] = integrity
|
|
447
|
+
assert.equal(files[rel], integrity, `bundle file ${file} conflicts with its recorded hash`)
|
|
503
448
|
}
|
|
504
449
|
}
|
|
505
450
|
}
|
|
506
451
|
}
|
|
507
452
|
|
|
508
|
-
//
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
assert.equal(bundle.config.scope, this.config.scope)
|
|
516
|
-
// A populated imports/entries map means mixed halves or tampering -- widens trust on the code side.
|
|
453
|
+
// The resources half of a split layout, when configured and on disk: ONLY resource-format files.
|
|
454
|
+
#absorbResourcesBundleFile() {
|
|
455
|
+
const resourcesPath = this.config.resourcesBundleFile
|
|
456
|
+
const resourcesData = resourcesPath ? this.#readMaybe(resourcesPath) : null
|
|
457
|
+
if (!resourcesData || !this.#absorbsBundles) return
|
|
458
|
+
const bundle = this.#parseServedBundle(resourcesData, resourcesPath)
|
|
459
|
+
// Populated imports/entries mean mixed halves or tampering, widening trust on the code side.
|
|
517
460
|
assert.equal(bundle.imports.size, 0, `resources bundle ${resourcesPath} must have empty imports`)
|
|
518
461
|
assert.equal(bundle.entries.size, 0, `resources bundle ${resourcesPath} must have empty entries`)
|
|
519
462
|
for (const [file, format] of bundle.formats) {
|
|
520
|
-
assert.ok(Bundle.isResourceFormat(format),
|
|
521
|
-
`resources bundle ${resourcesPath} declares non-resource format='${format}' for ${file}`)
|
|
463
|
+
assert.ok(Bundle.isResourceFormat(format), `resources bundle ${resourcesPath} declares non-resource format='${format}' for ${file}`)
|
|
522
464
|
}
|
|
523
|
-
this.#mergeBundleMetadata(bundle
|
|
465
|
+
this.#mergeBundleMetadata(bundle)
|
|
524
466
|
for (const [file, content] of bundle.sources) this.resources.set(file, content)
|
|
525
|
-
// Union formats: the two halves' file sets must be disjoint.
|
|
526
467
|
for (const [file, format] of bundle.formats) {
|
|
527
468
|
const existing = this.formats.get(file)
|
|
528
469
|
assert.ok(existing === undefined || existing === format,
|
|
529
470
|
`format conflict for ${file}: bundleFile declares '${existing}', resourcesBundleFile declares '${format}'`)
|
|
530
471
|
this.formats.set(file, format)
|
|
531
472
|
}
|
|
532
|
-
this.#
|
|
533
|
-
this.#seedReasonFromBundle(bundle)
|
|
534
|
-
this.#absorbRepo('resources', bundle)
|
|
535
|
-
if (this.config.frozenBundle) {
|
|
536
|
-
// Extend the frozen snapshot, init lazily: resources-only deployments are legal, so
|
|
537
|
-
// bundleFile may not have populated these.
|
|
538
|
-
this.#bundleResources ??= new Set()
|
|
539
|
-
this.#bundleFormats ??= new Map()
|
|
540
|
-
this.#bundleImports ??= objectToMaps(fileMapToObject(this.imports))
|
|
541
|
-
this.#bundleSources ??= new Set(this.sources.keys())
|
|
542
|
-
for (const f of bundle.sources.keys()) this.#bundleResources.add(f)
|
|
543
|
-
for (const [f, fmt] of bundle.formats) this.#bundleFormats.set(f, fmt)
|
|
544
|
-
}
|
|
473
|
+
this.#absorbBundleExtras('resources', bundle)
|
|
545
474
|
}
|
|
546
475
|
|
|
547
|
-
//
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
476
|
+
// A bundle half's `executable` (unioned: a lockfile or the other half may have contributed), `repo` and `reason`.
|
|
477
|
+
#absorbBundleExtras(half, bundle) {
|
|
478
|
+
for (const file of bundle.executable) this.executable.add(file)
|
|
479
|
+
this.#absorbedRepos.set(half, this.#absorbedRepos.has(half) ? mergeRepo(this.#absorbedRepos.get(half), bundle.repo) : bundle.repo)
|
|
480
|
+
// So a bundle=add re-run preserves other consumers' attribution.
|
|
481
|
+
if (!this.config.writeBundle || bundle.reason === undefined) return
|
|
482
|
+
for (const [consumer, files] of Object.entries(bundle.reason)) {
|
|
483
|
+
for (const file of files) this.#recordReason(consumer, file)
|
|
484
|
+
}
|
|
551
485
|
}
|
|
552
486
|
|
|
553
|
-
//
|
|
554
|
-
|
|
555
|
-
#assertAttestedResolution(attestation, conditions, parent, specifier, file, { what, source = 'lockfile', tolerateUnknown = false }) {
|
|
487
|
+
// Under the exact conditions key, else only where every condition set agrees on one target.
|
|
488
|
+
#assertAttestedResolution(attestation, conditions, parent, specifier, file, { what, source, tolerateUnknown = false, targetPlatform }) {
|
|
556
489
|
const edge = `'${specifier}' from ${parent} (${conditions})`
|
|
557
490
|
let attested = attestation.get(conditions)?.get(parent)?.get(specifier)
|
|
558
491
|
if (attested === undefined) {
|
|
559
492
|
const targets = new Set()
|
|
560
|
-
for (const
|
|
493
|
+
for (const parents of attestation.values()) {
|
|
561
494
|
const target = parents.get(parent)?.get(specifier)
|
|
562
495
|
if (target !== undefined) targets.add(target)
|
|
563
496
|
}
|
|
@@ -566,33 +499,39 @@ export class State {
|
|
|
566
499
|
assert.ok(targets.size === 1, `${what} resolution ${edge} is attested inconsistently across condition sets in the ${source}`)
|
|
567
500
|
;[attested] = targets
|
|
568
501
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
assert.ok(matches, `${what} resolution ${edge} mismatches the ${source}`)
|
|
502
|
+
if (targetPlatform !== undefined && attested instanceof Map && !(file instanceof Map)) {
|
|
503
|
+
attested = attested.get(targetPlatform)
|
|
504
|
+
assert.ok(attested !== undefined, `${what} resolution ${edge} is not attested for platform '${targetPlatform}' by the ${source}`)
|
|
505
|
+
}
|
|
506
|
+
assert.ok(importTargetsEqual(file, attested), `${what} resolution ${edge} mismatches the ${source}`)
|
|
575
507
|
}
|
|
576
508
|
|
|
577
|
-
//
|
|
578
|
-
|
|
579
|
-
|
|
509
|
+
// Closes the attested set of a frozen lockfile and bundle; the caller's noupsert checks the bytes.
|
|
510
|
+
#assertFrozenAttests(noun, file, module, rel, bundled, format) {
|
|
511
|
+
if (this.config.frozen) assert.ok(Object.hasOwn(module.files, rel), `${noun} not attested by the frozen lockfile: ${file}`)
|
|
512
|
+
if (this.config.frozenBundle) assert.ok(bundled?.has(file), `${noun} not attested by the frozen bundle: ${file}`)
|
|
513
|
+
// `type` is usually not hash-attested, so a format flip would recategorize a hash-valid file.
|
|
514
|
+
if (format == null) return
|
|
515
|
+
if (this.config.frozen && this.#lockFormats !== null) this.#assertAttestedFormat(this.#lockFormats, file, format, 'observed', 'lockfile')
|
|
516
|
+
if (this.config.frozenBundle) this.#assertAttestedFormat(this.#bundleFormats, file, format, 'observed', 'frozen bundle')
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// Callers only pass files that must be attested: an unattested one is a forged/extra entry.
|
|
520
|
+
#assertAttestedFormat(attestation, file, format, what, source) {
|
|
580
521
|
const attested = attestation.get(file)
|
|
581
522
|
assert.ok(attested !== undefined, `${what} format for ${file} is not attested by the ${source}`)
|
|
582
523
|
assert.equal(format, attested, `${what} format for ${file} mismatches the ${source}`)
|
|
583
524
|
}
|
|
584
525
|
|
|
585
526
|
assertEntry(url) {
|
|
586
|
-
//
|
|
587
|
-
// already refuses a runnable code bundle that declares none.
|
|
527
|
+
// #loadCodeBundle already refuses a runnable code bundle that declares none.
|
|
588
528
|
if (this.entries.size === 0) return
|
|
589
529
|
const file = this.#canonicalFile(url)
|
|
590
530
|
assert.ok(this.entries.has(file), `Unknown entry point: ${file}`)
|
|
591
531
|
}
|
|
592
532
|
|
|
593
533
|
static get preload() {
|
|
594
|
-
|
|
595
|
-
return undefined
|
|
534
|
+
return liveStates().values().find((state) => state.isPreload)
|
|
596
535
|
}
|
|
597
536
|
|
|
598
537
|
// Public so State.preload works across copies.
|
|
@@ -605,8 +544,7 @@ export class State {
|
|
|
605
544
|
return this.#claims.get(canonical)
|
|
606
545
|
}
|
|
607
546
|
|
|
608
|
-
// Refuse a path another live State (ANY copy) already claims; canonicalized so `./x`/`x`/symlinks
|
|
609
|
-
// compare equal. `key` names the config option; nothing is claimed when it is unset.
|
|
547
|
+
// Refuse a path another live State (ANY copy) already claims; canonicalized so `./x`/`x`/symlinks compare equal.
|
|
610
548
|
#claimWritePath(key, prefix = '') {
|
|
611
549
|
const value = this.config[key]
|
|
612
550
|
if (!value || !this.#claim) return
|
|
@@ -624,23 +562,40 @@ export class State {
|
|
|
624
562
|
return VERSION
|
|
625
563
|
}
|
|
626
564
|
|
|
627
|
-
//
|
|
628
|
-
// class brand.
|
|
565
|
+
// Public, so a sidecar from another copy can read it across the class brand.
|
|
629
566
|
sidecarInheritance() {
|
|
630
567
|
return {
|
|
631
568
|
preloadRoot: this.#preloadRoot,
|
|
632
569
|
lockImports: this.#lockImports,
|
|
633
570
|
lockFormats: this.#lockFormats,
|
|
634
571
|
lockfileLoaded: this.#lockfileLoaded,
|
|
572
|
+
policy: this.#policy,
|
|
573
|
+
absorbed: this.#absorbed,
|
|
635
574
|
}
|
|
636
575
|
}
|
|
637
576
|
|
|
638
|
-
//
|
|
577
|
+
// linksOn() of the root: what the State records under a link is its target's.
|
|
578
|
+
get #links() {
|
|
579
|
+
return (this.#linksOf ??= linksOn(this.root, this.#host))
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// The Policy's path rules on a path recorded without reading it, in the attested zone and recorded nowhere yet.
|
|
583
|
+
#checkUnreadPath(file) {
|
|
584
|
+
if (this.#policy !== null && (this.config.full || hasNodeModulesSegment(file)) && !this.hashes.has(file) && !this.formats.has(file)) {
|
|
585
|
+
this.#policy.assertPath(file, undefined, this.#links(file))
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Before a run verifies against, serves or (adding to it) rewrites an artifact the Policy refuses.
|
|
590
|
+
#assertLoaded(artifact, path) {
|
|
591
|
+
this.#policy?.assertArtifact(artifact, { hint: ` -- recorded in ${relative(this.root, resolve(path))}; rebuild it (lock=replace / bundle=replace)`, links: this.#links })
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Public, cross-copy.
|
|
639
595
|
registerSidecar(sidecar) {
|
|
640
596
|
this.#sidecars.add(sidecar)
|
|
641
597
|
}
|
|
642
598
|
|
|
643
|
-
// LOAD-mode sidecar: served by getFs*Family, never a lockfile contributor. Public (cross-copy).
|
|
644
599
|
registerReadSidecar(sidecar) {
|
|
645
600
|
this.#readSidecars.add(sidecar)
|
|
646
601
|
}
|
|
@@ -660,102 +615,109 @@ export class State {
|
|
|
660
615
|
return file === '' ? '.' : file
|
|
661
616
|
}
|
|
662
617
|
|
|
618
|
+
#urlOf(file) {
|
|
619
|
+
return pathToFileURL(resolve(this.root, file)).toString()
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// As opposed to known by a stat record or a seeded hash alone.
|
|
623
|
+
#bundled(file) {
|
|
624
|
+
return this.sources.has(file) || this.resources.has(file)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// { url, absolute, file }: `file` the project-relative key (this.relative's), undefined outside the root.
|
|
663
628
|
#canonical(url) {
|
|
664
629
|
let result = this.#canonicalCache.get(url)
|
|
665
630
|
if (result === undefined) {
|
|
666
631
|
result = this.#canonicalUncached(url)
|
|
632
|
+
const file = relative(this.root, result.absolute)
|
|
633
|
+
result.file = relativeEscapes(file) ? undefined : file === '' ? '.' : file
|
|
667
634
|
this.#canonicalCache.set(url, result)
|
|
668
635
|
}
|
|
669
636
|
return result
|
|
670
637
|
}
|
|
671
638
|
|
|
672
|
-
// A node_modules file whose REAL path is outside node_modules is a linked-in workspace source
|
|
673
|
-
// (pnpm): canonicalize so it's recorded as a source, not a dependency. Everything else passes through.
|
|
639
|
+
// A node_modules file whose REAL path is outside node_modules is a linked-in workspace source (pnpm).
|
|
674
640
|
#canonicalUncached(url) {
|
|
675
641
|
const absolute = this.absolute(url)
|
|
676
642
|
const file = relative(this.root, absolute)
|
|
677
643
|
if (relativeEscapes(file) || !splitNodeModulesPath(file)) return { url, absolute }
|
|
678
644
|
let real
|
|
679
|
-
try {
|
|
680
|
-
real = this.#host.realpath(absolute)
|
|
681
|
-
} catch {
|
|
682
|
-
return { url, absolute }
|
|
683
|
-
}
|
|
645
|
+
try { real = this.#host.realpath(absolute) } catch { return { url, absolute } }
|
|
684
646
|
if (real === absolute) return { url, absolute }
|
|
685
647
|
const realFile = relative(this.root, real)
|
|
686
648
|
if (relativeEscapes(realFile) || splitNodeModulesPath(realFile)) return { url, absolute }
|
|
687
649
|
return { url: pathToFileURL(real).toString(), absolute: real }
|
|
688
650
|
}
|
|
689
651
|
|
|
652
|
+
// Throws, as this.relative, for a URL outside the project root.
|
|
690
653
|
#canonicalFile(url) {
|
|
691
|
-
|
|
654
|
+
const { absolute, file } = this.#canonical(url)
|
|
655
|
+
return file ?? this.relative(absolute)
|
|
692
656
|
}
|
|
693
657
|
|
|
694
|
-
//
|
|
658
|
+
// Undefined outside the project root: the shims then pass through.
|
|
695
659
|
#canonicalFileMaybe(url) {
|
|
696
|
-
try {
|
|
697
|
-
return this.#canonicalFile(url)
|
|
698
|
-
} catch {
|
|
699
|
-
return undefined
|
|
700
|
-
}
|
|
660
|
+
try { return this.#canonical(url).file } catch { return undefined }
|
|
701
661
|
}
|
|
702
662
|
|
|
703
|
-
//
|
|
704
|
-
// `/node_modules/` substring test, so a symlinked workspace source is read from disk.
|
|
663
|
+
// Through any workspace symlink, not a raw substring: a symlinked workspace source is read from disk.
|
|
705
664
|
inNodeModules(url) {
|
|
706
665
|
const file = this.#canonicalFileMaybe(url)
|
|
707
666
|
return file !== undefined && splitNodeModulesPath(file) !== null
|
|
708
667
|
}
|
|
709
668
|
|
|
710
|
-
//
|
|
711
|
-
// URL, see #locateModule), refusing one there that can't be read as findPackageJSON does
|
|
712
|
-
// (packageJSONStat). Bounded by the project root.
|
|
669
|
+
// findPackageJSON is unreliable for a directory (EISDIR, or the parent's); an unreadable one is refused as it does.
|
|
713
670
|
#nearestPackageJsonFor(dirAbsolute) {
|
|
714
671
|
for (let dir = dirAbsolute; ; dir = dirname(dir)) {
|
|
715
672
|
const candidate = join(dir, 'package.json')
|
|
716
673
|
if (packageJSONStat(this.#host, candidate)?.isFile()) return candidate
|
|
717
|
-
// The root's package.json was just checked; never escape root.
|
|
718
674
|
if (dir === this.root || dirname(dir) === dir) break
|
|
719
675
|
}
|
|
720
676
|
// Unreachable: state.root is resolved to a dir that has package.json.
|
|
721
677
|
assert.fail(`no package.json at or above directory ${this.relative(dirAbsolute)}`)
|
|
722
678
|
}
|
|
723
679
|
|
|
724
|
-
//
|
|
725
|
-
|
|
726
|
-
#locateModule(url, { directory = false } = {}) {
|
|
727
|
-
// Canonicalize first: a linked-in workspace source is recorded under its real path (a source).
|
|
680
|
+
// The owning package bucket, registered. `synthetic` (EMPTY_MODULE_PATH) must NOT exist on disk.
|
|
681
|
+
#locateModule(url, { directory = false, synthetic = false } = {}) {
|
|
728
682
|
const { absolute } = this.#canonical(url)
|
|
729
|
-
|
|
730
|
-
|
|
683
|
+
const file = this.#canonicalFile(url)
|
|
684
|
+
if (synthetic) assert.ok(this.#host.stat(absolute) === null, `the reserved path ${file} is needed for a synthetic empty module, but the project has a real file there`)
|
|
685
|
+
else assert.ok(this.#host.stat(absolute) !== null)
|
|
731
686
|
|
|
732
|
-
const closestPkgAbsolute = directory ? this.#nearestPackageJsonFor(absolute)
|
|
687
|
+
const closestPkgAbsolute = directory ? this.#nearestPackageJsonFor(absolute)
|
|
688
|
+
: synthetic ? this.#nearestPackageJsonFor(dirname(absolute)) : this.#host.findPackageJSON(absolute)
|
|
733
689
|
const closestPkg = readPackageJSON(this.#host, closestPkgAbsolute)
|
|
734
690
|
|
|
735
691
|
const closestType = closestPkg.type
|
|
736
692
|
assert.ok(closestType === undefined || closestType === 'module' || closestType === 'commonjs')
|
|
737
693
|
|
|
738
|
-
// findPackageJSON may land on a `{"type":"module"}`
|
|
739
|
-
const nmRoot = splitNodeModulesPath(file)
|
|
740
|
-
|
|
694
|
+
// findPackageJSON may land on a `{"type":"module"}` marker lacking name/version.
|
|
695
|
+
const { dir: nmRoot, rel: nmRel } = splitNodeModulesPath(file) ?? {}
|
|
696
|
+
// rootPkg: a dependency's package.json, whose repo is read only where a record lacks one.
|
|
697
|
+
let pkgAbsolute, rootPkg, name, version, vendored, subpackage
|
|
741
698
|
if (nmRoot) {
|
|
742
699
|
pkgAbsolute = resolve(this.root, nmRoot, 'package.json')
|
|
743
|
-
|
|
700
|
+
rootPkg = pkgAbsolute === closestPkgAbsolute ? closestPkg : readPackageJSON(this.#host, pkgAbsolute)
|
|
744
701
|
;({ name, version } = rootPkg)
|
|
745
|
-
repo = packageRepo(rootPkg)
|
|
746
702
|
assert.ok(name, `Missing name in ${this.relative(pkgAbsolute)}`)
|
|
747
703
|
assert.ok(version, `Missing version in ${this.relative(pkgAbsolute)}`)
|
|
748
|
-
|
|
704
|
+
// A package.json of a package vendored into this one names that package: the file is listed in `vendored`.
|
|
705
|
+
const vendorDir = vendorDirOf(name, nmRel)
|
|
706
|
+
const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, this.#manifestsBelow(nmRoot))
|
|
707
|
+
vendored = vendor?.vendored
|
|
708
|
+
if (closestPkgAbsolute !== pkgAbsolute && !vendor?.below && !isInconsistentPackageJsonException(this.relative(closestPkgAbsolute))) {
|
|
749
709
|
const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
|
|
750
710
|
// Allow fake module-name subpaths: the real module owns the prefix (npm wouldn't publish this).
|
|
751
711
|
if (closestPkg.name !== undefined && closestPkg.name !== name) assert.ok(closestPkg.name.startsWith(`${name}/`), message)
|
|
752
|
-
|
|
712
|
+
// A subpackage whose own package.json is the closest has its own version.
|
|
713
|
+
subpackage = this.#subpackageOf(nmRoot, nmRel, closestPkg, name)
|
|
714
|
+
const ownVersion = subpackage !== undefined && closestPkg.name !== undefined
|
|
715
|
+
if (closestPkg.version !== undefined && !ownVersion) assert.equal(closestPkg.version, version, message)
|
|
753
716
|
}
|
|
754
717
|
} else {
|
|
755
718
|
pkgAbsolute = closestPkgAbsolute
|
|
756
719
|
let json = closestPkg
|
|
757
|
-
// A workspace package
|
|
758
|
-
// name alone claims the bucket. node_modules buckets (above) still require both.
|
|
720
|
+
// A workspace package may omit version (private/unpublished): the name alone claims the bucket.
|
|
759
721
|
while (json.name === undefined) {
|
|
760
722
|
assert.ok(Object.keys(json).every((k) => k === 'type'))
|
|
761
723
|
const dir = dirname(pkgAbsolute)
|
|
@@ -764,51 +726,67 @@ export class State {
|
|
|
764
726
|
json = readPackageJSON(this.#host, pkgAbsolute)
|
|
765
727
|
}
|
|
766
728
|
name = json.name
|
|
767
|
-
//
|
|
768
|
-
// way, and recording null verbatim would split identity on the next run's re-read.
|
|
729
|
+
// `"version": null` folds to undefined as the parsers do, else identity splits on the next re-read.
|
|
769
730
|
version = json.version ?? undefined
|
|
770
731
|
}
|
|
771
732
|
const pkg = this.relative(pkgAbsolute)
|
|
772
733
|
assert.ok(pkg === 'package.json' || pkg.endsWith('/package.json'))
|
|
773
|
-
assert.equal(basename(pkg), 'package.json')
|
|
774
734
|
const dir = dirname(pkg)
|
|
775
735
|
if (nmRoot) assert.equal(dir, nmRoot)
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
}
|
|
736
|
+
// Checked as the run reaches it, before it is recorded; a hashed file was checked when recorded or loaded.
|
|
737
|
+
if (this.#policy !== null && (nmRoot || this.config.full) && !this.hashes.has(file)) this.#policy.assertPath(file, nmRoot ? { dir, name, version } : undefined, synthetic ? undefined : this.#links(file))
|
|
738
|
+
if (!this.modules.has(dir)) this.modules.set(dir, moduleInfo({ name, version, ecosystem: nmRoot ? 'npm' : undefined, files: Object.create(null) }))
|
|
780
739
|
const module = this.modules.get(dir)
|
|
781
740
|
if (module.name !== name || module.version !== version) {
|
|
782
741
|
// Message built only on failure: addFile is hot, and the mismatch is a migration/drift event.
|
|
783
|
-
const hint = (module.version == null) === (version == null) ? '' :
|
|
784
|
-
'
|
|
785
|
-
'package without one; regenerate it (lock=replace / bundle=replace)'
|
|
742
|
+
const hint = (module.version == null) === (version == null) ? '' : ' -- an artifact from an older stasis may record a ' +
|
|
743
|
+
'placeholder version for a workspace package without one; regenerate it (lock=replace / bundle=replace)'
|
|
786
744
|
assert.fail(`module identity mismatch for '${dir}': artifact records ` +
|
|
787
745
|
`'${module.name}@${module.version ?? '(none)'}', package.json has '${name}@${version ?? '(none)'}'${hint}`)
|
|
788
746
|
}
|
|
789
747
|
// A dependency's repo is metadata, its package.json's, held to nothing: a record with none gets it.
|
|
790
|
-
|
|
748
|
+
const repo = module.repo === undefined && rootPkg ? packageRepo(rootPkg) : undefined
|
|
749
|
+
if (repo !== undefined) this.modules.set(dir, moduleInfo({ ...module, repo }))
|
|
791
750
|
|
|
792
|
-
return { absolute, file, dir, module, closestType }
|
|
751
|
+
return { absolute, file, dir, module: this.modules.get(dir), closestType, vendored, subpackage }
|
|
793
752
|
}
|
|
794
753
|
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
if (!asResource && Bundle.isResourceFormat(format)) {
|
|
800
|
-
throw new Error(`addFile: format '${format}' requires resource: true`)
|
|
801
|
-
}
|
|
802
|
-
if (isStatFormat(format)) {
|
|
803
|
-
throw new Error(`addFile: format '${format}' is a payload-free stat record; use addFsStat`)
|
|
804
|
-
}
|
|
805
|
-
if (asResource && isEntry) {
|
|
806
|
-
throw new Error(`addFile: a resource can't be an entry (resource:true + isEntry:true)`)
|
|
754
|
+
#manifestsBelow(nmRoot) {
|
|
755
|
+
return (sub) => {
|
|
756
|
+
const candidate = resolve(this.root, nmRoot, sub, 'package.json')
|
|
757
|
+
return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
|
|
807
758
|
}
|
|
808
|
-
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// The nearest named package.json above `nmRel`, walked by the file's own path, not findPackageJSON's real one
|
|
762
|
+
// through a symlinked install. Only where `closest` is a `{"type":"module"}` marker or names a subpath.
|
|
763
|
+
#subpackageOf(nmRoot, nmRel, closest, name) {
|
|
764
|
+
if (closest.name !== undefined && !(typeof closest.name === 'string' && closest.name.startsWith(`${name}/`))) return undefined
|
|
765
|
+
const found = namedManifestOf(nmRel, this.#manifestsBelow(nmRoot))
|
|
766
|
+
if (found === undefined || !isSubpackage(found.pkg.name, name, found.dir)) return undefined
|
|
767
|
+
return { dir: found.dir, name: found.pkg.name, ...(isPackageString(found.pkg.version) ? { version: found.pkg.version } : {}) }
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// `resource: true` (legacy `isBinary`): format derived from bytes. `inferFormat: false`: the loader decides.
|
|
771
|
+
addFile(url, options = {}) {
|
|
772
|
+
this.#addFile(url, options)
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// The empty module a disabled import (`browser` field `false`) resolves to, never read from disk. -> its URL
|
|
776
|
+
addEmptyModule({ reason = 'run' } = {}) {
|
|
777
|
+
const url = this.#urlOf(EMPTY_MODULE_PATH)
|
|
778
|
+
this.#addFile(url, { source: '', format: 'commonjs', inferFormat: false, reason }, { synthetic: true })
|
|
779
|
+
return url
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
#addFile(url, { source, format, isEntry, isBinary, resource, inferFormat = true, reason = 'run', fsRead = false } = {}, { synthetic = false } = {}) {
|
|
783
|
+
const asResource = resource === true || isBinary === true
|
|
784
|
+
if (!asResource && Bundle.isResourceFormat(format)) throw new Error(`addFile: format '${format}' requires resource: true`)
|
|
785
|
+
if (isStatFormat(format)) throw new Error(`addFile: format '${format}' is a payload-free stat record; use addFsStat`)
|
|
786
|
+
if (asResource && isEntry) throw new Error(`addFile: a resource can't be an entry (resource:true + isEntry:true)`)
|
|
787
|
+
const { absolute, file, dir, module, closestType, vendored, subpackage } = this.#locateModule(url, { synthetic })
|
|
809
788
|
|
|
810
|
-
// Real content supersedes a payload-free stat record
|
|
811
|
-
// actual format instead of conflicting with 'stat:*'.
|
|
789
|
+
// Real content supersedes a payload-free stat record.
|
|
812
790
|
if (isStatFormat(this.formats.get(file))) this.formats.delete(file)
|
|
813
791
|
|
|
814
792
|
const ext = extname(file)
|
|
@@ -817,26 +795,20 @@ export class State {
|
|
|
817
795
|
}
|
|
818
796
|
|
|
819
797
|
if (!asResource && inferFormat) {
|
|
820
|
-
const inferredFormat =
|
|
798
|
+
const inferredFormat = nodeExtFormat(file) ??
|
|
821
799
|
(closestType === undefined ? undefined : ext === '.js' ? closestType : ext === '.ts' ? `${closestType}-typescript` : undefined)
|
|
822
800
|
if (inferredFormat !== undefined) {
|
|
823
|
-
// A transforming preload (
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
if (format != null && format === erasedTypeScriptFormat(inferredFormat)) format = inferredFormat
|
|
827
|
-
if (format != null) assert.equal(format, inferredFormat)
|
|
828
|
-
else format = inferredFormat
|
|
801
|
+
// A transforming preload (`--import tsx`) loads TypeScript as its post-erasure family: attest the on-disk one.
|
|
802
|
+
if (format == null || format === erasedTypeScriptFormat(inferredFormat)) format = inferredFormat
|
|
803
|
+
else assert.equal(format, inferredFormat)
|
|
829
804
|
} else if (format == null) {
|
|
830
|
-
// No `type`:
|
|
831
|
-
// loader's call, or the parent's for a sidecar) rather than re-defaulting to commonjs and mis-attesting.
|
|
805
|
+
// No `type`: one already recorded (the loader's, or a sidecar's parent's), not a commonjs re-default.
|
|
832
806
|
const variants = UNTYPED_VARIANTS[ext]
|
|
833
807
|
const known = this.formats.get(file) ?? this.#parent?.formats.get(file)
|
|
834
808
|
format = variants?.includes(known) ? known : variants?.[0]
|
|
835
809
|
} else if (ext === '.ts' && (format === 'commonjs' || format === 'module')) {
|
|
836
|
-
//
|
|
837
|
-
//
|
|
838
|
-
// Stacks can still honestly disagree about a no-`type` .ts (tsx defaults to commonjs,
|
|
839
|
-
// Node syntax-detects) -- that rightly surfaces as a format flip, not silently here.
|
|
810
|
+
// A transforming preload's post-erasure family again. Stacks can still disagree on a no-`type` .ts (tsx
|
|
811
|
+
// defaults to commonjs, Node syntax-detects): that rightly surfaces as a format flip.
|
|
840
812
|
format = `${format}-typescript`
|
|
841
813
|
}
|
|
842
814
|
}
|
|
@@ -854,8 +826,7 @@ export class State {
|
|
|
854
826
|
}
|
|
855
827
|
|
|
856
828
|
const buf = typeof source === 'string' ? Buffer.from(source) : source
|
|
857
|
-
|
|
858
|
-
if (!sourceFromDisk) assert.deepStrictEqual(this.#host.readFile(absolute), buf)
|
|
829
|
+
if (!sourceFromDisk && !synthetic) assert.deepStrictEqual(this.#host.readFile(absolute), buf)
|
|
859
830
|
|
|
860
831
|
if (asResource) {
|
|
861
832
|
const derived = isUtf8(buf) ? 'resource' : 'resource:base64'
|
|
@@ -864,45 +835,22 @@ export class State {
|
|
|
864
835
|
}
|
|
865
836
|
|
|
866
837
|
if (isEntry) this.entries.add(file)
|
|
867
|
-
const integrity = sha512integrity(
|
|
838
|
+
const integrity = sha512integrity(buf)
|
|
868
839
|
noupsert(this.hashes, file, integrity)
|
|
869
840
|
this.#filesCapturedThisRun.add(file)
|
|
870
|
-
if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it
|
|
871
|
-
//
|
|
841
|
+
if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it
|
|
842
|
+
// A null reason: the caller attributes it (includePackageJson).
|
|
872
843
|
if (this.config.bundle && reason !== null) {
|
|
873
844
|
this.#recordReason(reason, file)
|
|
874
|
-
|
|
875
|
-
if (reason === 'run') {
|
|
876
|
-
if (!fsRead) this.#runImported.add(file)
|
|
877
|
-
else if (this.#pluginAttached) this.#fsReadPostPlugin.add(file)
|
|
878
|
-
}
|
|
845
|
+
if (reason === 'run') (fsRead && this.#pluginAttached ? this.#fsReadPostPlugin : this.#runOwn).add(file)
|
|
879
846
|
}
|
|
880
847
|
const rel = relative(dir, file)
|
|
881
848
|
assert.ok(!relativeEscapes(rel))
|
|
882
849
|
|
|
883
|
-
const inAttestedZone =
|
|
884
|
-
if (this
|
|
885
|
-
assert.ok(Object.hasOwn(module.files, rel), `File not attested by the frozen lockfile: ${file}`)
|
|
886
|
-
}
|
|
850
|
+
const inAttestedZone = this.config.full || hasNodeModulesSegment(dir)
|
|
851
|
+
if (inAttestedZone) this.#assertFrozenAttests('File', file, module, rel, asResource ? this.#bundleResources : this.#bundleSources, format)
|
|
887
852
|
|
|
888
|
-
//
|
|
889
|
-
// deliberately doesn't attest workspace files.
|
|
890
|
-
if (this.config.frozenBundle && inAttestedZone) {
|
|
891
|
-
const attested = asResource ? this.#bundleResources : this.#bundleSources
|
|
892
|
-
assert.ok(attested?.has(file), `File not attested by the frozen bundle: ${file}`)
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
// `type` is usually not hash-attested, so a flip recategorizes a hash-valid file
|
|
896
|
-
// (commonjs<->module) with no hash mismatch.
|
|
897
|
-
if (this.config.frozen && this.#lockFormats !== null && format != null && inAttestedZone) {
|
|
898
|
-
this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
|
|
899
|
-
}
|
|
900
|
-
if (this.config.frozenBundle && this.#bundleFormats !== null && format != null && inAttestedZone) {
|
|
901
|
-
this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
// Catch a format flip on already-attested bytes here; at write()'s #mergedFormats it would
|
|
905
|
-
// surface only as a bare `Conflict`.
|
|
853
|
+
// Caught here: at write()'s #mergedFormats a format flip would surface only as a bare `Conflict`.
|
|
906
854
|
if (this.config.writeLockfile && this.#lockFormats !== null && format != null && inAttestedZone) {
|
|
907
855
|
const attested = this.#lockFormats.get(file)
|
|
908
856
|
// A 'stat:*' baseline is not a flip: real content this run UPGRADES the record (#mergedFormats).
|
|
@@ -912,6 +860,12 @@ export class State {
|
|
|
912
860
|
|
|
913
861
|
if (!Object.hasOwn(module.files, rel)) module.files[rel] = integrity
|
|
914
862
|
assert.equal(module.files[rel], integrity)
|
|
863
|
+
// The first identity recorded for a vendored package's or subpackage's dir stands.
|
|
864
|
+
for (const [field, entry] of [['vendored', vendored], ['subpackages', subpackage]]) {
|
|
865
|
+
if (entry === undefined || Object.hasOwn(this.modules.get(dir)[field] ?? {}, entry.dir)) continue
|
|
866
|
+
const { dir: sub, ...identity } = entry
|
|
867
|
+
this.modules.set(dir, withMetadataOf(this.modules.get(dir), { [field]: { [sub]: Object.freeze(identity) } }))
|
|
868
|
+
}
|
|
915
869
|
|
|
916
870
|
if (this.config.bundle) {
|
|
917
871
|
if (asResource) noupsert(this.resources, file, buf.toString(format === 'resource:base64' ? 'base64' : 'utf8'))
|
|
@@ -920,13 +874,11 @@ export class State {
|
|
|
920
874
|
|
|
921
875
|
if (format) noupsert(this.formats, file, format)
|
|
922
876
|
|
|
923
|
-
this.#recordExecutable(file, absolute)
|
|
877
|
+
if (!synthetic) this.#recordExecutable(file, absolute)
|
|
924
878
|
}
|
|
925
879
|
|
|
926
|
-
//
|
|
927
|
-
//
|
|
928
|
-
// A record touches only this State; a REFUTATION must reach the whole family, else
|
|
929
|
-
// #mergedExecutable's union resurrects the bit from a sibling's seeded entry.
|
|
880
|
+
// Disk wins over a seeded lockfile/bundle, but only a real observation refutes (Windows can't). A REFUTATION
|
|
881
|
+
// must reach the whole family, else #mergedExecutable's union resurrects the bit from a sibling's seed.
|
|
930
882
|
#recordExecutable(file, absolute) {
|
|
931
883
|
const executable = observeExecutable(absolute, this.#host)
|
|
932
884
|
if (executable === true) {
|
|
@@ -940,83 +892,121 @@ export class State {
|
|
|
940
892
|
for (const sidecar of root.sidecars()) sidecar.executable.delete(file)
|
|
941
893
|
}
|
|
942
894
|
|
|
943
|
-
// Public so #recordExecutable reaches them across the class brand
|
|
895
|
+
// Public, so #recordExecutable reaches them across the class brand.
|
|
944
896
|
sidecars() {
|
|
945
897
|
return this.#sidecars
|
|
946
898
|
}
|
|
947
899
|
|
|
948
|
-
//
|
|
949
|
-
// is code, null defers to the loader, and undefined (or a binary plist, whose bytes can't be the
|
|
950
|
-
// UTF-8 code its format implies) is a resource IFF allowlisted, else THROW -- an undeclared file
|
|
951
|
-
// would widen the attested set.
|
|
900
|
+
// An undeclared file THROWS: it would widen the attested set.
|
|
952
901
|
addFsFile(url, source) {
|
|
953
902
|
assert.ok(Buffer.isBuffer(source), 'addFsFile requires a Buffer source')
|
|
954
903
|
const path = fileURLToPath(url)
|
|
955
|
-
const
|
|
956
|
-
if (
|
|
957
|
-
this.addFile(url, { source, format: format ?? undefined, inferFormat: false, fsRead: true })
|
|
958
|
-
} else if (this.config.resources.has(pathExt(path) || basename(path).toLowerCase())) {
|
|
959
|
-
this.addFile(url, { source, resource: true, fsRead: true })
|
|
960
|
-
} else {
|
|
904
|
+
const options = this.#readFileOptions(path, source)
|
|
905
|
+
if (options === null) {
|
|
961
906
|
throw new Error(`addFsFile: ${path} is neither code nor a declared resource; add its extension or filename to the resources allowlist or stop reading it`)
|
|
962
907
|
}
|
|
908
|
+
this.addFile(url, { source, ...options, fsRead: true })
|
|
963
909
|
}
|
|
964
910
|
|
|
965
|
-
//
|
|
966
|
-
// '
|
|
911
|
+
// For a file read rather than loaded: a concrete format is code, null defers to the loader, and undefined (or a
|
|
912
|
+
// binary plist, which can't be the UTF-8 code its format implies) is a resource IFF in `resources`, else null.
|
|
913
|
+
#readFileOptions(path, source, resources = this.config.resources) {
|
|
914
|
+
const format = classifyFormat(path, { content: source })
|
|
915
|
+
if (format !== undefined && !isBinaryPlist(path, source)) return { format: format ?? undefined, inferFormat: false }
|
|
916
|
+
if (inResources(path, resources)) return { resource: true }
|
|
917
|
+
return null
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// Each file `extra` names is carried as an fs read is, unless the run captured it itself. The whole set is
|
|
921
|
+
// validated first, naming every offender at once; `outputs` never go in.
|
|
922
|
+
includeExtra({ outputs = this.#writeTargets(), resources = this.config.resources } = {}) {
|
|
923
|
+
if (this.config.extra.length === 0) return
|
|
924
|
+
// Resolved as write() writes them: a relative one from the cwd.
|
|
925
|
+
const written = new Set(outputs.map((file) => toPosix(relative(this.root, resolve(file)))))
|
|
926
|
+
const { files } = expandExtra(this.root, this.config.extra, written, this.#host)
|
|
927
|
+
const realRoot = this.#host.realpath(this.root)
|
|
928
|
+
const missing = []
|
|
929
|
+
const undeclared = []
|
|
930
|
+
const nonUtf8 = []
|
|
931
|
+
const toAdd = []
|
|
932
|
+
const captured = []
|
|
933
|
+
for (const [rel, stats] of files) {
|
|
934
|
+
if (stats === undefined) {
|
|
935
|
+
missing.push(rel)
|
|
936
|
+
continue
|
|
937
|
+
}
|
|
938
|
+
assertRealPathWithinBase(realRoot, this.root, rel, this.#host)
|
|
939
|
+
const absolute = resolve(this.root, rel)
|
|
940
|
+
const url = pathToFileURL(absolute).toString()
|
|
941
|
+
// A node_modules scope leaves the project's own files out, as it does the run's.
|
|
942
|
+
if (!inScope(this, url)) continue
|
|
943
|
+
// The run's record of a file it captured stands, and `extra` is one more consumer of it.
|
|
944
|
+
const file = this.#canonicalFile(url)
|
|
945
|
+
if (this.#filesCapturedThisRun.has(file)) {
|
|
946
|
+
captured.push(file)
|
|
947
|
+
continue
|
|
948
|
+
}
|
|
949
|
+
const source = this.#host.readFile(absolute)
|
|
950
|
+
const options = this.#readFileOptions(absolute, source, resources)
|
|
951
|
+
if (options === null) undeclared.push(rel)
|
|
952
|
+
else if (!options.resource && !isUtf8(source)) nonUtf8.push({ rel, format: options.format })
|
|
953
|
+
else toAdd.push({ url, source, options })
|
|
954
|
+
}
|
|
955
|
+
if (missing.length + undeclared.length + nonUtf8.length > 0) throw validationError({ missing, undeclared, nonUtf8 }, 'extra')
|
|
956
|
+
for (const { url, source, options } of toAdd) this.addFile(url, { source, ...options, reason: 'extra' })
|
|
957
|
+
if (this.config.bundle) for (const file of captured) this.#recordReason('extra', file)
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
get #lockPath() {
|
|
961
|
+
return this.config.lockFile || join(this.root, LOCK_FILE)
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
get #bundlePath() {
|
|
965
|
+
return this.config.bundleFile || join(this.root, BUNDLE_FILE)
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// A sidecar never writes the lockfile.
|
|
969
|
+
#writeTargets() {
|
|
970
|
+
const lock = this.config.writeLockfile && !this.#parent ? [this.#lockPath] : []
|
|
971
|
+
const bundles = this.config.writeBundle ? [this.#bundlePath, this.config.resourcesBundleFile] : []
|
|
972
|
+
return [...lock, ...bundles].filter((file) => file !== undefined)
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
// The listing is SORTED, for reproducibility.
|
|
967
976
|
addFsDir(url, names) {
|
|
968
|
-
assert.ok(Array.isArray(names) && names.every((n) => typeof n === 'string'),
|
|
969
|
-
'addFsDir requires an array of string names')
|
|
977
|
+
assert.ok(Array.isArray(names) && names.every((n) => typeof n === 'string'), 'addFsDir requires an array of string names')
|
|
970
978
|
const { file, dir, module } = this.#locateModule(url, { directory: true })
|
|
971
|
-
// As in addFile: a real record supersedes a payload-free stat record.
|
|
972
979
|
if (isStatFormat(this.formats.get(file))) this.formats.delete(file)
|
|
973
980
|
const content = JSON.stringify(names.toSorted())
|
|
974
|
-
const format = 'directory'
|
|
975
981
|
const integrity = sha512integrity(content)
|
|
976
982
|
noupsert(this.hashes, file, integrity)
|
|
977
|
-
if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it
|
|
983
|
+
if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it
|
|
978
984
|
if (this.config.bundle) this.#recordReason('run', file)
|
|
979
985
|
const rel = relative(dir, file)
|
|
980
986
|
assert.ok(!relativeEscapes(rel))
|
|
981
987
|
|
|
982
|
-
|
|
983
|
-
if (this.config.frozen && inAttestedZone) {
|
|
984
|
-
assert.ok(Object.hasOwn(module.files, rel), `Directory not attested by the frozen lockfile: ${file}`)
|
|
985
|
-
}
|
|
986
|
-
if (this.config.frozenBundle && inAttestedZone) {
|
|
987
|
-
assert.ok(this.#bundleResources?.has(file), `Directory not attested by the frozen bundle: ${file}`)
|
|
988
|
-
}
|
|
989
|
-
if (this.config.frozen && this.#lockFormats !== null && inAttestedZone) {
|
|
990
|
-
this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
|
|
991
|
-
}
|
|
992
|
-
if (this.config.frozenBundle && this.#bundleFormats !== null && inAttestedZone) {
|
|
993
|
-
this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
|
|
994
|
-
}
|
|
988
|
+
if (this.config.full || hasNodeModulesSegment(dir)) this.#assertFrozenAttests('Directory', file, module, rel, this.#bundleResources, 'directory')
|
|
995
989
|
|
|
996
990
|
if (!Object.hasOwn(module.files, rel)) module.files[rel] = integrity
|
|
997
991
|
assert.equal(module.files[rel], integrity)
|
|
998
992
|
if (this.config.bundle) noupsert(this.resources, file, content)
|
|
999
|
-
noupsert(this.formats, file,
|
|
1000
|
-
//
|
|
1001
|
-
// any bit a prior content record left on this path.
|
|
993
|
+
noupsert(this.formats, file, 'directory')
|
|
994
|
+
// Both artifacts' parsers refuse a listing in `executable`.
|
|
1002
995
|
this.executable.delete(file)
|
|
1003
996
|
}
|
|
1004
997
|
|
|
1005
|
-
//
|
|
1006
|
-
// no bytes/hash/module-files entry), so getFsStat answers isFile()/isDirectory() at load. Skipped
|
|
1007
|
-
// when a content record already exists; upsertFormat lets a real format win, a kind flip is fatal.
|
|
998
|
+
// A PAYLOAD-FREE record (no bytes/hash/module-files entry), so getFsStat answers at load. A real format wins.
|
|
1008
999
|
addFsStat(url, kind) {
|
|
1009
1000
|
assert.ok(kind === 'file' || kind === 'directory', `addFsStat: unsupported kind '${kind}'`)
|
|
1010
1001
|
const file = this.#canonicalFile(url)
|
|
1011
|
-
if (this.hashes.has(file) || this
|
|
1002
|
+
if (this.hashes.has(file) || this.#bundled(file)) return
|
|
1003
|
+
this.#checkUnreadPath(file)
|
|
1012
1004
|
upsertFormat(this.formats, file, `stat:${kind}`)
|
|
1013
|
-
// Only
|
|
1014
|
-
// format, and forwarding THAT would ship an entry mergeShard's stat replay ignores.
|
|
1005
|
+
// Only where a stat record landed: one yielded to a real format is an entry mergeShard's stat replay ignores.
|
|
1015
1006
|
if (this.config.childProcess && isStatFormat(this.formats.get(file))) this.#observed.add(file)
|
|
1016
1007
|
}
|
|
1017
1008
|
|
|
1018
|
-
//
|
|
1019
|
-
// falls back to disk) or a captured directory. Served by presence, not format.
|
|
1009
|
+
// Undefined (the hook falls back to disk) when uncaptured or a directory. Served by presence, not format.
|
|
1020
1010
|
getFsFile(url) {
|
|
1021
1011
|
if (!this.hasFsFileContent(url)) return undefined
|
|
1022
1012
|
const { source } = this.getFile(url)
|
|
@@ -1031,58 +1021,45 @@ export class State {
|
|
|
1031
1021
|
return names
|
|
1032
1022
|
}
|
|
1033
1023
|
|
|
1034
|
-
//
|
|
1024
|
+
// Built once, at load: stat records prove existence too.
|
|
1035
1025
|
#impliedDirs() {
|
|
1036
1026
|
if (this.#impliedDirIndex) return this.#impliedDirIndex
|
|
1037
|
-
const dirs = new Set(['.'])
|
|
1038
|
-
const
|
|
1027
|
+
const dirs = (this.#impliedDirIndex = new Set(['.']))
|
|
1028
|
+
const stats = this.formats.entries().filter(([, format]) => isStatFormat(format)).map(([key]) => key)
|
|
1029
|
+
for (const key of [...this.sources.keys(), ...this.resources.keys(), ...stats]) {
|
|
1039
1030
|
const parts = key.split('/')
|
|
1040
1031
|
for (let i = 1; i < parts.length; i++) dirs.add(parts.slice(0, i).join('/'))
|
|
1041
1032
|
}
|
|
1042
|
-
for (const key of this.sources.keys()) add(key)
|
|
1043
|
-
for (const key of this.resources.keys()) add(key)
|
|
1044
|
-
// Stat records prove existence too, so they imply their ancestor dirs.
|
|
1045
|
-
for (const [key, format] of this.formats) if (isStatFormat(format)) add(key)
|
|
1046
|
-
this.#impliedDirIndex = dirs
|
|
1047
1033
|
return dirs
|
|
1048
1034
|
}
|
|
1049
1035
|
|
|
1050
|
-
//
|
|
1051
|
-
// shim falls back to disk). Order matters: 'directory' first, content before stat records, implied
|
|
1052
|
-
// dirs last. NB: 'file' means "recorded", NOT "bytes serveable" (use hasFsFileContent).
|
|
1036
|
+
// Undefined: uncaptured, the shim falls back to disk. 'file' means "recorded", NOT "bytes serveable".
|
|
1053
1037
|
getFsStat(url) {
|
|
1054
1038
|
const file = this.#canonicalFileMaybe(url)
|
|
1055
1039
|
if (file === undefined) return undefined
|
|
1056
1040
|
const format = this.formats.get(file)
|
|
1057
1041
|
if (format === 'directory') return 'directory'
|
|
1058
|
-
if (this
|
|
1042
|
+
if (this.#bundled(file) || format === 'stat:file') return 'file'
|
|
1059
1043
|
if (format === 'stat:directory' || this.#impliedDirs().has(file)) return 'directory'
|
|
1060
1044
|
return undefined
|
|
1061
1045
|
}
|
|
1062
1046
|
|
|
1063
|
-
// True when the bundle carries actual BYTE content for `url` (not a directory/stat-only record).
|
|
1064
1047
|
hasFsFileContent(url) {
|
|
1065
1048
|
const file = this.#canonicalFileMaybe(url)
|
|
1066
|
-
return file !== undefined && this.formats.get(file) !== 'directory' &&
|
|
1049
|
+
return file !== undefined && this.formats.get(file) !== 'directory' && this.#bundled(file)
|
|
1067
1050
|
}
|
|
1068
1051
|
|
|
1069
|
-
// --fs
|
|
1070
|
-
//
|
|
1052
|
+
// Capture skips these, else --fs re-records the bundler's module graph into the main bundle. Not getFsStat:
|
|
1053
|
+
// skipping an implied dir or stat-only record would leave a real read unattested.
|
|
1071
1054
|
attestedBySidecar(url) {
|
|
1072
|
-
|
|
1073
|
-
// leave a real read unattested.
|
|
1074
|
-
for (const s of this.#sidecars) if (s.hasFsFileContent(url)) return true
|
|
1075
|
-
return false
|
|
1055
|
+
return this.#sidecars.values().some((s) => s.hasFsFileContent(url))
|
|
1076
1056
|
}
|
|
1077
1057
|
|
|
1078
|
-
//
|
|
1079
|
-
// sidecars only capture.
|
|
1058
|
+
// Write-mode sidecars only capture.
|
|
1080
1059
|
#fromFamily(method, url) {
|
|
1081
|
-
const
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
const v = s[method](url)
|
|
1085
|
-
if (v !== undefined) return v
|
|
1060
|
+
for (const state of [this, ...this.#readSidecars]) {
|
|
1061
|
+
const value = state[method](url)
|
|
1062
|
+
if (value !== undefined) return value
|
|
1086
1063
|
}
|
|
1087
1064
|
return undefined
|
|
1088
1065
|
}
|
|
@@ -1100,37 +1077,26 @@ export class State {
|
|
|
1100
1077
|
}
|
|
1101
1078
|
|
|
1102
1079
|
getFile(url) {
|
|
1103
|
-
// Wrap #canonicalFile so a URL escaping state.root gives a contextual error, not a bare assert.
|
|
1104
1080
|
let file
|
|
1105
1081
|
try { file = this.#canonicalFile(url) }
|
|
1106
1082
|
catch (cause) { throw new Error(`stasis: file is outside the project root: ${url}`, { cause }) }
|
|
1107
1083
|
const format = this.formats.get(file) // might be undefined e.g. for some bundlers
|
|
1108
|
-
//
|
|
1109
|
-
let source
|
|
1110
|
-
if (
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
} else {
|
|
1115
|
-
source = this.sources.get(file)
|
|
1116
|
-
if (source === undefined) {
|
|
1117
|
-
// Stat-record case: the bundle knows the path but not its bytes -- "not attested" would misdiagnose it.
|
|
1118
|
-
if (isStatFormat(format)) {
|
|
1119
|
-
throw new Error(
|
|
1120
|
-
`stasis: only a payload-free stat record ('${format}') is attested for ${url} -- ` +
|
|
1121
|
-
`the capture stat'd this path but never read or imported it, so the bundle has no bytes to serve`
|
|
1122
|
-
)
|
|
1123
|
-
}
|
|
1124
|
-
throw new Error(`stasis: file not attested in bundle: ${url}`)
|
|
1084
|
+
// The fail-closed gate.
|
|
1085
|
+
let source = (Bundle.isResourceFormat(format) ? this.resources : this.sources).get(file)
|
|
1086
|
+
if (source === undefined) {
|
|
1087
|
+
if (isStatFormat(format)) {
|
|
1088
|
+
throw new Error(`stasis: only a payload-free stat record ('${format}') is attested for ${url} -- the capture ` +
|
|
1089
|
+
"stat'd this path but never read or imported it, so the bundle has no bytes to serve")
|
|
1125
1090
|
}
|
|
1091
|
+
throw new Error(`stasis: file not attested in bundle: ${url}`)
|
|
1126
1092
|
}
|
|
1127
|
-
|
|
1093
|
+
if (format === 'resource:base64') source = Buffer.from(source, 'base64')
|
|
1094
|
+
// Without a lockfile the bundle is self-attesting.
|
|
1128
1095
|
if (this.config.useLockfile) assert.equal(this.hashes.get(file), sha512integrity(source))
|
|
1129
1096
|
return { source, format }
|
|
1130
1097
|
}
|
|
1131
1098
|
|
|
1132
|
-
//
|
|
1133
|
-
// stat'd require.resolve target isn't refused by the resolve hook's format gate; getFile does NOT mask.
|
|
1099
|
+
// Masks 'stat:*' (existence, not parsing), so the resolve hook's format gate passes a stat'd target; getFile doesn't.
|
|
1134
1100
|
#loaderFormat(file) {
|
|
1135
1101
|
const format = this.formats.get(file)
|
|
1136
1102
|
return isStatFormat(format) ? undefined : format
|
|
@@ -1150,9 +1116,7 @@ export class State {
|
|
|
1150
1116
|
return `${cond} (with: ${JSON.stringify(sorted)})`
|
|
1151
1117
|
}
|
|
1152
1118
|
|
|
1153
|
-
// An
|
|
1154
|
-
// machine-specific lockfile key, so renormalize in-root ones against the IMPORTING FILE's dir.
|
|
1155
|
-
// Must be applied identically in addImport and getImport so keys written and queried agree.
|
|
1119
|
+
// An ABSOLUTE in-root specifier would be a machine-specific key: renormalized against the IMPORTING FILE's dir.
|
|
1156
1120
|
#canonicalSpecifier(parentURL, specifier) {
|
|
1157
1121
|
if (typeof specifier !== 'string') return specifier
|
|
1158
1122
|
const path = specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier
|
|
@@ -1165,85 +1129,110 @@ export class State {
|
|
|
1165
1129
|
|
|
1166
1130
|
addImport(parentURL, specifier, url, { conditions = '*', format, importAttributes } = {}) {
|
|
1167
1131
|
if (conditions !== '*') assert.ok(Array.isArray(conditions))
|
|
1168
|
-
|
|
1169
|
-
if (this.#requireConditions === null && Array.isArray(conditions) &&
|
|
1170
|
-
conditions.includes('require') && !conditions.includes('import')) {
|
|
1171
|
-
this.#requireConditions = conditions
|
|
1172
|
-
}
|
|
1132
|
+
if (this.#requireConditions === null && resolutionContext(conditions) === 'require') this.#requireConditions = conditions
|
|
1173
1133
|
assert.ok(parentURL, 'addImport requires a parent (entries go through addFile)')
|
|
1174
1134
|
const parent = this.#canonicalFile(parentURL)
|
|
1175
1135
|
const file = this.#canonicalFile(url)
|
|
1176
1136
|
specifier = this.#canonicalSpecifier(parentURL, specifier)
|
|
1177
1137
|
const key = this.#conditionsKey(conditions, importAttributes)
|
|
1178
1138
|
|
|
1179
|
-
//
|
|
1180
|
-
// another attested file. Unknown edges are tolerated only for workspace parents in nm scope.
|
|
1139
|
+
// Hashes can't see a redirect to another attested file. Workspace parents in nm scope may be unknown.
|
|
1181
1140
|
const tolerateUnknown = !this.config.full && !hasNodeModulesSegment(parent)
|
|
1182
1141
|
if (this.config.frozen && this.#lockImports !== null) {
|
|
1183
1142
|
this.#assertAttestedResolution(this.#lockImports, key, parent, specifier, file, { what: 'observed', source: 'lockfile', tolerateUnknown })
|
|
1184
1143
|
}
|
|
1185
|
-
if (this.config.frozenBundle
|
|
1144
|
+
if (this.config.frozenBundle) {
|
|
1186
1145
|
this.#assertAttestedResolution(this.#bundleImports, key, parent, specifier, file, { what: 'observed', source: 'frozen bundle', tolerateUnknown })
|
|
1187
1146
|
}
|
|
1188
1147
|
|
|
1189
1148
|
noupsert(nestedMap(nestedMap(this.imports, key), parent), specifier, file)
|
|
1190
|
-
//
|
|
1191
|
-
if (format)
|
|
1149
|
+
// A real format replaces a weak 'stat:*' record.
|
|
1150
|
+
if (format) {
|
|
1151
|
+
this.#checkUnreadPath(file)
|
|
1152
|
+
upsertFormat(this.formats, file, format)
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
// A resolution a bundler made itself (Metro's) must be the attested edge, `platform`'s where per-platform.
|
|
1157
|
+
assertImport(parentURL, specifier, url, { conditions = '*', importAttributes, platform: targetPlatform } = {}) {
|
|
1158
|
+
assert.ok(this.config.loadBundle, 'assertImport checks a resolution against a loaded bundle (bundle=load)')
|
|
1159
|
+
if (conditions !== '*') assert.ok(Array.isArray(conditions))
|
|
1160
|
+
assert.ok(parentURL, 'assertImport requires a parent')
|
|
1161
|
+
const parent = this.#canonicalFile(parentURL)
|
|
1162
|
+
const file = this.#canonicalFile(url)
|
|
1163
|
+
specifier = this.#canonicalSpecifier(parentURL, specifier)
|
|
1164
|
+
const key = this.#conditionsKey(conditions, importAttributes)
|
|
1165
|
+
const tolerateUnknown = !this.config.full && !hasNodeModulesSegment(parent)
|
|
1166
|
+
this.#assertAttestedResolution(this.imports, key, parent, specifier, file, { what: 'observed', source: 'bundle', tolerateUnknown, targetPlatform })
|
|
1192
1167
|
}
|
|
1193
1168
|
|
|
1194
|
-
getImport(parentURL, specifier, { conditions = '*', importAttributes } = {}) {
|
|
1169
|
+
getImport(parentURL, specifier, { conditions = '*', importAttributes, platform } = {}) {
|
|
1195
1170
|
if (conditions !== '*') assert.ok(Array.isArray(conditions))
|
|
1196
|
-
assert.ok(parentURL, 'getImport requires a parent')
|
|
1171
|
+
assert.ok(parentURL, 'getImport requires a parent')
|
|
1197
1172
|
const parent = this.#canonicalFile(parentURL)
|
|
1198
1173
|
specifier = this.#canonicalSpecifier(parentURL, specifier)
|
|
1199
1174
|
const key = this.#conditionsKey(conditions, importAttributes)
|
|
1200
|
-
// Static bundles store edges under '*' (Node's condition set is unpredictable); the
|
|
1201
|
-
// loader records precise conditions, so the specific lookup wins.
|
|
1175
|
+
// Static bundles store edges under '*' (Node's condition set is unpredictable); the precise key wins.
|
|
1202
1176
|
let file = this.imports.get(key)?.get(parent)?.get(specifier)
|
|
1203
1177
|
if (file === undefined && key !== '*') file = this.imports.get('*')?.get(parent)?.get(specifier)
|
|
1204
1178
|
if (file === undefined) {
|
|
1205
|
-
// The edge may be
|
|
1179
|
+
// The edge may be under another conditions bucket.
|
|
1206
1180
|
if (this.config.loadBundle) {
|
|
1207
|
-
const abs = this.resolveBundled(parentURL, specifier)
|
|
1181
|
+
const abs = this.resolveBundled(parentURL, specifier, { conditions })
|
|
1208
1182
|
if (abs !== undefined) return { url: pathToFileURL(abs).toString(), format: this.#loaderFormat(relative(this.root, abs)) }
|
|
1209
1183
|
}
|
|
1210
|
-
//
|
|
1211
|
-
// code, which ERR_ASSERTION escapes.
|
|
1184
|
+
// Not a bare assert: dynamic-import callers guard on ERR_MODULE_NOT_FOUND.
|
|
1212
1185
|
throw Object.assign(new Error(`Cannot find module '${specifier}' imported from ${parent}`), { code: 'ERR_MODULE_NOT_FOUND' })
|
|
1213
1186
|
}
|
|
1214
|
-
// A --metro per-platform edge is a { platform: file } Map; plain bundle=load has no platform
|
|
1215
|
-
// context to pick one, so fail closed rather than feed a Map where a path is expected.
|
|
1187
|
+
// A --metro per-platform edge is a { platform: file } Map; plain bundle=load has no platform to pick by.
|
|
1216
1188
|
if (typeof file !== 'string') {
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1189
|
+
if (!file.has(platform)) {
|
|
1190
|
+
const message = platform === undefined
|
|
1191
|
+
? `Resolution of '${specifier}' from ${parent} is platform-specific (a --metro multi-platform bundle, which is Metro's to build); plain node has no platform to pick one by`
|
|
1192
|
+
: `Resolution of '${specifier}' from ${parent} is platform-specific and records no '${platform}' target (only ${[...file.keys()].join(', ')}): the bundle wasn't made for '${platform}'`
|
|
1193
|
+
throw Object.assign(new Error(message), { code: 'ERR_STASIS_PLATFORM_SPECIFIC' })
|
|
1194
|
+
}
|
|
1195
|
+
file = file.get(platform)
|
|
1220
1196
|
}
|
|
1221
|
-
return { url:
|
|
1197
|
+
return { url: this.#urlOf(file), format: this.#loaderFormat(file) }
|
|
1222
1198
|
}
|
|
1223
1199
|
|
|
1224
|
-
//
|
|
1225
|
-
//
|
|
1226
|
-
|
|
1227
|
-
resolveBundled(parentURL, specifier) {
|
|
1200
|
+
// A CJS require() target as a bundled path, or undefined to defer to Node. Every conditions bucket matches,
|
|
1201
|
+
// but where conditions decide (readsConditions), the other context's keys are another resolution.
|
|
1202
|
+
resolveBundled(parentURL, specifier, { conditions = '*' } = {}) {
|
|
1228
1203
|
const parent = this.#canonicalFileMaybe(parentURL)
|
|
1229
1204
|
if (parent === undefined) return undefined
|
|
1230
1205
|
const spec = this.#canonicalSpecifier(parentURL, specifier)
|
|
1206
|
+
const context = resolutionContext(conditions)
|
|
1207
|
+
// Node's own context's set is stable, the rest varying by flag; a relative edge resolves alike under either,
|
|
1208
|
+
// as a capture's replay can ask it (tsx's CJS lane does).
|
|
1209
|
+
const contextual = context !== null && readsConditions(spec)
|
|
1231
1210
|
const matches = new Set()
|
|
1232
|
-
|
|
1211
|
+
const ownContext = new Set()
|
|
1212
|
+
for (const [key, byParent] of this.imports) {
|
|
1233
1213
|
const file = byParent.get(parent)?.get(spec)
|
|
1234
|
-
if (file
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1214
|
+
if (file === undefined) continue
|
|
1215
|
+
const keyContext = resolutionContext(key)
|
|
1216
|
+
if (contextual && keyContext !== null && keyContext !== context) continue
|
|
1217
|
+
matches.add(file)
|
|
1218
|
+
if (context !== null && keyContext === context) ownContext.add(file)
|
|
1219
|
+
}
|
|
1220
|
+
// Divergent buckets are settled by the lookup's own context where those agree, else defer.
|
|
1221
|
+
const candidates = matches.size === 1 ? matches : ownContext
|
|
1222
|
+
if (candidates.size !== 1) return undefined
|
|
1223
|
+
const [only] = candidates
|
|
1224
|
+
// A per-platform Map (--metro) has no single path.
|
|
1239
1225
|
return typeof only === 'string' ? resolve(this.root, only) : undefined
|
|
1240
1226
|
}
|
|
1241
1227
|
|
|
1242
|
-
|
|
1243
|
-
|
|
1228
|
+
get #contributors() {
|
|
1229
|
+
return [this, ...this.#sidecars]
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
// So a partial lock=add run extends without dropping edges.
|
|
1244
1233
|
#mergedImports() {
|
|
1245
1234
|
const merged = new Map()
|
|
1246
|
-
const
|
|
1235
|
+
for (const imports of [this.#lockImports ?? [], ...this.#contributors.map((state) => state.imports)]) {
|
|
1247
1236
|
for (const [conditions, byParent] of imports) {
|
|
1248
1237
|
const mergedParents = nestedMap(merged, conditions)
|
|
1249
1238
|
for (const [parent, specifiers] of byParent) {
|
|
@@ -1252,44 +1241,27 @@ export class State {
|
|
|
1252
1241
|
}
|
|
1253
1242
|
}
|
|
1254
1243
|
}
|
|
1255
|
-
if (this.#lockImports) mergeIn(this.#lockImports)
|
|
1256
|
-
mergeIn(this.imports)
|
|
1257
|
-
// Write-mode sidecars too, so the lockfile attests their edges for frozen verify.
|
|
1258
|
-
for (const sidecar of this.#sidecars) mergeIn(sidecar.imports)
|
|
1259
1244
|
return merged
|
|
1260
1245
|
}
|
|
1261
1246
|
|
|
1262
|
-
//
|
|
1263
|
-
// except that a weak 'stat:*' is UPGRADED by real content; two real formats stay fatal.
|
|
1247
|
+
// Append-only like #mergedImports, except that a weak 'stat:*' is UPGRADED by real content.
|
|
1264
1248
|
#mergedFormats() {
|
|
1265
1249
|
const merged = new Map(this.#lockFormats ?? [])
|
|
1266
|
-
for (const
|
|
1267
|
-
|
|
1268
|
-
for (const [file, format] of sidecar.formats) upsertFormat(merged, file, format)
|
|
1269
|
-
}
|
|
1270
|
-
// A payload-free 'stat:*' must never survive on a file that now carries a content hash: a stat
|
|
1271
|
-
// entry re-seeded from #lockFormats is beyond addFile's delete, and an ambiguous .js/.ts yields
|
|
1272
|
-
// no real format to override it above.
|
|
1273
|
-
for (const [file, format] of merged) {
|
|
1274
|
-
if (isStatFormat(format) && this.hashes.has(file)) merged.delete(file)
|
|
1250
|
+
for (const { formats } of this.#contributors) {
|
|
1251
|
+
for (const [file, format] of formats) upsertFormat(merged, file, format)
|
|
1275
1252
|
}
|
|
1253
|
+
// Never on a file carrying a content hash: one re-seeded from #lockFormats is beyond addFile's delete,
|
|
1254
|
+
// and an ambiguous .js/.ts yields no real format to override it.
|
|
1255
|
+
for (const [file, format] of merged) if (isStatFormat(format) && this.hashes.has(file)) merged.delete(file)
|
|
1276
1256
|
return merged
|
|
1277
1257
|
}
|
|
1278
1258
|
|
|
1279
|
-
//
|
|
1280
|
-
// scope to the keys it will SERIALIZE: a non-full-scope lockfile drops its workspace buckets, and
|
|
1281
|
-
// an entry for a dropped bucket writes a lockfile Lockfile.parse then refuses.
|
|
1259
|
+
// Narrowed to the keys the lockfile SERIALIZES: an entry for a dropped bucket would be refused at parse.
|
|
1282
1260
|
#mergedExecutable(formats) {
|
|
1283
|
-
const all = new Set(this.executable)
|
|
1284
|
-
for (const sidecar of this.#sidecars) for (const file of sidecar.executable) all.add(file)
|
|
1261
|
+
const all = new Set(this.#contributors.flatMap((state) => [...state.executable]))
|
|
1285
1262
|
return narrowExecutable(all, { modules: this.modules, formats, scope: this.config.scope })
|
|
1286
1263
|
}
|
|
1287
1264
|
|
|
1288
|
-
// The executable list ONE bundle declares, narrowed to that half's own buckets and formats.
|
|
1289
|
-
#bundleExecutable(modules, formats) {
|
|
1290
|
-
return narrowExecutable(this.executable, { modules, formats, scope: this.config.scope })
|
|
1291
|
-
}
|
|
1292
|
-
|
|
1293
1265
|
get lockfile() {
|
|
1294
1266
|
const formats = this.#mergedFormats()
|
|
1295
1267
|
return new Lockfile({
|
|
@@ -1306,7 +1278,6 @@ export class State {
|
|
|
1306
1278
|
return this.lockfile.serialize()
|
|
1307
1279
|
}
|
|
1308
1280
|
|
|
1309
|
-
// Pair each module's recorded file list with content from perFile, dropping modules with none.
|
|
1310
1281
|
#bundleModules(perFile) {
|
|
1311
1282
|
const modules = new Map()
|
|
1312
1283
|
for (const [dir, info] of this.modules) {
|
|
@@ -1320,85 +1291,72 @@ export class State {
|
|
|
1320
1291
|
return modules
|
|
1321
1292
|
}
|
|
1322
1293
|
|
|
1323
|
-
// Files fs-READ after this point
|
|
1324
|
-
// #bundleReason). Public, cross-copy.
|
|
1294
|
+
// Files fs-READ after this point for a plugin are the plugin's, not run's (#reasonClaims). Public, cross-copy.
|
|
1325
1295
|
markPluginAttached() {
|
|
1326
1296
|
this.#pluginAttached = true
|
|
1327
1297
|
}
|
|
1328
1298
|
|
|
1329
1299
|
#recordReason(reason, file) {
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1300
|
+
upsert(this.#reasonFiles, reason, () => new Set()).add(file)
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
// #reasonFiles less what run only fs-READ after a plugin attached that a plugin bundles. `extra` bundles nothing.
|
|
1304
|
+
#reasonClaims() {
|
|
1305
|
+
const isPlugin = (who) => who !== 'run' && who !== 'extra'
|
|
1306
|
+
const pluginFiles = new Set(this.#reasonFiles.entries().filter(([who]) => isPlugin(who)).flatMap(([, recorded]) => recorded))
|
|
1307
|
+
const fsReadOnly = (file) => this.#fsReadPostPlugin.has(file) && !this.#runOwn.has(file)
|
|
1308
|
+
const claimOf = (who, recorded) => (who === 'run' ? [...recorded].filter((file) => !(fsReadOnly(file) && pluginFiles.has(file))) : recorded)
|
|
1309
|
+
const claims = new Map(this.#reasonFiles.entries().map(([who, recorded]) => [who, new Set(claimOf(who, recorded))]))
|
|
1310
|
+
const run = claims.get('run')
|
|
1311
|
+
if (run === undefined || pluginFiles.size === 0 || this.#fsReadPostPlugin.size === 0) return claims
|
|
1312
|
+
// A package.json fs-read post-plugin, of a package a plugin bundles, is one a bundler reads to resolve but
|
|
1313
|
+
// never bundles: that plugin's too, and run's only where run has its own claim on it or another file.
|
|
1314
|
+
for (const [dir, module] of this.modules) {
|
|
1315
|
+
const rels = Object.keys(module.files)
|
|
1316
|
+
const read = new Set(rels.filter((rel) => basename(rel) === 'package.json').map((rel) => moduleFileKey(dir, rel)).filter((file) => this.#fsReadPostPlugin.has(file) && !pluginFiles.has(file)))
|
|
1317
|
+
if (read.size === 0) continue
|
|
1318
|
+
const files = rels.map((rel) => moduleFileKey(dir, rel)).filter((file) => !read.has(file))
|
|
1319
|
+
const plugins = [...claims].filter(([who, claimed]) => isPlugin(who) && files.some((file) => claimed.has(file))).map(([, claimed]) => claimed)
|
|
1320
|
+
if (plugins.length === 0) continue // no plugin bundles the package: run's own read
|
|
1321
|
+
const runClaimsPackage = files.some((file) => run.has(file))
|
|
1322
|
+
for (const file of read) {
|
|
1323
|
+
for (const claimed of plugins) claimed.add(file)
|
|
1324
|
+
if (fsReadOnly(file) && !runClaimsPackage) run.delete(file)
|
|
1325
|
+
}
|
|
1341
1326
|
}
|
|
1327
|
+
return claims
|
|
1342
1328
|
}
|
|
1343
1329
|
|
|
1344
|
-
//
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
const inBundle = bundledFiles instanceof Set ? bundledFiles : new Set(bundledFiles)
|
|
1348
|
-
// A file run merely fs-READ post-plugin that a plugin also bundles is the plugin's, not run's.
|
|
1349
|
-
const pluginFiles = new Set()
|
|
1350
|
-
for (const [who, recorded] of this.#reasonFiles) {
|
|
1351
|
-
if (who !== 'run') for (const file of recorded) pluginFiles.add(file)
|
|
1352
|
-
}
|
|
1353
|
-
const runOverclaims = (file) =>
|
|
1354
|
-
this.#fsReadPostPlugin.has(file) && !this.#runImported.has(file) && pluginFiles.has(file)
|
|
1355
|
-
|
|
1330
|
+
// Informational, never attested.
|
|
1331
|
+
#bundleReason(contents) {
|
|
1332
|
+
const claims = this.#reasonClaims()
|
|
1356
1333
|
const reason = {}
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
const files = [...this.#reasonFiles.get(who)].filter((file) => inBundle.has(file) && !(who === 'run' && runOverclaims(file)))
|
|
1362
|
-
if (files.length > 0) {
|
|
1363
|
-
reason[who] = files.toSorted(sortPaths)
|
|
1364
|
-
consumers += 1
|
|
1365
|
-
}
|
|
1334
|
+
// Sorted: else #emitBundle's compare-and-skip sees spurious diffs.
|
|
1335
|
+
for (const who of [...claims.keys()].toSorted()) {
|
|
1336
|
+
const files = [...claims.get(who)].filter((file) => contents.has(file))
|
|
1337
|
+
if (files.length > 0) reason[who] = sortedByPath(files)
|
|
1366
1338
|
}
|
|
1367
|
-
return
|
|
1339
|
+
return Object.keys(reason).length > 1 ? reason : undefined
|
|
1368
1340
|
}
|
|
1369
1341
|
|
|
1370
|
-
//
|
|
1371
|
-
// upgraded to a real format (else the bundle fails its lockfile cross-check at load).
|
|
1342
|
+
// this.formats less a 'stat:*' the unified attestation upgraded, else the bundle fails its lockfile check at load.
|
|
1372
1343
|
#formatsForBundle() {
|
|
1373
|
-
if (this.#sidecars.size === 0) return this.formats
|
|
1374
1344
|
const merged = this.#mergedFormats()
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
out ??= new Map(this.formats)
|
|
1379
|
-
out.delete(file)
|
|
1380
|
-
}
|
|
1381
|
-
}
|
|
1382
|
-
return out ?? this.formats
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
#absorbRepo(half, bundle) {
|
|
1386
|
-
this.#absorbedRepos.set(half, this.#absorbedRepos.has(half) ? mergeRepo(this.#absorbedRepos.get(half), bundle.repo) : bundle.repo)
|
|
1345
|
+
const upgraded = ([file, format]) => isStatFormat(format) && merged.get(file) !== format
|
|
1346
|
+
if (!this.formats.entries().some(upgraded)) return this.formats
|
|
1347
|
+
return new Map(this.formats.entries().filter((entry) => !upgraded(entry)))
|
|
1387
1348
|
}
|
|
1388
1349
|
|
|
1389
1350
|
get #detectedRepo() {
|
|
1390
1351
|
return (this.#detectedRepoCache ??= { value: detectRepo(this.root, this.#host) }).value
|
|
1391
1352
|
}
|
|
1392
1353
|
|
|
1393
|
-
// `repo` to write for a half: detected, merged with that half's absorbed one when adding to it.
|
|
1394
1354
|
#repoFor(half) {
|
|
1395
1355
|
if (!this.#absorbedRepos.has(half)) return this.#detectedRepo
|
|
1396
1356
|
const absorbed = this.#absorbedRepos.get(half)
|
|
1397
1357
|
return absorbed && mergeRepo(absorbed, this.#detectedRepo)
|
|
1398
1358
|
}
|
|
1399
1359
|
|
|
1400
|
-
// One bundle artifact over `contents` (file -> stored content) with the `formats` it declares;
|
|
1401
|
-
// the code side carries entries/imports, the resources half none.
|
|
1402
1360
|
#bundle(half, contents, formats, { entries = this.entries, imports = this.imports } = {}) {
|
|
1403
1361
|
const modules = this.#bundleModules(contents)
|
|
1404
1362
|
return new Bundle({
|
|
@@ -1407,15 +1365,13 @@ export class State {
|
|
|
1407
1365
|
modules,
|
|
1408
1366
|
formats,
|
|
1409
1367
|
imports,
|
|
1410
|
-
executable: this
|
|
1411
|
-
reason: this.#bundleReason(contents
|
|
1368
|
+
executable: narrowExecutable(this.executable, { modules, formats, scope: this.config.scope }),
|
|
1369
|
+
reason: this.#bundleReason(contents),
|
|
1412
1370
|
repo: this.#repoFor(half),
|
|
1413
1371
|
})
|
|
1414
1372
|
}
|
|
1415
1373
|
|
|
1416
1374
|
get sourceBundle() {
|
|
1417
|
-
// One bundle holds code + resources, with `formats` tagging which; the key sets must be
|
|
1418
|
-
// disjoint, asserted so an overlap fails locally rather than silently.
|
|
1419
1375
|
const contents = new Map(this.sources)
|
|
1420
1376
|
for (const [file, content] of this.resources) {
|
|
1421
1377
|
assert.ok(!contents.has(file), `state invariant: file ${file} in both sources and resources`)
|
|
@@ -1428,10 +1384,7 @@ export class State {
|
|
|
1428
1384
|
return this.sourceBundle.serialize()
|
|
1429
1385
|
}
|
|
1430
1386
|
|
|
1431
|
-
// Split-bundle
|
|
1432
|
-
// formats (stat records too: they aren't resource formats, so the resources half's shape check
|
|
1433
|
-
// would reject them), the resources half owns resource formats + bytes. Both declare per-dir
|
|
1434
|
-
// module identity so each file verifies alone.
|
|
1387
|
+
// Split-bundle halves. Stat records go in the code half: the resources half's shape check would reject them.
|
|
1435
1388
|
get codeBundle() {
|
|
1436
1389
|
const codeFormats = new Map([...this.#formatsForBundle()].filter(([, format]) => !Bundle.isResourceFormat(format)))
|
|
1437
1390
|
return this.#bundle('code', this.sources, codeFormats)
|
|
@@ -1439,60 +1392,45 @@ export class State {
|
|
|
1439
1392
|
|
|
1440
1393
|
get resourcesBundle() {
|
|
1441
1394
|
const resourceFormats = new Map([...this.formats].filter(([, format]) => Bundle.isResourceFormat(format)))
|
|
1442
|
-
//
|
|
1395
|
+
// Bundle.parse waives the entries requirement with no code.
|
|
1443
1396
|
return this.#bundle('resources', this.resources, resourceFormats, { entries: new Set(), imports: new Map() })
|
|
1444
1397
|
}
|
|
1445
1398
|
|
|
1446
|
-
//
|
|
1447
|
-
// #backfillObservedResolutions decides at write() which to add.
|
|
1399
|
+
// Verbatim, last-write-wins: #backfillObservedResolutions decides which to add.
|
|
1448
1400
|
observeResolution(parentURL, specifier, resolvedURL) {
|
|
1449
1401
|
nestedMap(this.#observedResolutions, parentURL).set(specifier, resolvedURL)
|
|
1450
1402
|
}
|
|
1451
1403
|
|
|
1452
|
-
// Record that a require() executed `url` (from the Module._load shim). Out-of-root targets are
|
|
1453
|
-
// skipped like #backfillObservedResolutions' -- the live hooks correctly ignore them too.
|
|
1454
1404
|
observeExecution(url) {
|
|
1455
1405
|
const file = this.#canonicalFileMaybe(url)
|
|
1456
1406
|
if (file !== undefined) this.#executedFiles.add(file)
|
|
1457
1407
|
}
|
|
1458
1408
|
|
|
1459
|
-
// Whether the bundle carries source bytes for `url` (the hooks' bundle-bypass reconciliation).
|
|
1460
1409
|
isBundledSource(url) {
|
|
1461
1410
|
const file = this.#canonicalFileMaybe(url)
|
|
1462
1411
|
return file !== undefined && this.sources.has(file)
|
|
1463
1412
|
}
|
|
1464
1413
|
|
|
1465
|
-
//
|
|
1466
|
-
//
|
|
1467
|
-
// leaving it silent would ship an artifact that misses code the run provably evaluated, and a
|
|
1468
|
-
// frozen replay would never verify it. addFile re-checks bytes against the lockfile/bundle in
|
|
1469
|
-
// frozen modes, so tampering still fails the run -- at write time, but loudly.
|
|
1414
|
+
// A resolve-only edge stays byte-less by design, but an EXECUTED file the load hook never saw is a hole a
|
|
1415
|
+
// frozen replay would never verify. addFile re-checks bytes in frozen modes, so tampering fails loudly.
|
|
1470
1416
|
#backfillExecutedFiles() {
|
|
1471
1417
|
for (const file of this.#executedFiles) {
|
|
1472
|
-
// "Captured this run", NOT this.hashes: a lockfile-seeded hash must still be VERIFIED
|
|
1473
|
-
// against disk here when the run executed the file without the load hook observing it.
|
|
1474
1418
|
if (this.#filesCapturedThisRun.has(file)) continue
|
|
1475
|
-
// A
|
|
1476
|
-
// (non-deterministic across installs, like the rest of NATIVE_ARTIFACT_EXTS).
|
|
1419
|
+
// A native addon is deliberately not captured: non-deterministic across installs.
|
|
1477
1420
|
if (isNativeArtifact(file)) continue
|
|
1478
|
-
const url =
|
|
1479
|
-
if (
|
|
1480
|
-
this.addFile(url, { reason: 'run' })
|
|
1421
|
+
const url = this.#urlOf(file)
|
|
1422
|
+
if (inScope(this, url)) this.addFile(url, { reason: 'run' })
|
|
1481
1423
|
}
|
|
1482
1424
|
}
|
|
1483
1425
|
|
|
1484
|
-
//
|
|
1485
|
-
// The resolution is attested but its bytes are NOT seeded -- that would widen trust to everything
|
|
1486
|
-
// that merely resolves. Skips out-of-scope targets and dedups across all condition buckets.
|
|
1426
|
+
// Attested, but the bytes are NOT seeded: that would widen trust to everything that merely resolves.
|
|
1487
1427
|
#backfillObservedResolutions() {
|
|
1488
1428
|
for (const [parentURL, specs] of this.#observedResolutions) {
|
|
1489
1429
|
const parent = this.#canonicalFileMaybe(parentURL)
|
|
1490
1430
|
if (parent === undefined) continue
|
|
1491
1431
|
for (const [specifier, resolvedURL] of specs) {
|
|
1492
1432
|
const file = this.#canonicalFileMaybe(resolvedURL)
|
|
1493
|
-
if (file === undefined) continue
|
|
1494
|
-
const bundled = this.sources.has(file) || this.resources.has(file)
|
|
1495
|
-
if (!bundled && !this.config.full && !this.inNodeModules(resolvedURL)) continue
|
|
1433
|
+
if (file === undefined || (!this.#bundled(file) && !inScope(this, resolvedURL))) continue
|
|
1496
1434
|
const spec = this.#canonicalSpecifier(parentURL, specifier)
|
|
1497
1435
|
const recorded = this.imports.values().some((byParent) => byParent.get(parent)?.get(spec) !== undefined)
|
|
1498
1436
|
if (!recorded) this.addImport(parentURL, specifier, resolvedURL, { conditions: this.#requireConditions ?? '*' })
|
|
@@ -1500,77 +1438,86 @@ export class State {
|
|
|
1500
1438
|
}
|
|
1501
1439
|
}
|
|
1502
1440
|
|
|
1503
|
-
//
|
|
1504
|
-
//
|
|
1505
|
-
// membership, NOT this.hashes (which also holds lockfile-absorbed entries and is shared by
|
|
1506
|
-
// reference across sidecars).
|
|
1441
|
+
// Every bundled module's package.json, reached or not, for a load/prune to read, and a bundled file's nearer
|
|
1442
|
+
// one (an `esm/package.json` marker). Keyed on bundle membership, NOT this.hashes (lockfile-seeded, shared).
|
|
1507
1443
|
includePackageJson() {
|
|
1508
|
-
//
|
|
1444
|
+
// Gathered first: addFile mutates this.modules.
|
|
1509
1445
|
const realRoot = this.#host.realpath(this.root)
|
|
1446
|
+
const claims = this.#reasonClaims()
|
|
1447
|
+
const exists = memoized((rel) => packageJSONStat(this.#host, resolve(this.root, rel))?.isFile() ?? false)
|
|
1448
|
+
const read = (rel) => {
|
|
1449
|
+
const buf = readModuleManifest({ baseDir: this.root, realBase: realRoot, rel, host: this.#host })
|
|
1450
|
+
if (!buf) return null
|
|
1451
|
+
try {
|
|
1452
|
+
const pkg = JSON.parse(packageJSONText(buf))
|
|
1453
|
+
return isPlainObject(pkg) ? { buf, pkg } : null
|
|
1454
|
+
} catch {
|
|
1455
|
+
return null
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1510
1458
|
const toAdd = []
|
|
1511
1459
|
for (const [dir, module] of this.modules) {
|
|
1512
|
-
const
|
|
1513
|
-
|
|
1514
|
-
const
|
|
1515
|
-
if (
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1460
|
+
const own = moduleFileKey(dir, 'package.json')
|
|
1461
|
+
// Under bundle=add a dep may have drifted, and #locateModule would crash on it.
|
|
1462
|
+
const manifest = read(own)
|
|
1463
|
+
if (manifest === null || manifest.pkg.name !== module.name || manifest.pkg.version !== module.version) continue
|
|
1464
|
+
const files = Object.keys(module.files).map((f) => moduleFileKey(dir, f))
|
|
1465
|
+
const scopes = new Map([[own, files]])
|
|
1466
|
+
for (const file of files) {
|
|
1467
|
+
const nearest = this.#bundled(file) ? scopeManifestOf(file, dir, exists) : undefined
|
|
1468
|
+
if (nearest !== undefined) upsert(scopes, nearest, () => []).push(file)
|
|
1469
|
+
}
|
|
1470
|
+
for (const [rel, of] of scopes) {
|
|
1471
|
+
if (this.#bundled(rel)) continue // already in this bundle
|
|
1472
|
+
const buf = rel === own ? manifest.buf : read(rel)?.buf
|
|
1473
|
+
if (buf === undefined) continue
|
|
1474
|
+
// It rides along BECAUSE a consumer bundled its files, so it inherits each. Derived BEFORE the adds
|
|
1475
|
+
// below, so one fold's manifest can't count as another bucket's consumer evidence.
|
|
1476
|
+
const consumers = [...claims].filter(([, claimed]) => of.some((file) => claimed.has(file))).map(([who]) => who)
|
|
1477
|
+
toAdd.push({ rel, buf, consumers })
|
|
1478
|
+
}
|
|
1528
1479
|
}
|
|
1529
1480
|
for (const { rel, buf, consumers } of toAdd) {
|
|
1530
|
-
this.addFile(
|
|
1481
|
+
this.addFile(this.#urlOf(rel), { source: buf, reason: null })
|
|
1531
1482
|
for (const who of consumers) this.#recordReason(who, rel)
|
|
1532
1483
|
}
|
|
1533
1484
|
}
|
|
1534
1485
|
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
// and the BFS must see it to seed the file's bytes, else the bundle ships a dangling edge.
|
|
1486
|
+
// Native resolutions BEFORE the stasis-core BFS, which must see an edge they add to seed its bytes.
|
|
1487
|
+
#backfill() {
|
|
1538
1488
|
this.#backfillObservedResolutions()
|
|
1539
1489
|
this.#backfillExecutedFiles()
|
|
1540
1490
|
this.#backfillBeforeWrite()
|
|
1541
|
-
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
write() {
|
|
1494
|
+
this.#backfill()
|
|
1495
|
+
const writing = this.config.writeLockfile || this.config.writeBundle
|
|
1496
|
+
// A frozen run verifies what it reached, not `extra`.
|
|
1497
|
+
if (writing) this.includeExtra()
|
|
1498
|
+
// AFTER the backfills and `extra`, so buckets they create are covered too.
|
|
1542
1499
|
if (this.config.writeBundle && this.config.packageJSON) this.includePackageJson()
|
|
1500
|
+
if (writing) this.#recordInstalled()
|
|
1543
1501
|
|
|
1544
|
-
//
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
if (this.config.resourcesBundleFile) {
|
|
1557
|
-
// hasContent gates each half so an empty one is dropped under bundle=replace.
|
|
1558
|
-
this.#lastCodeBundle = this.#emitBundle(sourcesPath, this.sources.size > 0, this.#lastCodeBundle, () => this.codeBundle.serialize())
|
|
1559
|
-
this.#lastResourcesBundle = this.#emitBundle(this.config.resourcesBundleFile, this.resources.size > 0, this.#lastResourcesBundle, () => this.resourcesBundle.serialize())
|
|
1560
|
-
} else {
|
|
1561
|
-
this.#lastUnifiedBundle = this.#emitBundle(sourcesPath, this.sources.size > 0 || this.resources.size > 0, this.#lastUnifiedBundle, () => this.sourceData)
|
|
1562
|
-
}
|
|
1502
|
+
// All serialized before the first is written, so one that can't be leaves the others as they were.
|
|
1503
|
+
const lockText = this.config.writeLockfile && !this.#parent ? this.lockData : null
|
|
1504
|
+
const sourcesPath = this.#bundlePath
|
|
1505
|
+
const bundles = !this.config.writeBundle ? [] : this.config.resourcesBundleFile
|
|
1506
|
+
? [[sourcesPath, this.#bundleText(this.sources.size > 0, () => this.codeBundle.serialize())],
|
|
1507
|
+
[this.config.resourcesBundleFile, this.#bundleText(this.resources.size > 0, () => this.resourcesBundle.serialize())]]
|
|
1508
|
+
: [[sourcesPath, this.#bundleText(this.sources.size > 0 || this.resources.size > 0, () => this.sourceData)]]
|
|
1509
|
+
|
|
1510
|
+
if (lockText !== null && lockText !== this.#lastLockData) {
|
|
1511
|
+
mkdirSync(dirname(this.#lockPath), { recursive: true })
|
|
1512
|
+
writeFileSync(this.#lockPath, lockText)
|
|
1513
|
+
this.#lastLockData = lockText
|
|
1563
1514
|
}
|
|
1515
|
+
for (const [path, text] of bundles) this.#emitBundle(path, text)
|
|
1564
1516
|
}
|
|
1565
1517
|
|
|
1566
|
-
//
|
|
1567
|
-
// omitted -- the root re-reads bytes from disk.
|
|
1518
|
+
// For a forked child to hand back; the root re-reads bytes. Entries are dropped: a child's is its fork target.
|
|
1568
1519
|
shardSnapshot() {
|
|
1569
|
-
this.#
|
|
1570
|
-
this.#backfillExecutedFiles()
|
|
1571
|
-
this.#backfillBeforeWrite()
|
|
1572
|
-
// Only what THIS process observed, never the seeded baseline (shard-size bloat). Entries are
|
|
1573
|
-
// dropped: a child's "entry" is its fork target, not a root's.
|
|
1520
|
+
this.#backfill()
|
|
1574
1521
|
const files = []
|
|
1575
1522
|
const formats = new Map()
|
|
1576
1523
|
for (const file of this.#observed) {
|
|
@@ -1578,7 +1525,7 @@ export class State {
|
|
|
1578
1525
|
if (format !== undefined) formats.set(file, format)
|
|
1579
1526
|
if (this.hashes.has(file)) files.push(file)
|
|
1580
1527
|
}
|
|
1581
|
-
// Only edges whose
|
|
1528
|
+
// Only edges whose parent was observed: the root attests the bundle-seeded rest already.
|
|
1582
1529
|
const imports = new Map()
|
|
1583
1530
|
for (const [conditions, byParent] of this.imports) {
|
|
1584
1531
|
for (const parent of this.#observed) {
|
|
@@ -1589,134 +1536,95 @@ export class State {
|
|
|
1589
1536
|
return serializeShard({ scope: this.config.scope, files, formats, imports })
|
|
1590
1537
|
}
|
|
1591
1538
|
|
|
1592
|
-
//
|
|
1593
|
-
// (re-reading bytes/listings from disk) and each edge. Best-effort -- a gone/out-of-scope record is
|
|
1594
|
-
// skipped rather than aborting the merge.
|
|
1539
|
+
// As if this process read those files itself, re-read from disk. Best-effort: a gone/out-of-scope record is skipped.
|
|
1595
1540
|
mergeShard(shardText) {
|
|
1596
1541
|
const shard = parseShard(shardText)
|
|
1597
|
-
// Defense-in-depth: cross-scope is signature-rejected upstream, but a mismatch here means a
|
|
1598
|
-
// malformed/foreign shard -- refuse.
|
|
1599
1542
|
assert.equal(shard.scope, this.config.scope, `shard scope "${shard.scope}" != root scope "${this.config.scope}"`)
|
|
1600
|
-
// An in-root
|
|
1601
|
-
// replay re-checks real-path containment -- the same boundary fs.js enforces on live --fs reads.
|
|
1602
|
-
let realRoot
|
|
1603
|
-
const realContained = (absolute) => {
|
|
1604
|
-
if (realRoot === undefined) {
|
|
1605
|
-
try { realRoot = realpathSync(this.root) } catch { realRoot = this.root }
|
|
1606
|
-
}
|
|
1607
|
-
let real
|
|
1608
|
-
try { real = realpathSync(absolute) } catch { return false }
|
|
1609
|
-
return isPathWithin(realRoot, real)
|
|
1610
|
-
}
|
|
1611
|
-
// Files first, then edges. Merged files are NEVER marked entries -- a child's "entry" is its
|
|
1612
|
-
// fork-target main, not a root entry.
|
|
1543
|
+
// An in-root KEY may resolve through a symlink OUTSIDE the root on THIS disk: real containment is re-checked.
|
|
1613
1544
|
for (const file of shard.files) {
|
|
1614
1545
|
const absolute = resolve(this.root, file)
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
// ABOVE the already-attested fast path below: a child that re-read a file and found no bit
|
|
1618
|
-
// refutes it by OMISSION, so every file the shard RECORDS needs an observation. Re-derived
|
|
1619
|
-
// from THIS process's disk, so the shard cannot inject a forged bit.
|
|
1546
|
+
if (!realContained(this.root, absolute)) continue
|
|
1547
|
+
// ABOVE the fast path: a child refutes a bit by OMISSION. Re-derived from THIS disk, never forged.
|
|
1620
1548
|
this.#recordExecutable(file, absolute)
|
|
1621
|
-
// Skip
|
|
1622
|
-
// carry a concrete format the root LACKS, else a later frozen run rejects the unattested format.
|
|
1549
|
+
// Skip one the root attests (the re-read dominates merge cost), unless carrying a format it LACKS.
|
|
1623
1550
|
const format = shard.formats.get(file)
|
|
1624
1551
|
if (this.hashes.has(file) && (format === undefined || (this.formats.get(file) ?? this.#lockFormats?.get(file)) !== undefined)) continue
|
|
1625
1552
|
const url = pathToFileURL(absolute).toString()
|
|
1626
1553
|
try {
|
|
1627
1554
|
if (format === 'directory') {
|
|
1628
|
-
// Range-
|
|
1555
|
+
// Range-checked FIRST, so a child-forged `..` key can't make us list it.
|
|
1629
1556
|
if (!isPathWithin(this.root, absolute)) continue
|
|
1630
|
-
this.addFsDir(url,
|
|
1631
|
-
} else if (format
|
|
1632
|
-
//
|
|
1633
|
-
// the shard's and risking a mismatch.
|
|
1557
|
+
this.addFsDir(url, this.#host.readdir(absolute).map((entry) => entry.name))
|
|
1558
|
+
} else if (Bundle.isResourceFormat(format)) {
|
|
1559
|
+
// The resource tag is re-derived from the re-read bytes.
|
|
1634
1560
|
this.addFile(url, { resource: true })
|
|
1635
1561
|
} else if (format === undefined) {
|
|
1636
|
-
//
|
|
1637
|
-
// commonjs default and collide with the `module` the loader records if it's later imported.
|
|
1562
|
+
// An fs-READ .js/.ts: a commonjs default would collide with the `module` a later import records.
|
|
1638
1563
|
this.addFile(url, { inferFormat: false })
|
|
1639
1564
|
} else {
|
|
1640
|
-
//
|
|
1641
|
-
// to commonjs and a frozen run rejects the mismatch.
|
|
1565
|
+
// Else a child-only ESM .js under a no-`type` package re-defaults to commonjs.
|
|
1642
1566
|
this.addFile(url, { format })
|
|
1643
1567
|
}
|
|
1644
|
-
} catch {
|
|
1645
|
-
// Gone/unreadable, or addFile-rejected (out of scope): skip, like the edge replay below.
|
|
1646
|
-
}
|
|
1568
|
+
} catch { /* gone/unreadable, or out of scope */ }
|
|
1647
1569
|
}
|
|
1648
|
-
// Stat records live ONLY in the shard's formats
|
|
1649
|
-
// from DISK (a shard can't inject a forged kind). Range-check BEFORE touching disk.
|
|
1570
|
+
// Stat records live ONLY in the shard's formats; the kind is re-derived from DISK, range-checked first.
|
|
1650
1571
|
for (const [file, format] of shard.formats) {
|
|
1651
1572
|
if (!isStatFormat(format)) continue
|
|
1652
1573
|
const absolute = resolve(this.root, file)
|
|
1653
1574
|
if (!isPathWithin(this.root, absolute)) continue
|
|
1654
1575
|
try {
|
|
1655
|
-
//
|
|
1656
|
-
|
|
1657
|
-
const
|
|
1658
|
-
|
|
1659
|
-
// statSync follows links: a symlink pointing OUT must not attest an external kind under an
|
|
1660
|
-
// in-root key.
|
|
1661
|
-
if (kind === null || !realContained(absolute)) continue
|
|
1576
|
+
// A stat, NOT an lstat, as the child's capture; a symlink pointing OUT must not attest an external kind.
|
|
1577
|
+
const stats = this.#host.stat(absolute)
|
|
1578
|
+
const kind = stats?.isDirectory() ? 'directory' : stats?.isFile() ? 'file' : null
|
|
1579
|
+
if (kind === null || !realContained(this.root, absolute)) continue
|
|
1662
1580
|
this.addFsStat(pathToFileURL(absolute).toString(), kind)
|
|
1663
|
-
} catch {
|
|
1664
|
-
// Gone/dangling, or a kind conflict with the root's capture: skip.
|
|
1665
|
-
}
|
|
1581
|
+
} catch { /* gone/dangling, or a kind conflict with the root's capture */ }
|
|
1666
1582
|
}
|
|
1667
1583
|
for (const [conditions, byParent] of shard.imports) {
|
|
1668
|
-
// Reverse #conditionsKey
|
|
1669
|
-
// suffix -- parse it so attributed edges merge under the child's key.
|
|
1670
|
-
let condStr = conditions
|
|
1671
|
-
let importAttributes
|
|
1584
|
+
// Reverse #conditionsKey, so attributed edges merge under the child's key.
|
|
1672
1585
|
const withAt = conditions.indexOf(' (with: ')
|
|
1586
|
+
const condStr = withAt === -1 ? conditions : conditions.slice(0, withAt)
|
|
1587
|
+
let importAttributes
|
|
1673
1588
|
if (withAt !== -1) {
|
|
1674
|
-
condStr = conditions.slice(0, withAt)
|
|
1675
1589
|
try { importAttributes = JSON.parse(conditions.slice(withAt + ' (with: '.length, -1)) } catch { /* leave undefined */ }
|
|
1676
1590
|
}
|
|
1677
1591
|
const cond = condStr === '*' ? '*' : condStr.split(', ')
|
|
1678
1592
|
for (const [parent, specs] of byParent) {
|
|
1679
|
-
const parentURL =
|
|
1593
|
+
const parentURL = this.#urlOf(parent)
|
|
1680
1594
|
for (const [specifier, file] of specs) {
|
|
1681
|
-
const url =
|
|
1682
|
-
// Same best-effort stance as addFile above.
|
|
1595
|
+
const url = this.#urlOf(file)
|
|
1683
1596
|
try { this.addImport(parentURL, specifier, url, { conditions: cond, importAttributes }) } catch { /* skipped */ }
|
|
1684
1597
|
}
|
|
1685
1598
|
}
|
|
1686
1599
|
}
|
|
1687
1600
|
}
|
|
1688
1601
|
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1602
|
+
#bundleText(hasContent, serialize) {
|
|
1603
|
+
return !hasContent && this.config.replaceBundle ? null : serialize()
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
// Null DELETES the file: a replace run is authoritative.
|
|
1607
|
+
#emitBundle(path, text) {
|
|
1608
|
+
if (text === null) {
|
|
1693
1609
|
rmSync(path, { force: true })
|
|
1694
|
-
|
|
1695
|
-
}
|
|
1696
|
-
const text = serialize()
|
|
1697
|
-
if (text !== lastText) {
|
|
1610
|
+
} else if (text !== this.#lastBundleText.get(path)) {
|
|
1698
1611
|
mkdirSync(dirname(path), { recursive: true })
|
|
1699
|
-
writeFileSync(path,
|
|
1612
|
+
writeFileSync(path, compressBundleText(text, this.config.brotliQuality))
|
|
1700
1613
|
}
|
|
1701
|
-
|
|
1614
|
+
this.#lastBundleText.set(path, text)
|
|
1702
1615
|
}
|
|
1703
1616
|
|
|
1704
1617
|
get parent() {
|
|
1705
1618
|
return this.#parent
|
|
1706
1619
|
}
|
|
1707
1620
|
|
|
1708
|
-
//
|
|
1709
|
-
// module the resolve hook records the edge but the load hook never fires. The BFS below is scoped
|
|
1710
|
-
// to the stasis-core src shape.
|
|
1621
|
+
// For a preload-cached stasis-core module the resolve hook records the edge but the load hook never fires.
|
|
1711
1622
|
#backfillBeforeWrite() {
|
|
1712
|
-
const missing = this.#collectMissingImportedFiles()
|
|
1713
|
-
if (missing.size === 0) return
|
|
1714
|
-
|
|
1715
1623
|
const isStasisCoreFile = (file) => STASIS_CORE_FILE_RE.test(`/${file}`)
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1624
|
+
// Anything else stays a resolution attested without bytes: the load-time getFile gate enforces trust.
|
|
1625
|
+
const imported = new Set(this.imports.values().flatMap((byParent) => byParent.values()).flatMap((specifiers) => specifiers.values()))
|
|
1626
|
+
const stasisCoreMissing = [...imported].filter((file) => typeof file === 'string' && isStasisCoreFile(file) &&
|
|
1627
|
+
!this.hashes.has(file) && !this.#bundled(file) && inScope(this, this.#urlOf(file)))
|
|
1720
1628
|
if (stasisCoreMissing.length === 0) return
|
|
1721
1629
|
|
|
1722
1630
|
const preloadRel = this.#preloadRoot ? relative(this.root, this.#preloadRoot) : null
|
|
@@ -1726,35 +1634,21 @@ export class State {
|
|
|
1726
1634
|
`investigate rather than silently patch: ${stasisCoreMissing.slice(0, 3).join(', ')}`)
|
|
1727
1635
|
|
|
1728
1636
|
const PRELOAD_CONDITIONS = ['node', 'import', 'module-sync', 'node-addons']
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
// why this backfill exists), so they're run-loaded modules like any other. A consumer-less
|
|
1735
|
-
// record can't be sorted by provenance downstream (app code vs toolchain).
|
|
1736
|
-
this.addFile(pathToFileURL(resolve(this.root, file)).toString(), { reason: 'run' })
|
|
1737
|
-
}
|
|
1738
|
-
|
|
1739
|
-
// Seeded from the missing files + every stasis-core file already in sources, to walk their
|
|
1740
|
-
// transitive imports too.
|
|
1741
|
-
const queue = [...stasisCoreMissing, ...[...this.sources.keys()].filter(isStasisCoreFile)]
|
|
1742
|
-
const processed = new Set()
|
|
1743
|
-
while (queue.length > 0) {
|
|
1744
|
-
const file = queue.shift()
|
|
1745
|
-
if (processed.has(file)) continue
|
|
1746
|
-
processed.add(file)
|
|
1747
|
-
if (!this.sources.has(file) && !this.resources.has(file)) stasisAddFile(file)
|
|
1637
|
+
// Every stasis-core file already in sources too, to walk their transitive imports.
|
|
1638
|
+
const queue = new Set([...stasisCoreMissing, ...[...this.sources.keys()].filter(isStasisCoreFile)])
|
|
1639
|
+
for (const file of queue) {
|
|
1640
|
+
// 'run', as Node evaluated these before registerHooks could see: a consumer-less record can't be sorted.
|
|
1641
|
+
if (!this.#bundled(file)) this.addFile(this.#urlOf(file), { reason: 'run' })
|
|
1748
1642
|
const source = this.sources.get(file)
|
|
1749
1643
|
if (typeof source !== 'string') continue
|
|
1750
|
-
const baseURL =
|
|
1644
|
+
const baseURL = this.#urlOf(file)
|
|
1751
1645
|
const recordEdge = (specifier) => {
|
|
1752
1646
|
const targetURL = new URL(specifier, baseURL).toString()
|
|
1753
1647
|
const targetAbsolute = fileURLToPath(targetURL)
|
|
1754
1648
|
let targetFile
|
|
1755
1649
|
try { targetFile = this.relative(targetAbsolute) } catch { return }
|
|
1756
|
-
if (!isStasisCoreFile(targetFile) || !
|
|
1757
|
-
queue.
|
|
1650
|
+
if (!isStasisCoreFile(targetFile) || !this.#exists(targetAbsolute)) return
|
|
1651
|
+
queue.add(targetFile)
|
|
1758
1652
|
this.addImport(baseURL, specifier, targetURL, { conditions: PRELOAD_CONDITIONS })
|
|
1759
1653
|
}
|
|
1760
1654
|
// stasis-core's ESM import shape, tight enough to ignore JSDoc/template-literal false positives.
|
|
@@ -1763,26 +1657,7 @@ export class State {
|
|
|
1763
1657
|
for (const m of source.matchAll(/require\('(\.\.\/package\.json)'\)/g)) recordEdge(m[1])
|
|
1764
1658
|
}
|
|
1765
1659
|
}
|
|
1766
|
-
|
|
1767
|
-
// Files referenced by this.imports but not captured, restricted to the in-scope set (out-of-scope
|
|
1768
|
-
// files are excluded -- the live hooks correctly skip them).
|
|
1769
|
-
#collectMissingImportedFiles() {
|
|
1770
|
-
const missing = new Set()
|
|
1771
|
-
for (const [, byParent] of this.imports) {
|
|
1772
|
-
for (const [, specifiers] of byParent) {
|
|
1773
|
-
for (const [, file] of specifiers) {
|
|
1774
|
-
if (typeof file !== 'string' || missing.has(file)) continue
|
|
1775
|
-
if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) continue
|
|
1776
|
-
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
1777
|
-
if (!this.config.full && !this.inNodeModules(url)) continue
|
|
1778
|
-
missing.add(file)
|
|
1779
|
-
}
|
|
1780
|
-
}
|
|
1781
|
-
}
|
|
1782
|
-
return missing
|
|
1783
|
-
}
|
|
1784
1660
|
}
|
|
1785
1661
|
|
|
1786
|
-
//
|
|
1787
|
-
// in `@exodus/stasis-core/...` isn't matched.
|
|
1662
|
+
// Anchored against `/${file}`, so a project-root file merely ending in `@exodus/stasis-core/...` isn't matched.
|
|
1788
1663
|
const STASIS_CORE_FILE_RE = /\/@exodus\/stasis-core\/(?:src\/[a-zA-Z-]+\.c?js|package\.json)$/
|