@astrojs/cloudflare 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- @astrojs/cloudflare:build: cache hit, replaying output 90a638f6bf8ef71a
2
- @astrojs/cloudflare:build: 
3
- @astrojs/cloudflare:build: > @astrojs/cloudflare@4.0.0 build /home/runner/work/astro/astro/packages/integrations/cloudflare
4
- @astrojs/cloudflare:build: > astro-scripts build "src/**/*.ts" && tsc
5
- @astrojs/cloudflare:build: 
1
+ @astrojs/cloudflare:build: cache hit, replaying output 17da9601b72c4888
2
+ @astrojs/cloudflare:build: 
3
+ @astrojs/cloudflare:build: > @astrojs/cloudflare@4.0.1 build /home/runner/work/astro/astro/packages/integrations/cloudflare
4
+ @astrojs/cloudflare:build: > astro-scripts build "src/**/*.ts" && tsc
5
+ @astrojs/cloudflare:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @astrojs/cloudflare
2
2
 
3
+ ## 4.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#5301](https://github.com/withastro/astro/pull/5301) [`a79a37cad`](https://github.com/withastro/astro/commit/a79a37cad549b21f91599ff86899e456d9dcc7df) Thanks [@bluwy](https://github.com/bluwy)! - Fix environment variables usage in worker output and warn if environment variables are accessedd too early
8
+
9
+ - Updated dependencies [[`88c1bbe3a`](https://github.com/withastro/astro/commit/88c1bbe3a71f85e92f42f13d0f310c6b2a264ade), [`a79a37cad`](https://github.com/withastro/astro/commit/a79a37cad549b21f91599ff86899e456d9dcc7df)]:
10
+ - astro@1.6.5
11
+
3
12
  ## 4.0.0
4
13
 
5
14
  ### Major Changes
package/README.md CHANGED
@@ -92,16 +92,18 @@ To do this:
92
92
 
93
93
  ## Environment Variables
94
94
 
95
- As Cloudflare Pages Functions [provides environment variables differently](https://developers.cloudflare.com/pages/platform/functions/#adding-environment-variables-locally), private environment variables needs to be set through [`vite.define`](https://vitejs.dev/config/shared-options.html#define) to work in builds.
95
+ As Cloudflare Pages Functions [provides environment variables per request](https://developers.cloudflare.com/pages/platform/functions/#adding-environment-variables-locally), you can only access private environment variables when a request has happened. Usually, this means moving environment variable access inside a function.
96
96
 
97
97
  ```js
98
- // astro.config.mjs
99
- export default {
100
- vite: {
101
- define: {
102
- 'process.env.MY_SECRET': JSON.stringify(process.env.MY_SECRET),
103
- },
104
- },
98
+ // pages/[id].json.js
99
+
100
+ export function get({ params }) {
101
+ // Access environment variables per request inside a function
102
+ const serverUrl = import.meta.env.SERVER_URL;
103
+ const result = await fetch(serverUrl + "/user/" + params.id);
104
+ return {
105
+ body: await result.text(),
106
+ };
105
107
  }
106
108
  ```
107
109
 
@@ -1,4 +1,3 @@
1
- import './shim.js';
2
1
  import type { SSRManifest } from 'astro';
3
2
  declare type Env = {
4
3
  ASSETS: {
@@ -1,8 +1,10 @@
1
- import "./shim.js";
2
1
  import { App } from "astro/app";
2
+ import { getProcessEnvProxy } from "./util.js";
3
+ process.env = getProcessEnvProxy();
3
4
  function createExports(manifest) {
4
5
  const app = new App(manifest, false);
5
6
  const fetch = async (request, env, context) => {
7
+ process.env = env;
6
8
  const { origin, pathname } = new URL(request.url);
7
9
  if (manifest.assets.has(pathname)) {
8
10
  const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
@@ -1,4 +1,3 @@
1
- import './shim.js';
2
1
  import type { SSRManifest } from 'astro';
3
2
  export declare function createExports(manifest: SSRManifest): {
4
3
  onRequest: ({ request, next, ...runtimeEnv }: {
@@ -1,5 +1,6 @@
1
- import "./shim.js";
2
1
  import { App } from "astro/app";
2
+ import { getProcessEnvProxy } from "./util.js";
3
+ process.env = getProcessEnvProxy();
3
4
  function createExports(manifest) {
4
5
  const app = new App(manifest, false);
5
6
  const onRequest = async ({
@@ -7,6 +8,7 @@ function createExports(manifest) {
7
8
  next,
8
9
  ...runtimeEnv
9
10
  }) => {
11
+ process.env = runtimeEnv.env;
10
12
  const { origin, pathname } = new URL(request.url);
11
13
  if (manifest.assets.has(pathname)) {
12
14
  const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
package/dist/util.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function getProcessEnvProxy(): {};
package/dist/util.js ADDED
@@ -0,0 +1,15 @@
1
+ function getProcessEnvProxy() {
2
+ return new Proxy(
3
+ {},
4
+ {
5
+ get: (target, prop) => {
6
+ console.warn(
7
+ `Unable to access \`import.meta\0.env.${prop.toString()}\` on initialization as the Cloudflare platform only provides the environment variables per request. Please move the environment variable access inside a function that's only called after a request has been received.`
8
+ );
9
+ }
10
+ }
11
+ );
12
+ }
13
+ export {
14
+ getProcessEnvProxy
15
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/cloudflare",
3
3
  "description": "Deploy your site to cloudflare workers or cloudflare pages",
4
- "version": "4.0.0",
4
+ "version": "4.0.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -31,10 +31,10 @@
31
31
  "esbuild": "^0.14.42"
32
32
  },
33
33
  "peerDependencies": {
34
- "astro": "^1.6.4"
34
+ "astro": "^1.6.5"
35
35
  },
36
36
  "devDependencies": {
37
- "astro": "1.6.4",
37
+ "astro": "1.6.5",
38
38
  "astro-scripts": "0.0.9",
39
39
  "chai": "^4.3.6",
40
40
  "cheerio": "^1.0.0-rc.11",
@@ -1,7 +1,8 @@
1
- import './shim.js';
2
-
3
1
  import type { SSRManifest } from 'astro';
4
2
  import { App } from 'astro/app';
3
+ import { getProcessEnvProxy } from './util.js';
4
+
5
+ process.env = getProcessEnvProxy();
5
6
 
6
7
  type Env = {
7
8
  ASSETS: { fetch: (req: Request) => Promise<Response> };
@@ -12,6 +13,8 @@ export function createExports(manifest: SSRManifest) {
12
13
  const app = new App(manifest, false);
13
14
 
14
15
  const fetch = async (request: Request, env: Env, context: any) => {
16
+ process.env = env as any;
17
+
15
18
  const { origin, pathname } = new URL(request.url);
16
19
 
17
20
  // static assets
@@ -1,7 +1,8 @@
1
- import './shim.js';
2
-
3
1
  import type { SSRManifest } from 'astro';
4
2
  import { App } from 'astro/app';
3
+ import { getProcessEnvProxy } from './util.js';
4
+
5
+ process.env = getProcessEnvProxy();
5
6
 
6
7
  export function createExports(manifest: SSRManifest) {
7
8
  const app = new App(manifest, false);
@@ -14,6 +15,8 @@ export function createExports(manifest: SSRManifest) {
14
15
  request: Request;
15
16
  next: (request: Request) => void;
16
17
  } & Record<string, unknown>) => {
18
+ process.env = runtimeEnv.env as any;
19
+
17
20
  const { origin, pathname } = new URL(request.url);
18
21
  // static assets
19
22
  if (manifest.assets.has(pathname)) {
package/src/util.ts ADDED
@@ -0,0 +1,16 @@
1
+ export function getProcessEnvProxy() {
2
+ return new Proxy(
3
+ {},
4
+ {
5
+ get: (target, prop) => {
6
+ console.warn(
7
+ // NOTE: \0 prevents Vite replacement
8
+ `Unable to access \`import.meta\0.env.${prop.toString()}\` on initialization ` +
9
+ `as the Cloudflare platform only provides the environment variables per request. ` +
10
+ `Please move the environment variable access inside a function ` +
11
+ `that's only called after a request has been received.`
12
+ );
13
+ },
14
+ }
15
+ );
16
+ }
@@ -24,6 +24,7 @@ describe.skip('Basic app', () => {
24
24
  let html = await res.text();
25
25
  let $ = cheerio.load(html);
26
26
  expect($('h1').text()).to.equal('Testing');
27
+ expect($('#env').text()).to.equal('secret');
27
28
  } finally {
28
29
  stop();
29
30
  }
@@ -1,6 +1,9 @@
1
1
  import { defineConfig } from 'astro/config';
2
2
  import cloudflare from '@astrojs/cloudflare';
3
3
 
4
+ // test env var
5
+ process.env.SECRET_STUFF = 'secret'
6
+
4
7
  export default defineConfig({
5
8
  adapter: cloudflare(),
6
9
  output: 'server',
@@ -4,5 +4,6 @@
4
4
  </head>
5
5
  <body>
6
6
  <h1>Testing</h1>
7
+ <div id="env">{import.meta.env.SECRET_STUFF}</div>
7
8
  </body>
8
9
  </html>
@@ -0,0 +1,4 @@
1
+ # for tests only
2
+
3
+ [vars]
4
+ SECRET_STUFF = "secret"
package/dist/shim.d.ts DELETED
File without changes
package/dist/shim.js DELETED
@@ -1,4 +0,0 @@
1
- globalThis.process = {
2
- argv: [],
3
- env: {}
4
- };
package/src/shim.ts DELETED
@@ -1,4 +0,0 @@
1
- (globalThis as any).process = {
2
- argv: [],
3
- env: {},
4
- };