@exodus/stasis-core 1.0.0-beta.13 → 1.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/hooks.js CHANGED
@@ -8,6 +8,7 @@ import assert from 'node:assert/strict'
8
8
 
9
9
  import { State } from './state.js'
10
10
  import { installFsHooks } from './fs.js'
11
+ import { inScope } from './state-util.js'
11
12
  import { NATIVE_BUILD_FORMATS, NODE_FORMATS, RESOURCE_FORMATS, SOURCE_LANGUAGE_FORMATS, STAT_FORMATS, erasedTypeScriptFormat, nestedMap } from './util.js'
12
13
 
13
14
  // Snapshot off the namespace so `--fs` can't redirect stasis's own capture/shard reads.
@@ -368,7 +369,7 @@ function load(url, context, nextLoad) {
368
369
 
369
370
  if (state?.config.loadBundle) {
370
371
  // node_modules scope serves only nm deps; gate on inNodeModules(), not a raw substring (symlinks).
371
- if (!state.config.full && !state.inNodeModules(url)) return nextLoad(url, context)
372
+ if (!inScope(state, url)) return nextLoad(url, context)
372
373
  const { source, format } = state.getFile(url)
373
374
  // Cross-check format only when the resolve chain set it; the formats map is itself attested
374
375
  // against the lockfile at construction. A transforming preload (`stasis run --import tsx`)
@@ -564,7 +565,7 @@ function patchCjsResolution() {
564
565
  if (loadMode && typeof request === 'string' && !isBuiltin(request)) {
565
566
  if (typeof parent?.filename === 'string') {
566
567
  const parentURL = pathToFileURL(parent.filename).toString()
567
- if (state.config.full || state.inNodeModules(parentURL)) {
568
+ if (inScope(state, parentURL)) {
568
569
  // Module._resolveFilename resolves for require(), whose target settles a divergent edge.
569
570
  const resolved = state.resolveBundled(parentURL, request, { conditions: ['require'] })
570
571
  if (resolved !== undefined) {
@@ -578,7 +579,7 @@ function patchCjsResolution() {
578
579
  // record) keeps a listing or payload-free stat from hijacking resolution. Node 24.14-24.17
579
580
  // re-resolve here every commonjs-sync eval; the no-parent shape survives on >=24.18 too.
580
581
  const url = pathToFileURL(request).toString()
581
- if ((state.config.full || state.inNodeModules(url)) && state.hasFsFileContent(url)) {
582
+ if (inScope(state, url) && state.hasFsFileContent(url)) {
582
583
  assertInfraMatchesBundle(url)
583
584
  return resolvePath(request)
584
585
  }
package/src/host.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import * as fs from 'node:fs'
2
2
  import { createRequire, findPackageJSON } from 'node:module'
3
+ import { join, matchesGlob, relative } from 'node:path'
3
4
  import { pathToFileURL } from 'node:url'
4
5
 
5
6
  // Snapshotted before `stasis run --fs` patches node:fs, whose realpathSync doesn't throw ENOENT and
6
7
  // whose statSync answers with synthetic modes.
7
- const { lstatSync, opendirSync, readFileSync, readdirSync, readlinkSync, realpathSync, statSync } = fs
8
+ const { globSync, lstatSync, opendirSync, readFileSync, readdirSync, readlinkSync, realpathSync, statSync } = fs
8
9
 
9
10
  export const byName = (a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
10
11
 
@@ -34,6 +35,16 @@ export const diskHost = {
34
35
  },
35
36
  readlink: (p) => (lstatSync(p).isSymbolicLink() ? readlinkSync(p) : null),
36
37
  realpath: (p) => realpathSync(p),
38
+ // fs.globSync's matches of `pattern` from `cwd`, as it spells them, never walking into a path
39
+ // `exclude` takes, given its absolute path (Node leaves one out where its `**` meets it alone).
40
+ // With `exclude`, the walk is over Dirents, which know their parent (Node hands its callback a
41
+ // bare name for a file otherwise), each match then spelled from `cwd`.
42
+ glob(pattern, { cwd, exclude } = {}) {
43
+ if (exclude === undefined) return globSync(pattern, { cwd })
44
+ const pathOf = (dirent) => join(dirent.parentPath, dirent.name)
45
+ return globSync(pattern, { cwd, withFileTypes: true, exclude: (dirent) => exclude(pathOf(dirent)) }).map((dirent) => relative(cwd, pathOf(dirent)))
46
+ },
47
+ matchesGlob,
37
48
  findPackageJSON: (p) => findPackageJSON(pathToFileURL(p).href),
38
49
  resolve(parentFile, specifier, conditions) {
39
50
  if (parentFile !== lastParent) {
package/src/lockfile.js CHANGED
@@ -1,4 +1,4 @@
1
- import { assert, assertArtifactPath, duplicateKeyError, serializeExecutable, flatFileKeys, groupModules, hasNodeModulesSegment, mergeExecutableSets, mergeFormatMaps, mergeImportMaps, mergeModuleMaps, normalizeModule, parseEntries, parseExecutable, parseFormats, parseImports, serializeEntries, serializeFormats, serializeImports } from './artifact-util.js'
1
+ import { assert, assertArtifactPath, duplicateKeyError, serializeExecutable, flatFileKeys, groupModules, hasNodeModulesSegment, mergeExecutableSets, mergeFormatMaps, mergeImportMaps, mergeModuleMaps, moduleFileKey, moduleInfo, normalizeModule, parseEntries, parseExecutable, parseFormats, parseImports, serializeEntries, serializeFormats, serializeImports } from './artifact-util.js'
2
2
 
3
3
  const VERSION = 0
4
4
 
@@ -33,6 +33,20 @@ export class Lockfile {
33
33
  this.executable = executable ?? new Set()
34
34
  }
35
35
 
36
+ // The companion lockfile of `bundle`: its shape, each file's content swapped for its integrity,
37
+ // `integrityOf(file, content)` of its project-relative path. Imports and formats carry across, else
38
+ // the lockfile is bytes-only and a frozen run skips those cross-checks; the file set is the bundle's,
39
+ // so its executable list attests it as-is.
40
+ static fromBundle(bundle, integrityOf) {
41
+ const modules = new Map()
42
+ for (const [dir, m] of bundle.modules) {
43
+ const files = Object.create(null)
44
+ for (const [rel, content] of Object.entries(m.files)) files[rel] = integrityOf(moduleFileKey(dir, rel), content)
45
+ modules.set(dir, moduleInfo({ ...m, files }))
46
+ }
47
+ return new Lockfile({ config: bundle.config, entries: bundle.entries, modules, imports: bundle.imports, formats: bundle.formats, executable: bundle.executable })
48
+ }
49
+
36
50
  static parse(text) {
37
51
  const json = JSON.parse(text)
38
52
  assert(json.version === VERSION)
package/src/state-util.js CHANGED
@@ -2,8 +2,10 @@ import assert from 'node:assert/strict'
2
2
  import { hash } from 'node:crypto'
3
3
  import * as fs from 'node:fs'
4
4
  import { join, resolve, sep } from 'node:path'
5
+ import { pathToFileURL } from 'node:url'
5
6
 
6
7
  import { diskHost } from './host.js'
8
+ import { isPathWithin } from './util.js'
7
9
 
8
10
  // Genuine fs readers snapshotted off the namespace before any --fs patch. A plain `import { readFileSync }`
9
11
  // won't do: --fs monkey-patches fs then syncBuiltinESMExports() rebinds named imports, so snapshotting dodges that.
@@ -11,6 +13,21 @@ export const realReadFileSync = fs.readFileSync
11
13
  export const realReadFile = fs.promises.readFile
12
14
  export const realReaddirSync = fs.readdirSync
13
15
  export const realExistsSync = fs.existsSync
16
+ const realRealpathSync = fs.realpathSync
17
+
18
+ let realRootCache
19
+ // Capture-side containment: the REAL location of `abs` must stay inside `root`'s real path (`root`
20
+ // itself where that has none), else an in-tree symlink pointing OUT would pull external content in.
21
+ export function realContained(root, abs) {
22
+ if (realRootCache?.root !== root) {
23
+ let real
24
+ try { real = realRealpathSync(root) } catch { real = root }
25
+ realRootCache = { root, real }
26
+ }
27
+ let real
28
+ try { real = realRealpathSync(abs) } catch { return false }
29
+ return isPathWithin(realRootCache.real, real)
30
+ }
14
31
 
15
32
  assert.equal(sep, '/', 'Not tested on Windows')
16
33
 
@@ -44,3 +61,12 @@ export function canonicalizePath(p, host = diskHost) {
44
61
  return abs
45
62
  }
46
63
  }
64
+
65
+ // Whether `url` is in `state`'s scope, as it attests one: anywhere under scope=full, else in
66
+ // node_modules. Over State's public members alone, so a plugin may ask it of a State another
67
+ // stasis-core copy made (State.preload).
68
+ export const inScope = (state, url) => state.config.full || state.inNodeModules(url)
69
+
70
+ // Whether the file at `absolute` is one `state`'s bundle serves under load: within its root (outside
71
+ // it: system files, scratch, read from disk) and in its scope. Over State's public members, as inScope.
72
+ export const servedByBundle = (state, absolute) => isPathWithin(state.root, absolute) && inScope(state, pathToFileURL(absolute).toString())
package/src/state.js CHANGED
@@ -3,23 +3,22 @@ import { isUtf8 } from 'node:buffer'
3
3
  import * as fs from 'node:fs'
4
4
  import { join, resolve, relative, basename, dirname, extname, isAbsolute } from 'node:path'
5
5
  import { fileURLToPath, pathToFileURL } from 'node:url'
6
- import { brotliCompressSync, brotliDecompressSync } from 'node:zlib'
7
6
 
8
7
  import { Config } from './config.js'
9
8
  import { Bundle, mergeRepo } from './bundle.js'
10
9
  import { Lockfile } from './lockfile.js'
11
10
  import { parseShard, serializeShard } from './shard.js'
12
- import { canonicalizePath, sha512integrity, readFileSyncMaybe, noupsert } from './state-util.js'
13
- import { brotliOptions } from './brotli.js'
14
- import { CODE_EXTENSIONS, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, isPlainObject, isStatFormat, isSubpackage, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, sortPaths, splitNodeModulesPath, toPosix, withMetadataOf } from './util.js'
15
- import { detectRepo, namedManifestOf, packageJSONStat, packageJSONText, packageRepo, readModuleManifest, scopeManifestOf, vendorDirOf, vendoredPackageOf } from './bundle-util.js'
11
+ import { canonicalizePath, inScope, sha512integrity, readFileSyncMaybe, noupsert, realContained } from './state-util.js'
12
+ import { compressBundleText, parseBundleBytes } from './brotli.js'
13
+ import { CODE_EXTENSIONS, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, importTargetsEqual, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, isPlainObject, isStatFormat, isSubpackage, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, sortPaths, splitNodeModulesPath, toPosix, withMetadataOf } from './util.js'
14
+ import { detectRepo, invalidPackageConfig, namedManifestOf, packageJSONStat, packageJSONText, packageRepo, readModuleManifest, scopeManifestOf, vendorDirOf, vendoredPackageOf } from './bundle-util.js'
16
15
  import { expandExtra, validationError } from './add.js'
17
16
  import { diskHost } from './host.js'
18
17
  import corePackage from './package.cjs'
19
18
 
20
19
  // Destructure off the namespace: captures the real fns at eval time, so writes survive --mock's
21
20
  // syncBuiltinESMExports() remock of node:fs.
22
- const { existsSync, mkdirSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } = fs
21
+ const { existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } = fs
23
22
 
24
23
  const FILE_CONFIG = 'stasis.config.json'
25
24
  const FILE_LOCK = 'stasis.lock.json'
@@ -30,10 +29,6 @@ const VERSION = corePackage.version
30
29
 
31
30
  const CAN_OBSERVE_EXECUTE_BITS = canObserveExecuteBits()
32
31
 
33
- const invalidPackageConfig = (pkgAbsolute, cause) => Object.assign(
34
- cause === undefined ? new Error(`Invalid package config ${pkgAbsolute}.`) : new Error(`Invalid package config ${pkgAbsolute}.`, { cause }),
35
- { code: 'ERR_INVALID_PACKAGE_CONFIG' })
36
-
37
32
  // A package.json's fields; one that is no JSON object is refused as Node refuses it.
38
33
  function readPackageJSON(host, pkgAbsolute) {
39
34
  const buf = host.readFile(pkgAbsolute)
@@ -44,7 +39,7 @@ function readPackageJSON(host, pkgAbsolute) {
44
39
  } catch (cause) {
45
40
  throw invalidPackageConfig(pkgAbsolute, cause)
46
41
  }
47
- if (json === null || typeof json !== 'object' || Array.isArray(json)) throw invalidPackageConfig(pkgAbsolute)
42
+ if (!isPlainObject(json)) throw invalidPackageConfig(pkgAbsolute)
48
43
  return json
49
44
  }
50
45
 
@@ -173,11 +168,9 @@ export class State {
173
168
  // statically parses files under it to recover edges the resolve hook never observed.
174
169
  #preloadRoot = null
175
170
 
176
- // Per-artifact "last serialized" caches: write() skips brotli + writeFileSync on unchanged text.
171
+ // Per-artifact "last serialized" caches (the bundles' by path): write() skips brotli + writeFileSync on unchanged text.
177
172
  #lastLockData = null
178
- #lastUnifiedBundle = null
179
- #lastCodeBundle = null
180
- #lastResourcesBundle = null
173
+ #lastBundleText = new Map()
181
174
  // Lazy `{ value: detectRepo(this.root, this.#host) }`.
182
175
  #detectedRepoCache
183
176
  // Half ('code' | 'resources') -> `repo` of the bundle absorbed there for adding to.
@@ -229,7 +222,7 @@ export class State {
229
222
  const sourcesPath = this.config.bundleFile
230
223
  const sources = this.#readMaybe(sourcesPath)
231
224
  if (sources && !this.config.replaceBundle) {
232
- const bundle = Bundle.parse(brotliDecompressSync(sources).toString('utf-8'))
225
+ const bundle = parseBundleBytes(sources)
233
226
  // v0 bundle has no per-file formats or import map -- serving one via a sidecar widens trust.
234
227
  assert.equal(bundle.version, Bundle.VERSION,
235
228
  `stasis sidecar requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
@@ -419,7 +412,7 @@ export class State {
419
412
  }
420
413
 
421
414
  #loadCodeBundle(sources, sourcesPath, lockfileLoaded) {
422
- const bundle = Bundle.parse(brotliDecompressSync(sources).toString('utf-8'))
415
+ const bundle = parseBundleBytes(sources)
423
416
  // Bundle.parse accepts v0 for offline tooling; the runtime refuses it -- v0 has no per-file
424
417
  // formats or import map, so serving one widens the trust boundary.
425
418
  assert.equal(bundle.version, Bundle.VERSION,
@@ -547,7 +540,7 @@ export class State {
547
540
  // Load the resources half of a split-bundle layout: ONLY resource-format files, unioned into the
548
541
  // bundleFile state.
549
542
  #absorbResourcesBundle(resourcesData, { lockfileLoaded, resourcesPath }) {
550
- const bundle = Bundle.parse(brotliDecompressSync(resourcesData).toString('utf-8'))
543
+ const bundle = parseBundleBytes(resourcesData)
551
544
  assert.equal(bundle.version, Bundle.VERSION,
552
545
  `stasis run requires a v1 resources bundle; ${resourcesPath} is v${bundle.version}. ` +
553
546
  `Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point to upgrade.`)
@@ -610,12 +603,26 @@ export class State {
610
603
  attested = attested.get(targetPlatform)
611
604
  assert.ok(attested !== undefined, `${what} resolution ${edge} is not attested for platform '${targetPlatform}' by the ${source}`)
612
605
  }
613
- // A per-platform edge is a Map<platform, target>: compare structurally (assert.equal is
614
- // reference equality).
615
- const matches = file instanceof Map && attested instanceof Map
616
- ? file.size === attested.size && [...file].every(([platform, target]) => attested.get(platform) === target)
617
- : file === attested
618
- assert.ok(matches, `${what} resolution ${edge} mismatches the ${source}`)
606
+ // A per-platform edge is a Map<platform, target>, compared structurally.
607
+ assert.ok(importTargetsEqual(file, attested), `${what} resolution ${edge} mismatches the ${source}`)
608
+ }
609
+
610
+ // A frozen lockfile's and frozen bundle's checks of a `noun` ('File', or a 'Directory' listing) this
611
+ // run records, in the zone they attest (node_modules, or all in full scope): each attests it, as
612
+ // `module`'s `rel` and among the bundle's `bundled` (its sources or resources), and its `format`,
613
+ // where it has one. This closes the attested set; the bytes are checked by the caller's noupsert.
614
+ // `type` is usually not hash-attested, so a format flip would recategorize a hash-valid file
615
+ // (commonjs<->module) with no hash mismatch.
616
+ #assertFrozenAttests(noun, file, module, rel, bundled, format) {
617
+ if (this.config.frozen) assert.ok(Object.hasOwn(module.files, rel), `${noun} not attested by the frozen lockfile: ${file}`)
618
+ if (this.config.frozenBundle) assert.ok(bundled?.has(file), `${noun} not attested by the frozen bundle: ${file}`)
619
+ if (format == null) return
620
+ if (this.config.frozen && this.#lockFormats !== null) {
621
+ this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
622
+ }
623
+ if (this.config.frozenBundle && this.#bundleFormats !== null) {
624
+ this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
625
+ }
619
626
  }
620
627
 
621
628
  // Verify one file's loader format. Callers only pass files that must be attested, so an
@@ -798,10 +805,7 @@ export class State {
798
805
  // A package.json in a package vendored into this one (bundle-util's vendor dirs) names that
799
806
  // package: the file is this one's, listed as that package's in `vendored`.
800
807
  const vendorDir = vendorDirOf(name, nmRel)
801
- const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, (sub) => {
802
- const candidate = resolve(this.root, nmRoot, sub, 'package.json')
803
- return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
804
- })
808
+ const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, this.#manifestsBelow(nmRoot))
805
809
  vendored = vendor?.vendored
806
810
  if (closestPkgAbsolute !== pkgAbsolute && !vendor?.below && !isInconsistentPackageJsonException(this.relative(closestPkgAbsolute))) {
807
811
  const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
@@ -832,7 +836,6 @@ export class State {
832
836
  }
833
837
  const pkg = this.relative(pkgAbsolute)
834
838
  assert.ok(pkg === 'package.json' || pkg.endsWith('/package.json'))
835
- assert.equal(basename(pkg), 'package.json')
836
839
  const dir = dirname(pkg)
837
840
  if (nmRoot) assert.equal(dir, nmRoot)
838
841
  if (!this.modules.has(dir)) {
@@ -854,6 +857,14 @@ export class State {
854
857
  return { absolute, file, dir, module: this.modules.get(dir), closestType, vendored, subpackage }
855
858
  }
856
859
 
860
+ // The package.json in the directory `sub` of the package at `nmRoot`, parsed, or null where there is none.
861
+ #manifestsBelow(nmRoot) {
862
+ return (sub) => {
863
+ const candidate = resolve(this.root, nmRoot, sub, 'package.json')
864
+ return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
865
+ }
866
+ }
867
+
857
868
  // The subpackage (isSubpackage) of the package `name` at `nmRoot` that its file `nmRel` is in: the nearest
858
869
  // package.json above the file with a name, past `{"type":"module"}` markers (namedManifestOf), walked by
859
870
  // the file's own path, not the real one findPackageJSON resolves through a symlinked install -> { dir, name,
@@ -861,10 +872,7 @@ export class State {
861
872
  // names a subpath.
862
873
  #subpackageOf(nmRoot, nmRel, closest, name) {
863
874
  if (closest.name !== undefined && !(typeof closest.name === 'string' && closest.name.startsWith(`${name}/`))) return undefined
864
- const found = namedManifestOf(nmRel, (sub) => {
865
- const candidate = resolve(this.root, nmRoot, sub, 'package.json')
866
- return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
867
- })
875
+ const found = namedManifestOf(nmRel, this.#manifestsBelow(nmRoot))
868
876
  if (found === undefined || !isSubpackage(found.pkg.name, name, found.dir)) return undefined
869
877
  return { dir: found.dir, name: found.pkg.name, ...(isPackageString(found.pkg.version) ? { version: found.pkg.version } : {}) }
870
878
  }
@@ -970,25 +978,7 @@ export class State {
970
978
  assert.ok(!relativeEscapes(rel))
971
979
 
972
980
  const inAttestedZone = hasNodeModulesSegment(dir) || this.config.full
973
- if (this.config.frozen && inAttestedZone) {
974
- assert.ok(Object.hasOwn(module.files, rel), `File not attested by the frozen lockfile: ${file}`)
975
- }
976
-
977
- // Close the attested set (bytes are checked by the noupsert below); node_modules scope
978
- // deliberately doesn't attest workspace files.
979
- if (this.config.frozenBundle && inAttestedZone) {
980
- const attested = asResource ? this.#bundleResources : this.#bundleSources
981
- assert.ok(attested?.has(file), `File not attested by the frozen bundle: ${file}`)
982
- }
983
-
984
- // `type` is usually not hash-attested, so a flip recategorizes a hash-valid file
985
- // (commonjs<->module) with no hash mismatch.
986
- if (this.config.frozen && this.#lockFormats !== null && format != null && inAttestedZone) {
987
- this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
988
- }
989
- if (this.config.frozenBundle && this.#bundleFormats !== null && format != null && inAttestedZone) {
990
- this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
991
- }
981
+ if (inAttestedZone) this.#assertFrozenAttests('File', file, module, rel, asResource ? this.#bundleResources : this.#bundleSources, format)
992
982
 
993
983
  // Catch a format flip on already-attested bytes here; at write()'s #mergedFormats it would
994
984
  // surface only as a bare `Conflict`.
@@ -1064,15 +1054,16 @@ export class State {
1064
1054
  }
1065
1055
 
1066
1056
  // Attest the files stasis.config.json's `extra` names (expandExtra: paths, directories and globs,
1067
- // as `stasis add` takes them) beside what the run reached, each carried as an fs read is
1068
- // (#readFileOptions) unless the run captured it itself, whose record stands. The whole set is
1069
- // validated before a file is added, every offender named at once. `outputs` are the paths written,
1070
- // never taken in: by default the ones write() writes. `resources` is the allowlist, the config's by default.
1057
+ // as `stasis add` takes them, swept through the State's host) beside what the run reached, each
1058
+ // carried as an fs read is (#readFileOptions) unless the run captured it itself, whose record
1059
+ // stands. The whole set is validated before a file is added, every offender named at once.
1060
+ // `outputs` are the paths written, never taken in: by default the ones write() writes. `resources`
1061
+ // is the allowlist, the config's by default.
1071
1062
  includeExtra({ outputs = this.#writeTargets(), resources = this.config.resources } = {}) {
1072
1063
  if (this.config.extra.length === 0) return
1073
1064
  // Resolved as write() writes them: a relative one from the cwd.
1074
1065
  const written = new Set(outputs.map((file) => toPosix(relative(this.root, resolve(file)))))
1075
- const { files } = expandExtra(this.root, this.config.extra, written)
1066
+ const { files } = expandExtra(this.root, this.config.extra, written, this.#host)
1076
1067
  const realRoot = this.#host.realpath(this.root)
1077
1068
  const missing = []
1078
1069
  const undeclared = []
@@ -1088,7 +1079,7 @@ export class State {
1088
1079
  const absolute = resolve(this.root, rel)
1089
1080
  const url = pathToFileURL(absolute).toString()
1090
1081
  // A node_modules scope leaves the project's own files out, as it does the run's.
1091
- if (!this.config.full && !this.inNodeModules(url)) continue
1082
+ if (!inScope(this, url)) continue
1092
1083
  // The run's record of a file it captured stands, and `extra` is one more consumer of it.
1093
1084
  const file = this.#canonicalFile(url)
1094
1085
  if (this.#filesCapturedThisRun.has(file)) {
@@ -1130,19 +1121,7 @@ export class State {
1130
1121
  const rel = relative(dir, file)
1131
1122
  assert.ok(!relativeEscapes(rel))
1132
1123
 
1133
- const inAttestedZone = hasNodeModulesSegment(dir) || this.config.full
1134
- if (this.config.frozen && inAttestedZone) {
1135
- assert.ok(Object.hasOwn(module.files, rel), `Directory not attested by the frozen lockfile: ${file}`)
1136
- }
1137
- if (this.config.frozenBundle && inAttestedZone) {
1138
- assert.ok(this.#bundleResources?.has(file), `Directory not attested by the frozen bundle: ${file}`)
1139
- }
1140
- if (this.config.frozen && this.#lockFormats !== null && inAttestedZone) {
1141
- this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
1142
- }
1143
- if (this.config.frozenBundle && this.#bundleFormats !== null && inAttestedZone) {
1144
- this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
1145
- }
1124
+ if (hasNodeModulesSegment(dir) || this.config.full) this.#assertFrozenAttests('Directory', file, module, rel, this.#bundleResources, format)
1146
1125
 
1147
1126
  if (!Object.hasOwn(module.files, rel)) module.files[rel] = integrity
1148
1127
  assert.equal(module.files[rel], integrity)
@@ -1358,7 +1337,7 @@ export class State {
1358
1337
  this.#assertAttestedResolution(this.imports, key, parent, specifier, file, { what: 'observed', source: 'bundle', tolerateUnknown, targetPlatform })
1359
1338
  }
1360
1339
 
1361
- getImport(parentURL, specifier, { conditions = '*', importAttributes } = {}) {
1340
+ getImport(parentURL, specifier, { conditions = '*', importAttributes, platform } = {}) {
1362
1341
  if (conditions !== '*') assert.ok(Array.isArray(conditions))
1363
1342
  assert.ok(parentURL, 'getImport requires a parent') // matches addImport; #canonicalSpecifier needs it
1364
1343
  const parent = this.#canonicalFile(parentURL)
@@ -1379,12 +1358,17 @@ export class State {
1379
1358
  // code, which ERR_ASSERTION escapes.
1380
1359
  throw Object.assign(new Error(`Cannot find module '${specifier}' imported from ${parent}`), { code: 'ERR_MODULE_NOT_FOUND' })
1381
1360
  }
1382
- // A --metro per-platform edge is a { platform: file } Map; plain bundle=load has no platform
1383
- // context to pick one, so fail closed rather than feed a Map where a path is expected.
1361
+ // A --metro per-platform edge is a { platform: file } Map: `platform`'s target where the caller
1362
+ // builds for one (`stasis build --metro=ios`), which the edge must record. Plain bundle=load has
1363
+ // no platform context to pick one, so fail closed rather than feed a Map where a path is expected.
1384
1364
  if (typeof file !== 'string') {
1385
- throw Object.assign(
1386
- new Error(`Resolution of '${specifier}' from ${parent} is platform-specific (a --metro multi-platform bundle, which is Metro's to build); plain node has no platform to pick one by`),
1387
- { code: 'ERR_STASIS_PLATFORM_SPECIFIC' })
1365
+ if (!file.has(platform)) {
1366
+ const message = platform === undefined
1367
+ ? `Resolution of '${specifier}' from ${parent} is platform-specific (a --metro multi-platform bundle, which is Metro's to build); plain node has no platform to pick one by`
1368
+ : `Resolution of '${specifier}' from ${parent} is platform-specific and records no '${platform}' target (only ${[...file.keys()].join(', ')}): the bundle wasn't made for '${platform}'`
1369
+ throw Object.assign(new Error(message), { code: 'ERR_STASIS_PLATFORM_SPECIFIC' })
1370
+ }
1371
+ file = file.get(platform)
1388
1372
  }
1389
1373
  return { url: pathToFileURL(resolve(this.root, file)).toString(), format: this.#loaderFormat(file) }
1390
1374
  }
@@ -1565,17 +1549,13 @@ export class State {
1565
1549
  const claims = this.#reasonClaims()
1566
1550
 
1567
1551
  const reason = {}
1568
- let consumers = 0
1569
1552
  // Sort keys and file lists: the JSON must be byte-reproducible regardless of record order, else
1570
1553
  // #emitBundle's compare-and-skip sees spurious diffs.
1571
1554
  for (const who of [...claims.keys()].toSorted()) {
1572
1555
  const files = [...claims.get(who)].filter((file) => inBundle.has(file))
1573
- if (files.length > 0) {
1574
- reason[who] = files.toSorted(sortPaths)
1575
- consumers += 1
1576
- }
1556
+ if (files.length > 0) reason[who] = files.toSorted(sortPaths)
1577
1557
  }
1578
- return consumers > 1 ? reason : undefined
1558
+ return Object.keys(reason).length > 1 ? reason : undefined
1579
1559
  }
1580
1560
 
1581
1561
  // The formats a bundle declares: `this.formats`, minus a 'stat:*' the run's unified attestation
@@ -1687,7 +1667,7 @@ export class State {
1687
1667
  // (non-deterministic across installs, like the rest of NATIVE_ARTIFACT_EXTS).
1688
1668
  if (isNativeArtifact(file)) continue
1689
1669
  const url = pathToFileURL(resolve(this.root, file)).toString()
1690
- if (!this.config.full && !this.inNodeModules(url)) continue
1670
+ if (!inScope(this, url)) continue
1691
1671
  this.addFile(url, { reason: 'run' })
1692
1672
  }
1693
1673
  }
@@ -1703,7 +1683,7 @@ export class State {
1703
1683
  const file = this.#canonicalFileMaybe(resolvedURL)
1704
1684
  if (file === undefined) continue
1705
1685
  const bundled = this.sources.has(file) || this.resources.has(file)
1706
- if (!bundled && !this.config.full && !this.inNodeModules(resolvedURL)) continue
1686
+ if (!bundled && !inScope(this, resolvedURL)) continue
1707
1687
  const spec = this.#canonicalSpecifier(parentURL, specifier)
1708
1688
  const recorded = this.imports.values().some((byParent) => byParent.get(parent)?.get(spec) !== undefined)
1709
1689
  if (!recorded) this.addImport(parentURL, specifier, resolvedURL, { conditions: this.#requireConditions ?? '*' })
@@ -1779,26 +1759,24 @@ export class State {
1779
1759
  // AFTER the backfills and `extra`, so buckets they create are covered too.
1780
1760
  if (this.config.writeBundle && this.config.packageJSON) this.includePackageJson()
1781
1761
 
1782
- // Sidecars never write the lockfile (parent owns it); they only emit their bundle.
1783
- if (this.config.writeLockfile && !this.#parent) {
1784
- const lockText = this.lockData
1785
- if (lockText !== this.#lastLockData) {
1786
- const lockPath = this.config.lockFile || join(this.root, FILE_LOCK)
1787
- mkdirSync(dirname(lockPath), { recursive: true })
1788
- writeFileSync(lockPath, lockText)
1789
- this.#lastLockData = lockText
1790
- }
1791
- }
1792
- if (this.config.writeBundle) {
1793
- const sourcesPath = this.config.bundleFile || join(this.root, FILE_CODE)
1794
- if (this.config.resourcesBundleFile) {
1795
- // hasContent gates each half so an empty one is dropped under bundle=replace.
1796
- this.#lastCodeBundle = this.#emitBundle(sourcesPath, this.sources.size > 0, this.#lastCodeBundle, () => this.codeBundle.serialize())
1797
- this.#lastResourcesBundle = this.#emitBundle(this.config.resourcesBundleFile, this.resources.size > 0, this.#lastResourcesBundle, () => this.resourcesBundle.serialize())
1798
- } else {
1799
- this.#lastUnifiedBundle = this.#emitBundle(sourcesPath, this.sources.size > 0 || this.resources.size > 0, this.#lastUnifiedBundle, () => this.sourceData)
1800
- }
1762
+ // Every artifact is serialized before the first is written, so one that can't be (no bundle may carry
1763
+ // the lockfile) leaves the others as they were. Sidecars never write the lockfile (parent owns it).
1764
+ const lockText = this.config.writeLockfile && !this.#parent ? this.lockData : null
1765
+ const sourcesPath = this.config.bundleFile || join(this.root, FILE_CODE)
1766
+ // Each bundle written, [path, text]: the code bundle, with the resources too unless they are split
1767
+ // into resourcesBundleFile. hasContent gates each so an empty one is dropped under bundle=replace.
1768
+ const bundles = !this.config.writeBundle ? [] : this.config.resourcesBundleFile
1769
+ ? [[sourcesPath, this.#bundleText(this.sources.size > 0, () => this.codeBundle.serialize())],
1770
+ [this.config.resourcesBundleFile, this.#bundleText(this.resources.size > 0, () => this.resourcesBundle.serialize())]]
1771
+ : [[sourcesPath, this.#bundleText(this.sources.size > 0 || this.resources.size > 0, () => this.sourceData)]]
1772
+
1773
+ if (lockText !== null && lockText !== this.#lastLockData) {
1774
+ const lockPath = this.config.lockFile || join(this.root, FILE_LOCK)
1775
+ mkdirSync(dirname(lockPath), { recursive: true })
1776
+ writeFileSync(lockPath, lockText)
1777
+ this.#lastLockData = lockText
1801
1778
  }
1779
+ for (const [path, text] of bundles) this.#emitBundle(path, text)
1802
1780
  }
1803
1781
 
1804
1782
  // Snapshot of what THIS process observed, for a forked child to hand back to the root. Content is
@@ -1836,22 +1814,13 @@ export class State {
1836
1814
  // malformed/foreign shard -- refuse.
1837
1815
  assert.equal(shard.scope, this.config.scope, `shard scope "${shard.scope}" != root scope "${this.config.scope}"`)
1838
1816
  // An in-root shard KEY may resolve (through a symlink) OUTSIDE the root on THIS disk, so every
1839
- // replay re-checks real-path containment -- the same boundary fs.js enforces on live --fs reads.
1840
- let realRoot
1841
- const realContained = (absolute) => {
1842
- if (realRoot === undefined) {
1843
- try { realRoot = realpathSync(this.root) } catch { realRoot = this.root }
1844
- }
1845
- let real
1846
- try { real = realpathSync(absolute) } catch { return false }
1847
- return isPathWithin(realRoot, real)
1848
- }
1817
+ // replay re-checks real-path containment (realContained), the boundary fs.js enforces on live --fs reads.
1849
1818
  // Files first, then edges. Merged files are NEVER marked entries -- a child's "entry" is its
1850
1819
  // fork-target main, not a root entry.
1851
1820
  for (const file of shard.files) {
1852
1821
  const absolute = resolve(this.root, file)
1853
1822
  // Skip a key whose on-disk path escapes the root through a symlink.
1854
- if (!realContained(absolute)) continue
1823
+ if (!realContained(this.root, absolute)) continue
1855
1824
  // ABOVE the already-attested fast path below: a child that re-read a file and found no bit
1856
1825
  // refutes it by OMISSION, so every file the shard RECORDS needs an observation. Re-derived
1857
1826
  // from THIS process's disk, so the shard cannot inject a forged bit.
@@ -1896,7 +1865,7 @@ export class State {
1896
1865
  const kind = stats.isDirectory() ? 'directory' : stats.isFile() ? 'file' : null
1897
1866
  // statSync follows links: a symlink pointing OUT must not attest an external kind under an
1898
1867
  // in-root key.
1899
- if (kind === null || !realContained(absolute)) continue
1868
+ if (kind === null || !realContained(this.root, absolute)) continue
1900
1869
  this.addFsStat(pathToFileURL(absolute).toString(), kind)
1901
1870
  } catch {
1902
1871
  // Gone/dangling, or a kind conflict with the root's capture: skip.
@@ -1924,19 +1893,21 @@ export class State {
1924
1893
  }
1925
1894
  }
1926
1895
 
1927
- // Emit one bundle artifact, returning its new "last serialized" cache. Empty under bundle=replace
1928
- // DELETES the file (a replace run is authoritative); unchanged text skips brotli + writeFileSync.
1929
- #emitBundle(path, hasContent, lastText, serialize) {
1930
- if (!hasContent && this.config.replaceBundle) {
1896
+ // What a bundle write leaves at its path: the serialized bundle, or null to drop an empty one under bundle=replace.
1897
+ #bundleText(hasContent, serialize) {
1898
+ return !hasContent && this.config.replaceBundle ? null : serialize()
1899
+ }
1900
+
1901
+ // Emit one bundle artifact. Null (empty under bundle=replace) DELETES the file (a replace run is
1902
+ // authoritative); text unchanged since this State last wrote it skips brotli + writeFileSync.
1903
+ #emitBundle(path, text) {
1904
+ if (text === null) {
1931
1905
  rmSync(path, { force: true })
1932
- return null
1933
- }
1934
- const text = serialize()
1935
- if (text !== lastText) {
1906
+ } else if (text !== this.#lastBundleText.get(path)) {
1936
1907
  mkdirSync(dirname(path), { recursive: true })
1937
- writeFileSync(path, brotliCompressSync(text, brotliOptions(this.config.brotliQuality)))
1908
+ writeFileSync(path, compressBundleText(text, this.config.brotliQuality))
1938
1909
  }
1939
- return text
1910
+ this.#lastBundleText.set(path, text)
1940
1911
  }
1941
1912
 
1942
1913
  get parent() {
@@ -2012,7 +1983,7 @@ export class State {
2012
1983
  if (typeof file !== 'string' || missing.has(file)) continue
2013
1984
  if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) continue
2014
1985
  const url = pathToFileURL(resolve(this.root, file)).toString()
2015
- if (!this.config.full && !this.inNodeModules(url)) continue
1986
+ if (!inScope(this, url)) continue
2016
1987
  missing.add(file)
2017
1988
  }
2018
1989
  }