[go: up one dir, main page]

Crate worker

source ·
Expand description

§Features

§d1

Allows the use of D1 bindings and query! macro.

§queue

Enables queue event type in [event] macro.

// Consume messages from a queue
#[event(queue)]
pub async fn main(message_batch: MessageBatch<MyType>, env: Env, _ctx: Context) -> Result<()> {
    Ok(())
}

§http

worker 0.0.21 introduced an http feature flag which starts to replace custom types with widely used types from the http crate.

This makes it much easier to use crates which use these standard types such as axum.

This currently does a few things:

  1. Introduce Body, which implements http_body::Body and is a simple wrapper around web_sys::ReadableStream.
  2. The req argument when using the [event(fetch)] macro becomes http::Request<worker::Body>.
  3. The expected return type for the fetch handler is http::Response<B> where B can be any http_body::Body<Data=Bytes>.
  4. The argument for Fetcher::fetch_request is http::Request<worker::Body>.
  5. The return type of Fetcher::fetch_request is http::Response<worker::Body>.

The end result is being able to use frameworks like axum directly (see example):

pub async fn root() -> &'static str {
    "Hello Axum!"
}

fn router() -> Router {
    Router::new().route("/", get(root))
}

#[event(fetch)]
async fn fetch(
    req: HttpRequest,
    _env: Env,
    _ctx: Context,
) -> Result<http::Response<axum::body::Body>> {
    Ok(router().call(req).await?)
}

We also implement try_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.

Re-exports§

Modules§

  • Requires d1 feature.
  • Durable Objects provide low-latency coordination and consistent storage for the Workers platform. A given namespace can support essentially unlimited Durable Objects, with each Object having access to a transactional, key-value storage API.
  • Implements TlsConnect for Socket to enable tokio_postgres connections to databases using TLS.

Macros§

  • When debugging your Worker via wrangler dev, wrangler tail, or from the Workers Dashboard, anything passed to this macro will be printed to the terminal or written to the console.
  • When debugging your Worker via wrangler dev, wrangler tail, or from the Workers Dashboard, anything passed to this macro will be printed to the terminal or written to the console.
  • When debugging your Worker via wrangler dev, wrangler tail, or from the Workers Dashboard, anything passed to this macro will be printed to the terminal or written to the console.
  • When debugging your Worker via wrangler dev, wrangler tail, or from the Workers Dashboard, anything passed to this macro will be printed to the terminal or written to the console.
  • Requires d1 feature. Prepare a D1 query from the provided D1Database, query string, and optional query parameters.

Structs§

Enums§

Traits§

Functions§

Type Aliases§

  • Requires http feature. Type alias for http::Request<worker::Body>.
  • Requires http feature. Type alias for http::Response<worker::Body>.
  • A string value representing a binding to a secret in a Worker.
  • A string value representing a binding to an environment variable in a Worker.

Attribute Macros§