Files
romm/frontend/src/v2/utils/scriptLoader.ts
Georges-Antoine Assi 1d532d8f70 refactor(pico8): move the player helpers into v2/utils
The PICO-8 modules sat beside the view they serve, while every other v2
helper lives in v2/utils, player ones included (playerBezel, playerDisc,
playerFirmware).

scriptLoader moves with them: pico8Runtime imports it, and nothing in
v2/utils reaches into v2/views, so leaving it behind would have been the
first exception. EmulatorJS and JsDos pick it up from the new path.

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

39 lines
1.3 KiB
TypeScript

// Shared runtime injection for the player views. Runtimes are served from
// /assets in the full image; only EmulatorJS and js-dos have a CDN fallback.
/** Inject a <script> and resolve once it has executed. */
export function loadScript(src: string): Promise<void> {
return new Promise((resolve, reject) => {
const s = document.createElement("script");
s.src = src;
s.async = true;
s.onload = () => resolve();
s.onerror = () => reject(new Error(`Failed loading ${src}`));
document.body.appendChild(s);
});
}
/**
* Check that a URL really serves JavaScript.
*
* The Vite dev server and many SPA hosts answer a missing asset with 200 plus
* index.html, which a <script> tag loads without ever firing onerror.
*
* @param url The script URL to pre-flight.
* @returns True when the body is JavaScript, false otherwise.
*/
export async function isJsResource(url: string): Promise<boolean> {
try {
const res = await fetch(url);
if (!res.ok) return false;
const ct = res.headers.get("content-type") ?? "";
if (/javascript|ecmascript/i.test(ct)) return true;
if (/text\/html/i.test(ct)) return false;
// Content-Type may be absent (older servers); sniff the body.
const text = await res.clone().text();
return !text.trimStart().startsWith("<");
} catch {
return false;
}
}