import type { MountConfiguration } from '../config.js'; import type { CreationOptions } from '../internal/filesystem.js'; import type { InodeLike } from '../internal/inode.js'; import type { Backend, FilesystemOf } from './backend.js'; import { FileSystem } from '../internal/filesystem.js'; import { Inode } from '../internal/inode.js'; import * as RPC from '../internal/rpc.js'; import '../polyfills.js'; export { RPC }; /** * @category Backends and Configuration */ export interface PortOptions { /** * The target port that you want to connect to, or the current port if in a port context. */ port: RPC.Channel; /** * How long to wait for a request to complete */ timeout?: number; } declare const PortFS_base: import("../index.js").Mixin; /** * PortFS lets you access an FS instance that is running in a port, or the other way around. * * Note that *direct* synchronous operations are not permitted on the PortFS, * regardless of the configuration option of the remote FS. * @category Internals * @internal */ export declare class PortFS extends PortFS_base { readonly channel: T; readonly timeout: number; readonly port: RPC.Port; /** * @hidden */ _sync: import("./index.js").StoreFS; /** * Constructs a new PortFS instance that connects with the FS running on `options.port`. */ constructor(channel: T, timeout?: number); protected rpc(method: T, ...args: Parameters): Promise>>; ready(): Promise; rename(oldPath: string, newPath: string): Promise; stat(path: string): Promise; touch(path: string, metadata: InodeLike | Inode): Promise; sync(): Promise; createFile(path: string, options: CreationOptions): Promise; unlink(path: string): Promise; rmdir(path: string): Promise; mkdir(path: string, options: CreationOptions): Promise; readdir(path: string): Promise; exists(path: string): Promise; link(srcpath: string, dstpath: string): Promise; read(path: string, buffer: Uint8Array, start: number, end: number): Promise; write(path: string, buffer: Uint8Array, offset: number): Promise; } export declare function attachFS(channel: RPC.Channel | RPC.Port, fs: FileSystem): void; export declare function detachFS(channel: RPC.Channel | RPC.Port, fs: FileSystem): void; declare const _Port: { name: string; options: { port: { type: ({ new (): EventTarget; prototype: EventTarget; } | ((e: any) => boolean))[]; required: true; }; timeout: { type: string; required: false; }; }; create(opt: PortOptions): PortFS; }; type _Port = typeof _Port; /** * @category Backends and Configuration */ export interface Port extends _Port { } /** * A backend for usage with ports and workers. See the examples below. * * #### Accessing an FS on a remote Worker from the main thread * * Main: * * ```ts * import { configure } from '@zenfs/core'; * import { Port } from '@zenfs/port'; * import { Worker } from 'node:worker_threads'; * * const worker = new Worker('worker.js'); * * await configure({ * mounts: { * '/worker': { * backend: Port, * port: worker, * }, * }, * }); * ``` * * Worker: * * ```ts * import { InMemory, resolveRemoteMount, attachFS } from '@zenfs/core'; * import { parentPort } from 'node:worker_threads'; * * await resolveRemoteMount(parentPort, { backend: InMemory, name: 'tmp' }); * ``` * * If you are using using web workers, you would use `self` instead of importing `parentPort` in the worker, and would not need to import `Worker` in the main thread. * * #### Using with multiple ports on the same thread * * ```ts * import { InMemory, fs, resolveMountConfig, resolveRemoteMount, Port } from '@zenfs/core'; * import { MessageChannel } from 'node:worker_threads'; * * const { port1: localPort, port2: remotePort } = new MessageChannel(); * * fs.mount('/remote', await resolveRemoteMount(remotePort, { backend: InMemory, name: 'tmp' })); * fs.mount('/port', await resolveMountConfig({ backend: Port, port: localPort })); * * const content = 'FS is in a port'; * * await fs.promises.writeFile('/port/test', content); * * fs.readFileSync('/remote/test', 'utf8'); // FS is in a port * await fs.promises.readFile('/port/test', 'utf8'); // FS is in a port * ``` * * @category Backends and Configuration */ export declare const Port: Port; /** * @category Backends and Configuration */ export declare function resolveRemoteMount(channel: RPC.Channel | RPC.Port, config: MountConfiguration, _depth?: number): Promise>;