@astrojs/cloudflare 0.4.0 → 0.5.0

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 709829411c5ced06
2
- @astrojs/cloudflare:build: 
3
- @astrojs/cloudflare:build: > @astrojs/cloudflare@0.4.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 6b7581791e59c114
2
+ @astrojs/cloudflare:build: 
3
+ @astrojs/cloudflare:build: > @astrojs/cloudflare@0.5.0 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,11 @@
1
1
  # @astrojs/cloudflare
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#3806](https://github.com/withastro/astro/pull/3806) [`f4c571bdb`](https://github.com/withastro/astro/commit/f4c571bdb0bbcd0dfed68a484dfbfe274f8a5f45) Thanks [@nrgnrg](https://github.com/nrgnrg)! - add support for compiling functions to a functions directory rather than `_worker.js`
8
+
3
9
  ## 0.4.0
4
10
 
5
11
  ### Minor Changes
@@ -44,7 +50,7 @@
44
50
  The new `Astro.clientAddress` property allows you to get the IP address of the requested user.
45
51
 
46
52
  ```astro
47
- <div>Your address { Astro.clientAddress }</div>
53
+
48
54
  ```
49
55
 
50
56
  This property is only available when building for SSR, and only if the adapter you are using supports providing the IP address. If you attempt to access the property in a SSG app it will throw an error.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @astrojs/cloudflare
2
2
 
3
- An SSR adapter for use with Cloudflare Pages Functions targets. Write your code in Astro/Node and deploy to Cloudflare Pages.
3
+ An SSR adapter for use with Cloudflare Pages Functions targets. Write your code in Astro/Javascript and deploy to Cloudflare Pages.
4
4
 
5
5
  In your `astro.config.mjs` use:
6
6
 
@@ -14,6 +14,29 @@ export default defineConfig({
14
14
  });
15
15
  ```
16
16
 
17
+ ## Options
18
+
19
+
20
+ ### Mode
21
+
22
+ `mode: "advanced" | "directory"`
23
+
24
+ default `"advanced"`
25
+
26
+ 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.
27
+
28
+ 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.
29
+
30
+ 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 middlewhere which can be checked into version control .
31
+
32
+ ```ts
33
+ // directory mode
34
+ export default defineConfig({
35
+ adapter: cloudflare({ mode: "directory" }),
36
+ });
37
+
38
+ ```
39
+
17
40
  ## Enabling Preview
18
41
 
19
42
  In order for preview to work you must install `wrangler`
package/dist/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
1
  import type { AstroAdapter, AstroIntegration } from 'astro';
2
- export declare function getAdapter(): AstroAdapter;
3
- export default function createIntegration(): AstroIntegration;
2
+ declare type Options = {
3
+ mode: 'directory' | 'advanced';
4
+ };
5
+ export declare function getAdapter(isModeDirectory: boolean): AstroAdapter;
6
+ export default function createIntegration(args?: Options): AstroIntegration;
7
+ export {};
package/dist/index.js CHANGED
@@ -1,10 +1,14 @@
1
1
  import esbuild from "esbuild";
2
2
  import * as fs from "fs";
3
3
  import { fileURLToPath } from "url";
4
- function getAdapter() {
5
- return {
4
+ function getAdapter(isModeDirectory) {
5
+ return isModeDirectory ? {
6
+ name: "@astrojs/cloudflare",
7
+ serverEntrypoint: "@astrojs/cloudflare/server.directory.js",
8
+ exports: ["onRequest"]
9
+ } : {
6
10
  name: "@astrojs/cloudflare",
7
- serverEntrypoint: "@astrojs/cloudflare/server.js",
11
+ serverEntrypoint: "@astrojs/cloudflare/server.advanced.js",
8
12
  exports: ["default"]
9
13
  };
10
14
  }
@@ -12,14 +16,15 @@ const SHIM = `globalThis.process = {
12
16
  argv: [],
13
17
  env: {},
14
18
  };`;
15
- function createIntegration() {
19
+ function createIntegration(args) {
16
20
  let _config;
17
21
  let _buildConfig;
22
+ const isModeDirectory = (args == null ? void 0 : args.mode) === "directory";
18
23
  return {
19
24
  name: "@astrojs/cloudflare",
20
25
  hooks: {
21
26
  "astro:config:done": ({ setAdapter, config }) => {
22
- setAdapter(getAdapter());
27
+ setAdapter(getAdapter(isModeDirectory));
23
28
  _config = config;
24
29
  if (config.output === "static") {
25
30
  throw new Error(`
@@ -30,8 +35,8 @@ function createIntegration() {
30
35
  },
31
36
  "astro:build:start": ({ buildConfig }) => {
32
37
  _buildConfig = buildConfig;
33
- buildConfig.serverEntry = "_worker.js";
34
38
  buildConfig.client = new URL("./static/", _config.outDir);
39
+ buildConfig.serverEntry = "_worker.js";
35
40
  buildConfig.server = new URL("./", _config.outDir);
36
41
  },
37
42
  "astro:build:setup": ({ vite, target }) => {
@@ -70,6 +75,12 @@ function createIntegration() {
70
75
  });
71
76
  const chunksUrl = new URL("./chunks", _buildConfig.server);
72
77
  await fs.promises.rm(chunksUrl, { recursive: true, force: true });
78
+ if (isModeDirectory) {
79
+ const functionsUrl = new URL(`file://${process.cwd()}/functions/`);
80
+ await fs.promises.mkdir(functionsUrl, { recursive: true });
81
+ const directoryUrl = new URL("[[path]].js", functionsUrl);
82
+ await fs.promises.rename(entryUrl, directoryUrl);
83
+ }
73
84
  }
74
85
  }
75
86
  };
File without changes
File without changes
@@ -0,0 +1,8 @@
1
+ import './shim.js';
2
+ import type { SSRManifest } from 'astro';
3
+ export declare function createExports(manifest: SSRManifest): {
4
+ onRequest: ({ request, next, }: {
5
+ request: Request;
6
+ next: (request: Request) => void;
7
+ }) => Promise<void | Response>;
8
+ };
@@ -0,0 +1,32 @@
1
+ import "./shim.js";
2
+ import { App } from "astro/app";
3
+ function createExports(manifest) {
4
+ const app = new App(manifest, false);
5
+ const onRequest = async ({
6
+ request,
7
+ next
8
+ }) => {
9
+ const { origin, pathname } = new URL(request.url);
10
+ if (manifest.assets.has(pathname)) {
11
+ const assetRequest = new Request(`${origin}/static${pathname}`, request);
12
+ return next(assetRequest);
13
+ }
14
+ let routeData = app.match(request, { matchNotFound: true });
15
+ if (routeData) {
16
+ Reflect.set(
17
+ request,
18
+ Symbol.for("astro.clientAddress"),
19
+ request.headers.get("cf-connecting-ip")
20
+ );
21
+ return app.render(request, routeData);
22
+ }
23
+ return new Response(null, {
24
+ status: 404,
25
+ statusText: "Not found"
26
+ });
27
+ };
28
+ return { onRequest };
29
+ }
30
+ export {
31
+ createExports
32
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/cloudflare",
3
3
  "description": "Deploy your site to cloudflare pages functions",
4
- "version": "0.4.0",
4
+ "version": "0.5.0",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -18,16 +18,17 @@
18
18
  "homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
19
19
  "exports": {
20
20
  ".": "./dist/index.js",
21
- "./server.js": "./dist/server.js",
21
+ "./server.advanced.js": "./dist/server.advanced.js",
22
+ "./server.directory.js": "./dist/server.directory.js",
22
23
  "./package.json": "./package.json"
23
24
  },
24
25
  "dependencies": {
25
- "esbuild": "^0.14.42",
26
- "wrangler": "^2.0.23"
26
+ "esbuild": "^0.14.42"
27
27
  },
28
28
  "devDependencies": {
29
- "astro": "1.0.0-rc.2",
30
- "astro-scripts": "0.0.6"
29
+ "astro": "1.0.0-rc.8",
30
+ "astro-scripts": "0.0.6",
31
+ "wrangler": "^2.0.23"
31
32
  },
32
33
  "scripts": {
33
34
  "build": "astro-scripts build \"src/**/*.ts\" && tsc",
package/src/index.ts CHANGED
@@ -3,12 +3,22 @@ import esbuild from 'esbuild';
3
3
  import * as fs from 'fs';
4
4
  import { fileURLToPath } from 'url';
5
5
 
6
- export function getAdapter(): AstroAdapter {
7
- return {
8
- name: '@astrojs/cloudflare',
9
- serverEntrypoint: '@astrojs/cloudflare/server.js',
10
- exports: ['default'],
11
- };
6
+ type Options = {
7
+ mode: 'directory' | 'advanced';
8
+ };
9
+
10
+ export function getAdapter(isModeDirectory: boolean): AstroAdapter {
11
+ return isModeDirectory
12
+ ? {
13
+ name: '@astrojs/cloudflare',
14
+ serverEntrypoint: '@astrojs/cloudflare/server.directory.js',
15
+ exports: ['onRequest'],
16
+ }
17
+ : {
18
+ name: '@astrojs/cloudflare',
19
+ serverEntrypoint: '@astrojs/cloudflare/server.advanced.js',
20
+ exports: ['default'],
21
+ };
12
22
  }
13
23
 
14
24
  const SHIM = `globalThis.process = {
@@ -16,15 +26,16 @@ const SHIM = `globalThis.process = {
16
26
  env: {},
17
27
  };`;
18
28
 
19
- export default function createIntegration(): AstroIntegration {
29
+ export default function createIntegration(args?: Options): AstroIntegration {
20
30
  let _config: AstroConfig;
21
31
  let _buildConfig: BuildConfig;
32
+ const isModeDirectory = args?.mode === 'directory';
22
33
 
23
34
  return {
24
35
  name: '@astrojs/cloudflare',
25
36
  hooks: {
26
37
  'astro:config:done': ({ setAdapter, config }) => {
27
- setAdapter(getAdapter());
38
+ setAdapter(getAdapter(isModeDirectory));
28
39
  _config = config;
29
40
 
30
41
  if (config.output === 'static') {
@@ -36,8 +47,8 @@ export default function createIntegration(): AstroIntegration {
36
47
  },
37
48
  'astro:build:start': ({ buildConfig }) => {
38
49
  _buildConfig = buildConfig;
39
- buildConfig.serverEntry = '_worker.js';
40
50
  buildConfig.client = new URL('./static/', _config.outDir);
51
+ buildConfig.serverEntry = '_worker.js';
41
52
  buildConfig.server = new URL('./', _config.outDir);
42
53
  },
43
54
  'astro:build:setup': ({ vite, target }) => {
@@ -64,7 +75,6 @@ export default function createIntegration(): AstroIntegration {
64
75
  'astro:build:done': async () => {
65
76
  const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
66
77
  const pkg = fileURLToPath(entryUrl);
67
-
68
78
  await esbuild.build({
69
79
  target: 'es2020',
70
80
  platform: 'browser',
@@ -82,6 +92,13 @@ export default function createIntegration(): AstroIntegration {
82
92
  // throw the server folder in the bin
83
93
  const chunksUrl = new URL('./chunks', _buildConfig.server);
84
94
  await fs.promises.rm(chunksUrl, { recursive: true, force: true });
95
+
96
+ if (isModeDirectory) {
97
+ const functionsUrl = new URL(`file://${process.cwd()}/functions/`);
98
+ await fs.promises.mkdir(functionsUrl, { recursive: true });
99
+ const directoryUrl = new URL('[[path]].js', functionsUrl);
100
+ await fs.promises.rename(entryUrl, directoryUrl);
101
+ }
85
102
  },
86
103
  },
87
104
  };
File without changes
@@ -0,0 +1,41 @@
1
+ import './shim.js';
2
+
3
+ import type { SSRManifest } from 'astro';
4
+ import { App } from 'astro/app';
5
+
6
+ export function createExports(manifest: SSRManifest) {
7
+ const app = new App(manifest, false);
8
+
9
+ const onRequest = async ({
10
+ request,
11
+ next,
12
+ }: {
13
+ request: Request;
14
+ next: (request: Request) => void;
15
+ }) => {
16
+ const { origin, pathname } = new URL(request.url);
17
+
18
+ // static assets
19
+ if (manifest.assets.has(pathname)) {
20
+ const assetRequest = new Request(`${origin}/static${pathname}`, request);
21
+ return next(assetRequest);
22
+ }
23
+
24
+ let routeData = app.match(request, { matchNotFound: true });
25
+ if (routeData) {
26
+ Reflect.set(
27
+ request,
28
+ Symbol.for('astro.clientAddress'),
29
+ request.headers.get('cf-connecting-ip')
30
+ );
31
+ return app.render(request, routeData);
32
+ }
33
+
34
+ return new Response(null, {
35
+ status: 404,
36
+ statusText: 'Not found',
37
+ });
38
+ };
39
+
40
+ return { onRequest };
41
+ }
@@ -2,7 +2,7 @@ import { loadFixture, runCLI } from './test-utils.js';
2
2
  import { expect } from 'chai';
3
3
  import * as cheerio from 'cheerio';
4
4
 
5
- describe('Basic app', () => {
5
+ describe.skip('Basic app', () => {
6
6
  /** @type {import('./test-utils').Fixture} */
7
7
  let fixture;
8
8
 
@@ -25,7 +25,7 @@ describe('Basic app', () => {
25
25
  let $ = cheerio.load(html);
26
26
  expect($('h1').text()).to.equal('Testing');
27
27
  } finally {
28
- await stop();
28
+ stop();
29
29
  }
30
30
  });
31
31
  });
@@ -57,11 +57,6 @@ export function runCLI(basePath, { silent }) {
57
57
  ready,
58
58
  stop() {
59
59
  p.kill();
60
- return new Promise((resolve) => {
61
- p.addListener('exit', () => {
62
- resolve();
63
- });
64
- });
65
60
  },
66
61
  };
67
62
  }