npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-response-time

v1.1.0

Published

Add X-Response-Time header at each request (in ms) for Fastify

Downloads

513

Readme

fastify-response-time

Build Status Coverage Status Known Vulnerabilities

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

Add Server-Timing header, that could be used to pass timing server to client (useful to debug). You could find some documentation at W3C Server-Timing documentation.

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

npm install --save fastify-response-time

Usage

Add it to you project with register and you are done!

// Register the plugin
fastify.register(require("fastify-response-time"));

// Define a new route in hapijs notation
fastify.route({
  method: "GET",
  url: "/header-hapi",
  handler: (request, reply) => {
    reply.send();
  }
});

// Define a new route in express notation
fastify.get("/header-express", (request, reply) => {
  reply.send();
});

// Add server timing information
fastify.get("/header-express", (request, reply) => {
  reply.setServerTiming("cache", 5.3, "Cache read");
  reply.send();
});

Both examples responds with:

< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 17
< X-Response-Time: 0.08
< Date: Fri, 15 Sep 2017 21:14:33 GMT
< Connection: keep-alive
<

Options

This plugin allow you to specify options:

  • header can be used to change default header name to something else (X-Response-Time by default)
  • digits to specify the number of digits in the response (2 by default, so 1.03)

reply.setServerTiming usage

reply.setServerTiming( name , duration , description ) => boolean

With:

  • name: [mandatory] the name of the measure, and must be unique. Any already existing value will not be replaced
  • duration: [optional] the duration, that must be an integer or float. If not needed, pass 0 or null
  • description: [optional] the description is needed. It must be a string
  • return true if the measure is added the the list that will be send with the request, false is the name already exists

The response will be:

< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 17
< X-Response-Time: 0.08
< Server-Timing: cache;dur=5.3;desc="Cache read"
< Date: Fri, 15 Sep 2017 21:14:33 GMT
< Connection: keep-alive
<