Skip to content

Commit

Permalink
Add Service Bindings support
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-melnychuk authored and zebp committed Nov 17, 2022
1 parent f3e65bd commit cd1d989
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
9 changes: 7 additions & 2 deletions worker/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::error::Error;
use crate::Result;
use crate::{durable::ObjectNamespace, DynamicDispatcher};
use crate::{durable::ObjectNamespace, DynamicDispatcher, Fetcher, Result};

use js_sys::Object;
use wasm_bindgen::{prelude::*, JsCast, JsValue};
Expand Down Expand Up @@ -51,6 +50,12 @@ impl Env {
pub fn dynamic_dispatcher(&self, binding: &str) -> Result<DynamicDispatcher> {
self.get_binding(binding)
}

/// Get a [Service Binding](https://developers.cloudflare.com/workers/runtime-apis/service-bindings/)
/// for Worker-to-Worker communication.
pub fn service(&self, binding: &str) -> Result<Fetcher> {
self.get_binding(binding)
}
}

pub trait EnvBinding: Sized + JsCast {
Expand Down
34 changes: 32 additions & 2 deletions worker/src/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use wasm_bindgen::JsCast;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use worker_sys::{Fetcher as FetcherSys, Response as ResponseSys};

use crate::{Request, RequestInit, Response, Result};
use crate::{Request, RequestInit, Response, Result, env::EnvBinding};

/// A struct for invoking fetch events to other Workers.
pub struct Fetcher(FetcherSys);
Expand Down Expand Up @@ -32,6 +32,36 @@ impl Fetcher {
}
}

impl EnvBinding for Fetcher {
const TYPE_NAME: &'static str = "Fetcher";
}

impl JsCast for Fetcher {
fn instanceof(val: &wasm_bindgen::JsValue) -> bool {
val.is_instance_of::<Fetcher>()
}

fn unchecked_from_js(val: wasm_bindgen::JsValue) -> Self {
Self(val.into())
}

fn unchecked_from_js_ref(val: &wasm_bindgen::JsValue) -> &Self {
unsafe { &*(val as *const JsValue as *const Self) }
}
}

impl From<Fetcher> for JsValue {
fn from(service: Fetcher) -> Self {
JsValue::from(service.0)
}
}

impl AsRef<wasm_bindgen::JsValue> for Fetcher {
fn as_ref(&self) -> &wasm_bindgen::JsValue {
&self.0
}
}

impl From<FetcherSys> for Fetcher {
fn from(inner: FetcherSys) -> Self {
Self(inner)
Expand Down
8 changes: 7 additions & 1 deletion worker/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
http::Method,
request::Request,
response::Response,
Result,
Result, Fetcher,
};

type HandlerFn<D> = fn(Request, RouteContext<D>) -> Result<Response>;
Expand Down Expand Up @@ -94,6 +94,12 @@ impl<D> RouteContext<D> {
pub fn param(&self, key: &str) -> Option<&String> {
self.params.get(key)
}

/// Get a [Service Binding](https://developers.cloudflare.com/workers/runtime-apis/service-bindings/)
/// for Worker-to-Worker communication.
pub fn service(&self, binding: &str) -> Result<Fetcher> {
self.env.service(binding)
}
}

impl<'a> Router<'a, ()> {
Expand Down

0 comments on commit cd1d989

Please sign in to comment.