Skip to content

Commit 1bc536d

Browse files
committed
chore: update readme to align with latest changes
1 parent 91883da commit 1bc536d

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

README.md

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ use worker::*;
4848

4949
#[event(fetch)]
5050
pub async fn main(req: Request, env: Env) -> Result<Response> {
51-
52-
// Create an instance of the Router, and pass it some shared data to be used within routes.
53-
// In this case, `()` is used for "no data" so the type information is set for the generic used.
54-
// Access the shared data in your routes using the `ctx.data()` method.
55-
let router = Router::new(());
51+
52+
// Create an instance of the Router, which can use paramaters (/user/:name) or wildcard values
53+
// (/file/*pathname). Alternatively, use `Router::with_data(D)` and pass in arbitrary data for
54+
// routes to access and share using the `ctx.data()` method.
55+
let router = Router::new();
5656

5757
// useful for JSON APIs
5858
#[derive(Deserialize, Serialize)]
@@ -102,14 +102,13 @@ pub async fn main(req: Request, env: Env) -> Result<Response> {
102102
Response::from_bytes(data)
103103
})
104104
.run(req, env).await
105-
}
105+
}
106106
```
107107

108-
109108
## Getting Started
110109

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

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

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

126125
Once you're ready to run your project:
127126

@@ -130,15 +129,16 @@ wrangler dev
130129
```
131130

132131
And then go live:
132+
133133
```bash
134134
# configure your routes, zones & more in your worker's `wrangler.toml` file
135135
wrangler publish
136136
```
137137

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

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

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

151-
let router = Router::new(());
151+
let router = Router::new();
152152

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

176-
For more information about how to configure these bindings, see:
176+
For more information about how to configure these bindings, see:
177+
177178
- https://developers.cloudflare.com/workers/cli-wrangler/configuration#keys
178179
- https://developers.cloudflare.com/workers/learning/using-durable-objects#configuring-durable-object-bindings
179180

180181
## Durable Objects
181182

182183
### BETA WARNING
184+
183185
Durable Objects are still in **BETA**, so the same rules apply to the Durable Object code and APIs here in these crates.
184186

185187
### Define a Durable Object in Rust
186-
To define a Durable Object using the `worker` crate you need to implement the `DurableObject` trait
187-
on your own struct. Additionally, the `#[durable_object]` attribute macro must be applied to _both_
188+
189+
To define a Durable Object using the `worker` crate you need to implement the `DurableObject` trait
190+
on your own struct. Additionally, the `#[durable_object]` attribute macro must be applied to _both_
188191
your struct definition and the trait `impl` block for it.
189192

190193
```rust
@@ -193,10 +196,9 @@ use worker::*;
193196
#[durable_object]
194197
pub struct Chatroom {
195198
users: Vec<User>,
196-
messages: Vec<Message>
199+
messages: Vec<Message>,
197200
state: State,
198201
env: Env, // access `Env` across requests, use inside `fetch`
199-
200202
}
201203

202204
#[durable_object]
@@ -217,7 +219,7 @@ impl DurableObject for Chatroom {
217219
}
218220
```
219221

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

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

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

241243
# Notes and FAQ
242244

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

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

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

259-
If this is interesting to you and you want to help out, we’d be happy to get outside contributors
260-
started. We know there are improvements to be made such as compatibility with popular Rust HTTP
261-
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,
262-
and more. In fact, we’re always on the lookout for great engineers, and hiring for many open roles -
261+
If this is interesting to you and you want to help out, we’d be happy to get outside contributors
262+
started. We know there are improvements to be made such as compatibility with popular Rust HTTP
263+
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,
264+
and more. In fact, we’re always on the lookout for great engineers, and hiring for many open roles -
263265
please [take a look](https://www.cloudflare.com/careers/).
264266

265267
### FAQ
266268

267-
1. Can I deploy a Worker that uses `tokio` or `async_std` runtimes?
269+
1. Can I deploy a Worker that uses `tokio` or `async_std` runtimes?
268270

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

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

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

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

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

283285
# Contributing
284286

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

289291
## Project Contents
290292

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

0 commit comments

Comments
 (0)