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: handle net.connect() without arguments #34022

Closed
wants to merge 2 commits into from
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
5 changes: 4 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,10 @@ E('ERR_MISSING_ARGS',
assert(args.length > 0, 'At least one arg needs to be specified');
let msg = 'The ';
const len = args.length;
args = args.map((a) => `"${a}"`);
const wrap = (a) => `"${a}"`;
args = args.map(
(a) => (ArrayIsArray(a) ? a.map(wrap).join(' or ') : wrap(a))
);
switch (len) {
case 1:
msg += `${args[0]} argument`;
Expand Down
7 changes: 6 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ const {
ERR_INVALID_OPT_VALUE,
ERR_SERVER_ALREADY_LISTEN,
ERR_SERVER_NOT_RUNNING,
ERR_SOCKET_CLOSED
ERR_SOCKET_CLOSED,
ERR_MISSING_ARGS,
},
errnoException,
exceptionWithHostPort,
Expand Down Expand Up @@ -921,6 +922,10 @@ Socket.prototype.connect = function(...args) {
const options = normalized[0];
const cb = normalized[1];

// options.port === null will be checked later.
if (options.port === undefined && options.path == null)
throw new ERR_MISSING_ARGS(['options', 'port', 'path']);

if (this.write !== Socket.prototype.write)
this.write = Socket.prototype.write;

Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-net-connect-no-arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

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

// Tests that net.connect() called without arguments throws ERR_MISSING_ARGS.

assert.throws(() => {
net.connect();
}, {
code: 'ERR_MISSING_ARGS',
message: 'The "options" or "port" or "path" argument must be specified',
});

assert.throws(() => {
new net.Socket().connect();
}, {
code: 'ERR_MISSING_ARGS',
message: 'The "options" or "port" or "path" argument must be specified',
});

assert.throws(() => {
net.connect({});
}, {
code: 'ERR_MISSING_ARGS',
message: 'The "options" or "port" or "path" argument must be specified',
});

assert.throws(() => {
new net.Socket().connect({});
}, {
code: 'ERR_MISSING_ARGS',
message: 'The "options" or "port" or "path" argument must be specified',
});
3 changes: 1 addition & 2 deletions test/parallel/test-net-connect-options-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const net = require('net');
{
// connect({hint}, cb) and connect({hint})
const hints = (dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL) + 42;
const hintOptBlocks = doConnect([{ hints }],
const hintOptBlocks = doConnect([{ port: 42, hints }],
() => common.mustNotCall());
for (const fn of hintOptBlocks) {
assert.throws(fn, {
Expand Down Expand Up @@ -95,7 +95,6 @@ const net = require('net');
// Try connecting to random ports, but do so once the server is closed
server.on('close', () => {
asyncFailToConnect(0);
asyncFailToConnect(/* undefined */);
});
}

Expand Down
16 changes: 6 additions & 10 deletions test/parallel/test-net-normalize-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const common = require('../common');
const assert = require('assert');
const net = require('net');
const { normalizedArgsSymbol } = require('internal/net');
const { getSystemErrorName } = require('util');

function validateNormalizedArgs(input, output) {
const args = net._normalizeArgs(input);
Expand All @@ -27,18 +26,15 @@ validateNormalizedArgs([{ port: 1234 }, assert.fail], res);
const server = net.createServer(common.mustNotCall('should not connect'));

server.listen(common.mustCall(() => {
const possibleErrors = ['ECONNREFUSED', 'EADDRNOTAVAIL'];
const port = server.address().port;
const socket = new net.Socket();

socket.on('error', common.mustCall((err) => {
assert(possibleErrors.includes(err.code));
assert(possibleErrors.includes(getSystemErrorName(err.errno)));
assert.strictEqual(err.syscall, 'connect');
server.close();
}));

socket.connect([{ port }, assert.fail]);
assert.throws(() => {
socket.connect([{ port }, assert.fail]);
}, {
code: 'ERR_MISSING_ARGS'
});
server.close();
}));
}

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-tls-connect-allow-half-open-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const fixtures = require('../common/fixtures');
const tls = require('tls');

{
const socket = tls.connect({ lookup() {} });
const socket = tls.connect({ port: 42, lookup() {} });
assert.strictEqual(socket.allowHalfOpen, false);
}

{
const socket = tls.connect({ allowHalfOpen: false, lookup() {} });
const socket = tls.connect({ port: 42, allowHalfOpen: false, lookup() {} });
assert.strictEqual(socket.allowHalfOpen, false);
}

Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-tls-connect-hints-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ assert.notStrictEqual(hints, dns.V4MAPPED | dns.ALL);
assert.notStrictEqual(hints, dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL);

tls.connect({
port: 42,
lookup: common.mustCall((host, options) => {
assert.strictEqual(host, 'localhost');
assert.deepStrictEqual(options, { family: undefined, hints });
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-tls-connect-timeout-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const assert = require('assert');
const tls = require('tls');

const socket = tls.connect({
port: 42,
lookup: () => {},
timeout: 1000
});
Expand Down