Files
linuxcncWebWASM/scripts/test_browser_opfs_playwright_s6_05.mjs

51 lines
2.5 KiB
JavaScript

import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { normalize, join } from "node:path";
import { pathToFileURL } from "node:url";
const root = new URL("../web/", import.meta.url).pathname.replace(/\/$/, "");
const modulePath = process.env.PLAYWRIGHT_MODULE;
function blocked(code, reason = null) {
console.log(JSON.stringify({ schema: "browser-opfs-s6-05-playwright-sample.v1", status: "blocked", browser: "chromium", code, reason }));
}
if (!modulePath) {
blocked("playwright_unavailable", "PLAYWRIGHT_MODULE is not set");
} else {
let server;
let browser;
try {
const imported = await import(pathToFileURL(modulePath).href);
const playwright = imported.default ?? imported;
server = createServer(async (request, response) => {
const requested = (request.url ?? "/").split("?")[0];
const relative = requested === "/" ? "/app/browser-opfs-probe.html" : requested;
const file = normalize(join(root, relative));
if (!file.startsWith(root + "/")) { response.writeHead(403); response.end(); return; }
try {
const body = await readFile(file);
response.writeHead(200, { "content-type": file.endsWith(".html") ? "text/html" : "text/javascript", "cache-control": "no-store" });
response.end(body);
} catch { response.writeHead(404); response.end(); }
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const port = server.address().port;
browser = await playwright.chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto(`http://127.0.0.1:${port}/app/browser-opfs-probe.html`);
await page.waitForFunction(() => document.querySelector("#result")?.textContent !== "pending", null, { timeout: 10000 });
const result = JSON.parse(await page.locator("#result").textContent());
if (result.status !== "verified") {
blocked(result.code ?? "opfs_probe_blocked", result.reason ?? null);
} else {
console.log(JSON.stringify({ schema: "browser-opfs-s6-05-playwright-sample.v1", status: "verified", browser: "chromium", browser_version: browser.version(), mode: result.mode, project_id: result.project_id, bytes: result.bytes, commit_seq: result.commit_seq, content_hash: result.content_hash, cleanup: result.cleanup }));
}
} catch (error) {
blocked("playwright_probe_failed", error?.name ?? String(error));
} finally {
await browser?.close().catch(() => {});
server?.close();
}
}