refactor: update devbox configuration and scripts

This commit is contained in:
Prad Nukala
2024-11-25 13:54:33 -05:00
parent 1568844255
commit e04d071ca7
63 changed files with 1069 additions and 202 deletions
+45
View File
@@ -0,0 +1,45 @@
export * from "./stub.js";
/**
* 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.toml within JavaScript
*
* @typedef {Object} Env
* @property {DurableObjectNamespace} SONR_DURABLE_CLIENT - The Durable Object namespace binding
*/
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.toml
* @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) {
// We will create a `DurableObjectId` using the pathname from the Worker request
// This id refers to a unique instance of our 'MyDurableObject' class above
let id = env.SONR_DURABLE_CLIENT.idFromName(new URL(request.url).pathname);
// 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 = env.SONR_DURABLE_CLIENT.get(id);
// 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 new Response(greeting);
},
};
+26
View File
@@ -0,0 +1,26 @@
import { DurableObject } from "cloudflare:workers";
/** A Durable Object's behavior is defined in an exported Javascript class */
export class SonrDurableClient 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.toml
*/
constructor(ctx, env) {
super(ctx, env);
}
/**
* 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 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}!`;
}
}