@zenfs/core 0.9.6 → 0.10.0

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 (82) hide show
  1. package/dist/backends/AsyncStore.d.ts +3 -2
  2. package/dist/backends/AsyncStore.js +40 -33
  3. package/dist/backends/Fetch.d.ts +84 -0
  4. package/dist/backends/Fetch.js +171 -0
  5. package/dist/backends/InMemory.d.ts +1 -1
  6. package/dist/backends/Index.d.ts +7 -10
  7. package/dist/backends/Index.js +26 -24
  8. package/dist/backends/Locked.d.ts +11 -11
  9. package/dist/backends/Locked.js +50 -49
  10. package/dist/backends/Overlay.js +22 -22
  11. package/dist/backends/SyncStore.d.ts +6 -6
  12. package/dist/backends/SyncStore.js +30 -30
  13. package/dist/backends/backend.d.ts +5 -4
  14. package/dist/backends/backend.js +6 -6
  15. package/dist/backends/port/fs.d.ts +124 -0
  16. package/dist/backends/port/fs.js +241 -0
  17. package/dist/backends/port/rpc.d.ts +60 -0
  18. package/dist/backends/port/rpc.js +71 -0
  19. package/dist/backends/port/store.d.ts +30 -0
  20. package/dist/backends/port/store.js +142 -0
  21. package/dist/browser.min.js +4 -4
  22. package/dist/browser.min.js.map +4 -4
  23. package/dist/config.d.ts +9 -11
  24. package/dist/config.js +13 -13
  25. package/dist/emulation/async.d.ts +76 -77
  26. package/dist/emulation/async.js +45 -45
  27. package/dist/emulation/dir.js +8 -7
  28. package/dist/emulation/index.d.ts +1 -1
  29. package/dist/emulation/index.js +1 -1
  30. package/dist/emulation/path.d.ts +3 -2
  31. package/dist/emulation/path.js +19 -45
  32. package/dist/emulation/promises.d.ts +112 -113
  33. package/dist/emulation/promises.js +167 -173
  34. package/dist/emulation/shared.d.ts +6 -17
  35. package/dist/emulation/shared.js +9 -9
  36. package/dist/emulation/streams.js +3 -2
  37. package/dist/emulation/sync.d.ts +71 -64
  38. package/dist/emulation/sync.js +62 -63
  39. package/dist/{ApiError.d.ts → error.d.ts} +15 -15
  40. package/dist/error.js +292 -0
  41. package/dist/file.d.ts +6 -4
  42. package/dist/file.js +17 -9
  43. package/dist/filesystem.d.ts +1 -1
  44. package/dist/filesystem.js +18 -15
  45. package/dist/index.d.ts +4 -1
  46. package/dist/index.js +4 -1
  47. package/dist/mutex.js +4 -3
  48. package/dist/stats.d.ts +7 -7
  49. package/dist/stats.js +50 -10
  50. package/dist/utils.d.ts +10 -9
  51. package/dist/utils.js +15 -15
  52. package/package.json +5 -5
  53. package/readme.md +19 -11
  54. package/src/backends/AsyncStore.ts +42 -36
  55. package/src/backends/Fetch.ts +230 -0
  56. package/src/backends/Index.ts +33 -29
  57. package/src/backends/Locked.ts +50 -49
  58. package/src/backends/Overlay.ts +24 -24
  59. package/src/backends/SyncStore.ts +34 -34
  60. package/src/backends/backend.ts +13 -11
  61. package/src/backends/port/fs.ts +308 -0
  62. package/src/backends/port/readme.md +59 -0
  63. package/src/backends/port/rpc.ts +144 -0
  64. package/src/backends/port/store.ts +187 -0
  65. package/src/config.ts +25 -29
  66. package/src/emulation/async.ts +191 -199
  67. package/src/emulation/dir.ts +8 -8
  68. package/src/emulation/index.ts +1 -1
  69. package/src/emulation/path.ts +25 -49
  70. package/src/emulation/promises.ts +286 -287
  71. package/src/emulation/shared.ts +14 -23
  72. package/src/emulation/streams.ts +9 -8
  73. package/src/emulation/sync.ts +182 -182
  74. package/src/{ApiError.ts → error.ts} +91 -89
  75. package/src/file.ts +23 -13
  76. package/src/filesystem.ts +26 -22
  77. package/src/index.ts +4 -1
  78. package/src/mutex.ts +6 -4
  79. package/src/stats.ts +32 -23
  80. package/src/utils.ts +23 -24
  81. package/tsconfig.json +4 -3
  82. package/dist/ApiError.js +0 -292
package/src/config.ts CHANGED
@@ -1,14 +1,15 @@
1
- import { ApiError, ErrorCode } from './ApiError.js';
1
+ import { ErrnoError, Errno } from './error.js';
2
2
  import type { Backend, BackendConfiguration } from './backends/backend.js';
3
3
  import { checkOptions, isBackend, isBackendConfig } from './backends/backend.js';
4
4
  import * as fs from './emulation/index.js';
5
- import { setCred, type MountMapping } from './emulation/shared.js';
5
+ import { setCred, type MountObject } from './emulation/shared.js';
6
6
  import { FileSystem } from './filesystem.js';
7
+ import type { AbsolutePath } from './emulation/path.js';
7
8
 
8
9
  /**
9
10
  * Configuration for a specific mount point
10
11
  */
11
- export type MountConfiguration<FS extends FileSystem = FileSystem, TOptions extends object = object> = FS | BackendConfiguration<FS, TOptions> | Backend<FS, TOptions>;
12
+ export type MountConfiguration<FS extends FileSystem = FileSystem, TOptions extends object = object> = FS | BackendConfiguration<Backend<FS, TOptions>> | Backend<FS, TOptions>;
12
13
 
13
14
  function isMountConfig(arg: unknown): arg is MountConfiguration {
14
15
  return isBackendConfig(arg) || isBackend(arg) || arg instanceof FileSystem;
@@ -20,11 +21,11 @@ function isMountConfig(arg: unknown): arg is MountConfiguration {
20
21
  */
21
22
  export async function resolveMountConfig<FS extends FileSystem, TOptions extends object = object>(config: MountConfiguration<FS, TOptions>, _depth = 0): Promise<FS> {
22
23
  if (typeof config !== 'object' || config == null) {
23
- throw new ApiError(ErrorCode.EINVAL, 'Invalid options on mount configuration');
24
+ throw new ErrnoError(Errno.EINVAL, 'Invalid options on mount configuration');
24
25
  }
25
26
 
26
27
  if (!isMountConfig(config)) {
27
- throw new ApiError(ErrorCode.EINVAL, 'Invalid mount configuration');
28
+ throw new ErrnoError(Errno.EINVAL, 'Invalid mount configuration');
28
29
  }
29
30
 
30
31
  if (config instanceof FileSystem) {
@@ -32,7 +33,7 @@ export async function resolveMountConfig<FS extends FileSystem, TOptions extends
32
33
  }
33
34
 
34
35
  if (isBackend(config)) {
35
- config = <BackendConfiguration<FS, TOptions>>{ backend: config };
36
+ config = <BackendConfiguration<Backend<FS, TOptions>>>{ backend: config };
36
37
  }
37
38
 
38
39
  for (const [key, value] of Object.entries(config)) {
@@ -45,16 +46,16 @@ export async function resolveMountConfig<FS extends FileSystem, TOptions extends
45
46
  }
46
47
 
47
48
  if (_depth > 10) {
48
- throw new ApiError(ErrorCode.EINVAL, 'Invalid configuration, too deep and possibly infinite');
49
+ throw new ErrnoError(Errno.EINVAL, 'Invalid configuration, too deep and possibly infinite');
49
50
  }
50
51
 
51
- config[key] = await resolveMountConfig(value, ++_depth);
52
+ (<Record<string, FileSystem>>config)[key] = await resolveMountConfig(value, ++_depth);
52
53
  }
53
54
 
54
55
  const { backend } = config;
55
56
 
56
57
  if (!(await backend.isAvailable())) {
57
- throw new ApiError(ErrorCode.EPERM, 'Backend not available: ' + backend);
58
+ throw new ErrnoError(Errno.EPERM, 'Backend not available: ' + backend);
58
59
  }
59
60
  checkOptions(backend, config);
60
61
  const mount = backend.create(config);
@@ -63,39 +64,34 @@ export async function resolveMountConfig<FS extends FileSystem, TOptions extends
63
64
  }
64
65
 
65
66
  /**
66
- *A mapping of mount points to their configurations
67
+ * Configuration
67
68
  */
68
- export type MappingConfiguration = Partial<{
69
- uid: number;
70
- gid: number;
71
- }> &
72
- Record<string, FileSystem | BackendConfiguration | Backend>;
73
-
74
- /**
75
- * Configuration for the file systems
76
- */
77
- export type Configuration = MountConfiguration | MappingConfiguration;
69
+ export interface Configuration {
70
+ mounts: Record<AbsolutePath, MountConfiguration>;
71
+ uid?: number;
72
+ gid?: number;
73
+ }
78
74
 
79
75
  /**
80
76
  * Creates filesystems with the given configuration, and initializes ZenFS with it.
81
77
  * @see Configuration for more info on the configuration object.
82
78
  */
83
- export async function configure(config: Configuration): Promise<void> {
84
- const uid = 'uid' in config ? +config.uid || 0 : 0;
85
- const gid = 'gid' in config ? +config.gid || 0 : 0;
79
+ export async function configure(config: MountConfiguration | Configuration): Promise<void> {
80
+ const uid = 'uid' in config ? config.uid || 0 : 0;
81
+ const gid = 'gid' in config ? config.gid || 0 : 0;
86
82
 
87
83
  if (isMountConfig(config)) {
88
84
  // single FS
89
- config = { '/': config };
85
+ config = { mounts: { '/': config } };
90
86
  }
91
87
 
92
- for (const [point, value] of Object.entries(config)) {
93
- if (point == 'uid' || point == 'gid' || typeof value == 'number') {
94
- continue;
88
+ for (const [point, value] of Object.entries(config.mounts) as [AbsolutePath, MountConfiguration][]) {
89
+ if (!point.startsWith('/')) {
90
+ throw new ErrnoError(Errno.EINVAL, 'Mount points must have absolute paths');
95
91
  }
96
- config[point] = await resolveMountConfig(value);
92
+ config.mounts[point] = await resolveMountConfig(value);
97
93
  }
98
94
 
99
- fs.mountMapping(<MountMapping>config);
95
+ fs.mountObject(config.mounts as MountObject);
100
96
  setCred({ uid, gid, suid: uid, sgid: gid, euid: uid, egid: gid });
101
97
  }