Comparing version 0.1.2 to 1.0.0
@@ -1,1 +0,3 @@ | ||
module.exports = require('./lib'); | ||
'use strict'; | ||
module.exports = require('./lib'); |
'use strict'; | ||
var OS = require('os'); | ||
var Joi = require('joi'); | ||
var Hoek = require('hoek'); | ||
const OS = require('os'); | ||
const Joi = require('joi'); | ||
const Hoek = require('hoek'); | ||
const name = 'hapi-ding'; | ||
var internals = { | ||
const internals = { | ||
defaults: { | ||
@@ -13,2 +14,3 @@ path: '/ding', | ||
}, | ||
options: Joi.object({ | ||
@@ -22,17 +24,20 @@ path: Joi.string().optional(), | ||
exports.register = function(plugin, options, next){ | ||
var validateOptions = internals.options.validate(options); | ||
const register = function (server, options) { | ||
const validateOptions = internals.options.validate(options); | ||
if (validateOptions.error) { | ||
return next(validateOptions.error); | ||
throw validateOptions.error; | ||
} | ||
var settings = Hoek.clone(internals.defaults); | ||
const settings = Hoek.clone(internals.defaults); | ||
Hoek.merge(settings, options); | ||
plugin.route({ | ||
server.route({ | ||
method: 'GET', | ||
path: settings.path, | ||
config: settings.config, | ||
handler: function(req, reply) { | ||
var res = { | ||
handler (request, h) { | ||
const { otherData, objectName } = settings; | ||
const stats = { | ||
cpu: OS.loadavg(), | ||
@@ -43,26 +48,28 @@ mem: OS.freemem(), | ||
if(req.server.settings.load.sampleInterval) { | ||
res.heap = req.server.load.heapUsed; | ||
res.loop = req.server.load.eventLoopDelay; | ||
} | ||
const { server: { settings: { load } } } = request; | ||
const { sampleInterval, heapUsed, eventLoopDelay } = load; | ||
if(settings.otherData) { | ||
Hoek.merge(res, settings.otherData, false); | ||
if (sampleInterval) { | ||
stats.heap = heapUsed; | ||
stats.loop = eventLoopDelay; | ||
} | ||
if (otherData) { | ||
Hoek.merge(stats, otherData, false); | ||
} | ||
if(settings.objectName) { | ||
var res2 = {}; | ||
res2[settings.objectName] = res; | ||
res = res2; | ||
if (objectName) { | ||
return h.response({ [objectName]: stats }); | ||
} | ||
reply(res); | ||
return h.response(stats); | ||
} | ||
}); | ||
next(); | ||
}; | ||
exports.register.attributes = { | ||
pkg: require('../package.json') | ||
}; | ||
exports.plugin = { | ||
register, | ||
name, | ||
pkg: require('../package.json'), | ||
version: process.env.npm_package_version | ||
} | ||
; |
{ | ||
"name": "hapi-ding", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "Informational status page for Hapi apps", | ||
@@ -8,17 +8,36 @@ "main": "index.js", | ||
"test": "lab -r console -t 100 -a code", | ||
"test-ci": "lab -r lcov -t 100 -a code | ./node_modules/.bin/coveralls" | ||
"test-ci": "lab -r lcov -t 100 -a code | ./node_modules/.bin/coveralls", | ||
"lint": "eslint --fix ." | ||
}, | ||
"repository": "git://github.com/dialexa/hapi-ding", | ||
"author": "Powell Kinney <powell@dialexa.com>", | ||
"contributors": [ | ||
"Ehis Ojielu <ehis@dialexa.com>" | ||
], | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=8.9.0" | ||
}, | ||
"dependencies": { | ||
"hoek": "^2.11.1", | ||
"joi": "^6.0.8" | ||
"hoek": "^5.0.2", | ||
"joi": "^13.0.2" | ||
}, | ||
"devDependencies": { | ||
"code": "^1.3.0", | ||
"coveralls": "^2.11.2", | ||
"hapi": "^8.4.0", | ||
"lab": "^5.5.0" | ||
"@dialexa/eslint-config-dialexa": "^4.1.1", | ||
"code": "^5.1.2", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.11.0", | ||
"hapi": "^17.0.2", | ||
"lab": "^15.1.2" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@dialexa/eslint-config-dialexa/node-es6", | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 8 | ||
} | ||
} | ||
} |
@@ -9,13 +9,11 @@ Ding Route Plugin for Hapi | ||
This plugin works with Hapi 8 and above. | ||
This plugin works with Hapi 17 and above. | ||
```javascript | ||
var Hapi = require('hapi'); | ||
const Hapi = require('hapi'); | ||
var server = new Hapi.Server({ | ||
const server = Hapi.Server({ | ||
load: { | ||
sampleInterval: 1000 | ||
} | ||
}); | ||
server.connection({ | ||
}, | ||
host: 'localhost', | ||
@@ -25,6 +23,4 @@ port: 8000 | ||
server.register({ | ||
register: require('hapi-ding') | ||
}, function(){ | ||
server.start(); | ||
await server.register({ | ||
plugin: require('hapi-ding') | ||
}); | ||
@@ -31,0 +27,0 @@ ``` |
@@ -1,185 +0,157 @@ | ||
var Lab = require('lab'); | ||
var Hapi = require('hapi'); | ||
'use strict'; | ||
var lab = exports.lab = Lab.script(); | ||
var before = lab.before; | ||
var beforeEach = lab.beforeEach; | ||
var describe = lab.experiment; | ||
var it = lab.test; | ||
var expect = require('code').expect; | ||
const Lab = require('lab'); | ||
const Hapi = require('hapi'); | ||
describe('Registration', function(){ | ||
var server; | ||
const lab = exports.lab = Lab.script(); | ||
const beforeEach = lab.beforeEach; | ||
const afterEach = lab.afterEach; | ||
const describe = lab.experiment; | ||
const it = lab.test; | ||
const expect = require('code').expect; | ||
beforeEach(function(done){ | ||
server = new Hapi.Server().connection({ host: 'test' }); | ||
done(); | ||
describe('Registration', () => { | ||
let server; | ||
beforeEach(() => { | ||
server = Hapi.Server({ }); | ||
}); | ||
it('should register', function(done) { | ||
server.register(require('../'), function() { | ||
var routes = server.table(); | ||
expect(routes).to.have.length(1); | ||
expect(routes[0].table).to.have.length(1); | ||
done(); | ||
}); | ||
it('should register', async () => { | ||
await server.register({ plugin: require('../') }); | ||
const table = server.table(); | ||
expect(table).to.have.length(1); | ||
}); | ||
it('should let you set the path', function(done){ | ||
server.register({register: require('../'), options: {path: '/api/v1/new_ping'}}, function() { | ||
var routes = server.table(); | ||
expect(routes[0].table[0].path).to.equal('/api/v1/new_ping'); | ||
done(); | ||
}); | ||
it('should let you set the path', async () => { | ||
await server.register({ plugin: require('../'), options: { path: '/api/v1/new_ping' } }); | ||
const table = server.table(); | ||
const [route] = table; | ||
expect(route.path).to.equal('/api/v1/new_ping'); | ||
}); | ||
it('should let you set otherData', function(done){ | ||
server.register({register: require('../'), options: {otherData: {foo: 'bar'}}}, function() { | ||
var routes = server.table(); | ||
expect(routes[0].table[0].path).to.equal('/ding'); | ||
done(); | ||
}); | ||
it('should let you set otherData', async () => { | ||
await server.register({ plugin: require('../'), options: { otherData: { foo: 'bar' } } }); | ||
const table = server.table(); | ||
const [route] = table; | ||
expect(route.path).to.equal('/ding'); | ||
}); | ||
it('should let you set config options', function(done){ | ||
server.register({register: require('../'), options: {config: {description: 'Custom Hapi Ding'}}}, function() { | ||
var routes = server.table(); | ||
expect(routes[0].table[0].settings.description).to.equal('Custom Hapi Ding'); | ||
done(); | ||
}); | ||
it('should let you set config options', async () => { | ||
await server.register({ plugin: require('../'), options: { config: { description: 'Custom Hapi Ding' } } }); | ||
const table = server.table(); | ||
const [route] = table; | ||
expect(route.settings.description).to.equal('Custom Hapi Ding'); | ||
}); | ||
it('should not let you set unknown options', function(done){ | ||
server.register({register: require('../'), options: {badName: {foo: 'bar'}}}, function(err) { | ||
expect(err).to.not.be.null(); | ||
done(); | ||
}); | ||
it('should not let you set unknown options', async () => { | ||
try { | ||
await server.register({ plugin: require('../'), options: { badName: { foo: 'bar' } } }); | ||
} catch (err) { | ||
expect(err).to.be.an.object(); | ||
expect(err.name).to.equal('ValidationError'); | ||
expect(err.details[0].message).to.include('badName'); | ||
} | ||
}); | ||
}); | ||
describe('Default /ding response', function(){ | ||
var server; | ||
describe('Default /ding response', () => { | ||
beforeEach(async () => { | ||
server = Hapi.Server({ }); | ||
await server.register({ plugin: require('../') }); | ||
}); | ||
beforeEach(function(done) { | ||
server = new Hapi.Server().connection({ host: 'test' }); | ||
server.register(require('../'), function() { | ||
done(); | ||
it('should reply with cpu', async () => { | ||
const { result, statusCode } = await server.inject('/ding'); | ||
expect(result).to.be.an.object(); | ||
expect(result.ding).to.be.an.object(); | ||
expect(result.ding.cpu).to.be.an.array(); | ||
expect(result.ding.cpu).to.have.length(3); | ||
expect(result.ding.cpu[0]).to.be.a.number(); | ||
expect(statusCode).to.equal(200); | ||
}); | ||
}); | ||
it('should reply with cpu', function(done){ | ||
server.inject('/ding', function(res){ | ||
expect(res.result).to.be.an.object(); | ||
expect(res.result.ding).to.be.an.object(); | ||
expect(res.result.ding.cpu).to.be.an.array(); | ||
expect(res.result.ding.cpu).to.have.length(3); | ||
expect(res.result.ding.cpu[0]).to.be.a.number(); | ||
done(); | ||
it('should reply with mem', async () => { | ||
const { result, statusCode } = await server.inject('/ding'); | ||
expect(result.ding).to.be.an.object(); | ||
expect(result.ding.mem).to.be.a.number(); | ||
expect(statusCode).to.equal(200); | ||
}); | ||
}); | ||
it('should reply with mem', function(done){ | ||
server.inject('/ding', function(res){ | ||
expect(res.result.ding).to.be.an.object(); | ||
expect(res.result.ding.mem).to.be.a.number(); | ||
done(); | ||
it('should reply with time', async () => { | ||
const { result, statusCode } = await server.inject('/ding'); | ||
expect(result.ding).to.be.an.object(); | ||
expect(result.ding.time).to.be.a.number(); | ||
expect(statusCode).to.equal(200); | ||
}); | ||
}); | ||
it('should reply with time', function(done){ | ||
server.inject('/ding', function(res){ | ||
expect(res.result.ding).to.be.an.object(); | ||
expect(res.result.ding.time).to.be.a.number(); | ||
done(); | ||
describe('Hapi load reporting', () => { | ||
beforeEach(async () => { | ||
server = Hapi.Server({ load: { sampleInterval: 1000 } }); | ||
await server.register({ plugin: require('../') }); | ||
await server.start(); | ||
}); | ||
}); | ||
}); | ||
describe('Hapi load reporting', function(){ | ||
it('should reply with heap', function(done){ | ||
var server = new Hapi.Server({load: { sampleInterval: 1000 }}).connection({ host: 'test' }); | ||
server.register({register: require('../')}, function() { | ||
server.connections[0]._load._process.load.heapUsed = 100000; | ||
server.connections[0]._load.check = function(){ return false; }; | ||
server.inject('/ding', function(res){ | ||
expect(res.result.ding).to.be.an.object(); | ||
expect(res.result.ding.heap).to.be.a.number(); | ||
expect(res.result.ding.heap).to.equal(100000); | ||
done(); | ||
}); | ||
afterEach(async () => { | ||
await server.stop(); | ||
}); | ||
}); | ||
it('should reply with heap', function(done){ | ||
var server = new Hapi.Server({load: { sampleInterval: 1000 }}).connection({ host: 'test' }); | ||
it('should reply with heapUsed', async () => { | ||
const fakeHeapUsed = 100000; | ||
server.settings.load.heapUsed = fakeHeapUsed; | ||
server.register({register: require('../')}, function() { | ||
server.connections[0]._load._process.load.eventLoopDelay = 1.34; | ||
server.connections[0]._load.check = function(){ return false; }; | ||
server.inject('/ding', function(res){ | ||
expect(res.result.ding).to.be.an.object(); | ||
expect(res.result.ding.loop).to.be.a.number(); | ||
expect(res.result.ding.loop).to.equal(1.34); | ||
done(); | ||
}); | ||
const { result } = await server.inject('/ding'); | ||
expect(result.ding).to.be.an.object(); | ||
expect(result.ding.heap).to.be.a.number(); | ||
expect(result.ding.heap).to.equal(fakeHeapUsed); | ||
}); | ||
}); | ||
it('should not reply with heap or loop if the server does not have the load settings', function(done){ | ||
var server = new Hapi.Server().connection({ host: 'test' }); | ||
it('should reply with eventLoopDelay', async () => { | ||
const fakeEventLoopDelay = 1.34; | ||
server.settings.load.eventLoopDelay = fakeEventLoopDelay; | ||
server.register({register: require('../')}, function() { | ||
server.connections[0]._load._process.load.eventLoopDelay = 1.34; | ||
server.connections[0]._load.check = function(){ return false; }; | ||
server.inject('/ding', function(res){ | ||
expect(res.result.ding).to.be.an.object(); | ||
expect(res.result.ding.loop).to.be.undefined(); | ||
expect(res.result.ding.heap).to.be.undefined(); | ||
done(); | ||
}); | ||
const { result } = await server.inject('/ding'); | ||
expect(result.ding).to.be.an.object(); | ||
expect(result.ding.loop).to.be.a.number(); | ||
expect(result.ding.loop).to.equal(1.34); | ||
}); | ||
}); | ||
}); | ||
describe('Custom /ding response', function(){ | ||
it('should reply with given other data', function(done){ | ||
var server = new Hapi.Server().connection({ host: 'test' }); | ||
server.register({register: require('../'), options: {otherData: {version: '1.2.3'}}}, function() { | ||
server.inject('/ding', function(res){ | ||
expect(res.result.ding).to.be.an.object(); | ||
expect(res.result.ding.version).to.be.an.string(); | ||
expect(res.result.ding.version).to.equal('1.2.3'); | ||
done(); | ||
}); | ||
it('should not reply with heap or loop if the server does not have the load settings', async () => { | ||
const { result } = await server.inject('/ding'); | ||
expect(result.ding).to.be.an.object(); | ||
expect(result.ding.loop).to.be.undefined(); | ||
expect(result.ding.heap).to.be.undefined(); | ||
}); | ||
}); | ||
it('should reply without objectName', function(done){ | ||
var server = new Hapi.Server().connection({ host: 'test' }); | ||
describe('Custom /ding response', () => { | ||
beforeEach(() => { | ||
server = Hapi.Server({ }); | ||
}); | ||
server.register({register: require('../'), options: {objectName: false}}, function() { | ||
server.inject('/ding', function(res){ | ||
expect(res.result).to.be.an.object(); | ||
expect(res.result.ding).to.be.undefined(); | ||
expect(res.result.mem).to.be.a.number(); | ||
done(); | ||
}); | ||
it('should reply with given other data', async () => { | ||
await server.register({ plugin: require('../'), options: { otherData: { version: '1.2.3' } } }); | ||
const { result } = await server.inject('/ding'); | ||
expect(result.ding).to.be.an.object(); | ||
expect(result.ding.version).to.be.an.string(); | ||
expect(result.ding.version).to.equal('1.2.3'); | ||
}); | ||
}); | ||
it('should reply with a specific objectName', function(done){ | ||
var server = new Hapi.Server().connection({ host: 'test' }); | ||
server.register({register: require('../'), options: {objectName: 'foo'}}, function() { | ||
server.inject('/ding', function(res){ | ||
expect(res.result).to.be.an.object(); | ||
expect(res.result.ding).to.be.undefined(); | ||
expect(res.result.foo).to.be.an.object(); | ||
expect(res.result.foo.mem).to.be.a.number(); | ||
done(); | ||
}); | ||
it('should reply without objectName', async () => { | ||
await server.register({ plugin: require('../'), options: { objectName: false } }); | ||
const { result } = await server.inject('/ding'); | ||
expect(result).to.be.an.object(); | ||
expect(result.ding).to.be.undefined(); | ||
expect(result.mem).to.be.a.number(); | ||
}); | ||
}); | ||
}); | ||
it('should reply with a specific objectName', async () => { | ||
await server.register({ plugin: require('../'), options: { objectName: 'foo' } }); | ||
const { result } = await server.inject('/ding'); | ||
expect(result).to.be.an.object(); | ||
expect(result.ding).to.be.undefined(); | ||
expect(result.foo).to.be.an.object(); | ||
expect(result.foo.mem).to.be.a.number(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
1
9576
6
7
194
51
2
2
+ Addedhoek@5.0.46.1.3(transitive)
+ Addedisemail@3.2.0(transitive)
+ Addedjoi@13.7.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedtopo@3.0.3(transitive)
- Removedhoek@2.16.3(transitive)
- Removedisemail@1.2.0(transitive)
- Removedjoi@6.10.1(transitive)
- Removedmoment@2.30.1(transitive)
- Removedtopo@1.1.0(transitive)
Updatedhoek@^5.0.2
Updatedjoi@^13.0.2