Skip to content

Commit b8af7e5

Browse files
committed
Updated Durable Object fetch method to be optional.
1 parent d4852fa commit b8af7e5

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

worker-build/src/js/durable_objects_shim.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ const globalStorePrefix = "__DO_WRAPPED_"; // For globalThis storage
66
if (typeof __WORKER_BUILD_DO_NAMES__ !== 'undefined' && Array.isArray(__WORKER_BUILD_DO_NAMES__)) {
77
__WORKER_BUILD_DO_NAMES__.forEach(className => {
88
const OriginalClass = imports[className];
9-
if (typeof OriginalClass === 'function' && OriginalClass.prototype && typeof OriginalClass.prototype.fetch === 'function') {
10-
console.log(`[shim.js] Wrapping DO (identified by worker-build): ${className}`);
9+
if (typeof OriginalClass === 'function' && OriginalClass.prototype) {
10+
console.log(`[shim.js] Wrapping DO: ${className}`);
1111
successfullyWrappedDONames.push(className);
1212
const WrappedClass = class extends DurableObject {
1313
constructor(state, env) { super(state, env); this._inner = new OriginalClass(state, env); }
14-
async fetch(request) { return this._inner.fetch(request); }
1514
};
1615
Object.getOwnPropertyNames(OriginalClass.prototype).forEach(methodName => {
17-
if (methodName !== 'constructor' && methodName !== 'fetch') {
16+
if (methodName !== 'constructor') {
1817
if (typeof OriginalClass.prototype[methodName] === 'function') {
1918
WrappedClass.prototype[methodName] = function(...args) { return this._inner[methodName](...args); };
2019
}

worker-macros/src/durable_object.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn expand_macro(tokens: TokenStream) -> syn::Result<TokenStream> {
2323
#[derive(Default)]
2424
struct OptionalMethods {
2525
has_alarm: bool,
26+
has_fetch: bool,
2627
has_websocket_message: bool,
2728
has_websocket_close: bool,
2829
has_websocket_error: bool,
@@ -75,6 +76,8 @@ pub fn expand_macro(tokens: TokenStream) -> syn::Result<TokenStream> {
7576
})
7677
},
7778
"fetch" => {
79+
optional_methods.has_fetch = true;
80+
7881
let mut method = impl_method.clone();
7982
method.sig.ident = Ident::new("_fetch_raw", method.sig.ident.span());
8083
method.vis = Visibility::Inherited;
@@ -219,6 +222,12 @@ pub fn expand_macro(tokens: TokenStream) -> syn::Result<TokenStream> {
219222
tokenized.push(tokens?);
220223
}
221224

225+
let fetch = optional_methods.has_fetch.then(|| quote!{
226+
async fn fetch(&mut self, req: ::worker::Request) -> ::worker::Result<worker::Response> {
227+
self._fetch_raw(req).await
228+
}
229+
});
230+
222231
let alarm_tokens = optional_methods.has_alarm.then(|| quote! {
223232
async fn alarm(&mut self) -> ::worker::Result<worker::Response> {
224233
self._alarm_raw().await
@@ -255,9 +264,7 @@ pub fn expand_macro(tokens: TokenStream) -> syn::Result<TokenStream> {
255264
Self::_new(state._inner(), env)
256265
}
257266

258-
async fn fetch(&mut self, req: ::worker::Request) -> ::worker::Result<worker::Response> {
259-
self._fetch_raw(req).await
260-
}
267+
#fetch
261268

262269
#alarm_tokens
263270

worker/src/durable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,9 @@ impl DurableObject for Chatroom {
814814
pub trait DurableObject {
815815
fn new(state: State, env: Env) -> Self;
816816

817-
async fn fetch(&mut self, req: Request) -> Result<Response>;
817+
async fn fetch(&mut self, _req: Request) -> Result<Response> {
818+
unimplemented!("fetch() handler not implemented")
819+
}
818820

819821
#[allow(clippy::diverging_sub_expression)]
820822
async fn alarm(&mut self) -> Result<Response> {

0 commit comments

Comments
 (0)