export const observable_esm = async (request: Request): Promise<Response> => {
const url = new URL(request.url);
url.protocol = "https";
url.hostname = "esm.run";
const body = `
import * as module from ${JSON.stringify(url)};
export default function define(runtime, observer) {
const main = runtime.module();
main.variable(observer("__module")).define("__module", () => module);
for(const key of Object.keys(module)) {
const alias = key === "default" ? "__" + key : key;
main.variable(observer(alias)).define(alias, () => module[key]);
}
return main;
}
`;
const maxAge = 60 * 60 * (url.pathname.slice(2).includes("@") ? 24 : 1);
return new Response(body, {
headers: {
"Content-Type": "application/javascript",
"Cache-Control": `public, max-age=${maxAge}`,
},
});
};