Comparing version 1.0.0 to 1.1.0
'use strict'; | ||
const EventEmitter = require('events'); | ||
const { promisify } = require('util'); | ||
const extIp = require('external-ip'); | ||
module.exports = class Watcher extends EventEmitter { | ||
constructor(config = {}) { | ||
static isPositiveInteger(value) { | ||
return Number.isInteger(value) && value > 0; | ||
} | ||
constructor(userConfig = {}) { | ||
super(); | ||
config.pollingInterval = config.pollingInterval || 8.64e+7; //One day | ||
if (!this.isPositiveInteger(config.pollingInterval)) { | ||
throw new Error('Invalid polling pollingInterval'); | ||
const defaultConfig = { | ||
pollingInterval: 8.64e+7, //One day | ||
verbose: false, | ||
externalIp: {} | ||
}; | ||
// Merge user provided config with defaults | ||
const config = Object.assign(defaultConfig, userConfig); | ||
// Check if interval time is valid | ||
if (!Watcher.isPositiveInteger(config.pollingInterval)) { | ||
throw new Error('pollingInterval must be a possitive integer'); | ||
} | ||
this.pollingInterval = config.pollingInterval; | ||
this.getIP = promisify(extIp(config.externalIp)); | ||
this.log = config.verbose ? console.log.bind(console, '[info]: ') : () => {}; | ||
this.getIP = extIp(config.externalIp); | ||
this.log = config.verbose ? console.log.bind(console, '[ip-monitor]: ') : () => {}; | ||
this.previousIp = null; | ||
this.started = false; | ||
this.timeoutId = null; | ||
} | ||
isPositiveInteger(value) { | ||
return Number.isInteger(value) && value > 0; | ||
} | ||
resolveAfterTime(time) { | ||
this.log(`Waiting for ${this.pollingInterval} ms`); | ||
return new Promise(resolve => { | ||
// .stop has been invoked already, bail out | ||
poll() { | ||
this.getIP((error, ip) => { | ||
// .stop has been invoked after poll() but before this callback, bail out | ||
if (!this.started) { | ||
return resolve(); | ||
return; | ||
} | ||
const timeoutId = setTimeout(() => { | ||
// .stop has never been invoked, clean up the listener. | ||
this.removeListener('_cancel', cancel); | ||
resolve(); | ||
}, time); | ||
// Prepare to poll again, this must be done before emitting any events | ||
// otherwise the timeout will not be cleared if .stop is called inside the event handler | ||
this.timeoutId = setTimeout(this.poll.bind(this), this.pollingInterval); | ||
// .stop has been invoked and the timeout has not finished. resolve immediately | ||
const cancel = () => { | ||
clearTimeout(timeoutId); | ||
resolve(); | ||
}; | ||
// If an error has been encountered, emit it but do not stop polling | ||
if (error) { | ||
this.emit('error', error); | ||
} | ||
this.once('_cancel', cancel); | ||
}); | ||
} | ||
async poll() { | ||
try { | ||
const ip = await this.getIP(); | ||
// Got an IP, awesome, emit the change event if required | ||
this.log('Got ip:', ip); | ||
@@ -59,8 +58,7 @@ if (ip !== this.previousIp) { | ||
} | ||
} catch (error) { | ||
this.emit('error', error); | ||
} | ||
}); | ||
} | ||
async start() { | ||
start() { | ||
if (this.started) { | ||
@@ -71,13 +69,11 @@ return this.emit('error', 'Watcher already started'); | ||
this.started = true; | ||
while (this.started) { | ||
await this.poll(); | ||
await this.resolveAfterTime(this.pollingInterval); | ||
} | ||
this.poll(); | ||
} | ||
stop() { | ||
this.log('stopping'); | ||
this.started = false; | ||
this.emit('_cancel'); | ||
clearTimeout(this.timeoutId); | ||
} | ||
}; |
{ | ||
"name": "ip-monitor", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A node.js library to monitor your external ip", | ||
@@ -28,3 +28,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"external-ip": "^2.0.3" | ||
"external-ip": "^2.3.1" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
@@ -17,3 +17,3 @@ 'use strict'; | ||
const ipMonitor = new IpMonitor(); | ||
const ipMonitor = new IpMonitor({verbose: true}); | ||
@@ -20,0 +20,0 @@ ipMonitor.on('change', (previousIp, newIp) => { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8114
1
166
Updatedexternal-ip@^2.3.1