mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 22:40:57 +00:00

Previously Node.js would handle empty `net.connect()` and `socket.connect()` call as if the user passed empty options object which doesn't really make sense. This was due to the fact that it uses the same `normalizeArgs` function as `.listen()` call where such call is perfectly fine. This will make it clear what is the problem with such call and how it can be resolved. It now throws `ERR_MISSING_ARGS` if no arguments were passed or neither `path` nor `port` is specified. Fixes: https://github.com/nodejs/node/issues/33930 PR-URL: https://github.com/nodejs/node/pull/34022 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
'use strict';
|
|
// Flags: --expose-internals
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const net = require('net');
|
|
const { normalizedArgsSymbol } = require('internal/net');
|
|
|
|
function validateNormalizedArgs(input, output) {
|
|
const args = net._normalizeArgs(input);
|
|
|
|
assert.deepStrictEqual(args, output);
|
|
assert.strictEqual(args[normalizedArgsSymbol], true);
|
|
}
|
|
|
|
// Test creation of normalized arguments.
|
|
const res = [{}, null];
|
|
res[normalizedArgsSymbol] = true;
|
|
validateNormalizedArgs([], res);
|
|
res[0].port = 1234;
|
|
validateNormalizedArgs([{ port: 1234 }], res);
|
|
res[1] = assert.fail;
|
|
validateNormalizedArgs([{ port: 1234 }, assert.fail], res);
|
|
|
|
// Connecting to the server should fail with a standard array.
|
|
{
|
|
const server = net.createServer(common.mustNotCall('should not connect'));
|
|
|
|
server.listen(common.mustCall(() => {
|
|
const port = server.address().port;
|
|
const socket = new net.Socket();
|
|
|
|
assert.throws(() => {
|
|
socket.connect([{ port }, assert.fail]);
|
|
}, {
|
|
code: 'ERR_MISSING_ARGS'
|
|
});
|
|
server.close();
|
|
}));
|
|
}
|
|
|
|
// Connecting to the server should succeed with a normalized array.
|
|
{
|
|
const server = net.createServer(common.mustCall((connection) => {
|
|
connection.end();
|
|
server.close();
|
|
}));
|
|
|
|
server.listen(common.mustCall(() => {
|
|
const port = server.address().port;
|
|
const socket = new net.Socket();
|
|
const args = net._normalizeArgs([{ port }, common.mustCall()]);
|
|
|
|
socket.connect(args);
|
|
}));
|
|
}
|