@astrojs/cloudflare 0.0.0-content-schemas-20221118232407
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 -0
- package/CHANGELOG.md +256 -0
- package/LICENSE +61 -0
- package/README.md +120 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +102 -0
- package/dist/runtime.d.ts +16 -0
- package/dist/runtime.js +12 -0
- package/dist/server.advanced.d.ts +13 -0
- package/dist/server.advanced.js +38 -0
- package/dist/server.directory.d.ts +7 -0
- package/dist/server.directory.js +46 -0
- package/dist/util.d.ts +1 -0
- package/dist/util.js +15 -0
- package/package.json +50 -0
- package/runtime.d.ts +3 -0
- package/src/index.ts +122 -0
- package/src/runtime.ts +28 -0
- package/src/server.advanced.ts +52 -0
- package/src/server.directory.ts +57 -0
- package/src/util.ts +16 -0
- package/test/basics.test.js +32 -0
- package/test/fixtures/basics/astro.config.mjs +10 -0
- package/test/fixtures/basics/node_modules/.bin/astro +17 -0
- package/test/fixtures/basics/package.json +9 -0
- package/test/fixtures/basics/src/pages/index.astro +9 -0
- package/test/fixtures/no-output/astro.config.mjs +6 -0
- package/test/fixtures/no-output/node_modules/.bin/astro +17 -0
- package/test/fixtures/no-output/package.json +9 -0
- package/test/no-output.test.js +24 -0
- package/test/test-utils.js +62 -0
- package/test/wrangler.toml +4 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
[34m@astrojs/cloudflare:build: [0mcache hit, replaying output [2m73245442b70a93b7[0m
|
|
2
|
+
[34m@astrojs/cloudflare:build: [0m
|
|
3
|
+
[34m@astrojs/cloudflare:build: [0m> @astrojs/cloudflare@0.0.0-content-schemas-20221118232407 build /home/runner/work/astro/astro/packages/integrations/cloudflare
|
|
4
|
+
[34m@astrojs/cloudflare:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[34m@astrojs/cloudflare:build: [0m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# @astrojs/cloudflare
|
|
2
|
+
|
|
3
|
+
## 0.0.0-content-schemas-20221118232407
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`936c1e411`](https://github.com/withastro/astro/commit/936c1e411d77c69b2b60a061c54704200716800a), [`4b188132e`](https://github.com/withastro/astro/commit/4b188132ef68f8d9951cec86418ef50bb4df4a96), [`b44202bc4`](https://github.com/withastro/astro/commit/b44202bc4595b5a35056882b11303f3719061e91), [`f5ed630bc`](https://github.com/withastro/astro/commit/f5ed630bca05ebbfcc6ac994ced3911e41daedcc)]:
|
|
8
|
+
- astro@0.0.0-content-schemas-20221118232407
|
|
9
|
+
|
|
10
|
+
## 4.0.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- [#5301](https://github.com/withastro/astro/pull/5301) [`a79a37cad`](https://github.com/withastro/astro/commit/a79a37cad549b21f91599ff86899e456d9dcc7df) Thanks [@bluwy](https://github.com/bluwy)! - Fix environment variables usage in worker output and warn if environment variables are accessedd too early
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [[`88c1bbe3a`](https://github.com/withastro/astro/commit/88c1bbe3a71f85e92f42f13d0f310c6b2a264ade), [`a79a37cad`](https://github.com/withastro/astro/commit/a79a37cad549b21f91599ff86899e456d9dcc7df)]:
|
|
17
|
+
- astro@1.6.5
|
|
18
|
+
|
|
19
|
+
## 4.0.0
|
|
20
|
+
|
|
21
|
+
### Major Changes
|
|
22
|
+
|
|
23
|
+
- [#5290](https://github.com/withastro/astro/pull/5290) [`b2b291d29`](https://github.com/withastro/astro/commit/b2b291d29143703cece0d12c8e74b2e1151d2061) Thanks [@matthewp](https://github.com/matthewp)! - Handle base configuration in adapters
|
|
24
|
+
|
|
25
|
+
This allows adapters to correctly handle `base` configuration. Internally Astro now matches routes when the URL includes the `base`.
|
|
26
|
+
|
|
27
|
+
Adapters now also have access to the `removeBase` method which will remove the `base` from a pathname. This is useful to look up files for static assets.
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- Updated dependencies [[`b2b291d29`](https://github.com/withastro/astro/commit/b2b291d29143703cece0d12c8e74b2e1151d2061), [`97e2b6ad7`](https://github.com/withastro/astro/commit/97e2b6ad7a6fa23e82be28b2f57cdf3f85fab112), [`4af4d8fa0`](https://github.com/withastro/astro/commit/4af4d8fa0035130fbf31c82d72777c3679bc1ca5), [`f6add3924`](https://github.com/withastro/astro/commit/f6add3924d5cd59925a6ea4bf7f2f731709bc893), [`247eb7411`](https://github.com/withastro/astro/commit/247eb7411f429317e5cd7d401a6660ee73641313)]:
|
|
32
|
+
- astro@1.6.4
|
|
33
|
+
|
|
34
|
+
## 3.1.2
|
|
35
|
+
|
|
36
|
+
### Patch Changes
|
|
37
|
+
|
|
38
|
+
- [#5230](https://github.com/withastro/astro/pull/5230) [`69a532ab6`](https://github.com/withastro/astro/commit/69a532ab60a85d30c2395969593c4d38f9a2fbbe) Thanks [@matthewp](https://github.com/matthewp)! - Exports new runtime entrypoint's types
|
|
39
|
+
|
|
40
|
+
## 3.1.1
|
|
41
|
+
|
|
42
|
+
### Patch Changes
|
|
43
|
+
|
|
44
|
+
- [#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]
|
|
45
|
+
- 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.
|
|
46
|
+
|
|
47
|
+
## 3.1.0
|
|
48
|
+
|
|
49
|
+
### Minor Changes
|
|
50
|
+
|
|
51
|
+
- [#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
|
|
52
|
+
|
|
53
|
+
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:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { defineConfig } from 'astro/config';
|
|
57
|
+
|
|
58
|
+
export default defineConfig({
|
|
59
|
+
output: 'server',
|
|
60
|
+
build: {
|
|
61
|
+
server: './dist/server/',
|
|
62
|
+
client: './dist/client/',
|
|
63
|
+
serverEntry: 'entry.mjs',
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
These new configuration options are only supported in SSR mode and are ignored when building to SSG (a static site).
|
|
69
|
+
|
|
70
|
+
## Integration hook change
|
|
71
|
+
|
|
72
|
+
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:
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
export default function myIntegration() {
|
|
76
|
+
return {
|
|
77
|
+
name: 'my-integration',
|
|
78
|
+
hooks: {
|
|
79
|
+
'astro:config:setup': ({ updateConfig }) => {
|
|
80
|
+
updateConfig({
|
|
81
|
+
build: {
|
|
82
|
+
server: '...',
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 3.0.0
|
|
92
|
+
|
|
93
|
+
### Major Changes
|
|
94
|
+
|
|
95
|
+
- [#4888](https://github.com/withastro/astro/pull/4888) [`2dc582ac5`](https://github.com/withastro/astro/commit/2dc582ac5e2d6e1d434ccfe21616182e453feec3) Thanks [@AirBorne04](https://github.com/AirBorne04)! - adjusting the build settings for cloudflare (reverting back to platform browser over neutral)
|
|
96
|
+
adjusting the ssr settings for solidjs (to build for node)
|
|
97
|
+
|
|
98
|
+
## 2.1.0
|
|
99
|
+
|
|
100
|
+
### Minor Changes
|
|
101
|
+
|
|
102
|
+
- [#4876](https://github.com/withastro/astro/pull/4876) [`d3091f89e`](https://github.com/withastro/astro/commit/d3091f89e92fcfe1ad48daca74055d54b1c853a3) Thanks [@matthewp](https://github.com/matthewp)! - Adds the Astro.cookies API
|
|
103
|
+
|
|
104
|
+
`Astro.cookies` is a new API for manipulating cookies in Astro components and API routes.
|
|
105
|
+
|
|
106
|
+
In Astro components, the new `Astro.cookies` object is a map-like object that allows you to get, set, delete, and check for a cookie's existence (`has`):
|
|
107
|
+
|
|
108
|
+
```astro
|
|
109
|
+
---
|
|
110
|
+
type Prefs = {
|
|
111
|
+
darkMode: boolean;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
Astro.cookies.set<Prefs>(
|
|
115
|
+
'prefs',
|
|
116
|
+
{ darkMode: true },
|
|
117
|
+
{
|
|
118
|
+
expires: '1 month',
|
|
119
|
+
}
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const prefs = Astro.cookies.get<Prefs>('prefs').json();
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
<body data-theme={prefs.darkMode ? 'dark' : 'light'}></body>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Once you've set a cookie with Astro.cookies it will automatically be included in the outgoing response.
|
|
129
|
+
|
|
130
|
+
This API is also available with the same functionality in API routes:
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
export function post({ cookies }) {
|
|
134
|
+
cookies.set('loggedIn', false);
|
|
135
|
+
|
|
136
|
+
return new Response(null, {
|
|
137
|
+
status: 302,
|
|
138
|
+
headers: {
|
|
139
|
+
Location: '/login',
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
See [the RFC](https://github.com/withastro/rfcs/blob/main/proposals/0025-cookie-management.md) to learn more.
|
|
146
|
+
|
|
147
|
+
## 2.0.0
|
|
148
|
+
|
|
149
|
+
### Major Changes
|
|
150
|
+
|
|
151
|
+
- [#4815](https://github.com/withastro/astro/pull/4815) [`ce0b92ba7`](https://github.com/withastro/astro/commit/ce0b92ba73072c0f0143829a53f870155ad4c7ff) Thanks [@AirBorne04](https://github.com/AirBorne04)! - adjusted esbuild config to work with worker environment (fixing solid js ssr)
|
|
152
|
+
|
|
153
|
+
## 1.0.2
|
|
154
|
+
|
|
155
|
+
### Patch Changes
|
|
156
|
+
|
|
157
|
+
- [#4558](https://github.com/withastro/astro/pull/4558) [`742966456`](https://github.com/withastro/astro/commit/7429664566f05ecebf6d57906f950627e62e690c) Thanks [@tony-sull](https://github.com/tony-sull)! - Adding the `withastro` keyword to include the adapters on the [Integrations Catalog](https://astro.build/integrations)
|
|
158
|
+
|
|
159
|
+
## 1.0.1
|
|
160
|
+
|
|
161
|
+
### Patch Changes
|
|
162
|
+
|
|
163
|
+
- [#4232](https://github.com/withastro/astro/pull/4232) [`bfbd32588`](https://github.com/withastro/astro/commit/bfbd32588f7e2c0a9e43cd1a571a0dc9c5f7e645) Thanks [@Ekwuno](https://github.com/Ekwuno)! - Update README
|
|
164
|
+
|
|
165
|
+
## 1.0.0
|
|
166
|
+
|
|
167
|
+
### Major Changes
|
|
168
|
+
|
|
169
|
+
- [`04ad44563`](https://github.com/withastro/astro/commit/04ad445632c67bdd60c1704e1e0dcbcaa27b9308) - > Astro v1.0 is out! Read the [official announcement post](https://astro.build/blog/astro-1/).
|
|
170
|
+
|
|
171
|
+
**No breaking changes**. This package is now officially stable and compatible with `astro@1.0.0`!
|
|
172
|
+
|
|
173
|
+
## 0.5.0
|
|
174
|
+
|
|
175
|
+
### Minor Changes
|
|
176
|
+
|
|
177
|
+
- [#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`
|
|
178
|
+
|
|
179
|
+
## 0.4.0
|
|
180
|
+
|
|
181
|
+
### Minor Changes
|
|
182
|
+
|
|
183
|
+
- [#4068](https://github.com/withastro/astro/pull/4068) [`54b33d50f`](https://github.com/withastro/astro/commit/54b33d50fdb995ac056461be7e2128d911624f2d) Thanks [@matthewp](https://github.com/matthewp)! - Add explicit errors when omitting output config
|
|
184
|
+
|
|
185
|
+
### Patch Changes
|
|
186
|
+
|
|
187
|
+
- [#4072](https://github.com/withastro/astro/pull/4072) [`a198028b0`](https://github.com/withastro/astro/commit/a198028b04234d0b8dcb0b6bcb47c5831d7a15f9) Thanks [@matthewp](https://github.com/matthewp)! - Fixes Cloudflare throwing an error for process
|
|
188
|
+
|
|
189
|
+
## 0.3.0
|
|
190
|
+
|
|
191
|
+
### Minor Changes
|
|
192
|
+
|
|
193
|
+
- [#4015](https://github.com/withastro/astro/pull/4015) [`6fd161d76`](https://github.com/withastro/astro/commit/6fd161d7691cbf9d3ffa4646e46059dfd0940010) Thanks [@matthewp](https://github.com/matthewp)! - New `output` configuration option
|
|
194
|
+
|
|
195
|
+
This change introduces a new "output target" configuration option (`output`). Setting the output target lets you decide the format of your final build, either:
|
|
196
|
+
|
|
197
|
+
- `"static"` (default): A static site. Your final build will be a collection of static assets (HTML, CSS, JS) that you can deploy to any static site host.
|
|
198
|
+
- `"server"`: A dynamic server application. Your final build will be an application that will run in a hosted server environment, generating HTML dynamically for different requests.
|
|
199
|
+
|
|
200
|
+
If `output` is omitted from your config, the default value `"static"` will be used.
|
|
201
|
+
|
|
202
|
+
When using the `"server"` output target, you must also include a runtime adapter via the `adapter` configuration. An adapter will _adapt_ your final build to run on the deployed platform of your choice (Netlify, Vercel, Node.js, Deno, etc).
|
|
203
|
+
|
|
204
|
+
To migrate: No action is required for most users. If you currently define an `adapter`, you will need to also add `output: 'server'` to your config file to make it explicit that you are building a server. Here is an example of what that change would look like for someone deploying to Netlify:
|
|
205
|
+
|
|
206
|
+
```diff
|
|
207
|
+
import { defineConfig } from 'astro/config';
|
|
208
|
+
import netlify from '@astrojs/netlify/functions';
|
|
209
|
+
|
|
210
|
+
export default defineConfig({
|
|
211
|
+
adapter: netlify(),
|
|
212
|
+
+ output: 'server',
|
|
213
|
+
});
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
* [#4018](https://github.com/withastro/astro/pull/4018) [`0cc6ede36`](https://github.com/withastro/astro/commit/0cc6ede362996b9faba57481a790d6eb7fba2045) Thanks [@okikio](https://github.com/okikio)! - Support for 404 and 500 pages in SSR
|
|
217
|
+
|
|
218
|
+
- [#3973](https://github.com/withastro/astro/pull/3973) [`5a23483ef`](https://github.com/withastro/astro/commit/5a23483efb3ba614b05a00064f84415620605204) Thanks [@matthewp](https://github.com/matthewp)! - Adds support for Astro.clientAddress
|
|
219
|
+
|
|
220
|
+
The new `Astro.clientAddress` property allows you to get the IP address of the requested user.
|
|
221
|
+
|
|
222
|
+
```astro
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
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.
|
|
227
|
+
|
|
228
|
+
## 0.2.4
|
|
229
|
+
|
|
230
|
+
### Patch Changes
|
|
231
|
+
|
|
232
|
+
- [#3885](https://github.com/withastro/astro/pull/3885) [`bf5d1cc1e`](https://github.com/withastro/astro/commit/bf5d1cc1e71da38a14658c615e9481f2145cc6e7) Thanks [@delucis](https://github.com/delucis)! - Integration README fixes
|
|
233
|
+
|
|
234
|
+
## 0.2.3
|
|
235
|
+
|
|
236
|
+
### Patch Changes
|
|
237
|
+
|
|
238
|
+
- [#3854](https://github.com/withastro/astro/pull/3854) [`b012ee55`](https://github.com/withastro/astro/commit/b012ee55b107dea0730286263b27d83e530fad5d) Thanks [@bholmesdev](https://github.com/bholmesdev)! - [astro add] Support adapters and third party packages
|
|
239
|
+
|
|
240
|
+
## 0.2.2
|
|
241
|
+
|
|
242
|
+
### Patch Changes
|
|
243
|
+
|
|
244
|
+
- [#3777](https://github.com/withastro/astro/pull/3777) [`976e1f17`](https://github.com/withastro/astro/commit/976e1f175a95ea39f737b8575e4fdf3c3d89e1ee) Thanks [@tony-sull](https://github.com/tony-sull)! - Disables HTTP streaming in Cloudflare Pages deployments
|
|
245
|
+
|
|
246
|
+
## 0.2.1
|
|
247
|
+
|
|
248
|
+
### Patch Changes
|
|
249
|
+
|
|
250
|
+
- [#3695](https://github.com/withastro/astro/pull/3695) [`0d667d0e`](https://github.com/withastro/astro/commit/0d667d0e572d76d4c819816ddf51ed14b43e2551) Thanks [@nrgnrg](https://github.com/nrgnrg)! - fix custom 404 pages not rendering
|
|
251
|
+
|
|
252
|
+
## 0.2.0
|
|
253
|
+
|
|
254
|
+
### Minor Changes
|
|
255
|
+
|
|
256
|
+
- [#3600](https://github.com/withastro/astro/pull/3600) [`7f423581`](https://github.com/withastro/astro/commit/7f423581411648c9a69b68918ff930581f12cf16) Thanks [@nrgnrg](https://github.com/nrgnrg)! - add SSR adaptor for Cloudflare Pages functions
|
package/LICENSE
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Fred K. Schott
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
|
|
26
|
+
|
|
27
|
+
Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
|
|
28
|
+
|
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
32
|
+
|
|
33
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
|
|
39
|
+
|
|
40
|
+
MIT License
|
|
41
|
+
|
|
42
|
+
Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
|
|
43
|
+
|
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
45
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
46
|
+
in the Software without restriction, including without limitation the rights
|
|
47
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
48
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
49
|
+
furnished to do so, subject to the following conditions:
|
|
50
|
+
|
|
51
|
+
The above copyright notice and this permission notice shall be included in all
|
|
52
|
+
copies or substantial portions of the Software.
|
|
53
|
+
|
|
54
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
55
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
56
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
57
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
58
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
59
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
60
|
+
SOFTWARE.
|
|
61
|
+
"""
|
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# @astrojs/cloudflare
|
|
2
|
+
|
|
3
|
+
An SSR adapter for use with Cloudflare Pages Functions targets. Write your code in Astro/Javascript and deploy to Cloudflare Pages.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Add the Cloudflare adapter to enable SSR in your Astro project with the following `astro add` command. This will install the adapter and make the appropriate changes to your `astro.config.mjs` file in one step.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
# Using NPM
|
|
11
|
+
npx astro add cloudflare
|
|
12
|
+
# Using Yarn
|
|
13
|
+
yarn astro add cloudflare
|
|
14
|
+
# Using PNPM
|
|
15
|
+
pnpm astro add cloudflare
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
If you prefer to install the adapter manually instead, complete the following two steps:
|
|
19
|
+
|
|
20
|
+
1. Add the Cloudflare adapter to your project's dependencies using your preferred package manager. If you’re using npm or aren’t sure, run this in the terminal:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @astrojs/cloudflare
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
2. Add the following to your `astro.config.mjs` file:
|
|
27
|
+
|
|
28
|
+
```js title="astro.config.mjs" ins={2, 5-6}
|
|
29
|
+
import { defineConfig } from 'astro/config';
|
|
30
|
+
import cloudflare from '@astrojs/cloudflare';
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
output: 'server',
|
|
34
|
+
adapter: cloudflare()
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Options
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### Mode
|
|
42
|
+
|
|
43
|
+
`mode: "advanced" | "directory"`
|
|
44
|
+
|
|
45
|
+
default `"advanced"`
|
|
46
|
+
|
|
47
|
+
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.
|
|
48
|
+
|
|
49
|
+
For most projects the adaptor default of `advanced` will be sufficient; the `dist` folder will contain your compiled project. Switching to directory mode allows you to use [pages plugins](https://developers.cloudflare.com/pages/platform/functions/plugins/) such as [Sentry](https://developers.cloudflare.com/pages/platform/functions/plugins/sentry/) or write custom code to enable logging.
|
|
50
|
+
|
|
51
|
+
In directory mode the adaptor will compile the client side part of you app the same way, but moves 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 middleware which can be checked into version control. Cloudflare documentation contains more information about [writing custom functions](https://developers.cloudflare.com/pages/platform/functions/).
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// directory mode
|
|
55
|
+
export default defineConfig({
|
|
56
|
+
adapter: cloudflare({ mode: "directory" }),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Enabling Preview
|
|
62
|
+
|
|
63
|
+
In order for preview to work you must install `wrangler`
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
$ pnpm install wrangler --save-dev
|
|
67
|
+
```
|
|
68
|
+
|
|
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
|
+
|
|
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
|
+
```js
|
|
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
|
+
|
|
83
|
+
## Streams
|
|
84
|
+
|
|
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.
|
|
86
|
+
|
|
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
|
|
92
|
+
|
|
93
|
+
## Environment Variables
|
|
94
|
+
|
|
95
|
+
As Cloudflare Pages Functions [provides environment variables per request](https://developers.cloudflare.com/pages/platform/functions/#adding-environment-variables-locally), you can only access private environment variables when a request has happened. Usually, this means moving environment variable access inside a function.
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
// pages/[id].json.js
|
|
99
|
+
|
|
100
|
+
export function get({ params }) {
|
|
101
|
+
// Access environment variables per request inside a function
|
|
102
|
+
const serverUrl = import.meta.env.SERVER_URL;
|
|
103
|
+
const result = await fetch(serverUrl + "/user/" + params.id);
|
|
104
|
+
return {
|
|
105
|
+
body: await result.text(),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Troubleshooting
|
|
111
|
+
|
|
112
|
+
For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
|
|
113
|
+
|
|
114
|
+
You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
|
|
115
|
+
|
|
116
|
+
## Contributing
|
|
117
|
+
|
|
118
|
+
This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
|
|
119
|
+
|
|
120
|
+
[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AstroAdapter, AstroIntegration } from 'astro';
|
|
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
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import esbuild from "esbuild";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
function getAdapter(isModeDirectory) {
|
|
5
|
+
return isModeDirectory ? {
|
|
6
|
+
name: "@astrojs/cloudflare",
|
|
7
|
+
serverEntrypoint: "@astrojs/cloudflare/server.directory.js",
|
|
8
|
+
exports: ["onRequest"]
|
|
9
|
+
} : {
|
|
10
|
+
name: "@astrojs/cloudflare",
|
|
11
|
+
serverEntrypoint: "@astrojs/cloudflare/server.advanced.js",
|
|
12
|
+
exports: ["default"]
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const SHIM = `globalThis.process = {
|
|
16
|
+
argv: [],
|
|
17
|
+
env: {},
|
|
18
|
+
};`;
|
|
19
|
+
function createIntegration(args) {
|
|
20
|
+
let _config;
|
|
21
|
+
let _buildConfig;
|
|
22
|
+
let needsBuildConfig = false;
|
|
23
|
+
const isModeDirectory = (args == null ? void 0 : args.mode) === "directory";
|
|
24
|
+
return {
|
|
25
|
+
name: "@astrojs/cloudflare",
|
|
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
|
+
},
|
|
37
|
+
"astro:config:done": ({ setAdapter, config }) => {
|
|
38
|
+
setAdapter(getAdapter(isModeDirectory));
|
|
39
|
+
_config = config;
|
|
40
|
+
_buildConfig = config.build;
|
|
41
|
+
if (config.output === "static") {
|
|
42
|
+
throw new Error(`
|
|
43
|
+
[@astrojs/cloudflare] \`output: "server"\` is required to use this adapter. Otherwise, this adapter is not necessary to deploy a static site to Cloudflare.
|
|
44
|
+
|
|
45
|
+
`);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"astro:build:setup": ({ vite, target }) => {
|
|
49
|
+
if (target === "server") {
|
|
50
|
+
vite.resolve = vite.resolve || {};
|
|
51
|
+
vite.resolve.alias = vite.resolve.alias || {};
|
|
52
|
+
const aliases = [{ find: "react-dom/server", replacement: "react-dom/server.browser" }];
|
|
53
|
+
if (Array.isArray(vite.resolve.alias)) {
|
|
54
|
+
vite.resolve.alias = [...vite.resolve.alias, ...aliases];
|
|
55
|
+
} else {
|
|
56
|
+
for (const alias of aliases) {
|
|
57
|
+
vite.resolve.alias[alias.find] = alias.replacement;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
vite.ssr = vite.ssr || {};
|
|
61
|
+
vite.ssr.target = vite.ssr.target || "webworker";
|
|
62
|
+
}
|
|
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
|
+
},
|
|
71
|
+
"astro:build:done": async () => {
|
|
72
|
+
const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
|
|
73
|
+
const pkg = fileURLToPath(entryUrl);
|
|
74
|
+
await esbuild.build({
|
|
75
|
+
target: "es2020",
|
|
76
|
+
platform: "browser",
|
|
77
|
+
entryPoints: [pkg],
|
|
78
|
+
outfile: pkg,
|
|
79
|
+
allowOverwrite: true,
|
|
80
|
+
format: "esm",
|
|
81
|
+
bundle: true,
|
|
82
|
+
minify: true,
|
|
83
|
+
banner: {
|
|
84
|
+
js: SHIM
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const chunksUrl = new URL("./chunks", _buildConfig.server);
|
|
88
|
+
await fs.promises.rm(chunksUrl, { recursive: true, force: true });
|
|
89
|
+
if (isModeDirectory) {
|
|
90
|
+
const functionsUrl = new URL(`file://${process.cwd()}/functions/`);
|
|
91
|
+
await fs.promises.mkdir(functionsUrl, { recursive: true });
|
|
92
|
+
const directoryUrl = new URL("[[path]].js", functionsUrl);
|
|
93
|
+
await fs.promises.rename(entryUrl, directoryUrl);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
export {
|
|
100
|
+
createIntegration as default,
|
|
101
|
+
getAdapter
|
|
102
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SSRManifest } from 'astro';
|
|
2
|
+
declare type Env = {
|
|
3
|
+
ASSETS: {
|
|
4
|
+
fetch: (req: Request) => Promise<Response>;
|
|
5
|
+
};
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function createExports(manifest: SSRManifest): {
|
|
9
|
+
default: {
|
|
10
|
+
fetch: (request: Request, env: Env, context: any) => Promise<Response>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { App } from "astro/app";
|
|
2
|
+
import { getProcessEnvProxy } from "./util.js";
|
|
3
|
+
process.env = getProcessEnvProxy();
|
|
4
|
+
function createExports(manifest) {
|
|
5
|
+
const app = new App(manifest, false);
|
|
6
|
+
const fetch = async (request, env, context) => {
|
|
7
|
+
process.env = env;
|
|
8
|
+
const { origin, pathname } = new URL(request.url);
|
|
9
|
+
if (manifest.assets.has(pathname)) {
|
|
10
|
+
const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
|
|
11
|
+
return env.ASSETS.fetch(assetRequest);
|
|
12
|
+
}
|
|
13
|
+
let routeData = app.match(request, { matchNotFound: true });
|
|
14
|
+
if (routeData) {
|
|
15
|
+
Reflect.set(
|
|
16
|
+
request,
|
|
17
|
+
Symbol.for("astro.clientAddress"),
|
|
18
|
+
request.headers.get("cf-connecting-ip")
|
|
19
|
+
);
|
|
20
|
+
Reflect.set(request, Symbol.for("runtime"), { env, name: "cloudflare", ...context });
|
|
21
|
+
let response = await app.render(request, routeData);
|
|
22
|
+
if (app.setCookieHeaders) {
|
|
23
|
+
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
|
24
|
+
response.headers.append("Set-Cookie", setCookieHeader);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return response;
|
|
28
|
+
}
|
|
29
|
+
return new Response(null, {
|
|
30
|
+
status: 404,
|
|
31
|
+
statusText: "Not found"
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
return { default: { fetch } };
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
createExports
|
|
38
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SSRManifest } from 'astro';
|
|
2
|
+
export declare function createExports(manifest: SSRManifest): {
|
|
3
|
+
onRequest: ({ request, next, ...runtimeEnv }: {
|
|
4
|
+
request: Request;
|
|
5
|
+
next: (request: Request) => void;
|
|
6
|
+
} & Record<string, unknown>) => Promise<void | Response>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { App } from "astro/app";
|
|
2
|
+
import { getProcessEnvProxy } from "./util.js";
|
|
3
|
+
process.env = getProcessEnvProxy();
|
|
4
|
+
function createExports(manifest) {
|
|
5
|
+
const app = new App(manifest, false);
|
|
6
|
+
const onRequest = async ({
|
|
7
|
+
request,
|
|
8
|
+
next,
|
|
9
|
+
...runtimeEnv
|
|
10
|
+
}) => {
|
|
11
|
+
process.env = runtimeEnv.env;
|
|
12
|
+
const { origin, pathname } = new URL(request.url);
|
|
13
|
+
if (manifest.assets.has(pathname)) {
|
|
14
|
+
const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
|
|
15
|
+
return next(assetRequest);
|
|
16
|
+
}
|
|
17
|
+
let routeData = app.match(request, { matchNotFound: true });
|
|
18
|
+
if (routeData) {
|
|
19
|
+
Reflect.set(
|
|
20
|
+
request,
|
|
21
|
+
Symbol.for("astro.clientAddress"),
|
|
22
|
+
request.headers.get("cf-connecting-ip")
|
|
23
|
+
);
|
|
24
|
+
Reflect.set(request, Symbol.for("runtime"), {
|
|
25
|
+
...runtimeEnv,
|
|
26
|
+
name: "cloudflare",
|
|
27
|
+
next
|
|
28
|
+
});
|
|
29
|
+
let response = await app.render(request, routeData);
|
|
30
|
+
if (app.setCookieHeaders) {
|
|
31
|
+
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
|
32
|
+
response.headers.append("Set-Cookie", setCookieHeader);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
return new Response(null, {
|
|
38
|
+
status: 404,
|
|
39
|
+
statusText: "Not found"
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
return { onRequest };
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
createExports
|
|
46
|
+
};
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getProcessEnvProxy(): {};
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function getProcessEnvProxy() {
|
|
2
|
+
return new Proxy(
|
|
3
|
+
{},
|
|
4
|
+
{
|
|
5
|
+
get: (target, prop) => {
|
|
6
|
+
console.warn(
|
|
7
|
+
`Unable to access \`import.meta\0.env.${prop.toString()}\` on initialization as the Cloudflare platform only provides the environment variables per request. Please move the environment variable access inside a function that's only called after a request has been received.`
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
getProcessEnvProxy
|
|
15
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@astrojs/cloudflare",
|
|
3
|
+
"description": "Deploy your site to cloudflare workers or cloudflare pages",
|
|
4
|
+
"version": "0.0.0-content-schemas-20221118232407",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"author": "withastro",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/withastro/astro.git",
|
|
12
|
+
"directory": "packages/integrations/cloudflare"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"withastro",
|
|
16
|
+
"astro-adapter"
|
|
17
|
+
],
|
|
18
|
+
"bugs": "https://github.com/withastro/astro/issues",
|
|
19
|
+
"homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./dist/index.js",
|
|
22
|
+
"./runtime": {
|
|
23
|
+
"types": "./runtime.d.ts",
|
|
24
|
+
"default": "./dist/runtime.js"
|
|
25
|
+
},
|
|
26
|
+
"./server.advanced.js": "./dist/server.advanced.js",
|
|
27
|
+
"./server.directory.js": "./dist/server.directory.js",
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"esbuild": "^0.14.42"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"astro": "0.0.0-content-schemas-20221118232407"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"astro": "0.0.0-content-schemas-20221118232407",
|
|
38
|
+
"astro-scripts": "0.0.9",
|
|
39
|
+
"chai": "^4.3.6",
|
|
40
|
+
"cheerio": "^1.0.0-rc.11",
|
|
41
|
+
"mocha": "^9.2.2",
|
|
42
|
+
"wrangler": "^2.0.23"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
|
46
|
+
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
|
47
|
+
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
|
48
|
+
"test": "mocha --exit --timeout 30000 test/"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/runtime.d.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
|
|
2
|
+
import esbuild from 'esbuild';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
|
|
6
|
+
type Options = {
|
|
7
|
+
mode: 'directory' | 'advanced';
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
interface BuildConfig {
|
|
11
|
+
server: URL;
|
|
12
|
+
client: URL;
|
|
13
|
+
serverEntry: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getAdapter(isModeDirectory: boolean): AstroAdapter {
|
|
17
|
+
return isModeDirectory
|
|
18
|
+
? {
|
|
19
|
+
name: '@astrojs/cloudflare',
|
|
20
|
+
serverEntrypoint: '@astrojs/cloudflare/server.directory.js',
|
|
21
|
+
exports: ['onRequest'],
|
|
22
|
+
}
|
|
23
|
+
: {
|
|
24
|
+
name: '@astrojs/cloudflare',
|
|
25
|
+
serverEntrypoint: '@astrojs/cloudflare/server.advanced.js',
|
|
26
|
+
exports: ['default'],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const SHIM = `globalThis.process = {
|
|
31
|
+
argv: [],
|
|
32
|
+
env: {},
|
|
33
|
+
};`;
|
|
34
|
+
|
|
35
|
+
export default function createIntegration(args?: Options): AstroIntegration {
|
|
36
|
+
let _config: AstroConfig;
|
|
37
|
+
let _buildConfig: BuildConfig;
|
|
38
|
+
let needsBuildConfig = false;
|
|
39
|
+
const isModeDirectory = args?.mode === 'directory';
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
name: '@astrojs/cloudflare',
|
|
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
|
+
},
|
|
54
|
+
'astro:config:done': ({ setAdapter, config }) => {
|
|
55
|
+
setAdapter(getAdapter(isModeDirectory));
|
|
56
|
+
_config = config;
|
|
57
|
+
_buildConfig = config.build;
|
|
58
|
+
|
|
59
|
+
if (config.output === 'static') {
|
|
60
|
+
throw new Error(`
|
|
61
|
+
[@astrojs/cloudflare] \`output: "server"\` is required to use this adapter. Otherwise, this adapter is not necessary to deploy a static site to Cloudflare.
|
|
62
|
+
|
|
63
|
+
`);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
'astro:build:setup': ({ vite, target }) => {
|
|
67
|
+
if (target === 'server') {
|
|
68
|
+
vite.resolve = vite.resolve || {};
|
|
69
|
+
vite.resolve.alias = vite.resolve.alias || {};
|
|
70
|
+
|
|
71
|
+
const aliases = [{ find: 'react-dom/server', replacement: 'react-dom/server.browser' }];
|
|
72
|
+
|
|
73
|
+
if (Array.isArray(vite.resolve.alias)) {
|
|
74
|
+
vite.resolve.alias = [...vite.resolve.alias, ...aliases];
|
|
75
|
+
} else {
|
|
76
|
+
for (const alias of aliases) {
|
|
77
|
+
(vite.resolve.alias as Record<string, string>)[alias.find] = alias.replacement;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
vite.ssr = vite.ssr || {};
|
|
81
|
+
vite.ssr.target = vite.ssr.target || 'webworker';
|
|
82
|
+
}
|
|
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
|
+
},
|
|
92
|
+
'astro:build:done': async () => {
|
|
93
|
+
const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
|
|
94
|
+
const pkg = fileURLToPath(entryUrl);
|
|
95
|
+
await esbuild.build({
|
|
96
|
+
target: 'es2020',
|
|
97
|
+
platform: 'browser',
|
|
98
|
+
entryPoints: [pkg],
|
|
99
|
+
outfile: pkg,
|
|
100
|
+
allowOverwrite: true,
|
|
101
|
+
format: 'esm',
|
|
102
|
+
bundle: true,
|
|
103
|
+
minify: true,
|
|
104
|
+
banner: {
|
|
105
|
+
js: SHIM,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// throw the server folder in the bin
|
|
110
|
+
const chunksUrl = new URL('./chunks', _buildConfig.server);
|
|
111
|
+
await fs.promises.rm(chunksUrl, { recursive: true, force: true });
|
|
112
|
+
|
|
113
|
+
if (isModeDirectory) {
|
|
114
|
+
const functionsUrl = new URL(`file://${process.cwd()}/functions/`);
|
|
115
|
+
await fs.promises.mkdir(functionsUrl, { recursive: true });
|
|
116
|
+
const directoryUrl = new URL('[[path]].js', functionsUrl);
|
|
117
|
+
await fs.promises.rename(entryUrl, directoryUrl);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
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
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SSRManifest } from 'astro';
|
|
2
|
+
import { App } from 'astro/app';
|
|
3
|
+
import { getProcessEnvProxy } from './util.js';
|
|
4
|
+
|
|
5
|
+
process.env = getProcessEnvProxy();
|
|
6
|
+
|
|
7
|
+
type Env = {
|
|
8
|
+
ASSETS: { fetch: (req: Request) => Promise<Response> };
|
|
9
|
+
name: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function createExports(manifest: SSRManifest) {
|
|
13
|
+
const app = new App(manifest, false);
|
|
14
|
+
|
|
15
|
+
const fetch = async (request: Request, env: Env, context: any) => {
|
|
16
|
+
process.env = env as any;
|
|
17
|
+
|
|
18
|
+
const { origin, pathname } = new URL(request.url);
|
|
19
|
+
|
|
20
|
+
// static assets
|
|
21
|
+
if (manifest.assets.has(pathname)) {
|
|
22
|
+
const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
|
|
23
|
+
return env.ASSETS.fetch(assetRequest);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let routeData = app.match(request, { matchNotFound: true });
|
|
27
|
+
if (routeData) {
|
|
28
|
+
Reflect.set(
|
|
29
|
+
request,
|
|
30
|
+
Symbol.for('astro.clientAddress'),
|
|
31
|
+
request.headers.get('cf-connecting-ip')
|
|
32
|
+
);
|
|
33
|
+
Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context });
|
|
34
|
+
let response = await app.render(request, routeData);
|
|
35
|
+
|
|
36
|
+
if (app.setCookieHeaders) {
|
|
37
|
+
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
|
38
|
+
response.headers.append('Set-Cookie', setCookieHeader);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return response;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return new Response(null, {
|
|
46
|
+
status: 404,
|
|
47
|
+
statusText: 'Not found',
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return { default: { fetch } };
|
|
52
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { SSRManifest } from 'astro';
|
|
2
|
+
import { App } from 'astro/app';
|
|
3
|
+
import { getProcessEnvProxy } from './util.js';
|
|
4
|
+
|
|
5
|
+
process.env = getProcessEnvProxy();
|
|
6
|
+
|
|
7
|
+
export function createExports(manifest: SSRManifest) {
|
|
8
|
+
const app = new App(manifest, false);
|
|
9
|
+
|
|
10
|
+
const onRequest = async ({
|
|
11
|
+
request,
|
|
12
|
+
next,
|
|
13
|
+
...runtimeEnv
|
|
14
|
+
}: {
|
|
15
|
+
request: Request;
|
|
16
|
+
next: (request: Request) => void;
|
|
17
|
+
} & Record<string, unknown>) => {
|
|
18
|
+
process.env = runtimeEnv.env as any;
|
|
19
|
+
|
|
20
|
+
const { origin, pathname } = new URL(request.url);
|
|
21
|
+
// static assets
|
|
22
|
+
if (manifest.assets.has(pathname)) {
|
|
23
|
+
const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
|
|
24
|
+
return next(assetRequest);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let routeData = app.match(request, { matchNotFound: true });
|
|
28
|
+
if (routeData) {
|
|
29
|
+
Reflect.set(
|
|
30
|
+
request,
|
|
31
|
+
Symbol.for('astro.clientAddress'),
|
|
32
|
+
request.headers.get('cf-connecting-ip')
|
|
33
|
+
);
|
|
34
|
+
Reflect.set(request, Symbol.for('runtime'), {
|
|
35
|
+
...runtimeEnv,
|
|
36
|
+
name: 'cloudflare',
|
|
37
|
+
next,
|
|
38
|
+
});
|
|
39
|
+
let response = await app.render(request, routeData);
|
|
40
|
+
|
|
41
|
+
if (app.setCookieHeaders) {
|
|
42
|
+
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
|
43
|
+
response.headers.append('Set-Cookie', setCookieHeader);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return response;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return new Response(null, {
|
|
51
|
+
status: 404,
|
|
52
|
+
statusText: 'Not found',
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return { onRequest };
|
|
57
|
+
}
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function getProcessEnvProxy() {
|
|
2
|
+
return new Proxy(
|
|
3
|
+
{},
|
|
4
|
+
{
|
|
5
|
+
get: (target, prop) => {
|
|
6
|
+
console.warn(
|
|
7
|
+
// NOTE: \0 prevents Vite replacement
|
|
8
|
+
`Unable to access \`import.meta\0.env.${prop.toString()}\` on initialization ` +
|
|
9
|
+
`as the Cloudflare platform only provides the environment variables per request. ` +
|
|
10
|
+
`Please move the environment variable access inside a function ` +
|
|
11
|
+
`that's only called after a request has been received.`
|
|
12
|
+
);
|
|
13
|
+
},
|
|
14
|
+
}
|
|
15
|
+
);
|
|
16
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { loadFixture, runCLI } from './test-utils.js';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import * as cheerio from 'cheerio';
|
|
4
|
+
|
|
5
|
+
describe.skip('Basic app', () => {
|
|
6
|
+
/** @type {import('./test-utils').Fixture} */
|
|
7
|
+
let fixture;
|
|
8
|
+
|
|
9
|
+
before(async () => {
|
|
10
|
+
fixture = await loadFixture({
|
|
11
|
+
root: './fixtures/basics/',
|
|
12
|
+
});
|
|
13
|
+
await fixture.build();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('can render', async () => {
|
|
17
|
+
const { ready, stop } = runCLI('./fixtures/basics/', { silent: true });
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
await ready;
|
|
21
|
+
|
|
22
|
+
let res = await fetch(`http://localhost:8787/`);
|
|
23
|
+
expect(res.status).to.equal(200);
|
|
24
|
+
let html = await res.text();
|
|
25
|
+
let $ = cheerio.load(html);
|
|
26
|
+
expect($('h1').text()).to.equal('Testing');
|
|
27
|
+
expect($('#env').text()).to.equal('secret');
|
|
28
|
+
} finally {
|
|
29
|
+
stop();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
|
|
3
|
+
|
|
4
|
+
case `uname` in
|
|
5
|
+
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
|
|
6
|
+
esac
|
|
7
|
+
|
|
8
|
+
if [ -z "$NODE_PATH" ]; then
|
|
9
|
+
export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
|
|
10
|
+
else
|
|
11
|
+
export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
|
|
12
|
+
fi
|
|
13
|
+
if [ -x "$basedir/node" ]; then
|
|
14
|
+
exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
|
|
15
|
+
else
|
|
16
|
+
exec node "$basedir/../../../../../../../astro/astro.js" "$@"
|
|
17
|
+
fi
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
|
|
3
|
+
|
|
4
|
+
case `uname` in
|
|
5
|
+
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
|
|
6
|
+
esac
|
|
7
|
+
|
|
8
|
+
if [ -z "$NODE_PATH" ]; then
|
|
9
|
+
export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
|
|
10
|
+
else
|
|
11
|
+
export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
|
|
12
|
+
fi
|
|
13
|
+
if [ -x "$basedir/node" ]; then
|
|
14
|
+
exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
|
|
15
|
+
else
|
|
16
|
+
exec node "$basedir/../../../../../../../astro/astro.js" "$@"
|
|
17
|
+
fi
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { loadFixture } from './test-utils.js';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
|
|
4
|
+
describe('Missing output config', () => {
|
|
5
|
+
/** @type {import('./test-utils').Fixture} */
|
|
6
|
+
let fixture;
|
|
7
|
+
|
|
8
|
+
before(async () => {
|
|
9
|
+
fixture = await loadFixture({
|
|
10
|
+
root: './fixtures/no-output/',
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('throws during the build', async () => {
|
|
15
|
+
let error = undefined;
|
|
16
|
+
try {
|
|
17
|
+
await fixture.build();
|
|
18
|
+
} catch (err) {
|
|
19
|
+
error = err;
|
|
20
|
+
}
|
|
21
|
+
expect(error).to.not.be.equal(undefined);
|
|
22
|
+
expect(error.message).to.include(`output: "server"`);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
|
|
2
|
+
import { spawn } from 'child_process';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
export { fixLineEndings } from '../../../astro/test/test-utils.js';
|
|
6
|
+
|
|
7
|
+
export function loadFixture(config) {
|
|
8
|
+
if (config?.root) {
|
|
9
|
+
config.root = new URL(config.root, import.meta.url);
|
|
10
|
+
}
|
|
11
|
+
return baseLoadFixture(config);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const wranglerPath = fileURLToPath(
|
|
15
|
+
new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
export function runCLI(basePath, { silent }) {
|
|
19
|
+
const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
|
|
20
|
+
const p = spawn('node', [wranglerPath, 'dev', '-l', script]);
|
|
21
|
+
|
|
22
|
+
p.stderr.setEncoding('utf-8');
|
|
23
|
+
p.stdout.setEncoding('utf-8');
|
|
24
|
+
|
|
25
|
+
const timeout = 10000;
|
|
26
|
+
|
|
27
|
+
const ready = new Promise(async (resolve, reject) => {
|
|
28
|
+
const failed = setTimeout(
|
|
29
|
+
() => reject(new Error(`Timed out starting the wrangler CLI`)),
|
|
30
|
+
timeout
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
(async function () {
|
|
34
|
+
for (const msg of p.stderr) {
|
|
35
|
+
if (!silent) {
|
|
36
|
+
// eslint-disable-next-line
|
|
37
|
+
console.error(msg);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
})();
|
|
41
|
+
|
|
42
|
+
for await (const msg of p.stdout) {
|
|
43
|
+
if (!silent) {
|
|
44
|
+
// eslint-disable-next-line
|
|
45
|
+
console.log(msg);
|
|
46
|
+
}
|
|
47
|
+
if (msg.includes(`Listening on`)) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
clearTimeout(failed);
|
|
53
|
+
resolve();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
ready,
|
|
58
|
+
stop() {
|
|
59
|
+
p.kill();
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|