Comparing version 0.0.1 to 0.1.0
'use strict'; | ||
var ipMon = require('../index'); | ||
var watcher = ipMon.createWatcher({/*config*/}); | ||
var watcher = ipMon.createWatcher(); | ||
watcher.on('ipChanged', function (prevIP, newIP) { | ||
watcher.on('IP:change', function (prevIP, newIP) { | ||
console.log('Prev IP: %s, New IP: %s', prevIP, newIP); | ||
}); | ||
// Handle errors better. | ||
/* | ||
Generic error event | ||
*/ | ||
watcher.on('error', function (error) { | ||
//throw new Error(error); | ||
console.log('Error: ' + error); | ||
throw error; | ||
}); | ||
watcher.on('start', function (IP) { | ||
console.log('Starting... IP: %s', IP); | ||
setTimeout(function () { | ||
//watcher.stop(); | ||
}, 10000); | ||
/* | ||
Seperate event for ip error handling. | ||
It will fire when the connection has been lost, e.g your router is restarting, | ||
thats why you may want to handle it differently than other errors. | ||
*/ | ||
watcher.on('IP:error', function (error) { | ||
console.log('Cant get external IP: ' + error); | ||
}); | ||
watcher.on('stop', function () { | ||
console.log('Stopping...'); | ||
watcher.on('IP:success', function (IP) { | ||
console.log('Got IP: %s', IP); | ||
}); | ||
// will trigger internal check too. | ||
watcher.getIP(function (IP) { | ||
console.log('Le IP iz: %s', IP); | ||
}); | ||
watcher.start(); | ||
watcher.start(); |
@@ -9,2 +9,3 @@ 'use strict'; | ||
module.exports.createWatcher = function (extConfig) { | ||
extConfig = extConfig || {}; | ||
var isValid = utils.validateConfig(extConfig); | ||
@@ -11,0 +12,0 @@ if(isValid.errors.length) { |
@@ -10,4 +10,2 @@ 'use strict'; | ||
this.config = config; | ||
this._getIP = getIP; | ||
this.timeout = null; | ||
@@ -17,2 +15,24 @@ this.prevIP = null; | ||
this.poll = function () { | ||
getIP(function (err, IP) { | ||
if (this.isWatching()) { | ||
this.timeout = setTimeout(function () { | ||
this.poll(); | ||
}.bind(this), this.config.polling); | ||
} | ||
return this.emit.apply(this, err ? ['IP:error', err] : ['IP:success', IP]); | ||
}.bind(this)); | ||
}; | ||
this.on('IP:success', function (IP) { | ||
if (IP !== this.IP) { | ||
this.prevIP = this.IP; | ||
this.IP = IP; | ||
this.emit('IP:change', this.prevIP, this.IP); | ||
} | ||
}.bind(this)); | ||
}; | ||
@@ -24,50 +44,27 @@ | ||
W._checkNewIP = function (IP) { | ||
//console.log('Checking: %s', IP); | ||
if (IP && IP !== this.IP) { | ||
this.prevIP = this.IP; | ||
this.IP = IP; | ||
this.emit('ipChanged', this.prevIP, this.IP); | ||
} | ||
W.isWatching = function () { | ||
return !!this.timeout; | ||
}; | ||
// When half of a function is 'this.' Kill it with fire! | ||
W._watch = function () { | ||
this.timeout = setTimeout(function () { | ||
this.getIP(this._watch.bind(this)); | ||
}.bind(this), this.config.polling); | ||
}; | ||
W.start = function () { | ||
if (this.timeout) { | ||
this.emit('error', 'Already started'); | ||
if (this.isWatching()) { | ||
return this.emit('error', 'Already started'); | ||
} | ||
this.getIP(function (IP) { | ||
this.emit('start', IP); | ||
this._watch(); | ||
}.bind(this)); | ||
this.timeout = true; | ||
this.poll(); | ||
return this; | ||
}; | ||
W.stop = function () { | ||
if (!this.timeout) { | ||
this.emit('error', 'Not started'); | ||
} else { | ||
clearTimeout(this.timeout); | ||
this.timeout = null; | ||
this.emit('stop'); | ||
if (!this.isWatching()) { | ||
return this.emit('error', 'Not started'); | ||
} | ||
clearTimeout(this.timeout); | ||
this.timeout = null; | ||
return this; | ||
}; | ||
W.getIP = function (cb) { | ||
this._getIP(function (err, IP) { | ||
if (err) { | ||
this.emit('error', err); | ||
} | ||
this._checkNewIP(IP); | ||
cb(IP); | ||
}.bind(this)); | ||
}; | ||
module.exports.Watcher = Watcher; |
{ | ||
"name": "ip-monitor", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "A node.js library to monitor your external ip", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node node_modules/mocha/bin/mocha" | ||
}, | ||
@@ -28,5 +28,9 @@ "repository": { | ||
"dependencies": { | ||
"external-ip": "^0.1.2", | ||
"external-ip": "^0.2.0", | ||
"revalidator": "^0.2.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^1.9.1", | ||
"mocha": "^1.21.4" | ||
} | ||
} |
@@ -1,42 +0,78 @@ | ||
ip-monitor | ||
========== | ||
#ip-monitor [![Build Status](https://travis-ci.org/J-Chaniotis/ip-monitor.svg)](https://travis-ci.org/J-Chaniotis/ip-monitor) [![Dependency Status](https://david-dm.org/j-Chaniotis/ip-monitor.svg)](https://david-dm.org/j-Chaniotis/ip-monitor) | ||
A node.js library to monitor your external ip for changes | ||
check examples/basic.js for an alpha implementation | ||
This is a work in progress. | ||
Probably its going to look like this | ||
## Installation | ||
`npm install ip-monitor` | ||
## Usage | ||
basic | ||
```javascript | ||
'use strict'; | ||
var ipMon = require('ip-monitor'); | ||
var watcher = ipMon.createWatcher({ | ||
interval: 60*1000, | ||
externalIP: {/*Config passed to external-ip*/} | ||
}); | ||
var watcher = ipMon.createWatcher(); | ||
watcher.on('ipChanged', function (prevIP, newIP) { | ||
watcher.on('IP:change', function (prevIP, newIP) { | ||
console.log('Prev IP: %s, New IP: %s', prevIP, newIP); | ||
}); | ||
/* | ||
Generic error event | ||
*/ | ||
watcher.on('error', function (error) { | ||
throw error; | ||
}); | ||
/* | ||
Seperate event for ip error handling. | ||
It will fire when the connection has been lost, e.g your router is restarting, | ||
thats why you may want to handle it differently than regular errors. | ||
*/ | ||
watcher.on('IP:error', function (error) { | ||
console.log('Cant get external IP: ' + error); | ||
}); | ||
watcher.on('start', function (ip) { | ||
watcher.on('IP:success', function (IP) { | ||
console.log('Got IP: %s', IP); | ||
}); | ||
watcher.on('stop', function () { | ||
}); | ||
watcher.start(); | ||
watcher.getIP(function (error, ip) { | ||
``` | ||
with custom configuration | ||
```javascript | ||
var watcher = ipMon.createWatcher({ | ||
polling: 10000, | ||
externalIP: { | ||
timeout: 1000, | ||
getIP: 'parallel', | ||
services: ['http://ifconfig.co/x-real-ip', 'http://icanhazip.com/'], | ||
replace: true | ||
} | ||
}); | ||
``` | ||
##API | ||
watcher.start(); | ||
### Configuration | ||
`ipmon.createWatcher([config])` accepts a configuration object with the following optional properties: | ||
* <b>`polling: <Integer>`:</b> how often to poll for ip changes, default 20000ms | ||
* <b>`externalIP: <Object>`:</b> configuration passed directly to [`external-ip`](https://github.com/J-Chaniotis/external-ip/blob/master/README.md) | ||
watcher.stop(); | ||
``` | ||
### Events | ||
//todo | ||
* <b>`IP:success` :</b> | ||
* <b>`IP:change`:</b> | ||
* <b>`IP:error`:</b> | ||
### Methods | ||
* <b>`.start()`:</b> start watching | ||
* <b>`.stop()`:</b> stop watching | ||
* <b>`.poll()`:</b> poll for ip manually | ||
* <b>`.isWatching()`:</b> check if ip-monitor has started |
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
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
12801
12
281
1
78
2
1
+ Addedcommander@2.20.3(transitive)
+ Addedevented-async-loop@0.1.1(transitive)
+ Addedexternal-ip@0.2.4(transitive)
+ Addedrevalidator@0.3.1(transitive)
- Removedexternal-ip@0.1.5(transitive)
Updatedexternal-ip@^0.2.0