@astrojs/cloudflare 3.1.0 → 3.1.2

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 bb5a25392613b0d2
2
- @astrojs/cloudflare:build: 
3
- @astrojs/cloudflare:build: > @astrojs/cloudflare@3.1.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 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: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @astrojs/cloudflare
2
2
 
3
+ ## 3.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#5230](https://github.com/withastro/astro/pull/5230) [`69a532ab6`](https://github.com/withastro/astro/commit/69a532ab60a85d30c2395969593c4d38f9a2fbbe) Thanks [@matthewp](https://github.com/matthewp)! - Exports new runtime entrypoint's types
8
+
9
+ ## 3.1.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#5103](https://github.com/withastro/astro/pull/5103) [`d151d9f3f`](https://github.com/withastro/astro/commit/d151d9f3f29c0a57c59b8029a18717808ccc7f8f) Thanks [@AirBorne04](https://github.com/AirBorne04)! - enable access to Cloudflare runtime [KV, R2, Durable Objects]
14
+ - access native Cloudflare runtime through `import { getRuntime } from "@astrojs/cloudflare/runtime"`; now you can call `getRuntime(Astro.request)` and get access to the runtime environment.
15
+
3
16
  ## 3.1.0
4
17
 
5
18
  ### Minor Changes
package/README.md CHANGED
@@ -68,13 +68,27 @@ $ pnpm install wrangler --save-dev
68
68
 
69
69
  It's then possible to update the preview script in your `package.json` to `"preview": "wrangler pages dev ./dist"`.This will allow you run your entire application locally with [Wrangler](https://github.com/cloudflare/wrangler2), which supports secrets, environment variables, KV namespaces, Durable Objects and [all other supported Cloudflare bindings](https://developers.cloudflare.com/pages/platform/functions/#adding-bindings).
70
70
 
71
+ ## Access to the Cloudflare runtime
72
+
73
+ You can access all the Cloudflare bindings and environment variables from Astro components and API routes through the adapter API.
74
+
75
+ ```js
76
+ import { getRuntime } from "@astrojs/cloudflare/runtime";
77
+
78
+ getRuntime(Astro.request);
79
+ ```
80
+
81
+ Depending on your adapter mode (advanced = worker, directory = pages), the runtime object will look a little different due to differences in the Cloudflare API.
82
+
71
83
  ## Streams
72
84
 
73
- Some integrations such as [React](https://github.com/withastro/astro/tree/main/packages/integrations/react) rely on web streams. Currently Cloudflare Pages functions are in beta and don't support the `streams_enable_constructors` feature flag.
85
+ Some integrations such as [React](https://github.com/withastro/astro/tree/main/packages/integrations/react) rely on web streams. Currently Cloudflare Pages Functions require enabling a flag to support Streams.
74
86
 
75
- In order to work around this:
76
- - install the `"web-streams-polyfill"` package
77
- - add `import "web-streams-polyfill/es2018";` to the top of the front matter of every page which requires streams, such as server rendering a React component.
87
+ To do this:
88
+ - go to the Cloudflare Pages project
89
+ - click on Settings in the top bar, then Functions in the sidebar
90
+ - scroll down to Compatibility Flags, click Configure Production Compatibility Flags, and add `streams_enable_constructors`
91
+ - do this for both the Production Compatibility Flags and Preview Compatibility Flags
78
92
 
79
93
  ## Environment Variables
80
94
 
@@ -0,0 +1,16 @@
1
+ export declare type WorkerRuntime<T = unknown> = {
2
+ name: 'cloudflare';
3
+ env: T;
4
+ waitUntil(promise: Promise<any>): void;
5
+ passThroughOnException(): void;
6
+ };
7
+ export declare type PagesRuntime<T = unknown, U = unknown> = {
8
+ name: 'cloudflare';
9
+ env: T;
10
+ functionPath: string;
11
+ params: Record<string, string>;
12
+ data: U;
13
+ waitUntil(promise: Promise<any>): void;
14
+ next(request: Request): void;
15
+ };
16
+ export declare function getRuntime<T = unknown, U = unknown>(request: Request): WorkerRuntime<T> | PagesRuntime<T, U>;
@@ -0,0 +1,12 @@
1
+ function getRuntime(request) {
2
+ if (!!request) {
3
+ return Reflect.get(request, Symbol.for("runtime"));
4
+ } else {
5
+ throw new Error(
6
+ "To retrieve the current cloudflare runtime you need to pass in the Astro request object"
7
+ );
8
+ }
9
+ }
10
+ export {
11
+ getRuntime
12
+ };
@@ -4,10 +4,11 @@ declare type Env = {
4
4
  ASSETS: {
5
5
  fetch: (req: Request) => Promise<Response>;
6
6
  };
7
+ name: string;
7
8
  };
8
9
  export declare function createExports(manifest: SSRManifest): {
9
10
  default: {
10
- fetch: (request: Request, env: Env) => Promise<Response>;
11
+ fetch: (request: Request, env: Env, context: any) => Promise<Response>;
11
12
  };
12
13
  };
13
14
  export {};
@@ -2,7 +2,7 @@ import "./shim.js";
2
2
  import { App } from "astro/app";
3
3
  function createExports(manifest) {
4
4
  const app = new App(manifest, false);
5
- const fetch = async (request, env) => {
5
+ const fetch = async (request, env, context) => {
6
6
  const { origin, pathname } = new URL(request.url);
7
7
  if (manifest.assets.has(pathname)) {
8
8
  const assetRequest = new Request(`${origin}/static${pathname}`, request);
@@ -15,6 +15,7 @@ function createExports(manifest) {
15
15
  Symbol.for("astro.clientAddress"),
16
16
  request.headers.get("cf-connecting-ip")
17
17
  );
18
+ Reflect.set(request, Symbol.for("runtime"), { env, name: "cloudflare", ...context });
18
19
  let response = await app.render(request, routeData);
19
20
  if (app.setCookieHeaders) {
20
21
  for (const setCookieHeader of app.setCookieHeaders(response)) {
@@ -1,8 +1,8 @@
1
1
  import './shim.js';
2
2
  import type { SSRManifest } from 'astro';
3
3
  export declare function createExports(manifest: SSRManifest): {
4
- onRequest: ({ request, next, }: {
4
+ onRequest: ({ request, next, ...runtimeEnv }: {
5
5
  request: Request;
6
6
  next: (request: Request) => void;
7
- }) => Promise<void | Response>;
7
+ } & Record<string, unknown>) => Promise<void | Response>;
8
8
  };
@@ -4,7 +4,8 @@ function createExports(manifest) {
4
4
  const app = new App(manifest, false);
5
5
  const onRequest = async ({
6
6
  request,
7
- next
7
+ next,
8
+ ...runtimeEnv
8
9
  }) => {
9
10
  const { origin, pathname } = new URL(request.url);
10
11
  if (manifest.assets.has(pathname)) {
@@ -18,6 +19,11 @@ function createExports(manifest) {
18
19
  Symbol.for("astro.clientAddress"),
19
20
  request.headers.get("cf-connecting-ip")
20
21
  );
22
+ Reflect.set(request, Symbol.for("runtime"), {
23
+ ...runtimeEnv,
24
+ name: "cloudflare",
25
+ next
26
+ });
21
27
  let response = await app.render(request, routeData);
22
28
  if (app.setCookieHeaders) {
23
29
  for (const setCookieHeader of app.setCookieHeaders(response)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/cloudflare",
3
- "description": "Deploy your site to cloudflare pages functions",
4
- "version": "3.1.0",
3
+ "description": "Deploy your site to cloudflare workers or cloudflare pages",
4
+ "version": "3.1.2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -19,6 +19,10 @@
19
19
  "homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
20
20
  "exports": {
21
21
  ".": "./dist/index.js",
22
+ "./runtime": {
23
+ "types": "./runtime.d.ts",
24
+ "default": "./dist/runtime.js"
25
+ },
22
26
  "./server.advanced.js": "./dist/server.advanced.js",
23
27
  "./server.directory.js": "./dist/server.directory.js",
24
28
  "./package.json": "./package.json"
@@ -27,7 +31,7 @@
27
31
  "esbuild": "^0.14.42"
28
32
  },
29
33
  "devDependencies": {
30
- "astro": "1.5.0",
34
+ "astro": "1.6.1",
31
35
  "astro-scripts": "0.0.8",
32
36
  "chai": "^4.3.6",
33
37
  "cheerio": "^1.0.0-rc.11",
package/runtime.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export type { WorkerRuntime, PagesRuntime } from './dist/runtime';
2
+
3
+ export { getRuntime } from './dist/runtime';
package/src/runtime.ts ADDED
@@ -0,0 +1,28 @@
1
+ export type WorkerRuntime<T = unknown> = {
2
+ name: 'cloudflare';
3
+ env: T;
4
+ waitUntil(promise: Promise<any>): void;
5
+ passThroughOnException(): void;
6
+ };
7
+
8
+ export type PagesRuntime<T = unknown, U = unknown> = {
9
+ name: 'cloudflare';
10
+ env: T;
11
+ functionPath: string;
12
+ params: Record<string, string>;
13
+ data: U;
14
+ waitUntil(promise: Promise<any>): void;
15
+ next(request: Request): void;
16
+ };
17
+
18
+ export function getRuntime<T = unknown, U = unknown>(
19
+ request: Request
20
+ ): WorkerRuntime<T> | PagesRuntime<T, U> {
21
+ if (!!request) {
22
+ return Reflect.get(request, Symbol.for('runtime'));
23
+ } else {
24
+ throw new Error(
25
+ 'To retrieve the current cloudflare runtime you need to pass in the Astro request object'
26
+ );
27
+ }
28
+ }
@@ -5,12 +5,13 @@ import { App } from 'astro/app';
5
5
 
6
6
  type Env = {
7
7
  ASSETS: { fetch: (req: Request) => Promise<Response> };
8
+ name: string;
8
9
  };
9
10
 
10
11
  export function createExports(manifest: SSRManifest) {
11
12
  const app = new App(manifest, false);
12
13
 
13
- const fetch = async (request: Request, env: Env) => {
14
+ const fetch = async (request: Request, env: Env, context: any) => {
14
15
  const { origin, pathname } = new URL(request.url);
15
16
 
16
17
  // static assets
@@ -26,6 +27,7 @@ export function createExports(manifest: SSRManifest) {
26
27
  Symbol.for('astro.clientAddress'),
27
28
  request.headers.get('cf-connecting-ip')
28
29
  );
30
+ Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context });
29
31
  let response = await app.render(request, routeData);
30
32
 
31
33
  if (app.setCookieHeaders) {
@@ -9,12 +9,12 @@ export function createExports(manifest: SSRManifest) {
9
9
  const onRequest = async ({
10
10
  request,
11
11
  next,
12
+ ...runtimeEnv
12
13
  }: {
13
14
  request: Request;
14
15
  next: (request: Request) => void;
15
- }) => {
16
+ } & Record<string, unknown>) => {
16
17
  const { origin, pathname } = new URL(request.url);
17
-
18
18
  // static assets
19
19
  if (manifest.assets.has(pathname)) {
20
20
  const assetRequest = new Request(`${origin}/static${pathname}`, request);
@@ -28,6 +28,11 @@ export function createExports(manifest: SSRManifest) {
28
28
  Symbol.for('astro.clientAddress'),
29
29
  request.headers.get('cf-connecting-ip')
30
30
  );
31
+ Reflect.set(request, Symbol.for('runtime'), {
32
+ ...runtimeEnv,
33
+ name: 'cloudflare',
34
+ next,
35
+ });
31
36
  let response = await app.render(request, routeData);
32
37
 
33
38
  if (app.setCookieHeaders) {