Comparing version 0.1.1 to 0.2.0
87
index.js
@@ -1,3 +0,3 @@ | ||
'use strict'; | ||
const execa = require('execa'); | ||
import process from 'node:process'; | ||
import execa from 'execa'; | ||
@@ -12,3 +12,3 @@ const netstat = async type => { | ||
netstat('tcp'), | ||
netstat('udp') | ||
netstat('udp'), | ||
]); | ||
@@ -30,4 +30,5 @@ | ||
const getListFunction = process.platform === 'darwin' ? macos : (process.platform === 'linux' ? linux : win32); | ||
const columns = process.platform === 'darwin' ? [3, 8] : (process.platform === 'linux' ? [4, 6] : [1, 4]); | ||
const isProtocol = x => /^\s*(tcp|udp)/i.test(x); | ||
const addressColumn = process.platform === 'darwin' ? 3 : (process.platform === 'linux' ? 4 : 1); | ||
const portColumn = process.platform === 'darwin' ? 8 : (process.platform === 'linux' ? 6 : 4); | ||
const isProtocol = value => /^\s*(tcp|udp)/i.test(value); | ||
@@ -39,8 +40,6 @@ const parsePid = pid => { | ||
const match = /(?:^|",|",pid=)(\d+)/.exec(pid); | ||
if (!match) { | ||
return; | ||
const {groups} = /(?:^|",|",pid=)(?<pid>\d+)/.exec(pid) || {}; | ||
if (groups) { | ||
return Number.parseInt(groups.pid, 10); | ||
} | ||
return Number.parseInt(match[1], 10); | ||
}; | ||
@@ -50,3 +49,3 @@ | ||
const regex = new RegExp(`[.:]${port}$`); | ||
const foundPort = list.find(x => regex.test(x[columns[0]])); | ||
const foundPort = list.find(line => regex.test(line[addressColumn])); | ||
@@ -57,3 +56,3 @@ if (!foundPort) { | ||
return parsePid(foundPort[columns[1]]); | ||
return parsePid(foundPort[portColumn]); | ||
}; | ||
@@ -64,36 +63,58 @@ | ||
return list.split('\n') | ||
// TODO: Remove the `.reduce`. | ||
// eslint-disable-next-line unicorn/no-reduce | ||
.reduce((result, x) => { | ||
if (isProtocol(x)) { | ||
result.push(x.match(/\S+/g) || []); | ||
} | ||
return result; | ||
}, []); | ||
return list | ||
.split('\n') | ||
.filter(line => isProtocol(line)) | ||
.map(line => line.match(/\S+/g) || []); | ||
}; | ||
module.exports.portToPid = async port => { | ||
export async function portToPid(port) { | ||
if (Array.isArray(port)) { | ||
const list = await getList(); | ||
const tuples = await Promise.all(port.map(x => [x, getPort(x, list)])); | ||
const tuples = await Promise.all(port.map(port_ => [port_, getPort(port_, list)])); | ||
return new Map(tuples); | ||
} | ||
if (typeof port !== 'number') { | ||
throw new TypeError(`Expected a number, got ${typeof port}`); | ||
if (!Number.isInteger(port)) { | ||
throw new TypeError(`Expected an integer, got ${typeof port}`); | ||
} | ||
return getPort(port, await getList()); | ||
}; | ||
} | ||
module.exports.all = async () => { | ||
export async function pidToPorts(pid) { | ||
if (Array.isArray(pid)) { | ||
const returnValue = new Map(pid.map(pid_ => [pid_, new Set()])); | ||
for (const [port, pid_] of await allPortsWithPid()) { | ||
if (returnValue.has(pid_)) { | ||
returnValue.get(pid_).add(port); | ||
} | ||
} | ||
return returnValue; | ||
} | ||
if (!Number.isInteger(pid)) { | ||
throw new TypeError(`Expected an integer, got ${typeof pid}`); | ||
} | ||
const returnValue = new Set(); | ||
for (const [port, pid_] of await allPortsWithPid()) { | ||
if (pid_ === pid) { | ||
returnValue.add(port); | ||
} | ||
} | ||
return returnValue; | ||
} | ||
export async function allPortsWithPid() { | ||
const list = await getList(); | ||
const returnValue = new Map(); | ||
for (const x of list) { | ||
const match = /[^]*[.:](\d+)$/.exec(x[columns[0]]); | ||
if (match) { | ||
returnValue.set(Number.parseInt(match[1], 10), parsePid(x[columns[1]])); | ||
for (const line of list) { | ||
const {groups} = /[^]*[.:](?<port>\d+)$/.exec(line[addressColumn]) || {}; | ||
if (groups) { | ||
returnValue.set(Number.parseInt(groups.port, 10), parsePid(line[portColumn])); | ||
} | ||
@@ -103,2 +124,2 @@ } | ||
return returnValue; | ||
}; | ||
} |
{ | ||
"name": "pid-port", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Get the ID of the process that uses a certain port", | ||
"license": "MIT", | ||
"repository": "sindresorhus/pid-port", | ||
"funding": { | ||
"url": "https://github.com/sponsors/sindresorhus" | ||
}, | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -31,9 +31,12 @@ "scripts": { | ||
"dependencies": { | ||
"execa": "^5.0.0" | ||
"execa": "^5.1.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"get-port": "^5.1.1", | ||
"xo": "^0.36.1" | ||
"ava": "^3.15.0", | ||
"get-port": "^6.0.0", | ||
"xo": "^0.45.0" | ||
}, | ||
"ava": { | ||
"serial": true | ||
} | ||
} |
@@ -7,5 +7,5 @@ # pid-port | ||
```sh | ||
npm install pid-port | ||
``` | ||
$ npm install pid-port | ||
``` | ||
@@ -15,21 +15,19 @@ ## Usage | ||
```js | ||
const pidPort = require('pid-port'); | ||
import {portToPid} from 'pid-port'; | ||
(async () => { | ||
try { | ||
console.log(await pidPort.portToPid(8080)); | ||
//=> 1337 | ||
try { | ||
console.log(await portToPid(8080)); | ||
//=> 1337 | ||
const pids = await pidPort.portToPid([8080, 22]); | ||
const pids = await portToPid([8080, 22]); | ||
console.log(pids.get(8080)); | ||
//=> 1337 | ||
console.log(pids.get(8080)); | ||
//=> 1337 | ||
console.log(pids.get(22)); | ||
//=> 12345 | ||
} catch (error) { | ||
console.log(error); | ||
//=> 'Could not find a process that uses port `8080`' | ||
} | ||
})(); | ||
console.log(pids.get(22)); | ||
//=> 12345 | ||
} catch (error) { | ||
console.log(error); | ||
//=> 'Could not find a process that uses port `8080`' | ||
} | ||
``` | ||
@@ -39,26 +37,51 @@ | ||
### pidPort.portToPid(port) | ||
### portToPid(port) | ||
Returns a `Promise<number>` with the process ID. | ||
Returns a `Promise<number>` *(integer)* with the process ID. | ||
#### port | ||
Type: `number` | ||
Type: `number` *(integer)* | ||
Port to look up. | ||
The port to look up. | ||
### pidPort.portToPid(ports) | ||
### portToPid(ports) | ||
Returns a `Promise<Map<number, number>>` with the port as key and the process ID as value. | ||
Returns a `Promise<Map<number, number>>` *(integer)* with the port as key and the process ID as value. | ||
#### ports | ||
Type: `number[]` *(integer)* | ||
The ports to look up. | ||
### pidToPorts(pid) | ||
Returns a `Promise<Set<number>>` with the ports. | ||
#### pid | ||
Type: `number` | ||
The process ID to look up. | ||
### pidToPorts(pids) | ||
Returns a `Promise<Map<number, Set<number>>>` with the process ID as the key and the ports as value. | ||
#### pids | ||
Type: `number[]` | ||
Ports to look up. | ||
The process IDs to look up. | ||
### pidPort.all() | ||
### allPortsWithPid() | ||
Get all process IDs from ports. | ||
Get all ports with their process ID. | ||
Returns a `Promise<Map<number, number>>` with the port as key and the process ID as value. | ||
Returns a `Promise<Map<number, number>>` *(integer)* with the port as key and the process ID as value. | ||
## Related | ||
- [fkill-cli](https://github.com/sindresorhus/fkill-cli) - Uses this package to let you kill the process that occupies a certain port | ||
- [pid-cwd](https://github.com/neeksandhu/pid-cwd) - Find the working directory of a process from its process ID |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6202
92
85
Yes
Updatedexeca@^5.1.1