@exodus/stasis-core 1.0.0-beta.13 → 1.0.0-beta.15
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 +75 -74
- package/src/artifact-util.js +48 -21
- package/src/brotli.js +23 -3
- package/src/bundle-util.js +39 -7
- package/src/bundle.js +14 -2
- package/src/cli.js +153 -0
- package/src/config.js +12 -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 +44 -0
- package/src/state.js +99 -130
- package/src/util.js +53 -70
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
|
|
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
|
|
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
|
|
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,11 @@ 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 { Lockfile } from './lockfile.js'
|
|
9
|
+
import { isPathWithin } from './util.js'
|
|
7
10
|
|
|
8
11
|
// Genuine fs readers snapshotted off the namespace before any --fs patch. A plain `import { readFileSync }`
|
|
9
12
|
// won't do: --fs monkey-patches fs then syncBuiltinESMExports() rebinds named imports, so snapshotting dodges that.
|
|
@@ -11,11 +14,43 @@ export const realReadFileSync = fs.readFileSync
|
|
|
11
14
|
export const realReadFile = fs.promises.readFile
|
|
12
15
|
export const realReaddirSync = fs.readdirSync
|
|
13
16
|
export const realExistsSync = fs.existsSync
|
|
17
|
+
const realRealpathSync = fs.realpathSync
|
|
18
|
+
|
|
19
|
+
let realRootCache
|
|
20
|
+
// Capture-side containment: the REAL location of `abs` must stay inside `root`'s real path (`root`
|
|
21
|
+
// itself where that has none), else an in-tree symlink pointing OUT would pull external content in.
|
|
22
|
+
export function realContained(root, abs) {
|
|
23
|
+
if (realRootCache?.root !== root) {
|
|
24
|
+
let real
|
|
25
|
+
try { real = realRealpathSync(root) } catch { real = root }
|
|
26
|
+
realRootCache = { root, real }
|
|
27
|
+
}
|
|
28
|
+
let real
|
|
29
|
+
try { real = realRealpathSync(abs) } catch { return false }
|
|
30
|
+
return isPathWithin(realRootCache.real, real)
|
|
31
|
+
}
|
|
14
32
|
|
|
15
33
|
assert.equal(sep, '/', 'Not tested on Windows')
|
|
16
34
|
|
|
17
35
|
export const sha512integrity = (x) => `sha512-${hash('sha512', x, 'base64')}`
|
|
18
36
|
|
|
37
|
+
// A bundled file's integrity from its `content` as a bundle stores it: of the bytes it stands for,
|
|
38
|
+
// a 'resource:base64' payload's decoded ones, else its UTF-8 text's.
|
|
39
|
+
export const bundledIntegrity = (content, format) => sha512integrity(format === 'resource:base64' ? Buffer.from(content, 'base64') : content)
|
|
40
|
+
|
|
41
|
+
// The companion lockfile of `bundle` as it is now (Lockfile.fromBundle's), built on the first call,
|
|
42
|
+
// the same one after: each file's integrity that of the bytes its content stands for
|
|
43
|
+
// (bundledIntegrity), so a file is hashed only once the lockfile is wanted, for a bundle whose
|
|
44
|
+
// content is the bytes read, encoded without loss. Its records are taken now: one the bundle
|
|
45
|
+
// replaces after (a commit pinned onto a module) is not the lockfile's.
|
|
46
|
+
// -> () => Lockfile
|
|
47
|
+
export function deferredLockfile(bundle) {
|
|
48
|
+
const { config, entries, imports, formats, executable } = bundle
|
|
49
|
+
const now = { config, entries, modules: new Map(bundle.modules), imports, formats, executable }
|
|
50
|
+
let lockfile
|
|
51
|
+
return () => (lockfile ??= Lockfile.fromBundle(now, (file, content) => bundledIntegrity(content, formats.get(file))))
|
|
52
|
+
}
|
|
53
|
+
|
|
19
54
|
export function readFileSyncMaybe(dir, file, encoding, host = diskHost) {
|
|
20
55
|
let buf
|
|
21
56
|
try {
|
|
@@ -44,3 +79,12 @@ export function canonicalizePath(p, host = diskHost) {
|
|
|
44
79
|
return abs
|
|
45
80
|
}
|
|
46
81
|
}
|
|
82
|
+
|
|
83
|
+
// Whether `url` is in `state`'s scope, as it attests one: anywhere under scope=full, else in
|
|
84
|
+
// node_modules. Over State's public members alone, so a plugin may ask it of a State another
|
|
85
|
+
// stasis-core copy made (State.preload).
|
|
86
|
+
export const inScope = (state, url) => state.config.full || state.inNodeModules(url)
|
|
87
|
+
|
|
88
|
+
// Whether the file at `absolute` is one `state`'s bundle serves under load: within its root (outside
|
|
89
|
+
// it: system files, scratch, read from disk) and in its scope. Over State's public members, as inScope.
|
|
90
|
+
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 {
|
|
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 { bundledIntegrity, 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,9 +49,6 @@ function upsertFormat(map, file, format) {
|
|
|
54
49
|
map.set(file, currentFormat === undefined ? format : reconcileFormat(format, currentFormat, file))
|
|
55
50
|
}
|
|
56
51
|
|
|
57
|
-
// A bundled file's integrity as addFile/addFsDir hash it: a 'resource:base64' payload's is its decoded bytes'.
|
|
58
|
-
const bundledIntegrity = (content, format) => sha512integrity(format === 'resource:base64' ? Buffer.from(content, 'base64') : content)
|
|
59
|
-
|
|
60
52
|
// Loader formats the extension alone decides; `.js`/`.ts` follow the nearest package `type`.
|
|
61
53
|
const EXT_FORMATS = { __proto__: null, '.json': 'json', '.mjs': 'module', '.cjs': 'commonjs', '.mts': 'module-typescript', '.cts': 'commonjs-typescript' }
|
|
62
54
|
// With no `type`, Node decides .js/.ts by syntax detection: either variant is possible, the first is the default.
|
|
@@ -68,6 +60,7 @@ const UNTYPED_VARIANTS = { __proto__: null, '.js': ['commonjs', 'module'], '.ts'
|
|
|
68
60
|
// Maintained list: add only a confirmed upstream false positive, with its reason.
|
|
69
61
|
const INCONSISTENT_PACKAGE_JSON_EXCEPTIONS = [
|
|
70
62
|
'node_modules/@redis/client/dist/package.json', // stale build-time copy
|
|
63
|
+
'node_modules/hashids/esm/package.json', // a `type` marker named `hashids-esm`, in every 2.x release
|
|
71
64
|
]
|
|
72
65
|
const isInconsistentPackageJsonException = (file) =>
|
|
73
66
|
INCONSISTENT_PACKAGE_JSON_EXCEPTIONS.some((suffix) => file === suffix || file.endsWith(`/${suffix}`))
|
|
@@ -173,11 +166,9 @@ export class State {
|
|
|
173
166
|
// statically parses files under it to recover edges the resolve hook never observed.
|
|
174
167
|
#preloadRoot = null
|
|
175
168
|
|
|
176
|
-
// Per-artifact "last serialized" caches: write() skips brotli + writeFileSync on unchanged text.
|
|
169
|
+
// Per-artifact "last serialized" caches (the bundles' by path): write() skips brotli + writeFileSync on unchanged text.
|
|
177
170
|
#lastLockData = null
|
|
178
|
-
#
|
|
179
|
-
#lastCodeBundle = null
|
|
180
|
-
#lastResourcesBundle = null
|
|
171
|
+
#lastBundleText = new Map()
|
|
181
172
|
// Lazy `{ value: detectRepo(this.root, this.#host) }`.
|
|
182
173
|
#detectedRepoCache
|
|
183
174
|
// Half ('code' | 'resources') -> `repo` of the bundle absorbed there for adding to.
|
|
@@ -229,7 +220,7 @@ export class State {
|
|
|
229
220
|
const sourcesPath = this.config.bundleFile
|
|
230
221
|
const sources = this.#readMaybe(sourcesPath)
|
|
231
222
|
if (sources && !this.config.replaceBundle) {
|
|
232
|
-
const bundle =
|
|
223
|
+
const bundle = parseBundleBytes(sources)
|
|
233
224
|
// v0 bundle has no per-file formats or import map -- serving one via a sidecar widens trust.
|
|
234
225
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
235
226
|
`stasis sidecar requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
|
|
@@ -419,7 +410,7 @@ export class State {
|
|
|
419
410
|
}
|
|
420
411
|
|
|
421
412
|
#loadCodeBundle(sources, sourcesPath, lockfileLoaded) {
|
|
422
|
-
const bundle =
|
|
413
|
+
const bundle = parseBundleBytes(sources)
|
|
423
414
|
// Bundle.parse accepts v0 for offline tooling; the runtime refuses it -- v0 has no per-file
|
|
424
415
|
// formats or import map, so serving one widens the trust boundary.
|
|
425
416
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
@@ -547,7 +538,7 @@ export class State {
|
|
|
547
538
|
// Load the resources half of a split-bundle layout: ONLY resource-format files, unioned into the
|
|
548
539
|
// bundleFile state.
|
|
549
540
|
#absorbResourcesBundle(resourcesData, { lockfileLoaded, resourcesPath }) {
|
|
550
|
-
const bundle =
|
|
541
|
+
const bundle = parseBundleBytes(resourcesData)
|
|
551
542
|
assert.equal(bundle.version, Bundle.VERSION,
|
|
552
543
|
`stasis run requires a v1 resources bundle; ${resourcesPath} is v${bundle.version}. ` +
|
|
553
544
|
`Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point to upgrade.`)
|
|
@@ -610,12 +601,26 @@ export class State {
|
|
|
610
601
|
attested = attested.get(targetPlatform)
|
|
611
602
|
assert.ok(attested !== undefined, `${what} resolution ${edge} is not attested for platform '${targetPlatform}' by the ${source}`)
|
|
612
603
|
}
|
|
613
|
-
// A per-platform edge is a Map<platform, target
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
604
|
+
// A per-platform edge is a Map<platform, target>, compared structurally.
|
|
605
|
+
assert.ok(importTargetsEqual(file, attested), `${what} resolution ${edge} mismatches the ${source}`)
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// A frozen lockfile's and frozen bundle's checks of a `noun` ('File', or a 'Directory' listing) this
|
|
609
|
+
// run records, in the zone they attest (node_modules, or all in full scope): each attests it, as
|
|
610
|
+
// `module`'s `rel` and among the bundle's `bundled` (its sources or resources), and its `format`,
|
|
611
|
+
// where it has one. This closes the attested set; the bytes are checked by the caller's noupsert.
|
|
612
|
+
// `type` is usually not hash-attested, so a format flip would recategorize a hash-valid file
|
|
613
|
+
// (commonjs<->module) with no hash mismatch.
|
|
614
|
+
#assertFrozenAttests(noun, file, module, rel, bundled, format) {
|
|
615
|
+
if (this.config.frozen) assert.ok(Object.hasOwn(module.files, rel), `${noun} not attested by the frozen lockfile: ${file}`)
|
|
616
|
+
if (this.config.frozenBundle) assert.ok(bundled?.has(file), `${noun} not attested by the frozen bundle: ${file}`)
|
|
617
|
+
if (format == null) return
|
|
618
|
+
if (this.config.frozen && this.#lockFormats !== null) {
|
|
619
|
+
this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
|
|
620
|
+
}
|
|
621
|
+
if (this.config.frozenBundle && this.#bundleFormats !== null) {
|
|
622
|
+
this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
|
|
623
|
+
}
|
|
619
624
|
}
|
|
620
625
|
|
|
621
626
|
// Verify one file's loader format. Callers only pass files that must be attested, so an
|
|
@@ -798,10 +803,7 @@ export class State {
|
|
|
798
803
|
// A package.json in a package vendored into this one (bundle-util's vendor dirs) names that
|
|
799
804
|
// package: the file is this one's, listed as that package's in `vendored`.
|
|
800
805
|
const vendorDir = vendorDirOf(name, nmRel)
|
|
801
|
-
const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, (
|
|
802
|
-
const candidate = resolve(this.root, nmRoot, sub, 'package.json')
|
|
803
|
-
return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
|
|
804
|
-
})
|
|
806
|
+
const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, this.#manifestsBelow(nmRoot))
|
|
805
807
|
vendored = vendor?.vendored
|
|
806
808
|
if (closestPkgAbsolute !== pkgAbsolute && !vendor?.below && !isInconsistentPackageJsonException(this.relative(closestPkgAbsolute))) {
|
|
807
809
|
const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
|
|
@@ -832,7 +834,6 @@ export class State {
|
|
|
832
834
|
}
|
|
833
835
|
const pkg = this.relative(pkgAbsolute)
|
|
834
836
|
assert.ok(pkg === 'package.json' || pkg.endsWith('/package.json'))
|
|
835
|
-
assert.equal(basename(pkg), 'package.json')
|
|
836
837
|
const dir = dirname(pkg)
|
|
837
838
|
if (nmRoot) assert.equal(dir, nmRoot)
|
|
838
839
|
if (!this.modules.has(dir)) {
|
|
@@ -854,6 +855,14 @@ export class State {
|
|
|
854
855
|
return { absolute, file, dir, module: this.modules.get(dir), closestType, vendored, subpackage }
|
|
855
856
|
}
|
|
856
857
|
|
|
858
|
+
// The package.json in the directory `sub` of the package at `nmRoot`, parsed, or null where there is none.
|
|
859
|
+
#manifestsBelow(nmRoot) {
|
|
860
|
+
return (sub) => {
|
|
861
|
+
const candidate = resolve(this.root, nmRoot, sub, 'package.json')
|
|
862
|
+
return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
857
866
|
// The subpackage (isSubpackage) of the package `name` at `nmRoot` that its file `nmRel` is in: the nearest
|
|
858
867
|
// package.json above the file with a name, past `{"type":"module"}` markers (namedManifestOf), walked by
|
|
859
868
|
// the file's own path, not the real one findPackageJSON resolves through a symlinked install -> { dir, name,
|
|
@@ -861,10 +870,7 @@ export class State {
|
|
|
861
870
|
// names a subpath.
|
|
862
871
|
#subpackageOf(nmRoot, nmRel, closest, name) {
|
|
863
872
|
if (closest.name !== undefined && !(typeof closest.name === 'string' && closest.name.startsWith(`${name}/`))) return undefined
|
|
864
|
-
const found = namedManifestOf(nmRel, (
|
|
865
|
-
const candidate = resolve(this.root, nmRoot, sub, 'package.json')
|
|
866
|
-
return packageJSONStat(this.#host, candidate)?.isFile() ? readPackageJSON(this.#host, candidate) : null
|
|
867
|
-
})
|
|
873
|
+
const found = namedManifestOf(nmRel, this.#manifestsBelow(nmRoot))
|
|
868
874
|
if (found === undefined || !isSubpackage(found.pkg.name, name, found.dir)) return undefined
|
|
869
875
|
return { dir: found.dir, name: found.pkg.name, ...(isPackageString(found.pkg.version) ? { version: found.pkg.version } : {}) }
|
|
870
876
|
}
|
|
@@ -970,25 +976,7 @@ export class State {
|
|
|
970
976
|
assert.ok(!relativeEscapes(rel))
|
|
971
977
|
|
|
972
978
|
const inAttestedZone = hasNodeModulesSegment(dir) || this.config.full
|
|
973
|
-
if (this
|
|
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
|
-
}
|
|
979
|
+
if (inAttestedZone) this.#assertFrozenAttests('File', file, module, rel, asResource ? this.#bundleResources : this.#bundleSources, format)
|
|
992
980
|
|
|
993
981
|
// Catch a format flip on already-attested bytes here; at write()'s #mergedFormats it would
|
|
994
982
|
// surface only as a bare `Conflict`.
|
|
@@ -1064,15 +1052,16 @@ export class State {
|
|
|
1064
1052
|
}
|
|
1065
1053
|
|
|
1066
1054
|
// 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
|
|
1068
|
-
// (#readFileOptions) unless the run captured it itself, whose record
|
|
1069
|
-
// validated before a file is added, every offender named at once.
|
|
1070
|
-
// never taken in: by default the ones write() writes. `resources`
|
|
1055
|
+
// as `stasis add` takes them, swept through the State's host) beside what the run reached, each
|
|
1056
|
+
// carried as an fs read is (#readFileOptions) unless the run captured it itself, whose record
|
|
1057
|
+
// stands. The whole set is validated before a file is added, every offender named at once.
|
|
1058
|
+
// `outputs` are the paths written, never taken in: by default the ones write() writes. `resources`
|
|
1059
|
+
// is the allowlist, the config's by default.
|
|
1071
1060
|
includeExtra({ outputs = this.#writeTargets(), resources = this.config.resources } = {}) {
|
|
1072
1061
|
if (this.config.extra.length === 0) return
|
|
1073
1062
|
// Resolved as write() writes them: a relative one from the cwd.
|
|
1074
1063
|
const written = new Set(outputs.map((file) => toPosix(relative(this.root, resolve(file)))))
|
|
1075
|
-
const { files } = expandExtra(this.root, this.config.extra, written)
|
|
1064
|
+
const { files } = expandExtra(this.root, this.config.extra, written, this.#host)
|
|
1076
1065
|
const realRoot = this.#host.realpath(this.root)
|
|
1077
1066
|
const missing = []
|
|
1078
1067
|
const undeclared = []
|
|
@@ -1088,7 +1077,7 @@ export class State {
|
|
|
1088
1077
|
const absolute = resolve(this.root, rel)
|
|
1089
1078
|
const url = pathToFileURL(absolute).toString()
|
|
1090
1079
|
// A node_modules scope leaves the project's own files out, as it does the run's.
|
|
1091
|
-
if (!this
|
|
1080
|
+
if (!inScope(this, url)) continue
|
|
1092
1081
|
// The run's record of a file it captured stands, and `extra` is one more consumer of it.
|
|
1093
1082
|
const file = this.#canonicalFile(url)
|
|
1094
1083
|
if (this.#filesCapturedThisRun.has(file)) {
|
|
@@ -1130,19 +1119,7 @@ export class State {
|
|
|
1130
1119
|
const rel = relative(dir, file)
|
|
1131
1120
|
assert.ok(!relativeEscapes(rel))
|
|
1132
1121
|
|
|
1133
|
-
|
|
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
|
-
}
|
|
1122
|
+
if (hasNodeModulesSegment(dir) || this.config.full) this.#assertFrozenAttests('Directory', file, module, rel, this.#bundleResources, format)
|
|
1146
1123
|
|
|
1147
1124
|
if (!Object.hasOwn(module.files, rel)) module.files[rel] = integrity
|
|
1148
1125
|
assert.equal(module.files[rel], integrity)
|
|
@@ -1358,7 +1335,7 @@ export class State {
|
|
|
1358
1335
|
this.#assertAttestedResolution(this.imports, key, parent, specifier, file, { what: 'observed', source: 'bundle', tolerateUnknown, targetPlatform })
|
|
1359
1336
|
}
|
|
1360
1337
|
|
|
1361
|
-
getImport(parentURL, specifier, { conditions = '*', importAttributes } = {}) {
|
|
1338
|
+
getImport(parentURL, specifier, { conditions = '*', importAttributes, platform } = {}) {
|
|
1362
1339
|
if (conditions !== '*') assert.ok(Array.isArray(conditions))
|
|
1363
1340
|
assert.ok(parentURL, 'getImport requires a parent') // matches addImport; #canonicalSpecifier needs it
|
|
1364
1341
|
const parent = this.#canonicalFile(parentURL)
|
|
@@ -1379,12 +1356,17 @@ export class State {
|
|
|
1379
1356
|
// code, which ERR_ASSERTION escapes.
|
|
1380
1357
|
throw Object.assign(new Error(`Cannot find module '${specifier}' imported from ${parent}`), { code: 'ERR_MODULE_NOT_FOUND' })
|
|
1381
1358
|
}
|
|
1382
|
-
// A --metro per-platform edge is a { platform: file } Map
|
|
1383
|
-
//
|
|
1359
|
+
// A --metro per-platform edge is a { platform: file } Map: `platform`'s target where the caller
|
|
1360
|
+
// builds for one (`stasis build --metro=ios`), which the edge must record. Plain bundle=load has
|
|
1361
|
+
// no platform context to pick one, so fail closed rather than feed a Map where a path is expected.
|
|
1384
1362
|
if (typeof file !== 'string') {
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1363
|
+
if (!file.has(platform)) {
|
|
1364
|
+
const message = platform === undefined
|
|
1365
|
+
? `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`
|
|
1366
|
+
: `Resolution of '${specifier}' from ${parent} is platform-specific and records no '${platform}' target (only ${[...file.keys()].join(', ')}): the bundle wasn't made for '${platform}'`
|
|
1367
|
+
throw Object.assign(new Error(message), { code: 'ERR_STASIS_PLATFORM_SPECIFIC' })
|
|
1368
|
+
}
|
|
1369
|
+
file = file.get(platform)
|
|
1388
1370
|
}
|
|
1389
1371
|
return { url: pathToFileURL(resolve(this.root, file)).toString(), format: this.#loaderFormat(file) }
|
|
1390
1372
|
}
|
|
@@ -1565,17 +1547,13 @@ export class State {
|
|
|
1565
1547
|
const claims = this.#reasonClaims()
|
|
1566
1548
|
|
|
1567
1549
|
const reason = {}
|
|
1568
|
-
let consumers = 0
|
|
1569
1550
|
// Sort keys and file lists: the JSON must be byte-reproducible regardless of record order, else
|
|
1570
1551
|
// #emitBundle's compare-and-skip sees spurious diffs.
|
|
1571
1552
|
for (const who of [...claims.keys()].toSorted()) {
|
|
1572
1553
|
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
|
-
}
|
|
1554
|
+
if (files.length > 0) reason[who] = files.toSorted(sortPaths)
|
|
1577
1555
|
}
|
|
1578
|
-
return
|
|
1556
|
+
return Object.keys(reason).length > 1 ? reason : undefined
|
|
1579
1557
|
}
|
|
1580
1558
|
|
|
1581
1559
|
// The formats a bundle declares: `this.formats`, minus a 'stat:*' the run's unified attestation
|
|
@@ -1687,7 +1665,7 @@ export class State {
|
|
|
1687
1665
|
// (non-deterministic across installs, like the rest of NATIVE_ARTIFACT_EXTS).
|
|
1688
1666
|
if (isNativeArtifact(file)) continue
|
|
1689
1667
|
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
1690
|
-
if (!this
|
|
1668
|
+
if (!inScope(this, url)) continue
|
|
1691
1669
|
this.addFile(url, { reason: 'run' })
|
|
1692
1670
|
}
|
|
1693
1671
|
}
|
|
@@ -1703,7 +1681,7 @@ export class State {
|
|
|
1703
1681
|
const file = this.#canonicalFileMaybe(resolvedURL)
|
|
1704
1682
|
if (file === undefined) continue
|
|
1705
1683
|
const bundled = this.sources.has(file) || this.resources.has(file)
|
|
1706
|
-
if (!bundled && !this
|
|
1684
|
+
if (!bundled && !inScope(this, resolvedURL)) continue
|
|
1707
1685
|
const spec = this.#canonicalSpecifier(parentURL, specifier)
|
|
1708
1686
|
const recorded = this.imports.values().some((byParent) => byParent.get(parent)?.get(spec) !== undefined)
|
|
1709
1687
|
if (!recorded) this.addImport(parentURL, specifier, resolvedURL, { conditions: this.#requireConditions ?? '*' })
|
|
@@ -1779,26 +1757,24 @@ export class State {
|
|
|
1779
1757
|
// AFTER the backfills and `extra`, so buckets they create are covered too.
|
|
1780
1758
|
if (this.config.writeBundle && this.config.packageJSON) this.includePackageJson()
|
|
1781
1759
|
|
|
1782
|
-
//
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
} else {
|
|
1799
|
-
this.#lastUnifiedBundle = this.#emitBundle(sourcesPath, this.sources.size > 0 || this.resources.size > 0, this.#lastUnifiedBundle, () => this.sourceData)
|
|
1800
|
-
}
|
|
1760
|
+
// Every artifact is serialized before the first is written, so one that can't be (no bundle may carry
|
|
1761
|
+
// the lockfile) leaves the others as they were. Sidecars never write the lockfile (parent owns it).
|
|
1762
|
+
const lockText = this.config.writeLockfile && !this.#parent ? this.lockData : null
|
|
1763
|
+
const sourcesPath = this.config.bundleFile || join(this.root, FILE_CODE)
|
|
1764
|
+
// Each bundle written, [path, text]: the code bundle, with the resources too unless they are split
|
|
1765
|
+
// into resourcesBundleFile. hasContent gates each so an empty one is dropped under bundle=replace.
|
|
1766
|
+
const bundles = !this.config.writeBundle ? [] : this.config.resourcesBundleFile
|
|
1767
|
+
? [[sourcesPath, this.#bundleText(this.sources.size > 0, () => this.codeBundle.serialize())],
|
|
1768
|
+
[this.config.resourcesBundleFile, this.#bundleText(this.resources.size > 0, () => this.resourcesBundle.serialize())]]
|
|
1769
|
+
: [[sourcesPath, this.#bundleText(this.sources.size > 0 || this.resources.size > 0, () => this.sourceData)]]
|
|
1770
|
+
|
|
1771
|
+
if (lockText !== null && lockText !== this.#lastLockData) {
|
|
1772
|
+
const lockPath = this.config.lockFile || join(this.root, FILE_LOCK)
|
|
1773
|
+
mkdirSync(dirname(lockPath), { recursive: true })
|
|
1774
|
+
writeFileSync(lockPath, lockText)
|
|
1775
|
+
this.#lastLockData = lockText
|
|
1801
1776
|
}
|
|
1777
|
+
for (const [path, text] of bundles) this.#emitBundle(path, text)
|
|
1802
1778
|
}
|
|
1803
1779
|
|
|
1804
1780
|
// Snapshot of what THIS process observed, for a forked child to hand back to the root. Content is
|
|
@@ -1836,22 +1812,13 @@ export class State {
|
|
|
1836
1812
|
// malformed/foreign shard -- refuse.
|
|
1837
1813
|
assert.equal(shard.scope, this.config.scope, `shard scope "${shard.scope}" != root scope "${this.config.scope}"`)
|
|
1838
1814
|
// 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
|
|
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
|
-
}
|
|
1815
|
+
// replay re-checks real-path containment (realContained), the boundary fs.js enforces on live --fs reads.
|
|
1849
1816
|
// Files first, then edges. Merged files are NEVER marked entries -- a child's "entry" is its
|
|
1850
1817
|
// fork-target main, not a root entry.
|
|
1851
1818
|
for (const file of shard.files) {
|
|
1852
1819
|
const absolute = resolve(this.root, file)
|
|
1853
1820
|
// Skip a key whose on-disk path escapes the root through a symlink.
|
|
1854
|
-
if (!realContained(absolute)) continue
|
|
1821
|
+
if (!realContained(this.root, absolute)) continue
|
|
1855
1822
|
// ABOVE the already-attested fast path below: a child that re-read a file and found no bit
|
|
1856
1823
|
// refutes it by OMISSION, so every file the shard RECORDS needs an observation. Re-derived
|
|
1857
1824
|
// from THIS process's disk, so the shard cannot inject a forged bit.
|
|
@@ -1896,7 +1863,7 @@ export class State {
|
|
|
1896
1863
|
const kind = stats.isDirectory() ? 'directory' : stats.isFile() ? 'file' : null
|
|
1897
1864
|
// statSync follows links: a symlink pointing OUT must not attest an external kind under an
|
|
1898
1865
|
// in-root key.
|
|
1899
|
-
if (kind === null || !realContained(absolute)) continue
|
|
1866
|
+
if (kind === null || !realContained(this.root, absolute)) continue
|
|
1900
1867
|
this.addFsStat(pathToFileURL(absolute).toString(), kind)
|
|
1901
1868
|
} catch {
|
|
1902
1869
|
// Gone/dangling, or a kind conflict with the root's capture: skip.
|
|
@@ -1924,19 +1891,21 @@ export class State {
|
|
|
1924
1891
|
}
|
|
1925
1892
|
}
|
|
1926
1893
|
|
|
1927
|
-
//
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1894
|
+
// What a bundle write leaves at its path: the serialized bundle, or null to drop an empty one under bundle=replace.
|
|
1895
|
+
#bundleText(hasContent, serialize) {
|
|
1896
|
+
return !hasContent && this.config.replaceBundle ? null : serialize()
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
// Emit one bundle artifact. Null (empty under bundle=replace) DELETES the file (a replace run is
|
|
1900
|
+
// authoritative); text unchanged since this State last wrote it skips brotli + writeFileSync.
|
|
1901
|
+
#emitBundle(path, text) {
|
|
1902
|
+
if (text === null) {
|
|
1931
1903
|
rmSync(path, { force: true })
|
|
1932
|
-
|
|
1933
|
-
}
|
|
1934
|
-
const text = serialize()
|
|
1935
|
-
if (text !== lastText) {
|
|
1904
|
+
} else if (text !== this.#lastBundleText.get(path)) {
|
|
1936
1905
|
mkdirSync(dirname(path), { recursive: true })
|
|
1937
|
-
writeFileSync(path,
|
|
1906
|
+
writeFileSync(path, compressBundleText(text, this.config.brotliQuality))
|
|
1938
1907
|
}
|
|
1939
|
-
|
|
1908
|
+
this.#lastBundleText.set(path, text)
|
|
1940
1909
|
}
|
|
1941
1910
|
|
|
1942
1911
|
get parent() {
|
|
@@ -2012,7 +1981,7 @@ export class State {
|
|
|
2012
1981
|
if (typeof file !== 'string' || missing.has(file)) continue
|
|
2013
1982
|
if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) continue
|
|
2014
1983
|
const url = pathToFileURL(resolve(this.root, file)).toString()
|
|
2015
|
-
if (!this
|
|
1984
|
+
if (!inScope(this, url)) continue
|
|
2016
1985
|
missing.add(file)
|
|
2017
1986
|
}
|
|
2018
1987
|
}
|