-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Misc refactors
- Loading branch information
Showing
11 changed files
with
85 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
* text=auto | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
*.txt | ||
*.txt | ||
yarn.lock | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |