rays/app
Declare the Workers and Durable Object classes of a rays app in Gleam, and reach them from each other without knowing their binding names.
Proof of concept. The API and the names it derives may change.
Each Worker module declares its Worker as a constant, beside the
main function that builds it:
// src/shop/inventory.gleam
import rays/app
import rays/worker
pub const worker = app.Worker("inventory", entrypoint: main)
pub fn main() -> worker.Worker {
worker.new(handle_request)
|> worker_rpc.bind(stock)
}
A Durable Object class needs no constant: the function that defines it is its handle, and carries its memory type.
The app module’s pub fn app() -> App registers them all, and says
which Worker hosts each class. Nothing imports it: the build reads it to
make the Cloudflare configuration.
// src/shop.gleam
import rays/app.{type App}
import shop/cart
import shop/inventory
import shop/storefront
pub fn app() -> App {
app.new("shop", main: storefront.worker)
|> app.register_worker(inventory.worker)
|> app.register_durable_object(cart.cart, hosted_by: storefront.worker)
}
A Worker then reaches another through its constant, and a Durable Object class through its function. Calling another Worker’s methods means importing its module anyway, so the constant is already in reach:
// src/shop/storefront.gleam
app.connect_worker(inventory.worker)
|> worker_rpc.call(inventory.stock, sku)
case app.connect_durable_object(cart.cart) {
Ok(carts) -> durable_object.get_by_name(carts, user_id)
Error(Nil) -> todo
}
A misspelt Worker is a compile error, not a missing binding at runtime,
and a Durable Object namespace has the memory type of its class.
Neither needs the Worker’s env: rays keeps the one the runtime gives
it, which is the same for every request, RPC call and Durable Object of
the Worker.
How it works
The binding names are derived, the same way on both sides, from what the
code already holds: a Worker’s name, or a class’s name. raysApp() in
@rays/config/app calls app() when the configuration is loaded and
gives every Worker of the app a binding to every other Worker except the
main one, which is the app’s front door, and to every Durable Object
class, under those names. Nothing is generated.
| Resource | Binding |
|---|---|
app.Worker("inventory", ..) | INVENTORY |
app.Worker("order-history", ..) | ORDER_HISTORY |
pub fn shopping_cart() | SHOPPING_CART |
Types
A mistake in how an app is put together, found by validate.
pub type AppError {
InvalidName(name: String)
DuplicateWorker(name: String)
DuplicateClass(class_name: String)
UnknownHost(class_name: String, host: String)
BindingClash(binding: String, first: String, second: String)
}
Constructors
-
InvalidName(name: String)The app’s name, or a Worker’s, isn’t lowercase letters, digits and hyphens starting with a letter, as a Worker’s name must be.
-
DuplicateWorker(name: String)The app adds two Workers with the same name.
-
DuplicateClass(class_name: String)The app adds the same Durable Object class twice.
-
UnknownHost(class_name: String, host: String)A Durable Object class is hosted by a Worker the app doesn’t add.
-
BindingClash(binding: String, first: String, second: String)Two resources of the app would have the same binding name.
A Worker of the app: its name within the app, and its entrypoint, the
function that builds it. Declare one as a constant in the Worker’s own
module, conventionally named worker, and pass it around instead of a
binding name.
The name is lowercase letters, digits and hyphens, starting with a
letter. The app’s main Worker is deployed under the app’s name, and the
others under <app name>-<name>.
pub type Worker {
Worker(name: String, entrypoint: fn() -> worker.Worker)
}
Constructors
-
Worker(name: String, entrypoint: fn() -> worker.Worker)
Values
pub fn connect_durable_object(
class: fn() -> durable_object.DurableObject(memory),
) -> Result(durable_object.Namespace(memory), Nil)
Connect to a Durable Object class of the app, from any of its Workers or Durable Objects, whichever Worker hosts it.
This gives the class’s namespace, all of its instances. Choose one with
durable_object.get_by_name or durable_object.get, then call its
methods with durable_object.call.
Returns an error if the Worker has no binding to the class, as when the
app doesn’t register it with register_durable_object. Unlike
connect_worker, this can’t wait until a call, since ids and stubs are
made from the namespace straight away.
Examples
case app.connect_durable_object(visit_counter.visit_counter) {
Ok(counters) -> durable_object.get_by_name(counters, "everyone")
Error(Nil) -> todo
}
pub fn connect_durable_object_binding(
binding: String,
) -> Result(durable_object.Namespace(memory), Nil)
Connect to a Durable Object class outside the app, through a Durable
Object binding configured by hand, such as one to a class another
project hosts. For a class of the app, use connect_durable_object.
A binding name carries no type, so nothing checks that the namespace is
of the class you expect: its memory type is whatever the first method
you call it with says. Calling a method of another class fails with
durable_object.RpcFailed(worker_rpc.NotBound(..)), at runtime.
Returns an error if the Worker has no binding with that name, or the binding isn’t a Durable Object namespace.
Examples
case app.connect_durable_object_binding("SESSIONS") {
Ok(sessions) -> durable_object.get_by_name(sessions, session_id)
Error(Nil) -> todo
}
pub fn connect_worker(worker: Worker) -> worker_rpc.Service
Connect to another Worker of the app, to call its RPC methods with
worker_rpc.call.
This always succeeds: the binding is looked up in the Worker’s env when a
method is called. If the Worker has no binding to it, as when its
configuration doesn’t come from raysApp(), or a test didn’t set up the
app with simulate, the call fails with worker_rpc.NotConfigured.
Examples
app.connect_worker(names.worker)
|> worker_rpc.call(names.find_visitor, Nil)
pub fn connect_worker_binding(
binding: String,
) -> worker_rpc.Service
Connect to a rays Worker outside the app, through a service binding
configured by hand, such as one to another team’s Worker. For a Worker
of the app, use connect_worker, which needs no binding name.
Like connect_worker, this always succeeds, and a call fails with
worker_rpc.NotConfigured if the Worker has no binding with that name.
Whether the binding really is a rays Worker that serves the method is
only known when it is called.
Examples
app.connect_worker_binding("BILLING")
|> worker_rpc.call(billing.charge, order)
pub fn describe_error(error: AppError) -> String
A sentence describing the mistake, for the build to report.
pub fn new(name: String, main main: Worker) -> App
Start an app with its main Worker: the one that receives the app’s requests, deployed under the app’s name. The app’s other Workers don’t get a binding to it.
pub fn register_durable_object(
app: App,
class: fn() -> durable_object.DurableObject(memory),
hosted_by host: Worker,
) -> App
Add a Durable Object class to the app, hosted by one of its Workers.
Cloudflare has two relationships between a class and Workers. Exactly
one Worker hosts the class: it exports it, and owns its instances
and their storage. Any number of Workers bind to it, to call its
instances. hosted_by names the host; every Worker of the app, the host
included, is bound to the class, so any of them, and any Durable Object,
can reach it with connect_durable_object.
Choose the host with care: moving a class to another Worker later is a transfer of its stored data, which Cloudflare makes in two deploys.
The class keeps the name the build gives it, from its function, so
pub fn chat_room() is ChatRoom.
pub fn register_worker(app: App, worker: Worker) -> App
Add a Worker to the app. Every Worker of the app can reach it with
connect_worker.
pub fn simulate(app: App, context context: worker.Context) -> Nil
Set up the app for gleam test, so that connect_worker reaches every
Worker of the app but the main one, as it does when deployed. Each is
built by its entrypoint and given an empty env of its own. Call it at the
start of each test that needs it.
The Workers of a test all run in one JavaScript runtime, so they share
this setup: a simulated Worker can also reach itself, which a deployed
one can’t. Durable Object classes aren’t simulated, so
connect_durable_object finds no binding.
Examples
pub fn greets_the_visitor_test() {
app.simulate(greeter.app(), context: simulate.context())
front.handle_request(request, simulate.env(), simulate.context())
// ...
}