@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.
- package/dist/backends/memory.d.ts +4 -4
- package/dist/backends/memory.js +4 -4
- package/dist/backends/overlay.d.ts +5 -2
- package/dist/backends/overlay.js +7 -10
- package/dist/backends/port/fs.js +1 -4
- package/dist/config.js +4 -8
- package/dist/context.d.ts +32 -0
- package/dist/context.js +23 -0
- package/dist/credentials.d.ts +5 -5
- package/dist/credentials.js +10 -6
- package/dist/emulation/async.d.ts +90 -89
- package/dist/emulation/async.js +76 -75
- package/dist/emulation/dir.d.ts +3 -1
- package/dist/emulation/dir.js +6 -7
- package/dist/emulation/index.d.ts +1 -1
- package/dist/emulation/index.js +1 -1
- package/dist/emulation/promises.d.ts +50 -48
- package/dist/emulation/promises.js +78 -77
- package/dist/emulation/shared.d.ts +35 -8
- package/dist/emulation/shared.js +37 -11
- package/dist/emulation/sync.d.ts +63 -62
- package/dist/emulation/sync.js +72 -73
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/stats.d.ts +2 -1
- package/dist/stats.js +5 -4
- package/package.json +3 -5
- package/scripts/test.js +78 -17
- package/tests/assignment.ts +1 -1
- package/tests/common/context.test.ts +19 -0
- package/tests/{devices.test.ts → common/devices.test.ts} +3 -3
- package/tests/{handle.test.ts → common/handle.test.ts} +1 -1
- package/tests/common/mounts.test.ts +36 -0
- package/tests/{mutex.test.ts → common/mutex.test.ts} +3 -3
- package/tests/common/path.test.ts +34 -0
- package/tests/common.ts +4 -3
- package/tests/fs/dir.test.ts +11 -11
- package/tests/fs/directory.test.ts +17 -17
- package/tests/fs/errors.test.ts +29 -39
- package/tests/fs/watch.test.ts +2 -2
- package/tests/setup/context.ts +9 -0
- package/tests/setup/cow+fetch.ts +1 -1
- package/tests/setup/memory.ts +1 -1
- package/tests/{setup/common.ts → setup.ts} +6 -5
- package/src/backends/backend.ts +0 -161
- package/src/backends/fetch.ts +0 -180
- package/src/backends/file_index.ts +0 -206
- package/src/backends/memory.ts +0 -45
- package/src/backends/overlay.ts +0 -560
- package/src/backends/port/fs.ts +0 -329
- package/src/backends/port/readme.md +0 -54
- package/src/backends/port/rpc.ts +0 -167
- package/src/backends/readme.md +0 -3
- package/src/backends/store/fs.ts +0 -667
- package/src/backends/store/readme.md +0 -9
- package/src/backends/store/simple.ts +0 -154
- package/src/backends/store/store.ts +0 -189
- package/src/config.ts +0 -227
- package/src/credentials.ts +0 -49
- package/src/devices.ts +0 -521
- package/src/emulation/async.ts +0 -834
- package/src/emulation/cache.ts +0 -86
- package/src/emulation/config.ts +0 -21
- package/src/emulation/constants.ts +0 -182
- package/src/emulation/dir.ts +0 -138
- package/src/emulation/index.ts +0 -8
- package/src/emulation/path.ts +0 -440
- package/src/emulation/promises.ts +0 -1140
- package/src/emulation/shared.ts +0 -172
- package/src/emulation/streams.ts +0 -34
- package/src/emulation/sync.ts +0 -863
- package/src/emulation/watchers.ts +0 -194
- package/src/error.ts +0 -307
- package/src/file.ts +0 -631
- package/src/filesystem.ts +0 -174
- package/src/index.ts +0 -35
- package/src/inode.ts +0 -128
- package/src/mixins/async.ts +0 -230
- package/src/mixins/index.ts +0 -5
- package/src/mixins/mutexed.ts +0 -257
- package/src/mixins/readonly.ts +0 -96
- package/src/mixins/shared.ts +0 -25
- package/src/mixins/sync.ts +0 -58
- package/src/polyfills.ts +0 -21
- package/src/stats.ts +0 -405
- package/src/utils.ts +0 -276
- package/tests/mounts.test.ts +0 -18
- package/tests/path.test.ts +0 -34
package/src/emulation/shared.ts
DELETED
|
@@ -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
|
-
}
|
package/src/emulation/streams.ts
DELETED
|
@@ -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
|
-
}
|