Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 18, 2019
1 parent 45a71bc commit 609ac44
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
1 change: 0 additions & 1 deletion index.d.ts
@@ -1,5 +1,4 @@
/// <reference types="node"/>
import {Omit} from 'type-fest';
import {ListenOptions} from 'net';

declare namespace getPort {
Expand Down
12 changes: 6 additions & 6 deletions index.js
Expand Up @@ -12,13 +12,13 @@ const lockedPorts = {
young: new Set()
};

// On this interval the old locked ports are discarded
// the young locked ports are moved to old locked ports
// and a new young set for locked ports is created
// On this interval, the old locked ports are discarded,
// the young locked ports are moved to old locked ports,
// and a new young set for locked ports are created.
const releaseOldLockedPortsIntervalMs = 1000 * 15;

// Lazily create interval on first use
let interval = null;
let interval;

const getAvailablePort = options => new Promise((resolve, reject) => {
const server = net.createServer();
Expand All @@ -41,13 +41,13 @@ const portCheckSequence = function * (ports) {
};

module.exports = async options => {
let ports = null;
let ports;

if (options) {
ports = typeof options.port === 'number' ? [options.port] : options.port;
}

if (interval === null) {
if (interval === undefined) {
interval = setInterval(() => {
lockedPorts.old = lockedPorts.young;
lockedPorts.young = new Set();
Expand Down
12 changes: 5 additions & 7 deletions package.json
Expand Up @@ -4,6 +4,7 @@
"description": "Get an available port",
"license": "MIT",
"repository": "sindresorhus/get-port",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
Expand Down Expand Up @@ -36,13 +37,10 @@
"preferred",
"chosen"
],
"dependencies": {
"type-fest": "^0.3.0"
},
"devDependencies": {
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"@types/node": "^12.12.21",
"ava": "^2.4.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
}
}
5 changes: 0 additions & 5 deletions readme.md
Expand Up @@ -2,14 +2,12 @@

> Get an available [TCP port](https://en.wikipedia.org/wiki/Port_(computer_networking))

## Install

```
$ npm install get-port
```


## Usage

```js
Expand Down Expand Up @@ -88,19 +86,16 @@ Type: `number`

Last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.


## Beware

There is a very tiny chance of a race condition if another process starts using the same port number as you in between the time you get the port number and you actually start using it.

Race conditions in the same process are mitigated against by using a lightweight locking mechanism where a port will be held for a minimum of 15 seconds and a maximum of 30 seconds before being released again.


## Related

- [get-port-cli](https://github.com/sindresorhus/get-port-cli) - CLI for this module


---

<div align="center">
Expand Down

0 comments on commit 609ac44

Please sign in to comment.