Skip to content

Commit

Permalink
v0.0.6
Browse files Browse the repository at this point in the history
Misc refactors
  • Loading branch information
radiovisual committed Jun 9, 2019
2 parents 075db86 + 2547f32 commit 7daff3b
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 87 deletions.
7 changes: 2 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
indent_size = 2
12 changes: 12 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"plugins": ["jest"],
"extends": ["plugin:jest/recommended", "airbnb"],
"env": {
"jest/globals": true,
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2017
}
}
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
*.txt
*.txt
yarn.lock
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- 'stable'
- '0.12'
- '0.10'
- '10'
- '9'
- '8'
51 changes: 21 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
'use strict';
require('native-promise-only');
var exec = require('child_process').exec;
const util = require('util');
const exec = util.promisify(require('child_process').exec);

function command(port) {
var win = {
exe: '\\windows\\system32\\netstat.exe',
arg: ['-a -n -o ^| findstr :' + port],
cmd: '\\windows\\system32\\netstat.exe -a -n -o | findstr.exe :' + port
};
const win = {
exe: '\\windows\\system32\\netstat.exe',
arg: [`-a -n -o ^| findstr :${port}`],
cmd: `\\windows\\system32\\netstat.exe -a -n -o | findstr.exe :${port}`,
};

var dar = {
exe: 'lsof',
arg: ['-i', ':' + port],
cmd: 'lsof -i :' + port
};
const dar = {
exe: 'lsof',
arg: ['-i', `:${port}`],
cmd: `lsof -i :${port}`,
};

return process.platform === 'win32' ? win : dar;
return process.platform === 'win32' ? win : dar;
}

module.exports = function (port, opts) {
if (typeof port !== 'number') {
throw new TypeError('Expected a port number');
}
function netstats(port) {
if (typeof port !== 'number') {
throw new TypeError('Expected a port number');
}

opts = opts || {};
const cmd = command(port);

return new Promise(function (resolve, reject) {
var cmd = command(port);
return exec(cmd.cmd).then(({ stdout }) => Promise.resolve(stdout.split('\n'))).catch(err => Promise.reject(err));
}

exec(cmd.cmd, function (err, stdout, stderr) {
var _err = err || stderr;
if (_err) {
reject(_err);
}
resolve(stdout.split('\n'));
});
});
};
module.exports = netstats;
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) Michael Wuergler <wuergler@gmail.com> (numetriclabs.com)
Copyright (c) Michael Wuergler <wuergler@gmail.com> (https://github.com/radiovisual)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 14 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "netstats",
"version": "0.0.5",
"version": "0.0.6",
"description": "Get the netstat activity on a given port.",
"license": "MIT",
"repository": "radiovisual/netstats",
"author": {
"name": "Michael Wuergler",
"email": "wuergler@gmail.com",
"url": "numetriclabs.com"
"url": "https://github.com/radiovisual"
},
"engines": {
"node": ">=0.10.0"
"node": ">=8"
},
"scripts": {
"test": "xo && ava --verbose"
"test": "eslint index.js test.js && jest"
},
"files": [
"index.js"
Expand All @@ -28,21 +28,16 @@
"udp",
"activity"
],
"dependencies": {
"native-promise-only": "^0.8.1"
},
"dependencies": {},
"devDependencies": {
"ava": "^0.8.0",
"express": "^4.13.4",
"selective-whitespace": "^1.0.0",
"xo": "^0.12.1"
},
"ava": {
"verbose": true
},
"xo": {
"rules": {
"linebreak-style": 0
}
"eslint": "^5.3.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-jest": "^22.6.4",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.13.0",
"express": "^4.17.1",
"jest": "^24.8.0",
"selective-whitespace": "^1.0.0"
}
}
9 changes: 3 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# netstats
# netstats [![Build Status](https://travis-ci.org/radiovisual/netstats.svg?branch=master)](https://travis-ci.org/radiovisual/netstats)

> Get the netstat activity on a given port.
[![Build Status](https://travis-ci.org/radiovisual/netstats.svg?branch=master)](https://travis-ci.org/radiovisual/netstats)


## Install

```
Expand Down Expand Up @@ -56,11 +53,11 @@ Returns a promise carrying the output array of line items.

#### port

Type: `number`
Type: `number`<br/>
*Required*

The port number you are enquiring about.

## License

MIT © [Michael Wuergler](http://numetriclabs.com)
MIT © [Michael Wuergler](https://github.com/radiovisual)
46 changes: 24 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import test from 'ava';
import netstats from './';
import condense from 'selective-whitespace';
/* eslint prefer-destructuring: 0 */
const condense = require('selective-whitespace');

const server = require('express')();
const netstats = require('./index.js');

test.beforeEach(t => {
const listener = server.listen(0);
t.context.listener = listener;
t.context.port = listener.address().port;
console.log('running test server on port: ', t.context.port);
});
let listener;
let port;

test('gets the netstats', async t => {
await netstats(t.context.port).then(stats => {
t.plan(3);
describe('netstats', () => {
afterEach(() => {
listener.close();
});

const v = condense(stats[0]);
const value = v.split(' ')[0];
t.true(value === 'COMMAND' || value === 'TCP' || value === 'UDP');
t.true(stats.length > 0);
t.true(Array.isArray(stats));
});
});
beforeEach(() => {
listener = server.listen(0);
port = listener.address().port;
});

test('gets the netstats', () => {
expect.assertions(3);

test.afterEach(t => {
t.context.listener.close();
console.log('closing test server on port: ', t.context.port);
return netstats(port).then((stats) => {
const v = condense(stats[0]);
const value = v.split(' ')[0];
expect(value === 'COMMAND' || value === 'TCP' || value === 'UDP').toEqual(true);
expect(stats.length > 0).toEqual(true);
expect(Array.isArray(stats)).toEqual(true);
});
});
});

0 comments on commit 7daff3b

Please sign in to comment.