Skip to content

Commit 3833905

Browse files
committed
调试&实时日志
1 parent a999e1f commit 3833905

File tree

11 files changed

+539
-2
lines changed

11 files changed

+539
-2
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,79 @@
55

66
## 已支持能力
77

8+
- [在线调试&实时日志功能](#在线调试&实时日志功能)
89
- [实时日志功能](#实时日志功能)
910
- [获取用户信息](#获取用户信息)
1011
- [一键登录功能](#一键登录功能)
1112
- [检测用户实名](#检测用户实名)
1213
- [判断中国用户](#判断中国用户)
1314
- [ServerlessApi](#ServerlessApi)
1415

15-
1616
## 基本功能
1717

18+
### 在线调试&实时日志功能
19+
基本使用方法(开启调试和实时日志):
20+
```javascript
21+
const tencentCloudTools = require('../../serverless-tencent-tools')
22+
const sdk = tencentCloudTools.Debug
23+
const region = 'ap-aaaaa'
24+
const auth = {
25+
SecretId: '****',
26+
SecretKey: '*****'
27+
}
28+
const func = {
29+
functionName: 'course'
30+
}
31+
console.log(sdk.remoteDebug(auth, func, region))
32+
33+
```
34+
输出结果:无
35+
36+
输入参数:
37+
38+
| 参数 | 必须 | 默认 | 描述 |
39+
| --- | --- | --- | ---|
40+
| auth || - | 鉴权信息 |
41+
| func || - | 函数信息 |
42+
| region || ap-guangzhou | 地域 |
43+
44+
auth参数描述:
45+
46+
| 参数 | 必须 | 默认 | 描述 |
47+
| --- | --- | --- | ---|
48+
| SecretId || - | 用户密钥Id |
49+
| SecretKey || - | 用户密钥Key |
50+
| token || - | 临时密钥需要传递此参数 |
51+
52+
func参数描述:
53+
54+
| 参数 | 必须 | 默认 | 描述 |
55+
| --- | --- | --- | ---|
56+
| functionName || - | 地域 |
57+
| nameSpace || default | 命名空间 |
58+
| qualifier || $LATEST | 版本 |
59+
60+
输出参数:
61+
62+
无输出。调试和实时日志能力都集成在接口里面实现
63+
64+
基本使用方法(结束调试模式):
65+
```javascript
66+
const tencentCloudTools = require('../../serverless-tencent-tools')
67+
const sdk = tencentCloudTools.Debug
68+
const region = 'ap-aaaaa'
69+
const auth = {
70+
SecretId: '****',
71+
SecretKey: '*****'
72+
}
73+
const func = {
74+
functionName: 'course'
75+
}
76+
console.log(sdk.stop(auth, func, region))
77+
78+
```
79+
输出结果:stop接口返回内容
80+
1881
### 实时日志功能
1982
基本使用方法(getAddr):
2083
```javascript

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
"author": "Dfounderliu",
1919
"license": "Apache",
2020
"dependencies": {
21-
"dijkstrajs": "^1.0.1"
21+
"@tencent-sdk/capi": "^0.2.15-alpha.0",
22+
"dijkstrajs": "^1.0.1",
23+
"duplexify": "^4.1.1",
24+
"end-of-stream": "^1.4.4",
25+
"https-proxy-agent": "^5.0.0",
26+
"socket.io-client": "^2.3.0",
27+
"socket.io-stream": "^0.9.1",
28+
"winston": "^3.2.1"
2229
},
2330
"devDependencies": {
2431
"babel-eslint": "9.0.0",

sdk/debug/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const ApiRequest = require("./lib/apiRequest")
2+
const WshubClient = require("./lib/client")
3+
4+
module.exports = {
5+
ApiRequest,
6+
WshubClient,
7+
remoteDebug: async function (auth, func, Region = 'ap-guangzhou') {
8+
const request = new ApiRequest(auth, func, Region)
9+
await request.startDebugging()
10+
const { Url, Token } = await request.getDebuggingInfo()
11+
if (!Url || !Token) {
12+
throw Error('Get debugging info error: the remote Url or Token does not exist.');
13+
}
14+
const client = new WshubClient({ Url, Token })
15+
try {
16+
await client.forwardDebug()
17+
console.log('Debugging listening on ws://127.0.0.1:9222.')
18+
console.log('For help see https://nodejs.org/en/docs/inspector.')
19+
console.log('Please open chorme, and visit chrome://inspect, click [Open dedicated DevTools for Node] to debug your code.')
20+
} catch (e) {
21+
console.error('Debug init error. Please confirm if the local port 9222 is used');
22+
}
23+
24+
console.log('--------------------- The realtime log ---------------------')
25+
await client.forwardLog()
26+
},
27+
stop: async function (auth, func, Region = 'ap-guangzhou') {
28+
const request = new ApiRequest(auth, func, Region)
29+
return request.stopDebugging()
30+
}
31+
32+
};

sdk/debug/lib/apiRequest.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const { Capi } = require('@tencent-sdk/capi');
2+
3+
const ApiRequest = function (auth, func, Region, debugOptions) {
4+
const { SecretId, SecretKey } = auth
5+
const Token = auth.Token || auth.token
6+
const body = {
7+
FunctionName: func.functionName || func.FunctionName,
8+
Namespace: func.nameSpace || func.Namespace || 'default',
9+
Qualifier: func.Qualifier || func.qualifier || '$LATEST'
10+
}
11+
if (!SecretId || !SecretKey) {
12+
throw Error('The SecretId or SecretKey does not exist.');
13+
}
14+
if (!body.FunctionName) {
15+
throw Error('The FunctionName does not exist.');
16+
}
17+
18+
this.client = new Capi({
19+
Region,
20+
SecretId,
21+
SecretKey,
22+
Token,
23+
ServiceType: 'scf',
24+
baseHost: 'tencentcloudapi.com'
25+
});
26+
this.commonParams = {
27+
Version: '2018-04-16',
28+
...body
29+
}
30+
this.debugOptions = debugOptions
31+
}
32+
33+
ApiRequest.prototype.request = async function (action, params) {
34+
return this.client.request(
35+
{
36+
Action: action,
37+
...this.commonParams,
38+
...params,
39+
},
40+
this.debugOptions,
41+
true
42+
);
43+
}
44+
45+
ApiRequest.prototype.startDebugging = async function (params) {
46+
return this.request('StartDebugging', params);
47+
}
48+
49+
ApiRequest.prototype.stopDebugging = async function (params) {
50+
return this.request('StopDebugging', params);
51+
}
52+
53+
ApiRequest.prototype.getDebuggingInfo = async function (params) {
54+
const getDebuggingInfoPm = () => {
55+
return new Promise((resolve, reject) => {
56+
let debuggingInfo
57+
let count = 0
58+
let timer = setInterval(async () => {
59+
try {
60+
count++
61+
if (count > 20) {
62+
clearInterval(timer)
63+
reject({
64+
"Response":
65+
{
66+
"Error":
67+
{
68+
"Code": "Timeout",
69+
"Message": "Get debugging info timeout."
70+
}
71+
}
72+
})
73+
}
74+
debuggingInfo = await this.request('GetDebuggingInfo', params) || {}
75+
const { Response = {} } = debuggingInfo
76+
if (Response.Status === 'Active') {
77+
resolve(Response.DebuggingInfo)
78+
clearInterval(timer)
79+
}
80+
} catch (e) {
81+
clearInterval(timer)
82+
reject(e)
83+
}
84+
}, 2000);
85+
})
86+
}
87+
return getDebuggingInfoPm()
88+
}
89+
90+
module.exports = ApiRequest

sdk/debug/lib/client.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const { default: connect } = require('./wshub-client');
2+
const { default: createLogger } = require('./wshub-logger');
3+
4+
const duplex = require('duplexify');
5+
6+
const Client = function (options) {
7+
const {
8+
Url,
9+
Token,
10+
logType = 'error',
11+
localPort = 9222,
12+
debugRemotePort = 9000,
13+
logRemotePort = 3332,
14+
timeout,
15+
} = options
16+
//日志类型,verbose或者info,测试阶段建议verbose
17+
this.logger = createLogger(logType)
18+
//映射的本地端口,默认9222
19+
this.localPort = localPort
20+
//远端调试端口,默认9000
21+
this.debugRemotePort = debugRemotePort
22+
//远端日志端口,默认3222
23+
this.logRemotePort = logRemotePort
24+
//连接wshub的超时时间
25+
this.timeout = timeout
26+
this.Url = Url
27+
this.Token = Token
28+
}
29+
30+
/**
31+
* connect to ws-hub service
32+
* @param url ws-hub service url
33+
* @param token ws-hub token
34+
* @returns the client promise
35+
*/
36+
Client.prototype._connect = async function () {
37+
return connect({ url: this.Url, token: this.Token, logger: this.logger, timeout: this.timeout })
38+
}
39+
40+
41+
/**
42+
* forward for debug
43+
* @param client ws-hub client
44+
* @returns the client local port
45+
*/
46+
Client.prototype.forwardDebug = async function () {
47+
if (!this.client) {
48+
this.client = await this._connect()
49+
}
50+
return this.client.forward(this.localPort, this.debugRemotePort);
51+
}
52+
53+
/**
54+
* forward for real time log
55+
* @param client ws-hub client
56+
* @returns the client local port
57+
*/
58+
Client.prototype.forwardLog = async function () {
59+
if (!this.client) {
60+
this.client = await this._connect()
61+
}
62+
return this.client.forward(duplex(process.stdout, null, { end: false }), this.logRemotePort)
63+
}
64+
65+
module.exports = Client
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var eos = require("end-of-stream");
4+
function pipe(stream1, stream2, cb) {
5+
stream1.pipe(stream2);
6+
stream2.pipe(stream1);
7+
var closed = false;
8+
var cleanup = function () {
9+
if (closed) {
10+
return;
11+
}
12+
if (!stream1.writableEnded) {
13+
stream1.end();
14+
}
15+
if (!stream2.writableEnded) {
16+
stream2.end();
17+
}
18+
closed = true;
19+
cb();
20+
};
21+
eos(stream1, cleanup);
22+
eos(stream2, cleanup);
23+
}
24+
exports.default = pipe;

0 commit comments

Comments
 (0)