@exodus/stasis-core 1.0.0-beta.4 → 1.0.0-beta.5

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/fs.js CHANGED
@@ -5,42 +5,27 @@
5
5
  // captured/served; anything else falls through to the real fs and is never attested.
6
6
 
7
7
  import { createRequire, syncBuiltinESMExports } from 'node:module'
8
- import { isAbsolute, relative, resolve } from 'node:path'
8
+ import { resolve } from 'node:path'
9
9
  import { fileURLToPath, pathToFileURL } from 'node:url'
10
10
 
11
11
  import { realReadFileSync } from './state-util.js'
12
- import { isDotEnvFile, isStasisArtifactName } from './util.js'
12
+ import { isDotEnvFile, isPathWithin, isStasisArtifactName } from './util.js'
13
13
 
14
14
  const require = createRequire(import.meta.url)
15
15
  const fs = require('node:fs')
16
16
 
17
17
  // Snapshot the real fns while still genuine builtins; the hooks call THESE, never patched `fs.*`, so
18
- // there's no recursion and capture/containment always see real bytes/paths.
19
- const realReaddirSync = fs.readdirSync
20
- const realLstatSync = fs.lstatSync
21
- const realStatSync = fs.statSync
22
- const realRealpathSync = fs.realpathSync
23
- const realRealpathSyncNative = fs.realpathSync.native
24
- const realExistsSync = fs.existsSync
25
- const realAccessSync = fs.accessSync
26
-
27
- // Async counterparts, snapshotted for `--fs=async`. fs.promises IS node:fs/promises (same object).
28
- const realReadFile = fs.readFile
29
- const realReaddir = fs.readdir
30
- const realLstat = fs.lstat
31
- const realStat = fs.stat
32
- const realReadFileP = fs.promises.readFile
33
- const realReaddirP = fs.promises.readdir
34
- const realLstatP = fs.promises.lstat
35
- const realStatP = fs.promises.stat
36
-
37
- // The legacy callback fs.exists is deliberately NOT reassigned (wrapping would clobber its
38
- // util.promisify.custom); it still answers via the patched fs.access.
39
- const realAccess = fs.access
40
- const realAccessP = fs.promises.access
41
- const realRealpath = fs.realpath
42
- const realRealpathNative = fs.realpath.native
43
- const realRealpathP = fs.promises.realpath
18
+ // there's no recursion and capture/containment always see real bytes/paths. fs.promises IS
19
+ // node:fs/promises (same object). The legacy callback fs.exists is deliberately NOT reassigned
20
+ // (wrapping would clobber its util.promisify.custom); it still answers via the patched fs.access.
21
+ const {
22
+ readdirSync: realReaddirSync, lstatSync: realLstatSync, statSync: realStatSync, realpathSync: realRealpathSync,
23
+ existsSync: realExistsSync, accessSync: realAccessSync,
24
+ readFile: realReadFile, readdir: realReaddir, lstat: realLstat, stat: realStat, access: realAccess, realpath: realRealpath,
25
+ } = fs
26
+ const { readFile: realReadFileP, readdir: realReaddirP, lstat: realLstatP, stat: realStatP, access: realAccessP, realpath: realRealpathP } = fs.promises
27
+ const realRealpathSyncNative = realRealpathSync.native
28
+ const realRealpathNative = realRealpath.native
44
29
 
45
30
  // The bundle serves read-only, so only an F_OK/R_OK-only mode may be answered from it; allowlist form
46
31
  // so a W_OK/X_OK or out-of-range mode defers to the real fs.
@@ -55,12 +40,6 @@ function toAbsPath(path) {
55
40
  return null
56
41
  }
57
42
 
58
- // True when `abs` is the project root or lives beneath it -- lexical only (see realContained).
59
- function withinRoot(root, abs) {
60
- const rel = relative(root, abs)
61
- return !rel.startsWith('..') && !isAbsolute(rel)
62
- }
63
-
64
43
  let realRootCache
65
44
  function realRootOf(root) {
66
45
  if (realRootCache?.root !== root) {
@@ -76,22 +55,20 @@ function realRootOf(root) {
76
55
  function realContained(root, abs) {
77
56
  let real
78
57
  try { real = realRealpathSync(abs) } catch { return false }
79
- const rel = relative(realRootOf(root), real)
80
- return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))
58
+ return isPathWithin(realRootOf(root), real)
81
59
  }
82
60
 
83
61
  // At the project root, drop stasis's own artifacts from the RECORDED listing so it doesn't depend on
84
62
  // whether an earlier run left them behind; the program still receives the true on-disk `names`.
85
63
  function dirCaptureNames(root, abs, names) {
86
- if (relative(root, abs) !== '') return names
64
+ if (!isPathWithin(abs, root)) return names
87
65
  return names.filter((name) => !isStasisArtifactName(name))
88
66
  }
89
67
 
90
68
  // Apply readFileSync's encoding arg to raw bytes; Buffer.toString matches fs's ERR_UNKNOWN_ENCODING.
91
69
  function decode(buf, options) {
92
70
  const encoding = typeof options === 'string' ? options : options?.encoding
93
- if (encoding == null) return buf
94
- return buf.toString(encoding)
71
+ return encoding == null ? buf : buf.toString(encoding)
95
72
  }
96
73
 
97
74
  // Honour realpath's `encoding` arg for the synthetic answer when a bundle-served path is gone from disk.
@@ -100,36 +77,19 @@ function encodeRealpath(abs, options) {
100
77
  return encoding === 'buffer' ? Buffer.from(abs) : abs
101
78
  }
102
79
 
103
- // Source-map sidecars (`*.map`) are NON-EXISTENT under --fs (faithful ENOENT in both modes, so a
104
- // captured build replays byte-identically). Opt out via `map` in resources, or bundle membership.
105
- function isSkippedSourceMap(state, abs) {
106
- if (!abs.toLowerCase().endsWith('.map')) return false
107
- if (state.config.resources?.has('map')) return false
108
- if (state.config.loadBundle && state.getFsStatFamily(pathToFileURL(abs).toString()) !== undefined) return false
109
- return true
110
- }
111
-
112
- // `.env`/`.env.*` are NON-EXISTENT under --fs so an automated capture can never bake a secret in. No
113
- // resources opt-in: the ONE way one serves at load is membership (an explicit `stasis add .env`).
114
- function isSkippedEnvFile(state, abs) {
115
- if (!isDotEnvFile(abs)) return false
116
- if (state.config.loadBundle && state.getFsStatFamily(pathToFileURL(abs).toString()) !== undefined) return false
117
- return true
118
- }
119
-
80
+ // NON-EXISTENT under --fs (a faithful ENOENT from every reader, listings included, in both modes):
81
+ // source-map sidecars (`*.map`, so a captured build replays byte-identically; opt out via `map` in
82
+ // resources) and `.env`/`.env.*` (an automated capture can never bake a secret in; no opt-in). The
83
+ // ONE way either serves at load is bundle membership (an explicit `stasis add .env`).
120
84
  function isSkippedFsPath(state, abs) {
121
- return isSkippedSourceMap(state, abs) || isSkippedEnvFile(state, abs)
85
+ const skipped = (abs.toLowerCase().endsWith('.map') && !state.config.resources?.has('map')) || isDotEnvFile(abs)
86
+ return skipped && !(state.config.loadBundle && state.getFsStatFamily(pathToFileURL(abs).toString()) !== undefined)
122
87
  }
123
88
 
124
89
  // A faithful Node-shaped ENOENT, indistinguishable from a real absent file.
125
90
  function enoent(syscall, path) {
126
91
  const p = typeof path === 'string' ? path : Buffer.isBuffer(path) ? path.toString() : String(path)
127
- const err = new Error(`ENOENT: no such file or directory, ${syscall} '${p}'`)
128
- err.code = 'ENOENT'
129
- err.errno = -2
130
- err.syscall = syscall
131
- err.path = p
132
- return err
92
+ return Object.assign(new Error(`ENOENT: no such file or directory, ${syscall} '${p}'`), { code: 'ENOENT', errno: -2, syscall, path: p })
133
93
  }
134
94
 
135
95
  // File-type bits, so code reading stats.mode (not the is*() methods) still sees file-vs-dir.
@@ -175,145 +135,112 @@ export function installFsHooks({ async: patchAsync, getState, markAborted, isLoa
175
135
  if (installed) return
176
136
  installed = true
177
137
 
178
- fs.readFileSync = function readFileSync(path, options) {
138
+ // Classify a path for every hook: null passes through to the real fs; else { mode, abs, url, state }
139
+ // with mode 'absent' (a skipped path: ENOENT), 'serve' (bundle=load; an uncaptured path still falls
140
+ // through to disk) or 'capture' (bundle=add|replace, outside a loader read).
141
+ const classify = (path) => {
179
142
  const state = getState()
180
- if (state) {
181
- const abs = toAbsPath(path)
182
- if (abs !== null && withinRoot(state.root, abs)) {
183
- if (isSkippedFsPath(state, abs)) throw enoent('open', path)
184
- const url = pathToFileURL(abs).toString()
185
- if (state.config.loadBundle) {
186
- const buf = state.getFsFileFamily(url)
187
- if (buf !== undefined) return decode(buf, options)
188
- } else if (state.config.writeBundle && !isLoadingModule()) {
189
- const buf = realReadFileSync(path)
190
- // A path whose real location escapes root (in-tree symlink) is read but NOT recorded, and
191
- // neither is a read a bundler-plugin sidecar already attests.
192
- if (realContained(state.root, abs) && !state.attestedBySidecar(url)) {
193
- try { state.addFsFile(url, buf) } catch (err) { markAborted(err) }
194
- }
195
- return decode(buf, options)
196
- }
197
- }
198
- }
199
- return realReadFileSync(path, options)
143
+ if (!state) return null
144
+ const abs = toAbsPath(path)
145
+ if (abs === null || !isPathWithin(state.root, abs)) return null
146
+ if (isSkippedFsPath(state, abs)) return { mode: 'absent', abs }
147
+ const url = pathToFileURL(abs).toString()
148
+ if (state.config.loadBundle) return { mode: 'serve', state, url, abs }
149
+ if (state.config.writeBundle && !isLoadingModule()) return { mode: 'capture', state, url, abs }
150
+ return null
200
151
  }
201
-
202
- fs.readdirSync = function readdirSync(path, options) {
203
- const state = getState()
204
- // Single-argument form only; any options pass straight through.
205
- if (state && options == null) {
206
- const abs = toAbsPath(path)
207
- if (abs !== null && withinRoot(state.root, abs)) {
208
- const url = pathToFileURL(abs).toString()
209
- if (state.config.loadBundle) {
210
- const names = state.getFsDirFamily(url)
211
- if (names !== undefined) return names // sorted at capture time
212
- } else if (state.config.writeBundle && !isLoadingModule()) {
213
- const names = realReaddirSync(path)
214
- // As in readFileSync: a dir whose real path escapes root is not recorded.
215
- if (realContained(state.root, abs)) {
216
- try { state.addFsDir(url, dirCaptureNames(state.root, abs, names)) } catch (err) { markAborted(err) }
217
- }
218
- return names
219
- }
220
- }
221
- }
222
- return realReaddirSync(path, options)
152
+ // A path the bundle knows (content, listing, stat record or implied dir), for the serve-only probes.
153
+ const served = (t) => t?.mode === 'serve' && t.state.getFsStatFamily(t.url) !== undefined
154
+
155
+ // Capture-side records; a rejected capture taints the run (markAborted) rather than failing the read.
156
+ const record = (fn) => { try { fn() } catch (err) { markAborted(err) } }
157
+ // A path whose real location escapes root (in-tree symlink) is read but NOT recorded, and neither is
158
+ // a read a bundler-plugin sidecar already attests.
159
+ const captureFile = ({ state, abs, url }, buf) => {
160
+ if (realContained(state.root, abs) && !state.attestedBySidecar(url)) record(() => state.addFsFile(url, buf))
223
161
  }
224
-
225
- // Shared capture-side stat recording: record the path's KIND as a payload-free stat record so a
226
- // stat-ONLY path's type getters answer at load. Only file and dir are modelled.
227
- const captureStat = (state, abs, stats) => {
162
+ const captureDir = ({ state, abs, url }, names) => {
163
+ if (realContained(state.root, abs)) record(() => state.addFsDir(url, dirCaptureNames(state.root, abs, names)))
164
+ }
165
+ // A stat records the path's KIND as a payload-free record so a stat-ONLY path's type getters answer
166
+ // at load. Only file and dir are modelled.
167
+ const captureStat = ({ state, abs, url }, stats) => {
228
168
  const isDir = stats.isDirectory()
229
169
  if (!isDir && !stats.isFile()) return
230
- const url = pathToFileURL(abs).toString()
231
- if (!realContained(state.root, abs) || state.attestedBySidecar(url)) return
232
- try { state.addFsStat(url, isDir ? 'directory' : 'file') } catch (err) { markAborted(err) }
170
+ if (realContained(state.root, abs) && !state.attestedBySidecar(url)) record(() => state.addFsStat(url, isDir ? 'directory' : 'file'))
233
171
  }
234
172
 
235
- fs.lstatSync = function lstatSync(path, options) {
236
- const state = getState()
237
- // Single-arg form only; capture still hands the caller the REAL Stats/errors.
238
- if (state && options == null) {
239
- const abs = toAbsPath(path)
240
- if (abs !== null && withinRoot(state.root, abs)) {
241
- if (isSkippedFsPath(state, abs)) throw enoent('lstat', path)
242
- if (state.config.loadBundle) {
243
- const kind = state.getFsStatFamily(pathToFileURL(abs).toString())
244
- if (kind !== undefined) return bundleStats(realLstatSync, path, kind === 'directory')
245
- } else if (state.config.writeBundle && !isLoadingModule()) {
246
- const stats = realLstatSync(path)
247
- captureStat(state, abs, stats)
248
- return stats
249
- }
250
- }
173
+ fs.readFileSync = function readFileSync(path, options) {
174
+ const t = classify(path)
175
+ if (t?.mode === 'absent') throw enoent('open', path)
176
+ if (t?.mode === 'serve') {
177
+ const buf = t.state.getFsFileFamily(t.url)
178
+ if (buf !== undefined) return decode(buf, options)
179
+ } else if (t?.mode === 'capture') {
180
+ const buf = realReadFileSync(path)
181
+ captureFile(t, buf)
182
+ return decode(buf, options)
251
183
  }
252
- return realLstatSync(path, options)
184
+ return realReadFileSync(path, options)
253
185
  }
254
186
 
255
- // Mirrors lstatSync but follows symlinks, so it must use the real statSync: an in-root symlink
256
- // records the TARGET's kind here, nothing under lstatSync.
257
- fs.statSync = function statSync(path, options) {
258
- const state = getState()
259
- if (state && options == null) {
260
- const abs = toAbsPath(path)
261
- if (abs !== null && withinRoot(state.root, abs)) {
262
- if (isSkippedFsPath(state, abs)) throw enoent('stat', path)
263
- if (state.config.loadBundle) {
264
- const kind = state.getFsStatFamily(pathToFileURL(abs).toString())
265
- if (kind !== undefined) return bundleStats(realStatSync, path, kind === 'directory')
266
- } else if (state.config.writeBundle && !isLoadingModule()) {
267
- const stats = realStatSync(path)
268
- captureStat(state, abs, stats)
269
- return stats
270
- }
271
- }
187
+ fs.readdirSync = function readdirSync(path, options) {
188
+ const t = classify(path)
189
+ if (t?.mode === 'absent') throw enoent('scandir', path)
190
+ // Served/captured in the single-argument form only; any options pass straight through.
191
+ if (options == null && t?.mode === 'serve') {
192
+ const names = t.state.getFsDirFamily(t.url)
193
+ if (names !== undefined) return names // sorted at capture time
194
+ } else if (options == null && t?.mode === 'capture') {
195
+ const names = realReaddirSync(path)
196
+ captureDir(t, names)
197
+ return names
272
198
  }
273
- return realStatSync(path, options)
199
+ return realReaddirSync(path, options)
274
200
  }
275
201
 
202
+ // Single-arg form only; capture still hands the caller the REAL Stats/errors. statSync follows
203
+ // symlinks, so it must use the real statSync: an in-root symlink records the TARGET's kind there.
204
+ const servedStatSync = (syscall, realFn) => ({
205
+ [`${syscall}Sync`](path, options) {
206
+ const t = options == null ? classify(path) : null
207
+ if (t?.mode === 'absent') throw enoent(syscall, path)
208
+ if (t?.mode === 'serve') {
209
+ const kind = t.state.getFsStatFamily(t.url)
210
+ if (kind !== undefined) return bundleStats(realFn, path, kind === 'directory')
211
+ } else if (t?.mode === 'capture') {
212
+ const stats = realFn(path)
213
+ captureStat(t, stats)
214
+ return stats
215
+ }
216
+ return realFn(path, options)
217
+ },
218
+ })[`${syscall}Sync`]
219
+ fs.lstatSync = servedStatSync('lstat', realLstatSync)
220
+ fs.statSync = servedStatSync('stat', realStatSync)
221
+
276
222
  // existsSync/accessSync/realpathSync: existence + canonical-path PROBES, serve-only (bundle=load) --
277
223
  // a tool may probe before reading bytes (@babel/core does); capture mode and unrecorded paths defer.
278
224
  fs.existsSync = function existsSync(path) {
279
- const state = getState()
280
- if (state) {
281
- const abs = toAbsPath(path)
282
- if (abs !== null && withinRoot(state.root, abs)) {
283
- if (isSkippedFsPath(state, abs)) return false
284
- if (state.config.loadBundle && state.getFsStatFamily(pathToFileURL(abs).toString()) !== undefined) return true
285
- }
286
- }
287
- return realExistsSync(path)
225
+ const t = classify(path)
226
+ if (t?.mode === 'absent') return false
227
+ return served(t) || realExistsSync(path)
288
228
  }
289
229
 
290
230
  fs.accessSync = function accessSync(path, mode) {
291
- const state = getState()
292
- if (state) {
293
- const abs = toAbsPath(path)
294
- if (abs !== null && withinRoot(state.root, abs)) {
295
- if (isSkippedFsPath(state, abs)) throw enoent('access', path)
296
- if (state.config.loadBundle && isReadOnlyAccessMode(mode) &&
297
- state.getFsStatFamily(pathToFileURL(abs).toString()) !== undefined) {
298
- return undefined
299
- }
300
- }
301
- }
231
+ const t = classify(path)
232
+ if (t?.mode === 'absent') throw enoent('access', path)
233
+ if (isReadOnlyAccessMode(mode) && served(t)) return undefined
302
234
  return realAccessSync(path, mode)
303
235
  }
304
236
 
305
237
  // Try the real realpath first (true symlink resolution while on disk), fall back to the lexical abs
306
238
  // once gone (ancestor symlinks not re-resolved then).
307
239
  const servedRealpathSync = (realFn) => function realpathSync(path, options) {
308
- const state = getState()
309
- if (state) {
310
- const abs = toAbsPath(path)
311
- if (abs !== null && withinRoot(state.root, abs)) {
312
- if (isSkippedFsPath(state, abs)) throw enoent('realpath', path)
313
- if (state.config.loadBundle && state.getFsStatFamily(pathToFileURL(abs).toString()) !== undefined) {
314
- try { return realFn(path, options) } catch { return encodeRealpath(abs, options) }
315
- }
316
- }
240
+ const t = classify(path)
241
+ if (t?.mode === 'absent') throw enoent('realpath', path)
242
+ if (served(t)) {
243
+ try { return realFn(path, options) } catch { return encodeRealpath(t.abs, options) }
317
244
  }
318
245
  return realFn(path, options)
319
246
  }
@@ -323,19 +250,9 @@ export function installFsHooks({ async: patchAsync, getState, markAborted, isLoa
323
250
  // --fs=async counterparts; a served callback is deferred (queueMicrotask) to preserve fs's
324
251
  // always-async contract.
325
252
  if (patchAsync) {
326
- // Classify a path as the sync readers do: 'serve', 'capture', 'absent', or null (pass through).
327
- const fsTarget = (path) => {
328
- const state = getState()
329
- if (!state) return null
330
- const abs = toAbsPath(path)
331
- if (abs === null || !withinRoot(state.root, abs)) return null
332
- if (isSkippedFsPath(state, abs)) return { mode: 'absent', abs }
333
- const url = pathToFileURL(abs).toString()
334
- if (state.config.loadBundle) return { mode: 'serve', state, url, abs }
335
- if (state.config.writeBundle && !isLoadingModule()) return { mode: 'capture', state, url, abs }
336
- return null
337
- }
338
-
253
+ // Split a trailing callback from its optional options argument.
254
+ const cbArgs = (options, callback) => (typeof options === 'function' ? [undefined, options] : [options, callback])
255
+ const later = (cb, ...args) => queueMicrotask(() => cb(...args))
339
256
  // Route a decode error to the callback (as fs.readFile does); a throw from a deferred callback
340
257
  // would escape as an uncaught exception and crash the process.
341
258
  const decodeToCb = (cb, buf, options) => {
@@ -345,171 +262,138 @@ export function installFsHooks({ async: patchAsync, getState, markAborted, isLoa
345
262
  }
346
263
 
347
264
  fs.readFile = function readFile(path, options, callback) {
348
- const cb = typeof options === 'function' ? options : callback
349
- const opts = typeof options === 'function' ? undefined : options
350
- const t = typeof cb === 'function' ? fsTarget(path) : null
351
- if (t?.mode === 'absent') { queueMicrotask(() => cb(enoent('open', path))); return }
265
+ const [opts, cb] = cbArgs(options, callback)
266
+ const t = typeof cb === 'function' ? classify(path) : null
267
+ if (t?.mode === 'absent') return later(cb, enoent('open', path))
352
268
  if (t?.mode === 'serve') {
353
269
  const buf = t.state.getFsFileFamily(t.url)
354
- if (buf !== undefined) { queueMicrotask(() => decodeToCb(cb, buf, opts)); return }
270
+ if (buf !== undefined) return later(decodeToCb, cb, buf, opts)
355
271
  } else if (t?.mode === 'capture') {
356
- realReadFile(path, (err, buf) => {
272
+ return realReadFile(path, (err, buf) => {
357
273
  if (err) return cb(err)
358
- if (realContained(t.state.root, t.abs) && !t.state.attestedBySidecar(t.url)) { try { t.state.addFsFile(t.url, buf) } catch (e) { markAborted(e) } }
274
+ captureFile(t, buf)
359
275
  decodeToCb(cb, buf, opts)
360
276
  })
361
- return
362
277
  }
363
278
  return realReadFile(path, options, callback)
364
279
  }
365
280
 
366
281
  fs.promises.readFile = async function readFile(path, options) {
367
- const t = fsTarget(path)
282
+ const t = classify(path)
368
283
  if (t?.mode === 'absent') throw enoent('open', path)
369
284
  if (t?.mode === 'serve') {
370
285
  const buf = t.state.getFsFileFamily(t.url)
371
286
  if (buf !== undefined) return decode(buf, options)
372
287
  } else if (t?.mode === 'capture') {
373
288
  const buf = await realReadFileP(path)
374
- if (realContained(t.state.root, t.abs) && !t.state.attestedBySidecar(t.url)) { try { t.state.addFsFile(t.url, buf) } catch (e) { markAborted(e) } }
289
+ captureFile(t, buf)
375
290
  return decode(buf, options)
376
291
  }
377
292
  return realReadFileP(path, options)
378
293
  }
379
294
 
380
295
  fs.readdir = function readdir(path, options, callback) {
381
- const cb = typeof options === 'function' ? options : callback
382
- // Single-arg form only, and a null options must count as "no options" (graceful-fs normalises to
383
- // that), else its reads never hit the bundle.
384
- const t = ((options == null || typeof options === 'function') && typeof cb === 'function') ? fsTarget(path) : null
385
- if (t?.mode === 'serve') {
296
+ const [opts, cb] = cbArgs(options, callback)
297
+ const t = typeof cb === 'function' ? classify(path) : null
298
+ if (t?.mode === 'absent') return later(cb, enoent('scandir', path))
299
+ // Served/captured in the single-arg form only, where a null options must count as "no options"
300
+ // (graceful-fs normalises to that), else its reads never hit the bundle.
301
+ if (opts == null && t?.mode === 'serve') {
386
302
  const names = t.state.getFsDirFamily(t.url)
387
- if (names !== undefined) { queueMicrotask(() => cb(null, names)); return }
388
- } else if (t?.mode === 'capture') {
389
- realReaddir(path, (err, names) => {
303
+ if (names !== undefined) return later(cb, null, names)
304
+ } else if (opts == null && t?.mode === 'capture') {
305
+ return realReaddir(path, (err, names) => {
390
306
  if (err) return cb(err)
391
- if (realContained(t.state.root, t.abs)) { try { t.state.addFsDir(t.url, dirCaptureNames(t.state.root, t.abs, names)) } catch (e) { markAborted(e) } }
307
+ captureDir(t, names)
392
308
  cb(null, names)
393
309
  })
394
- return
395
310
  }
396
311
  return realReaddir(path, options, callback)
397
312
  }
398
313
 
399
314
  fs.promises.readdir = async function readdir(path, options) {
400
- const t = options == null ? fsTarget(path) : null
401
- if (t?.mode === 'serve') {
315
+ const t = classify(path)
316
+ if (t?.mode === 'absent') throw enoent('scandir', path)
317
+ if (options == null && t?.mode === 'serve') {
402
318
  const names = t.state.getFsDirFamily(t.url)
403
319
  if (names !== undefined) return names
404
- } else if (t?.mode === 'capture') {
320
+ } else if (options == null && t?.mode === 'capture') {
405
321
  const names = await realReaddirP(path)
406
- if (realContained(t.state.root, t.abs)) { try { t.state.addFsDir(t.url, dirCaptureNames(t.state.root, t.abs, names)) } catch (e) { markAborted(e) } }
322
+ captureDir(t, names)
407
323
  return names
408
324
  }
409
325
  return realReaddirP(path, options)
410
326
  }
411
327
 
412
328
  // lstat/stat mirror their sync siblings; bundleStats's field fallback stays sync.
413
- fs.lstat = function lstat(path, options, callback) {
414
- const cb = typeof options === 'function' ? options : callback
415
- const t = ((options == null || typeof options === 'function') && typeof cb === 'function') ? fsTarget(path) : null
416
- if (t?.mode === 'absent') { queueMicrotask(() => cb(enoent('lstat', path))); return }
417
- if (t?.mode === 'serve') {
418
- const kind = t.state.getFsStatFamily(t.url)
419
- if (kind !== undefined) { queueMicrotask(() => cb(null, bundleStats(realLstatSync, path, kind === 'directory'))); return }
420
- } else if (t?.mode === 'capture') {
421
- realLstat(path, (err, stats) => {
422
- if (err) return cb(err)
423
- captureStat(t.state, t.abs, stats)
424
- cb(null, stats)
425
- })
426
- return
427
- }
428
- return realLstat(path, options, callback)
429
- }
430
-
431
- fs.stat = function stat(path, options, callback) {
432
- const cb = typeof options === 'function' ? options : callback
433
- const t = ((options == null || typeof options === 'function') && typeof cb === 'function') ? fsTarget(path) : null
434
- if (t?.mode === 'absent') { queueMicrotask(() => cb(enoent('stat', path))); return }
435
- if (t?.mode === 'serve') {
436
- const kind = t.state.getFsStatFamily(t.url)
437
- if (kind !== undefined) { queueMicrotask(() => cb(null, bundleStats(realStatSync, path, kind === 'directory'))); return }
438
- } else if (t?.mode === 'capture') {
439
- realStat(path, (err, stats) => {
440
- if (err) return cb(err)
441
- captureStat(t.state, t.abs, stats)
442
- cb(null, stats)
443
- })
444
- return
445
- }
446
- return realStat(path, options, callback)
447
- }
448
-
449
- fs.promises.lstat = async function lstat(path, options) {
450
- const t = options == null ? fsTarget(path) : null
451
- if (t?.mode === 'absent') throw enoent('lstat', path)
452
- if (t?.mode === 'serve') {
453
- const kind = t.state.getFsStatFamily(t.url)
454
- if (kind !== undefined) return bundleStats(realLstatSync, path, kind === 'directory')
455
- } else if (t?.mode === 'capture') {
456
- const stats = await realLstatP(path)
457
- captureStat(t.state, t.abs, stats)
458
- return stats
459
- }
460
- return realLstatP(path, options)
461
- }
462
-
463
- fs.promises.stat = async function stat(path, options) {
464
- const t = options == null ? fsTarget(path) : null
465
- if (t?.mode === 'absent') throw enoent('stat', path)
466
- if (t?.mode === 'serve') {
467
- const kind = t.state.getFsStatFamily(t.url)
468
- if (kind !== undefined) return bundleStats(realStatSync, path, kind === 'directory')
469
- } else if (t?.mode === 'capture') {
470
- const stats = await realStatP(path)
471
- captureStat(t.state, t.abs, stats)
472
- return stats
473
- }
474
- return realStatP(path, options)
475
- }
329
+ const servedStat = (syscall, realFn, realSyncFn) => ({
330
+ [syscall](path, options, callback) {
331
+ const [opts, cb] = cbArgs(options, callback)
332
+ const t = opts == null && typeof cb === 'function' ? classify(path) : null
333
+ if (t?.mode === 'absent') return later(cb, enoent(syscall, path))
334
+ if (t?.mode === 'serve') {
335
+ const kind = t.state.getFsStatFamily(t.url)
336
+ if (kind !== undefined) return later(cb, null, bundleStats(realSyncFn, path, kind === 'directory'))
337
+ } else if (t?.mode === 'capture') {
338
+ return realFn(path, (err, stats) => {
339
+ if (err) return cb(err)
340
+ captureStat(t, stats)
341
+ cb(null, stats)
342
+ })
343
+ }
344
+ return realFn(path, options, callback)
345
+ },
346
+ })[syscall]
347
+ fs.lstat = servedStat('lstat', realLstat, realLstatSync)
348
+ fs.stat = servedStat('stat', realStat, realStatSync)
349
+
350
+ const servedStatP = (syscall, realFn, realSyncFn) => ({
351
+ async [syscall](path, options) {
352
+ const t = options == null ? classify(path) : null
353
+ if (t?.mode === 'absent') throw enoent(syscall, path)
354
+ if (t?.mode === 'serve') {
355
+ const kind = t.state.getFsStatFamily(t.url)
356
+ if (kind !== undefined) return bundleStats(realSyncFn, path, kind === 'directory')
357
+ } else if (t?.mode === 'capture') {
358
+ const stats = await realFn(path)
359
+ captureStat(t, stats)
360
+ return stats
361
+ }
362
+ return realFn(path, options)
363
+ },
364
+ })[syscall]
365
+ fs.promises.lstat = servedStatP('lstat', realLstatP, realLstatSync)
366
+ fs.promises.stat = servedStatP('stat', realStatP, realStatSync)
476
367
 
477
368
  fs.access = function access(path, mode, callback) {
478
- const cb = typeof mode === 'function' ? mode : callback
479
- const accessMode = typeof mode === 'function' ? undefined : mode
480
- const t = typeof cb === 'function' ? fsTarget(path) : null
481
- if (t?.mode === 'absent') { queueMicrotask(() => cb(enoent('access', path))); return }
482
- if (t?.mode === 'serve' && isReadOnlyAccessMode(accessMode) && t.state.getFsStatFamily(t.url) !== undefined) {
483
- queueMicrotask(() => cb(null)); return
484
- }
369
+ const [accessMode, cb] = cbArgs(mode, callback)
370
+ const t = typeof cb === 'function' ? classify(path) : null
371
+ if (t?.mode === 'absent') return later(cb, enoent('access', path))
372
+ if (isReadOnlyAccessMode(accessMode) && served(t)) return later(cb, null)
485
373
  return realAccess(path, mode, callback)
486
374
  }
487
375
 
488
376
  fs.promises.access = async function access(path, mode) {
489
- const t = fsTarget(path)
377
+ const t = classify(path)
490
378
  if (t?.mode === 'absent') throw enoent('access', path)
491
- if (t?.mode === 'serve' && isReadOnlyAccessMode(mode) && t.state.getFsStatFamily(t.url) !== undefined) return undefined
379
+ if (isReadOnlyAccessMode(mode) && served(t)) return undefined
492
380
  return realAccessP(path, mode)
493
381
  }
494
382
 
495
383
  const servedRealpath = (realFn) => function realpath(path, options, callback) {
496
- const cb = typeof options === 'function' ? options : callback
497
- const opts = typeof options === 'function' ? undefined : options
498
- const t = typeof cb === 'function' ? fsTarget(path) : null
499
- if (t?.mode === 'absent') { queueMicrotask(() => cb(enoent('realpath', path))); return }
500
- if (t?.mode === 'serve' && t.state.getFsStatFamily(t.url) !== undefined) {
501
- realFn(path, opts, (err, resolved) => (err ? cb(null, encodeRealpath(t.abs, opts)) : cb(null, resolved)))
502
- return
503
- }
384
+ const [opts, cb] = cbArgs(options, callback)
385
+ const t = typeof cb === 'function' ? classify(path) : null
386
+ if (t?.mode === 'absent') return later(cb, enoent('realpath', path))
387
+ if (served(t)) return realFn(path, opts, (err, resolved) => cb(null, err ? encodeRealpath(t.abs, opts) : resolved))
504
388
  return realFn(path, options, callback)
505
389
  }
506
390
  fs.realpath = servedRealpath(realRealpath)
507
391
  fs.realpath.native = servedRealpath(realRealpathNative)
508
392
 
509
393
  fs.promises.realpath = async function realpath(path, options) {
510
- const t = fsTarget(path)
394
+ const t = classify(path)
511
395
  if (t?.mode === 'absent') throw enoent('realpath', path)
512
- if (t?.mode === 'serve' && t.state.getFsStatFamily(t.url) !== undefined) {
396
+ if (served(t)) {
513
397
  try { return await realRealpathP(path, options) } catch { return encodeRealpath(t.abs, options) }
514
398
  }
515
399
  return realRealpathP(path, options)