@astrojs/cloudflare 3.1.0 → 3.1.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/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +7 -0
- package/README.md +18 -4
- package/dist/runtime.d.ts +16 -0
- package/dist/runtime.js +12 -0
- package/dist/server.advanced.d.ts +2 -1
- package/dist/server.advanced.js +2 -1
- package/dist/server.directory.d.ts +2 -2
- package/dist/server.directory.js +7 -1
- package/package.json +4 -3
- package/src/runtime.ts +28 -0
- package/src/server.advanced.ts +3 -1
- package/src/server.directory.ts +7 -2
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[36m@astrojs/cloudflare:build: [0mcache hit, replaying output [2m5d20b41ab47023ca[0m
|
|
2
|
+
[36m@astrojs/cloudflare:build: [0m
|
|
3
|
+
[36m@astrojs/cloudflare:build: [0m> @astrojs/cloudflare@3.1.1 build /home/runner/work/astro/astro/packages/integrations/cloudflare
|
|
4
|
+
[36m@astrojs/cloudflare:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[36m@astrojs/cloudflare:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @astrojs/cloudflare
|
|
2
2
|
|
|
3
|
+
## 3.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#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]
|
|
8
|
+
- 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.
|
|
9
|
+
|
|
3
10
|
## 3.1.0
|
|
4
11
|
|
|
5
12
|
### 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
|
+
```
|
|
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
|
|
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
|
-
|
|
76
|
-
-
|
|
77
|
-
-
|
|
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>;
|
package/dist/runtime.js
ADDED
|
@@ -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 {};
|
package/dist/server.advanced.js
CHANGED
|
@@ -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
|
};
|
package/dist/server.directory.js
CHANGED
|
@@ -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
|
|
4
|
-
"version": "3.1.
|
|
3
|
+
"description": "Deploy your site to cloudflare workers or cloudflare pages",
|
|
4
|
+
"version": "3.1.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": "./dist/index.js",
|
|
22
|
+
"./runtime": "./dist/runtime.js",
|
|
22
23
|
"./server.advanced.js": "./dist/server.advanced.js",
|
|
23
24
|
"./server.directory.js": "./dist/server.directory.js",
|
|
24
25
|
"./package.json": "./package.json"
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
"esbuild": "^0.14.42"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
|
-
"astro": "1.
|
|
31
|
+
"astro": "1.6.0",
|
|
31
32
|
"astro-scripts": "0.0.8",
|
|
32
33
|
"chai": "^4.3.6",
|
|
33
34
|
"cheerio": "^1.0.0-rc.11",
|
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
|
+
}
|
package/src/server.advanced.ts
CHANGED
|
@@ -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) {
|
package/src/server.directory.ts
CHANGED
|
@@ -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) {
|