@zenfs/core 1.11.4 → 2.1.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/backend.d.ts +19 -15
- package/dist/backends/backend.js +36 -19
- package/dist/backends/cow.d.ts +20 -30
- package/dist/backends/cow.js +83 -192
- package/dist/backends/fetch.d.ts +1 -0
- package/dist/backends/fetch.js +30 -30
- package/dist/backends/index.d.ts +1 -1
- package/dist/backends/index.js +1 -1
- package/dist/backends/memory.d.ts +5 -7
- package/dist/backends/memory.js +2 -3
- package/dist/backends/passthrough.d.ts +19 -23
- package/dist/backends/passthrough.js +98 -288
- package/dist/backends/port.d.ts +220 -0
- package/dist/backends/port.js +328 -0
- package/dist/backends/single_buffer.d.ts +59 -47
- package/dist/backends/single_buffer.js +468 -219
- package/dist/backends/store/fs.d.ts +25 -35
- package/dist/backends/store/fs.js +276 -315
- package/dist/backends/store/store.d.ts +10 -15
- package/dist/backends/store/store.js +11 -10
- package/dist/config.d.ts +3 -12
- package/dist/config.js +17 -19
- package/dist/context.d.ts +8 -21
- package/dist/context.js +33 -10
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/internal/contexts.d.ts +63 -0
- package/dist/internal/contexts.js +15 -0
- package/dist/internal/credentials.d.ts +2 -11
- package/dist/internal/credentials.js +0 -19
- package/dist/internal/devices.d.ts +18 -80
- package/dist/internal/devices.js +103 -316
- package/dist/internal/error.d.ts +9 -204
- package/dist/internal/error.js +19 -288
- package/dist/internal/file_index.d.ts +1 -1
- package/dist/internal/file_index.js +11 -11
- package/dist/internal/filesystem.d.ts +51 -94
- package/dist/internal/filesystem.js +21 -20
- package/dist/internal/index.d.ts +1 -2
- package/dist/internal/index.js +1 -2
- package/dist/internal/index_fs.d.ts +12 -30
- package/dist/internal/index_fs.js +37 -69
- package/dist/internal/inode.d.ts +140 -24
- package/dist/internal/inode.js +515 -66
- package/dist/mixins/async.js +52 -112
- package/dist/mixins/mutexed.d.ts +19 -18
- package/dist/mixins/mutexed.js +62 -64
- package/dist/mixins/readonly.d.ts +7 -6
- package/dist/mixins/readonly.js +24 -18
- package/dist/mixins/sync.js +8 -8
- package/dist/{vfs/path.d.ts → path.d.ts} +3 -4
- package/dist/{vfs/path.js → path.js} +6 -9
- package/dist/polyfills.js +1 -1
- package/dist/readline.d.ts +134 -0
- package/dist/readline.js +623 -0
- package/dist/utils.d.ts +9 -37
- package/dist/utils.js +17 -85
- package/dist/vfs/acl.d.ts +42 -0
- package/dist/vfs/acl.js +268 -0
- package/dist/vfs/async.d.ts +9 -23
- package/dist/vfs/async.js +25 -27
- package/dist/vfs/config.d.ts +6 -18
- package/dist/vfs/config.js +8 -18
- package/dist/vfs/dir.d.ts +3 -3
- package/dist/vfs/dir.js +12 -12
- package/dist/vfs/file.d.ts +106 -0
- package/dist/vfs/file.js +244 -0
- package/dist/vfs/flags.d.ts +19 -0
- package/dist/vfs/flags.js +62 -0
- package/dist/vfs/index.d.ts +4 -10
- package/dist/vfs/index.js +4 -13
- package/dist/vfs/ioctl.d.ts +88 -0
- package/dist/vfs/ioctl.js +409 -0
- package/dist/vfs/promises.d.ts +81 -19
- package/dist/vfs/promises.js +404 -288
- package/dist/vfs/shared.d.ts +7 -37
- package/dist/vfs/shared.js +29 -85
- package/dist/{stats.d.ts → vfs/stats.d.ts} +14 -28
- package/dist/{stats.js → vfs/stats.js} +11 -66
- package/dist/vfs/streams.d.ts +1 -0
- package/dist/vfs/streams.js +32 -27
- package/dist/vfs/sync.d.ts +3 -3
- package/dist/vfs/sync.js +263 -260
- package/dist/vfs/watchers.d.ts +2 -2
- package/dist/vfs/watchers.js +12 -12
- package/dist/vfs/xattr.d.ts +116 -0
- package/dist/vfs/xattr.js +201 -0
- package/package.json +5 -3
- package/readme.md +1 -1
- package/scripts/test.js +2 -2
- package/tests/assignment.ts +1 -1
- package/tests/backend/config.worker.js +4 -1
- package/tests/backend/fetch.test.ts +3 -0
- package/tests/backend/port.test.ts +19 -33
- package/tests/backend/remote.worker.js +4 -1
- package/tests/backend/single-buffer.test.ts +53 -0
- package/tests/backend/single-buffer.worker.js +30 -0
- package/tests/common/context.test.ts +3 -3
- package/tests/common/handle.test.ts +17 -12
- package/tests/common/mutex.test.ts +9 -9
- package/tests/common/path.test.ts +1 -1
- package/tests/common/readline.test.ts +104 -0
- package/tests/common.ts +4 -19
- package/tests/fetch/fetch.ts +2 -2
- package/tests/fs/append.test.ts +4 -4
- package/tests/fs/directory.test.ts +25 -25
- package/tests/fs/errors.test.ts +15 -19
- package/tests/fs/links.test.ts +4 -3
- package/tests/fs/open.test.ts +4 -21
- package/tests/fs/permissions.test.ts +14 -18
- package/tests/fs/read.test.ts +10 -9
- package/tests/fs/readFile.test.ts +10 -26
- package/tests/fs/rename.test.ts +4 -9
- package/tests/fs/stat.test.ts +8 -8
- package/tests/fs/streams.test.ts +2 -11
- package/tests/fs/times.test.ts +7 -7
- package/tests/fs/truncate.test.ts +8 -36
- package/tests/fs/watch.test.ts +10 -10
- package/tests/fs/write.test.ts +77 -13
- package/tests/fs/xattr.test.ts +85 -0
- package/tests/logs.js +22 -0
- package/tests/setup/context.ts +1 -1
- package/tests/setup/index.ts +3 -3
- package/tests/setup/port.ts +7 -1
- package/dist/backends/port/fs.d.ts +0 -84
- package/dist/backends/port/fs.js +0 -151
- package/dist/backends/port/rpc.d.ts +0 -77
- package/dist/backends/port/rpc.js +0 -100
- package/dist/backends/store/simple.d.ts +0 -20
- package/dist/backends/store/simple.js +0 -13
- package/dist/internal/file.d.ts +0 -359
- package/dist/internal/file.js +0 -751
- package/dist/internal/log.d.ts +0 -133
- package/dist/internal/log.js +0 -218
- package/tests/fs/writeFile.test.ts +0 -70
package/tests/setup/port.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { after } from 'node:test';
|
|
1
2
|
import { MessageChannel } from 'node:worker_threads';
|
|
2
3
|
import { InMemory, Port, configureSingle, fs, resolveMountConfig, resolveRemoteMount } from '../../dist/index.js';
|
|
3
4
|
import { copySync, data } from '../setup.js';
|
|
@@ -5,7 +6,7 @@ import { copySync, data } from '../setup.js';
|
|
|
5
6
|
const { port1: localPort, port2: remotePort } = new MessageChannel();
|
|
6
7
|
|
|
7
8
|
fs.umount('/');
|
|
8
|
-
const tmpfs = await resolveMountConfig({ backend: InMemory,
|
|
9
|
+
const tmpfs = await resolveMountConfig({ backend: InMemory, label: 'tmp' });
|
|
9
10
|
|
|
10
11
|
fs.mount('/', tmpfs);
|
|
11
12
|
copySync(data, fs);
|
|
@@ -13,3 +14,8 @@ copySync(data, fs);
|
|
|
13
14
|
await resolveRemoteMount(remotePort, tmpfs);
|
|
14
15
|
|
|
15
16
|
await configureSingle({ backend: Port, port: localPort });
|
|
17
|
+
|
|
18
|
+
after(() => {
|
|
19
|
+
localPort.close();
|
|
20
|
+
remotePort.close();
|
|
21
|
+
});
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import type { ExtractProperties } from 'utilium';
|
|
2
|
-
import type { Inode, InodeLike } from '../..//internal/inode.js';
|
|
3
|
-
import type { MountConfiguration } from '../../config.js';
|
|
4
|
-
import type { File } from '../../internal/file.js';
|
|
5
|
-
import type { CreationOptions, UsageInfo } from '../../internal/filesystem.js';
|
|
6
|
-
import type { Backend, FilesystemOf } from '../backend.js';
|
|
7
|
-
import { FileSystem } from '../../internal/filesystem.js';
|
|
8
|
-
import { Stats } from '../../stats.js';
|
|
9
|
-
import * as RPC from './rpc.js';
|
|
10
|
-
type FSMethods = ExtractProperties<FileSystem, (...args: any[]) => Promise<any> | UsageInfo>;
|
|
11
|
-
type FSMethod = keyof FSMethods;
|
|
12
|
-
export type FSRequest<TMethod extends FSMethod = FSMethod> = RPC.Message & {
|
|
13
|
-
[M in TMethod]: {
|
|
14
|
-
method: M;
|
|
15
|
-
args: Parameters<FSMethods[M]>;
|
|
16
|
-
};
|
|
17
|
-
}[TMethod];
|
|
18
|
-
declare const PortFS_base: import("../../index.js").Mixin<typeof FileSystem, import("../../mixins/async.js").AsyncMixin>;
|
|
19
|
-
/**
|
|
20
|
-
* PortFS lets you access an FS instance that is running in a port, or the other way around.
|
|
21
|
-
*
|
|
22
|
-
* Note that *direct* synchronous operations are not permitted on the PortFS,
|
|
23
|
-
* regardless of the configuration option of the remote FS.
|
|
24
|
-
*/
|
|
25
|
-
export declare class PortFS extends PortFS_base {
|
|
26
|
-
readonly options: RPC.Options;
|
|
27
|
-
readonly port: RPC.Port;
|
|
28
|
-
/**`
|
|
29
|
-
* @hidden
|
|
30
|
-
*/
|
|
31
|
-
_sync: import("../index.js").StoreFS<import("../memory.js").InMemoryStore>;
|
|
32
|
-
/**
|
|
33
|
-
* Constructs a new PortFS instance that connects with the FS running on `options.port`.
|
|
34
|
-
*/
|
|
35
|
-
constructor(options: RPC.Options);
|
|
36
|
-
protected rpc<const T extends FSMethod>(method: T, ...args: Parameters<FSMethods[T]>): Promise<Awaited<ReturnType<FSMethods[T]>>>;
|
|
37
|
-
ready(): Promise<void>;
|
|
38
|
-
rename(oldPath: string, newPath: string): Promise<void>;
|
|
39
|
-
stat(path: string): Promise<Stats>;
|
|
40
|
-
sync(path: string, data: Uint8Array | undefined, stats: Readonly<InodeLike | Inode>): Promise<void>;
|
|
41
|
-
openFile(path: string, flag: string): Promise<File>;
|
|
42
|
-
createFile(path: string, flag: string, mode: number, options: CreationOptions): Promise<File>;
|
|
43
|
-
unlink(path: string): Promise<void>;
|
|
44
|
-
rmdir(path: string): Promise<void>;
|
|
45
|
-
mkdir(path: string, mode: number, options: CreationOptions): Promise<void>;
|
|
46
|
-
readdir(path: string): Promise<string[]>;
|
|
47
|
-
exists(path: string): Promise<boolean>;
|
|
48
|
-
link(srcpath: string, dstpath: string): Promise<void>;
|
|
49
|
-
read(path: string, buffer: Uint8Array, offset: number, length: number): Promise<void>;
|
|
50
|
-
write(path: string, buffer: Uint8Array, offset: number): Promise<void>;
|
|
51
|
-
}
|
|
52
|
-
/** @internal */
|
|
53
|
-
export declare function handleRequest(port: RPC.Port, fs: FileSystem & {
|
|
54
|
-
_descriptors?: Map<number, File>;
|
|
55
|
-
}, request: FSRequest): Promise<void>;
|
|
56
|
-
export declare function attachFS(port: RPC.Port, fs: FileSystem): void;
|
|
57
|
-
export declare function detachFS(port: RPC.Port, fs: FileSystem): void;
|
|
58
|
-
declare const _Port: {
|
|
59
|
-
name: string;
|
|
60
|
-
options: {
|
|
61
|
-
port: {
|
|
62
|
-
type: "object";
|
|
63
|
-
required: true;
|
|
64
|
-
validator(port: RPC.Port): void;
|
|
65
|
-
};
|
|
66
|
-
timeout: {
|
|
67
|
-
type: "number";
|
|
68
|
-
required: false;
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
create(options: RPC.Options): PortFS;
|
|
72
|
-
};
|
|
73
|
-
type _Port = typeof _Port;
|
|
74
|
-
export interface Port extends _Port {
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* @category Backends and Configuration
|
|
78
|
-
*/
|
|
79
|
-
export declare const Port: Port;
|
|
80
|
-
/**
|
|
81
|
-
* @category Backends and Configuration
|
|
82
|
-
*/
|
|
83
|
-
export declare function resolveRemoteMount<T extends Backend>(port: RPC.Port, config: MountConfiguration<T>, _depth?: number): Promise<FilesystemOf<T>>;
|
|
84
|
-
export {};
|
package/dist/backends/port/fs.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { pick } from 'utilium';
|
|
2
|
-
import { resolveMountConfig } from '../../config.js';
|
|
3
|
-
import { Errno, ErrnoError } from '../../internal/error.js';
|
|
4
|
-
import { FileSystem } from '../../internal/filesystem.js';
|
|
5
|
-
import { err, info } from '../../internal/log.js';
|
|
6
|
-
import { Async } from '../../mixins/async.js';
|
|
7
|
-
import { Stats } from '../../stats.js';
|
|
8
|
-
import { InMemory } from '../memory.js';
|
|
9
|
-
import * as RPC from './rpc.js';
|
|
10
|
-
/**
|
|
11
|
-
* PortFS lets you access an FS instance that is running in a port, or the other way around.
|
|
12
|
-
*
|
|
13
|
-
* Note that *direct* synchronous operations are not permitted on the PortFS,
|
|
14
|
-
* regardless of the configuration option of the remote FS.
|
|
15
|
-
*/
|
|
16
|
-
export class PortFS extends Async(FileSystem) {
|
|
17
|
-
/**
|
|
18
|
-
* Constructs a new PortFS instance that connects with the FS running on `options.port`.
|
|
19
|
-
*/
|
|
20
|
-
constructor(options) {
|
|
21
|
-
super(0x706f7274, 'portfs');
|
|
22
|
-
this.options = options;
|
|
23
|
-
/**`
|
|
24
|
-
* @hidden
|
|
25
|
-
*/
|
|
26
|
-
this._sync = InMemory.create({ name: 'tmpfs:port' });
|
|
27
|
-
this.port = options.port;
|
|
28
|
-
RPC.attach(this.port, RPC.handleResponse);
|
|
29
|
-
}
|
|
30
|
-
rpc(method, ...args) {
|
|
31
|
-
return RPC.request({ method, args }, {
|
|
32
|
-
...this.options,
|
|
33
|
-
fs: this,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
async ready() {
|
|
37
|
-
await this.rpc('ready');
|
|
38
|
-
await super.ready();
|
|
39
|
-
}
|
|
40
|
-
rename(oldPath, newPath) {
|
|
41
|
-
return this.rpc('rename', oldPath, newPath);
|
|
42
|
-
}
|
|
43
|
-
async stat(path) {
|
|
44
|
-
return new Stats(await this.rpc('stat', path));
|
|
45
|
-
}
|
|
46
|
-
sync(path, data, stats) {
|
|
47
|
-
stats = 'toJSON' in stats ? stats.toJSON() : stats;
|
|
48
|
-
return this.rpc('sync', path, data, stats);
|
|
49
|
-
}
|
|
50
|
-
openFile(path, flag) {
|
|
51
|
-
return this.rpc('openFile', path, flag);
|
|
52
|
-
}
|
|
53
|
-
createFile(path, flag, mode, options) {
|
|
54
|
-
return this.rpc('createFile', path, flag, mode, options);
|
|
55
|
-
}
|
|
56
|
-
unlink(path) {
|
|
57
|
-
return this.rpc('unlink', path);
|
|
58
|
-
}
|
|
59
|
-
rmdir(path) {
|
|
60
|
-
return this.rpc('rmdir', path);
|
|
61
|
-
}
|
|
62
|
-
mkdir(path, mode, options) {
|
|
63
|
-
return this.rpc('mkdir', path, mode, options);
|
|
64
|
-
}
|
|
65
|
-
readdir(path) {
|
|
66
|
-
return this.rpc('readdir', path);
|
|
67
|
-
}
|
|
68
|
-
exists(path) {
|
|
69
|
-
return this.rpc('exists', path);
|
|
70
|
-
}
|
|
71
|
-
link(srcpath, dstpath) {
|
|
72
|
-
return this.rpc('link', srcpath, dstpath);
|
|
73
|
-
}
|
|
74
|
-
async read(path, buffer, offset, length) {
|
|
75
|
-
const _buf = (await this.rpc('read', path, buffer, offset, length));
|
|
76
|
-
buffer.set(_buf);
|
|
77
|
-
}
|
|
78
|
-
write(path, buffer, offset) {
|
|
79
|
-
return this.rpc('write', path, buffer, offset);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/** @internal */
|
|
83
|
-
export async function handleRequest(port, fs, request) {
|
|
84
|
-
if (!RPC.isMessage(request))
|
|
85
|
-
return;
|
|
86
|
-
const { method, args, id, stack } = request;
|
|
87
|
-
let value, error = false;
|
|
88
|
-
try {
|
|
89
|
-
// @ts-expect-error 2556
|
|
90
|
-
value = await fs[method](...args);
|
|
91
|
-
switch (method) {
|
|
92
|
-
case 'openFile':
|
|
93
|
-
case 'createFile': {
|
|
94
|
-
value = {
|
|
95
|
-
path: args[0],
|
|
96
|
-
flag: args[1],
|
|
97
|
-
stats: await fs.stat(args[0]),
|
|
98
|
-
};
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
case 'read':
|
|
102
|
-
value = args[1];
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
catch (e) {
|
|
107
|
-
value = e instanceof ErrnoError ? e.toJSON() : pick(e, 'message', 'stack');
|
|
108
|
-
error = true;
|
|
109
|
-
}
|
|
110
|
-
port.postMessage({ _zenfs: true, id, error, method, stack, value });
|
|
111
|
-
}
|
|
112
|
-
export function attachFS(port, fs) {
|
|
113
|
-
RPC.attach(port, request => handleRequest(port, fs, request));
|
|
114
|
-
}
|
|
115
|
-
export function detachFS(port, fs) {
|
|
116
|
-
RPC.detach(port, request => handleRequest(port, fs, request));
|
|
117
|
-
}
|
|
118
|
-
const _Port = {
|
|
119
|
-
name: 'Port',
|
|
120
|
-
options: {
|
|
121
|
-
port: {
|
|
122
|
-
type: 'object',
|
|
123
|
-
required: true,
|
|
124
|
-
validator(port) {
|
|
125
|
-
// Check for a `postMessage` function.
|
|
126
|
-
if (typeof (port === null || port === void 0 ? void 0 : port.postMessage) != 'function') {
|
|
127
|
-
throw err(new ErrnoError(Errno.EINVAL, 'option must be a port'));
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
timeout: { type: 'number', required: false },
|
|
132
|
-
},
|
|
133
|
-
create(options) {
|
|
134
|
-
return new PortFS(options);
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
/**
|
|
138
|
-
* @category Backends and Configuration
|
|
139
|
-
*/
|
|
140
|
-
export const Port = _Port;
|
|
141
|
-
/**
|
|
142
|
-
* @category Backends and Configuration
|
|
143
|
-
*/
|
|
144
|
-
export async function resolveRemoteMount(port, config, _depth = 0) {
|
|
145
|
-
const stopAndReplay = RPC.catchMessages(port);
|
|
146
|
-
const fs = await resolveMountConfig(config, _depth);
|
|
147
|
-
attachFS(port, fs);
|
|
148
|
-
await stopAndReplay(fs);
|
|
149
|
-
info('Resolved remote mount: ' + fs.toString());
|
|
150
|
-
return fs;
|
|
151
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import type { TransferListItem } from 'node:worker_threads';
|
|
2
|
-
import type { WithOptional } from 'utilium';
|
|
3
|
-
import type { ErrnoErrorJSON } from '../../internal/error.js';
|
|
4
|
-
import type { Backend, FilesystemOf } from '../backend.js';
|
|
5
|
-
import type { PortFS } from './fs.js';
|
|
6
|
-
import { type StatsLike } from '../../stats.js';
|
|
7
|
-
type _MessageEvent<T = any> = T | {
|
|
8
|
-
data: T;
|
|
9
|
-
};
|
|
10
|
-
/** @internal */
|
|
11
|
-
export interface Port {
|
|
12
|
-
postMessage(value: unknown, transfer?: TransferListItem[]): void;
|
|
13
|
-
on?(event: 'message' | 'online', listener: (value: unknown) => void): this;
|
|
14
|
-
off?(event: 'message', listener: (value: unknown) => void): this;
|
|
15
|
-
addEventListener?(type: 'message', listener: (ev: _MessageEvent) => void): void;
|
|
16
|
-
removeEventListener?(type: 'message', listener: (ev: _MessageEvent) => void): void;
|
|
17
|
-
}
|
|
18
|
-
export interface Options {
|
|
19
|
-
/**
|
|
20
|
-
* The target port that you want to connect to, or the current port if in a port context.
|
|
21
|
-
*/
|
|
22
|
-
port: Port;
|
|
23
|
-
/**
|
|
24
|
-
* How long to wait for a request to complete
|
|
25
|
-
*/
|
|
26
|
-
timeout?: number;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* An RPC message
|
|
30
|
-
*/
|
|
31
|
-
export interface Message {
|
|
32
|
-
_zenfs: true;
|
|
33
|
-
id: string;
|
|
34
|
-
method: string;
|
|
35
|
-
stack: string;
|
|
36
|
-
}
|
|
37
|
-
export interface Request extends Message {
|
|
38
|
-
args: unknown[];
|
|
39
|
-
}
|
|
40
|
-
interface _ResponseWithError extends Message {
|
|
41
|
-
error: true;
|
|
42
|
-
value: WithOptional<ErrnoErrorJSON, 'code' | 'errno'>;
|
|
43
|
-
}
|
|
44
|
-
interface _ResponseWithValue<T> extends Message {
|
|
45
|
-
error: false;
|
|
46
|
-
value: Awaited<T> extends File ? FileData : Awaited<T>;
|
|
47
|
-
}
|
|
48
|
-
interface _ResponseRead extends Message {
|
|
49
|
-
error: false;
|
|
50
|
-
method: 'read';
|
|
51
|
-
value: Uint8Array;
|
|
52
|
-
}
|
|
53
|
-
export type Response<T = unknown> = _ResponseWithError | _ResponseWithValue<T> | _ResponseRead;
|
|
54
|
-
export interface FileData {
|
|
55
|
-
path: string;
|
|
56
|
-
flag: string;
|
|
57
|
-
stats: StatsLike<number>;
|
|
58
|
-
}
|
|
59
|
-
export type { FileData as File };
|
|
60
|
-
export declare function isMessage(arg: unknown): arg is Message;
|
|
61
|
-
type _Executor = Parameters<ConstructorParameters<typeof Promise<any>>[0]>;
|
|
62
|
-
export interface Executor {
|
|
63
|
-
resolve: _Executor[0];
|
|
64
|
-
reject: _Executor[1];
|
|
65
|
-
fs?: PortFS;
|
|
66
|
-
}
|
|
67
|
-
export declare function request<const TRequest extends Request, TValue>(request: Omit<TRequest, 'id' | 'stack' | '_zenfs'>, { port, timeout, fs }?: Partial<Options> & {
|
|
68
|
-
fs?: PortFS;
|
|
69
|
-
}): Promise<TValue>;
|
|
70
|
-
export declare function handleResponse<const TResponse extends Response>(response: TResponse): void;
|
|
71
|
-
export declare function attach<T extends Message>(port: Port, handler: (message: T) => unknown): void;
|
|
72
|
-
export declare function detach<T extends Message>(port: Port, handler: (message: T) => unknown): void;
|
|
73
|
-
export declare function catchMessages<T extends Backend>(port: Port): (fs: FilesystemOf<T>) => Promise<void>;
|
|
74
|
-
/**
|
|
75
|
-
* @internal
|
|
76
|
-
*/
|
|
77
|
-
export declare function waitOnline(port: Port): Promise<void>;
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { Errno, ErrnoError } from '../../internal/error.js';
|
|
2
|
-
import { LazyFile } from '../../internal/file.js';
|
|
3
|
-
import { err, info } from '../../internal/log.js';
|
|
4
|
-
import { Stats } from '../../stats.js';
|
|
5
|
-
import { handleRequest } from './fs.js';
|
|
6
|
-
function isFileData(value) {
|
|
7
|
-
return typeof value == 'object' && value != null && 'path' in value && 'flag' in value;
|
|
8
|
-
}
|
|
9
|
-
// general types
|
|
10
|
-
export function isMessage(arg) {
|
|
11
|
-
return typeof arg == 'object' && arg != null && '_zenfs' in arg && !!arg._zenfs;
|
|
12
|
-
}
|
|
13
|
-
const executors = new Map();
|
|
14
|
-
export function request(request, { port, timeout = 1000, fs } = {}) {
|
|
15
|
-
const stack = '\n' + new Error().stack.slice('Error:'.length);
|
|
16
|
-
if (!port)
|
|
17
|
-
throw err(new ErrnoError(Errno.EINVAL, 'Can not make an RPC request without a port'));
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
const id = Math.random().toString(16).slice(10);
|
|
20
|
-
executors.set(id, { resolve, reject, fs });
|
|
21
|
-
port.postMessage({ ...request, _zenfs: true, id, stack });
|
|
22
|
-
const _ = setTimeout(() => {
|
|
23
|
-
const error = err(new ErrnoError(Errno.EIO, 'RPC Failed', typeof request.args[0] == 'string' ? request.args[0] : '', request.method), {
|
|
24
|
-
fs,
|
|
25
|
-
});
|
|
26
|
-
error.stack += stack;
|
|
27
|
-
reject(error);
|
|
28
|
-
if (typeof _ == 'object')
|
|
29
|
-
_.unref();
|
|
30
|
-
}, timeout);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
export function handleResponse(response) {
|
|
34
|
-
if (!isMessage(response)) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const { id, value, error, stack } = response;
|
|
38
|
-
if (!executors.has(id)) {
|
|
39
|
-
const error = err(new ErrnoError(Errno.EIO, 'Invalid RPC id:' + id));
|
|
40
|
-
error.stack += stack;
|
|
41
|
-
throw error;
|
|
42
|
-
}
|
|
43
|
-
const { resolve, reject, fs } = executors.get(id);
|
|
44
|
-
if (error) {
|
|
45
|
-
const e = ErrnoError.fromJSON({ code: 'EIO', errno: Errno.EIO, ...value });
|
|
46
|
-
e.stack += stack;
|
|
47
|
-
reject(e);
|
|
48
|
-
executors.delete(id);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
if (isFileData(value)) {
|
|
52
|
-
const { path, flag, stats } = value;
|
|
53
|
-
const file = new LazyFile(fs, path, flag, new Stats(stats));
|
|
54
|
-
resolve(file);
|
|
55
|
-
executors.delete(id);
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
resolve(value);
|
|
59
|
-
executors.delete(id);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
export function attach(port, handler) {
|
|
63
|
-
if (!port)
|
|
64
|
-
throw err(new ErrnoError(Errno.EINVAL, 'Cannot attach to non-existent port'));
|
|
65
|
-
info('Attached handler to port: ' + handler.name);
|
|
66
|
-
port['on' in port ? 'on' : 'addEventListener']('message', (message) => {
|
|
67
|
-
handler(typeof message == 'object' && message !== null && 'data' in message ? message.data : message);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
export function detach(port, handler) {
|
|
71
|
-
if (!port)
|
|
72
|
-
throw err(new ErrnoError(Errno.EINVAL, 'Cannot detach from non-existent port'));
|
|
73
|
-
info('Detached handler from port: ' + handler.name);
|
|
74
|
-
port['off' in port ? 'off' : 'removeEventListener']('message', (message) => {
|
|
75
|
-
handler(typeof message == 'object' && message !== null && 'data' in message ? message.data : message);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
export function catchMessages(port) {
|
|
79
|
-
const events = [];
|
|
80
|
-
const handler = events.push.bind(events);
|
|
81
|
-
attach(port, handler);
|
|
82
|
-
return async function (fs) {
|
|
83
|
-
detach(port, handler);
|
|
84
|
-
for (const event of events) {
|
|
85
|
-
const request = 'data' in event ? event.data : event;
|
|
86
|
-
await handleRequest(port, fs, request);
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* @internal
|
|
92
|
-
*/
|
|
93
|
-
export async function waitOnline(port) {
|
|
94
|
-
if (!('on' in port))
|
|
95
|
-
return; // Only need to wait in Node.js
|
|
96
|
-
const online = Promise.withResolvers();
|
|
97
|
-
setTimeout(online.reject, 500);
|
|
98
|
-
port.on('online', online.resolve);
|
|
99
|
-
await online.promise;
|
|
100
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { AsyncMap, SyncMapStore } from './map.js';
|
|
2
|
-
import { SyncMapTransaction } from './map.js';
|
|
3
|
-
import type { Store } from './store.js';
|
|
4
|
-
/**
|
|
5
|
-
* @category Stores and Transactions
|
|
6
|
-
* @deprecated Use `MapStore` instead.
|
|
7
|
-
*/
|
|
8
|
-
export type SimpleSyncStore = SyncMapStore;
|
|
9
|
-
/**
|
|
10
|
-
* @category Stores and Transactions
|
|
11
|
-
* @deprecated Use `AsyncMapStore` instead.
|
|
12
|
-
*/
|
|
13
|
-
export type SimpleAsyncStore = AsyncMap & Store;
|
|
14
|
-
/**
|
|
15
|
-
* @category Stores and Transactions
|
|
16
|
-
* @deprecated Use `MapTransaction` instead.
|
|
17
|
-
*/
|
|
18
|
-
export declare class SimpleTransaction extends SyncMapTransaction {
|
|
19
|
-
constructor(store: SyncMapStore);
|
|
20
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { log_deprecated } from '../../internal/log.js';
|
|
2
|
-
import { SyncMapTransaction } from './map.js';
|
|
3
|
-
/**
|
|
4
|
-
* @category Stores and Transactions
|
|
5
|
-
* @deprecated Use `MapTransaction` instead.
|
|
6
|
-
*/
|
|
7
|
-
export class SimpleTransaction extends SyncMapTransaction {
|
|
8
|
-
constructor(store) {
|
|
9
|
-
log_deprecated('SimpleTransaction');
|
|
10
|
-
super(store);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
/* node:coverage enable */
|