Skip to content

Commit c64af7d

Browse files
jhamhaderrvagg
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 d0b8c5d commit c64af7d

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

doc/api/tls.markdown

+4-3
Original file line numberDiff line numberDiff line change
@@ -454,18 +454,19 @@ Or
454454
Wrapper for instance of [net.Socket][], replaces internal socket read/write
455455
routines to perform transparent encryption/decryption of incoming/outgoing data.
456456

457-
## new tls.TLSSocket(socket, options)
457+
## new tls.TLSSocket(socket[, options])
458458

459459
Construct a new TLSSocket object from existing TCP socket.
460460

461461
`socket` is an instance of [net.Socket][]
462462

463-
`options` is an object that might contain following properties:
463+
`options` is an optional object that might contain following properties:
464464

465465
- `secureContext`: An optional TLS context object from
466466
`tls.createSecureContext( ... )`
467467

468-
- `isServer`: If true - TLS socket will be instantiated in server-mode
468+
- `isServer`: If `true` - TLS socket will be instantiated in server-mode.
469+
Default: `false`
469470

470471
- `server`: An optional [net.Server][] instance
471472

lib/_tls_wrap.js

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

230230
function TLSSocket(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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
if (!common.hasCrypto) {
6+
console.log('1..0 # Skipped: missing crypto');
7+
return;
8+
}
9+
const tls = require('tls');
10+
11+
const fs = require('fs');
12+
const net = require('net');
13+
14+
const sent = 'hello world';
15+
16+
const serverOptions = {
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+
function testSocketOptions(socket, socketOptions) {
23+
let received = '';
24+
const server = 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+
let c = new tls.TLSSocket(socket, socketOptions);
37+
c.connect(common.PORT, function() {
38+
c.end(sent);
39+
});
40+
});
41+
42+
}
43+
44+
const testArgs = [
45+
[],
46+
[undefined, {}]
47+
];
48+
49+
let n = 0;
50+
function runTests() {
51+
if (n++ < testArgs.length) {
52+
testSocketOptions.apply(null, testArgs[n]);
53+
}
54+
}
55+
56+
runTests();

0 commit comments

Comments
 (0)