import { Worker } from "node:worker_threads";
import { describe, expect, it } from "vitest";
// shiki's js regex engine backtracks without end on this go struct literal. on the
// server each blob page with it pinned an instance's event loop for good; highlights'
// hand-written lexers have no regex engine to run away
const BACKTRACKING_GO = 'pp := struct {\n\t}{t(*p), strings.TrimSuffix(p.URL.String(), "/")}\n';
// a runaway regex can't be interrupted on its own thread, so this tokenizes in a
// worker and terminates it at the deadline
const WORKER = `
const { parentPort, workerData } = require("node:worker_threads");
import(workerData.pierre).then(async ({ getSharedHighlighter, registerCustomCSSVariableTheme }) => {
registerCustomCSSVariableTheme("test", { background: "#ffffff", foreground: "#000000" });
const highlighter = await getSharedHighlighter({
themes: ["test"],
langs: ["go"],
preferredHighlighter: "highlights"
});
highlighter.codeToTokens(workerData.code, { lang: "go", theme: "test" });
parentPort.postMessage("done");
});
`;
const tokenizesWithin = (code: string, ms: number): Promise<boolean> =>
new Promise((resolve, reject) => {
const worker = new Worker(WORKER, {
eval: true,
workerData: { pierre: import.meta.resolve("@pierre/diffs"), code }
});
const finish = (done: boolean) => {
clearTimeout(timer);
void worker.terminate();
resolve(done);
};
const timer = setTimeout(() => finish(false), ms);
worker.once("message", () => finish(true));
worker.once("error", (err) => {
clearTimeout(timer);
reject(err);
});
});
describe("shared highlighter on the server", () => {
it("tokenizes the backtracking go literal with highlights", async () => {
expect(await tokenizesWithin(BACKTRACKING_GO, 10_000)).toBe(true);
}, 15_000);
});