Files
romm/frontend/scripts/playerIsolationHeaders.ts
Georges-Antoine Assi b5cd86e3f7 fix(frontend): restate the player isolation URLs instead of reading nginx
The vite plugin parsed the COOP/COEP patterns out of the nginx template at
config load, but the Dockerfile's frontend-build stage copies frontend/
alone, so the config threw ENOENT and the image build failed.

Restate the two patterns in the plugin and let its test assert they still
match the template's map, which also drops the dev container mount that
existed only to feed that read.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 09:13:32 -04:00

43 lines
1.5 KiB
TypeScript

// The dev server's stand-in for nginx's COOP/COEP map. Only the player URLs
// are isolated: COEP would block the third-party images the rest of the app
// embeds.
import type { Connect, Plugin } from "vite";
// A copy of the `$coep_header`/`$coop_header` map entries in
// docker/nginx/templates/default.conf.template, restated here because the
// production build runs from frontend/ alone and cannot read that file.
// playerIsolationHeaders.test.ts fails when the two drift.
export const ISOLATED_PLAYER_URLS: readonly RegExp[] = [
/^\/rom\/.*\/(ejs|jsdos)(\?|$)/,
/^\/console\/rom\/[0-9]+\/play/,
];
// nginx matches the request URI rather than the path, since try_files rewrites
// the path to /index.html before it reaches the headers.
function isIsolated(url: string): boolean {
return ISOLATED_PLAYER_URLS.some((pattern) => pattern.test(url));
}
// `vite preview` serves the built app with no nginx in front of it either, so
// it answers the player URLs the same way the dev server does.
export function playerIsolationHeaders(): Plugin {
const isolate: Connect.NextHandleFunction = (req, res, next) => {
if (isIsolated(req.url ?? "")) {
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
}
next();
};
return {
name: "romm:player-isolation-headers",
apply: "serve",
configureServer(server) {
server.middlewares.use(isolate);
},
configurePreviewServer(server) {
server.middlewares.use(isolate);
},
};
}