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

Fix exporting a class instance as an entrypoint. #3715

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kentonv
Copy link
Member

@kentonv kentonv commented Mar 13, 2025

This should work:

class Foo {
  async fetch(req, env, ctx) { ... }
}

export default new Foo();

But it had a slight bug: validateHandlers() did not report the fetch method as existing. This meant you couldn't actually deploy a worker defined this way as the upload validator on the server side would reject it for not implementing fetch.

I asked Claude Code to fix it (as a test... I knew what the problem was). Transcript: https://claude-workerd-transcript.pages.dev/validate-handlers

This should work:

```
class Foo {
  async fetch(req, env, ctx) { ... }
}

export default new Foo();
```

But it had a slight bug: `validateHandlers()` did not report the `fetch` method as existing. This meant you couldn't actually deploy a worker defined this way as the upload validator on the server side would reject it for not implementing `fetch`.

I asked Claude Code to fix it (as a test... I knew what the problem was). Transcript: https://claude-workerd-transcript.pages.dev/validate-handlers
@kentonv kentonv requested review from a team as code owners March 13, 2025 02:45

// Then, check for methods in the prototype chain (like a class instance)
js.withinHandleScope([&]() {
collectMethodsFromPrototypeChain(jsg::JsObject(handle).getPrototype(js), methodSet);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is the meaningful line change in this PR, yeah? Previously you weren't walking the prototype chain for default exports, is that right? I guess I'm not super clear why default is special, I suppose historical reasons?

I presume worker.impl->statelessClasses includes both WorkerEntrypoint and ExportedHandler style exports, right? Or is ExportedHandler handled elsewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants