@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.
@@ -60,3 +60,17 @@ export declare function fixError<E extends Error>(e: E, paths: {
60
60
  [from: string]: string;
61
61
  }): E;
62
62
  export declare function initialize(mountMapping: MountMapping): void;
63
+ /**
64
+ * Types supports as path parameters.
65
+ *
66
+ * In the future, maybe support URL?
67
+ */
68
+ export type PathLike = string;
69
+ /**
70
+ * @internal
71
+ *
72
+ * Converts any Buffer in T to Uint8Array
73
+ */
74
+ export type BufferToUint8Array<T> = T extends Buffer ? Uint8Array : T extends (...args: any[]) => unknown ? (...args: Parameters<T>) => BufferToUint8Array<ReturnType<T>> : T extends object ? {
75
+ [K in keyof T]: BufferToUint8Array<T[K]>;
76
+ } : T;
@@ -2,7 +2,6 @@
2
2
  import { resolve } from './path.js';
3
3
  import { ApiError, ErrorCode } from '../ApiError.js';
4
4
  import { Cred } from '../cred.js';
5
- //import { BackendConstructor } from '../backends.js';
6
5
  import { InMemoryFileSystem } from '../backends/InMemory.js';
7
6
  /**
8
7
  * converts Date or number to a fractional UNIX timestamp
@@ -170,12 +169,14 @@ export function resolveFS(path) {
170
169
  */
171
170
  export function fixPaths(text, paths) {
172
171
  for (const [from, to] of Object.entries(paths)) {
173
- text = text.replaceAll(from, to);
172
+ text = text?.replaceAll(from, to);
174
173
  }
175
174
  return text;
176
175
  }
177
176
  export function fixError(e, paths) {
178
- e.stack = fixPaths(e.stack, paths);
177
+ if (typeof e.stack == 'string') {
178
+ e.stack = fixPaths(e.stack, paths);
179
+ }
179
180
  e.message = fixPaths(e.message, paths);
180
181
  return e;
181
182
  }
@@ -0,0 +1,102 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import type * as Node from 'fs';
4
+ import { Readable, Writable } from 'readable-stream';
5
+ import { NoArgCallback } from '../filesystem.js';
6
+ export declare class ReadStream extends Readable implements Node.ReadStream {
7
+ close(callback?: NoArgCallback): void;
8
+ bytesRead: number;
9
+ path: string | Buffer;
10
+ pending: boolean;
11
+ addListener(event: 'close', listener: () => void): this;
12
+ addListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
13
+ addListener(event: 'end', listener: () => void): this;
14
+ addListener(event: 'error', listener: (err: Error) => void): this;
15
+ addListener(event: 'open', listener: (fd: number) => void): this;
16
+ addListener(event: 'pause', listener: () => void): this;
17
+ addListener(event: 'readable', listener: () => void): this;
18
+ addListener(event: 'ready', listener: () => void): this;
19
+ addListener(event: 'resume', listener: () => void): this;
20
+ on(event: 'close', listener: () => void): this;
21
+ on(event: 'data', listener: (chunk: Buffer | string) => void): this;
22
+ on(event: 'end', listener: () => void): this;
23
+ on(event: 'error', listener: (err: Error) => void): this;
24
+ on(event: 'open', listener: (fd: number) => void): this;
25
+ on(event: 'pause', listener: () => void): this;
26
+ on(event: 'readable', listener: () => void): this;
27
+ on(event: 'ready', listener: () => void): this;
28
+ on(event: 'resume', listener: () => void): this;
29
+ once(event: 'close', listener: () => void): this;
30
+ once(event: 'data', listener: (chunk: Buffer | string) => void): this;
31
+ once(event: 'end', listener: () => void): this;
32
+ once(event: 'error', listener: (err: Error) => void): this;
33
+ once(event: 'open', listener: (fd: number) => void): this;
34
+ once(event: 'pause', listener: () => void): this;
35
+ once(event: 'readable', listener: () => void): this;
36
+ once(event: 'ready', listener: () => void): this;
37
+ once(event: 'resume', listener: () => void): this;
38
+ prependListener(event: 'close', listener: () => void): this;
39
+ prependListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
40
+ prependListener(event: 'end', listener: () => void): this;
41
+ prependListener(event: 'error', listener: (err: Error) => void): this;
42
+ prependListener(event: 'open', listener: (fd: number) => void): this;
43
+ prependListener(event: 'pause', listener: () => void): this;
44
+ prependListener(event: 'readable', listener: () => void): this;
45
+ prependListener(event: 'ready', listener: () => void): this;
46
+ prependListener(event: 'resume', listener: () => void): this;
47
+ prependOnceListener(event: 'close', listener: () => void): this;
48
+ prependOnceListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
49
+ prependOnceListener(event: 'end', listener: () => void): this;
50
+ prependOnceListener(event: 'error', listener: (err: Error) => void): this;
51
+ prependOnceListener(event: 'open', listener: (fd: number) => void): this;
52
+ prependOnceListener(event: 'pause', listener: () => void): this;
53
+ prependOnceListener(event: 'readable', listener: () => void): this;
54
+ prependOnceListener(event: 'ready', listener: () => void): this;
55
+ prependOnceListener(event: 'resume', listener: () => void): this;
56
+ }
57
+ export declare class WriteStream extends Writable implements Node.WriteStream {
58
+ close(callback?: NoArgCallback): void;
59
+ bytesWritten: number;
60
+ path: string | Buffer;
61
+ pending: boolean;
62
+ addListener(event: 'close', listener: () => void): this;
63
+ addListener(event: 'drain', listener: () => void): this;
64
+ addListener(event: 'error', listener: (err: Error) => void): this;
65
+ addListener(event: 'finish', listener: () => void): this;
66
+ addListener(event: 'open', listener: (fd: number) => void): this;
67
+ addListener(event: 'pipe', listener: (src: Readable) => void): this;
68
+ addListener(event: 'ready', listener: () => void): this;
69
+ addListener(event: 'unpipe', listener: (src: Readable) => void): this;
70
+ on(event: 'close', listener: () => void): this;
71
+ on(event: 'drain', listener: () => void): this;
72
+ on(event: 'error', listener: (err: Error) => void): this;
73
+ on(event: 'finish', listener: () => void): this;
74
+ on(event: 'open', listener: (fd: number) => void): this;
75
+ on(event: 'pipe', listener: (src: Readable) => void): this;
76
+ on(event: 'ready', listener: () => void): this;
77
+ on(event: 'unpipe', listener: (src: Readable) => void): this;
78
+ once(event: 'close', listener: () => void): this;
79
+ once(event: 'drain', listener: () => void): this;
80
+ once(event: 'error', listener: (err: Error) => void): this;
81
+ once(event: 'finish', listener: () => void): this;
82
+ once(event: 'open', listener: (fd: number) => void): this;
83
+ once(event: 'pipe', listener: (src: Readable) => void): this;
84
+ once(event: 'ready', listener: () => void): this;
85
+ once(event: 'unpipe', listener: (src: Readable) => void): this;
86
+ prependListener(event: 'close', listener: () => void): this;
87
+ prependListener(event: 'drain', listener: () => void): this;
88
+ prependListener(event: 'error', listener: (err: Error) => void): this;
89
+ prependListener(event: 'finish', listener: () => void): this;
90
+ prependListener(event: 'open', listener: (fd: number) => void): this;
91
+ prependListener(event: 'pipe', listener: (src: Readable) => void): this;
92
+ prependListener(event: 'ready', listener: () => void): this;
93
+ prependListener(event: 'unpipe', listener: (src: Readable) => void): this;
94
+ prependOnceListener(event: 'close', listener: () => void): this;
95
+ prependOnceListener(event: 'drain', listener: () => void): this;
96
+ prependOnceListener(event: 'error', listener: (err: Error) => void): this;
97
+ prependOnceListener(event: 'finish', listener: () => void): this;
98
+ prependOnceListener(event: 'open', listener: (fd: number) => void): this;
99
+ prependOnceListener(event: 'pipe', listener: (src: Readable) => void): this;
100
+ prependOnceListener(event: 'ready', listener: () => void): this;
101
+ prependOnceListener(event: 'unpipe', listener: (src: Readable) => void): this;
102
+ }
@@ -0,0 +1,55 @@
1
+ import { Readable, Writable } from 'readable-stream';
2
+ export class ReadStream extends Readable {
3
+ close(callback = () => null) {
4
+ try {
5
+ super.destroy();
6
+ super.emit('close');
7
+ callback();
8
+ }
9
+ catch (err) {
10
+ callback(err);
11
+ }
12
+ }
13
+ addListener(event, listener) {
14
+ return super.addListener(event, listener);
15
+ }
16
+ on(event, listener) {
17
+ return super.on(event, listener);
18
+ }
19
+ once(event, listener) {
20
+ return super.once(event, listener);
21
+ }
22
+ prependListener(event, listener) {
23
+ return super.prependListener(event, listener);
24
+ }
25
+ prependOnceListener(event, listener) {
26
+ return super.prependOnceListener(event, listener);
27
+ }
28
+ }
29
+ export class WriteStream extends Writable {
30
+ close(callback = () => null) {
31
+ try {
32
+ super.destroy();
33
+ super.emit('close');
34
+ callback();
35
+ }
36
+ catch (err) {
37
+ callback(err);
38
+ }
39
+ }
40
+ addListener(event, listener) {
41
+ return super.addListener(event, listener);
42
+ }
43
+ on(event, listener) {
44
+ return super.on(event, listener);
45
+ }
46
+ once(event, listener) {
47
+ return super.once(event, listener);
48
+ }
49
+ prependListener(event, listener) {
50
+ return super.prependListener(event, listener);
51
+ }
52
+ prependOnceListener(event, listener) {
53
+ return super.prependOnceListener(event, listener);
54
+ }
55
+ }
@@ -1,60 +1,72 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  /// <reference types="node" resolution-mode="require"/>
3
3
  import { FileContents } from '../filesystem.js';
4
- import { Stats } from '../stats.js';
5
- import type { symlink, ReadSyncOptions } from 'fs';
4
+ import { BigIntStats, Stats } from '../stats.js';
5
+ import type { symlink, ReadSyncOptions, BaseEncodingOptions, BufferEncodingOption } from 'fs';
6
+ import type * as Node from 'fs';
7
+ import { PathLike } from './shared.js';
8
+ import { Dir, Dirent } from './dir.js';
6
9
  /**
7
10
  * Synchronous rename.
8
11
  * @param oldPath
9
12
  * @param newPath
10
13
  */
11
- export declare function renameSync(oldPath: string, newPath: string): void;
14
+ export declare function renameSync(oldPath: PathLike, newPath: PathLike): void;
12
15
  /**
13
16
  * Test whether or not the given path exists by checking with the file system.
14
17
  * @param path
15
18
  */
16
- export declare function existsSync(path: string): boolean;
19
+ export declare function existsSync(path: PathLike): boolean;
17
20
  /**
18
21
  * Synchronous `stat`.
19
22
  * @param path
20
23
  * @returns Stats
21
24
  */
22
- export declare function statSync(path: string): Stats;
25
+ export declare function statSync(path: PathLike, options?: {
26
+ bigint: false;
27
+ }): Stats;
28
+ export declare function statSync(path: PathLike, options: {
29
+ bigint: true;
30
+ }): BigIntStats;
23
31
  /**
24
32
  * Synchronous `lstat`.
25
33
  * `lstat()` is identical to `stat()`, except that if path is a symbolic link,
26
34
  * then the link itself is stat-ed, not the file that it refers to.
27
35
  * @param path
28
- * @return [ZenFS.node.fs.Stats]
29
36
  */
30
- export declare function lstatSync(path: string): Stats;
37
+ export declare function lstatSync(path: PathLike, options?: {
38
+ bigint: false;
39
+ }): Stats;
40
+ export declare function lstatSync(path: PathLike, options: {
41
+ bigint: true;
42
+ }): BigIntStats;
31
43
  /**
32
44
  * Synchronous `truncate`.
33
45
  * @param path
34
46
  * @param len
35
47
  */
36
- export declare function truncateSync(path: string, len?: number): void;
48
+ export declare function truncateSync(path: PathLike, len?: number): void;
37
49
  /**
38
50
  * Synchronous `unlink`.
39
51
  * @param path
40
52
  */
41
- export declare function unlinkSync(path: string): void;
53
+ export declare function unlinkSync(path: PathLike): void;
42
54
  /**
43
55
  * Synchronous file open.
44
56
  * @see http://www.manpagez.com/man/2/open/
45
57
  * @param path
46
58
  * @param flags
47
59
  * @param mode defaults to `0644`
48
- * @return [ZenFS.File]
60
+ * @returns file descriptor
49
61
  */
50
- export declare function openSync(path: string, flag: string, mode?: number | string): number;
62
+ export declare function openSync(path: PathLike, flag: string, mode?: number | string): number;
51
63
  /**
52
64
  * Synchronously reads the entire contents of a file.
53
65
  * @param filename
54
66
  * @param options
55
- * @option options [String] encoding The string encoding for the file contents. Defaults to `null`.
56
- * @option options [String] flag Defaults to `'r'`.
57
- * @return [String | ZenFS.node.Uint8Array]
67
+ * @option options encoding The string encoding for the file contents. Defaults to `null`.
68
+ * @option options flag Defaults to `'r'`.
69
+ * @returns file contents
58
70
  */
59
71
  export declare function readFileSync(filename: string, options?: {
60
72
  flag?: string;
@@ -72,15 +84,11 @@ export declare function readFileSync(filename: string, encoding: string): string
72
84
  * @param filename
73
85
  * @param data
74
86
  * @param options
75
- * @option options [String] encoding Defaults to `'utf8'`.
76
- * @option options [Number] mode Defaults to `0644`.
77
- * @option options [String] flag Defaults to `'w'`.
87
+ * @option options encoding Defaults to `'utf8'`.
88
+ * @option options mode Defaults to `0644`.
89
+ * @option options flag Defaults to `'w'`.
78
90
  */
79
- export declare function writeFileSync(filename: string, data: FileContents, options?: {
80
- encoding?: string;
81
- mode?: number | string;
82
- flag?: string;
83
- }): void;
91
+ export declare function writeFileSync(filename: string, data: FileContents, options?: Node.WriteFileOptions): void;
84
92
  export declare function writeFileSync(filename: string, data: FileContents, encoding?: string): void;
85
93
  /**
86
94
  * Asynchronously append data to a file, creating the file if it not yet
@@ -94,24 +102,24 @@ export declare function writeFileSync(filename: string, data: FileContents, enco
94
102
  * @param filename
95
103
  * @param data
96
104
  * @param options
97
- * @option options [String] encoding Defaults to `'utf8'`.
98
- * @option options [Number] mode Defaults to `0644`.
99
- * @option options [String] flag Defaults to `'a'`.
105
+ * @option options encoding Defaults to `'utf8'`.
106
+ * @option options mode Defaults to `0644`.
107
+ * @option options flag Defaults to `'a'`.
100
108
  */
101
- export declare function appendFileSync(filename: string, data: FileContents, options?: {
102
- encoding?: string;
103
- mode?: number | string;
104
- flag?: string;
105
- }): void;
109
+ export declare function appendFileSync(filename: string, data: FileContents, options?: Node.WriteFileOptions): void;
106
110
  export declare function appendFileSync(filename: string, data: FileContents, encoding?: string): void;
107
111
  /**
108
112
  * Synchronous `fstat`.
109
113
  * `fstat()` is identical to `stat()`, except that the file to be stat-ed is
110
114
  * specified by the file descriptor `fd`.
111
115
  * @param fd
112
- * @return [ZenFS.node.fs.Stats]
113
116
  */
114
- export declare function fstatSync(fd: number): Stats;
117
+ export declare function fstatSync(fd: number, options?: {
118
+ bigint?: false;
119
+ }): Stats;
120
+ export declare function fstatSync(fd: number, options: {
121
+ bigint: true;
122
+ }): BigIntStats;
115
123
  /**
116
124
  * Synchronous close.
117
125
  * @param fd
@@ -187,92 +195,113 @@ export declare function futimesSync(fd: number, atime: number | Date, mtime: num
187
195
  * Synchronous `rmdir`.
188
196
  * @param path
189
197
  */
190
- export declare function rmdirSync(path: string): void;
198
+ export declare function rmdirSync(path: PathLike): void;
191
199
  /**
192
200
  * Synchronous `mkdir`.
193
201
  * @param path
194
- * @param mode defaults to `0777`
202
+ * @param mode defaults to o777
203
+ * @todo Implement recursion
195
204
  */
196
- export declare function mkdirSync(path: string, mode?: number | string): void;
205
+ export declare function mkdirSync(path: PathLike, options: Node.MakeDirectoryOptions & {
206
+ recursive: true;
207
+ }): string;
208
+ export declare function mkdirSync(path: PathLike, options?: Node.Mode | (Node.MakeDirectoryOptions & {
209
+ recursive?: false;
210
+ })): void;
197
211
  /**
198
212
  * Synchronous `readdir`. Reads the contents of a directory.
199
213
  * @param path
200
- * @return [String[]]
201
214
  */
202
- export declare function readdirSync(path: string): string[];
215
+ export declare function readdirSync(path: PathLike, options: {
216
+ encoding?: BufferEncoding;
217
+ withFileTypes?: false;
218
+ } | BufferEncoding): string[];
219
+ export declare function readdirSync(path: PathLike, options: {
220
+ encoding: 'buffer';
221
+ withFileTypes?: false;
222
+ } | 'buffer'): Uint8Array[];
223
+ export declare function readdirSync(path: PathLike, options: {
224
+ withFileTypes: true;
225
+ }): Dirent[];
203
226
  /**
204
227
  * Synchronous `link`.
205
228
  * @param srcpath
206
229
  * @param dstpath
207
230
  */
208
- export declare function linkSync(srcpath: string, dstpath: string): void;
231
+ export declare function linkSync(srcpath: PathLike, dstpath: PathLike): void;
209
232
  /**
210
233
  * Synchronous `symlink`.
211
234
  * @param srcpath
212
235
  * @param dstpath
213
236
  * @param type can be either `'dir'` or `'file'` (default is `'file'`)
214
237
  */
215
- export declare function symlinkSync(srcpath: string, dstpath: string, type?: symlink.Type): void;
238
+ export declare function symlinkSync(srcpath: PathLike, dstpath: PathLike, type?: symlink.Type): void;
216
239
  /**
217
240
  * Synchronous readlink.
218
241
  * @param path
219
- * @return [String]
220
242
  */
221
- export declare function readlinkSync(path: string): string;
243
+ export declare function readlinkSync(path: PathLike, options?: BufferEncodingOption): Uint8Array;
244
+ export declare function readlinkSync(path: PathLike, options: BaseEncodingOptions | BufferEncoding): string;
222
245
  /**
223
246
  * Synchronous `chown`.
224
247
  * @param path
225
248
  * @param uid
226
249
  * @param gid
227
250
  */
228
- export declare function chownSync(path: string, uid: number, gid: number): void;
251
+ export declare function chownSync(path: PathLike, uid: number, gid: number): void;
229
252
  /**
230
253
  * Synchronous `lchown`.
231
254
  * @param path
232
255
  * @param uid
233
256
  * @param gid
234
257
  */
235
- export declare function lchownSync(path: string, uid: number, gid: number): void;
258
+ export declare function lchownSync(path: PathLike, uid: number, gid: number): void;
236
259
  /**
237
260
  * Synchronous `chmod`.
238
261
  * @param path
239
262
  * @param mode
240
263
  */
241
- export declare function chmodSync(path: string, mode: string | number): void;
264
+ export declare function chmodSync(path: PathLike, mode: string | number): void;
242
265
  /**
243
266
  * Synchronous `lchmod`.
244
267
  * @param path
245
268
  * @param mode
246
269
  */
247
- export declare function lchmodSync(path: string, mode: number | string): void;
270
+ export declare function lchmodSync(path: PathLike, mode: number | string): void;
248
271
  /**
249
272
  * Change file timestamps of the file referenced by the supplied path.
250
273
  * @param path
251
274
  * @param atime
252
275
  * @param mtime
253
276
  */
254
- export declare function utimesSync(path: string, atime: number | Date, mtime: number | Date): void;
277
+ export declare function utimesSync(path: PathLike, atime: number | Date, mtime: number | Date): void;
255
278
  /**
256
279
  * Change file timestamps of the file referenced by the supplied path.
257
280
  * @param path
258
281
  * @param atime
259
282
  * @param mtime
260
283
  */
261
- export declare function lutimesSync(path: string, atime: number | Date, mtime: number | Date): void;
284
+ export declare function lutimesSync(path: PathLike, atime: number | Date, mtime: number | Date): void;
262
285
  /**
263
286
  * Synchronous `realpath`.
264
287
  * @param path
265
288
  * @param cache An object literal of mapped paths that can be used to
266
289
  * force a specific path resolution or avoid additional `fs.stat` calls for
267
290
  * known real paths.
268
- * @return [String]
291
+ * @returns the real path
269
292
  */
270
- export declare function realpathSync(path: string, cache?: {
271
- [path: string]: string;
272
- }): string;
293
+ export declare function realpathSync(path: PathLike, options: BufferEncodingOption): Uint8Array;
294
+ export declare function realpathSync(path: PathLike, options?: BaseEncodingOptions | BufferEncoding): string;
273
295
  /**
274
296
  * Synchronous `access`.
275
297
  * @param path
276
298
  * @param mode
277
299
  */
278
- export declare function accessSync(path: string, mode?: number): void;
300
+ export declare function accessSync(path: PathLike, mode?: number): void;
301
+ export declare function rmSync(path: PathLike): void;
302
+ export declare function mkdtempSync(prefix: string, options: BufferEncodingOption): Uint8Array;
303
+ export declare function mkdtempSync(prefix: string, options?: BaseEncodingOptions | BufferEncoding): string;
304
+ export declare function copyFileSync(src: string, dest: string, flags?: number): void;
305
+ export declare function readvSync(fd: number, buffers: readonly Uint8Array[], position?: number): number;
306
+ export declare function writevSync(fd: number, buffers: readonly Uint8Array[], position?: number): number;
307
+ export declare function opendirSync(path: PathLike, options?: Node.OpenDirOptions): Dir;