1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}
`;
// Increase caching duration if we have a version specifier.
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}`,
},
});
};