@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,9 +1,11 @@
1
1
  import { Buffer } from 'buffer';
2
- import type * as Node from 'node:fs';
2
+ import type * as fs from 'node:fs';
3
3
  import type * as promises from 'node:fs/promises';
4
4
  import type { CreateReadStreamOptions, CreateWriteStreamOptions, FileChangeInfo, FileReadResult, FlagAndOpenMode } from 'node:fs/promises';
5
+ import type { Stream } from 'node:stream';
5
6
  import type { ReadableStream as TReadableStream } from 'node:stream/web';
6
7
  import type { Interface as ReadlineInterface } from 'readline';
8
+ import type { ReadableStreamController } from 'stream/web';
7
9
  import { ApiError, ErrorCode } from '../ApiError.js';
8
10
  import { ActionType, File, isAppendable, isReadable, isWriteable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js';
9
11
  import { FileContents, FileSystem } from '../filesystem.js';
@@ -12,8 +14,7 @@ import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '.
12
14
  import * as constants from './constants.js';
13
15
  import { Dir, Dirent } from './dir.js';
14
16
  import { dirname, join, parse } from './path.js';
15
- import type { PathLike } from './shared.js';
16
- import { cred, fd2file, fdMap, fixError, getFdForFile, mounts, resolveMount } from './shared.js';
17
+ import { cred, fd2file, fdMap, file2fd, fixError, mounts, resolveMount } from './shared.js';
17
18
  import { ReadStream, WriteStream } from './streams.js';
18
19
  export * as constants from './constants.js';
19
20
 
@@ -43,7 +44,7 @@ export class FileHandle implements promises.FileHandle {
43
44
  * Asynchronous fchmod(2) - Change permissions of a file.
44
45
  * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
45
46
  */
46
- public chmod(mode: Node.Mode): Promise<void> {
47
+ public chmod(mode: fs.Mode): Promise<void> {
47
48
  const numMode = normalizeMode(mode, -1);
48
49
  if (numMode < 0) {
49
50
  throw new ApiError(ErrorCode.EINVAL, 'Invalid mode.');
@@ -69,7 +70,8 @@ export class FileHandle implements promises.FileHandle {
69
70
  * Asynchronous ftruncate(2) - Truncate a file to a specified length.
70
71
  * @param len If not specified, defaults to `0`.
71
72
  */
72
- public truncate(len?: number): Promise<void> {
73
+ public truncate(len?: number | null): Promise<void> {
74
+ len ||= 0;
73
75
  if (len < 0) {
74
76
  throw new ApiError(ErrorCode.EINVAL);
75
77
  }
@@ -95,7 +97,7 @@ export class FileHandle implements promises.FileHandle {
95
97
  * If `mode` is a string, it is parsed as an octal integer.
96
98
  * If `flag` is not supplied, the default of `'a'` is used.
97
99
  */
98
- public async appendFile(data: string | Uint8Array, _options?: (Node.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<void> {
100
+ public async appendFile(data: string | Uint8Array, _options: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding = {}): Promise<void> {
99
101
  const options = normalizeOptions(_options, 'utf8', 'a', 0o644);
100
102
  const flag = parseFlag(options.flag);
101
103
  if (!isAppendable(flag)) {
@@ -104,7 +106,7 @@ export class FileHandle implements promises.FileHandle {
104
106
  if (typeof data != 'string' && !options.encoding) {
105
107
  throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
106
108
  }
107
- const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data;
109
+ const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding!) : data;
108
110
  await this.file.write(encodedData, 0, encodedData.length, null);
109
111
  }
110
112
 
@@ -116,11 +118,11 @@ export class FileHandle implements promises.FileHandle {
116
118
  * @param length The number of bytes to read.
117
119
  * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position.
118
120
  */
119
- public read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<FileReadResult<TBuffer>> {
120
- if (isNaN(+position)) {
121
- position = this.file.position!;
121
+ public read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number | null): Promise<FileReadResult<TBuffer>> {
122
+ if (isNaN(+position!)) {
123
+ position = this.file.position;
122
124
  }
123
- return this.file.read(buffer, offset, length, position);
125
+ return this.file.read(buffer, offset, length, position!);
124
126
  }
125
127
 
126
128
  /**
@@ -129,9 +131,9 @@ export class FileHandle implements promises.FileHandle {
129
131
  * @param _options An object that may contain an optional flag.
130
132
  * If a flag is not provided, it defaults to `'r'`.
131
133
  */
132
- public async readFile(_options?: { flag?: Node.OpenMode }): Promise<Buffer>;
133
- public async readFile(_options: (Node.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<string>;
134
- public async readFile(_options?: (Node.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<string | Buffer> {
134
+ public async readFile(_options?: { flag?: fs.OpenMode }): Promise<Buffer>;
135
+ public async readFile(_options: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<string>;
136
+ public async readFile(_options: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding = {}): Promise<string | Buffer> {
135
137
  const options = normalizeOptions(_options, null, 'r', 0o444);
136
138
  const flag = parseFlag(options.flag);
137
139
  if (!isReadable(flag)) {
@@ -156,18 +158,18 @@ export class FileHandle implements promises.FileHandle {
156
158
  * @since v17.0.0
157
159
  * @experimental
158
160
  */
159
- public readableWebStream(options?: promises.ReadableWebStreamOptions): TReadableStream<Uint8Array> {
161
+ public readableWebStream(options: promises.ReadableWebStreamOptions = {}): TReadableStream<Uint8Array> {
160
162
  // Note: using an arrow function to preserve `this`
161
- const start = async ({ close, enqueue, error }) => {
163
+ const start = async ({ close, enqueue, error }: ReadableStreamController<Uint8Array>) => {
162
164
  try {
163
165
  const chunkSize = 64 * 1024,
164
166
  maxChunks = 1e7;
165
167
  let i = 0,
166
168
  position = 0,
167
- result: FileReadResult<Uint8Array>;
169
+ bytesRead = NaN;
168
170
 
169
- while (result.bytesRead > 0) {
170
- result = await this.read(new Uint8Array(chunkSize), 0, chunkSize, position);
171
+ while (bytesRead > 0) {
172
+ const result = await this.read(new Uint8Array(chunkSize), 0, chunkSize, position);
171
173
  if (!result.bytesRead) {
172
174
  close();
173
175
  return;
@@ -177,13 +179,14 @@ export class FileHandle implements promises.FileHandle {
177
179
  if (++i >= maxChunks) {
178
180
  throw new ApiError(ErrorCode.EFBIG, 'Too many iterations on readable stream', this.file.path, 'FileHandle.readableWebStream');
179
181
  }
182
+ bytesRead = result.bytesRead;
180
183
  }
181
184
  } catch (e) {
182
185
  error(e);
183
186
  }
184
187
  };
185
188
 
186
- return new globalThis.ReadableStream({ start, type: options.type });
189
+ return new (globalThis as any).ReadableStream({ start, type: options.type });
187
190
  }
188
191
 
189
192
  public readLines(options?: promises.CreateReadStreamOptions): ReadlineInterface {
@@ -197,40 +200,34 @@ export class FileHandle implements promises.FileHandle {
197
200
  /**
198
201
  * Asynchronous fstat(2) - Get file status.
199
202
  */
200
- public async stat(opts: Node.BigIntOptions): Promise<BigIntStats>;
201
- public async stat(opts?: Node.StatOptions & { bigint?: false }): Promise<Stats>;
202
- public async stat(opts?: Node.StatOptions): Promise<Stats | BigIntStats> {
203
+ public async stat(opts: fs.BigIntOptions): Promise<BigIntStats>;
204
+ public async stat(opts?: fs.StatOptions & { bigint?: false }): Promise<Stats>;
205
+ public async stat(opts?: fs.StatOptions): Promise<Stats | BigIntStats> {
203
206
  const stats = await this.file.stat();
204
207
  return opts?.bigint ? new BigIntStats(stats) : stats;
205
208
  }
206
209
 
207
- public async write(data: FileContents, posOrOff?: number, lenOrEnc?: BufferEncoding | number, position?: number): Promise<{ bytesWritten: number; buffer: FileContents }>;
208
-
209
- /**
210
- * Asynchronously writes `buffer` to the file.
211
- * The `FileHandle` must have been opened for writing.
212
- * @param buffer The buffer that the data will be written to.
213
- * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
214
- * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
215
- * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
216
- */
217
- public async write<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ bytesWritten: number; buffer: TBuffer }>;
218
-
219
210
  /**
220
211
  * Asynchronously writes `string` to the file.
221
212
  * The `FileHandle` must have been opened for writing.
222
213
  * It is unsafe to call `write()` multiple times on the same file without waiting for the `Promise`
223
214
  * to be resolved (or rejected). For this scenario, `fs.createWriteStream` is strongly recommended.
224
- * @param string A string to write.
225
- * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
226
- * @param encoding The expected string encoding.
227
215
  */
216
+ public async write(
217
+ data: FileContents,
218
+ posOrOff?: number | null,
219
+ lenOrEnc?: BufferEncoding | number,
220
+ position?: number | null
221
+ ): Promise<{ bytesWritten: number; buffer: FileContents }>;
222
+ public async write<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ bytesWritten: number; buffer: TBuffer }>;
228
223
  public async write(data: string, position?: number, encoding?: BufferEncoding): Promise<{ bytesWritten: number; buffer: string }>;
229
-
230
- public async write(data: FileContents, posOrOff?: number, lenOrEnc?: BufferEncoding | number, position?: number): Promise<{ bytesWritten: number; buffer: FileContents }> {
231
- let buffer: Uint8Array,
232
- offset: number = 0,
233
- length: number;
224
+ public async write(
225
+ data: FileContents,
226
+ posOrOff?: number,
227
+ lenOrEnc?: BufferEncoding | number,
228
+ position?: number | null
229
+ ): Promise<{ bytesWritten: number; buffer: FileContents }> {
230
+ let buffer: Uint8Array, offset: number | null | undefined, length: number;
234
231
  if (typeof data === 'string') {
235
232
  // Signature 1: (fd, string, [position?, [encoding?]])
236
233
  position = typeof posOrOff === 'number' ? posOrOff : null;
@@ -240,13 +237,12 @@ export class FileHandle implements promises.FileHandle {
240
237
  length = buffer.length;
241
238
  } else {
242
239
  // Signature 2: (fd, buffer, offset, length, position?)
243
- buffer = data;
240
+ buffer = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
244
241
  offset = posOrOff;
245
242
  length = lenOrEnc as number;
246
243
  position = typeof position === 'number' ? position : null;
247
244
  }
248
-
249
- position ??= this.file.position!;
245
+ position ??= this.file.position;
250
246
  const bytesWritten = await this.file.write(buffer, offset, length, position);
251
247
  return { buffer, bytesWritten };
252
248
  }
@@ -262,7 +258,7 @@ export class FileHandle implements promises.FileHandle {
262
258
  * If `mode` is a string, it is parsed as an octal integer.
263
259
  * If `flag` is not supplied, the default of `'w'` is used.
264
260
  */
265
- public async writeFile(data: string | Uint8Array, _options?: Node.WriteFileOptions): Promise<void> {
261
+ public async writeFile(data: string | Uint8Array, _options: fs.WriteFileOptions = {}): Promise<void> {
266
262
  const options = normalizeOptions(_options, 'utf8', 'w', 0o644);
267
263
  const flag = parseFlag(options.flag);
268
264
  if (!isWriteable(flag)) {
@@ -271,7 +267,7 @@ export class FileHandle implements promises.FileHandle {
271
267
  if (typeof data != 'string' && !options.encoding) {
272
268
  throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
273
269
  }
274
- const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data;
270
+ const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding!) : data;
275
271
  await this.file.write(encodedData, 0, encodedData.length, 0);
276
272
  }
277
273
 
@@ -289,11 +285,11 @@ export class FileHandle implements promises.FileHandle {
289
285
  * @param position The position in the file where to begin writing.
290
286
  * @returns The number of bytes written.
291
287
  */
292
- public async writev(buffers: Uint8Array[], position?: number): Promise<Node.WriteVResult> {
288
+ public async writev(buffers: Uint8Array[], position?: number): Promise<fs.WriteVResult> {
293
289
  let bytesWritten = 0;
294
290
 
295
291
  for (const buffer of buffers) {
296
- bytesWritten += (await this.write(buffer, 0, buffer.length, position + bytesWritten)).bytesWritten;
292
+ bytesWritten += (await this.write(buffer, 0, buffer.length, position! + bytesWritten)).bytesWritten;
297
293
  }
298
294
 
299
295
  return { bytesWritten, buffers };
@@ -305,11 +301,11 @@ export class FileHandle implements promises.FileHandle {
305
301
  * @param position The position in the file where to begin reading.
306
302
  * @returns The number of bytes read.
307
303
  */
308
- public async readv(buffers: NodeJS.ArrayBufferView[], position?: number): Promise<Node.ReadVResult> {
304
+ public async readv(buffers: NodeJS.ArrayBufferView[], position?: number): Promise<fs.ReadVResult> {
309
305
  let bytesRead = 0;
310
306
 
311
307
  for (const buffer of buffers) {
312
- bytesRead += (await this.read(buffer, 0, buffer.byteLength, position + bytesRead)).bytesRead;
308
+ bytesRead += (await this.read(buffer, 0, buffer.byteLength, position! + bytesRead)).bytesRead;
313
309
  }
314
310
 
315
311
  return { bytesRead, buffers };
@@ -322,9 +318,9 @@ export class FileHandle implements promises.FileHandle {
322
318
  * @returns A `ReadStream` object.
323
319
  */
324
320
  public createReadStream(options?: CreateReadStreamOptions): ReadStream {
325
- const streamOptions = {
321
+ const stream = new ReadStream({
326
322
  highWaterMark: options?.highWaterMark || 64 * 1024,
327
- encoding: options?.encoding,
323
+ encoding: options!.encoding!,
328
324
 
329
325
  read: async (size: number) => {
330
326
  try {
@@ -332,12 +328,11 @@ export class FileHandle implements promises.FileHandle {
332
328
  stream.push(!result.bytesRead ? null : result.buffer.slice(0, result.bytesRead)); // Push data or null for EOF
333
329
  this.file.position += result.bytesRead;
334
330
  } catch (error) {
335
- stream.destroy(error);
331
+ stream.destroy(<Error>error);
336
332
  }
337
333
  },
338
- };
334
+ });
339
335
 
340
- const stream = new ReadStream(streamOptions);
341
336
  stream.path = this.file.path;
342
337
  return stream;
343
338
  }
@@ -358,7 +353,7 @@ export class FileHandle implements promises.FileHandle {
358
353
  const { bytesWritten } = await this.write(chunk, null, encoding);
359
354
  callback(bytesWritten == chunk.length ? null : new Error('Failed to write full chunk'));
360
355
  } catch (error) {
361
- callback(error);
356
+ callback(<Error>error);
362
357
  }
363
358
  },
364
359
  };
@@ -370,7 +365,7 @@ export class FileHandle implements promises.FileHandle {
370
365
  }
371
366
 
372
367
  type FileSystemMethod = {
373
- [K in keyof FileSystem]: FileSystem[K] extends (...args) => unknown
368
+ [K in keyof FileSystem]: FileSystem[K] extends (...args: any[]) => unknown
374
369
  ? (name: K, resolveSymlinks: boolean, ...args: Parameters<FileSystem[K]>) => ReturnType<FileSystem[K]>
375
370
  : never;
376
371
  }[keyof FileSystem]; // https://stackoverflow.com/a/76335220/17637456
@@ -388,14 +383,14 @@ type FileSystemMethod = {
388
383
  * @returns
389
384
  */
390
385
  async function doOp<M extends FileSystemMethod, RT extends ReturnType<M> = ReturnType<M>>(...[name, resolveSymlinks, rawPath, ...args]: Parameters<M>): Promise<RT> {
391
- rawPath = normalizePath(rawPath);
386
+ rawPath = normalizePath(rawPath!);
392
387
  const _path = resolveSymlinks && (await exists(rawPath)) ? await realpath(rawPath) : rawPath;
393
388
  const { fs, path } = resolveMount(_path);
394
389
  try {
395
390
  // @ts-expect-error 2556 (since ...args is not correctly picked up as being a tuple)
396
391
  return fs[name](path, ...args) as Promise<RT>;
397
392
  } catch (e) {
398
- throw fixError(e, { [path]: rawPath });
393
+ throw fixError(<Error>e, { [path]: rawPath });
399
394
  }
400
395
  }
401
396
 
@@ -406,7 +401,7 @@ async function doOp<M extends FileSystemMethod, RT extends ReturnType<M> = Retur
406
401
  * @param oldPath
407
402
  * @param newPath
408
403
  */
409
- export async function rename(oldPath: PathLike, newPath: PathLike): Promise<void> {
404
+ export async function rename(oldPath: fs.PathLike, newPath: fs.PathLike): Promise<void> {
410
405
  oldPath = normalizePath(oldPath);
411
406
  newPath = normalizePath(newPath);
412
407
  const src = resolveMount(oldPath);
@@ -419,7 +414,7 @@ export async function rename(oldPath: PathLike, newPath: PathLike): Promise<void
419
414
  await writeFile(newPath, await readFile(oldPath));
420
415
  await unlink(oldPath);
421
416
  } catch (e) {
422
- throw fixError(e, { [src.path]: oldPath, [dst.path]: newPath });
417
+ throw fixError(<Error>e, { [src.path]: oldPath, [dst.path]: newPath });
423
418
  }
424
419
  }
425
420
  rename satisfies typeof promises.rename;
@@ -428,7 +423,7 @@ rename satisfies typeof promises.rename;
428
423
  * Test whether or not the given path exists by checking with the file system.
429
424
  * @param _path
430
425
  */
431
- export async function exists(_path: PathLike): Promise<boolean> {
426
+ export async function exists(_path: fs.PathLike): Promise<boolean> {
432
427
  try {
433
428
  const { fs, path } = resolveMount(await realpath(_path));
434
429
  return await fs.exists(path, cred);
@@ -446,11 +441,11 @@ export async function exists(_path: PathLike): Promise<boolean> {
446
441
  * @param path
447
442
  * @returns Stats
448
443
  */
449
- export async function stat(path: PathLike, options: Node.BigIntOptions): Promise<BigIntStats>;
450
- export async function stat(path: PathLike, options?: { bigint?: false }): Promise<Stats>;
451
- export async function stat(path: PathLike, options?: Node.StatOptions): Promise<Stats | BigIntStats>;
452
- export async function stat(path: PathLike, options?: Node.StatOptions): Promise<Stats | BigIntStats> {
453
- const stats: Stats = await doOp('stat', true, path, cred);
444
+ export async function stat(path: fs.PathLike, options: fs.BigIntOptions): Promise<BigIntStats>;
445
+ export async function stat(path: fs.PathLike, options?: { bigint?: false }): Promise<Stats>;
446
+ export async function stat(path: fs.PathLike, options?: fs.StatOptions): Promise<Stats | BigIntStats>;
447
+ export async function stat(path: fs.PathLike, options?: fs.StatOptions): Promise<Stats | BigIntStats> {
448
+ const stats: Stats = await doOp('stat', true, path.toString(), cred);
454
449
  return options?.bigint ? new BigIntStats(stats) : stats;
455
450
  }
456
451
  stat satisfies typeof promises.stat;
@@ -462,10 +457,10 @@ stat satisfies typeof promises.stat;
462
457
  * @param path
463
458
  * @return
464
459
  */
465
- export async function lstat(path: PathLike, options?: { bigint?: false }): Promise<Stats>;
466
- export async function lstat(path: PathLike, options: { bigint: true }): Promise<BigIntStats>;
467
- export async function lstat(path: PathLike, options?: Node.StatOptions): Promise<Stats | BigIntStats> {
468
- const stats: Stats = await doOp('stat', false, path, cred);
460
+ export async function lstat(path: fs.PathLike, options?: { bigint?: boolean }): Promise<Stats>;
461
+ export async function lstat(path: fs.PathLike, options: { bigint: true }): Promise<BigIntStats>;
462
+ export async function lstat(path: fs.PathLike, options?: fs.StatOptions): Promise<Stats | BigIntStats> {
463
+ const stats: Stats = await doOp('stat', false, path.toString(), cred);
469
464
  return options?.bigint ? new BigIntStats(stats) : stats;
470
465
  }
471
466
  lstat satisfies typeof promises.lstat;
@@ -477,7 +472,7 @@ lstat satisfies typeof promises.lstat;
477
472
  * @param path
478
473
  * @param len
479
474
  */
480
- export async function truncate(path: PathLike, len: number = 0): Promise<void> {
475
+ export async function truncate(path: fs.PathLike, len: number = 0): Promise<void> {
481
476
  const handle = await open(path, 'r+');
482
477
  try {
483
478
  await handle.truncate(len);
@@ -491,8 +486,8 @@ truncate satisfies typeof promises.truncate;
491
486
  * `unlink`.
492
487
  * @param path
493
488
  */
494
- export async function unlink(path: PathLike): Promise<void> {
495
- return doOp('unlink', false, path, cred);
489
+ export async function unlink(path: fs.PathLike): Promise<void> {
490
+ return doOp('unlink', false, path.toString(), cred);
496
491
  }
497
492
  unlink satisfies typeof promises.unlink;
498
493
 
@@ -500,7 +495,7 @@ unlink satisfies typeof promises.unlink;
500
495
  * Opens a file. This helper handles the complexity of file flags.
501
496
  * @internal
502
497
  */
503
- async function _open(_path: PathLike, _flag: string, _mode: Node.Mode = 0o644, resolveSymlinks: boolean): Promise<File> {
498
+ async function _open(_path: fs.PathLike, _flag: fs.OpenMode, _mode: fs.Mode = 0o644, resolveSymlinks: boolean): Promise<File> {
504
499
  const path = normalizePath(_path),
505
500
  mode = normalizeMode(_mode, 0o644),
506
501
  flag = parseFlag(_flag);
@@ -552,21 +547,12 @@ async function _open(_path: PathLike, _flag: string, _mode: Node.Mode = 0o644, r
552
547
  * @param flags Handles the complexity of the various file modes. See its API for more details.
553
548
  * @param mode Mode to use to open the file. Can be ignored if the filesystem doesn't support permissions.
554
549
  */
555
- export async function open(path: PathLike, flag: string, mode: Node.Mode = 0o644): Promise<FileHandle> {
550
+ export async function open(path: fs.PathLike, flag: fs.OpenMode = 'r', mode: fs.Mode = 0o644): Promise<FileHandle> {
556
551
  const file = await _open(path, flag, mode, true);
557
- return new FileHandle(getFdForFile(file));
552
+ return new FileHandle(file2fd(file));
558
553
  }
559
554
  open satisfies typeof promises.open;
560
555
 
561
- /**
562
- * Opens a file without resolving symlinks
563
- * @internal
564
- */
565
- export async function lopen(path: PathLike, flag: string, mode: Node.Mode = 0o644): Promise<FileHandle> {
566
- const file: File = await _open(path, flag, mode, false);
567
- return new FileHandle(getFdForFile(file));
568
- }
569
-
570
556
  /**
571
557
  * Asynchronously reads the entire contents of a file.
572
558
  */
@@ -593,16 +579,23 @@ async function _readFile(fname: string, flag: string, resolveSymlinks: boolean):
593
579
  * options.flag Defaults to `'r'`.
594
580
  * @returns file data
595
581
  */
596
- export async function readFile(filename: PathLike, options?: { flag?: Node.OpenMode }): Promise<Buffer>;
597
- export async function readFile(filename: PathLike, options: (Node.EncodingOption & { flag?: Node.OpenMode }) | BufferEncoding): Promise<string>;
598
- export async function readFile(filename: PathLike, _options?: (Node.EncodingOption & { flag?: Node.OpenMode }) | BufferEncoding): Promise<Buffer | string> {
582
+ export async function readFile(path: fs.PathLike | promises.FileHandle, options?: { encoding?: null; flag?: fs.OpenMode } | null): Promise<Buffer>;
583
+ export async function readFile(path: fs.PathLike | promises.FileHandle, options: { encoding: BufferEncoding; flag?: fs.OpenMode } | BufferEncoding): Promise<string>;
584
+ export async function readFile(
585
+ path: fs.PathLike | promises.FileHandle,
586
+ options?: (fs.ObjectEncodingOptions & { flag?: fs.OpenMode }) | BufferEncoding | null
587
+ ): Promise<string | Buffer>;
588
+ export async function readFile(
589
+ path: fs.PathLike | promises.FileHandle,
590
+ _options?: (fs.ObjectEncodingOptions & { flag?: fs.OpenMode }) | BufferEncoding | null
591
+ ): Promise<Buffer | string> {
599
592
  const options = normalizeOptions(_options, null, 'r', 0);
600
593
  const flag = parseFlag(options.flag);
601
594
  if (!isReadable(flag)) {
602
595
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed must allow for reading.');
603
596
  }
604
-
605
- const data: Buffer = Buffer.from(await _readFile(filename, options.flag, true));
597
+ path = path instanceof FileHandle ? path.file.path : path.toString();
598
+ const data: Buffer = Buffer.from(await _readFile(path, options.flag, true));
606
599
  return options.encoding ? data.toString(options.encoding) : data;
607
600
  }
608
601
  readFile satisfies typeof promises.readFile;
@@ -611,18 +604,26 @@ readFile satisfies typeof promises.readFile;
611
604
  * Asynchronously writes data to a file, replacing the file if it already exists.
612
605
  *
613
606
  * The encoding option is ignored if data is a buffer.
614
- * @param filename
615
- * @param data
607
+ * @param path
608
+ * @param data Note:
616
609
  * @param _options
617
610
  * @option options encoding Defaults to `'utf8'`.
618
611
  * @option options mode Defaults to `0644`.
619
612
  * @option options flag Defaults to `'w'`.
620
613
  */
621
- export async function writeFile(filename: PathLike, data: FileContents, _options?: Node.WriteFileOptions): Promise<void> {
614
+ export async function writeFile(
615
+ path: fs.PathLike | promises.FileHandle,
616
+ data: FileContents | Stream | Iterable<string | ArrayBufferView> | AsyncIterable<string | ArrayBufferView>,
617
+ _options?: (fs.ObjectEncodingOptions & { mode?: fs.Mode; flag?: fs.OpenMode; flush?: boolean }) | BufferEncoding | null
618
+ ): Promise<void> {
622
619
  const options = normalizeOptions(_options, 'utf8', 'w+', 0o644);
623
- const handle = await open(filename, options.flag, options.mode);
620
+ const handle = path instanceof FileHandle ? path : await open(path.toString(), options.flag, options.mode);
624
621
  try {
625
- await handle.writeFile(data, options);
622
+ const _data = typeof data == 'string' ? data : data;
623
+ if (typeof _data != 'string' && !(_data instanceof Uint8Array)) {
624
+ throw new ApiError(ErrorCode.EINVAL, 'Iterables and streams not supported', handle.file.path, 'writeFile');
625
+ }
626
+ await handle.writeFile(_data, options);
626
627
  } finally {
627
628
  await handle.close();
628
629
  }
@@ -633,8 +634,8 @@ writeFile satisfies typeof promises.writeFile;
633
634
  * Asynchronously append data to a file, creating the file if
634
635
  * it not yet exists.
635
636
  */
636
- async function _appendFile(fname: string, data: Uint8Array, flag: string, mode: number, resolveSymlinks: boolean): Promise<void> {
637
- const file = await _open(fname, flag, mode, resolveSymlinks);
637
+ async function _appendFile(path: fs.PathLike, data: Uint8Array, flag: string, mode: number, resolveSymlinks: boolean): Promise<void> {
638
+ const file = await _open(path, flag, mode, resolveSymlinks);
638
639
  try {
639
640
  await file.write(data, 0, data.length, null);
640
641
  } finally {
@@ -645,7 +646,7 @@ async function _appendFile(fname: string, data: Uint8Array, flag: string, mode:
645
646
  /**
646
647
  * Asynchronously append data to a file, creating the file if it not yet
647
648
  * exists.
648
- * @param filename
649
+ * @param path
649
650
  * @param data
650
651
  * @param options
651
652
  * @option options encoding Defaults to `'utf8'`.
@@ -653,9 +654,9 @@ async function _appendFile(fname: string, data: Uint8Array, flag: string, mode:
653
654
  * @option options flag Defaults to `'a'`.
654
655
  */
655
656
  export async function appendFile(
656
- filename: PathLike,
657
+ path: fs.PathLike | promises.FileHandle,
657
658
  data: FileContents,
658
- _options?: BufferEncoding | (Node.EncodingOption & { mode?: Node.Mode; flag?: Node.OpenMode })
659
+ _options?: BufferEncoding | (fs.EncodingOption & { mode?: fs.Mode; flag?: fs.OpenMode }) | null
659
660
  ): Promise<void> {
660
661
  const options = normalizeOptions(_options, 'utf8', 'a', 0o644);
661
662
  const flag = parseFlag(options.flag);
@@ -665,8 +666,8 @@ export async function appendFile(
665
666
  if (typeof data != 'string' && !options.encoding) {
666
667
  throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
667
668
  }
668
- const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data;
669
- await _appendFile(filename, encodedData, options.flag, options.mode, true);
669
+ const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding!) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
670
+ await _appendFile(path instanceof FileHandle ? path.file.path : path.toString(), encodedData, options.flag, options.mode, true);
670
671
  }
671
672
  appendFile satisfies typeof promises.appendFile;
672
673
 
@@ -676,33 +677,41 @@ appendFile satisfies typeof promises.appendFile;
676
677
  * `rmdir`.
677
678
  * @param path
678
679
  */
679
- export async function rmdir(path: PathLike): Promise<void> {
680
- return doOp('rmdir', true, path, cred);
680
+ export async function rmdir(path: fs.PathLike): Promise<void> {
681
+ return doOp('rmdir', true, path.toString(), cred);
681
682
  }
682
683
  rmdir satisfies typeof promises.rmdir;
683
684
 
684
685
  /**
685
- * `mkdir`.
686
- * @param path
687
- * @param mode defaults to `0777`
686
+ * Asynchronous mkdir(2) - create a directory.
687
+ * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
688
+ * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
689
+ * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
688
690
  */
689
- export async function mkdir(path: PathLike, mode?: Node.Mode | (Node.MakeDirectoryOptions & { recursive?: false })): Promise<void>;
690
- export async function mkdir(path: PathLike, mode: Node.MakeDirectoryOptions & { recursive: true }): Promise<string>;
691
- export async function mkdir(path: PathLike, mode?: Node.Mode | Node.MakeDirectoryOptions): Promise<string | void> {
692
- await doOp('mkdir', true, path, normalizeMode(typeof mode == 'object' ? mode?.mode : mode, 0o777), cred);
691
+ export async function mkdir(path: fs.PathLike, options: fs.MakeDirectoryOptions & { recursive: true }): Promise<string | undefined>;
692
+ export async function mkdir(path: fs.PathLike, options?: fs.Mode | (fs.MakeDirectoryOptions & { recursive?: false | undefined }) | null): Promise<void>;
693
+ export async function mkdir(path: fs.PathLike, options?: fs.Mode | fs.MakeDirectoryOptions | null): Promise<string | undefined>;
694
+ export async function mkdir(path: fs.PathLike, options?: fs.Mode | fs.MakeDirectoryOptions | null): Promise<string | undefined | void> {
695
+ await doOp('mkdir', true, path.toString(), normalizeMode(typeof options == 'object' ? options?.mode : options, 0o777), cred);
696
+ return;
693
697
  }
694
698
  mkdir satisfies typeof promises.mkdir;
695
699
 
696
700
  /**
697
- * `readdir`. Reads the contents of a directory.
698
- * @param path
701
+ * Asynchronous readdir(3) - read a directory.
702
+ * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
703
+ * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
699
704
  */
700
- export async function readdir(path: PathLike, options?: (Node.EncodingOption & { withFileTypes?: false }) | BufferEncoding): Promise<string[]>;
701
- export async function readdir(path: PathLike, options: Node.BufferEncodingOption & { withFileTypes?: false }): Promise<Buffer[]>;
702
- export async function readdir(path: PathLike, options: Node.EncodingOption & { withFileTypes: true }): Promise<Dirent[]>;
705
+ export async function readdir(path: fs.PathLike, options?: (fs.ObjectEncodingOptions & { withFileTypes?: false; recursive?: boolean }) | BufferEncoding | null): Promise<string[]>;
706
+ export async function readdir(path: fs.PathLike, options: fs.BufferEncodingOption & { withFileTypes?: false; recursive?: boolean }): Promise<Buffer[]>;
707
+ export async function readdir(
708
+ path: fs.PathLike,
709
+ options?: (fs.ObjectEncodingOptions & { withFileTypes?: false; recursive?: boolean }) | BufferEncoding | null
710
+ ): Promise<string[] | Buffer[]>;
711
+ export async function readdir(path: fs.PathLike, options: fs.ObjectEncodingOptions & { withFileTypes: true; recursive?: boolean }): Promise<Dirent[]>;
703
712
  export async function readdir(
704
- path: PathLike,
705
- options?: (Node.EncodingOption & { withFileTypes?: boolean }) | BufferEncoding | (Node.BufferEncodingOption & { withFileTypes?: boolean })
713
+ path: fs.PathLike,
714
+ options?: { withFileTypes?: boolean; recursive?: boolean; encoding?: BufferEncoding | 'buffer' | null } | BufferEncoding | 'buffer' | null
706
715
  ): Promise<string[] | Dirent[] | Buffer[]> {
707
716
  path = normalizePath(path);
708
717
  const entries: string[] = await doOp('readdir', true, path, cred);
@@ -732,9 +741,9 @@ readdir satisfies typeof promises.readdir;
732
741
  * @param existing
733
742
  * @param newpath
734
743
  */
735
- export async function link(existing: PathLike, newpath: PathLike): Promise<void> {
744
+ export async function link(existing: fs.PathLike, newpath: fs.PathLike): Promise<void> {
736
745
  newpath = normalizePath(newpath);
737
- return doOp('link', false, existing, newpath, cred);
746
+ return doOp('link', false, existing.toString(), newpath, cred);
738
747
  }
739
748
  link satisfies typeof promises.link;
740
749
 
@@ -744,16 +753,16 @@ link satisfies typeof promises.link;
744
753
  * @param path link path
745
754
  * @param type can be either `'dir'` or `'file'` (default is `'file'`)
746
755
  */
747
- export async function symlink(target: PathLike, path: PathLike, type: Node.symlink.Type = 'file'): Promise<void> {
748
- if (!['file', 'dir', 'junction'].includes(type)) {
756
+ export async function symlink(target: fs.PathLike, path: fs.PathLike, type: fs.symlink.Type | string | null = 'file'): Promise<void> {
757
+ if (!['file', 'dir', 'junction'].includes(type!)) {
749
758
  throw new ApiError(ErrorCode.EINVAL, 'Invalid symlink type: ' + type);
750
759
  }
751
760
 
752
761
  if (await exists(path)) {
753
- throw ApiError.With('EEXIST', path, 'symlink');
762
+ throw ApiError.With('EEXIST', path.toString(), 'symlink');
754
763
  }
755
764
 
756
- await writeFile(path, target);
765
+ await writeFile(path, target.toString());
757
766
  const file = await _open(path, 'r+', 0o644, false);
758
767
  await file._setType(FileType.SYMLINK);
759
768
  }
@@ -763,15 +772,13 @@ symlink satisfies typeof promises.symlink;
763
772
  * readlink.
764
773
  * @param path
765
774
  */
766
- export async function readlink(path: PathLike, options: Node.BufferEncodingOption): Promise<Buffer>;
767
- export async function readlink(path: PathLike, options?: Node.EncodingOption | BufferEncoding): Promise<string>;
768
- export async function readlink(path: PathLike, options?: Node.BufferEncodingOption | Node.EncodingOption | BufferEncoding): Promise<string | Buffer> {
769
- const value: Buffer = Buffer.from(await _readFile(path, 'r', false));
770
- const encoding: BufferEncoding | 'buffer' = typeof options == 'object' ? options.encoding : options;
771
- if (encoding == 'buffer') {
772
- return value;
773
- }
774
- return value.toString(encoding);
775
+ export async function readlink(path: fs.PathLike, options: fs.BufferEncodingOption): Promise<Buffer>;
776
+ export async function readlink(path: fs.PathLike, options?: fs.EncodingOption | null): Promise<string>;
777
+ export async function readlink(path: fs.PathLike, options?: fs.BufferEncodingOption | fs.EncodingOption | string | null): Promise<string | Buffer>;
778
+ export async function readlink(path: fs.PathLike, options?: fs.BufferEncodingOption | fs.EncodingOption | string | null): Promise<string | Buffer> {
779
+ const value: Buffer = Buffer.from(await _readFile(path.toString(), 'r', false));
780
+ const encoding = typeof options == 'object' ? options?.encoding : options;
781
+ return encoding == 'buffer' ? value : value.toString(encoding! as BufferEncoding);
775
782
  }
776
783
  readlink satisfies typeof promises.readlink;
777
784
 
@@ -783,7 +790,7 @@ readlink satisfies typeof promises.readlink;
783
790
  * @param uid
784
791
  * @param gid
785
792
  */
786
- export async function chown(path: PathLike, uid: number, gid: number): Promise<void> {
793
+ export async function chown(path: fs.PathLike, uid: number, gid: number): Promise<void> {
787
794
  const handle = await open(path, 'r+');
788
795
  try {
789
796
  await handle.chown(uid, gid);
@@ -799,12 +806,12 @@ chown satisfies typeof promises.chown;
799
806
  * @param uid
800
807
  * @param gid
801
808
  */
802
- export async function lchown(path: PathLike, uid: number, gid: number): Promise<void> {
803
- const handle = await lopen(path, 'r+');
809
+ export async function lchown(path: fs.PathLike, uid: number, gid: number): Promise<void> {
810
+ const file: File = await _open(path, 'r+', 0o644, false);
804
811
  try {
805
- await handle.chown(uid, gid);
812
+ await file.chown(uid, gid);
806
813
  } finally {
807
- await handle.close();
814
+ await file.close();
808
815
  }
809
816
  }
810
817
  lchown satisfies typeof promises.lchown;
@@ -814,7 +821,7 @@ lchown satisfies typeof promises.lchown;
814
821
  * @param path
815
822
  * @param mode
816
823
  */
817
- export async function chmod(path: PathLike, mode: Node.Mode): Promise<void> {
824
+ export async function chmod(path: fs.PathLike, mode: fs.Mode): Promise<void> {
818
825
  const handle = await open(path, 'r+');
819
826
  try {
820
827
  await handle.chmod(mode);
@@ -829,12 +836,12 @@ chmod satisfies typeof promises.chmod;
829
836
  * @param path
830
837
  * @param mode
831
838
  */
832
- export async function lchmod(path: PathLike, mode: Node.Mode): Promise<void> {
833
- const handle = await lopen(path, 'r+');
839
+ export async function lchmod(path: fs.PathLike, mode: fs.Mode): Promise<void> {
840
+ const file: File = await _open(path, 'r+', 0o644, false);
834
841
  try {
835
- await handle.chmod(mode);
842
+ await new FileHandle(file2fd(file)).chmod(mode);
836
843
  } finally {
837
- await handle.close();
844
+ await file.close();
838
845
  }
839
846
  }
840
847
  lchmod satisfies typeof promises.lchmod;
@@ -845,7 +852,7 @@ lchmod satisfies typeof promises.lchmod;
845
852
  * @param atime
846
853
  * @param mtime
847
854
  */
848
- export async function utimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date): Promise<void> {
855
+ export async function utimes(path: fs.PathLike, atime: string | number | Date, mtime: string | number | Date): Promise<void> {
849
856
  const handle = await open(path, 'r+');
850
857
  try {
851
858
  await handle.utimes(atime, mtime);
@@ -861,12 +868,12 @@ utimes satisfies typeof promises.utimes;
861
868
  * @param atime
862
869
  * @param mtime
863
870
  */
864
- export async function lutimes(path: PathLike, atime: number | Date, mtime: number | Date): Promise<void> {
865
- const handle = await lopen(path, 'r+');
871
+ export async function lutimes(path: fs.PathLike, atime: fs.TimeLike, mtime: fs.TimeLike): Promise<void> {
872
+ const file: File = await _open(path, 'r+', 0o644, false);
866
873
  try {
867
- await handle.utimes(atime, mtime);
874
+ await file.utimes(new Date(atime), new Date(mtime));
868
875
  } finally {
869
- await handle.close();
876
+ await file.close();
870
877
  }
871
878
  }
872
879
  lutimes satisfies typeof promises.lutimes;
@@ -878,9 +885,9 @@ lutimes satisfies typeof promises.lutimes;
878
885
  *
879
886
  * Note: This *Can not* use doOp since doOp depends on it
880
887
  */
881
- export async function realpath(path: PathLike, options: Node.BufferEncodingOption): Promise<Buffer>;
882
- export async function realpath(path: PathLike, options?: Node.EncodingOption | BufferEncoding): Promise<string>;
883
- export async function realpath(path: PathLike, options?: Node.EncodingOption | BufferEncoding | Node.BufferEncodingOption): Promise<string | Buffer> {
888
+ export async function realpath(path: fs.PathLike, options: fs.BufferEncodingOption): Promise<Buffer>;
889
+ export async function realpath(path: fs.PathLike, options?: fs.EncodingOption | BufferEncoding): Promise<string>;
890
+ export async function realpath(path: fs.PathLike, options?: fs.EncodingOption | BufferEncoding | fs.BufferEncodingOption): Promise<string | Buffer> {
884
891
  path = normalizePath(path);
885
892
  const { base, dir } = parse(path);
886
893
  const lpath = join(dir == '/' ? '/' : await realpath(dir), base);
@@ -894,7 +901,7 @@ export async function realpath(path: PathLike, options?: Node.EncodingOption | B
894
901
 
895
902
  return realpath(mountPoint + (await readlink(lpath)));
896
903
  } catch (e) {
897
- throw fixError(e, { [resolvedPath]: lpath });
904
+ throw fixError(<Error>e, { [resolvedPath]: lpath });
898
905
  }
899
906
  }
900
907
  realpath satisfies typeof promises.realpath;
@@ -902,10 +909,11 @@ realpath satisfies typeof promises.realpath;
902
909
  /**
903
910
  * @todo Implement
904
911
  */
905
- export function watch(filename: PathLike, options: (Node.WatchOptions & { encoding: 'buffer' }) | 'buffer'): AsyncIterable<FileChangeInfo<Buffer>>;
906
- export function watch(filename: PathLike, options?: Node.WatchOptions | BufferEncoding): AsyncIterable<FileChangeInfo<string>>;
907
- export function watch(filename: PathLike, options: Node.WatchOptions | string): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>> {
908
- throw ApiError.With('ENOSYS', filename, 'watch');
912
+ export function watch(filename: fs.PathLike, options?: fs.WatchOptions | BufferEncoding): AsyncIterable<FileChangeInfo<string>>;
913
+ export function watch(filename: fs.PathLike, options: fs.WatchOptions | fs.BufferEncodingOption): AsyncIterable<FileChangeInfo<Buffer>>;
914
+ export function watch(filename: fs.PathLike, options?: fs.WatchOptions | string): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>>;
915
+ export function watch(filename: fs.PathLike, options: fs.WatchOptions | string = {}): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>> {
916
+ throw ApiError.With('ENOSYS', filename.toString(), 'watch');
909
917
  }
910
918
  watch satisfies typeof promises.watch;
911
919
 
@@ -914,7 +922,7 @@ watch satisfies typeof promises.watch;
914
922
  * @param path
915
923
  * @param mode
916
924
  */
917
- export async function access(path: PathLike, mode: number = constants.F_OK): Promise<void> {
925
+ export async function access(path: fs.PathLike, mode: number = constants.F_OK): Promise<void> {
918
926
  const stats = await stat(path);
919
927
  if (!stats.hasAccess(mode, cred)) {
920
928
  throw new ApiError(ErrorCode.EACCES);
@@ -926,7 +934,7 @@ access satisfies typeof promises.access;
926
934
  * Asynchronous `rm`. Removes files or directories (recursively).
927
935
  * @param path The path to the file or directory to remove.
928
936
  */
929
- export async function rm(path: PathLike, options?: Node.RmOptions) {
937
+ export async function rm(path: fs.PathLike, options?: fs.RmOptions) {
930
938
  path = normalizePath(path);
931
939
 
932
940
  const stats = await stat(path);
@@ -961,10 +969,10 @@ rm satisfies typeof promises.rm;
961
969
  * @param options The encoding (or an object including `encoding`).
962
970
  * @returns The path to the created temporary directory, encoded as a string or buffer.
963
971
  */
964
- export async function mkdtemp(prefix: string, options?: Node.EncodingOption): Promise<string>;
965
- export async function mkdtemp(prefix: string, options?: Node.BufferEncodingOption): Promise<Buffer>;
966
- export async function mkdtemp(prefix: string, options?: Node.EncodingOption | Node.BufferEncodingOption): Promise<string | Buffer> {
967
- const encoding = typeof options === 'object' ? options.encoding : options || 'utf8';
972
+ export async function mkdtemp(prefix: string, options?: fs.EncodingOption): Promise<string>;
973
+ export async function mkdtemp(prefix: string, options?: fs.BufferEncodingOption): Promise<Buffer>;
974
+ export async function mkdtemp(prefix: string, options?: fs.EncodingOption | fs.BufferEncodingOption): Promise<string | Buffer> {
975
+ const encoding = typeof options === 'object' ? options?.encoding : options || 'utf8';
968
976
  const fsName = `${prefix}${Date.now()}-${Math.random().toString(36).slice(2)}`;
969
977
  const resolvedPath = '/tmp/' + fsName;
970
978
 
@@ -981,7 +989,7 @@ mkdtemp satisfies typeof promises.mkdtemp;
981
989
  * @param mode Optional flags for the copy operation. Currently supports these flags:
982
990
  * * `fs.constants.COPYFILE_EXCL`: If the destination file already exists, the operation fails.
983
991
  */
984
- export async function copyFile(src: PathLike, dest: PathLike, mode?: number): Promise<void> {
992
+ export async function copyFile(src: fs.PathLike, dest: fs.PathLike, mode?: number): Promise<void> {
985
993
  src = normalizePath(src);
986
994
  dest = normalizePath(dest);
987
995
 
@@ -999,7 +1007,7 @@ copyFile satisfies typeof promises.copyFile;
999
1007
  * @param options Options for opening the directory.
1000
1008
  * @returns A `Dir` object representing the opened directory.
1001
1009
  */
1002
- export async function opendir(path: PathLike, options?: Node.OpenDirOptions): Promise<Dir> {
1010
+ export async function opendir(path: fs.PathLike, options?: fs.OpenDirOptions): Promise<Dir> {
1003
1011
  path = normalizePath(path);
1004
1012
  return new Dir(path);
1005
1013
  }
@@ -1017,7 +1025,7 @@ opendir satisfies typeof promises.opendir;
1017
1025
  * * `preserveTimestamps`: Preserve file timestamps.
1018
1026
  * * `recursive`: If `true`, copies directories recursively.
1019
1027
  */
1020
- export async function cp(source: PathLike, destination: PathLike, opts?: Node.CopyOptions): Promise<void> {
1028
+ export async function cp(source: fs.PathLike, destination: fs.PathLike, opts?: fs.CopyOptions): Promise<void> {
1021
1029
  source = normalizePath(source);
1022
1030
  destination = normalizePath(destination);
1023
1031
 
@@ -1063,9 +1071,9 @@ cp satisfies typeof promises.cp;
1063
1071
  * @since v18.15.0
1064
1072
  * @return Fulfills with an {fs.StatFs} for the file system.
1065
1073
  */
1066
- export async function statfs(path: PathLike, opts?: Node.StatFsOptions & { bigint?: false }): Promise<StatsFs>;
1067
- export async function statfs(path: PathLike, opts: Node.StatFsOptions & { bigint: true }): Promise<BigIntStatsFs>;
1068
- export async function statfs(path: PathLike, opts?: Node.StatFsOptions): Promise<StatsFs | BigIntStatsFs>;
1069
- export async function statfs(path: PathLike, opts?: Node.StatFsOptions): Promise<StatsFs | BigIntStatsFs> {
1070
- throw ApiError.With('ENOSYS', path, 'statfs');
1074
+ export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions & { bigint?: false }): Promise<StatsFs>;
1075
+ export async function statfs(path: fs.PathLike, opts: fs.StatFsOptions & { bigint: true }): Promise<BigIntStatsFs>;
1076
+ export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<StatsFs | BigIntStatsFs>;
1077
+ export async function statfs(path: fs.PathLike, opts?: fs.StatFsOptions): Promise<StatsFs | BigIntStatsFs> {
1078
+ throw ApiError.With('ENOSYS', path.toString(), 'statfs');
1071
1079
  }