@zenfs/core 0.9.6 → 0.9.7

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 (56) hide show
  1. package/dist/ApiError.d.ts +4 -3
  2. package/dist/ApiError.js +1 -1
  3. package/dist/backends/AsyncStore.d.ts +3 -2
  4. package/dist/backends/AsyncStore.js +12 -5
  5. package/dist/backends/InMemory.d.ts +1 -1
  6. package/dist/backends/Index.d.ts +7 -10
  7. package/dist/backends/Index.js +7 -5
  8. package/dist/backends/Overlay.js +1 -1
  9. package/dist/backends/SyncStore.d.ts +6 -6
  10. package/dist/backends/SyncStore.js +4 -4
  11. package/dist/backends/backend.d.ts +5 -4
  12. package/dist/backends/backend.js +2 -2
  13. package/dist/browser.min.js +4 -4
  14. package/dist/browser.min.js.map +3 -3
  15. package/dist/config.d.ts +1 -1
  16. package/dist/config.js +2 -2
  17. package/dist/emulation/async.d.ts +76 -77
  18. package/dist/emulation/async.js +42 -42
  19. package/dist/emulation/dir.js +6 -5
  20. package/dist/emulation/promises.d.ts +106 -102
  21. package/dist/emulation/promises.js +61 -65
  22. package/dist/emulation/shared.d.ts +1 -7
  23. package/dist/emulation/shared.js +1 -1
  24. package/dist/emulation/streams.js +3 -2
  25. package/dist/emulation/sync.d.ts +71 -64
  26. package/dist/emulation/sync.js +39 -40
  27. package/dist/file.d.ts +4 -4
  28. package/dist/file.js +7 -5
  29. package/dist/filesystem.d.ts +1 -1
  30. package/dist/filesystem.js +3 -0
  31. package/dist/mutex.js +2 -2
  32. package/dist/stats.d.ts +7 -7
  33. package/dist/stats.js +50 -10
  34. package/dist/utils.d.ts +5 -5
  35. package/dist/utils.js +4 -3
  36. package/package.json +3 -3
  37. package/readme.md +2 -2
  38. package/src/ApiError.ts +3 -1
  39. package/src/backends/AsyncStore.ts +14 -8
  40. package/src/backends/Index.ts +14 -10
  41. package/src/backends/Overlay.ts +3 -3
  42. package/src/backends/SyncStore.ts +8 -8
  43. package/src/backends/backend.ts +7 -5
  44. package/src/config.ts +5 -5
  45. package/src/emulation/async.ts +188 -196
  46. package/src/emulation/dir.ts +6 -6
  47. package/src/emulation/promises.ts +181 -173
  48. package/src/emulation/shared.ts +2 -9
  49. package/src/emulation/streams.ts +9 -8
  50. package/src/emulation/sync.ts +159 -159
  51. package/src/file.ts +11 -9
  52. package/src/filesystem.ts +11 -7
  53. package/src/mutex.ts +3 -3
  54. package/src/stats.ts +32 -23
  55. package/src/utils.ts +10 -9
  56. package/tsconfig.json +2 -1
@@ -1,32 +1,31 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  /// <reference types="node" resolution-mode="require"/>
3
+ /// <reference types="node" resolution-mode="require"/>
3
4
  import { Buffer } from 'buffer';
4
- import type * as Node from 'fs';
5
- import type { BufferEncodingOption, EncodingOption, ReadSyncOptions, symlink } from 'fs';
5
+ import type * as fs from 'node:fs';
6
6
  import { FileContents } from '../filesystem.js';
7
7
  import { BigIntStats, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js';
8
8
  import { Dir, Dirent } from './dir.js';
9
- import { PathLike } from './shared.js';
10
9
  /**
11
10
  * Synchronous rename.
12
11
  * @param oldPath
13
12
  * @param newPath
14
13
  */
15
- export declare function renameSync(oldPath: PathLike, newPath: PathLike): void;
14
+ export declare function renameSync(oldPath: fs.PathLike, newPath: fs.PathLike): void;
16
15
  /**
17
16
  * Test whether or not the given path exists by checking with the file system.
18
17
  * @param path
19
18
  */
20
- export declare function existsSync(path: PathLike): boolean;
19
+ export declare function existsSync(path: fs.PathLike): boolean;
21
20
  /**
22
21
  * Synchronous `stat`.
23
22
  * @param path
24
23
  * @returns Stats
25
24
  */
26
- export declare function statSync(path: PathLike, options?: {
27
- bigint?: false;
25
+ export declare function statSync(path: fs.PathLike, options?: {
26
+ bigint?: boolean;
28
27
  }): Stats;
29
- export declare function statSync(path: PathLike, options: {
28
+ export declare function statSync(path: fs.PathLike, options: {
30
29
  bigint: true;
31
30
  }): BigIntStats;
32
31
  /**
@@ -35,10 +34,10 @@ export declare function statSync(path: PathLike, options: {
35
34
  * then the link itself is stat-ed, not the file that it refers to.
36
35
  * @param path
37
36
  */
38
- export declare function lstatSync(path: PathLike, options?: {
39
- bigint?: false;
37
+ export declare function lstatSync(path: fs.PathLike, options?: {
38
+ bigint?: boolean;
40
39
  }): Stats;
41
- export declare function lstatSync(path: PathLike, options: {
40
+ export declare function lstatSync(path: fs.PathLike, options: {
42
41
  bigint: true;
43
42
  }): BigIntStats;
44
43
  /**
@@ -46,12 +45,12 @@ export declare function lstatSync(path: PathLike, options: {
46
45
  * @param path
47
46
  * @param len
48
47
  */
49
- export declare function truncateSync(path: PathLike, len?: number): void;
48
+ export declare function truncateSync(path: fs.PathLike, len?: number | null): void;
50
49
  /**
51
50
  * Synchronous `unlink`.
52
51
  * @param path
53
52
  */
54
- export declare function unlinkSync(path: PathLike): void;
53
+ export declare function unlinkSync(path: fs.PathLike): void;
55
54
  /**
56
55
  * Synchronous file open.
57
56
  * @see http://www.manpagez.com/man/2/open/
@@ -60,40 +59,40 @@ export declare function unlinkSync(path: PathLike): void;
60
59
  * @param mode Mode to use to open the file. Can be ignored if the
61
60
  * filesystem doesn't support permissions.
62
61
  */
63
- export declare function openSync(path: PathLike, flag: string, mode?: Node.Mode): number;
62
+ export declare function openSync(path: fs.PathLike, flag: fs.OpenMode, mode?: fs.Mode | null): number;
64
63
  /**
65
64
  * Opens a file or symlink
66
65
  * @internal
67
66
  */
68
- export declare function lopenSync(path: PathLike, flag: string, mode?: Node.Mode): number;
67
+ export declare function lopenSync(path: fs.PathLike, flag: string, mode?: fs.Mode | null): number;
69
68
  /**
70
69
  * Synchronously reads the entire contents of a file.
71
- * @param filename
70
+ * @param path
72
71
  * @param options
73
72
  * @option options encoding The string encoding for the file contents. Defaults to `null`.
74
73
  * @option options flag Defaults to `'r'`.
75
74
  * @returns file contents
76
75
  */
77
- export declare function readFileSync(filename: string, options?: {
76
+ export declare function readFileSync(path: fs.PathOrFileDescriptor, options?: {
78
77
  flag?: string;
79
- }): Buffer;
80
- export declare function readFileSync(filename: string, options: (Node.EncodingOption & {
78
+ } | null): Buffer;
79
+ export declare function readFileSync(path: fs.PathOrFileDescriptor, options?: (fs.EncodingOption & {
81
80
  flag?: string;
82
- }) | BufferEncoding): string;
81
+ }) | BufferEncoding | null): string;
83
82
  /**
84
83
  * Synchronously writes data to a file, replacing the file if it already
85
84
  * exists.
86
85
  *
87
86
  * The encoding option is ignored if data is a buffer.
88
- * @param filename
87
+ * @param path
89
88
  * @param data
90
89
  * @param options
91
90
  * @option options encoding Defaults to `'utf8'`.
92
91
  * @option options mode Defaults to `0644`.
93
92
  * @option options flag Defaults to `'w'`.
94
93
  */
95
- export declare function writeFileSync(filename: string, data: FileContents, options?: Node.WriteFileOptions): void;
96
- export declare function writeFileSync(filename: string, data: FileContents, encoding?: BufferEncoding): void;
94
+ export declare function writeFileSync(path: fs.PathOrFileDescriptor, data: FileContents, options?: fs.WriteFileOptions): void;
95
+ export declare function writeFileSync(path: fs.PathOrFileDescriptor, data: FileContents, encoding?: BufferEncoding): void;
97
96
  /**
98
97
  * Asynchronously append data to a file, creating the file if it not yet
99
98
  * exists.
@@ -105,7 +104,7 @@ export declare function writeFileSync(filename: string, data: FileContents, enco
105
104
  * @option options mode Defaults to `0644`.
106
105
  * @option options flag Defaults to `'a'`.
107
106
  */
108
- export declare function appendFileSync(filename: string, data: FileContents, _options?: Node.WriteFileOptions): void;
107
+ export declare function appendFileSync(filename: fs.PathOrFileDescriptor, data: FileContents, _options?: fs.WriteFileOptions): void;
109
108
  /**
110
109
  * Synchronous `fstat`.
111
110
  * `fstat()` is identical to `stat()`, except that the file to be stat-ed is
@@ -113,7 +112,7 @@ export declare function appendFileSync(filename: string, data: FileContents, _op
113
112
  * @param fd
114
113
  */
115
114
  export declare function fstatSync(fd: number, options?: {
116
- bigint?: false;
115
+ bigint?: boolean;
117
116
  }): Stats;
118
117
  export declare function fstatSync(fd: number, options: {
119
118
  bigint: true;
@@ -128,7 +127,7 @@ export declare function closeSync(fd: number): void;
128
127
  * @param fd
129
128
  * @param len
130
129
  */
131
- export declare function ftruncateSync(fd: number, len?: number): void;
130
+ export declare function ftruncateSync(fd: number, len?: number | null): void;
132
131
  /**
133
132
  * Synchronous fsync.
134
133
  * @param fd
@@ -152,8 +151,8 @@ export declare function fdatasyncSync(fd: number): void;
152
151
  * data should be written. If position is null, the data will be written at
153
152
  * the current position.
154
153
  */
155
- export declare function writeSync(fd: number, data: Uint8Array, offset: number, length: number, position?: number): number;
156
- export declare function writeSync(fd: number, data: string, position?: number, encoding?: BufferEncoding): number;
154
+ export declare function writeSync(fd: number, data: ArrayBufferView, offset?: number | null, length?: number | null, position?: number | null): number;
155
+ export declare function writeSync(fd: number, data: string, position?: number | null, encoding?: BufferEncoding | null): number;
157
156
  /**
158
157
  * Read data from the file specified by `fd`.
159
158
  * @param fd
@@ -166,8 +165,8 @@ export declare function writeSync(fd: number, data: string, position?: number, e
166
165
  * in the file. If position is null, data will be read from the current file
167
166
  * position.
168
167
  */
169
- export declare function readSync(fd: number, buffer: Uint8Array, opts?: ReadSyncOptions): number;
170
- export declare function readSync(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number): number;
168
+ export declare function readSync(fd: number, buffer: ArrayBufferView, opts?: fs.ReadSyncOptions): number;
169
+ export declare function readSync(fd: number, buffer: ArrayBufferView, offset: number, length: number, position?: fs.ReadPosition | null): number;
171
170
  /**
172
171
  * Synchronous `fchown`.
173
172
  * @param fd
@@ -193,93 +192,101 @@ export declare function futimesSync(fd: number, atime: string | number | Date, m
193
192
  * Synchronous `rmdir`.
194
193
  * @param path
195
194
  */
196
- export declare function rmdirSync(path: PathLike): void;
195
+ export declare function rmdirSync(path: fs.PathLike): void;
197
196
  /**
198
197
  * Synchronous `mkdir`.
199
198
  * @param path
200
199
  * @param mode defaults to o777
201
200
  * @todo Implement recursion
202
201
  */
203
- export declare function mkdirSync(path: PathLike, options: Node.MakeDirectoryOptions & {
202
+ export declare function mkdirSync(path: fs.PathLike, options: fs.MakeDirectoryOptions & {
204
203
  recursive: true;
205
- }): string;
206
- export declare function mkdirSync(path: PathLike, options?: Node.Mode | (Node.MakeDirectoryOptions & {
204
+ }): string | undefined;
205
+ export declare function mkdirSync(path: fs.PathLike, options?: fs.Mode | (fs.MakeDirectoryOptions & {
207
206
  recursive?: false;
208
- })): void;
207
+ }) | null): void;
208
+ export declare function mkdirSync(path: fs.PathLike, options?: fs.Mode | fs.MakeDirectoryOptions | null): string | undefined;
209
209
  /**
210
210
  * Synchronous `readdir`. Reads the contents of a directory.
211
211
  * @param path
212
212
  */
213
- export declare function readdirSync(path: PathLike, options?: {
214
- encoding?: BufferEncoding;
213
+ export declare function readdirSync(path: fs.PathLike, options?: {
214
+ recursive?: boolean;
215
+ encoding?: BufferEncoding | null;
215
216
  withFileTypes?: false;
216
- } | BufferEncoding): string[];
217
- export declare function readdirSync(path: PathLike, options: {
217
+ } | BufferEncoding | null): string[];
218
+ export declare function readdirSync(path: fs.PathLike, options: {
219
+ recursive?: boolean;
218
220
  encoding: 'buffer';
219
221
  withFileTypes?: false;
220
222
  } | 'buffer'): Buffer[];
221
- export declare function readdirSync(path: PathLike, options: {
223
+ export declare function readdirSync(path: fs.PathLike, options: {
224
+ recursive?: boolean;
222
225
  withFileTypes: true;
223
226
  }): Dirent[];
227
+ export declare function readdirSync(path: fs.PathLike, options?: (fs.ObjectEncodingOptions & {
228
+ withFileTypes?: false;
229
+ recursive?: boolean;
230
+ }) | BufferEncoding | null): string[] | Buffer[];
224
231
  /**
225
232
  * Synchronous `link`.
226
233
  * @param existing
227
234
  * @param newpath
228
235
  */
229
- export declare function linkSync(existing: PathLike, newpath: PathLike): void;
236
+ export declare function linkSync(existing: fs.PathLike, newpath: fs.PathLike): void;
230
237
  /**
231
238
  * Synchronous `symlink`.
232
239
  * @param target target path
233
240
  * @param path link path
234
241
  * @param type can be either `'dir'` or `'file'` (default is `'file'`)
235
242
  */
236
- export declare function symlinkSync(target: PathLike, path: PathLike, type?: symlink.Type): void;
243
+ export declare function symlinkSync(target: fs.PathLike, path: fs.PathLike, type?: fs.symlink.Type | null): void;
237
244
  /**
238
245
  * Synchronous readlink.
239
246
  * @param path
240
247
  */
241
- export declare function readlinkSync(path: PathLike, options?: BufferEncodingOption): Buffer;
242
- export declare function readlinkSync(path: PathLike, options: EncodingOption | BufferEncoding): string;
248
+ export declare function readlinkSync(path: fs.PathLike, options?: fs.BufferEncodingOption): Buffer;
249
+ export declare function readlinkSync(path: fs.PathLike, options: fs.EncodingOption | BufferEncoding): string;
243
250
  /**
244
251
  * Synchronous `chown`.
245
252
  * @param path
246
253
  * @param uid
247
254
  * @param gid
248
255
  */
249
- export declare function chownSync(path: PathLike, uid: number, gid: number): void;
256
+ export declare function chownSync(path: fs.PathLike, uid: number, gid: number): void;
250
257
  /**
251
258
  * Synchronous `lchown`.
252
259
  * @param path
253
260
  * @param uid
254
261
  * @param gid
255
262
  */
256
- export declare function lchownSync(path: PathLike, uid: number, gid: number): void;
263
+ export declare function lchownSync(path: fs.PathLike, uid: number, gid: number): void;
257
264
  /**
258
265
  * Synchronous `chmod`.
259
266
  * @param path
260
267
  * @param mode
261
268
  */
262
- export declare function chmodSync(path: PathLike, mode: Node.Mode): void;
269
+ export declare function chmodSync(path: fs.PathLike, mode: fs.Mode): void;
263
270
  /**
264
271
  * Synchronous `lchmod`.
265
272
  * @param path
266
273
  * @param mode
267
274
  */
268
- export declare function lchmodSync(path: PathLike, mode: number | string): void;
275
+ export declare function lchmodSync(path: fs.PathLike, mode: number | string): void;
269
276
  /**
270
277
  * Change file timestamps of the file referenced by the supplied path.
271
278
  * @param path
272
279
  * @param atime
273
280
  * @param mtime
274
281
  */
275
- export declare function utimesSync(path: PathLike, atime: string | number | Date, mtime: string | number | Date): void;
282
+ export declare function utimesSync(path: fs.PathLike, atime: string | number | Date, mtime: string | number | Date): void;
276
283
  /**
277
284
  * Change file timestamps of the file referenced by the supplied path.
278
285
  * @param path
279
286
  * @param atime
280
287
  * @param mtime
281
288
  */
282
- export declare function lutimesSync(path: PathLike, atime: string | number | Date, mtime: string | number | Date): void;
289
+ export declare function lutimesSync(path: fs.PathLike, atime: string | number | Date, mtime: string | number | Date): void;
283
290
  /**
284
291
  * Synchronous `realpath`.
285
292
  * @param path
@@ -288,27 +295,27 @@ export declare function lutimesSync(path: PathLike, atime: string | number | Dat
288
295
  * known real paths.
289
296
  * @returns the real path
290
297
  */
291
- export declare function realpathSync(path: PathLike, options: BufferEncodingOption): Buffer;
292
- export declare function realpathSync(path: PathLike, options?: EncodingOption): string;
298
+ export declare function realpathSync(path: fs.PathLike, options: fs.BufferEncodingOption): Buffer;
299
+ export declare function realpathSync(path: fs.PathLike, options?: fs.EncodingOption): string;
293
300
  /**
294
301
  * Synchronous `access`.
295
302
  * @param path
296
303
  * @param mode
297
304
  */
298
- export declare function accessSync(path: PathLike, mode?: number): void;
305
+ export declare function accessSync(path: fs.PathLike, mode?: number): void;
299
306
  /**
300
307
  * Synchronous `rm`. Removes files or directories (recursively).
301
308
  * @param path The path to the file or directory to remove.
302
309
  */
303
- export declare function rmSync(path: PathLike, options?: Node.RmOptions): void;
310
+ export declare function rmSync(path: fs.PathLike, options?: fs.RmOptions): void;
304
311
  /**
305
312
  * Synchronous `mkdtemp`. Creates a unique temporary directory.
306
313
  * @param prefix The directory prefix.
307
314
  * @param options The encoding (or an object including `encoding`).
308
315
  * @returns The path to the created temporary directory, encoded as a string or buffer.
309
316
  */
310
- export declare function mkdtempSync(prefix: string, options: BufferEncodingOption): Buffer;
311
- export declare function mkdtempSync(prefix: string, options?: EncodingOption): string;
317
+ export declare function mkdtempSync(prefix: string, options: fs.BufferEncodingOption): Buffer;
318
+ export declare function mkdtempSync(prefix: string, options?: fs.EncodingOption): string;
312
319
  /**
313
320
  * Synchronous `copyFile`. Copies a file.
314
321
  * @param src The source file.
@@ -316,7 +323,7 @@ export declare function mkdtempSync(prefix: string, options?: EncodingOption): s
316
323
  * @param flags Optional flags for the copy operation. Currently supports these flags:
317
324
  * * `fs.constants.COPYFILE_EXCL`: If the destination file already exists, the operation fails.
318
325
  */
319
- export declare function copyFileSync(src: PathLike, dest: PathLike, flags?: number): void;
326
+ export declare function copyFileSync(src: fs.PathLike, dest: fs.PathLike, flags?: number): void;
320
327
  /**
321
328
  * Synchronous `readv`. Reads from a file descriptor into multiple buffers.
322
329
  * @param fd The file descriptor.
@@ -324,7 +331,7 @@ export declare function copyFileSync(src: PathLike, dest: PathLike, flags?: numb
324
331
  * @param position The position in the file where to begin reading.
325
332
  * @returns The number of bytes read.
326
333
  */
327
- export declare function readvSync(fd: number, buffers: readonly Uint8Array[], position?: number): number;
334
+ export declare function readvSync(fd: number, buffers: readonly NodeJS.ArrayBufferView[], position?: number): number;
328
335
  /**
329
336
  * Synchronous `writev`. Writes from multiple buffers into a file descriptor.
330
337
  * @param fd The file descriptor.
@@ -332,14 +339,14 @@ export declare function readvSync(fd: number, buffers: readonly Uint8Array[], po
332
339
  * @param position The position in the file where to begin writing.
333
340
  * @returns The number of bytes written.
334
341
  */
335
- export declare function writevSync(fd: number, buffers: readonly Uint8Array[], position?: number): number;
342
+ export declare function writevSync(fd: number, buffers: readonly ArrayBufferView[], position?: number): number;
336
343
  /**
337
344
  * Synchronous `opendir`. Opens a directory.
338
345
  * @param path The path to the directory.
339
346
  * @param options Options for opening the directory.
340
347
  * @returns A `Dir` object representing the opened directory.
341
348
  */
342
- export declare function opendirSync(path: PathLike, options?: Node.OpenDirOptions): Dir;
349
+ export declare function opendirSync(path: fs.PathLike, options?: fs.OpenDirOptions): Dir;
343
350
  /**
344
351
  * Synchronous `cp`. Recursively copies a file or directory.
345
352
  * @param source The source file or directory.
@@ -352,17 +359,17 @@ export declare function opendirSync(path: PathLike, options?: Node.OpenDirOption
352
359
  * * `preserveTimestamps`: Preserve file timestamps.
353
360
  * * `recursive`: If `true`, copies directories recursively.
354
361
  */
355
- export declare function cpSync(source: PathLike, destination: PathLike, opts?: Node.CopySyncOptions): void;
362
+ export declare function cpSync(source: fs.PathLike, destination: fs.PathLike, opts?: fs.CopySyncOptions): void;
356
363
  /**
357
364
  * Synchronous statfs(2). Returns information about the mounted file system which contains path.
358
365
  * In case of an error, the err.code will be one of Common System Errors.
359
366
  * @param path A path to an existing file or directory on the file system to be queried.
360
367
  * @param callback
361
368
  */
362
- export declare function statfsSync(path: PathLike, options?: Node.StatFsOptions & {
369
+ export declare function statfsSync(path: fs.PathLike, options?: fs.StatFsOptions & {
363
370
  bigint?: false;
364
371
  }): StatsFs;
365
- export declare function statfsSync(path: PathLike, options: Node.StatFsOptions & {
372
+ export declare function statfsSync(path: fs.PathLike, options: fs.StatFsOptions & {
366
373
  bigint: true;
367
374
  }): BigIntStatsFs;
368
- export declare function statfsSync(path: PathLike, options?: Node.StatFsOptions): StatsFs | BigIntStatsFs;
375
+ export declare function statfsSync(path: fs.PathLike, options?: fs.StatFsOptions): StatsFs | BigIntStatsFs;
@@ -3,10 +3,10 @@ import { ApiError, ErrorCode } from '../ApiError.js';
3
3
  import { ActionType, isAppendable, isReadable, isWriteable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js';
4
4
  import { BigIntStats, FileType } from '../stats.js';
5
5
  import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js';
6
- import { COPYFILE_EXCL, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } from './constants.js';
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, getFdForFile, mounts, resolveMount } from './shared.js';
9
+ import { cred, fd2file, fdMap, fixError, file2fd, mounts, resolveMount } from './shared.js';
10
10
  function doOp(...[name, resolveSymlinks, path, ...args]) {
11
11
  path = normalizePath(path);
12
12
  const { fs, path: resolvedPath } = resolveMount(resolveSymlinks && existsSync(path) ? realpathSync(path) : path);
@@ -60,12 +60,12 @@ export function existsSync(path) {
60
60
  }
61
61
  existsSync;
62
62
  export function statSync(path, options) {
63
- const stats = doOp('statSync', true, path, cred);
63
+ const stats = doOp('statSync', true, path.toString(), cred);
64
64
  return options?.bigint ? new BigIntStats(stats) : stats;
65
65
  }
66
66
  statSync;
67
67
  export function lstatSync(path, options) {
68
- const stats = doOp('statSync', false, path, cred);
68
+ const stats = doOp('statSync', false, path.toString(), cred);
69
69
  return options?.bigint ? new BigIntStats(stats) : stats;
70
70
  }
71
71
  lstatSync;
@@ -89,10 +89,10 @@ truncateSync;
89
89
  * @param path
90
90
  */
91
91
  export function unlinkSync(path) {
92
- return doOp('unlinkSync', false, path, cred);
92
+ return doOp('unlinkSync', false, path.toString(), cred);
93
93
  }
94
94
  unlinkSync;
95
- function _openSync(_path, _flag, _mode, resolveSymlinks) {
95
+ function _openSync(_path, _flag, _mode, resolveSymlinks = true) {
96
96
  const path = normalizePath(_path), mode = normalizeMode(_mode, 0o644), flag = parseFlag(_flag);
97
97
  // Check if the path exists, and is a file.
98
98
  let stats;
@@ -146,8 +146,8 @@ function _openSync(_path, _flag, _mode, resolveSymlinks) {
146
146
  * @param mode Mode to use to open the file. Can be ignored if the
147
147
  * filesystem doesn't support permissions.
148
148
  */
149
- export function openSync(path, flag, mode) {
150
- return getFdForFile(_openSync(path, flag, mode, true));
149
+ export function openSync(path, flag, mode = F_OK) {
150
+ return file2fd(_openSync(path, flag, mode, true));
151
151
  }
152
152
  openSync;
153
153
  /**
@@ -155,7 +155,7 @@ openSync;
155
155
  * @internal
156
156
  */
157
157
  export function lopenSync(path, flag, mode) {
158
- return getFdForFile(_openSync(path, flag, mode, false));
158
+ return file2fd(_openSync(path, flag, mode, false));
159
159
  }
160
160
  /**
161
161
  * Synchronously reads the entire contents of a file.
@@ -175,13 +175,13 @@ function _readFileSync(fname, flag, resolveSymlinks) {
175
175
  file.closeSync();
176
176
  }
177
177
  }
178
- export function readFileSync(filename, arg2 = {}) {
179
- const options = normalizeOptions(arg2, null, 'r', 0o644);
178
+ export function readFileSync(path, _options = {}) {
179
+ const options = normalizeOptions(_options, null, 'r', 0o644);
180
180
  const flag = parseFlag(options.flag);
181
181
  if (!isReadable(flag)) {
182
182
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed to readFile must allow for reading.');
183
183
  }
184
- const data = Buffer.from(_readFileSync(filename, options.flag, true));
184
+ const data = Buffer.from(_readFileSync(typeof path == 'number' ? fd2file(path).path : path.toString(), options.flag, true));
185
185
  return options.encoding ? data.toString(options.encoding) : data;
186
186
  }
187
187
  readFileSync;
@@ -194,13 +194,13 @@ readFileSync;
194
194
  function _writeFileSync(fname, data, flag, mode, resolveSymlinks) {
195
195
  const file = _openSync(fname, flag, mode, resolveSymlinks);
196
196
  try {
197
- file.writeSync(data, 0, data.length, 0);
197
+ file.writeSync(data, 0, data.byteLength, 0);
198
198
  }
199
199
  finally {
200
200
  file.closeSync();
201
201
  }
202
202
  }
203
- export function writeFileSync(filename, data, _options) {
203
+ export function writeFileSync(path, data, _options = {}) {
204
204
  const options = normalizeOptions(_options, 'utf8', 'w+', 0o644);
205
205
  const flag = parseFlag(options.flag);
206
206
  if (!isWriteable(flag)) {
@@ -209,11 +209,11 @@ export function writeFileSync(filename, data, _options) {
209
209
  if (typeof data != 'string' && !options.encoding) {
210
210
  throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
211
211
  }
212
- const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data;
213
- if (encodedData === undefined) {
212
+ const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
213
+ if (!encodedData) {
214
214
  throw new ApiError(ErrorCode.EINVAL, 'Data not specified');
215
215
  }
216
- _writeFileSync(filename, encodedData, options.flag, options.mode, true);
216
+ _writeFileSync(typeof path == 'number' ? fd2file(path).path : path.toString(), encodedData, options.flag, options.mode, true);
217
217
  }
218
218
  writeFileSync;
219
219
  /**
@@ -223,7 +223,7 @@ writeFileSync;
223
223
  function _appendFileSync(fname, data, flag, mode, resolveSymlinks) {
224
224
  const file = _openSync(fname, flag, mode, resolveSymlinks);
225
225
  try {
226
- file.writeSync(data, 0, data.length, null);
226
+ file.writeSync(data, 0, data.byteLength, null);
227
227
  }
228
228
  finally {
229
229
  file.closeSync();
@@ -240,7 +240,7 @@ function _appendFileSync(fname, data, flag, mode, resolveSymlinks) {
240
240
  * @option options mode Defaults to `0644`.
241
241
  * @option options flag Defaults to `'a'`.
242
242
  */
243
- export function appendFileSync(filename, data, _options) {
243
+ export function appendFileSync(filename, data, _options = {}) {
244
244
  const options = normalizeOptions(_options, 'utf8', 'a', 0o644);
245
245
  const flag = parseFlag(options.flag);
246
246
  if (!isAppendable(flag)) {
@@ -249,8 +249,8 @@ export function appendFileSync(filename, data, _options) {
249
249
  if (typeof data != 'string' && !options.encoding) {
250
250
  throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
251
251
  }
252
- const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data;
253
- _appendFileSync(filename, encodedData, options.flag, options.mode, true);
252
+ const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
253
+ _appendFileSync(typeof filename == 'number' ? fd2file(filename).path : filename.toString(), encodedData, options.flag, options.mode, true);
254
254
  }
255
255
  appendFileSync;
256
256
  export function fstatSync(fd, options) {
@@ -273,6 +273,7 @@ closeSync;
273
273
  * @param len
274
274
  */
275
275
  export function ftruncateSync(fd, len = 0) {
276
+ len || (len = 0);
276
277
  if (len < 0) {
277
278
  throw new ApiError(ErrorCode.EINVAL);
278
279
  }
@@ -296,26 +297,24 @@ export function fdatasyncSync(fd) {
296
297
  }
297
298
  fdatasyncSync;
298
299
  export function writeSync(fd, data, posOrOff, lenOrEnc, pos) {
299
- let buffer, offset = 0, length, position;
300
+ let buffer, offset, length, position;
300
301
  if (typeof data === 'string') {
301
302
  // Signature 1: (fd, string, [position?, [encoding?]])
302
303
  position = typeof posOrOff === 'number' ? posOrOff : null;
303
304
  const encoding = (typeof lenOrEnc === 'string' ? lenOrEnc : 'utf8');
304
305
  offset = 0;
305
306
  buffer = Buffer.from(data, encoding);
306
- length = buffer.length;
307
+ length = buffer.byteLength;
307
308
  }
308
309
  else {
309
310
  // Signature 2: (fd, buffer, offset, length, position?)
310
- buffer = data;
311
+ buffer = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
311
312
  offset = posOrOff;
312
313
  length = lenOrEnc;
313
314
  position = typeof pos === 'number' ? pos : null;
314
315
  }
315
316
  const file = fd2file(fd);
316
- if (position === undefined || position === null) {
317
- position = file.position;
318
- }
317
+ position ?? (position = file.position);
319
318
  return file.writeSync(buffer, offset, length, position);
320
319
  }
321
320
  writeSync;
@@ -372,13 +371,13 @@ futimesSync;
372
371
  * @param path
373
372
  */
374
373
  export function rmdirSync(path) {
375
- return doOp('rmdirSync', true, path, cred);
374
+ return doOp('rmdirSync', true, path.toString(), cred);
376
375
  }
377
376
  rmdirSync;
378
377
  export function mkdirSync(path, options) {
379
378
  const mode = typeof options == 'number' || typeof options == 'string' ? options : options?.mode;
380
379
  const recursive = typeof options == 'object' && options?.recursive;
381
- doOp('mkdirSync', true, path, normalizeMode(mode, 0o777), cred);
380
+ doOp('mkdirSync', true, path.toString(), normalizeMode(mode, 0o777), cred);
382
381
  }
383
382
  mkdirSync;
384
383
  export function readdirSync(path, options) {
@@ -397,9 +396,9 @@ export function readdirSync(path, options) {
397
396
  }
398
397
  return entries.map((entry) => {
399
398
  if (typeof options == 'object' && options?.withFileTypes) {
400
- return new Dirent(entry, statSync(join(path, entry)));
399
+ return new Dirent(entry, statSync(join(path.toString(), entry)));
401
400
  }
402
- if (options == 'buffer' || (typeof options == 'object' && options.encoding == 'buffer')) {
401
+ if (options == 'buffer' || (typeof options == 'object' && options?.encoding == 'buffer')) {
403
402
  return Buffer.from(entry);
404
403
  }
405
404
  return entry;
@@ -414,7 +413,7 @@ readdirSync;
414
413
  */
415
414
  export function linkSync(existing, newpath) {
416
415
  newpath = normalizePath(newpath);
417
- return doOp('linkSync', false, existing, newpath, cred);
416
+ return doOp('linkSync', false, existing.toString(), newpath.toString(), cred);
418
417
  }
419
418
  linkSync;
420
419
  /**
@@ -428,16 +427,16 @@ export function symlinkSync(target, path, type = 'file') {
428
427
  throw new ApiError(ErrorCode.EINVAL, 'Invalid type: ' + type);
429
428
  }
430
429
  if (existsSync(path)) {
431
- throw ApiError.With('EEXIST', path, 'symlink');
430
+ throw ApiError.With('EEXIST', path.toString(), 'symlink');
432
431
  }
433
- writeFileSync(path, target);
432
+ writeFileSync(path, target.toString());
434
433
  const file = _openSync(path, 'r+', 0o644, false);
435
434
  file._setTypeSync(FileType.SYMLINK);
436
435
  }
437
436
  symlinkSync;
438
437
  export function readlinkSync(path, options) {
439
- const value = Buffer.from(_readFileSync(path, 'r', false));
440
- const encoding = typeof options == 'object' ? options.encoding : options;
438
+ const value = Buffer.from(_readFileSync(path.toString(), 'r', false));
439
+ const encoding = typeof options == 'object' ? options?.encoding : options;
441
440
  if (encoding == 'buffer') {
442
441
  return value;
443
442
  }
@@ -574,7 +573,7 @@ export function rmSync(path, options) {
574
573
  }
575
574
  rmSync;
576
575
  export function mkdtempSync(prefix, options) {
577
- const encoding = typeof options === 'object' ? options.encoding : options || 'utf8';
576
+ const encoding = typeof options === 'object' ? options?.encoding : options || 'utf8';
578
577
  const fsName = `${prefix}${Date.now()}-${Math.random().toString(36).slice(2)}`;
579
578
  const resolvedPath = '/tmp/' + fsName;
580
579
  mkdirSync(resolvedPath);
@@ -608,7 +607,7 @@ export function readvSync(fd, buffers, position) {
608
607
  const file = fd2file(fd);
609
608
  let bytesRead = 0;
610
609
  for (const buffer of buffers) {
611
- bytesRead += file.readSync(buffer, 0, buffer.length, position + bytesRead);
610
+ bytesRead += file.readSync(buffer, 0, buffer.byteLength, position + bytesRead);
612
611
  }
613
612
  return bytesRead;
614
613
  }
@@ -624,7 +623,7 @@ export function writevSync(fd, buffers, position) {
624
623
  const file = fd2file(fd);
625
624
  let bytesWritten = 0;
626
625
  for (const buffer of buffers) {
627
- bytesWritten += file.writeSync(buffer, 0, buffer.length, position + bytesWritten);
626
+ bytesWritten += file.writeSync(new Uint8Array(buffer.buffer), 0, buffer.byteLength, position + bytesWritten);
628
627
  }
629
628
  return bytesWritten;
630
629
  }
@@ -690,5 +689,5 @@ export function cpSync(source, destination, opts) {
690
689
  }
691
690
  cpSync;
692
691
  export function statfsSync(path, options) {
693
- throw ApiError.With('ENOSYS', path, 'statfs');
692
+ throw ApiError.With('ENOSYS', path.toString(), 'statfs');
694
693
  }