@midwayjs/code-dye 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -0
- package/dist/codeDye.d.ts +2 -0
- package/dist/codeDye.js +121 -0
- package/dist/config/config.default.d.ts +3 -0
- package/dist/config/config.default.js +9 -0
- package/dist/configuration.d.ts +7 -0
- package/dist/configuration.js +57 -0
- package/dist/html.d.ts +3 -0
- package/dist/html.js +136 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +22 -0
- package/dist/interface.d.ts +6 -0
- package/dist/interface.js +3 -0
- package/dist/middleware.d.ts +8 -0
- package/dist/middleware.js +82 -0
- package/dist/reqInfo.d.ts +5 -0
- package/dist/reqInfo.js +51 -0
- package/index.d.ts +9 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
## Code-Dye 代码染色组件
|
|
2
|
+
|
|
3
|
+
适用于 `@midwayjs/faas` 、`@midwayjs/web` 、`@midwayjs/koa` 和 `@midwayjs/express` 多种框架的代码染色组件,清晰的展示调用链路耗时与各个方法的出入参,帮你更快地定位代码问题。
|
|
4
|
+

|
|
5
|
+
### Usage
|
|
6
|
+
|
|
7
|
+
1. 安装依赖
|
|
8
|
+
```shell
|
|
9
|
+
tnpm i @midwayjs/code-dye --save
|
|
10
|
+
```
|
|
11
|
+
2. 在 configuration 中引入组件,
|
|
12
|
+
```ts
|
|
13
|
+
import * as codeDye from '@midwayjs/code-dye';
|
|
14
|
+
@Configuration({
|
|
15
|
+
imports: [
|
|
16
|
+
// ...other components
|
|
17
|
+
codeDye
|
|
18
|
+
],
|
|
19
|
+
})
|
|
20
|
+
export class AutoConfiguration {}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
3. 请求你的接口,添加配置的 header 或 query 进入染色链路
|
|
24
|
+
|
|
25
|
+
例如,配置为:
|
|
26
|
+
```ts
|
|
27
|
+
// src/config/config.default.ts
|
|
28
|
+
export const codeDye = {
|
|
29
|
+
matchQueryKey: 'codeDyeABC',
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
然后请求你的接口,例如:http://127.0.0.1:7001/test?codeDyeABC=html
|
|
33
|
+
|
|
34
|
+
### 配置
|
|
35
|
+
```ts
|
|
36
|
+
export const codeDye = {
|
|
37
|
+
// 是否启用
|
|
38
|
+
enable: boolean;
|
|
39
|
+
// 匹配到对应的 header key时,进行代码染色,值可以为 html、json、log
|
|
40
|
+
matchHeaderKey: string;
|
|
41
|
+
// 匹配到对应的 query key时,进行代码染色
|
|
42
|
+
// 例如 http://127.0.0.1:7001/test?codeDye=html
|
|
43
|
+
matchQueryKey: string;
|
|
44
|
+
}
|
|
45
|
+
```
|
package/dist/codeDye.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.codeDye = void 0;
|
|
4
|
+
const reqInfo_1 = require("./reqInfo");
|
|
5
|
+
const copyObject = (obj, paths) => {
|
|
6
|
+
const properties = Object.getOwnPropertyNames(obj);
|
|
7
|
+
properties.forEach(property => {
|
|
8
|
+
if (property === 'constructor') {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const propertyInfo = Object.getOwnPropertyDescriptor(obj, property);
|
|
12
|
+
if (!propertyInfo.configurable) {
|
|
13
|
+
// skip un configurable
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
let isCanDefine = false;
|
|
17
|
+
if (propertyInfo.get) {
|
|
18
|
+
propertyInfo.get = (0, exports.codeDye)(propertyInfo.get, paths.concat('[prototype get] ' + property));
|
|
19
|
+
isCanDefine = true;
|
|
20
|
+
}
|
|
21
|
+
if (propertyInfo.set) {
|
|
22
|
+
propertyInfo.set = (0, exports.codeDye)(propertyInfo.set, paths.concat('[prototype set] ' + property));
|
|
23
|
+
isCanDefine = true;
|
|
24
|
+
}
|
|
25
|
+
if (propertyInfo.value) {
|
|
26
|
+
propertyInfo.value = (0, exports.codeDye)(propertyInfo.value, paths);
|
|
27
|
+
isCanDefine = true;
|
|
28
|
+
}
|
|
29
|
+
if (isCanDefine) {
|
|
30
|
+
Object.defineProperty(obj, property, propertyInfo);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const codeDyeAFuncWrapper = function (func, paths) {
|
|
35
|
+
return async function (...args) {
|
|
36
|
+
const info = (0, reqInfo_1.getAsyncInfo)();
|
|
37
|
+
const newInfo = {
|
|
38
|
+
codeDyeParent: info.codeDyeParent,
|
|
39
|
+
};
|
|
40
|
+
newInfo.call = [];
|
|
41
|
+
const id = (0, reqInfo_1.genId)();
|
|
42
|
+
newInfo.id = id;
|
|
43
|
+
newInfo.paths = paths;
|
|
44
|
+
newInfo.start = {
|
|
45
|
+
time: Date.now(),
|
|
46
|
+
args: args,
|
|
47
|
+
};
|
|
48
|
+
if (info.codeDyeParent && info.codeDyeParent.call) {
|
|
49
|
+
info.codeDyeParent.call.push(newInfo);
|
|
50
|
+
}
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
52
|
+
const _this = this;
|
|
53
|
+
const res = await (0, reqInfo_1.asyncRunWrapper)(info.codeDyeConfig, newInfo, async () => {
|
|
54
|
+
return func.call(_this, ...args);
|
|
55
|
+
});
|
|
56
|
+
newInfo.end = {
|
|
57
|
+
time: Date.now(),
|
|
58
|
+
result: res,
|
|
59
|
+
};
|
|
60
|
+
return res;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
const codeDyeFuncWrapper = function (func, paths) {
|
|
64
|
+
return function (...args) {
|
|
65
|
+
const info = (0, reqInfo_1.getAsyncInfo)();
|
|
66
|
+
const newInfo = {
|
|
67
|
+
codeDyeParent: info.codeDyeParent,
|
|
68
|
+
};
|
|
69
|
+
newInfo.call = [];
|
|
70
|
+
const id = (0, reqInfo_1.genId)();
|
|
71
|
+
newInfo.id = id;
|
|
72
|
+
newInfo.paths = paths;
|
|
73
|
+
newInfo.start = {
|
|
74
|
+
time: Date.now(),
|
|
75
|
+
args: args,
|
|
76
|
+
};
|
|
77
|
+
if (info.codeDyeParent && info.codeDyeParent.call) {
|
|
78
|
+
info.codeDyeParent.call.push(newInfo);
|
|
79
|
+
}
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
81
|
+
const _this = this;
|
|
82
|
+
const res = (0, reqInfo_1.asyncRunWrapper)(info.codeDyeConfig, newInfo, () => {
|
|
83
|
+
return func.call(_this, ...args);
|
|
84
|
+
});
|
|
85
|
+
newInfo.end = {
|
|
86
|
+
time: Date.now(),
|
|
87
|
+
result: res,
|
|
88
|
+
};
|
|
89
|
+
return res;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
const codeDye = (variable, paths) => {
|
|
93
|
+
if (!paths) {
|
|
94
|
+
paths = [];
|
|
95
|
+
}
|
|
96
|
+
if (paths[0] === __filename) {
|
|
97
|
+
return variable;
|
|
98
|
+
}
|
|
99
|
+
const type = {}.toString.call(variable).slice(8, -1).toLowerCase();
|
|
100
|
+
switch (type) {
|
|
101
|
+
case 'object':
|
|
102
|
+
copyObject(variable, paths);
|
|
103
|
+
return variable;
|
|
104
|
+
case 'array':
|
|
105
|
+
return variable.map((variable, index) => {
|
|
106
|
+
return (0, exports.codeDye)(variable, paths.concat('[index]' + index));
|
|
107
|
+
});
|
|
108
|
+
case 'function':
|
|
109
|
+
if (variable.toString().startsWith('class ')) {
|
|
110
|
+
// class
|
|
111
|
+
copyObject(variable.prototype, paths.concat(`[class] ${variable.name}`));
|
|
112
|
+
return variable;
|
|
113
|
+
}
|
|
114
|
+
return codeDyeFuncWrapper(variable, paths.concat('[func] ' + variable.name));
|
|
115
|
+
case 'asyncfunction':
|
|
116
|
+
return codeDyeAFuncWrapper(variable, paths.concat('[async func] ' + variable.name));
|
|
117
|
+
}
|
|
118
|
+
return variable;
|
|
119
|
+
};
|
|
120
|
+
exports.codeDye = codeDye;
|
|
121
|
+
//# sourceMappingURL=codeDye.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CodeDyeConfiguration = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const DefaultConfig = require("./config/config.default");
|
|
15
|
+
const core_1 = require("@midwayjs/core");
|
|
16
|
+
const middleware_1 = require("./middleware");
|
|
17
|
+
const codeDye_1 = require("./codeDye");
|
|
18
|
+
let CodeDyeConfiguration = class CodeDyeConfiguration {
|
|
19
|
+
async onReady(container) {
|
|
20
|
+
if (!this.codeDye.enable) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
this.applicationManager
|
|
24
|
+
.getApplications(['koa', 'faas', 'express', 'egg'])
|
|
25
|
+
.forEach(app => {
|
|
26
|
+
app.getMiddleware().insertFirst(middleware_1.CodeDyeMW);
|
|
27
|
+
});
|
|
28
|
+
// 将ioc内的代码进行包裹
|
|
29
|
+
for (const [key, value] of container.registry) {
|
|
30
|
+
if (!(value === null || value === void 0 ? void 0 : value.path) || !value.srcPath) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
value.path = (0, codeDye_1.codeDye)(value.path, [value.srcPath || key]);
|
|
34
|
+
container.registry[key] = value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
__decorate([
|
|
39
|
+
(0, decorator_1.Inject)(),
|
|
40
|
+
__metadata("design:type", core_1.MidwayApplicationManager)
|
|
41
|
+
], CodeDyeConfiguration.prototype, "applicationManager", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, decorator_1.Config)(),
|
|
44
|
+
__metadata("design:type", Object)
|
|
45
|
+
], CodeDyeConfiguration.prototype, "codeDye", void 0);
|
|
46
|
+
CodeDyeConfiguration = __decorate([
|
|
47
|
+
(0, decorator_1.Configuration)({
|
|
48
|
+
namespace: 'code-dye',
|
|
49
|
+
importConfigs: [
|
|
50
|
+
{
|
|
51
|
+
default: DefaultConfig,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
})
|
|
55
|
+
], CodeDyeConfiguration);
|
|
56
|
+
exports.CodeDyeConfiguration = CodeDyeConfiguration;
|
|
57
|
+
//# sourceMappingURL=configuration.js.map
|
package/dist/html.d.ts
ADDED
package/dist/html.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toHTML = exports.foreach = void 0;
|
|
4
|
+
const foreach = (info, start, pi, level) => {
|
|
5
|
+
const timeUse = info.end.time - info.start.time;
|
|
6
|
+
const timeDiff = info.start.time - start;
|
|
7
|
+
const paths = info.paths.filter(path => {
|
|
8
|
+
return !path.startsWith('/');
|
|
9
|
+
});
|
|
10
|
+
return `
|
|
11
|
+
<div class="item">
|
|
12
|
+
<div class="info" style="padding-left: ${(level - 1) * 24}px">
|
|
13
|
+
<div class="infoText">${paths.join(' / ')}</div>
|
|
14
|
+
<div class="moreInfo">
|
|
15
|
+
<div class="infoBtn">入参<div class="infoCard"><pre>${JSON.stringify(info.start.args, null, 2)}</pre></div></div>
|
|
16
|
+
<div class="infoBtn">返回值<div class="infoCard"><pre>${JSON.stringify(info.end.result, null, 2)}</pre></div></div>
|
|
17
|
+
</div>
|
|
18
|
+
</div>
|
|
19
|
+
<div class="timeContainer">
|
|
20
|
+
<div class="time" style="width:${timeUse / pi}px;left: ${timeDiff / pi}px"><div class="timeValue">${timeUse} ms</div></div>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
</div>
|
|
24
|
+
<div class="child">
|
|
25
|
+
<div class="childLine" style="left: ${(level - 1) * 24 + 12}px"></div>
|
|
26
|
+
${info.call
|
|
27
|
+
.map(info => {
|
|
28
|
+
return (0, exports.foreach)(info, start, pi, level + 1);
|
|
29
|
+
})
|
|
30
|
+
.join('')}
|
|
31
|
+
</div>
|
|
32
|
+
`;
|
|
33
|
+
};
|
|
34
|
+
exports.foreach = foreach;
|
|
35
|
+
const toHTML = info => {
|
|
36
|
+
const timeDiff = info.end.time - info.start.time;
|
|
37
|
+
let pi = timeDiff / 600;
|
|
38
|
+
if (pi < 1) {
|
|
39
|
+
pi = 1;
|
|
40
|
+
}
|
|
41
|
+
return `
|
|
42
|
+
<title>Midway CodeDye</title>
|
|
43
|
+
<meta charset="utf-8" />
|
|
44
|
+
<style>
|
|
45
|
+
.item {
|
|
46
|
+
display: flex;
|
|
47
|
+
flex-wrap: nowrap;
|
|
48
|
+
height: 24px;
|
|
49
|
+
}
|
|
50
|
+
.info {
|
|
51
|
+
position: relative;
|
|
52
|
+
width: 360px;
|
|
53
|
+
box-sizing: border-box;
|
|
54
|
+
border-right: 1px solid #eee;
|
|
55
|
+
height: 24px;
|
|
56
|
+
padding: 0 12px;
|
|
57
|
+
padding-right: 60px;
|
|
58
|
+
}
|
|
59
|
+
.infoText {
|
|
60
|
+
height: 24px;
|
|
61
|
+
text-overflow: ellipsis;
|
|
62
|
+
overflow: hidden;
|
|
63
|
+
font-size: 13px;
|
|
64
|
+
line-height: 24px;
|
|
65
|
+
white-space: nowrap;
|
|
66
|
+
}
|
|
67
|
+
.moreInfo {
|
|
68
|
+
position: absolute;
|
|
69
|
+
right: 6px;
|
|
70
|
+
top: 0;
|
|
71
|
+
display: flex;
|
|
72
|
+
}
|
|
73
|
+
.timeContainer {
|
|
74
|
+
position: relative;
|
|
75
|
+
margin-left: 12px;
|
|
76
|
+
height: 24px;
|
|
77
|
+
}
|
|
78
|
+
.time {
|
|
79
|
+
position: absolute;
|
|
80
|
+
height: 16px;
|
|
81
|
+
min-width: 12px;
|
|
82
|
+
background-color: #66c;
|
|
83
|
+
border-radius: 3px;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.timeValue {
|
|
87
|
+
position: absolute;
|
|
88
|
+
line-height: 16px;
|
|
89
|
+
top: 0;
|
|
90
|
+
right: 0;
|
|
91
|
+
transform: translate(110%, 0);
|
|
92
|
+
white-space: nowrap;
|
|
93
|
+
}
|
|
94
|
+
.infoBtn {
|
|
95
|
+
position: relative;
|
|
96
|
+
cursor: pointer;
|
|
97
|
+
margin-left: 6px;
|
|
98
|
+
border-radius: 3px;
|
|
99
|
+
background: #eee;
|
|
100
|
+
padding: 2px;
|
|
101
|
+
line-height: 14px;
|
|
102
|
+
font-size: 12px;
|
|
103
|
+
}
|
|
104
|
+
.infoBtn:hover .infoCard {
|
|
105
|
+
display: block;
|
|
106
|
+
}
|
|
107
|
+
.infoCard {
|
|
108
|
+
display: none;
|
|
109
|
+
position: absolute;
|
|
110
|
+
background: #eee;
|
|
111
|
+
z-index: 3;
|
|
112
|
+
padding: 12px;
|
|
113
|
+
}
|
|
114
|
+
.child {
|
|
115
|
+
position: relative;
|
|
116
|
+
}
|
|
117
|
+
.childLine {
|
|
118
|
+
position: absolute;
|
|
119
|
+
top: 0;
|
|
120
|
+
height: 100%;
|
|
121
|
+
width: 2px;
|
|
122
|
+
border-radius: 3px;
|
|
123
|
+
background: #ccc;
|
|
124
|
+
}
|
|
125
|
+
</style>
|
|
126
|
+
<h1>Midway CodeDye</h1><hr />
|
|
127
|
+
<div>调用链路总耗时 ${timeDiff}ms<div>
|
|
128
|
+
<div>调用结果:<br /><pre>${JSON.stringify(info.end.result, null, 2)}</pre></div>
|
|
129
|
+
<hr />
|
|
130
|
+
<div style="padding: 12px;">
|
|
131
|
+
${(0, exports.foreach)(info, info.start.time, pi, 1)}</div>
|
|
132
|
+
<hr /><a href="https://www.midwayjs.org/">Powered by Midway.js</a>
|
|
133
|
+
`;
|
|
134
|
+
};
|
|
135
|
+
exports.toHTML = toHTML;
|
|
136
|
+
//# sourceMappingURL=html.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Configuration = void 0;
|
|
18
|
+
var configuration_1 = require("./configuration");
|
|
19
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.CodeDyeConfiguration; } });
|
|
20
|
+
__exportStar(require("./middleware"), exports);
|
|
21
|
+
__exportStar(require("./interface"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CodeDyeOptions } from './interface';
|
|
2
|
+
export declare class CodeDyeMW {
|
|
3
|
+
codeDye: CodeDyeOptions;
|
|
4
|
+
resolve(app: any): (req: any, res: any, next: any) => Promise<any>;
|
|
5
|
+
check(request: any): any;
|
|
6
|
+
compatibleMiddleware(request: any, response: any, next: any): Promise<any>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CodeDyeMW = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const html_1 = require("./html");
|
|
15
|
+
const reqInfo_1 = require("./reqInfo");
|
|
16
|
+
let CodeDyeMW = class CodeDyeMW {
|
|
17
|
+
resolve(app) {
|
|
18
|
+
if (app.getFrameworkType() === decorator_1.MidwayFrameworkType.WEB_EXPRESS) {
|
|
19
|
+
return async (req, res, next) => {
|
|
20
|
+
return this.compatibleMiddleware(req, res, next);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return async (ctx, next) => {
|
|
25
|
+
return this.compatibleMiddleware(ctx.request, ctx, next);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
check(request) {
|
|
30
|
+
let outputType;
|
|
31
|
+
if (this.codeDye.matchQueryKey &&
|
|
32
|
+
request.query[this.codeDye.matchQueryKey]) {
|
|
33
|
+
outputType = request.query[this.codeDye.matchQueryKey];
|
|
34
|
+
}
|
|
35
|
+
else if (this.codeDye.matchHeaderKey &&
|
|
36
|
+
request.headers[this.codeDye.matchHeaderKey]) {
|
|
37
|
+
outputType = request.headers[this.codeDye.matchQueryKey];
|
|
38
|
+
}
|
|
39
|
+
if (!outputType) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return outputType;
|
|
43
|
+
}
|
|
44
|
+
async compatibleMiddleware(request, response, next) {
|
|
45
|
+
const reqInfo = {
|
|
46
|
+
call: [],
|
|
47
|
+
};
|
|
48
|
+
const outputType = this.check(request);
|
|
49
|
+
if (!outputType) {
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
52
|
+
return (0, reqInfo_1.asyncRunWrapper)(this.codeDye, reqInfo, async () => {
|
|
53
|
+
const res = await next();
|
|
54
|
+
const reqInfoJSON = JSON.stringify(reqInfo, (key, value) => {
|
|
55
|
+
if (key === 'codeDyeConfig' || key === 'codeDyeParent') {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}, 0);
|
|
60
|
+
if (outputType === 'log') {
|
|
61
|
+
console.log(reqInfoJSON);
|
|
62
|
+
}
|
|
63
|
+
else if (outputType === 'html') {
|
|
64
|
+
response.set('Content-Type', 'text/html');
|
|
65
|
+
return (0, html_1.toHTML)(reqInfo.call[0]);
|
|
66
|
+
}
|
|
67
|
+
else if (outputType === 'json') {
|
|
68
|
+
return reqInfoJSON;
|
|
69
|
+
}
|
|
70
|
+
return res;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
__decorate([
|
|
75
|
+
(0, decorator_1.Config)('codeDye'),
|
|
76
|
+
__metadata("design:type", Object)
|
|
77
|
+
], CodeDyeMW.prototype, "codeDye", void 0);
|
|
78
|
+
CodeDyeMW = __decorate([
|
|
79
|
+
(0, decorator_1.Middleware)()
|
|
80
|
+
], CodeDyeMW);
|
|
81
|
+
exports.CodeDyeMW = CodeDyeMW;
|
|
82
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const genId: () => string;
|
|
2
|
+
export declare const getAsyncLocalStoreage: () => any;
|
|
3
|
+
export declare const getAsyncInfo: () => any;
|
|
4
|
+
export declare const asyncRunWrapper: (codeDyeConfig: any, parentInfo: any, fun: any) => any;
|
|
5
|
+
//# sourceMappingURL=reqInfo.d.ts.map
|
package/dist/reqInfo.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.asyncRunWrapper = exports.getAsyncInfo = exports.getAsyncLocalStoreage = exports.genId = void 0;
|
|
4
|
+
let asyncStorage;
|
|
5
|
+
const asyncNotSupport = 'un';
|
|
6
|
+
let index = 0;
|
|
7
|
+
const genId = () => {
|
|
8
|
+
index++;
|
|
9
|
+
if (index > 100000000) {
|
|
10
|
+
index = 0;
|
|
11
|
+
}
|
|
12
|
+
return `${Date.now()}:${index}:${Math.ceil(Math.random() * 10000)}`;
|
|
13
|
+
};
|
|
14
|
+
exports.genId = genId;
|
|
15
|
+
const getAsyncLocalStoreage = () => {
|
|
16
|
+
if (asyncStorage) {
|
|
17
|
+
return asyncStorage;
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
const ALSClass = require('async_hooks').AsyncLocalStorage;
|
|
21
|
+
asyncStorage = new ALSClass();
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
asyncStorage = asyncNotSupport;
|
|
25
|
+
}
|
|
26
|
+
return asyncStorage;
|
|
27
|
+
};
|
|
28
|
+
exports.getAsyncLocalStoreage = getAsyncLocalStoreage;
|
|
29
|
+
const getAsyncInfo = () => {
|
|
30
|
+
const asyncStorage = (0, exports.getAsyncLocalStoreage)();
|
|
31
|
+
if (asyncStorage === asyncNotSupport) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
const info = asyncStorage.getStore();
|
|
35
|
+
return info || {};
|
|
36
|
+
};
|
|
37
|
+
exports.getAsyncInfo = getAsyncInfo;
|
|
38
|
+
const asyncRunWrapper = (codeDyeConfig, parentInfo, fun) => {
|
|
39
|
+
const asyncStorage = (0, exports.getAsyncLocalStoreage)();
|
|
40
|
+
if (asyncStorage === asyncNotSupport) {
|
|
41
|
+
return fun();
|
|
42
|
+
}
|
|
43
|
+
return asyncStorage.run({
|
|
44
|
+
codeDyeConfig,
|
|
45
|
+
codeDyeParent: parentInfo,
|
|
46
|
+
}, () => {
|
|
47
|
+
return fun();
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
exports.asyncRunWrapper = asyncRunWrapper;
|
|
51
|
+
//# sourceMappingURL=reqInfo.js.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/code-dye",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Midway Code Dye Component",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
11
|
+
"ci": "npm run test"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/**/*.js",
|
|
17
|
+
"dist/**/*.d.ts",
|
|
18
|
+
"index.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@midwayjs/core": "^3.4.0-beta.2",
|
|
28
|
+
"@midwayjs/decorator": "^3.4.0-beta.2",
|
|
29
|
+
"@midwayjs/express": "^3.4.0-beta.2",
|
|
30
|
+
"@midwayjs/faas": "^3.4.0-beta.2",
|
|
31
|
+
"@midwayjs/koa": "^3.4.0-beta.2",
|
|
32
|
+
"@midwayjs/mock": "^3.4.0-beta.2",
|
|
33
|
+
"@midwayjs/serverless-app": "^3.4.0-beta.2",
|
|
34
|
+
"@midwayjs/web": "^3.4.0-beta.2"
|
|
35
|
+
}
|
|
36
|
+
}
|