Skip to content

Commit 3c2aa4b

Browse files
maclover7addaleax
authored andcommitted
tls: de-duplicate for TLSSocket methods
Similar approach is used for `TLSWrap`, where C++ handle methods are mapped one-to-one in JS. PR-URL: #22142 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 29cf335 commit 3c2aa4b

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

lib/_tls_wrap.js

+15-45
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,6 @@ TLSSocket.prototype.setMaxSendFragment = function setMaxSendFragment(size) {
591591
return this._handle.setMaxSendFragment(size) === 1;
592592
};
593593

594-
TLSSocket.prototype.getTLSTicket = function getTLSTicket() {
595-
return this._handle.getTLSTicket();
596-
};
597-
598594
TLSSocket.prototype._handleTimeout = function() {
599595
this._emitTLSError(new ERR_TLS_HANDSHAKE_TIMEOUT());
600596
};
@@ -671,51 +667,25 @@ TLSSocket.prototype.getPeerCertificate = function(detailed) {
671667
return null;
672668
};
673669

674-
TLSSocket.prototype.getFinished = function() {
675-
if (this._handle)
676-
return this._handle.getFinished();
677-
};
678-
679-
TLSSocket.prototype.getPeerFinished = function() {
680-
if (this._handle)
681-
return this._handle.getPeerFinished();
682-
};
683-
684-
TLSSocket.prototype.getSession = function() {
685-
if (this._handle) {
686-
return this._handle.getSession();
687-
}
688-
689-
return null;
690-
};
691-
692-
TLSSocket.prototype.isSessionReused = function() {
693-
if (this._handle) {
694-
return this._handle.isSessionReused();
695-
}
696-
697-
return null;
698-
};
699-
700-
TLSSocket.prototype.getCipher = function(err) {
701-
if (this._handle) {
702-
return this._handle.getCurrentCipher();
703-
} else {
670+
// Proxy TLSSocket handle methods
671+
function makeSocketMethodProxy(name) {
672+
return function socketMethodProxy(...args) {
673+
if (this._handle)
674+
return this._handle[name].apply(this._handle, args);
704675
return null;
705-
}
706-
};
707-
708-
TLSSocket.prototype.getEphemeralKeyInfo = function() {
709-
if (this._handle)
710-
return this._handle.getEphemeralKeyInfo();
676+
};
677+
}
711678

712-
return null;
713-
};
679+
[
680+
'getFinished', 'getPeerFinished', 'getSession', 'isSessionReused',
681+
'getEphemeralKeyInfo', 'getProtocol', 'getTLSTicket'
682+
].forEach((method) => {
683+
TLSSocket.prototype[method] = makeSocketMethodProxy(method);
684+
});
714685

715-
TLSSocket.prototype.getProtocol = function() {
686+
TLSSocket.prototype.getCipher = function(err) {
716687
if (this._handle)
717-
return this._handle.getProtocol();
718-
688+
return this._handle.getCurrentCipher();
719689
return null;
720690
};
721691

0 commit comments

Comments
 (0)