From 9fa8622cef7c664d3597f25b7647b1af594e4734 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 3 Dec 2024 18:53:18 +0100 Subject: [PATCH] refactor!: drop experimental vendor presets (#366) --- README.md | 46 --------------------- package.json | 5 +-- src/presets/deno.ts | 77 ----------------------------------- src/presets/index.ts | 2 - src/presets/vercel.ts | 25 ------------ test/cloudflare.ts | 43 -------------------- test/deno.ts | 37 ----------------- test/vercel.ts | 93 ------------------------------------------- 8 files changed, 1 insertion(+), 327 deletions(-) delete mode 100644 src/presets/deno.ts delete mode 100644 src/presets/vercel.ts delete mode 100644 test/cloudflare.ts delete mode 100644 test/deno.ts delete mode 100644 test/vercel.ts diff --git a/README.md b/README.md index c8382462..20c7748e 100644 --- a/README.md +++ b/README.md @@ -81,52 +81,6 @@ import { env, nodeless } from "unenv"; const envConfig = env(nodeless, {}); ``` -### `deno` - -[(view source)](./src/presets/deno.ts) - -This preset can be used to extend `nodeless` to use Deno's Node.js API Compatibility ([docs](https://docs.deno.com/runtime/manual/node/compatibility), [docs](https://docs.deno.com/deploy/api/runtime-node)). - -> [!WARNING] -> This preset is **experimental** and behavior might change! - -```js -import { env, nodeless, deno } from "unenv"; - -const envConfig = env(nodeless, deno, {}); -``` - -### `cloudflare` - -[(view source)](./src/presets/cloudflare.ts) - -This preset can be used to extend `nodeless` to use [Cloudflare Worker](https://workers.cloudflare.com/) Node.js API Compatibility ([docs](https://developers.cloudflare.com/workers/runtime-apis/nodejs/)). - -> [!WARNING] -> This preset is **experimental** and behavior might change! - -> [!NOTE] -> Make sure to enable [`nodejs_compat`](https://developers.cloudflare.com/workers/configuration/compatibility-dates/#nodejs-compatibility-flag) compatibility flag. - -```js -import { env, nodeless, cloudflare } from "unenv"; - -const envConfig = env(nodeless, cloudflare, {}); -``` - -### `vercel` - -This preset can be used to extend `nodeless` to use [Vercel Edge](https://vercel.com/docs/functions/edge-functions/edge-runtime) Node.js API Compatibility ([docs](https://vercel.com/docs/functions/edge-functions/edge-runtime#compatible-node.js-modules)). - -> [!WARNING] -> This preset is **experimental** and behavior might change! - -```js -import { env, nodeless, vercel } from "unenv"; - -const envConfig = env(nodeless, vercel, {}); -``` - ## Built-in Node.js modules `unenv` provides a replacement for Node.js built-in modules compatible with any runtime. diff --git a/package.json b/package.json index d37f4479..3ef0a7f1 100644 --- a/package.json +++ b/package.json @@ -32,13 +32,10 @@ "prepack": "unbuild", "release": "pnpm test && changelogen --release && pnpm publish && git push --follow-tags", "test": "pnpm lint && pnpm test:types && pnpm test:node", - "test:cf": "pnpm jiti test/cloudflare.ts", - "test:deno": "NODE_NO_WARNINGS=1 pnpm jiti test/deno.ts", "test:node": "node --test --import jiti/register ./test/node/test-*", "test:node-coverage": "node test/node-coverage.mjs", "test:node:watch": "node --test --watch --import jiti/register ./test/node/test-*", "test:types": "tsc --noEmit", - "test:vc": "pnpm jiti test/vercel.ts", "test:workerd": "node test/workerd/main.mjs" }, "dependencies": { @@ -64,4 +61,4 @@ "wrangler": "^3.91.0" }, "packageManager": "pnpm@9.14.4" -} \ No newline at end of file +} diff --git a/src/presets/deno.ts b/src/presets/deno.ts deleted file mode 100644 index c551d4e6..00000000 --- a/src/presets/deno.ts +++ /dev/null @@ -1,77 +0,0 @@ -import type { Preset } from "../types"; - -// https://docs.deno.com/runtime/manual/node/compatibility -// https://docs.deno.com/deploy/api/runtime-node -// Last checked: 2023-12-13 -const denoNodeCompatModules = [ - "assert", - "assert/strict", - "async_hooks", - "buffer", - "child_process", - "cluster", - "console", - "constants", - "crypto", - "dgram", - "diagnostics_channel", - "dns", - "dns/promises", - "domain", - "events", - "fs", - "fs/promises", - "http", - "http2", - "https", - "module", - "net", - "os", - "path", - "path/posix", - "path/win32", - "perf_hooks", - "process", - "punycode", - "querystring", - "readline", - "stream", - "stream/consumers", - "stream/promises", - "stream/web", - "string_decoder", - "sys", - "timers", - "timers/promises", - "tls", - "tty", - "url", - "util", - "util/types", - "v8", - "vm", - "worker_threads", - "zlib", -]; - -const denoPreset: Preset = { - alias: { - ...Object.fromEntries(denoNodeCompatModules.map((p) => [p, `node:${p}`])), - ...Object.fromEntries( - denoNodeCompatModules.map((p) => [`node:${p}`, `node:${p}`]), - ), - }, - // Deno's listed globals manually tested against deno@1.38.5 - // TODO: missing BroadcastChannel, PerformanceObserverEntryList, PerformanceResourceTiming - // TODO: global and process - inject: { - setImmediate: "node:timers", - clearImmediate: "node:timers", - Buffer: "node:buffer", - PerformanceObserver: "node:perf_hooks", - }, - polyfill: ["unenv/runtime/polyfill/deno-env"], - external: denoNodeCompatModules.map((p) => `node:${p}`), -}; - -export default denoPreset; diff --git a/src/presets/index.ts b/src/presets/index.ts index 330b6ffd..12efbbff 100644 --- a/src/presets/index.ts +++ b/src/presets/index.ts @@ -1,5 +1,3 @@ export { default as node } from "./node"; export { default as nodeless } from "./nodeless"; -export { default as deno } from "./deno"; export { default as cloudflare } from "./cloudflare"; -export { default as vercel } from "./vercel"; diff --git a/src/presets/vercel.ts b/src/presets/vercel.ts deleted file mode 100644 index 756d9109..00000000 --- a/src/presets/vercel.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { Preset } from "../types"; - -// https://vercel.com/docs/functions/edge-functions/edge-runtime#compatible-node.js-modules -// Last checked: 2023-12-14 -const vercelNodeCompatModules = [ - "async_hooks", - "events", - "buffer", - "assert", - "util", -]; - -const vercelPreset: Preset = { - alias: { - ...Object.fromEntries(vercelNodeCompatModules.map((p) => [p, `node:${p}`])), - ...Object.fromEntries( - vercelNodeCompatModules.map((p) => [`node:${p}`, `node:${p}`]), - ), - }, - inject: {}, - polyfill: [], - external: vercelNodeCompatModules.map((p) => `node:${p}`), -}; - -export default vercelPreset; diff --git a/test/cloudflare.ts b/test/cloudflare.ts deleted file mode 100644 index a77ddd0b..00000000 --- a/test/cloudflare.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { writeFileSync, mkdirSync } from "node:fs"; -import { env, nodeless, cloudflare } from "../src"; -import { - RuntimeTestResult, - analyzeRuntimeTestResult, - genRuntimeTest, - resolveTmp, -} from "./_utils"; - -async function main() { - const _env = env(nodeless, cloudflare); - - const testCode = ` -${genRuntimeTest(_env)} -export default { - async fetch(request, env, ctx) { - return new Response(JSON.stringify(await testRuntime(), null, 2), { - headers: { "content-type": "application/json" } - }); - }, -}; -`; - - mkdirSync(resolveTmp("."), { recursive: true }); - - writeFileSync(resolveTmp("cloudflare.mjs"), testCode); - - const worker = await import("wrangler").then((w) => - w.unstable_dev(resolveTmp("cloudflare.mjs"), { - compatibilityFlags: ["nodejs_compat"], - }), - ); - - const result = (await worker - .fetch("/") - .then((r) => r.json())) as RuntimeTestResult; - await worker.stop(); - - analyzeRuntimeTestResult(result); -} - -// eslint-disable-next-line unicorn/prefer-top-level-await -main(); diff --git a/test/deno.ts b/test/deno.ts deleted file mode 100644 index 30cae848..00000000 --- a/test/deno.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { writeFileSync, mkdirSync, readFileSync } from "node:fs"; -import { execSync } from "node:child_process"; -import { env, nodeless, deno } from "../src"; -import { - RuntimeTestResult, - analyzeRuntimeTestResult, - genRuntimeTest, - resolveTmp, -} from "./_utils"; - -async function main() { - const _env = env(nodeless, deno); - - const testCode = ` -${genRuntimeTest(_env)} -const result = await testRuntime(); -await Deno.writeFile("deno.json", new TextEncoder().encode(JSON.stringify(result, null, 2))); - `; - - mkdirSync(resolveTmp(), { recursive: true }); - - writeFileSync(resolveTmp("deno.mjs"), testCode); - - execSync("deno run -A deno.mjs", { - cwd: resolveTmp(), - stdio: "inherit", - }); - - const result = JSON.parse( - readFileSync(resolveTmp("deno.json"), "utf8"), - ) as RuntimeTestResult; - - analyzeRuntimeTestResult(result); -} - -// eslint-disable-next-line unicorn/prefer-top-level-await -main(); diff --git a/test/vercel.ts b/test/vercel.ts deleted file mode 100644 index e3920c50..00000000 --- a/test/vercel.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { writeFileSync, mkdirSync } from "node:fs"; -import { execSync } from "node:child_process"; -import { env, nodeless, vercel } from "../src"; -import { - RuntimeTestResult, - analyzeRuntimeTestResult, - genRuntimeTest, - resolveTmp, -} from "./_utils"; - -async function main() { - const _env = env(nodeless, vercel); - - const outDir = "vercel/output"; - const funcDir = `${outDir}/functions/__test.func`; - - mkdirSync(resolveTmp(funcDir), { recursive: true }); - - const testCode = ` -${genRuntimeTest(_env)} -export default async function handleEvent(request, event) { - const url = new URL(request.url); - const result = await testRuntime(); - return new Response(JSON.stringify(result, null, 2), { - headers: { - "content-type": "application/json; charset=UTF-8", - }, - }); -} - `; - - writeFileSync(resolveTmp(`${funcDir}/index.mjs`), testCode); - - writeFileSync( - resolveTmp(`${funcDir}/.vc-config.json`), - JSON.stringify( - { - runtime: "edge", - entrypoint: "index.mjs", - }, - null, - 2, - ), - ); - - writeFileSync( - resolveTmp(`${outDir}/config.json`), - JSON.stringify( - { - version: 3, - routes: [ - { - src: "/(.*)", - dest: "/__test", - }, - ], - }, - null, - 2, - ), - ); - - writeFileSync( - resolveTmp(`vercel/package.json`), - JSON.stringify( - { - name: "vercel-edge-test", - private: true, - scripts: { - build: "mkdir -p .vercel && cp -vr output .vercel", - }, - }, - null, - 2, - ), - ); - - await execSync(`bunx vercel --prod`, { - cwd: resolveTmp("vercel"), - stdio: "inherit", - }); - - const deployURL = "https://unenv.vercel.app/"; - - const result = (await fetch("https://unenv.vercel.app/").then((r) => - r.json(), - )) as RuntimeTestResult; - - analyzeRuntimeTestResult(result); -} - -// eslint-disable-next-line unicorn/prefer-top-level-await -main();