@zenfs/core 0.7.0 → 0.7.2
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/dist/backends/backend.d.ts +18 -24
- package/dist/browser.min.js.map +2 -2
- package/dist/config.d.ts +2 -2
- package/package.json +1 -1
- package/readme.md +18 -39
package/dist/config.d.ts
CHANGED
|
@@ -3,12 +3,12 @@ import { FileSystem } from './filesystem.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* Configuration for a specific mount point
|
|
5
5
|
*/
|
|
6
|
-
export type MountConfiguration<FS extends FileSystem = FileSystem> = FS | BackendConfiguration<FS> | Backend<FS>;
|
|
6
|
+
export type MountConfiguration<FS extends FileSystem = FileSystem, TOptions extends object = object> = FS | BackendConfiguration<FS, TOptions> | Backend<FS, TOptions>;
|
|
7
7
|
/**
|
|
8
8
|
* Retrieve a file system with the given configuration.
|
|
9
9
|
* @param config A BackendConfig object.
|
|
10
10
|
*/
|
|
11
|
-
export declare function resolveMountConfig<FS extends FileSystem>(config: MountConfiguration<FS>, _depth?: number): Promise<FS>;
|
|
11
|
+
export declare function resolveMountConfig<FS extends FileSystem, TOptions extends object = object>(config: MountConfiguration<FS, TOptions>, _depth?: number): Promise<FS>;
|
|
12
12
|
/**
|
|
13
13
|
*A mapping of mount points to their configurations
|
|
14
14
|
*/
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -8,14 +8,10 @@ ZenFS is a fork of [BrowserFS](https://github.com/jvilk/BrowserFS).
|
|
|
8
8
|
|
|
9
9
|
## Backends
|
|
10
10
|
|
|
11
|
-
ZenFS is modular and extensible. The core includes
|
|
11
|
+
ZenFS is modular and extensible. The core includes two built-in backends:
|
|
12
12
|
|
|
13
13
|
- `InMemory`: Stores files in-memory. This is cleared when the runtime ends (e.g. a user navigating away from a web page or a Node process exiting)
|
|
14
|
-
- `Overlay`: Use read-only file system as read-write by overlaying a writable file system on top of it.
|
|
15
|
-
- `AsyncMirror`: Use an asynchronous backend synchronously. This is very helpful for asynchronous backends
|
|
16
|
-
|
|
17
|
-
> [!NOTE]
|
|
18
|
-
> When constructed, `AsyncMirror` loads the entire contents of the async file system into a synchronous backend. It performs operations on the synchronous file system and then queues them to be mirrored onto the asynchronous backend.
|
|
14
|
+
- `Overlay`: Use read-only file system as read-write by overlaying a writable file system on top of it. ([copy-on-write](https://en.wikipedia.org/wiki/Copy-on-write))
|
|
19
15
|
|
|
20
16
|
ZenFS supports a number of other backends. Many are provided as separate packages under `@zenfs`. More backends can be defined by separate libraries by extending the `FileSystem` class and/or providing a `Backend` object.
|
|
21
17
|
|
|
@@ -74,13 +70,13 @@ await configure({
|
|
|
74
70
|
> 2. An object that has the options accepted by the backend and a `backend` property which is a `Backend` object
|
|
75
71
|
> 3. A `FileSystem` instance (_not recommended_)
|
|
76
72
|
|
|
77
|
-
Here is an example that mounts the `
|
|
73
|
+
Here is an example that mounts the `WebStorage` backend from `@zenfs/dom` on `/`:
|
|
78
74
|
|
|
79
75
|
```js
|
|
80
76
|
import { configure, fs } from '@zenfs/core';
|
|
81
|
-
import {
|
|
77
|
+
import { WebStorage } from '@zenfs/dom';
|
|
82
78
|
|
|
83
|
-
await configure({ backend:
|
|
79
|
+
await configure({ backend: WebStorage });
|
|
84
80
|
|
|
85
81
|
if (!fs.existsSync('/test.txt')) {
|
|
86
82
|
fs.writeFileSync('/test.txt', 'This will persist across reloads!');
|
|
@@ -95,50 +91,33 @@ console.log(contents);
|
|
|
95
91
|
The FS promises API is exposed as `promises`.
|
|
96
92
|
|
|
97
93
|
```js
|
|
98
|
-
import { configure
|
|
94
|
+
import { configure } from '@zenfs/core';
|
|
95
|
+
import { exists, writeFile } from '@zenfs/core/promises';
|
|
99
96
|
import { IndexedDB } from '@zenfs/dom';
|
|
100
97
|
|
|
101
98
|
await configure({ '/': IndexedDB });
|
|
102
99
|
|
|
103
|
-
const exists = await
|
|
100
|
+
const exists = await exists('/myfile.txt');
|
|
104
101
|
if (!exists) {
|
|
105
|
-
await
|
|
102
|
+
await writeFile('/myfile.txt', 'Lots of persistant data');
|
|
106
103
|
}
|
|
107
104
|
```
|
|
108
105
|
|
|
109
106
|
> [!NOTE]
|
|
110
|
-
> You can import the promises API using
|
|
111
|
-
|
|
112
|
-
>
|
|
113
|
-
>
|
|
114
|
-
|
|
115
|
-
#### Using asynchronous backends synchronously
|
|
116
|
-
|
|
117
|
-
You may have noticed that attempting to use a synchronous function on an asynchronous backend (e.g. `IndexedDB`) results in a "not supplied" error (`ENOTSUP`). If you would like to use an asynchronous backend synchronously you need to wrap it in an `AsyncMirror`:
|
|
118
|
-
|
|
119
|
-
```js
|
|
120
|
-
import { configure, fs, AsyncMirror, InMemory } from '@zenfs/core';
|
|
121
|
-
import { IndexedDB } from '@zenfs/dom';
|
|
122
|
-
|
|
123
|
-
await configure({
|
|
124
|
-
'/': {
|
|
125
|
-
backend: AsyncMirror,
|
|
126
|
-
sync: InMemory,
|
|
127
|
-
async: IndexedDB,
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
fs.writeFileSync('/persistant.txt', 'My persistant data');
|
|
132
|
-
```
|
|
107
|
+
> You can import the promises API using:
|
|
108
|
+
>
|
|
109
|
+
> 1. Exports from `@zenfs/core/promises`
|
|
110
|
+
> 2. The `promises` export from `@zenfs/core`
|
|
111
|
+
> 3. `fs.promises` on the exported `fs` from `@zenfs/core`.
|
|
133
112
|
|
|
134
113
|
#### Mounting and unmounting, creating backends
|
|
135
114
|
|
|
136
|
-
If you would like to create backends without configure (e.g. to do something dynamic at runtime), you may do so by importing the backend and calling `
|
|
115
|
+
If you would like to create backends without configure (e.g. to do something dynamic at runtime), you may do so by importing the backend and calling `resolveMountConfig` with it.
|
|
137
116
|
|
|
138
117
|
You can then mount and unmount the backend instance by using `mount` and `umount`.
|
|
139
118
|
|
|
140
119
|
```js
|
|
141
|
-
import { configure,
|
|
120
|
+
import { configure, resolveMountConfig, InMemory } from '@zenfs/core';
|
|
142
121
|
import { IndexedDB } from '@zenfs/dom';
|
|
143
122
|
import { Zip } from '@zenfs/zip';
|
|
144
123
|
|
|
@@ -150,8 +129,8 @@ await configure({
|
|
|
150
129
|
fs.mkdirSync('/mnt');
|
|
151
130
|
|
|
152
131
|
const res = await fetch('mydata.zip');
|
|
153
|
-
const
|
|
154
|
-
fs.mount('/mnt/zip',
|
|
132
|
+
const zipfs = await resolveMountConfig({ backend: Zip, zipData: await res.arrayBuffer() });
|
|
133
|
+
fs.mount('/mnt/zip', zipfs);
|
|
155
134
|
|
|
156
135
|
// do stuff with the mounted zip
|
|
157
136
|
|