@zenfs/core
Advanced tools
Comparing version 0.7.0 to 0.7.1
@@ -11,3 +11,3 @@ import { FileSystem } from '../filesystem.js'; | ||
*/ | ||
type: OptionType | OptionType[]; | ||
type: OptionType | readonly OptionType[]; | ||
/** | ||
@@ -14,0 +14,0 @@ * Whether or not the option is required (optional can be set to null or undefined). Defaults to false. |
@@ -6,3 +6,3 @@ import type { Backend, BackendConfiguration } from './backends/backend.js'; | ||
*/ | ||
export type MountConfiguration<FS extends FileSystem = FileSystem> = FS | BackendConfiguration<FS> | Backend<FS>; | ||
export type MountConfiguration<FS extends FileSystem = FileSystem, TOptions extends object = object> = FS | BackendConfiguration<FS, TOptions> | Backend<FS, TOptions>; | ||
/** | ||
@@ -12,3 +12,3 @@ * Retrieve a file system with the given configuration. | ||
*/ | ||
export declare function resolveMountConfig<FS extends FileSystem>(config: MountConfiguration<FS>, _depth?: number): Promise<FS>; | ||
export declare function resolveMountConfig<FS extends FileSystem, TOptions extends object = object>(config: MountConfiguration<FS, TOptions>, _depth?: number): Promise<FS>; | ||
/** | ||
@@ -15,0 +15,0 @@ *A mapping of mount points to their configurations |
{ | ||
"name": "@zenfs/core", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "A filesystem in your browser", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -11,11 +11,7 @@ # ZenFS | ||
ZenFS is modular and extensible. The core includes a few built-in backends: | ||
ZenFS is modular and extensible. The core includes two built-in backends: | ||
- `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) | ||
- `Overlay`: Use read-only file system as read-write by overlaying a writable file system on top of it. | ||
- `AsyncMirror`: Use an asynchronous backend synchronously. This is very helpful for asynchronous backends | ||
- `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)) | ||
> [!NOTE] | ||
> 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. | ||
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. | ||
@@ -78,9 +74,9 @@ | ||
Here is an example that mounts the `Storage` backend from `@zenfs/dom` on `/`: | ||
Here is an example that mounts the `WebStorage` backend from `@zenfs/dom` on `/`: | ||
```js | ||
import { configure, fs } from '@zenfs/core'; | ||
import { Storage } from '@zenfs/dom'; | ||
import { WebStorage } from '@zenfs/dom'; | ||
await configure({ backend: Storage }); | ||
await configure({ backend: WebStorage }); | ||
@@ -100,3 +96,4 @@ if (!fs.existsSync('/test.txt')) { | ||
```js | ||
import { configure, promises } from '@zenfs/core'; | ||
import { configure } from '@zenfs/core'; | ||
import { exists, writeFile } from '@zenfs/core/promises'; | ||
import { IndexedDB } from '@zenfs/dom'; | ||
@@ -106,5 +103,5 @@ | ||
const exists = await promises.exists('/myfile.txt'); | ||
const exists = await exists('/myfile.txt'); | ||
if (!exists) { | ||
await promises.write('/myfile.txt', 'Lots of persistant data'); | ||
await writeFile('/myfile.txt', 'Lots of persistant data'); | ||
} | ||
@@ -114,29 +111,11 @@ ``` | ||
> [!NOTE] | ||
> You can import the promises API using `promises`, or using `fs.promises` on the exported `fs`. | ||
> You can import the promises API using: | ||
> | ||
> 1. Exports from `@zenfs/core/promises` | ||
> 2. The `promises` export from `@zenfs/core` | ||
> 3. `fs.promises` on the exported `fs` from `@zenfs/core`. | ||
> [!IMPORTANT] | ||
> ZenFS does _not_ provide a separate public import for importing promises like `fs/promises`. If you are using ESM, you can import promises functions like `fs/promises` from the `dist/emulation/promises.ts` file, though this may change at any time and is **not recommended**. | ||
#### Using asynchronous backends synchronously | ||
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`: | ||
```js | ||
import { configure, fs, AsyncMirror, InMemory } from '@zenfs/core'; | ||
import { IndexedDB } from '@zenfs/dom'; | ||
await configure({ | ||
'/': { | ||
backend: AsyncMirror, | ||
sync: InMemory, | ||
async: IndexedDB, | ||
}, | ||
}); | ||
fs.writeFileSync('/persistant.txt', 'My persistant data'); | ||
``` | ||
#### Mounting and unmounting, creating backends | ||
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 `createBackend` with it. | ||
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. | ||
@@ -146,3 +125,3 @@ You can then mount and unmount the backend instance by using `mount` and `umount`. | ||
```js | ||
import { configure, createBackend, InMemory } from '@zenfs/core'; | ||
import { configure, resolveMountConfig, InMemory } from '@zenfs/core'; | ||
import { IndexedDB } from '@zenfs/dom'; | ||
@@ -159,4 +138,4 @@ import { Zip } from '@zenfs/zip'; | ||
const res = await fetch('mydata.zip'); | ||
const zipFs = await createBackend(Zip, { zipData: await res.arrayBuffer() }); | ||
fs.mount('/mnt/zip', zipFs); | ||
const zipfs = await resolveMountConfig({ backend: Zip, zipData: await res.arrayBuffer() }); | ||
fs.mount('/mnt/zip', zipfs); | ||
@@ -163,0 +142,0 @@ // do stuff with the mounted zip |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1466067
157