@tschk/moonshine-deploy-cloudflare 0.4.4 → 0.4.80
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 +16 -7
- package/package.json +6 -6
- package/src/index.ts +86 -38
package/README.md
CHANGED
|
@@ -9,12 +9,15 @@ await cloudflareAdapter.build(manifest, ".moonshine");
|
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
- Target runtime: `cloudflare`.
|
|
12
|
-
- Capabilities: `streaming`, `islands`, `edge`, `revalidation
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
- Capabilities: `streaming`, `islands`, `edge`, `revalidation` (route cache
|
|
13
|
+
storage is disabled as described below).
|
|
14
|
+
- Compiler builds output a bundled `server.js`, `public/` assets and
|
|
15
|
+
`wrangler.jsonc`. The React worker uses an `ASSETS` binding for static files.
|
|
16
|
+
Legacy manifests without a compiler server
|
|
17
|
+
bundle still emit `server.ts` and require a separate bundling step.
|
|
15
18
|
|
|
16
|
-
Limitations: requires an `ASSETS` binding for static files; worker size
|
|
17
|
-
|
|
19
|
+
Limitations: requires an `ASSETS` binding for static files; worker size limits
|
|
20
|
+
apply.
|
|
18
21
|
|
|
19
22
|
See [docs/DEPLOYMENT.md](../../docs/DEPLOYMENT.md) and
|
|
20
23
|
[docs/MANIFEST.md](../../docs/MANIFEST.md).
|
|
@@ -37,8 +40,14 @@ date as its compatibility date. A recent Wrangler/workerd is required; older loc
|
|
|
37
40
|
runtimes may require an isolated test configuration with an older supported date.
|
|
38
41
|
|
|
39
42
|
Native hydration/islands, opt-in request context, and route caching/revalidation
|
|
40
|
-
are rejected explicitly.
|
|
41
|
-
|
|
43
|
+
are rejected explicitly. React builds use the compiler's module table and renderer
|
|
44
|
+
through `cloudflareFetch`. It does not read or write the shared Cache API:
|
|
45
|
+
loaders can personalize responses using arbitrary request data even without a
|
|
46
|
+
`Vary` header. For routes declaring a cache policy, dynamic responses instead
|
|
47
|
+
receive `Cache-Control: private, no-store`, sacrificing shared caching until an
|
|
48
|
+
explicit safe contract exists. Asset binding responses retain their own headers.
|
|
49
|
+
This change does not purge entries already stored by previous deployments or
|
|
50
|
+
override separately configured caching rules.
|
|
42
51
|
|
|
43
52
|
The optional native artifact test discovers an existing Wrangler/Miniflare
|
|
44
53
|
installation and runs real workerd. It skips when that tool is unavailable and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tschk/moonshine-deploy-cloudflare",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.80",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -15,20 +15,20 @@
|
|
|
15
15
|
"test": "bun test"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@tschk/moonshine-framework": "^0.4.
|
|
19
|
-
"@tschk/moonshine-router": "^0.4.
|
|
20
|
-
"@tschk/moonshine-server": "^0.4.
|
|
18
|
+
"@tschk/moonshine-framework": "^0.4.80",
|
|
19
|
+
"@tschk/moonshine-router": "^0.4.80",
|
|
20
|
+
"@tschk/moonshine-server": "^0.4.80"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/bun": "1.3.14",
|
|
24
24
|
"typescript": "7.0.2",
|
|
25
|
-
"@tschk/moonshine-adapter-conformance": "^0.4.
|
|
25
|
+
"@tschk/moonshine-adapter-conformance": "^0.4.80"
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
28
|
"src"
|
|
29
29
|
],
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@tschk/moonshine-react": "^0.4.
|
|
31
|
+
"@tschk/moonshine-react": "^0.4.80"
|
|
32
32
|
},
|
|
33
33
|
"peerDependenciesMeta": {
|
|
34
34
|
"@tschk/moonshine-react": {
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { writeFile, mkdir } from "node:fs/promises";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
2
3
|
import { resolve } from "node:path";
|
|
3
4
|
import {
|
|
4
5
|
createRequestHandler,
|
|
@@ -8,6 +9,7 @@ import {
|
|
|
8
9
|
import { matchRoutes, createRouteGraph } from "@tschk/moonshine-router/graph";
|
|
9
10
|
import {
|
|
10
11
|
type DeploymentAdapter,
|
|
12
|
+
type Renderer,
|
|
11
13
|
type MoonshineManifest,
|
|
12
14
|
type Harness,
|
|
13
15
|
type HarnessFactory,
|
|
@@ -25,12 +27,23 @@ export type CloudflareContext = {
|
|
|
25
27
|
waitUntil: (promise: Promise<unknown>) => void;
|
|
26
28
|
};
|
|
27
29
|
|
|
30
|
+
function preventSharedCaching(response: Response): Response {
|
|
31
|
+
const headers = new Headers(response.headers);
|
|
32
|
+
headers.set("cache-control", "private, no-store");
|
|
33
|
+
return new Response(response.body, {
|
|
34
|
+
status: response.status,
|
|
35
|
+
statusText: response.statusText,
|
|
36
|
+
headers,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
28
40
|
export async function cloudflareFetch(
|
|
29
41
|
request: Request,
|
|
30
42
|
env: CloudflareEnv,
|
|
31
|
-
|
|
43
|
+
_ctx: CloudflareContext,
|
|
32
44
|
manifest: MoonshineManifest,
|
|
33
45
|
modules: Record<string, RouteModule> = {},
|
|
46
|
+
bundledRenderer?: Renderer,
|
|
34
47
|
): Promise<Response> {
|
|
35
48
|
const url = new URL(request.url);
|
|
36
49
|
const pathname = url.pathname.replace(/\/+$/, "") || "/";
|
|
@@ -42,46 +55,21 @@ export async function cloudflareFetch(
|
|
|
42
55
|
) {
|
|
43
56
|
const assetRes = await env.ASSETS.fetch(request);
|
|
44
57
|
if (assetRes.status !== 404) return assetRes;
|
|
58
|
+
if (assetRes.body) void assetRes.body.cancel().catch(() => {});
|
|
45
59
|
}
|
|
46
60
|
|
|
47
|
-
const { reactRenderer, registerRouteModules } =
|
|
48
|
-
await import("@tschk/moonshine-react");
|
|
49
61
|
const graph = createRouteGraph(manifest.routes);
|
|
50
62
|
const match = matchRoutes(graph, pathname);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const cache =
|
|
58
|
-
typeof caches !== "undefined"
|
|
59
|
-
? (caches as { default?: Cache }).default
|
|
60
|
-
: undefined;
|
|
61
|
-
|
|
62
|
-
if (match && match.route.cache && cache) {
|
|
63
|
-
const cached = await cache.match(request);
|
|
64
|
-
if (cached) return cached;
|
|
65
|
-
const response = await handler(request);
|
|
66
|
-
if (
|
|
67
|
-
response.status >= 200 &&
|
|
68
|
-
response.status < 400 &&
|
|
69
|
-
(request.method === "GET" || request.method === "HEAD")
|
|
70
|
-
) {
|
|
71
|
-
ctx.waitUntil(
|
|
72
|
-
(async () => {
|
|
73
|
-
try {
|
|
74
|
-
await cache.put(request.clone(), response.clone());
|
|
75
|
-
} catch (err) {
|
|
76
|
-
console.warn("Failed to cache response:", err);
|
|
77
|
-
}
|
|
78
|
-
})(),
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
return response;
|
|
63
|
+
let renderer = bundledRenderer;
|
|
64
|
+
if (!renderer) {
|
|
65
|
+
const { reactRenderer, registerRouteModules } =
|
|
66
|
+
await import("@tschk/moonshine-react");
|
|
67
|
+
registerRouteModules(modules);
|
|
68
|
+
renderer = reactRenderer;
|
|
82
69
|
}
|
|
83
|
-
|
|
84
|
-
|
|
70
|
+
const handler = createRequestHandler({ manifest, modules, renderer });
|
|
71
|
+
const response = await handler(request);
|
|
72
|
+
return match?.route.cache ? preventSharedCaching(response) : response;
|
|
85
73
|
}
|
|
86
74
|
|
|
87
75
|
export default cloudflareFetch;
|
|
@@ -146,6 +134,59 @@ export default {
|
|
|
146
134
|
);
|
|
147
135
|
}
|
|
148
136
|
|
|
137
|
+
async function buildReactWorker(
|
|
138
|
+
manifest: MoonshineManifest,
|
|
139
|
+
outDir: string,
|
|
140
|
+
): Promise<void> {
|
|
141
|
+
await writeFile(
|
|
142
|
+
resolve(outDir, "manifest.json"),
|
|
143
|
+
JSON.stringify(manifest) + "\n",
|
|
144
|
+
);
|
|
145
|
+
await mkdir(resolve(outDir, "public"), { recursive: true });
|
|
146
|
+
const entryPath = resolve(outDir, "server.ts");
|
|
147
|
+
await writeFile(
|
|
148
|
+
entryPath,
|
|
149
|
+
`import { cloudflareFetch } from ${JSON.stringify(import.meta.path)};
|
|
150
|
+
import { modules, renderer } from ${JSON.stringify(`./${manifest.entries.server}`)};
|
|
151
|
+
import manifest from "./manifest.json" with { type: "json" };
|
|
152
|
+
export default { fetch(request, env, ctx) {
|
|
153
|
+
return cloudflareFetch(request, env, ctx, manifest, modules, renderer);
|
|
154
|
+
} };
|
|
155
|
+
`,
|
|
156
|
+
);
|
|
157
|
+
const result = await Bun.build({
|
|
158
|
+
entrypoints: [entryPath],
|
|
159
|
+
outdir: outDir,
|
|
160
|
+
target: "browser",
|
|
161
|
+
external: ["node:*"],
|
|
162
|
+
packages: "bundle",
|
|
163
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
164
|
+
});
|
|
165
|
+
if (!result.success)
|
|
166
|
+
throw new AggregateError(
|
|
167
|
+
result.logs,
|
|
168
|
+
"React Cloudflare worker bundle failed",
|
|
169
|
+
);
|
|
170
|
+
await writeFile(
|
|
171
|
+
resolve(outDir, "wrangler.jsonc"),
|
|
172
|
+
JSON.stringify(
|
|
173
|
+
{
|
|
174
|
+
name: "moonshine-react",
|
|
175
|
+
main: "server.js",
|
|
176
|
+
compatibility_date: new Date().toISOString().slice(0, 10),
|
|
177
|
+
compatibility_flags: ["nodejs_compat"],
|
|
178
|
+
assets: {
|
|
179
|
+
directory: "./public",
|
|
180
|
+
binding: "ASSETS",
|
|
181
|
+
run_worker_first: true,
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
null,
|
|
185
|
+
2,
|
|
186
|
+
) + "\n",
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
149
190
|
export const cloudflareAdapter: DeploymentAdapter = {
|
|
150
191
|
name: "cloudflare",
|
|
151
192
|
runtimes: ["cloudflare"],
|
|
@@ -174,6 +215,13 @@ export const cloudflareAdapter: DeploymentAdapter = {
|
|
|
174
215
|
await buildNativeWorker(manifest, outDir);
|
|
175
216
|
return;
|
|
176
217
|
}
|
|
218
|
+
if (
|
|
219
|
+
manifest.entries.server &&
|
|
220
|
+
existsSync(resolve(outDir, manifest.entries.server))
|
|
221
|
+
) {
|
|
222
|
+
await buildReactWorker(manifest, outDir);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
177
225
|
const projectRoot = resolve(outDir, "..");
|
|
178
226
|
const resolvedManifest = resolveManifestFiles(manifest, projectRoot);
|
|
179
227
|
|
|
@@ -187,13 +235,13 @@ export const cloudflareAdapter: DeploymentAdapter = {
|
|
|
187
235
|
// dynamic `import(route.file)` the renderer would otherwise fall back to.
|
|
188
236
|
const serverEntry = `import cloudflareFetch from "@tschk/moonshine-deploy-cloudflare";
|
|
189
237
|
import manifest from "./manifest.json" with { type: "json" };
|
|
190
|
-
import { modules } from "./dist/server.js";
|
|
238
|
+
import { modules, renderer } from "./dist/server.js";
|
|
191
239
|
|
|
192
240
|
type Env = { ASSETS?: { fetch: (request: Request) => Promise<Response> } };
|
|
193
241
|
|
|
194
242
|
export default {
|
|
195
243
|
async fetch(request: Request, env: Env, ctx: { waitUntil: (promise: Promise<unknown>) => void }) {
|
|
196
|
-
return cloudflareFetch(request, env, ctx, manifest, modules);
|
|
244
|
+
return cloudflareFetch(request, env, ctx, manifest, modules, renderer);
|
|
197
245
|
},
|
|
198
246
|
};
|
|
199
247
|
`;
|