Skip to content

Commit

Permalink
Implements opened attribute in Socket. Implements #1018.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Nov 6, 2023
1 parent ec83c3a commit 79ab507
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/workerd/api/sockets.c++
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ jsg::Ref<Socket> setupSocket(
if (!allowHalfOpen) {
eofPromise = readable->onEof(js);
}
auto openedPrPair = js.newPromiseAndResolver<void>();
auto openedPrPair = js.newPromiseAndResolver<SocketInfo>();
openedPrPair.promise.markAsHandled(js);
auto writable = jsg::alloc<WritableStream>(
ioContext, kj::mv(sysStreams.writable), kj::none, openedPrPair.promise.whenResolved(js));
Expand Down Expand Up @@ -343,7 +343,8 @@ void Socket::handleProxyStatus(
}
handleProxyError(js, JSG_KJ_EXCEPTION(FAILED, Error, msg));
} else {
openedResolver.resolve(js);
// TODO(later): implement local and remote address in proxy and read it here
openedResolver.resolve(js, SocketInfo{ kj::none, kj::none });
}
});
result.markAsHandled(js);
Expand All @@ -365,7 +366,8 @@ void Socket::handleProxyStatus(jsg::Lock& js, kj::Promise<kj::Maybe<kj::Exceptio
if (result != kj::none) {
handleProxyError(js, JSG_KJ_EXCEPTION(FAILED, Error, "connection attempt failed"));
} else {
openedResolver.resolve(js);
// TODO(later): implement local and remote address in proxy and read it here
openedResolver.resolve(js, SocketInfo{ kj::none, kj::none });
}
});
result.markAsHandled(js);
Expand Down
18 changes: 13 additions & 5 deletions src/workerd/api/sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ struct SocketAddress {
JSG_STRUCT(hostname, port);
};

struct SocketInfo {
jsg::Optional<kj::String> remoteAddress;
jsg::Optional<kj::String> localAddress;
JSG_STRUCT(remoteAddress, localAddress);
};

typedef kj::OneOf<SocketAddress, kj::String> AnySocketAddress;

struct SocketOptions {
Expand All @@ -48,7 +54,7 @@ class Socket: public jsg::Object {
jsg::PromiseResolverPair<void> closedPrPair, kj::Promise<void> watchForDisconnectTask,
jsg::Optional<SocketOptions> options, kj::Own<kj::TlsStarterCallback> tlsStarter,
bool isSecureSocket, kj::String domain, bool isDefaultFetchPort,
jsg::PromiseResolverPair<void> openedPrPair)
jsg::PromiseResolverPair<SocketInfo> openedPrPair)
: connectionStream(context.addObject(kj::mv(connectionStream))),
readable(kj::mv(readableParam)), writable(kj::mv(writable)),
closedResolver(kj::mv(closedPrPair.resolver)),
Expand All @@ -67,7 +73,7 @@ class Socket: public jsg::Object {
jsg::MemoizedIdentity<jsg::Promise<void>>& getClosed() {
return closedPromise;
}
jsg::MemoizedIdentity<jsg::Promise<void>>& getOpened() {
jsg::MemoizedIdentity<jsg::Promise<SocketInfo>>& getOpened() {
return openedPromise;
}

Expand Down Expand Up @@ -99,6 +105,7 @@ class Socket: public jsg::Object {
JSG_READONLY_PROTOTYPE_PROPERTY(readable, getReadable);
JSG_READONLY_PROTOTYPE_PROPERTY(writable, getWritable);
JSG_READONLY_PROTOTYPE_PROPERTY(closed, getClosed);
JSG_READONLY_PROTOTYPE_PROPERTY(opened, getOpened);
JSG_METHOD(close);
JSG_METHOD(startTls);
}
Expand All @@ -125,8 +132,8 @@ class Socket: public jsg::Object {
// Whether the port this socket connected to is 80/443. Used for nicer errors.
bool isDefaultFetchPort;
// This fulfiller is used to resolve the `openedPromise` below.
jsg::Promise<void>::Resolver openedResolver;
jsg::MemoizedIdentity<jsg::Promise<void>> openedPromise;
jsg::Promise<SocketInfo>::Resolver openedResolver;
jsg::MemoizedIdentity<jsg::Promise<SocketInfo>> openedPromise;

kj::Promise<kj::Own<kj::AsyncIoStream>> processConnection();
jsg::Promise<void> maybeCloseWriteSide(jsg::Lock& js);
Expand Down Expand Up @@ -191,7 +198,8 @@ void registerSocketsModule(
api::SocketOptions, \
api::SocketAddress, \
api::TlsOptions, \
api::SocketsModule
api::SocketsModule, \
api::SocketInfo

// The list of sockets.h types that are added to worker.c++'s JSG_DECLARE_ISOLATE_TYPE
} // namespace workerd::api

0 comments on commit 79ab507

Please sign in to comment.