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

‎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/parse-cert-string');
5760

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

‎lib/internal/streams/duplexpair.js‎

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
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+
functionparseCertString(s){
16+
constout=ObjectCreate(null);
17+
ArrayPrototypeForEach(StringPrototypeSplit(s,'\n'),(part)=>{
18+
constsepIndex=StringPrototypeIndexOf(part,'=');
19+
if(sepIndex>0){
20+
constkey=StringPrototypeSlice(part,0,sepIndex);
21+
constvalue=StringPrototypeSlice(part,sepIndex+1);
22+
if(keyinout){
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+
returnout;
33+
}
34+
35+
exports.parseCertString=parseCertString;
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-
functionparseCertString(s){
48-
constout=ObjectCreate(null);
49-
ArrayPrototypeForEach(StringPrototypeSplit(s,'\n'),(part)=>{
50-
constsepIndex=StringPrototypeIndexOf(part,'=');
51-
if(sepIndex>0){
52-
constkey=StringPrototypeSlice(part,0,sepIndex);
53-
constvalue=StringPrototypeSlice(part,sepIndex+1);
54-
if(keyinout){
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-
returnout;
65-
}
66-
6741
functiongetDefaultEcdhCurve(){
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/internal/tls/secure-pair.js‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
constEventEmitter=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+
constkCallback=Symbol('Callback');
14+
constkOtherSide=Symbol('Other');
15+
16+
classDuplexSocketextendsDuplex{
17+
constructor(){
18+
super();
19+
this[kCallback]=null;
20+
this[kOtherSide]=null;
21+
}
22+
23+
_read(){
24+
constcallback=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+
classDuplexPair{
47+
constructor(){
48+
this.socket1=newDuplexSocket();
49+
this.socket2=newDuplexSocket();
50+
this.socket1[kOtherSide]=this.socket2;
51+
this.socket2[kOtherSide]=this.socket1;
52+
}
53+
}
54+
55+
classSecurePairextendsEventEmitter{
56+
constructor(secureContext=_tls_common.createSecureContext(),
57+
isServer=false,
58+
requestCert=!isServer,
59+
rejectUnauthorized=false,
60+
options={}){
61+
super();
62+
const{ socket1, socket2 }=newDuplexPair();
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=functioncreateSecurePair(...args){
85+
returnReflectConstruct(SecurePair,args);
86+
};

‎lib/tls.js‎

Lines changed: 9 additions & 44 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,18 @@ const{
5049
}=require('internal/errors').codes;
5150
constinternalUtil=require('internal/util');
5251
internalUtil.assertCrypto();
53-
constinternalTLS=require('internal/tls');
5452
const{ isArrayBufferView }=require('internal/util/types');
5553

5654
constnet=require('net');
5755
const{ getOptionValue }=require('internal/options');
5856
const{ getRootCertificates, getSSLCiphers }=internalBinding('crypto');
5957
const{ Buffer }=require('buffer');
60-
constEventEmitter=require('events');
6158
const{URL}=require('internal/url');
62-
constDuplexPair=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-
classSecurePairextendsEventEmitter{
305-
constructor(secureContext=exports.createSecureContext(),
306-
isServer=false,
307-
requestCert=!isServer,
308-
rejectUnauthorized=false,
309-
options={}){
310-
super();
311-
const{ socket1, socket2 }=newDuplexPair();
312-
313-
this.server=options.server;
314-
this.credentials=secureContext;
315-
316-
this.encrypted=socket1;
317-
this.cleartext=newexports.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-
functioncreateSecurePair(...args){
349-
returnReflectConstruct(SecurePair,args);
350-
},
315+
createSecurePair,
351316
'tls.createSecurePair() is deprecated. Please use '+
352317
'tls.TLSSocket instead.','DEP0064');

‎src/node_native_module.cc‎

Lines changed: 3 additions & 1 deletion
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",

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

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

1717
constnoOutput=common.mustNotCall();
@@ -20,7 +20,7 @@ hijackStderr(noOutput);
2020
{
2121
constsingles='C=US\nST=CA\nL=SF\nO=Node.js Foundation\nOU=Node.js\n'+
2222
23-
constsinglesOut=internalTLS.parseCertString(singles);
23+
constsinglesOut=parseCertString(singles);
2424
assert.deepStrictEqual(singlesOut,{
2525
__proto__: null,
2626
C: 'US',
@@ -36,7 +36,7 @@ hijackStderr(noOutput);
3636
{
3737
constdoubles='OU=Domain Control Validated\nOU=PositiveSSL Wildcard\n'+
3838
'CN=*.nodejs.org';
39-
constdoublesOut=internalTLS.parseCertString(doubles);
39+
constdoublesOut=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
constinvalid='fhqwhgads';
49-
constinvalidOut=internalTLS.parseCertString(invalid);
49+
constinvalidOut=parseCertString(invalid);
5050
assert.deepStrictEqual(invalidOut,{__proto__: null});
5151
}
5252

@@ -55,7 +55,7 @@ hijackStderr(noOutput);
5555
constexpected=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)