Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/presets/cloudflare/runtime/cloudflare-durable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useNitroApp } from "nitropack/runtime";
import { isPublicAssetURL } from "#nitro-internal-virtual/public-assets";
import { createHandler, fetchHandler } from "./_module-handler";

const DURABLE_BINDING = "$DurableObject";
const DURABLE_BINDING = import.meta._durableBindingName || "$DurableObject";
const DURABLE_INSTANCE = "server";

interface Env {
Expand Down
12 changes: 12 additions & 0 deletions src/presets/cloudflare/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ export interface CloudflareOptions {
*/
defaultRoutes?: boolean;
};

/**
* Options for the `cloudflare-durable` preset.
*/
durable?: {
/**
* The binding name for the Durable Object used by `defineWebSocketHandler`.
*
* @default "$DurableObject"
*/
bindingName?: string;
};
}

type DurableObjectState = ConstructorParameters<typeof DurableObject>[0];
Expand Down
17 changes: 17 additions & 0 deletions src/presets/cloudflare/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,23 @@ export async function writeWranglerConfig(
}
wranglerConfig.compatibility_flags = [...compatFlags];

// Durable Objects config for cloudflare-durable preset
if (nitro.options.preset === "cloudflare-durable") {
const bindingName =
nitro.options.cloudflare?.durable?.bindingName || "$DurableObject";
wranglerConfig.durable_objects ??= { bindings: [] };
wranglerConfig.durable_objects.bindings ??= [];
const hasBinding = wranglerConfig.durable_objects.bindings.some(
(b) => b.name === bindingName && b.class_name === "$DurableObject"
);
if (!hasBinding) {
wranglerConfig.durable_objects.bindings.push({
name: bindingName,
class_name: "$DurableObject",
});
}
}

// Write wrangler.json
await writeFile(
wranglerConfigPath,
Expand Down
2 changes: 2 additions & 0 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => {
_asyncContext: nitro.options.experimental.asyncContext,
_websocket: nitro.options.experimental.websocket,
_tasks: nitro.options.experimental.tasks,
_durableBindingName:
nitro.options.cloudflare?.durable?.bindingName || "$DurableObject",
};

// Universal import.meta
Expand Down
1 change: 1 addition & 0 deletions src/types/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface NitroStaticBuildFlags {
_asyncContext?: boolean;
_websocket?: boolean;
_tasks?: boolean;
_durableBindingName?: string;
dev?: boolean;
client?: boolean;
nitro?: boolean;
Expand Down