@@ -84,11 +84,11 @@ class StreamWorkerInterface;
8484
8585jsg::Ref<Socket> setupSocket (jsg::Lock& js,
8686 kj::Own<kj::AsyncIoStream> connection,
87- kj::String remoteAddress,
87+ kj::Maybe<kj:: String> remoteAddress,
8888 jsg::Optional<SocketOptions> options,
8989 kj::Own<kj::TlsStarterCallback> tlsStarter,
9090 SecureTransportKind secureTransport,
91- kj::String domain,
91+ kj::Maybe<kj:: String> domain,
9292 bool isDefaultFetchPort,
9393 kj::Maybe<jsg::PromiseResolverPair<SocketInfo>> maybeOpenedPrPair) {
9494 auto & ioContext = IoContext::current ();
@@ -323,10 +323,10 @@ jsg::Ref<Socket> Socket::startTls(jsg::Lock& js, jsg::Optional<TlsOptions> tlsOp
323323 secureTransport != SecureTransportKind::ON , TypeError, " Cannot startTls on a TLS socket." );
324324 JSG_REQUIRE (connectionData != kj::none, TypeError,
325325 " The connection was closed before startTls could be started." );
326- JSG_REQUIRE (domain != nullptr , TypeError, " startTls can only be called once." );
327326 auto invalidOptKindMsg =
328327 " The `secureTransport` socket option must be set to 'starttls' for startTls to be used." ;
329328 JSG_REQUIRE (secureTransport == SecureTransportKind::STARTTLS , TypeError, invalidOptKindMsg);
329+ JSG_REQUIRE (domain != kj::none, TypeError, " startTls can only be called once." );
330330
331331 // The current socket's writable buffers need to be flushed. The socket's WritableStream is backed
332332 // by an AsyncIoStream which doesn't implement any buffering, so we don't need to worry about
@@ -346,10 +346,10 @@ jsg::Ref<Socket> Socket::startTls(jsg::Lock& js, jsg::Optional<TlsOptions> tlsOp
346346 // flush to complete. While it is unlikely to be GC'd while we are waiting because
347347 // the user code *likely* is holding a active reference to it at this point, we
348348 // don't want to take any chances. This prevents a possible UAF.
349- JSG_VISITABLE_LAMBDA (
350- (self = JSG_THIS , domain = kj::heapString (domain), tlsOptions = kj::mv (tlsOptions),
351- openedResolver = openedPrPair.resolver .addRef (js),
352- remoteAddress = kj::str (remoteAddress)),
349+ JSG_VISITABLE_LAMBDA ((self = JSG_THIS , domain = kj::heapString ( KJ_ASSERT_NONNULL (domain)),
350+ tlsOptions = kj::mv (tlsOptions),
351+ openedResolver = openedPrPair.resolver .addRef (js),
352+ remoteAddress = mapCopyString (remoteAddress)),
353353 (self, openedResolver), (jsg::Lock & js) mutable {
354354 auto & context = IoContext::current ();
355355
@@ -381,7 +381,7 @@ jsg::Ref<Socket> Socket::startTls(jsg::Lock& js, jsg::Optional<TlsOptions> tlsOp
381381
382382 // Fork the starter promise because we need to create two separate things waiting
383383 // on it below. The first is resolving the openedResolver with a JS promise that
384- // wraps one branch, the secnod is the kj::Promise that we use to resolve the
384+ // wraps one branch, the second is the kj::Promise that we use to resolve the
385385 // secureStream for the promised stream. This keeps us from having to bounce in and
386386 // out of the JS isolate lock.
387387 auto forkedPromise = KJ_ASSERT_NONNULL (*tlsStarter)(acceptedHostname).fork ();
@@ -410,9 +410,9 @@ jsg::Ref<Socket> Socket::startTls(jsg::Lock& js, jsg::Optional<TlsOptions> tlsOp
410410 // The existing tlsStarter gets consumed and we won't need it again. Pass in an empty tlsStarter
411411 // to `setupSocket`.
412412 auto newTlsStarter = kj::heap<kj::TlsStarterCallback>();
413- return setupSocket (js, kj::newPromisedStream (kj::mv (secureStreamPromise)), kj::str (remoteAddress),
414- kj::mv (options), kj::mv (newTlsStarter), SecureTransportKind::ON , kj::mv (domain) ,
415- isDefaultFetchPort, kj::mv (openedPrPair));
413+ return setupSocket (js, kj::newPromisedStream (kj::mv (secureStreamPromise)),
414+ mapCopyString (remoteAddress), kj::mv (options), kj::mv (newTlsStarter), SecureTransportKind::ON ,
415+ kj::mv (domain), isDefaultFetchPort, kj::mv (openedPrPair));
416416}
417417
418418void Socket::handleProxyStatus (
@@ -436,7 +436,7 @@ void Socket::handleProxyStatus(
436436 if (isDefaultFetchPort) {
437437 msg = kj::str (msg, " . It looks like you might be trying to connect to a HTTP-based service" ,
438438 " — consider using fetch instead" );
439- } else if (remoteAddress.contains (" .hyperdrive.local" _kj)) {
439+ } else if (remoteAddress.orDefault ( kj::String ()). contains (" .hyperdrive.local" _kj)) {
440440 // No attempts to connect to Hyperdrive should end up here, since they go through the other
441441 // version of handleProxyStatus. If they end up here somehow, log about it to get some
442442 // context that can aid in debugging.
@@ -450,7 +450,7 @@ void Socket::handleProxyStatus(
450450 // because there's no useful value we can provide.
451451 openedResolver.resolve (js,
452452 SocketInfo{
453- .remoteAddress = kj::str (remoteAddress),
453+ .remoteAddress = mapCopyString (remoteAddress),
454454 .localAddress = kj::none,
455455 });
456456 }
@@ -478,7 +478,7 @@ void Socket::handleProxyStatus(jsg::Lock& js, kj::Promise<kj::Maybe<kj::Exceptio
478478 // because there's no useful value we can provide.
479479 openedResolver.resolve (js,
480480 SocketInfo{
481- .remoteAddress = kj::str (remoteAddress),
481+ .remoteAddress = mapCopyString (remoteAddress),
482482 .localAddress = kj::none,
483483 });
484484 }
0 commit comments