@zenfs/core 1.3.6 → 1.4.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 (88) hide show
  1. package/dist/backends/memory.d.ts +4 -4
  2. package/dist/backends/memory.js +4 -4
  3. package/dist/backends/overlay.d.ts +5 -2
  4. package/dist/backends/overlay.js +7 -10
  5. package/dist/backends/port/fs.js +1 -4
  6. package/dist/config.js +4 -8
  7. package/dist/context.d.ts +32 -0
  8. package/dist/context.js +23 -0
  9. package/dist/credentials.d.ts +5 -5
  10. package/dist/credentials.js +10 -6
  11. package/dist/emulation/async.d.ts +90 -89
  12. package/dist/emulation/async.js +76 -75
  13. package/dist/emulation/dir.d.ts +3 -1
  14. package/dist/emulation/dir.js +6 -7
  15. package/dist/emulation/index.d.ts +1 -1
  16. package/dist/emulation/index.js +1 -1
  17. package/dist/emulation/promises.d.ts +50 -48
  18. package/dist/emulation/promises.js +78 -77
  19. package/dist/emulation/shared.d.ts +35 -8
  20. package/dist/emulation/shared.js +37 -11
  21. package/dist/emulation/sync.d.ts +63 -62
  22. package/dist/emulation/sync.js +72 -73
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/stats.d.ts +2 -1
  26. package/dist/stats.js +5 -4
  27. package/package.json +3 -5
  28. package/scripts/test.js +78 -17
  29. package/tests/assignment.ts +1 -1
  30. package/tests/common/context.test.ts +19 -0
  31. package/tests/{devices.test.ts → common/devices.test.ts} +3 -3
  32. package/tests/{handle.test.ts → common/handle.test.ts} +1 -1
  33. package/tests/common/mounts.test.ts +36 -0
  34. package/tests/{mutex.test.ts → common/mutex.test.ts} +3 -3
  35. package/tests/common/path.test.ts +34 -0
  36. package/tests/common.ts +4 -3
  37. package/tests/fs/dir.test.ts +11 -11
  38. package/tests/fs/directory.test.ts +17 -17
  39. package/tests/fs/errors.test.ts +29 -39
  40. package/tests/fs/watch.test.ts +2 -2
  41. package/tests/setup/context.ts +9 -0
  42. package/tests/setup/cow+fetch.ts +1 -1
  43. package/tests/setup/memory.ts +1 -1
  44. package/tests/{setup/common.ts → setup.ts} +6 -5
  45. package/src/backends/backend.ts +0 -161
  46. package/src/backends/fetch.ts +0 -180
  47. package/src/backends/file_index.ts +0 -206
  48. package/src/backends/memory.ts +0 -45
  49. package/src/backends/overlay.ts +0 -560
  50. package/src/backends/port/fs.ts +0 -329
  51. package/src/backends/port/readme.md +0 -54
  52. package/src/backends/port/rpc.ts +0 -167
  53. package/src/backends/readme.md +0 -3
  54. package/src/backends/store/fs.ts +0 -667
  55. package/src/backends/store/readme.md +0 -9
  56. package/src/backends/store/simple.ts +0 -154
  57. package/src/backends/store/store.ts +0 -189
  58. package/src/config.ts +0 -227
  59. package/src/credentials.ts +0 -49
  60. package/src/devices.ts +0 -521
  61. package/src/emulation/async.ts +0 -834
  62. package/src/emulation/cache.ts +0 -86
  63. package/src/emulation/config.ts +0 -21
  64. package/src/emulation/constants.ts +0 -182
  65. package/src/emulation/dir.ts +0 -138
  66. package/src/emulation/index.ts +0 -8
  67. package/src/emulation/path.ts +0 -440
  68. package/src/emulation/promises.ts +0 -1140
  69. package/src/emulation/shared.ts +0 -172
  70. package/src/emulation/streams.ts +0 -34
  71. package/src/emulation/sync.ts +0 -863
  72. package/src/emulation/watchers.ts +0 -194
  73. package/src/error.ts +0 -307
  74. package/src/file.ts +0 -631
  75. package/src/filesystem.ts +0 -174
  76. package/src/index.ts +0 -35
  77. package/src/inode.ts +0 -128
  78. package/src/mixins/async.ts +0 -230
  79. package/src/mixins/index.ts +0 -5
  80. package/src/mixins/mutexed.ts +0 -257
  81. package/src/mixins/readonly.ts +0 -96
  82. package/src/mixins/shared.ts +0 -25
  83. package/src/mixins/sync.ts +0 -58
  84. package/src/polyfills.ts +0 -21
  85. package/src/stats.ts +0 -405
  86. package/src/utils.ts +0 -276
  87. package/tests/mounts.test.ts +0 -18
  88. package/tests/path.test.ts +0 -34
@@ -1,172 +0,0 @@
1
- // Utilities and shared data
2
-
3
- import type { BigIntStatsFs, StatsFs } from 'node:fs';
4
- import { InMemory } from '../backends/memory.js';
5
- import { Errno, ErrnoError } from '../error.js';
6
- import type { File } from '../file.js';
7
- import type { FileSystem } from '../filesystem.js';
8
- import { normalizePath } from '../utils.js';
9
- import { resolve, type AbsolutePath } from './path.js';
10
- import { size_max } from './constants.js';
11
- import { paths as pathCache } from './cache.js';
12
-
13
- // descriptors
14
- export const fdMap: Map<number, File> = new Map();
15
- let nextFd = 100;
16
- export function file2fd(file: File): number {
17
- const fd = nextFd++;
18
- fdMap.set(fd, file);
19
- return fd;
20
- }
21
- export function fd2file(fd: number): File {
22
- if (!fdMap.has(fd)) {
23
- throw new ErrnoError(Errno.EBADF);
24
- }
25
- return fdMap.get(fd)!;
26
- }
27
-
28
- export type MountObject = Record<AbsolutePath, FileSystem>;
29
-
30
- /**
31
- * The map of mount points
32
- * @internal
33
- */
34
- export const mounts: Map<string, FileSystem> = new Map();
35
-
36
- // Set a default root.
37
- mount('/', InMemory.create({ name: 'root' }));
38
-
39
- /**
40
- * Mounts the file system at `mountPoint`.
41
- */
42
- export function mount(mountPoint: string, fs: FileSystem): void {
43
- if (mountPoint[0] !== '/') {
44
- mountPoint = '/' + mountPoint;
45
- }
46
- mountPoint = resolve(mountPoint);
47
- if (mounts.has(mountPoint)) {
48
- throw new ErrnoError(Errno.EINVAL, 'Mount point ' + mountPoint + ' is already in use.');
49
- }
50
- mounts.set(mountPoint, fs);
51
- pathCache.clear();
52
- }
53
-
54
- /**
55
- * Unmounts the file system at `mountPoint`.
56
- */
57
- export function umount(mountPoint: string): void {
58
- if (mountPoint[0] !== '/') {
59
- mountPoint = '/' + mountPoint;
60
- }
61
- mountPoint = resolve(mountPoint);
62
- if (!mounts.has(mountPoint)) {
63
- throw new ErrnoError(Errno.EINVAL, 'Mount point ' + mountPoint + ' is already unmounted.');
64
- }
65
- mounts.delete(mountPoint);
66
- pathCache.clear();
67
- }
68
-
69
- /**
70
- * Gets the internal `FileSystem` for the path, then returns it along with the path relative to the FS' root
71
- */
72
- export function resolveMount(path: string): { fs: FileSystem; path: string; mountPoint: string } {
73
- path = normalizePath(path);
74
- const sortedMounts = [...mounts].sort((a, b) => (a[0].length > b[0].length ? -1 : 1)); // descending order of the string length
75
- for (const [mountPoint, fs] of sortedMounts) {
76
- // We know path is normalized, so it would be a substring of the mount point.
77
- if (mountPoint.length <= path.length && path.startsWith(mountPoint)) {
78
- path = path.slice(mountPoint.length > 1 ? mountPoint.length : 0); // Resolve the path relative to the mount point
79
- if (path === '') {
80
- path = '/';
81
- }
82
- return { fs, path, mountPoint };
83
- }
84
- }
85
-
86
- throw new ErrnoError(Errno.EIO, 'ZenFS not initialized with a file system');
87
- }
88
-
89
- /**
90
- * Wait for all file systems to be ready and synced.
91
- * May be removed at some point.
92
- * @experimental @internal
93
- */
94
- export async function _synced(): Promise<void> {
95
- await Promise.all([...mounts.values()].map(m => m.ready()));
96
- return;
97
- }
98
-
99
- /**
100
- * Reverse maps the paths in text from the mounted FileSystem to the global path
101
- * @hidden
102
- */
103
- export function fixPaths(text: string, paths: Record<string, string>): string {
104
- for (const [from, to] of Object.entries(paths)) {
105
- text = text?.replaceAll(from, to);
106
- }
107
- return text;
108
- }
109
-
110
- /**
111
- * Fix paths in error stacks
112
- * @hidden
113
- */
114
- export function fixError<E extends ErrnoError>(e: E, paths: Record<string, string>): E {
115
- if (typeof e.stack == 'string') {
116
- e.stack = fixPaths(e.stack, paths);
117
- }
118
- try {
119
- e.message = fixPaths(e.message, paths);
120
- } catch {
121
- // `message` is read only
122
- }
123
- return e;
124
- }
125
-
126
- /**
127
- * @deprecated
128
- */
129
- export function mountObject(mounts: MountObject): void {
130
- if ('/' in mounts) {
131
- umount('/');
132
- }
133
- for (const [point, fs] of Object.entries(mounts)) {
134
- mount(point, fs);
135
- }
136
- }
137
-
138
- /**
139
- * @hidden
140
- */
141
- export function _statfs<const T extends boolean>(fs: FileSystem, bigint?: T): T extends true ? BigIntStatsFs : StatsFs {
142
- const md = fs.metadata();
143
- const bs = md.blockSize || 4096;
144
-
145
- return {
146
- type: (bigint ? BigInt : Number)(md.type),
147
- bsize: (bigint ? BigInt : Number)(bs),
148
- ffree: (bigint ? BigInt : Number)(md.freeNodes || size_max),
149
- files: (bigint ? BigInt : Number)(md.totalNodes || size_max),
150
- bavail: (bigint ? BigInt : Number)(md.freeSpace / bs),
151
- bfree: (bigint ? BigInt : Number)(md.freeSpace / bs),
152
- blocks: (bigint ? BigInt : Number)(md.totalSpace / bs),
153
- } as T extends true ? BigIntStatsFs : StatsFs;
154
- }
155
-
156
- /**
157
- * Options used for caching, among other things.
158
- * @internal *UNSTABLE*
159
- */
160
- export interface InternalOptions {
161
- /**
162
- * If true, then this readdir was called from another function.
163
- * In this case, don't clear the cache when done.
164
- * @internal *UNSTABLE*
165
- */
166
- _isIndirect?: boolean;
167
- }
168
-
169
- export interface ReaddirOptions extends InternalOptions {
170
- withFileTypes?: boolean;
171
- recursive?: boolean;
172
- }
@@ -1,34 +0,0 @@
1
- import type * as Node from 'node:fs';
2
- import { Readable, Writable } from 'readable-stream';
3
- import type { Callback } from '../utils.js';
4
- import { ErrnoError, Errno } from '../error.js';
5
-
6
- export class ReadStream extends Readable implements Node.ReadStream {
7
- close(callback: Callback = () => null): void {
8
- try {
9
- super.destroy();
10
- super.emit('close');
11
- callback();
12
- } catch (err) {
13
- callback(new ErrnoError(Errno.EIO, (err as Error).toString()));
14
- }
15
- }
16
- declare bytesRead: number;
17
- declare path: string | Buffer;
18
- declare pending: boolean;
19
- }
20
-
21
- export class WriteStream extends Writable implements Node.WriteStream {
22
- close(callback: Callback = () => null): void {
23
- try {
24
- super.destroy();
25
- super.emit('close');
26
- callback();
27
- } catch (err) {
28
- callback(new ErrnoError(Errno.EIO, (err as Error).toString()));
29
- }
30
- }
31
- declare bytesWritten: number;
32
- declare path: string | Buffer;
33
- declare pending: boolean;
34
- }