Skip to content

Commit

Permalink
Change Durable Objects "Hello, World!" template to use RPC methods (#…
Browse files Browse the repository at this point in the history
…5637)

* Change Durable Objects "Hello, World!" template to use RPC methods

* Update .changeset/serious-socks-grab.md

Co-authored-by: Pete Bacon Darwin <[email protected]>

---------

Co-authored-by: Pete Bacon Darwin <[email protected]>
  • Loading branch information
joshthoward and petebacondarwin authored Apr 18, 2024
1 parent e60a675 commit b41c4ed
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-socks-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-cloudflare": minor
---

feat: change Durable Objects "Hello, World!" template to use RPC methods
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DurableObject } from "cloudflare:workers";

/**
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
*
Expand All @@ -16,25 +18,27 @@
*/

/** A Durable Object's behavior is defined in an exported Javascript class */
export class MyDurableObject {
export class MyDurableObject extends DurableObject {
/**
* The constructor is invoked once upon creation of the Durable Object, i.e. the first call to
* `DurableObjectStub::get` for a given identifier
* `DurableObjectStub::get` for a given identifier (no-op constructors can be omitted)
*
* @param {DurableObjectState} state - The interface for interacting with Durable Object state
* @param {DurableObjectState} ctx - The interface for interacting with Durable Object state
* @param {Env} env - The interface to reference bindings declared in wrangler.toml
*/
constructor(state, env) {}
constructor(ctx, env) {
super(ctx, env);
}

/**
* The Durable Object fetch handler will be invoked when a Durable Object instance receives a
* request from a Worker via an associated stub
* The Durable Object exposes an RPC method sayHello which will be invoked when when a Durable
* Object instance receives a request from a Worker via the same method invokation on the stub
*
* @param {Request} request - The request submitted to a Durable Object instance from a Worker
* @returns {Promise<Response>} The response to be sent back to the Worker
* @param {string} name - The name provided to a Durable Object instance from a Worker
* @returns {Promise<string>} The greeting to be sent back to the Worker
*/
async fetch(request) {
return new Response('Hello World');
async sayHello(name) {
return `Hello, ${name}!`;
}
}

Expand All @@ -56,10 +60,10 @@ export default {
// The Durable Object constructor will be invoked upon the first call for a given id
let stub = env.MY_DURABLE_OBJECT.get(id);

// We call `fetch()` on the stub to send a request to the Durable Object instance
// The Durable Object instance will invoke its fetch handler to handle the request
let response = await stub.fetch(request);
// We call the `sayHello()` RPC method on the stub to invoke the method on the remote
// Durable Object instance
let greeting = await stub.sayHello("world");

return response;
return new Response(greeting);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"cf-typegen": "wrangler types"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
"typescript": "^5.0.4",
"wrangler": "^3.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DurableObject } from "cloudflare:workers";

/**
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
*
Expand All @@ -11,26 +13,49 @@
* Learn more at https://developers.cloudflare.com/durable-objects
*/


/**
* Associate bindings declared in wrangler.toml with the TypeScript type system
*/
export interface Env {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace;
//
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
MY_DURABLE_OBJECT: DurableObjectNamespace<MyDurableObject>;
//
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
// MY_BUCKET: R2Bucket;
//
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
// MY_SERVICE: Fetcher;
//
// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
// MY_QUEUE: Queue;
}

/** A Durable Object's behavior is defined in an exported Javascript class */
export class MyDurableObject {
export class MyDurableObject extends DurableObject {
/**
* The constructor is invoked once upon creation of the Durable Object, i.e. the first call to
* `DurableObjectStub::get` for a given identifier
* `DurableObjectStub::get` for a given identifier (no-op constructors can be omitted)
*
* @param state - The interface for interacting with Durable Object state
* @param ctx - The interface for interacting with Durable Object state
* @param env - The interface to reference bindings declared in wrangler.toml
*/
constructor(state: DurableObjectState, env: Env) {}
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
}

/**
* The Durable Object fetch handler will be invoked when a Durable Object instance receives a
* request from a Worker via an associated stub
* The Durable Object exposes an RPC method sayHello which will be invoked when when a Durable
* Object instance receives a request from a Worker via the same method invokation on the stub
*
* @param request - The request submitted to a Durable Object instance from a Worker
* @returns The response to be sent back to the Worker
* @param name - The name provided to a Durable Object instance from a Worker
* @returns The greeting to be sent back to the Worker
*/
async fetch(request: Request): Promise<Response> {
return new Response('Hello World');
async sayHello(name: string): Promise<string> {
return `Hello, ${name}!`;
}
}

Expand All @@ -50,12 +75,12 @@ export default {

// This stub creates a communication channel with the Durable Object instance
// The Durable Object constructor will be invoked upon the first call for a given id
let stub: DurableObjectStub = env.MY_DURABLE_OBJECT.get(id);
let stub = env.MY_DURABLE_OBJECT.get(id);

// We call `fetch()` on the stub to send a request to the Durable Object instance
// The Durable Object instance will invoke its fetch handler to handle the request
let response = await stub.fetch(request);
// We call the `sayHello()` RPC method on the stub to invoke the method on the remote
// Durable Object instance
let greeting = await stub.sayHello("world");

return response;
return new Response(greeting);
},
};

0 comments on commit b41c4ed

Please sign in to comment.