express-perf
Advanced tools
Comparing version 1.0.0 to 1.1.0
122
index.js
@@ -1,14 +0,98 @@ | ||
var timingData = [] | ||
var https = require('https') | ||
var sendTimingData = function () { | ||
var patchExpress = function (express) { | ||
// this helps us understand the pathing structure | ||
// that the code configures and binds a __basePath | ||
// to the layer | ||
var use = express.Router.use | ||
express.Router.use = function (fn) { | ||
if (typeof fn === 'string' && Array.isArray(this.stack)) { | ||
var offset = this.stack.length | ||
var result = use.apply(this, arguments) | ||
var layer | ||
for (; offset < this.stack.length; offset++) { | ||
layer = this.stack[offset] | ||
if (layer && layer.regexp && !layer.regexp.fast_slash) { | ||
layer.__basePath = fn | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
var recordTimingData = function (apiKey, method, url, time, statusCode) { | ||
return use.apply(this, arguments) | ||
} | ||
// when the layer gets called, we continue attaching | ||
// the __basePath from each layer onto the __baseRoute | ||
var pp = express.Router.process_params | ||
express.Router.process_params = function (layer, called, req, res, done) { | ||
if (layer.__basePath) { | ||
req.__baseRoute = (req.__baseRoute || '') + layer.__basePath | ||
} | ||
return pp.apply(this, arguments) | ||
} | ||
} | ||
module.exports = function (options) { | ||
if (!options.apiKey) { | ||
console.warn('node-perf expects an \'apiKey\'') | ||
module.exports = function (express, opts) { | ||
patchExpress(express) | ||
var timingData = [] | ||
var timer = null | ||
var sendTimingData = function () { | ||
if (!opts.apiKey) { | ||
return | ||
} | ||
var sendData = timingData.splice(0, timingData.length) | ||
timer = null | ||
var handleResp = function (resp) { | ||
if (!resp || resp.statusCode !== 200) { | ||
if (opts.debug) { | ||
if (resp.statusCode) { | ||
console.warn('[express-perf]', 'non-200 status code, got', resp.statusCode) | ||
} | ||
} | ||
for (var i in sendData) { | ||
recordTimingData(sendData[i]) | ||
} | ||
} | ||
if (opts.debug) { | ||
console.info('[express-perf]', sendData.length, 'rows sent') | ||
} | ||
} | ||
var req = https.request({ | ||
hostname: 'data.perf.sh', | ||
port: 443, | ||
path: '/ingest', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Perf-Public-API-Key': opts.apiKey | ||
} | ||
}, handleResp) | ||
req.on('error', function (){ | ||
handleResp() | ||
}) | ||
req.write(JSON.stringify(sendData)) | ||
req.end() | ||
} | ||
var recordTimingData = function (data) { | ||
timingData.push(data) | ||
if (!timer) { | ||
timer = setTimeout(sendTimingData, opts.sendInterval || 1000) | ||
} | ||
} | ||
if (!opts.apiKey) { | ||
console.warn('[express-perf]', 'missing \'apiKey\'') | ||
return | ||
@@ -21,3 +105,22 @@ } | ||
function finish () { | ||
console.log(req) | ||
var method = req.method | ||
var url = req.protocol + '://' + req.get('host') + req.originalUrl | ||
var time = (new Date().getTime()) - startTime | ||
var statusCode = res.statusCode | ||
var data = { | ||
request_method: req.method, | ||
request_url: req.protocol + '://' + req.get('host') + req.originalUrl, | ||
normalized_uri: req.__baseRoute + req.route.path, | ||
status_code: res.statusCode, | ||
time_in_millis: (new Date().getTime()) - startTime | ||
} | ||
recordTimingData(data) | ||
if (opts.debug) { | ||
console.info('[express-perf]', data) | ||
} | ||
cleanup() | ||
} | ||
@@ -31,3 +134,3 @@ | ||
res.once('finish', sendStats) | ||
res.once('finish', finish) | ||
res.once('error', cleanup) | ||
@@ -40,3 +143,2 @@ res.once('close', cleanup) | ||
} | ||
} |
{ | ||
"name": "express-perf", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
Network access
Supply chain riskThis module accesses the network.
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
39076
5
115
1