koatty_trace
Advanced tools
Comparing version 1.4.7 to 1.4.9
@@ -63,2 +63,5 @@ "use strict"; | ||
} | ||
else if (protocol === "ws" || protocol === "wss") { | ||
return wsHandler(ctx, next, { timeout, currTraceId, encoding }); | ||
} | ||
else { | ||
@@ -77,2 +80,3 @@ return httpHandler(ctx, next, { timeout, currTraceId }); | ||
async function grpcHandler(ctx, next, ext) { | ||
var _a; | ||
const timeout = ext.timeout || 10000; | ||
@@ -82,16 +86,2 @@ // set ctx start time | ||
ctx.setMetaData("startTime", `${startTime}`); | ||
if (ctx.call) { | ||
let handler = {}; | ||
if (Object.hasOwnProperty.call(ctx.call, "handler")) { | ||
handler = Reflect.get(ctx.call, "handler") || {}; | ||
} | ||
else if (Object.hasOwnProperty.call(ctx.call, "call")) { | ||
const call = Reflect.get(ctx.call, "call") || {}; | ||
handler = call.handler || {}; | ||
} | ||
const cmd = handler.path || ''; | ||
// originalPath | ||
ctx.setMetaData("path", cmd); | ||
ctx.setMetaData("originalPath", cmd); | ||
} | ||
ctx.call.on("end", () => { | ||
@@ -114,3 +104,3 @@ const now = Date.now(); | ||
}), next()]); | ||
return res; | ||
return (_a = res !== null && res !== void 0 ? res : ctx.body) !== null && _a !== void 0 ? _a : ""; | ||
} | ||
@@ -137,3 +127,2 @@ catch (err) { | ||
const timeout = ext.timeout || 10000; | ||
const encoding = ext.encoding || 'utf-8'; | ||
// set ctx start time | ||
@@ -146,3 +135,3 @@ Helper.define(ctx, 'startTime', Date.now()); | ||
// Encoding | ||
ctx.encoding = encoding; | ||
ctx.encoding = ext.encoding; | ||
// auto send security header | ||
@@ -173,3 +162,3 @@ ctx.set('X-Powered-By', 'Koatty'); | ||
} | ||
return ctx.body; | ||
return null; | ||
} | ||
@@ -188,2 +177,58 @@ catch (err) { | ||
/** | ||
* wsHandler | ||
* | ||
* @param {Koatty} app | ||
* @returns {*} | ||
*/ | ||
async function wsHandler(ctx, next, ext) { | ||
var _a; | ||
const timeout = ext.timeout || 10000; | ||
// set ctx start time | ||
Helper.define(ctx, 'startTime', Date.now()); | ||
// http version | ||
Helper.define(ctx, 'version', ctx.req.httpVersion); | ||
// originalPath | ||
Helper.define(ctx, 'originalPath', ctx.path); | ||
// Encoding | ||
ctx.encoding = ext.encoding; | ||
// auto send security header | ||
ctx.set('X-Powered-By', 'Koatty'); | ||
ctx.set('X-Content-Type-Options', 'nosniff'); | ||
ctx.set('X-XSS-Protection', '1;mode=block'); | ||
// response finish | ||
ctx.res.once('finish', () => { | ||
const { method, startTime, status, originalPath } = ctx; | ||
const now = Date.now(); | ||
const cmd = originalPath || '/'; | ||
const msg = `{"action":"${method}","code":"${status}","startTime":"${startTime}","duration":"${(now - startTime) || 0}","traceId":"${ext.currTraceId}","endTime":"${now}","path":"${cmd}"}`; | ||
koatty_logger_1.DefaultLogger[(ctx.status >= 400 ? 'Error' : 'Info')](msg); | ||
ctx = null; | ||
}); | ||
// try /catch | ||
const response = ctx.res; | ||
try { | ||
response.timeout = null; | ||
// promise.race | ||
let res = await Promise.race([new Promise((resolve, reject) => { | ||
response.timeout = setTimeout(reject, timeout, new koatty_exception_1.Exception('Request Timeout', 1, 408)); | ||
return; | ||
}), next()]); | ||
if (res && ctx.status !== 304) { | ||
res = (_a = res !== null && res !== void 0 ? res : ctx.body) !== null && _a !== void 0 ? _a : ""; | ||
} | ||
return res; | ||
} | ||
catch (err) { | ||
// skip prevent errors | ||
if (koatty_exception_1.isPrevent(err)) { | ||
return null; | ||
} | ||
koatty_logger_1.DefaultLogger.Error(err); | ||
return null; | ||
} | ||
finally { | ||
clearTimeout(response.timeout); | ||
} | ||
} | ||
/** | ||
* error catcher | ||
@@ -190,0 +235,0 @@ * |
{ | ||
"name": "koatty_trace", | ||
"version": "1.4.7", | ||
"version": "1.4.9", | ||
"description": "Full link tracking and error interception for koatty.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2041490
587