Skip to content

Commit dfe99d2

Browse files
ronagtargos
authored andcommitted
tls: move legacy code into own file
PR-URL: #39333 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent adb812c commit dfe99d2

File tree

8 files changed

+142
-129
lines changed

8 files changed

+142
-129
lines changed

Diff for: lib/_tls_common.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ const {
5252

5353
const {
5454
configSecureContext,
55+
} = require('internal/tls/secure-context');
56+
57+
const {
5558
parseCertString,
56-
} = require('internal/tls');
59+
} = require('internal/tls/parse-cert-string');
5760

5861
function toV(which, v, def) {
5962
if (v == null) v = def;

Diff for: lib/internal/streams/duplexpair.js

-51
This file was deleted.

Diff for: lib/internal/tls/parse-cert-string.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const {
4+
ArrayIsArray,
5+
ArrayPrototypeForEach,
6+
ArrayPrototypePush,
7+
StringPrototypeIndexOf,
8+
StringPrototypeSlice,
9+
StringPrototypeSplit,
10+
ObjectCreate,
11+
} = primordials;
12+
13+
// Example:
14+
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\[email protected]
15+
function parseCertString(s) {
16+
const out = ObjectCreate(null);
17+
ArrayPrototypeForEach(StringPrototypeSplit(s, '\n'), (part) => {
18+
const sepIndex = StringPrototypeIndexOf(part, '=');
19+
if (sepIndex > 0) {
20+
const key = StringPrototypeSlice(part, 0, sepIndex);
21+
const value = StringPrototypeSlice(part, sepIndex + 1);
22+
if (key in out) {
23+
if (!ArrayIsArray(out[key])) {
24+
out[key] = [out[key]];
25+
}
26+
ArrayPrototypePush(out[key], value);
27+
} else {
28+
out[key] = value;
29+
}
30+
}
31+
});
32+
return out;
33+
}
34+
35+
exports.parseCertString = parseCertString;

Diff for: lib/internal/tls.js renamed to lib/internal/tls/secure-context.js

-27
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ const {
55
ArrayPrototypeFilter,
66
ArrayPrototypeForEach,
77
ArrayPrototypeJoin,
8-
ArrayPrototypePush,
9-
StringPrototypeIndexOf,
10-
StringPrototypeSlice,
118
StringPrototypeSplit,
129
StringPrototypeStartsWith,
13-
ObjectCreate,
1410
} = primordials;
1511

1612
const {
@@ -42,28 +38,6 @@ const {
4238
},
4339
} = internalBinding('constants');
4440

45-
// Example:
46-
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\[email protected]
47-
function parseCertString(s) {
48-
const out = ObjectCreate(null);
49-
ArrayPrototypeForEach(StringPrototypeSplit(s, '\n'), (part) => {
50-
const sepIndex = StringPrototypeIndexOf(part, '=');
51-
if (sepIndex > 0) {
52-
const key = StringPrototypeSlice(part, 0, sepIndex);
53-
const value = StringPrototypeSlice(part, sepIndex + 1);
54-
if (key in out) {
55-
if (!ArrayIsArray(out[key])) {
56-
out[key] = [out[key]];
57-
}
58-
ArrayPrototypePush(out[key], value);
59-
} else {
60-
out[key] = value;
61-
}
62-
}
63-
});
64-
return out;
65-
}
66-
6741
function getDefaultEcdhCurve() {
6842
// We do it this way because DEFAULT_ECDH_CURVE can be
6943
// changed by users, so we need to grab the current
@@ -340,5 +314,4 @@ function configSecureContext(context, options = {}, name = 'options') {
340314

341315
module.exports = {
342316
configSecureContext,
343-
parseCertString,
344317
};

Diff for: lib/internal/tls/secure-pair.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
const EventEmitter = require('events');
4+
const { Duplex } = require('stream');
5+
const _tls_wrap = require('_tls_wrap');
6+
const _tls_common = require('_tls_common');
7+
8+
const {
9+
Symbol,
10+
ReflectConstruct,
11+
} = primordials;
12+
13+
const kCallback = Symbol('Callback');
14+
const kOtherSide = Symbol('Other');
15+
16+
class DuplexSocket extends Duplex {
17+
constructor() {
18+
super();
19+
this[kCallback] = null;
20+
this[kOtherSide] = null;
21+
}
22+
23+
_read() {
24+
const callback = this[kCallback];
25+
if (callback) {
26+
this[kCallback] = null;
27+
callback();
28+
}
29+
}
30+
31+
_write(chunk, encoding, callback) {
32+
if (chunk.length === 0) {
33+
process.nextTick(callback);
34+
} else {
35+
this[kOtherSide].push(chunk);
36+
this[kOtherSide][kCallback] = callback;
37+
}
38+
}
39+
40+
_final(callback) {
41+
this[kOtherSide].on('end', callback);
42+
this[kOtherSide].push(null);
43+
}
44+
}
45+
46+
class DuplexPair {
47+
constructor() {
48+
this.socket1 = new DuplexSocket();
49+
this.socket2 = new DuplexSocket();
50+
this.socket1[kOtherSide] = this.socket2;
51+
this.socket2[kOtherSide] = this.socket1;
52+
}
53+
}
54+
55+
class SecurePair extends EventEmitter {
56+
constructor(secureContext = _tls_common.createSecureContext(),
57+
isServer = false,
58+
requestCert = !isServer,
59+
rejectUnauthorized = false,
60+
options = {}) {
61+
super();
62+
const { socket1, socket2 } = new DuplexPair();
63+
64+
this.server = options.server;
65+
this.credentials = secureContext;
66+
67+
this.encrypted = socket1;
68+
this.cleartext = new _tls_wrap.TLSSocket(socket2, {
69+
secureContext,
70+
isServer,
71+
requestCert,
72+
rejectUnauthorized,
73+
...options
74+
});
75+
this.cleartext.once('secure', () => this.emit('secure'));
76+
}
77+
78+
destroy() {
79+
this.cleartext.destroy();
80+
this.encrypted.destroy();
81+
}
82+
}
83+
84+
exports.createSecurePair = function createSecurePair(...args) {
85+
return ReflectConstruct(SecurePair, args);
86+
};

Diff for: lib/tls.js

+9-44
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const {
3232
ArrayPrototypeSome,
3333
ObjectDefineProperty,
3434
ObjectFreeze,
35-
ReflectConstruct,
3635
RegExpPrototypeTest,
3736
StringFromCharCode,
3837
StringPrototypeCharCodeAt,
@@ -50,19 +49,18 @@ const {
5049
} = require('internal/errors').codes;
5150
const internalUtil = require('internal/util');
5251
internalUtil.assertCrypto();
53-
const internalTLS = require('internal/tls');
5452
const { isArrayBufferView } = require('internal/util/types');
5553

5654
const net = require('net');
5755
const { getOptionValue } = require('internal/options');
5856
const { getRootCertificates, getSSLCiphers } = internalBinding('crypto');
5957
const { Buffer } = require('buffer');
60-
const EventEmitter = require('events');
6158
const { URL } = require('internal/url');
62-
const DuplexPair = require('internal/streams/duplexpair');
6359
const { canonicalizeIP } = internalBinding('cares_wrap');
6460
const _tls_common = require('_tls_common');
6561
const _tls_wrap = require('_tls_wrap');
62+
const { createSecurePair } = require('internal/tls/secure-pair');
63+
const { parseCertString } = require('internal/tls/parse-cert-string');
6664

6765
// Allow {CLIENT_RENEG_LIMIT} client-initiated session renegotiations
6866
// every {CLIENT_RENEG_WINDOW} seconds. An error event is emitted if more
@@ -300,53 +298,20 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
300298
}
301299
};
302300

303-
304-
class SecurePair extends EventEmitter {
305-
constructor(secureContext = exports.createSecureContext(),
306-
isServer = false,
307-
requestCert = !isServer,
308-
rejectUnauthorized = false,
309-
options = {}) {
310-
super();
311-
const { socket1, socket2 } = new DuplexPair();
312-
313-
this.server = options.server;
314-
this.credentials = secureContext;
315-
316-
this.encrypted = socket1;
317-
this.cleartext = new exports.TLSSocket(socket2, {
318-
secureContext,
319-
isServer,
320-
requestCert,
321-
rejectUnauthorized,
322-
...options
323-
});
324-
this.cleartext.once('secure', () => this.emit('secure'));
325-
}
326-
327-
destroy() {
328-
this.cleartext.destroy();
329-
this.encrypted.destroy();
330-
}
331-
}
332-
333-
334-
exports.parseCertString = internalUtil.deprecate(
335-
internalTLS.parseCertString,
336-
'tls.parseCertString() is deprecated. ' +
337-
'Please use querystring.parse() instead.',
338-
'DEP0076');
339-
340301
exports.createSecureContext = _tls_common.createSecureContext;
341302
exports.SecureContext = _tls_common.SecureContext;
342303
exports.TLSSocket = _tls_wrap.TLSSocket;
343304
exports.Server = _tls_wrap.Server;
344305
exports.createServer = _tls_wrap.createServer;
345306
exports.connect = _tls_wrap.connect;
346307

308+
exports.parseCertString = internalUtil.deprecate(
309+
parseCertString,
310+
'tls.parseCertString() is deprecated. ' +
311+
'Please use querystring.parse() instead.',
312+
'DEP0076');
313+
347314
exports.createSecurePair = internalUtil.deprecate(
348-
function createSecurePair(...args) {
349-
return ReflectConstruct(SecurePair, args);
350-
},
315+
createSecurePair,
351316
'tls.createSecurePair() is deprecated. Please use ' +
352317
'tls.TLSSocket instead.', 'DEP0064');

Diff for: src/node_native_module.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ void NativeModuleLoader::InitializeModuleCategories() {
9999
"tls",
100100
"_tls_common",
101101
"_tls_wrap",
102-
"internal/tls",
102+
"internal/tls/secure-pair",
103+
"internal/tls/parse-cert-string",
104+
"internal/tls/secure-context",
103105
"internal/http2/core",
104106
"internal/http2/compat",
105107
"internal/policy/manifest",

Diff for: test/parallel/test-tls-parse-cert-string.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
} = require('../common/hijackstdio');
1212
const assert = require('assert');
1313
// Flags: --expose-internals
14-
const internalTLS = require('internal/tls');
14+
const { parseCertString } = require('internal/tls/parse-cert-string');
1515
const tls = require('tls');
1616

1717
const noOutput = common.mustNotCall();
@@ -20,7 +20,7 @@ hijackStderr(noOutput);
2020
{
2121
const singles = 'C=US\nST=CA\nL=SF\nO=Node.js Foundation\nOU=Node.js\n' +
2222
23-
const singlesOut = internalTLS.parseCertString(singles);
23+
const singlesOut = parseCertString(singles);
2424
assert.deepStrictEqual(singlesOut, {
2525
__proto__: null,
2626
C: 'US',
@@ -36,7 +36,7 @@ hijackStderr(noOutput);
3636
{
3737
const doubles = 'OU=Domain Control Validated\nOU=PositiveSSL Wildcard\n' +
3838
'CN=*.nodejs.org';
39-
const doublesOut = internalTLS.parseCertString(doubles);
39+
const doublesOut = parseCertString(doubles);
4040
assert.deepStrictEqual(doublesOut, {
4141
__proto__: null,
4242
OU: [ 'Domain Control Validated', 'PositiveSSL Wildcard' ],
@@ -46,7 +46,7 @@ hijackStderr(noOutput);
4646

4747
{
4848
const invalid = 'fhqwhgads';
49-
const invalidOut = internalTLS.parseCertString(invalid);
49+
const invalidOut = parseCertString(invalid);
5050
assert.deepStrictEqual(invalidOut, { __proto__: null });
5151
}
5252

@@ -55,7 +55,7 @@ hijackStderr(noOutput);
5555
const expected = Object.create(null);
5656
expected.__proto__ = 'mostly harmless';
5757
expected.hasOwnProperty = 'not a function';
58-
assert.deepStrictEqual(internalTLS.parseCertString(input), expected);
58+
assert.deepStrictEqual(parseCertString(input), expected);
5959
}
6060

6161
restoreStderr();

0 commit comments

Comments
 (0)