request-received
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -5,13 +5,34 @@ "use strict"; | ||
const startTimeSymbol = Symbol.for('request-received.startTime'); | ||
const pinoHttpStartAtSymbol = Symbol.for('pino-http.startAt'); | ||
const pinoHttpStartTimeSymbol = Symbol.for('pino-http.startTime'); | ||
module.exports = function (...args) { | ||
// start the timers | ||
const startAt = process.hrtime(); | ||
const startTime = new Date(); // support both express and koa route middleware | ||
let startAt = process.hrtime(); | ||
let startTime = Date.now(); // support both express and koa route middleware | ||
const isExpress = typeof args[2] !== 'undefined' && typeof args[2] === 'function'; | ||
const isExpress = typeof args[2] !== 'undefined' && typeof args[2] === 'function'; // | ||
// detect pino-http Symbols | ||
// | ||
// express - req[Symbol] | ||
// koa - ctx[Symbol], ctx.req[Symbol], ctx.request[Symbol] | ||
if (args[0][pinoHttpStartAtSymbol]) { | ||
startAt = args[0][pinoHttpStartAtSymbol]; | ||
} else if (!isExpress) { | ||
if (args[0].req[pinoHttpStartAtSymbol]) startAt = args[0].req[pinoHttpStartAtSymbol];else if (args[0].request[pinoHttpStartAtSymbol]) startAt = args[0].request[pinoHttpStartAtSymbol]; | ||
} | ||
if (args[0][pinoHttpStartTimeSymbol]) { | ||
startTime = args[0][pinoHttpStartTimeSymbol]; | ||
} else if (!isExpress) { | ||
if (args[0].req[pinoHttpStartTimeSymbol]) startTime = args[0].req[pinoHttpStartTimeSymbol];else if (args[0].request[pinoHttpStartTimeSymbol]) startTime = args[0].request[pinoHttpStartTimeSymbol]; | ||
} | ||
if (isExpress) { | ||
args[0][startAtSymbol] = startAt; | ||
args[0][startTimeSymbol] = startTime; | ||
args[0][startTimeSymbol] = startTime; // pino-http support | ||
args[0][pinoHttpStartAtSymbol] = startAt; | ||
args[0][pinoHttpStartTimeSymbol] = startTime; | ||
} else { | ||
@@ -23,3 +44,10 @@ args[0][startAtSymbol] = startAt; | ||
args[0].req[startTimeSymbol] = startTime; | ||
args[0].request[startTimeSymbol] = startTime; | ||
args[0].request[startTimeSymbol] = startTime; // pino-http support | ||
args[0][pinoHttpStartAtSymbol] = startAt; | ||
args[0].req[pinoHttpStartAtSymbol] = startAt; | ||
args[0].request[pinoHttpStartAtSymbol] = startAt; | ||
args[0][pinoHttpStartTimeSymbol] = startTime; | ||
args[0].req[pinoHttpStartTimeSymbol] = startTime; | ||
args[0].request[pinoHttpStartTimeSymbol] = startTime; | ||
} | ||
@@ -26,0 +54,0 @@ |
{ | ||
"name": "request-received", | ||
"description": "Route middleware for Koa and Express that adds a request received high-resolution timer and Date to the request object using easily accessible Symbols to prevent request object pollution. Made for Cabin.", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)", | ||
@@ -6,0 +6,0 @@ "ava": { |
@@ -63,4 +63,4 @@ # request-received | ||
// [ 472542, 431456521 ] | ||
console.log('startTime', req[startTime]); // new Date() | ||
// 2000-01-01T06:00:00.000Z | ||
console.log('startTime', req[startTime]); // Date.now() | ||
// 1560499520000 | ||
next(); | ||
@@ -98,4 +98,4 @@ }); | ||
// [ 472542, 431456521 ] | ||
console.log('startTime', ctx[startTime]); // new Date() | ||
// 2000-01-01T06:00:00.000Z | ||
console.log('startTime', ctx[startTime]); // Date.now() | ||
// 1560499520000 | ||
@@ -102,0 +102,0 @@ // note that the symbols are also accessible via: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10165
44