Skip to content

Commit

Permalink
Merge cea7031 into ce0ecf7
Browse files Browse the repository at this point in the history
  • Loading branch information
dickydoouk committed Jan 29, 2021
2 parents ce0ecf7 + cea7031 commit 7f65124
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 7 additions & 0 deletions __tests__/index.js
Expand Up @@ -29,6 +29,8 @@ describe('local-devices', () => {
})
})

afterEach(() => cp.exec.mockClear())

it('returns the result of all IPs', async () => {
const result = await find()
expect(result).toEqual([
Expand Down Expand Up @@ -87,6 +89,11 @@ describe('local-devices', () => {
expect(cp.exec).toHaveBeenCalledWith('arp -a', { maxBuffer: TEN_MEGA_BYTE, timeout: ONE_MINUTE })
})

it('invokes cp.exec with maxBuffer of 10 MB and a timeout of 1 minute, when invoking find without an ip and skip name resolution', async () => {
await find(null, true)
expect(cp.exec).toHaveBeenCalledWith('arp -an', { maxBuffer: TEN_MEGA_BYTE, timeout: ONE_MINUTE })
})

it('invokes cp.exec with maxBuffer of 10 MB and a timeout of 1 minute, when invoking find with a single ip', async () => {
await find('192.168.0.242')
expect(cp.exec).toHaveBeenCalledWith('arp -n 192.168.0.242', { maxBuffer: TEN_MEGA_BYTE, timeout: ONE_MINUTE })
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -75,6 +75,8 @@
"describe",
"beforeAll",
"afterAll",
"beforeEach",
"afterEach",
"it",
"expect"
]
Expand Down
13 changes: 7 additions & 6 deletions src/index.js
Expand Up @@ -19,9 +19,9 @@ const options = {
}

/**
* Finds all local devices (ip and mac address) connectd to the current network.
* Finds all local devices (ip and mac address) connected to the current network.
*/
module.exports = function findLocalDevices (address) {
module.exports = function findLocalDevices (address, skipNameResolution = false) {
var key = String(address)

if (isRange(address)) {
Expand All @@ -38,7 +38,7 @@ module.exports = function findLocalDevices (address) {

if (!lock[key]) {
if (!address || isRange(key)) {
lock[key] = pingServers().then(arpAll).then(unlock(key))
lock[key] = pingServers().then((servers) => arpAll(servers, skipNameResolution)).then(unlock(key))
} else {
lock[key] = pingServer(address).then(arpOne).then(unlock(key))
}
Expand Down Expand Up @@ -82,7 +82,7 @@ function pingServers () {
}

/**
* Pings and individual server to update the arp table.
* Pings an individual server to update the arp table.
*/
function pingServer (address) {
return new Promise(function (resolve) {
Expand All @@ -101,8 +101,9 @@ function pingServer (address) {
/**
* Reads the arp table.
*/
function arpAll () {
return cp.exec('arp -a', options).then(parseAll)
function arpAll (_, skipNameResolution = false) {
const cmd = skipNameResolution ? 'arp -an' : 'arp -a'
return cp.exec(cmd, options).then(parseAll)
}

/**
Expand Down

0 comments on commit 7f65124

Please sign in to comment.