Move response filters into Rust (#755)

This commit is contained in:
Gregory Schier authored and GitHub committed 2026-10-15 06:22:23 -07:00
1 parent 19908b1902
commit 9b3715024d
42 files changed
+15538 -399

No files matched your search

@@ -15,6 +15,8 @@ import type {
BootRequest,
DeleteKeyValueResponse,
DeleteModelResponse,
FilterLanguage,
FilterResponse,
FindHttpResponsesResponse,
Folder,
FormInput,
@@ -657,6 +659,14 @@ export class PluginInstance {
);
};
const filterText = (language: FilterLanguage, text: string, query: string) =>
this.#sendForReply<FilterResponse>(context, {
type: "filter_text_request",
language,
text,
query,
});
const _windowInfo = async () => {
if (context.label == null) {
throw new Error("Can't get window context without an active window");
@@ -1003,6 +1013,10 @@ export class PluginInstance {
return result.data as any;
},
},
filter: {
jsonPath: (text: string, query: string) => filterText("jsonpath", text, query),
xPath: (text: string, query: string) => filterText("xpath", text, query),
},
store: {
get: async <T>(key: string) => {
const payload = { type: "get_key_value_request", key } as const;
@@ -0,0 +1,71 @@
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import type { InternalEvent, InternalEventPayload } from "@yaakapp/api";
import { expect, test } from "vite-plus/test";
import { EventChannel } from "../src/EventChannel";
import { PluginInstance } from "../src/PluginInstance";
test("ctx.filter asks the host and hands back its reply", async () => {
const dir = await mkdtemp(path.join(tmpdir(), "yaak-filter-"));
await mkdir(path.join(dir, "build"));
await writeFile(
path.join(dir, "build/index.js"),
`exports.plugin = { async init(ctx) {
const results = [
await ctx.filter.jsonPath('{"a":1}', "$.a"),
await ctx.filter.xPath("<a/>", "//b["),
];
await ctx.toast.show({ message: JSON.stringify(results) });
} };`,
);
const channel = new EventChannel();
const context = { workspaceId: null, label: null };
const bootRequest = { dir, watch: false };
const instance = new PluginInstance({ bootRequest, pluginRefId: "test", context }, channel);
const requests: InternalEventPayload[] = [];
const reply = (event: InternalEvent, payload: InternalEventPayload) =>
instance.postMessage({ ...event, id: `reply-${event.id}`, replyId: event.id, payload });
const toast = new Promise<string>((resolve, reject) => {
channel.listen((event) => {
const { payload } = event;
if (payload.type === "filter_text_request") {
// Match the JSON transport used by the real plugin host.
requests.push(JSON.parse(JSON.stringify(payload)) as InternalEventPayload);
reply(
event,
payload.language === "jsonpath"
? { type: "filter_text_response", content: "[\n 1\n]" }
: { type: "filter_text_response", content: "", error: "Invalid filter: bad" },
);
} else if (payload.type === "show_toast_request") {
resolve(payload.message);
} else if (payload.type === "error_response") {
reject(new Error(payload.error));
}
});
});
try {
instance.postMessage({
id: "boot",
replyId: null,
pluginRefId: "test",
pluginName: "test",
context,
payload: { type: "boot_request", ...bootRequest },
});
expect(JSON.parse(await toast)).toEqual([
{ content: "[\n 1\n]" },
{ content: "", error: "Invalid filter: bad" },
]);
expect(requests).toEqual([
{ type: "filter_text_request", language: "jsonpath", text: '{"a":1}', query: "$.a" },
{ type: "filter_text_request", language: "xpath", text: "<a/>", query: "//b[" },
]);
} finally {
await instance.terminate();
await rm(dir, { recursive: true, force: true });
}
});