http-party / node-portfinder

A simple tool to find an open port or domain socket on the current machine
https://github.com/http-party/node-portfinder
Other
887 stars 95 forks source link

better docs #57

Closed liesislukas closed 6 years ago

liesislukas commented 7 years ago

great tool, thanks for the code but i had to find out myself by navigating your source that it does have option to define preferred port. Please add at least config options to readme.

const preferredPort = process.env.PORT || 3003;
portfinder.getPortPromise({port: preferredPort})
  .then((port) => {
...
eriktrom commented 7 years ago

agreed - thanks for the push - btw - u can use portfinder.basePort for the rhs of your preferredPort above e.g., portfinder.basePort = 3003 will automatically begin the port iteration with port 3003.

emileber commented 6 years ago

@eriktrom portfinder.basePort is a problem when searching for 2 or more ports on different ranges at once. I almost dismissed this package since I thought it wasn't possible, until I saw this issue.

Documenting the options would really help.

eriktrom commented 6 years ago

Documenting the options would really help.

Yes - sorry about that

portfinder.basePort is a problem when searching for 2 or more ports on different ranges at once

regarding this - keep in mind that there is a rather random (depending on ur version of node, and your os) amount of time that it takes for the socket to close(and the file descriptor to be cleaned up) - thus if u run portfinder concurrently, don't set the basePort to be the same for each process you spawn - in practice(in the tests actually) i recall this being an issue - separate basePort by 3-5 ports for each process u spawn - otherwise, i'm willing to bet the processes will collide as they try to roughly iterate over the same range at the same time...

liesislukas commented 6 years ago

@eriktrom it could add all possible ports to an array and pick a random item from an array on each iteration so collision probability will be a lot lower.

I've tried to use this package inside tests and I would love to run 8 tests at the same time but can't because of the collision. Now tests are running 1-by-1 and take 8x more time to run.

eriktrom commented 6 years ago
eriktrom commented 6 years ago

@liesislukas

to get a random port, modify this (CLRS based random number generator, low probability for collisions), pass that in to portfinder.getPort

  _randomInt(min=0, max=min) {
    if (max - min < 0) { throw Error('max - min must be greater than 0'); }
    if (!Number.isInteger(min) || !Number.isInteger(max)) { throw Error('Must pass integers'); }
    if (min < 0 || max < 0) { throw Error('max and min must be positive integers'); }
    return Math.floor(Math.random() * (max - min + 1) + min);
  },

  _shuffle(input=[]) {
    const n = input.length - 1;
    for (const index of input.keys()) {
      const randomIndex = this._randomInt(index, n);
      [input[index], input[randomIndex]] = [input[randomIndex], input[index]];
    }
    return input;
  },

cheers

eriktrom commented 6 years ago

closed by #70

Jaykamdar commented 5 years ago

No need to include external dependencies:

Just add or config from your config file

for vue config: const portfinder = require("portfinder/lib/portfinder"); // Give appropriate path of portfinder.js portfinder.startPort = 44342; // required port - 1 portfinder.highestPort = 44344; // required port + 1

devServer{ port: 44343 // required port }