Skip to content

Commit

Permalink
Merge 369cdf7 into ce0ecf7
Browse files Browse the repository at this point in the history
  • Loading branch information
dickydoouk committed Feb 3, 2021
2 parents ce0ecf7 + 369cdf7 commit d5e6416
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -76,6 +76,18 @@ find('192.168.0.0/24').then(devices => {
]
*/
})

// Find all devices without resolving host names - this is more performant if hostnames are not needed
find(null, true).then(devices => {
devices /*
[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '?', ip: '192.168.0.50', mac: '...' },
{ name: '?', ip: '192.168.0.155', mac: '...' },
{ name: '?', ip: '192.168.0.211', mac: '...' }
]
*/
})
```

## Contributions
Expand Down
17 changes: 17 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 All @@ -40,6 +42,12 @@ describe('local-devices', () => {
])
})

it('returns empty list if empty response returned', async () => {
cp.exec.mockImplementationOnce(_ => Promise.resolve())
const result = await find()
expect(result).toEqual([])
})

it('returns all IPs within /24 range', async () => {
const result = await find('192.168.1.0/24')
expect(result).toEqual([
Expand Down Expand Up @@ -87,6 +95,15 @@ 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)
if (process.platform.includes('win32')) {
expect(cp.exec).toHaveBeenCalledWith('arp -a', { maxBuffer: TEN_MEGA_BYTE, timeout: ONE_MINUTE })
} else {
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
14 changes: 8 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,10 @@ function pingServer (address) {
/**
* Reads the arp table.
*/
function arpAll () {
return cp.exec('arp -a', options).then(parseAll)
function arpAll (_, skipNameResolution = false) {
const isWindows = process.platform.includes('win32')
const cmd = (skipNameResolution && !isWindows) ? 'arp -an' : 'arp -a'
return cp.exec(cmd, options).then(parseAll)
}

/**
Expand Down

0 comments on commit d5e6416

Please sign in to comment.