@zenfs/core 0.0.12 → 0.1.0

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.
@@ -0,0 +1,104 @@
1
+ import { readdir } from './promises.js';
2
+ import { ApiError, ErrorCode } from '../ApiError.js';
3
+ import { readdirSync } from './sync.js';
4
+ export class Dirent {
5
+ constructor(name, stats) {
6
+ this.name = name;
7
+ this.stats = stats;
8
+ }
9
+ isFile() {
10
+ return this.stats.isFile();
11
+ }
12
+ isDirectory() {
13
+ return this.stats.isDirectory();
14
+ }
15
+ isBlockDevice() {
16
+ return this.stats.isBlockDevice();
17
+ }
18
+ isCharacterDevice() {
19
+ return this.stats.isCharacterDevice();
20
+ }
21
+ isSymbolicLink() {
22
+ return this.stats.isSymbolicLink();
23
+ }
24
+ isFIFO() {
25
+ return this.stats.isFIFO();
26
+ }
27
+ isSocket() {
28
+ return this.stats.isSocket();
29
+ }
30
+ }
31
+ /**
32
+ * A class representing a directory stream.
33
+ */
34
+ export class Dir {
35
+ checkClosed() {
36
+ if (this.closed) {
37
+ throw new ApiError(ErrorCode.EBADF, 'Can not use closed Dir');
38
+ }
39
+ }
40
+ constructor(path) {
41
+ this.path = path;
42
+ this.closed = false;
43
+ }
44
+ close(cb) {
45
+ this.closed = true;
46
+ if (!cb) {
47
+ return Promise.resolve();
48
+ }
49
+ cb();
50
+ }
51
+ /**
52
+ * Synchronously close the directory's underlying resource handle.
53
+ * Subsequent reads will result in errors.
54
+ */
55
+ closeSync() {
56
+ this.closed = true;
57
+ }
58
+ async _read() {
59
+ if (!this._entries) {
60
+ this._entries = await readdir(this.path, { withFileTypes: true });
61
+ }
62
+ if (this._entries.length == 0) {
63
+ return null;
64
+ }
65
+ return this._entries.shift();
66
+ }
67
+ read(cb) {
68
+ if (!cb) {
69
+ return this._read();
70
+ }
71
+ this._read().then(value => cb(null, value));
72
+ }
73
+ /**
74
+ * Synchronously read the next directory entry via `readdir(3)` as a `Dirent`.
75
+ * If there are no more directory entries to read, null will be returned.
76
+ * Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms.
77
+ */
78
+ readSync() {
79
+ if (!this._entries) {
80
+ this._entries = readdirSync(this.path, { withFileTypes: true });
81
+ }
82
+ if (this._entries.length == 0) {
83
+ return null;
84
+ }
85
+ return this._entries.shift();
86
+ }
87
+ /**
88
+ * Asynchronously iterates over the directory via `readdir(3)` until all entries have been read.
89
+ */
90
+ [Symbol.asyncIterator]() {
91
+ const _this = this;
92
+ return {
93
+ [Symbol.asyncIterator]: this[Symbol.asyncIterator],
94
+ async next() {
95
+ const value = await _this._read();
96
+ if (value != null) {
97
+ return { done: false, value };
98
+ }
99
+ await _this.close();
100
+ return { done: true, value: undefined };
101
+ },
102
+ };
103
+ }
104
+ }
@@ -1,7 +1,6 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import * as fs_mock from './index.js';
3
3
  import type * as fs_node from 'node:fs';
4
- type ZenFSModule = typeof fs_node & typeof fs_mock;
5
- declare const fs: ZenFSModule;
4
+ declare const fs: typeof fs_node & typeof fs_mock;
6
5
  export * from './index.js';
7
6
  export default fs;
@@ -1,5 +1,4 @@
1
1
  import * as fs_mock from './index.js';
2
- // @ts-expect-error 2322
3
2
  const fs = fs_mock;
4
3
  export * from './index.js';
5
4
  export default fs;
@@ -2,4 +2,7 @@ export * from './callbacks.js';
2
2
  export * from './sync.js';
3
3
  export * as promises from './promises.js';
4
4
  export * as constants from './constants.js';
5
+ export * from './streams.js';
6
+ export * from './dir.js';
5
7
  export { initialize, getMount, getMounts, mount, umount, _toUnixTimestamp } from './shared.js';
8
+ export { Stats, BigIntStats } from '../stats.js';
@@ -2,4 +2,7 @@ export * from './callbacks.js';
2
2
  export * from './sync.js';
3
3
  export * as promises from './promises.js';
4
4
  export * as constants from './constants.js';
5
+ export * from './streams.js';
6
+ export * from './dir.js';
5
7
  export { initialize, getMount, getMounts, mount, umount, _toUnixTimestamp } from './shared.js';
8
+ export { Stats, BigIntStats } from '../stats.js';
@@ -1,27 +1,32 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  /// <reference types="node" resolution-mode="require"/>
3
- import type { ReadStream, WriteStream, FSWatcher, symlink as _symlink } from 'node:fs';
3
+ import type { ReadStream, WriteStream, FSWatcher, symlink as _symlink, BaseEncodingOptions, BufferEncodingOption, BigIntOptions } from 'node:fs';
4
4
  import * as constants from './constants.js';
5
5
  export { constants };
6
+ import { PathLike } from './shared.js';
6
7
  import { FileContents } from '../filesystem.js';
7
- import { Stats } from '../stats.js';
8
+ import { BigIntStats, Stats } from '../stats.js';
9
+ import { Dirent } from './dir.js';
8
10
  /**
9
11
  * Renames a file
10
12
  * @param oldPath
11
13
  * @param newPath
12
14
  */
13
- export declare function rename(oldPath: string, newPath: string): Promise<void>;
15
+ export declare function rename(oldPath: PathLike, newPath: PathLike): Promise<void>;
14
16
  /**
15
17
  * Test whether or not the given path exists by checking with the file system.
16
18
  * @param path
17
19
  */
18
- export declare function exists(path: string): Promise<boolean>;
20
+ export declare function exists(path: PathLike): Promise<boolean>;
19
21
  /**
20
22
  * `stat`.
21
23
  * @param path
22
24
  * @returns Stats
23
25
  */
24
- export declare function stat(path: string): Promise<Stats>;
26
+ export declare function stat(path: PathLike, options: BigIntOptions): Promise<BigIntStats>;
27
+ export declare function stat(path: PathLike, options?: {
28
+ bigint?: false;
29
+ }): Promise<Stats>;
25
30
  /**
26
31
  * `lstat`.
27
32
  * `lstat()` is identical to `stat()`, except that if path is a symbolic link,
@@ -29,18 +34,23 @@ export declare function stat(path: string): Promise<Stats>;
29
34
  * @param path
30
35
  * @return [ZenFS.node.fs.Stats]
31
36
  */
32
- export declare function lstat(path: string): Promise<Stats>;
37
+ export declare function lstat(path: PathLike, options?: {
38
+ bigint?: false;
39
+ }): Promise<Stats>;
40
+ export declare function lstat(path: PathLike, options: {
41
+ bigint: true;
42
+ }): Promise<BigIntStats>;
33
43
  /**
34
44
  * `truncate`.
35
45
  * @param path
36
46
  * @param len
37
47
  */
38
- export declare function truncate(path: string, len?: number): Promise<void>;
48
+ export declare function truncate(path: PathLike, len?: number): Promise<void>;
39
49
  /**
40
50
  * `unlink`.
41
51
  * @param path
42
52
  */
43
- export declare function unlink(path: string): Promise<void>;
53
+ export declare function unlink(path: PathLike): Promise<void>;
44
54
  /**
45
55
  * file open.
46
56
  * @see http://www.manpagez.com/man/2/open/
@@ -48,23 +58,23 @@ export declare function unlink(path: string): Promise<void>;
48
58
  * @param flags
49
59
  * @param mode defaults to `0644`
50
60
  */
51
- export declare function open(path: string, flag: string, mode?: number | string): Promise<number>;
61
+ export declare function open(path: PathLike, flag: string, mode?: number | string): Promise<number>;
52
62
  /**
53
63
  * Synchronously reads the entire contents of a file.
54
64
  * @param filename
55
65
  * @param options
56
- * @option options [String] encoding The string encoding for the file contents. Defaults to `null`.
57
- * @option options [String] flag Defaults to `'r'`.
58
- * @return [String | ZenFS.node.Uint8Array]
66
+ * options.encoding The string encoding for the file contents. Defaults to `null`.
67
+ * options.flag Defaults to `'r'`.
68
+ * @return Uint8Array
59
69
  */
60
- export declare function readFile(filename: string, options?: {
70
+ export declare function readFile(filename: PathLike, options?: {
61
71
  flag?: string;
62
72
  }): Promise<Uint8Array>;
63
- export declare function readFile(filename: string, options: {
73
+ export declare function readFile(filename: PathLike, options: {
64
74
  encoding: string;
65
75
  flag?: string;
66
76
  }): Promise<string>;
67
- export declare function readFile(filename: string, encoding: string): Promise<string>;
77
+ export declare function readFile(filename: PathLike, encoding: string): Promise<string>;
68
78
  /**
69
79
  * Synchronously writes data to a file, replacing the file if it already
70
80
  * exists.
@@ -77,13 +87,13 @@ export declare function readFile(filename: string, encoding: string): Promise<st
77
87
  * @option options [Number] mode Defaults to `0644`.
78
88
  * @option options [String] flag Defaults to `'w'`.
79
89
  */
80
- export declare function writeFile(filename: string, data: FileContents, options?: {
90
+ export declare function writeFile(filename: PathLike, data: FileContents, options?: {
81
91
  encoding?: string;
82
92
  mode?: number | string;
83
93
  flag?: string;
84
94
  }): Promise<void>;
85
- export declare function writeFile(filename: string, data: FileContents, encoding?: string): Promise<void>;
86
- export declare function writeFile(filename: string, data: FileContents, options?: {
95
+ export declare function writeFile(filename: PathLike, data: FileContents, encoding?: string): Promise<void>;
96
+ export declare function writeFile(filename: PathLike, data: FileContents, options?: {
87
97
  encoding?: string;
88
98
  mode?: number | string;
89
99
  flag?: string;
@@ -104,12 +114,12 @@ export declare function writeFile(filename: string, data: FileContents, options?
104
114
  * @option options [Number] mode Defaults to `0644`.
105
115
  * @option options [String] flag Defaults to `'a'`.
106
116
  */
107
- export declare function appendFile(filename: string, data: FileContents, options?: {
117
+ export declare function appendFile(filename: PathLike, data: FileContents, options?: {
108
118
  encoding?: string;
109
119
  mode?: number | string;
110
120
  flag?: string;
111
121
  }): Promise<void>;
112
- export declare function appendFile(filename: string, data: FileContents, encoding?: string): Promise<void>;
122
+ export declare function appendFile(filename: PathLike, data: FileContents, encoding?: string): Promise<void>;
113
123
  /**
114
124
  * `fstat`.
115
125
  * `fstat()` is identical to `stat()`, except that the file to be stat-ed is
@@ -117,7 +127,12 @@ export declare function appendFile(filename: string, data: FileContents, encodin
117
127
  * @param fd
118
128
  * @return [ZenFS.node.fs.Stats]
119
129
  */
120
- export declare function fstat(fd: number): Promise<Stats>;
130
+ export declare function fstat(fd: number, options?: {
131
+ bigint?: false;
132
+ }): Promise<Stats>;
133
+ export declare function fstat(fd: number, options: {
134
+ bigint: true;
135
+ }): Promise<BigIntStats>;
121
136
  /**
122
137
  * close.
123
138
  * @param fd
@@ -195,97 +210,100 @@ export declare function futimes(fd: number, atime: number | Date, mtime: number
195
210
  * `rmdir`.
196
211
  * @param path
197
212
  */
198
- export declare function rmdir(path: string): Promise<void>;
213
+ export declare function rmdir(path: PathLike): Promise<void>;
199
214
  /**
200
215
  * `mkdir`.
201
216
  * @param path
202
217
  * @param mode defaults to `0777`
203
218
  */
204
- export declare function mkdir(path: string, mode?: number | string): Promise<void>;
219
+ export declare function mkdir(path: PathLike, mode?: number | string): Promise<void>;
205
220
  /**
206
221
  * `readdir`. Reads the contents of a directory.
207
222
  * @param path
208
- * @return [String[]]
209
223
  */
210
- export declare function readdir(path: string): Promise<string[]>;
224
+ export declare function readdir(path: PathLike, options: {
225
+ withFileTypes?: false;
226
+ }): Promise<string[]>;
227
+ export declare function readdir(path: PathLike, options: {
228
+ withFileTypes: true;
229
+ }): Promise<Dirent[]>;
211
230
  /**
212
231
  * `link`.
213
232
  * @param srcpath
214
233
  * @param dstpath
215
234
  */
216
- export declare function link(srcpath: string, dstpath: string): Promise<void>;
235
+ export declare function link(srcpath: PathLike, dstpath: PathLike): Promise<void>;
217
236
  /**
218
237
  * `symlink`.
219
238
  * @param srcpath
220
239
  * @param dstpath
221
240
  * @param type can be either `'dir'` or `'file'` (default is `'file'`)
222
241
  */
223
- export declare function symlink(srcpath: string, dstpath: string, type?: _symlink.Type): Promise<void>;
242
+ export declare function symlink(srcpath: PathLike, dstpath: PathLike, type?: _symlink.Type): Promise<void>;
224
243
  /**
225
244
  * readlink.
226
245
  * @param path
227
- * @return [String]
228
246
  */
229
- export declare function readlink(path: string): Promise<string>;
247
+ export declare function readlink(path: PathLike, options?: BaseEncodingOptions | BufferEncoding): Promise<string>;
248
+ export declare function readlink(path: PathLike, options: BufferEncodingOption): Promise<Uint8Array>;
249
+ export declare function readlink(path: PathLike, options?: BaseEncodingOptions | string): Promise<string | Uint8Array>;
230
250
  /**
231
251
  * `chown`.
232
252
  * @param path
233
253
  * @param uid
234
254
  * @param gid
235
255
  */
236
- export declare function chown(path: string, uid: number, gid: number): Promise<void>;
256
+ export declare function chown(path: PathLike, uid: number, gid: number): Promise<void>;
237
257
  /**
238
258
  * `lchown`.
239
259
  * @param path
240
260
  * @param uid
241
261
  * @param gid
242
262
  */
243
- export declare function lchown(path: string, uid: number, gid: number): Promise<void>;
263
+ export declare function lchown(path: PathLike, uid: number, gid: number): Promise<void>;
244
264
  /**
245
265
  * `chmod`.
246
266
  * @param path
247
267
  * @param mode
248
268
  */
249
- export declare function chmod(path: string, mode: string | number): Promise<void>;
269
+ export declare function chmod(path: PathLike, mode: string | number): Promise<void>;
250
270
  /**
251
271
  * `lchmod`.
252
272
  * @param path
253
273
  * @param mode
254
274
  */
255
- export declare function lchmod(path: string, mode: number | string): Promise<void>;
275
+ export declare function lchmod(path: PathLike, mode: number | string): Promise<void>;
256
276
  /**
257
277
  * Change file timestamps of the file referenced by the supplied path.
258
278
  * @param path
259
279
  * @param atime
260
280
  * @param mtime
261
281
  */
262
- export declare function utimes(path: string, atime: number | Date, mtime: number | Date): Promise<void>;
282
+ export declare function utimes(path: PathLike, atime: number | Date, mtime: number | Date): Promise<void>;
263
283
  /**
264
284
  * Change file timestamps of the file referenced by the supplied path.
265
285
  * @param path
266
286
  * @param atime
267
287
  * @param mtime
268
288
  */
269
- export declare function lutimes(path: string, atime: number | Date, mtime: number | Date): Promise<void>;
289
+ export declare function lutimes(path: PathLike, atime: number | Date, mtime: number | Date): Promise<void>;
270
290
  /**
271
291
  * `realpath`.
272
292
  * @param path
273
- * @param cache An object literal of mapped paths that can be used to
274
- * force a specific path resolution or avoid additional `fs.stat` calls for
275
- * known real paths.
276
- * @return [String]
293
+ * @param options
294
+ * @return resolved path
295
+ *
296
+ * Note: This *Can not* use doOp since doOp depends on it
277
297
  */
278
- export declare function realpath(path: string, cache?: {
279
- [path: string]: string;
280
- }): Promise<string>;
281
- export declare function watchFile(filename: string, listener: (curr: Stats, prev: Stats) => void): Promise<void>;
282
- export declare function watchFile(filename: string, options: {
298
+ export declare function realpath(path: PathLike, options?: BaseEncodingOptions): Promise<string>;
299
+ export declare function watchFile(filename: PathLike, listener: (curr: Stats, prev: Stats) => void): Promise<void>;
300
+ export declare function watchFile(filename: PathLike, options: {
283
301
  persistent?: boolean;
284
302
  interval?: number;
285
303
  }, listener: (curr: Stats, prev: Stats) => void): Promise<void>;
286
- export declare function unwatchFile(filename: string, listener?: (curr: Stats, prev: Stats) => void): Promise<void>;
287
- export declare function watch(filename: string, listener?: (event: string, filename: string) => any): Promise<FSWatcher>;
288
- export declare function watch(filename: string, options: {
304
+ export declare function unwatchFile(filename: PathLike, listener?: (curr: Stats, prev: Stats) => void): Promise<void>;
305
+ export declare function watch(filename: PathLike, listener?: (event: string, filename: PathLike) => any): Promise<FSWatcher>;
306
+ export declare function watch(filename: PathLike, options: {
289
307
  persistent?: boolean;
290
308
  }, listener?: (event: string, filename: string) => any): Promise<FSWatcher>;
291
309
  /**
@@ -293,17 +311,20 @@ export declare function watch(filename: string, options: {
293
311
  * @param path
294
312
  * @param mode
295
313
  */
296
- export declare function access(path: string, mode?: number): Promise<void>;
297
- export declare function createReadStream(path: string, options?: {
314
+ export declare function access(path: PathLike, mode?: number): Promise<void>;
315
+ export declare function createReadStream(path: PathLike, options?: {
298
316
  flags?: string;
299
317
  encoding?: string;
300
318
  fd?: number;
301
319
  mode?: number;
302
320
  autoClose?: boolean;
303
321
  }): Promise<ReadStream>;
304
- export declare function createWriteStream(path: string, options?: {
322
+ export declare function createWriteStream(path: PathLike, options?: {
305
323
  flags?: string;
306
324
  encoding?: string;
307
325
  fd?: number;
308
326
  mode?: number;
309
327
  }): Promise<WriteStream>;
328
+ export declare function rm(path: PathLike): Promise<void>;
329
+ export declare function mkdtemp(path: PathLike): Promise<void>;
330
+ export declare function copyFile(path: PathLike): Promise<void>;