Skip to content

Commit dc33b3e

Browse files
BeniChenijasnell
authored andcommitted
tls: update test & docs for ArrayBuffer/DataView
In tls module, accept ArrayBuffer/DataView in place of isUint8Array in the source code & related test code in "test-tls-basic-validations.js", per the "tls" item in the checklist of the comment in #1826. PR-URL: #23210 Refs: #1826 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
1 parent c457391 commit dc33b3e

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

doc/api/tls.md

+20-15
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,8 @@ changes:
856856
description: The `lookup` option is supported now.
857857
- version: v8.0.0
858858
pr-url: https://github.com/nodejs/node/pull/11984
859-
description: The `ALPNProtocols` option can be a `Uint8Array` now.
859+
description: The `ALPNProtocols` option can be a `TypedArray` or
860+
`DataView` now.
860861
- version: v5.3.0, v4.7.0
861862
pr-url: https://github.com/nodejs/node/pull/4246
862863
description: The `secureContext` option is supported now.
@@ -884,12 +885,14 @@ changes:
884885
verified against the list of supplied CAs. An `'error'` event is emitted if
885886
verification fails; `err.code` contains the OpenSSL error code. **Default:**
886887
`true`.
887-
* `ALPNProtocols`: {string[]|Buffer[]|Uint8Array[]|Buffer|Uint8Array}
888-
An array of strings, `Buffer`s or `Uint8Array`s, or a single `Buffer` or
889-
`Uint8Array` containing the supported ALPN protocols. `Buffer`s should have
890-
the format `[len][name][len][name]...` e.g. `0x05hello0x05world`, where the
891-
first byte is the length of the next protocol name. Passing an array is
892-
usually much simpler, e.g. `['hello', 'world']`.
888+
* `ALPNProtocols`: {string[]|Buffer[]|TypedArray[]|DataView[]|Buffer|
889+
TypedArray|DataView}
890+
An array of strings, `Buffer`s or `TypedArray`s or `DataView`s, or a
891+
single `Buffer` or `TypedArray` or `DataView` containing the supported ALPN
892+
protocols. `Buffer`s should have the format `[len][name][len][name]...`
893+
e.g. `0x05hello0x05world`, where the first byte is the length of the next
894+
protocol name. Passing an array is usually much simpler, e.g.
895+
`['hello', 'world']`.
893896
* `servername`: {string} Server name for the SNI (Server Name Indication) TLS
894897
extension.
895898
* `checkServerIdentity(servername, cert)` {Function} A callback function
@@ -1134,20 +1137,22 @@ changes:
11341137
description: The `options` parameter can now include `clientCertEngine`.
11351138
- version: v8.0.0
11361139
pr-url: https://github.com/nodejs/node/pull/11984
1137-
description: The `ALPNProtocols` option can be a `Uint8Array` now.
1140+
description: The `ALPNProtocols` option can be a `TypedArray` or
1141+
`DataView` now.
11381142
- version: v5.0.0
11391143
pr-url: https://github.com/nodejs/node/pull/2564
11401144
description: ALPN options are supported now.
11411145
-->
11421146

11431147
* `options` {Object}
1144-
* `ALPNProtocols`: {string[]|Buffer[]|Uint8Array[]|Buffer|Uint8Array}
1145-
An array of strings, `Buffer`s or `Uint8Array`s, or a single `Buffer` or
1146-
`Uint8Array` containing the supported ALPN protocols. `Buffer`s should have
1147-
the format `[len][name][len][name]...` e.g. `0x05hello0x05world`, where the
1148-
first byte is the length of the next protocol name. Passing an array is
1149-
usually much simpler, e.g. `['hello', 'world']`.
1150-
(Protocols should be ordered by their priority.)
1148+
* `ALPNProtocols`: {string[]|Buffer[]|TypedArray[]|DataView[]|Buffer|
1149+
TypedArray|DataView}
1150+
An array of strings, `Buffer`s or `TypedArray`s or `DataView`s, or a single
1151+
`Buffer` or `TypedArray` or `DataView` containing the supported ALPN
1152+
protocols. `Buffer`s should have the format `[len][name][len][name]...`
1153+
e.g. `0x05hello0x05world`, where the first byte is the length of the next
1154+
protocol name. Passing an array is usually much simpler, e.g.
1155+
`['hello', 'world']`. (Protocols should be ordered by their priority.)
11511156
* `clientCertEngine` {string} Name of an OpenSSL engine which can provide the
11521157
client certificate.
11531158
* `handshakeTimeout` {number} Abort the connection if the SSL/TLS handshake

lib/tls.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const { ERR_TLS_CERT_ALTNAME_INVALID } = require('internal/errors').codes;
2525
const internalUtil = require('internal/util');
2626
const internalTLS = require('internal/tls');
2727
internalUtil.assertCrypto();
28-
const { isUint8Array } = require('internal/util/types');
28+
const { isArrayBufferView } = require('internal/util/types');
2929

3030
const net = require('net');
3131
const url = require('url');
@@ -78,7 +78,7 @@ exports.convertALPNProtocols = function convertALPNProtocols(protocols, out) {
7878
// If protocols is Array - translate it into buffer
7979
if (Array.isArray(protocols)) {
8080
out.ALPNProtocols = convertProtocols(protocols);
81-
} else if (isUint8Array(protocols)) {
81+
} else if (isArrayBufferView(protocols)) {
8282
// Copy new buffer not to be modified by user.
8383
out.ALPNProtocols = Buffer.from(protocols);
8484
}

test/parallel/test-tls-basic-validations.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ common.expectsError(
9494
}
9595

9696
{
97-
const buffer = new Uint8Array(Buffer.from('abcd'));
98-
const out = {};
99-
tls.convertALPNProtocols(buffer, out);
100-
assert(out.ALPNProtocols.equals(Buffer.from('abcd')));
97+
const arrayBufferViewStr = 'abcd';
98+
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
99+
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
100+
const out = {};
101+
tls.convertALPNProtocols(expectView, out);
102+
assert(out.ALPNProtocols.equals(Buffer.from(expectView)));
103+
}
101104
}

0 commit comments

Comments
 (0)