@zenfs/core 0.1.0 → 0.2.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.
Files changed (44) hide show
  1. package/dist/ApiError.d.ts +51 -14
  2. package/dist/ApiError.js +60 -34
  3. package/dist/FileIndex.d.ts +32 -35
  4. package/dist/FileIndex.js +93 -109
  5. package/dist/backends/AsyncMirror.d.ts +42 -43
  6. package/dist/backends/AsyncMirror.js +146 -133
  7. package/dist/backends/AsyncStore.d.ts +29 -28
  8. package/dist/backends/AsyncStore.js +139 -189
  9. package/dist/backends/InMemory.d.ts +16 -13
  10. package/dist/backends/InMemory.js +29 -14
  11. package/dist/backends/Locked.d.ts +8 -28
  12. package/dist/backends/Locked.js +44 -148
  13. package/dist/backends/OverlayFS.d.ts +26 -34
  14. package/dist/backends/OverlayFS.js +208 -371
  15. package/dist/backends/SyncStore.d.ts +54 -72
  16. package/dist/backends/SyncStore.js +159 -161
  17. package/dist/backends/backend.d.ts +45 -29
  18. package/dist/backends/backend.js +83 -13
  19. package/dist/backends/index.d.ts +6 -7
  20. package/dist/backends/index.js +5 -6
  21. package/dist/browser.min.js +5 -7
  22. package/dist/browser.min.js.map +4 -4
  23. package/dist/emulation/callbacks.d.ts +36 -67
  24. package/dist/emulation/callbacks.js +90 -46
  25. package/dist/emulation/constants.js +1 -1
  26. package/dist/emulation/promises.d.ts +228 -129
  27. package/dist/emulation/promises.js +414 -172
  28. package/dist/emulation/shared.d.ts +10 -10
  29. package/dist/emulation/shared.js +18 -20
  30. package/dist/emulation/sync.d.ts +25 -25
  31. package/dist/emulation/sync.js +187 -73
  32. package/dist/file.d.ts +166 -170
  33. package/dist/file.js +199 -218
  34. package/dist/filesystem.d.ts +68 -241
  35. package/dist/filesystem.js +59 -383
  36. package/dist/index.d.ts +7 -44
  37. package/dist/index.js +13 -52
  38. package/dist/inode.d.ts +37 -28
  39. package/dist/inode.js +123 -65
  40. package/dist/stats.d.ts +21 -19
  41. package/dist/stats.js +35 -56
  42. package/dist/utils.d.ts +26 -9
  43. package/dist/utils.js +73 -102
  44. package/package.json +4 -3
@@ -1,15 +1,14 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import { Cred } from '../cred.js';
3
3
  import { FileSystem } from '../filesystem.js';
4
- import { File } from '../file.js';
5
- import { BackendConstructor } from '../backends/backend.js';
4
+ import type { File } from '../file.js';
6
5
  /**
7
- * converts Date or number to a fractional UNIX timestamp
6
+ * converts Date or number to a integer UNIX timestamp
8
7
  * Grabbed from NodeJS sources (lib/fs.js)
9
8
  */
10
9
  export declare function _toUnixTimestamp(time: Date | number): number;
11
- export declare function normalizeMode(mode: unknown, def: number): number;
12
- export declare function normalizeTime(time: number | Date): Date;
10
+ export declare function normalizeMode(mode: string | number | unknown, def?: number): number;
11
+ export declare function normalizeTime(time: string | number | Date): Date;
13
12
  export declare function normalizePath(p: string): string;
14
13
  export declare function normalizeOptions(options: any, defEnc: string | null, defFlag: string, defMode: number | null): {
15
14
  encoding: BufferEncoding;
@@ -23,7 +22,7 @@ export declare const fdMap: Map<number, File>;
23
22
  export declare function getFdForFile(file: File): number;
24
23
  export declare function fd2file(fd: number): File;
25
24
  export interface MountMapping {
26
- [point: string]: InstanceType<BackendConstructor>;
25
+ [point: string]: FileSystem;
27
26
  }
28
27
  export declare const mounts: Map<string, FileSystem>;
29
28
  /**
@@ -69,8 +68,9 @@ export type PathLike = string;
69
68
  /**
70
69
  * @internal
71
70
  *
72
- * Converts any Buffer in T to Uint8Array
71
+ * Recursivly converts `From` in `Target` to `To`
73
72
  */
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;
73
+ export type Convert<Target, From, To> = Target extends From ? To : Target extends (...args: any[]) => unknown ? (...args: Convert<Parameters<Target>, From, To> & Array<unknown>) => Convert<ReturnType<Target>, From, To> : Target extends object ? {
74
+ [K in keyof Target]: Convert<Target[K], From, To>;
75
+ } : Target;
76
+ export type BufferToUint8Array<T> = Convert<T, Buffer, Uint8Array>;
@@ -2,17 +2,17 @@
2
2
  import { resolve } from './path.js';
3
3
  import { ApiError, ErrorCode } from '../ApiError.js';
4
4
  import { Cred } from '../cred.js';
5
- import { InMemoryFileSystem } from '../backends/InMemory.js';
5
+ import { InMemory } from '../backends/InMemory.js';
6
6
  /**
7
- * converts Date or number to a fractional UNIX timestamp
7
+ * converts Date or number to a integer UNIX timestamp
8
8
  * Grabbed from NodeJS sources (lib/fs.js)
9
9
  */
10
10
  export function _toUnixTimestamp(time) {
11
11
  if (typeof time === 'number') {
12
- return time;
12
+ return Math.floor(time);
13
13
  }
14
- else if (time instanceof Date) {
15
- return time.getTime() / 1000;
14
+ if (time instanceof Date) {
15
+ return Math.floor(time.getTime() / 1000);
16
16
  }
17
17
  throw new Error('Cannot parse time: ' + time);
18
18
  }
@@ -27,20 +27,23 @@ export function normalizeMode(mode, def) {
27
27
  if (!isNaN(trueMode)) {
28
28
  return trueMode;
29
29
  }
30
- // Invalid string.
31
- return def;
32
- default:
33
- return def;
34
30
  }
31
+ if (typeof def == 'number') {
32
+ return def;
33
+ }
34
+ throw new ApiError(ErrorCode.EINVAL, 'Invalid mode: ' + mode?.toString());
35
35
  }
36
36
  export function normalizeTime(time) {
37
37
  if (time instanceof Date) {
38
38
  return time;
39
39
  }
40
- if (typeof time === 'number') {
40
+ if (typeof time == 'number') {
41
41
  return new Date(time * 1000);
42
42
  }
43
- throw new ApiError(ErrorCode.EINVAL, `Invalid time.`);
43
+ if (typeof time == 'string') {
44
+ return new Date(time);
45
+ }
46
+ throw new ApiError(ErrorCode.EINVAL, 'Invalid time.');
44
47
  }
45
48
  export function normalizePath(p) {
46
49
  // Node doesn't allow null characters in paths.
@@ -98,17 +101,15 @@ export function getFdForFile(file) {
98
101
  }
99
102
  export function fd2file(fd) {
100
103
  if (!fdMap.has(fd)) {
101
- throw new ApiError(ErrorCode.EBADF, 'Invalid file descriptor.');
104
+ throw new ApiError(ErrorCode.EBADF);
102
105
  }
103
106
  return fdMap.get(fd);
104
107
  }
105
108
  export const mounts = new Map();
106
109
  /*
107
110
  Set a default root.
108
- There is a very small but not 0 change that initialize() will try to unmount the default before it is mounted.
109
- This can be fixed by using a top-level await, which is not done to maintain ES6 compatibility.
110
111
  */
111
- InMemoryFileSystem.Create().then(fs => mount('/', fs));
112
+ mount('/', InMemory.create({ name: 'root' }));
112
113
  /**
113
114
  * Gets the file system mounted at `mountPoint`
114
115
  */
@@ -151,6 +152,7 @@ export function umount(mountPoint) {
151
152
  * Gets the internal FileSystem for the path, then returns it along with the path relative to the FS' root
152
153
  */
153
154
  export function resolveFS(path) {
155
+ path = normalizePath(path);
154
156
  const sortedMounts = [...mounts].sort((a, b) => (a[0].length > b[0].length ? -1 : 1)); // decending order of the string length
155
157
  for (const [mountPoint, fs] of sortedMounts) {
156
158
  // We know path is normalized, so it would be a substring of the mount point.
@@ -181,14 +183,10 @@ export function fixError(e, paths) {
181
183
  return e;
182
184
  }
183
185
  export function initialize(mountMapping) {
184
- if (mountMapping['/']) {
186
+ if ('/' in mountMapping) {
185
187
  umount('/');
186
188
  }
187
189
  for (const [point, fs] of Object.entries(mountMapping)) {
188
- const FS = fs.constructor;
189
- if (!FS.isAvailable()) {
190
- throw new ApiError(ErrorCode.EINVAL, `Can not mount "${point}" since the filesystem is unavailable.`);
191
- }
192
190
  mount(point, fs);
193
191
  }
194
192
  }
@@ -54,12 +54,17 @@ export declare function unlinkSync(path: PathLike): void;
54
54
  /**
55
55
  * Synchronous file open.
56
56
  * @see http://www.manpagez.com/man/2/open/
57
- * @param path
58
- * @param flags
59
- * @param mode defaults to `0644`
60
- * @returns file descriptor
57
+ * @param flags Handles the complexity of the various file
58
+ * modes. See its API for more details.
59
+ * @param mode Mode to use to open the file. Can be ignored if the
60
+ * filesystem doesn't support permissions.
61
+ */
62
+ export declare function openSync(path: PathLike, flag: string, mode?: Node.Mode): number;
63
+ /**
64
+ * Opens a file or symlink
65
+ * @internal
61
66
  */
62
- export declare function openSync(path: PathLike, flag: string, mode?: number | string): number;
67
+ export declare function lopenSync(path: PathLike, flag: string, mode?: Node.Mode): number;
63
68
  /**
64
69
  * Synchronously reads the entire contents of a file.
65
70
  * @param filename
@@ -89,16 +94,11 @@ export declare function readFileSync(filename: string, encoding: string): string
89
94
  * @option options flag Defaults to `'w'`.
90
95
  */
91
96
  export declare function writeFileSync(filename: string, data: FileContents, options?: Node.WriteFileOptions): void;
92
- export declare function writeFileSync(filename: string, data: FileContents, encoding?: string): void;
97
+ export declare function writeFileSync(filename: string, data: FileContents, encoding?: BufferEncoding): void;
93
98
  /**
94
99
  * Asynchronously append data to a file, creating the file if it not yet
95
100
  * exists.
96
101
  *
97
- * @example Usage example
98
- * fs.appendFile('message.txt', 'data to append', function (err) {
99
- * if (err) throw err;
100
- * console.log('The "data to append" was appended to file!');
101
- * });
102
102
  * @param filename
103
103
  * @param data
104
104
  * @param options
@@ -146,7 +146,7 @@ export declare function fdatasyncSync(fd: number): void;
146
146
  * Note that it is unsafe to use fs.write multiple times on the same file
147
147
  * without waiting for it to return.
148
148
  * @param fd
149
- * @param buffer Uint8Array containing the data to write to
149
+ * @param data Uint8Array containing the data to write to
150
150
  * the file.
151
151
  * @param offset Offset in the buffer to start reading data from.
152
152
  * @param length The amount of bytes to write to the file.
@@ -154,8 +154,8 @@ export declare function fdatasyncSync(fd: number): void;
154
154
  * data should be written. If position is null, the data will be written at
155
155
  * the current position.
156
156
  */
157
- export declare function writeSync(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number | null): number;
158
- export declare function writeSync(fd: number, data: string, position?: number | null, encoding?: BufferEncoding): number;
157
+ export declare function writeSync(fd: number, data: Uint8Array, offset: number, length: number, position?: number): number;
158
+ export declare function writeSync(fd: number, data: string, position?: number, encoding?: BufferEncoding): number;
159
159
  /**
160
160
  * Read data from the file specified by `fd`.
161
161
  * @param fd
@@ -190,7 +190,7 @@ export declare function fchmodSync(fd: number, mode: number | string): void;
190
190
  * @param atime
191
191
  * @param mtime
192
192
  */
193
- export declare function futimesSync(fd: number, atime: number | Date, mtime: number | Date): void;
193
+ export declare function futimesSync(fd: number, atime: string | number | Date, mtime: string | number | Date): void;
194
194
  /**
195
195
  * Synchronous `rmdir`.
196
196
  * @param path
@@ -212,7 +212,7 @@ export declare function mkdirSync(path: PathLike, options?: Node.Mode | (Node.Ma
212
212
  * Synchronous `readdir`. Reads the contents of a directory.
213
213
  * @param path
214
214
  */
215
- export declare function readdirSync(path: PathLike, options: {
215
+ export declare function readdirSync(path: PathLike, options?: {
216
216
  encoding?: BufferEncoding;
217
217
  withFileTypes?: false;
218
218
  } | BufferEncoding): string[];
@@ -225,17 +225,17 @@ export declare function readdirSync(path: PathLike, options: {
225
225
  }): Dirent[];
226
226
  /**
227
227
  * Synchronous `link`.
228
- * @param srcpath
229
- * @param dstpath
228
+ * @param existing
229
+ * @param newpath
230
230
  */
231
- export declare function linkSync(srcpath: PathLike, dstpath: PathLike): void;
231
+ export declare function linkSync(existing: PathLike, newpath: PathLike): void;
232
232
  /**
233
233
  * Synchronous `symlink`.
234
- * @param srcpath
235
- * @param dstpath
234
+ * @param target target path
235
+ * @param path link path
236
236
  * @param type can be either `'dir'` or `'file'` (default is `'file'`)
237
237
  */
238
- export declare function symlinkSync(srcpath: PathLike, dstpath: PathLike, type?: symlink.Type): void;
238
+ export declare function symlinkSync(target: PathLike, path: PathLike, type?: symlink.Type): void;
239
239
  /**
240
240
  * Synchronous readlink.
241
241
  * @param path
@@ -261,7 +261,7 @@ export declare function lchownSync(path: PathLike, uid: number, gid: number): vo
261
261
  * @param path
262
262
  * @param mode
263
263
  */
264
- export declare function chmodSync(path: PathLike, mode: string | number): void;
264
+ export declare function chmodSync(path: PathLike, mode: Node.Mode): void;
265
265
  /**
266
266
  * Synchronous `lchmod`.
267
267
  * @param path
@@ -274,14 +274,14 @@ export declare function lchmodSync(path: PathLike, mode: number | string): void;
274
274
  * @param atime
275
275
  * @param mtime
276
276
  */
277
- export declare function utimesSync(path: PathLike, atime: number | Date, mtime: number | Date): void;
277
+ export declare function utimesSync(path: PathLike, atime: string | number | Date, mtime: string | number | Date): void;
278
278
  /**
279
279
  * Change file timestamps of the file referenced by the supplied path.
280
280
  * @param path
281
281
  * @param atime
282
282
  * @param mtime
283
283
  */
284
- export declare function lutimesSync(path: PathLike, atime: number | Date, mtime: number | Date): void;
284
+ export declare function lutimesSync(path: PathLike, atime: string | number | Date, mtime: string | number | Date): void;
285
285
  /**
286
286
  * Synchronous `realpath`.
287
287
  * @param path