Skip to content

Commit df81eb1

Browse files
committed
Documentation
1 parent 51866bf commit df81eb1

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,43 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
3838
}
3939
```
4040

41+
### `http` Feature
42+
43+
`worker` `0.1` introduced an `http` feature which starts to replace custom types with widely used types from the [`http`](https://docs.rs/http/latest/http/) crate.
44+
45+
This makes it much easier to use crates which use these standard types such as `axum` and `hyper`.
46+
47+
This currently does a few things:
48+
49+
1. Introduce `Body`, which implements `http_body::Body` and is a simple wrapper around `web_sys::ReadableStream`.
50+
1. The `req` argument when using the `[event(fetch)]` macro becomes `http::Request<worker::Body>`.
51+
1. The expected return type for the fetch handler is `http::Response<B>` where `B` can be any `http_body::Body`.
52+
1. The argument for `Fetcher::fetch_request` is `http::Request<worker::Body>`.
53+
1. The return type of `Fetcher::fetch_request` is `http::Response<worker::Body>`.
54+
55+
The end result is being able to use frameworks like `axum` directly (see [example](./examples/axum)):
56+
57+
```rust
58+
pub async fn root() -> &'static str {
59+
"Hello Axum!"
60+
}
61+
62+
fn router() -> Router {
63+
Router::new().route("/", get(root))
64+
}
65+
66+
#[event(fetch)]
67+
async fn fetch(
68+
req: HttpRequest,
69+
_env: Env,
70+
_ctx: Context,
71+
) -> Result<axum::http::Response<axum::body::Body>> {
72+
Ok(router().call(req).await?)
73+
}
74+
```
75+
76+
We also implement `From::from` between `worker::Request` and `http::Request<worker::Body>`, and between `worker::Response` and `http::Response<worker::Body>`. This allows you to convert your code incrementally if it is tightly coupled to the original types.
77+
4178
### Or use the `Router`:
4279

4380
Parameterize routes and access the parameter values from within a handler. Each handler function takes a

worker/src/http/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Body {
3535
Self(None)
3636
}
3737

38-
/// Create a `Body` using a [`Stream`](futures::stream::Stream)
38+
/// Create a `Body` using a [`Stream`](futures_util::stream::Stream)
3939
pub fn from_stream<S>(stream: S) -> Result<Self, crate::Error>
4040
where
4141
S: TryStream + 'static,

worker/src/http/request.rs

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ fn version_from_string(version: &str) -> http::Version {
1616
}
1717
}
1818

19+
/// **Requires** `http` feature. Convert [`web_sys::Request`](web_sys::Request)
20+
/// to [`worker::HttpRequest`](worker::HttpRequest)
1921
pub fn from_wasm(req: web_sys::Request) -> http::Request<Body> {
2022
let mut builder = http::request::Builder::new()
2123
.uri(req.url())
@@ -38,6 +40,8 @@ pub fn from_wasm(req: web_sys::Request) -> http::Request<Body> {
3840
}
3941
}
4042

43+
/// **Requires** `http` feature. Convert [`worker::HttpRequest`](worker::HttpRequest)
44+
/// to [`web_sys::Request`](web_sys::Request)
4145
pub fn to_wasm(mut req: http::Request<Body>) -> web_sys::Request {
4246
let mut init = web_sys::RequestInit::new();
4347
init.method(req.method().as_str());

worker/src/http/response.rs

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ impl<B: http_body::Body<Data = Bytes>> Stream for BodyStream<B> {
5858
}
5959
}
6060

61+
/// **Requires** `http` feature. Convert generic [`http::Response<B>`](worker::HttpResponse)
62+
/// to [`web_sys::Resopnse`](web_sys::Response) where `B` can be any [`http_body::Body`](http_body::Body)
6163
pub fn to_wasm<B>(mut res: http::Response<B>) -> web_sys::Response
6264
where
6365
B: http_body::Body<Data = Bytes> + 'static,
@@ -85,6 +87,8 @@ where
8587
.unwrap()
8688
}
8789

90+
/// **Requires** `http` feature. Convert [`web_sys::Resopnse`](web_sys::Response)
91+
/// to [`worker::HttpResponse`](worker::HttpResponse)
8892
pub fn from_wasm(res: web_sys::Response) -> HttpResponse {
8993
let mut builder =
9094
http::response::Builder::new().status(http::StatusCode::from_u16(res.status()).unwrap());

worker/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,17 @@ mod websocket;
8484
pub type Result<T> = StdResult<T, error::Error>;
8585

8686
#[cfg(feature = "http")]
87+
/// **Requires** `http` feature. A convenience Body type which wraps [`web_sys::ReadableStream`](web_sys::ReadableStream)
88+
/// and implements [`http_body::Body`](http_body::Body)
8789
pub use http::body::Body;
8890
#[cfg(feature = "http")]
8991
pub use http::{
9092
request::from_wasm as request_from_wasm, request::to_wasm as request_to_wasm,
9193
response::from_wasm as response_from_wasm, response::to_wasm as response_to_wasm,
9294
};
9395
#[cfg(feature = "http")]
96+
/// **Requires** `http` feature. Type alias for `http::Request<worker::Body>`.
9497
pub type HttpRequest = ::http::Request<http::body::Body>;
9598
#[cfg(feature = "http")]
99+
/// **Requires** `http` feature. Type alias for `http::Response<worker::Body>`.
96100
pub type HttpResponse = ::http::Response<http::body::Body>;

0 commit comments

Comments
 (0)