@exodus/stasis-core 1.0.0-beta.12 → 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/bin/stasis-core.js +9 -98
- package/package.json +4 -2
- package/src/add.js +68 -70
- package/src/artifact-util.js +48 -21
- package/src/brotli.js +23 -3
- package/src/bundle-util.js +65 -10
- package/src/bundle.js +14 -2
- package/src/cli.js +153 -0
- package/src/config.js +14 -4
- package/src/extract.js +148 -41
- package/src/fs.js +1 -19
- package/src/hooks.js +4 -3
- package/src/host.js +12 -1
- package/src/lockfile.js +15 -1
- package/src/state-util.js +26 -0
- package/src/state.js +155 -146
- package/src/util.js +48 -70
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 {
|
|
14
|
-
import { CODE_EXTENSIONS, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, 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, 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,
|
|
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 (
|
|
42
|
+
if (!isPlainObject(json)) throw invalidPackageConfig(pkgAbsolute)
|
|
48
43
|
return json
|
|
49
44
|
}
|
|
50
45
|
|
|
@@ -54,6 +49,9 @@ function upsertFormat(map, file, format) {
|
|
|
54
49
|
map.set(file, currentFormat === undefined ? format : reconcileFormat(format, currentFormat, file))
|
|
55
50
|
}
|
|
56
51
|
|
|
52
|
+
// A bundled file's integrity as addFile/addFsDir hash it: a 'resource:base64' payload's is its decoded bytes'.
|
|
53
|
+
const bundledIntegrity = (content, format) => sha512integrity(format === 'resource:base64' ? Buffer.from(content, 'base64') : content)
|
|
54
|
+
|
|
57
55
|
// Loader formats the extension alone decides; `.js`/`.ts` follow the nearest package `type`.
|
|
58
56
|
const EXT_FORMATS = { __proto__: null, '.json': 'json', '.mjs': 'module', '.cjs': 'commonjs', '.mts': 'module-typescript', '.cts': 'commonjs-typescript' }
|
|
59
57
|
// With no `type`, Node decides .js/.ts by syntax detection: either variant is possible, the first is the default.
|
|
@@ -170,11 +168,9 @@ export class State {
|
|
|
170
168
|
// statically parses files under it to recover edges the resolve hook never observed.
|
|
171
169
|
#preloadRoot = null
|
|
172
170
|
|
|
173
|
-
// 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.
|
|
174
172
|
#lastLockData = null
|
|
175
|
-
#
|
|
176
|
-
#lastCodeBundle = null
|
|
177
|
-
#lastResourcesBundle = null
|
|
173
|
+
#lastBundleText = new Map()
|
|
178
174
|
// Lazy `{ value: detectRepo(this.root, this.#host) }`.
|
|
179
175
|
#detectedRepoCache
|
|
180
176
|
// Half ('code' | 'resources') -> `repo` of the bundle absorbed there for adding to.
|
|
@@ -226,7 +222,7 @@ export class State {
|
|
|
226
222
|
const sourcesPath = this.config.bundleFile
|
|
227
223
|
const sources = this.#readMaybe(sourcesPath)
|
|
228
224
|
if (sources && !this.config.replaceBundle) {
|
|
229
|
-
const bundle =
|
|
225
|
+
const bundle = parseBundleBytes(sources)
|
|
230
226
|
// v0 bundle has no per-file formats or import map -- serving one via a sidecar widens trust.
|
|
231
227
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
232
228
|
`stasis sidecar requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
|
|
@@ -416,7 +412,7 @@ export class State {
|
|
|
416
412
|
}
|
|
417
413
|
|
|
418
414
|
#loadCodeBundle(sources, sourcesPath, lockfileLoaded) {
|
|
419
|
-
const bundle =
|
|
415
|
+
const bundle = parseBundleBytes(sources)
|
|
420
416
|
// Bundle.parse accepts v0 for offline tooling; the runtime refuses it -- v0 has no per-file
|
|
421
417
|
// formats or import map, so serving one widens the trust boundary.
|
|
422
418
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
@@ -525,6 +521,18 @@ export class State {
|
|
|
525
521
|
const withMetadata = withMetadataOf(existing, info)
|
|
526
522
|
if (withMetadata !== existing) this.modules.set(dir, withMetadata)
|
|
527
523
|
}
|
|
524
|
+
// A bundle=add run writes these buckets back, and #bundleModules carries only the files a
|
|
525
|
+
// bucket lists: seed them as #absorbLockfile does, else a bundled file this run doesn't
|
|
526
|
+
// load again drops out of the rewritten bundle.
|
|
527
|
+
if (!this.config.writeBundle) continue
|
|
528
|
+
const { files } = this.modules.get(dir)
|
|
529
|
+
for (const [rel, content] of Object.entries(info.files)) {
|
|
530
|
+
const file = moduleFileKey(dir, rel)
|
|
531
|
+
const integrity = bundledIntegrity(content, bundle.formats.get(file))
|
|
532
|
+
noupsert(this.hashes, file, integrity)
|
|
533
|
+
if (!Object.hasOwn(files, rel)) files[rel] = integrity
|
|
534
|
+
assert.equal(files[rel], integrity, `bundle file ${file} conflicts with its recorded hash`)
|
|
535
|
+
}
|
|
528
536
|
}
|
|
529
537
|
}
|
|
530
538
|
}
|
|
@@ -532,7 +540,7 @@ export class State {
|
|
|
532
540
|
// Load the resources half of a split-bundle layout: ONLY resource-format files, unioned into the
|
|
533
541
|
// bundleFile state.
|
|
534
542
|
#absorbResourcesBundle(resourcesData, { lockfileLoaded, resourcesPath }) {
|
|
535
|
-
const bundle =
|
|
543
|
+
const bundle = parseBundleBytes(resourcesData)
|
|
536
544
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
537
545
|
`stasis run requires a v1 resources bundle; ${resourcesPath} is v${bundle.version}. ` +
|
|
538
546
|
`Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point to upgrade.`)
|
|
@@ -595,12 +603,26 @@ export class State {
|
|
|
595
603
|
attested = attested.get(targetPlatform)
|
|
596
604
|
assert.ok(attested !== undefined, `${what} resolution ${edge} is not attested for platform '${targetPlatform}' by the ${source}`)
|
|
597
605
|
}
|
|
598
|
-
// A per-platform edge is a Map<platform, target
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
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
|
+
}
|
|
604
626
|
}
|
|
605
627
|
|
|
606
628
|
// Verify one file's loader format. Callers only pass files that must be attested, so an
|
|
@@ -783,10 +805,7 @@ export class State {
|
|
|
783
805
|
// A package.json in a package vendored into this one (bundle-util's vendor dirs) names that
|
|
784
806
|
// package: the file is this one's, listed as that package's in `vendored`.
|
|
785
807
|
const vendorDir = vendorDirOf(name, nmRel)
|
|
786
|
-
const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, (
|
|
787
|
-
const candidate = resolve(this.root, nmRoot, sub, 'package.json')
|
|
788
|
-
return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
|
|
789
|
-
})
|
|
808
|
+
const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, this.#manifestsBelow(nmRoot))
|
|
790
809
|
vendored = vendor?.vendored
|
|
791
810
|
if (closestPkgAbsolute !== pkgAbsolute && !vendor?.below && !isInconsistentPackageJsonException(this.relative(closestPkgAbsolute))) {
|
|
792
811
|
const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
|
|
@@ -817,7 +836,6 @@ export class State {
|
|
|
817
836
|
}
|
|
818
837
|
const pkg = this.relative(pkgAbsolute)
|
|
819
838
|
assert.ok(pkg === 'package.json' || pkg.endsWith('/package.json'))
|
|
820
|
-
assert.equal(basename(pkg), 'package.json')
|
|
821
839
|
const dir = dirname(pkg)
|
|
822
840
|
if (nmRoot) assert.equal(dir, nmRoot)
|
|
823
841
|
if (!this.modules.has(dir)) {
|
|
@@ -839,6 +857,14 @@ export class State {
|
|
|
839
857
|
return { absolute, file, dir, module: this.modules.get(dir), closestType, vendored, subpackage }
|
|
840
858
|
}
|
|
841
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
|
+
|
|
842
868
|
// The subpackage (isSubpackage) of the package `name` at `nmRoot` that its file `nmRel` is in: the nearest
|
|
843
869
|
// package.json above the file with a name, past `{"type":"module"}` markers (namedManifestOf), walked by
|
|
844
870
|
// the file's own path, not the real one findPackageJSON resolves through a symlinked install -> { dir, name,
|
|
@@ -846,10 +872,7 @@ export class State {
|
|
|
846
872
|
// names a subpath.
|
|
847
873
|
#subpackageOf(nmRoot, nmRel, closest, name) {
|
|
848
874
|
if (closest.name !== undefined && !(typeof closest.name === 'string' && closest.name.startsWith(`${name}/`))) return undefined
|
|
849
|
-
const found = namedManifestOf(nmRel, (
|
|
850
|
-
const candidate = resolve(this.root, nmRoot, sub, 'package.json')
|
|
851
|
-
return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
|
|
852
|
-
})
|
|
875
|
+
const found = namedManifestOf(nmRel, this.#manifestsBelow(nmRoot))
|
|
853
876
|
if (found === undefined || !isSubpackage(found.pkg.name, name, found.dir)) return undefined
|
|
854
877
|
return { dir: found.dir, name: found.pkg.name, ...(isPackageString(found.pkg.version) ? { version: found.pkg.version } : {}) }
|
|
855
878
|
}
|
|
@@ -955,25 +978,7 @@ export class State {
|
|
|
955
978
|
assert.ok(!relativeEscapes(rel))
|
|
956
979
|
|
|
957
980
|
const inAttestedZone = hasNodeModulesSegment(dir) || this.config.full
|
|
958
|
-
if (this
|
|
959
|
-
assert.ok(Object.hasOwn(module.files, rel), `File not attested by the frozen lockfile: ${file}`)
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
// Close the attested set (bytes are checked by the noupsert below); node_modules scope
|
|
963
|
-
// deliberately doesn't attest workspace files.
|
|
964
|
-
if (this.config.frozenBundle && inAttestedZone) {
|
|
965
|
-
const attested = asResource ? this.#bundleResources : this.#bundleSources
|
|
966
|
-
assert.ok(attested?.has(file), `File not attested by the frozen bundle: ${file}`)
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
// `type` is usually not hash-attested, so a flip recategorizes a hash-valid file
|
|
970
|
-
// (commonjs<->module) with no hash mismatch.
|
|
971
|
-
if (this.config.frozen && this.#lockFormats !== null && format != null && inAttestedZone) {
|
|
972
|
-
this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
|
|
973
|
-
}
|
|
974
|
-
if (this.config.frozenBundle && this.#bundleFormats !== null && format != null && inAttestedZone) {
|
|
975
|
-
this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
|
|
976
|
-
}
|
|
981
|
+
if (inAttestedZone) this.#assertFrozenAttests('File', file, module, rel, asResource ? this.#bundleResources : this.#bundleSources, format)
|
|
977
982
|
|
|
978
983
|
// Catch a format flip on already-attested bytes here; at write()'s #mergedFormats it would
|
|
979
984
|
// surface only as a bare `Conflict`.
|
|
@@ -1049,15 +1054,16 @@ export class State {
|
|
|
1049
1054
|
}
|
|
1050
1055
|
|
|
1051
1056
|
// Attest the files stasis.config.json's `extra` names (expandExtra: paths, directories and globs,
|
|
1052
|
-
// as `stasis add` takes them) beside what the run reached, each
|
|
1053
|
-
// (#readFileOptions) unless the run captured it itself, whose record
|
|
1054
|
-
// validated before a file is added, every offender named at once.
|
|
1055
|
-
// never taken in: by default the ones write() writes. `resources`
|
|
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.
|
|
1056
1062
|
includeExtra({ outputs = this.#writeTargets(), resources = this.config.resources } = {}) {
|
|
1057
1063
|
if (this.config.extra.length === 0) return
|
|
1058
1064
|
// Resolved as write() writes them: a relative one from the cwd.
|
|
1059
1065
|
const written = new Set(outputs.map((file) => toPosix(relative(this.root, resolve(file)))))
|
|
1060
|
-
const { files } = expandExtra(this.root, this.config.extra, written)
|
|
1066
|
+
const { files } = expandExtra(this.root, this.config.extra, written, this.#host)
|
|
1061
1067
|
const realRoot = this.#host.realpath(this.root)
|
|
1062
1068
|
const missing = []
|
|
1063
1069
|
const undeclared = []
|
|
@@ -1073,7 +1079,7 @@ export class State {
|
|
|
1073
1079
|
const absolute = resolve(this.root, rel)
|
|
1074
1080
|
const url = pathToFileURL(absolute).toString()
|
|
1075
1081
|
// A node_modules scope leaves the project's own files out, as it does the run's.
|
|
1076
|
-
if (!this
|
|
1082
|
+
if (!inScope(this, url)) continue
|
|
1077
1083
|
// The run's record of a file it captured stands, and `extra` is one more consumer of it.
|
|
1078
1084
|
const file = this.#canonicalFile(url)
|
|
1079
1085
|
if (this.#filesCapturedThisRun.has(file)) {
|
|
@@ -1115,19 +1121,7 @@ export class State {
|
|
|
1115
1121
|
const rel = relative(dir, file)
|
|
1116
1122
|
assert.ok(!relativeEscapes(rel))
|
|
1117
1123
|
|
|
1118
|
-
|
|
1119
|
-
if (this.config.frozen && inAttestedZone) {
|
|
1120
|
-
assert.ok(Object.hasOwn(module.files, rel), `Directory not attested by the frozen lockfile: ${file}`)
|
|
1121
|
-
}
|
|
1122
|
-
if (this.config.frozenBundle && inAttestedZone) {
|
|
1123
|
-
assert.ok(this.#bundleResources?.has(file), `Directory not attested by the frozen bundle: ${file}`)
|
|
1124
|
-
}
|
|
1125
|
-
if (this.config.frozen && this.#lockFormats !== null && inAttestedZone) {
|
|
1126
|
-
this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
|
|
1127
|
-
}
|
|
1128
|
-
if (this.config.frozenBundle && this.#bundleFormats !== null && inAttestedZone) {
|
|
1129
|
-
this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
|
|
1130
|
-
}
|
|
1124
|
+
if (hasNodeModulesSegment(dir) || this.config.full) this.#assertFrozenAttests('Directory', file, module, rel, this.#bundleResources, format)
|
|
1131
1125
|
|
|
1132
1126
|
if (!Object.hasOwn(module.files, rel)) module.files[rel] = integrity
|
|
1133
1127
|
assert.equal(module.files[rel], integrity)
|
|
@@ -1343,7 +1337,7 @@ export class State {
|
|
|
1343
1337
|
this.#assertAttestedResolution(this.imports, key, parent, specifier, file, { what: 'observed', source: 'bundle', tolerateUnknown, targetPlatform })
|
|
1344
1338
|
}
|
|
1345
1339
|
|
|
1346
|
-
getImport(parentURL, specifier, { conditions = '*', importAttributes } = {}) {
|
|
1340
|
+
getImport(parentURL, specifier, { conditions = '*', importAttributes, platform } = {}) {
|
|
1347
1341
|
if (conditions !== '*') assert.ok(Array.isArray(conditions))
|
|
1348
1342
|
assert.ok(parentURL, 'getImport requires a parent') // matches addImport; #canonicalSpecifier needs it
|
|
1349
1343
|
const parent = this.#canonicalFile(parentURL)
|
|
@@ -1364,12 +1358,17 @@ export class State {
|
|
|
1364
1358
|
// code, which ERR_ASSERTION escapes.
|
|
1365
1359
|
throw Object.assign(new Error(`Cannot find module '${specifier}' imported from ${parent}`), { code: 'ERR_MODULE_NOT_FOUND' })
|
|
1366
1360
|
}
|
|
1367
|
-
// A --metro per-platform edge is a { platform: file } Map
|
|
1368
|
-
//
|
|
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.
|
|
1369
1364
|
if (typeof file !== 'string') {
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
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)
|
|
1373
1372
|
}
|
|
1374
1373
|
return { url: pathToFileURL(resolve(this.root, file)).toString(), format: this.#loaderFormat(file) }
|
|
1375
1374
|
}
|
|
@@ -1550,17 +1549,13 @@ export class State {
|
|
|
1550
1549
|
const claims = this.#reasonClaims()
|
|
1551
1550
|
|
|
1552
1551
|
const reason = {}
|
|
1553
|
-
let consumers = 0
|
|
1554
1552
|
// Sort keys and file lists: the JSON must be byte-reproducible regardless of record order, else
|
|
1555
1553
|
// #emitBundle's compare-and-skip sees spurious diffs.
|
|
1556
1554
|
for (const who of [...claims.keys()].toSorted()) {
|
|
1557
1555
|
const files = [...claims.get(who)].filter((file) => inBundle.has(file))
|
|
1558
|
-
if (files.length > 0)
|
|
1559
|
-
reason[who] = files.toSorted(sortPaths)
|
|
1560
|
-
consumers += 1
|
|
1561
|
-
}
|
|
1556
|
+
if (files.length > 0) reason[who] = files.toSorted(sortPaths)
|
|
1562
1557
|
}
|
|
1563
|
-
return
|
|
1558
|
+
return Object.keys(reason).length > 1 ? reason : undefined
|
|
1564
1559
|
}
|
|
1565
1560
|
|
|
1566
1561
|
// The formats a bundle declares: `this.formats`, minus a 'stat:*' the run's unified attestation
|
|
@@ -1672,7 +1667,7 @@ export class State {
|
|
|
1672
1667
|
// (non-deterministic across installs, like the rest of NATIVE_ARTIFACT_EXTS).
|
|
1673
1668
|
if (isNativeArtifact(file)) continue
|
|
1674
1669
|
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
1675
|
-
if (!this
|
|
1670
|
+
if (!inScope(this, url)) continue
|
|
1676
1671
|
this.addFile(url, { reason: 'run' })
|
|
1677
1672
|
}
|
|
1678
1673
|
}
|
|
@@ -1688,7 +1683,7 @@ export class State {
|
|
|
1688
1683
|
const file = this.#canonicalFileMaybe(resolvedURL)
|
|
1689
1684
|
if (file === undefined) continue
|
|
1690
1685
|
const bundled = this.sources.has(file) || this.resources.has(file)
|
|
1691
|
-
if (!bundled && !this
|
|
1686
|
+
if (!bundled && !inScope(this, resolvedURL)) continue
|
|
1692
1687
|
const spec = this.#canonicalSpecifier(parentURL, specifier)
|
|
1693
1688
|
const recorded = this.imports.values().some((byParent) => byParent.get(parent)?.get(spec) !== undefined)
|
|
1694
1689
|
if (!recorded) this.addImport(parentURL, specifier, resolvedURL, { conditions: this.#requireConditions ?? '*' })
|
|
@@ -1697,32 +1692,55 @@ export class State {
|
|
|
1697
1692
|
}
|
|
1698
1693
|
|
|
1699
1694
|
// Add every bundled module's `<dir>/package.json` (config.packageJSON) even if the run never
|
|
1700
|
-
// reached it, so a load/prune can read each dependency's manifest
|
|
1701
|
-
//
|
|
1702
|
-
//
|
|
1695
|
+
// reached it, so a load/prune can read each dependency's manifest, and the nearer one a bundled
|
|
1696
|
+
// file of it has (scopeManifestOf: a `{"type":"module"}` marker such as `esm/package.json`), which
|
|
1697
|
+
// Node and Metro's resolver read for the file. The skip keys on bundle membership, NOT this.hashes
|
|
1698
|
+
// (which also holds lockfile-absorbed entries and is shared by reference across sidecars).
|
|
1703
1699
|
includePackageJson() {
|
|
1704
1700
|
// Gather first: addFile mutates this.modules, so don't addFile while iterating it.
|
|
1705
1701
|
const realRoot = this.#host.realpath(this.root)
|
|
1706
1702
|
const claims = this.#reasonClaims()
|
|
1703
|
+
const bundled = (rel) => this.sources.has(rel) || this.resources.has(rel)
|
|
1704
|
+
const found = new Map()
|
|
1705
|
+
const exists = (rel) => found.get(rel) ?? found.set(rel, packageJSONStat(this.#host, resolve(this.root, rel))?.isFile() ?? false).get(rel)
|
|
1706
|
+
// A manifest's bytes and parsed fields, or null where it's gone or malformed (skipped).
|
|
1707
|
+
const read = (rel) => {
|
|
1708
|
+
const buf = readModuleManifest({ baseDir: this.root, realBase: realRoot, rel, host: this.#host })
|
|
1709
|
+
if (!buf) return null
|
|
1710
|
+
try {
|
|
1711
|
+
const pkg = JSON.parse(packageJSONText(buf))
|
|
1712
|
+
return isPlainObject(pkg) ? { buf, pkg } : null
|
|
1713
|
+
} catch {
|
|
1714
|
+
return null
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1707
1717
|
const toAdd = []
|
|
1708
1718
|
for (const [dir, module] of this.modules) {
|
|
1709
|
-
const
|
|
1710
|
-
|
|
1711
|
-
const buf = readModuleManifest({ baseDir: this.root, realBase: realRoot, rel, host: this.#host })
|
|
1712
|
-
if (!buf) continue
|
|
1713
|
-
// Only carry a manifest whose on-disk identity still matches the bundled bucket: under
|
|
1719
|
+
const own = moduleFileKey(dir, 'package.json')
|
|
1720
|
+
// Only carry the manifests of a bucket whose on-disk identity still matches: under
|
|
1714
1721
|
// bundle=add a dep may have drifted, and addFile -> #locateModule would crash on it.
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
//
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1722
|
+
const manifest = read(own)
|
|
1723
|
+
if (manifest === null || manifest.pkg.name !== module.name || manifest.pkg.version !== module.version) continue
|
|
1724
|
+
// Each manifest -> the files it is the package.json of: the bucket's own, of every file; a
|
|
1725
|
+
// nearer one, of the bundled files below it.
|
|
1726
|
+
const files = Object.keys(module.files).map((f) => moduleFileKey(dir, f))
|
|
1727
|
+
const scopes = new Map([[own, files]])
|
|
1728
|
+
for (const file of files) {
|
|
1729
|
+
const nearest = bundled(file) ? scopeManifestOf(file, dir, exists) : undefined
|
|
1730
|
+
if (nearest !== undefined) (scopes.get(nearest) ?? scopes.set(nearest, []).get(nearest)).push(file)
|
|
1731
|
+
}
|
|
1732
|
+
for (const [rel, of] of scopes) {
|
|
1733
|
+
if (bundled(rel)) continue // already in this bundle
|
|
1734
|
+
const buf = rel === own ? manifest.buf : read(rel)?.buf
|
|
1735
|
+
if (buf === undefined) continue
|
|
1736
|
+
// The manifest rides along BECAUSE some consumer bundled its files, so it inherits every
|
|
1737
|
+
// consumer that claims one (#reasonClaims: not run for a file it merely fs-read for a
|
|
1738
|
+
// plugin) -- nothing observed the manifest itself, but an unattributed file can't be
|
|
1739
|
+
// sorted by provenance downstream. Derived BEFORE the adds below, so one fold's manifest
|
|
1740
|
+
// can't count as another bucket's consumer evidence.
|
|
1741
|
+
const consumers = [...claims].filter(([, claimed]) => of.some((file) => claimed.has(file))).map(([who]) => who)
|
|
1742
|
+
toAdd.push({ rel, buf, consumers })
|
|
1743
|
+
}
|
|
1726
1744
|
}
|
|
1727
1745
|
for (const { rel, buf, consumers } of toAdd) {
|
|
1728
1746
|
this.addFile(pathToFileURL(resolve(this.root, rel)).toString(), { source: buf, reason: null })
|
|
@@ -1741,26 +1759,24 @@ export class State {
|
|
|
1741
1759
|
// AFTER the backfills and `extra`, so buckets they create are covered too.
|
|
1742
1760
|
if (this.config.writeBundle && this.config.packageJSON) this.includePackageJson()
|
|
1743
1761
|
|
|
1744
|
-
//
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
} else {
|
|
1761
|
-
this.#lastUnifiedBundle = this.#emitBundle(sourcesPath, this.sources.size > 0 || this.resources.size > 0, this.#lastUnifiedBundle, () => this.sourceData)
|
|
1762
|
-
}
|
|
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
|
|
1763
1778
|
}
|
|
1779
|
+
for (const [path, text] of bundles) this.#emitBundle(path, text)
|
|
1764
1780
|
}
|
|
1765
1781
|
|
|
1766
1782
|
// Snapshot of what THIS process observed, for a forked child to hand back to the root. Content is
|
|
@@ -1798,22 +1814,13 @@ export class State {
|
|
|
1798
1814
|
// malformed/foreign shard -- refuse.
|
|
1799
1815
|
assert.equal(shard.scope, this.config.scope, `shard scope "${shard.scope}" != root scope "${this.config.scope}"`)
|
|
1800
1816
|
// An in-root shard KEY may resolve (through a symlink) OUTSIDE the root on THIS disk, so every
|
|
1801
|
-
// replay re-checks real-path containment
|
|
1802
|
-
let realRoot
|
|
1803
|
-
const realContained = (absolute) => {
|
|
1804
|
-
if (realRoot === undefined) {
|
|
1805
|
-
try { realRoot = realpathSync(this.root) } catch { realRoot = this.root }
|
|
1806
|
-
}
|
|
1807
|
-
let real
|
|
1808
|
-
try { real = realpathSync(absolute) } catch { return false }
|
|
1809
|
-
return isPathWithin(realRoot, real)
|
|
1810
|
-
}
|
|
1817
|
+
// replay re-checks real-path containment (realContained), the boundary fs.js enforces on live --fs reads.
|
|
1811
1818
|
// Files first, then edges. Merged files are NEVER marked entries -- a child's "entry" is its
|
|
1812
1819
|
// fork-target main, not a root entry.
|
|
1813
1820
|
for (const file of shard.files) {
|
|
1814
1821
|
const absolute = resolve(this.root, file)
|
|
1815
1822
|
// Skip a key whose on-disk path escapes the root through a symlink.
|
|
1816
|
-
if (!realContained(absolute)) continue
|
|
1823
|
+
if (!realContained(this.root, absolute)) continue
|
|
1817
1824
|
// ABOVE the already-attested fast path below: a child that re-read a file and found no bit
|
|
1818
1825
|
// refutes it by OMISSION, so every file the shard RECORDS needs an observation. Re-derived
|
|
1819
1826
|
// from THIS process's disk, so the shard cannot inject a forged bit.
|
|
@@ -1858,7 +1865,7 @@ export class State {
|
|
|
1858
1865
|
const kind = stats.isDirectory() ? 'directory' : stats.isFile() ? 'file' : null
|
|
1859
1866
|
// statSync follows links: a symlink pointing OUT must not attest an external kind under an
|
|
1860
1867
|
// in-root key.
|
|
1861
|
-
if (kind === null || !realContained(absolute)) continue
|
|
1868
|
+
if (kind === null || !realContained(this.root, absolute)) continue
|
|
1862
1869
|
this.addFsStat(pathToFileURL(absolute).toString(), kind)
|
|
1863
1870
|
} catch {
|
|
1864
1871
|
// Gone/dangling, or a kind conflict with the root's capture: skip.
|
|
@@ -1886,19 +1893,21 @@ export class State {
|
|
|
1886
1893
|
}
|
|
1887
1894
|
}
|
|
1888
1895
|
|
|
1889
|
-
//
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
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) {
|
|
1893
1905
|
rmSync(path, { force: true })
|
|
1894
|
-
|
|
1895
|
-
}
|
|
1896
|
-
const text = serialize()
|
|
1897
|
-
if (text !== lastText) {
|
|
1906
|
+
} else if (text !== this.#lastBundleText.get(path)) {
|
|
1898
1907
|
mkdirSync(dirname(path), { recursive: true })
|
|
1899
|
-
writeFileSync(path,
|
|
1908
|
+
writeFileSync(path, compressBundleText(text, this.config.brotliQuality))
|
|
1900
1909
|
}
|
|
1901
|
-
|
|
1910
|
+
this.#lastBundleText.set(path, text)
|
|
1902
1911
|
}
|
|
1903
1912
|
|
|
1904
1913
|
get parent() {
|
|
@@ -1974,7 +1983,7 @@ export class State {
|
|
|
1974
1983
|
if (typeof file !== 'string' || missing.has(file)) continue
|
|
1975
1984
|
if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) continue
|
|
1976
1985
|
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
1977
|
-
if (!this
|
|
1986
|
+
if (!inScope(this, url)) continue
|
|
1978
1987
|
missing.add(file)
|
|
1979
1988
|
}
|
|
1980
1989
|
}
|