-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudflare-module): use new workers static assets (#2800)
- Loading branch information
Showing
12 changed files
with
191 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { defineNitroConfig } from "nitropack/config"; | ||
|
||
export default defineNitroConfig({ | ||
compatibilityDate: "2024-09-29", | ||
compatibilityDate: "2024-09-19", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name = "nitro-test" | ||
compatibility_date = "2024-09-19" | ||
assets = { directory = "./.output/public/", binding = "ASSETS" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/presets/cloudflare/runtime/cloudflare-module-legacy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import "#nitro-internal-pollyfills"; | ||
import { | ||
getAssetFromKV, | ||
mapRequestToAsset, | ||
} from "@cloudflare/kv-asset-handler"; | ||
import wsAdapter from "crossws/adapters/cloudflare"; | ||
import { withoutBase } from "ufo"; | ||
import { useNitroApp, useRuntimeConfig } from "nitropack/runtime"; | ||
import { getPublicAssetMeta } from "#nitro-internal-virtual/public-assets"; | ||
import { createHandler } from "./_module-handler"; | ||
|
||
// @ts-ignore Bundled by Wrangler | ||
// See https://github.com/cloudflare/kv-asset-handler#asset_manifest-required-for-es-modules | ||
import manifest from "__STATIC_CONTENT_MANIFEST"; | ||
|
||
const nitroApp = useNitroApp(); | ||
|
||
const ws = import.meta._websocket | ||
? wsAdapter(nitroApp.h3App.websocket) | ||
: undefined; | ||
|
||
interface Env { | ||
__STATIC_CONTENT?: any; | ||
} | ||
|
||
export default createHandler<Env>({ | ||
async fetch(request, env, context) { | ||
// Websocket upgrade | ||
// https://crossws.unjs.io/adapters/cloudflare | ||
if ( | ||
import.meta._websocket && | ||
request.headers.get("upgrade") === "websocket" | ||
) { | ||
return ws!.handleUpgrade(request as any, env, context); | ||
} | ||
|
||
try { | ||
// https://github.com/cloudflare/kv-asset-handler#es-modules | ||
return await getAssetFromKV( | ||
{ | ||
request: request as unknown as Request, | ||
waitUntil(promise) { | ||
return context.waitUntil(promise); | ||
}, | ||
}, | ||
{ | ||
cacheControl: assetsCacheControl, | ||
mapRequestToAsset: baseURLModifier, | ||
ASSET_NAMESPACE: env.__STATIC_CONTENT, | ||
ASSET_MANIFEST: JSON.parse(manifest), | ||
} | ||
); | ||
} catch { | ||
// Ignore | ||
} | ||
}, | ||
}); | ||
|
||
function assetsCacheControl(_request: Request) { | ||
const url = new URL(_request.url); | ||
const meta = getPublicAssetMeta(url.pathname); | ||
if (meta.maxAge) { | ||
return { | ||
browserTTL: meta.maxAge, | ||
edgeTTL: meta.maxAge, | ||
}; | ||
} | ||
return {}; | ||
} | ||
|
||
const baseURLModifier = (request: Request) => { | ||
const url = withoutBase(request.url, useRuntimeConfig().app.baseURL); | ||
return mapRequestToAsset(new Request(url, request)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
name = "nitro-test" | ||
|
||
[site] | ||
bucket = ".output/public" | ||
compatibility_date = "2024-09-19" | ||
assets = { directory = "./.output/public/", binding = "ASSETS"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Miniflare } from "miniflare"; | ||
import { resolve } from "pathe"; | ||
import { Response as _Response } from "undici"; | ||
import { describe } from "vitest"; | ||
|
||
import { setupTest, testNitro } from "../tests"; | ||
|
||
describe("nitro:preset:cloudflare-module", async () => { | ||
const ctx = await setupTest("cloudflare-module-legacy", {}); | ||
|
||
testNitro(ctx, () => { | ||
const mf = new Miniflare({ | ||
modules: true, | ||
scriptPath: resolve(ctx.outDir, "server/index.mjs"), | ||
modulesRules: [{ type: "CompiledWasm", include: ["**/*.wasm"] }], | ||
sitePath: resolve(ctx.outDir, "public"), | ||
compatibilityFlags: ["streams_enable_constructors"], | ||
bindings: { ...ctx.env }, | ||
}); | ||
|
||
return async ({ url, headers, method, body }) => { | ||
const res = await mf.dispatchFetch("http://localhost" + url, { | ||
headers: headers || {}, | ||
method: method || "GET", | ||
redirect: "manual", | ||
body, | ||
}); | ||
return res as unknown as Response; | ||
}; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters