A library for writing type-safe Durable Objects in Rust.

Overview

do-proxy

Crates.io Docs.rs MIT licensed

A library for writing type-safe Durable Objects (DOs) in Rust.

With do-proxy you can:

  • Easily write type-safe APIs for Durable Objects.
  • Abstract over fetch, alarm and request-response glue code.

Overview

do-proxy provides a core trait DoProxy that abstracts over ser/de request response code, object initalization and loading, and Error handling glue code.

After a struct implements DoProxy, the macro do_proxy! creates the workers-rs' #[DurableObject] struct which ends up generating the final object.

Object Lifecycle

This library provides two separate Request type. A normal Request which is what the durable object will be sent 99% of the time and an Init type, which is optionally sent to initialize the object.

For example, lets say we have a Person object. The struct might look like:

struct Person {
    birthday: DateTime<Utc>,
    name: String
}

impl DoProxy for Person {
    // ...
}

do_proxy!(Person, PersonObject);

The birthday and name fields are non-optional and required. However, when constructing a durable object with new in Rust or constructor in TypeScript, the only information you get is the State and Storage. So when you're loading the Person object for the first time, you'll have to use bogus values for name and birthday because they haven't been set yet.

The request that prompted the creation of the object will likely be some kind of "create" command which sets birthday and name to something realistic. But that's not guaranteed.

What if the person receives a command CalculateNextBirthday before its been created? Now you need to explicitly check that those values aren't bogus or wrap everything in an Option. Both of those options are either prone to errors (bogus value) or unergonomic (Option).

To solve this, do-proxy has two functions that are used to construct an object.

  • init: crates and saves all information necessary to construct an object in load_from_storage. When a user of the library sends a request to the Person object, they'll be able to optionally add DoProxy::Init data. If that data is present, init will be called before load_from_storage.
  • load_from_storage: loads an object from storage. If the object is missing fields or hasn't been initialized, this function should error.

In the following example, we send both initialization information along with a command. The object is first initialized and then the command is handled:

let proxy = env.obj::<Person>("[email protected]");
let resp = proxy
    .init(Person::new("Bob", bobs_birthday))
    .and_send(Command::CalculateNextBirthday).await?;

If you know that the object must be initialized or can't create initialization information, you can just send the command:

let proxy = env.obj::<Person>("[email protected]");
let resp = proxy.send(Command::CalculateNextBirthday).await?;

This approach lets you avoid options, bogus values and the init function is async.

Examples

The crates under ./examples act as examples for the library, and in the future, they act as fully-fledged copy+paste building blocks for distributed systems.

Each crate has a hurl script that show how the wrapping worker can be queried.

  • inserter: An InserterObject responds to a simple KV-like API for getting, inserting and deleting KV pairs in its storage. Example:
POST http://localhost:8787/test_do
{
    "insert": {
        "key": "hello",
        "value": "world!"
    }
}

POST http://localhost:8787/test_do
{
    "get": {
        "key": "hello"
    }
}

# returns { "value": "world!" }
You might also like...
Backroll is a pure Rust implementation of GGPO rollback networking library.

backroll-rs Backroll is a pure Rust implementation of GGPO rollback networking library. Development Status This is still in an untested alpha stage. A

A Rust library for parsing the SOME/IP network protocol (without payload interpretation).

someip_parse A Rust library for parsing the SOME/IP network protocol (without payload interpretation). Usage Add the following to your Cargo.toml: [de

A library for easily creating WebRTC data channel connections in Rust

Cyberdeck A library for easily creating WebRTC data channel connections in Rust.

Modrinth API is a simple library for using Modrinth's API in Rust projects

Ferinth is a simple library for using the Modrinth API in Rust projects. It uses reqwest as its HTTP(S) client and deserialises responses to typed structs using serde.

An LV2 host library for Rust.

Livi A library for hosting LV2 plugins. Note: This is a work in progress and has not yet been full tested. Supported LV2 Features LV2 has a simple cor

A Rust compiler plugin and support library to annotate overflow behavior

overflower This project contains a compiler plugin and supporting library to allow the programmer to annotate their code to declare how integer overfl

Dav-server-rs - Rust WebDAV server library. A fork of the webdav-handler crate.

dav-server-rs A fork of the webdav-handler-rs project. Generic async HTTP/Webdav handler Webdav (RFC4918) is defined as HTTP (GET/HEAD/PUT/DELETE) plu

Peer-to-peer communications library for Rust based on QUIC protocol

qp2p Crate Documentation MaidSafe website SAFE Dev Forum SAFE Network Forum Overview This library provides an API to simplify common tasks when creati

A BitTorrent V1 engine library for Rust (and currently Linux)

cratetorrent Cratetorrent is a Rust crate implementing the BitTorrent version 1 protocol. It can be used as a library and also provides a simple examp

Owner
Fisher Darling
Systems Engineer at Cloudflare. My interests are Rust, Cybersecurity, cloud architecture and programming languages!
Fisher Darling
DNS Server written in Rust for fun, see https://dev.to/xfbs/writing-a-dns-server-in-rust-1gpn

DNS Fun Ever wondered how you can write a DNS server in Rust? No? Well, too bad, I'm telling you anyways. But don't worry, this is going to be a fun o

Patrick Elsen 26 Jan 13, 2023
A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...

Tokio A runtime for writing reliable, asynchronous, and slim applications with the Rust programming language. It is: Fast: Tokio's zero-cost abstracti

Tokio 18.7k Dec 30, 2022
Safe Rust crate for creating socket servers and clients with ease.

bitsock Safe Rust crate for creating socket servers and clients with ease. Description This crate can be used for Client <--> Server applications of e

Lorenzo Torres 3 Nov 25, 2021
The Safe Network Core. API message definitions, routing and nodes, client core api.

safe_network The Safe Network Core. API message definitions, routing and nodes, client core api. License This Safe Network repository is licensed unde

MaidSafe 101 Dec 19, 2022
🔌 A curseforge proxy server, keeping your API key safe and sound.

?? CFPROXY - The curseforge proxy server Curseforge has locked down their API and now restricts access without authentification. This spells trouble f

null 6 Nov 7, 2022
The gRPC library for Rust built on C Core library and futures

gRPC-rs gRPC-rs is a Rust wrapper of gRPC Core. gRPC is a high performance, open source universal RPC framework that puts mobile and HTTP/2 first. Sta

TiKV Project 1.6k Jan 7, 2023
A µTP (Micro/uTorrent Transport Library) library implemented in Rust

rust-utp A Micro Transport Protocol library implemented in Rust. API documentation Overview The Micro Transport Protocol is a reliable transport proto

Ricardo Martins 134 Dec 11, 2022
A library to work with CIDRs in rust

ipnetwork This is a library to work with IPv4 and IPv6 CIDRs in Rust Run Clippy by doing rustup component add clippy cargo clippy Installation This c

Abhishek Chanda 98 Dec 12, 2022
Nanomsg library for Rust

Nanomsg Documentation Nanomsg is a modern messaging library that is the successor to ZeroMQ, written in C by Martin Sustrik and colleagues. The nanoms

Daniel Fagnan 371 Nov 18, 2022
A Constrained Application Protocol(CoAP) library implemented in Rust.

coap-rs A fast and stable Constrained Application Protocol(CoAP) library implemented in Rust. Features: CoAP core protocol RFC 7252 CoAP Observe optio

Covertness 170 Dec 19, 2022