-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (53 loc) · 1.41 KB
/
index.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Miniflare } from "miniflare";
const modules = {
"/hello.js": {
esModule: 'export const hello = "Hello";',
},
};
const mf = new Miniflare({
unsafeModuleFallbackService(request) {
const resolveMethod = request.headers.get("X-Resolve-Method");
if (resolveMethod !== "import" && resolveMethod !== "require") {
throw new Error("unrecognized resolvedMethod");
}
const url = new URL(request.url);
const specifier = url.searchParams.get("specifier");
if (!specifier) {
throw new Error("no specifier provided");
}
if (!modules[specifier]) {
return new Response(null, { status: 404 });
}
return new Response(
JSON.stringify({
...modules[specifier],
})
);
},
workers: [
{
name: "entrypoint",
modulesRoot: "/",
compatibilityFlags: ["nodejs_compat"],
modules: [
{
type: "ESModule",
path: "/index.mjs",
contents: `
export default {
async fetch() {
const { hello } = await import("./hello.js");
return new Response(hello + " World");
}
}
`,
},
],
unsafeUseModuleFallbackService: true,
},
],
});
const resp = await mf.dispatchFetch("http://localhost/");
const text = await resp.text();
console.log(`Response from Miniflare: "${text}"\n`);
await mf.dispose();