Skip to content

Constructing a WorkerEntrypoint from userland#3782

Merged
danlapid merged 1 commit intomainfrom
penalosa/construct-entrypoint
Mar 24, 2025
Merged

Constructing a WorkerEntrypoint from userland#3782
danlapid merged 1 commit intomainfrom
penalosa/construct-entrypoint

Conversation

@penalosa
Copy link
Copy Markdown
Contributor

@penalosa penalosa commented Mar 24, 2025

This PR adds a sample that demonstrates that it's not possible to construct a WorkerEntrypoint in userland with a user-provided ctx object. Instead, this error appears:

workerd/server/server.c++:3880: error: Uncaught exception: workerd/jsg/_virtual_includes/iterator/workerd/jsg/value.h:1473: failed: remote.jsg.TypeError: Failed to construct 'WorkerEntrypoint': constructor parameter 1 is not of type 'ExecutionContext'.

This is important for the Vitest integration because we need to be able to (within a single "real" execution context):

  • Construct a WorkerEntrypoint with a custom execution context
  • wait for all waitUntils in the custom execution context to be settled
  • Do some assertions

This works with default export-style workers, because the fetch() method can just be called directly with the custom execution context, but that's not possible with WorkerEntrypoint-style workers.


Or at least that was the case! Thanks to the changes now in this PR, the above is possible—and WorkerEntrypoint can be constructed with any object, allowing for custom execution contexts.

@penalosa penalosa requested review from a team as code owners March 24, 2025 13:57
@kentonv
Copy link
Copy Markdown
Member

kentonv commented Mar 24, 2025

I guess this is just because the constructor is declared to take ExecutionContext:

class WorkerEntrypoint: public jsg::Object {
public:
static jsg::Ref<WorkerEntrypoint> constructor(const v8::FunctionCallbackInfo<v8::Value>& args,
jsg::Ref<ExecutionContext> ctx,
jsg::JsObject env);
JSG_RESOURCE_TYPE(WorkerEntrypoint) {}
};

I think literally just changing the type of ctx from jsg::Ref<ExecutionContext> to jsg::JsObject would solve the problem, no other code changes.

@danlapid danlapid requested a review from kentonv March 24, 2025 15:02
@kentonv
Copy link
Copy Markdown
Member

kentonv commented Mar 24, 2025

Can we convert the stample into a test, please?

Our samples are meant to be documentation. We should only have samples covering common use cases, to avoid overwhelming users with noise.

@kentonv
Copy link
Copy Markdown
Member

kentonv commented Mar 24, 2025

Could add this to js-rpc-test pretty easily.

@danlapid danlapid force-pushed the penalosa/construct-entrypoint branch from 355f22c to 124d73a Compare March 24, 2025 19:04
@danlapid danlapid force-pushed the penalosa/construct-entrypoint branch from 124d73a to 04f97bd Compare March 24, 2025 19:04
@danlapid
Copy link
Copy Markdown
Collaborator

@kentonv added test and cleaned up sample

@danlapid danlapid merged commit ed6d688 into main Mar 24, 2025
42 of 49 checks passed
@danlapid danlapid deleted the penalosa/construct-entrypoint branch March 24, 2025 21:59
penalosa added a commit to cloudflare/workers-sdk that referenced this pull request Mar 27, 2026
…ypoint with mocked env

Closes #7077. The underlying fix landed in workerd via
cloudflare/workerd#3782, which relaxed the WorkerEntrypoint
constructor to accept any object as ctx rather than requiring
a native ExecutionContext.
penalosa added a commit to cloudflare/workers-sdk that referenced this pull request Mar 27, 2026
…ypoint with mocked env

Closes #7077. The underlying fix landed in workerd via
cloudflare/workerd#3782, which relaxed the WorkerEntrypoint
constructor to accept any object as ctx rather than requiring
a native ExecutionContext.
penalosa added a commit to cloudflare/workers-sdk that referenced this pull request Mar 31, 2026
…ypoint with mocked env

Closes #7077. The underlying fix landed in workerd via
cloudflare/workerd#3782, which relaxed the WorkerEntrypoint
constructor to accept any object as ctx rather than requiring
a native ExecutionContext.
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.

3 participants