@exodus/stasis-core 1.0.0-beta.1 → 1.0.0-beta.3
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/README.md +1 -1
- package/bin/stasis-core.js +38 -28
- package/package.json +8 -1
- package/src/add.js +325 -0
- package/src/brotli.js +15 -16
- package/src/bundle-util.js +66 -0
- package/src/bundle.js +99 -88
- package/src/config.js +78 -104
- package/src/extract.js +159 -0
- package/src/fs.js +123 -313
- package/src/hooks.js +129 -449
- package/src/loader.js +1 -3
- package/src/lockfile.js +82 -62
- package/src/prune.js +37 -152
- package/src/shard.js +60 -0
- package/src/state-util.js +4 -26
- package/src/state.js +579 -1128
- package/src/util.js +545 -97
package/src/state.js
CHANGED
|
@@ -9,18 +9,16 @@ import { brotliCompressSync, brotliDecompressSync } from 'node:zlib'
|
|
|
9
9
|
import { Config } from './config.js'
|
|
10
10
|
import { Bundle } from './bundle.js'
|
|
11
11
|
import { Lockfile } from './lockfile.js'
|
|
12
|
+
import { parseShard, serializeShard } from './shard.js'
|
|
12
13
|
import { canonicalizePath, sha512integrity, readFileSyncMaybe, noupsert } from './state-util.js'
|
|
13
14
|
import { brotliOptions } from './brotli.js'
|
|
14
|
-
import { CODE_EXTENSIONS,
|
|
15
|
+
import { CODE_EXTENSIONS, canObserveExecuteBits, classifyFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isStatFormat, moduleFileKey, narrowExecutable, objectToMaps, observeExecutable, pathExt, reconcileFormat, sortPaths, splitNodeModulesPath } from './util.js'
|
|
16
|
+
import { readModuleManifest } from './bundle-util.js'
|
|
15
17
|
import corePackage from './package.cjs'
|
|
16
18
|
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
// module.syncBuiltinESMExports() to refresh node:fs's ESM wrapper for user code.
|
|
21
|
-
// Direct ESM destructured imports are live bindings and would re-resolve to the
|
|
22
|
-
// mocked names when state.write() fires on beforeExit.
|
|
23
|
-
const { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } = fs
|
|
19
|
+
// Destructure off the namespace: captures the real fns at eval time, so writes survive --mock's
|
|
20
|
+
// syncBuiltinESMExports() remock of node:fs.
|
|
21
|
+
const { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } = fs
|
|
24
22
|
|
|
25
23
|
const FILE_CONFIG = 'stasis.config.json'
|
|
26
24
|
const FILE_LOCK = 'stasis.lock.json'
|
|
@@ -32,26 +30,20 @@ function readPackageJSON(pkgAbsolute) {
|
|
|
32
30
|
return JSON.parse(buf.toString())
|
|
33
31
|
}
|
|
34
32
|
|
|
35
|
-
//
|
|
36
|
-
// package.json -- bundler-safe, unlike a runtime fs read or `import ... with`). A child
|
|
37
|
-
// (sidecar) State must EXACTLY match its parent's version (enforced in the constructor's
|
|
38
|
-
// parent branch): a sidecar shares the parent's maps by reference and reads its private
|
|
39
|
-
// fields, so a version skew between two @exodus/stasis-core copies in one process would
|
|
40
|
-
// be unsound.
|
|
33
|
+
// Via src/package.cjs: a bundler-safe re-export of package.json.
|
|
41
34
|
const VERSION = corePackage.version
|
|
42
35
|
|
|
36
|
+
const CAN_OBSERVE_EXECUTE_BITS = canObserveExecuteBits()
|
|
37
|
+
|
|
38
|
+
// A weak 'stat:*' yields to a real format; two real formats or a stat kind flip are fatal.
|
|
39
|
+
function upsertFormat(map, file, format) {
|
|
40
|
+
const currentFormat = map.get(file)
|
|
41
|
+
map.set(file, currentFormat === undefined ? format : reconcileFormat(format, currentFormat, file))
|
|
42
|
+
}
|
|
43
|
+
|
|
43
44
|
// TODO: stricter format validation
|
|
44
45
|
|
|
45
|
-
//
|
|
46
|
-
// variable) so it is shared across DUPLICATE stasis-core copies in one process -- e.g. a
|
|
47
|
-
// stasis bundle that carries its own stasis-core alongside the outer loader's. A
|
|
48
|
-
// cross-realm `Symbol.for` key makes every state.js copy reach the same Set. Two roles:
|
|
49
|
-
// - the preload (top-level) State is the unique member flagged isPreload, found by
|
|
50
|
-
// State.preload; constructing a second preload (from ANY copy) is rejected, so the
|
|
51
|
-
// "one top-level state" invariant holds process-wide; and
|
|
52
|
-
// - write-target collision detection (#claimWritePath) scans every member's claimed
|
|
53
|
-
// paths, so no two live States write the same lockfile/bundle/resourcesBundle path --
|
|
54
|
-
// across copies, AND whether or not a preload exists.
|
|
46
|
+
// On globalThis via `Symbol.for` so the registry is shared across duplicate stasis-core copies.
|
|
55
47
|
const STATES_KEY = Symbol.for('@exodus/stasis-core/states')
|
|
56
48
|
function liveStates() {
|
|
57
49
|
return (globalThis[STATES_KEY] ??= new Set())
|
|
@@ -65,150 +57,76 @@ export class State {
|
|
|
65
57
|
formats = new Map()
|
|
66
58
|
modules = new Map()
|
|
67
59
|
imports = new Map()
|
|
60
|
+
// Project-relative files carrying a POSIX execute bit; disk is authoritative (a file that lost
|
|
61
|
+
// the bit drops out). Emitted as the artifacts' `executable` list.
|
|
62
|
+
executable = new Set()
|
|
68
63
|
config
|
|
69
64
|
root
|
|
70
65
|
#parent
|
|
71
66
|
|
|
72
|
-
// True iff this is THE preload (top-level) State. Exposed via `get isPreload()` so
|
|
73
|
-
// State.preload can find the unique flagged member of the global registry across copies.
|
|
74
67
|
#isPreload = false
|
|
75
68
|
|
|
76
|
-
//
|
|
77
|
-
// collision detection. Exposed via `claimedWritePathLabel()` so the global scan in
|
|
78
|
-
// #claimWritePath can consult States from other stasis-core copies.
|
|
69
|
+
// Claimed write-target paths (canonicalized -> label), for cross-State collision detection.
|
|
79
70
|
#claims = new Map()
|
|
80
71
|
|
|
81
|
-
//
|
|
82
|
-
// CONTRIBUTORS. The unified lockfile (built by the parent's lockData via
|
|
83
|
-
// #mergedImports/#mergedFormats) must union the parent's own imports / formats with
|
|
84
|
-
// every sidecar's, otherwise resolution edges and format attestations a sidecar
|
|
85
|
-
// bundler-plugin observed (sidecar.addImport / addFile populate sidecar-local
|
|
86
|
-
// this.imports/this.formats) would never reach the lockfile. Hashes / entries /
|
|
87
|
-
// modules are already shared by reference; this closes the same gap for imports and
|
|
88
|
-
// formats. Also the set attestedBySidecar consults, so --fs skips re-capturing a
|
|
89
|
-
// read the bundler plugin already records here. Unused on sidecars themselves.
|
|
72
|
+
// WRITE-mode sidecars: lockfile CONTRIBUTORS (#mergedImports/#mergedFormats union theirs in).
|
|
90
73
|
#sidecars = new Set()
|
|
91
74
|
|
|
92
|
-
//
|
|
93
|
-
// contributors. A bundler plugin's load-mode sidecar carries the module graph in its
|
|
94
|
-
// bundle; getFs*Family consults this set so a --fs read at load of a graph file the
|
|
95
|
-
// sidecar carries is served from it (the symmetric counterpart to the #sidecars
|
|
96
|
-
// capture-skip). Kept separate from #sidecars precisely so these never enter
|
|
97
|
-
// #mergedImports/#mergedFormats -- a load-mode sidecar contributes no resolution edges
|
|
98
|
-
// or formats (the lockfile's trust-bearing content) to the unified lockfile (the boundary
|
|
99
|
-
// the writeBundle gate on registration enforces; see the constructor and registerReadSidecar).
|
|
75
|
+
// LOAD-mode sidecars: read-only, NOT lockfile contributors (getFs*Family serves bytes from them).
|
|
100
76
|
#readSidecars = new Set()
|
|
101
77
|
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
// resolution attestation. Kept separate from this.imports (the live
|
|
105
|
-
// observed/bundle-served map): the lockfile may attest a superset of what a
|
|
106
|
-
// given run observes or a given bundle carries.
|
|
78
|
+
// Lockfile-attested resolutions (conditions -> parent -> specifier -> file), or null; may be a
|
|
79
|
+
// superset of what a run observes, so kept apart from this.imports.
|
|
107
80
|
#lockImports = null
|
|
108
81
|
|
|
109
|
-
//
|
|
110
|
-
// when none was loaded / it predates format attestation. Like #lockImports,
|
|
111
|
-
// kept apart from this.formats (the live observed/bundle-served map).
|
|
82
|
+
// Lockfile-attested loader formats (file -> format), or null; kept apart from this.formats.
|
|
112
83
|
#lockFormats = null
|
|
113
84
|
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
// branch: with a lockfile, the bundle's metadata must agree with the lockfile's
|
|
117
|
-
// attestations; without one, the bundle's metadata is absorbed as the source of
|
|
118
|
-
// truth. Sidecars inherit this from the parent at construction time so a sidecar
|
|
119
|
-
// bundle is cross-checked against the parent's lockfile attestation rather than
|
|
120
|
-
// silently trusted.
|
|
85
|
+
// Loaded a lockfile at construction: #mergeBundleMetadata cross-checks bundle metadata against
|
|
86
|
+
// it instead of absorbing it as source of truth.
|
|
121
87
|
#lockfileLoaded = false
|
|
122
88
|
|
|
123
|
-
//
|
|
124
|
-
// run (as opposed to the lockfile baseline seeded at construction). shardSnapshot() forwards
|
|
125
|
-
// only these, so a forked child's shard carries what it observed -- not a re-ship of the whole
|
|
126
|
-
// seeded lockfile.
|
|
89
|
+
// Keys THIS process recorded this run (not the seeded baseline); shardSnapshot forwards only these.
|
|
127
90
|
#observed = new Set()
|
|
128
|
-
// file ->
|
|
129
|
-
// Keyed by consumer ('run' for the loader/CLI; a plugin label like 'StasisMetro' for a plugin
|
|
130
|
-
// sharing this State). Populated only when bundling. See #recordReason / #bundleReason.
|
|
91
|
+
// file -> consumers that recorded it, for the bundle's informational `reason` field.
|
|
131
92
|
#reasonFiles = new Map()
|
|
132
|
-
// Supporting state so the bundle `reason` map's "run" entry stays accurate (see #bundleReason):
|
|
133
|
-
// files run loaded as a MODULE (#runImported), files run merely fs-READ after a plugin attached
|
|
134
|
-
// (#fsReadPostPlugin), and whether a bundler plugin has attached to this State (#pluginAttached).
|
|
135
93
|
#runImported = new Set()
|
|
136
94
|
#fsReadPostPlugin = new Set()
|
|
137
95
|
#pluginAttached = false
|
|
138
96
|
|
|
139
|
-
//
|
|
140
|
-
//
|
|
141
|
-
// read-only attestation role a frozen lockfile does, but anchored in the
|
|
142
|
-
// bundle's own bytes -- so bundle=frozen needs no sibling lockfile. The byte
|
|
143
|
-
// check itself is the noupsert of observed disk content into this.sources/
|
|
144
|
-
// this.resources (both pre-seeded from the bundle); these sets/maps close the
|
|
145
|
-
// attested set so a file, resolution, or format the bundle never recorded is
|
|
146
|
-
// rejected. Kept separate from the live this.sources/this.imports/this.formats,
|
|
147
|
-
// which addFile/addImport mutate.
|
|
97
|
+
// Attested by a frozen bundle (bundle=frozen), or null: closes the attested set, so anything the
|
|
98
|
+
// bundle never recorded is rejected. Separate from the live maps.
|
|
148
99
|
#bundleSources = null
|
|
149
100
|
#bundleResources = null
|
|
150
101
|
#bundleImports = null
|
|
151
102
|
#bundleFormats = null
|
|
152
103
|
|
|
153
|
-
// Lazily-built
|
|
154
|
-
// (files, resources, directory listings): a recorded `node_modules/dep/index.js`
|
|
155
|
-
// proves `node_modules` and `node_modules/dep` exist as directories even though
|
|
156
|
-
// neither was readdir'd. getFsStat consults it so
|
|
157
|
-
// `fs.statSync('node_modules').isDirectory()` succeeds at bundle=load. Built once
|
|
158
|
-
// in load mode, where this.imports / this.sources are immutable after construction.
|
|
104
|
+
// Lazily-built dirs IMPLIED by recorded files, so getFsStat answers isDirectory() at bundle=load.
|
|
159
105
|
#impliedDirIndex = null
|
|
160
106
|
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
// require() that bypasses the resolve hook. write()'s backfill records the edges
|
|
164
|
-
// the hook missed (deduped, in-bundle targets only). Populated only during capture.
|
|
107
|
+
// Native resolutions (parent URL -> specifier -> resolved URL) from the Module._resolveFilename
|
|
108
|
+
// shim: require.resolve()/CJS require()s bypassing the resolve hook.
|
|
165
109
|
#observedResolutions = new Map()
|
|
166
110
|
|
|
167
|
-
// Node's require-condition set
|
|
168
|
-
//
|
|
169
|
-
// require()-context edge. require()/require.resolve() resolve under THESE conditions
|
|
170
|
-
// -- they honor the `require` branch of conditional `exports`, so they're not
|
|
171
|
-
// condition-free -- so #backfillObservedResolutions keys the native
|
|
172
|
-
// (Module._resolveFilename) edges it recovers under this set rather than the
|
|
173
|
-
// wildcard '*'. That matches what the resolve hook records directly (newer Node) and
|
|
174
|
-
// unifies them with the require() bucket, so any capture that does at least one
|
|
175
|
-
// require() (the common case) yields the same lockfile on every Node version. null
|
|
176
|
-
// until observed -- a capture that ONLY require.resolve()s and never require()s
|
|
177
|
-
// leaves it null and backfill falls back to '*' (as before this change), which the
|
|
178
|
-
// getImport/resolveBundled wildcard fallbacks still match.
|
|
111
|
+
// Node's require-condition set, from the first require()-context addImport;
|
|
112
|
+
// #backfillObservedResolutions keys native edges under it so the lockfile is Node-version-stable.
|
|
179
113
|
#requireConditions = null
|
|
180
114
|
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
// initState to stasis-core's own root; sidecars inherit it from their
|
|
184
|
-
// parent so plugin-driven write()s also backfill internal edges. write()'s
|
|
185
|
-
// backfill statically parses each file under this root and addImports its
|
|
186
|
-
// relative-specifier edges -- recovering the (state.js -> config.js)-shape
|
|
187
|
-
// edges that the live resolve hook never observed.
|
|
115
|
+
// Root of a package Node loaded BEFORE our hooks (stasis-core's own); write()'s backfill
|
|
116
|
+
// statically parses files under it to recover edges the resolve hook never observed.
|
|
188
117
|
#preloadRoot = null
|
|
189
118
|
|
|
190
|
-
// Per-artifact "last serialized" caches: write()
|
|
191
|
-
// JSON text against the cached one for each output (lockfile / unified bundle /
|
|
192
|
-
// split code / split resources) and skips both the brotli step and the writeFileSync
|
|
193
|
-
// when they match. Brotli at quality 11 dominates write cost, and webpack/esbuild
|
|
194
|
-
// watch-mode rebuilds call write() on every rebuild whether or not the captured
|
|
195
|
-
// graph actually changed -- this cache makes those no-ops fast. Persistent across
|
|
196
|
-
// write() calls within one process; null on the first call.
|
|
119
|
+
// Per-artifact "last serialized" caches: write() skips brotli + writeFileSync on unchanged text.
|
|
197
120
|
#lastLockData = null
|
|
198
121
|
#lastUnifiedBundle = null
|
|
199
122
|
#lastCodeBundle = null
|
|
200
123
|
#lastResourcesBundle = null
|
|
201
124
|
|
|
202
|
-
// Options:
|
|
203
|
-
//
|
|
204
|
-
//
|
|
205
|
-
// parent (State) -- run as a sidecar of this parent. Sidecar shares the parent's
|
|
206
|
-
// hashes/entries/modules (so addFile updates the unified lockfile through them) but
|
|
207
|
-
// manages its own sources/formats/imports/resources and emits its own bundle file.
|
|
208
|
-
// write() on a sidecar skips the lockfile (parent owns it) and only writes its bundle.
|
|
209
|
-
// All other keys forward to Config.
|
|
125
|
+
// Options: `preload` (the unique preload State) and `parent` (run as a sidecar sharing the
|
|
126
|
+
// parent's hashes/entries/modules, with its own sources/formats/imports/resources and bundle).
|
|
127
|
+
// All other keys forward to Config.
|
|
210
128
|
constructor(root, options = {}) {
|
|
211
|
-
// skipDiscovery:
|
|
129
|
+
// skipDiscovery: caller-populated in-memory State, NO filesystem discovery; read-only modes only.
|
|
212
130
|
const { preload: isPreload = false, parent: parentState, preloadRoot, skipDiscovery = false, ...configOptions } = options
|
|
213
131
|
this.config = new Config(configOptions)
|
|
214
132
|
if (isPreload) assert.ok(!State.preload, 'Only one preload Stasis instance is supported')
|
|
@@ -216,46 +134,24 @@ export class State {
|
|
|
216
134
|
this.#isPreload = isPreload
|
|
217
135
|
|
|
218
136
|
if (parentState) {
|
|
219
|
-
// A
|
|
220
|
-
//
|
|
221
|
-
// need NOT be the same module copy. A bundle that carries its OWN stasis-core (e.g.
|
|
222
|
-
// a webpack script + StasisWebpack run under `stasis run --bundle=load`) has the
|
|
223
|
-
// plugin's copy coordinate with the OUTER loader's preload across the class brand.
|
|
224
|
-
// The exact-version match makes that sound; everything the sidecar reads from the
|
|
225
|
-
// parent goes through the parent's PUBLIC surface (`version`, `sidecarInheritance`,
|
|
226
|
-
// `registerSidecar`, and the shared public maps), never a private field a foreign
|
|
227
|
-
// class brand would deny. A version skew is unsound -- reject it with a clear error.
|
|
137
|
+
// A sidecar shares the parent's maps and inherits its attestation, so a version skew is
|
|
138
|
+
// unsound (a different module copy is fine -- it reads only the parent's PUBLIC surface).
|
|
228
139
|
assert.equal(parentState.version, VERSION,
|
|
229
140
|
`child State version '${VERSION}' must exactly match parent version '${parentState.version}'`)
|
|
230
|
-
// Sidecar mode: short-circuit the on-disk discovery path. Lockfile data is shared by
|
|
231
|
-
// reference with the parent; only the sidecar's bundleFile (if any) is parsed here.
|
|
232
141
|
this.#parent = parentState
|
|
233
142
|
this.root = parentState.root
|
|
234
143
|
this.hashes = parentState.hashes
|
|
235
144
|
this.entries = parentState.entries
|
|
236
145
|
this.modules = parentState.modules
|
|
237
|
-
//
|
|
238
|
-
//
|
|
239
|
-
// saves) backfill stasis-core's internal edges the same way the parent does;
|
|
240
|
-
// - the lockfile attestation maps, so #mergeBundleMetadata cross-checks the sidecar
|
|
241
|
-
// bundle against the same attestation the parent loaded -- without this a tampered
|
|
242
|
-
// sidecar bundle could redirect a resolution or flip a format (commonjs <->
|
|
243
|
-
// module-typescript) for hash-valid bytes, which the byte check alone misses.
|
|
146
|
+
// Public sidecarInheritance() (works across copies): the lockfile attestation must reach the
|
|
147
|
+
// sidecar, else a tampered bundle could redirect a resolution on hash-valid bytes.
|
|
244
148
|
const inherited = parentState.sidecarInheritance()
|
|
245
149
|
this.#preloadRoot = inherited.preloadRoot
|
|
246
150
|
this.#lockImports = inherited.lockImports
|
|
247
151
|
this.#lockFormats = inherited.lockFormats
|
|
248
152
|
this.#lockfileLoaded = inherited.lockfileLoaded
|
|
249
153
|
assert.ok(this.config.bundleFile, 'sidecar State requires bundleFile')
|
|
250
|
-
//
|
|
251
|
-
// process-wide registry so two write-intent States anywhere -- whether a sibling
|
|
252
|
-
// sidecar or an independent top-level State (a CLI/env run with a custom
|
|
253
|
-
// lockfile/bundle path) -- can't silently target the same on-disk file
|
|
254
|
-
// (last-write-wins on `beforeExit`). Only writing States claim; read-only modes
|
|
255
|
-
// (load / frozen) may legitimately share a path with the writer that produced the
|
|
256
|
-
// file. The Config's own pairwise cross-path check (config.js:#checkInvariants)
|
|
257
|
-
// already rejects intra-State collisions, so #claimWritePath only fires on
|
|
258
|
-
// cross-State conflicts.
|
|
154
|
+
// Only writing States claim, so two write-intent States can't silently target the same file.
|
|
259
155
|
if (this.config.writeBundle) {
|
|
260
156
|
this.#claimWritePath(`sidecar bundleFile '${this.config.bundleFile}'`, this.config.bundleFile)
|
|
261
157
|
if (this.config.resourcesBundleFile) {
|
|
@@ -263,26 +159,19 @@ export class State {
|
|
|
263
159
|
this.config.resourcesBundleFile)
|
|
264
160
|
}
|
|
265
161
|
}
|
|
266
|
-
// frozenBundle MUST go through this branch
|
|
267
|
-
// own attestation snapshot to build, and was previously missed (silently
|
|
268
|
-
// skipping parsing made the post-construction frozen-bundle invariant
|
|
269
|
-
// unreachable on the sidecar path).
|
|
162
|
+
// frozenBundle MUST go through this branch too -- it has its own attestation snapshot to build.
|
|
270
163
|
if (this.config.writeBundle || this.config.loadBundle || this.config.frozenBundle) {
|
|
271
164
|
const sourcesPath = this.config.bundleFile
|
|
272
165
|
const sources = readFileSyncMaybe(dirname(sourcesPath), basename(sourcesPath))
|
|
273
166
|
if (sources && !this.config.replaceBundle) {
|
|
274
167
|
const bundle = Bundle.parse(brotliDecompressSync(sources).toString('utf-8'))
|
|
275
|
-
//
|
|
276
|
-
// formats and no import map, so serving / verifying one through a
|
|
277
|
-
// sidecar would silently widen trust.
|
|
168
|
+
// v0 bundle has no per-file formats or import map -- serving one via a sidecar widens trust.
|
|
278
169
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
279
170
|
`stasis sidecar requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
|
|
280
171
|
`Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point.`)
|
|
281
172
|
assert.equal(bundle.config.scope, this.config.scope)
|
|
282
|
-
// Cross-check the
|
|
283
|
-
//
|
|
284
|
-
// with the parent, so #mergeBundleMetadata's writes -- when no lockfile is
|
|
285
|
-
// loaded -- propagate into the unified attestation.
|
|
173
|
+
// Cross-check against the parent's lockfile, or absorb if none (shared hashes/entries/
|
|
174
|
+
// modules mean those writes propagate to the parent).
|
|
286
175
|
this.#mergeBundleMetadata(bundle, { lockfileLoaded: this.#lockfileLoaded })
|
|
287
176
|
for (const [file, content] of bundle.sources) {
|
|
288
177
|
if (Bundle.isResourceFormat(bundle.formats.get(file))) this.resources.set(file, content)
|
|
@@ -290,9 +179,9 @@ export class State {
|
|
|
290
179
|
}
|
|
291
180
|
this.formats = bundle.formats
|
|
292
181
|
this.imports = bundle.imports
|
|
182
|
+
this.#absorbExecutable(bundle)
|
|
183
|
+
this.#seedReasonFromBundle(bundle)
|
|
293
184
|
if (this.config.frozenBundle) {
|
|
294
|
-
// Same snapshot the main path takes -- closes the attested set so
|
|
295
|
-
// addFile/addImport reject anything the bundle didn't carry.
|
|
296
185
|
this.#bundleSources = new Set(this.sources.keys())
|
|
297
186
|
this.#bundleResources = new Set(this.resources.keys())
|
|
298
187
|
this.#bundleImports = objectToMaps(fileMapToObject(bundle.imports))
|
|
@@ -300,7 +189,6 @@ export class State {
|
|
|
300
189
|
}
|
|
301
190
|
}
|
|
302
191
|
|
|
303
|
-
// Sidecar split-bundle: same shape as the main path's resources-side load.
|
|
304
192
|
if (this.config.resourcesBundleFile) {
|
|
305
193
|
const resourcesPath = this.config.resourcesBundleFile
|
|
306
194
|
const resourcesData = readFileSyncMaybe(dirname(resourcesPath), basename(resourcesPath))
|
|
@@ -309,40 +197,18 @@ export class State {
|
|
|
309
197
|
}
|
|
310
198
|
}
|
|
311
199
|
}
|
|
312
|
-
// Mirror the post-discovery invariant from the main path: frozen modes must
|
|
313
|
-
// have actually loaded their attestation, otherwise the run would fail open.
|
|
314
200
|
if (this.config.frozenBundle) {
|
|
315
201
|
assert.ok(this.#bundleSources !== null, 'No bundle, but attempting to run in frozen bundle mode')
|
|
316
202
|
}
|
|
317
|
-
// Register
|
|
318
|
-
//
|
|
319
|
-
// throw never leaves a dead reference in the parent's registries.
|
|
320
|
-
// Write-mode sidecars are lockfile CONTRIBUTORS -- their addImport / addFile
|
|
321
|
-
// observations have to land in the unified lockfile, otherwise a `lock=frozen`
|
|
322
|
-
// re-run rejects them as unattested -- so they join #sidecars (which feeds
|
|
323
|
-
// #mergedImports/#mergedFormats and attestedBySidecar). Load-mode sidecars are
|
|
324
|
-
// CONSUMERS: they must NOT contribute to the parent's lockfile (the bundle has
|
|
325
|
-
// its own integrity story, and under `lock=add` with no prior lockfile a
|
|
326
|
-
// load-mode sidecar would otherwise silently make the bundle's content the
|
|
327
|
-
// lockfile's source of truth), but they DO carry bundle bytes that --fs serves at
|
|
328
|
-
// load, so they join the read-only #readSidecars (consulted by getFs*Family only).
|
|
329
|
-
// Frozen sidecars join neither: --fs neither captures nor serves in frozen-bundle
|
|
330
|
-
// mode (fs.js gates capture on writeBundle and serve on loadBundle), and their
|
|
331
|
-
// lockfile cross-check already happened in #mergeBundleMetadata above.
|
|
203
|
+
// Register AFTER every fallible step so a throw leaves no dead reference. Frozen sidecars
|
|
204
|
+
// join neither registry (--fs neither captures nor serves in frozen-bundle mode).
|
|
332
205
|
if (this.config.writeBundle) parentState.registerSidecar(this)
|
|
333
206
|
else if (this.config.loadBundle) parentState.registerReadSidecar(this)
|
|
334
207
|
liveStates().add(this)
|
|
335
208
|
return
|
|
336
209
|
}
|
|
337
210
|
|
|
338
|
-
// skipDiscovery (see the constructor comment): a caller-populated, in-memory State.
|
|
339
|
-
// Skip the entire filesystem-discovery path -- anchor at the given root, read nothing --
|
|
340
|
-
// and let the caller fill in the maps. Only sound for read-only load/ignore modes:
|
|
341
|
-
// there is no on-disk attestation to load or verify, so frozen modes are rejected.
|
|
342
211
|
if (skipDiscovery) {
|
|
343
|
-
// Read-only by contract: a caller-populated in-memory State has no on-disk baseline,
|
|
344
|
-
// so neither frozen verification (nothing to verify against) nor write modes (nothing
|
|
345
|
-
// discovered to write) are meaningful. `stasis build` uses bundle:'load', lock:'ignore'.
|
|
346
212
|
assert.ok(!this.config.frozen && !this.config.frozenBundle && !this.config.writeBundle && !this.config.writeLockfile,
|
|
347
213
|
'skipDiscovery is read-only: incompatible with frozen and write (add/replace) lock/bundle modes')
|
|
348
214
|
this.root = resolve(root)
|
|
@@ -354,9 +220,6 @@ export class State {
|
|
|
354
220
|
return
|
|
355
221
|
}
|
|
356
222
|
|
|
357
|
-
// Preload-uniqueness invariant must be checked before construction proceeds; the
|
|
358
|
-
// actual liveStates() registration happens after construction succeeds so a throw in
|
|
359
|
-
// setup can't leave a half-built (and, for a preload, singleton-claiming) State behind.
|
|
360
223
|
const potentialRoots = []
|
|
361
224
|
let cursor = root
|
|
362
225
|
while (cursor) {
|
|
@@ -370,8 +233,8 @@ export class State {
|
|
|
370
233
|
}
|
|
371
234
|
|
|
372
235
|
if (cursor === process.env.PROJECT_CWD) break // e.g. yarn sets this
|
|
373
|
-
if (existsSync(join(cursor, '.git'))) break
|
|
374
|
-
if (existsSync(join(cursor, 'pnpm-workspace.yaml'))) break
|
|
236
|
+
if (existsSync(join(cursor, '.git'))) break
|
|
237
|
+
if (existsSync(join(cursor, 'pnpm-workspace.yaml'))) break
|
|
375
238
|
const parent = dirname(cursor)
|
|
376
239
|
if (!parent || parent === cursor) break
|
|
377
240
|
cursor = parent
|
|
@@ -379,43 +242,24 @@ export class State {
|
|
|
379
242
|
|
|
380
243
|
// default root is top-level package.json, to opt-in to per-dir create stasis.config.json
|
|
381
244
|
this.root = potentialRoots.at(-1)
|
|
245
|
+
assert.ok(this.root, `stasis: no package.json found at or above ${resolve(root)}; run stasis from within a project`)
|
|
382
246
|
|
|
383
247
|
let loaded = false
|
|
384
248
|
let lockfileLoaded = false
|
|
385
|
-
//
|
|
386
|
-
//
|
|
387
|
-
// for root-detection (so a stale stasis.lock.json at the project root can't shadow
|
|
388
|
-
// the explicit one), then substitute the explicit content once root-detection has
|
|
389
|
-
// committed to a rootDir below.
|
|
249
|
+
// Explicit lockFile: suppress the per-rootDir probe so a stale project-root lockfile can't
|
|
250
|
+
// shadow it; substituted below.
|
|
390
251
|
const explicitLockPath = this.config.lockFile
|
|
391
|
-
// A construction-time `bundleFile`
|
|
392
|
-
//
|
|
393
|
-
//
|
|
394
|
-
// the innermost package.json dir would always match first and win, diverging load's root from
|
|
395
|
-
// the (outer) root the bundle was captured with. Because every bundle key is stored relative
|
|
396
|
-
// to the capture root, that divergence makes every bundled dependency unreachable at load: the
|
|
397
|
-
// load hook throws "outside project root" / "not attested", and Node's ESM->CJS translator --
|
|
398
|
-
// which re-resolves a required dependency through Module._load(absPath, /* no parent */) -- hits
|
|
399
|
-
// native disk resolution for a file the bundle carries but disk (post-prune / never-shipped)
|
|
400
|
-
// does not, i.e. `Cannot find module '<abs node_modules path>'`. Root is instead chosen from
|
|
401
|
-
// rootDir-DEPENDENT signals (stasis.config.json, the default lockfile/bundle at the dir), then
|
|
402
|
-
// the explicit bundle is loaded at the committed root -- or, when nothing else committed, at the
|
|
403
|
-
// outermost root post-loop (mirroring explicitLockPath). A `bundleFile` that comes only from
|
|
404
|
-
// stasis.config.json is NOT suppressed: it lives at a dir already carrying the config signal, so
|
|
405
|
-
// it selects that dir consistently at capture and load alike.
|
|
252
|
+
// A construction-time (flag/env) `bundleFile` is rootDir-INDEPENDENT, so it must NOT be a
|
|
253
|
+
// root-detection signal: picking the innermost dir would diverge load's root from the capture
|
|
254
|
+
// root. A config-only bundleFile is NOT suppressed (its dir already carries the config signal).
|
|
406
255
|
const explicitBundlePath = this.config.bundleFile
|
|
407
256
|
for (const rootDir of potentialRoots) {
|
|
408
257
|
const config = readFileSyncMaybe(rootDir, FILE_CONFIG, 'utf-8')
|
|
409
258
|
const lockProbe = explicitLockPath ? null : readFileSyncMaybe(rootDir, FILE_LOCK, 'utf-8')
|
|
410
|
-
// Read the bundle at the construction-time bundleFile (a --bundle-file flag or
|
|
411
|
-
// EXODUS_STASIS_BUNDLE_FILE) or, absent that, the default <rootDir>/stasis.code.br -- used
|
|
412
|
-
// to LOAD the bundle once a root is committed. The authoritative path can still change when
|
|
413
|
-
// loadConfig() applies a `bundleFile` from stasis.config.json, so it's re-read below.
|
|
414
259
|
let sourcesPath = this.config.bundleFile || join(rootDir, FILE_CODE)
|
|
415
260
|
let sources = readFileSyncMaybe(dirname(sourcesPath), basename(sourcesPath))
|
|
416
|
-
// Root-SELECTION signal: only the DEFAULT <rootDir>/stasis.code.br counts
|
|
417
|
-
//
|
|
418
|
-
// can't bias selection to the innermost dir; it is still loaded via `sources` once committed.
|
|
261
|
+
// Root-SELECTION signal: only the DEFAULT <rootDir>/stasis.code.br counts (see
|
|
262
|
+
// explicitBundlePath); an explicit bundleFile is still loaded via `sources`.
|
|
419
263
|
const bundleProbe = explicitBundlePath ? null : sources
|
|
420
264
|
if (config !== null || lockProbe !== null || bundleProbe !== null) {
|
|
421
265
|
if (loaded) throw new Error('Stasis config already loaded')
|
|
@@ -424,11 +268,7 @@ export class State {
|
|
|
424
268
|
|
|
425
269
|
if (config) {
|
|
426
270
|
this.config.loadConfig(config)
|
|
427
|
-
// stasis.config.json may set `bundleFile
|
|
428
|
-
// construction-time value) could not see. Re-resolve against the now-authoritative
|
|
429
|
-
// config and re-read, so a config-only bundleFile is honored for load/frozen -- the
|
|
430
|
-
// write side (state.write) already resolves bundleFile after loadConfig. This mirrors
|
|
431
|
-
// the resourcesBundleFile read below, which is naturally post-loadConfig.
|
|
271
|
+
// stasis.config.json may set a `bundleFile` the probe couldn't see -- re-resolve and re-read.
|
|
432
272
|
const configuredPath = this.config.bundleFile || join(rootDir, FILE_CODE)
|
|
433
273
|
if (configuredPath !== sourcesPath) {
|
|
434
274
|
sourcesPath = configuredPath
|
|
@@ -436,9 +276,6 @@ export class State {
|
|
|
436
276
|
}
|
|
437
277
|
}
|
|
438
278
|
|
|
439
|
-
// Resolve the effective lockfile content + path for this run. With an explicit
|
|
440
|
-
// `config.lockFile`, the lockfile is at that exact path (loaded once, not at a
|
|
441
|
-
// per-rootDir probe); without one, fall back to `<rootDir>/stasis.lock.json`.
|
|
442
279
|
const lock = explicitLockPath
|
|
443
280
|
? readFileSyncMaybe(dirname(explicitLockPath), basename(explicitLockPath), 'utf-8')
|
|
444
281
|
: lockProbe
|
|
@@ -447,40 +284,22 @@ export class State {
|
|
|
447
284
|
lockfileLoaded = this.#absorbLockfile(lock, lockPath)
|
|
448
285
|
this.#loadBundleArtifacts(sources, sourcesPath, lockfileLoaded)
|
|
449
286
|
|
|
450
|
-
//
|
|
451
|
-
//
|
|
452
|
-
// this break an outer candidate root re-detects the SAME bundle file at its own probe and
|
|
453
|
-
// trips the `loaded` guard above -- which crashed bundle=load/frozen in any nested-package
|
|
454
|
-
// (monorepo) layout where a leaf package sits under an ancestor that also has a package.json.
|
|
287
|
+
// Innermost matching rootDir wins (per-dir opt-in); without this break an outer root
|
|
288
|
+
// re-detects a rootDir-independent bundleFile and trips `loaded`.
|
|
455
289
|
break
|
|
456
290
|
}
|
|
457
291
|
}
|
|
458
292
|
|
|
459
|
-
// Explicit lockfile
|
|
460
|
-
//
|
|
461
|
-
// the explicit lockfile here against the already-resolved `this.root` (outermost
|
|
462
|
-
// package.json) so the run still has its attestation.
|
|
293
|
+
// Explicit lockfile but no discovery indicator: the loop never ran, so absorb here against
|
|
294
|
+
// `this.root` (outermost) so the run still has attestation.
|
|
463
295
|
if (!loaded && explicitLockPath) {
|
|
464
296
|
const lock = readFileSyncMaybe(dirname(explicitLockPath), basename(explicitLockPath), 'utf-8')
|
|
465
297
|
lockfileLoaded = this.#absorbLockfile(lock, explicitLockPath)
|
|
466
298
|
}
|
|
467
299
|
|
|
468
|
-
// Explicit
|
|
469
|
-
//
|
|
470
|
-
//
|
|
471
|
-
// package, so the loop never loaded it. Load it here against `this.root` (the outermost
|
|
472
|
-
// package.json) -- the same root capture commits to when no signal exists -- so its
|
|
473
|
-
// capture-root-relative keys line up with how they're looked up. `resourcesBundleFile` is in
|
|
474
|
-
// the gate for the resources-only split shape (#absorbResourcesBundle documents it as legal):
|
|
475
|
-
// with no code bundle on disk there is no other reason to reach the loader here, yet the
|
|
476
|
-
// explicit resources half must still be absorbed. Post-loop, both paths are necessarily
|
|
477
|
-
// flag/env: a stasis.config.json could only have set them by also committing the loop.
|
|
478
|
-
//
|
|
479
|
-
// NB: the outermost root depends on where the upward walk stopped (PROJECT_CWD, .git,
|
|
480
|
-
// pnpm-workspace.yaml -- see potentialRoots above). Capture and load must agree on that
|
|
481
|
-
// boundary for the keys to line up: e.g. a capture under yarn (PROJECT_CWD set to an inner
|
|
482
|
-
// workspace dir) and a bare load of the same bundle (walks up to the repo's .git) commit
|
|
483
|
-
// different roots and fail with "outside the project root" / "not attested".
|
|
300
|
+
// Explicit bundleFile suppressed as a root signal above: load against `this.root` (outermost).
|
|
301
|
+
// Capture and load MUST agree on where the upward walk stops (PROJECT_CWD/.git/
|
|
302
|
+
// pnpm-workspace.yaml), else they commit different roots and fail "outside the project root".
|
|
484
303
|
if (!loaded && (explicitBundlePath || this.config.resourcesBundleFile)) {
|
|
485
304
|
const sources = explicitBundlePath
|
|
486
305
|
? readFileSyncMaybe(dirname(explicitBundlePath), basename(explicitBundlePath))
|
|
@@ -488,23 +307,11 @@ export class State {
|
|
|
488
307
|
this.#loadBundleArtifacts(sources, explicitBundlePath, lockfileLoaded)
|
|
489
308
|
}
|
|
490
309
|
|
|
491
|
-
//
|
|
492
|
-
// after the discovery loop -- rather than only inside it closes a fail-open: with
|
|
493
|
-
// no stasis files on disk at all the loop body never runs, so a per-rootDir guard
|
|
494
|
-
// would never fire, and in node_modules scope the workspace entry sits outside the
|
|
495
|
-
// attested zone (its bytes/format/resolutions aren't checked), letting it execute
|
|
496
|
-
// unverified. A frozen run with nothing to verify against must fail closed instead.
|
|
310
|
+
// Post-loop: with no stasis files the loop never runs, and a frozen run must fail closed.
|
|
497
311
|
if (this.config.frozen) assert.ok(lockfileLoaded, 'No lockfile, but attempting to run in frozen mode')
|
|
498
312
|
if (this.config.frozenBundle) assert.ok(this.#bundleSources !== null, 'No bundle, but attempting to run in frozen bundle mode')
|
|
499
313
|
|
|
500
|
-
// Claim
|
|
501
|
-
// so a sidecar OR an independent top-level State -- in this OR another stasis-core
|
|
502
|
-
// copy -- can't silently target the same on-disk file. Load / frozen / no-bundle modes
|
|
503
|
-
// don't claim (they legitimately read what a previous writer produced). lockFile is
|
|
504
|
-
// claimed only by writing top-level States -- sidecars don't write the lockfile, and
|
|
505
|
-
// the explicit path may legitimately point at the parent's under unification. The
|
|
506
|
-
// State itself is added to liveStates() below, AFTER every fallible step (including
|
|
507
|
-
// these claims), so a throw during discovery/parsing/claiming never leaves a dead member.
|
|
314
|
+
// Claim write targets so no other live State (any copy) silently targets the same file.
|
|
508
315
|
if (this.config.writeBundle && this.config.bundleFile) {
|
|
509
316
|
this.#claimWritePath(`bundleFile '${this.config.bundleFile}'`, this.config.bundleFile)
|
|
510
317
|
}
|
|
@@ -515,10 +322,6 @@ export class State {
|
|
|
515
322
|
this.#claimWritePath(`lockFile '${this.config.lockFile}'`, this.config.lockFile)
|
|
516
323
|
}
|
|
517
324
|
|
|
518
|
-
// preloadRoot is opt-in -- only the runtime loader (hooks.js's initState)
|
|
519
|
-
// sets it, pointing at stasis-core's own package root. Plain top-level
|
|
520
|
-
// States constructed via the CLI / direct API leave it null and skip
|
|
521
|
-
// the internal-edges backfill entirely.
|
|
522
325
|
if (preloadRoot !== undefined) {
|
|
523
326
|
assert.equal(typeof preloadRoot, 'string', 'preloadRoot must be a string')
|
|
524
327
|
this.#preloadRoot = preloadRoot
|
|
@@ -527,12 +330,8 @@ export class State {
|
|
|
527
330
|
liveStates().add(this)
|
|
528
331
|
}
|
|
529
332
|
|
|
530
|
-
// Absorb a lockfile's attestation
|
|
531
|
-
// imports
|
|
532
|
-
// attestations. Shared by the discovery loop (a committed rootDir) and the post-loop fallback
|
|
533
|
-
// for an explicit `config.lockFile` -- a rootDir-independent path, so it is suppressed as a
|
|
534
|
-
// root-detection signal and loaded at the outermost root instead. Returns whether the lockfile
|
|
535
|
-
// was actually absorbed (the caller's `lockfileLoaded`).
|
|
333
|
+
// Absorb a lockfile's attestation; returns whether it was actually absorbed. Lockfile.parse
|
|
334
|
+
// requires both imports and formats, so an absorbed lockfile always attests them.
|
|
536
335
|
#absorbLockfile(lock, lockPath) {
|
|
537
336
|
if (lock && !this.config.useLockfile && !this.config.ignoreLockfile) {
|
|
538
337
|
throw new Error(`Unexpected ${lockPath} with config.lock = 'none'`)
|
|
@@ -543,7 +342,7 @@ export class State {
|
|
|
543
342
|
|
|
544
343
|
const includeSources = lockfile.config.scope === 'full' && this.config.full
|
|
545
344
|
for (const [dir, info] of lockfile.modules) {
|
|
546
|
-
if (!dir
|
|
345
|
+
if (!hasNodeModulesSegment(dir) && !includeSources) continue
|
|
547
346
|
this.modules.set(dir, info)
|
|
548
347
|
}
|
|
549
348
|
if (includeSources) this.entries = lockfile.entries
|
|
@@ -555,50 +354,26 @@ export class State {
|
|
|
555
354
|
}
|
|
556
355
|
this.#lockImports = lockfile.imports
|
|
557
356
|
this.#lockFormats = lockfile.formats
|
|
558
|
-
//
|
|
559
|
-
//
|
|
560
|
-
|
|
561
|
-
// regenerated.
|
|
562
|
-
if (this.config.frozen && this.#lockImports === null) {
|
|
563
|
-
console.warn('[stasis] Warning: lockfile does not attest resolutions; they are trusted as-is. Regenerate the lockfile to enable this check.')
|
|
564
|
-
}
|
|
565
|
-
if (this.config.frozen && this.#lockFormats === null) {
|
|
566
|
-
console.warn('[stasis] Warning: lockfile does not attest formats; they are trusted as-is. Regenerate the lockfile to enable this check.')
|
|
567
|
-
}
|
|
357
|
+
// Seeded into the live set (not a separate baseline) so re-reading a file this run can CLEAR
|
|
358
|
+
// a stale exec bit.
|
|
359
|
+
this.#absorbExecutable(lockfile)
|
|
568
360
|
this.#lockfileLoaded = true
|
|
569
361
|
return true
|
|
570
362
|
}
|
|
571
363
|
|
|
572
|
-
// Gate and load the bundle artifacts for
|
|
573
|
-
//
|
|
574
|
-
// one is configured. Shared by the discovery loop and the post-loop explicit-path fallback so
|
|
575
|
-
// the mode guards cannot drift between the two. `sources` may be null -- no bundle on disk, or
|
|
576
|
-
// a resources-only deployment -- in which case only the resources half applies.
|
|
577
|
-
//
|
|
578
|
-
// The missing-lockfile guard tests `lockfileLoaded` where the pre-extraction in-loop code
|
|
579
|
-
// tested raw lockfile presence; equivalent at this call point, since whenever the guard can
|
|
580
|
-
// fire (useLockfile && !replaceLockfile) #absorbLockfile absorbed the lockfile iff it existed.
|
|
364
|
+
// Gate and load the bundle artifacts for a committed root; `sources` may be null (no bundle, or
|
|
365
|
+
// resources-only).
|
|
581
366
|
#loadBundleArtifacts(sources, sourcesPath, lockfileLoaded) {
|
|
582
367
|
if (sources && !this.config.writeBundle && !this.config.loadBundle && !this.config.ignoreBundle && !this.config.frozenBundle) {
|
|
583
368
|
throw new Error(`Unexpected ${sourcesPath} with config.bundle = 'none'`)
|
|
584
369
|
}
|
|
585
|
-
// A frozen bundle is self-attesting
|
|
586
|
-
// own bytes, so (unlike load/add) it needs no sibling lockfile and is exempt
|
|
587
|
-
// from this "a lockfile is required before a bundle's sources can be trusted"
|
|
588
|
-
// guard. This is what lets bundle=frozen compose with lock=add (bootstrapping a
|
|
589
|
-
// fresh lockfile from the verified run) without a pre-existing lockfile.
|
|
370
|
+
// A frozen bundle is self-attesting, so it needs no sibling lockfile and is exempt here.
|
|
590
371
|
if (sources && !lockfileLoaded && this.config.useLockfile && !this.config.replaceLockfile && !this.config.frozenBundle) {
|
|
591
372
|
throw new Error('stasis.lock.json missing, can not use sources')
|
|
592
373
|
}
|
|
593
374
|
if (sources && (this.config.writeBundle || this.config.loadBundle || this.config.frozenBundle) && !this.config.replaceBundle) {
|
|
594
375
|
this.#absorbCodeBundle(sources, sourcesPath, lockfileLoaded)
|
|
595
376
|
}
|
|
596
|
-
// Split-bundle layout: when `resourcesBundleFile` is configured, resources
|
|
597
|
-
// live in a separate file. Load it as a standalone Bundle (entries empty,
|
|
598
|
-
// imports empty, formats restricted to resource tags) and union into the
|
|
599
|
-
// state already absorbed from `bundleFile`. The strict-shape asserts catch
|
|
600
|
-
// a writer that accidentally leaked code into the resources file or a
|
|
601
|
-
// tampered file claiming entries/imports it shouldn't have.
|
|
602
377
|
if (this.config.resourcesBundleFile) {
|
|
603
378
|
const resourcesPath = this.config.resourcesBundleFile
|
|
604
379
|
const resourcesData = readFileSyncMaybe(dirname(resourcesPath), basename(resourcesPath))
|
|
@@ -608,26 +383,20 @@ export class State {
|
|
|
608
383
|
}
|
|
609
384
|
}
|
|
610
385
|
|
|
611
|
-
// Absorb a unified code+resource bundle's metadata and payloads into this State. Shared by the
|
|
612
|
-
// discovery loop (a committed rootDir carrying a stasis signal) and the post-loop fallback that
|
|
613
|
-
// loads an explicit (rootDir-independent) bundleFile against the outermost root.
|
|
614
386
|
#absorbCodeBundle(sources, sourcesPath, lockfileLoaded) {
|
|
615
|
-
// One unified bundle carries both code and resources. Split the flat file
|
|
616
|
-
// view into this.sources (code) and this.resources (resource payloads --
|
|
617
|
-
// raw UTF-8 for 'resource', base64 for 'resource:base64') by format.
|
|
618
387
|
const bundle = Bundle.parse(brotliDecompressSync(sources).toString('utf-8'))
|
|
619
|
-
//
|
|
620
|
-
//
|
|
621
|
-
// path is stricter: a v0 bundle has no per-file `formats` attestation
|
|
622
|
-
// (resources can't be distinguished from code, the loader can't pick
|
|
623
|
-
// module-vs-commonjs) and no import map (resolution edges go unchecked),
|
|
624
|
-
// so serving / verifying one under `stasis run` would silently widen the
|
|
625
|
-
// trust boundary. Refuse it explicitly and point at the upgrade path.
|
|
388
|
+
// Bundle.parse accepts v0 for offline tooling; the runtime refuses it -- v0 has no per-file
|
|
389
|
+
// formats or import map, so serving one widens the trust boundary.
|
|
626
390
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
627
391
|
`stasis run requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
|
|
628
392
|
`Re-bundle with the current stasis (\`stasis bundle\`) or \`stasis run --bundle=replace\` ` +
|
|
629
393
|
`against a v0-free starting point to upgrade.`)
|
|
630
394
|
assert.equal(bundle.config.scope, this.config.scope)
|
|
395
|
+
// assertEntry short-circuits on an empty set, so a runnable full-scope code bundle must declare
|
|
396
|
+
// an entry here -- else any bundled file could run as the root. Add-only bundles have none.
|
|
397
|
+
assert.ok(!this.config.full || bundle.entries.size > 0 || !bundle.hasCode,
|
|
398
|
+
`${sourcesPath}: a full-scope bundle carrying code must declare an entry to run ` +
|
|
399
|
+
`(an add-only attestation has none; run a bundle built by \`stasis bundle\`/\`stasis run\`)`)
|
|
631
400
|
this.#mergeBundleMetadata(bundle, { lockfileLoaded })
|
|
632
401
|
for (const [file, content] of bundle.sources) {
|
|
633
402
|
if (Bundle.isResourceFormat(bundle.formats.get(file))) this.resources.set(file, content)
|
|
@@ -635,12 +404,11 @@ export class State {
|
|
|
635
404
|
}
|
|
636
405
|
this.formats = bundle.formats
|
|
637
406
|
this.imports = bundle.imports
|
|
407
|
+
this.#absorbExecutable(bundle)
|
|
408
|
+
this.#seedReasonFromBundle(bundle)
|
|
638
409
|
if (this.config.frozenBundle) {
|
|
639
|
-
// Snapshot
|
|
640
|
-
//
|
|
641
|
-
// which addImport extends); formats is a flat file->string map, so a shallow
|
|
642
|
-
// copy suffices. Code and resource file sets are tracked separately to match
|
|
643
|
-
// addFile's per-kind frozen-bundle membership check.
|
|
410
|
+
// Snapshot before addFile/addImport mutate the live maps; imports is deep-cloned because
|
|
411
|
+
// this.imports shares bundle.imports's nested Maps.
|
|
644
412
|
this.#bundleSources = new Set(this.sources.keys())
|
|
645
413
|
this.#bundleResources = new Set(this.resources.keys())
|
|
646
414
|
this.#bundleImports = objectToMaps(fileMapToObject(bundle.imports))
|
|
@@ -648,18 +416,8 @@ export class State {
|
|
|
648
416
|
}
|
|
649
417
|
}
|
|
650
418
|
|
|
651
|
-
// Cross-check bundle metadata (entries/modules)
|
|
652
|
-
//
|
|
653
|
-
// v0 bundles infer `name` from path for node_modules buckets but carry no
|
|
654
|
-
// version; the workspace "." bucket has no inferable name. We skip entries
|
|
655
|
-
// we can't cross-check (no name with lockfile, or no version when populating
|
|
656
|
-
// state.modules — addFile later asserts strict name+version equality against
|
|
657
|
-
// disk's package.json, which would clash with a null we stashed earlier).
|
|
658
|
-
// Note: the path-inferred name for v0 will disagree with the lockfile when a
|
|
659
|
-
// dep is installed under an alias (`foo: npm:bar` writes `node_modules/foo`
|
|
660
|
-
// with `name: "bar"` in package.json); the strict equality below will fail
|
|
661
|
-
// in that case, which is the right call — the v0 bundle has no way to
|
|
662
|
-
// attest the real package identity, so we refuse to silently mismatch.
|
|
419
|
+
// Cross-check bundle metadata (entries/modules) against the loaded lockfile, or absorb it as
|
|
420
|
+
// source of truth when none. v0 buckets carry no version, so unverifiable entries are skipped.
|
|
663
421
|
#mergeBundleMetadata(bundle, { lockfileLoaded }) {
|
|
664
422
|
if (lockfileLoaded) {
|
|
665
423
|
if (bundle.entries.size > 0) {
|
|
@@ -679,13 +437,8 @@ export class State {
|
|
|
679
437
|
assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
|
|
680
438
|
}
|
|
681
439
|
}
|
|
682
|
-
//
|
|
683
|
-
// the lockfile
|
|
684
|
-
// alone don't close this hole: a tampered bundle could redirect a
|
|
685
|
-
// specifier to a *different* hash-valid file. Unknown edges are fatal.
|
|
686
|
-
// The lockfile may attest a superset (same stance as the file lists
|
|
687
|
-
// above). Lockfiles that predate resolution attestation
|
|
688
|
-
// (imports === null) skip the check -- regenerate with lock=replace.
|
|
440
|
+
// Hashes alone don't stop a redirect to a different hash-valid file, so every edge the bundle
|
|
441
|
+
// could serve must match the lockfile's attested target (a lockfile superset is fine).
|
|
689
442
|
if (this.#lockImports !== null) {
|
|
690
443
|
for (const [conditions, byParent] of bundle.imports) {
|
|
691
444
|
for (const [parent, specifiers] of byParent) {
|
|
@@ -695,27 +448,14 @@ export class State {
|
|
|
695
448
|
}
|
|
696
449
|
}
|
|
697
450
|
}
|
|
698
|
-
//
|
|
699
|
-
//
|
|
700
|
-
// bundle can change how hash-valid bytes parse/run without touching a
|
|
701
|
-
// hash. Every format the bundle declares must match the lockfile's. The
|
|
702
|
-
// bundle's file set is a subset of the lockfile, so an unattested format
|
|
703
|
-
// is fatal here (it can only mean a forged entry).
|
|
451
|
+
// A format flip changes how hash-valid bytes parse (module<->commonjs) without touching a
|
|
452
|
+
// hash, so every bundle format must match the lockfile's; unattested is fatal.
|
|
704
453
|
if (this.#lockFormats !== null) {
|
|
705
454
|
for (const [file, format] of bundle.formats) {
|
|
706
455
|
this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'bundle', source: 'lockfile' })
|
|
707
456
|
}
|
|
708
|
-
// Inverse
|
|
709
|
-
//
|
|
710
|
-
// this, a tampered bundle could OMIT the resource tag entirely -- State.load
|
|
711
|
-
// would then see no resource format, route the base64 payload through
|
|
712
|
-
// `this.sources` (the code path), and hand it to the loader. Caught at the
|
|
713
|
-
// loader's NODEJS_FORMATS gate too, but checking here surfaces it as a clean
|
|
714
|
-
// bundle/lockfile mismatch rather than as an opaque "unknown format" later.
|
|
715
|
-
// (We don't extend this to code formats: doing so would mean asserting
|
|
716
|
-
// `formats` is total over `modules`, which is a stronger schema invariant
|
|
717
|
-
// than the historical "format may be missing per file" contract -- and the
|
|
718
|
-
// code-format hole is closed by the loader allowlist anyway.)
|
|
457
|
+
// Inverse: a lockfile-tagged resource MUST be tagged in the bundle too, else omitting the
|
|
458
|
+
// tag routes the base64 payload through this.sources (code) to the loader.
|
|
719
459
|
for (const [dir, { files }] of bundle.modules) {
|
|
720
460
|
for (const rel of Object.keys(files)) {
|
|
721
461
|
const file = moduleFileKey(dir, rel)
|
|
@@ -727,16 +467,12 @@ export class State {
|
|
|
727
467
|
}
|
|
728
468
|
}
|
|
729
469
|
} else {
|
|
730
|
-
// Mutate in place
|
|
731
|
-
// reference with a sidecar's parent (the unified-lockfile model), and a
|
|
732
|
-
// reassignment would silently fork the shared set.
|
|
470
|
+
// Mutate in place: this.entries may be shared by reference with a sidecar's parent.
|
|
733
471
|
for (const e of bundle.entries) this.entries.add(e)
|
|
734
472
|
for (const [dir, info] of bundle.modules) {
|
|
735
473
|
if (!info.name || !info.version) continue // partial metadata
|
|
736
474
|
if (this.modules.has(dir)) {
|
|
737
|
-
// A
|
|
738
|
-
// entries; if the same dir is added twice (e.g. legacy duplicated metadata)
|
|
739
|
-
// they must agree on name/version.
|
|
475
|
+
// A dir may be added twice (code + resource entries), and both must agree.
|
|
740
476
|
const existing = this.modules.get(dir)
|
|
741
477
|
assert.equal(info.name, existing.name, `bundle ${dir} name mismatch`)
|
|
742
478
|
assert.equal(info.version, existing.version, `bundle ${dir} version mismatch`)
|
|
@@ -749,23 +485,15 @@ export class State {
|
|
|
749
485
|
}
|
|
750
486
|
}
|
|
751
487
|
|
|
752
|
-
// Load the resources
|
|
753
|
-
//
|
|
754
|
-
// code-format declarations). Unions into the state already absorbed from
|
|
755
|
-
// `bundleFile`, extending this.resources, this.formats, and -- under
|
|
756
|
-
// frozenBundle -- the attestation snapshots. The shape asserts catch a writer
|
|
757
|
-
// that leaked code into the resources file or a tampered file claiming
|
|
758
|
-
// metadata it shouldn't carry.
|
|
488
|
+
// Load the resources half of a split-bundle layout: ONLY resource-format files, unioned into the
|
|
489
|
+
// bundleFile state.
|
|
759
490
|
#absorbResourcesBundle(resourcesData, { lockfileLoaded, resourcesPath }) {
|
|
760
491
|
const bundle = Bundle.parse(brotliDecompressSync(resourcesData).toString('utf-8'))
|
|
761
492
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
762
493
|
`stasis run requires a v1 resources bundle; ${resourcesPath} is v${bundle.version}. ` +
|
|
763
494
|
`Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point to upgrade.`)
|
|
764
495
|
assert.equal(bundle.config.scope, this.config.scope)
|
|
765
|
-
//
|
|
766
|
-
// belong to the code bundle. A populated map here means the writer mixed
|
|
767
|
-
// halves (or the file was tampered with) and serving it would silently widen
|
|
768
|
-
// trust on the code-bundle side's metadata.
|
|
496
|
+
// A populated imports/entries map means mixed halves or tampering -- widens trust on the code side.
|
|
769
497
|
assert.equal(bundle.imports.size, 0, `resources bundle ${resourcesPath} must have empty imports`)
|
|
770
498
|
assert.equal(bundle.entries.size, 0, `resources bundle ${resourcesPath} must have empty entries`)
|
|
771
499
|
for (const [file, format] of bundle.formats) {
|
|
@@ -776,19 +504,18 @@ export class State {
|
|
|
776
504
|
for (const [file, content] of bundle.sources) {
|
|
777
505
|
this.resources.set(file, content)
|
|
778
506
|
}
|
|
779
|
-
// Union formats:
|
|
780
|
-
// resource formats -- the file sets must be disjoint).
|
|
507
|
+
// Union formats: the two halves' file sets must be disjoint.
|
|
781
508
|
for (const [file, format] of bundle.formats) {
|
|
782
509
|
const existing = this.formats.get(file)
|
|
783
510
|
assert.ok(existing === undefined || existing === format,
|
|
784
511
|
`format conflict for ${file}: bundleFile declares '${existing}', resourcesBundleFile declares '${format}'`)
|
|
785
512
|
this.formats.set(file, format)
|
|
786
513
|
}
|
|
514
|
+
this.#absorbExecutable(bundle)
|
|
515
|
+
this.#seedReasonFromBundle(bundle)
|
|
787
516
|
if (this.config.frozenBundle) {
|
|
788
|
-
// Extend the frozen snapshot
|
|
789
|
-
//
|
|
790
|
-
// so initialize lazily; #bundleSources may stay null in that case, which the
|
|
791
|
-
// post-discovery invariant check accepts because #bundleResources is set.
|
|
517
|
+
// Extend the frozen snapshot, init lazily: resources-only deployments are legal, so
|
|
518
|
+
// bundleFile may not have populated these.
|
|
792
519
|
if (this.#bundleResources === null) this.#bundleResources = new Set()
|
|
793
520
|
if (this.#bundleFormats === null) this.#bundleFormats = new Map()
|
|
794
521
|
if (this.#bundleImports === null) this.#bundleImports = objectToMaps(fileMapToObject(this.imports))
|
|
@@ -798,17 +525,14 @@ export class State {
|
|
|
798
525
|
}
|
|
799
526
|
}
|
|
800
527
|
|
|
801
|
-
//
|
|
802
|
-
//
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
//
|
|
808
|
-
//
|
|
809
|
-
// fatal: either target would be served under conditions where the other is
|
|
810
|
-
// the attested resolution. Unknown edges are fatal unless the caller opts out
|
|
811
|
-
// (see addImport's node_modules-scope carve-out).
|
|
528
|
+
// Union (never replace) an absorbed artifact's `executable` list into the live set: a lockfile
|
|
529
|
+
// seed, or the code half of a split layout, may already have contributed.
|
|
530
|
+
#absorbExecutable(artifact) {
|
|
531
|
+
for (const file of artifact.executable) this.executable.add(file)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Verify one resolution edge against an attestation map: exact conditions key first, else accept
|
|
535
|
+
// only if every condition set agrees on one target. Unknown edges fatal unless tolerateUnknown.
|
|
812
536
|
#assertAttestedResolution(attestation, conditions, parent, specifier, file, { what, source = 'lockfile', tolerateUnknown = false }) {
|
|
813
537
|
const edge = `'${specifier}' from ${parent} (${conditions})`
|
|
814
538
|
let attested = attestation.get(conditions)?.get(parent)?.get(specifier)
|
|
@@ -823,21 +547,16 @@ export class State {
|
|
|
823
547
|
assert.ok(targets.size === 1, `${what} resolution ${edge} is attested inconsistently across condition sets in the ${source}`)
|
|
824
548
|
;[attested] = targets
|
|
825
549
|
}
|
|
826
|
-
// A per-platform edge is a Map<platform, target
|
|
827
|
-
//
|
|
828
|
-
// lockfile's) would never match. A flat string edge stays a plain ===; a Map-vs-string
|
|
829
|
-
// (e.g. a per-platform attestation vs a single observed runtime target) is a mismatch.
|
|
550
|
+
// A per-platform edge is a Map<platform, target>: compare structurally (assert.equal is
|
|
551
|
+
// reference equality).
|
|
830
552
|
const matches = file instanceof Map && attested instanceof Map
|
|
831
553
|
? file.size === attested.size && [...file].every(([platform, target]) => attested.get(platform) === target)
|
|
832
554
|
: file === attested
|
|
833
555
|
assert.ok(matches, `${what} resolution ${edge} mismatches the ${source}`)
|
|
834
556
|
}
|
|
835
557
|
|
|
836
|
-
// Verify one file's loader format
|
|
837
|
-
//
|
|
838
|
-
// invoke this for files that must be attested (the attestation is a superset
|
|
839
|
-
// of any bundle, and on the disk path the call is gated to the hash-attested
|
|
840
|
-
// zone), so an unattested file means a forged/extra entry and is fatal.
|
|
558
|
+
// Verify one file's loader format. Callers only pass files that must be attested, so an
|
|
559
|
+
// unattested file means a forged/extra entry -- fatal.
|
|
841
560
|
#assertAttestedFormat(attestation, file, format, { what, source = 'lockfile' }) {
|
|
842
561
|
const attested = attestation.get(file)
|
|
843
562
|
assert.ok(attested !== undefined, `${what} format for ${file} is not attested by the ${source}`)
|
|
@@ -845,39 +564,30 @@ export class State {
|
|
|
845
564
|
}
|
|
846
565
|
|
|
847
566
|
assertEntry(url) {
|
|
848
|
-
//
|
|
849
|
-
//
|
|
850
|
-
// so this fallback only ever triggers for the legacy path.
|
|
567
|
+
// No entry attestation to check against (lock=none/ignore, fresh build) -- #absorbCodeBundle
|
|
568
|
+
// already refuses a runnable code bundle that declares none.
|
|
851
569
|
if (this.entries.size === 0) return
|
|
852
570
|
const file = this.#canonicalFile(url)
|
|
853
571
|
assert.ok(this.entries.has(file), `Unknown entry point: ${file}`)
|
|
854
572
|
}
|
|
855
573
|
|
|
856
|
-
// The one preload (top-level) State, or undefined. Scans the process-wide registry
|
|
857
|
-
// (shared across stasis-core copies) for the member flagged isPreload.
|
|
858
574
|
static get preload() {
|
|
859
575
|
for (const state of liveStates()) if (state.isPreload) return state
|
|
860
576
|
return undefined
|
|
861
577
|
}
|
|
862
578
|
|
|
863
|
-
//
|
|
864
|
-
// it even when this State came from a different stasis-core copy.
|
|
579
|
+
// Public so State.preload works across copies.
|
|
865
580
|
get isPreload() {
|
|
866
581
|
return this.#isPreload
|
|
867
582
|
}
|
|
868
583
|
|
|
869
|
-
//
|
|
870
|
-
// Public so #claimWritePath's collision scan can consult a State from another copy.
|
|
584
|
+
// Public for #claimWritePath's cross-copy collision scan.
|
|
871
585
|
claimedWritePathLabel(canonical) {
|
|
872
586
|
return this.#claims.get(canonical)
|
|
873
587
|
}
|
|
874
588
|
|
|
875
|
-
//
|
|
876
|
-
//
|
|
877
|
-
// registry, so collisions are caught across copies and without a preload. Intra-State
|
|
878
|
-
// collisions are caught earlier by Config (#checkInvariants), so this only fires
|
|
879
|
-
// cross-State. The claim is recorded on this instance; it becomes visible to later
|
|
880
|
-
// States once this one is added to liveStates() at the end of construction.
|
|
589
|
+
// Refuse a path another live State (ANY copy) already claims; canonicalized so `./x`/`x`/symlinks
|
|
590
|
+
// compare equal.
|
|
881
591
|
#claimWritePath(label, value) {
|
|
882
592
|
if (!value) return
|
|
883
593
|
const canonical = canonicalizePath(value)
|
|
@@ -888,19 +598,13 @@ export class State {
|
|
|
888
598
|
this.#claims.set(canonical, label)
|
|
889
599
|
}
|
|
890
600
|
|
|
891
|
-
//
|
|
892
|
-
// exactly matches its parent's even when the two are different module copies
|
|
893
|
-
// sharing the process-wide registry (see the constructor's parent branch).
|
|
601
|
+
// Public so a sidecar can verify it matches its parent's across copies.
|
|
894
602
|
get version() {
|
|
895
603
|
return VERSION
|
|
896
604
|
}
|
|
897
605
|
|
|
898
|
-
//
|
|
899
|
-
//
|
|
900
|
-
// different-but-version-matched stasis-core copy -- a bundle carrying its own
|
|
901
|
-
// stasis-core, whose plugin sidecars the outer loader's preload -- can read it across
|
|
902
|
-
// the class brand. Only ever called by the sidecar constructor, after its version
|
|
903
|
-
// assert; the exact-version match guarantees these shapes are compatible.
|
|
606
|
+
// PUBLIC (not private-field reads) so a sidecar from a different copy can read it across the
|
|
607
|
+
// class brand.
|
|
904
608
|
sidecarInheritance() {
|
|
905
609
|
return {
|
|
906
610
|
preloadRoot: this.#preloadRoot,
|
|
@@ -910,20 +614,12 @@ export class State {
|
|
|
910
614
|
}
|
|
911
615
|
}
|
|
912
616
|
|
|
913
|
-
//
|
|
914
|
-
// imports/formats (and attestedBySidecar sees its captures). Public (not a direct
|
|
915
|
-
// `#sidecars.add`) for the same cross-copy reason as sidecarInheritance: the call runs
|
|
916
|
-
// on the parent, touching the parent's own #sidecars, so it works even when the sidecar
|
|
917
|
-
// is a foreign-copy instance.
|
|
617
|
+
// WRITE-mode sidecar: this parent's lockData unions its imports/formats. Public (cross-copy).
|
|
918
618
|
registerSidecar(sidecar) {
|
|
919
619
|
this.#sidecars.add(sidecar)
|
|
920
620
|
}
|
|
921
621
|
|
|
922
|
-
//
|
|
923
|
-
// bytes from it (a --fs read at load of a file the sidecar carries) but, unlike
|
|
924
|
-
// registerSidecar, it never enters #mergedImports/#mergedFormats, so a load-mode sidecar
|
|
925
|
-
// contributes no resolution edges or formats (the lockfile's trust-bearing content) to the
|
|
926
|
-
// unified lockfile. Public for the same cross-copy reason as registerSidecar.
|
|
622
|
+
// LOAD-mode sidecar: served by getFs*Family, never a lockfile contributor. Public (cross-copy).
|
|
927
623
|
registerReadSidecar(sidecar) {
|
|
928
624
|
this.#readSidecars.add(sidecar)
|
|
929
625
|
}
|
|
@@ -939,17 +635,27 @@ export class State {
|
|
|
939
635
|
assert.ok(absolute)
|
|
940
636
|
const file = relative(this.root, absolute)
|
|
941
637
|
assert.ok(!file.startsWith('..'))
|
|
942
|
-
|
|
638
|
+
// The project root is keyed '.', never '' -- mixing the two desyncs write from read.
|
|
639
|
+
return file === '' ? '.' : file
|
|
943
640
|
}
|
|
944
641
|
|
|
945
|
-
//
|
|
946
|
-
//
|
|
947
|
-
//
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
// untouched: plain sources, real dependencies, and node_modules -> node_modules
|
|
951
|
-
// links (e.g. pnpm's `.pnpm` store) keep their original path.
|
|
642
|
+
// Memoized #canonicalUncached (per URL, State-lifetime): a realpathSync per node_modules URL, and
|
|
643
|
+
// hot callers repeat relentlessly. Sound only because the tree holds still mid-run; the cached
|
|
644
|
+
// result is shared -- never mutate it.
|
|
645
|
+
#canonicalCache = new Map()
|
|
646
|
+
|
|
952
647
|
#canonical(url) {
|
|
648
|
+
let result = this.#canonicalCache.get(url)
|
|
649
|
+
if (result === undefined) {
|
|
650
|
+
result = this.#canonicalUncached(url)
|
|
651
|
+
this.#canonicalCache.set(url, result)
|
|
652
|
+
}
|
|
653
|
+
return result
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// A node_modules file whose REAL path is outside node_modules is a linked-in workspace source
|
|
657
|
+
// (pnpm): canonicalize so it's recorded as a source, not a dependency. Everything else passes through.
|
|
658
|
+
#canonicalUncached(url) {
|
|
953
659
|
const absolute = this.absolute(url)
|
|
954
660
|
const file = relative(this.root, absolute)
|
|
955
661
|
if (file.startsWith('..') || !splitNodeModulesPath(file)) return { url, absolute }
|
|
@@ -959,27 +665,18 @@ export class State {
|
|
|
959
665
|
} catch {
|
|
960
666
|
return { url, absolute }
|
|
961
667
|
}
|
|
962
|
-
if (real === absolute) return { url, absolute }
|
|
668
|
+
if (real === absolute) return { url, absolute }
|
|
963
669
|
const realFile = relative(this.root, real)
|
|
964
|
-
// Outside the root, or still inside some node_modules (a real dependency,
|
|
965
|
-
// e.g. pnpm's `.pnpm` store): leave the recorded path unchanged.
|
|
966
670
|
if (realFile.startsWith('..') || splitNodeModulesPath(realFile)) return { url, absolute }
|
|
967
671
|
return { url: pathToFileURL(real).toString(), absolute: real }
|
|
968
672
|
}
|
|
969
673
|
|
|
970
|
-
// The canonical project-relative path a URL is recorded/looked up under.
|
|
971
674
|
#canonicalFile(url) {
|
|
972
675
|
return this.relative(this.#canonical(url).absolute)
|
|
973
676
|
}
|
|
974
677
|
|
|
975
|
-
// True when `url` resolves (through any workspace symlink)
|
|
976
|
-
// node_modules
|
|
977
|
-
// linked into node_modules (which #canonical records as a source). The loader
|
|
978
|
-
// hooks gate on this rather than a raw `/node_modules/` substring, so a
|
|
979
|
-
// symlinked workspace source is read from disk / resolved by Node instead of
|
|
980
|
-
// being served from a node_modules-scope bundle that deliberately omits it.
|
|
981
|
-
// Tolerant: a URL it can't canonicalize (non-file, outside root) is treated as
|
|
982
|
-
// not-a-dependency, so the hook falls back to Node's default handling.
|
|
678
|
+
// True when `url` resolves (through any workspace symlink) into node_modules -- not a raw
|
|
679
|
+
// `/node_modules/` substring test, so a symlinked workspace source is read from disk.
|
|
983
680
|
inNodeModules(url) {
|
|
984
681
|
let file
|
|
985
682
|
try {
|
|
@@ -990,11 +687,8 @@ export class State {
|
|
|
990
687
|
return splitNodeModulesPath(file) !== null
|
|
991
688
|
}
|
|
992
689
|
|
|
993
|
-
// Nearest package.json at or above a directory
|
|
994
|
-
//
|
|
995
|
-
// URL (see #locateModule). Walks up using the real fs (the destructured existsSync
|
|
996
|
-
// snapshot), bounded by the project root -- which is always an existing-package.json
|
|
997
|
-
// dir, so an in-root directory always resolves. Returns an absolute package.json path.
|
|
690
|
+
// Nearest package.json at or above a directory (findPackageJSON is unreliable for a directory
|
|
691
|
+
// URL, see #locateModule). Bounded by the project root.
|
|
998
692
|
#nearestPackageJsonFor(dirAbsolute) {
|
|
999
693
|
let dir = dirAbsolute
|
|
1000
694
|
while (true) {
|
|
@@ -1002,29 +696,17 @@ export class State {
|
|
|
1002
696
|
if (existsSync(candidate)) return candidate
|
|
1003
697
|
if (dir === this.root) break // checked the root's package.json; never escape root
|
|
1004
698
|
const parent = dirname(dir)
|
|
1005
|
-
if (parent === dir) break
|
|
699
|
+
if (parent === dir) break
|
|
1006
700
|
dir = parent
|
|
1007
701
|
}
|
|
1008
|
-
// Unreachable
|
|
702
|
+
// Unreachable: state.root is resolved to a dir that has package.json.
|
|
1009
703
|
assert.fail(`no package.json at or above directory ${this.relative(dirAbsolute)}`)
|
|
1010
704
|
}
|
|
1011
705
|
|
|
1012
|
-
// Canonicalize `url` and resolve the package bucket
|
|
1013
|
-
//
|
|
1014
|
-
// by addFile (code/resource) and addFsDir (directory captures): everything here is
|
|
1015
|
-
// independent of a file's bytes or loader format, so both reuse it. Returns the
|
|
1016
|
-
// canonical absolute path, the project-relative `file`, the bucket `dir`/`module`,
|
|
1017
|
-
// and the nearest package `type` (for addFile's extension inference).
|
|
1018
|
-
//
|
|
1019
|
-
// `directory: true` (addFsDir): the url names a directory, not a file. Node's
|
|
1020
|
-
// findPackageJSON is unreliable for a directory URL -- depending on Node version
|
|
1021
|
-
// it returns the PARENT's package.json, `undefined` (at a bucket root), or even a
|
|
1022
|
-
// `node_modules/` DIRECTORY path, which readPackageJSON would then read as a file
|
|
1023
|
-
// and throw EISDIR. So for a directory we walk up to the nearest package.json
|
|
1024
|
-
// ourselves (#nearestPackageJsonFor), which is correct on every Node version.
|
|
706
|
+
// Canonicalize `url` and resolve/register the owning package bucket. `directory: true` walks up
|
|
707
|
+
// ourselves -- Node's findPackageJSON is unreliable for a directory URL (EISDIR, or the parent's).
|
|
1025
708
|
#locateModule(url, { directory = false } = {}) {
|
|
1026
|
-
// Canonicalize first: a workspace source
|
|
1027
|
-
// under its real (non-node_modules) path so it classifies as a source.
|
|
709
|
+
// Canonicalize first: a linked-in workspace source is recorded under its real path (a source).
|
|
1028
710
|
const canonical = this.#canonical(url)
|
|
1029
711
|
url = canonical.url
|
|
1030
712
|
const absolute = canonical.absolute
|
|
@@ -1037,8 +719,7 @@ export class State {
|
|
|
1037
719
|
const closestType = closestPkg.type
|
|
1038
720
|
assert.ok(closestType === undefined || closestType === 'module' || closestType === 'commonjs')
|
|
1039
721
|
|
|
1040
|
-
// findPackageJSON may land on a `{"type":"module"}
|
|
1041
|
-
// marker that lacks name/version.
|
|
722
|
+
// findPackageJSON may land on a `{"type":"module"}` sub-bucket marker lacking name/version.
|
|
1042
723
|
const nmRoot = splitNodeModulesPath(file)?.dir
|
|
1043
724
|
let pkgAbsolute, name, version
|
|
1044
725
|
if (nmRoot) {
|
|
@@ -1050,8 +731,7 @@ export class State {
|
|
|
1050
731
|
if (closestPkgAbsolute !== pkgAbsolute) {
|
|
1051
732
|
const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
|
|
1052
733
|
if (closestPkg.name !== undefined && closestPkg.name !== name) {
|
|
1053
|
-
// Allow fake module
|
|
1054
|
-
// Npm publish wouldn't accept this anyway, so it's not a conflict.
|
|
734
|
+
// Allow fake module-name subpaths: the real module owns the prefix (npm wouldn't publish this).
|
|
1055
735
|
assert.ok(closestPkg.name.startsWith(`${name}/`), message)
|
|
1056
736
|
}
|
|
1057
737
|
|
|
@@ -1081,10 +761,7 @@ export class State {
|
|
|
1081
761
|
const dir = dirname(pkg)
|
|
1082
762
|
if (nmRoot) assert.equal(dir, nmRoot)
|
|
1083
763
|
if (!this.modules.has(dir)) {
|
|
1084
|
-
// node_modules buckets
|
|
1085
|
-
// ecosystem so consumers can tell deps from the workspace's own packages
|
|
1086
|
-
// without re-deriving it from the path. Workspace/top-level buckets (the
|
|
1087
|
-
// scope=full `sources`) carry no ecosystem.
|
|
764
|
+
// Tag node_modules buckets `npm`; workspace/top-level buckets carry no ecosystem.
|
|
1088
765
|
this.modules.set(dir, nmRoot
|
|
1089
766
|
? { name, version, ecosystem: 'npm', files: Object.create(null) }
|
|
1090
767
|
: { name, version, files: Object.create(null) })
|
|
@@ -1096,57 +773,29 @@ export class State {
|
|
|
1096
773
|
return { absolute, file, dir, module, closestType }
|
|
1097
774
|
}
|
|
1098
775
|
|
|
1099
|
-
// `resource: true` (alias
|
|
1100
|
-
//
|
|
1101
|
-
// UTF-8 (stored raw, human-readable in the bundle) or 'resource:base64' for binary
|
|
1102
|
-
// (base64-encoded). Code files keep their inferred loader format.
|
|
1103
|
-
//
|
|
1104
|
-
// `inferFormat: false` records the bytes WITHOUT inferring or recording any loader
|
|
1105
|
-
// format (an existing recorded format, if any, is left intact). addFsFile uses it
|
|
1106
|
-
// for `.js`/`.ts`, whose module-vs-commonjs is Node's syntax-detection call (not
|
|
1107
|
-
// knowable from bytes): the module loader is the authority, so the fs-read capture
|
|
1108
|
-
// must not impose the legacy `commonjs` default and collide with a `module` the
|
|
1109
|
-
// loader records for the same file when it is both imported and read.
|
|
776
|
+
// `resource: true` (legacy alias `isBinary: true`): format derived from bytes. `inferFormat: false`
|
|
777
|
+
// records bytes without imposing a loader format (defers module-vs-commonjs to the loader).
|
|
1110
778
|
addFile(url, { source, format, isEntry, isBinary, resource, inferFormat = true, reason = 'run', fsRead = false } = {}) {
|
|
1111
779
|
const asResource = resource === true || isBinary === true
|
|
1112
|
-
// A resource format must not arrive on the code path: 'resource' /
|
|
1113
|
-
// 'resource:base64' identifies asset payloads, which State stores in
|
|
1114
|
-
// `this.resources` with content-derived encoding (UTF-8 vs base64). A caller
|
|
1115
|
-
// who passes the format string without setting resource:true is asking for
|
|
1116
|
-
// a code file tagged as a resource -- nonsense, and exactly the kind of
|
|
1117
|
-
// accidental misclassification the per-file format design is meant to
|
|
1118
|
-
// catch. The reverse (resource:true with a non-resource format) is rejected
|
|
1119
|
-
// below when the content-derived format is checked against any explicit one.
|
|
1120
780
|
if (!asResource && Bundle.isResourceFormat(format)) {
|
|
1121
781
|
throw new Error(`addFile: format '${format}' requires resource: true`)
|
|
1122
782
|
}
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
// (state.js's split-bundle getter), producing a v1 bundle whose entries refer
|
|
1127
|
-
// to files absent from its sources -- structurally valid but semantically
|
|
1128
|
-
// broken, and the kind of misclassification that's silent at capture time and
|
|
1129
|
-
// surfaces opaquely at the loader's NODEJS_FORMATS gate later.
|
|
783
|
+
if (isStatFormat(format)) {
|
|
784
|
+
throw new Error(`addFile: format '${format}' is a payload-free stat record; use addFsStat`)
|
|
785
|
+
}
|
|
1130
786
|
if (asResource && isEntry) {
|
|
1131
787
|
throw new Error(`addFile: a resource can't be an entry (resource:true + isEntry:true)`)
|
|
1132
788
|
}
|
|
1133
|
-
// Canonicalize + bucket by the owning package (shared with addFsDir).
|
|
1134
789
|
const { absolute, file, dir, module, closestType } = this.#locateModule(url)
|
|
1135
790
|
|
|
1136
|
-
//
|
|
1137
|
-
//
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
// `.jsx` got tagged 'resource', desyncing the unified lockfile (resource) from
|
|
1141
|
-
// the bundle (code) until it surfaced as an opaque format mismatch at load. Fail
|
|
1142
|
-
// at the recording site, where the cause is obvious, instead.
|
|
791
|
+
// Real content supersedes a payload-free stat record: drop it so the noupsert below records the
|
|
792
|
+
// actual format instead of conflicting with 'stat:*'.
|
|
793
|
+
if (isStatFormat(this.formats.get(file))) this.formats.delete(file)
|
|
794
|
+
|
|
1143
795
|
if (asResource && CODE_EXTENSIONS.has(extname(file).slice(1).toLowerCase())) {
|
|
1144
796
|
throw new Error(`addFile: a code file can't be recorded as a resource: ${file}`)
|
|
1145
797
|
}
|
|
1146
798
|
|
|
1147
|
-
// Resources don't get a loader format inferred from extension/package type --
|
|
1148
|
-
// their format ('resource' / 'resource:base64') is chosen by content below.
|
|
1149
|
-
// inferFormat:false (addFsFile for .js/.ts) skips inference entirely.
|
|
1150
799
|
if (!asResource && inferFormat) {
|
|
1151
800
|
const extToFormat = {
|
|
1152
801
|
__proto__: null,
|
|
@@ -1165,21 +814,9 @@ export class State {
|
|
|
1165
814
|
if (format != null) assert.equal(format, inferredFormat)
|
|
1166
815
|
else format = inferredFormat
|
|
1167
816
|
} else if (format == null) {
|
|
1168
|
-
// No `type
|
|
1169
|
-
//
|
|
1170
|
-
//
|
|
1171
|
-
// (e.g. bundler plugins, whose afterResolve/onLoad pass none), defer to
|
|
1172
|
-
// a format already recorded for this file THIS session before falling
|
|
1173
|
-
// back to the legacy commonjs default. The runtime loader records Node's
|
|
1174
|
-
// authoritative module/commonjs choice (hooks.js's load hook), so a
|
|
1175
|
-
// later no-format capture of the same file must KEEP a known `module`
|
|
1176
|
-
// rather than re-default it to commonjs -- which would both mis-attest
|
|
1177
|
-
// the bundle and collide at the noupsert below (and, for a bundler
|
|
1178
|
-
// sidecar whose formats merge into the parent's unified lockfile, at
|
|
1179
|
-
// #mergedFormats). A sidecar's loader records land in the parent's
|
|
1180
|
-
// formats, so consult it too. Only an extension-appropriate code format
|
|
1181
|
-
// is reused; a stale resource tag from a conflicting prior capture falls
|
|
1182
|
-
// through to the default so the conflict still surfaces at noupsert.
|
|
817
|
+
// No `type`: Node decides .js/.ts by syntax detection, so reuse an extension-appropriate
|
|
818
|
+
// format already recorded this session (the loader's call, or the parent's for a sidecar)
|
|
819
|
+
// rather than re-defaulting to commonjs and mis-attesting.
|
|
1183
820
|
const variants = {
|
|
1184
821
|
__proto__: null,
|
|
1185
822
|
'.js': ['commonjs', 'module'],
|
|
@@ -1200,14 +837,9 @@ export class State {
|
|
|
1200
837
|
}
|
|
1201
838
|
|
|
1202
839
|
const buf = typeof source === 'string' ? Buffer.from(source) : source
|
|
1203
|
-
// Verify a CALLER-PROVIDED source against disk
|
|
1204
|
-
// execute; this catches a loader serving bytes that differ from the file on disk). When we
|
|
1205
|
-
// just read `source` from disk ourselves, that comparison is against the same read -- it is
|
|
1206
|
-
// tautological -- so skip the redundant second read.
|
|
840
|
+
// Verify a CALLER-PROVIDED source against disk; tautological when we read it ourselves.
|
|
1207
841
|
if (!sourceFromDisk) assert.deepStrictEqual(readFileSync(absolute), buf)
|
|
1208
842
|
|
|
1209
|
-
// Resource format is content-driven: raw UTF-8 stays 'resource' (don't base64
|
|
1210
|
-
// bytes that are already text); anything else is 'resource:base64'.
|
|
1211
843
|
if (asResource) {
|
|
1212
844
|
const derived = isUtf8(buf) ? 'resource' : 'resource:base64'
|
|
1213
845
|
if (format != null) assert.equal(format, derived, `resource format mismatch for ${file}`)
|
|
@@ -1220,8 +852,7 @@ export class State {
|
|
|
1220
852
|
if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it; skip when the channel is off
|
|
1221
853
|
if (this.config.bundle && reason !== null) {
|
|
1222
854
|
this.#recordReason(reason, file) // provenance for the bundle's `reason` field (null = not a consumer observation, e.g. backfill)
|
|
1223
|
-
// Track HOW 'run' saw this file so #bundleReason can drop files run merely fs-READ
|
|
1224
|
-
// plugin attached that a plugin actually bundles (e.g. webpack+babel reading app source).
|
|
855
|
+
// Track HOW 'run' saw this file so #bundleReason can drop files run merely fs-READ post-plugin.
|
|
1225
856
|
if (reason === 'run') {
|
|
1226
857
|
if (!fsRead) this.#runImported.add(file)
|
|
1227
858
|
else if (this.#pluginAttached) this.#fsReadPostPlugin.add(file)
|
|
@@ -1230,32 +861,20 @@ export class State {
|
|
|
1230
861
|
const rel = relative(dir, file)
|
|
1231
862
|
assert.ok(!rel.startsWith('..'))
|
|
1232
863
|
|
|
1233
|
-
const inAttestedZone = dir
|
|
864
|
+
const inAttestedZone = hasNodeModulesSegment(dir) || this.config.full
|
|
1234
865
|
if (this.config.frozen && inAttestedZone) {
|
|
1235
|
-
assert.ok(Object.hasOwn(module.files, rel))
|
|
866
|
+
assert.ok(Object.hasOwn(module.files, rel), `File not attested by the frozen lockfile: ${file}`)
|
|
1236
867
|
}
|
|
1237
868
|
|
|
1238
|
-
//
|
|
1239
|
-
//
|
|
1240
|
-
// bundle); here we close the set, rejecting a file the bundle never recorded
|
|
1241
|
-
// (mirrors the lockfile membership check above, anchored in the bundle). The
|
|
1242
|
-
// scope carve-out matches the lockfile's: node_modules scope doesn't attest
|
|
1243
|
-
// workspace files, so they aren't required to be in the bundle.
|
|
869
|
+
// Close the attested set (bytes are checked by the noupsert below); node_modules scope
|
|
870
|
+
// deliberately doesn't attest workspace files.
|
|
1244
871
|
if (this.config.frozenBundle && inAttestedZone) {
|
|
1245
872
|
const attested = asResource ? this.#bundleResources : this.#bundleSources
|
|
1246
873
|
assert.ok(attested?.has(file), `File not attested by the frozen bundle: ${file}`)
|
|
1247
874
|
}
|
|
1248
875
|
|
|
1249
|
-
//
|
|
1250
|
-
//
|
|
1251
|
-
// file extension + nearest package.json `type`, and `type` is usually not
|
|
1252
|
-
// itself hash-attested, so flipping it recategorizes a hash-valid file
|
|
1253
|
-
// (commonjs<->module, or .js<->module-typescript) with no hash mismatch.
|
|
1254
|
-
// Checked against the lockfile and/or the frozen bundle, each anchored in its
|
|
1255
|
-
// own recorded formats. Gated to the same attested zone as the hash check
|
|
1256
|
-
// above (the node_modules-scope workspace is deliberately unattested, like
|
|
1257
|
-
// its bytes); files with no derived format (binary resources) are skipped via
|
|
1258
|
-
// `format != null`.
|
|
876
|
+
// `type` is usually not hash-attested, so a flip recategorizes a hash-valid file
|
|
877
|
+
// (commonjs<->module) with no hash mismatch.
|
|
1259
878
|
if (this.config.frozen && this.#lockFormats !== null && format != null && inAttestedZone) {
|
|
1260
879
|
this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
|
|
1261
880
|
}
|
|
@@ -1263,18 +882,12 @@ export class State {
|
|
|
1263
882
|
this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
|
|
1264
883
|
}
|
|
1265
884
|
|
|
1266
|
-
//
|
|
1267
|
-
//
|
|
1268
|
-
// noupsert below won't see the clash; it would otherwise surface only later at write(), when
|
|
1269
|
-
// #mergedFormats unions the baseline with the session formats and noupsert throws a bare
|
|
1270
|
-
// `Conflict for <file>` with no cause or remedy. Detect it here and throw a clear, actionable
|
|
1271
|
-
// error instead. (A frozen run rejects it via the format check above; lock=replace discards the
|
|
1272
|
-
// baseline, so #lockFormats is null; and bundle=add seeds the bundle's formats into this.formats,
|
|
1273
|
-
// so a bundle-side flip already conflicts at the noupsert below. Re-attest a genuine change with
|
|
1274
|
-
// --lock=replace / --bundle=replace.)
|
|
885
|
+
// Catch a format flip on already-attested bytes here; at write()'s #mergedFormats it would
|
|
886
|
+
// surface only as a bare `Conflict`.
|
|
1275
887
|
if (this.config.writeLockfile && this.#lockFormats !== null && format != null && inAttestedZone) {
|
|
1276
888
|
const attested = this.#lockFormats.get(file)
|
|
1277
|
-
|
|
889
|
+
// A 'stat:*' baseline is not a flip: real content this run UPGRADES the record (#mergedFormats).
|
|
890
|
+
assert.ok(attested === undefined || attested === format || isStatFormat(attested),
|
|
1278
891
|
`format flip for ${file}: lockfile attests '${attested}', observed '${format}' (bytes unchanged) -- re-run with --lock=replace to re-attest`)
|
|
1279
892
|
}
|
|
1280
893
|
|
|
@@ -1283,7 +896,6 @@ export class State {
|
|
|
1283
896
|
|
|
1284
897
|
if (this.config.bundle) {
|
|
1285
898
|
if (asResource) {
|
|
1286
|
-
// 'resource' stores the raw UTF-8 string; 'resource:base64' stores base64.
|
|
1287
899
|
const content = format === 'resource:base64' ? buf.toString('base64') : buf.toString('utf8')
|
|
1288
900
|
noupsert(this.resources, file, content)
|
|
1289
901
|
} else {
|
|
@@ -1292,73 +904,77 @@ export class State {
|
|
|
1292
904
|
}
|
|
1293
905
|
|
|
1294
906
|
if (format) noupsert(this.formats, file, format)
|
|
907
|
+
|
|
908
|
+
this.#recordExecutable(file, absolute)
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// Observe `file` on disk and record or refute its execute bit; disk wins over whatever a lockfile/
|
|
912
|
+
// bundle seeded, but only a real observation refutes (`undefined` and Windows mean "unknowable").
|
|
913
|
+
// A record touches only this State; a REFUTATION must reach the whole family, else
|
|
914
|
+
// #mergedExecutable's union resurrects the bit from a sibling's seeded entry.
|
|
915
|
+
#recordExecutable(file, absolute) {
|
|
916
|
+
const executable = observeExecutable(absolute)
|
|
917
|
+
if (executable === true) {
|
|
918
|
+
this.executable.add(file)
|
|
919
|
+
return
|
|
920
|
+
}
|
|
921
|
+
if (executable !== false || !CAN_OBSERVE_EXECUTE_BITS) return
|
|
922
|
+
const root = this.#parent ?? this
|
|
923
|
+
if (root.executable.size === 0 && root.sidecars().size === 0) {
|
|
924
|
+
this.executable.delete(file)
|
|
925
|
+
return
|
|
926
|
+
}
|
|
927
|
+
this.executable.delete(file)
|
|
928
|
+
root.executable.delete(file)
|
|
929
|
+
for (const sidecar of root.sidecars()) sidecar.executable.delete(file)
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// Public so #recordExecutable reaches them across the class brand (duplicate stasis-core copies).
|
|
933
|
+
sidecars() {
|
|
934
|
+
return this.#sidecars
|
|
1295
935
|
}
|
|
1296
936
|
|
|
1297
|
-
// Record an `fs.readFileSync` capture (
|
|
1298
|
-
// the
|
|
1299
|
-
//
|
|
1300
|
-
//
|
|
1301
|
-
// extension. .js/.ts defer it (inferFormat:false): module-vs-commonjs is Node's
|
|
1302
|
-
// syntax-detection call, the loader is the authority, and forcing a default here
|
|
1303
|
-
// would collide (noupsert) with the loader's record when a file is both imported and
|
|
1304
|
-
// fs-read. .jsx/.tsx are code but have no Node loader format, so none is imposed.
|
|
1305
|
-
// addFile asserts the bytes are UTF-8 -- a non-UTF-8 code file is malformed input,
|
|
1306
|
-
// refused there rather than silently re-tagged as a resource.
|
|
1307
|
-
// - 'resource' (an extension in the resources allowlist) → a 'resource'/'resource:base64'
|
|
1308
|
-
// payload, encoding chosen from the bytes.
|
|
1309
|
-
// - 'unknown' (neither code nor an allowlisted resource) → THROW. Recording it as a
|
|
1310
|
-
// resource would silently widen the attested set (the exact desync this guards); the
|
|
1311
|
-
// --fs hook catches the throw, warns, and writes nothing. Declare the extension in
|
|
1312
|
-
// `resources` (the --resources flag / EXODUS_STASIS_RESOURCES / a plugin's resources
|
|
1313
|
-
// option) or stop reading it.
|
|
1314
|
-
// Routing through addFile means a file that is BOTH imported and fs-read lands in one
|
|
1315
|
-
// consistent, deduped entry, hashed and bucketed exactly like every other file.
|
|
937
|
+
// Record an `fs.readFileSync` capture (--fs). classifyFormat routes the bytes: a concrete format
|
|
938
|
+
// is code, null defers to the loader, and undefined (or a binary plist, whose bytes can't be the
|
|
939
|
+
// UTF-8 code its format implies) is a resource IFF allowlisted, else THROW -- an undeclared file
|
|
940
|
+
// would widen the attested set.
|
|
1316
941
|
addFsFile(url, source) {
|
|
1317
942
|
assert.ok(Buffer.isBuffer(source), 'addFsFile requires a Buffer source')
|
|
1318
943
|
const path = fileURLToPath(url)
|
|
1319
|
-
const
|
|
1320
|
-
if (
|
|
1321
|
-
this.
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
if (kind === 'unknown') {
|
|
944
|
+
const format = classifyFormat(path, { content: source })
|
|
945
|
+
if (format === undefined || isBinaryPlist(path, source)) {
|
|
946
|
+
if (this.config.resources.has(pathExt(path) || basename(path).toLowerCase())) {
|
|
947
|
+
this.addFile(url, { source, resource: true, fsRead: true })
|
|
948
|
+
return
|
|
949
|
+
}
|
|
1326
950
|
throw new Error(
|
|
1327
951
|
`addFsFile: ${path} is neither code nor a declared resource; ` +
|
|
1328
952
|
`add its extension or filename to the resources allowlist or stop reading it`
|
|
1329
953
|
)
|
|
1330
954
|
}
|
|
1331
|
-
this.addFile(url, { source,
|
|
955
|
+
this.addFile(url, { source, format: format ?? undefined, inferFormat: false, fsRead: true })
|
|
1332
956
|
}
|
|
1333
957
|
|
|
1334
|
-
// Record an `fs.readdirSync
|
|
1335
|
-
//
|
|
1336
|
-
// order, JSON-serialized, and stored as a 'directory'-format payload -- a
|
|
1337
|
-
// resource-like file whose integrity (sha512 over the JSON text) lands in the
|
|
1338
|
-
// lockfile just like any other content. Buckets/dedups via #locateModule like
|
|
1339
|
-
// addFile; the directory's own project-relative path is the key (a readdir of a
|
|
1340
|
-
// module root keys the listing at the bucket itself -- see moduleFileKey).
|
|
958
|
+
// Record an `fs.readdirSync` capture: the listing is SORTED for reproducibility and stored as a
|
|
959
|
+
// 'directory'-format payload.
|
|
1341
960
|
addFsDir(url, names) {
|
|
1342
961
|
assert.ok(Array.isArray(names) && names.every((n) => typeof n === 'string'),
|
|
1343
962
|
'addFsDir requires an array of string names')
|
|
1344
963
|
const { file, dir, module } = this.#locateModule(url, { directory: true })
|
|
964
|
+
// As in addFile: a real record supersedes a payload-free stat record.
|
|
965
|
+
if (isStatFormat(this.formats.get(file))) this.formats.delete(file)
|
|
1345
966
|
const content = JSON.stringify(names.toSorted())
|
|
1346
967
|
const format = 'directory'
|
|
1347
968
|
const integrity = sha512integrity(content)
|
|
1348
969
|
noupsert(this.hashes, file, integrity)
|
|
1349
970
|
if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it; skip when the channel is off
|
|
1350
|
-
// Directory captures come only from the loader's --fs, and a plugin never records a readdir
|
|
1351
|
-
// listing (plugins only addFile), so a directory key is never in #bundleReason's pluginFiles
|
|
1352
|
-
// and can never be dropped from 'run' -- no need to track it in #fsReadPostPlugin.
|
|
1353
971
|
if (this.config.bundle) this.#recordReason('run', file)
|
|
1354
972
|
const rel = relative(dir, file)
|
|
1355
973
|
assert.ok(!rel.startsWith('..'))
|
|
1356
974
|
|
|
1357
|
-
|
|
1358
|
-
// already be recorded (closed set) and its format/integrity must match.
|
|
1359
|
-
const inAttestedZone = dir.includes('node_modules') || this.config.full
|
|
975
|
+
const inAttestedZone = hasNodeModulesSegment(dir) || this.config.full
|
|
1360
976
|
if (this.config.frozen && inAttestedZone) {
|
|
1361
|
-
assert.ok(Object.hasOwn(module.files, rel))
|
|
977
|
+
assert.ok(Object.hasOwn(module.files, rel), `Directory not attested by the frozen lockfile: ${file}`)
|
|
1362
978
|
}
|
|
1363
979
|
if (this.config.frozenBundle && inAttestedZone) {
|
|
1364
980
|
assert.ok(this.#bundleResources?.has(file), `Directory not attested by the frozen bundle: ${file}`)
|
|
@@ -1374,15 +990,26 @@ export class State {
|
|
|
1374
990
|
assert.equal(module.files[rel], integrity)
|
|
1375
991
|
if (this.config.bundle) noupsert(this.resources, file, content)
|
|
1376
992
|
noupsert(this.formats, file, format)
|
|
993
|
+
// A listing is never executable (both artifacts' parsers refuse one in `executable`), so drop
|
|
994
|
+
// any bit a prior content record left on this path.
|
|
995
|
+
this.executable.delete(file)
|
|
1377
996
|
}
|
|
1378
997
|
|
|
1379
|
-
//
|
|
1380
|
-
//
|
|
1381
|
-
//
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
998
|
+
// Record a stat capture as a PAYLOAD-FREE record (`formats[file] = 'stat:file'|'stat:directory'`,
|
|
999
|
+
// no bytes/hash/module-files entry), so getFsStat answers isFile()/isDirectory() at load. Skipped
|
|
1000
|
+
// when a content record already exists; upsertFormat lets a real format win, a kind flip is fatal.
|
|
1001
|
+
addFsStat(url, kind) {
|
|
1002
|
+
assert.ok(kind === 'file' || kind === 'directory', `addFsStat: unsupported kind '${kind}'`)
|
|
1003
|
+
const file = this.#canonicalFile(url)
|
|
1004
|
+
if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) return
|
|
1005
|
+
upsertFormat(this.formats, file, `stat:${kind}`)
|
|
1006
|
+
// Only forward when a stat record actually landed -- upsertFormat may have yielded to a real
|
|
1007
|
+
// format, and forwarding THAT would ship an entry mergeShard's stat replay ignores.
|
|
1008
|
+
if (this.config.childProcess && isStatFormat(this.formats.get(file))) this.#observed.add(file)
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// Serve an `fs.readFileSync` from the bundle: raw bytes, or undefined when uncaptured (the hook
|
|
1012
|
+
// falls back to disk) or a captured directory. Served by presence, not format.
|
|
1386
1013
|
getFsFile(url) {
|
|
1387
1014
|
let file
|
|
1388
1015
|
try { file = this.#canonicalFile(url) } catch { return undefined }
|
|
@@ -1392,9 +1019,6 @@ export class State {
|
|
|
1392
1019
|
return Buffer.isBuffer(source) ? source : Buffer.from(source, 'utf8')
|
|
1393
1020
|
}
|
|
1394
1021
|
|
|
1395
|
-
// Serve an `fs.readdirSync(path)` from the bundle: the sorted listing captured at
|
|
1396
|
-
// build time, or undefined when the path was not captured as a directory (the hook
|
|
1397
|
-
// falls back to a real disk read). Reuses getFile for the hash check.
|
|
1398
1022
|
getFsDir(url) {
|
|
1399
1023
|
let file
|
|
1400
1024
|
try { file = this.#canonicalFile(url) } catch { return undefined }
|
|
@@ -1405,12 +1029,10 @@ export class State {
|
|
|
1405
1029
|
return names
|
|
1406
1030
|
}
|
|
1407
1031
|
|
|
1408
|
-
// Every ancestor directory implied by
|
|
1409
|
-
// keys. A recorded `node_modules/dep/index.js` implies `node_modules/dep` and
|
|
1410
|
-
// `node_modules` (and the root, ''). Built once; load-mode immutable (see field).
|
|
1032
|
+
// Every ancestor directory implied by recorded keys; built once.
|
|
1411
1033
|
#impliedDirs() {
|
|
1412
1034
|
if (this.#impliedDirIndex) return this.#impliedDirIndex
|
|
1413
|
-
const dirs = new Set([''])
|
|
1035
|
+
const dirs = new Set(['.'])
|
|
1414
1036
|
const add = (key) => {
|
|
1415
1037
|
let prefix = ''
|
|
1416
1038
|
const parts = key.split('/')
|
|
@@ -1421,60 +1043,46 @@ export class State {
|
|
|
1421
1043
|
}
|
|
1422
1044
|
for (const key of this.sources.keys()) add(key)
|
|
1423
1045
|
for (const key of this.resources.keys()) add(key)
|
|
1046
|
+
// Stat records prove existence too, so they imply their ancestor dirs.
|
|
1047
|
+
for (const [key, format] of this.formats) if (isStatFormat(format)) add(key)
|
|
1424
1048
|
this.#impliedDirIndex = dirs
|
|
1425
1049
|
return dirs
|
|
1426
1050
|
}
|
|
1427
1051
|
|
|
1428
|
-
// Classify a captured path for the
|
|
1429
|
-
// shim
|
|
1430
|
-
//
|
|
1431
|
-
// file (e.g. `node_modules` when the bundle carries `node_modules/dep/index.js`), or
|
|
1432
|
-
// undefined when the path wasn't captured (the shim then falls back to a real on-disk
|
|
1433
|
-
// lstatSync). A directory lives in this.resources too, so the 'directory' format is
|
|
1434
|
-
// checked first; implied dirs are checked last so an explicit file/dir record wins.
|
|
1052
|
+
// Classify a captured path for the stat shim: 'directory', 'file', or undefined (uncaptured -> the
|
|
1053
|
+
// shim falls back to disk). Order matters: 'directory' first, content before stat records, implied
|
|
1054
|
+
// dirs last. NB: 'file' means "recorded", NOT "bytes serveable" (use hasFsFileContent).
|
|
1435
1055
|
getFsStat(url) {
|
|
1436
1056
|
let file
|
|
1437
1057
|
try { file = this.#canonicalFile(url) } catch { return undefined }
|
|
1438
|
-
|
|
1058
|
+
const format = this.formats.get(file)
|
|
1059
|
+
if (format === 'directory') return 'directory'
|
|
1439
1060
|
if (this.sources.has(file) || this.resources.has(file)) return 'file'
|
|
1061
|
+
if (format === 'stat:file') return 'file'
|
|
1062
|
+
if (format === 'stat:directory') return 'directory'
|
|
1440
1063
|
if (this.#impliedDirs().has(file)) return 'directory'
|
|
1441
1064
|
return undefined
|
|
1442
1065
|
}
|
|
1443
1066
|
|
|
1444
|
-
//
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
//
|
|
1453
|
-
//
|
|
1454
|
-
// when main doesn't carry it (a program raw-reading a graph file via --fs at load).
|
|
1455
|
-
// The two paths consult DIFFERENT registries because a sidecar is a contributor XOR a
|
|
1456
|
-
// consumer (its bundle mode is the parent's): write-mode sidecars (#sidecars) feed the
|
|
1457
|
-
// lockfile; load-mode sidecars (#readSidecars) must not. readdir capture has no skip --
|
|
1458
|
-
// bundler plugins record files (addFile), never directory LISTINGS (addFsDir), so a
|
|
1459
|
-
// sidecar never owns a readdir result; a readdir is a genuine program read for main.
|
|
1460
|
-
//
|
|
1461
|
-
// attestedBySidecar consults #sidecars (write-mode contributors) and excludes THIS State
|
|
1462
|
-
// on purpose: a file only this State has stays subject to --fs's own dedup / byte-conflict
|
|
1463
|
-
// detection (addFsFile's noupsert); the skip applies solely to a file a *sidecar* carries.
|
|
1067
|
+
// True when the bundle carries actual BYTE content for `url` (not a directory/stat-only record).
|
|
1068
|
+
hasFsFileContent(url) {
|
|
1069
|
+
let file
|
|
1070
|
+
try { file = this.#canonicalFile(url) } catch { return false }
|
|
1071
|
+
if (this.formats.get(file) === 'directory') return false
|
|
1072
|
+
return this.sources.has(file) || this.resources.has(file)
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// --fs <-> bundler-plugin coordination: CAPTURE skips a read a write-mode sidecar already attests,
|
|
1076
|
+
// else --fs re-records the bundler's whole module graph into the main bundle and defeats the split.
|
|
1464
1077
|
attestedBySidecar(url) {
|
|
1465
|
-
//
|
|
1466
|
-
//
|
|
1467
|
-
|
|
1468
|
-
// directory throws EISDIR before this is consulted) but `=== 'file'` states the intent exactly.
|
|
1469
|
-
for (const s of this.#sidecars) if (s.getFsStat(url) === 'file') return true
|
|
1078
|
+
// hasFsFileContent, not getFsStat: over-skipping on an implied dir or stat-only record would
|
|
1079
|
+
// leave a real read unattested.
|
|
1080
|
+
for (const s of this.#sidecars) if (s.hasFsFileContent(url)) return true
|
|
1470
1081
|
return false
|
|
1471
1082
|
}
|
|
1472
1083
|
|
|
1473
|
-
// getFs* variants that also consult load-mode sidecars (
|
|
1474
|
-
//
|
|
1475
|
-
// main doesn't carry it. Only #readSidecars (not #sidecars): write-mode sidecars exist
|
|
1476
|
-
// only during capture, where --fs records rather than serves, so they're never consulted
|
|
1477
|
-
// here -- and they're kept out of the lockfile-contributing path regardless.
|
|
1084
|
+
// getFs* variants that also consult load-mode sidecars (this State first). Only #readSidecars --
|
|
1085
|
+
// write-mode sidecars only capture.
|
|
1478
1086
|
getFsFileFamily(url) {
|
|
1479
1087
|
const own = this.getFsFile(url)
|
|
1480
1088
|
if (own !== undefined) return own
|
|
@@ -1492,27 +1100,17 @@ export class State {
|
|
|
1492
1100
|
getFsDirFamily(url) {
|
|
1493
1101
|
const own = this.getFsDir(url)
|
|
1494
1102
|
if (own !== undefined) return own
|
|
1495
|
-
// The sidecar loop is inert in practice: bundler plugins record files (addFile), never
|
|
1496
|
-
// directory LISTINGS (addFsDir), so a sidecar's bundle has no `directory` entry and
|
|
1497
|
-
// s.getFsDir always returns undefined. Kept for symmetry with the file/stat families.
|
|
1498
1103
|
for (const s of this.#readSidecars) { const v = s.getFsDir(url); if (v !== undefined) return v }
|
|
1499
1104
|
return undefined
|
|
1500
1105
|
}
|
|
1501
1106
|
|
|
1502
1107
|
getFile(url) {
|
|
1503
|
-
// #canonicalFile
|
|
1504
|
-
// state.root (an absolute path that escapes the project). Wrap it so callers
|
|
1505
|
-
// get a contextual message instead of a stack pointing at relative()'s
|
|
1506
|
-
// assert.ok(!file.startsWith('..')).
|
|
1108
|
+
// Wrap #canonicalFile so a URL escaping state.root gives a contextual error, not a bare assert.
|
|
1507
1109
|
let file
|
|
1508
1110
|
try { file = this.#canonicalFile(url) }
|
|
1509
1111
|
catch (cause) { throw new Error(`stasis: file is outside the project root: ${url}`, { cause }) }
|
|
1510
1112
|
const format = this.formats.get(file) // might be undefined e.g. for some bundlers
|
|
1511
|
-
//
|
|
1512
|
-
// 'resource:base64' is base64, 'resource' is raw UTF-8, code is raw text.
|
|
1513
|
-
// A missing entry throws with the URL in context so load-mode callers (the
|
|
1514
|
-
// bundler plugins, the run loader) don't have to pre-check existence -- the
|
|
1515
|
-
// single getFile call is the fail-closed gate.
|
|
1113
|
+
// A missing entry throws: this single getFile call is the fail-closed gate.
|
|
1516
1114
|
let source
|
|
1517
1115
|
if (Bundle.isResourceFormat(format)) {
|
|
1518
1116
|
source = this.resources.get(file)
|
|
@@ -1520,16 +1118,31 @@ export class State {
|
|
|
1520
1118
|
if (format === 'resource:base64') source = Buffer.from(source, 'base64')
|
|
1521
1119
|
} else {
|
|
1522
1120
|
source = this.sources.get(file)
|
|
1523
|
-
if (source === undefined)
|
|
1121
|
+
if (source === undefined) {
|
|
1122
|
+
// Stat-record case: the bundle knows the path but not its bytes -- "not attested" would misdiagnose it.
|
|
1123
|
+
if (isStatFormat(format)) {
|
|
1124
|
+
throw new Error(
|
|
1125
|
+
`stasis: only a payload-free stat record ('${format}') is attested for ${url} -- ` +
|
|
1126
|
+
`the capture stat'd this path but never read or imported it, so the bundle has no bytes to serve`
|
|
1127
|
+
)
|
|
1128
|
+
}
|
|
1129
|
+
throw new Error(`stasis: file not attested in bundle: ${url}`)
|
|
1130
|
+
}
|
|
1524
1131
|
}
|
|
1525
|
-
// Without a lockfile the bundle is self-attesting
|
|
1526
|
-
// from the same source bytes we'd then verify, which is a tautology.
|
|
1132
|
+
// Without a lockfile the bundle is self-attesting -- verifying its hash against its own bytes is a tautology.
|
|
1527
1133
|
if (this.config.useLockfile) assert.equal(this.hashes.get(file), sha512integrity(source))
|
|
1528
1134
|
return { source, format }
|
|
1529
1135
|
}
|
|
1530
1136
|
|
|
1137
|
+
// The recorded LOADER format, or undefined. Masks 'stat:*' (existence, not how bytes parse) so a
|
|
1138
|
+
// stat'd require.resolve target isn't refused by the resolve hook's format gate; getFile does NOT mask.
|
|
1139
|
+
#loaderFormat(file) {
|
|
1140
|
+
const format = this.formats.get(file)
|
|
1141
|
+
return isStatFormat(format) ? undefined : format
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1531
1144
|
getFormat(url) {
|
|
1532
|
-
return this
|
|
1145
|
+
return this.#loaderFormat(this.#canonicalFile(url))
|
|
1533
1146
|
}
|
|
1534
1147
|
|
|
1535
1148
|
// Different conditions / import attributes can yield different URLs/formats for the same parent+specifier
|
|
@@ -1542,41 +1155,22 @@ export class State {
|
|
|
1542
1155
|
return `${cond} (with: ${JSON.stringify(sorted)})`
|
|
1543
1156
|
}
|
|
1544
1157
|
|
|
1545
|
-
//
|
|
1546
|
-
//
|
|
1547
|
-
//
|
|
1548
|
-
// loader-runner does `require(loader.path)`). Recorded verbatim, that
|
|
1549
|
-
// machine-specific absolute path becomes a resolution KEY in the lockfile and
|
|
1550
|
-
// bundle: non-portable, and unmatchable on any other machine. Once the target
|
|
1551
|
-
// is confirmed inside the project root, renormalize it to a path relative to
|
|
1552
|
-
// the IMPORTING FILE's directory -- the same shape a hand-written
|
|
1553
|
-
// `require('./x')` / `require('../../pkg/x')` has. The root check is only the
|
|
1554
|
-
// gate; the resulting key may itself contain '../' (going up from the importing
|
|
1555
|
-
// file), which is expected and fine as long as the target stayed inside root.
|
|
1556
|
-
//
|
|
1557
|
-
// Keying against the parent makes the key portable AND collision-free: it is
|
|
1558
|
-
// always '.'-prefixed, so it can never clash with a bare ('pkg') or
|
|
1559
|
-
// root-relative key, and an absolute path naming the same file as a relative
|
|
1560
|
-
// import unifies onto the identical key (same value -> noupsert agrees, no
|
|
1561
|
-
// throw). Bare/relative specifiers and file: URLs (handled separately by the
|
|
1562
|
-
// loader) pass through untouched; an absolute path that escapes the root has no
|
|
1563
|
-
// in-root relative form, so it's left as-is. Applied identically in addImport
|
|
1564
|
-
// (record) and getImport (lookup) so the key written and the key queried agree.
|
|
1158
|
+
// An already-resolved ABSOLUTE require()/import() target as the specifier would be a
|
|
1159
|
+
// machine-specific lockfile key, so renormalize in-root ones against the IMPORTING FILE's dir.
|
|
1160
|
+
// Must be applied identically in addImport and getImport so keys written and queried agree.
|
|
1565
1161
|
#canonicalSpecifier(parentURL, specifier) {
|
|
1566
|
-
if (typeof specifier !== 'string'
|
|
1567
|
-
const
|
|
1162
|
+
if (typeof specifier !== 'string') return specifier
|
|
1163
|
+
const path = specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier
|
|
1164
|
+
if (!isAbsolute(path)) return specifier
|
|
1165
|
+
const fromRoot = relative(this.root, path)
|
|
1568
1166
|
if (fromRoot === '' || fromRoot.startsWith('..')) return specifier // outside the project root
|
|
1569
|
-
const rel = relative(dirname(fileURLToPath(parentURL)),
|
|
1167
|
+
const rel = relative(dirname(fileURLToPath(parentURL)), path)
|
|
1570
1168
|
return rel.startsWith('.') ? rel : `./${rel}`
|
|
1571
1169
|
}
|
|
1572
1170
|
|
|
1573
1171
|
addImport(parentURL, specifier, url, { conditions = '*', format, importAttributes } = {}) {
|
|
1574
1172
|
if (conditions !== '*') assert.ok(Array.isArray(conditions))
|
|
1575
|
-
// Capture Node's require-condition set the first
|
|
1576
|
-
// edge: it carries 'require' and never 'import' (the import set carries 'import'
|
|
1577
|
-
// instead). Requiring the absence of 'import' also rejects an import edge a user
|
|
1578
|
-
// forced 'require' onto via --conditions. #backfillObservedResolutions reuses this
|
|
1579
|
-
// set to key native require.resolve() edges accurately instead of '*' (see field doc).
|
|
1173
|
+
// Capture Node's require-condition set on the first require()-context edge (see #requireConditions).
|
|
1580
1174
|
if (this.#requireConditions === null && Array.isArray(conditions) &&
|
|
1581
1175
|
conditions.includes('require') && !conditions.includes('import')) {
|
|
1582
1176
|
this.#requireConditions = conditions
|
|
@@ -1587,22 +1181,12 @@ export class State {
|
|
|
1587
1181
|
specifier = this.#canonicalSpecifier(parentURL, specifier)
|
|
1588
1182
|
const key = this.#conditionsKey(conditions, importAttributes)
|
|
1589
1183
|
|
|
1590
|
-
// Frozen runs verify resolutions
|
|
1591
|
-
//
|
|
1592
|
-
|
|
1593
|
-
// package.json `main` or a symlink). An edge the lockfile attests must
|
|
1594
|
-
// match in every scope. Unknown edges are fatal in full scope (the
|
|
1595
|
-
// attested file set is closed, so the traversed edge set should be too),
|
|
1596
|
-
// but tolerated for workspace parents in node_modules scope -- the
|
|
1597
|
-
// workspace is deliberately unattested there, so changed workspace code
|
|
1598
|
-
// may legitimately introduce new edges, including into pinned packages.
|
|
1599
|
-
const tolerateUnknown = !this.config.full && !parent.includes('node_modules')
|
|
1184
|
+
// Frozen runs verify disk resolutions against the lockfile: hashes can't see a redirect to
|
|
1185
|
+
// another attested file. Unknown edges are tolerated only for workspace parents in nm scope.
|
|
1186
|
+
const tolerateUnknown = !this.config.full && !hasNodeModulesSegment(parent)
|
|
1600
1187
|
if (this.config.frozen && this.#lockImports !== null) {
|
|
1601
1188
|
this.#assertAttestedResolution(this.#lockImports, key, parent, specifier, file, { what: 'observed', source: 'lockfile', tolerateUnknown })
|
|
1602
1189
|
}
|
|
1603
|
-
// A frozen bundle attests resolutions the same way (every v1 bundle carries an
|
|
1604
|
-
// `imports` map, so #bundleImports is never null once one is loaded). Same
|
|
1605
|
-
// redirect protection and node_modules-scope carve-out, anchored in the bundle.
|
|
1606
1190
|
if (this.config.frozenBundle && this.#bundleImports !== null) {
|
|
1607
1191
|
this.#assertAttestedResolution(this.#bundleImports, key, parent, specifier, file, { what: 'observed', source: 'frozen bundle', tolerateUnknown })
|
|
1608
1192
|
}
|
|
@@ -1612,7 +1196,8 @@ export class State {
|
|
|
1612
1196
|
if (!imports.has(parent)) imports.set(parent, new Map())
|
|
1613
1197
|
const specifiers = imports.get(parent)
|
|
1614
1198
|
noupsert(specifiers, specifier, file)
|
|
1615
|
-
|
|
1199
|
+
// upsertFormat, not noupsert: a real format must replace a weak 'stat:*' record from an --fs stat.
|
|
1200
|
+
if (format) upsertFormat(this.formats, file, format)
|
|
1616
1201
|
}
|
|
1617
1202
|
|
|
1618
1203
|
getImport(parentURL, specifier, { conditions = '*', importAttributes } = {}) {
|
|
@@ -1621,73 +1206,42 @@ export class State {
|
|
|
1621
1206
|
const parent = this.#canonicalFile(parentURL)
|
|
1622
1207
|
specifier = this.#canonicalSpecifier(parentURL, specifier)
|
|
1623
1208
|
const key = this.#conditionsKey(conditions, importAttributes)
|
|
1624
|
-
//
|
|
1625
|
-
//
|
|
1626
|
-
// predict the exact condition set Node will pass at load time. The runtime
|
|
1627
|
-
// loader always records the precise conditions Node reported, so the
|
|
1628
|
-
// specific lookup wins when a runtime bundle is in play and only static
|
|
1629
|
-
// bundles take the wildcard fallback. Mismatches between the resolved file
|
|
1630
|
-
// for a given (parent, spec) under the two condition sets remain caller
|
|
1631
|
-
// territory -- the static bundle is authoritative for whichever conditions
|
|
1632
|
-
// the static resolver used.
|
|
1209
|
+
// Static bundles store edges under '*' (Node's condition set is unpredictable); the runtime
|
|
1210
|
+
// loader records precise conditions, so the specific lookup wins.
|
|
1633
1211
|
let file = this.imports.get(key)?.get(parent)?.get(specifier)
|
|
1634
1212
|
if (file === undefined && key !== '*') {
|
|
1635
1213
|
file = this.imports.get('*')?.get(parent)?.get(specifier)
|
|
1636
1214
|
}
|
|
1637
1215
|
if (file === undefined) {
|
|
1638
|
-
// The
|
|
1639
|
-
// under a DIFFERENT conditions bucket -- e.g. the esbuild plugin asks with
|
|
1640
|
-
// conditions='*' while a Node-loader-captured bundle keyed the edge under
|
|
1641
|
-
// 'require, node, ...'. resolveBundled scans every bucket for the recorded edge.
|
|
1216
|
+
// The edge may be recorded under a DIFFERENT conditions bucket -- resolveBundled scans every bucket.
|
|
1642
1217
|
if (this.config.loadBundle) {
|
|
1643
1218
|
const abs = this.resolveBundled(parentURL, specifier)
|
|
1644
1219
|
if (abs !== undefined) {
|
|
1645
1220
|
const f = relative(this.root, abs)
|
|
1646
|
-
return { url: pathToFileURL(abs).toString(), format: this
|
|
1221
|
+
return { url: pathToFileURL(abs).toString(), format: this.#loaderFormat(f) }
|
|
1647
1222
|
}
|
|
1648
1223
|
}
|
|
1649
|
-
//
|
|
1650
|
-
//
|
|
1651
|
-
// can't be found, and ESM dynamic-import callers commonly do
|
|
1652
|
-
// `try { await import(x) } catch (e) { if (e.code !== 'ERR_MODULE_NOT_FOUND') throw e }`.
|
|
1653
|
-
// A bare assertion produced an ERR_ASSERTION code that re-threw out of
|
|
1654
|
-
// those guards.
|
|
1224
|
+
// Node's ERR_MODULE_NOT_FOUND shape, not a bare assert: dynamic-import callers guard on that
|
|
1225
|
+
// code, which ERR_ASSERTION escapes.
|
|
1655
1226
|
const err = new Error(`Cannot find module '${specifier}' imported from ${parent}`)
|
|
1656
1227
|
err.code = 'ERR_MODULE_NOT_FOUND'
|
|
1657
1228
|
throw err
|
|
1658
1229
|
}
|
|
1659
|
-
// A
|
|
1660
|
-
//
|
|
1661
|
-
// has no platform context to pick one, so it can't serve such an edge -- fail
|
|
1662
|
-
// closed with a clear error rather than feeding a Map where a path is expected.
|
|
1230
|
+
// A --metro per-platform edge is a { platform: file } Map; plain bundle=load has no platform
|
|
1231
|
+
// context to pick one, so fail closed rather than feed a Map where a path is expected.
|
|
1663
1232
|
if (typeof file !== 'string') {
|
|
1664
1233
|
const err = new Error(`Resolution of '${specifier}' from ${parent} is platform-specific (a --metro multi-platform bundle); it can't be loaded by plain node, build a single-platform bundle to load`)
|
|
1665
1234
|
err.code = 'ERR_STASIS_PLATFORM_SPECIFIC'
|
|
1666
1235
|
throw err
|
|
1667
1236
|
}
|
|
1668
1237
|
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
1669
|
-
const format = this
|
|
1238
|
+
const format = this.#loaderFormat(file)
|
|
1670
1239
|
return { url, format }
|
|
1671
1240
|
}
|
|
1672
1241
|
|
|
1673
|
-
// Resolve a
|
|
1674
|
-
//
|
|
1675
|
-
//
|
|
1676
|
-
//
|
|
1677
|
-
// Node's CommonJS loader resolves require() through Module._resolveFilename, which
|
|
1678
|
-
// registerHooks does NOT intercept. When the ESM->CJS translator runs a CJS module we
|
|
1679
|
-
// served from the bundle (`import chalk from 'chalk'`, chalk being CJS), that module's
|
|
1680
|
-
// nested require()s take this native path and Node resolves them against disk -- so a
|
|
1681
|
-
// bundle is not self-contained for a CJS dependency graph once node_modules is
|
|
1682
|
-
// pruned/absent. The CJS-loader shim in hooks.js consults this to serve them from the
|
|
1683
|
-
// bundle instead.
|
|
1684
|
-
//
|
|
1685
|
-
// Returns the recorded edge for this (parent, specifier) under ANY conditions bucket --
|
|
1686
|
-
// a native require gives no conditions to key on, and divergent targets across buckets
|
|
1687
|
-
// are ambiguous, so we defer. EVERY require() edge is recorded at capture, including the
|
|
1688
|
-
// sibling requires Node's module cache used to hide from the resolve hook (the
|
|
1689
|
-
// Module._load shim in hooks.js observes them) -- so a miss here means the bundle simply
|
|
1690
|
-
// doesn't carry the target (an external), and we defer to Node rather than guess.
|
|
1242
|
+
// Resolve a CJS require() target to a bundled absolute path, or undefined to defer to Node (the
|
|
1243
|
+
// hooks.js CJS shim needs this: registerHooks can't intercept Module._resolveFilename). Matches
|
|
1244
|
+
// under ANY conditions bucket, since native require gives none; divergent buckets -> defer.
|
|
1691
1245
|
resolveBundled(parentURL, specifier) {
|
|
1692
1246
|
let parent
|
|
1693
1247
|
try { parent = this.#canonicalFile(parentURL) } catch { return undefined }
|
|
@@ -1697,14 +1251,15 @@ export class State {
|
|
|
1697
1251
|
const file = byParent.get(parent)?.get(spec)
|
|
1698
1252
|
if (file !== undefined) matches.add(file)
|
|
1699
1253
|
}
|
|
1700
|
-
|
|
1254
|
+
if (matches.size !== 1) return undefined
|
|
1255
|
+
const [only] = matches
|
|
1256
|
+
// Per-platform { platform: file } Map (--metro) has no single path: defer to native, not resolve()'s TypeError.
|
|
1257
|
+
if (typeof only !== 'string') return undefined
|
|
1258
|
+
return resolve(this.root, only)
|
|
1701
1259
|
}
|
|
1702
1260
|
|
|
1703
|
-
// Union of
|
|
1704
|
-
//
|
|
1705
|
-
// run extends the recorded resolutions without dropping edges it didn't
|
|
1706
|
-
// reach. Conflicting targets for the same edge are fatal (add semantics,
|
|
1707
|
-
// same as hashes).
|
|
1261
|
+
// Union of lockfile-attested and live resolutions, so a partial lock=add run extends without
|
|
1262
|
+
// dropping edges. Conflicting targets are fatal (add semantics).
|
|
1708
1263
|
#mergedImports() {
|
|
1709
1264
|
const merged = new Map()
|
|
1710
1265
|
const mergeIn = (imports) => {
|
|
@@ -1722,40 +1277,56 @@ export class State {
|
|
|
1722
1277
|
}
|
|
1723
1278
|
if (this.#lockImports) mergeIn(this.#lockImports)
|
|
1724
1279
|
mergeIn(this.imports)
|
|
1725
|
-
//
|
|
1726
|
-
// webpack-graph edges into its own this.imports, and the lockfile must
|
|
1727
|
-
// attest those edges so a frozen run can verify them.
|
|
1280
|
+
// Write-mode sidecars too, so the lockfile attests their edges for frozen verify.
|
|
1728
1281
|
for (const sidecar of this.#sidecars) mergeIn(sidecar.imports)
|
|
1729
1282
|
return merged
|
|
1730
1283
|
}
|
|
1731
1284
|
|
|
1732
|
-
// Union of lockfile-attested and observed
|
|
1733
|
-
//
|
|
1734
|
-
// fatal (noupsert). Sidecars' formats are unioned in for the same reason.
|
|
1285
|
+
// Union of lockfile-attested and observed formats (+ sidecars'), append-only like #mergedImports,
|
|
1286
|
+
// except that a weak 'stat:*' is UPGRADED by real content; two real formats stay fatal.
|
|
1735
1287
|
#mergedFormats() {
|
|
1736
1288
|
const merged = new Map()
|
|
1737
1289
|
if (this.#lockFormats) for (const [file, format] of this.#lockFormats) merged.set(file, format)
|
|
1738
|
-
for (const [file, format] of this.formats)
|
|
1290
|
+
for (const [file, format] of this.formats) upsertFormat(merged, file, format)
|
|
1739
1291
|
for (const sidecar of this.#sidecars) {
|
|
1740
|
-
for (const [file, format] of sidecar.formats)
|
|
1292
|
+
for (const [file, format] of sidecar.formats) upsertFormat(merged, file, format)
|
|
1293
|
+
}
|
|
1294
|
+
// A payload-free 'stat:*' must never survive on a file that now carries a content hash: a stat
|
|
1295
|
+
// entry re-seeded from #lockFormats is beyond addFile's delete, and an ambiguous .js/.ts yields
|
|
1296
|
+
// no real format to override it above.
|
|
1297
|
+
for (const [file, format] of merged) {
|
|
1298
|
+
if (isStatFormat(format) && this.hashes.has(file)) merged.delete(file)
|
|
1741
1299
|
}
|
|
1742
1300
|
return merged
|
|
1743
1301
|
}
|
|
1744
1302
|
|
|
1303
|
+
// The executable list the LOCKFILE attests (this State's + write-mode sidecars'), narrowed by
|
|
1304
|
+
// scope to the keys it will SERIALIZE: a non-full-scope lockfile drops its workspace buckets, and
|
|
1305
|
+
// an entry for a dropped bucket writes a lockfile Lockfile.parse then refuses.
|
|
1306
|
+
#mergedExecutable(formats) {
|
|
1307
|
+
const all = new Set(this.executable)
|
|
1308
|
+
for (const sidecar of this.#sidecars) for (const file of sidecar.executable) all.add(file)
|
|
1309
|
+
return narrowExecutable(all, { modules: this.modules, formats, scope: this.config.scope })
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// The executable list ONE bundle declares, narrowed to that half's own buckets and formats.
|
|
1313
|
+
#bundleExecutable(modules, formats) {
|
|
1314
|
+
return narrowExecutable(this.executable, { modules, formats, scope: this.config.scope })
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1745
1317
|
get lockData() {
|
|
1318
|
+
const formats = this.#mergedFormats()
|
|
1746
1319
|
return new Lockfile({
|
|
1747
1320
|
config: this.config.values,
|
|
1748
1321
|
entries: this.entries,
|
|
1749
1322
|
modules: this.modules,
|
|
1750
1323
|
imports: this.#mergedImports(),
|
|
1751
|
-
formats
|
|
1324
|
+
formats,
|
|
1325
|
+
executable: this.#mergedExecutable(formats),
|
|
1752
1326
|
}).serialize()
|
|
1753
1327
|
}
|
|
1754
1328
|
|
|
1755
|
-
// Pair each module's recorded file list with
|
|
1756
|
-
// perFile (state.sources for code, state.resources for resources), dropping
|
|
1757
|
-
// modules with no content. Bundle.serialize* then groups them into sources/
|
|
1758
|
-
// modules buckets and writes the lockfile-shaped output.
|
|
1329
|
+
// Pair each module's recorded file list with content from perFile, dropping modules with none.
|
|
1759
1330
|
#bundleModules(perFile) {
|
|
1760
1331
|
const modules = new Map()
|
|
1761
1332
|
for (const [dir, info] of this.modules) {
|
|
@@ -1774,31 +1345,32 @@ export class State {
|
|
|
1774
1345
|
return modules
|
|
1775
1346
|
}
|
|
1776
1347
|
|
|
1777
|
-
//
|
|
1778
|
-
//
|
|
1779
|
-
// bundles are the plugin's modules, not run's -- see #bundleReason. Idempotent. Public so it
|
|
1780
|
-
// works across stasis-core copies (the plugin may be a foreign-copy instance), like
|
|
1781
|
-
// registerSidecar / sidecarInheritance.
|
|
1348
|
+
// Files fs-READ after this point that a plugin also bundles are the plugin's, not run's (see
|
|
1349
|
+
// #bundleReason). Public, cross-copy.
|
|
1782
1350
|
markPluginAttached() {
|
|
1783
1351
|
this.#pluginAttached = true
|
|
1784
1352
|
}
|
|
1785
1353
|
|
|
1786
|
-
// Record which consumer (`reason`) observed `file`, for the bundle's informational `reason` map.
|
|
1787
1354
|
#recordReason(reason, file) {
|
|
1788
1355
|
let set = this.#reasonFiles.get(reason)
|
|
1789
1356
|
if (set === undefined) this.#reasonFiles.set(reason, set = new Set())
|
|
1790
1357
|
set.add(file)
|
|
1791
1358
|
}
|
|
1792
1359
|
|
|
1793
|
-
//
|
|
1794
|
-
//
|
|
1795
|
-
|
|
1796
|
-
|
|
1360
|
+
// Seed `reason` from an absorbed on-disk bundle so a bundle=add re-run preserves other consumers'
|
|
1361
|
+
// attribution. Called from every absorb site, including the sidecar's inlined one.
|
|
1362
|
+
#seedReasonFromBundle(bundle) {
|
|
1363
|
+
if (!this.config.writeBundle || bundle.reason === undefined) return
|
|
1364
|
+
for (const [consumer, files] of Object.entries(bundle.reason)) {
|
|
1365
|
+
for (const file of files) this.#recordReason(consumer, file)
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
// The bundle's informational `reason` map, restricted to `bundledFiles`; only when >1 consumer
|
|
1370
|
+
// contributed. Never attested.
|
|
1797
1371
|
#bundleReason(bundledFiles) {
|
|
1798
1372
|
const inBundle = bundledFiles instanceof Set ? bundledFiles : new Set(bundledFiles)
|
|
1799
|
-
//
|
|
1800
|
-
// imported) after a plugin attached, that a plugin also bundles, is the plugin's module -- e.g.
|
|
1801
|
-
// webpack+babel reading app source through --fs to transform it -- so 'run' must not claim it.
|
|
1373
|
+
// A file run merely fs-READ post-plugin that a plugin also bundles is the plugin's, not run's.
|
|
1802
1374
|
const pluginFiles = new Set()
|
|
1803
1375
|
for (const [who, recorded] of this.#reasonFiles) {
|
|
1804
1376
|
if (who === 'run') continue
|
|
@@ -1809,9 +1381,8 @@ export class State {
|
|
|
1809
1381
|
|
|
1810
1382
|
const reason = {}
|
|
1811
1383
|
let consumers = 0
|
|
1812
|
-
// Sort
|
|
1813
|
-
//
|
|
1814
|
-
// loader-vs-plugin record order, else #emitBundle's compare-and-skip would see spurious diffs.
|
|
1384
|
+
// Sort keys and file lists: the JSON must be byte-reproducible regardless of record order, else
|
|
1385
|
+
// #emitBundle's compare-and-skip sees spurious diffs.
|
|
1815
1386
|
for (const who of [...this.#reasonFiles.keys()].toSorted()) {
|
|
1816
1387
|
const files = []
|
|
1817
1388
|
for (const file of this.#reasonFiles.get(who)) {
|
|
@@ -1824,25 +1395,38 @@ export class State {
|
|
|
1824
1395
|
return consumers > 1 ? reason : undefined
|
|
1825
1396
|
}
|
|
1826
1397
|
|
|
1398
|
+
// The formats a bundle declares: `this.formats`, minus a 'stat:*' the run's unified attestation
|
|
1399
|
+
// upgraded to a real format (else the bundle fails its lockfile cross-check at load).
|
|
1400
|
+
#formatsForBundle() {
|
|
1401
|
+
if (this.#sidecars.size === 0) return this.formats
|
|
1402
|
+
const merged = this.#mergedFormats()
|
|
1403
|
+
let out = null // copy-on-write: clone only when an entry must be dropped
|
|
1404
|
+
for (const [file, format] of this.formats) {
|
|
1405
|
+
if (isStatFormat(format) && merged.get(file) !== format) {
|
|
1406
|
+
if (out === null) out = new Map(this.formats)
|
|
1407
|
+
out.delete(file)
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
return out ?? this.formats
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1827
1413
|
get sourceBundle() {
|
|
1828
|
-
// One bundle holds
|
|
1829
|
-
//
|
|
1830
|
-
// which is which. Resource payloads are already encoded per their format. The
|
|
1831
|
-
// disjointness invariant is enforced upstream in addFile via noupsert on
|
|
1832
|
-
// this.formats (a file's format is either a resource one or not, and cannot
|
|
1833
|
-
// change), so an overlap here would be a real bug -- assert it explicitly at
|
|
1834
|
-
// the merge site so the failure is local, not a silently-clobbered value.
|
|
1414
|
+
// One bundle holds code + resources, with `formats` tagging which; the key sets must be
|
|
1415
|
+
// disjoint, asserted so an overlap fails locally rather than silently.
|
|
1835
1416
|
const contents = new Map(this.sources)
|
|
1836
1417
|
for (const [file, content] of this.resources) {
|
|
1837
1418
|
assert.ok(!contents.has(file), `state invariant: file ${file} in both sources and resources`)
|
|
1838
1419
|
contents.set(file, content)
|
|
1839
1420
|
}
|
|
1421
|
+
const modules = this.#bundleModules(contents)
|
|
1422
|
+
const formats = this.#formatsForBundle()
|
|
1840
1423
|
return new Bundle({
|
|
1841
1424
|
config: this.config.values,
|
|
1842
1425
|
entries: this.entries,
|
|
1843
|
-
modules
|
|
1844
|
-
formats
|
|
1426
|
+
modules,
|
|
1427
|
+
formats,
|
|
1845
1428
|
imports: this.imports,
|
|
1429
|
+
executable: this.#bundleExecutable(modules, formats),
|
|
1846
1430
|
reason: this.#bundleReason(contents.keys()),
|
|
1847
1431
|
})
|
|
1848
1432
|
}
|
|
@@ -1851,27 +1435,24 @@ export class State {
|
|
|
1851
1435
|
return this.sourceBundle.serialize()
|
|
1852
1436
|
}
|
|
1853
1437
|
|
|
1854
|
-
// Split-bundle counterparts to
|
|
1855
|
-
//
|
|
1856
|
-
//
|
|
1857
|
-
// metadata is partitioned, not duplicated -- the code half owns entries/imports
|
|
1858
|
-
// and code-format declarations, the resources half owns resource-format
|
|
1859
|
-
// declarations and the resource bytes. Both halves declare their per-dir
|
|
1860
|
-
// module identity (name/version) so each file is verifiable on its own.
|
|
1438
|
+
// Split-bundle counterparts to sourceBundle/sourceData: the code half owns entries/imports + code
|
|
1439
|
+
// formats, the resources half owns resource formats + bytes. Both declare per-dir module identity
|
|
1440
|
+
// so each file verifies alone.
|
|
1861
1441
|
get codeBundle() {
|
|
1862
|
-
//
|
|
1863
|
-
//
|
|
1864
|
-
// view. Formats and modules are filtered to drop resource-only dirs/files.
|
|
1442
|
+
// Stat records ride the code half -- they aren't resource formats, so the resources half's shape
|
|
1443
|
+
// check would reject them.
|
|
1865
1444
|
const codeFormats = new Map()
|
|
1866
|
-
for (const [file, format] of this
|
|
1445
|
+
for (const [file, format] of this.#formatsForBundle()) {
|
|
1867
1446
|
if (!Bundle.isResourceFormat(format)) codeFormats.set(file, format)
|
|
1868
1447
|
}
|
|
1448
|
+
const modules = this.#bundleModules(this.sources)
|
|
1869
1449
|
return new Bundle({
|
|
1870
1450
|
config: this.config.values,
|
|
1871
1451
|
entries: this.entries,
|
|
1872
|
-
modules
|
|
1452
|
+
modules,
|
|
1873
1453
|
formats: codeFormats,
|
|
1874
1454
|
imports: this.imports,
|
|
1455
|
+
executable: this.#bundleExecutable(modules, codeFormats),
|
|
1875
1456
|
reason: this.#bundleReason(this.sources.keys()),
|
|
1876
1457
|
})
|
|
1877
1458
|
}
|
|
@@ -1881,54 +1462,30 @@ export class State {
|
|
|
1881
1462
|
for (const [file, format] of this.formats) {
|
|
1882
1463
|
if (Bundle.isResourceFormat(format)) resourceFormats.set(file, format)
|
|
1883
1464
|
}
|
|
1465
|
+
const modules = this.#bundleModules(this.resources)
|
|
1884
1466
|
return new Bundle({
|
|
1885
1467
|
config: this.config.values,
|
|
1886
|
-
// Resources bundle carries no entries
|
|
1887
|
-
// Bundle.parse accepts an empty entries set when no code is present (the hasCode
|
|
1888
|
-
// check sees only resource-format files and waives the requirement).
|
|
1468
|
+
// Resources bundle carries no entries/imports (Bundle.parse waives the entries requirement when no code).
|
|
1889
1469
|
entries: new Set(),
|
|
1890
|
-
modules
|
|
1470
|
+
modules,
|
|
1891
1471
|
formats: resourceFormats,
|
|
1892
1472
|
imports: new Map(),
|
|
1473
|
+
executable: this.#bundleExecutable(modules, resourceFormats),
|
|
1893
1474
|
reason: this.#bundleReason(this.resources.keys()),
|
|
1894
1475
|
})
|
|
1895
1476
|
}
|
|
1896
1477
|
|
|
1897
|
-
// Record a
|
|
1898
|
-
//
|
|
1899
|
-
// to add. Last-write-wins per (parent, specifier) so duplicates collapse.
|
|
1478
|
+
// Record a native resolution verbatim (last-write-wins per parent+specifier);
|
|
1479
|
+
// #backfillObservedResolutions decides at write() which to add.
|
|
1900
1480
|
observeResolution(parentURL, specifier, resolvedURL) {
|
|
1901
1481
|
let byParent = this.#observedResolutions.get(parentURL)
|
|
1902
1482
|
if (byParent === undefined) this.#observedResolutions.set(parentURL, (byParent = new Map()))
|
|
1903
1483
|
byParent.set(specifier, resolvedURL)
|
|
1904
1484
|
}
|
|
1905
1485
|
|
|
1906
|
-
// Backfill
|
|
1907
|
-
//
|
|
1908
|
-
//
|
|
1909
|
-
// - under-recorded require()s whose target the bundle carries (Node's module
|
|
1910
|
-
// cache hid the require() from the resolve hook -- another module loaded the
|
|
1911
|
-
// target first), and
|
|
1912
|
-
// - resolve-only edges whose target it does NOT carry: require.resolve() of a
|
|
1913
|
-
// module never loaded in-process (an optional-dependency probe, a forked-
|
|
1914
|
-
// worker child like worker-farm's, a .node addon). The resolution is real and
|
|
1915
|
-
// must be attested so the same require.resolve() returns the same path at
|
|
1916
|
-
// load; the file simply has no bytes -- require.resolve returns a path, load()
|
|
1917
|
-
// never runs for it, so getFile is never asked to serve it.
|
|
1918
|
-
// Guards:
|
|
1919
|
-
// - skip out-of-scope targets (mirroring #collectMissingImportedFiles) so a
|
|
1920
|
-
// node_modules-scope artifact doesn't attest workspace resolutions it isn't
|
|
1921
|
-
// responsible for; an in-bundle target is always in scope, so it's exempt.
|
|
1922
|
-
// (Targets escaping the root throw in #canonicalFile -> caught below.)
|
|
1923
|
-
// - only edges the hook didn't already record (dedup across all condition
|
|
1924
|
-
// buckets), so this never double-records a normal require().
|
|
1925
|
-
// Keyed under the observed require-condition set (#requireConditions) -- require()/
|
|
1926
|
-
// require.resolve() resolve under those conditions, not condition-free -- so a
|
|
1927
|
-
// native-resolve Node (where these edges land here) keys them the same way the
|
|
1928
|
-
// resolve hook keys them directly on newer Node. Any capture that does at least one
|
|
1929
|
-
// require() thus yields the same lockfile on every version; '*' only when the set was
|
|
1930
|
-
// never observed (a capture with no require() at all), which getImport/resolveBundled
|
|
1931
|
-
// still match via their wildcard fallback.
|
|
1486
|
+
// Backfill edges the live resolve hook never observed (require.resolve()/native CJS require()).
|
|
1487
|
+
// The resolution is attested but its bytes are NOT seeded -- that would widen trust to everything
|
|
1488
|
+
// that merely resolves. Skips out-of-scope targets and dedups across all condition buckets.
|
|
1932
1489
|
#backfillObservedResolutions() {
|
|
1933
1490
|
if (this.#observedResolutions.size === 0) return
|
|
1934
1491
|
for (const [parentURL, specs] of this.#observedResolutions) {
|
|
@@ -1949,31 +1506,39 @@ export class State {
|
|
|
1949
1506
|
}
|
|
1950
1507
|
}
|
|
1951
1508
|
|
|
1509
|
+
// Add every bundled module's `<dir>/package.json` (config.packageJSON) even if the run never
|
|
1510
|
+
// reached it, so a load/prune can read each dependency's manifest. The skip keys on bundle
|
|
1511
|
+
// membership, NOT this.hashes (which also holds lockfile-absorbed entries and is shared by
|
|
1512
|
+
// reference across sidecars).
|
|
1513
|
+
includePackageJson() {
|
|
1514
|
+
// Gather first: addFile mutates this.modules, so don't addFile while iterating it.
|
|
1515
|
+
const realRoot = realpathSync(this.root)
|
|
1516
|
+
const toAdd = []
|
|
1517
|
+
for (const [dir, module] of this.modules) {
|
|
1518
|
+
const rel = moduleFileKey(dir, 'package.json')
|
|
1519
|
+
if (this.sources.has(rel) || this.resources.has(rel)) continue // already in this bundle
|
|
1520
|
+
const buf = readModuleManifest({ baseDir: this.root, realBase: realRoot, rel })
|
|
1521
|
+
if (!buf) continue
|
|
1522
|
+
// Only carry a manifest whose on-disk identity still matches the bundled bucket: under
|
|
1523
|
+
// bundle=add a dep may have drifted, and addFile -> #locateModule would crash on it.
|
|
1524
|
+
let pkg
|
|
1525
|
+
try { pkg = JSON.parse(buf.toString()) } catch { continue } // malformed manifest for an untouched bucket: skip
|
|
1526
|
+
if (pkg?.name !== module.name || pkg?.version !== module.version) continue
|
|
1527
|
+
toAdd.push({ rel, buf })
|
|
1528
|
+
}
|
|
1529
|
+
for (const { rel, buf } of toAdd) {
|
|
1530
|
+
this.addFile(pathToFileURL(resolve(this.root, rel)).toString(), { source: buf, reason: null })
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1952
1534
|
write() {
|
|
1953
|
-
//
|
|
1954
|
-
//
|
|
1955
|
-
// stasis-core src/*.js submodule (e.g. require.resolve('@exodus/stasis-core/
|
|
1956
|
-
// prune') of a module never loaded) is added only here; running the BFS first
|
|
1957
|
-
// would compute its missing-set without that edge, never seed the file, and
|
|
1958
|
-
// ship a dangling edge with no bytes -- which a later lock=frozen replay then
|
|
1959
|
-
// rejects. Recording it first lets the BFS capture the bytes, matching what the
|
|
1960
|
-
// resolve-hook path already records in-line on newer Node. Order is otherwise
|
|
1961
|
-
// independent: the BFS doesn't read #observedResolutions, and observe-backfill's
|
|
1962
|
-
// `bundled` check sees files loaded during capture (already in this.sources)
|
|
1963
|
-
// regardless of when the BFS runs.
|
|
1535
|
+
// BEFORE the stasis-core BFS: a resolve-only edge to a stasis-core submodule is added only here,
|
|
1536
|
+
// and the BFS must see it to seed the file's bytes, else the bundle ships a dangling edge.
|
|
1964
1537
|
this.#backfillObservedResolutions()
|
|
1965
1538
|
this.#backfillBeforeWrite()
|
|
1966
|
-
//
|
|
1967
|
-
|
|
1968
|
-
//
|
|
1969
|
-
// State with its own lockFile path (constructed via the no-parent code path) writes its
|
|
1970
|
-
// own lockfile to `config.lockFile` instead of the rootDir default.
|
|
1971
|
-
//
|
|
1972
|
-
// Per-artifact compare-and-skip: each output's serialized text is compared against the
|
|
1973
|
-
// cached previous text and the brotli + writeFileSync are both skipped when they match.
|
|
1974
|
-
// Critical for webpack/esbuild watch-mode rebuilds, where compiler.hooks.done fires on
|
|
1975
|
-
// every rebuild but the captured graph usually doesn't change. Without this, every
|
|
1976
|
-
// rebuild brotli-compresses (quality 11) and rewrites every output.
|
|
1539
|
+
// AFTER the backfills, so buckets they create are covered too.
|
|
1540
|
+
if (this.config.writeBundle && this.config.packageJSON) this.includePackageJson()
|
|
1541
|
+
// Sidecars never write the lockfile (parent owns it); they only emit their bundle.
|
|
1977
1542
|
|
|
1978
1543
|
if (this.config.writeLockfile && !this.#parent) {
|
|
1979
1544
|
const lockText = this.lockData
|
|
@@ -1987,12 +1552,7 @@ export class State {
|
|
|
1987
1552
|
if (this.config.writeBundle) {
|
|
1988
1553
|
const sourcesPath = this.config.bundleFile || join(this.root, FILE_CODE)
|
|
1989
1554
|
if (this.config.resourcesBundleFile) {
|
|
1990
|
-
//
|
|
1991
|
-
// Each is a standalone v1 Bundle (cross-check on load enforces shape). hasContent
|
|
1992
|
-
// gates per half so an empty half (no code / no resources) is dropped under replace
|
|
1993
|
-
// rather than written as an empty bundle -- see #emitBundle. sources/resources size
|
|
1994
|
-
// is the exact emptiness test: a code bundle's entries/imports are non-empty only
|
|
1995
|
-
// when sources are (every entry/edge parent is itself an addFile'd source).
|
|
1555
|
+
// hasContent gates each half so an empty one is dropped under bundle=replace.
|
|
1996
1556
|
this.#lastCodeBundle = this.#emitBundle(
|
|
1997
1557
|
sourcesPath, this.sources.size > 0, this.#lastCodeBundle, () => this.codeBundle.serialize())
|
|
1998
1558
|
this.#lastResourcesBundle = this.#emitBundle(
|
|
@@ -2006,185 +1566,143 @@ export class State {
|
|
|
2006
1566
|
}
|
|
2007
1567
|
}
|
|
2008
1568
|
|
|
2009
|
-
//
|
|
2010
|
-
//
|
|
2011
|
-
// a Metro transform worker loading babel.config.js + its babel preset/plugins). Runs the
|
|
2012
|
-
// same backfills write() runs so observed native (Module._resolveFilename) resolutions and
|
|
2013
|
-
// stasis-core internal edges are materialized first. Content is intentionally omitted: the
|
|
2014
|
-
// snapshot carries project-relative paths + the import graph + formats + entries, and the
|
|
2015
|
-
// root re-reads each file's bytes from disk when it replays via mergeShard().
|
|
1569
|
+
// Snapshot of what THIS process observed, for a forked child to hand back to the root. Content is
|
|
1570
|
+
// omitted -- the root re-reads bytes from disk.
|
|
2016
1571
|
shardSnapshot() {
|
|
2017
1572
|
this.#backfillObservedResolutions()
|
|
2018
1573
|
this.#backfillBeforeWrite()
|
|
2019
|
-
//
|
|
2020
|
-
//
|
|
2021
|
-
|
|
2022
|
-
// MAX_SHARD_BYTES, past which mergeChildShards silently drops it) and make the root re-merge it
|
|
2023
|
-
// once per worker. #observed holds the files addFile/addFsDir recorded this run; modules, formats
|
|
2024
|
-
// AND imports are all filtered to it below. (Lockfile-seeded edges/formats live in
|
|
2025
|
-
// #lockImports/#lockFormats, but a bundle load seeds this.imports/this.formats DIRECTLY -- so we
|
|
2026
|
-
// filter explicitly rather than rely on which map holds the baseline.) Entries are dropped:
|
|
2027
|
-
// mergeShard never marks a merged file as an entry, and this.entries is itself seeded.
|
|
2028
|
-
const modules = new Map()
|
|
2029
|
-
for (const [dir, info] of this.modules) {
|
|
2030
|
-
const files = {}
|
|
2031
|
-
for (const rel of Object.keys(info.files)) {
|
|
2032
|
-
if (this.#observed.has(moduleFileKey(dir, rel))) files[rel] = info.files[rel]
|
|
2033
|
-
}
|
|
2034
|
-
if (Object.keys(files).length === 0) continue
|
|
2035
|
-
modules.set(dir, info.ecosystem === undefined
|
|
2036
|
-
? { name: info.name, version: info.version, files }
|
|
2037
|
-
: { name: info.name, version: info.version, ecosystem: info.ecosystem, files })
|
|
2038
|
-
}
|
|
2039
|
-
// formats: only observed files. imports: only edges whose PARENT was observed -- a child adds an
|
|
2040
|
-
// edge solely from a file it loaded (addImport's parent is addFile'd -> in #observed), so its
|
|
2041
|
-
// session edges are kept while bundle-seeded edges (parent never loaded here) are dropped; the
|
|
2042
|
-
// root already attests those from its own seed, so dropping them loses nothing.
|
|
1574
|
+
// Only what THIS process observed, never the seeded baseline (shard-size bloat). Entries are
|
|
1575
|
+
// dropped: a child's "entry" is its fork target, not a root's.
|
|
1576
|
+
const files = []
|
|
2043
1577
|
const formats = new Map()
|
|
2044
1578
|
for (const file of this.#observed) {
|
|
2045
|
-
const
|
|
2046
|
-
if (
|
|
1579
|
+
const format = this.formats.get(file)
|
|
1580
|
+
if (format !== undefined) formats.set(file, format)
|
|
1581
|
+
if (this.hashes.has(file)) files.push(file)
|
|
2047
1582
|
}
|
|
1583
|
+
// Only edges whose PARENT was observed, dropping the bundle-seeded ones the root already attests.
|
|
2048
1584
|
const imports = new Map()
|
|
2049
1585
|
for (const [conditions, byParent] of this.imports) {
|
|
2050
1586
|
let kept
|
|
2051
|
-
for (const
|
|
2052
|
-
|
|
2053
|
-
if (
|
|
1587
|
+
for (const parent of this.#observed) {
|
|
1588
|
+
const specs = byParent.get(parent)
|
|
1589
|
+
if (specs === undefined) continue
|
|
1590
|
+
if (kept === undefined) imports.set(conditions, (kept = new Map()))
|
|
2054
1591
|
kept.set(parent, specs)
|
|
2055
1592
|
}
|
|
2056
1593
|
}
|
|
2057
|
-
return
|
|
2058
|
-
config: this.config.values,
|
|
2059
|
-
entries: new Set(),
|
|
2060
|
-
modules,
|
|
2061
|
-
imports,
|
|
2062
|
-
formats,
|
|
2063
|
-
}).serialize()
|
|
1594
|
+
return serializeShard({ scope: this.config.scope, files, formats, imports })
|
|
2064
1595
|
}
|
|
2065
1596
|
|
|
2066
|
-
// Merge a child
|
|
2067
|
-
//
|
|
2068
|
-
//
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
const
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
if (this.
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
if (
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
this.addFile(url, { resource: true })
|
|
2123
|
-
} else if (format === undefined) {
|
|
2124
|
-
// Code file the child recorded with NO format -- it fs-READ a `.js`/`.ts` (or
|
|
2125
|
-
// `.jsx`/`.tsx`), where addFsFile defers module-vs-commonjs to the loader rather than
|
|
2126
|
-
// imposing one (Node's syntax-detection call). Replay the same way (inferFormat:false):
|
|
2127
|
-
// record the bytes but impose no format, so we don't bake in the legacy commonjs
|
|
2128
|
-
// default and collide -- at the frozen format check, or noupsert -- with the `module`
|
|
2129
|
-
// the loader records when that file is later imported. (Babel reading a worker's source
|
|
2130
|
-
// `.js` to transform it is exactly this case under `--fs --child-process`.)
|
|
2131
|
-
this.addFile(url, { inferFormat: false })
|
|
2132
|
-
} else {
|
|
2133
|
-
// Code file with a concrete loader format the child recorded (it imported the file, or
|
|
2134
|
-
// fs-read an unambiguous `.mjs`/`.cjs`/`.json`). Carry it: without it, a child-only ESM
|
|
2135
|
-
// `.js`/`.ts` under a no-`type` package would re-default to commonjs, and the subsequent
|
|
2136
|
-
// frozen run -- where the child re-loads it as module -- would reject the mismatch.
|
|
2137
|
-
// addFile still asserts the format agrees with an explicit package.json `type` when one
|
|
2138
|
-
// exists, so this can't smuggle a wrong format.
|
|
2139
|
-
this.addFile(url, { format })
|
|
2140
|
-
}
|
|
2141
|
-
} catch {
|
|
2142
|
-
// A file the child loaded but that's gone/unreadable here, or that addFile rejects
|
|
2143
|
-
// (e.g. an out-of-scope path under a different scope): skip it. The edge replay below
|
|
2144
|
-
// tolerates the resulting gap the same way.
|
|
1597
|
+
// Merge a child's shardSnapshot() as if this process read those files itself: replay each file
|
|
1598
|
+
// (re-reading bytes/listings from disk) and each edge. Best-effort -- a gone/out-of-scope record is
|
|
1599
|
+
// skipped rather than aborting the merge.
|
|
1600
|
+
mergeShard(shardText) {
|
|
1601
|
+
const shard = parseShard(shardText)
|
|
1602
|
+
// Defense-in-depth: cross-scope is signature-rejected upstream, but a mismatch here means a
|
|
1603
|
+
// malformed/foreign shard -- refuse.
|
|
1604
|
+
assert.equal(shard.scope, this.config.scope, `shard scope "${shard.scope}" != root scope "${this.config.scope}"`)
|
|
1605
|
+
// An in-root shard KEY may resolve (through a symlink) OUTSIDE the root on THIS disk, so every
|
|
1606
|
+
// replay re-checks real-path containment -- the same boundary fs.js enforces on live --fs reads.
|
|
1607
|
+
let realRoot
|
|
1608
|
+
const realContained = (absolute) => {
|
|
1609
|
+
if (realRoot === undefined) {
|
|
1610
|
+
try { realRoot = realpathSync(this.root) } catch { realRoot = this.root }
|
|
1611
|
+
}
|
|
1612
|
+
let real
|
|
1613
|
+
try { real = realpathSync(absolute) } catch { return false }
|
|
1614
|
+
const rel = relative(realRoot, real)
|
|
1615
|
+
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))
|
|
1616
|
+
}
|
|
1617
|
+
// Files first, then edges. Merged files are NEVER marked entries -- a child's "entry" is its
|
|
1618
|
+
// fork-target main, not a root entry.
|
|
1619
|
+
for (const file of shard.files) {
|
|
1620
|
+
const absolute = resolve(this.root, file)
|
|
1621
|
+
// Skip a key whose on-disk path escapes the root through a symlink.
|
|
1622
|
+
if (!realContained(absolute)) continue
|
|
1623
|
+
// ABOVE the already-attested fast path below: a child that re-read a file and found no bit
|
|
1624
|
+
// refutes it by OMISSION, so every file the shard RECORDS needs an observation. Re-derived
|
|
1625
|
+
// from THIS process's disk, so the shard cannot inject a forged bit.
|
|
1626
|
+
this.#recordExecutable(file, absolute)
|
|
1627
|
+
// Skip a file the root already attests (the byte re-read dominates merge cost), EXCEPT to
|
|
1628
|
+
// carry a concrete format the root LACKS, else a later frozen run rejects the unattested format.
|
|
1629
|
+
if (this.hashes.has(file)) {
|
|
1630
|
+
const shardFormat = shard.formats.get(file)
|
|
1631
|
+
if (shardFormat === undefined || (this.formats.get(file) ?? this.#lockFormats?.get(file)) !== undefined) continue
|
|
1632
|
+
}
|
|
1633
|
+
const url = pathToFileURL(absolute).toString()
|
|
1634
|
+
const format = shard.formats.get(file)
|
|
1635
|
+
try {
|
|
1636
|
+
if (format === 'directory') {
|
|
1637
|
+
// Range-check FIRST so a child-forged `..` key can't make us list it.
|
|
1638
|
+
const relFromRoot = relative(this.root, absolute)
|
|
1639
|
+
if (relFromRoot.startsWith('..') || isAbsolute(relFromRoot)) continue
|
|
1640
|
+
this.addFsDir(url, readdirSync(absolute))
|
|
1641
|
+
} else if (format === 'resource' || format === 'resource:base64') {
|
|
1642
|
+
// Let addFile re-derive the exact resource tag from the re-read bytes rather than trusting
|
|
1643
|
+
// the shard's and risking a mismatch.
|
|
1644
|
+
this.addFile(url, { resource: true })
|
|
1645
|
+
} else if (format === undefined) {
|
|
1646
|
+
// No format recorded (an fs-READ .js/.ts): inferFormat:false so we don't bake in the
|
|
1647
|
+
// commonjs default and collide with the `module` the loader records if it's later imported.
|
|
1648
|
+
this.addFile(url, { inferFormat: false })
|
|
1649
|
+
} else {
|
|
1650
|
+
// Carry the child's format, else a child-only ESM .js under a no-`type` package re-defaults
|
|
1651
|
+
// to commonjs and a frozen run rejects the mismatch.
|
|
1652
|
+
this.addFile(url, { format })
|
|
2145
1653
|
}
|
|
1654
|
+
} catch {
|
|
1655
|
+
// Gone/unreadable, or addFile-rejected (out of scope): skip, like the edge replay below.
|
|
2146
1656
|
}
|
|
2147
1657
|
}
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
1658
|
+
// Stat records live ONLY in the shard's formats map, so replay each here, re-deriving the kind
|
|
1659
|
+
// from DISK (a shard can't inject a forged kind). Range-check BEFORE touching disk.
|
|
1660
|
+
for (const [file, format] of shard.formats) {
|
|
1661
|
+
if (!isStatFormat(format)) continue
|
|
1662
|
+
const absolute = resolve(this.root, file)
|
|
1663
|
+
const relFromRoot = relative(this.root, absolute)
|
|
1664
|
+
if (relFromRoot.startsWith('..') || isAbsolute(relFromRoot)) continue
|
|
1665
|
+
try {
|
|
1666
|
+
// statSync, NOT lstatSync: the child's capture followed symlinks, so this reproduces the kind
|
|
1667
|
+
// a legitimate shard carries where lstatSync would report the link and drop it.
|
|
1668
|
+
const stats = statSync(absolute)
|
|
1669
|
+
const kind = stats.isDirectory() ? 'directory' : stats.isFile() ? 'file' : null
|
|
1670
|
+
if (kind === null) continue
|
|
1671
|
+
// statSync follows links: a symlink pointing OUT must not attest an external kind under an
|
|
1672
|
+
// in-root key.
|
|
1673
|
+
if (!realContained(absolute)) continue
|
|
1674
|
+
this.addFsStat(pathToFileURL(absolute).toString(), kind)
|
|
1675
|
+
} catch {
|
|
1676
|
+
// Gone/dangling, or a kind conflict with the root's capture: skip.
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
for (const [conditions, byParent] of shard.imports) {
|
|
1680
|
+
// Reverse #conditionsKey: '*', else a ', '-joined list with an optional ' (with: {json})'
|
|
1681
|
+
// suffix -- parse it so attributed edges merge under the child's key.
|
|
1682
|
+
let condStr = conditions
|
|
1683
|
+
let importAttributes
|
|
1684
|
+
const withAt = conditions.indexOf(' (with: ')
|
|
1685
|
+
if (withAt !== -1) {
|
|
1686
|
+
condStr = conditions.slice(0, withAt)
|
|
1687
|
+
try { importAttributes = JSON.parse(conditions.slice(withAt + ' (with: '.length, -1)) } catch { /* leave undefined */ }
|
|
1688
|
+
}
|
|
1689
|
+
const cond = condStr === '*' ? '*' : condStr.split(', ')
|
|
1690
|
+
for (const [parent, specs] of byParent) {
|
|
1691
|
+
const parentURL = pathToFileURL(resolve(this.root, parent)).toString()
|
|
1692
|
+
for (const [specifier, file] of specs) {
|
|
1693
|
+
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
1694
|
+
try {
|
|
1695
|
+
this.addImport(parentURL, specifier, url, { conditions: cond, importAttributes })
|
|
1696
|
+
} catch {
|
|
1697
|
+
// Same best-effort stance as addFile above.
|
|
2171
1698
|
}
|
|
2172
1699
|
}
|
|
2173
1700
|
}
|
|
2174
1701
|
}
|
|
2175
1702
|
}
|
|
2176
1703
|
|
|
2177
|
-
// Emit one bundle artifact, returning its new "last serialized" cache
|
|
2178
|
-
//
|
|
2179
|
-
// this artifact, so DELETE any file at `path` rather than write an empty bundle. A
|
|
2180
|
-
// `bundle=replace` run is authoritative -- it deliberately ignores the prior on-disk
|
|
2181
|
-
// bundle (the constructor skips loading it), so a stale file the run didn't produce
|
|
2182
|
-
// must not survive as the "old one". Returning null resets the cache so a later
|
|
2183
|
-
// non-empty write() (watch-mode rebuild) re-emits the file.
|
|
2184
|
-
// - otherwise (has content, OR empty under add): serialize and write, skipping the
|
|
2185
|
-
// brotli + writeFileSync when the text is byte-identical to the last write (the
|
|
2186
|
-
// watch-mode compare-and-skip this cache exists for). add keeps emitting an empty
|
|
2187
|
-
// bundle so the file always exists -- only replace prunes a not-written artifact.
|
|
1704
|
+
// Emit one bundle artifact, returning its new "last serialized" cache. Empty under bundle=replace
|
|
1705
|
+
// DELETES the file (a replace run is authoritative); unchanged text skips brotli + writeFileSync.
|
|
2188
1706
|
#emitBundle(path, hasContent, lastText, serialize) {
|
|
2189
1707
|
if (!hasContent && this.config.replaceBundle) {
|
|
2190
1708
|
rmSync(path, { force: true })
|
|
@@ -2193,7 +1711,7 @@ export class State {
|
|
|
2193
1711
|
const text = serialize()
|
|
2194
1712
|
if (text !== lastText) {
|
|
2195
1713
|
mkdirSync(dirname(path), { recursive: true })
|
|
2196
|
-
writeFileSync(path, brotliCompressSync(text, brotliOptions()))
|
|
1714
|
+
writeFileSync(path, brotliCompressSync(text, brotliOptions(this.config.brotliQuality)))
|
|
2197
1715
|
return text
|
|
2198
1716
|
}
|
|
2199
1717
|
return lastText
|
|
@@ -2203,54 +1721,23 @@ export class State {
|
|
|
2203
1721
|
return this.#parent
|
|
2204
1722
|
}
|
|
2205
1723
|
|
|
2206
|
-
// Capture stasis-core's own files + internal edges
|
|
2207
|
-
//
|
|
2208
|
-
//
|
|
2209
|
-
// the resolve hook records the edge but the load hook never fires, leaving
|
|
2210
|
-
// addFile / addImport blind to the file's bytes AND its outgoing edges. This
|
|
2211
|
-
// BFS, scoped strictly to the `<root>/.../@exodus/stasis-core/src/<name>.js`
|
|
2212
|
-
// shape, walks the graph starting from missing imports targets + already-
|
|
2213
|
-
// captured stasis-core files: for each, addFile (asserting the shape),
|
|
2214
|
-
// statically scans for relative-`.js` specifiers, resolves them, and
|
|
2215
|
-
// addImports each edge. The strict shape allowlist + existsSync gate filter
|
|
2216
|
-
// out comment-noise like a `require('../../pkg/x')` sitting in a JSDoc block.
|
|
2217
|
-
//
|
|
2218
|
-
// Called from write() so every State variant (preload via beforeExit/exit;
|
|
2219
|
-
// sidecar / independent plugin States via compiler.hooks.done) gets the
|
|
2220
|
-
// same coverage.
|
|
1724
|
+
// Capture stasis-core's own files + internal edges the live hooks missed: for a preload-cached
|
|
1725
|
+
// module the resolve hook records the edge but the load hook never fires. The BFS below is scoped
|
|
1726
|
+
// to the stasis-core src shape.
|
|
2221
1727
|
#backfillBeforeWrite() {
|
|
2222
|
-
// List what the live hooks missed first. An empty list means there's
|
|
2223
|
-
// nothing to do -- either the live hooks captured everything (the common
|
|
2224
|
-
// case for non-preload runs and for plugin-driven States that don't
|
|
2225
|
-
// touch preload-cached modules), or we're a State variant that doesn't
|
|
2226
|
-
// observe imports at all.
|
|
2227
1728
|
const missing = this.#collectMissingImportedFiles()
|
|
2228
1729
|
if (missing.size === 0) return
|
|
2229
1730
|
|
|
2230
1731
|
const isStasisCoreFile = (file) => STASIS_CORE_FILE_RE.test(`/${file}`)
|
|
2231
1732
|
|
|
2232
|
-
//
|
|
2233
|
-
//
|
|
2234
|
-
// BFS below). Everything else uncaptured is a resolution we attest WITHOUT
|
|
2235
|
-
// bundling: require.resolve() of a module never loaded in-process (an optional-
|
|
2236
|
-
// dependency probe, a forked-worker child like worker-farm's), or a file Node
|
|
2237
|
-
// can't serve from a bundle anyway (a .node addon). Those edges stay in
|
|
2238
|
-
// this.imports -- the user needs the resolution pinned even though the bytes are
|
|
2239
|
-
// absent -- and we do nothing further with them here. The load-time getFile
|
|
2240
|
-
// gate, not this capture-time pass, enforces the trust boundary: an actual load
|
|
2241
|
-
// of un-bundled bytes still fails closed there, while require.resolve (which
|
|
2242
|
-
// never loads) is served the attested path.
|
|
1733
|
+
// Only stasis-core files are reconstructed; everything else stays a resolution attested without
|
|
1734
|
+
// bytes (the load-time getFile gate, not this pass, enforces the trust boundary).
|
|
2243
1735
|
const stasisCoreMissing = [...missing].filter((file) => isStasisCoreFile(file))
|
|
2244
1736
|
if (stasisCoreMissing.length === 0) return
|
|
2245
1737
|
|
|
2246
1738
|
const preloadRel = this.#preloadRoot ? relative(this.root, this.#preloadRoot) : null
|
|
2247
1739
|
const canBackfill = preloadRel !== null && !preloadRel.startsWith('..')
|
|
2248
1740
|
|
|
2249
|
-
// We have stasis-core files to reconstruct but no usable preloadRoot (no loader
|
|
2250
|
-
// present, or stasis-core is a workspace sibling not under state.root): nothing
|
|
2251
|
-
// this method can do, and a stasis-core source going uncaptured IS a real gap --
|
|
2252
|
-
// the bundle would be missing part of its own runtime. Fail loud rather than
|
|
2253
|
-
// silently ship it.
|
|
2254
1741
|
assert.ok(canBackfill,
|
|
2255
1742
|
`state.write() has imports referencing un-captured stasis-core files but no usable preloadRoot ` +
|
|
2256
1743
|
`(preloadRoot=${this.#preloadRoot ?? 'unset'}). The live load hook missed an in-scope target -- ` +
|
|
@@ -2258,29 +1745,14 @@ export class State {
|
|
|
2258
1745
|
|
|
2259
1746
|
const PRELOAD_CONDITIONS = ['node', 'import', 'module-sync', 'node-addons']
|
|
2260
1747
|
const stasisAddFile = (file) => {
|
|
2261
|
-
// Tight invariant: every backfilled file MUST match the stasis-core
|
|
2262
|
-
// source-file shape. The caller pre-filters via isStasisCoreFile, but
|
|
2263
|
-
// asserting here keeps the contract local and catches any future code
|
|
2264
|
-
// path that forgets.
|
|
2265
1748
|
assert.ok(isStasisCoreFile(file),
|
|
2266
1749
|
`state.write() backfill refused: '${file}' is not a stasis-core source file ` +
|
|
2267
1750
|
`(expected '.../@exodus/stasis-core/src/<name>.js')`)
|
|
2268
1751
|
this.addFile(pathToFileURL(resolve(this.root, file)).toString(), { reason: null }) // backfill is a write-time self-containment step, not a consumer observation -- don't attribute it
|
|
2269
1752
|
}
|
|
2270
1753
|
|
|
2271
|
-
//
|
|
2272
|
-
//
|
|
2273
|
-
// live load hook missed because Node's module cache short-circuited it.
|
|
2274
|
-
// (b) every stasis-core file already in sources -- a static `stasis
|
|
2275
|
-
// bundle` pass may have pulled one in via the live hook; we still
|
|
2276
|
-
// need to walk its transitive imports.
|
|
2277
|
-
//
|
|
2278
|
-
// For each queued file: addFile if not yet captured, then scan its
|
|
2279
|
-
// source for relative-`.js` specifiers, validate the resolved target,
|
|
2280
|
-
// addImport the edge, and queue the target. The single addFile site
|
|
2281
|
-
// covers seeds AND transitive targets -- a queued target gets addFile'd
|
|
2282
|
-
// on its next pop, no separate inner addFile call needed. BFS converges
|
|
2283
|
-
// because the matching file set is finite and `processed` dedups.
|
|
1754
|
+
// Seeded from the missing files + every stasis-core file already in sources, to walk their
|
|
1755
|
+
// transitive imports too.
|
|
2284
1756
|
const queue = [...stasisCoreMissing]
|
|
2285
1757
|
for (const file of this.sources.keys()) {
|
|
2286
1758
|
if (isStasisCoreFile(file)) queue.push(file)
|
|
@@ -2305,26 +1777,15 @@ export class State {
|
|
|
2305
1777
|
queue.push(targetFile)
|
|
2306
1778
|
this.addImport(baseURL, specifier, targetURL, { conditions: PRELOAD_CONDITIONS })
|
|
2307
1779
|
}
|
|
2308
|
-
//
|
|
2309
|
-
// (`import ... from` / `export ... from`, single-quoted, basename without `.`).
|
|
2310
|
-
// Tight enough to ignore JSDoc / template-literal false positives, narrow enough
|
|
2311
|
-
// to reject parent-relative (`../`), subpath (`./dir/file.js`), or extensionless.
|
|
2312
|
-
// The `.cjs` arm covers state.js -> ./package.cjs (the version re-export shim).
|
|
1780
|
+
// stasis-core's ESM import shape, tight enough to ignore JSDoc/template-literal false positives.
|
|
2313
1781
|
for (const m of source.matchAll(/ from '(\.\/[a-zA-Z-]+\.c?js)'\n/g)) recordEdge(m[1])
|
|
2314
|
-
//
|
|
2315
|
-
// (src/package.cjs) re-exporting stasis-core's own package.json so state.js can
|
|
2316
|
-
// read its version. Matched exactly (not a general require/`../` relaxation) to
|
|
2317
|
-
// stay as tight as the ESM arm.
|
|
1782
|
+
// The src/package.cjs version shim, matched exactly.
|
|
2318
1783
|
for (const m of source.matchAll(/require\('(\.\.\/package\.json)'\)/g)) recordEdge(m[1])
|
|
2319
1784
|
}
|
|
2320
1785
|
}
|
|
2321
1786
|
|
|
2322
|
-
//
|
|
2323
|
-
//
|
|
2324
|
-
// in-scope set (full: everything under state.root; nm: only node_modules).
|
|
2325
|
-
// Out-of-scope files are NOT included -- the live hooks correctly skip them
|
|
2326
|
-
// (workspace files under nm-scope), and including them would force the
|
|
2327
|
-
// backfill caller to wave away false alarms.
|
|
1787
|
+
// Files referenced by this.imports but not captured, restricted to the in-scope set (out-of-scope
|
|
1788
|
+
// files are excluded -- the live hooks correctly skip them).
|
|
2328
1789
|
#collectMissingImportedFiles() {
|
|
2329
1790
|
const missing = new Set()
|
|
2330
1791
|
for (const [, byParent] of this.imports) {
|
|
@@ -2332,10 +1793,6 @@ export class State {
|
|
|
2332
1793
|
for (const [, file] of specifiers) {
|
|
2333
1794
|
if (typeof file !== 'string') continue
|
|
2334
1795
|
if (missing.has(file)) continue
|
|
2335
|
-
// Union of three "captured" signals: hashes (addFile ran -- present
|
|
2336
|
-
// under any lock mode), sources/resources (bundle pre-seeded -- present
|
|
2337
|
-
// under bundle=load even without a lockfile). Any one suffices; a
|
|
2338
|
-
// file in NONE of them is the actual missing-from-capture case.
|
|
2339
1796
|
if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) continue
|
|
2340
1797
|
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
2341
1798
|
if (!this.config.full && !this.inNodeModules(url)) continue
|
|
@@ -2347,13 +1804,7 @@ export class State {
|
|
|
2347
1804
|
}
|
|
2348
1805
|
}
|
|
2349
1806
|
|
|
2350
|
-
//
|
|
2351
|
-
//
|
|
2352
|
-
// src/package.cjs version shim). Used by the backfill BFS to reject anything that
|
|
2353
|
-
// doesn't look like a real stasis-core file (comment noise, paths escaping `src/`,
|
|
2354
|
-
// etc.). Matched against `/${projectRelativeFile}` so the leading-`/` anchor isn't
|
|
2355
|
-
// fooled by a file at the project root happening to end in `@exodus/stasis-core/...`.
|
|
2356
|
-
// Source basenames are `[a-zA-Z-]+` only -- no `.` in the basename, which is why
|
|
2357
|
-
// `state.util.js` was renamed to `state-util.js`.
|
|
1807
|
+
// Backfillable stasis-core paths. Anchored against `/${file}` so a project-root file merely ending
|
|
1808
|
+
// in `@exodus/stasis-core/...` isn't matched.
|
|
2358
1809
|
const STASIS_CORE_FILE_RE = /\/@exodus\/stasis-core\/(?:src\/[a-zA-Z-]+\.c?js|package\.json)$/
|
|
2359
1810
|
|