nitrojs / nitrojs/nitro

Support for Custom Cloudflare Durable Objects with `cloudflare_durable` preset

Open
#3,378 5 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Describe the feature

Hi Nitro Team,

I'm exploring the use of Cloudflare Durable Objects with my Nuxt project and had a question regarding custom implementations.

What I'm trying to achieve:
I want to define a custom Durable Object class and access it from my API routes.

What I've tried:

  1. I've created a custom class, let's call it Counter, which extends DurableObject (from Cloudflare Workers types) within server/durable-objects/Counter.ts.
    // server/durable-objects/Counter.ts
    export class Counter extends DurableObject {
      // ... implementation ...
      constructor(state: DurableObjectState, env: Env) { /* ... */ }
      async fetch(request: Request) { /* ... */ }
    }
    
  2. I've configured this in my wrangler.jsonc similar to how one would for a standard Workers project:
    // wrangler.jsonc (simplified)
    {
      "durable_objects": {
        "bindings": [
          { "name": "counter", "class_name": "Counter" }
        ]
      },
      "migrations": [
        { "tag": "v1", "new_sqlite_classes": ["Counter"] }
      ]
    }
    
  3. So that I can asscess counter from another API route (e.g., server/api/test.ts):
    // server/api/test.ts
    export default defineEventHandler(async (counter) => {
      // How to correctly get the Durable Object namespace and stub here?
      // const counter = event.context.cloudflare.env.counter;
      // const id = counter.idFromName("my-counter");
      // const stub = counter.get(id);
      // ...
    });
    

Problem:
When I attempt to deploy my application, I encounter the following error:
Your Worker depends on the following Durable Objects, which are not exported in your entrypoint file: Counter..

Question:

  1. Is it generally supported to define and use custom Durable Object classes (like my Counter example) with presets in Nitro?
  2. If so, what is the correct way to ensure these custom DO classes are properly exported or recognized by Nitro's build process to avoid the deploy error?

Any pointers to relevant documentation or examples of this setup would be very helpful.
Thanks for your time and help!

Any guidance or clarification on this would be greatly appreciated!

Thanks.

Additional information
  • Would you be willing to help implement this feature?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the custom class in server/durable-objects/Counter.ts, its wrangler.jsonc durable_objects and migrations entries, and the server/api/test.ts route. Verify the deployment entrypoint recognizes Counter and that the API route can obtain its namespace and stub without the reported unexported Durable Object error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend, cloud
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.