@zenfs/core 0.0.1
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.
- package/README.md +293 -0
- package/dist/ApiError.d.ts +86 -0
- package/dist/ApiError.js +135 -0
- package/dist/backends/AsyncMirror.d.ts +102 -0
- package/dist/backends/AsyncMirror.js +252 -0
- package/dist/backends/AsyncStore.d.ts +166 -0
- package/dist/backends/AsyncStore.js +620 -0
- package/dist/backends/FolderAdapter.d.ts +52 -0
- package/dist/backends/FolderAdapter.js +184 -0
- package/dist/backends/InMemory.d.ts +25 -0
- package/dist/backends/InMemory.js +46 -0
- package/dist/backends/Locked.d.ts +64 -0
- package/dist/backends/Locked.js +302 -0
- package/dist/backends/OverlayFS.d.ts +120 -0
- package/dist/backends/OverlayFS.js +749 -0
- package/dist/backends/SyncStore.d.ts +223 -0
- package/dist/backends/SyncStore.js +479 -0
- package/dist/backends/backend.d.ts +73 -0
- package/dist/backends/backend.js +14 -0
- package/dist/backends/index.d.ts +11 -0
- package/dist/backends/index.js +15 -0
- package/dist/browser.min.js +12 -0
- package/dist/browser.min.js.map +7 -0
- package/dist/cred.d.ts +14 -0
- package/dist/cred.js +15 -0
- package/dist/emulation/callbacks.d.ts +382 -0
- package/dist/emulation/callbacks.js +422 -0
- package/dist/emulation/constants.d.ts +101 -0
- package/dist/emulation/constants.js +110 -0
- package/dist/emulation/fs.d.ts +7 -0
- package/dist/emulation/fs.js +5 -0
- package/dist/emulation/index.d.ts +5 -0
- package/dist/emulation/index.js +7 -0
- package/dist/emulation/promises.d.ts +309 -0
- package/dist/emulation/promises.js +521 -0
- package/dist/emulation/shared.d.ts +62 -0
- package/dist/emulation/shared.js +192 -0
- package/dist/emulation/sync.d.ts +278 -0
- package/dist/emulation/sync.js +392 -0
- package/dist/file.d.ts +449 -0
- package/dist/file.js +576 -0
- package/dist/filesystem.d.ts +367 -0
- package/dist/filesystem.js +542 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.js +113 -0
- package/dist/inode.d.ts +51 -0
- package/dist/inode.js +112 -0
- package/dist/mutex.d.ts +12 -0
- package/dist/mutex.js +48 -0
- package/dist/stats.d.ts +98 -0
- package/dist/stats.js +226 -0
- package/dist/utils.d.ts +52 -0
- package/dist/utils.js +261 -0
- package/license.md +122 -0
- package/package.json +61 -0
package/README.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# ZenFS
|
|
2
|
+
|
|
3
|
+
ZenFS is an in-browser file system that emulates the [Node JS file system API](http://nodejs.org/api/fs.html) and supports storing and retrieving files from various backends. ZenFS also integrates nicely into the Emscripten file system.
|
|
4
|
+
|
|
5
|
+
## Backends
|
|
6
|
+
|
|
7
|
+
ZenFS is highly extensible, and includes many builtin filesystem backends:
|
|
8
|
+
|
|
9
|
+
- `InMemory`: Stores files in-memory. It is a temporary file store that clears when the user navigates away.
|
|
10
|
+
- `OverlayFS`: Mount a read-only file system as read-write by overlaying a writable file system on top of it. Like Docker's overlayfs, it will only write changed files to the writable file system.
|
|
11
|
+
- `AsyncMirror`: Use an asynchronous backend synchronously. Invaluable for Emscripten; let your Emscripten applications write to larger file stores with no additional effort!
|
|
12
|
+
- `AsyncMirror` loads the entire contents of the async file system into a synchronous backend during construction. It performs operations synchronous file system and then queues them to be mirrored onto the asynchronous backend.
|
|
13
|
+
- `FolderAdapter`: Wraps a file system, and scopes all interactions to a subfolder of that file system.
|
|
14
|
+
|
|
15
|
+
More backends can be defined by separate libraries, so long as they extend they implement `ZenFS.FileSystem`. Multiple backends can be active at once at different locations in the directory hierarchy.
|
|
16
|
+
|
|
17
|
+
ZenFS supports a number of other backends (as `@zenfs/fs-[name]`).
|
|
18
|
+
|
|
19
|
+
For more information, see the [API documentation for ZenFS](https://zen-fs.github.io/core).
|
|
20
|
+
|
|
21
|
+
## Installing
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install @zenfs/core
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Building
|
|
28
|
+
|
|
29
|
+
- Make sure you have Node and NPM installed. You must have Node v18 or newer.
|
|
30
|
+
- Install dependencies with `npm install`
|
|
31
|
+
- Build using `npm run build`
|
|
32
|
+
- You can find the built code in `dist`.
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
> 🛈 The examples are written in ESM. If you are using CJS, you can `require` the package. If running in a borwser you can add a script tag to your HTML pointing to the `browser.min.js` and use ZenFS via the global `ZenFS` object.
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { fs } from '@zenfs/core';
|
|
40
|
+
|
|
41
|
+
fs.writeFileSync('/test.txt', 'Cool, I can do this in the browser!');
|
|
42
|
+
|
|
43
|
+
const contents = fs.readFileSync('/test.txt', 'utf-8');
|
|
44
|
+
console.log(contents);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### Using different backends
|
|
48
|
+
|
|
49
|
+
A `InMemory` backend is created by default. If you would like to use a different one, you must configure ZenFS. It is recommended to do so using the `configure` function. Here is an example using the `LocalStorage` backend from `@zenfs/fs-dom`:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { configure, fs } from '@zenfs/core';
|
|
53
|
+
import '@zenfs/fs-dom'; // size effects are needed
|
|
54
|
+
|
|
55
|
+
// you can also add a callback as the last parameter instead of using promises
|
|
56
|
+
await configure({ fs: 'LocalStorage' });
|
|
57
|
+
|
|
58
|
+
if (!fs.existsSync('/test.txt')) {
|
|
59
|
+
fs.writeFileSync('/test.txt', 'This will persist across reloads!');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const contents = fs.readFileSync('/test.txt', 'utf-8');
|
|
63
|
+
console.log(contents);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Using multiple backends
|
|
67
|
+
|
|
68
|
+
You can use multiple backends by passing an object to `configure` which maps paths to file systems. The following example mounts a zip file to `/zip`, in-memory storage to `/tmp`, and IndexedDB browser-local storage to `/home` (note that `/` has the default in-memory backend):
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
import { configure } from '@zenfs/core';
|
|
72
|
+
import '@zenfs/fs-dom';
|
|
73
|
+
import '@zenfs/fs-zip';
|
|
74
|
+
import Buffer from 'buffer';
|
|
75
|
+
|
|
76
|
+
const zipData = await (await fetch('mydata.zip')).arrayBuffer();
|
|
77
|
+
|
|
78
|
+
await configure({
|
|
79
|
+
'/mnt/zip': {
|
|
80
|
+
fs: 'ZipFS',
|
|
81
|
+
options: {
|
|
82
|
+
zipData: Buffer.from(zipData)
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
'/tmp': 'InMemory',
|
|
86
|
+
'/home': 'IndexedDB',
|
|
87
|
+
};
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### FS Promises API
|
|
91
|
+
|
|
92
|
+
The FS promises API is exposed as `promises`.
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
import { configure, promises } from '@zenfs/core';
|
|
96
|
+
import '@zenfs/fs-dom';
|
|
97
|
+
|
|
98
|
+
await configure({ '/': 'IndexedDB' });
|
|
99
|
+
|
|
100
|
+
const exists = await promises.exists('/myfile.txt');
|
|
101
|
+
if (!exists) {
|
|
102
|
+
await promises.write('/myfile.txt', 'Lots of persistant data');
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
ZenFS does _not_ provide a seperate method for importing promises in its built form. If you are using Typescript, you can import the promises API from source code (perhaps to reduce you bundle size). Doing so it not recommended as the files may be moved without notice.
|
|
107
|
+
|
|
108
|
+
#### Using asynchronous backends synchronously
|
|
109
|
+
|
|
110
|
+
You may have noticed that attempting to use a synchronous method on an asynchronous backend (e.g. IndexedDB) results in a "not supplied" error (`ENOTSUP`). If you wish to use an asynchronous backend synchronously you need to wrap it in an `AsyncMirror`:
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
import { configure, fs } from '@zenfs/core';
|
|
114
|
+
import '@zenfs/fs-dom';
|
|
115
|
+
|
|
116
|
+
await configure({
|
|
117
|
+
'/': { fs: 'AsyncMirror', options: { sync: { fs: 'InMemory' }, async: { fs: 'IndexedDB' } } }
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
fs.writeFileSync('/persistant.txt', 'My persistant data'); // This fails if you configure the FS as IndexedDB
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Advanced usage
|
|
124
|
+
|
|
125
|
+
#### Creating backends
|
|
126
|
+
|
|
127
|
+
If you would like to create backends without configure, you may do so by importing the backend's class and calling its `Create` method. You can import the backend directly or with `backends`:
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
import { configure, backends, InMemory } from '@zenfs/core';
|
|
131
|
+
|
|
132
|
+
console.log(backends.InMemory === InMemory) // they are the same
|
|
133
|
+
|
|
134
|
+
const inMemoryFS = await InMemory.Create();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
> âš Instances of backends follow the ***internal*** ZenFS API. You should never use a backend's method unless you are extending a backend.
|
|
138
|
+
|
|
139
|
+
Coming soon:
|
|
140
|
+
```js
|
|
141
|
+
import { configure, InMemory } from '@zenfs/core';
|
|
142
|
+
|
|
143
|
+
const inMemoryFS = new InMemory();
|
|
144
|
+
await inMemoryFS.whenReady();
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### Mounting
|
|
148
|
+
|
|
149
|
+
If you would like to mount and unmount backends, you can do so using the `mount` and `umount` functions:
|
|
150
|
+
|
|
151
|
+
```js
|
|
152
|
+
import { fs, InMemory } from '@zenfs/core';
|
|
153
|
+
|
|
154
|
+
const inMemoryFS = await InMemory.Create(); // create an FS instance
|
|
155
|
+
|
|
156
|
+
fs.mount('/tmp', inMemoryFS); // mount
|
|
157
|
+
|
|
158
|
+
fs.umount('/tmp'); // unmount /tmp
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
This could be used in the "multiple backends" example like so:
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
import { configure, fs, ZipFS } from '@zenfs/core';
|
|
165
|
+
import '@zenfs/fs-dom';
|
|
166
|
+
import '@zenfs/fs-zip';
|
|
167
|
+
import Buffer from 'buffer';
|
|
168
|
+
|
|
169
|
+
await configure({
|
|
170
|
+
'/tmp': 'InMemory',
|
|
171
|
+
'/home': 'IndexedDB',
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
fs.mkdirSync('/mnt');
|
|
175
|
+
|
|
176
|
+
const res = await fetch('mydata.zip');
|
|
177
|
+
const zipData = Buffer.from(await res.arrayBuffer());
|
|
178
|
+
const zipFs = await ZipFS.Create({ zipData });
|
|
179
|
+
fs.mount('/mnt/zip', zipFs);
|
|
180
|
+
|
|
181
|
+
// do stuff with the mounted zip
|
|
182
|
+
|
|
183
|
+
fs.umount('/mnt/zip'); // finished using the zip
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Using with bundlers
|
|
187
|
+
|
|
188
|
+
ZenFS exports a drop-in for Node's `fs` module (up to the version of `@types/node` in package.json), so you can use it for your bundler of preference using the default export.
|
|
189
|
+
|
|
190
|
+
#### ESBuild
|
|
191
|
+
|
|
192
|
+
tsconfig.json
|
|
193
|
+
|
|
194
|
+
```json
|
|
195
|
+
{
|
|
196
|
+
...
|
|
197
|
+
"paths": {
|
|
198
|
+
"fs": ["node_modules/zenfs/dist/index.js"]
|
|
199
|
+
}
|
|
200
|
+
...
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
[Why tsconfig.json?](https://stackoverflow.com/a/71935037/17637456)
|
|
205
|
+
|
|
206
|
+
Webpack:
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
module.exports = {
|
|
210
|
+
// ...
|
|
211
|
+
resolve: {
|
|
212
|
+
alias: {
|
|
213
|
+
fs: require.resolve('zenfs'),
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
// ...
|
|
217
|
+
};
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Rollup:
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
import alias from '@rollup/plugin-alias';
|
|
224
|
+
|
|
225
|
+
export default {
|
|
226
|
+
// ...
|
|
227
|
+
plugins: [
|
|
228
|
+
alias({
|
|
229
|
+
entries: [{ find: 'fs', replacement: 'zenfs' }],
|
|
230
|
+
}),
|
|
231
|
+
],
|
|
232
|
+
// ...
|
|
233
|
+
};
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Using with Emscripten
|
|
237
|
+
|
|
238
|
+
You can use any _synchronous_ ZenFS file systems with Emscripten.
|
|
239
|
+
|
|
240
|
+
```js
|
|
241
|
+
import { EmscriptenFS } from '@zenfs/fs-emscripten';
|
|
242
|
+
const BFS = new EmscriptenFS(); // Create a ZenFS Emscripten FS plugin.
|
|
243
|
+
FS.createFolder(FS.root, 'data', true, true); // Create the folder that we'll turn into a mount point.
|
|
244
|
+
FS.mount(BFS, { root: '/' }, '/data'); // Mount BFS's root folder into the '/data' folder.
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
If you want to use an asynchronous backend, you must wrap it in an `AsyncMirror`.
|
|
248
|
+
|
|
249
|
+
### Testing
|
|
250
|
+
|
|
251
|
+
Run unit tests with `npm test`.
|
|
252
|
+
|
|
253
|
+
### Citing
|
|
254
|
+
|
|
255
|
+
ZenFS is a component of the [Doppio](http://doppiojvm.org/) and [Browsix](https://browsix.org/) research projects from the PLASMA lab at the University of Massachusetts Amherst. If you decide to use ZenFS in a project that leads to a publication, please cite the academic papers on [Doppio](https://dl.acm.org/citation.cfm?doid=2594291.2594293) and [Browsix](https://dl.acm.org/citation.cfm?id=3037727):
|
|
256
|
+
|
|
257
|
+
> John Vilk and Emery D. Berger. Doppio: Breaking the Browser Language Barrier. In
|
|
258
|
+
> _Proceedings of the 35th ACM SIGPLAN Conference on Programming Language Design and Implementation_
|
|
259
|
+
> (2014), pp. 508–518.
|
|
260
|
+
|
|
261
|
+
```bibtex
|
|
262
|
+
@inproceedings{VilkDoppio,
|
|
263
|
+
author = {John Vilk and
|
|
264
|
+
Emery D. Berger},
|
|
265
|
+
title = {{Doppio: Breaking the Browser Language Barrier}},
|
|
266
|
+
booktitle = {Proceedings of the 35th {ACM} {SIGPLAN} Conference on Programming Language Design and Implementation},
|
|
267
|
+
pages = {508--518},
|
|
268
|
+
year = {2014},
|
|
269
|
+
url = {http://doi.acm.org/10.1145/2594291.2594293},
|
|
270
|
+
doi = {10.1145/2594291.2594293}
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
> Bobby Powers, John Vilk, and Emery D. Berger. Browsix: Bridging the Gap Between Unix and the Browser. In _Proceedings of the Twenty-Second International Conference on Architectural Support for Programming Languages and Operating Systems_ (2017), pp. 253–266.
|
|
275
|
+
|
|
276
|
+
```bibtex
|
|
277
|
+
@inproceedings{PowersBrowsix,
|
|
278
|
+
author = {Bobby Powers and
|
|
279
|
+
John Vilk and
|
|
280
|
+
Emery D. Berger},
|
|
281
|
+
title = {{Browsix: Bridging the Gap Between Unix and the Browser}},
|
|
282
|
+
booktitle = {Proceedings of the Twenty-Second International Conference on Architectural
|
|
283
|
+
Support for Programming Languages and Operating Systems},
|
|
284
|
+
pages = {253--266},
|
|
285
|
+
year = {2017},
|
|
286
|
+
url = {http://doi.acm.org/10.1145/3037697.3037727},
|
|
287
|
+
doi = {10.1145/3037697.3037727}
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### License
|
|
292
|
+
|
|
293
|
+
ZenFS is licensed under the MIT License. See `LICENSE` for details.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* Standard libc error codes. More will be added to this enum and ErrorStrings as they are
|
|
4
|
+
* needed.
|
|
5
|
+
* @url http://www.gnu.org/software/libc/manual/html_node/Error-Codes.html
|
|
6
|
+
*/
|
|
7
|
+
export declare enum ErrorCode {
|
|
8
|
+
EPERM = 1,
|
|
9
|
+
ENOENT = 2,
|
|
10
|
+
EIO = 5,
|
|
11
|
+
EBADF = 9,
|
|
12
|
+
EACCES = 13,
|
|
13
|
+
EBUSY = 16,
|
|
14
|
+
EEXIST = 17,
|
|
15
|
+
ENOTDIR = 20,
|
|
16
|
+
EISDIR = 21,
|
|
17
|
+
EINVAL = 22,
|
|
18
|
+
EFBIG = 27,
|
|
19
|
+
ENOSPC = 28,
|
|
20
|
+
EROFS = 30,
|
|
21
|
+
ENOTEMPTY = 39,
|
|
22
|
+
ENOTSUP = 95
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Strings associated with each error code.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export declare const ErrorStrings: {
|
|
29
|
+
[code: string | number]: string;
|
|
30
|
+
};
|
|
31
|
+
interface ApiErrorJSON {
|
|
32
|
+
errno: ErrorCode;
|
|
33
|
+
message: string;
|
|
34
|
+
path: string;
|
|
35
|
+
code: string;
|
|
36
|
+
stack: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Represents a ZenFS error. Passed back to applications after a failed
|
|
40
|
+
* call to the ZenFS API.
|
|
41
|
+
*/
|
|
42
|
+
export declare class ApiError extends Error implements NodeJS.ErrnoException {
|
|
43
|
+
static fromJSON(json: ApiErrorJSON): ApiError;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an ApiError object from a buffer.
|
|
46
|
+
*/
|
|
47
|
+
static fromBuffer(buffer: Buffer, i?: number): ApiError;
|
|
48
|
+
static FileError(code: ErrorCode, p: string): ApiError;
|
|
49
|
+
static EACCES(path: string): ApiError;
|
|
50
|
+
static ENOENT(path: string): ApiError;
|
|
51
|
+
static EEXIST(path: string): ApiError;
|
|
52
|
+
static EISDIR(path: string): ApiError;
|
|
53
|
+
static ENOTDIR(path: string): ApiError;
|
|
54
|
+
static EPERM(path: string): ApiError;
|
|
55
|
+
static ENOTEMPTY(path: string): ApiError;
|
|
56
|
+
errno: ErrorCode;
|
|
57
|
+
code: string;
|
|
58
|
+
path?: string;
|
|
59
|
+
syscall: string;
|
|
60
|
+
stack?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Represents a ZenFS error. Passed back to applications after a failed
|
|
63
|
+
* call to the ZenFS API.
|
|
64
|
+
*
|
|
65
|
+
* Error codes mirror those returned by regular Unix file operations, which is
|
|
66
|
+
* what Node returns.
|
|
67
|
+
* @constructor ApiError
|
|
68
|
+
* @param type The type of the error.
|
|
69
|
+
* @param [message] A descriptive error message.
|
|
70
|
+
*/
|
|
71
|
+
constructor(type: ErrorCode, message?: string, path?: string);
|
|
72
|
+
/**
|
|
73
|
+
* @return A friendly error message.
|
|
74
|
+
*/
|
|
75
|
+
toString(): string;
|
|
76
|
+
toJSON(): any;
|
|
77
|
+
/**
|
|
78
|
+
* Writes the API error into a buffer.
|
|
79
|
+
*/
|
|
80
|
+
writeToBuffer(buffer?: Buffer, i?: number): Buffer;
|
|
81
|
+
/**
|
|
82
|
+
* The size of the API error in buffer-form in bytes.
|
|
83
|
+
*/
|
|
84
|
+
bufferSize(): number;
|
|
85
|
+
}
|
|
86
|
+
export {};
|
package/dist/ApiError.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
/**
|
|
3
|
+
* Standard libc error codes. More will be added to this enum and ErrorStrings as they are
|
|
4
|
+
* needed.
|
|
5
|
+
* @url http://www.gnu.org/software/libc/manual/html_node/Error-Codes.html
|
|
6
|
+
*/
|
|
7
|
+
export var ErrorCode;
|
|
8
|
+
(function (ErrorCode) {
|
|
9
|
+
ErrorCode[ErrorCode["EPERM"] = 1] = "EPERM";
|
|
10
|
+
ErrorCode[ErrorCode["ENOENT"] = 2] = "ENOENT";
|
|
11
|
+
ErrorCode[ErrorCode["EIO"] = 5] = "EIO";
|
|
12
|
+
ErrorCode[ErrorCode["EBADF"] = 9] = "EBADF";
|
|
13
|
+
ErrorCode[ErrorCode["EACCES"] = 13] = "EACCES";
|
|
14
|
+
ErrorCode[ErrorCode["EBUSY"] = 16] = "EBUSY";
|
|
15
|
+
ErrorCode[ErrorCode["EEXIST"] = 17] = "EEXIST";
|
|
16
|
+
ErrorCode[ErrorCode["ENOTDIR"] = 20] = "ENOTDIR";
|
|
17
|
+
ErrorCode[ErrorCode["EISDIR"] = 21] = "EISDIR";
|
|
18
|
+
ErrorCode[ErrorCode["EINVAL"] = 22] = "EINVAL";
|
|
19
|
+
ErrorCode[ErrorCode["EFBIG"] = 27] = "EFBIG";
|
|
20
|
+
ErrorCode[ErrorCode["ENOSPC"] = 28] = "ENOSPC";
|
|
21
|
+
ErrorCode[ErrorCode["EROFS"] = 30] = "EROFS";
|
|
22
|
+
ErrorCode[ErrorCode["ENOTEMPTY"] = 39] = "ENOTEMPTY";
|
|
23
|
+
ErrorCode[ErrorCode["ENOTSUP"] = 95] = "ENOTSUP";
|
|
24
|
+
})(ErrorCode || (ErrorCode = {}));
|
|
25
|
+
/**
|
|
26
|
+
* Strings associated with each error code.
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
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.';
|
|
45
|
+
/**
|
|
46
|
+
* Represents a ZenFS error. Passed back to applications after a failed
|
|
47
|
+
* call to the ZenFS API.
|
|
48
|
+
*/
|
|
49
|
+
export class ApiError extends Error {
|
|
50
|
+
static fromJSON(json) {
|
|
51
|
+
const err = new ApiError(json.errno, json.message, json.path);
|
|
52
|
+
err.code = json.code;
|
|
53
|
+
err.stack = json.stack;
|
|
54
|
+
return err;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates an ApiError object from a buffer.
|
|
58
|
+
*/
|
|
59
|
+
static fromBuffer(buffer, i = 0) {
|
|
60
|
+
return ApiError.fromJSON(JSON.parse(buffer.toString('utf8', i + 4, i + 4 + buffer.readUInt32LE(i))));
|
|
61
|
+
}
|
|
62
|
+
static FileError(code, p) {
|
|
63
|
+
return new ApiError(code, ErrorStrings[code], p);
|
|
64
|
+
}
|
|
65
|
+
static EACCES(path) {
|
|
66
|
+
return this.FileError(ErrorCode.EACCES, path);
|
|
67
|
+
}
|
|
68
|
+
static ENOENT(path) {
|
|
69
|
+
return this.FileError(ErrorCode.ENOENT, path);
|
|
70
|
+
}
|
|
71
|
+
static EEXIST(path) {
|
|
72
|
+
return this.FileError(ErrorCode.EEXIST, path);
|
|
73
|
+
}
|
|
74
|
+
static EISDIR(path) {
|
|
75
|
+
return this.FileError(ErrorCode.EISDIR, path);
|
|
76
|
+
}
|
|
77
|
+
static ENOTDIR(path) {
|
|
78
|
+
return this.FileError(ErrorCode.ENOTDIR, path);
|
|
79
|
+
}
|
|
80
|
+
static EPERM(path) {
|
|
81
|
+
return this.FileError(ErrorCode.EPERM, path);
|
|
82
|
+
}
|
|
83
|
+
static ENOTEMPTY(path) {
|
|
84
|
+
return this.FileError(ErrorCode.ENOTEMPTY, path);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Represents a ZenFS error. Passed back to applications after a failed
|
|
88
|
+
* call to the ZenFS API.
|
|
89
|
+
*
|
|
90
|
+
* Error codes mirror those returned by regular Unix file operations, which is
|
|
91
|
+
* what Node returns.
|
|
92
|
+
* @constructor ApiError
|
|
93
|
+
* @param type The type of the error.
|
|
94
|
+
* @param [message] A descriptive error message.
|
|
95
|
+
*/
|
|
96
|
+
constructor(type, message = ErrorStrings[type], path) {
|
|
97
|
+
super(message);
|
|
98
|
+
// Unsupported.
|
|
99
|
+
this.syscall = '';
|
|
100
|
+
this.errno = type;
|
|
101
|
+
this.code = ErrorCode[type];
|
|
102
|
+
this.path = path;
|
|
103
|
+
this.message = `Error: ${this.code}: ${message}${this.path ? `, '${this.path}'` : ''}`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @return A friendly error message.
|
|
107
|
+
*/
|
|
108
|
+
toString() {
|
|
109
|
+
return this.message;
|
|
110
|
+
}
|
|
111
|
+
toJSON() {
|
|
112
|
+
return {
|
|
113
|
+
errno: this.errno,
|
|
114
|
+
code: this.code,
|
|
115
|
+
path: this.path,
|
|
116
|
+
stack: this.stack,
|
|
117
|
+
message: this.message,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Writes the API error into a buffer.
|
|
122
|
+
*/
|
|
123
|
+
writeToBuffer(buffer = Buffer.alloc(this.bufferSize()), i = 0) {
|
|
124
|
+
const bytesWritten = buffer.write(JSON.stringify(this.toJSON()), i + 4);
|
|
125
|
+
buffer.writeUInt32LE(bytesWritten, i);
|
|
126
|
+
return buffer;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The size of the API error in buffer-form in bytes.
|
|
130
|
+
*/
|
|
131
|
+
bufferSize() {
|
|
132
|
+
// 4 bytes for string length.
|
|
133
|
+
return 4 + Buffer.byteLength(JSON.stringify(this.toJSON()));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { type FileSystem, SynchronousFileSystem, FileSystemMetadata } from '../filesystem';
|
|
2
|
+
import { File, FileFlag, PreloadFile } from '../file';
|
|
3
|
+
import { Stats } from '../stats';
|
|
4
|
+
import { Cred } from '../cred';
|
|
5
|
+
import { type BackendOptions } from './backend';
|
|
6
|
+
export declare namespace AsyncMirror {
|
|
7
|
+
/**
|
|
8
|
+
* Configuration options for the AsyncMirror file system.
|
|
9
|
+
*/
|
|
10
|
+
interface Options {
|
|
11
|
+
/**
|
|
12
|
+
* The synchronous file system to mirror the asynchronous file system to.
|
|
13
|
+
*/
|
|
14
|
+
sync: FileSystem;
|
|
15
|
+
/**
|
|
16
|
+
* The asynchronous file system to mirror.
|
|
17
|
+
*/
|
|
18
|
+
async: FileSystem;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* AsyncMirrorFS mirrors a synchronous filesystem into an asynchronous filesystem
|
|
23
|
+
* by:
|
|
24
|
+
*
|
|
25
|
+
* * Performing operations over the in-memory copy, while asynchronously pipelining them
|
|
26
|
+
* to the backing store.
|
|
27
|
+
* * During application loading, the contents of the async file system can be reloaded into
|
|
28
|
+
* the synchronous store, if desired.
|
|
29
|
+
*
|
|
30
|
+
* The two stores will be kept in sync. The most common use-case is to pair a synchronous
|
|
31
|
+
* in-memory filesystem with an asynchronous backing store.
|
|
32
|
+
*
|
|
33
|
+
* Example: Mirroring an IndexedDB file system to an in memory file system. Now, you can use
|
|
34
|
+
* IndexedDB synchronously.
|
|
35
|
+
*
|
|
36
|
+
* ```javascript
|
|
37
|
+
* ZenFS.configure({
|
|
38
|
+
* fs: "AsyncMirror",
|
|
39
|
+
* options: {
|
|
40
|
+
* sync: { fs: "InMemory" },
|
|
41
|
+
* async: { fs: "IndexedDB" }
|
|
42
|
+
* }
|
|
43
|
+
* }, function(e) {
|
|
44
|
+
* // ZenFS is initialized and ready-to-use!
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* Or, alternatively:
|
|
49
|
+
*
|
|
50
|
+
* ```javascript
|
|
51
|
+
* ZenFS.Backend.IndexedDB.Create(function(e, idbfs) {
|
|
52
|
+
* ZenFS.Backend.InMemory.Create(function(e, inMemory) {
|
|
53
|
+
* ZenFS.Backend.AsyncMirror({
|
|
54
|
+
* sync: inMemory, async: idbfs
|
|
55
|
+
* }, function(e, mirrored) {
|
|
56
|
+
* ZenFS.initialize(mirrored);
|
|
57
|
+
* });
|
|
58
|
+
* });
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare class AsyncMirror extends SynchronousFileSystem {
|
|
63
|
+
static readonly Name = "AsyncMirror";
|
|
64
|
+
static Create: any;
|
|
65
|
+
static readonly Options: BackendOptions;
|
|
66
|
+
static isAvailable(): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Queue of pending asynchronous operations.
|
|
69
|
+
*/
|
|
70
|
+
private _queue;
|
|
71
|
+
private _queueRunning;
|
|
72
|
+
private _sync;
|
|
73
|
+
private _async;
|
|
74
|
+
private _isInitialized;
|
|
75
|
+
private _initializeCallbacks;
|
|
76
|
+
/**
|
|
77
|
+
*
|
|
78
|
+
* Mirrors the synchronous file system into the asynchronous file system.
|
|
79
|
+
*
|
|
80
|
+
* @param sync The synchronous file system to mirror the asynchronous file system to.
|
|
81
|
+
* @param async The asynchronous file system to mirror.
|
|
82
|
+
*/
|
|
83
|
+
constructor({ sync, async }: AsyncMirror.Options);
|
|
84
|
+
get metadata(): FileSystemMetadata;
|
|
85
|
+
_syncSync(fd: PreloadFile<AsyncMirror>): void;
|
|
86
|
+
renameSync(oldPath: string, newPath: string, cred: Cred): void;
|
|
87
|
+
statSync(p: string, cred: Cred): Stats;
|
|
88
|
+
openSync(p: string, flag: FileFlag, mode: number, cred: Cred): File;
|
|
89
|
+
unlinkSync(p: string, cred: Cred): void;
|
|
90
|
+
rmdirSync(p: string, cred: Cred): void;
|
|
91
|
+
mkdirSync(p: string, mode: number, cred: Cred): void;
|
|
92
|
+
readdirSync(p: string, cred: Cred): string[];
|
|
93
|
+
existsSync(p: string, cred: Cred): boolean;
|
|
94
|
+
chmodSync(p: string, mode: number, cred: Cred): void;
|
|
95
|
+
chownSync(p: string, new_uid: number, new_gid: number, cred: Cred): void;
|
|
96
|
+
utimesSync(p: string, atime: Date, mtime: Date, cred: Cred): void;
|
|
97
|
+
/**
|
|
98
|
+
* Called once to load up files from async storage into sync storage.
|
|
99
|
+
*/
|
|
100
|
+
private _initialize;
|
|
101
|
+
private enqueueOp;
|
|
102
|
+
}
|