Skip to content

Commit

Permalink
Update ALPNCallback param names & other tweaks after review
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed May 5, 2023
1 parent 58af789 commit d3440e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -2045,13 +2045,13 @@ changes:
e.g. `0x05hello0x05world`, where the first byte is the length of the next
protocol name. Passing an array is usually much simpler, e.g.
`['hello', 'world']`. (Protocols should be ordered by their priority.)
* `ALPNCallback(params)`: {Function} If set, this will be called when a
* `ALPNCallback`: {Function} If set, this will be called when a
client opens a connection using the ALPN extension. One argument will
be passed to the callback: an object containing `serverName` and
`clientALPNProtocols` fields, respectively containing the server name from
be passed to the callback: an object containing `servername` and
`protocols` fields, respectively containing the server name from
the SNI extension (if any) and an array of ALPN protocol name strings. The
callback must return either one of the strings listed in
`clientALPNProtocols`, which will be returned to the client as the selected
`protocols`, which will be returned to the client as the selected
ALPN protocol, or `undefined`, to reject the connection with a fatal alert.
If a string is returned that does not match one of the client's ALPN
protocols, an error will be thrown. This option cannot be used with the
Expand Down
16 changes: 10 additions & 6 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const kErrorEmitted = Symbol('error-emitted');
const kHandshakeTimeout = Symbol('handshake-timeout');
const kRes = Symbol('res');
const kSNICallback = Symbol('snicallback');
const kALPNCallback = Symbol('alpncallback');
const kEnableTrace = Symbol('enableTrace');
const kPskCallback = Symbol('pskcallback');
const kPskIdentityHint = Symbol('pskidentityhint');
Expand Down Expand Up @@ -239,7 +240,7 @@ function callALPNCallback(protocolsBuffer) {
const handle = this;
const socket = handle[owner_symbol];

const serverName = handle.getServername();
const servername = handle.getServername();

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

const selectedProtocol = socket._ALPNCallback({
serverName,
clientALPNProtocols: protocols
const selectedProtocol = socket[kALPNCallback]({
servername,
protocols
});

Check failure on line 261 in lib/_tls_wrap.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma

// Undefined -> all proposed protocols rejected
Expand Down Expand Up @@ -534,7 +535,7 @@ function TLSSocket(socket, opts) {
this._controlReleased = false;
this.secureConnecting = true;
this._SNICallback = null;
this._ALPNCallback = null;
this[kALPNCallback] = null;
this.servername = null;
this.alpnProtocol = null;
this.authorized = false;
Expand Down Expand Up @@ -761,8 +762,11 @@ TLSSocket.prototype._init = function(socket, wrap) {
ssl.handshakes = 0;

if (options.ALPNCallback) {
if (typeof options.ALPNCallback !== 'function') {
throw new ERR_INVALID_ARG_TYPE('options.ALPNCallback', 'Function', options.ALPNCallback);
}
assert(typeof options.ALPNCallback === 'function');
this._ALPNCallback = options.ALPNCallback;
this[kALPNCallback] = options.ALPNCallback;
ssl.ALPNCallback = callALPNCallback;
ssl.enableALPNCb();
}
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-tls-alpn-server-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ function TestFatalAlert() {
function TestALPNCallback() {
// Server always selects the client's 2nd preference:
const serverOptions = {
ALPNCallback: ({ clientALPNProtocols }) => {
return clientALPNProtocols[1];
}
ALPNCallback: common.mustCall(({ protocols }) => {
return protocols[1];
}, 2)
};

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

const clientsOptions = [{
Expand Down

0 comments on commit d3440e7

Please sign in to comment.