create-cloudflare
Version:
A CLI for creating and deploying new applications to Cloudflare.
69 lines (61 loc) • 2.58 kB
JavaScript
import { DurableObject } from "cloudflare:workers";
/**
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your Durable Object in action
* - Run `npm run deploy` to publish your application
*
* Learn more at https://developers.cloudflare.com/durable-objects
*/
/**
* Env provides a mechanism to reference bindings declared in wrangler.jsonc within JavaScript
*
* @typedef {Object} Env
* @property {DurableObjectNamespace} MY_DURABLE_OBJECT - The Durable Object namespace binding
*/
/** A Durable Object's behavior is defined in an exported Javascript class */
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 (no-op constructors can be omitted)
*
* @param {DurableObjectState} ctx - The interface for interacting with Durable Object state
* @param {Env} env - The interface to reference bindings declared in wrangler.jsonc
*/
constructor(ctx, env) {
super(ctx, env);
}
/**
* The Durable Object exposes an RPC method sayHello which will be invoked when a Durable
* Object instance receives a request from a Worker via the same method invocation on the stub
*
* @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 sayHello(name) {
return `Hello, ${name}!`;
}
}
export default {
/**
* This is the standard fetch handler for a Cloudflare Worker
*
* @param {Request} request - The request submitted to the Worker from the client
* @param {Env} env - The interface to reference bindings declared in wrangler.jsonc
* @param {ExecutionContext} ctx - The execution context of the Worker
* @returns {Promise<Response>} The response to be sent back to the client
*/
async fetch(request, env, ctx) {
// Create a stub to open a communication channel with the Durable Object
// instance named "foo".
//
// Requests from all Workers to the Durable Object instance named "foo"
// will go to a single remote Durable Object instance.
const stub = env.MY_DURABLE_OBJECT.getByName("foo");
// Call the `sayHello()` RPC method on the stub to invoke the method on
// the remote Durable Object instance.
const greeting = await stub.sayHello("world");
return new Response(greeting);
},
};