@zenfs/core 0.0.12 → 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 (54) hide show
  1. package/dist/ApiError.d.ts +52 -15
  2. package/dist/ApiError.js +77 -50
  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 +154 -147
  7. package/dist/backends/AsyncStore.d.ts +29 -28
  8. package/dist/backends/AsyncStore.js +375 -482
  9. package/dist/backends/FolderAdapter.js +8 -19
  10. package/dist/backends/InMemory.d.ts +16 -13
  11. package/dist/backends/InMemory.js +29 -14
  12. package/dist/backends/Locked.d.ts +8 -28
  13. package/dist/backends/Locked.js +74 -224
  14. package/dist/backends/OverlayFS.d.ts +26 -34
  15. package/dist/backends/OverlayFS.js +303 -511
  16. package/dist/backends/SyncStore.d.ts +54 -72
  17. package/dist/backends/SyncStore.js +159 -161
  18. package/dist/backends/backend.d.ts +45 -29
  19. package/dist/backends/backend.js +83 -13
  20. package/dist/backends/index.d.ts +6 -7
  21. package/dist/backends/index.js +5 -6
  22. package/dist/browser.min.js +21 -6
  23. package/dist/browser.min.js.map +4 -4
  24. package/dist/emulation/callbacks.d.ts +119 -113
  25. package/dist/emulation/callbacks.js +129 -92
  26. package/dist/emulation/constants.js +1 -1
  27. package/dist/emulation/dir.d.ts +55 -0
  28. package/dist/emulation/dir.js +104 -0
  29. package/dist/emulation/fs.d.ts +1 -2
  30. package/dist/emulation/fs.js +0 -1
  31. package/dist/emulation/index.d.ts +3 -0
  32. package/dist/emulation/index.js +3 -0
  33. package/dist/emulation/promises.d.ts +265 -145
  34. package/dist/emulation/promises.js +526 -383
  35. package/dist/emulation/shared.d.ts +20 -6
  36. package/dist/emulation/shared.js +22 -23
  37. package/dist/emulation/streams.d.ts +102 -0
  38. package/dist/emulation/streams.js +55 -0
  39. package/dist/emulation/sync.d.ts +98 -69
  40. package/dist/emulation/sync.js +280 -133
  41. package/dist/file.d.ts +175 -173
  42. package/dist/file.js +257 -273
  43. package/dist/filesystem.d.ts +71 -244
  44. package/dist/filesystem.js +67 -472
  45. package/dist/index.d.ts +7 -44
  46. package/dist/index.js +22 -75
  47. package/dist/inode.d.ts +37 -28
  48. package/dist/inode.js +123 -65
  49. package/dist/stats.d.ts +91 -36
  50. package/dist/stats.js +138 -110
  51. package/dist/utils.d.ts +26 -13
  52. package/dist/utils.js +79 -107
  53. package/package.json +7 -4
  54. package/readme.md +2 -40
@@ -5,20 +5,65 @@
5
5
  * @url http://www.gnu.org/software/libc/manual/html_node/Error-Codes.html
6
6
  */
7
7
  export declare enum ErrorCode {
8
+ /**
9
+ * Operation not permitted
10
+ */
8
11
  EPERM = 1,
12
+ /**
13
+ * No such file or directory
14
+ */
9
15
  ENOENT = 2,
16
+ /**
17
+ * Input/output error
18
+ */
10
19
  EIO = 5,
20
+ /**
21
+ * Bad file descriptor
22
+ */
11
23
  EBADF = 9,
24
+ /**
25
+ * Permission denied
26
+ */
12
27
  EACCES = 13,
28
+ /**
29
+ * Resource busy or locked
30
+ */
13
31
  EBUSY = 16,
32
+ /**
33
+ * File exists
34
+ */
14
35
  EEXIST = 17,
36
+ /**
37
+ * File is not a directory
38
+ */
15
39
  ENOTDIR = 20,
40
+ /**
41
+ * File is a directory
42
+ */
16
43
  EISDIR = 21,
44
+ /**
45
+ * Invalid argument
46
+ */
17
47
  EINVAL = 22,
48
+ /**
49
+ * File is too big
50
+ */
18
51
  EFBIG = 27,
52
+ /**
53
+ * No space left on disk
54
+ */
19
55
  ENOSPC = 28,
56
+ /**
57
+ * Cannot modify a read-only file system
58
+ */
20
59
  EROFS = 30,
60
+ /**
61
+ * Directory is not empty
62
+ */
21
63
  ENOTEMPTY = 39,
64
+ /**
65
+ * Operation is not supported
66
+ */
22
67
  ENOTSUP = 95
23
68
  }
24
69
  /**
@@ -26,7 +71,7 @@ export declare enum ErrorCode {
26
71
  * @internal
27
72
  */
28
73
  export declare const ErrorStrings: {
29
- [code: string | number]: string;
74
+ [code in ErrorCode]: string;
30
75
  };
31
76
  interface ApiErrorJSON {
32
77
  errno: ErrorCode;
@@ -40,12 +85,10 @@ interface ApiErrorJSON {
40
85
  * call to the ZenFS API.
41
86
  */
42
87
  export declare class ApiError extends Error implements NodeJS.ErrnoException {
88
+ errno: ErrorCode;
89
+ path?: string;
43
90
  static fromJSON(json: ApiErrorJSON): ApiError;
44
- /**
45
- * Creates an ApiError object from a buffer.
46
- */
47
- static Derserialize(data: ArrayBufferLike | ArrayBufferView, i?: number): ApiError;
48
- static FileError(code: ErrorCode, p: string): ApiError;
91
+ static OnPath(code: ErrorCode, path: string): ApiError;
49
92
  static EACCES(path: string): ApiError;
50
93
  static ENOENT(path: string): ApiError;
51
94
  static EEXIST(path: string): ApiError;
@@ -53,9 +96,7 @@ export declare class ApiError extends Error implements NodeJS.ErrnoException {
53
96
  static ENOTDIR(path: string): ApiError;
54
97
  static EPERM(path: string): ApiError;
55
98
  static ENOTEMPTY(path: string): ApiError;
56
- errno: ErrorCode;
57
99
  code: string;
58
- path?: string;
59
100
  syscall: string;
60
101
  stack?: string;
61
102
  /**
@@ -66,18 +107,14 @@ export declare class ApiError extends Error implements NodeJS.ErrnoException {
66
107
  * what Node returns.
67
108
  * @constructor ApiError
68
109
  * @param type The type of the error.
69
- * @param [message] A descriptive error message.
110
+ * @param message A descriptive error message.
70
111
  */
71
- constructor(type: ErrorCode, message?: string, path?: string);
112
+ constructor(errno: ErrorCode, message?: string, path?: string);
72
113
  /**
73
114
  * @return A friendly error message.
74
115
  */
75
116
  toString(): string;
76
- toJSON(): any;
77
- /**
78
- * Writes the API error into a buffer.
79
- */
80
- serialize(data?: ArrayBufferLike | ArrayBufferView, i?: number): Uint8Array;
117
+ toJSON(): ApiErrorJSON;
81
118
  /**
82
119
  * The size of the API error in buffer-form in bytes.
83
120
  */
package/dist/ApiError.js CHANGED
@@ -1,4 +1,3 @@
1
- import { decode, encode } from './utils.js';
2
1
  /**
3
2
  * Standard libc error codes. More will be added to this enum and ErrorStrings as they are
4
3
  * needed.
@@ -6,42 +5,88 @@ import { decode, encode } from './utils.js';
6
5
  */
7
6
  export var ErrorCode;
8
7
  (function (ErrorCode) {
8
+ /**
9
+ * Operation not permitted
10
+ */
9
11
  ErrorCode[ErrorCode["EPERM"] = 1] = "EPERM";
12
+ /**
13
+ * No such file or directory
14
+ */
10
15
  ErrorCode[ErrorCode["ENOENT"] = 2] = "ENOENT";
16
+ /**
17
+ * Input/output error
18
+ */
11
19
  ErrorCode[ErrorCode["EIO"] = 5] = "EIO";
20
+ /**
21
+ * Bad file descriptor
22
+ */
12
23
  ErrorCode[ErrorCode["EBADF"] = 9] = "EBADF";
24
+ /**
25
+ * Permission denied
26
+ */
13
27
  ErrorCode[ErrorCode["EACCES"] = 13] = "EACCES";
28
+ /**
29
+ * Resource busy or locked
30
+ */
14
31
  ErrorCode[ErrorCode["EBUSY"] = 16] = "EBUSY";
32
+ /**
33
+ * File exists
34
+ */
15
35
  ErrorCode[ErrorCode["EEXIST"] = 17] = "EEXIST";
36
+ /**
37
+ * File is not a directory
38
+ */
16
39
  ErrorCode[ErrorCode["ENOTDIR"] = 20] = "ENOTDIR";
40
+ /**
41
+ * File is a directory
42
+ */
17
43
  ErrorCode[ErrorCode["EISDIR"] = 21] = "EISDIR";
44
+ /**
45
+ * Invalid argument
46
+ */
18
47
  ErrorCode[ErrorCode["EINVAL"] = 22] = "EINVAL";
48
+ /**
49
+ * File is too big
50
+ */
19
51
  ErrorCode[ErrorCode["EFBIG"] = 27] = "EFBIG";
52
+ /**
53
+ * No space left on disk
54
+ */
20
55
  ErrorCode[ErrorCode["ENOSPC"] = 28] = "ENOSPC";
56
+ /**
57
+ * Cannot modify a read-only file system
58
+ */
21
59
  ErrorCode[ErrorCode["EROFS"] = 30] = "EROFS";
60
+ /**
61
+ * Directory is not empty
62
+ */
22
63
  ErrorCode[ErrorCode["ENOTEMPTY"] = 39] = "ENOTEMPTY";
64
+ /**
65
+ * Operation is not supported
66
+ */
23
67
  ErrorCode[ErrorCode["ENOTSUP"] = 95] = "ENOTSUP";
24
68
  })(ErrorCode = ErrorCode || (ErrorCode = {}));
25
69
  /**
26
70
  * Strings associated with each error code.
27
71
  * @internal
28
72
  */
29
- export const ErrorStrings = {};
30
- ErrorStrings[ErrorCode.EPERM] = 'Operation not permitted.';
31
- ErrorStrings[ErrorCode.ENOENT] = 'No such file or directory.';
32
- ErrorStrings[ErrorCode.EIO] = 'Input/output error.';
33
- ErrorStrings[ErrorCode.EBADF] = 'Bad file descriptor.';
34
- ErrorStrings[ErrorCode.EACCES] = 'Permission denied.';
35
- ErrorStrings[ErrorCode.EBUSY] = 'Resource busy or locked.';
36
- ErrorStrings[ErrorCode.EEXIST] = 'File exists.';
37
- ErrorStrings[ErrorCode.ENOTDIR] = 'File is not a directory.';
38
- ErrorStrings[ErrorCode.EISDIR] = 'File is a directory.';
39
- ErrorStrings[ErrorCode.EINVAL] = 'Invalid argument.';
40
- ErrorStrings[ErrorCode.EFBIG] = 'File is too big.';
41
- ErrorStrings[ErrorCode.ENOSPC] = 'No space left on disk.';
42
- ErrorStrings[ErrorCode.EROFS] = 'Cannot modify a read-only file system.';
43
- ErrorStrings[ErrorCode.ENOTEMPTY] = 'Directory is not empty.';
44
- ErrorStrings[ErrorCode.ENOTSUP] = 'Operation is not supported.';
73
+ export const ErrorStrings = {
74
+ [ErrorCode.EPERM]: 'Operation not permitted.',
75
+ [ErrorCode.ENOENT]: 'No such file or directory.',
76
+ [ErrorCode.EIO]: 'Input/output error.',
77
+ [ErrorCode.EBADF]: 'Bad file descriptor.',
78
+ [ErrorCode.EACCES]: 'Permission denied.',
79
+ [ErrorCode.EBUSY]: 'Resource busy or locked.',
80
+ [ErrorCode.EEXIST]: 'File exists.',
81
+ [ErrorCode.ENOTDIR]: 'File is not a directory.',
82
+ [ErrorCode.EISDIR]: 'File is a directory.',
83
+ [ErrorCode.EINVAL]: 'Invalid argument.',
84
+ [ErrorCode.EFBIG]: 'File is too big.',
85
+ [ErrorCode.ENOSPC]: 'No space left on disk.',
86
+ [ErrorCode.EROFS]: 'Cannot modify a read-only file system.',
87
+ [ErrorCode.ENOTEMPTY]: 'Directory is not empty.',
88
+ [ErrorCode.ENOTSUP]: 'Operation is not supported.',
89
+ };
45
90
  /**
46
91
  * Represents a ZenFS error. Passed back to applications after a failed
47
92
  * call to the ZenFS API.
@@ -53,37 +98,29 @@ export class ApiError extends Error {
53
98
  err.stack = json.stack;
54
99
  return err;
55
100
  }
56
- /**
57
- * Creates an ApiError object from a buffer.
58
- */
59
- static Derserialize(data, i = 0) {
60
- const view = new DataView('buffer' in data ? data.buffer : data);
61
- const dataText = decode(view.buffer.slice(i + 4, i + 4 + view.getUint32(i, true)));
62
- return ApiError.fromJSON(JSON.parse(dataText));
63
- }
64
- static FileError(code, p) {
65
- return new ApiError(code, ErrorStrings[code], p);
101
+ static OnPath(code, path) {
102
+ return new ApiError(code, ErrorStrings[code], path);
66
103
  }
67
104
  static EACCES(path) {
68
- return this.FileError(ErrorCode.EACCES, path);
105
+ return this.OnPath(ErrorCode.EACCES, path);
69
106
  }
70
107
  static ENOENT(path) {
71
- return this.FileError(ErrorCode.ENOENT, path);
108
+ return this.OnPath(ErrorCode.ENOENT, path);
72
109
  }
73
110
  static EEXIST(path) {
74
- return this.FileError(ErrorCode.EEXIST, path);
111
+ return this.OnPath(ErrorCode.EEXIST, path);
75
112
  }
76
113
  static EISDIR(path) {
77
- return this.FileError(ErrorCode.EISDIR, path);
114
+ return this.OnPath(ErrorCode.EISDIR, path);
78
115
  }
79
116
  static ENOTDIR(path) {
80
- return this.FileError(ErrorCode.ENOTDIR, path);
117
+ return this.OnPath(ErrorCode.ENOTDIR, path);
81
118
  }
82
119
  static EPERM(path) {
83
- return this.FileError(ErrorCode.EPERM, path);
120
+ return this.OnPath(ErrorCode.EPERM, path);
84
121
  }
85
122
  static ENOTEMPTY(path) {
86
- return this.FileError(ErrorCode.ENOTEMPTY, path);
123
+ return this.OnPath(ErrorCode.ENOTEMPTY, path);
87
124
  }
88
125
  /**
89
126
  * Represents a ZenFS error. Passed back to applications after a failed
@@ -93,16 +130,16 @@ export class ApiError extends Error {
93
130
  * what Node returns.
94
131
  * @constructor ApiError
95
132
  * @param type The type of the error.
96
- * @param [message] A descriptive error message.
133
+ * @param message A descriptive error message.
97
134
  */
98
- constructor(type, message = ErrorStrings[type], path) {
135
+ constructor(errno, message = ErrorStrings[errno], path) {
99
136
  super(message);
137
+ this.errno = errno;
138
+ this.path = path;
100
139
  // Unsupported.
101
140
  this.syscall = '';
102
- this.errno = type;
103
- this.code = ErrorCode[type];
104
- this.path = path;
105
- this.message = `Error: ${this.code}: ${message}${this.path ? `, '${this.path}'` : ''}`;
141
+ this.code = ErrorCode[errno];
142
+ this.message = `${this.code}: ${message}${this.path ? `, '${this.path}'` : ''}`;
106
143
  }
107
144
  /**
108
145
  * @return A friendly error message.
@@ -119,16 +156,6 @@ export class ApiError extends Error {
119
156
  message: this.message,
120
157
  };
121
158
  }
122
- /**
123
- * Writes the API error into a buffer.
124
- */
125
- serialize(data = new Uint8Array(this.bufferSize()), i = 0) {
126
- const view = new DataView('buffer' in data ? data.buffer : data), buffer = new Uint8Array(view.buffer);
127
- const newData = encode(JSON.stringify(this.toJSON()));
128
- buffer.set(newData);
129
- view.setUint32(i, newData.byteLength, true);
130
- return buffer;
131
- }
132
159
  /**
133
160
  * The size of the API error in buffer-form in bytes.
134
161
  */
@@ -1,4 +1,7 @@
1
1
  import { Stats } from './stats.js';
2
+ type ListingTree = {
3
+ [key: string]: ListingTree | null;
4
+ };
2
5
  /**
3
6
  * A simple class for storing a filesystem index. Assumes that all paths passed
4
7
  * to it are *absolute* paths.
@@ -9,11 +12,11 @@ import { Stats } from './stats.js';
9
12
  export declare class FileIndex<T> {
10
13
  /**
11
14
  * Static method for constructing indices from a JSON listing.
12
- * @param listing Directory listing generated by tools/XHRIndexer.coffee
15
+ * @param listing Directory listing generated by tools
13
16
  * @return A new FileIndex object.
14
17
  */
15
- static fromListing<T>(listing: any): FileIndex<T>;
16
- private _index;
18
+ static fromListing<T>(listing: ListingTree): FileIndex<T>;
19
+ protected _index: Map<string, IndexDirInode<T>>;
17
20
  /**
18
21
  * Constructs a new FileIndex.
19
22
  */
@@ -21,7 +24,7 @@ export declare class FileIndex<T> {
21
24
  /**
22
25
  * Runs the given function over all files in the index.
23
26
  */
24
- fileIterator<T>(cb: (file: T | null) => void): void;
27
+ forEachFile(cb: (file?: T) => void): void;
25
28
  /**
26
29
  * Adds the given absolute path to the index if it is not already in the index.
27
30
  * Creates any needed parent directories.
@@ -34,7 +37,7 @@ export declare class FileIndex<T> {
34
37
  * @todo If adding fails and implicitly creates directories, we do not clean up
35
38
  * the new empty directories.
36
39
  */
37
- addPath(path: string, inode: IndexInode): boolean;
40
+ addPath(path: string, inode: IndexInode<T>): boolean;
38
41
  /**
39
42
  * Adds the given absolute path to the index if it is not already in the index.
40
43
  * The path is added without special treatment (no joining of adjacent separators, etc).
@@ -48,13 +51,13 @@ export declare class FileIndex<T> {
48
51
  * @todo If adding fails and implicitly creates directories, we do not clean up
49
52
  * the new empty directories.
50
53
  */
51
- addPathFast(path: string, inode: IndexInode): boolean;
54
+ addPathFast(path: string, inode: IndexInode<T>): boolean;
52
55
  /**
53
56
  * Removes the given path. Can be a file or a directory.
54
57
  * @return The removed item,
55
58
  * or null if it did not exist.
56
59
  */
57
- removePath(path: string): IndexInode | null;
60
+ removePath(path: string): IndexInode<T> | null;
58
61
  /**
59
62
  * Retrieves the directory listing of the given path.
60
63
  * @return An array of files in the given path, or 'null' if it does not exist.
@@ -64,56 +67,49 @@ export declare class FileIndex<T> {
64
67
  * Returns the inode of the given item.
65
68
  * @return Returns null if the item does not exist.
66
69
  */
67
- getInode(path: string): IndexInode | null;
70
+ getInode(path: string): IndexInode<T> | null;
68
71
  /**
69
72
  * Split into a (directory path, item name) pair
70
73
  */
71
- private _split_path;
74
+ protected splitPath(p: string): string[];
72
75
  }
73
76
  /**
74
77
  * Generic interface for file/directory inodes.
75
78
  * Note that Stats objects are what we use for file inodes.
76
79
  */
77
- export interface IndexInode {
78
- isFile(): boolean;
79
- isDir(): boolean;
80
- toStats(): Stats;
80
+ export declare abstract class IndexInode<T> {
81
+ data?: T;
82
+ constructor(data?: T);
83
+ abstract isFile(): boolean;
84
+ abstract isDir(): boolean;
85
+ abstract toStats(): Stats;
81
86
  }
82
87
  /**
83
88
  * Inode for a file. Stores an arbitrary (filesystem-specific) data payload.
84
89
  */
85
- export declare class IndexFileInode<T> implements IndexInode {
86
- private data;
87
- constructor(data: T);
90
+ export declare class IndexFileInode<T> extends IndexInode<T> {
88
91
  isFile(): boolean;
89
92
  isDir(): boolean;
90
- getData(): T;
91
- setData(data: T): void;
92
93
  toStats(): Stats;
93
94
  }
94
95
  /**
95
96
  * Inode for a directory. Currently only contains the directory listing.
96
97
  */
97
- export declare class IndexDirInode<T> implements IndexInode {
98
- private data;
99
- private _ls;
98
+ export declare class IndexDirInode<T> extends IndexInode<T> {
100
99
  /**
101
- * Constructs an inode for a directory.
100
+ * @internal
102
101
  */
103
- constructor(data?: T | null);
102
+ _listing: Map<string, IndexInode<T>>;
104
103
  isFile(): boolean;
105
104
  isDir(): boolean;
106
- getData(): T | null;
107
105
  /**
108
106
  * Return a Stats object for this inode.
109
- * @todo Should probably remove this at some point. This isn't the
110
- * responsibility of the FileIndex.
107
+ * @todo Should probably remove this at some point. This isn't the responsibility of the FileIndex.
111
108
  */
112
- getStats(): Stats;
109
+ get stats(): Stats;
113
110
  /**
114
111
  * Alias of getStats()
115
- * @todo Remove this at some point. This isn't the
116
- * responsibility of the FileIndex.
112
+ * @todo Remove this at some point. This isn't the responsibility of the FileIndex.
117
113
  */
118
114
  toStats(): Stats;
119
115
  /**
@@ -121,12 +117,12 @@ export declare class IndexDirInode<T> implements IndexInode {
121
117
  * relative to the directory's path.
122
118
  * @return The directory listing for this directory.
123
119
  */
124
- getListing(): string[];
120
+ get listing(): string[];
125
121
  /**
126
122
  * Returns the inode for the indicated item, or null if it does not exist.
127
123
  * @param p Name of item in this directory.
128
124
  */
129
- getItem(p: string): IndexInode | null;
125
+ get(p: string): IndexInode<T> | null;
130
126
  /**
131
127
  * Add the given item to the directory listing. Note that the given inode is
132
128
  * not copied, and will be mutated by the DirInode if it is a DirInode.
@@ -135,20 +131,21 @@ export declare class IndexDirInode<T> implements IndexInode {
135
131
  * item to add to the directory inode.
136
132
  * @return True if it was added, false if it already existed.
137
133
  */
138
- addItem(p: string, inode: IndexInode): boolean;
134
+ add(p: string, inode: IndexInode<T>): boolean;
139
135
  /**
140
136
  * Removes the given item from the directory listing.
141
137
  * @param p Name of item to remove from the directory listing.
142
138
  * @return Returns the item
143
139
  * removed, or null if the item did not exist.
144
140
  */
145
- remItem(p: string): IndexInode | null;
141
+ remove(p: string): IndexInode<T> | null;
146
142
  }
147
143
  /**
148
144
  * @hidden
149
145
  */
150
- export declare function isIndexFileInode<T>(inode: IndexInode | null): inode is IndexFileInode<T>;
146
+ export declare function isIndexFileInode<T>(inode?: IndexInode<T>): inode is IndexFileInode<T>;
151
147
  /**
152
148
  * @hidden
153
149
  */
154
- export declare function isIndexDirInode<T>(inode: IndexInode | null): inode is IndexDirInode<T>;
150
+ export declare function isIndexDirInode<T>(inode?: IndexInode<T>): inode is IndexDirInode<T>;
151
+ export {};