-
Notifications
You must be signed in to change notification settings - Fork 22
/
cloudflare.ts
78 lines (72 loc) · 2.07 KB
/
cloudflare.ts
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
70
71
72
73
74
75
76
77
78
import type { Preset } from "../types";
// Built-in APIs provided by workerd.
// https://developers.cloudflare.com/workers/runtime-apis/nodejs/
// https://github.com/cloudflare/workerd/tree/main/src/node
// Last checked: 2024-10-22
const cloudflareNodeCompatModules = [
"_stream_duplex",
"_stream_passthrough",
"_stream_readable",
"_stream_transform",
"_stream_writable",
"assert",
"assert/strict",
"buffer",
"diagnostics_channel",
"events",
"path",
"querystring",
"stream",
"stream/consumers",
"stream/promises",
"stream/web",
"string_decoder",
"url",
"util/types",
"zlib",
];
// Modules implemented via a mix of workerd APIs and polyfills.
// See `src/runtime/node/<module name>/$cloudflare.ts`.
const hybridNodeCompatModules = [
"async_hooks",
"console",
"crypto",
"module",
"process",
"timers",
"util",
];
const cloudflarePreset: Preset = {
alias: {
...Object.fromEntries(
cloudflareNodeCompatModules.flatMap((p) => [
[p, p],
[`node:${p}`, `node:${p}`],
]),
),
// The `node:sys` module is just a deprecated alias for `node:util`
// Keep this until util is a full compat module.
sys: "unenv/runtime/node/util/$cloudflare",
"node:sys": "unenv/runtime/node/util/$cloudflare",
// define aliases for hybrid modules
...Object.fromEntries(
hybridNodeCompatModules.flatMap((m) => [
[m, `unenv/runtime/node/${m}/$cloudflare`],
[`node:${m}`, `unenv/runtime/node/${m}/$cloudflare`],
]),
),
},
inject: {
// workerd already defines `global` and `Buffer`
// override the previous presets so that we use the native implementation
Buffer: false,
global: false,
console: "unenv/runtime/node/console/$cloudflare",
process: "unenv/runtime/node/process/$cloudflare",
setImmediate: ["unenv/runtime/node/timers/$cloudflare", "setImmediate"],
clearImmediate: ["unenv/runtime/node/timers/$cloudflare", "clearImmediate"],
},
polyfill: [],
external: cloudflareNodeCompatModules.flatMap((p) => [p, `node:${p}`]),
};
export default cloudflarePreset;