@exodus/stasis-core 1.0.0-beta.0 → 1.0.0-beta.10

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/state-util.js CHANGED
@@ -3,37 +3,28 @@ 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
+ import { diskHost } from './host.js'
7
+
8
+ // Genuine fs readers snapshotted off the namespace before any --fs patch. A plain `import { readFileSync }`
9
+ // won't do: --fs monkey-patches fs then syncBuiltinESMExports() rebinds named imports, so snapshotting dodges that.
22
10
  export const realReadFileSync = fs.readFileSync
23
11
  export const realReadFile = fs.promises.readFile
24
- const { realpathSync } = fs
12
+ export const realReaddirSync = fs.readdirSync
13
+ export const realExistsSync = fs.existsSync
25
14
 
26
15
  assert.equal(sep, '/', 'Not tested on Windows')
27
16
 
28
17
  export const sha512integrity = (x) => `sha512-${hash('sha512', x, 'base64')}`
29
18
 
30
- export function readFileSyncMaybe(dir, file, encoding) {
19
+ export function readFileSyncMaybe(dir, file, encoding, host = diskHost) {
20
+ let buf
31
21
  try {
32
- return realReadFileSync(join(dir, file), encoding)
22
+ buf = host.readFile(join(dir, file))
33
23
  } catch (err) {
34
24
  if (err.code === 'ENOENT') return null
35
25
  throw err
36
26
  }
27
+ return encoding === undefined ? buf : buf.toString(encoding)
37
28
  }
38
29
 
39
30
  export function noupsert(map, key, value) {
@@ -44,20 +35,11 @@ export function noupsert(map, key, value) {
44
35
  }
45
36
  }
46
37
 
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.
57
- export function canonicalizePath(p) {
38
+ // Canonicalize for write-target collision detection: the host's realpath catches symlink aliases resolve() would miss; lexical fallback for not-yet-written targets.
39
+ export function canonicalizePath(p, host = diskHost) {
58
40
  const abs = resolve(p)
59
41
  try {
60
- return realpathSync(abs)
42
+ return host.realpath(abs)
61
43
  } catch {
62
44
  return abs
63
45
  }