@zenfs/core 0.12.1 → 0.12.2

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/dist/file.js CHANGED
@@ -191,7 +191,7 @@ export class PreloadFile extends File {
191
191
  this.stats = stats;
192
192
  this._buffer = _buffer;
193
193
  this._position = 0;
194
- this._dirty = false;
194
+ this.dirty = false;
195
195
  /*
196
196
  Note:
197
197
  This invariant is *not* maintained once the file starts getting modified.
@@ -203,7 +203,7 @@ export class PreloadFile extends File {
203
203
  if (isReadable(this.flag)) {
204
204
  throw new Error(`Size mismatch: buffer length ${_buffer.byteLength}, stats size ${this.stats.size}`);
205
205
  }
206
- this._dirty = true;
206
+ this.dirty = true;
207
207
  }
208
208
  /**
209
209
  * Get the underlying buffer for this file. Mutating not recommended and will mess up dirty tracking.
@@ -234,18 +234,18 @@ export class PreloadFile extends File {
234
234
  this._position = newPos;
235
235
  }
236
236
  async sync() {
237
- if (!this.isDirty()) {
237
+ if (!this.dirty) {
238
238
  return;
239
239
  }
240
240
  await this.fs.sync(this.path, this._buffer, this.stats);
241
- this._dirty = false;
241
+ this.dirty = false;
242
242
  }
243
243
  syncSync() {
244
- if (!this.isDirty()) {
244
+ if (!this.dirty) {
245
245
  return;
246
246
  }
247
247
  this.fs.syncSync(this.path, this._buffer, this.stats);
248
- this._dirty = false;
248
+ this.dirty = false;
249
249
  }
250
250
  async close() {
251
251
  await this.sync();
@@ -267,39 +267,32 @@ export class PreloadFile extends File {
267
267
  }
268
268
  /**
269
269
  * Asynchronous truncate.
270
- * @param len
270
+ * @param length
271
271
  */
272
- async truncate(len) {
273
- this.truncateSync(len);
274
- if (isSynchronous(this.flag)) {
275
- return this.sync();
276
- }
272
+ async truncate(length) {
273
+ this.truncateSync(length);
274
+ return this.sync();
277
275
  }
278
276
  /**
279
277
  * Synchronous truncate.
280
- * @param len
278
+ * @param length
281
279
  */
282
- truncateSync(len) {
283
- this._dirty = true;
280
+ truncateSync(length) {
281
+ this.dirty = true;
284
282
  if (!isWriteable(this.flag)) {
285
283
  throw new ErrnoError(Errno.EPERM, 'File not opened with a writeable mode.');
286
284
  }
287
285
  this.stats.mtimeMs = Date.now();
288
- if (len > this._buffer.length) {
289
- const buf = new Uint8Array(len - this._buffer.length);
290
- // Write will set stats.size for us.
291
- this.writeSync(buf, 0, buf.length, this._buffer.length);
292
- if (isSynchronous(this.flag)) {
293
- this.syncSync();
294
- }
286
+ if (length > this._buffer.length) {
287
+ const data = new Uint8Array(length - this._buffer.length);
288
+ // Write will set stats.size and handle syncing.
289
+ this.writeSync(data, 0, data.length, this._buffer.length);
295
290
  return;
296
291
  }
297
- this.stats.size = len;
298
- // Truncate buffer to 'len'.
299
- this._buffer = this._buffer.subarray(0, len);
300
- if (isSynchronous(this.flag)) {
301
- this.syncSync();
302
- }
292
+ this.stats.size = length;
293
+ // Truncate.
294
+ this._buffer = this._buffer.slice(0, length);
295
+ this.syncSync();
303
296
  }
304
297
  /**
305
298
  * Write buffer to the file.
@@ -332,7 +325,7 @@ export class PreloadFile extends File {
332
325
  * @returns bytes written
333
326
  */
334
327
  writeSync(buffer, offset = 0, length = this.stats.size, position = 0) {
335
- this._dirty = true;
328
+ this.dirty = true;
336
329
  position ?? (position = this.position);
337
330
  if (!isWriteable(this.flag)) {
338
331
  throw new ErrnoError(Errno.EPERM, 'File not opened with a writeable mode.');
@@ -356,11 +349,8 @@ export class PreloadFile extends File {
356
349
  this._buffer.set(slice, position);
357
350
  const bytesWritten = slice.byteLength;
358
351
  this.stats.mtimeMs = Date.now();
359
- if (isSynchronous(this.flag)) {
360
- this.syncSync();
361
- return bytesWritten;
362
- }
363
352
  this.position = position + bytesWritten;
353
+ this.syncSync();
364
354
  return bytesWritten;
365
355
  }
366
356
  /**
@@ -392,6 +382,7 @@ export class PreloadFile extends File {
392
382
  if (!isReadable(this.flag)) {
393
383
  throw new ErrnoError(Errno.EPERM, 'File not opened with a readable mode.');
394
384
  }
385
+ this.dirty = true;
395
386
  position ?? (position = this.position);
396
387
  let end = position + length;
397
388
  if (end > this.stats.size) {
@@ -400,6 +391,7 @@ export class PreloadFile extends File {
400
391
  this.stats.atimeMs = Date.now();
401
392
  this._position = end;
402
393
  const bytesRead = end - position;
394
+ this.syncSync();
403
395
  if (bytesRead == 0) {
404
396
  // No copy/read. Return immediatly for better performance
405
397
  return bytesRead;
@@ -419,7 +411,7 @@ export class PreloadFile extends File {
419
411
  * @param mode
420
412
  */
421
413
  chmodSync(mode) {
422
- this._dirty = true;
414
+ this.dirty = true;
423
415
  this.stats.chmod(mode);
424
416
  this.syncSync();
425
417
  }
@@ -437,7 +429,7 @@ export class PreloadFile extends File {
437
429
  * @param gid
438
430
  */
439
431
  chownSync(uid, gid) {
440
- this._dirty = true;
432
+ this.dirty = true;
441
433
  this.stats.chown(uid, gid);
442
434
  this.syncSync();
443
435
  }
@@ -445,27 +437,18 @@ export class PreloadFile extends File {
445
437
  this.utimesSync(atime, mtime);
446
438
  }
447
439
  utimesSync(atime, mtime) {
448
- this._dirty = true;
440
+ this.dirty = true;
449
441
  this.stats.atime = atime;
450
442
  this.stats.mtime = mtime;
451
443
  this.syncSync();
452
444
  }
453
- isDirty() {
454
- return this._dirty;
455
- }
456
- /**
457
- * Resets the dirty bit. Should only be called after a sync has completed successfully.
458
- */
459
- resetDirty() {
460
- this._dirty = false;
461
- }
462
445
  _setType(type) {
463
- this._dirty = true;
446
+ this.dirty = true;
464
447
  this.stats.mode = (this.stats.mode & ~S_IFMT) | type;
465
448
  return this.sync();
466
449
  }
467
450
  _setTypeSync(type) {
468
- this._dirty = true;
451
+ this.dirty = true;
469
452
  this.stats.mode = (this.stats.mode & ~S_IFMT) | type;
470
453
  this.syncSync();
471
454
  }
@@ -34,6 +34,23 @@ export interface FileSystemMetadata {
34
34
  * @default false
35
35
  */
36
36
  noAsyncCache: boolean;
37
+ /**
38
+ * The optimal block size to use with the file system
39
+ * @default 4096
40
+ */
41
+ blockSize?: number;
42
+ /**
43
+ * Total number of (file) nodes available
44
+ */
45
+ totalNodes?: number;
46
+ /**
47
+ * Number of free (file) nodes available
48
+ */
49
+ freeNodes?: number;
50
+ /**
51
+ * The type of the FS
52
+ */
53
+ type?: number;
37
54
  }
38
55
  /**
39
56
  * Structure for a filesystem. All ZenFS backends must extend this.
@@ -46,11 +63,16 @@ export interface FileSystemMetadata {
46
63
  * - All arguments are present. Any optional arguments at the Node API level have been passed in with their default values.
47
64
  */
48
65
  export declare abstract class FileSystem {
66
+ /**
67
+ * Numeric type, used for statfs
68
+ * @internal @protected
69
+ */
70
+ _type?: number;
49
71
  /**
50
72
  * Get metadata about the current file system
51
73
  */
52
74
  metadata(): FileSystemMetadata;
53
- constructor(options?: object);
75
+ constructor();
54
76
  ready(): Promise<void>;
55
77
  /**
56
78
  * Asynchronous rename. No arguments other than a possible exception
@@ -24,10 +24,10 @@ export class FileSystem {
24
24
  freeSpace: 0,
25
25
  noResizableBuffers: false,
26
26
  noAsyncCache: false,
27
+ type: this._type,
27
28
  };
28
29
  }
29
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
30
- constructor(options) { }
30
+ constructor() { }
31
31
  async ready() { }
32
32
  /**
33
33
  * Test whether or not the given path exists by checking with the file system.
package/dist/stats.d.ts CHANGED
@@ -57,11 +57,9 @@ export interface StatsLike<T extends number | bigint = number | bigint> {
57
57
  * Common code used by both Stats and BigIntStats.
58
58
  */
59
59
  export declare abstract class StatsCommon<T extends number | bigint> implements Node.StatsBase<T>, StatsLike {
60
- protected abstract _isBigint: boolean;
61
- protected get _typename(): string;
62
- protected get _typename_inverse(): string;
60
+ protected abstract _isBigint: T extends bigint ? true : false;
63
61
  protected _convert(arg: number | bigint | string | boolean): T;
64
- blocks: T;
62
+ get blocks(): T;
65
63
  /**
66
64
  * Unix-style file mode (e.g. 0o644) that includes the type of the item.
67
65
  * Type of the item can be FILE, DIRECTORY, SYMLINK, or SOCKET
@@ -184,27 +182,26 @@ export declare abstract class StatsCommon<T extends number | bigint> implements
184
182
  * Attribute descriptions are from `man 2 stat'
185
183
  * @see http://nodejs.org/api/fs.html#fs_class_fs_stats
186
184
  * @see http://man7.org/linux/man-pages/man2/stat.2.html
185
+ * @internal
187
186
  */
188
187
  export declare class Stats extends StatsCommon<number> implements Node.Stats, StatsLike {
189
- protected _isBigint: boolean;
190
- /**
191
- * Clones the stats object.
192
- * @deprecated use `new Stats(stats)`
193
- */
194
- static clone(stats: Stats): Stats;
188
+ protected _isBigint: false;
195
189
  }
196
190
  /**
197
191
  * Stats with bigint
198
192
  * @todo Implement with bigint instead of wrapping Stats
193
+ * @internal
199
194
  */
200
195
  export declare class BigIntStats extends StatsCommon<bigint> implements Node.BigIntStats, StatsLike {
201
- protected _isBigint: boolean;
202
- /**
203
- * Clone a stats object.
204
- * @deprecated use `new BigIntStats(stats)`
205
- */
206
- static clone(stats: BigIntStats | Stats): BigIntStats;
196
+ protected _isBigint: true;
207
197
  }
198
+ /**
199
+ * @internal
200
+ */
201
+ export declare const ZenFsType = 525687744115;
202
+ /**
203
+ * @hidden
204
+ */
208
205
  export declare class StatsFs implements Node.StatsFsBase<number> {
209
206
  /** Type of file system. */
210
207
  type: number;
@@ -221,6 +218,9 @@ export declare class StatsFs implements Node.StatsFsBase<number> {
221
218
  /** Free file nodes in file system. */
222
219
  ffree: number;
223
220
  }
221
+ /**
222
+ * @hidden
223
+ */
224
224
  export declare class BigIntStatsFs implements Node.StatsFsBase<bigint> {
225
225
  /** Type of file system. */
226
226
  type: bigint;
package/dist/stats.js CHANGED
@@ -1,4 +1,5 @@
1
- import { S_IFDIR, S_IFLNK, S_IFMT, S_IFREG, S_IRWXG, S_IRWXO, S_IRWXU } from './emulation/constants.js';
1
+ import { S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, S_IRWXG, S_IRWXO, S_IRWXU } from './emulation/constants.js';
2
+ import { size_max } from './inode.js';
2
3
  /**
3
4
  * Indicates the type of the given file. Applied to 'mode'.
4
5
  */
@@ -13,15 +14,12 @@ export var FileType;
13
14
  * Common code used by both Stats and BigIntStats.
14
15
  */
15
16
  export class StatsCommon {
16
- get _typename() {
17
- return this._isBigint ? 'bigint' : 'number';
18
- }
19
- get _typename_inverse() {
20
- return this._isBigint ? 'number' : 'bigint';
21
- }
22
17
  _convert(arg) {
23
18
  return (this._isBigint ? BigInt(arg) : Number(arg));
24
19
  }
20
+ get blocks() {
21
+ return this._convert(Math.ceil(Number(this.size) / 512));
22
+ }
25
23
  get atime() {
26
24
  return new Date(Number(this.atimeMs));
27
25
  }
@@ -78,16 +76,15 @@ export class StatsCommon {
78
76
  * group ID of owner
79
77
  */
80
78
  this.gid = this._convert(0);
81
- const currentTime = Date.now();
82
- const resolveT = (val, _default) => typeof val == this._typename ? val : this._convert(typeof val == this._typename_inverse ? val : _default);
83
- this.atimeMs = resolveT(atimeMs, currentTime);
84
- this.mtimeMs = resolveT(mtimeMs, currentTime);
85
- this.ctimeMs = resolveT(ctimeMs, currentTime);
86
- this.birthtimeMs = resolveT(birthtimeMs, currentTime);
87
- this.uid = resolveT(uid, 0);
88
- this.gid = resolveT(gid, 0);
89
- this.size = resolveT(size, 0);
90
- this.ino = resolveT(ino, 0);
79
+ const now = Date.now();
80
+ this.atimeMs = this._convert(atimeMs ?? now);
81
+ this.mtimeMs = this._convert(mtimeMs ?? now);
82
+ this.ctimeMs = this._convert(ctimeMs ?? now);
83
+ this.birthtimeMs = this._convert(birthtimeMs ?? now);
84
+ this.uid = this._convert(uid ?? 0);
85
+ this.gid = this._convert(gid ?? 0);
86
+ this.size = this._convert(size ?? 0);
87
+ this.ino = this._convert(ino ?? 0);
91
88
  const itemType = Number(mode) & S_IFMT || FileType.FILE;
92
89
  if (mode) {
93
90
  this.mode = this._convert(mode);
@@ -102,8 +99,6 @@ export class StatsCommon {
102
99
  this.mode = this._convert(0o777);
103
100
  }
104
101
  }
105
- // number of 512B blocks allocated
106
- this.blocks = this._convert(Math.ceil(Number(size) / 512));
107
102
  // Check if mode also includes top-most bits, which indicate the file's type.
108
103
  if ((this.mode & S_IFMT) == 0) {
109
104
  this.mode = (this.mode | this._convert(itemType));
@@ -129,16 +124,16 @@ export class StatsCommon {
129
124
  }
130
125
  // Currently unsupported
131
126
  isSocket() {
132
- return false;
127
+ return (this.mode & S_IFMT) === S_IFSOCK;
133
128
  }
134
129
  isBlockDevice() {
135
- return false;
130
+ return (this.mode & S_IFMT) === S_IFBLK;
136
131
  }
137
132
  isCharacterDevice() {
138
- return false;
133
+ return (this.mode & S_IFMT) === S_IFCHR;
139
134
  }
140
135
  isFIFO() {
141
- return false;
136
+ return (this.mode & S_IFMT) === S_IFIFO;
142
137
  }
143
138
  /**
144
139
  * Checks if a given user/group has access to this item
@@ -194,16 +189,16 @@ export class StatsCommon {
194
189
  }
195
190
  }
196
191
  get atimeNs() {
197
- return BigInt(this.atimeMs);
192
+ return BigInt(this.atimeMs) * 1000n;
198
193
  }
199
194
  get mtimeNs() {
200
- return BigInt(this.mtimeMs);
195
+ return BigInt(this.mtimeMs) * 1000n;
201
196
  }
202
197
  get ctimeNs() {
203
- return BigInt(this.ctimeMs);
198
+ return BigInt(this.ctimeMs) * 1000n;
204
199
  }
205
200
  get birthtimeNs() {
206
- return BigInt(this.birthtimeMs);
201
+ return BigInt(this.birthtimeMs) * 1000n;
207
202
  }
208
203
  }
209
204
  /**
@@ -212,44 +207,39 @@ export class StatsCommon {
212
207
  * Attribute descriptions are from `man 2 stat'
213
208
  * @see http://nodejs.org/api/fs.html#fs_class_fs_stats
214
209
  * @see http://man7.org/linux/man-pages/man2/stat.2.html
210
+ * @internal
215
211
  */
216
212
  export class Stats extends StatsCommon {
217
213
  constructor() {
218
214
  super(...arguments);
219
215
  this._isBigint = false;
220
216
  }
221
- /**
222
- * Clones the stats object.
223
- * @deprecated use `new Stats(stats)`
224
- */
225
- static clone(stats) {
226
- return new Stats(stats);
227
- }
228
217
  }
229
218
  Stats;
230
219
  /**
231
220
  * Stats with bigint
232
221
  * @todo Implement with bigint instead of wrapping Stats
222
+ * @internal
233
223
  */
234
224
  export class BigIntStats extends StatsCommon {
235
225
  constructor() {
236
226
  super(...arguments);
237
227
  this._isBigint = true;
238
228
  }
239
- /**
240
- * Clone a stats object.
241
- * @deprecated use `new BigIntStats(stats)`
242
- */
243
- static clone(stats) {
244
- return new BigIntStats(stats);
245
- }
246
229
  }
230
+ /**
231
+ * @internal
232
+ */
233
+ export const ZenFsType = 0x7a656e6673; // 'z' 'e' 'n' 'f' 's'
234
+ /**
235
+ * @hidden
236
+ */
247
237
  export class StatsFs {
248
238
  constructor() {
249
239
  /** Type of file system. */
250
- this.type = 0;
240
+ this.type = 0x7a656e6673;
251
241
  /** Optimal transfer block size. */
252
- this.bsize = 0;
242
+ this.bsize = 4096;
253
243
  /** Total data blocks in file system. */
254
244
  this.blocks = 0;
255
245
  /** Free blocks in file system. */
@@ -257,17 +247,20 @@ export class StatsFs {
257
247
  /** Available blocks for unprivileged users */
258
248
  this.bavail = 0;
259
249
  /** Total file nodes in file system. */
260
- this.files = 0;
250
+ this.files = size_max;
261
251
  /** Free file nodes in file system. */
262
- this.ffree = 0;
252
+ this.ffree = size_max;
263
253
  }
264
254
  }
255
+ /**
256
+ * @hidden
257
+ */
265
258
  export class BigIntStatsFs {
266
259
  constructor() {
267
260
  /** Type of file system. */
268
- this.type = 0n;
261
+ this.type = 0x7a656e6673n;
269
262
  /** Optimal transfer block size. */
270
- this.bsize = 0n;
263
+ this.bsize = 4096n;
271
264
  /** Total data blocks in file system. */
272
265
  this.blocks = 0n;
273
266
  /** Free blocks in file system. */
@@ -275,8 +268,8 @@ export class BigIntStatsFs {
275
268
  /** Available blocks for unprivileged users */
276
269
  this.bavail = 0n;
277
270
  /** Total file nodes in file system. */
278
- this.files = 0n;
271
+ this.files = BigInt(size_max);
279
272
  /** Free file nodes in file system. */
280
- this.ffree = 0n;
273
+ this.ffree = BigInt(size_max);
281
274
  }
282
275
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/core",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "description": "A filesystem in your browser",
5
5
  "main": "dist/index.js",
6
6
  "types": "src/index.ts",
@@ -201,7 +201,7 @@ export class UnlockedOverlayFS extends FileSystem {
201
201
  }
202
202
  // Create an OverlayFile.
203
203
  const file = this._readable.openFileSync(path, parseFlag('r'), cred);
204
- const stats = Stats.clone(file.statSync());
204
+ const stats = new Stats(file.statSync());
205
205
  const data = new Uint8Array(stats.size);
206
206
  file.readSync(data);
207
207
  return new PreloadFile(this, path, flag, stats, data);
@@ -2,7 +2,7 @@ import { Buffer } from 'buffer';
2
2
  import type * as fs from 'node:fs';
3
3
  import { ErrnoError, Errno } from '../error.js';
4
4
  import type { FileContents } from '../filesystem.js';
5
- import { BigIntStats, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js';
5
+ import { BigIntStats, type Stats } from '../stats.js';
6
6
  import { nop, normalizeMode, type Callback } from '../utils.js';
7
7
  import { R_OK } from './constants.js';
8
8
  import { Dirent, type Dir } from './dir.js';
@@ -882,14 +882,14 @@ export function cp(source: fs.PathLike, destination: fs.PathLike, opts: fs.CopyO
882
882
  }
883
883
  cp satisfies Omit<typeof fs.cp, '__promisify__'>;
884
884
 
885
- export function statfs(path: fs.PathLike, callback: Callback<[StatsFs]>): void;
886
- export function statfs(path: fs.PathLike, options: fs.StatFsOptions & { bigint?: false }, callback: Callback<[StatsFs]>): void;
887
- export function statfs(path: fs.PathLike, options: fs.StatFsOptions & { bigint: true }, callback: Callback<[BigIntStatsFs]>): void;
888
- export function statfs(path: fs.PathLike, options?: fs.StatFsOptions | Callback<[StatsFs]>, callback: Callback<[StatsFs]> | Callback<[BigIntStatsFs]> = nop): void {
885
+ export function statfs(path: fs.PathLike, callback: Callback<[fs.StatsFs]>): void;
886
+ export function statfs(path: fs.PathLike, options: fs.StatFsOptions & { bigint?: false }, callback: Callback<[fs.StatsFs]>): void;
887
+ export function statfs(path: fs.PathLike, options: fs.StatFsOptions & { bigint: true }, callback: Callback<[fs.BigIntStatsFs]>): void;
888
+ export function statfs(path: fs.PathLike, options?: fs.StatFsOptions | Callback<[fs.StatsFs]>, callback: Callback<[fs.StatsFs]> | Callback<[fs.BigIntStatsFs]> = nop): void {
889
889
  callback = typeof options === 'function' ? options : callback;
890
890
  promises
891
891
  .statfs(path, typeof options === 'function' ? undefined : options)
892
- .then(result => (callback as Callback<[StatsFs | BigIntStatsFs]>)(undefined, result))
892
+ .then(result => (callback as Callback<[fs.StatsFs | fs.BigIntStatsFs]>)(undefined, result))
893
893
  .catch(callback);
894
894
  }
895
895
  statfs satisfies Omit<typeof fs.statfs, '__promisify__'>;
@@ -5,4 +5,4 @@ export * as constants from './constants.js';
5
5
  export * from './streams.js';
6
6
  export * from './dir.js';
7
7
  export { mountObject, mounts, mount, umount } from './shared.js';
8
- export { Stats, BigIntStats, StatsFs } from '../stats.js';
8
+ export { Stats, StatsFs, BigIntStatsFs } from '../stats.js';
@@ -6,15 +6,15 @@ import type { Stream } from 'node:stream';
6
6
  import type { ReadableStream as TReadableStream } from 'node:stream/web';
7
7
  import type { Interface as ReadlineInterface } from 'readline';
8
8
  import type { ReadableStreamController } from 'stream/web';
9
- import { ErrnoError, Errno } from '../error.js';
9
+ import { Errno, ErrnoError } from '../error.js';
10
10
  import { ActionType, File, isAppendable, isReadable, isWriteable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js';
11
11
  import type { FileContents } from '../filesystem.js';
12
- import { BigIntStats, FileType, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js';
12
+ import { BigIntStats, FileType, type Stats } from '../stats.js';
13
13
  import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js';
14
14
  import * as constants from './constants.js';
15
15
  import { Dir, Dirent } from './dir.js';
16
16
  import { dirname, join, parse } from './path.js';
17
- import { cred, fd2file, fdMap, file2fd, fixError, mounts, resolveMount } from './shared.js';
17
+ import { _statfs, cred, fd2file, fdMap, file2fd, fixError, mounts, resolveMount } from './shared.js';
18
18
  import { ReadStream, WriteStream } from './streams.js';
19
19
  export * as constants from './constants.js';
20
20
 
@@ -491,52 +491,41 @@ async function _open(path: fs.PathLike, _flag: fs.OpenMode, _mode: fs.Mode = 0o6
491
491
  path = resolveSymlinks && (await exists(path)) ? await realpath(path) : path;
492
492
  const { fs, path: resolved } = resolveMount(path);
493
493
 
494
- try {
495
- switch (pathExistsAction(flag)) {
494
+ if (!(await fs.exists(path, cred))) {
495
+ switch (pathNotExistsAction(flag)) {
496
+ case ActionType.CREATE:
497
+ // Ensure parent exists.
498
+ const parentStats: Stats = await fs.stat(dirname(resolved), cred);
499
+ if (parentStats && !parentStats.isDirectory()) {
500
+ throw ErrnoError.With('ENOTDIR', dirname(path), '_open');
501
+ }
502
+ return new FileHandle(await fs.createFile(resolved, flag, mode, cred));
496
503
  case ActionType.THROW:
497
- throw ErrnoError.With('EEXIST', path, '_open');
498
- case ActionType.TRUNCATE:
499
- /*
504
+ throw ErrnoError.With('ENOENT', path, '_open');
505
+ default:
506
+ throw new ErrnoError(Errno.EINVAL, 'Invalid file flag');
507
+ }
508
+ }
509
+
510
+ switch (pathExistsAction(flag)) {
511
+ case ActionType.THROW:
512
+ throw ErrnoError.With('EEXIST', path, '_open');
513
+ case ActionType.TRUNCATE:
514
+ /*
500
515
  In a previous implementation, we deleted the file and
501
516
  re-created it. However, this created a race condition if another
502
517
  asynchronous request was trying to read the file, as the file
503
518
  would not exist for a small period of time.
504
519
  */
505
- const file: File = await fs.openFile(resolved, flag, cred);
506
- await file.truncate(0);
507
- await file.sync();
508
- return new FileHandle(file);
509
- case ActionType.NOP:
510
- // Must await so thrown errors are caught by the catch below
511
- return new FileHandle(await fs.openFile(resolved, flag, cred));
512
- default:
513
- throw new ErrnoError(Errno.EINVAL, 'Invalid file flag');
514
- }
515
- } catch (_) {
516
- const original = _ as ErrnoError;
517
- if (original.code != 'ENOENT') {
518
- throw original;
519
- }
520
- try {
521
- switch (pathNotExistsAction(flag)) {
522
- case ActionType.CREATE:
523
- // Ensure parent exists.
524
- const parentStats: Stats = await fs.stat(dirname(resolved), cred);
525
- if (parentStats && !parentStats.isDirectory()) {
526
- throw ErrnoError.With('ENOTDIR', dirname(path), '_open');
527
- }
528
- return new FileHandle(await fs.createFile(resolved, flag, mode, cred));
529
- case ActionType.THROW:
530
- throw ErrnoError.With('ENOENT', path, '_open');
531
- default:
532
- throw new ErrnoError(Errno.EINVAL, 'Invalid file flag');
533
- }
534
- } catch (_) {
535
- const ex = _ as ErrnoError;
536
- ex.stack += '\n<original>\n';
537
- ex.stack += (original as Error).stack;
538
- throw ex;
539
- }
520
+ const file: File = await fs.openFile(resolved, flag, cred);
521
+ await file.truncate(0);
522
+ await file.sync();
523
+ return new FileHandle(file);
524
+ case ActionType.NOP:
525
+ // Must await so thrown errors are caught by the catch below
526
+ return new FileHandle(await fs.openFile(resolved, flag, cred));
527
+ default:
528
+ throw new ErrnoError(Errno.EINVAL, 'Invalid file flag');
540
529
  }
541
530
  }
542
531
 
@@ -1072,9 +1061,11 @@ cp satisfies typeof promises.cp;
1072
1061
  * @since v18.15.0
1073
1062
  * @return Fulfills with an {fs.StatFs} for the file system.
1074
1063
  */
1075
- export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions & { bigint?: false }): Promise<StatsFs>;
1076
- export async function statfs(path: fs.PathLike, opts: fs.StatFsOptions & { bigint: true }): Promise<BigIntStatsFs>;
1077
- export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<StatsFs | BigIntStatsFs>;
1078
- export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<StatsFs | BigIntStatsFs> {
1079
- throw ErrnoError.With('ENOSYS', path.toString(), 'statfs');
1064
+ export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions & { bigint?: false }): Promise<fs.StatsFs>;
1065
+ export async function statfs(path: fs.PathLike, opts: fs.StatFsOptions & { bigint: true }): Promise<fs.BigIntStatsFs>;
1066
+ export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<fs.StatsFs | fs.BigIntStatsFs>;
1067
+ export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<fs.StatsFs | fs.BigIntStatsFs> {
1068
+ path = normalizePath(path);
1069
+ const { fs } = resolveMount(path);
1070
+ return _statfs(fs, opts?.bigint);
1080
1071
  }