diff --git a/.gitignore b/.gitignore index 32206f8..f4437d0 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ coverage/ # Astro (docs site) .astro/ website/dist/ + +# Edge (Miniflare) test bundle artifact +test/edge/_worker.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0efc786..45124f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ green-tea uses calendar versioning: `YY.MM.PATCH`. (WebSocket and mesh remain Node-only). - **Deno adapter** (`@green-tea/core/deno`): `serveDeno(app)` runs HTTP + SSE + WebSocket on Deno. - **Bun adapter** (`@green-tea/core/bun`): `serveBun(app)` runs HTTP + SSE + WebSocket on Bun, reusing the neutral `app.upgrade` / `WsSocket` capability. WebSocket, rooms, and channels behave identically to Node and Deno. +- **Cloudflare Workers / edge adapter** (`@green-tea/core/edge`): `edgeHandler(app)` runs HTTP + SSE + WebSocket on workerd, reusing the neutral `app.upgrade` / `WsSocket` capability. Requires the `nodejs_compat` compatibility flag. Green Tea now runs on Node, Deno, Bun, and the edge — with identical WebSocket, rooms, and channel behaviour on all four. - **`app.upgrade(request, socket)`**: neutral WebSocket entry point for non-Node runtimes, built on a shared `WsSocket` capability. WebSocket logic is now runtime-agnostic (`src/http/ws-core.ts`). ### Changed diff --git a/package-lock.json b/package-lock.json index a78fbe7..383ec9e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@green-tea/core", - "version": "0.0.0", + "version": "26.7.0-beta.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@green-tea/core", - "version": "0.0.0", + "version": "26.7.0-beta.0", "license": "MIT", "dependencies": { "reflect-metadata": "0.2.2" @@ -30,6 +30,7 @@ "express": "^5.2.1", "fastify": "^5.10.0", "graphql": "^16.9.0", + "miniflare": "^4.20260708.1", "prettier": "^3.9.4", "rxjs": "^7.8.2", "tsup": "^8.5.1", @@ -39,6 +40,9 @@ "vitest": "^2.0.0", "ws": "^8.21.0" }, + "engines": { + "node": ">=18" + }, "peerDependencies": { "busboy": "^1.6.0", "ws": "^8.18.0" @@ -70,6 +74,91 @@ "url": "https://github.com/sponsors/Borewit" } }, + "node_modules/@cloudflare/workerd-darwin-64": { + "version": "1.20260708.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-64/-/workerd-darwin-64-1.20260708.1.tgz", + "integrity": "sha512-HXFCvhS1wpg3uXO0CLUwmwC41i2loM5FSK69EUchOBpmYBAXxT1oHLm6EOA5lqhTk5Mu9kjRiQYxa1GwKPwfJg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-darwin-arm64": { + "version": "1.20260708.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-arm64/-/workerd-darwin-arm64-1.20260708.1.tgz", + "integrity": "sha512-JVlJaKDoRTVKSroHIlf8g3UCPjKj4iDbMZE2CNYht5qQ+2rL0FAUiVlV82G3BqKnnw9kHYnnsMzC08b9zVtdzA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-linux-64": { + "version": "1.20260708.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-64/-/workerd-linux-64-1.20260708.1.tgz", + "integrity": "sha512-3daE60YdD7YX0Jtuzc9DE/r/qMkmx8ZvHTkF8Mzmp3F5tbzlV0DAzmu5PFUPF2WuvtKbAhZKbvC2cHmWpQYxnA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-linux-arm64": { + "version": "1.20260708.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-arm64/-/workerd-linux-arm64-1.20260708.1.tgz", + "integrity": "sha512-VLdNYOx5Hj+9C6isy0ACWZsbMtSxex2DIJWEe7cZxUdlphZ58ZT8zxNXK8yunFiowd34hn3VwGMopdvdj8lvmA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-windows-64": { + "version": "1.20260708.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-windows-64/-/workerd-windows-64-1.20260708.1.tgz", + "integrity": "sha512-bC/aSAwLy16Vjo24i9XU3aWH+eRgz7NeR5xPKavGbembO18ZywYTQbXh14eXtY6fAqN3RzRG8psijTdhX4xydA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16" + } + }, "node_modules/@colors/colors": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz", @@ -81,6 +170,41 @@ "node": ">=0.1.90" } }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@emnapi/runtime": { + "version": "1.11.2", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.2.tgz", + "integrity": "sha512-kyOl3X0DuTiT1h2ft8r2fYO8JYtU9a9Xis/zBSiGArNaagCOWx90N1k2wxp18czFDH+OgcWGb5ZP/XMt3dcyPA==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.21.5", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz", @@ -950,6 +1074,496 @@ "url": "https://github.com/sponsors/nzakas" } }, + "node_modules/@img/colour": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz", + "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/sharp-darwin-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.34.5.tgz", + "integrity": "sha512-imtQ3WMJXbMY4fxb/Ndp6HBTNVtWCUI0WdobyheGf5+ad6xX8VIDO8u2xE4qc/fr08CKG/7dDseFtn6M6g/r3w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-darwin-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.34.5.tgz", + "integrity": "sha512-YNEFAF/4KQ/PeW0N+r+aVVsoIY0/qxxikF2SWdp+NRkmMB7y9LBZAVqQ4yhGCm/H3H270OSykqmQMKLBhBJDEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-libvips-darwin-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.2.4.tgz", + "integrity": "sha512-zqjjo7RatFfFoP0MkQ51jfuFZBnVE2pRiaydKJ1G/rHZvnsrHAOcQALIi9sA5co5xenQdTugCvtb1cuf78Vf4g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.2.4.tgz", + "integrity": "sha512-1IOd5xfVhlGwX+zXv2N93k0yMONvUlANylbJw1eTah8K/Jtpi15KC+WSiaX/nBmbm2HxRM1gZ0nSdjSsrZbGKg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.2.4.tgz", + "integrity": "sha512-bFI7xcKFELdiNCVov8e44Ia4u2byA+l3XtsAj+Q8tfCwO6BQ8iDojYdvoPMqsKDkuoOo+X6HZA0s0q11ANMQ8A==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.2.4.tgz", + "integrity": "sha512-excjX8DfsIcJ10x1Kzr4RcWe1edC9PquDRRPx3YVCvQv+U5p7Yin2s32ftzikXojb1PIFc/9Mt28/y+iRklkrw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-ppc64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.2.4.tgz", + "integrity": "sha512-FMuvGijLDYG6lW+b/UvyilUWu5Ayu+3r2d1S8notiGCIyYU/76eig1UfMmkZ7vwgOrzKzlQbFSuQfgm7GYUPpA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-riscv64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.2.4.tgz", + "integrity": "sha512-oVDbcR4zUC0ce82teubSm+x6ETixtKZBh/qbREIOcI3cULzDyb18Sr/Wcyx7NRQeQzOiHTNbZFF1UwPS2scyGA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-s390x": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.2.4.tgz", + "integrity": "sha512-qmp9VrzgPgMoGZyPvrQHqk02uyjA0/QrTO26Tqk6l4ZV0MPWIW6LTkqOIov+J1yEu7MbFQaDpwdwJKhbJvuRxQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.2.4.tgz", + "integrity": "sha512-tJxiiLsmHc9Ax1bz3oaOYBURTXGIRDODBqhveVHonrHJ9/+k89qbLl0bcJns+e4t4rvaNBxaEZsFtSfAdquPrw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.2.4.tgz", + "integrity": "sha512-FVQHuwx1IIuNow9QAbYUzJ+En8KcVm9Lk5+uGUQJHaZmMECZmOlix9HnH7n1TRkXMS0pGxIJokIVB9SuqZGGXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.2.4.tgz", + "integrity": "sha512-+LpyBk7L44ZIXwz/VYfglaX/okxezESc6UxDSoyo2Ks6Jxc4Y7sGjpgU9s4PMgqgjj1gZCylTieNamqA1MF7Dg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-linux-arm": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.34.5.tgz", + "integrity": "sha512-9dLqsvwtg1uuXBGZKsxem9595+ujv0sJ6Vi8wcTANSFpwV/GONat5eCkzQo/1O6zRIkh0m/8+5BjrRr7jDUSZw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.34.5.tgz", + "integrity": "sha512-bKQzaJRY/bkPOXyKx5EVup7qkaojECG6NLYswgktOZjaXecSAeCWiZwwiFf3/Y+O1HrauiE3FVsGxFg8c24rZg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-ppc64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.34.5.tgz", + "integrity": "sha512-7zznwNaqW6YtsfrGGDA6BRkISKAAE1Jo0QdpNYXNMHu2+0dTrPflTLNkpc8l7MUP5M16ZJcUvysVWWrMefZquA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-ppc64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-riscv64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.34.5.tgz", + "integrity": "sha512-51gJuLPTKa7piYPaVs8GmByo7/U7/7TZOq+cnXJIHZKavIRHAP77e3N2HEl3dgiqdD/w0yUfiJnII77PuDDFdw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-riscv64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-s390x": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.34.5.tgz", + "integrity": "sha512-nQtCk0PdKfho3eC5MrbQoigJ2gd1CgddUMkabUj+rBevs8tZ2cULOx46E7oyX+04WGfABgIwmMC0VqieTiR4jg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-s390x": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.34.5.tgz", + "integrity": "sha512-MEzd8HPKxVxVenwAa+JRPwEC7QFjoPWuS5NZnBt6B3pu7EG2Ge0id1oLHZpPJdn3OQK+BQDiw9zStiHBTJQQQQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-linuxmusl-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.34.5.tgz", + "integrity": "sha512-fprJR6GtRsMt6Kyfq44IsChVZeGN97gTD331weR1ex1c1rypDEABN6Tm2xa1wE6lYb5DdEnk03NZPqA7Id21yg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-linuxmusl-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.34.5.tgz", + "integrity": "sha512-Jg8wNT1MUzIvhBFxViqrEhWDGzqymo3sV7z7ZsaWbZNDLXRJZoRGrjulp60YYtV4wfY8VIKcWidjojlLcWrd8Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-wasm32": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.34.5.tgz", + "integrity": "sha512-OdWTEiVkY2PHwqkbBI8frFxQQFekHaSSkUIJkwzclWZe64O1X4UlUjqqqLaPbUpMOQk6FBu/HtlGXNblIs0huw==", + "cpu": [ + "wasm32" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "optional": true, + "dependencies": { + "@emnapi/runtime": "^1.7.0" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.34.5.tgz", + "integrity": "sha512-WQ3AgWCWYSb2yt+IG8mnC6Jdk9Whs7O0gxphblsLvdhSpSTtmu69ZG1Gkb6NuvxsNACwiPV6cNSZNzt0KPsw7g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-ia32": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.34.5.tgz", + "integrity": "sha512-FV9m/7NmeCmSHDD5j4+4pNI8Cp3aW+JvLoXcTUo0IqyjSfAZJ8dIUmijx1qaJsIiU+Hosw6xM5KijAWRJCSgNg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.34.5.tgz", + "integrity": "sha512-+29YMsqY2/9eFEiW93eqWnuLcWcufowXewwSNIT6UwZdUUCrM3oFjMWH/Z6/TMmb4hlFenmfAVbpWeup2jryCw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, "node_modules/@isaacs/cliui": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz", @@ -1296,6 +1910,48 @@ "node": ">=14" } }, + "node_modules/@poppinss/colors": { + "version": "4.1.6", + "resolved": "https://registry.npmjs.org/@poppinss/colors/-/colors-4.1.6.tgz", + "integrity": "sha512-H9xkIdFswbS8n1d6vmRd8+c10t2Qe+rZITbbDHHkQixH5+2x1FDGmi/0K+WgWiqQFKPSlIYB7jlH6Kpfn6Fleg==", + "dev": true, + "license": "MIT", + "dependencies": { + "kleur": "^4.1.5" + } + }, + "node_modules/@poppinss/dumper": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/@poppinss/dumper/-/dumper-0.6.5.tgz", + "integrity": "sha512-NBdYIb90J7LfOI32dOewKI1r7wnkiH6m920puQ3qHUeZkxNkQiFnXVWoE6YtFSv6QOiPPf7ys6i+HWWecDz7sw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/colors": "^4.1.5", + "@sindresorhus/is": "^7.0.2", + "supports-color": "^10.0.0" + } + }, + "node_modules/@poppinss/dumper/node_modules/supports-color": { + "version": "10.2.2", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-10.2.2.tgz", + "integrity": "sha512-SS+jx45GF1QjgEXQx4NJZV9ImqmO2NPz5FNsIHrsDjh2YsHnawpan7SNQ1o8NuhrbHZy9AZhIoCUiCeaW/C80g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/@poppinss/exception": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@poppinss/exception/-/exception-1.2.3.tgz", + "integrity": "sha512-dCED+QRChTVatE9ibtoaxc+WkdzOSjYTKi/+uacHWIsfodVfpsueo3+DKpgU5Px8qXjgmXkSvhXvSCz3fnP9lw==", + "dev": true, + "license": "MIT" + }, "node_modules/@rollup/rollup-android-arm-eabi": { "version": "4.62.2", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.62.2.tgz", @@ -1646,6 +2302,26 @@ "win32" ] }, + "node_modules/@sindresorhus/is": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-7.2.0.tgz", + "integrity": "sha512-P1Cz1dWaFfR4IR+U13mqqiGsLFf1KbayybWwdd2vfctdV6hDpUkgCY0nKOLLTMSoRd/jJNjtbqzf13K8DCCXQw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sindresorhus/is?sponsor=1" + } + }, + "node_modules/@speed-highlight/core": { + "version": "1.2.17", + "resolved": "https://registry.npmjs.org/@speed-highlight/core/-/core-1.2.17.tgz", + "integrity": "sha512-Z92FwKpCtfaW1V0jTU/fh3QzYEZN8wDwrzRIBoADCJfn4mJCNcJN/XegifX7BDrQ8/h9Xh/JnbyMchL0FqXrkg==", + "dev": true, + "license": "CC0-1.0" + }, "node_modules/@stylistic/eslint-plugin": { "version": "5.10.0", "resolved": "https://registry.npmjs.org/@stylistic/eslint-plugin/-/eslint-plugin-5.10.0.tgz", @@ -3064,6 +3740,16 @@ "node": ">=6" } }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, "node_modules/dunder-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", @@ -3155,6 +3841,16 @@ "node": ">= 0.8" } }, + "node_modules/error-stack-parser-es": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/error-stack-parser-es/-/error-stack-parser-es-1.0.5.tgz", + "integrity": "sha512-5qucVt2XcuGMcEGgWI7i+yZpmpByQ8J1lHhcL7PwqCwu9FPP3VUXzT4ltHe5i2z9dePwEHcDVOAfSnHsOlCXRA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, "node_modules/es-define-property": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", @@ -4515,6 +5211,16 @@ "json-buffer": "3.0.1" } }, + "node_modules/kleur": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz", + "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -4767,6 +5473,27 @@ "url": "https://opencollective.com/express" } }, + "node_modules/miniflare": { + "version": "4.20260708.1", + "resolved": "https://registry.npmjs.org/miniflare/-/miniflare-4.20260708.1.tgz", + "integrity": "sha512-c94O9zRDISdqO18EHt6l0iF/fWgWt8p18PJvRsA/L/NJZ9Cfke3s/F5Blg1XXF7WDutVRzWVWy8Vy4LaT5ifsA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@cspotcode/source-map-support": "0.8.1", + "sharp": "0.34.5", + "undici": "7.28.0", + "workerd": "1.20260708.1", + "ws": "8.21.0", + "youch": "4.1.0-beta.10" + }, + "bin": { + "miniflare": "bootstrap.js" + }, + "engines": { + "node": ">=22.0.0" + } + }, "node_modules/minimatch": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.5.tgz", @@ -5876,6 +6603,51 @@ "dev": true, "license": "ISC" }, + "node_modules/sharp": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.34.5.tgz", + "integrity": "sha512-Ou9I5Ft9WNcCbXrU9cMgPBcCK8LiwLqcbywW3t4oDV37n1pzpuNLsYiAV8eODnjbtQlSDwZ2cUEeQz4E54Hltg==", + "dev": true, + "hasInstallScript": true, + "license": "Apache-2.0", + "dependencies": { + "@img/colour": "^1.0.0", + "detect-libc": "^2.1.2", + "semver": "^7.7.3" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "0.34.5", + "@img/sharp-darwin-x64": "0.34.5", + "@img/sharp-libvips-darwin-arm64": "1.2.4", + "@img/sharp-libvips-darwin-x64": "1.2.4", + "@img/sharp-libvips-linux-arm": "1.2.4", + "@img/sharp-libvips-linux-arm64": "1.2.4", + "@img/sharp-libvips-linux-ppc64": "1.2.4", + "@img/sharp-libvips-linux-riscv64": "1.2.4", + "@img/sharp-libvips-linux-s390x": "1.2.4", + "@img/sharp-libvips-linux-x64": "1.2.4", + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", + "@img/sharp-libvips-linuxmusl-x64": "1.2.4", + "@img/sharp-linux-arm": "0.34.5", + "@img/sharp-linux-arm64": "0.34.5", + "@img/sharp-linux-ppc64": "0.34.5", + "@img/sharp-linux-riscv64": "0.34.5", + "@img/sharp-linux-s390x": "0.34.5", + "@img/sharp-linux-x64": "0.34.5", + "@img/sharp-linuxmusl-arm64": "0.34.5", + "@img/sharp-linuxmusl-x64": "0.34.5", + "@img/sharp-wasm32": "0.34.5", + "@img/sharp-win32-arm64": "0.34.5", + "@img/sharp-win32-ia32": "0.34.5", + "@img/sharp-win32-x64": "0.34.5" + } + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -7546,6 +8318,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/undici": { + "version": "7.28.0", + "resolved": "https://registry.npmjs.org/undici/-/undici-7.28.0.tgz", + "integrity": "sha512-cRZYrTDwWznlnRiPjggAGxZXanty6M8RV1ff8Wm4LWXBp7/IG8v5DnOm74DtUBp9OONpK75YlPnIjQqX0dBDtA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.18.1" + } + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", @@ -7800,6 +8582,27 @@ "node": ">=0.10.0" } }, + "node_modules/workerd": { + "version": "1.20260708.1", + "resolved": "https://registry.npmjs.org/workerd/-/workerd-1.20260708.1.tgz", + "integrity": "sha512-WAK+Kt/VVCSldH2qSr8lx46XCJ4Q+bdlHNaFqUtOHthBEIB8C1N8HVW+VOLrxDoTCk0NGNv0zajnBeQK4JOB9w==", + "dev": true, + "hasInstallScript": true, + "license": "Apache-2.0", + "bin": { + "workerd": "bin/workerd" + }, + "engines": { + "node": ">=16" + }, + "optionalDependencies": { + "@cloudflare/workerd-darwin-64": "1.20260708.1", + "@cloudflare/workerd-darwin-arm64": "1.20260708.1", + "@cloudflare/workerd-linux-64": "1.20260708.1", + "@cloudflare/workerd-linux-arm64": "1.20260708.1", + "@cloudflare/workerd-windows-64": "1.20260708.1" + } + }, "node_modules/wrap-ansi": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz", @@ -7962,6 +8765,45 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/youch": { + "version": "4.1.0-beta.10", + "resolved": "https://registry.npmjs.org/youch/-/youch-4.1.0-beta.10.tgz", + "integrity": "sha512-rLfVLB4FgQneDr0dv1oddCVZmKjcJ6yX6mS4pU82Mq/Dt9a3cLZQ62pDBL4AUO+uVrCvtWz3ZFUL2HFAFJ/BXQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/colors": "^4.1.5", + "@poppinss/dumper": "^0.6.4", + "@speed-highlight/core": "^1.2.7", + "cookie": "^1.0.2", + "youch-core": "^0.3.3" + } + }, + "node_modules/youch-core": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/youch-core/-/youch-core-0.3.3.tgz", + "integrity": "sha512-ho7XuGjLaJ2hWHoK8yFnsUGy2Y5uDpqSTq1FkHLK4/oqKtyUU1AFbOOxY4IpC9f0fTLjwYbslUz0Po5BpD1wrA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/exception": "^1.2.2", + "error-stack-parser-es": "^1.0.5" + } + }, + "node_modules/youch/node_modules/cookie": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-1.1.1.tgz", + "integrity": "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } } } } diff --git a/package.json b/package.json index 7f29f35..b17be00 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,16 @@ "types": "./dist/bun.d.ts", "default": "./dist/bun.js" } + }, + "./edge": { + "import": { + "types": "./dist/edge.d.mts", + "default": "./dist/edge.mjs" + }, + "require": { + "types": "./dist/edge.d.ts", + "default": "./dist/edge.js" + } } }, "files": [ @@ -71,6 +81,7 @@ "test:dev": "vitest", "test:deno": "deno test --allow-net --allow-read --no-check test/deno/", "test:bun": "bun test test/bun/", + "test:edge": "tsx test/edge/run.ts", "typecheck": "tsc --noEmit -p tsconfig.json", "typecheck:bench": "tsc --noEmit -p bench/tsconfig.json", "lint": "eslint src", @@ -120,6 +131,7 @@ "express": "^5.2.1", "fastify": "^5.10.0", "graphql": "^16.9.0", + "miniflare": "^4.20260708.1", "prettier": "^3.9.4", "rxjs": "^7.8.2", "tsup": "^8.5.1", diff --git a/src/app/index.ts b/src/app/index.ts index 6976798..0d1c0b7 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -1,4 +1,4 @@ -import http from 'http'; +import type http from 'http'; import { Bus } from '../bus'; import { Container } from '../container'; import { topoSort, subgraphFor, GraphNode, nearest } from '../graph'; diff --git a/src/edge.ts b/src/edge.ts new file mode 100644 index 0000000..ec4780c --- /dev/null +++ b/src/edge.ts @@ -0,0 +1,108 @@ +import { channel } from './channel'; +import type { App } from './app/types'; +import type { WsSocket, WsRequest } from './http/ws-core'; + +// Minimal ambient for the Cloudflare Workers globals we use — avoids @cloudflare/workers-types. +// Declares only what we call; mirrors the real workerd signatures. +interface EdgeWebSocket { + accept(): void; + addEventListener( + type: 'message' | 'close' | 'error', + listener: (event: { data?: string | ArrayBuffer }) => void, + ): void; + send(data: string): void; + close(code?: number, reason?: string): void; + readyState: number; +} +interface EdgeCtx { + waitUntil(promise: Promise): void; +} +declare const WebSocketPair: { new (): { 0: EdgeWebSocket; 1: EdgeWebSocket } }; + +const decoder = new TextDecoder(); + +/** Builds a neutral WsRequest from a Cloudflare Worker request. */ +function toWsRequest(request: Request): WsRequest { + const url = new URL(request.url); + const headers: Record = {}; + request.headers.forEach((v, k) => { + headers[k] = v; + }); + return { + url: url.pathname + url.search, + headers, + protocol: url.protocol === 'https:' ? 'https' : 'http', + ip: request.headers.get('cf-connecting-ip') ?? '', + }; +} + +/** Wraps a Cloudflare server-side WebSocket (already accept()ed) as a neutral WsSocket. */ +function edgeSocket(server: EdgeWebSocket): WsSocket { + const inbound = channel(); + const ac = new AbortController(); + server.addEventListener('message', (event) => { + const data = event.data; + inbound.push(typeof data === 'string' ? data : decoder.decode(data as ArrayBuffer)); + }); + server.addEventListener('close', () => { + inbound.close(); + ac.abort(); + }); + server.addEventListener('error', () => { + inbound.fail(new Error('websocket error')); + ac.abort(); + }); + return { + inbound, + abort: ac.signal, + get isOpen() { + return server.readyState === 1; // 1 = OPEN + }, + send: (data) => { + server.send(data); + }, + close: (code, reason) => { + try { + server.close(code, reason); + } catch { + /* already closed */ + } + }, + terminate: () => { + try { + server.close(); + } catch { + /* already closed */ + } + }, + }; +} + +/** + * Cloudflare Workers / edge handler: HTTP + SSE via app.fetch, WebSocket via WebSocketPair. + * Use as `export default { fetch: edgeHandler(app) }`. Requires the `nodejs_compat` + * compatibility flag. The WS lifecycle runs on ctx.waitUntil so it survives past the 101 response. + */ +export function edgeHandler( + app: App, +): (request: Request, env?: unknown, ctx?: EdgeCtx) => Response | Promise { + return (request, _env, ctx) => { + if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') { + const [client, server] = Object.values(new WebSocketPair()) as [EdgeWebSocket, EdgeWebSocket]; + server.accept(); + const lifecycle = app.upgrade(toWsRequest(request), edgeSocket(server)).catch(() => { + try { + server.close(1011, 'internal error'); + } catch { + /* already closed */ + } + }); + if (ctx?.waitUntil) ctx.waitUntil(lifecycle); + else void lifecycle; + + return new Response(null, { status: 101, webSocket: client } as ResponseInit & { webSocket: EdgeWebSocket }); + } + + return app.fetch(request); + }; +} diff --git a/src/http/request.ts b/src/http/request.ts index 2eef0cd..b6fc751 100644 --- a/src/http/request.ts +++ b/src/http/request.ts @@ -1,4 +1,4 @@ -import http from 'http'; +import type http from 'http'; import { HttpError } from '../signals'; /** Reads the full request body, enforcing the max-bytes ceiling (throws 413 if exceeded). */ diff --git a/src/http/server.ts b/src/http/server.ts index 27ec76a..b54f2a4 100644 --- a/src/http/server.ts +++ b/src/http/server.ts @@ -1,5 +1,5 @@ -import http from 'http'; -import https from 'https'; +import type http from 'http'; +import type https from 'https'; import { renderError, type ErrorRequest } from '../transformers'; import { isHttpError, HttpError } from '../signals'; import type { Bus } from '../bus'; @@ -48,10 +48,17 @@ export function createHttpServer( const trustProxy = opts?.trustProxy ?? false; const handler = createRequestHandler({ routes, bus, opts, maxBody, trustProxy }); + // Node http/https are loaded lazily so importing createApp stays edge/workerd-safe + // (workerd's nodejs_compat provides no node:http/node:https). This path only runs under listen(). + // eslint-disable-next-line @typescript-eslint/no-require-imports + const nodeHttp = require('http') as typeof import('http'); + // eslint-disable-next-line @typescript-eslint/no-require-imports + const nodeHttps = require('https') as typeof import('https'); + // runtime-compatible; only @types differ (https.Server lacks the http-only timeout props) const server: http.Server = (opts?.tls - ? https.createServer(opts.tls as https.ServerOptions, handler) - : http.createServer(handler)) as unknown as http.Server; + ? nodeHttps.createServer(opts.tls as https.ServerOptions, handler) + : nodeHttp.createServer(handler)) as unknown as http.Server; server.requestTimeout = opts?.limits?.requestTimeoutMs ?? 30_000; server.headersTimeout = opts?.limits?.headersTimeoutMs ?? 10_000; server.keepAliveTimeout = opts?.limits?.keepAliveTimeoutMs ?? 5_000; diff --git a/src/http/stream.ts b/src/http/stream.ts index 1239459..9532fa2 100644 --- a/src/http/stream.ts +++ b/src/http/stream.ts @@ -1,4 +1,4 @@ -import http from 'http'; +import type http from 'http'; import { once } from 'events'; import { sseEncoder, ndjsonEncoder, StreamEncoder } from '../encoders'; import type { Transport } from '../metadata'; diff --git a/src/http/types.ts b/src/http/types.ts index ddd02db..a12c1e4 100644 --- a/src/http/types.ts +++ b/src/http/types.ts @@ -1,4 +1,4 @@ -import http from 'http'; +import type http from 'http'; import type { Transport } from '../metadata'; import type { PipelineResult } from '../pipeline'; import type { ErrorRenderer } from '../transformers'; diff --git a/src/http/ws.ts b/src/http/ws.ts index 5f0ae62..2f34b8a 100644 --- a/src/http/ws.ts +++ b/src/http/ws.ts @@ -1,4 +1,4 @@ -import http from 'http'; +import type http from 'http'; import { channel } from '../channel'; import type { Bus } from '../bus'; import { runWsConnection, matchWsRoute, type WsSocket, type WsRequest } from './ws-core'; diff --git a/test/edge/run.ts b/test/edge/run.ts new file mode 100644 index 0000000..58dc066 --- /dev/null +++ b/test/edge/run.ts @@ -0,0 +1,96 @@ +// Real Miniflare (workerd) integration test for edgeHandler: bundles test/edge/worker.ts +// for the Cloudflare Workers runtime and boots it in the actual workerd engine (via the +// `miniflare` npm package — no CF account/deploy needed), then round-trips HTTP + WS +// through the real graph. +// +// Run with: npm run test:edge +// +// NODEJS_COMPAT: workerd's `nodejs_compat` genuinely does NOT implement `node:http`/ +// `node:https` (confirmed in isolation: `node:crypto`, `node:stream`, `node:net`, `node:tls`, +// `node:events`, `node:util`, `node:buffer` all load fine under the same flags/compat date; +// only `http`/`https` fail with "Uncaught Error: No such module 'node:http'" at worker LOAD +// time) — a real platform gap, not a bundler misconfiguration. The barrel used to statically +// VALUE-import `http`/`https` for the Node `listen()` path even though `edgeHandler` never +// touches them, which broke worker load with no shim. The fix (see src/http/server.ts, +// src/app/index.ts, and the other src/http/*.ts files) converts every `http`/`https` import +// to `import type` (zero runtime code) and lazy-`require()`s them only inside +// `createHttpServer`, which the edge path never calls. That means the real barrel now loads +// on workerd with no test-time stub — this run exercises it as-is. +import { strict as assert } from 'node:assert'; +import { build } from 'esbuild'; +import { Miniflare } from 'miniflare'; + +const BUNDLE = 'test/edge/_worker.mjs'; + +async function main() { + // Bundle the worker for workerd: ESM, Node builtins left external + aliased to node:* + // so workerd's nodejs_compat provides them (not esbuild). `http`/`https` are no longer + // imported as VALUES by the barrel at module-eval time (see comment above) — they're now + // `import type` (erased) plus a lazy `require()` reached only from `createHttpServer`, + // which `edgeHandler` never calls. esbuild still needs a resolution for that bare + // `require('http')`/`require('https')` text even though it's dead code on this path, so + // they're marked external here (left unresolved, exactly like the `node:*` builtins) rather + // than stubbed — no fake implementation is provided, and the real barrel is what's tested. + await build({ + entryPoints: ['test/edge/worker.ts'], + bundle: true, + format: 'esm', + outfile: BUNDLE, + platform: 'browser', // workerd is not node; keep esbuild from injecting node shims + target: 'esnext', + // Leave Node builtins for workerd's nodejs_compat to resolve; http/https are external + // too (unresolved, never called by edgeHandler) since workerd has no node:http/https. + external: ['node:*', 'http', 'https'], + alias: { + crypto: 'node:crypto', + stream: 'node:stream', + zlib: 'node:zlib', + buffer: 'node:buffer', + events: 'node:events', + net: 'node:net', + tls: 'node:tls', + util: 'node:util', + }, + }); + + const mf = new Miniflare({ + modules: true, + scriptPath: BUNDLE, + compatibilityDate: '2024-11-01', + compatibilityFlags: ['nodejs_compat'], + }); + + try { + // HTTP + const httpRes = await mf.dispatchFetch('http://localhost/rt/hello'); + assert.equal(httpRes.status, 200); + assert.deepEqual(await httpRes.json(), { ok: true }); + + // WebSocket + const wsRes = await mf.dispatchFetch('http://localhost/rt/echo', { + headers: { Upgrade: 'websocket' }, + }); + assert.equal(wsRes.status, 101); + const ws = wsRes.webSocket; + assert.ok(ws, 'expected a client webSocket on the 101 response'); + ws.accept(); + const got = await new Promise((resolve, reject) => { + ws.addEventListener('message', (e: { data: unknown }) => { + resolve(String(e.data)); + ws.close(); + }); + ws.addEventListener('error', () => reject(new Error('ws error'))); + ws.send('hi'); + }); + assert.equal(got, 'echo:hi'); + + console.log('edge (workerd via miniflare): HTTP 200 + WS echo:hi — PASS'); + } finally { + await mf.dispose(); + } +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/test/edge/worker.ts b/test/edge/worker.ts new file mode 100644 index 0000000..33753b1 --- /dev/null +++ b/test/edge/worker.ts @@ -0,0 +1,27 @@ +// Worker entry for the real workerd (Miniflare) integration test. +// Mirrors test/deno/ws.test.ts's graph so HTTP + WebSocket behavior is proven +// identically across runtimes. Run via `npm run test:edge` (see test/edge/run.ts). +import { createApp, Route, Get, Ws, ctx, Module } from '../../src/index'; +import { edgeHandler } from '../../src/edge'; + +@Route('/rt') +class Rt { + @Get('/hello') + hello() { + return { ok: true }; + } + + @Ws('/echo') + echo(@ctx() { inbound }: any) { + return (async function* () { + for await (const msg of inbound) yield `echo:${msg}`; + })(); + } +} + +@Module({ mountpoint: '/', controllers: [Rt] }) +class M {} + +const app = createApp({ modules: [M] }); + +export default { fetch: edgeHandler(app) }; diff --git a/tsconfig.json b/tsconfig.json index 6dff172..8d268e0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,5 +15,5 @@ "forceConsistentCasingInFileNames": true }, "include": ["src", "test", "example"], - "exclude": ["test/deno", "test/bun"] + "exclude": ["test/deno", "test/bun", "test/edge"] } diff --git a/tsup.config.ts b/tsup.config.ts index bfad144..99f1c7f 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -4,7 +4,7 @@ import { defineConfig } from 'tsup'; // tsup injects a createRequire shim into the ESM output so the lazy require('ws') / require('busboy') // (optional peer deps) keep working there. reflect-metadata and the peer deps stay external. export default defineConfig({ - entry: ['src/index.ts', 'src/deno.ts', 'src/bun.ts'], + entry: ['src/index.ts', 'src/deno.ts', 'src/bun.ts', 'src/edge.ts'], format: ['cjs', 'esm'], dts: true, clean: true, diff --git a/vitest.config.ts b/vitest.config.ts index 48255af..32a48dc 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -7,6 +7,6 @@ export default defineConfig({ include: ['test/**/*.test.ts'], // test/deno/** holds Deno.test files run separately via `npm run test:deno`; // they use `npm:`/`jsr:` specifiers vitest can't resolve. - exclude: [...configDefaults.exclude, 'test/deno/**', 'test/bun/**'], + exclude: [...configDefaults.exclude, 'test/deno/**', 'test/bun/**', 'test/edge/**'], }, }); diff --git a/website/src/content/docs/guides/runtimes.md b/website/src/content/docs/guides/runtimes.md index 7dd7470..c1bb622 100644 --- a/website/src/content/docs/guides/runtimes.md +++ b/website/src/content/docs/guides/runtimes.md @@ -25,9 +25,10 @@ export default { fetch: app.fetch }; **Node is the reference implementation** — every runtime returns the same status, headers, and body for the same request (enforced by a parity test suite). -**Not yet on edge runtimes:** WebSocket (`@Ws`) and mesh — still Node-only there; Deno and Bun -now have full WebSocket support via `serveDeno` and `serveBun` (see below). `app.listen()` and -TLS remain Node-only; on Deno/Bun/edge you get `app.fetch`. +**All four runtimes now have full WebSocket support:** Node, Deno, and Bun via `serveDeno` / +`serveBun`, and Cloudflare Workers / edge via `edgeHandler` (see below) — same graph, same +`@Ws` handlers, same rooms/channels everywhere. `app.listen()`, TLS, per-request timeouts, +and mesh remain Node-only; on Deno/Bun/edge you get `app.fetch` + the runtime's adapter. **Body-size enforcement differs slightly:** on `app.fetch`, an oversized body is read in full before the `413` is returned, whereas Node aborts mid-stream once the limit is hit — so on @@ -54,8 +55,8 @@ run on Node. Behaviour matches the Node reference. **Advanced:** `app.upgrade(request, socket)` is the neutral primitive `serveDeno` uses. Any runtime can build a `WsSocket` capability (`inbound`, `abort`, `isOpen`, `send`, -`close`, `terminate`) and drive the graph — `serveBun` builds on the same primitive -(see below), and this is how future edge support will land. +`close`, `terminate`) and drive the graph — `serveBun` and `edgeHandler` build on the +same primitive (see below). **Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. On Deno you get `app.fetch` + `serveDeno`. @@ -81,3 +82,38 @@ run on Node and Deno. Behaviour matches the Node reference. **Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. On Bun you get `app.fetch` + `serveBun`. + +## Cloudflare Workers / edge + +`app.fetch` covers HTTP + SSE on every runtime. WebSocket needs a runtime-specific +upgrade, so Cloudflare Workers get a dedicated adapter at `@green-tea/core/edge`: + +```ts +import { createApp } from '@green-tea/core'; +import { edgeHandler } from '@green-tea/core/edge'; + +const app = createApp({ modules: [ChatModule] }); + +export default { fetch: edgeHandler(app) }; +``` + +`edgeHandler` routes normal requests through `app.fetch` and WebSocket upgrades through +`WebSocketPair` and `app.upgrade` — the same neutral primitive `serveDeno` and `serveBun` +use. Same graph, same `@Ws` handlers, same rooms/channels as Node/Deno/Bun; behaviour +matches the Node reference; validated on real workerd (Miniflare). + +**Requirement:** the Worker must enable the `nodejs_compat` compatibility flag — +`wrangler.toml`: + +```toml +compatibility_flags = ["nodejs_compat"] +compatibility_date = "2024-09-23" # or later +``` + +Green Tea's core statically imports Node built-ins that workerd only provides under +this flag; without it, the Worker fails to load. + +**Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. Cloudflare's +Durable Objects and WebSocket Hibernation are **not** used — `edgeHandler` accepts +WebSockets with the standard `WebSocketPair` model, so a Worker holds the connection +open for its lifetime rather than hibernating between messages.