From 2560773c10fb108ef297a4f249b0569d1c70e89a Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Fri, 15 Sep 2023 07:08:51 +0200 Subject: [PATCH 1/7] add support for nodejs_compat --- .changeset/fifty-weeks-bake.md | 5 ++++ packages/integrations/cloudflare/README.md | 4 ++++ packages/integrations/cloudflare/src/index.ts | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 .changeset/fifty-weeks-bake.md diff --git a/.changeset/fifty-weeks-bake.md b/.changeset/fifty-weeks-bake.md new file mode 100644 index 000000000000..5d3c70b3043c --- /dev/null +++ b/.changeset/fifty-weeks-bake.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': minor +--- + +Fix build issues for `node:*`, for supported runtime APIs diff --git a/packages/integrations/cloudflare/README.md b/packages/integrations/cloudflare/README.md index 99b2d75282c5..6888500744d5 100644 --- a/packages/integrations/cloudflare/README.md +++ b/packages/integrations/cloudflare/README.md @@ -170,6 +170,10 @@ This will enable Cloudflare to serve files and process static redirects without See [Cloudflare's documentation](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) for more details. +## Node.js compatibility + +Cloudflare offers opt-in support for certain Node.js runtime APIs, and with the Cloudflare Adapter, Astro provides seamless integration for these APIs. To leverage this, ensure that your page or endpoint is set up for Server-Side Rendering (SSR). Additionally, you'll need to enable the Compatibility Flag in Cloudflare. The configuration for this flag may vary based on where you deploy your Astro site. For detailed guidance, please refer to the [Cloudflare documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs). + ## Troubleshooting For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help! diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 718b1efa8648..f0269a15090a 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -163,6 +163,18 @@ export default function createIntegration(args?: Options): AstroIntegration { target: 'es2020', platform: 'browser', conditions: ['workerd', 'worker', 'browser'], + external: [ + 'node:assert', + 'node:async_hooks', + 'node:buffer', + 'node:diagnostics_channel', + 'node:events', + 'node:path', + 'node:process', + 'node:stream', + 'node:string_decoder', + 'node:util', + ], entryPoints: entryPaths, outdir: outputDir, allowOverwrite: true, @@ -224,6 +236,18 @@ export default function createIntegration(args?: Options): AstroIntegration { target: 'es2020', platform: 'browser', conditions: ['workerd', 'worker', 'browser'], + external: [ + 'node:assert', + 'node:async_hooks', + 'node:buffer', + 'node:diagnostics_channel', + 'node:events', + 'node:path', + 'node:process', + 'node:stream', + 'node:string_decoder', + 'node:util', + ], entryPoints: [entryPath], outfile: buildPath, allowOverwrite: true, From b23e18f331aa313f332f3c0166ac6a95d460b94a Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Fri, 15 Sep 2023 13:37:05 +0200 Subject: [PATCH 2/7] update Readme --- packages/integrations/cloudflare/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/integrations/cloudflare/README.md b/packages/integrations/cloudflare/README.md index 6888500744d5..84dc347353e4 100644 --- a/packages/integrations/cloudflare/README.md +++ b/packages/integrations/cloudflare/README.md @@ -172,7 +172,18 @@ See [Cloudflare's documentation](https://developers.cloudflare.com/pages/platfor ## Node.js compatibility -Cloudflare offers opt-in support for certain Node.js runtime APIs, and with the Cloudflare Adapter, Astro provides seamless integration for these APIs. To leverage this, ensure that your page or endpoint is set up for Server-Side Rendering (SSR). Additionally, you'll need to enable the Compatibility Flag in Cloudflare. The configuration for this flag may vary based on where you deploy your Astro site. For detailed guidance, please refer to the [Cloudflare documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs). +Cloudflare offers opt-in support for the following Node.js runtime APIs, and with the Cloudflare Adapter, Astro provides seamless integration for these APIs. To leverage this, ensure that your page or endpoint is set up for Server-Side Rendering (SSR). Cloudflare only supports the `import {} from 'node:*'` import syntax. Additionally, you'll need to enable the Compatibility Flag in Cloudflare. The configuration for this flag may vary based on where you deploy your Astro site. For detailed guidance, please refer to the [Cloudflare documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs). + +- assert +- AsyncLocalStorage +- Buffer +- Diagnostics Channel +- EventEmitter +- path +- process +- Streams +- StringDecoder +- util ## Troubleshooting From 2c24221887afb9bd1418851ec7ee74ca3d02b8b0 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Fri, 15 Sep 2023 16:09:20 +0200 Subject: [PATCH 3/7] improve changeset --- .changeset/fifty-weeks-bake.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/.changeset/fifty-weeks-bake.md b/.changeset/fifty-weeks-bake.md index 5d3c70b3043c..f36bb7495be7 100644 --- a/.changeset/fifty-weeks-bake.md +++ b/.changeset/fifty-weeks-bake.md @@ -2,4 +2,19 @@ '@astrojs/cloudflare': minor --- -Fix build issues for `node:*`, for supported runtime APIs +Add support for following Node.js Runtime APIs, which are availabe in [Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/nodejs) using the `node:` syntax. + +- assert +- AsyncLocalStorage +- Buffer +- Diagnostics Channel +- EventEmitter +- path +- process +- Streams +- StringDecoder +- util + +```js +import { Buffer } from 'node:buffer'; +``` From 5ccd893b8cc27c2094df1ba33bd93b32f4f03aa3 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Sat, 16 Sep 2023 07:02:26 +0200 Subject: [PATCH 4/7] improve changeset Co-authored-by: Sarah Rainsberger --- .changeset/fifty-weeks-bake.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/fifty-weeks-bake.md b/.changeset/fifty-weeks-bake.md index f36bb7495be7..714be802ece2 100644 --- a/.changeset/fifty-weeks-bake.md +++ b/.changeset/fifty-weeks-bake.md @@ -2,7 +2,7 @@ '@astrojs/cloudflare': minor --- -Add support for following Node.js Runtime APIs, which are availabe in [Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/nodejs) using the `node:` syntax. +Add support for the following Node.js Runtime APIs, which are availabe in [Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/nodejs) using the `node:` syntax. - assert - AsyncLocalStorage From 6ef55e937577efedfb5207942709529694e6fd57 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Sat, 16 Sep 2023 07:04:51 +0200 Subject: [PATCH 5/7] improve readme Co-authored-by: Sarah Rainsberger --- packages/integrations/cloudflare/README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/integrations/cloudflare/README.md b/packages/integrations/cloudflare/README.md index 84dc347353e4..b92b810ed723 100644 --- a/packages/integrations/cloudflare/README.md +++ b/packages/integrations/cloudflare/README.md @@ -172,7 +172,7 @@ See [Cloudflare's documentation](https://developers.cloudflare.com/pages/platfor ## Node.js compatibility -Cloudflare offers opt-in support for the following Node.js runtime APIs, and with the Cloudflare Adapter, Astro provides seamless integration for these APIs. To leverage this, ensure that your page or endpoint is set up for Server-Side Rendering (SSR). Cloudflare only supports the `import {} from 'node:*'` import syntax. Additionally, you'll need to enable the Compatibility Flag in Cloudflare. The configuration for this flag may vary based on where you deploy your Astro site. For detailed guidance, please refer to the [Cloudflare documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs). +Astro's Cloudflare adapter allows you to use the following Node.js runtime APIs (that are supported by Cloudflare): - assert - AsyncLocalStorage @@ -185,6 +185,18 @@ Cloudflare offers opt-in support for the following Node.js runtime APIs, and wit - StringDecoder - util +To use these APIs, your page or endpoint must be server-side rendered (not pre-rendered) and must use the the `import {} from 'node:*'` import syntax. + +```js +// put a file path for the endpoint here +export const prerender = false; +import { Buffer } from 'node:buffer'; +``` + +Additionally, you'll need to enable the Compatibility Flag in Cloudflare. The configuration for this flag may vary based on where you deploy your Astro site. + +For detailed guidance, please refer to the [Cloudflare documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs). + ## Troubleshooting For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help! From 6a0943f0491f0922365a56d800c18a681a677d12 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Sat, 16 Sep 2023 07:10:45 +0200 Subject: [PATCH 6/7] improve readme --- packages/integrations/cloudflare/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/integrations/cloudflare/README.md b/packages/integrations/cloudflare/README.md index acd33ec2d5a4..f8f6801d66f1 100644 --- a/packages/integrations/cloudflare/README.md +++ b/packages/integrations/cloudflare/README.md @@ -172,7 +172,7 @@ This optional flag enables the Astro dev server to populate environment variable ```js // astro.config.mjs -import { defineConfig } from 'astro/config'; +import { defineConfig } from 'astro/config;' import cloudflare from '@astrojs/cloudflare'; export default defineConfig({ @@ -196,7 +196,7 @@ See [Cloudflare's documentation](https://developers.cloudflare.com/pages/platfor ## Node.js compatibility -Astro's Cloudflare adapter allows you to use the following Node.js runtime APIs (that are supported by Cloudflare): +Astro's Cloudflare adapter allows you to use any Node.js runtime API supported by Cloudflare: - assert - AsyncLocalStorage @@ -212,7 +212,7 @@ Astro's Cloudflare adapter allows you to use the following Node.js runtime APIs To use these APIs, your page or endpoint must be server-side rendered (not pre-rendered) and must use the the `import {} from 'node:*'` import syntax. ```js -// put a file path for the endpoint here +// pages/api/endpoint.js export const prerender = false; import { Buffer } from 'node:buffer'; ``` From 42795276a1026b6f08ef9d4c317d6f4277b47465 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Sat, 16 Sep 2023 14:14:56 +0200 Subject: [PATCH 7/7] fix typo Co-authored-by: Florian Lefebvre --- packages/integrations/cloudflare/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/cloudflare/README.md b/packages/integrations/cloudflare/README.md index f8f6801d66f1..767073c3cd2a 100644 --- a/packages/integrations/cloudflare/README.md +++ b/packages/integrations/cloudflare/README.md @@ -172,7 +172,7 @@ This optional flag enables the Astro dev server to populate environment variable ```js // astro.config.mjs -import { defineConfig } from 'astro/config;' +import { defineConfig } from 'astro/config'; import cloudflare from '@astrojs/cloudflare'; export default defineConfig({