Skip to content

Commit 385bfb2

Browse files
committed
quic: implement session.path as documented
1 parent 4b063b2 commit 385bfb2

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/internal/quic/quic.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,8 @@ class QuicSession {
11141114
#ondatagram = undefined;
11151115
/** @type {OnDatagramStatusCallback|undefined} */
11161116
#ondatagramstatus = undefined;
1117+
/** @type {{ local: SocketAddress, remote: SocketAddress }|undefined} */
1118+
#path = undefined;
11171119
/** @type {object|undefined} */
11181120
#sessionticket = undefined;
11191121
/** @type {object|undefined} */
@@ -1266,6 +1268,20 @@ class QuicSession {
12661268
return this.#endpoint;
12671269
}
12681270

1271+
/**
1272+
* The local and remote socket addresses associated with the session.
1273+
* @type {{ local: SocketAddress, remote: SocketAddress } | undefined}
1274+
*/
1275+
get path() {
1276+
QuicSession.#assertIsQuicSession(this);
1277+
if (this.destroyed) return undefined;
1278+
return this.#path ??= {
1279+
__proto__: null,
1280+
local: new InternalSocketAddress(this.#handle.getLocalAddress()),
1281+
remote: new InternalSocketAddress(this.#handle.getRemoteAddress()),
1282+
};
1283+
}
1284+
12691285
/**
12701286
* @param {number} direction
12711287
* @param {OpenStreamOptions} options
@@ -1570,6 +1586,7 @@ class QuicSession {
15701586
this.#onstream = undefined;
15711587
this.#ondatagram = undefined;
15721588
this.#ondatagramstatus = undefined;
1589+
this.#path = undefined;
15731590
this.#sessionticket = undefined;
15741591
this.#token = undefined;
15751592

src/quic/session.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ namespace quic {
115115
#define SESSION_JS_METHODS(V) \
116116
V(Destroy, destroy, SIDE_EFFECT) \
117117
V(GetRemoteAddress, getRemoteAddress, NO_SIDE_EFFECT) \
118+
V(GetLocalAddress, getLocalAddress, NO_SIDE_EFFECT) \
118119
V(GetCertificate, getCertificate, NO_SIDE_EFFECT) \
119120
V(GetEphemeralKeyInfo, getEphemeralKey, NO_SIDE_EFFECT) \
120121
V(GetPeerCertificate, getPeerCertificate, NO_SIDE_EFFECT) \
@@ -221,7 +222,7 @@ void ngtcp2_debug_log(void* user_data, const char* fmt, ...) {
221222
va_end(ap);
222223
}
223224

224-
template <typename Opt, PreferredAddress::Policy Opt::*member>
225+
template <typename Opt, PreferredAddress::Policy Opt::* member>
225226
bool SetOption(Environment* env,
226227
Opt* options,
227228
const Local<Object>& object,
@@ -236,7 +237,7 @@ bool SetOption(Environment* env,
236237
return true;
237238
}
238239

239-
template <typename Opt, TLSContext::Options Opt::*member>
240+
template <typename Opt, TLSContext::Options Opt::* member>
240241
bool SetOption(Environment* env,
241242
Opt* options,
242243
const Local<Object>& object,
@@ -251,7 +252,7 @@ bool SetOption(Environment* env,
251252
return true;
252253
}
253254

254-
template <typename Opt, TransportParams::Options Opt::*member>
255+
template <typename Opt, TransportParams::Options Opt::* member>
255256
bool SetOption(Environment* env,
256257
Opt* options,
257258
const Local<Object>& object,
@@ -266,7 +267,7 @@ bool SetOption(Environment* env,
266267
return true;
267268
}
268269

269-
template <typename Opt, ngtcp2_cc_algo Opt::*member>
270+
template <typename Opt, ngtcp2_cc_algo Opt::* member>
270271
bool SetOption(Environment* env,
271272
Opt* options,
272273
const Local<Object>& object,
@@ -712,6 +713,21 @@ struct Session::Impl final : public MemoryRetainer {
712713
->object());
713714
}
714715

716+
JS_METHOD(GetLocalAddress) {
717+
auto env = Environment::GetCurrent(args);
718+
Session* session;
719+
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
720+
721+
if (session->is_destroyed()) {
722+
return THROW_ERR_INVALID_STATE(env, "Session is destroyed");
723+
}
724+
725+
auto address = session->local_address();
726+
args.GetReturnValue().Set(
727+
SocketAddressBase::Create(env, std::make_shared<SocketAddress>(address))
728+
->object());
729+
}
730+
715731
JS_METHOD(GetCertificate) {
716732
auto env = Environment::GetCurrent(args);
717733
Session* session;

0 commit comments

Comments
 (0)