Skip to content

Commit

Permalink
Also add ctx.exports to Durable Objects' ctx.
Browse files Browse the repository at this point in the history
  • Loading branch information
kentonv committed Feb 6, 2025
1 parent 76f6577 commit fc4398e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/workerd/api/actor-state.c++
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,11 @@ kj::OneOf<jsg::Ref<DurableObjectId>, kj::StringPtr> ActorState::getId() {
}

DurableObjectState::DurableObjectState(Worker::Actor::Id actorId,
jsg::JsRef<jsg::JsValue> exports,
kj::Maybe<jsg::Ref<DurableObjectStorage>> storage,
kj::Maybe<rpc::Container::Client> container)
: id(kj::mv(actorId)),
exports(kj::mv(exports)),
storage(kj::mv(storage)),
container(container.map(
[&](rpc::Container::Client& cap) { return jsg::alloc<Container>(kj::mv(cap)); })) {}
Expand Down
11 changes: 11 additions & 0 deletions src/workerd/api/actor-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,16 @@ class WebSocketRequestResponsePair: public jsg::Object {
class DurableObjectState: public jsg::Object {
public:
DurableObjectState(Worker::Actor::Id actorId,
jsg::JsRef<jsg::JsValue> exports,
kj::Maybe<jsg::Ref<DurableObjectStorage>> storage,
kj::Maybe<rpc::Container::Client> container);

void waitUntil(kj::Promise<void> promise);

jsg::JsValue getExports(jsg::Lock& js) {
return exports.getHandle(js);
}

kj::OneOf<jsg::Ref<DurableObjectId>, kj::StringPtr> getId();

jsg::Optional<jsg::Ref<DurableObjectStorage>> getStorage() {
Expand Down Expand Up @@ -541,6 +546,11 @@ class DurableObjectState: public jsg::Object {

JSG_RESOURCE_TYPE(DurableObjectState, CompatibilityFlags::Reader flags) {
JSG_METHOD(waitUntil);
if (flags.getWorkerdExperimental()) {
// TODO(soon): Remove experimental gate as soon as we've wired up the control plane so that
// this works in production.
JSG_LAZY_INSTANCE_PROPERTY(exports, getExports);
}
JSG_LAZY_INSTANCE_PROPERTY(id, getId);
JSG_LAZY_INSTANCE_PROPERTY(storage, getStorage);
JSG_LAZY_INSTANCE_PROPERTY(container, getContainer);
Expand Down Expand Up @@ -581,6 +591,7 @@ class DurableObjectState: public jsg::Object {

private:
Worker::Actor::Id id;
jsg::JsRef<jsg::JsValue> exports;
kj::Maybe<jsg::Ref<DurableObjectStorage>> storage;
kj::Maybe<jsg::Ref<Container>> container;

Expand Down
5 changes: 4 additions & 1 deletion src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3454,7 +3454,10 @@ void Worker::Actor::ensureConstructed(IoContext& context) {
storage = impl->makeStorage(lock, worker->getIsolate().getApi(), *c);
}
auto handler = info.cls(lock,
jsg::alloc<api::DurableObjectState>(cloneId(), kj::mv(storage), kj::mv(impl->container)),
jsg::alloc<api::DurableObjectState>(cloneId(),
jsg::JsRef<jsg::JsValue>(js,
KJ_ASSERT_NONNULL(lock.getWorker().impl->ctxExports).addRef(js)),
kj::mv(storage), kj::mv(impl->container)),
KJ_ASSERT_NONNULL(lock.getWorker().impl->env).addRef(js));

// HACK: We set handler.env to undefined because we already passed the real env into the
Expand Down
5 changes: 4 additions & 1 deletion src/workerd/server/server-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4032,20 +4032,23 @@ KJ_TEST("Server: ctx.exports self-referential bindings") {
` await ctx.exports.AnotherEntrypoint.bar(321),
` await actor.baz(),
` await ctx.exports.default.corge(555),
` await actor.grault(456),
` "UnconfiguredActor" in ctx.exports, // should be false
` ].join(", "));
` },
` corge(i) { return `corge: ${i}` }
`}
`export class MyEntrypoint extends WorkerEntrypoint {
` foo(i) { return `foo: ${i}` }
` grault(i) { return `grault: ${i}` }
`}
`export class AnotherEntrypoint extends WorkerEntrypoint {
` bar(i) { return `bar: ${i}` }
`}
`export class MyActor extends DurableObject {
` setValue(i) { this.value = i; }
` baz() { return `baz: ${this.value}` }
` grault(i) { return this.ctx.exports.MyEntrypoint.grault(i); }
`}
`export class UnconfiguredActor extends DurableObject {
` qux(i) { return `qux: ${i}` }
Expand Down Expand Up @@ -4078,7 +4081,7 @@ KJ_TEST("Server: ctx.exports self-referential bindings") {
test.start();

auto conn = test.connect("test-addr");
conn.httpGet200("/", "foo: 123, bar: 321, baz: 234, corge: 555, false");
conn.httpGet200("/", "foo: 123, bar: 321, baz: 234, corge: 555, grault: 456, false");
}

// =======================================================================================
Expand Down

0 comments on commit fc4398e

Please sign in to comment.