Skip to content
Merged
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
9 changes: 9 additions & 0 deletions docs/Reply.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [.hasHeader(key)](#hasheaderkey)
- [.redirect(dest)](#redirectdest)
- [.callNotFound()](#callnotfound)
- [.getResponseTime()](#getresponsetime)
- [.type(contentType)](#typecontenttype)
- [.serializer(func)](#serializerfunc)
- [.sent](#sent)
Expand Down Expand Up @@ -109,6 +110,14 @@ Invokes the custom not found handler.
reply.callNotFound()
```

<a name="getResponseTime"></a>
### .getResponseTime()
Invokes the custom response time getter to calculate the amount of time passed since the request was started.

```js
const milliseconds = reply.getResponseTime()
```

<a name="type"></a>
### .type(contentType)
Sets the content type for the response.
Expand Down
1 change: 1 addition & 0 deletions fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ declare namespace fastify {
getHeader(name: string): string | undefined
hasHeader(name: string): boolean
callNotFound(): void
getResponseTime(): number
type(contentType: string): FastifyReply<HttpResponse>
redirect(url: string): FastifyReply<HttpResponse>
redirect(statusCode: number, url: string): FastifyReply<HttpResponse>
Expand Down
15 changes: 11 additions & 4 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ Reply.prototype.callNotFound = function () {
notFound(this)
}

Reply.prototype.getResponseTime = function () {
var responseTime = 0

if (this[kReplyStartTime] !== undefined) {
responseTime = now() - this[kReplyStartTime]
}

return responseTime
}

function preserializeHook (reply, payload) {
if (reply.context.preSerialization !== null) {
onSendHookRunner(
Expand Down Expand Up @@ -460,11 +470,8 @@ function onResponseCallback (err, request, reply) {
if (reply.log[kDisableRequestLogging]) {
return
}
var responseTime = 0

if (reply[kReplyStartTime] !== undefined) {
responseTime = now() - reply[kReplyStartTime]
}
var responseTime = reply.getResponseTime()

if (err != null) {
reply.log.error({
Expand Down
30 changes: 29 additions & 1 deletion test/internals/reply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
} = require('../../lib/symbols')

test('Once called, Reply should return an object with methods', t => {
t.plan(12)
t.plan(13)
const response = { res: 'res' }
function context () {}
function request () {}
Expand All @@ -27,6 +27,7 @@ test('Once called, Reply should return an object with methods', t => {
t.is(typeof reply.status, 'function')
t.is(typeof reply.header, 'function')
t.is(typeof reply.serialize, 'function')
t.is(typeof reply.getResponseTime, 'function')
t.is(typeof reply[kReplyHeaders], 'object')
t.strictEqual(reply.res, response)
t.strictEqual(reply.context, context)
Expand Down Expand Up @@ -977,3 +978,30 @@ test('should throw error when attempting to set reply.sent more than once', t =>
t.pass()
})
})

test('reply.getResponseTime() should return 0 before the timer is initialised on the reply by setting up response listeners', t => {
t.plan(1)
const response = { statusCode: 200 }
const context = {}
const reply = new Reply(response, context, null)
t.equal(reply.getResponseTime(), 0)
})

test('reply.getResponseTime() should return a number greater than 0 after the timer is initialised on the reply by setting up response listeners', t => {
t.plan(1)
const fastify = require('../..')()
fastify.route({
method: 'GET',
url: '/',
handler: (req, reply) => {
reply.send('hello world')
}
})

fastify.addHook('onResponse', (req, reply) => {
t.true(reply.getResponseTime() > 0)
t.end()
})

fastify.inject({ method: 'GET', url: '/' })
})
4 changes: 4 additions & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ server
.get('/deprecatedpath/*', (req, reply) => {
reply.callNotFound()
})
.get('/getResponseTime', function (req, reply) {
const milliseconds : number = reply.getResponseTime()
reply.send({ milliseconds })
})

// Generics example
interface Query {
Expand Down