// @ts-check
/** @param {{ args: string[], caller: { name: string } | null, accept: string[] }} context */
export default function whoami({ args, caller, accept }) {
if (args.includes("--help")) return "Show the Wimble user making this request.\n\nUsage: run.js\n";
for (const type of accept) {
if (type === "text/html") {
return new Response(`<p>Hello, <strong>${escapeHtml(caller?.name ?? "anonymous")}</strong>.</p>`, {
headers: { "content-type": "text/html; charset=utf-8" },
});
}
if (type === "application/json") return { caller: caller?.name ?? null };
if (type === "text/plain") return `${caller?.name ?? "anonymous"}\n`;
}
}
/** @param {string} value */
function escapeHtml(value) {
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """);
}