Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/popular-pigs-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@cloudflare/unenv-preset": minor
---

Use the workerd implementation for Node `net`, `timers`, and `timers/promises` modules

- drop the polyfills
- update `unenv` to 2.0.0-rc.1
4 changes: 2 additions & 2 deletions packages/unenv-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"wrangler": "workspace:*"
},
"peerDependencies": {
"unenv": "2.0.0-rc.0",
"workerd": "^1.20241230.0"
"unenv": "2.0.0-rc.1",
"workerd": "^1.20250124.0"
},
"peerDependenciesMeta": {
"workerd": {
Expand Down
16 changes: 11 additions & 5 deletions packages/unenv-preset/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { version } from "../package.json";
import type { Preset } from "unenv";

// 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 = [
//
// Last checked: 2025-01-24
//
// NOTE: Please sync any changes to `testNodeCompatModules`.
const nodeCompatModules = [
"_stream_duplex",
"_stream_passthrough",
"_stream_readable",
Expand All @@ -18,6 +22,7 @@ const cloudflareNodeCompatModules = [
"dns",
"dns/promises",
"events",
"net",
"path",
"path/posix",
"path/win32",
Expand All @@ -27,6 +32,8 @@ const cloudflareNodeCompatModules = [
"stream/promises",
"stream/web",
"string_decoder",
"timers",
"timers/promises",
"url",
"util/types",
"zlib",
Expand All @@ -39,7 +46,6 @@ const hybridNodeCompatModules = [
"crypto",
"module",
"process",
"timers",
"util",
];

Expand All @@ -51,7 +57,7 @@ export const cloudflare: Preset = {
},
alias: {
...Object.fromEntries(
cloudflareNodeCompatModules.flatMap((p) => [
nodeCompatModules.flatMap((p) => [
[p, p],
[`node:${p}`, `node:${p}`],
])
Expand Down Expand Up @@ -90,5 +96,5 @@ export const cloudflare: Preset = {
],
},
polyfill: [],
external: cloudflareNodeCompatModules.flatMap((p) => [p, `node:${p}`]),
external: nodeCompatModules.flatMap((p) => [p, `node:${p}`]),
};
18 changes: 16 additions & 2 deletions packages/unenv-preset/tests/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import assert from "node:assert";
export const TESTS = {
testCryptoGetRandomValues,
testImplementsBuffer,
testModules,
testNodeCompatModules,
testUtilImplements,
testPath,
testDns,
testTimers,
testNet,
};

export default {
Expand Down Expand Up @@ -74,7 +75,7 @@ async function testImplementsBuffer() {
assert.strictEqual(typeof buffer.resolveObjectURL, "function");
}

async function testModules() {
async function testNodeCompatModules() {
const module = await import("node:module");
// @ts-expect-error exposed by workerd
const require = module.createRequire("/");
Expand All @@ -86,6 +87,7 @@ async function testModules() {
"dns",
"dns/promises",
"events",
"net",
"path",
"path/posix",
"path/win32",
Expand All @@ -95,6 +97,8 @@ async function testModules() {
"stream/promises",
"stream/web",
"string_decoder",
"timers",
"timers/promises",
"url",
"util/types",
"zlib",
Expand Down Expand Up @@ -149,4 +153,14 @@ async function testTimers() {
// active is deprecated and no more in the type
(timers as any).active(timeout);
timers.clearTimeout(timeout);

const timersPromises = await import("node:timers/promises");
assert.strictEqual(await timersPromises.setTimeout(1, "timeout"), "timeout");
}

export async function testNet() {
const net = await import("node:net");
assert.strictEqual(typeof net, "object");
assert.strictEqual(typeof net.createConnection, "function");
assert.throws(() => net.createServer(), /not implemented/);
}
19 changes: 15 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading