Skip to content

Commit d3440e7

Browse files
committed
Update ALPNCallback param names & other tweaks after review
1 parent 58af789 commit d3440e7

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

doc/api/tls.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,13 +2045,13 @@ changes:
20452045
e.g. `0x05hello0x05world`, where the first byte is the length of the next
20462046
protocol name. Passing an array is usually much simpler, e.g.
20472047
`['hello', 'world']`. (Protocols should be ordered by their priority.)
2048-
* `ALPNCallback(params)`: {Function} If set, this will be called when a
2048+
* `ALPNCallback`: {Function} If set, this will be called when a
20492049
client opens a connection using the ALPN extension. One argument will
2050-
be passed to the callback: an object containing `serverName` and
2051-
`clientALPNProtocols` fields, respectively containing the server name from
2050+
be passed to the callback: an object containing `servername` and
2051+
`protocols` fields, respectively containing the server name from
20522052
the SNI extension (if any) and an array of ALPN protocol name strings. The
20532053
callback must return either one of the strings listed in
2054-
`clientALPNProtocols`, which will be returned to the client as the selected
2054+
`protocols`, which will be returned to the client as the selected
20552055
ALPN protocol, or `undefined`, to reject the connection with a fatal alert.
20562056
If a string is returned that does not match one of the client's ALPN
20572057
protocols, an error will be thrown. This option cannot be used with the

lib/_tls_wrap.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const kErrorEmitted = Symbol('error-emitted');
109109
const kHandshakeTimeout = Symbol('handshake-timeout');
110110
const kRes = Symbol('res');
111111
const kSNICallback = Symbol('snicallback');
112+
const kALPNCallback = Symbol('alpncallback');
112113
const kEnableTrace = Symbol('enableTrace');
113114
const kPskCallback = Symbol('pskcallback');
114115
const kPskIdentityHint = Symbol('pskidentityhint');
@@ -239,7 +240,7 @@ function callALPNCallback(protocolsBuffer) {
239240
const handle = this;
240241
const socket = handle[owner_symbol];
241242

242-
const serverName = handle.getServername();
243+
const servername = handle.getServername();
243244

244245
// Collect all the protocols from the given buffer:
245246
const protocols = [];
@@ -254,9 +255,9 @@ function callALPNCallback(protocolsBuffer) {
254255
protocols.push(protocol.toString('ascii'));
255256
}
256257

257-
const selectedProtocol = socket._ALPNCallback({
258-
serverName,
259-
clientALPNProtocols: protocols
258+
const selectedProtocol = socket[kALPNCallback]({
259+
servername,
260+
protocols
260261
});
261262

262263
// Undefined -> all proposed protocols rejected
@@ -534,7 +535,7 @@ function TLSSocket(socket, opts) {
534535
this._controlReleased = false;
535536
this.secureConnecting = true;
536537
this._SNICallback = null;
537-
this._ALPNCallback = null;
538+
this[kALPNCallback] = null;
538539
this.servername = null;
539540
this.alpnProtocol = null;
540541
this.authorized = false;
@@ -761,8 +762,11 @@ TLSSocket.prototype._init = function(socket, wrap) {
761762
ssl.handshakes = 0;
762763

763764
if (options.ALPNCallback) {
765+
if (typeof options.ALPNCallback !== 'function') {
766+
throw new ERR_INVALID_ARG_TYPE('options.ALPNCallback', 'Function', options.ALPNCallback);
767+
}
764768
assert(typeof options.ALPNCallback === 'function');
765-
this._ALPNCallback = options.ALPNCallback;
769+
this[kALPNCallback] = options.ALPNCallback;
766770
ssl.ALPNCallback = callALPNCallback;
767771
ssl.enableALPNCb();
768772
}

test/parallel/test-tls-alpn-server-client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ function TestFatalAlert() {
221221
function TestALPNCallback() {
222222
// Server always selects the client's 2nd preference:
223223
const serverOptions = {
224-
ALPNCallback: ({ clientALPNProtocols }) => {
225-
return clientALPNProtocols[1];
226-
}
224+
ALPNCallback: common.mustCall(({ protocols }) => {
225+
return protocols[1];
226+
}, 2)
227227
};
228228

229229
const clientsOptions = [{
@@ -249,7 +249,7 @@ function TestALPNCallback() {
249249
function TestBadALPNCallback() {
250250
// Server always returns a fixed invalid value:
251251
const serverOptions = {
252-
ALPNCallback: () => 'http/5'
252+
ALPNCallback: common.mustCall(() => 'http/5')
253253
};
254254

255255
const clientsOptions = [{

0 commit comments

Comments
 (0)