diff --git a/.changeset/popular-pigs-glow.md b/.changeset/popular-pigs-glow.md new file mode 100644 index 000000000000..7d328e17efc2 --- /dev/null +++ b/.changeset/popular-pigs-glow.md @@ -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 diff --git a/packages/unenv-preset/package.json b/packages/unenv-preset/package.json index a8d6f9e8d995..d7ef24842640 100644 --- a/packages/unenv-preset/package.json +++ b/packages/unenv-preset/package.json @@ -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": { diff --git a/packages/unenv-preset/src/preset.ts b/packages/unenv-preset/src/preset.ts index b936159f2b2e..ca798d4b522c 100644 --- a/packages/unenv-preset/src/preset.ts +++ b/packages/unenv-preset/src/preset.ts @@ -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", @@ -18,6 +22,7 @@ const cloudflareNodeCompatModules = [ "dns", "dns/promises", "events", + "net", "path", "path/posix", "path/win32", @@ -27,6 +32,8 @@ const cloudflareNodeCompatModules = [ "stream/promises", "stream/web", "string_decoder", + "timers", + "timers/promises", "url", "util/types", "zlib", @@ -39,7 +46,6 @@ const hybridNodeCompatModules = [ "crypto", "module", "process", - "timers", "util", ]; @@ -51,7 +57,7 @@ export const cloudflare: Preset = { }, alias: { ...Object.fromEntries( - cloudflareNodeCompatModules.flatMap((p) => [ + nodeCompatModules.flatMap((p) => [ [p, p], [`node:${p}`, `node:${p}`], ]) @@ -90,5 +96,5 @@ export const cloudflare: Preset = { ], }, polyfill: [], - external: cloudflareNodeCompatModules.flatMap((p) => [p, `node:${p}`]), + external: nodeCompatModules.flatMap((p) => [p, `node:${p}`]), }; diff --git a/packages/unenv-preset/tests/worker/index.ts b/packages/unenv-preset/tests/worker/index.ts index d851ca38dd8f..44926d0fd5c6 100644 --- a/packages/unenv-preset/tests/worker/index.ts +++ b/packages/unenv-preset/tests/worker/index.ts @@ -5,11 +5,12 @@ import assert from "node:assert"; export const TESTS = { testCryptoGetRandomValues, testImplementsBuffer, - testModules, + testNodeCompatModules, testUtilImplements, testPath, testDns, testTimers, + testNet, }; export default { @@ -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("/"); @@ -86,6 +87,7 @@ async function testModules() { "dns", "dns/promises", "events", + "net", "path", "path/posix", "path/win32", @@ -95,6 +97,8 @@ async function testModules() { "stream/promises", "stream/web", "string_decoder", + "timers", + "timers/promises", "url", "util/types", "zlib", @@ -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/); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1358803dfcb3..2321391ca7e4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1669,11 +1669,11 @@ importers: packages/unenv-preset: dependencies: unenv: - specifier: 2.0.0-rc.0 - version: 2.0.0-rc.0 + specifier: 2.0.0-rc.1 + version: 2.0.0-rc.1 workerd: - specifier: ^1.20241230.0 - version: 1.20241230.0 + specifier: ^1.20250124.0 + version: 1.20250124.0 devDependencies: '@types/node-unenv': specifier: npm:@types/node@^22.10.5 @@ -10572,6 +10572,9 @@ packages: unenv@2.0.0-rc.0: resolution: {integrity: sha512-H0kl2w8jFL/FAk0xvjVing4bS3jd//mbg1QChDnn58l9Sc5RtduaKmLAL8n+eBw5jJo8ZjYV7CrEGage5LAOZQ==} + unenv@2.0.0-rc.1: + resolution: {integrity: sha512-PU5fb40H8X149s117aB4ytbORcCvlASdtF97tfls4BPIyj4PeVxvpSuy1jAptqYHqB0vb2w2sHvzM0XWcp2OKg==} + unicorn-magic@0.1.0: resolution: {integrity: sha512-lRfVq8fE8gz6QMBuDM6a+LO3IAzTi05H6gCVaUpir2E1Rwpo4ZUog45KpNXKC/Mn3Yb9UDuHumeFTo9iV/D9FQ==} engines: {node: '>=18'} @@ -20032,6 +20035,14 @@ snapshots: pathe: 1.1.2 ufo: 1.5.4 + unenv@2.0.0-rc.1: + dependencies: + defu: 6.1.4 + mlly: 1.7.4 + ohash: 1.1.4 + pathe: 1.1.2 + ufo: 1.5.4 + unicorn-magic@0.1.0: {} universal-user-agent@6.0.1: {}