Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DO stub to workers-codegen #693

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions worker-codegen/src/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ fn expand_struct(struct_name: &Ident, sys_name: &Ident) -> anyhow::Result<syn::I
Ok(struct_item)
}

fn expand_from_impl(struct_name: &Ident) -> anyhow::Result<syn::ItemImpl> {
fn expand_from_impl(struct_name: &Ident, from_type: &syn::Type) -> anyhow::Result<syn::ItemImpl> {
let impl_raw = quote!(
impl From<::worker::Fetcher> for #struct_name {
fn from(fetcher: ::worker::Fetcher) -> Self {
impl From<#from_type> for #struct_name {
fn from(fetcher: #from_type) -> Self {
Self(::worker::send::SendWrapper::new(fetcher.into_rpc()))
}
}
Expand All @@ -117,7 +117,7 @@ fn expand_rpc_impl(
for (name, method) in &interface.functions {
println!("\tFound method: '{}'.", name);
let ident = format_ident!("{}", name.to_case(Case::Snake));
let invocation_raw = quote!(self.0.add());
let invocation_raw = quote!(self.0.#ident());
let mut invocation_item: syn::ExprMethodCall = syn::parse2(invocation_raw)?;
for (arg_name, _) in &method.params {
let mut segments = syn::punctuated::Punctuated::new();
Expand Down Expand Up @@ -222,8 +222,15 @@ fn expand_wit(path: &str) -> anyhow::Result<syn::File> {
&interface_name,
&struct_name,
)?));
// From Impl
items.push(syn::Item::Impl(expand_from_impl(&struct_name)?));
// From Impl for Fetcher and Stub
items.push(syn::Item::Impl(expand_from_impl(
&struct_name,
&syn::parse_str("::worker::Fetcher")?,
)?));
items.push(syn::Item::Impl(expand_from_impl(
&struct_name,
&syn::parse_str("::worker::Stub")?,
)?));
}

let rust_file = syn::File {
Expand Down
4 changes: 4 additions & 0 deletions worker/src/durable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl Stub {
let response = JsFuture::from(promise).await?;
Ok(response.dyn_into::<web_sys::Response>()?.into())
}

pub fn into_rpc<T: JsCast>(self) -> T {
self.inner.unchecked_into()
}
}

/// Use an ObjectNamespace to get access to Stubs for communication with a Durable Object instance.
Expand Down
Loading