Skip to content

Commit

Permalink
Merge f2c94ed into 2a0ce6f
Browse files Browse the repository at this point in the history
  • Loading branch information
bgardner87 committed Jan 21, 2018
2 parents 2a0ce6f + f2c94ed commit c3f527b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
61 changes: 46 additions & 15 deletions __tests__/middleware-wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,56 @@ describe('hapijs-status-monitor', () => {
});

describe('when invoked', () => {
const server = {
socket: {},
on: jest.fn(),
route: jest.fn(),
ext: jest.fn(),
};
const options = { send: jest.fn() };
const next = jest.fn();

it(`and server.path === ${defaultConfig.path}, then next() called one times`, () => {
let server;
let options;

beforeEach(() => {
server = {
socket: {},
events: {
on: jest.fn(),
},
route: jest.fn(),
ext: jest.fn(),
};

options = { send: jest.fn() };
});

it(`and server.path === ${defaultConfig.path}, then on() called one times`, () => {
server.path = defaultConfig.path;
middleware(server, options);
expect(server.events.on).toHaveBeenCalledTimes(1);
});

it(`and server.path === ${defaultConfig.path}, then route() called one times`, () => {
server.path = defaultConfig.path;
middleware(server, options);
expect(server.route).toHaveBeenCalledTimes(1);
});

it(`and server.path === ${defaultConfig.path}, then ext() called one times`, () => {
server.path = defaultConfig.path;
middleware(server, options, next);
expect(next).toHaveBeenCalledTimes(1);
middleware(server, options);
expect(server.ext).toHaveBeenCalledTimes(1);
});

it(`and server.path !== ${defaultConfig.path}, then on() called one times`, () => {
server.path = '/another-path';
middleware(server, options);
expect(server.events.on).toHaveBeenCalledTimes(1);
});

it(`and server.path !== ${defaultConfig.path}, then route() called one times`, () => {
server.path = '/another-path';
middleware(server, options);
expect(server.route).toHaveBeenCalledTimes(1);
});

it(`and server.path !== ${defaultConfig.path}, then next() called two times`, () => {
it(`and server.path !== ${defaultConfig.path}, then ext() called one times`, () => {
server.path = '/another-path';
middleware(server, options, next);
expect(next).toHaveBeenCalledTimes(2);
middleware(server, options);
expect(server.ext).toHaveBeenCalledTimes(1);
});
});
});
Expand Down
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const pkg = require('./package.json');
const register = require('./src/middleware-wrapper');

// provide meta-information as expected by hapi.js
register.attributes = { pkg };

// export register function, wrapped in a plugin object
module.exports = { register };
module.exports.plugin = {
register, name: 'HapiJS Status Monitor', pkg,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hapijs-status-monitor",
"version": "0.6.0",
"version": "1.0.0",
"description": "Monitoring plugin for hapi.js applications",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 7 additions & 1 deletion src/helpers/on-headers-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ module.exports = (statusCode, startTime, spans) => {
const category = Math.floor(statusCode / 100);

spans.forEach((span) => {
const last = span.responses[span.responses.length - 1];
let { responses } = span;

if (!responses) {
responses = [];
}

const last = responses[span.responses.length - 1];
if (last !== undefined && (last.timestamp / 1000) + span.interval > Date.now() / 1000) {
last[category] += 1;
last.count += 1;
Expand Down
22 changes: 9 additions & 13 deletions src/middleware-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,46 @@ const onHeadersListener = require('./helpers/on-headers-listener');
const socketIoInit = require('./helpers/socket-io-init');

// hapi.js plugin register function
const middlewareWrapper = (server, options, next) => {
const middlewareWrapper = (server, options) => {
const opts = validate(options);

// Setup Socket.IO
server.on('start', () => {
server.events.on('start', () => {
socketIoInit(server.listener, opts.spans);
});

server.route({
method: 'GET',
path: opts.path,
handler: (request, reply) => {
handler: () => {
const renderedHtml =
fs.readFileSync(path.join(__dirname, '/public/index.html'))
.toString()
.replace(/{{title}}/g, opts.title)
.replace(/{{script}}/g, fs.readFileSync(path.join(__dirname, '/public/javascripts/app.js')))
.replace(/{{style}}/g, fs.readFileSync(path.join(__dirname, '/public/stylesheets/style.css')));

reply(renderedHtml)
.header('Content-Type', 'text/html')
.code(200);
return renderedHtml;
},
config: opts.routeConfig,
});

// Hook into the middle of processing
server.ext('onPreResponse', (request, reply) => {
server.ext('onPreResponse', (request, h) => {
if (request.response.isBoom || request.path === opts.path) {
return reply.continue();
return h.continue;
}

const startTime = process.hrtime();
const resp = request.response;

resp.once('finish', () => {
resp.events.once('finish', () => {
onHeadersListener(resp.statusCode, startTime, opts.spans);
});

return reply.continue();
// Continue Processing
return h.continue;
});

// Continue processing
return next();
};

module.exports = middlewareWrapper;

0 comments on commit c3f527b

Please sign in to comment.