cloud / internal-packages / installer / container / check-imports.mjsmain at aacbc74
// check-imports.mjs — DO THE NO-BUILD APPS' IMPORTS ANSWER? For check-result.sh, in the run's
// container. A mind's Dash has no build: the browser loads iterate's
// components through the import map in each app's index.html, from a published build that the
// page pins. An app whose pinned build lacks what it imports still answers a signed-out visit as
// it should, and its page crashes after the sign-in: so the run asks the imports themselves.
//
// The run has no member's sign-in, so it cannot fetch an app's files through the mind (a
// signed-out request gets the sign-in, not the file). It reads the files that the mind was born
// with or was brought up to: the default config that this image's stack carries. For each app it
// asks every address of the import map, every stylesheet of another origin, and every module that
// a script of the app imports through the map (app.js and each module beside or below it). Each
// must answer 200.
//
// node check-imports.mjs <the apps folder> <app> … one line per app:
// `imports <app> works <count>` or `imports <app> missing <address> answered <status>; …`
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
const [folder, ...apps] = process.argv.slice(2);
/** The addresses an app's page loads from elsewhere. */
export function addressesOf(page, script) {
const map = JSON.parse(/<script type="importmap">([\s\S]*?)<\/script>/.exec(page)?.[1] || "{}");
const imports = Object.entries(map.imports || {});
// an entry that ends in `/` is a folder: it answers nothing by itself
const found = new Set(imports.map(([, to]) => to).filter((to) => !to.endsWith("/")));
for (const [, href] of page.matchAll(/<link\b[^>]*\bhref="(https:\/\/[^"]+)"/g)) found.add(href);
for (const [, , name] of script.matchAll(/\b(from|import)\s*\(?\s*["']([^"'./][^"']*)["']/g)) {
// the longest key of the map that the name starts with, as a browser resolves it
const [key, to] =
imports
.filter(([key]) => name === key || (key.endsWith("/") && name.startsWith(key)))
.sort((a, b) => b[0].length - a[0].length)[0] || [];
if (key) found.add(to + name.slice(key.length));
else if (/^https:\/\//.test(name)) found.add(name);
else found.add(`no entry in the import map for "${name}"`);
}
return [...found];
}
/** The text of every script of an app: public/app.js and the modules that it can import. */
export function scriptsOf(folder) {
return readdirSync(folder, { recursive: true, withFileTypes: true })
.filter((entry) => entry.isFile() && /\.m?js$/.test(entry.name))
.map((entry) => readFileSync(join(entry.parentPath, entry.name), "utf8"))
.join("\n");
}
async function statusOf(address) {
if (!address.startsWith("https://")) return "nothing";
for (let attempt = 1; attempt <= 3; attempt++) {
try {
const answer = await fetch(address, { signal: AbortSignal.timeout(20_000) });
await answer.body?.cancel();
if (answer.status === 200 || attempt === 3) return answer.status;
} catch {
if (attempt === 3) return "nothing";
}
await new Promise((done) => setTimeout(done, 2000));
}
}
for (const app of apps) {
let addresses;
try {
addresses = addressesOf(
readFileSync(join(folder, app, "index.html"), "utf8"),
scriptsOf(join(folder, app, "public")),
);
} catch (error) {
console.log(`imports ${app} unread ${String(error.message || error).slice(0, 120)}`);
continue;
}
const answers = await Promise.all(addresses.map(async (a) => [a, await statusOf(a)]));
const missing = answers.filter(([, status]) => status !== 200);
console.log(
missing.length === 0
? `imports ${app} works ${addresses.length}`
: `imports ${app} missing ${missing.map(([a, s]) => `${a} answered ${s}`).join("; ")}`,
);
}