@astrojs/cloudflare 6.7.0 → 6.8.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.
package/README.md CHANGED
@@ -73,16 +73,61 @@ It's then possible to update the preview script in your `package.json` to `"prev
73
73
 
74
74
  ## Access to the Cloudflare runtime
75
75
 
76
- You can access all the Cloudflare bindings and environment variables from Astro components and API routes through the adapter API.
76
+ You can access all the Cloudflare bindings and environment variables from Astro components and API routes through `Astro.locals`.
77
+
78
+ If you're inside an `.astro` file, you access the runtime using the `Astro.locals` global:
79
+
80
+ ```astro
81
+ const env = Astro.locals.runtime.env;
82
+ ```
83
+
84
+ From an endpoint:
77
85
 
78
86
  ```js
79
- import { getRuntime } from '@astrojs/cloudflare/runtime';
87
+ // src/pages/api/someFile.js
88
+ export function get(context) {
89
+ const runtime = context.locals.runtime;
80
90
 
81
- getRuntime(Astro.request);
91
+ return new Response('Some body');
92
+ }
82
93
  ```
83
94
 
84
95
  Depending on your adapter mode (advanced = worker, directory = pages), the runtime object will look a little different due to differences in the Cloudflare API.
85
96
 
97
+ If you're using the `advanced` runtime, you can type the `runtime` object as following:
98
+
99
+ ```ts
100
+ // src/env.d.ts
101
+ /// <reference types="astro/client" />
102
+ import type { AdvancedRuntime } from '@astrojs/cloudflare';
103
+
104
+ declare namespace App {
105
+ interface Locals extends AdvancedRuntime {
106
+ user: {
107
+ name: string;
108
+ surname: string;
109
+ };
110
+ }
111
+ }
112
+ ```
113
+
114
+ If you're using the `directory` runtime, you can type the `runtime` object as following:
115
+
116
+ ```ts
117
+ // src/env.d.ts
118
+ /// <reference types="astro/client" />
119
+ import type { DirectoryRuntime } from '@astrojs/cloudflare';
120
+
121
+ declare namespace App {
122
+ interface Locals extends DirectoryRuntime {
123
+ user: {
124
+ name: string;
125
+ surname: string;
126
+ };
127
+ }
128
+ }
129
+ ```
130
+
86
131
  ## Environment Variables
87
132
 
88
133
  See Cloudflare's documentation for [working with environment variables](https://developers.cloudflare.com/pages/platform/functions/bindings/#environment-variables).
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type { AstroAdapter, AstroIntegration } from 'astro';
2
+ export type { AdvancedRuntime } from './server.advanced';
3
+ export type { DirectoryRuntime } from './server.directory';
2
4
  type Options = {
3
5
  mode: 'directory' | 'advanced';
4
6
  };
5
7
  export declare function getAdapter(isModeDirectory: boolean): AstroAdapter;
6
8
  export default function createIntegration(args?: Options): AstroIntegration;
7
- export {};
package/dist/runtime.d.ts CHANGED
@@ -22,4 +22,14 @@ export type PagesRuntime<T = unknown, U = unknown> = {
22
22
  };
23
23
  cf?: IncomingRequestCfProperties;
24
24
  };
25
+ /**
26
+ * @deprecated since version 6.8.0
27
+ * The `getRuntime` utility has been deprecated and should be updated to the new [`Astro.locals`](https://docs.astro.build/en/guides/middleware/#locals) API.
28
+ * ```diff
29
+ * - import { getRuntime } from '@astrojs/cloudflare/runtime';
30
+ * - getRuntime(Astro.request);
31
+ *
32
+ * + const runtime = Astro.locals.runtime;
33
+ * ```
34
+ */
25
35
  export declare function getRuntime<T = unknown, U = unknown>(request: Request): WorkerRuntime<T> | PagesRuntime<T, U>;
@@ -6,6 +6,14 @@ type Env = {
6
6
  };
7
7
  name: string;
8
8
  };
9
+ export interface AdvancedRuntime {
10
+ runtime: {
11
+ waitUntil: (promise: Promise<any>) => void;
12
+ env: Env;
13
+ cf: CFRequest['cf'];
14
+ caches: typeof caches;
15
+ };
16
+ }
9
17
  export declare function createExports(manifest: SSRManifest): {
10
18
  default: {
11
19
  fetch: (request: Request & CFRequest, env: Env, context: ExecutionContext) => Promise<Response>;
@@ -28,7 +28,17 @@ function createExports(manifest) {
28
28
  context.waitUntil(promise);
29
29
  }
30
30
  });
31
- let response = await app.render(request, routeData);
31
+ const locals = {
32
+ runtime: {
33
+ waitUntil: (promise) => {
34
+ context.waitUntil(promise);
35
+ },
36
+ env,
37
+ cf: request.cf,
38
+ caches
39
+ }
40
+ };
41
+ let response = await app.render(request, routeData, locals);
32
42
  if (app.setCookieHeaders) {
33
43
  for (const setCookieHeader of app.setCookieHeaders(response)) {
34
44
  response.headers.append("Set-Cookie", setCookieHeader);
@@ -1,10 +1,14 @@
1
1
  import type { Request as CFRequest, EventContext } from '@cloudflare/workers-types';
2
2
  import type { SSRManifest } from 'astro';
3
+ export interface DirectoryRuntime {
4
+ runtime: {
5
+ waitUntil: (promise: Promise<any>) => void;
6
+ env: EventContext<unknown, string, unknown>['env'];
7
+ cf: CFRequest['cf'];
8
+ caches: typeof caches;
9
+ };
10
+ }
3
11
  export declare function createExports(manifest: SSRManifest): {
4
- onRequest: ({ request, next, ...runtimeEnv }: {
5
- request: Request & CFRequest;
6
- next: (request: Request) => void;
7
- waitUntil: EventContext<unknown, any, unknown>['waitUntil'];
8
- } & Record<string, unknown>) => Promise<import("@cloudflare/workers-types").Response | Response>;
12
+ onRequest: (context: EventContext<unknown, string, unknown>) => Promise<import("@cloudflare/workers-types").Response | Response>;
9
13
  manifest: SSRManifest;
10
14
  };
@@ -5,17 +5,13 @@ if (!isNode) {
5
5
  }
6
6
  function createExports(manifest) {
7
7
  const app = new App(manifest);
8
- const onRequest = async ({
9
- request,
10
- next,
11
- ...runtimeEnv
12
- }) => {
13
- process.env = runtimeEnv.env;
8
+ const onRequest = async (context) => {
9
+ const request = context.request;
10
+ const { next, env } = context;
11
+ process.env = env;
14
12
  const { pathname } = new URL(request.url);
15
13
  if (manifest.assets.has(pathname)) {
16
- return runtimeEnv.env.ASSETS.fetch(
17
- request
18
- );
14
+ return env.ASSETS.fetch(request);
19
15
  }
20
16
  let routeData = app.match(request, { matchNotFound: true });
21
17
  if (routeData) {
@@ -25,16 +21,26 @@ function createExports(manifest) {
25
21
  request.headers.get("cf-connecting-ip")
26
22
  );
27
23
  Reflect.set(request, Symbol.for("runtime"), {
28
- ...runtimeEnv,
24
+ ...context,
29
25
  waitUntil: (promise) => {
30
- runtimeEnv.waitUntil(promise);
26
+ context.waitUntil(promise);
31
27
  },
32
28
  name: "cloudflare",
33
29
  next,
34
30
  caches,
35
31
  cf: request.cf
36
32
  });
37
- let response = await app.render(request, routeData);
33
+ const locals = {
34
+ runtime: {
35
+ waitUntil: (promise) => {
36
+ context.waitUntil(promise);
37
+ },
38
+ env: context.env,
39
+ cf: request.cf,
40
+ caches
41
+ }
42
+ };
43
+ let response = await app.render(request, routeData, locals);
38
44
  if (app.setCookieHeaders) {
39
45
  for (const setCookieHeader of app.setCookieHeaders(response)) {
40
46
  response.headers.append("Set-Cookie", setCookieHeader);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/cloudflare",
3
3
  "description": "Deploy your site to Cloudflare Workers/Pages",
4
- "version": "6.7.0",
4
+ "version": "6.8.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -38,14 +38,15 @@
38
38
  "tiny-glob": "^0.2.9"
39
39
  },
40
40
  "peerDependencies": {
41
- "astro": "^2.10.5"
41
+ "astro": "^2.10.14"
42
42
  },
43
43
  "devDependencies": {
44
44
  "chai": "^4.3.7",
45
45
  "cheerio": "1.0.0-rc.12",
46
+ "kill-port": "^2.0.1",
46
47
  "mocha": "^9.2.2",
47
- "wrangler": "^2.0.23",
48
- "astro": "2.10.5",
48
+ "wrangler": "^3.5.0",
49
+ "astro": "2.10.14",
49
50
  "astro-scripts": "0.0.14"
50
51
  },
51
52
  "scripts": {