Skip to content

Conversation

@MasterOdin
Copy link
Contributor

PR fixes a bug introduced by #168 where when passing options to the getPort or getPorts function and no callback, then the options object would be assigned to the callback, and the whole thing would break.

As part of this, I've refactored the test suite so that all getPort and getPorts tests are now called for the callback and promise variants of the functions.

}
if(options.stopPort < options.startPort) {
throw Error('Provided options.stopPort(' + options.stopPort + 'is less than options.startPort (' + options.startPort + ')');
throw Error('Provided options.stopPort(' + options.stopPort + ') is less than options.startPort (' + options.startPort + ')');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the fix, but something I noticed when writing up the tests.

Comment on lines -127 to -130
if (!callback) {
callback = options;
options = {};
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed these from the internals call as these methods are not expected to be called by external user, and so we should be confident that options and callback are passed correctly.

const idx = exports._defaultHosts.indexOf(currentHost);
exports._defaultHosts.splice(idx, 1);
return exports.getPort(options, callback);
return internals.getPort(options, callback);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced all of these calls with internals.getPort as I don't think it's worth going through the user facing function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and your other comment https://github.com/http-party/node-portfinder/pull/172/files#r1994080054 made me realize we can fix the error message to provide the port that getPort failed on and also that we had a bad test b/c we were passing startPort through instead of port for the port 80 test which was passing b/c it tested that port finder returned a port > 80 - but it wasn't even using startPort, it was using the default port :)

I'll submit pr's and cc u

thanks for working on this stuff, this was a lot of fun tonight :)

}
});

test('with stopPort smaller than 3 available ports', function (done) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test does show a difference in behavior between the promise and callback. I think that the error message should maybe be different here, and should any ports be returned? 🤷

Out of scope of this PR though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - sending in a pr now for this good call

@eriktrom eriktrom merged commit aab95bb into http-party:master Mar 14, 2025
19 checks passed
method(3, { stopPort: 32774 }, function (err, ports) {
expect(err).not.toBeNull();
expect(err.message).toEqual('No open ports found in between 32768 and 32774');
expect(ports).toEqual([32773, 32774, undefined]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep good call - I made an initial pr for this @ #178 - so that the message is better.

@MasterOdin MasterOdin deleted the fix-promise branch July 30, 2025 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants