Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions src/presets/cloudflare/runtime/_module-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "#nitro/virtual/polyfills";
import type * as CF from "@cloudflare/workers-types";
import type { ExportedHandler } from "@cloudflare/workers-types";
import type { ServerRequest } from "srvx";
import type { ServerRequest, ServerRuntimeContext } from "srvx";

import { runCronTasks } from "#nitro/runtime/task";
import { useNitroApp, useNitroHooks } from "nitro/app";
Expand All @@ -22,6 +21,9 @@ export function createHandler<Env>(hooks: {

return {
async fetch(request, env, context) {
(globalThis as any).__env__ = env;
augmentReq(request as any, { env: env as any, context });

const ctxExt = {};
const url = new URL(request.url);

Expand All @@ -33,14 +35,7 @@ export function createHandler<Env>(hooks: {
}
}

return fetchHandler(
request,
env,
context,
url,
nitroApp,
ctxExt
) as Promise<any /* CF response! */>;
return (await nitroApp.fetch(request)) as any;
},

scheduled(controller, env, context) {
Expand Down Expand Up @@ -71,8 +66,8 @@ export function createHandler<Env>(hooks: {
(globalThis as any).__env__ = env;
context.waitUntil(
nitroHooks.callHook("cloudflare:email", {
message,
event: message, // backward compat
message: message as any,
event: message as any, // backward compat
env,
context,
}) || Promise.resolve()
Expand Down Expand Up @@ -115,22 +110,12 @@ export function createHandler<Env>(hooks: {
} satisfies ExportedHandler<Env>;
}

export async function fetchHandler(
export function augmentReq(
cfReq: Request | CF.Request,
env: unknown,
context: CF.ExecutionContext | DurableObjectState,
url: URL = new URL(cfReq.url),
nitroApp = useNitroApp(),
ctxExt: any
ctx: NonNullable<ServerRuntimeContext["cloudflare"]>
) {
// Expose latest env to the global context
(globalThis as any).__env__ = env;

// srvx compatibility
const req = cfReq as ServerRequest;
req.runtime ??= { name: "cloudflare" };
req.runtime.cloudflare ??= { context, env } as any;
req.waitUntil = context.waitUntil.bind(context);

return nitroApp.fetch(req) as unknown as Promise<Response>;
req.runtime.cloudflare = { ...req.runtime.cloudflare, ...ctx };
req.waitUntil = ctx.context?.waitUntil.bind(ctx.context);
}
19 changes: 11 additions & 8 deletions src/presets/cloudflare/runtime/cloudflare-durable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "#nitro/virtual/polyfills";
import type * as CF from "@cloudflare/workers-types";
import { DurableObject } from "cloudflare:workers";
import wsAdapter from "crossws/adapters/cloudflare";
import { createHandler, fetchHandler } from "./_module-handler.ts";
import { createHandler, augmentReq } from "./_module-handler.ts";

import { useNitroApp, useNitroHooks } from "nitro/app";
import { isPublicAssetURL } from "#nitro/virtual/public-assets";
Expand Down Expand Up @@ -43,11 +43,12 @@ export default createHandler<Env>({
fetch(request, env, context, url, ctxExt) {
// Static assets fallback (optional binding)
if (env.ASSETS && isPublicAssetURL(url.pathname)) {
return env.ASSETS.fetch(request);
return env.ASSETS.fetch(request as any);
}

// Expose stub fetch to the context
ctxExt.durableFetch = (req = request) => getDurableStub(env).fetch(req);
ctxExt.durableFetch = (req = request) =>
getDurableStub(env).fetch(req as any);

// Websocket upgrade
// https://crossws.unjs.io/adapters/cloudflare#durable-objects
Expand All @@ -72,14 +73,16 @@ export class $DurableObject extends DurableObject {
}

override fetch(request: Request) {
augmentReq(request, {
env: this.env,
context: this.ctx as any,
});

if (hasWebSocket && request.headers.get("upgrade") === "websocket") {
return ws!.handleDurableUpgrade(this, request);
}
// Main handler
const url = new URL(request.url);
return fetchHandler(request, this.env, this.ctx, url, nitroApp, {
durable: this,
});

return nitroApp.fetch(request);
}

override alarm(): void | Promise<void> {
Expand Down
8 changes: 4 additions & 4 deletions src/presets/cloudflare/runtime/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ interface Env {
}

export default createHandler<Env>({
fetch(request, env, context, url) {
fetch(cfRequest, env, context, url) {
// Static assets fallback (optional binding)
if (env.ASSETS && isPublicAssetURL(url.pathname)) {
return env.ASSETS.fetch(request);
return env.ASSETS.fetch(cfRequest as any);
}

// Websocket upgrade
// https://crossws.unjs.io/adapters/cloudflare
if (hasWebSocket && request.headers.get("upgrade") === "websocket") {
return ws!.handleUpgrade(request as any, env, context);
if (hasWebSocket && cfRequest.headers.get("upgrade") === "websocket") {
return ws!.handleUpgrade(cfRequest, env, context);
}
},
});
18 changes: 7 additions & 11 deletions src/presets/cloudflare/runtime/cloudflare-pages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "#nitro/virtual/polyfills";
import type { ServerRequest } from "srvx";
import type {
Request as CFRequest,
EventContext,
Expand All @@ -12,6 +11,7 @@ import { isPublicAssetURL } from "#nitro/virtual/public-assets";
import { runCronTasks } from "#nitro/runtime/task";
import { resolveWebsocketHooks } from "#nitro/runtime/app";
import { hasWebSocket } from "#nitro/virtual/feature-flags";
import { augmentReq } from "./_module-handler.ts";

/**
* Reference: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#parameters
Expand All @@ -38,17 +38,16 @@ export default {
env: CFPagesEnv,
context: EventContext<CFPagesEnv, string, any>
) {
// srvx compatibility
const req = cfReq as unknown as ServerRequest;
req.runtime ??= { name: "cloudflare" };
req.runtime.cloudflare ??= { context, env } as any;
req.waitUntil = context.waitUntil.bind(context);
augmentReq(cfReq, {
env,
context: context as any,
});

// Websocket upgrade
// https://crossws.unjs.io/adapters/cloudflare
if (hasWebSocket && cfReq.headers.get("upgrade") === "websocket") {
return ws!.handleUpgrade(
cfReq as any,
cfReq,
env,
context as unknown as ExecutionContext
);
Expand All @@ -59,10 +58,7 @@ export default {
return env.ASSETS.fetch(cfReq);
}

// Expose latest env to the global context
(globalThis as any).__env__ = env;

return nitroApp.fetch(req);
return nitroApp.fetch(cfReq as any);
},
scheduled(event: any, env: CFPagesEnv, context: ExecutionContext) {
if (import.meta._tasks) {
Expand Down
Loading