forked from nitrojs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare-module.ts
More file actions
31 lines (26 loc) · 992 Bytes
/
cloudflare-module.ts
File metadata and controls
31 lines (26 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import "#nitro/virtual/polyfills";
import type { fetch } from "@cloudflare/workers-types";
import wsAdapter from "crossws/adapters/cloudflare";
import { isPublicAssetURL } from "#nitro/virtual/public-assets";
import { createHandler } from "./_module-handler.ts";
import { resolveWebsocketHooks } from "#nitro/runtime/app";
import { hasWebSocket } from "#nitro/virtual/feature-flags";
const ws = hasWebSocket
? wsAdapter({ resolve: resolveWebsocketHooks })
: undefined;
interface Env {
ASSETS?: { fetch: typeof fetch };
}
export default createHandler<Env>({
fetch(cfRequest, env, context, url) {
// Static assets fallback (optional binding)
if (env.ASSETS && isPublicAssetURL(url.pathname)) {
return env.ASSETS.fetch(cfRequest as any);
}
// Websocket upgrade
// https://crossws.unjs.io/adapters/cloudflare
if (hasWebSocket && cfRequest.headers.get("upgrade") === "websocket") {
return ws!.handleUpgrade(cfRequest, env, context);
}
},
});