From 609ac44e8508c6e71baad48d080134bad7f15189 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 18 Dec 2019 18:17:53 +0100 Subject: [PATCH] Meta tweaks --- index.d.ts | 1 - index.js | 12 ++++++------ package.json | 12 +++++------- readme.md | 5 ----- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3ede336..d930546 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,4 @@ /// -import {Omit} from 'type-fest'; import {ListenOptions} from 'net'; declare namespace getPort { diff --git a/index.js b/index.js index da4a967..ec4ec49 100644 --- a/index.js +++ b/index.js @@ -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(); @@ -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(); diff --git a/package.json b/package.json index 70e2608..c759eb4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/readme.md b/readme.md index e21801f..05d78fb 100644 --- a/readme.md +++ b/readme.md @@ -2,14 +2,12 @@ > Get an available [TCP port](https://en.wikipedia.org/wiki/Port_(computer_networking)) - ## Install ``` $ npm install get-port ``` - ## Usage ```js @@ -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 - ---