@astrojs/cloudflare 3.1.2 → 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 8f63fdc483fc35de
2
- @astrojs/cloudflare:build: 
3
- @astrojs/cloudflare:build: > @astrojs/cloudflare@3.1.2 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,29 @@
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
+
12
+ ## 4.0.0
13
+
14
+ ### Major Changes
15
+
16
+ - [#5290](https://github.com/withastro/astro/pull/5290) [`b2b291d29`](https://github.com/withastro/astro/commit/b2b291d29143703cece0d12c8e74b2e1151d2061) Thanks [@matthewp](https://github.com/matthewp)! - Handle base configuration in adapters
17
+
18
+ This allows adapters to correctly handle `base` configuration. Internally Astro now matches routes when the URL includes the `base`.
19
+
20
+ Adapters now also have access to the `removeBase` method which will remove the `base` from a pathname. This is useful to look up files for static assets.
21
+
22
+ ### Patch Changes
23
+
24
+ - Updated dependencies [[`b2b291d29`](https://github.com/withastro/astro/commit/b2b291d29143703cece0d12c8e74b2e1151d2061), [`97e2b6ad7`](https://github.com/withastro/astro/commit/97e2b6ad7a6fa23e82be28b2f57cdf3f85fab112), [`4af4d8fa0`](https://github.com/withastro/astro/commit/4af4d8fa0035130fbf31c82d72777c3679bc1ca5), [`f6add3924`](https://github.com/withastro/astro/commit/f6add3924d5cd59925a6ea4bf7f2f731709bc893), [`247eb7411`](https://github.com/withastro/astro/commit/247eb7411f429317e5cd7d401a6660ee73641313)]:
25
+ - astro@1.6.4
26
+
3
27
  ## 3.1.2
4
28
 
5
29
  ### Patch Changes
package/README.md CHANGED
@@ -46,9 +46,9 @@ default `"advanced"`
46
46
 
47
47
  Cloudflare Pages has 2 different modes for deploying functions, `advanced` mode which picks up the `_worker.js` in `dist`, or a directory mode where pages will compile the worker out of a functions folder in the project root.
48
48
 
49
- For most projects the adaptor default of `advanced` will be sufficiant, when in this mode the `dist` folder will contain your compiled project. However if you'd like to use [pages plugins](https://developers.cloudflare.com/pages/platform/functions/plugins/) such as [Sentry](https://developers.cloudflare.com/pages/platform/functions/plugins/sentry/) for example to enable logging, you'll need to use directory mode.
49
+ For most projects the adaptor default of `advanced` will be sufficient; the `dist` folder will contain your compiled project. Switching to directory mode allows you to use [pages plugins](https://developers.cloudflare.com/pages/platform/functions/plugins/) such as [Sentry](https://developers.cloudflare.com/pages/platform/functions/plugins/sentry/) or write custom code to enable logging.
50
50
 
51
- In directory mode the adaptor will compile the client side part of you app the same way, but it will move the worker script into a `functions` folder in the project root. The adaptor will only ever place a `[[path]].js` in that folder, allowing you to add additional plugins and pages middleware which can be checked into version control.
51
+ In directory mode the adaptor will compile the client side part of you app the same way, but moves the worker script into a `functions` folder in the project root. The adaptor will only ever place a `[[path]].js` in that folder, allowing you to add additional plugins and pages middleware which can be checked into version control. Cloudflare documentation contains more information about [writing custom functions](https://developers.cloudflare.com/pages/platform/functions/).
52
52
 
53
53
  ```ts
54
54
  // directory mode
@@ -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,11 +1,13 @@
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
- const assetRequest = new Request(`${origin}/static${pathname}`, request);
10
+ const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
9
11
  return env.ASSETS.fetch(assetRequest);
10
12
  }
11
13
  let routeData = app.match(request, { matchNotFound: true });
@@ -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,9 +8,10 @@ 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
- const assetRequest = new Request(`${origin}/static${pathname}`, request);
14
+ const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
13
15
  return next(assetRequest);
14
16
  }
15
17
  let routeData = app.match(request, { matchNotFound: true });
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": "3.1.2",
4
+ "version": "4.0.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -30,9 +30,12 @@
30
30
  "dependencies": {
31
31
  "esbuild": "^0.14.42"
32
32
  },
33
+ "peerDependencies": {
34
+ "astro": "^1.6.5"
35
+ },
33
36
  "devDependencies": {
34
- "astro": "1.6.1",
35
- "astro-scripts": "0.0.8",
37
+ "astro": "1.6.5",
38
+ "astro-scripts": "0.0.9",
36
39
  "chai": "^4.3.6",
37
40
  "cheerio": "^1.0.0-rc.11",
38
41
  "mocha": "^9.2.2",
@@ -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,11 +13,13 @@ 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
18
21
  if (manifest.assets.has(pathname)) {
19
- const assetRequest = new Request(`${origin}/static${pathname}`, request);
22
+ const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
20
23
  return env.ASSETS.fetch(assetRequest);
21
24
  }
22
25
 
@@ -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,10 +15,12 @@ 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)) {
20
- const assetRequest = new Request(`${origin}/static${pathname}`, request);
23
+ const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
21
24
  return next(assetRequest);
22
25
  }
23
26
 
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
- };