Skip to content

Commit

Permalink
chore: update readme to align with latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Sep 16, 2021
1 parent 91883da commit 1bc536d
Showing 1 changed file with 53 additions and 51 deletions.
104 changes: 53 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ use worker::*;

#[event(fetch)]
pub async fn main(req: Request, env: Env) -> Result<Response> {
// Create an instance of the Router, and pass it some shared data to be used within routes.
// In this case, `()` is used for "no data" so the type information is set for the generic used.
// Access the shared data in your routes using the `ctx.data()` method.
let router = Router::new(());

// Create an instance of the Router, which can use paramaters (/user/:name) or wildcard values
// (/file/*pathname). Alternatively, use `Router::with_data(D)` and pass in arbitrary data for
// routes to access and share using the `ctx.data()` method.
let router = Router::new();

// useful for JSON APIs
#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -102,14 +102,13 @@ pub async fn main(req: Request, env: Env) -> Result<Response> {
Response::from_bytes(data)
})
.run(req, env).await
}
}
```


## Getting Started

Make sure you have [`wrangler`](https://github.com/cloudflare/wrangler) installed at a recent
version (>=v1.19.2). If you want to publish your Rust worker code, you will need to have a
Make sure you have [`wrangler`](https://github.com/cloudflare/wrangler) installed at a recent
version (>=v1.19.2). If you want to publish your Rust worker code, you will need to have a
[Cloudflare account](https://cloudflare.com).

Run `wrangler --version` to check your installation and if it meets the version requirements.
Expand All @@ -120,8 +119,8 @@ cd project_name
wrangler build
```

You should see a new project layout with a `src/lib.rs`. Start there! Use any local or remote crates
and modules (as long as they compile to the `wasm32-unknown-unknown` target).
You should see a new project layout with a `src/lib.rs`. Start there! Use any local or remote crates
and modules (as long as they compile to the `wasm32-unknown-unknown` target).

Once you're ready to run your project:

Expand All @@ -130,15 +129,16 @@ wrangler dev
```

And then go live:

```bash
# configure your routes, zones & more in your worker's `wrangler.toml` file
wrangler publish
```

## Durable Object, KV, Secret, & Variable Bindings

All "bindings" to your script (Durable Object & KV Namespaces, Secrets, and Variables) are
accessible from the `env` parameter provided to both the entrypoint (`main` in this example), and to
All "bindings" to your script (Durable Object & KV Namespaces, Secrets, and Variables) are
accessible from the `env` parameter provided to both the entrypoint (`main` in this example), and to
the route handler callback (in the `ctx` argument), if you use the `Router` from the `worker` crate.

```rust
Expand All @@ -148,7 +148,7 @@ use worker::*;
pub async fn main(req: Request, env: Env) -> Result<Response> {
utils::set_panic_hook();

let router = Router::new(());
let router = Router::new();

router
.on_async("/durable", |_req, ctx| async move {
Expand All @@ -173,18 +173,21 @@ pub async fn main(req: Request, env: Env) -> Result<Response> {
}
```

For more information about how to configure these bindings, see:
For more information about how to configure these bindings, see:

- https://developers.cloudflare.com/workers/cli-wrangler/configuration#keys
- https://developers.cloudflare.com/workers/learning/using-durable-objects#configuring-durable-object-bindings

## Durable Objects

### BETA WARNING

Durable Objects are still in **BETA**, so the same rules apply to the Durable Object code and APIs here in these crates.

### Define a Durable Object in Rust
To define a Durable Object using the `worker` crate you need to implement the `DurableObject` trait
on your own struct. Additionally, the `#[durable_object]` attribute macro must be applied to _both_

To define a Durable Object using the `worker` crate you need to implement the `DurableObject` trait
on your own struct. Additionally, the `#[durable_object]` attribute macro must be applied to _both_
your struct definition and the trait `impl` block for it.

```rust
Expand All @@ -193,10 +196,9 @@ use worker::*;
#[durable_object]
pub struct Chatroom {
users: Vec<User>,
messages: Vec<Message>
messages: Vec<Message>,
state: State,
env: Env, // access `Env` across requests, use inside `fetch`

}

#[durable_object]
Expand All @@ -217,7 +219,7 @@ impl DurableObject for Chatroom {
}
```

You'll need to "migrate" your worker script when it's published so that it is aware of this new
You'll need to "migrate" your worker script when it's published so that it is aware of this new
Durable Object, and include a binding in your `wrangler.toml`.

- Include the Durable Object binding type in you `wrangler.toml` file:
Expand All @@ -235,63 +237,63 @@ tag = "v1" # Should be unique for each entry
new_classes = ["Chatroom"] # Array of new classes
```

- For more information about migrating your Durable Object as it changes, see the docs here:
https://developers.cloudflare.com/workers/learning/using-durable-objects#durable-object-migrations-in-wranglertoml
- For more information about migrating your Durable Object as it changes, see the docs here:
https://developers.cloudflare.com/workers/learning/using-durable-objects#durable-object-migrations-in-wranglertoml

# Notes and FAQ

It is exciting to see how much is possible with a framework like this, by expanding the options
developers have when building on top of the Workers platform. However, there is still much to be
done. Expect a few rough edges, some unimplemented APIs, and maybe a bug or two here and there. It’s
worth calling out here that some things that may have worked in your Rust code might not work here -
it’s all WebAssembly at the end of the day, and if your code or third-party libraries don’t target
`wasm32-unknown-unknown`, they can’t be used on Workers. Additionally, you’ve got to leave your
threaded async runtimes at home; meaning no Tokio or async_std support. However, async/await syntax
is still available and supported out of the box when you use the `worker` crate.
It is exciting to see how much is possible with a framework like this, by expanding the options
developers have when building on top of the Workers platform. However, there is still much to be
done. Expect a few rough edges, some unimplemented APIs, and maybe a bug or two here and there. It’s
worth calling out here that some things that may have worked in your Rust code might not work here -
it’s all WebAssembly at the end of the day, and if your code or third-party libraries don’t target
`wasm32-unknown-unknown`, they can’t be used on Workers. Additionally, you’ve got to leave your
threaded async runtimes at home; meaning no Tokio or async_std support. However, async/await syntax
is still available and supported out of the box when you use the `worker` crate.

We fully intend to support this crate and continue to build out its missing features, but your help
and feedback is a must. We don’t like to build in a vacuum, and we’re in an incredibly fortunate
position to have brilliant customers like you who can help steer us towards an even better product.
We fully intend to support this crate and continue to build out its missing features, but your help
and feedback is a must. We don’t like to build in a vacuum, and we’re in an incredibly fortunate
position to have brilliant customers like you who can help steer us towards an even better product.

So give it a try, leave some feedback, and star the repo to encourage us to dedicate more time and
So give it a try, leave some feedback, and star the repo to encourage us to dedicate more time and
resources to this kind of project.

If this is interesting to you and you want to help out, we’d be happy to get outside contributors
started. We know there are improvements to be made such as compatibility with popular Rust HTTP
ecosystem types (we have an example conversion for [Headers](https://github.com/cloudflare/workers-rs/blob/3d5876a1aca0a649209152d1ffd52dae7bccda87/libworker/src/headers.rs#L131-L167) if you want to make one), implementing additional Web APIs, utility crates,
and more. In fact, we’re always on the lookout for great engineers, and hiring for many open roles -
If this is interesting to you and you want to help out, we’d be happy to get outside contributors
started. We know there are improvements to be made such as compatibility with popular Rust HTTP
ecosystem types (we have an example conversion for [Headers](https://github.com/cloudflare/workers-rs/blob/3d5876a1aca0a649209152d1ffd52dae7bccda87/libworker/src/headers.rs#L131-L167) if you want to make one), implementing additional Web APIs, utility crates,
and more. In fact, we’re always on the lookout for great engineers, and hiring for many open roles -
please [take a look](https://www.cloudflare.com/careers/).

### FAQ

1. Can I deploy a Worker that uses `tokio` or `async_std` runtimes?
1. Can I deploy a Worker that uses `tokio` or `async_std` runtimes?

- Currently no. All crates in your Worker project must compile to `wasm32-unknown-unknown` target,
which is more limited in some ways than targets for x86 and ARM64.
- Currently no. All crates in your Worker project must compile to `wasm32-unknown-unknown` target,
which is more limited in some ways than targets for x86 and ARM64.

2. The `worker` crate doesn't have _X_! Why not?

- Most likely, it should, we just haven't had the time to fully implement it or add a library to
wrap the FFI. Please let us know you need a feature by [opening an issue](https://github.com/cloudflare/workers-rs/issues).
- Most likely, it should, we just haven't had the time to fully implement it or add a library to
wrap the FFI. Please let us know you need a feature by [opening an issue](https://github.com/cloudflare/workers-rs/issues).

3. My bundle size exceeds Workers 1MB limits, what do I do?

- We're working on solutions here, but in the meantime you'll need to minimize the number of crates
your code depends on, or strip as much from the `.wasm` binary as possible. Here are some extra
steps you can try: https://rustwasm.github.io/book/reference/code-size.html#optimizing-builds-for-code-size
your code depends on, or strip as much from the `.wasm` binary as possible. Here are some extra
steps you can try: https://rustwasm.github.io/book/reference/code-size.html#optimizing-builds-for-code-size

# Contributing

Your feedback is welcome and appreciated! Please use the issue tracker to talk about potential
implementations or make feature requests. If you're interested in making a PR, we suggest opening up
Your feedback is welcome and appreciated! Please use the issue tracker to talk about potential
implementations or make feature requests. If you're interested in making a PR, we suggest opening up
an issue to talk about the change you'd like to make as early as possible.

## Project Contents

- **worker**: the user-facing crate, with Rust-famaliar abstractions over the Rust<->JS/WebAssembly
interop via wrappers and convenience library over the FFI bindings.
- **worker**: the user-facing crate, with Rust-famaliar abstractions over the Rust<->JS/WebAssembly
interop via wrappers and convenience library over the FFI bindings.
- **worker-sys**: Rust extern "C" definitions for FFI compatibility with the Workers JS Runtime.
- **worker-macros**: exports `event` and `durable_object` macros for wrapping Rust entry point in a
`fetch` method of an ES Module, and code generation to create and interact with Durable Objects.
- **worker-macros**: exports `event` and `durable_object` macros for wrapping Rust entry point in a
`fetch` method of an ES Module, and code generation to create and interact with Durable Objects.
- **worker-sandbox**: a functioning Cloudflare Worker for testing features and ergonomics.
- **worker-build**: a cross-platform build command for `workers-rs`-based projects.

0 comments on commit 1bc536d

Please sign in to comment.