Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: honor default values in Socket constructor #19971

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}));