@exodus/stasis-core 1.0.0-beta.2 → 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/shard.js ADDED
@@ -0,0 +1,60 @@
1
+ // Wire format for child->root capture forwarding (--child-process). A shard carries only keys, formats and
2
+ // resolution edges; content, listings, stat kinds and exec bits are re-derived from the root's OWN disk, so
3
+ // a shard cannot inject content or a forged bit. `files` and `formats` are independent -- don't collapse them.
4
+
5
+ import { KNOWN_FORMATS, assert, fileMapToObject, isPlainObject, objectToMaps, posixPathEscapes } from './util.js'
6
+
7
+ export const SHARD_VERSION = 1
8
+
9
+ // `imports` nests (conditions -> parent -> specifier -> file, or -> platform -> file under --metro),
10
+ // so the conversion recurses rather than flattening one level.
11
+ export function serializeShard({ scope, files, formats, imports }) {
12
+ return JSON.stringify({
13
+ version: SHARD_VERSION,
14
+ scope,
15
+ files,
16
+ formats: fileMapToObject(formats, { sorted: false }),
17
+ imports: fileMapToObject(imports, { sorted: false }),
18
+ })
19
+ }
20
+
21
+ // The upstream signature is what makes a shard trustworthy; this only guards a malformed message,
22
+ // applying the same schema rules Lockfile.parse will. Maps keep lookups off Object.prototype.
23
+ export function parseShard(text) {
24
+ const json = JSON.parse(text)
25
+ assert(isPlainObject(json), 'shard must be an object')
26
+ assert(json.version === SHARD_VERSION, `shard version ${json.version} != ${SHARD_VERSION}`)
27
+ assert(['node_modules', 'full'].includes(json.scope), `shard scope: ${json.scope}`)
28
+ assert(Array.isArray(json.files), 'shard files must be an array')
29
+ assert(isPlainObject(json.formats) && isPlainObject(json.imports), 'shard formats/imports must be objects')
30
+
31
+ for (const file of json.files) assertKey(file, 'shard file')
32
+ const formats = objectToMaps(json.formats)
33
+ for (const [file, format] of formats) {
34
+ assertKey(file, 'shard format')
35
+ assert(KNOWN_FORMATS.has(format), `shard format for ${file}: ${format}`)
36
+ }
37
+ const imports = objectToMaps(json.imports)
38
+ for (const [, byParent] of imports) {
39
+ assert(byParent instanceof Map, 'shard imports: expected a parent map')
40
+ for (const [parent, specs] of byParent) {
41
+ assertKey(parent, 'shard import parent')
42
+ assert(specs instanceof Map, `shard imports for ${parent}: expected a specifier map`)
43
+ for (const [, target] of specs) assertTarget(target, parent)
44
+ }
45
+ }
46
+ return { scope: json.scope, files: json.files, formats, imports }
47
+ }
48
+
49
+ function assertKey(key, what) {
50
+ assert(typeof key === 'string' && key !== '' && !posixPathEscapes(key), `${what}: ${key}`)
51
+ }
52
+
53
+ // A target is a file, or (--metro) a platform -> file map; nothing deeper.
54
+ function assertTarget(target, parent) {
55
+ if (target instanceof Map) {
56
+ for (const [, file] of target) assertKey(file, `shard import target from ${parent}`)
57
+ return
58
+ }
59
+ assertKey(target, `shard import target from ${parent}`)
60
+ }
package/src/state-util.js CHANGED
@@ -3,24 +3,11 @@ import { hash } from 'node:crypto'
3
3
  import * as fs from 'node:fs'
4
4
  import { join, resolve, sep } from 'node:path'
5
5
 
6
- // The genuine fs readers, snapshotted off the namespace before any --fs patch. This
7
- // module is the single source of truth for "the real reader" -- imported by stasis's
8
- // own internals (here; and fs.js's --fs hook imports realReadFileSync so its capture
9
- // path sees real bytes and never recurses) AND by every bundler plugin (webpack.js /
10
- // esbuild.js / metro.js; esbuild uses the async realReadFile for its async onLoad). A
11
- // plugin's capture read is stasis-internal bookkeeping, NOT a program fs
12
- // read, so routing it through the patched fs would re-record the bundler's module graph
13
- // into the preload/main bundle -- duplicating into main what the plugin captured into
14
- // its own sidecar. `import { readFileSync } from 'node:fs'` would NOT do: `stasis run
15
- // --fs` monkey-patches fs.readFileSync then calls module.syncBuiltinESMExports(), which
16
- // rebinds that named import to the patched fn -- and a plugin loads AFTER the patch.
17
- // Snapshotting off the namespace dodges that. state-util.js is the lowest-level module
18
- // (a leaf with no internal imports), evaluated during the `--import` preload before any
19
- // patch, so these are always the real builtins -- and the snapshot is taken as early as
20
- // possible. (`fs.promises` IS node:fs/promises, the same object, so realReadFile also
21
- // covers `import { readFile } from 'node:fs/promises'`.)
6
+ // Genuine fs readers snapshotted off the namespace before any --fs patch. A plain `import { readFileSync }`
7
+ // won't do: --fs monkey-patches fs then syncBuiltinESMExports() rebinds named imports, so snapshotting dodges that.
22
8
  export const realReadFileSync = fs.readFileSync
23
9
  export const realReadFile = fs.promises.readFile
10
+ export const realReaddirSync = fs.readdirSync
24
11
  const { realpathSync } = fs
25
12
 
26
13
  assert.equal(sep, '/', 'Not tested on Windows')
@@ -44,16 +31,7 @@ export function noupsert(map, key, value) {
44
31
  }
45
32
  }
46
33
 
47
- // Canonicalize a filesystem path for write-target collision detection.
48
- // `resolve()` normalizes `./` and `../` but NOT symlinks -- two paths through
49
- // different symlink chains that ultimately name the same inode would compare as
50
- // distinct strings, letting a "is this the same target?" check silently fork
51
- // (the resourcesBundleFile/lockFile/bundleFile claim sets). `realpathSync`
52
- // closes that hole when the file already exists;
53
- // for not-yet-written targets (the common case for a fresh capture's write
54
- // path), fall back to the lexical `resolve()` -- there's no symlink to follow,
55
- // and a later writer claiming the realpath of the same target will canonicalize
56
- // to the same string the first writer's resolve() produced.
34
+ // Canonicalize for write-target collision detection: realpathSync catches symlink aliases resolve() would miss; lexical fallback for not-yet-written targets.
57
35
  export function canonicalizePath(p) {
58
36
  const abs = resolve(p)
59
37
  try {