P1: typed RPC bridge + FileSystemProvider + dual-panel local browsing
- shared/types + shared/rpc: full wire contract (Result envelope, no exceptions cross the bridge) - FileSystemProvider interface, LocalProvider, SessionRegistry with built-in 'local' - walk.ts: provider-generic recursive walk + removeRecursive - rpc-handlers wired into BrowserWindow; 10min request timeout for slow SFTP ops - Angular: RpcService (Electroview bridge + plain-browser guard), PanelsService with signals-based PanelState (sort/cursor/selection), FilePanel component with keyboard nav (arrows/Space/Enter/Backspace/Tab), path bar, toasts - verified: both panels listDir home over the bridge in the running app Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { createReadStream, createWriteStream } from "node:fs";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { Stats } from "node:fs";
|
||||
import type { EntryType, FileEntry } from "../../shared/types";
|
||||
import type { FileSystemProvider } from "./provider";
|
||||
|
||||
function typeOf(st: Stats): EntryType {
|
||||
if (st.isSymbolicLink()) return "symlink";
|
||||
return st.isDirectory() ? "dir" : "file";
|
||||
}
|
||||
|
||||
function toEntry(name: string, full: string, st: Stats): FileEntry {
|
||||
return {
|
||||
name,
|
||||
path: full,
|
||||
type: typeOf(st),
|
||||
size: st.size,
|
||||
mtime: st.mtimeMs,
|
||||
mode: st.mode & 0o7777,
|
||||
};
|
||||
}
|
||||
|
||||
export class LocalProvider implements FileSystemProvider {
|
||||
readonly kind = "local" as const;
|
||||
|
||||
join = (...parts: string[]) => path.join(...parts);
|
||||
dirname = (p: string) => path.dirname(p);
|
||||
basename = (p: string) => path.basename(p);
|
||||
normalize = (p: string) => path.resolve(p);
|
||||
|
||||
async homeDir(): Promise<string> {
|
||||
return os.homedir();
|
||||
}
|
||||
|
||||
async list(dir: string): Promise<FileEntry[]> {
|
||||
const names = await fs.readdir(dir);
|
||||
const entries: FileEntry[] = [];
|
||||
for (const name of names) {
|
||||
const full = path.join(dir, name);
|
||||
try {
|
||||
entries.push(toEntry(name, full, await fs.lstat(full)));
|
||||
} catch {
|
||||
// entry vanished or is unreadable — skip it
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
async stat(p: string): Promise<FileEntry> {
|
||||
return toEntry(path.basename(p), p, await fs.lstat(p));
|
||||
}
|
||||
|
||||
async mkdir(p: string): Promise<void> {
|
||||
await fs.mkdir(p);
|
||||
}
|
||||
|
||||
async createEmptyFile(p: string): Promise<void> {
|
||||
await fs.writeFile(p, "", { flag: "wx" });
|
||||
}
|
||||
|
||||
async rename(oldPath: string, newPath: string): Promise<void> {
|
||||
await fs.rename(oldPath, newPath);
|
||||
}
|
||||
|
||||
async deleteFile(p: string): Promise<void> {
|
||||
await fs.unlink(p);
|
||||
}
|
||||
|
||||
async deleteEmptyDir(p: string): Promise<void> {
|
||||
await fs.rmdir(p);
|
||||
}
|
||||
|
||||
async chmod(p: string, mode: number): Promise<void> {
|
||||
await fs.chmod(p, mode);
|
||||
}
|
||||
|
||||
createReadStream(p: string) {
|
||||
return createReadStream(p);
|
||||
}
|
||||
|
||||
createWriteStream(p: string) {
|
||||
return createWriteStream(p);
|
||||
}
|
||||
|
||||
async dispose(): Promise<void> {}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { Readable, Writable } from "node:stream";
|
||||
import type { FileEntry } from "../../shared/types";
|
||||
|
||||
/**
|
||||
* The one abstraction that makes panels symmetric: local disk and SFTP servers
|
||||
* implement the same surface, so browsing and transfers are provider-agnostic.
|
||||
*
|
||||
* Providers are deliberately dumb (single-node ops + streams). Recursion lives
|
||||
* once in transfers/walk.ts. Path helpers belong to the provider because local
|
||||
* paths use the OS separator while remote paths are always POSIX — never join
|
||||
* a path for one filesystem with the other's helper.
|
||||
*/
|
||||
export interface FileSystemProvider {
|
||||
readonly kind: "local" | "sftp";
|
||||
join(...parts: string[]): string;
|
||||
dirname(p: string): string;
|
||||
basename(p: string): string;
|
||||
normalize(p: string): string;
|
||||
homeDir(): Promise<string>;
|
||||
list(dir: string): Promise<FileEntry[]>; // never includes "." / ".."
|
||||
stat(path: string): Promise<FileEntry>;
|
||||
mkdir(path: string): Promise<void>;
|
||||
createEmptyFile(path: string): Promise<void>; // fails if it already exists
|
||||
rename(oldPath: string, newPath: string): Promise<void>;
|
||||
deleteFile(path: string): Promise<void>; // also removes symlinks
|
||||
deleteEmptyDir(path: string): Promise<void>;
|
||||
chmod(path: string, mode: number): Promise<void>;
|
||||
createReadStream(path: string): Readable;
|
||||
createWriteStream(path: string): Writable;
|
||||
dispose(): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { LocalProvider } from "./local";
|
||||
import type { FileSystemProvider } from "./provider";
|
||||
|
||||
export const LOCAL_SESSION_ID = "local";
|
||||
|
||||
/**
|
||||
* Maps sessionId -> provider. "local" is always available; each SFTP connect
|
||||
* adds a new session (one SSH connection per panel-connect, no refcounting).
|
||||
*/
|
||||
export class SessionRegistry {
|
||||
private sessions = new Map<string, FileSystemProvider>([
|
||||
[LOCAL_SESSION_ID, new LocalProvider()],
|
||||
]);
|
||||
private counter = 0;
|
||||
|
||||
get(sessionId: string): FileSystemProvider {
|
||||
const provider = this.sessions.get(sessionId);
|
||||
if (!provider) throw new Error(`Session "${sessionId}" is not connected`);
|
||||
return provider;
|
||||
}
|
||||
|
||||
add(provider: FileSystemProvider): string {
|
||||
const id = `sftp-${++this.counter}`;
|
||||
this.sessions.set(id, provider);
|
||||
return id;
|
||||
}
|
||||
|
||||
async remove(sessionId: string): Promise<void> {
|
||||
if (sessionId === LOCAL_SESSION_ID)
|
||||
throw new Error("The local session cannot be disconnected");
|
||||
const provider = this.sessions.get(sessionId);
|
||||
if (!provider) return;
|
||||
this.sessions.delete(sessionId);
|
||||
await provider.dispose();
|
||||
}
|
||||
|
||||
/** Remove without dispose — for sessions that died on their own. */
|
||||
drop(sessionId: string): void {
|
||||
if (sessionId === LOCAL_SESSION_ID) return;
|
||||
this.sessions.delete(sessionId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user