net: honor default values in Socket constructor
Specifically `readable` and `writable` that default to `false`. PR-URL: https://github.com/nodejs/node/pull/19971 Fixes: https://github.com/libuv/libuv/issues/1794 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
This commit is contained in:
parent
8786889656
commit
67e2a15d75
@ -131,8 +131,11 @@ const handleConversion = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
got: function(message, handle, emit) {
|
got: function(message, handle, emit) {
|
||||||
var socket = new net.Socket({ handle: handle });
|
var socket = new net.Socket({
|
||||||
socket.readable = socket.writable = true;
|
handle: handle,
|
||||||
|
readable: true,
|
||||||
|
writable: true
|
||||||
|
});
|
||||||
|
|
||||||
// if the socket was created by net.Server we will track the socket
|
// if the socket was created by net.Server we will track the socket
|
||||||
if (message.key) {
|
if (message.key) {
|
||||||
|
@ -72,6 +72,8 @@ class JSStreamWrap extends Socket {
|
|||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this[kCurrentWriteRequest] = null;
|
this[kCurrentWriteRequest] = null;
|
||||||
this[kCurrentShutdownRequest] = null;
|
this[kCurrentShutdownRequest] = null;
|
||||||
|
this.readable = stream.readable;
|
||||||
|
this.writable = stream.writable;
|
||||||
|
|
||||||
// Start reading.
|
// Start reading.
|
||||||
this.read(0);
|
this.read(0);
|
||||||
|
11
lib/net.js
11
lib/net.js
@ -244,6 +244,8 @@ function Socket(options) {
|
|||||||
else
|
else
|
||||||
options = util._extend({}, options);
|
options = util._extend({}, options);
|
||||||
|
|
||||||
|
options.readable = options.readable || false;
|
||||||
|
options.writable = options.writable || false;
|
||||||
const allowHalfOpen = options.allowHalfOpen;
|
const allowHalfOpen = options.allowHalfOpen;
|
||||||
|
|
||||||
// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
|
// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
|
||||||
@ -283,9 +285,6 @@ function Socket(options) {
|
|||||||
value: 0, writable: true
|
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.
|
// shut down the socket when we're finished with it.
|
||||||
@ -1537,10 +1536,10 @@ function onconnection(err, clientHandle) {
|
|||||||
var socket = new Socket({
|
var socket = new Socket({
|
||||||
handle: clientHandle,
|
handle: clientHandle,
|
||||||
allowHalfOpen: self.allowHalfOpen,
|
allowHalfOpen: self.allowHalfOpen,
|
||||||
pauseOnCreate: self.pauseOnConnect
|
pauseOnCreate: self.pauseOnConnect,
|
||||||
|
readable: true,
|
||||||
|
writable: true
|
||||||
});
|
});
|
||||||
socket.readable = socket.writable = true;
|
|
||||||
|
|
||||||
|
|
||||||
self._connections++;
|
self._connections++;
|
||||||
socket.server = self;
|
socket.server = self;
|
||||||
|
43
test/parallel/test-net-socket-constructor.js
Normal file
43
test/parallel/test-net-socket-constructor.js
Normal file
@ -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);
|
||||||
|
}));
|
Loading…
x
Reference in New Issue
Block a user