@astrojs/cloudflare 7.5.4 → 7.6.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.
package/README.md CHANGED
@@ -186,17 +186,25 @@ export default defineConfig({
186
186
 
187
187
  ### `runtime`
188
188
 
189
- `runtime: "off" | "local"`
189
+ `runtime: { mode: "off" | "local", persistTo: string }`
190
190
 
191
- default `"off"`
191
+ default `{ mode: 'off', persistTo: '' }`
192
192
 
193
193
  Determines whether and how the Cloudflare Runtime is added to `astro dev`.
194
194
 
195
195
  The Cloudflare Runtime includes [Cloudflare bindings](https://developers.cloudflare.com/pages/platform/functions/bindings), [environment variables](https://developers.cloudflare.com/pages/platform/functions/bindings/#environment-variables), and the [cf object](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties). Read more about [accessing the Cloudflare Runtime](#cloudflare-runtime).
196
196
 
197
+ The `mode` property defines how the runtime is added to `astro dev`:
198
+
197
199
  - `local`: uses bindings mocking and locally static placeholders
198
200
  - `off`: no access to the Cloudflare runtime using `astro dev`. You can alternatively use [Preview with Wrangler](#preview-with-wrangler)
199
201
 
202
+ The `persistTo` property defines where the local runtime is persisted to when using `mode: local`. This value is a directory relative to your `astro dev` execution path.
203
+
204
+ The default value is set to `.wrangler/state/v3` to match the default path Wrangler uses. This means both tools are able to access and use the local state.
205
+
206
+ Whichever directory is set in `persistTo`, `.wrangler` or your custom value, must be added to `.gitignore`.
207
+
200
208
  ```diff lang="js"
201
209
  // astro.config.mjs
202
210
  import { defineConfig } from 'astro/config';
@@ -205,7 +213,7 @@ import cloudflare from '@astrojs/cloudflare';
205
213
  export default defineConfig({
206
214
  output: 'server',
207
215
  adapter: cloudflare({
208
- + runtime: 'local',
216
+ + runtime: { mode: 'local' },
209
217
  }),
210
218
  });
211
219
  ```
package/dist/index.d.ts CHANGED
@@ -18,11 +18,19 @@ type Options = {
18
18
  exclude?: string[];
19
19
  };
20
20
  /**
21
+ * Going forward only the object API should be used. The modes work as known before:
21
22
  * 'off': current behaviour (wrangler is needed)
22
23
  * 'local': use a static req.cf object, and env vars defined in wrangler.toml & .dev.vars (astro dev is enough)
23
24
  * 'remote': use a dynamic real-live req.cf object, and env vars defined in wrangler.toml & .dev.vars (astro dev is enough)
24
25
  */
25
- runtime?: 'off' | 'local' | 'remote';
26
+ runtime?: 'off' | 'local' | 'remote' | {
27
+ mode: 'off';
28
+ } | {
29
+ mode: 'remote';
30
+ } | {
31
+ mode: 'local';
32
+ persistTo?: string;
33
+ };
26
34
  wasmModuleImports?: boolean;
27
35
  };
28
36
  export default function createIntegration(args?: Options): AstroIntegration;
package/dist/index.js CHANGED
@@ -14,6 +14,13 @@ import { getD1Bindings, getDOBindings, getEnvVars, getKVBindings, getR2Bindings,
14
14
  import { prependForwardSlash } from './utils/prependForwardSlash.js';
15
15
  import { rewriteWasmImportPath } from './utils/rewriteWasmImportPath.js';
16
16
  import { wasmModuleLoader } from './utils/wasm-module-loader.js';
17
+ const RUNTIME_WARNING = `You are using a deprecated string format for the \`runtime\` API. Please update to the current format for better support.
18
+
19
+ Example:
20
+ Old Format: 'local'
21
+ Current Format: { mode: 'local', persistTo: '.wrangler/state/v3' }
22
+
23
+ Please refer to our runtime documentation for more details on the format. https://docs.astro.build/en/guides/integrations-guide/cloudflare/#runtime`;
17
24
  export default function createIntegration(args) {
18
25
  let _config;
19
26
  let _buildConfig;
@@ -22,7 +29,21 @@ export default function createIntegration(args) {
22
29
  const SERVER_BUILD_FOLDER = '/$server_build/';
23
30
  const isModeDirectory = args?.mode === 'directory';
24
31
  const functionPerRoute = args?.functionPerRoute ?? false;
25
- const runtimeMode = args?.runtime ?? 'off';
32
+ let runtimeMode = { mode: 'off' };
33
+ if (args?.runtime === 'remote') {
34
+ runtimeMode = { mode: 'remote' };
35
+ }
36
+ else if (args?.runtime === 'local') {
37
+ runtimeMode = { mode: 'local', persistTo: '.wrangler/state/v3' };
38
+ }
39
+ else if (typeof args?.runtime === 'object' &&
40
+ args?.runtime.mode === 'local' &&
41
+ args.runtime.persistTo === undefined) {
42
+ runtimeMode = { mode: 'local', persistTo: '.wrangler/state/v3' };
43
+ }
44
+ else {
45
+ runtimeMode = args?.runtime;
46
+ }
26
47
  return {
27
48
  name: '@astrojs/cloudflare',
28
49
  hooks: {
@@ -45,7 +66,7 @@ export default function createIntegration(args) {
45
66
  },
46
67
  });
47
68
  },
48
- 'astro:config:done': ({ setAdapter, config }) => {
69
+ 'astro:config:done': ({ setAdapter, config, logger }) => {
49
70
  setAdapter(getAdapter({ isModeDirectory, functionPerRoute }));
50
71
  _config = config;
51
72
  _buildConfig = config.build;
@@ -55,12 +76,16 @@ export default function createIntegration(args) {
55
76
  if (_config.base === SERVER_BUILD_FOLDER) {
56
77
  throw new AstroError('[@astrojs/cloudflare] `base: "${SERVER_BUILD_FOLDER}"` is not allowed. Please change your `base` config to something else.');
57
78
  }
79
+ if (typeof args?.runtime === 'string') {
80
+ logger.warn(RUNTIME_WARNING);
81
+ }
58
82
  },
59
83
  'astro:server:setup': ({ server }) => {
60
- if (runtimeMode !== 'off') {
84
+ if (runtimeMode.mode === 'local') {
85
+ const typedRuntimeMode = runtimeMode;
61
86
  server.middlewares.use(async function middleware(req, res, next) {
62
87
  try {
63
- const cf = await getCFObject(runtimeMode);
88
+ const cf = await getCFObject(typedRuntimeMode.mode);
64
89
  const vars = await getEnvVars();
65
90
  const D1Bindings = await getD1Bindings();
66
91
  const R2Bindings = await getR2Bindings();
@@ -78,13 +103,13 @@ export default function createIntegration(args) {
78
103
  cachePersist: true,
79
104
  cacheWarnUsage: true,
80
105
  d1Databases: D1Bindings,
81
- d1Persist: true,
106
+ d1Persist: `${typedRuntimeMode.persistTo}/d1`,
82
107
  r2Buckets: R2Bindings,
83
- r2Persist: true,
108
+ r2Persist: `${typedRuntimeMode.persistTo}/r2`,
84
109
  kvNamespaces: KVBindings,
85
- kvPersist: true,
110
+ kvPersist: `${typedRuntimeMode.persistTo}/kv`,
86
111
  durableObjects: DOBindings,
87
- durableObjectsPersist: true,
112
+ durableObjectsPersist: `${typedRuntimeMode.persistTo}/do`,
88
113
  });
89
114
  await _mf.ready;
90
115
  for (const D1Binding of D1Bindings) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "//comment": "test changeset-bot",
3
3
  "name": "@astrojs/cloudflare",
4
4
  "description": "Deploy your site to Cloudflare Workers/Pages",
5
- "version": "7.5.4",
5
+ "version": "7.6.0",
6
6
  "type": "module",
7
7
  "types": "./dist/index.d.ts",
8
8
  "author": "withastro",