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

[WIP] Error if a user tries to use durable objects with a service worker #634

Closed
wants to merge 5 commits into from

Conversation

caass
Copy link
Contributor

@caass caass commented Mar 18, 2022

Durable Objects require Module Workers to function, so we should error if we discover that a user is trying to use a durable object with a Service Worker.

Closes #635

Durable Objects [require Module Workers](https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/#advantages-of-migrating) to function, so we should error if we discover that a user is trying to use a durable object with a Service Worker.
@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2022

🦋 Changeset detected

Latest commit: e0935f2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Mar 18, 2022

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/2001973798/npm-package-wrangler-634

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/634/npm-package-wrangler-634

Or you can use npx with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/2001973798/npm-package-wrangler-634 dev path/to/script.js

@Electroid
Copy link
Contributor

There is a little more nuance: modules are required to implement a Durable Object, but not to bind to one. Therefore, it’s only an error when the format is service worker and the Durable Object binding does not have a “script_name”.

@caass caass changed the title Error if a user tries to use durable objects with a service worker [WIP] Error if a user tries to use durable objects with a service worker Mar 18, 2022
@caass
Copy link
Contributor Author

caass commented Mar 18, 2022

There is a little more nuance: modules are required to implement a Durable Object, but not to bind to one. Therefore, it’s only an error when the format is service worker and the Durable Object binding does not have a “script_name”.

Ah ok...so actually...the real error is that a worker cannot have both addEventListener and an export?

So like...is this legal?

EDIT: no, i don't think it is because you still need to export DurableObjectExample from index.js

durable_object.js

export class DurableObjectExample {
    constructor(state, env) {
    }

    async fetch(request) {
        return new Response('Hello World');
    }

}

index.js

addEventListener('fetch', event => {
  const example = EXAMPLE_CLASS.get('some-id');
 // ...
})

wrangler.toml

# ...
main = "index.js"
[durable_objects]
bindings = [
  { name = "EXAMPLE_CLASS", class_name = "DurableObjectExample", script_name = "durable_object.js" }
]

@caass
Copy link
Contributor Author

caass commented Mar 18, 2022

Closing this PR and copying over discussion to the issue until I've figured out a better way to check for this

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.

We should error when a user tries to implement a durable object in a worker using the service-worker format
2 participants