@astrojs/cloudflare 3.0.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.
@@ -1,5 +1,5 @@
1
- @astrojs/cloudflare:build: cache hit, replaying output 3257cd3ed40cd0ec
2
- @astrojs/cloudflare:build: 
3
- @astrojs/cloudflare:build: > @astrojs/cloudflare@3.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 5d20b41ab47023ca
2
+ @astrojs/cloudflare:build: 
3
+ @astrojs/cloudflare:build: > @astrojs/cloudflare@3.1.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,56 @@
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
+
10
+ ## 3.1.0
11
+
12
+ ### Minor Changes
13
+
14
+ - [#5056](https://github.com/withastro/astro/pull/5056) [`e55af8a23`](https://github.com/withastro/astro/commit/e55af8a23233b6335f45b7a04b9d026990fb616c) Thanks [@matthewp](https://github.com/matthewp)! - # New build configuration
15
+
16
+ The ability to customize SSR build configuration more granularly is now available in Astro. You can now customize the output folder for `server` (the server code for SSR), `client` (your client-side JavaScript and assets), and `serverEntry` (the name of the entrypoint server module). Here are the defaults:
17
+
18
+ ```js
19
+ import { defineConfig } from 'astro/config';
20
+
21
+ export default defineConfig({
22
+ output: 'server',
23
+ build: {
24
+ server: './dist/server/',
25
+ client: './dist/client/',
26
+ serverEntry: 'entry.mjs',
27
+ },
28
+ });
29
+ ```
30
+
31
+ These new configuration options are only supported in SSR mode and are ignored when building to SSG (a static site).
32
+
33
+ ## Integration hook change
34
+
35
+ The integration hook `astro:build:start` includes a param `buildConfig` which includes all of these same options. You can continue to use this param in Astro 1.x, but it is deprecated in favor of the new `build.config` options. All of the built-in adapters have been updated to the new format. If you have an integration that depends on this param we suggest upgrading to do this instead:
36
+
37
+ ```js
38
+ export default function myIntegration() {
39
+ return {
40
+ name: 'my-integration',
41
+ hooks: {
42
+ 'astro:config:setup': ({ updateConfig }) => {
43
+ updateConfig({
44
+ build: {
45
+ server: '...',
46
+ },
47
+ });
48
+ },
49
+ },
50
+ };
51
+ }
52
+ ```
53
+
3
54
  ## 3.0.0
4
55
 
5
56
  ### Major 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 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
 
package/dist/index.js CHANGED
@@ -19,13 +19,25 @@ const SHIM = `globalThis.process = {
19
19
  function createIntegration(args) {
20
20
  let _config;
21
21
  let _buildConfig;
22
+ let needsBuildConfig = false;
22
23
  const isModeDirectory = (args == null ? void 0 : args.mode) === "directory";
23
24
  return {
24
25
  name: "@astrojs/cloudflare",
25
26
  hooks: {
27
+ "astro:config:setup": ({ config, updateConfig }) => {
28
+ needsBuildConfig = !config.build.client;
29
+ updateConfig({
30
+ build: {
31
+ client: new URL("./static/", config.outDir),
32
+ server: new URL("./", config.outDir),
33
+ serverEntry: "_worker.js"
34
+ }
35
+ });
36
+ },
26
37
  "astro:config:done": ({ setAdapter, config }) => {
27
38
  setAdapter(getAdapter(isModeDirectory));
28
39
  _config = config;
40
+ _buildConfig = config.build;
29
41
  if (config.output === "static") {
30
42
  throw new Error(`
31
43
  [@astrojs/cloudflare] \`output: "server"\` is required to use this adapter. Otherwise, this adapter is not necessary to deploy a static site to Cloudflare.
@@ -33,12 +45,6 @@ function createIntegration(args) {
33
45
  `);
34
46
  }
35
47
  },
36
- "astro:build:start": ({ buildConfig }) => {
37
- _buildConfig = buildConfig;
38
- buildConfig.client = new URL("./static/", _config.outDir);
39
- buildConfig.serverEntry = "_worker.js";
40
- buildConfig.server = new URL("./", _config.outDir);
41
- },
42
48
  "astro:build:setup": ({ vite, target }) => {
43
49
  if (target === "server") {
44
50
  vite.resolve = vite.resolve || {};
@@ -55,6 +61,13 @@ function createIntegration(args) {
55
61
  vite.ssr.target = vite.ssr.target || "webworker";
56
62
  }
57
63
  },
64
+ "astro:build:start": ({ buildConfig }) => {
65
+ if (needsBuildConfig) {
66
+ buildConfig.client = new URL("./static/", _config.outDir);
67
+ buildConfig.server = new URL("./", _config.outDir);
68
+ buildConfig.serverEntry = "_worker.js";
69
+ }
70
+ },
58
71
  "astro:build:done": async () => {
59
72
  const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
60
73
  const pkg = fileURLToPath(entryUrl);
@@ -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.0.0",
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.4.5",
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/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AstroAdapter, AstroConfig, AstroIntegration, BuildConfig } from 'astro';
1
+ import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
2
2
  import esbuild from 'esbuild';
3
3
  import * as fs from 'fs';
4
4
  import { fileURLToPath } from 'url';
@@ -7,6 +7,12 @@ type Options = {
7
7
  mode: 'directory' | 'advanced';
8
8
  };
9
9
 
10
+ interface BuildConfig {
11
+ server: URL;
12
+ client: URL;
13
+ serverEntry: string;
14
+ }
15
+
10
16
  export function getAdapter(isModeDirectory: boolean): AstroAdapter {
11
17
  return isModeDirectory
12
18
  ? {
@@ -29,14 +35,26 @@ const SHIM = `globalThis.process = {
29
35
  export default function createIntegration(args?: Options): AstroIntegration {
30
36
  let _config: AstroConfig;
31
37
  let _buildConfig: BuildConfig;
38
+ let needsBuildConfig = false;
32
39
  const isModeDirectory = args?.mode === 'directory';
33
40
 
34
41
  return {
35
42
  name: '@astrojs/cloudflare',
36
43
  hooks: {
44
+ 'astro:config:setup': ({ config, updateConfig }) => {
45
+ needsBuildConfig = !config.build.client;
46
+ updateConfig({
47
+ build: {
48
+ client: new URL('./static/', config.outDir),
49
+ server: new URL('./', config.outDir),
50
+ serverEntry: '_worker.js',
51
+ },
52
+ });
53
+ },
37
54
  'astro:config:done': ({ setAdapter, config }) => {
38
55
  setAdapter(getAdapter(isModeDirectory));
39
56
  _config = config;
57
+ _buildConfig = config.build;
40
58
 
41
59
  if (config.output === 'static') {
42
60
  throw new Error(`
@@ -45,12 +63,6 @@ export default function createIntegration(args?: Options): AstroIntegration {
45
63
  `);
46
64
  }
47
65
  },
48
- 'astro:build:start': ({ buildConfig }) => {
49
- _buildConfig = buildConfig;
50
- buildConfig.client = new URL('./static/', _config.outDir);
51
- buildConfig.serverEntry = '_worker.js';
52
- buildConfig.server = new URL('./', _config.outDir);
53
- },
54
66
  'astro:build:setup': ({ vite, target }) => {
55
67
  if (target === 'server') {
56
68
  vite.resolve = vite.resolve || {};
@@ -69,6 +81,14 @@ export default function createIntegration(args?: Options): AstroIntegration {
69
81
  vite.ssr.target = vite.ssr.target || 'webworker';
70
82
  }
71
83
  },
84
+ 'astro:build:start': ({ buildConfig }) => {
85
+ // Backwards compat
86
+ if (needsBuildConfig) {
87
+ buildConfig.client = new URL('./static/', _config.outDir);
88
+ buildConfig.server = new URL('./', _config.outDir);
89
+ buildConfig.serverEntry = '_worker.js';
90
+ }
91
+ },
72
92
  'astro:build:done': async () => {
73
93
  const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
74
94
  const pkg = fileURLToPath(entryUrl);
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) {