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: 2 additions & 0 deletions src/build/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export function baseBuildConfig(nitro: Nitro) {
baseURL: nitro.options.baseURL,
_asyncContext: nitro.options.experimental.asyncContext,
_tasks: nitro.options.experimental.tasks,
_durableBindingName:
nitro.options.cloudflare?.durable?.bindingName || "$DurableObject",
};

const replacements = {
Expand Down
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 @@ -9,7 +9,7 @@ import { isPublicAssetURL } from "#nitro/virtual/public-assets";
import { resolveWebsocketHooks } from "#nitro/runtime/app";
import { hasWebSocket } from "#nitro/virtual/feature-flags";

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

interface Env {
Expand Down
14 changes: 14 additions & 0 deletions src/presets/cloudflare/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ export interface CloudflareOptions {
* Custom Cloudflare exports additional classes such as WorkflowEntrypoint.
*/
exports?: string;

/**
* Options for Durable Objects.
*/
durable?: {
/**
* The binding name for the Durable Object used by `defineWebSocketHandler`.
*
* Must match the `name` field in your wrangler.json `durable_objects.bindings` config.
*
* @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 @@ -328,6 +328,23 @@ export async function writeWranglerConfig(
globs: ["**/*.mjs", "**/*.js"],
});
}

// 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
Expand Down
1 change: 1 addition & 0 deletions src/types/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface NitroImportMeta {
runtimeConfig?: Record<string, any>;
_asyncContext?: boolean;
_tasks?: boolean;
_durableBindingName?: string;
}

declare global {
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export default defineConfig({
wrangler: {
compatibility_date: "2024-01-01",
},
durable: {
bindingName: "MyCustomDO",
},
},
typescript: {
generateRuntimeConfigTypes: true,
Expand Down
26 changes: 26 additions & 0 deletions test/presets/cloudflare-durable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { promises as fsp } from "node:fs";
import { resolve } from "pathe";
import { describe, expect, it } from "vitest";

import { setupTest } from "../tests.ts";

describe("nitro:preset:cloudflare-durable", async () => {
const ctx = await setupTest("cloudflare-durable");

it("should use custom durable binding name from config", async () => {
const entry = await fsp.readFile(
resolve(ctx.outDir, "server", "chunks", "nitro", "nitro.mjs"),
"utf8"
);
// Check that custom binding name is used instead of default $DurableObject
expect(entry).toContain("MyCustomDO");
});

it("should export $DurableObject class", async () => {
const entry = await fsp.readFile(
resolve(ctx.outDir, "server", "index.mjs"),
"utf8"
);
expect(entry).toMatch(/\$DurableObject/);
});
});