Skip to content

Commit 061587d

Browse files
anonrigvicbdario-piotrowicz
authored
add node:timers and node:net to unenv-preset (#7853)
* add node:timers and node:util to unenv-preset * update unenv to 2.0.0-rc.1 * Update .changeset/popular-pigs-glow.md Co-authored-by: Dario Piotrowicz <[email protected]> * fixup! * fixup! format * fixup! update module tests --------- Co-authored-by: Victor Berchet <[email protected]> Co-authored-by: Dario Piotrowicz <[email protected]>
1 parent a8ae6c8 commit 061587d

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

.changeset/popular-pigs-glow.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@cloudflare/unenv-preset": minor
3+
---
4+
5+
Use the workerd implementation for Node `net`, `timers`, and `timers/promises` modules
6+
7+
- drop the polyfills
8+
- update `unenv` to 2.0.0-rc.1

packages/unenv-preset/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"wrangler": "workspace:*"
5555
},
5656
"peerDependencies": {
57-
"unenv": "2.0.0-rc.0",
58-
"workerd": "^1.20241230.0"
57+
"unenv": "2.0.0-rc.1",
58+
"workerd": "^1.20250124.0"
5959
},
6060
"peerDependenciesMeta": {
6161
"workerd": {

packages/unenv-preset/src/preset.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import { version } from "../package.json";
22
import type { Preset } from "unenv";
33

44
// Built-in APIs provided by workerd.
5+
//
56
// https://developers.cloudflare.com/workers/runtime-apis/nodejs/
67
// https://github.com/cloudflare/workerd/tree/main/src/node
7-
// Last checked: 2024-10-22
8-
const cloudflareNodeCompatModules = [
8+
//
9+
// Last checked: 2025-01-24
10+
//
11+
// NOTE: Please sync any changes to `testNodeCompatModules`.
12+
const nodeCompatModules = [
913
"_stream_duplex",
1014
"_stream_passthrough",
1115
"_stream_readable",
@@ -18,6 +22,7 @@ const cloudflareNodeCompatModules = [
1822
"dns",
1923
"dns/promises",
2024
"events",
25+
"net",
2126
"path",
2227
"path/posix",
2328
"path/win32",
@@ -27,6 +32,8 @@ const cloudflareNodeCompatModules = [
2732
"stream/promises",
2833
"stream/web",
2934
"string_decoder",
35+
"timers",
36+
"timers/promises",
3037
"url",
3138
"util/types",
3239
"zlib",
@@ -39,7 +46,6 @@ const hybridNodeCompatModules = [
3946
"crypto",
4047
"module",
4148
"process",
42-
"timers",
4349
"util",
4450
];
4551

@@ -51,7 +57,7 @@ export const cloudflare: Preset = {
5157
},
5258
alias: {
5359
...Object.fromEntries(
54-
cloudflareNodeCompatModules.flatMap((p) => [
60+
nodeCompatModules.flatMap((p) => [
5561
[p, p],
5662
[`node:${p}`, `node:${p}`],
5763
])
@@ -90,5 +96,5 @@ export const cloudflare: Preset = {
9096
],
9197
},
9298
polyfill: [],
93-
external: cloudflareNodeCompatModules.flatMap((p) => [p, `node:${p}`]),
99+
external: nodeCompatModules.flatMap((p) => [p, `node:${p}`]),
94100
};

packages/unenv-preset/tests/worker/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import assert from "node:assert";
55
export const TESTS = {
66
testCryptoGetRandomValues,
77
testImplementsBuffer,
8-
testModules,
8+
testNodeCompatModules,
99
testUtilImplements,
1010
testPath,
1111
testDns,
1212
testTimers,
13+
testNet,
1314
};
1415

1516
export default {
@@ -74,7 +75,7 @@ async function testImplementsBuffer() {
7475
assert.strictEqual(typeof buffer.resolveObjectURL, "function");
7576
}
7677

77-
async function testModules() {
78+
async function testNodeCompatModules() {
7879
const module = await import("node:module");
7980
// @ts-expect-error exposed by workerd
8081
const require = module.createRequire("/");
@@ -86,6 +87,7 @@ async function testModules() {
8687
"dns",
8788
"dns/promises",
8889
"events",
90+
"net",
8991
"path",
9092
"path/posix",
9193
"path/win32",
@@ -95,6 +97,8 @@ async function testModules() {
9597
"stream/promises",
9698
"stream/web",
9799
"string_decoder",
100+
"timers",
101+
"timers/promises",
98102
"url",
99103
"util/types",
100104
"zlib",
@@ -149,4 +153,14 @@ async function testTimers() {
149153
// active is deprecated and no more in the type
150154
(timers as any).active(timeout);
151155
timers.clearTimeout(timeout);
156+
157+
const timersPromises = await import("node:timers/promises");
158+
assert.strictEqual(await timersPromises.setTimeout(1, "timeout"), "timeout");
159+
}
160+
161+
export async function testNet() {
162+
const net = await import("node:net");
163+
assert.strictEqual(typeof net, "object");
164+
assert.strictEqual(typeof net.createConnection, "function");
165+
assert.throws(() => net.createServer(), /not implemented/);
152166
}

pnpm-lock.yaml

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)