Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

healthchecks

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

healthchecks - npm Package Compare versions

Comparing version 1.4.4 to 1.4.5

112

lib/healthchecks.js

@@ -25,5 +25,5 @@ // Exports an Express middleware factory.

const Handlebars = require('handlebars');
const HTTP = require('http');
const ms = require('ms');
const Path = require('path');
const request = require('request');
const URL = require('url');

@@ -37,3 +37,3 @@

// Represents a check outcome with the properties url, reason, etc.
function Outcome(url, expected, error, response, body, elapsed) {
function Outcome(url, expected, error, statusCode, body, elapsed) {
this.url = url;

@@ -49,5 +49,5 @@ this.elapsed = elapsed;

this.statusCode = response.statusCode;
this.statusCode = statusCode;
this.body = body;
if (response.statusCode < 200 || response.statusCode >= 400)
if (statusCode < 200 || statusCode >= 400)
this.reason = 'statusCode';

@@ -64,4 +64,28 @@ else {

}
this.log();
}
// Log outcome, visible when DEBUG=healthchecks
Outcome.prototype.log = function() {
switch (this.reason) {
case 'error': {
debug('%s: Server responded with error %s', this.url, this.error);
break;
}
case 'timeout': {
debug('%s: Server response timeout', this.url);
break;
}
case 'statusCode': {
break;
}
case 'body': {
debug('%s: Server response did not contain expected text', this.url);
break;
}
}
};
// Easy way to log check outcome, shows the URL and the reason check failed

@@ -77,2 +101,3 @@ Outcome.prototype.toString = function() {

case undefined: {
debug('%s: Server responded with status code %s', this.url, this.statusCode);
return this.url;

@@ -112,44 +137,46 @@ }

checkURL = URL.resolve(protocol + '://localhost/', checkURL);
const requestURI = _.assign(URL.parse(checkURL), { host: null, port: port, hostname: hostname });
const headers = {
'host': URL.parse(checkURL).hostname,
'user-agent': 'Mozilla/5.0 (compatible) Healthchecks http://broadly.com'
const requestURL = _.assign(URL.parse(checkURL), { host: null, port: port, hostname: hostname });
const request = {
hostname: requestURL.hostname,
port: requestURL.port,
path: requestURL.path,
headers: {
'Host': URL.parse(checkURL).hostname,
'User-Agent': 'Mozilla/5.0 (compatible) Healthchecks http://broadly.com',
'X-Request-Id': requestID || ''
}
};
if (requestID)
headers['X-Request-Id'] = requestID;
const params = {
uri: URL.format(requestURI),
headers: headers,
timeout: timeout
};
const start = Date.now();
request(params, function(error, response, body) {
HTTP.get(request)
.on('response', function(response) {
const elapsed = Date.now() - start;
if (response.statusCode < 200 || response.statusCode >= 400)
resolve(new Outcome(checkURL, expected, null, response.statusCode, null, elapsed));
else {
const buffers = [];
response.on('data', function(buffer) {
buffers.push(buffer);
});
response.on('end', function() {
const body = Buffer.concat(buffers).toString();
resolve(new Outcome(checkURL, expected, null, response.statusCode, body, elapsed));
});
}
const elapsed = Date.now() - start;
const outcome = new Outcome(checkURL, expected, error, response, body, elapsed);
resolve(outcome);
})
.on('error', function(error) {
const elapsed = Date.now() - start;
resolve(new Outcome(checkURL, expected, error, null, null, elapsed));
});
switch (outcome.reason) {
case 'error': {
debug('Server responded with error ' + error.message, checkURL);
break;
}
case 'timeout': {
debug('Server response timeout', checkURL);
break;
}
case 'statusCode': {
debug('Server responded with status code ' + response.statusCode, checkURL);
break;
}
case 'body': {
debug('Server response did not contain expected text', checkURL);
break;
}
}
});
setTimeout(function() {
const elapsed = Date.now() - start;
const error = new Error('ETIMEDOUT');
error.code = 'ETIMEDOUT';
resolve(new Outcome(checkURL, expected, error, null, null, elapsed));
}, timeout);
});
});
// Run all checks in parallel

@@ -190,3 +217,3 @@ const allOutcomes = Promise.all(allChecks);

const match = line.match(/^(\S+)\s*(.*)/);
debug('Added check', match[1], match[2]);
//debug('Added check %s %s', match[1], match[2]);
return {

@@ -217,2 +244,3 @@ url: match[1],

};
debug('Added %d checks', checks.length);

@@ -259,6 +287,6 @@ return checkFunction.bind(context);

// Run all checks
debug('Health checks: running against ' + protocol + '://' + hostname + ':' + port + ' with request-ID ' + requestID);
debug('Running against %s://%s:%d with request-ID %s', protocol, hostname, port, requestID);
runChecks(protocol, hostname, port, requestID)
.then(function(outcomes) {
debug('Health checks: ' + outcomes.passed.length + ' passed and ' + outcomes.failed.length + ' failed');
debug('%d passed and %d failed', outcomes.passed.length, outcomes.failed.length);

@@ -265,0 +293,0 @@ // Respond with 200 only if all checks passed

{
"name": "healthchecks",
"version": "1.4.4",
"version": "1.4.5",
"scripts": {

@@ -10,13 +10,13 @@ "test": "mocha"

"bluebird": "^2.0",
"debug": "^2.0",
"debug": "^2.2.0",
"handlebars": "^3.0",
"lodash": "^3.0",
"ms": "^0.7.0",
"request": "^2.0"
"ms": "^0.7.1"
},
"devDependencies": {
"eslint": "^0.19.0",
"eslint": "^0.21.0",
"express": "^4.12.3",
"mocha": "^2.2.4",
"zombie": "^4.0.7"
"request": "^2.0",
"zombie": "^4.0.10"
},

@@ -23,0 +23,0 @@ "description": "Express middleware that runs health checks on your application",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc