Skip to content

Commit

Permalink
Convert to use the new onSend hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lolo32 committed Nov 8, 2017
1 parent d070af8 commit 8bcd0dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
[![Coverage Status](https://coveralls.io/repos/github/lolo32/fastify-response-time/badge.svg?branch=master)](https://coveralls.io/github/lolo32/fastify-response-time?branch=master)
[![Known Vulnerabilities](https://snyk.io/test/github/lolo32/fastify-response-time/badge.svg)](https://snyk.io/test/github/lolo32/fastify-response-time)

Add X-Response-Time header at each request for Fastify. The unit used is milliseconds.
Add `X-Response-Time` header at each request for Fastify. The unit used is milliseconds.

**You need to use Fastify version 0.31 or newer, the hook used (onSend) was added in this version**

_If you need older Fastify version, use version 1.0.1 of this plugin_

## Install

Expand Down
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

const fastifyPlugin = require("fastify-plugin");
const onHeaders = require("on-headers");

/**
* Decorators
Expand All @@ -12,15 +11,26 @@ const onHeaders = require("on-headers");
* @param {function} next
*/
module.exports = fastifyPlugin((instance, opts, next) => {
// Hook to be triggered on request (start time)
instance.addHook('onRequest', (req, res, next) => {
// Store the start timer in nanoseconds resolution
const requestTime = process.hrtime();

onHeaders(res, function () {
// Hook to be triggered just before response to be send
instance.addHook('onSend', (request, reply, payload, next) => {
// Calculate the duration, in nanoseconds …
const hrDuration = process.hrtime(requestTime);
const duration = hrDuration[0]*1e5 + hrDuration[1]/1e4;
this.setHeader("X-Response-Time", `${parseInt(duration, 10) / 100}`);
// … convert it to milliseconds …
const duration = Math.round(hrDuration[0]*1e5 + hrDuration[1]/1e4) / 100;
// … add the header to the response
reply.header("X-Response-Time", `${duration}`);

next();
});

next();
});

next();
}, "0.x");
// Not before 0.31 (onSend hook added to this version)
}, ">= 0.31");
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-response-time",
"version": "1.0.1",
"version": "1.0.2",
"description": "Add X-Response-Time header at each request (in ms) for Fastify",
"main": "index.js",
"scripts": {
Expand All @@ -19,18 +19,16 @@
"license": "MIT",
"keywords": [
"Fastify",
"on-header",
"response-time",
"headers",
"onheaders",
"event"
"header"
],
"devDependencies": {
"fastify": "^0.26.2",
"fastify": "^0.31.0",
"request": "^2.81.0",
"tap": "^10.7.2"
},
"dependencies": {
"fastify-plugin": "^0.1.0",
"on-headers": "^1.0.1"
"fastify-plugin": "^0.1.0"
}
}

0 comments on commit 8bcd0dc

Please sign in to comment.