forked from cloudflare/workers-nodejs-compat-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-worker-script.mjs
More file actions
69 lines (57 loc) · 1.54 KB
/
generate-worker-script.mjs
File metadata and controls
69 lines (57 loc) · 1.54 KB
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
63
64
65
66
67
68
69
import baseline from "./data/baseline.json" with { type: "json" };
let code = "";
const escapeIdentifier = (name) => name.replace("/", "_");
const modules = Object.keys(baseline).filter((k) => k !== "*globals*");
for (const moduleName of modules) {
code += `
let ${escapeIdentifier(moduleName)} = null;
try {
${escapeIdentifier(moduleName)} = await import("${moduleName}");
} catch (err) {}
`;
}
code += `
const importedModules = {
${modules.map((moduleName) => `"${moduleName}": ${escapeIdentifier(moduleName)}`).join(",")}
}
`;
const globals = Object.keys(baseline["*globals*"]);
code += `
const workerdGlobals = {
`;
for (const global of globals) {
if (global === "*self*") {
// skip synthetic synthetic *self* node
continue;
}
code += `
${global}: globalThis.${global},
`;
}
code += `
};
`;
console.log(
`
import { visit } from "../dump-utils.mjs";
import baseline from "../data/baseline.json";
export default {
async fetch(request, env, ctx) {
${code}
// delete any workerdGlobals that are undefined so that we can distinguish between undefined and missing globals
for (const global of Object.keys(workerdGlobals)) {
if (workerdGlobals[global] === undefined && !(global in globalThis)) {
delete workerdGlobals[global];
}
}
const result = visit(baseline, {
"*globals*": workerdGlobals,
...importedModules,
});
return new Response(JSON.stringify(result, null, 2), {
headers: { "Content-Type": "application/json" },
});
},
};
`
);