Skip to content

Commit

Permalink
Merge pull request #5 from martinheidegger/fix/issue_4
Browse files Browse the repository at this point in the history
fix: collecting addresses needs to be a class-method
  • Loading branch information
martinheidegger authored Apr 2, 2019
2 parents afefd12 + ffb11a7 commit 87509af
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,6 @@ function keyForInfo (interfaceId, info) {
])
}

function * collectAddresses (keys, interfaceId, infos) {
for (const info of infos) {
const key = keyForInfo(interfaceId, info)
const entry = {
...info,
key,
hash: objectHash(info),
interfaceId
}
if (keys.has(key)) {
this.emit('warning', Object.assign(new Error(`Multiple network addresses with same key detected ${key}`), {
a: keys.get(key),
b: entry,
interfaceId
}))
continue
}
keys.set(key, entry)
yield entry
}
}

function * collectInterfaces () {
const keys = new Map()
const rawInterfaces = os.networkInterfaces()
for (const interfaceId in rawInterfaces) {
yield {
interfaceId,
addresses: collectAddresses(keys, interfaceId, rawInterfaces[interfaceId])
}
}
}

function createNetworkInfo (interfaceId, activeInterfaceId, nicTypes) {
const info = {
id: interfaceId,
Expand Down Expand Up @@ -119,6 +86,39 @@ class NetworkInterfaces extends EventEmitter {
})
}

* _collectAddresses (keys, interfaceId, infos) {
for (const info of infos) {
const key = keyForInfo(interfaceId, info)
const entry = {
...info,
key,
hash: objectHash(info),
interfaceId
}
if (keys.has(key)) {
this.emit('warning', Object.assign(new Error(`Multiple network addresses with same key detected ${key}`), {
a: keys.get(key),
b: entry,
interfaceId
}))
continue
}
keys.set(key, entry)
yield entry
}
}

* _collectInterfaces () {
const keys = new Map()
const rawInterfaces = os.networkInterfaces()
for (const interfaceId in rawInterfaces) {
yield {
interfaceId,
addresses: this._collectAddresses(keys, interfaceId, rawInterfaces[interfaceId])
}
}
}

get maxAge () {
return this._maxAge
}
Expand Down Expand Up @@ -198,7 +198,7 @@ class NetworkInterfaces extends EventEmitter {

_update () {
const deletedInterfaces = new Set(this._interfaces.keys())
for (const { interfaceId, addresses } of collectInterfaces()) {
for (const { interfaceId, addresses } of this._collectInterfaces()) {
deletedInterfaces.delete(interfaceId)

const networkInterface = this._interfaces.get(interfaceId)
Expand Down

0 comments on commit 87509af

Please sign in to comment.