diff --git a/lib/_stream_wrap.js b/lib/_stream_wrap.js
index 10a0cf57e7789e..5b5f476948b23a 100644
--- a/lib/_stream_wrap.js
+++ b/lib/_stream_wrap.js
@@ -1,3 +1,3 @@
'use strict';
-module.exports = require('internal/wrap_js_stream');
+module.exports = require('internal/js_stream_socket');
diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js
index d2eb7befeaea37..a72091b22e97fb 100644
--- a/lib/_tls_wrap.js
+++ b/lib/_tls_wrap.js
@@ -29,7 +29,7 @@ const net = require('net');
const tls = require('tls');
const util = require('util');
const common = require('_tls_common');
-const { StreamWrap } = require('_stream_wrap');
+const JSStreamSocket = require('internal/js_stream_socket');
const { Buffer } = require('buffer');
const debug = util.debuglog('tls');
const { TCP, constants: TCPConstants } = internalBinding('tcp_wrap');
@@ -214,7 +214,7 @@ function requestOCSPDone(socket) {
}
-function onnewsession(key, session) {
+function onnewsession(sessionId, session) {
const owner = this[owner_symbol];
if (!owner.server)
@@ -238,7 +238,7 @@ function onnewsession(key, session) {
};
owner._newSessionPending = true;
- if (!owner.server.emit('newSession', key, session, done))
+ if (!owner.server.emit('newSession', sessionId, session, done))
done();
}
@@ -271,15 +271,17 @@ function onerror(err) {
}
}
-function initRead(tls, wrapped) {
+// Used by both client and server TLSSockets to start data flowing from _handle,
+// read(0) causes a StreamBase::ReadStart, via Socket._read.
+function initRead(tls, socket) {
// If we were destroyed already don't bother reading
if (!tls._handle)
return;
// Socket already has some buffered data - emulate receiving it
- if (wrapped && wrapped.readableLength) {
+ if (socket && socket.readableLength) {
var buf;
- while ((buf = wrapped.read()) !== null)
+ while ((buf = socket.read()) !== null)
tls._handle.receive(buf);
}
@@ -308,12 +310,14 @@ function TLSSocket(socket, opts) {
this.authorizationError = null;
this[kRes] = null;
- // Wrap plain JS Stream into StreamWrap
var wrap;
if ((socket instanceof net.Socket && socket._handle) || !socket) {
wrap = socket;
} else {
- wrap = new StreamWrap(socket);
+ // TLS expects to interact from C++ with a net.Socket that has a C++ stream
+ // handle, but a JS stream doesn't have one. Wrap it up to make it look like
+ // a socket.
+ wrap = new JSStreamSocket(socket);
wrap.once('close', () => this.destroy());
}
@@ -1225,7 +1229,7 @@ exports.connect = function connect(...args) {
const context = options.secureContext || tls.createSecureContext(options);
- var socket = new TLSSocket(options.socket, {
+ var tlssock = new TLSSocket(options.socket, {
pipe: !!options.path,
secureContext: context,
isServer: false,
@@ -1236,12 +1240,14 @@ exports.connect = function connect(...args) {
requestOCSP: options.requestOCSP
});
- socket[kConnectOptions] = options;
+ tlssock[kConnectOptions] = options;
if (cb)
- socket.once('secureConnect', cb);
+ tlssock.once('secureConnect', cb);
if (!options.socket) {
+ // If user provided the socket, its their responsibility to manage its
+ // connectivity. If we created one internally, we connect it.
const connectOpt = {
path: options.path,
port: options.port,
@@ -1250,13 +1256,13 @@ exports.connect = function connect(...args) {
localAddress: options.localAddress,
lookup: options.lookup
};
- socket.connect(connectOpt, socket._start);
+ tlssock.connect(connectOpt, tlssock._start);
}
- socket._releaseControl();
+ tlssock._releaseControl();
if (options.session)
- socket.setSession(options.session);
+ tlssock.setSession(options.session);
if (options.servername) {
if (!ipServernameWarned && net.isIP(options.servername)) {
@@ -1268,14 +1274,14 @@ exports.connect = function connect(...args) {
);
ipServernameWarned = true;
}
- socket.setServername(options.servername);
+ tlssock.setServername(options.servername);
}
if (options.socket)
- socket._start();
+ tlssock._start();
- socket.on('secure', onConnectSecure);
- socket.once('end', onConnectEnd);
+ tlssock.on('secure', onConnectSecure);
+ tlssock.once('end', onConnectEnd);
- return socket;
+ return tlssock;
};
diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js
index f33ec48ff23087..2bca31e24a5473 100644
--- a/lib/internal/http2/core.js
+++ b/lib/internal/http2/core.js
@@ -22,7 +22,7 @@ const util = require('util');
const { kIncomingMessage } = require('_http_common');
const { kServerResponse } = require('_http_server');
-const { StreamWrap } = require('_stream_wrap');
+const JSStreamSocket = require('internal/js_stream_socket');
const {
defaultTriggerAsyncIdScope,
@@ -935,7 +935,7 @@ class Http2Session extends EventEmitter {
super();
if (!socket._handle || !socket._handle._externalStream) {
- socket = new StreamWrap(socket);
+ socket = new JSStreamSocket(socket);
}
// No validation is performed on the input parameters because this
diff --git a/lib/internal/wrap_js_stream.js b/lib/internal/js_stream_socket.js
similarity index 94%
rename from lib/internal/wrap_js_stream.js
rename to lib/internal/js_stream_socket.js
index cf8f45aa4505ff..8343b6c2645842 100644
--- a/lib/internal/wrap_js_stream.js
+++ b/lib/internal/js_stream_socket.js
@@ -5,7 +5,7 @@ const util = require('util');
const { Socket } = require('net');
const { JSStream } = internalBinding('js_stream');
const uv = internalBinding('uv');
-const debug = util.debuglog('stream_wrap');
+const debug = util.debuglog('stream_socket');
const { owner_symbol } = require('internal/async_hooks').symbols;
const { ERR_STREAM_WRAP } = require('internal/errors').codes;
@@ -29,9 +29,9 @@ function onwrite(req, bufs) { return this[owner_symbol].doWrite(req, bufs); }
* can skip going through the JS layer and let TLS access the raw C++ handle
* of a net.Socket. The flipside of this is that, to maintain composability,
* we need a way to create "fake" net.Socket instances that call back into a
- * "real" JavaScript stream. JSStreamWrap is exactly this.
+ * "real" JavaScript stream. JSStreamSocket is exactly this.
*/
-class JSStreamWrap extends Socket {
+class JSStreamSocket extends Socket {
constructor(stream) {
const handle = new JSStream();
handle.close = (cb) => {
@@ -39,7 +39,7 @@ class JSStreamWrap extends Socket {
this.doClose(cb);
};
// Inside of the following functions, `this` refers to the handle
- // and `this[owner_symbol]` refers to this JSStreamWrap instance.
+ // and `this[owner_symbol]` refers to this JSStreamSocket instance.
handle.isClosing = isClosing;
handle.onreadstart = onreadstart;
handle.onreadstop = onreadstop;
@@ -88,9 +88,10 @@ class JSStreamWrap extends Socket {
this.read(0);
}
- // Legacy
+ // Allow legacy requires in the test suite to keep working:
+ // const { StreamWrap } = require('internal/js_stream_socket')
static get StreamWrap() {
- return JSStreamWrap;
+ return JSStreamSocket;
}
isClosing() {
@@ -223,4 +224,4 @@ class JSStreamWrap extends Socket {
}
}
-module.exports = JSStreamWrap;
+module.exports = JSStreamSocket;
diff --git a/node.gyp b/node.gyp
index 8c42ef446e70b6..e36f9f26aa2f06 100644
--- a/node.gyp
+++ b/node.gyp
@@ -126,6 +126,7 @@
'lib/internal/fs/watchers.js',
'lib/internal/http.js',
'lib/internal/inspector_async_hook.js',
+ 'lib/internal/js_stream_socket.js',
'lib/internal/linkedlist.js',
'lib/internal/modules/cjs/helpers.js',
'lib/internal/modules/cjs/loader.js',
@@ -188,7 +189,6 @@
'lib/internal/streams/state.js',
'lib/internal/streams/pipeline.js',
'lib/internal/streams/end-of-stream.js',
- 'lib/internal/wrap_js_stream.js',
'deps/v8/tools/splaytree.js',
'deps/v8/tools/codemap.js',
'deps/v8/tools/consarray.js',
diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index c2faad0a5966bc..201b1815e1ae80 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -1510,20 +1510,20 @@ int SSLWrap::NewSessionCallback(SSL* s, SSL_SESSION* sess) {
return 0;
// Serialize session
- Local