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

Updated jsdocs to be more descriptive #64

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

let responseTime = require('./');
let Koa = require('koa');
let app = new Koa();
const responseTime = require('./');
const Koa = require('koa');
const app = new Koa();

app.use(responseTime({ hrtime: true }));

Expand Down
27 changes: 16 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@

module.exports = responseTime;

const defaultProps = { hrtime: false };

/**
* @typedef {import("koa").Middleware} Middleware
*/

/**
* Add X-Response-Time header field.
* @param {Dictionary} options options dictionary. { hrtime }
*
* hrtime: boolean.
* `true` to use time in nanoseconds.
* `false` to use time in milliseconds.
* @param {Object} options options dictionary. { hrtime }
* @param {boolean} options.hrtime
* - `true` to use time in nanoseconds.
* - `false` to use time in milliseconds.
* Default is `false` to keep back compatible.
* @return {Function}
* @return {Middleware} Koa Middleware
* @api public
*/

function responseTime(options) {
let hrtime = options && options.hrtime;
function responseTime(options = defaultProps) {
const hrtime = options && options.hrtime;
return function responseTime(ctx, next) {
let start = ctx[Symbol.for('request-received.startAt')] ? ctx[Symbol.for('request-received.startAt')] : process.hrtime();
const start = ctx[Symbol.for('request-received.startAt')]
? ctx[Symbol.for('request-received.startAt')]
: process.hrtime();
return next().then(() => {
let delta = process.hrtime(start);

// Format to high resolution time with nano time
delta = delta[0] * 1000 + delta[1] / 1000000;
if (!hrtime) {
Expand Down