Skip to content

Commit c446fad

Browse files
committed
tls: move legacy code into own file
1 parent ff975fe commit c446fad

File tree

7 files changed

+133
-129
lines changed

7 files changed

+133
-129
lines changed

lib/_tls_common.js

Lines changed: 4 additions & 1 deletion
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/legacy');
5760

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

lib/internal/streams/duplexpair.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/internal/tls/legacy.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
'use strict';
2+
3+
const EventEmitter = require('events');
4+
const { Duplex } = require('stream');
5+
const internalUtil = require('internal/util');
6+
7+
const {
8+
ArrayIsArray,
9+
ArrayPrototypeForEach,
10+
ArrayPrototypePush,
11+
StringPrototypeIndexOf,
12+
StringPrototypeSlice,
13+
StringPrototypeSplit,
14+
ObjectCreate,
15+
Symbol,
16+
ReflectConstruct,
17+
} = primordials;
18+
19+
const kCallback = Symbol('Callback');
20+
const kOtherSide = Symbol('Other');
21+
22+
class DuplexSocket extends Duplex {
23+
constructor() {
24+
super();
25+
this[kCallback] = null;
26+
this[kOtherSide] = null;
27+
}
28+
29+
_read() {
30+
const callback = this[kCallback];
31+
if (callback) {
32+
this[kCallback] = null;
33+
callback();
34+
}
35+
}
36+
37+
_write(chunk, encoding, callback) {
38+
if (chunk.length === 0) {
39+
process.nextTick(callback);
40+
} else {
41+
this[kOtherSide].push(chunk);
42+
this[kOtherSide][kCallback] = callback;
43+
}
44+
}
45+
46+
_final(callback) {
47+
this[kOtherSide].on('end', callback);
48+
this[kOtherSide].push(null);
49+
}
50+
}
51+
52+
class DuplexPair {
53+
constructor() {
54+
this.socket1 = new DuplexSocket();
55+
this.socket2 = new DuplexSocket();
56+
this.socket1[kOtherSide] = this.socket2;
57+
this.socket2[kOtherSide] = this.socket1;
58+
}
59+
}
60+
61+
class SecurePair extends EventEmitter {
62+
constructor(secureContext = exports.createSecureContext(),
63+
isServer = false,
64+
requestCert = !isServer,
65+
rejectUnauthorized = false,
66+
options = {}) {
67+
super();
68+
const { socket1, socket2 } = new DuplexPair();
69+
70+
this.server = options.server;
71+
this.credentials = secureContext;
72+
73+
this.encrypted = socket1;
74+
this.cleartext = new exports.TLSSocket(socket2, {
75+
secureContext,
76+
isServer,
77+
requestCert,
78+
rejectUnauthorized,
79+
...options
80+
});
81+
this.cleartext.once('secure', () => this.emit('secure'));
82+
}
83+
84+
destroy() {
85+
this.cleartext.destroy();
86+
this.encrypted.destroy();
87+
}
88+
}
89+
90+
// Example:
91+
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\[email protected]
92+
function parseCertString(s) {
93+
const out = ObjectCreate(null);
94+
ArrayPrototypeForEach(StringPrototypeSplit(s, '\n'), (part) => {
95+
const sepIndex = StringPrototypeIndexOf(part, '=');
96+
if (sepIndex > 0) {
97+
const key = StringPrototypeSlice(part, 0, sepIndex);
98+
const value = StringPrototypeSlice(part, sepIndex + 1);
99+
if (key in out) {
100+
if (!ArrayIsArray(out[key])) {
101+
out[key] = [out[key]];
102+
}
103+
ArrayPrototypePush(out[key], value);
104+
} else {
105+
out[key] = value;
106+
}
107+
}
108+
});
109+
return out;
110+
}
111+
112+
exports.parseCertString = internalUtil.deprecate(
113+
parseCertString,
114+
'tls.parseCertString() is deprecated. ' +
115+
'Please use querystring.parse() instead.',
116+
'DEP0076');
117+
118+
exports.createSecurePair = internalUtil.deprecate(
119+
function createSecurePair(...args) {
120+
return ReflectConstruct(SecurePair, args);
121+
},
122+
'tls.createSecurePair() is deprecated. Please use ' +
123+
'tls.TLSSocket instead.', 'DEP0064');
Lines changed: 0 additions & 27 deletions
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
};

lib/tls.js

Lines changed: 3 additions & 48 deletions
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,17 @@ 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 { parseCertString, createSecurePair } = require('internal/tls/legacy');
6663

6764
// Allow {CLIENT_RENEG_LIMIT} client-initiated session renegotiations
6865
// every {CLIENT_RENEG_WINDOW} seconds. An error event is emitted if more
@@ -300,53 +297,11 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
300297
}
301298
};
302299

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-
340300
exports.createSecureContext = _tls_common.createSecureContext;
341301
exports.SecureContext = _tls_common.SecureContext;
342302
exports.TLSSocket = _tls_wrap.TLSSocket;
343303
exports.Server = _tls_wrap.Server;
344304
exports.createServer = _tls_wrap.createServer;
345305
exports.connect = _tls_wrap.connect;
346-
347-
exports.createSecurePair = internalUtil.deprecate(
348-
function createSecurePair(...args) {
349-
return ReflectConstruct(SecurePair, args);
350-
},
351-
'tls.createSecurePair() is deprecated. Please use ' +
352-
'tls.TLSSocket instead.', 'DEP0064');
306+
exports.parseCertString = parseCertString;
307+
exports.createSecurePair = createSecurePair;

src/node_native_module.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ void NativeModuleLoader::InitializeModuleCategories() {
9999
"tls",
100100
"_tls_common",
101101
"_tls_wrap",
102-
"internal/tls",
102+
"internal/tls/index",
103+
"internal/tls/secure-context",
103104
"internal/http2/core",
104105
"internal/http2/compat",
105106
"internal/policy/manifest",

test/parallel/test-tls-parse-cert-string.js

Lines changed: 1 addition & 1 deletion
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 internalTLS = require('internal/tls/legacy');
1515
const tls = require('tls');
1616

1717
const noOutput = common.mustNotCall();

0 commit comments

Comments
 (0)