Skip to content

Commit

Permalink
net: honor default values in Socket constructor
Browse files Browse the repository at this point in the history
Specifically `readable` and `writable` that default to `false`.

Fixes: libuv/libuv#1794
  • Loading branch information
santigimeno committed Apr 20, 2018
1 parent 998e174 commit 485bea9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ const handleConversion = {
},

got: function(message, handle, emit) {
var socket = new net.Socket({ handle: handle });
socket.readable = socket.writable = true;
var socket = new net.Socket({
handle: handle,
readable: true,
writable: true
});

// if the socket was created by net.Server we will track the socket
if (message.key) {
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/wrap_js_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class JSStreamWrap extends Socket {
this.stream = stream;
this[kCurrentWriteRequest] = null;
this[kCurrentShutdownRequest] = null;
this.readable = stream.readable;
this.writable = stream.writable;

// Start reading.
this.read(0);
Expand Down
11 changes: 5 additions & 6 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ function Socket(options) {
else
options = util._extend({}, options);

options.readable = options.readable || false;
options.writable = options.writable || false;
const allowHalfOpen = options.allowHalfOpen;

// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
Expand Down Expand Up @@ -283,9 +285,6 @@ function Socket(options) {
value: 0, writable: true
});
}
} else {
// these will be set once there is a connection
this.readable = this.writable = false;
}

// shut down the socket when we're finished with it.
Expand Down Expand Up @@ -1537,10 +1536,10 @@ function onconnection(err, clientHandle) {
var socket = new Socket({
handle: clientHandle,
allowHalfOpen: self.allowHalfOpen,
pauseOnCreate: self.pauseOnConnect
pauseOnCreate: self.pauseOnConnect,
readable: true,
writable: true
});
socket.readable = socket.writable = true;


self._connections++;
socket.server = self;
Expand Down
43 changes: 43 additions & 0 deletions test/parallel/test-net-socket-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const net = require('net');

function test(sock, readable, writable) {
let socket;
if (sock instanceof net.Socket) {
socket = sock;
} else {
socket = new net.Socket(sock);
socket.unref();
}

assert.strictEqual(socket.readable, readable);
assert.strictEqual(socket.writable, writable);
}

test(undefined, false, false);

const server = net.createServer(common.mustCall((socket) => {
test(socket, true, true);
test({ handle: socket._handle }, false, false);
test({ handle: socket._handle, readable: true, writable: true }, true, true);
if (socket._handle.fd >= 0) {
test(socket._handle.fd, false, false);
test({ fd: socket._handle.fd }, false, false);
test({ fd: socket._handle.fd, readable: true, writable: true }, true, true);
}

server.close();
}));

server.listen(common.mustCall(() => {
const { port } = server.address();
const socket = net.connect(port, common.mustCall(() => {
test(socket, true, true);
socket.end();
}));

test(socket, false, true);
}));

0 comments on commit 485bea9

Please sign in to comment.