@zenfs/core 0.9.2 → 0.9.3
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/package.json +2 -9
- package/src/ApiError.ts +310 -0
- package/src/backends/AsyncStore.ts +635 -0
- package/src/backends/InMemory.ts +56 -0
- package/src/backends/Index.ts +500 -0
- package/src/backends/Locked.ts +181 -0
- package/src/backends/Overlay.ts +591 -0
- package/src/backends/SyncStore.ts +589 -0
- package/src/backends/backend.ts +152 -0
- package/src/config.ts +101 -0
- package/src/cred.ts +21 -0
- package/src/emulation/async.ts +910 -0
- package/src/emulation/constants.ts +176 -0
- package/src/emulation/dir.ts +139 -0
- package/src/emulation/index.ts +8 -0
- package/src/emulation/path.ts +468 -0
- package/src/emulation/promises.ts +1071 -0
- package/src/emulation/shared.ts +128 -0
- package/src/emulation/streams.ts +33 -0
- package/src/emulation/sync.ts +898 -0
- package/src/file.ts +721 -0
- package/src/filesystem.ts +546 -0
- package/src/index.ts +21 -0
- package/src/inode.ts +229 -0
- package/src/mutex.ts +52 -0
- package/src/stats.ts +385 -0
- package/src/utils.ts +287 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/*
|
|
2
|
+
FS Constants
|
|
3
|
+
See https://nodejs.org/api/fs.html#file-access-constants
|
|
4
|
+
|
|
5
|
+
Note: Many of these are pulled from
|
|
6
|
+
https://github.com/torvalds/linux/blob/master/include/uapi/linux/stat.h
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// File Access Constants
|
|
10
|
+
|
|
11
|
+
/** File is visible to the calling process. */
|
|
12
|
+
export const F_OK = 0;
|
|
13
|
+
|
|
14
|
+
/** File can be read by the calling process. */
|
|
15
|
+
export const R_OK = 4;
|
|
16
|
+
|
|
17
|
+
/** File can be written by the calling process. */
|
|
18
|
+
export const W_OK = 2;
|
|
19
|
+
|
|
20
|
+
/** File can be executed by the calling process. */
|
|
21
|
+
export const X_OK = 1;
|
|
22
|
+
|
|
23
|
+
// File Copy Constants
|
|
24
|
+
|
|
25
|
+
/** Constant for fs.copyFile. Flag indicating the destination file should not be overwritten if it already exists. */
|
|
26
|
+
export const COPYFILE_EXCL = 1;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Constant for fs.copyFile. Copy operation will attempt to create a copy-on-write reflink.
|
|
30
|
+
* If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.
|
|
31
|
+
*/
|
|
32
|
+
export const COPYFILE_FICLONE = 2;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Constant for fs.copyFile. Copy operation will attempt to create a copy-on-write reflink.
|
|
36
|
+
* If the underlying platform does not support copy-on-write, then the operation will fail with an error.
|
|
37
|
+
*/
|
|
38
|
+
export const COPYFILE_FICLONE_FORCE = 4;
|
|
39
|
+
|
|
40
|
+
// File Open Constants
|
|
41
|
+
|
|
42
|
+
/** Flag indicating to open a file for read-only access. */
|
|
43
|
+
export const O_RDONLY = 0;
|
|
44
|
+
|
|
45
|
+
/** Flag indicating to open a file for write-only access. */
|
|
46
|
+
export const O_WRONLY = 1;
|
|
47
|
+
|
|
48
|
+
/** Flag indicating to open a file for read-write access. */
|
|
49
|
+
export const O_RDWR = 2;
|
|
50
|
+
|
|
51
|
+
/** Flag indicating to create the file if it does not already exist. */
|
|
52
|
+
export const O_CREAT = 0x40; // bit 6
|
|
53
|
+
|
|
54
|
+
/** Flag indicating that opening a file should fail if the O_CREAT flag is set and the file already exists. */
|
|
55
|
+
export const O_EXCL = 0x80; // bit 7
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Flag indicating that if path identifies a terminal device,
|
|
59
|
+
* opening the path shall not cause that terminal to become the controlling terminal for the process
|
|
60
|
+
* (if the process does not already have one).
|
|
61
|
+
*/
|
|
62
|
+
export const O_NOCTTY = 0x100; // bit 8
|
|
63
|
+
|
|
64
|
+
/** Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero. */
|
|
65
|
+
export const O_TRUNC = 0x200; // bit 9
|
|
66
|
+
|
|
67
|
+
/** Flag indicating that data will be appended to the end of the file. */
|
|
68
|
+
export const O_APPEND = 0x400; // bit 10
|
|
69
|
+
|
|
70
|
+
/** Flag indicating that the open should fail if the path is not a directory. */
|
|
71
|
+
export const O_DIRECTORY = 0x10000; // bit 16
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* constant for fs.open().
|
|
75
|
+
* Flag indicating reading accesses to the file system will no longer result in
|
|
76
|
+
* an update to the atime information associated with the file.
|
|
77
|
+
* This flag is available on Linux operating systems only.
|
|
78
|
+
*/
|
|
79
|
+
export const O_NOATIME = 0x40000; // bit 18
|
|
80
|
+
|
|
81
|
+
/** Flag indicating that the open should fail if the path is a symbolic link. */
|
|
82
|
+
export const O_NOFOLLOW = 0x20000; // bit 17
|
|
83
|
+
|
|
84
|
+
/** Flag indicating that the file is opened for synchronous I/O. */
|
|
85
|
+
export const O_SYNC = 0x101000; // bit 20 and bit 12
|
|
86
|
+
|
|
87
|
+
/** Flag indicating that the file is opened for synchronous I/O with write operations waiting for data integrity. */
|
|
88
|
+
export const O_DSYNC = 0x1000; // bit 12
|
|
89
|
+
|
|
90
|
+
/** Flag indicating to open the symbolic link itself rather than the resource it is pointing to. */
|
|
91
|
+
export const O_SYMLINK = 0x8000; // bit 15
|
|
92
|
+
|
|
93
|
+
/** When set, an attempt will be made to minimize caching effects of file I/O. */
|
|
94
|
+
export const O_DIRECT = 0x4000; // bit 14
|
|
95
|
+
|
|
96
|
+
/** Flag indicating to open the file in nonblocking mode when possible. */
|
|
97
|
+
export const O_NONBLOCK = 0x800; // bit 11
|
|
98
|
+
|
|
99
|
+
// File Type Constants
|
|
100
|
+
|
|
101
|
+
/** Bit mask used to extract the file type from mode. */
|
|
102
|
+
export const S_IFMT = 0xf000;
|
|
103
|
+
|
|
104
|
+
/** File type constant for a socket. */
|
|
105
|
+
export const S_IFSOCK = 0xc000;
|
|
106
|
+
|
|
107
|
+
/** File type constant for a symbolic link. */
|
|
108
|
+
export const S_IFLNK = 0xa000;
|
|
109
|
+
|
|
110
|
+
/** File type constant for a regular file. */
|
|
111
|
+
export const S_IFREG = 0x8000;
|
|
112
|
+
|
|
113
|
+
/** File type constant for a block-oriented device file. */
|
|
114
|
+
export const S_IFBLK = 0x6000;
|
|
115
|
+
|
|
116
|
+
/** File type constant for a directory. */
|
|
117
|
+
export const S_IFDIR = 0x4000;
|
|
118
|
+
|
|
119
|
+
/** File type constant for a character-oriented device file. */
|
|
120
|
+
export const S_IFCHR = 0x2000;
|
|
121
|
+
|
|
122
|
+
/** File type constant for a FIFO/pipe. */
|
|
123
|
+
export const S_IFIFO = 0x1000;
|
|
124
|
+
|
|
125
|
+
/** Set user id */
|
|
126
|
+
export const S_ISUID = 0o4000;
|
|
127
|
+
|
|
128
|
+
/** Set group id */
|
|
129
|
+
export const S_ISGID = 0o2000;
|
|
130
|
+
|
|
131
|
+
/** Sticky bit */
|
|
132
|
+
export const S_ISVTX = 0o1000;
|
|
133
|
+
|
|
134
|
+
// File Mode Constants
|
|
135
|
+
|
|
136
|
+
/** File mode indicating readable, writable and executable by owner. */
|
|
137
|
+
export const S_IRWXU = 0o700;
|
|
138
|
+
|
|
139
|
+
/** File mode indicating readable by owner. */
|
|
140
|
+
export const S_IRUSR = 0o400;
|
|
141
|
+
|
|
142
|
+
/** File mode indicating writable by owner. */
|
|
143
|
+
export const S_IWUSR = 0o200;
|
|
144
|
+
|
|
145
|
+
/** File mode indicating executable by owner. */
|
|
146
|
+
export const S_IXUSR = 0o100;
|
|
147
|
+
|
|
148
|
+
/** File mode indicating readable, writable and executable by group. */
|
|
149
|
+
export const S_IRWXG = 0o70;
|
|
150
|
+
|
|
151
|
+
/** File mode indicating readable by group. */
|
|
152
|
+
export const S_IRGRP = 0o40;
|
|
153
|
+
|
|
154
|
+
/** File mode indicating writable by group. */
|
|
155
|
+
export const S_IWGRP = 0o20;
|
|
156
|
+
|
|
157
|
+
/** File mode indicating executable by group. */
|
|
158
|
+
export const S_IXGRP = 0o10;
|
|
159
|
+
|
|
160
|
+
/** File mode indicating readable, writable and executable by others. */
|
|
161
|
+
export const S_IRWXO = 7;
|
|
162
|
+
|
|
163
|
+
/** File mode indicating readable by others. */
|
|
164
|
+
export const S_IROTH = 4;
|
|
165
|
+
|
|
166
|
+
/** File mode indicating writable by others. */
|
|
167
|
+
export const S_IWOTH = 2;
|
|
168
|
+
|
|
169
|
+
/** File mode indicating executable by others. */
|
|
170
|
+
export const S_IXOTH = 1;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* When set, a memory file mapping is used to access the file.
|
|
174
|
+
* This flag is ignored since a unix-like FS is emulated
|
|
175
|
+
*/
|
|
176
|
+
export const UV_FS_O_FILEMAP = 0;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { Dirent as _Dirent, Dir as _Dir } from 'fs';
|
|
2
|
+
import type { Callback } from '../utils.js';
|
|
3
|
+
import type { Stats } from '../stats.js';
|
|
4
|
+
import { readdir } from './promises.js';
|
|
5
|
+
import { ApiError, ErrorCode } from '../ApiError.js';
|
|
6
|
+
import { readdirSync } from './sync.js';
|
|
7
|
+
import { basename } from './path.js';
|
|
8
|
+
|
|
9
|
+
export class Dirent implements _Dirent {
|
|
10
|
+
public get name(): string {
|
|
11
|
+
return basename(this.path);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
public path: string,
|
|
16
|
+
protected stats: Stats
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
isFile(): boolean {
|
|
20
|
+
return this.stats.isFile();
|
|
21
|
+
}
|
|
22
|
+
isDirectory(): boolean {
|
|
23
|
+
return this.stats.isDirectory();
|
|
24
|
+
}
|
|
25
|
+
isBlockDevice(): boolean {
|
|
26
|
+
return this.stats.isBlockDevice();
|
|
27
|
+
}
|
|
28
|
+
isCharacterDevice(): boolean {
|
|
29
|
+
return this.stats.isCharacterDevice();
|
|
30
|
+
}
|
|
31
|
+
isSymbolicLink(): boolean {
|
|
32
|
+
return this.stats.isSymbolicLink();
|
|
33
|
+
}
|
|
34
|
+
isFIFO(): boolean {
|
|
35
|
+
return this.stats.isFIFO();
|
|
36
|
+
}
|
|
37
|
+
isSocket(): boolean {
|
|
38
|
+
return this.stats.isSocket();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A class representing a directory stream.
|
|
44
|
+
*/
|
|
45
|
+
export class Dir implements _Dir {
|
|
46
|
+
protected closed = false;
|
|
47
|
+
|
|
48
|
+
protected checkClosed(): void {
|
|
49
|
+
if (this.closed) {
|
|
50
|
+
throw new ApiError(ErrorCode.EBADF, 'Can not use closed Dir');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected _entries: Dirent[];
|
|
55
|
+
|
|
56
|
+
constructor(public readonly path: string) {}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Asynchronously close the directory's underlying resource handle.
|
|
60
|
+
* Subsequent reads will result in errors.
|
|
61
|
+
*/
|
|
62
|
+
close(): Promise<void>;
|
|
63
|
+
close(cb: Callback): void;
|
|
64
|
+
close(cb?: Callback): void | Promise<void> {
|
|
65
|
+
this.closed = true;
|
|
66
|
+
if (!cb) {
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
}
|
|
69
|
+
cb();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Synchronously close the directory's underlying resource handle.
|
|
74
|
+
* Subsequent reads will result in errors.
|
|
75
|
+
*/
|
|
76
|
+
closeSync(): void {
|
|
77
|
+
this.closed = true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected async _read(): Promise<Dirent | null> {
|
|
81
|
+
if (!this._entries) {
|
|
82
|
+
this._entries = await readdir(this.path, { withFileTypes: true });
|
|
83
|
+
}
|
|
84
|
+
if (this._entries.length == 0) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return this._entries.shift();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Asynchronously read the next directory entry via `readdir(3)` as an `Dirent`.
|
|
92
|
+
* After the read is completed, a value is returned that will be resolved with an `Dirent`, or `null` if there are no more directory entries to read.
|
|
93
|
+
* Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms.
|
|
94
|
+
*/
|
|
95
|
+
read(): Promise<Dirent | null>;
|
|
96
|
+
read(cb: Callback<[Dirent | null]>): void;
|
|
97
|
+
read(cb?: Callback<[Dirent | null]>): void | Promise<Dirent | null> {
|
|
98
|
+
if (!cb) {
|
|
99
|
+
return this._read();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this._read().then(value => cb(null, value));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Synchronously read the next directory entry via `readdir(3)` as a `Dirent`.
|
|
107
|
+
* If there are no more directory entries to read, null will be returned.
|
|
108
|
+
* Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms.
|
|
109
|
+
*/
|
|
110
|
+
readSync(): Dirent | null {
|
|
111
|
+
if (!this._entries) {
|
|
112
|
+
this._entries = readdirSync(this.path, { withFileTypes: true });
|
|
113
|
+
}
|
|
114
|
+
if (this._entries.length == 0) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
return this._entries.shift();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Asynchronously iterates over the directory via `readdir(3)` until all entries have been read.
|
|
122
|
+
*/
|
|
123
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<Dirent> {
|
|
124
|
+
const _this = this;
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
[Symbol.asyncIterator]: this[Symbol.asyncIterator],
|
|
128
|
+
async next(): Promise<IteratorResult<Dirent>> {
|
|
129
|
+
const value = await _this._read();
|
|
130
|
+
if (value != null) {
|
|
131
|
+
return { done: false, value };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
await _this.close();
|
|
135
|
+
return { done: true, value: undefined };
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './async.js';
|
|
2
|
+
export * from './sync.js';
|
|
3
|
+
export * as promises from './promises.js';
|
|
4
|
+
export * as constants from './constants.js';
|
|
5
|
+
export * from './streams.js';
|
|
6
|
+
export * from './dir.js';
|
|
7
|
+
export { mountMapping, mounts, mount, umount } from './shared.js';
|
|
8
|
+
export { Stats, BigIntStats, StatsFs } from '../stats.js';
|