-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cloudflare-module): extract reusable logic (#2799)
- Loading branch information
Showing
2 changed files
with
126 additions
and
101 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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import "#nitro-internal-pollyfills"; | ||
import type * as CF from "@cloudflare/workers-types"; | ||
import type { ExportedHandler } from "@cloudflare/workers-types"; | ||
import { useNitroApp } from "nitropack/runtime"; | ||
import { requestHasBody, runCronTasks } from "nitropack/runtime/internal"; | ||
|
||
type MaybePromise<T> = T | Promise<T>; | ||
|
||
export function createHandler<Env>(hooks: { | ||
fetch: ( | ||
...params: Parameters<NonNullable<ExportedHandler<Env>["fetch"]>> | ||
) => MaybePromise<Response | CF.Response | undefined>; | ||
}) { | ||
const nitroApp = useNitroApp(); | ||
|
||
return <ExportedHandler<Env>>{ | ||
async fetch(request, env, context) { | ||
// Preset-specific logic | ||
if (hooks.fetch) { | ||
const res = await hooks.fetch(request, env, context); | ||
if (res) { | ||
return res; | ||
} | ||
} | ||
|
||
const url = new URL(request.url); | ||
let body; | ||
if (requestHasBody(request as unknown as Request)) { | ||
body = Buffer.from(await request.arrayBuffer()); | ||
} | ||
|
||
// Expose latest env to the global context | ||
(globalThis as any).__env__ = env; | ||
|
||
return nitroApp.localFetch(url.pathname + url.search, { | ||
context: { | ||
cf: (request as any).cf, | ||
waitUntil: (promise: Promise<any>) => context.waitUntil(promise), | ||
cloudflare: { | ||
request, | ||
env, | ||
context, | ||
}, | ||
}, | ||
host: url.hostname, | ||
protocol: url.protocol, | ||
method: request.method, | ||
headers: request.headers as unknown as Headers, | ||
body, | ||
}) as unknown as Promise<Response>; | ||
}, | ||
|
||
scheduled(controller, env, context) { | ||
(globalThis as any).__env__ = env; | ||
context.waitUntil( | ||
nitroApp.hooks.callHook("cloudflare:scheduled", { | ||
controller, | ||
env, | ||
context, | ||
}) | ||
); | ||
if (import.meta._tasks) { | ||
context.waitUntil( | ||
runCronTasks(controller.cron, { | ||
context: { | ||
cloudflare: { | ||
env, | ||
context, | ||
}, | ||
}, | ||
payload: {}, | ||
}) | ||
); | ||
} | ||
}, | ||
|
||
email(message, env, context) { | ||
(globalThis as any).__env__ = env; | ||
context.waitUntil( | ||
nitroApp.hooks.callHook("cloudflare:email", { | ||
message, | ||
event: message, // backward compat | ||
env, | ||
context, | ||
}) | ||
); | ||
}, | ||
|
||
queue(batch, env, context) { | ||
(globalThis as any).__env__ = env; | ||
context.waitUntil( | ||
nitroApp.hooks.callHook("cloudflare:queue", { | ||
batch, | ||
event: batch, | ||
env, | ||
context, | ||
}) | ||
); | ||
}, | ||
|
||
tail(traces, env, context) { | ||
(globalThis as any).__env__ = env; | ||
context.waitUntil( | ||
nitroApp.hooks.callHook("cloudflare:tail", { | ||
traces, | ||
env, | ||
context, | ||
}) | ||
); | ||
}, | ||
|
||
trace(traces, env, context) { | ||
(globalThis as any).__env__ = env; | ||
context.waitUntil( | ||
nitroApp.hooks.callHook("cloudflare:trace", { | ||
traces, | ||
env, | ||
context, | ||
}) | ||
); | ||
}, | ||
}; | ||
} |
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