-
-
Notifications
You must be signed in to change notification settings - Fork 850
presets(cloudflare): support durable objects, workflows and bindings in local dev #4341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zsilbi
wants to merge
4
commits into
nitrojs:main
Choose a base branch
from
zsilbi:feat/cf-dev-bindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,50 @@ | ||
| This example shows how to use [Cloudflare Durable Objects](https://developers.cloudflare.com/durable-objects/) with Nitro β in production and in local dev, where `vite dev` runs your app inside [workerd](https://github.com/cloudflare/workerd) via Miniflare. | ||
|
|
||
| ## Defining a Durable Object | ||
|
|
||
| Durable Object classes are regular classes extending `DurableObject`: | ||
|
|
||
| ```ts [server/durable/counter.ts] | ||
| import { DurableObject } from "cloudflare:workers"; | ||
|
|
||
| export class CounterDO extends DurableObject { | ||
| async increment(amount: number = 1): Promise<number> { | ||
| const count = ((await this.ctx.storage.get<number>("count")) ?? 0) + amount; | ||
| await this.ctx.storage.put("count", count); | ||
| return count; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| They are exported to the worker entrypoint through `exports.cloudflare.ts`: | ||
|
|
||
| ```ts [exports.cloudflare.ts] | ||
| export { CounterDO } from "./server/durable/counter.ts"; | ||
| ``` | ||
|
|
||
| And bound in `wrangler.jsonc`: | ||
|
|
||
| ```jsonc [wrangler.jsonc] | ||
| { | ||
| "durable_objects": { | ||
| "bindings": [{ "name": "COUNTER", "class_name": "CounterDO" }] | ||
| }, | ||
| "migrations": [{ "tag": "v1", "new_sqlite_classes": ["CounterDO"] }] | ||
| } | ||
| ``` | ||
|
|
||
| ## Calling a Durable Object | ||
|
|
||
| The namespace binding is available from the request event: | ||
|
|
||
| ```ts [routes/counter.ts] | ||
| import { defineHandler } from "nitro"; | ||
|
|
||
| export default defineHandler(async (event) => { | ||
| const env = event.req.runtime?.cloudflare?.env; | ||
| const count = await env.COUNTER.getByName("global").increment(); | ||
| return { count }; | ||
| }); | ||
| ``` | ||
|
|
||
| Run `vite dev` and fetch `/counter` β the count increments in a real Durable Object running in workerd. Route handlers are hot-reloaded; after changing the Durable Object class itself, restart the dev server. |
This file contains hidden or 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 @@ | ||
| export { CounterDO } from "./server/durable/counter.ts"; |
This file contains hidden or 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,21 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>Nitro + Cloudflare Durable Objects</title> | ||
| </head> | ||
| <body> | ||
| <h1>Nitro + Cloudflare Durable Objects</h1> | ||
| <p>Counter: <span id="count">β¦</span></p> | ||
| <button id="increment">Increment</button> | ||
| <script type="module"> | ||
| const el = document.querySelector("#count"); | ||
| async function increment() { | ||
| const { count } = await fetch("/counter").then((r) => r.json()); | ||
| el.textContent = count; | ||
| } | ||
| document.querySelector("#increment").addEventListener("click", increment); | ||
| increment(); | ||
| </script> | ||
| </body> | ||
| </html> |
This file contains hidden or 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,6 @@ | ||
| import { defineConfig } from "nitro"; | ||
|
|
||
| export default defineConfig({ | ||
| preset: "cloudflare_module", | ||
| serverDir: "./", | ||
| }); |
This file contains hidden or 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,13 @@ | ||
| { | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite dev", | ||
| "build": "vite build", | ||
| "deploy": "vite build && wrangler deploy" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/workers-types": "^4.20260601.0", | ||
| "nitro": "latest", | ||
| "wrangler": "^4.99.0" | ||
| } | ||
| } |
This file contains hidden or 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,11 @@ | ||
| import type { CounterDO } from "../server/durable/counter.ts"; | ||
| import { defineHandler } from "nitro"; | ||
|
|
||
| export default defineHandler(async (event) => { | ||
| const env = event.req.runtime?.cloudflare?.env as { | ||
| COUNTER: DurableObjectNamespace<CounterDO>; | ||
| }; | ||
| const counter = env.COUNTER.getByName("global"); | ||
| const count = await counter.increment(); | ||
| return { count }; | ||
| }); | ||
This file contains hidden or 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,9 @@ | ||
| import { DurableObject } from "cloudflare:workers"; | ||
|
|
||
| export class CounterDO extends DurableObject { | ||
| async increment(amount: number = 1): Promise<number> { | ||
| const count = ((await this.ctx.storage.get<number>("count")) ?? 0) + amount; | ||
| await this.ctx.storage.put("count", count); | ||
| return count; | ||
| } | ||
| } |
This file contains hidden or 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,6 @@ | ||
| { | ||
| "extends": "nitro/tsconfig", | ||
| "compilerOptions": { | ||
| "types": ["@cloudflare/workers-types"] | ||
| } | ||
| } |
This file contains hidden or 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,4 @@ | ||
| import { defineConfig } from "vite"; | ||
| import { nitro } from "nitro/vite"; | ||
|
|
||
| export default defineConfig({ plugins: [nitro()] }); |
This file contains hidden or 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,9 @@ | ||
| { | ||
| "$schema": "https://www.unpkg.com/wrangler/config-schema.json", | ||
| "name": "example-cloudflare-durable", | ||
| "compatibility_date": "2026-06-01", | ||
| "durable_objects": { | ||
| "bindings": [{ "name": "COUNTER", "class_name": "CounterDO" }], | ||
| }, | ||
| "migrations": [{ "tag": "v1", "new_sqlite_classes": ["CounterDO"] }], | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add validation for missing Cloudflare bindings.
If
event.req.runtime?.cloudflare?.envis undefined, the type assertion on line 5 combined with property access on line 8 will throw aTypeErrorrather than a clear, actionable error. As per coding guidelines, prefer explicit errors over silent failures.π‘οΈ Proposed fix to add explicit validation
export default defineHandler(async (event) => { + if (!event.req.runtime?.cloudflare?.env) { + throw new Error("Cloudflare runtime environment not available"); + } const env = event.req.runtime?.cloudflare?.env as { COUNTER: DurableObjectNamespace<CounterDO>; };π€ Prompt for AI Agents
Source: Coding guidelines