@astrojs/cloudflare 7.5.3 → 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 +11 -3
- package/dist/entrypoints/server.advanced.d.ts +1 -1
- package/dist/entrypoints/server.advanced.js +39 -43
- package/dist/entrypoints/server.directory.d.ts +1 -1
- package/dist/entrypoints/server.directory.js +41 -45
- package/dist/getAdapter.js +26 -32
- package/dist/index.d.ts +9 -1
- package/dist/index.js +501 -423
- package/dist/util.js +12 -17
- package/dist/utils/deduplicatePatterns.js +22 -14
- package/dist/utils/getCFObject.js +64 -65
- package/dist/utils/parser.js +129 -124
- package/dist/utils/prependForwardSlash.js +2 -5
- package/dist/utils/rewriteWasmImportPath.js +22 -24
- package/dist/utils/wasm-module-loader.js +92 -79
- package/package.json +15 -16
- package/LICENSE +0 -61
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 `
|
|
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
|
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Request as CFRequest
|
|
1
|
+
import type { CacheStorage, ExecutionContext, Request as CFRequest } from '@cloudflare/workers-types';
|
|
2
2
|
import type { SSRManifest } from 'astro';
|
|
3
3
|
type Env = {
|
|
4
4
|
ASSETS: {
|
|
@@ -1,48 +1,44 @@
|
|
|
1
|
-
import { App } from
|
|
2
|
-
import { getProcessEnvProxy, isNode } from
|
|
1
|
+
import { App } from 'astro/app';
|
|
2
|
+
import { getProcessEnvProxy, isNode } from '../util.js';
|
|
3
3
|
if (!isNode) {
|
|
4
|
-
|
|
4
|
+
process.env = getProcessEnvProxy();
|
|
5
5
|
}
|
|
6
|
-
function createExports(manifest) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
Reflect.set(
|
|
17
|
-
request,
|
|
18
|
-
Symbol.for("astro.clientAddress"),
|
|
19
|
-
request.headers.get("cf-connecting-ip")
|
|
20
|
-
);
|
|
21
|
-
const locals = {
|
|
22
|
-
runtime: {
|
|
23
|
-
waitUntil: (promise) => {
|
|
24
|
-
context.waitUntil(promise);
|
|
25
|
-
},
|
|
26
|
-
env,
|
|
27
|
-
cf: request.cf,
|
|
28
|
-
caches
|
|
6
|
+
export function createExports(manifest) {
|
|
7
|
+
const app = new App(manifest);
|
|
8
|
+
const fetch = async (request, env, context) => {
|
|
9
|
+
// TODO: remove this any cast in the future
|
|
10
|
+
// REF: the type cast to any is needed because the Cloudflare Env Type is not assignable to type 'ProcessEnv'
|
|
11
|
+
process.env = env;
|
|
12
|
+
const { pathname } = new URL(request.url);
|
|
13
|
+
// static assets fallback, in case default _routes.json is not used
|
|
14
|
+
if (manifest.assets.has(pathname)) {
|
|
15
|
+
return env.ASSETS.fetch(request);
|
|
29
16
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
let routeData = app.match(request, { matchNotFound: true });
|
|
18
|
+
if (routeData) {
|
|
19
|
+
Reflect.set(request, Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip'));
|
|
20
|
+
const locals = {
|
|
21
|
+
runtime: {
|
|
22
|
+
waitUntil: (promise) => {
|
|
23
|
+
context.waitUntil(promise);
|
|
24
|
+
},
|
|
25
|
+
env: env,
|
|
26
|
+
cf: request.cf,
|
|
27
|
+
caches: caches,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
let response = await app.render(request, routeData, locals);
|
|
31
|
+
if (app.setCookieHeaders) {
|
|
32
|
+
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
|
33
|
+
response.headers.append('Set-Cookie', setCookieHeader);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return response;
|
|
35
37
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
return { default: { fetch } };
|
|
38
|
+
return new Response(null, {
|
|
39
|
+
status: 404,
|
|
40
|
+
statusText: 'Not found',
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
return { default: { fetch } };
|
|
45
44
|
}
|
|
46
|
-
export {
|
|
47
|
-
createExports
|
|
48
|
-
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Request as CFRequest
|
|
1
|
+
import type { CacheStorage, EventContext, Request as CFRequest } from '@cloudflare/workers-types';
|
|
2
2
|
import type { SSRManifest } from 'astro';
|
|
3
3
|
export interface DirectoryRuntime<T extends object = object> {
|
|
4
4
|
runtime: {
|
|
@@ -1,50 +1,46 @@
|
|
|
1
|
-
import { App } from
|
|
2
|
-
import { getProcessEnvProxy, isNode } from
|
|
1
|
+
import { App } from 'astro/app';
|
|
2
|
+
import { getProcessEnvProxy, isNode } from '../util.js';
|
|
3
3
|
if (!isNode) {
|
|
4
|
-
|
|
4
|
+
process.env = getProcessEnvProxy();
|
|
5
5
|
}
|
|
6
|
-
function createExports(manifest) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
Reflect.set(
|
|
19
|
-
request,
|
|
20
|
-
Symbol.for("astro.clientAddress"),
|
|
21
|
-
request.headers.get("cf-connecting-ip")
|
|
22
|
-
);
|
|
23
|
-
const locals = {
|
|
24
|
-
runtime: {
|
|
25
|
-
waitUntil: (promise) => {
|
|
26
|
-
context.waitUntil(promise);
|
|
27
|
-
},
|
|
28
|
-
env: context.env,
|
|
29
|
-
cf: request.cf,
|
|
30
|
-
caches
|
|
6
|
+
export function createExports(manifest) {
|
|
7
|
+
const app = new App(manifest);
|
|
8
|
+
const onRequest = async (context) => {
|
|
9
|
+
const request = context.request;
|
|
10
|
+
const { env } = context;
|
|
11
|
+
// TODO: remove this any cast in the future
|
|
12
|
+
// REF: the type cast to any is needed because the Cloudflare Env Type is not assignable to type 'ProcessEnv'
|
|
13
|
+
process.env = env;
|
|
14
|
+
const { pathname } = new URL(request.url);
|
|
15
|
+
// static assets fallback, in case default _routes.json is not used
|
|
16
|
+
if (manifest.assets.has(pathname)) {
|
|
17
|
+
return env.ASSETS.fetch(request);
|
|
31
18
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
19
|
+
let routeData = app.match(request, { matchNotFound: true });
|
|
20
|
+
if (routeData) {
|
|
21
|
+
Reflect.set(request, Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip'));
|
|
22
|
+
const locals = {
|
|
23
|
+
runtime: {
|
|
24
|
+
waitUntil: (promise) => {
|
|
25
|
+
context.waitUntil(promise);
|
|
26
|
+
},
|
|
27
|
+
env: context.env,
|
|
28
|
+
cf: request.cf,
|
|
29
|
+
caches: caches,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
let response = await app.render(request, routeData, locals);
|
|
33
|
+
if (app.setCookieHeaders) {
|
|
34
|
+
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
|
35
|
+
response.headers.append('Set-Cookie', setCookieHeader);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return response;
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
return { onRequest, manifest };
|
|
40
|
+
return new Response(null, {
|
|
41
|
+
status: 404,
|
|
42
|
+
statusText: 'Not found',
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
return { onRequest, manifest };
|
|
47
46
|
}
|
|
48
|
-
export {
|
|
49
|
-
createExports
|
|
50
|
-
};
|
package/dist/getAdapter.js
CHANGED
|
@@ -1,36 +1,30 @@
|
|
|
1
|
-
function getAdapter({
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
export function getAdapter({ isModeDirectory, functionPerRoute, }) {
|
|
2
|
+
const astroFeatures = {
|
|
3
|
+
hybridOutput: 'stable',
|
|
4
|
+
staticOutput: 'unsupported',
|
|
5
|
+
serverOutput: 'stable',
|
|
6
|
+
assets: {
|
|
7
|
+
supportKind: 'stable',
|
|
8
|
+
isSharpCompatible: false,
|
|
9
|
+
isSquooshCompatible: false,
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
if (isModeDirectory) {
|
|
13
|
+
return {
|
|
14
|
+
name: '@astrojs/cloudflare',
|
|
15
|
+
serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.directory.js',
|
|
16
|
+
exports: ['onRequest', 'manifest'],
|
|
17
|
+
adapterFeatures: {
|
|
18
|
+
functionPerRoute,
|
|
19
|
+
edgeMiddleware: false,
|
|
20
|
+
},
|
|
21
|
+
supportedAstroFeatures: astroFeatures,
|
|
22
|
+
};
|
|
13
23
|
}
|
|
14
|
-
};
|
|
15
|
-
if (isModeDirectory) {
|
|
16
24
|
return {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
functionPerRoute,
|
|
22
|
-
edgeMiddleware: false
|
|
23
|
-
},
|
|
24
|
-
supportedAstroFeatures: astroFeatures
|
|
25
|
+
name: '@astrojs/cloudflare',
|
|
26
|
+
serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.advanced.js',
|
|
27
|
+
exports: ['default'],
|
|
28
|
+
supportedAstroFeatures: astroFeatures,
|
|
25
29
|
};
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
name: "@astrojs/cloudflare",
|
|
29
|
-
serverEntrypoint: "@astrojs/cloudflare/entrypoints/server.advanced.js",
|
|
30
|
-
exports: ["default"],
|
|
31
|
-
supportedAstroFeatures: astroFeatures
|
|
32
|
-
};
|
|
33
30
|
}
|
|
34
|
-
export {
|
|
35
|
-
getAdapter
|
|
36
|
-
};
|
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;
|