koatty_trace
Advanced tools
Comparing version 1.5.4 to 1.6.0-0
@@ -5,2 +5,4 @@ # Changelog | ||
## [1.6.0-0](https://github.com/koatty/koatty_trace/compare/v1.5.4...v1.6.0-0) (2022-02-18) | ||
### [1.5.4](https://github.com/koatty/koatty_trace/compare/v1.5.4-0...v1.5.4) (2022-02-14) | ||
@@ -7,0 +9,0 @@ |
/*! | ||
* @Author: richen | ||
* @Date: 2022-02-14 14:44:11 | ||
* @Date: 2022-02-18 19:05:31 | ||
* @License: BSD (3-Clause) | ||
@@ -5,0 +5,0 @@ * @Copyright (c) - <richenlin(at)gmail.com> |
/*! | ||
* @Author: richen | ||
* @Date: 2022-02-14 14:43:58 | ||
* @Date: 2022-02-18 19:05:04 | ||
* @License: BSD (3-Clause) | ||
@@ -19,3 +19,7 @@ * @Copyright (c) - <richenlin(at)gmail.com> | ||
var util = require('util'); | ||
var statuses = require('statuses'); | ||
var stream = require('stream'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
function _interopNamespace(e) { | ||
@@ -40,2 +44,3 @@ if (e && e.__esModule) return e; | ||
var Helper__namespace = /*#__PURE__*/_interopNamespace(Helper); | ||
var statuses__default = /*#__PURE__*/_interopDefaultLegacy(statuses); | ||
@@ -47,3 +52,3 @@ /* | ||
* @Date: 2021-11-18 10:44:51 | ||
* @LastEditTime: 2021-11-19 00:21:44 | ||
* @LastEditTime: 2022-02-16 16:59:32 | ||
*/ | ||
@@ -138,3 +143,3 @@ const isWrappedSymbol = Symbol('cls-tracer-is-wrapped'); | ||
koatty_logger.DefaultLogger[(ctx.status >= 400 ? 'Error' : 'Info')](msg); | ||
ctx = null; | ||
// ctx = null; | ||
}); | ||
@@ -150,5 +155,3 @@ // try /catch | ||
}), next()]); | ||
if (res && ctx.status !== 304) { | ||
ctx.body = res !== null && res !== void 0 ? res : ""; | ||
} | ||
ctx.body = res !== null && res !== void 0 ? res : ""; | ||
return null; | ||
@@ -188,9 +191,2 @@ } | ||
/* | ||
* @Description: | ||
* @Usage: | ||
* @Author: richen | ||
* @Date: 2021-11-19 00:23:06 | ||
* @LastEditTime: 2022-02-14 10:12:38 | ||
*/ | ||
/** | ||
@@ -218,5 +214,5 @@ * grpcHandler | ||
koatty_logger.DefaultLogger[(status > 0 ? 'Error' : 'Info')](msg); | ||
ctx = null; | ||
// ctx = null; | ||
}; | ||
ctx.rpc.call.once("end", listener); | ||
ctx.res.once("finish", listener); | ||
ctx.rpc.call.once("error", listener); | ||
@@ -232,3 +228,7 @@ // try /catch | ||
}), next()]); | ||
ctx.rpc.callback(null, (_a = res !== null && res !== void 0 ? res : ctx.body) !== null && _a !== void 0 ? _a : ""); | ||
ctx.body = (_a = res !== null && res !== void 0 ? res : ctx.body) !== null && _a !== void 0 ? _a : ""; | ||
if (ctx.body && ctx.status === 404) { | ||
ctx.status = 200; | ||
} | ||
ctx.rpc.callback(null, ctx.body); | ||
return null; | ||
@@ -241,3 +241,3 @@ } | ||
finally { | ||
ctx.rpc.call.emit("end"); | ||
ctx.res.emit("finish"); | ||
clearTimeout(response.timeout); | ||
@@ -277,3 +277,3 @@ } | ||
* @Date: 2021-11-19 00:24:43 | ||
* @LastEditTime: 2022-02-14 10:13:08 | ||
* @LastEditTime: 2022-02-18 19:01:31 | ||
*/ | ||
@@ -306,10 +306,12 @@ /** | ||
koatty_logger.DefaultLogger[(ctx.status >= 400 ? 'Error' : 'Info')](msg); | ||
ctx = null; | ||
// ctx = null; | ||
}; | ||
ctx.websocket.once("afterSend", listener); | ||
ctx.websocket.once("error", listener); | ||
// close event | ||
ctx.websocket.once("close", (socket, code, reason) => { | ||
koatty_logger.DefaultLogger.Error("websocket closed: ", Helper__namespace.toString(reason)); | ||
}); | ||
ctx.res.once("finish", listener); | ||
// ctx.websocket.once("error", listener); | ||
// ctx.websocket.once("connection", () => { | ||
// Logger.Info("websocket connected"); | ||
// }); | ||
// ctx.websocket.once("close", (socket: any, code: number, reason: Buffer) => { | ||
// Logger.Error("websocket closed: ", Helper.toString(reason)); | ||
// }); | ||
// try /catch | ||
@@ -320,8 +322,11 @@ const response = ctx.res; | ||
// promise.race | ||
let res = await Promise.race([new Promise((resolve, reject) => { | ||
const res = await Promise.race([new Promise((resolve, reject) => { | ||
response.timeout = setTimeout(reject, timeout, new koatty_exception.Exception('Request Timeout', 1, 408)); | ||
return; | ||
}), next()]); | ||
res = util.inspect((_a = res !== null && res !== void 0 ? res : ctx.body) !== null && _a !== void 0 ? _a : ""); | ||
ctx.websocket.send(util.inspect(res), () => ctx.websocket.emit('afterSend')); | ||
ctx.body = util.inspect((_a = res !== null && res !== void 0 ? res : ctx.body) !== null && _a !== void 0 ? _a : ""); | ||
if (ctx.body && ctx.status === 404) { | ||
ctx.status = 200; | ||
} | ||
ctx.websocket.send(ctx.body, null); | ||
return null; | ||
@@ -334,2 +339,3 @@ } | ||
finally { | ||
ctx.res.emit("finish"); | ||
clearTimeout(response.timeout); | ||
@@ -363,3 +369,78 @@ } | ||
/* | ||
* @Description: | ||
* @Usage: | ||
* @Author: richen | ||
* @Date: 2022-02-18 16:55:05 | ||
* @LastEditTime: 2022-02-18 17:57:01 | ||
*/ | ||
/** | ||
* Response helper. | ||
* A copy of koa respond: https://github.com/koajs/koa/blob/aa816ca523e0f7f3ca7623163762a2e63a7b0ee3/lib/application.js#L220 | ||
* | ||
* @param {KoattyContext} ctx | ||
* @returns {*} | ||
*/ | ||
function respond(ctx) { | ||
// allow bypassing koa | ||
if (false === ctx.respond) | ||
return; | ||
if (!ctx.writable) | ||
return; | ||
const res = ctx.res; | ||
let body = ctx.body; | ||
const code = ctx.status; | ||
// ignore body | ||
if (statuses__default["default"].empty[code]) { | ||
// strip headers | ||
ctx.body = null; | ||
return res.end(); | ||
} | ||
if ('HEAD' === ctx.method) { | ||
if (!res.headersSent && !ctx.response.has('Content-Length')) { | ||
const { length } = ctx.response; | ||
if (Number.isInteger(length)) | ||
ctx.length = length; | ||
} | ||
return res.end(); | ||
} | ||
// status body | ||
if (null == body) { | ||
if (ctx.response._explicitNullBody) { | ||
ctx.response.remove('Content-Type'); | ||
ctx.response.remove('Transfer-Encoding'); | ||
return res.end(); | ||
} | ||
if (ctx.req.httpVersionMajor >= 2) { | ||
body = String(code); | ||
} | ||
else { | ||
body = ctx.message || String(code); | ||
} | ||
if (!res.headersSent) { | ||
ctx.type = 'text'; | ||
ctx.length = Buffer.byteLength(body); | ||
} | ||
return res.end(body); | ||
} | ||
// status | ||
if (code === 404) { | ||
ctx.status = 200; | ||
} | ||
// responses | ||
if (Buffer.isBuffer(body)) | ||
return res.end(body); | ||
if ('string' === typeof body) | ||
return res.end(body); | ||
if (body instanceof stream.Stream) | ||
return body.pipe(res); | ||
// body: json | ||
body = JSON.stringify(body); | ||
if (!res.headersSent) { | ||
ctx.length = Buffer.byteLength(body); | ||
} | ||
res.end(body); | ||
} | ||
/** | ||
* GetTraceId | ||
@@ -395,13 +476,16 @@ * | ||
// | ||
const respWapper = (currTraceId) => { | ||
const respWapper = async (currTraceId) => { | ||
// metadata | ||
ctx.setMetaData(options.HeaderName, currTraceId); | ||
if (ctx.protocol === "grpc") { | ||
// allow bypassing koa | ||
ctx.respond = false; | ||
ctx.rpc.call.metadata.set(options.HeaderName, currTraceId); | ||
return grpcHandler(ctx, next, { timeout, currTraceId, encoding }); | ||
await grpcHandler(ctx, next, { timeout, currTraceId, encoding }); | ||
} | ||
else if (ctx.protocol === "ws" || ctx.protocol === "wss") { | ||
// response header | ||
// allow bypassing koa | ||
ctx.respond = false; | ||
ctx.set(options.HeaderName, currTraceId); | ||
return wsHandler(ctx, next, { timeout, currTraceId, encoding }); | ||
await wsHandler(ctx, next, { timeout, currTraceId, encoding }); | ||
} | ||
@@ -411,4 +495,5 @@ else { | ||
ctx.set(options.HeaderName, currTraceId); | ||
return httpHandler(ctx, next, { timeout, currTraceId, encoding }); | ||
await httpHandler(ctx, next, { timeout, currTraceId, encoding }); | ||
} | ||
return respond(ctx); | ||
}; | ||
@@ -415,0 +500,0 @@ let currTraceId = ''; |
{ | ||
"name": "koatty_trace", | ||
"version": "1.5.4", | ||
"version": "1.6.0-0", | ||
"description": "Full link tracking and error interception for koatty.", | ||
@@ -69,2 +69,3 @@ "scripts": { | ||
"@types/node": "^16.x.x", | ||
"@types/statuses": "^2.0.0", | ||
"@types/uuid": "^8.x.x", | ||
@@ -92,6 +93,7 @@ "@types/ws": "^8.x.x", | ||
"dependencies": { | ||
"koatty_container": "^1.x.x", | ||
"koatty_core": "^1.x.x", | ||
"koatty_container": "^1.x.x", | ||
"koatty_exception": "^1.x.x", | ||
"koatty_lib": "^1.x.x", | ||
"statuses": "^2.0.1", | ||
"uuid": "^8.3.2" | ||
@@ -98,0 +100,0 @@ }, |
@@ -1,1 +0,1 @@ | ||
window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":0,"numPassedTests":0,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":0,"numTotalTests":0,"startTime":1644821036644,"success":false,"testResults":[],"wasInterrupted":false,"config":{"bail":0,"changedFilesWithAncestor":false,"collectCoverage":true,"collectCoverageFrom":[],"coverageDirectory":"coverage","coverageProvider":"babel","coverageReporters":["html","lcov","json","text","clover","text-summary"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":7,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"passWithNoTests":true,"projects":[],"reporters":[["default",{}],["/Users/richen/Workspace/nodejs/koatty_trace/node_modules/jest-html-reporters/index.js",{}]],"rootDir":"/Users/richen/Workspace/nodejs/koatty_trace","runTestsByPath":false,"skipFilter":false,"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/Users/richen/Workspace/nodejs/koatty_trace/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true},"endTime":1644821036710,"_reporterOptions":{"publicPath":"/Users/richen/Workspace/nodejs/koatty_trace","filename":"jest_html_reporters.html","expand":false,"pageTitle":"","hideIcon":false,"testCommand":"npx jest","openReport":false,"failureMessageOnly":false,"enableMergeData":false,"dataMergeLevel":1},"attachInfos":{}}) | ||
window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":0,"numPassedTests":0,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":0,"numTotalTests":0,"startTime":1645182300501,"success":false,"testResults":[],"wasInterrupted":false,"config":{"bail":0,"changedFilesWithAncestor":false,"collectCoverage":true,"collectCoverageFrom":[],"coverageDirectory":"coverage","coverageProvider":"babel","coverageReporters":["html","lcov","json","text","clover","text-summary"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":7,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"passWithNoTests":true,"projects":[],"reporters":[["default",{}],["/Users/richen/Workspace/nodejs/koatty_trace/node_modules/jest-html-reporters/index.js",{}]],"rootDir":"/Users/richen/Workspace/nodejs/koatty_trace","runTestsByPath":false,"skipFilter":false,"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/Users/richen/Workspace/nodejs/koatty_trace/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true},"endTime":1645182300609,"_reporterOptions":{"publicPath":"/Users/richen/Workspace/nodejs/koatty_trace","filename":"jest_html_reporters.html","expand":false,"pageTitle":"","hideIcon":false,"testCommand":"npx jest","openReport":false,"failureMessageOnly":false,"enableMergeData":false,"dataMergeLevel":1},"attachInfos":{}}) |
{ | ||
"name": "koatty_trace", | ||
"version": "1.5.4", | ||
"version": "1.6.0-0", | ||
"description": "Full link tracking and error interception for koatty.", | ||
@@ -69,2 +69,3 @@ "scripts": { | ||
"@types/node": "^16.x.x", | ||
"@types/statuses": "^2.0.0", | ||
"@types/uuid": "^8.x.x", | ||
@@ -92,6 +93,7 @@ "@types/ws": "^8.x.x", | ||
"dependencies": { | ||
"koatty_container": "^1.x.x", | ||
"koatty_core": "^1.x.x", | ||
"koatty_container": "^1.x.x", | ||
"koatty_exception": "^1.x.x", | ||
"koatty_lib": "^1.x.x", | ||
"statuses": "^2.0.1", | ||
"uuid": "^8.3.2" | ||
@@ -98,0 +100,0 @@ }, |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1783222
5163
0
9
28
1
+ Addedstatuses@^2.0.1
+ Addedkoatty_trace@1.13.1(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addeduuid@9.0.1(transitive)