@zenfs/core 0.3.5 → 0.4.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.
Files changed (47) hide show
  1. package/dist/FileIndex.d.ts +4 -0
  2. package/dist/FileIndex.js +3 -3
  3. package/dist/backends/AsyncMirror.d.ts +3 -5
  4. package/dist/backends/AsyncMirror.js +7 -6
  5. package/dist/backends/AsyncStore.d.ts +9 -4
  6. package/dist/backends/AsyncStore.js +7 -2
  7. package/dist/backends/Locked.d.ts +8 -8
  8. package/dist/backends/Locked.js +2 -1
  9. package/dist/backends/Overlay.d.ts +13 -1
  10. package/dist/backends/Overlay.js +16 -16
  11. package/dist/backends/SyncStore.d.ts +8 -5
  12. package/dist/backends/SyncStore.js +9 -5
  13. package/dist/backends/backend.js +8 -8
  14. package/dist/browser.min.js +4 -5
  15. package/dist/browser.min.js.map +4 -4
  16. package/dist/config.d.ts +23 -0
  17. package/dist/config.js +36 -0
  18. package/dist/cred.d.ts +1 -1
  19. package/dist/cred.js +1 -1
  20. package/dist/emulation/callbacks.d.ts +1 -1
  21. package/dist/emulation/callbacks.js +1 -1
  22. package/dist/emulation/constants.d.ts +48 -42
  23. package/dist/emulation/constants.js +68 -59
  24. package/dist/emulation/dir.d.ts +2 -2
  25. package/dist/emulation/promises.d.ts +1 -1
  26. package/dist/emulation/promises.js +6 -6
  27. package/dist/emulation/sync.d.ts +1 -1
  28. package/dist/emulation/sync.js +7 -7
  29. package/dist/file.d.ts +26 -12
  30. package/dist/file.js +68 -29
  31. package/dist/filesystem.d.ts +3 -3
  32. package/dist/index.d.ts +7 -29
  33. package/dist/index.js +7 -44
  34. package/dist/inode.d.ts +21 -15
  35. package/dist/inode.js +52 -40
  36. package/dist/mutex.d.ts +1 -2
  37. package/dist/mutex.js +1 -1
  38. package/dist/stats.d.ts +70 -18
  39. package/dist/stats.js +12 -18
  40. package/dist/utils.d.ts +3 -8
  41. package/dist/utils.js +60 -39
  42. package/license.md +3 -80
  43. package/package.json +71 -63
  44. package/readme.md +19 -11
  45. package/scripts/make-index.js +100 -0
  46. package/dist/backends/index.d.ts +0 -10
  47. package/dist/backends/index.js +0 -12
package/readme.md CHANGED
@@ -1,6 +1,10 @@
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 [Node JS file system 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 nicely with other tools.
6
+
7
+ ZenFS is a fork of [BrowserFS](https://github.com/jvilk/BrowserFS).
4
8
 
5
9
  ## Backends
6
10
 
@@ -33,13 +37,13 @@ npm install @zenfs/core
33
37
  ```js
34
38
  import fs from '@zenfs/core';
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);
40
44
  ```
41
45
 
42
- #### Using different and/or different backends
46
+ #### Using different and/or multiple backends
43
47
 
44
48
  A single `InMemory` backend is created by default, mounted on `/`.
45
49
 
@@ -50,7 +54,7 @@ You can use multiple backends by passing an object to `configure` which maps pat
50
54
  The following example mounts a zip file to `/zip`, in-memory storage to `/tmp`, and IndexedDB to `/home`. Note that `/` has the default in-memory backend.
51
55
 
52
56
  ```js
53
- import { configure } from '@zenfs/core';
57
+ import { configure, InMemory } from '@zenfs/core';
54
58
  import { IndexedDB } from '@zenfs/dom';
55
59
  import { Zip } from '@zenfs/zip';
56
60
 
@@ -58,13 +62,17 @@ const zipData = await (await fetch('mydata.zip')).arrayBuffer();
58
62
 
59
63
  await configure({
60
64
  '/mnt/zip': { backend: Zip, zipData },
61
- '/tmp': 'InMemory',
65
+ '/tmp': InMemory,
62
66
  '/home': IndexedDB,
63
67
  };
64
68
  ```
65
69
 
66
70
  > [!TIP]
67
- > When configuring a mount point, you can pass in 1. A string that maps to a built-in backend 2. A `Backend` object, if the backend has no required options 3. An object that has a `backend` property which is a `Backend` or a string that maps to a built-in backend and the options accepted by the backend
71
+ > When configuring a mount point, you can pass in
72
+ >
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_)
68
76
 
69
77
  Here is an example that mounts the `Storage` backend from `@zenfs/dom` on `/`:
70
78
 
@@ -109,13 +117,13 @@ if (!exists) {
109
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`:
110
118
 
111
119
  ```js
112
- import { configure, fs } from '@zenfs/core';
120
+ import { configure, fs, AsyncMirror, InMemory } from '@zenfs/core';
113
121
  import { IndexedDB } from '@zenfs/dom';
114
122
 
115
123
  await configure({
116
124
  '/': {
117
- backend: 'AsyncMirror',
118
- sync: 'InMemory',
125
+ backend: AsyncMirror,
126
+ sync: InMemory,
119
127
  async: IndexedDB,
120
128
  },
121
129
  });
@@ -130,12 +138,12 @@ If you would like to create backends without configure (e.g. to do something dyn
130
138
  You can then mount and unmount the backend instance by using `mount` and `umount`.
131
139
 
132
140
  ```js
133
- import { configure, createBackend } from '@zenfs/core';
141
+ import { configure, createBackend, InMemory } from '@zenfs/core';
134
142
  import { IndexedDB } from '@zenfs/dom';
135
143
  import { Zip } from '@zenfs/zip';
136
144
 
137
145
  await configure({
138
- '/tmp': 'InMemory',
146
+ '/tmp': InMemory,
139
147
  '/home': IndexedDB,
140
148
  };
141
149
 
@@ -0,0 +1,100 @@
1
+ #!/usr/bin/env node
2
+ import { parseArgs } from 'util';
3
+ import { statSync, readdirSync, writeFileSync } from 'fs';
4
+ import { join } from 'path/posix';
5
+ import { resolve } from 'path';
6
+ import { minimatch } from 'minimatch';
7
+
8
+ const {
9
+ values: options,
10
+ positionals: [root],
11
+ } = parseArgs({
12
+ options: {
13
+ help: { short: 'h', type: 'boolean', default: false },
14
+ ignore: { short: 'i', type: 'string', multiple: true, default: [] },
15
+ output: { short: 'o', type: 'string', default: 'index.json' },
16
+ quiet: { short: 'q', type: 'boolean', default: false },
17
+ verbose: { type: 'boolean', default: false },
18
+ },
19
+ allowPositionals: true,
20
+ strict: true,
21
+ });
22
+
23
+ if (options.help) {
24
+ console.log(`make-index <path> [...options]
25
+ path: The path to create a listing for
26
+
27
+ options:
28
+ --help, -h Outputs this help message
29
+ --quiet, -q Do not output messages about individual files
30
+ --verbose Output verbose messages
31
+
32
+ --output, -o <path> Path to the output file. Defaults to listing.
33
+ --ignore, -i <pattern> Ignores files which match the glob <pattern>. Can be passed multiple times.
34
+ `);
35
+ process.exit();
36
+ }
37
+
38
+ if (options.quiet && options.verbose) {
39
+ console.log('Can not use both --verbose and --quiet.');
40
+ process.exit();
41
+ }
42
+
43
+ function pathToPosix(path) {
44
+ return path.replaceAll('\\', '/');
45
+ }
46
+
47
+ const colors = {
48
+ reset: 0,
49
+ black: 30,
50
+ red: 31,
51
+ green: 32,
52
+ yellow: 33,
53
+ blue: 34,
54
+ magenta: 35,
55
+ cyan: 36,
56
+ white: 37,
57
+ bright_black: 90,
58
+ bright_red: 91,
59
+ bright_green: 92,
60
+ bright_yellow: 93,
61
+ bright_blue: 94,
62
+ bright_magenta: 95,
63
+ bright_cyan: 96,
64
+ bright_white: 97,
65
+ };
66
+
67
+ function color(color, text) {
68
+ return `\x1b[${colors[color]}m${text}\x1b[0m`;
69
+ }
70
+
71
+ function makeListing(path, seen = new Set()) {
72
+ try {
73
+ const stats = statSync(path);
74
+
75
+ if (stats.isFile()) {
76
+ return null;
77
+ }
78
+
79
+ let entries = {};
80
+ for (const file of readdirSync(path)) {
81
+ const full = join(path, file);
82
+ if (options.ignore.some(pattern => minimatch(full, pattern))) {
83
+ if (!options.quiet) console.log(`${color('yellow', 'skip')} ${full}`);
84
+ continue;
85
+ }
86
+
87
+ entries[file] = makeListing(full, seen);
88
+ }
89
+ return entries;
90
+ } catch (e) {
91
+ if (!options.quiet) {
92
+ console.log(`${color('red', 'fail')} ${path}: ${e.message}`);
93
+ }
94
+ }
95
+ }
96
+
97
+ const listing = makeListing(pathToPosix(root));
98
+ if (!options.quiet) console.log('Generated listing for ' + pathToPosix(resolve(root)));
99
+
100
+ writeFileSync(options.output, JSON.stringify(listing));
@@ -1,10 +0,0 @@
1
- import { AsyncMirror } from './AsyncMirror.js';
2
- import { InMemory } from './InMemory.js';
3
- import { Overlay } from './Overlay.js';
4
- import { Backend } from './backend.js';
5
- export declare const backends: {
6
- [backend: string]: Backend;
7
- };
8
- export default backends;
9
- export { AsyncMirror, InMemory, Overlay };
10
- export declare function registerBackend(..._backends: Backend[]): void;
@@ -1,12 +0,0 @@
1
- import { AsyncMirror } from './AsyncMirror.js';
2
- import { InMemory } from './InMemory.js';
3
- import { Overlay } from './Overlay.js';
4
- export const backends = {};
5
- export default backends;
6
- export { AsyncMirror, InMemory, Overlay };
7
- export function registerBackend(..._backends) {
8
- for (const backend of _backends) {
9
- backends[backend.name] = backend;
10
- }
11
- }
12
- registerBackend(AsyncMirror, InMemory, Overlay);