@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
@@ -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
  /**
@@ -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,20 +5,65 @@ 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
  /**
@@ -54,37 +98,29 @@ export class ApiError extends Error {
54
98
  err.stack = json.stack;
55
99
  return err;
56
100
  }
57
- /**
58
- * Creates an ApiError object from a buffer.
59
- */
60
- static Derserialize(data, i = 0) {
61
- const view = new DataView('buffer' in data ? data.buffer : data);
62
- const dataText = decode(view.buffer.slice(i + 4, i + 4 + view.getUint32(i, true)));
63
- return ApiError.fromJSON(JSON.parse(dataText));
64
- }
65
- static FileError(code, p) {
66
- return new ApiError(code, ErrorStrings[code], p);
101
+ static OnPath(code, path) {
102
+ return new ApiError(code, ErrorStrings[code], path);
67
103
  }
68
104
  static EACCES(path) {
69
- return this.FileError(ErrorCode.EACCES, path);
105
+ return this.OnPath(ErrorCode.EACCES, path);
70
106
  }
71
107
  static ENOENT(path) {
72
- return this.FileError(ErrorCode.ENOENT, path);
108
+ return this.OnPath(ErrorCode.ENOENT, path);
73
109
  }
74
110
  static EEXIST(path) {
75
- return this.FileError(ErrorCode.EEXIST, path);
111
+ return this.OnPath(ErrorCode.EEXIST, path);
76
112
  }
77
113
  static EISDIR(path) {
78
- return this.FileError(ErrorCode.EISDIR, path);
114
+ return this.OnPath(ErrorCode.EISDIR, path);
79
115
  }
80
116
  static ENOTDIR(path) {
81
- return this.FileError(ErrorCode.ENOTDIR, path);
117
+ return this.OnPath(ErrorCode.ENOTDIR, path);
82
118
  }
83
119
  static EPERM(path) {
84
- return this.FileError(ErrorCode.EPERM, path);
120
+ return this.OnPath(ErrorCode.EPERM, path);
85
121
  }
86
122
  static ENOTEMPTY(path) {
87
- return this.FileError(ErrorCode.ENOTEMPTY, path);
123
+ return this.OnPath(ErrorCode.ENOTEMPTY, path);
88
124
  }
89
125
  /**
90
126
  * Represents a ZenFS error. Passed back to applications after a failed
@@ -94,16 +130,16 @@ export class ApiError extends Error {
94
130
  * what Node returns.
95
131
  * @constructor ApiError
96
132
  * @param type The type of the error.
97
- * @param [message] A descriptive error message.
133
+ * @param message A descriptive error message.
98
134
  */
99
- constructor(type, message = ErrorStrings[type], path) {
135
+ constructor(errno, message = ErrorStrings[errno], path) {
100
136
  super(message);
137
+ this.errno = errno;
138
+ this.path = path;
101
139
  // Unsupported.
102
140
  this.syscall = '';
103
- this.errno = type;
104
- this.code = ErrorCode[type];
105
- this.path = path;
106
- 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}'` : ''}`;
107
143
  }
108
144
  /**
109
145
  * @return A friendly error message.
@@ -120,16 +156,6 @@ export class ApiError extends Error {
120
156
  message: this.message,
121
157
  };
122
158
  }
123
- /**
124
- * Writes the API error into a buffer.
125
- */
126
- serialize(data = new Uint8Array(this.bufferSize()), i = 0) {
127
- const view = new DataView('buffer' in data ? data.buffer : data), buffer = new Uint8Array(view.buffer);
128
- const newData = encode(JSON.stringify(this.toJSON()));
129
- buffer.set(newData);
130
- view.setUint32(i, newData.byteLength, true);
131
- return buffer;
132
- }
133
159
  /**
134
160
  * The size of the API error in buffer-form in bytes.
135
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 {};