feat(server): add persistent terminal and notebook watcher (#5)
Use node-pty for native terminal support and chokidar for recursive cross-platform watching with atomic-save normalization. Validate terminal messages, detach clients safely, and expose debounced typed notebook events.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { relative, resolve, isAbsolute, sep } from "node:path";
|
||||
import { watch } from "chokidar";
|
||||
import type { ServerEvent } from "@shellbook/core";
|
||||
|
||||
export type NotebookFileEvent = Extract<ServerEvent, { type: "file.changed" | "file.deleted" }>;
|
||||
export interface NotebookWatcher {
|
||||
/** Resolves after the initial scan; existing files do not emit changes. */
|
||||
ready: Promise<void>;
|
||||
/** Returns an unsubscribe function. */
|
||||
on(listener: (event: NotebookFileEvent) => void): () => void;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
/** Watches workspace/notebooks recursively, including newly created directories.
|
||||
* Chokidar normalizes cross-platform events and editor atomic renames; v4 has no glob dependency.
|
||||
*/
|
||||
export function createNotebookWatcher(workspaceDir: string, options: {
|
||||
debounceMs?: number;
|
||||
onError?: (error: unknown) => void;
|
||||
} = {}): NotebookWatcher {
|
||||
const root = resolve(workspaceDir, "notebooks");
|
||||
const listeners = new Set<(event: NotebookFileEvent) => void>();
|
||||
const pending = new Map<string, ReturnType<typeof setTimeout>>();
|
||||
let closed = false;
|
||||
const watcher = watch(root, { ignoreInitial: true, followSymlinks: false, atomic: true });
|
||||
const ready = new Promise<void>((resolveReady, reject) => {
|
||||
watcher.once("ready", resolveReady);
|
||||
watcher.on("error", (error) => {
|
||||
reject(error);
|
||||
if (options.onError) options.onError(error);
|
||||
else console.error("Notebook watcher error", error);
|
||||
});
|
||||
});
|
||||
// Callers can await ready; avoid an unhandled rejection before they attach.
|
||||
void ready.catch(() => {});
|
||||
const schedule = (type: NotebookFileEvent["type"], file: string) => {
|
||||
const path = relative(root, file).split(sep).join("/");
|
||||
if (closed || !path.endsWith(".md") || path.startsWith("../") || isAbsolute(path)) return;
|
||||
clearTimeout(pending.get(path));
|
||||
pending.set(path, setTimeout(() => {
|
||||
pending.delete(path);
|
||||
for (const listener of listeners) listener({ type, path });
|
||||
}, options.debounceMs ?? 100));
|
||||
};
|
||||
watcher.on("add", (file) => schedule("file.changed", file));
|
||||
watcher.on("change", (file) => schedule("file.changed", file));
|
||||
watcher.on("unlink", (file) => schedule("file.deleted", file));
|
||||
return {
|
||||
ready,
|
||||
on(listener) { listeners.add(listener); return () => { listeners.delete(listener); }; },
|
||||
async close() {
|
||||
closed = true;
|
||||
for (const timer of pending.values()) clearTimeout(timer);
|
||||
pending.clear(); listeners.clear();
|
||||
await watcher.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user