Skip to content

Commit

Permalink
Merge pull request #1 from ksathyanm/initial-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ksathyanm authored Nov 2, 2020
2 parents e747be5 + e62ad52 commit 6c0e057
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 104 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
"extends": [
"eslint:recommended",
"plugin:node/recommended",
"google",
],
"env": {
"node": true,
"commonjs": true,
"es2020": true,
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "script",
},
"rules": {
"quotes": ["error", "double"],
"max-len": ["error", { "code": 120 }],
"new-cap": ["off"],
"object-curly-spacing": ["error", "always"],
"semi": ["error", "never"],
},
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
28 changes: 28 additions & 0 deletions .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: test-linux

on:
push:
branches:
- main
pull_request:
branches:
- "**"

jobs:
test-linux:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npx envinfo
- run: npm install
- run: npm run ci
28 changes: 28 additions & 0 deletions .github/workflows/test-macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: test-macOS

on:
push:
branches:
- main
pull_request:
branches:
- "**"

jobs:
test-macOS:

runs-on: macos-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npx envinfo
- run: npm install
- run: npm run ci
28 changes: 28 additions & 0 deletions .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: test-windows

on:
push:
branches:
- main
pull_request:
branches:
- "**"

jobs:
test-windows:

runs-on: windows-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npx envinfo
- run: npm install
- run: npm run ci
104 changes: 1 addition & 103 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,2 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
package-lock.json
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# find-pid-from-port
Find the pid(s) of a process on a given port

![Test windows status](https://github.com/ksathyanm/find-pid-from-port/workflows/test-windows/badge.svg)
![Test macOS status](https://github.com/ksathyanm/find-pid-from-port/workflows/test-macOS/badge.svg)
![Test linux status](https://github.com/ksathyanm/find-pid-from-port/workflows/test-linux/badge.svg)

[![npm version](https://badge.fury.io/js/find-pid-from-port.svg)](https://badge.fury.io/js/find-pid-from-port)
[![Node.js version](https://img.shields.io/node/v/find-pid-from-port)](https://nodejs.org/en/download/)
[![npm downloads](https://img.shields.io/npm/dm/find-pid-from-port)](https://www.npmjs.com/package/find-pid-from-port)
[![Minified size](https://img.shields.io/bundlephobia/min/find-pid-from-port)](https://bundlephobia.com/result?p=find-pid-from-port)
[![Known vulnerabilities](https://snyk.io/test/npm/find-pid-from-port/badge.svg)](https://snyk.io/test/npm/find-pid-from-port)

> Find the pid(s) of a process on a given port
## Install

```
$ npm install find-pid-from-port
```

## Usage

```js
const findPidFromPort = require("find-pid-from-port")

const example = async () => {
try {
const pids = await findPidFromPort(8017)
console.log(pids.all)
//=> [1234, 5678]

console.log(pids.tcp)
//=> [1234]

console.log(pids.udp)
//=> [5678]
} catch (error) {
console.log(error)
//=> "Couldn't find a process with port `8017`"
}
}
example()
```

## API

### findPidFromPort(port)

#### port

Type: `number`

Port to lookup.
61 changes: 61 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const util = require("util")
const condense = require("selective-whitespace")
const execAsync = util.promisify(require("child_process").exec)

const isWindows = process.platform === "win32"

const command = (port) => {
if (isWindows) {
return `netstat.exe -a -n -o | findstr.exe :${port}`
}
return `lsof -i :${port}`
}

const netstats = async (port) => {
const cmd = command(port)
const { stdout } = await execAsync(cmd)
return stdout.split("\n")
}

const pushTo = (target, item) => {
if (item !== "" && typeof item === "number" && item !== 0 && target.indexOf(item) === -1) {
target.push(item)
}
}

const processNetStats = async (stats) => {
const pidIndex = 1
const pids = {
all: [],
tcp: [],
udp: [],
}
const items = isWindows ? stats : stats.slice(1)
for (const item of items) {
const values = condense(item).split(" ")
const pid = isWindows ? parseInt(values.pop(), 10) : parseInt(values[pidIndex], 10)
if (values.length > 1) {
if (values.indexOf("TCP") !== -1) {
pushTo(pids.tcp, pid)
pushTo(pids.all, pid)
} else if (values.indexOf("UDP") !== -1) {
pushTo(pids.udp, pid)
pushTo(pids.all, pid)
}
}
}
return pids
}

module.exports = async (port) => {
if (typeof port !== "number") {
throw new TypeError("Expected a port number")
}
try {
const stats = await netstats(port)
const pids = await processNetStats(stats)
return pids
} catch (error) {
throw new Error(`Couldn't find a process with port \`${port}\``)
}
}
Loading

0 comments on commit 6c0e057

Please sign in to comment.