@zenfs/core 0.12.1 → 0.12.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.
Files changed (54) hide show
  1. package/dist/backends/backend.d.ts +1 -1
  2. package/dist/backends/fetch.d.ts +1 -1
  3. package/dist/backends/index/fs.d.ts +2 -1
  4. package/dist/backends/index/index.d.ts +2 -1
  5. package/dist/backends/overlay.d.ts +4 -3
  6. package/dist/backends/overlay.js +1 -1
  7. package/dist/backends/port/fs.d.ts +5 -1
  8. package/dist/backends/port/fs.js +13 -10
  9. package/dist/browser.min.js +4 -8
  10. package/dist/browser.min.js.map +3 -3
  11. package/dist/config.d.ts +1 -1
  12. package/dist/config.js +1 -1
  13. package/dist/cred.d.ts +2 -1
  14. package/dist/emulation/async.d.ts +6 -5
  15. package/dist/emulation/index.d.ts +1 -1
  16. package/dist/emulation/index.js +1 -1
  17. package/dist/emulation/promises.d.ts +5 -5
  18. package/dist/emulation/promises.js +31 -51
  19. package/dist/emulation/shared.d.ts +8 -2
  20. package/dist/emulation/shared.js +18 -1
  21. package/dist/emulation/streams.d.ts +1 -1
  22. package/dist/emulation/sync.d.ts +5 -5
  23. package/dist/emulation/sync.js +94 -76
  24. package/dist/error.d.ts +1 -1
  25. package/dist/error.js +1 -1
  26. package/dist/file.d.ts +7 -23
  27. package/dist/file.js +33 -80
  28. package/dist/filesystem.d.ts +35 -31
  29. package/dist/filesystem.js +10 -14
  30. package/dist/stats.d.ts +17 -17
  31. package/dist/stats.js +42 -49
  32. package/dist/utils.d.ts +2 -2
  33. package/license.md +2 -26
  34. package/package.json +2 -2
  35. package/readme.md +1 -1
  36. package/src/backends/backend.ts +1 -1
  37. package/src/backends/fetch.ts +1 -1
  38. package/src/backends/index/fs.ts +2 -1
  39. package/src/backends/index/index.ts +2 -1
  40. package/src/backends/overlay.ts +7 -4
  41. package/src/backends/port/fs.ts +14 -10
  42. package/src/config.ts +1 -1
  43. package/src/cred.ts +2 -1
  44. package/src/emulation/async.ts +8 -7
  45. package/src/emulation/index.ts +1 -1
  46. package/src/emulation/promises.ts +40 -54
  47. package/src/emulation/shared.ts +24 -3
  48. package/src/emulation/streams.ts +1 -1
  49. package/src/emulation/sync.ts +100 -87
  50. package/src/error.ts +1 -1
  51. package/src/file.ts +35 -88
  52. package/src/filesystem.ts +40 -32
  53. package/src/stats.ts +47 -59
  54. package/src/utils.ts +2 -2
package/dist/config.d.ts CHANGED
@@ -6,7 +6,7 @@ import type { AbsolutePath } from './emulation/path.js';
6
6
  export type MountConfiguration<T extends Backend> = FilesystemOf<T> | BackendConfiguration<T> | T;
7
7
  /**
8
8
  * Retrieve a file system with the given configuration.
9
- * @param config A BackendConfig object.
9
+ * @see MountConfiguration
10
10
  */
11
11
  export declare function resolveMountConfig<T extends Backend>(config: MountConfiguration<T>, _depth?: number): Promise<FilesystemOf<T>>;
12
12
  type ConfigMounts = {
package/dist/config.js CHANGED
@@ -8,7 +8,7 @@ function isMountConfig(arg) {
8
8
  }
9
9
  /**
10
10
  * Retrieve a file system with the given configuration.
11
- * @param config A BackendConfig object.
11
+ * @see MountConfiguration
12
12
  */
13
13
  export async function resolveMountConfig(config, _depth = 0) {
14
14
  if (typeof config !== 'object' || config == null) {
package/dist/cred.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Credentials used for various operations.
3
- * Similar to Linux's cred struct. See https://github.com/torvalds/linux/blob/master/include/linux/cred.h
3
+ * Similar to Linux's cred struct.
4
+ * @see https://github.com/torvalds/linux/blob/master/include/linux/cred.h
4
5
  */
5
6
  export interface Cred {
6
7
  uid: number;
@@ -4,9 +4,10 @@
4
4
  import { Buffer } from 'buffer';
5
5
  import type * as fs from 'node:fs';
6
6
  import type { FileContents } from '../filesystem.js';
7
- import { BigIntStats, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js';
7
+ import { BigIntStats, type Stats } from '../stats.js';
8
8
  import { type Callback } from '../utils.js';
9
- import { Dirent, type Dir } from './dir.js';
9
+ import type { Dirent } from './dir.js';
10
+ import { type Dir } from './dir.js';
10
11
  import * as promises from './promises.js';
11
12
  import { ReadStream, WriteStream } from './streams.js';
12
13
  /**
@@ -441,12 +442,12 @@ export declare function opendir(path: fs.PathLike, cb: Callback<[Dir]>): void;
441
442
  export declare function opendir(path: fs.PathLike, options: fs.OpenDirOptions, cb: Callback<[Dir]>): void;
442
443
  export declare function cp(source: fs.PathLike, destination: fs.PathLike, callback: Callback): void;
443
444
  export declare function cp(source: fs.PathLike, destination: fs.PathLike, opts: fs.CopyOptions, callback: Callback): void;
444
- export declare function statfs(path: fs.PathLike, callback: Callback<[StatsFs]>): void;
445
+ export declare function statfs(path: fs.PathLike, callback: Callback<[fs.StatsFs]>): void;
445
446
  export declare function statfs(path: fs.PathLike, options: fs.StatFsOptions & {
446
447
  bigint?: false;
447
- }, callback: Callback<[StatsFs]>): void;
448
+ }, callback: Callback<[fs.StatsFs]>): void;
448
449
  export declare function statfs(path: fs.PathLike, options: fs.StatFsOptions & {
449
450
  bigint: true;
450
- }, callback: Callback<[BigIntStatsFs]>): void;
451
+ }, callback: Callback<[fs.BigIntStatsFs]>): void;
451
452
  export declare function openAsBlob(path: fs.PathLike, options?: fs.OpenAsBlobOptions): Promise<Blob>;
452
453
  export {};
@@ -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';
@@ -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';
@@ -12,9 +12,9 @@ import type { CreateReadStreamOptions, CreateWriteStreamOptions, FileChangeInfo,
12
12
  import type { Stream } from 'node:stream';
13
13
  import type { ReadableStream as TReadableStream } from 'node:stream/web';
14
14
  import type { Interface as ReadlineInterface } from 'readline';
15
- import { File } from '../file.js';
15
+ import type { File } from '../file.js';
16
16
  import type { FileContents } from '../filesystem.js';
17
- import { BigIntStats, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js';
17
+ import { BigIntStats, type Stats } from '../stats.js';
18
18
  import { Dir, Dirent } from './dir.js';
19
19
  import { ReadStream, WriteStream } from './streams.js';
20
20
  export * as constants from './constants.js';
@@ -437,8 +437,8 @@ export declare function cp(source: fs.PathLike, destination: fs.PathLike, opts?:
437
437
  */
438
438
  export declare function statfs(path: fs.PathLike, opts?: fs.StatFsOptions & {
439
439
  bigint?: false;
440
- }): Promise<StatsFs>;
440
+ }): Promise<fs.StatsFs>;
441
441
  export declare function statfs(path: fs.PathLike, opts: fs.StatFsOptions & {
442
442
  bigint: true;
443
- }): Promise<BigIntStatsFs>;
444
- export declare function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<StatsFs | BigIntStatsFs>;
443
+ }): Promise<fs.BigIntStatsFs>;
444
+ export declare function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<fs.StatsFs | fs.BigIntStatsFs>;
@@ -1,12 +1,12 @@
1
1
  import { Buffer } from 'buffer';
2
- import { ErrnoError, Errno } from '../error.js';
3
- import { ActionType, isAppendable, isReadable, isWriteable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js';
2
+ import { Errno, ErrnoError } from '../error.js';
3
+ import { isAppendable, isExclusive, isReadable, isTruncating, isWriteable, parseFlag } from '../file.js';
4
4
  import { BigIntStats, FileType } from '../stats.js';
5
5
  import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js';
6
6
  import * as constants from './constants.js';
7
7
  import { Dir, Dirent } from './dir.js';
8
8
  import { dirname, join, parse } from './path.js';
9
- import { cred, fd2file, fdMap, file2fd, fixError, mounts, resolveMount } from './shared.js';
9
+ import { _statfs, cred, fd2file, fdMap, file2fd, fixError, mounts, resolveMount } from './shared.js';
10
10
  import { ReadStream, WriteStream } from './streams.js';
11
11
  export * as constants from './constants.js';
12
12
  export class FileHandle {
@@ -83,7 +83,7 @@ export class FileHandle {
83
83
  throw new ErrnoError(Errno.EINVAL, 'Encoding not specified');
84
84
  }
85
85
  const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data;
86
- await this.file.write(encodedData, 0, encodedData.length, null);
86
+ await this.file.write(encodedData, 0, encodedData.length);
87
87
  }
88
88
  /**
89
89
  * Asynchronously reads data from the file.
@@ -386,55 +386,33 @@ async function _open(path, _flag, _mode = 0o644, resolveSymlinks) {
386
386
  const mode = normalizeMode(_mode, 0o644), flag = parseFlag(_flag);
387
387
  path = resolveSymlinks && (await exists(path)) ? await realpath(path) : path;
388
388
  const { fs, path: resolved } = resolveMount(path);
389
- try {
390
- switch (pathExistsAction(flag)) {
391
- case ActionType.THROW:
392
- throw ErrnoError.With('EEXIST', path, '_open');
393
- case ActionType.TRUNCATE:
394
- /*
395
- In a previous implementation, we deleted the file and
396
- re-created it. However, this created a race condition if another
397
- asynchronous request was trying to read the file, as the file
398
- would not exist for a small period of time.
399
- */
400
- const file = await fs.openFile(resolved, flag, cred);
401
- await file.truncate(0);
402
- await file.sync();
403
- return new FileHandle(file);
404
- case ActionType.NOP:
405
- // Must await so thrown errors are caught by the catch below
406
- return new FileHandle(await fs.openFile(resolved, flag, cred));
407
- default:
408
- throw new ErrnoError(Errno.EINVAL, 'Invalid file flag');
409
- }
410
- }
411
- catch (_) {
412
- const original = _;
413
- if (original.code != 'ENOENT') {
414
- throw original;
389
+ if (!(await fs.exists(path, cred))) {
390
+ if ((!isWriteable(flag) && !isAppendable(flag)) || flag == 'r+') {
391
+ throw ErrnoError.With('ENOENT', path, '_open');
415
392
  }
416
- try {
417
- switch (pathNotExistsAction(flag)) {
418
- case ActionType.CREATE:
419
- // Ensure parent exists.
420
- const parentStats = await fs.stat(dirname(resolved), cred);
421
- if (parentStats && !parentStats.isDirectory()) {
422
- throw ErrnoError.With('ENOTDIR', dirname(path), '_open');
423
- }
424
- return new FileHandle(await fs.createFile(resolved, flag, mode, cred));
425
- case ActionType.THROW:
426
- throw ErrnoError.With('ENOENT', path, '_open');
427
- default:
428
- throw new ErrnoError(Errno.EINVAL, 'Invalid file flag');
429
- }
430
- }
431
- catch (_) {
432
- const ex = _;
433
- ex.stack += '\n<original>\n';
434
- ex.stack += original.stack;
435
- throw ex;
393
+ // Create the file
394
+ const parentStats = await fs.stat(dirname(resolved), cred);
395
+ if (parentStats && !parentStats.isDirectory()) {
396
+ throw ErrnoError.With('ENOTDIR', dirname(path), '_open');
436
397
  }
398
+ return new FileHandle(await fs.createFile(resolved, flag, mode, cred));
399
+ }
400
+ if (isExclusive(flag)) {
401
+ throw ErrnoError.With('EEXIST', path, '_open');
437
402
  }
403
+ if (!isTruncating(flag)) {
404
+ return new FileHandle(await fs.openFile(resolved, flag, cred));
405
+ }
406
+ /*
407
+ In a previous implementation, we deleted the file and
408
+ re-created it. However, this created a race condition if another
409
+ asynchronous request was trying to read the file, as the file
410
+ would not exist for a small period of time.
411
+ */
412
+ const file = await fs.openFile(resolved, flag, cred);
413
+ await file.truncate(0);
414
+ await file.sync();
415
+ return new FileHandle(file);
438
416
  }
439
417
  /**
440
418
  * Asynchronous file open.
@@ -859,5 +837,7 @@ export async function cp(source, destination, opts) {
859
837
  }
860
838
  cp;
861
839
  export async function statfs(path, opts) {
862
- throw ErrnoError.With('ENOSYS', path.toString(), 'statfs');
840
+ path = normalizePath(path);
841
+ const { fs } = resolveMount(path);
842
+ return _statfs(fs, opts?.bigint);
863
843
  }
@@ -1,6 +1,8 @@
1
- import { Cred } from '../cred.js';
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import type { BigIntStatsFs, StatsFs } from 'node:fs';
3
+ import type { Cred } from '../cred.js';
2
4
  import type { File } from '../file.js';
3
- import { FileSystem } from '../filesystem.js';
5
+ import type { FileSystem } from '../filesystem.js';
4
6
  import { type AbsolutePath } from './path.js';
5
7
  export declare let cred: Cred;
6
8
  export declare function setCred(val: Cred): void;
@@ -35,3 +37,7 @@ export declare function resolveMount(path: string): {
35
37
  export declare function fixPaths(text: string, paths: Record<string, string>): string;
36
38
  export declare function fixError<E extends Error>(e: E, paths: Record<string, string>): E;
37
39
  export declare function mountObject(mounts: MountObject): void;
40
+ /**
41
+ * @hidden
42
+ */
43
+ export declare function _statfs<const T extends boolean>(fs: FileSystem, bigint?: T): T extends true ? BigIntStatsFs : StatsFs;
@@ -1,7 +1,8 @@
1
1
  // Utilities and shared data
2
- import { ErrnoError, Errno } from '../error.js';
3
2
  import { InMemory } from '../backends/memory.js';
4
3
  import { rootCred } from '../cred.js';
4
+ import { Errno, ErrnoError } from '../error.js';
5
+ import { size_max } from '../inode.js';
5
6
  import { normalizePath } from '../utils.js';
6
7
  import { resolve } from './path.js';
7
8
  // credentials
@@ -100,3 +101,19 @@ export function mountObject(mounts) {
100
101
  mount(point, fs);
101
102
  }
102
103
  }
104
+ /**
105
+ * @hidden
106
+ */
107
+ export function _statfs(fs, bigint) {
108
+ const md = fs.metadata();
109
+ const bs = md.blockSize || 4096;
110
+ return {
111
+ type: (bigint ? BigInt : Number)(md.type),
112
+ bsize: (bigint ? BigInt : Number)(bs),
113
+ ffree: (bigint ? BigInt : Number)(md.freeNodes || size_max),
114
+ files: (bigint ? BigInt : Number)(md.totalNodes || size_max),
115
+ bavail: (bigint ? BigInt : Number)(md.freeSpace / bs),
116
+ bfree: (bigint ? BigInt : Number)(md.freeSpace / bs),
117
+ blocks: (bigint ? BigInt : Number)(md.totalSpace / bs),
118
+ };
119
+ }
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" resolution-mode="require"/>
3
3
  import type * as Node from 'fs';
4
4
  import { Readable, Writable } from 'readable-stream';
5
- import { Callback } from '../utils.js';
5
+ import type { Callback } from '../utils.js';
6
6
  export declare class ReadStream extends Readable implements Node.ReadStream {
7
7
  close(callback?: Callback): void;
8
8
  bytesRead: number;
@@ -3,8 +3,8 @@
3
3
  /// <reference types="node" resolution-mode="require"/>
4
4
  import { Buffer } from 'buffer';
5
5
  import type * as fs from 'node:fs';
6
- import { FileContents } from '../filesystem.js';
7
- import { BigIntStats, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js';
6
+ import type { FileContents } from '../filesystem.js';
7
+ import { BigIntStats, type Stats } from '../stats.js';
8
8
  import { Dir, Dirent } from './dir.js';
9
9
  /**
10
10
  * Synchronous rename.
@@ -368,8 +368,8 @@ export declare function cpSync(source: fs.PathLike, destination: fs.PathLike, op
368
368
  */
369
369
  export declare function statfsSync(path: fs.PathLike, options?: fs.StatFsOptions & {
370
370
  bigint?: false;
371
- }): StatsFs;
371
+ }): fs.StatsFs;
372
372
  export declare function statfsSync(path: fs.PathLike, options: fs.StatFsOptions & {
373
373
  bigint: true;
374
- }): BigIntStatsFs;
375
- export declare function statfsSync(path: fs.PathLike, options?: fs.StatFsOptions): StatsFs | BigIntStatsFs;
374
+ }): fs.BigIntStatsFs;
375
+ export declare function statfsSync(path: fs.PathLike, options?: fs.StatFsOptions): fs.StatsFs | fs.BigIntStatsFs;
@@ -1,23 +1,12 @@
1
1
  import { Buffer } from 'buffer';
2
- import { ErrnoError, Errno } from '../error.js';
3
- import { ActionType, isAppendable, isReadable, isWriteable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js';
2
+ import { Errno, ErrnoError } from '../error.js';
3
+ import { isAppendable, isExclusive, isReadable, isTruncating, isWriteable, parseFlag } from '../file.js';
4
4
  import { BigIntStats, FileType } from '../stats.js';
5
5
  import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js';
6
6
  import { COPYFILE_EXCL, F_OK, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } from './constants.js';
7
7
  import { Dir, Dirent } from './dir.js';
8
8
  import { dirname, join, parse } from './path.js';
9
- import { cred, fd2file, fdMap, fixError, file2fd, mounts, resolveMount } from './shared.js';
10
- function wrap(...[name, resolveSymlinks, path, ...args]) {
11
- path = normalizePath(path);
12
- const { fs, path: resolvedPath } = resolveMount(resolveSymlinks && existsSync(path) ? realpathSync(path) : path);
13
- try {
14
- // @ts-expect-error 2556 (since ...args is not correctly picked up as being a tuple)
15
- return fs[name](resolvedPath, ...args);
16
- }
17
- catch (e) {
18
- throw fixError(e, { [resolvedPath]: path });
19
- }
20
- }
9
+ import { _statfs, cred, fd2file, fdMap, file2fd, fixError, mounts, resolveMount } from './shared.js';
21
10
  /**
22
11
  * Synchronous rename.
23
12
  * @param oldPath
@@ -60,13 +49,27 @@ export function existsSync(path) {
60
49
  }
61
50
  existsSync;
62
51
  export function statSync(path, options) {
63
- const stats = wrap('statSync', true, path.toString(), cred);
64
- return options?.bigint ? new BigIntStats(stats) : stats;
52
+ path = normalizePath(path);
53
+ const { fs, path: resolved } = resolveMount(existsSync(path) ? realpathSync(path) : path);
54
+ try {
55
+ const stats = fs.statSync(resolved, cred);
56
+ return options?.bigint ? new BigIntStats(stats) : stats;
57
+ }
58
+ catch (e) {
59
+ throw fixError(e, { [resolved]: path });
60
+ }
65
61
  }
66
62
  statSync;
67
63
  export function lstatSync(path, options) {
68
- const stats = wrap('statSync', false, path.toString(), cred);
69
- return options?.bigint ? new BigIntStats(stats) : stats;
64
+ path = normalizePath(path);
65
+ const { fs, path: resolved } = resolveMount(path);
66
+ try {
67
+ const stats = fs.statSync(resolved, cred);
68
+ return options?.bigint ? new BigIntStats(stats) : stats;
69
+ }
70
+ catch (e) {
71
+ throw fixError(e, { [resolved]: path });
72
+ }
70
73
  }
71
74
  lstatSync;
72
75
  /**
@@ -89,66 +92,51 @@ truncateSync;
89
92
  * @param path
90
93
  */
91
94
  export function unlinkSync(path) {
92
- return wrap('unlinkSync', false, path.toString(), cred);
93
- }
94
- unlinkSync;
95
- function _openSync(_path, _flag, _mode, resolveSymlinks = true) {
96
- const path = normalizePath(_path), mode = normalizeMode(_mode, 0o644), flag = parseFlag(_flag);
97
- // Check if the path exists, and is a file.
98
- let stats;
95
+ path = normalizePath(path);
96
+ const { fs, path: resolved } = resolveMount(path);
99
97
  try {
100
- stats = wrap('statSync', resolveSymlinks, path, cred);
98
+ return fs.unlinkSync(resolved, cred);
101
99
  }
102
- catch (_) {
103
- const original = _;
104
- if (original.code != 'ENOENT') {
105
- throw original;
106
- }
107
- try {
108
- // File does not exist.
109
- switch (pathNotExistsAction(flag)) {
110
- case ActionType.CREATE:
111
- // Ensure parent exists.
112
- const parentStats = wrap('statSync', resolveSymlinks, dirname(path), cred);
113
- if (!parentStats.isDirectory()) {
114
- throw ErrnoError.With('ENOTDIR', dirname(path), '_open');
115
- }
116
- return wrap('createFileSync', resolveSymlinks, path, flag, mode, cred);
117
- case ActionType.THROW:
118
- throw ErrnoError.With('ENOENT', path, '_open');
119
- default:
120
- throw new ErrnoError(Errno.EINVAL, 'Invalid FileFlag object.');
121
- }
100
+ catch (e) {
101
+ throw fixError(e, { [resolved]: path });
102
+ }
103
+ }
104
+ unlinkSync;
105
+ function _openSync(path, _flag, _mode, resolveSymlinks = true) {
106
+ path = normalizePath(path);
107
+ const mode = normalizeMode(_mode, 0o644), flag = parseFlag(_flag);
108
+ path = resolveSymlinks && existsSync(path) ? realpathSync(path) : path;
109
+ const { fs, path: resolved } = resolveMount(path);
110
+ if (!fs.existsSync(resolved, cred)) {
111
+ if ((!isWriteable(flag) && !isAppendable(flag)) || flag == 'r+') {
112
+ throw ErrnoError.With('ENOENT', path, '_open');
122
113
  }
123
- catch (_) {
124
- const ex = _;
125
- ex.stack += '\n<original>\n';
126
- ex.stack += original.stack;
127
- throw ex;
114
+ // Create the file
115
+ const parentStats = fs.statSync(dirname(resolved), cred);
116
+ if (!parentStats.isDirectory()) {
117
+ throw ErrnoError.With('ENOTDIR', dirname(path), '_open');
128
118
  }
119
+ return fs.createFileSync(resolved, flag, mode, cred);
129
120
  }
121
+ const stats = fs.statSync(resolved, cred);
130
122
  if (!stats.hasAccess(mode, cred)) {
131
123
  throw ErrnoError.With('EACCES', path, '_open');
132
124
  }
133
- // File exists.
134
- switch (pathExistsAction(flag)) {
135
- case ActionType.THROW:
136
- throw ErrnoError.With('EEXIST', path, '_open');
137
- case ActionType.TRUNCATE:
138
- // Delete file.
139
- wrap('unlinkSync', resolveSymlinks, path, cred);
140
- /*
141
- Create file. Use the same mode as the old file.
142
- Node itself modifies the ctime when this occurs, so this action
143
- will preserve that behavior if the underlying file system
144
- supports those properties.
145
- */
146
- return wrap('createFileSync', resolveSymlinks, path, flag, stats.mode, cred);
147
- case ActionType.NOP:
148
- return wrap('openFileSync', resolveSymlinks, path, flag, cred);
149
- default:
150
- throw new ErrnoError(Errno.EINVAL, 'Invalid FileFlag object.');
125
+ if (isExclusive(flag)) {
126
+ throw ErrnoError.With('EEXIST', path, '_open');
127
+ }
128
+ if (!isTruncating(flag)) {
129
+ return fs.openFileSync(resolved, flag, cred);
151
130
  }
131
+ // Delete file.
132
+ fs.unlinkSync(resolved, cred);
133
+ /*
134
+ Create file. Use the same mode as the old file.
135
+ Node itself modifies the ctime when this occurs, so this action
136
+ will preserve that behavior if the underlying file system
137
+ supports those properties.
138
+ */
139
+ return fs.createFileSync(resolved, flag, stats.mode, cred);
152
140
  }
153
141
  /**
154
142
  * Synchronous file open.
@@ -242,7 +230,7 @@ export function appendFileSync(filename, data, _options = {}) {
242
230
  const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
243
231
  const file = _openSync(typeof filename == 'number' ? fd2file(filename).path : filename.toString(), flag, options.mode, true);
244
232
  try {
245
- file.writeSync(encodedData, 0, encodedData.byteLength, null);
233
+ file.writeSync(encodedData, 0, encodedData.byteLength);
246
234
  }
247
235
  finally {
248
236
  file.closeSync();
@@ -367,18 +355,39 @@ futimesSync;
367
355
  * @param path
368
356
  */
369
357
  export function rmdirSync(path) {
370
- return wrap('rmdirSync', true, path.toString(), cred);
358
+ path = normalizePath(path);
359
+ const { fs, path: resolved } = resolveMount(existsSync(path) ? realpathSync(path) : path);
360
+ try {
361
+ fs.rmdirSync(resolved, cred);
362
+ }
363
+ catch (e) {
364
+ throw fixError(e, { [resolved]: path });
365
+ }
371
366
  }
372
367
  rmdirSync;
373
368
  export function mkdirSync(path, options) {
374
- const mode = typeof options == 'number' || typeof options == 'string' ? options : options?.mode;
369
+ const mode = normalizeMode(typeof options == 'number' || typeof options == 'string' ? options : options?.mode, 0o777);
375
370
  const recursive = typeof options == 'object' && options?.recursive;
376
- wrap('mkdirSync', true, path.toString(), normalizeMode(mode, 0o777), cred);
371
+ path = normalizePath(path);
372
+ const { fs, path: resolved } = resolveMount(existsSync(path) ? realpathSync(path) : path);
373
+ try {
374
+ return fs.mkdirSync(resolved, mode, cred);
375
+ }
376
+ catch (e) {
377
+ throw fixError(e, { [resolved]: path });
378
+ }
377
379
  }
378
380
  mkdirSync;
379
381
  export function readdirSync(path, options) {
380
382
  path = normalizePath(path);
381
- const entries = wrap('readdirSync', true, path, cred);
383
+ const { fs, path: resolved } = resolveMount(existsSync(path) ? realpathSync(path) : path);
384
+ let entries;
385
+ try {
386
+ entries = fs.readdirSync(resolved, cred);
387
+ }
388
+ catch (e) {
389
+ throw fixError(e, { [resolved]: path });
390
+ }
382
391
  for (const mount of mounts.keys()) {
383
392
  if (!mount.startsWith(path)) {
384
393
  continue;
@@ -408,8 +417,15 @@ readdirSync;
408
417
  * @param newpath
409
418
  */
410
419
  export function linkSync(existing, newpath) {
420
+ existing = normalizePath(existing);
411
421
  newpath = normalizePath(newpath);
412
- return wrap('linkSync', false, existing.toString(), newpath.toString(), cred);
422
+ const { fs, path: resolved } = resolveMount(existing);
423
+ try {
424
+ return fs.linkSync(resolved, newpath, cred);
425
+ }
426
+ catch (e) {
427
+ throw fixError(e, { [resolved]: existing });
428
+ }
413
429
  }
414
430
  linkSync;
415
431
  /**
@@ -685,5 +701,7 @@ export function cpSync(source, destination, opts) {
685
701
  }
686
702
  cpSync;
687
703
  export function statfsSync(path, options) {
688
- throw ErrnoError.With('ENOSYS', path.toString(), 'statfs');
704
+ path = normalizePath(path);
705
+ const { fs } = resolveMount(path);
706
+ return _statfs(fs, options?.bigint);
689
707
  }
package/dist/error.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * Standard libc error codes. More will be added to this enum and error strings as they are
4
4
  * needed.
5
- * @url https://en.wikipedia.org/wiki/Errno.h
5
+ * @see https://en.wikipedia.org/wiki/Errno.h
6
6
  */
7
7
  export declare enum Errno {
8
8
  /** Operation not permitted */
package/dist/error.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Standard libc error codes. More will be added to this enum and error strings as they are
3
3
  * needed.
4
- * @url https://en.wikipedia.org/wiki/Errno.h
4
+ * @see https://en.wikipedia.org/wiki/Errno.h
5
5
  */
6
6
  export var Errno;
7
7
  (function (Errno) {
package/dist/file.d.ts CHANGED
@@ -20,15 +20,6 @@ declare global {
20
20
  }): ArrayBuffer;
21
21
  }
22
22
  }
23
- /**
24
- * @hidden
25
- */
26
- export declare enum ActionType {
27
- NOP = 0,
28
- THROW = 1,
29
- TRUNCATE = 2,
30
- CREATE = 3
31
- }
32
23
  export declare function parseFlag(flag: string | number): string;
33
24
  export declare function flagToString(flag: number): string;
34
25
  export declare function flagToNumber(flag: string): number;
@@ -43,8 +34,6 @@ export declare function isTruncating(flag: string): boolean;
43
34
  export declare function isAppendable(flag: string): boolean;
44
35
  export declare function isSynchronous(flag: string): boolean;
45
36
  export declare function isExclusive(flag: string): boolean;
46
- export declare function pathExistsAction(flag: string): ActionType;
47
- export declare function pathNotExistsAction(flag: string): ActionType;
48
37
  export declare abstract class File {
49
38
  /**
50
39
  * Get the current file position.
@@ -101,7 +90,7 @@ export declare abstract class File {
101
90
  * the current position.
102
91
  * @returns Promise resolving to the new length of the buffer
103
92
  */
104
- abstract write(buffer: Uint8Array, offset?: number, length?: number, position?: number | null): Promise<number>;
93
+ abstract write(buffer: Uint8Array, offset?: number, length?: number, position?: number): Promise<number>;
105
94
  /**
106
95
  * Write buffer to the file.
107
96
  * Note that it is unsafe to use fs.writeSync multiple times on the same file
@@ -114,7 +103,7 @@ export declare abstract class File {
114
103
  * data should be written. If position is null, the data will be written at
115
104
  * the current position.
116
105
  */
117
- abstract writeSync(buffer: Uint8Array, offset?: number, length?: number, position?: number | null): number;
106
+ abstract writeSync(buffer: Uint8Array, offset?: number, length?: number, position?: number): number;
118
107
  /**
119
108
  * Read data from the file.
120
109
  * @param buffer The buffer that the data will be
@@ -204,7 +193,7 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
204
193
  readonly stats: Stats;
205
194
  protected _buffer: Uint8Array;
206
195
  protected _position: number;
207
- protected _dirty: boolean;
196
+ protected dirty: boolean;
208
197
  /**
209
198
  * Creates a file with the given path and, optionally, the given contents. Note
210
199
  * that, if contents is specified, it will be mutated by the file!
@@ -259,14 +248,14 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
259
248
  statSync(): Stats;
260
249
  /**
261
250
  * Asynchronous truncate.
262
- * @param len
251
+ * @param length
263
252
  */
264
- truncate(len: number): Promise<void>;
253
+ truncate(length: number): Promise<void>;
265
254
  /**
266
255
  * Synchronous truncate.
267
- * @param len
256
+ * @param length
268
257
  */
269
- truncateSync(len: number): void;
258
+ truncateSync(length: number): void;
270
259
  /**
271
260
  * Write buffer to the file.
272
261
  * Note that it is unsafe to use fs.write multiple times on the same file
@@ -345,11 +334,6 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
345
334
  chownSync(uid: number, gid: number): void;
346
335
  utimes(atime: Date, mtime: Date): Promise<void>;
347
336
  utimesSync(atime: Date, mtime: Date): void;
348
- protected isDirty(): boolean;
349
- /**
350
- * Resets the dirty bit. Should only be called after a sync has completed successfully.
351
- */
352
- protected resetDirty(): void;
353
337
  _setType(type: FileType): Promise<void>;
354
338
  _setTypeSync(type: FileType): void;
355
339
  }