Skip to content

Commit

Permalink
add resolveRemoteMount
Browse files Browse the repository at this point in the history
  • Loading branch information
UnCor3 committed Jul 28, 2024
1 parent baf1311 commit 49c5b3f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/backends/port/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { File } from '../../file.js';
import { Async, FileSystem, type FileSystemMetadata } from '../../filesystem.js';
import { Stats, type FileType } from '../../stats.js';
import { InMemory } from '../memory.js';
import type { Backend } from '../backend.js';
import { type Backend, type FilesystemOf } from '../backend.js';
import * as RPC from './rpc.js';
import { type MountConfiguration, resolveMountConfig } from '../../config.js';

type FileMethods = Omit<ExtractProperties<File, (...args: any[]) => Promise<any>>, typeof Symbol.asyncDispose>;
type FileMethod = keyof FileMethods;
Expand Down Expand Up @@ -223,9 +224,9 @@ let nextFd = 0;

const descriptors: Map<number, File> = new Map();

type FileOrFSRequest = FSRequest | FileRequest;
export type FileOrFSRequest = FSRequest | FileRequest;

async function handleRequest(port: RPC.Port, fs: FileSystem, request: FileOrFSRequest): Promise<void> {
export async function handleRequest(port: RPC.Port, fs: FileSystem, request: FileOrFSRequest): Promise<void> {
if (!RPC.isMessage(request)) {
return;
}
Expand Down Expand Up @@ -308,3 +309,11 @@ export const Port = {
return new PortFS(options);
},
} satisfies Backend<PortFS, RPC.Options>;

export async function resolveRemoteMount<T extends Backend>(port: RPC.Port, config: MountConfiguration<T>, _depth = 0): Promise<FilesystemOf<T>> {
const stopAndReplay = RPC.catchMessages(port);
const fs = await resolveMountConfig(config, _depth);
attachFS(port, fs);
stopAndReplay(fs);
return fs;
}
17 changes: 16 additions & 1 deletion src/backends/port/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Errno, ErrnoError, type ErrnoErrorJSON } from '../../error.js';
import { PortFile, type PortFS } from './fs.js';
import { type Backend, type FilesystemOf } from '../backend.js';
import { handleRequest, PortFile, type PortFS } from './fs.js';
import { type FileOrFSRequest } from './fs.js';

type _MessageEvent<T = any> = T | { data: T };

Expand Down Expand Up @@ -148,3 +150,16 @@ export function detach<T extends Message>(port: Port, handler: (message: T) => u
handler('data' in message ? message.data : message);
});
}

export function catchMessages<T extends Backend>(port: Port): (fs: FilesystemOf<T>) => void {
const events: _MessageEvent[] = [];
const handler = events.push.bind(events);
attach(port, handler);
return function (fs: any) {
detach(port, handler);
for (const event of events) {
const request: FileOrFSRequest = 'data' in event ? event.data : event;
handleRequest(port, fs, request);
}
};
}
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function resolveMountConfig<T extends Backend>(config: MountConfigu
return mount;
}

type ConfigMounts = { [K in AbsolutePath]: Backend };
export type ConfigMounts = { [K in AbsolutePath]: Backend };

/**
* Configuration
Expand Down

0 comments on commit 49c5b3f

Please sign in to comment.