@zenfs/core 0.4.0 → 0.4.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.
@@ -0,0 +1,23 @@
1
+ import type { Backend, BackendConfig } from './backends/backend.js';
2
+ import { FileSystem } from './filesystem.js';
3
+ /**
4
+ * Initializes ZenFS with the given file systems.
5
+ */
6
+ export declare function initialize(mounts: {
7
+ [point: string]: FileSystem;
8
+ }, uid?: number, gid?: number): void;
9
+ /**
10
+ * Defines a mapping of mount points to their configurations
11
+ */
12
+ export interface ConfigMapping {
13
+ [mountPoint: string]: FileSystem | BackendConfig | Backend;
14
+ }
15
+ /**
16
+ * A configuration for ZenFS
17
+ */
18
+ export type Configuration = FileSystem | BackendConfig | ConfigMapping;
19
+ /**
20
+ * Creates filesystems with the given configuration, and initializes ZenFS with it.
21
+ * See the Configuration type for more info on the configuration object.
22
+ */
23
+ export declare function configure(config: Configuration): Promise<void>;
package/dist/config.js ADDED
@@ -0,0 +1,36 @@
1
+ import { isBackend, resolveBackendConfig } from './backends/backend.js';
2
+ import { Cred } from './cred.js';
3
+ import * as fs from './emulation/index.js';
4
+ import { setCred } from './emulation/shared.js';
5
+ import { FileSystem } from './filesystem.js';
6
+ /**
7
+ * Initializes ZenFS with the given file systems.
8
+ */
9
+ export function initialize(mounts, uid = 0, gid = 0) {
10
+ setCred(new Cred(uid, gid, uid, gid, uid, gid));
11
+ fs.initialize(mounts);
12
+ }
13
+ /**
14
+ * Creates filesystems with the given configuration, and initializes ZenFS with it.
15
+ * See the Configuration type for more info on the configuration object.
16
+ */
17
+ export async function configure(config) {
18
+ if ('backend' in config || config instanceof FileSystem) {
19
+ // single FS
20
+ config = { '/': config };
21
+ }
22
+ for (let [point, value] of Object.entries(config)) {
23
+ if (typeof value == 'number') {
24
+ //should never happen
25
+ continue;
26
+ }
27
+ if (value instanceof FileSystem) {
28
+ continue;
29
+ }
30
+ if (isBackend(value)) {
31
+ value = { backend: value };
32
+ }
33
+ config[point] = await resolveBackendConfig(value);
34
+ }
35
+ initialize(config);
36
+ }
package/dist/index.d.ts CHANGED
@@ -1,31 +1,4 @@
1
- /**
2
- * ZenFS's main module. This is exposed in the browser via the ZenFS global.
3
- */
4
- import * as fs from './emulation/index.js';
5
- import { FileSystem } from './filesystem.js';
6
- import { type Backend, type BackendConfig } from './backends/backend.js';
7
- /**
8
- * Initializes ZenFS with the given file systems.
9
- */
10
- export declare function initialize(mounts: {
11
- [point: string]: FileSystem;
12
- }, uid?: number, gid?: number): void;
13
- /**
14
- * Defines a mapping of mount points to their configurations
15
- */
16
- export interface ConfigMapping {
17
- [mountPoint: string]: FileSystem | BackendConfig | Backend;
18
- }
19
- /**
20
- * A configuration for ZenFS
21
- */
22
- export type Configuration = FileSystem | BackendConfig | ConfigMapping;
23
- /**
24
- * Creates filesystems with the given configuration, and initializes ZenFS with it.
25
- * See the Configuration type for more info on the configuration object.
26
- */
27
- export declare function configure(config: Configuration): Promise<void>;
28
- export type { Backend, BackendConfig } from './backends/backend.js';
1
+ export * from './backends/backend.js';
29
2
  export * from './backends/AsyncMirror.js';
30
3
  export * from './backends/AsyncStore.js';
31
4
  export * from './backends/InMemory.js';
@@ -33,6 +6,7 @@ export * from './backends/Locked.js';
33
6
  export * from './backends/Overlay.js';
34
7
  export * from './backends/SyncStore.js';
35
8
  export * from './ApiError.js';
9
+ export * from './config.js';
36
10
  export * from './cred.js';
37
11
  export * from './file.js';
38
12
  export * from './filesystem.js';
@@ -41,5 +15,6 @@ export * from './inode.js';
41
15
  export * from './mutex.js';
42
16
  export * from './stats.js';
43
17
  export * from './utils.js';
18
+ import * as fs from './emulation/index.js';
44
19
  export { fs };
45
20
  export default fs;
package/dist/index.js CHANGED
@@ -1,42 +1,4 @@
1
- /**
2
- * ZenFS's main module. This is exposed in the browser via the ZenFS global.
3
- */
4
- import * as fs from './emulation/index.js';
5
- import { FileSystem } from './filesystem.js';
6
- import { Cred } from './cred.js';
7
- import { isBackend, resolveBackendConfig } from './backends/backend.js';
8
- import { setCred } from './emulation/shared.js';
9
- /**
10
- * Initializes ZenFS with the given file systems.
11
- */
12
- export function initialize(mounts, uid = 0, gid = 0) {
13
- setCred(new Cred(uid, gid, uid, gid, uid, gid));
14
- fs.initialize(mounts);
15
- }
16
- /**
17
- * Creates filesystems with the given configuration, and initializes ZenFS with it.
18
- * See the Configuration type for more info on the configuration object.
19
- */
20
- export async function configure(config) {
21
- if ('backend' in config || config instanceof FileSystem) {
22
- // single FS
23
- config = { '/': config };
24
- }
25
- for (let [point, value] of Object.entries(config)) {
26
- if (typeof value == 'number') {
27
- //should never happen
28
- continue;
29
- }
30
- if (value instanceof FileSystem) {
31
- continue;
32
- }
33
- if (isBackend(value)) {
34
- value = { backend: value };
35
- }
36
- config[point] = await resolveBackendConfig(value);
37
- }
38
- initialize(config);
39
- }
1
+ export * from './backends/backend.js';
40
2
  export * from './backends/AsyncMirror.js';
41
3
  export * from './backends/AsyncStore.js';
42
4
  export * from './backends/InMemory.js';
@@ -44,6 +6,7 @@ export * from './backends/Locked.js';
44
6
  export * from './backends/Overlay.js';
45
7
  export * from './backends/SyncStore.js';
46
8
  export * from './ApiError.js';
9
+ export * from './config.js';
47
10
  export * from './cred.js';
48
11
  export * from './file.js';
49
12
  export * from './filesystem.js';
@@ -52,5 +15,6 @@ export * from './inode.js';
52
15
  export * from './mutex.js';
53
16
  export * from './stats.js';
54
17
  export * from './utils.js';
18
+ import * as fs from './emulation/index.js';
55
19
  export { fs };
56
20
  export default fs;
package/dist/stats.d.ts CHANGED
@@ -202,3 +202,19 @@ export declare class BigIntStats extends StatsCommon<bigint> implements Node.Big
202
202
  */
203
203
  static clone(stats: BigIntStats | Stats): BigIntStats;
204
204
  }
205
+ /**
206
+ * @returns true if stats is a file.
207
+ */
208
+ export declare function isFile(stats: StatsLike): boolean;
209
+ /**
210
+ * @returns True if stats is a directory.
211
+ */
212
+ export declare function isDirectory(stats: StatsLike): boolean;
213
+ /**
214
+ * @returns true if stats is a symbolic link
215
+ */
216
+ export declare function isSymbolicLink(stats: StatsLike): boolean;
217
+ export declare function isSocket(): boolean;
218
+ export declare function isBlockDevice(): boolean;
219
+ export declare function isCharacterDevice(): boolean;
220
+ export declare function isFIFO(): boolean;
package/dist/stats.js CHANGED
@@ -246,3 +246,33 @@ export class BigIntStats extends StatsCommon {
246
246
  }
247
247
  }
248
248
  BigIntStats;
249
+ /**
250
+ * @returns true if stats is a file.
251
+ */
252
+ export function isFile(stats) {
253
+ return (Number(stats.mode) & S_IFMT) === S_IFREG;
254
+ }
255
+ /**
256
+ * @returns True if stats is a directory.
257
+ */
258
+ export function isDirectory(stats) {
259
+ return (Number(stats.mode) & S_IFMT) === S_IFDIR;
260
+ }
261
+ /**
262
+ * @returns true if stats is a symbolic link
263
+ */
264
+ export function isSymbolicLink(stats) {
265
+ return (Number(stats.mode) & S_IFMT) === S_IFLNK;
266
+ }
267
+ export function isSocket() {
268
+ return false;
269
+ }
270
+ export function isBlockDevice() {
271
+ return false;
272
+ }
273
+ export function isCharacterDevice() {
274
+ return false;
275
+ }
276
+ export function isFIFO() {
277
+ return false;
278
+ }
package/license.md CHANGED
@@ -1,8 +1,6 @@
1
- ZenFS's license follows:
1
+ Copyright (c) 2023-2024 James P. and other ZenFS contributors.
2
2
 
3
- ====
4
-
5
- Copyright (c) 2013-2023 John Vilk and other ZenFS contributors.
3
+ Copyright (c) 2013-2023 John Vilk and other BrowserFS contributors.
6
4
 
7
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
8
6
  this software and associated documentation files (the "Software"), to deal in
@@ -22,12 +20,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
21
  SOFTWARE.
24
22
 
25
- ====
26
-
27
23
  This license applies to all parts of ZenFS, except for the following items:
28
24
 
29
- - The test fixtures located in `test/fixtures/files/node`. Their license follows:
30
- """
25
+ - The test fixtures located in `test/fixtures/node`. Their license follows:
26
+ """
31
27
  Copyright Joyent, Inc. and other Node contributors. All rights reserved.
32
28
  Permission is hereby granted, free of charge, to any person obtaining a copy
33
29
  of this software and associated documentation files (the "Software"), to
@@ -46,77 +42,4 @@ This license applies to all parts of ZenFS, except for the following items:
46
42
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47
43
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
48
44
  IN THE SOFTWARE.
49
- """
50
-
51
- - The Emscripten file system in src/generic/emscripten_fs.ts is a modified
52
- version of Emscripten's NODEFS. Emscripten's license follows:
53
- """
54
- Emscripten is available under 2 licenses, the MIT license and the
55
- University of Illinois/NCSA Open Source License.
56
-
57
- Both are permissive open source licenses, with little if any
58
- practical difference between them.
59
-
60
- The reason for offering both is that (1) the MIT license is
61
- well-known, while (2) the University of Illinois/NCSA Open Source
62
- License allows Emscripten's code to be integrated upstream into
63
- LLVM, which uses that license, should the opportunity arise.
64
-
65
- The full text of both licenses follows.
66
-
67
- ==============================================================================
68
-
69
- Copyright (c) 2010-2011 Emscripten authors, see AUTHORS file.
70
-
71
- Permission is hereby granted, free of charge, to any person obtaining a copy
72
- of this software and associated documentation files (the "Software"), to deal
73
- in the Software without restriction, including without limitation the rights
74
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
75
- copies of the Software, and to permit persons to whom the Software is
76
- furnished to do so, subject to the following conditions:
77
-
78
- The above copyright notice and this permission notice shall be included in
79
- all copies or substantial portions of the Software.
80
-
81
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
82
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
83
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
84
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
85
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
86
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
87
- THE SOFTWARE.
88
-
89
- ==============================================================================
90
-
91
- Copyright (c) 2010-2011 Emscripten authors, see AUTHORS file.
92
- All rights reserved.
93
-
94
- Permission is hereby granted, free of charge, to any person obtaining a
95
- copy of this software and associated documentation files (the
96
- "Software"), to deal with the Software without restriction, including
97
- without limitation the rights to use, copy, modify, merge, publish,
98
- distribute, sublicense, and/or sell copies of the Software, and to
99
- permit persons to whom the Software is furnished to do so, subject to
100
- the following conditions:
101
-
102
- Redistributions of source code must retain the above copyright
103
- notice, this list of conditions and the following disclaimers.
104
-
105
- Redistributions in binary form must reproduce the above
106
- copyright notice, this list of conditions and the following disclaimers
107
- in the documentation and/or other materials provided with the
108
- distribution.
109
-
110
- Neither the names of Mozilla,
111
- nor the names of its contributors may be used to endorse
112
- or promote products derived from this Software without specific prior
113
- written permission.
114
-
115
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
116
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
117
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
118
- IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR
119
- ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
120
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
121
- SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
122
- """
45
+ """
package/package.json CHANGED
@@ -1,69 +1,73 @@
1
1
  {
2
- "name": "@zenfs/core",
3
- "version": "0.4.0",
4
- "description": "A filesystem in your browser",
5
- "main": "dist/index.js",
6
- "types": "dist",
7
- "keywords": [
8
- "filesystem",
9
- "node",
10
- "storage"
11
- ],
12
- "bin": {
13
- "make-index": "./scripts/make-index.js"
14
- },
15
- "type": "module",
16
- "homepage": "https://github.com/zen-fs/core",
17
- "author": "James P. <jp@drvortex.dev> (https://drvortex.dev)",
18
- "license": "MIT",
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/zen-fs/core.git"
22
- },
23
- "bugs": {
24
- "url": "https://github.com/zen-fs/core/issues"
25
- },
26
- "engines": {
27
- "node": ">= 18"
28
- },
29
- "exports": {
30
- "./*": "./dist/*"
31
- },
32
- "typesVersions": {
33
- "*": {
34
- "*": [
35
- "./dist/*"
36
- ]
37
- }
38
- },
39
- "scripts": {
40
- "format": "prettier --write src test",
41
- "format:check": "prettier --check src test",
42
- "lint": "eslint src test && tsc -p tsconfig.json --noEmit",
43
- "test": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest",
44
- "build": "node scripts/build.js",
45
- "build:docs": "typedoc --out docs --name ZenFS src/index.ts",
46
- "dev": "node scripts/build.js --watch",
47
- "prepublishOnly": "npm run build"
48
- },
49
- "dependencies": {
50
- "@types/node": "^14.0.0",
51
- "@types/readable-stream": "^4.0.10",
52
- "minimatch": "^9.0.3",
53
- "readable-stream": "^4.5.2"
54
- },
55
- "devDependencies": {
56
- "@jest/globals": "^29.5.0",
57
- "@types/jest": "^29.5.1",
58
- "@typescript-eslint/eslint-plugin": "^5.55.0",
59
- "@typescript-eslint/parser": "^5.55.0",
60
- "cross-env": "^7.0.3",
61
- "esbuild": "^0.17.18",
62
- "eslint": "^8.36.0",
63
- "jest": "^29.5.0",
64
- "prettier": "^2.8.7",
65
- "ts-jest": "^29.1.0",
66
- "typedoc": "^0.25.1",
67
- "typescript": "^4.9.5"
68
- }
2
+ "name": "@zenfs/core",
3
+ "version": "0.4.2",
4
+ "description": "A filesystem in your browser",
5
+ "main": "dist/index.js",
6
+ "types": "dist",
7
+ "keywords": [
8
+ "filesystem",
9
+ "node",
10
+ "storage"
11
+ ],
12
+ "bin": {
13
+ "make-index": "scripts/make-index.js"
14
+ },
15
+ "type": "module",
16
+ "homepage": "https://github.com/zen-fs/core",
17
+ "author": "James P. <jp@drvortex.dev> (https://drvortex.dev)",
18
+ "contributors": [
19
+ "John Vilk <jvilk@cs.umass.edu>"
20
+ ],
21
+ "license": "MIT",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/zen-fs/core.git"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/zen-fs/core/issues"
28
+ },
29
+ "engines": {
30
+ "node": ">= 18"
31
+ },
32
+ "exports": {
33
+ ".": "./dist/index.js",
34
+ "./*": "./dist/*"
35
+ },
36
+ "typesVersions": {
37
+ "*": {
38
+ "*": [
39
+ "./dist/*"
40
+ ]
41
+ }
42
+ },
43
+ "scripts": {
44
+ "format": "prettier --write .",
45
+ "format:check": "prettier --check .",
46
+ "lint": "eslint src tests && tsc -p tsconfig.json --noEmit",
47
+ "test": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest",
48
+ "build": "node scripts/build.js",
49
+ "build:docs": "typedoc --out docs --name ZenFS src/index.ts",
50
+ "dev": "node scripts/build.js --watch",
51
+ "prepublishOnly": "npm run build"
52
+ },
53
+ "dependencies": {
54
+ "@types/node": "^14.0.0",
55
+ "@types/readable-stream": "^4.0.10",
56
+ "minimatch": "^9.0.3",
57
+ "readable-stream": "^4.5.2"
58
+ },
59
+ "devDependencies": {
60
+ "@jest/globals": "^29.5.0",
61
+ "@types/jest": "^29.5.1",
62
+ "@typescript-eslint/eslint-plugin": "^5.55.0",
63
+ "@typescript-eslint/parser": "^5.55.0",
64
+ "cross-env": "^7.0.3",
65
+ "esbuild": "^0.17.18",
66
+ "eslint": "^8.36.0",
67
+ "jest": "^29.5.0",
68
+ "prettier": "^3.2.5",
69
+ "ts-jest": "^29.1.0",
70
+ "typedoc": "^0.25.1",
71
+ "typescript": "^4.9.5"
72
+ }
69
73
  }
package/readme.md CHANGED
@@ -1,23 +1,25 @@
1
1
  # ZenFS
2
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 with other tools.
3
+ ZenFS is a file system that emulates the [NodeJS filesystem API](http://nodejs.org/api/fs.html).
4
+
5
+ It works using a system of backends, which are used by ZenFS to store and retrieve data. ZenFS can also integrate with other tools.
6
+
7
+ ZenFS is a fork of [BrowserFS](https://github.com/jvilk/BrowserFS).
4
8
 
5
9
  ## Backends
6
10
 
7
- ZenFS is highly extensible, and includes a few built-in backends:
11
+ ZenFS is modular and extensible. The core includes a few built-in backends:
8
12
 
9
- - `InMemory`: Stores files in-memory. It is a temporary file store that clears when the user navigates away.
10
- - `Overlay`: 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!
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
12
16
 
13
17
  > [!NOTE]
14
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.
15
19
 
16
- More backends can be defined by separate libraries, as long as they implement `FileSystem`.
17
-
18
- ZenFS supports a number of other backends. Many are provided as seperate packages under `@zenfs`.
20
+ ZenFS supports a number of other backends. Many are provided as seperate packages under `@zenfs`. More backends can be defined by separate libraries by extending the `FileSystem` class and/or providing a `Backend` object.
19
21
 
20
- For more information, see the [API documentation for ZenFS](https://zen-fs.github.io/core).
22
+ For more information, see the [docs](https://zen-fs.github.io/core).
21
23
 
22
24
  ## Installing
23
25
 
@@ -28,12 +30,14 @@ npm install @zenfs/core
28
30
  ## Usage
29
31
 
30
32
  > [!NOTE]
31
- > The examples are written in ESM. If you are using CJS, you can `require` the package. If running in a browser you can add a script tag to your HTML pointing to the `browser.min.js` and use ZenFS via the global `ZenFS` object.
33
+ > The examples are written in ESM.
34
+ > If you are using CJS, you can `require` the package.
35
+ > If using a browser environment without support for `type=module` in `script` tags, you can add a `script` tag to your HTML pointing to the `browser.min.js` and use ZenFS with the global `ZenFS` object.
32
36
 
33
37
  ```js
34
- import fs from '@zenfs/core';
38
+ import fs from '@zenfs/core'; // You can also use the named export, `fs`
35
39
 
36
- fs.writeFileSync('/test.txt', 'Cool, I can do this in the browser!');
40
+ fs.writeFileSync('/test.txt', 'Cool, I can do this in any JS environment (including browsers)!');
37
41
 
38
42
  const contents = fs.readFileSync('/test.txt', 'utf-8');
39
43
  console.log(contents);
@@ -66,9 +70,9 @@ await configure({
66
70
  > [!TIP]
67
71
  > When configuring a mount point, you can pass in
68
72
  >
69
- > 1. A string that maps to a built-in backend
70
- > 2. A `Backend` object, if the backend has no required options
71
- > 3. An object that has the options accepted by the backend and a `backend` property which is (1) or (2)
73
+ > 1. A `Backend` object, if the backend has no required options
74
+ > 2. An object that has the options accepted by the backend and a `backend` property which is a `Backend` object
75
+ > 3. A `FileSystem` instance (_not recommended_)
72
76
 
73
77
  Here is an example that mounts the `Storage` backend from `@zenfs/dom` on `/`:
74
78