Skip to content

Commit

Permalink
fix: Pass ctx.props to default handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
tewaro committed Feb 6, 2025
1 parent a0b8d14 commit 7c05d79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/workerd/api/global-scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,35 @@ struct ExportedHandler {
jsg::Optional<jsg::Ref<ExecutionContext>> getCtx() {
return ctx.map([&](jsg::Ref<ExecutionContext>& p) { return p.addRef(); });
}

template <typename T>
static jsg::LenientOptional<T> ADDREF(jsg::LenientOptional<T>& function, jsg::Lock& js) {
return function.map([&](T& a) { return a.addRef(js); });
}

#define CLONE(x) \
.x { \
ADDREF(x, js) \
}

ExportedHandler clone(jsg::Lock& js) {
return ExportedHandler{
CLONE(fetch),
CLONE(tail),
CLONE(trace),
CLONE(tailStream),
CLONE(scheduled),
CLONE(alarm),
CLONE(test),
CLONE(webSocketMessage),
CLONE(webSocketClose),
CLONE(webSocketError),
.self{js.v8Isolate, v8::Object::New(js.v8Isolate)},
.env{env.addRef(js)},
.ctx{getCtx()},
.missingSuperclass = missingSuperclass,
};
}
};

// An approximation of Node.js setImmediate `Immediate` object.
Expand Down
6 changes: 6 additions & 0 deletions src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,10 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
$experimental
$neededByFl;
# Enables cache settings specified request in fetch api cf object to override cache rules. (only for user owned or grey-clouded sites)

uniqueCtxPerInvocation @73: Bool
$compatEnableFlag("unique_ctx_per_invocation")
$compatDisableFlag("nonclass_entrypoint_reuses_ctx_accros_invocations")
$compatEnableDate("2025-02-24");
# Creates a unique ExportedHandler for each call to `export default` allows a unique ctx per invocation
}
10 changes: 9 additions & 1 deletion src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,15 @@ kj::Maybe<kj::Own<api::ExportedHandler>> Worker::Lock::getExportedHandler(

kj::StringPtr n = name.orDefault("default"_kj);
KJ_IF_SOME(h, worker.impl->namedHandlers.find(n)) {
return fakeOwn(h);
kj::Own<api::ExportedHandler> ret = fakeOwn(h);
jsg::Lock& js = *this;
if (FeatureFlags::get(js).getUniqueCtxPerInvocation()) {
api::ExportedHandler constructedHandler = h.clone(js);
constructedHandler.ctx = jsg::alloc<api::ExecutionContext>(js, props.toJs(js));
kj::Own<api::ExportedHandler> handlerPtr = kj::heap(kj::mv(constructedHandler));
return handlerPtr;
}
return ret;
} else KJ_IF_SOME(cls, worker.impl->statelessClasses.find(n)) {
jsg::Lock& js = *this;
auto handler = kj::heap(cls(js, jsg::alloc<api::ExecutionContext>(js, props.toJs(js)),
Expand Down

0 comments on commit 7c05d79

Please sign in to comment.