fix(plugins): stream dropped files and folders into plugin workbenches

This commit is contained in:
Jinpy authored and GitHub committed 2026-09-30 15:08:17 +08:00
1 parent ce5217bd23
commit be82fdc989
13 files changed
+1717 -671

No files matched your search

@@ -10,7 +10,7 @@ const mocks = vi.hoisted(() => ({
subscribePluginEvents: vi.fn(),
repushPluginConnection: vi.fn(),
reopenPluginConnection: vi.fn(),
openPluginLocalFile: vi.fn(),
openDroppedPluginLocalFiles: vi.fn(),
readPluginLocalFileChunk: vi.fn(),
writePluginLocalFileChunk: vi.fn(),
closePluginLocalFile: vi.fn(),
@@ -18,7 +18,7 @@ const mocks = vi.hoisted(() => ({
}));
vi.mock("@/lib/backend/tauri", () => ({
openPluginLocalFile: mocks.openPluginLocalFile,
openDroppedPluginLocalFiles: mocks.openDroppedPluginLocalFiles,
readPluginLocalFileChunk: mocks.readPluginLocalFileChunk,
writePluginLocalFileChunk: mocks.writePluginLocalFileChunk,
closePluginLocalFile: mocks.closePluginLocalFile,
@@ -77,6 +77,7 @@ describe("PluginWorkbenchHost initialization", () => {
vi.stubGlobal("getComputedStyle", () => Object.assign([], { getPropertyValue: () => "" }));
mocks.readPluginUiEntry.mockResolvedValue({ dataBase64: btoa("<!doctype html><html><body></body></html>"), contentType: "text/html" });
mocks.subscribePluginEvents.mockResolvedValue(vi.fn());
mocks.closePluginLocalFile.mockResolvedValue(undefined);
mocks.repushPluginConnection.mockReset().mockResolvedValue(undefined);
mocks.reopenPluginConnection.mockResolvedValue(undefined);
root = document.createElement("div");
@@ -150,7 +151,7 @@ describe("PluginWorkbenchHost initialization", () => {
const { frame, postMessage } = await mountHost();
const elementFromPoint = vi.spyOn(document, "elementFromPoint").mockReturnValue(frame);
// The Rust registry hands out uuid strings; the `t` prefix stays opaque.
mocks.openPluginLocalFile.mockResolvedValue({ handleId: "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81", name: "a.txt", size: 3, contentType: "text/plain", write: false });
mocks.openDroppedPluginLocalFiles.mockResolvedValue({ dropId: "drop-0001", truncated: false, files: [{ handleId: "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81", name: "a.txt", size: 3, contentType: "text/plain", write: false }] });
const claimed = !document.dispatchEvent(
new CustomEvent("dbx:tauri-file-drop", {
@@ -164,7 +165,102 @@ describe("PluginWorkbenchHost initialization", () => {
const posted = postMessage.mock.calls.map(([message]) => message as Record<string, unknown>);
expect(posted.some((message) => message.type === "filedrop" && (message.files as Array<Record<string, unknown>>)?.some((file) => file.handleId === "t0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81" && file.name === "a.txt"))).toBe(true);
});
expect(mocks.openPluginLocalFile).toHaveBeenCalledWith("sample", "/tmp/a.txt", false);
expect(mocks.openDroppedPluginLocalFiles).toHaveBeenCalledWith("sample", ["/tmp/a.txt"]);
elementFromPoint.mockRestore();
});
it("forwards a dropped folder as the files the Rust side expanded it to", async () => {
const { frame, postMessage } = await mountHost();
const elementFromPoint = vi.spyOn(document, "elementFromPoint").mockReturnValue(frame);
mocks.openDroppedPluginLocalFiles.mockResolvedValue({
dropId: "drop-0002",
truncated: true,
files: [
{ handleId: "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81", name: "a.txt", size: 1, contentType: "text/plain", write: false, relativePath: "folder/a.txt" },
{ handleId: "1d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81", name: "nested.csv", size: 2, contentType: "text/csv", write: false, relativePath: "folder/nested/nested.csv" },
],
});
document.dispatchEvent(
new CustomEvent("dbx:tauri-file-drop", {
detail: { type: "drop", paths: ["/tmp/folder"], position: { x: 200, y: 200 } },
cancelable: true,
}),
);
await vi.waitFor(() => {
const posted = postMessage.mock.calls.map(([message]) => message as Record<string, unknown>);
const filedrop = posted.find((message) => message.type === "filedrop");
expect((filedrop?.files as Array<Record<string, unknown>>)?.map((file) => file.relativePath)).toEqual(["folder/a.txt", "folder/nested/nested.csv"]);
expect(filedrop?.dropId).toBe("drop-0002");
expect(filedrop?.truncated).toBe(true);
});
expect(mocks.openDroppedPluginLocalFiles).toHaveBeenCalledWith("sample", ["/tmp/folder"]);
elementFromPoint.mockRestore();
});
it("does not forward a filedrop when no dropped path could be opened", async () => {
const { frame, postMessage } = await mountHost();
const elementFromPoint = vi.spyOn(document, "elementFromPoint").mockReturnValue(frame);
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
mocks.openDroppedPluginLocalFiles.mockResolvedValue({ dropId: "drop-0003", truncated: false, files: [] });
const claimed = !document.dispatchEvent(
new CustomEvent("dbx:tauri-file-drop", {
detail: { type: "drop", paths: ["/tmp/gone"], position: { x: 200, y: 200 } },
cancelable: true,
}),
);
expect(claimed).toBe(true);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(postMessage.mock.calls.some(([message]) => (message as Record<string, unknown>).type === "filedrop")).toBe(false);
expect(consoleError).toHaveBeenCalled();
consoleError.mockRestore();
elementFromPoint.mockRestore();
});
it("sweeps opened drop entries when the workbench unmounts", async () => {
const { frame, postMessage } = await mountHost();
const elementFromPoint = vi.spyOn(document, "elementFromPoint").mockReturnValue(frame);
mocks.openDroppedPluginLocalFiles.mockResolvedValue({ dropId: "drop-0004", truncated: false, files: [{ handleId: "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81", name: "a.txt", size: 1, contentType: "text/plain", write: false }] });
document.dispatchEvent(
new CustomEvent("dbx:tauri-file-drop", {
detail: { type: "drop", paths: ["/tmp/a.txt"], position: { x: 200, y: 200 } },
cancelable: true,
}),
);
await vi.waitFor(() => expect(postMessage.mock.calls.some(([message]) => (message as Record<string, unknown>).type === "filedrop")).toBe(true));
app?.unmount();
app = undefined;
// Teardown closes exactly the handles this instance opened — never an
// owner-wide sweep, which would kill a sibling workbench's handles.
await new Promise((resolve) => setTimeout(resolve, 0));
expect(mocks.closePluginLocalFile).toHaveBeenCalledWith("sample", "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81");
elementFromPoint.mockRestore();
});
it("retires an in-flight drop whose expansion lands after unmount", async () => {
const { frame, postMessage } = await mountHost();
const elementFromPoint = vi.spyOn(document, "elementFromPoint").mockReturnValue(frame);
let resolveOpen!: (result: unknown) => void;
mocks.openDroppedPluginLocalFiles.mockReturnValue(new Promise((resolve) => (resolveOpen = resolve)));
document.dispatchEvent(
new CustomEvent("dbx:tauri-file-drop", {
detail: { type: "drop", paths: ["/tmp/folder"], position: { x: 200, y: 200 } },
cancelable: true,
}),
);
// The expansion is still in flight when the workbench tears down.
app?.unmount();
app = undefined;
resolveOpen({ dropId: "drop-late", truncated: false, files: [{ handleId: "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81", name: "late.txt", size: 1, contentType: "text/plain", write: false }] });
await new Promise((resolve) => setTimeout(resolve, 0));
expect(mocks.closePluginLocalFile).toHaveBeenCalledWith("sample", "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81");
expect(postMessage.mock.calls.some(([message]) => (message as Record<string, unknown>).type === "filedrop")).toBe(false);
elementFromPoint.mockRestore();
});
@@ -182,7 +278,7 @@ describe("PluginWorkbenchHost initialization", () => {
expect(claimed).toBe(false);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(postMessage).not.toHaveBeenCalled();
expect(mocks.openPluginLocalFile).not.toHaveBeenCalled();
expect(mocks.openDroppedPluginLocalFiles).not.toHaveBeenCalled();
elementFromPoint.mockRestore();
});
@@ -93,12 +93,16 @@ let bootCacheHit = false;
// read with "unknown plugin file handle". Every path is consented to on the
// Rust side — dialogs open there (plugin_file_pick_files / plugin_file_save_as)
// and drops are registered by the native drag-drop pipeline before
// plugin_file_open accepts them — never a plugin-supplied string.
// plugin_file_open_dropped accepts them — never a plugin-supplied string.
let webFileSequence = 0;
const webPickedFiles = new Map<string, File>();
const webSaveBuffers = new Map<string, { name: string; contentType: string; chunks: Map<number, Uint8Array> }>();
const openTauriHandles = new Set<string>();
// raw handle id -> the plugin id that opened it, recorded at open time.
// Teardown closes precisely these handles: an owner-wide sweep would also
// kill the SAME plugin's handles held by a sibling workbench instance, so it
// is reserved for Rust-side lifecycle (stop/uninstall) only.
const openTauriHandles = new Map<string, string>();
const tauriHandlePrefix = "t";
const webHandlePrefix = "w";
@@ -124,15 +128,21 @@ async function tauriFileApi() {
}
// OS drops are the one flow where the renderer still names a path: the native
// drag-drop pipeline registered the dropped files for THIS webview, and
// plugin_file_open accepts exactly those (one open per dropped file).
async function openTauriPluginFile(pluginId: string, path: string, write: boolean): Promise<PluginFileHandleMeta> {
const { openPluginLocalFile } = await tauriFileApi();
const handle = await openPluginLocalFile(pluginId, path, write);
// Track read AND write handles: unmount must reclaim both (leaked fds also
// burn the shared 64-handle registry quota).
openTauriHandles.add(handle.handleId);
return { handleId: `${tauriHandlePrefix}${handle.handleId}`, name: handle.name, size: handle.size, contentType: handle.contentType };
// drag-drop pipeline registered the dropped paths for THIS webview, and
// plugin_file_open_dropped accepts exactly those (one open attempt per dropped
// path). A dropped folder comes back expanded to its contained files on the
// Rust side, with `truncated` flagging any cap cutoff and a `dropId` grouping
// the entries of this one drop.
async function openDroppedPluginFiles(pluginId: string, paths: string[]): Promise<{ dropId: string; truncated: boolean; files: PluginFileHandleMeta[] }> {
const { openDroppedPluginLocalFiles } = await tauriFileApi();
const result = await openDroppedPluginLocalFiles(pluginId, paths);
const files = result.files.map((handle) => {
openTauriHandles.set(handle.handleId, pluginId);
const meta: PluginFileHandleMeta = { handleId: `${tauriHandlePrefix}${handle.handleId}`, name: handle.name, size: handle.size, contentType: handle.contentType };
if (handle.relativePath) meta.relativePath = handle.relativePath;
return meta;
});
return { dropId: result.dropId, truncated: result.truncated, files };
}
async function pickPluginFiles(pluginId: string, options: PluginPickFilesOptions): Promise<PluginFileHandleMeta[]> {
@@ -144,7 +154,7 @@ async function pickPluginFiles(pluginId: string, options: PluginPickFilesOptions
for (const handle of await pickPluginLocalFiles(pluginId, options.multiple === true)) {
// Track read AND write handles: unmount must reclaim both (leaked fds
// also burn the shared 64-handle registry quota).
openTauriHandles.add(handle.handleId);
openTauriHandles.set(handle.handleId, pluginId);
files.push({ handleId: `${tauriHandlePrefix}${handle.handleId}`, name: handle.name, size: handle.size, contentType: handle.contentType });
}
return files;
@@ -233,7 +243,7 @@ async function beginPluginFileSave(pluginId: string, request: { name?: string; c
const { savePluginLocalFileAs } = await tauriFileApi();
const handle = await savePluginLocalFileAs(pluginId, request.name || "download.bin");
if (!handle) return null;
openTauriHandles.add(handle.handleId);
openTauriHandles.set(handle.handleId, pluginId);
return { handleId: `${tauriHandlePrefix}${handle.handleId}`, chunkBytes: PLUGIN_SAVE_CHUNK_BYTES };
}
const handleId = `${webHandlePrefix}${++webFileSequence}`;
@@ -257,8 +267,11 @@ async function finishPluginFileSave(pluginId: string, handleId: string): Promise
const parsed = parseHandleId(handleId);
if (parsed.source === "tauri") {
const { closePluginLocalFile } = await tauriFileApi();
openTauriHandles.delete(parsed.rawId);
await closePluginLocalFile(pluginId, parsed.rawId);
// Drop local tracking only after the close succeeded: a failed flush
// leaves the Rust entry alive, and the map is what unmount cleanup uses
// to retry it.
openTauriHandles.delete(parsed.rawId);
return;
}
const buffer = webSaveBuffers.get(handleId);
@@ -284,8 +297,8 @@ async function closePluginFileHandleById(pluginId: string, handleId: string): Pr
const parsed = parseHandleId(handleId);
if (parsed.source === "tauri") {
const { closePluginLocalFile } = await tauriFileApi();
openTauriHandles.delete(parsed.rawId);
await closePluginLocalFile(pluginId, parsed.rawId);
openTauriHandles.delete(parsed.rawId);
return;
}
webPickedFiles.delete(handleId);
@@ -293,9 +306,11 @@ async function closePluginFileHandleById(pluginId: string, handleId: string): Pr
}
function disposeLocalFileHandles(): void {
for (const handleId of openTauriHandles)
tauriFileApi()
.then(({ closePluginLocalFile }) => closePluginLocalFile(props.plugin.manifest.id, handleId))
// Close exactly the handles THIS instance opened, with the plugin id that
// opened them — a sibling workbench of the same plugin keeps its own.
for (const [handleId, pluginId] of openTauriHandles)
void tauriFileApi()
.then(({ closePluginLocalFile }) => closePluginLocalFile(pluginId, handleId))
.catch(() => undefined);
openTauriHandles.clear();
webPickedFiles.clear();
@@ -352,16 +367,32 @@ function onHostFileDrop(event: Event): void {
forwardDragState(false);
const paths = (payload.paths || []).filter((path) => typeof path === "string" && path);
if (!paths.length || !bridge) return;
// Pin the owner and generation before the await: the expansion runs on a
// Rust blocking thread and can land after a teardown or identity rebuild.
const pluginId = props.plugin.manifest.id;
const generation = loadGeneration;
void (async () => {
const files: PluginFileHandleMeta[] = [];
for (const path of paths) {
try {
files.push(await openTauriPluginFile(props.plugin.manifest.id, path, false));
} catch (error) {
console.error("[DBX][plugin-workbench:drop]", error);
try {
// A granted folder comes back expanded to its files; a path the drop
// pipeline never granted (or that vanished) is skipped on the Rust side,
// so an empty result surfaces here instead of silently handing the
// plugin nothing while the drop looked successful.
const { dropId, truncated, files } = await openDroppedPluginFiles(pluginId, paths);
if (disposed || generation !== loadGeneration) {
// The teardown already ran while the expansion was in flight: retire
// exactly these late arrivals — a closeAll here would also kill the
// same plugin's handles in a freshly rebuilt workbench.
const rawIds = files.map((file) => file.handleId.replace(new RegExp(`^${tauriHandlePrefix}`), ""));
void tauriFileApi().then(({ closePluginLocalFile }) => {
for (const rawId of rawIds) closePluginLocalFile(pluginId, rawId).catch(() => undefined);
});
return;
}
if (files.length) bridge?.forwardFileDrop(files, { dropId, truncated });
else console.error("[DBX][plugin-workbench:drop] no dropped files could be opened", paths);
} catch (error) {
console.error("[DBX][plugin-workbench:drop]", error);
}
if (files.length) bridge?.forwardFileDrop(files);
})();
}
@@ -461,6 +492,9 @@ function createBridge() {
writeFileChunk: (pluginId, handleId, offset, bytes) => writePluginFileChunkById(pluginId, handleId, offset, bytes),
finishFileSave: (pluginId, handleId) => finishPluginFileSave(pluginId, handleId),
closeFileHandle: (pluginId, handleId) => closePluginFileHandleById(pluginId, handleId),
// OS drops (with folder expansion) are a desktop-host capability; the
// bridge advertises it to plugins through capabilities.fileTransfer.
receiveOsDrops: isTauriRuntime(),
storageGet: (pluginId, key) => getPluginStorage(pluginId, key),
storageSet: (pluginId, key, value) => setPluginStorage(pluginId, key, value),
storageDelete: (pluginId, key) => deletePluginStorage(pluginId, key),
@@ -549,6 +583,9 @@ function pluginUiBaseUrl(pluginId: string, entryDirectory: string): string | und
async function loadWorkbench() {
const generation = ++loadGeneration;
// An identity rebuild swaps the plugin this component serves; retire the
// previous plugin's handles first or they would outlive their owner.
disposeLocalFileHandles();
bridge?.dispose();
bridge = undefined;
loading.value = true;
+2 -2
View File
@@ -5389,9 +5389,9 @@ export * from "@/lib/backend/mqtt-http";
// Plugin local file streaming (native dialogs / OS drops are Tauri-only)
// ---------------------------------------------------------------------------
import type { PluginLocalFileChunk, PluginLocalFileHandle, PluginLocalFileWriteResult } from "./tauri";
import type { PluginDroppedFilesResult, PluginLocalFileChunk, PluginLocalFileHandle, PluginLocalFileWriteResult } from "./tauri";
export async function openPluginLocalFile(_pluginId: string, _path: string, _write: boolean): Promise<PluginLocalFileHandle> {
export async function openDroppedPluginLocalFiles(_pluginId: string, _paths: string[]): Promise<PluginDroppedFilesResult> {
throw new Error("Plugin local file access is not available in the web backend");
}
+17 -6
View File
@@ -2736,6 +2736,8 @@ export interface PluginLocalFileHandle {
size: number;
contentType: string;
write: boolean;
/** Only for files expanded out of a dropped folder: '/'-separated path relative to the dropped folder root. */
relativePath?: string;
}
export interface PluginLocalFileChunk {
@@ -2749,14 +2751,23 @@ export interface PluginLocalFileWriteResult {
nextOffset: number;
}
export async function openPluginLocalFile(pluginId: string, path: string, write: boolean): Promise<PluginLocalFileHandle> {
return invoke("plugin_file_open", { pluginId, path, write });
}
// The native open/save dialogs run on the Rust side: the host never passes
// paths into the plugin-file registry, it only receives handles for what the
// user picked. Only the OS drop flow still goes through openPluginLocalFile,
// and the Rust command accepts exactly the paths its own drop pipeline granted.
// user picked. Only the OS drop flow goes through openDroppedPluginLocalFiles:
// the Rust command accepts exactly the paths its own drop pipeline granted to
// this webview (one open attempt per granted path); a granted folder expands
// to its contained files on the Rust side, and `truncated` flags any cap
// cutoff so partial delivery is visible to the plugin.
export interface PluginDroppedFilesResult {
dropId: string;
files: PluginLocalFileHandle[];
truncated: boolean;
}
export async function openDroppedPluginLocalFiles(pluginId: string, paths: string[]): Promise<PluginDroppedFilesResult> {
return invoke("plugin_file_open_dropped", { pluginId, paths });
}
export async function pickPluginLocalFiles(pluginId: string, multiple: boolean): Promise<PluginLocalFileHandle[]> {
return invoke("plugin_file_pick_files", { pluginId, multiple });
}
@@ -120,6 +120,8 @@ export interface PluginFileHandleMeta {
name: string;
size: number;
contentType: string;
/** Only for files expanded out of a dropped folder: '/'-separated path relative to the dropped folder root, so the plugin can rebuild the dragged tree. */
relativePath?: string;
}
export interface PluginPickFilesOptions {
@@ -250,6 +252,8 @@ export interface PluginHostBridgeApi {
finishFileSave?(pluginId: string, handleId: string): Promise<void>;
/** Close any file handle, discarding unsaved state. */
closeFileHandle?(pluginId: string, handleId: string): Promise<void>;
/** Whether this host receives OS file drops on the plugin's behalf (desktop only). */
receiveOsDrops?: boolean;
/** Persistent per-plugin key-value storage for sandboxed UIs; resolves null when the key is unset. */
storageGet?(pluginId: string, key: string): Promise<unknown>;
storageSet?(pluginId: string, key: string, value: unknown): Promise<void>;
@@ -460,6 +464,16 @@ export class PluginHostBridge {
clipboardRead: !!this.api.clipboardRead,
clipboardImageRead: !!this.api.clipboardReadImage,
mediaUrl: !!this.api.openMedia && !!this.api.closeMedia,
// The namespace exists on both hosts, but what it can DO differs:
// portable plugins gate on these flags instead of probing calls.
fileTransfer: {
pick: !!this.api.pickFiles,
beginSave: !!this.api.beginFileSave && !!this.api.writeFileChunk && !!this.api.finishFileSave,
read: !!this.api.readFileChunk,
// OS drops (and their folder expansion) are desktop-only.
drop: !!this.api.receiveOsDrops,
folderExpansion: !!this.api.receiveOsDrops,
},
},
context: snapshotPluginWorkbenchContext(this.context),
});
@@ -539,8 +553,8 @@ export class PluginHostBridge {
}
/** Hand the plugin already-opened handles for files dropped onto its workbench. */
forwardFileDrop(files: PluginFileHandleMeta[]): void {
this.post({ source: HOST_MESSAGE_SOURCE, version: BRIDGE_VERSION, type: "filedrop", files });
forwardFileDrop(files: PluginFileHandleMeta[], drop?: { dropId?: string; truncated?: boolean }): void {
this.post({ source: HOST_MESSAGE_SOURCE, version: BRIDGE_VERSION, type: "filedrop", files, dropId: drop?.dropId, truncated: drop?.truncated === true });
}
private async handleRequest(request: PluginRequestMessage, target: Window): Promise<void> {
@@ -1352,8 +1366,11 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
document.dispatchEvent(new CustomEvent('dbx-plugin-binary', { detail: payload }));
} else if (message.type === 'filedrop') {
const files = Array.isArray(message.files) ? message.files : [];
listeners.filedrop.forEach((listener) => listener(files));
document.dispatchEvent(new CustomEvent('dbx-plugin-filedrop', { detail: files }));
// The drop metadata rides as a second listener argument so existing
// single-parameter listeners keep working unchanged.
const drop = { dropId: typeof message.dropId === 'string' ? message.dropId : undefined, truncated: message.truncated === true };
listeners.filedrop.forEach((listener) => listener(files, drop));
document.dispatchEvent(new CustomEvent('dbx-plugin-filedrop', { detail: { files, drop } }));
} else if (message.type === 'dragstate') {
const active = message.active === true;
listeners.dragstate.forEach((listener) => listener(active));