Comparing version 2.0.0-beta.6 to 2.0.0
@@ -0,1 +1,2 @@ | ||
'use strict' | ||
const Ping = require('../').Ping | ||
@@ -6,3 +7,5 @@ | ||
path:'/', | ||
timeout: 20 | ||
port: 443, | ||
// timeout: 20, | ||
useHttps: true | ||
} | ||
@@ -15,2 +18,3 @@ | ||
const ping = new Ping() | ||
ping.start(config, expectResponse, (err, diff) => { | ||
@@ -17,0 +21,0 @@ if (err) { |
@@ -0,1 +1,2 @@ | ||
'use strict' | ||
const probe = require('../') | ||
@@ -2,0 +3,0 @@ |
14
index.js
@@ -1,2 +0,3 @@ | ||
const schedule = require('./lib/recurring') | ||
'use strict' | ||
const Scheduler = require('./lib/recurring').Scheduler | ||
const EventEmitter = require('events') | ||
@@ -12,3 +13,2 @@ | ||
} | ||
this.schedule = schedule | ||
} | ||
@@ -19,8 +19,13 @@ | ||
super() | ||
patchEmitter(schedule, this) | ||
this.schedule = new Scheduler() | ||
patchEmitter(this.schedule, this) | ||
} | ||
add (config, expects) { | ||
schedule.add(config, expects) | ||
this.schedule.add(config, expects) | ||
} | ||
abort () { | ||
this.schedule.abort() | ||
} | ||
} | ||
@@ -34,1 +39,2 @@ | ||
module.exports.TimeRange = require('./lib/recurring').TimeRange | ||
module.exports.HTTPRequest = require('./lib/request') |
139
lib/ping.js
@@ -0,4 +1,6 @@ | ||
'use strict' | ||
const https= require('https') | ||
const http = require('http') | ||
const url = require('url') | ||
const Req = require('./request') | ||
@@ -9,139 +11,24 @@ class Ping { | ||
this.endTime = null | ||
this.req = null | ||
this.req = new Req() | ||
} | ||
start (target, expects, cb) { | ||
const self = this | ||
if (target.useHttps === true) { | ||
self.makeFallbackRequest(target, expects, true, (err) => { | ||
// when the user has actually used the wrong protocol or there was a | ||
// protocol redirect. May easily happen when one does not use 80 or 443 | ||
// as port, e.g. 22 or any other random | ||
if (err === 'EPROTO') { | ||
this.makeFallbackRequest(target, expects, false, (err) => { | ||
if (err) { | ||
return cb(err, self.end()) | ||
} | ||
return cb(null, self.end()) | ||
}) | ||
} else if (err) { | ||
return cb(err) | ||
} else { | ||
return cb(null, self.end()) | ||
} | ||
}) | ||
} else if (target.useHttps === false || target.useHttps !== undefined) { | ||
self.makeFallbackRequest(target, expects, false, cb) | ||
} else { | ||
self.makeFallbackRequest(target, expects, true, (err) => { | ||
if (err === 'ECONNREFUSED') { | ||
this.makeFallbackRequest(target, expects, false, (err) => { | ||
if (err) { | ||
return cb(err, self.end()) | ||
} | ||
return cb(null, self.end()) | ||
}) | ||
} else if (err) { | ||
return cb(err) | ||
} else { | ||
return cb(null, self.end()) | ||
} | ||
}) | ||
} | ||
} | ||
this.startTime = Date.now() | ||
expects (res, expects) { | ||
if (expects.code) { | ||
return res.statusCode === expects.code | ||
} else { | ||
return false | ||
} | ||
} | ||
end () { | ||
this.endTime = Date.now() | ||
return this.endTime - this.startTime | ||
} | ||
makeFallbackRequest (target, expects, useHttps, cb) { | ||
const self = this | ||
let hasCalled = false | ||
const options = { | ||
'method': 'GET', | ||
'hostname': target.host, | ||
'port': target.port || null, | ||
'path': target.path || '/', | ||
'headers': { | ||
'cache-control': 'no-cache', | ||
this.req.start(target, (err) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
} | ||
function reqCb (res) { | ||
if (res.statusCode > 300 && res.statusCode < 400 && res.headers.location) { | ||
// The location for some (most) redirects will only contain the path, not the hostname; | ||
// detect this and add the host to the path. | ||
if (url.parse(res.headers.location).hostname) { | ||
// Hostname included; make request to res.headers.location | ||
target.path = res.headers.location | ||
self.makeFallbackRequest(target, expects, useHttps, cb) | ||
} else { | ||
const err = new Error('didn\'t know how to handle this case') | ||
// Hostname not included; get host from requested URL (url.parse()) and prepend to location. | ||
if (!hasCalled) { | ||
hasCalled = true | ||
return cb(err) | ||
} | ||
} | ||
// Otherwise no redirect; capture the response as normal | ||
} else { | ||
// ignore actual data | ||
// | ||
// let chunks = [] | ||
// | ||
// res.on('data', function (chunk) { | ||
// chunks.push(chunk) | ||
// }) | ||
res.on('error', (err) => { | ||
if (!hasCalled) { | ||
hasCalled = true | ||
return cb(err) | ||
} | ||
}) | ||
res.on('end', () => { | ||
if (!hasCalled) { | ||
hasCalled = true | ||
if (self.expects(res, expects)) { | ||
return cb(null, self.end()) | ||
} else { | ||
return cb(new Error('not healthy according to expecation'), self.end()) | ||
} | ||
} | ||
}) | ||
if (!this.req.expect(expects)) { | ||
return cb(new Error('did not match expectation')) | ||
} | ||
} | ||
if (useHttps === true) { | ||
self.req = https.request(options, reqCb) | ||
} else { | ||
self.req = http.request(options, reqCb) | ||
} | ||
self.req.on('error', (err) => { | ||
if (!hasCalled) { | ||
hasCalled = true | ||
return cb(err.code) | ||
} | ||
return cb(null, this.end()) | ||
}) | ||
self.startTime = Date.now() | ||
self.req.end() | ||
} | ||
destroy () { | ||
this.req.removeAllListeners('error') | ||
this.req.abort() | ||
end () { | ||
this.endTime = Date.now() | ||
return this.endTime - this.startTime | ||
} | ||
@@ -148,0 +35,0 @@ } |
@@ -0,1 +1,2 @@ | ||
'use strict' | ||
const EventEmitter = require('events') | ||
@@ -64,2 +65,13 @@ const schedule = require('node-schedule') | ||
} | ||
abort () { | ||
if (!this.jobs.length) { | ||
return | ||
} | ||
for (var i = 0; i < this.jobs.length; i++) { | ||
this.jobs[i]._job.cancel() | ||
} | ||
return | ||
} | ||
} | ||
@@ -66,0 +78,0 @@ |
{ | ||
"name": "probe", | ||
"version": "2.0.0-beta.6", | ||
"version": "2.0.0", | ||
"description": "Ping and probe stuff.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape 'test/**/*.spec.js'" | ||
"test": "tape 'test/**/*.spec.js'", | ||
"postversion": "TAG=$(echo $(git describe --tags $(git rev-list --tags --max-count=1))) && git tag -a $TAG $TAG -f -m \"$(git log `git describe --tags --abbrev=0 HEAD^`..HEAD --oneline)\" && git push && git push --tags" | ||
}, | ||
@@ -27,2 +28,3 @@ "repository": { | ||
"nodemon": "^1.11.0", | ||
"steed": "^1.1.3", | ||
"tape": "^4.6.3" | ||
@@ -29,0 +31,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# Probe | ||
# Probe [![Build Status](https://travis-ci.org/eljefedelrodeodeljefe/probe.svg?branch=master)](https://travis-ci.org/eljefedelrodeodeljefe/probe) | ||
@@ -3,0 +3,0 @@ > Ping and probe stuff |
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12716
14
468
0
3
5