Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to hapi@17 #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var server = new Hapi.Server();

var options = {};

server.register({ register: require('hapi-cloudwatch'), options }, function(err) {
server.register({ plugin: require('hapi-cloudwatch'), options }, function(err) {
if (err) {
console.log('error', 'Failed loading plugin: hapi-cloudwatch');
}
Expand Down Expand Up @@ -64,8 +64,8 @@ A Hapi route configured like this:
server.route({
method: 'GET',
path: '/products/{id}',
handler: function(request, reply) {
reply('Success!');
handler: function(request, h) {
'Success!';
}
});
```
Expand All @@ -84,6 +84,8 @@ Here's an example of what can be graphed in CloudWatch with this metric:

## Version Compatibility

### Currently in use by me with with: Hapi 16.1.0 (Node v6)
| Version | [hapi.js](https://github.com/hapijs/hapi) |
| ------- | ----------------------------------------- |
| `2.x` | `>=17 hapi` |
| `1.x` | `<17 hapi` |

_I'll add tests for other hapi and node versions shortly_
25 changes: 12 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ const packageJSON = require('./package.json');
const lifecycle = require('./lib/lifecycle');
const metric = require('./lib/metric');

exports.register = (server, options, next) => {
const responseMetric = metric.create(options);

server.ext('onRequest', lifecycle.setStartTime);
server.ext('onPreResponse', lifecycle.setEndTime);
exports.plugin = {
pkg: packageJSON,
name: 'hapi-cloudwatch',

server.on('response', request => {
const dimensions = metric.createDimensions(request);
responseMetric.put(request.app.endTime - request.app.startTime, 'responseTime', dimensions);
});
async register(server, options) {
const responseMetric = metric.create(options);

return next();
};
server.ext('onRequest', lifecycle.setStartTime);
server.ext('onPreResponse', lifecycle.setEndTime);

exports.register.attributes = {
pkg: packageJSON,
server.events.on('response', request => {
const dimensions = metric.createDimensions(request);
responseMetric.put(request.app.endTime - request.app.startTime, 'responseTime', dimensions);
});
},
};
12 changes: 5 additions & 7 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import Hapi from 'hapi';
import plugin from './index';

test('plugin registered', async t => {
const server = new Hapi.Server();
server.connection({ port: 3000 });
const server = new Hapi.Server({ port: 3000 });

server.route({ method: 'GET', path: '/', handler: (request, reply) => reply(request.app) });
server.route({ method: 'GET', path: '/', handler: request => request.app });

await server.register(plugin);
const { result } = await server.inject({ url: '/' });
Expand All @@ -16,10 +15,9 @@ test('plugin registered', async t => {
});

test('plugin registered with all options', async t => {
const server = new Hapi.Server();
server.connection({ port: 3000 });
const server = new Hapi.Server({ port: 3000 });

server.route({ method: 'GET', path: '/', handler: (request, reply) => reply(request.app) });
server.route({ method: 'GET', path: '/', handler: request => request.app });

const options = {
enabled: false,
Expand All @@ -28,7 +26,7 @@ test('plugin registered with all options', async t => {
metricsSentCallback: () => {},
};

await server.register({ register: plugin, options });
await server.register({ plugin, options });
const { result } = await server.inject({ url: '/' });
t.truthy(result.startTime);
t.truthy(result.endTime);
Expand Down
8 changes: 4 additions & 4 deletions lib/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
exports.setStartTime = (request, reply) => {
exports.setStartTime = (request, h) => {
request.app.startTime = (new Date()).getTime();
return reply.continue();
return h.continue;
};

exports.setEndTime = (request, reply) => {
exports.setEndTime = (request, h) => {
request.app.endTime = (new Date()).getTime();
return reply.continue();
return h.continue;
};
5 changes: 2 additions & 3 deletions lib/lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import Hapi from 'hapi';
import lifecycle from './lifecycle';

const createHapiServer = () => {
const server = new Hapi.Server();
server.connection({ port: 3000 });
const server = new Hapi.Server({ port: 3000 });

server.ext('onRequest', lifecycle.setStartTime);
server.ext('onPreResponse', lifecycle.setEndTime);

server.route({ method: 'GET', path: '/', handler: (request, reply) => reply(request.app) });
server.route({ method: 'GET', path: '/', handler: request => request.app });

return server;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"eslint": "3.19.0",
"eslint-config-airbnb-base": "11.2.0",
"eslint-plugin-import": "2.3.0",
"hapi": "16.1.1",
"hapi": "17.8.5",
"nyc": "10.3.2",
"sinon": "2.3.1"
},
Expand Down