Skip to content

Commit e69c869

Browse files
jhamhaderjasnell
authored andcommitted
tls: TLSSocket options default isServer false
Upon creating a TLSSocket object, set the default isServer option to false Updated tls docs and added test-tls-socket-default-options PR-URL: #2614 Reviewed-By: Fedor Indutny <[email protected]>
1 parent b884899 commit e69c869

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

‎doc/api/tls.markdown‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,18 +449,19 @@ Or
449449
Wrapper for instance of [net.Socket][], replaces internal socket read/write
450450
routines to perform transparent encryption/decryption of incoming/outgoing data.
451451

452-
## new tls.TLSSocket(socket, options)
452+
## new tls.TLSSocket(socket[, options])
453453

454454
Construct a new TLSSocket object from existing TCP socket.
455455

456456
`socket` is an instance of [net.Socket][]
457457

458-
`options` is an object that might contain following properties:
458+
`options` is an optional object that might contain following properties:
459459

460460
-`secureContext`: An optional TLS context object from
461461
`tls.createSecureContext( ... )`
462462

463-
-`isServer`: If true - TLS socket will be instantiated in server-mode
463+
-`isServer`: If `true` - TLS socket will be instantiated in server-mode.
464+
Default: `false`
464465

465466
-`server`: An optional [net.Server][] instance
466467

‎lib/_tls_wrap.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ function initRead(tls, wrapped){
228228
*/
229229

230230
functionTLSSocket(socket,options){
231-
this._tlsOptions=options;
231+
if(options===undefined)
232+
this._tlsOptions={};
233+
else
234+
this._tlsOptions=options;
232235
this._secureEstablished=false;
233236
this._securePending=false;
234237
this._newSessionPending=false;
@@ -321,7 +324,7 @@ TLSSocket.prototype._wrapHandle = function(wrap){
321324
tls.createSecureContext();
322325
res=tls_wrap.wrap(handle._externalStream,
323326
context.context,
324-
options.isServer);
327+
!!options.isServer);
325328
res._parent=handle;
326329
res._parentWrap=wrap;
327330
res._secureContext=context;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
5+
if(!common.hasCrypto){
6+
console.log('1..0 # Skipped: missing crypto');
7+
return;
8+
}
9+
consttls=require('tls');
10+
11+
constfs=require('fs');
12+
constnet=require('net');
13+
14+
constsent='hello world';
15+
16+
constserverOptions={
17+
isServer: true,
18+
key: fs.readFileSync(common.fixturesDir+'/keys/agent1-key.pem'),
19+
cert: fs.readFileSync(common.fixturesDir+'/keys/agent1-cert.pem')
20+
};
21+
22+
functiontestSocketOptions(socket,socketOptions){
23+
letreceived='';
24+
constserver=tls.createServer(serverOptions,function(s){
25+
s.on('data',function(chunk){
26+
received+=chunk;
27+
});
28+
29+
s.on('end',function(){
30+
server.close();
31+
s.destroy();
32+
assert.equal(received,sent);
33+
setImmediate(runTests);
34+
});
35+
}).listen(common.PORT,function(){
36+
letc=newtls.TLSSocket(socket,socketOptions);
37+
c.connect(common.PORT,function(){
38+
c.end(sent);
39+
});
40+
});
41+
42+
}
43+
44+
consttestArgs=[
45+
[],
46+
[undefined,{}]
47+
];
48+
49+
letn=0;
50+
functionrunTests(){
51+
if(n++<testArgs.length){
52+
testSocketOptions.apply(null,testArgs[n]);
53+
}
54+
}
55+
56+
runTests();

0 commit comments

Comments
(0)