Skip to content

Commit 9799e2a

Browse files
committed
feat: 完善context的注释
1 parent ef922a7 commit 9799e2a

File tree

5 files changed

+86
-75
lines changed

5 files changed

+86
-75
lines changed

src/analyzer/response-analyzer.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* 用于解析jsonp
2+
* 用于解析jsonp响应
33
*/
44
class ResponseAnalyzer {
55

6-
// 2025-01-07 21:44:27 似乎并不需要这部分逻辑了,仅从请求参数进行推测就已经足够用了
6+
// TODO 2025-01-07 21:44:27 似乎并不需要这部分逻辑了,仅从请求参数进行推测就已经足够用了
77

88
/**
99
*
@@ -14,10 +14,9 @@ class ResponseAnalyzer {
1414
parseScriptContext(scriptContext) {
1515

1616
const responseFunctionNameSymbolSet = this.extractResponseFunctionNameSymbol(scriptContext.responseContext);
17-
for (let param of scriptContext.requestContext.params) {
18-
if (responseFunctionNameSymbolSet.has(param.value)) {
19-
param.isJsonpCallback = true;
20-
}
17+
const jsonpCallbackFuncName = scriptContext.requestContext.getJsonpCallbackFuncName();
18+
if (!responseFunctionNameSymbolSet.has(jsonpCallbackFuncName)) {
19+
// TODO 2025-01-08 00:59:45 完犊子了,发现了异常情况
2120
}
2221

2322
// TODO 2024-12-20 01:52:49
@@ -60,6 +59,6 @@ class ResponseAnalyzer {
6059
}
6160

6261
module.exports = {
63-
JsonpCallbackFunctionAnalyzer: ResponseAnalyzer
62+
ResponseAnalyzer
6463
}
6564

src/context/request/param.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
const {repeat} = require("../../utils/string-util");
22

33
/**
4-
* 表示url路径中的一个参数,对于script类型的请求来说,它只有query string类型的参数,因为它无法携带请求体
4+
* 表示 URL 路径中的一个参数。对于 script 类型的请求来说,它只有 query string 类型的参数,因为它无法携带请求体
55
*/
66
class Param {
77

88
/**
9+
* 构造函数,创建一个 Param 实例。
910
*
10-
* @param name 参数的名字
11-
* @param value 参数的值
12-
* @param isJsonpCallback 此参数是否是jsonp的回调函数名称,有可能会识别不准
11+
* @param {string} name - 参数的名字
12+
* @param {string} value - 参数的值
13+
* @param {boolean} [isJsonpCallback=false] - 此参数是否是 JSONP 的回调函数名称。注意:有可能会识别不准
1314
*/
1415
constructor(name, value, isJsonpCallback) {
1516

1617
// 参数名和值
1718
this.name = name;
1819
this.value = value;
1920

20-
// 这个参数是否是jsonp的回调函数
21+
// 这个参数是否是 JSONP 的回调函数
2122
this.isJsonpCallback = isJsonpCallback || false;
2223

23-
// 此参数是jsonp的callback参数的可能性有多大
24+
// 此参数是 JSONP 的 callback 参数的可能性有多大(用于评分)
2425
this.jsonpCallbackScore = 0;
2526

2627
}
2728

2829
/**
29-
* 把参数转为适合人类阅读的样式
30+
* 将参数转换为适合人类阅读的格式。
3031
*
31-
* @return {string}
32+
* @param {number} indent - 缩进空格数,用于格式化输出。
33+
* @return {string} - 返回格式化后的参数字符串。
3234
*/
3335
toHumanReadable(indent) {
3436
const indentString = repeat(" ", indent);
3537
if (this.isJsonpCallback) {
36-
return `${indentString}${this.name} = ${this.value} <---- this param is jsonp callback function name`
38+
return `${indentString}${this.name} = ${this.value} <---- this param is jsonp callback function name`;
3739
} else {
3840
return `${indentString}${this.name} = ${this.value}`;
3941
}
@@ -43,4 +45,4 @@ class Param {
4345

4446
module.exports = {
4547
Param
46-
}
48+
};

src/context/request/request-context.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
const {Param} = require("./param");
21
const {repeat} = require("../../utils/string-util");
32

43
/**
5-
* 用于封装请求的上下文
4+
* 用于封装请求的上下文,包含从 URL 中解析出的各种信息。
65
*/
76
class RequestContext {
87

98
/**
10-
* 一个请求上下文是从一个script的src的url中解析出来的
9+
* 构造函数,创建一个 RequestContext 实例。
1110
*
12-
* @param rawUrl {String} 要请求的URL的地址,到锚点这一层的
13-
* @param hostname {String}
14-
* @param host {String}
15-
* @param port {Number}
16-
* @param path {String}
17-
* @param params {Array<Param>}
18-
* @param hash {String}
11+
* @param {string} rawUrl - 原始的 URL 地址,包含锚点部分。
12+
* @param {string} hostname - 主机名(例如:example.com)。
13+
* @param {string} host - 主机(包含端口号,例如:example.com:8080)。
14+
* @param {number} port - 端口号。
15+
* @param {string} path - URL 路径部分(例如:/path/to/resource)。
16+
* @param {Array<Param>} params - URL 中的查询参数列表。
17+
* @param {string} hash - URL 中的锚点部分(例如:#section1)。
1918
*/
2019
constructor(rawUrl, hostname, host, port, path, params, hash) {
2120
this.rawUrl = rawUrl;
2221
this.hostname = hostname;
2322
this.host = host;
2423
this.port = port;
2524
this.path = path;
26-
// 避免为空
25+
// 避免 params 为空,初始化为空数组
2726
this.params = params || [];
2827
this.hash = hash;
2928
}
3029

3130
/**
32-
* 从URL解析请求上下文
31+
* 从 URL 解析请求上下文。
3332
*
34-
* @param requestUrl {String} 要被解析的URL
35-
* @return {RequestContext} 返回解析好的请求上下文
33+
* @param {string} requestUrl - 要被解析的 URL。
34+
* @return {RequestContext} - 返回解析好的请求上下文
3635
*/
3736
static parseRequestContext(requestUrl) {
38-
3937
const url = new URL(requestUrl);
4038

41-
// 解析URL上的参数
39+
// 解析 URL 上的参数
4240
const params = [];
4341
url.searchParams.forEach(function (value, key) {
4442
const param = new Param(key, value);
@@ -55,10 +53,10 @@ class RequestContext {
5553
}
5654

5755
/**
58-
* 根据参数名获取参数
56+
* 根据参数名获取参数对象。
5957
*
60-
* @param paramName
61-
* @return {Param|null}
58+
* @param {string} paramName - 要查找的参数名。
59+
* @return {Param|null} - 返回找到的参数对象,如果未找到则返回 null。
6260
*/
6361
getParam(paramName) {
6462
for (let param of this.params) {
@@ -70,10 +68,10 @@ class RequestContext {
7068
}
7169

7270
/**
73-
* 根据参数名获取参数值
71+
* 根据参数名获取参数值
7472
*
75-
* @param paramName
76-
* @return {*|null}
73+
* @param {string} paramName - 要查找的参数名。
74+
* @return {string|null} - 返回参数的值,如果未找到则返回 null。
7775
*/
7876
getParamValueByName(paramName) {
7977
const param = this.getParam(paramName);
@@ -85,9 +83,9 @@ class RequestContext {
8583
}
8684

8785
/**
88-
* 此请求是否是JSONP类型的请求
86+
* 判断此请求是否是 JSONP 类型的请求。
8987
*
90-
* @return {boolean}
88+
* @return {boolean} - 如果是 JSONP 请求则返回 true,否则返回 false。
9189
*/
9290
isJsonpRequest() {
9391
if (!this.params) {
@@ -102,9 +100,9 @@ class RequestContext {
102100
}
103101

104102
/**
105-
* 获取JsonP类型的请求的回调函数的名字
103+
* 获取 JSONP 请求的回调函数名称。
106104
*
107-
* @return {String}
105+
* @return {string|null} - 返回 JSONP 回调函数的名称,如果未找到则返回 null。
108106
*/
109107
getJsonpCallbackFuncName() {
110108
if (!this.params) {
@@ -119,18 +117,21 @@ class RequestContext {
119117
}
120118

121119
/**
120+
* 判断此请求是否是 .js 后缀的请求。
122121
*
123-
* @return {""|boolean}
122+
* @return {boolean} - 如果路径以 .js 结尾则返回 true,否则返回 false。
124123
*/
125124
isJsSuffixRequest() {
126-
return this.path && this.path.toLowerCase().endsWith(".js")
125+
return this.path && this.path.toLowerCase().endsWith(".js");
127126
}
128127

129128
/**
130-
* 转成方便阅读的格式
129+
* 将请求上下文转换为方便人类阅读的格式。
130+
*
131+
* @param {number} indent - 缩进空格数,用于格式化输出。
132+
* @return {string} - 返回格式化后的字符串。
131133
*/
132134
toHumanReadable(indent) {
133-
134135
const indentSpace = repeat(" ", indent);
135136

136137
const msgs = [];
@@ -146,17 +147,14 @@ class RequestContext {
146147
msgs.push(param.toHumanReadable(indent + 4));
147148
}
148149

149-
150150
if (this.hash) {
151-
msgs.push(`${indentSpace}hash: ${this.hash}`)
151+
msgs.push(`${indentSpace}hash: ${this.hash}`);
152152
}
153153

154-
return msgs.join("\n\n");
154+
return msgs.join("\n");
155155
}
156-
157156
}
158157

159158
module.exports = {
160159
RequestContext
161-
}
162-
160+
};

src/context/response/response-context.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
11
/**
2-
* 表示一个Script类型的请求的响应
2+
* 表示一个 Script 类型请求的响应,包含响应的 JavaScript 代码或 JSONP 回调参数。
33
*/
44
class ResponseContext {
55

66
/**
7+
* 构造函数,创建一个 ResponseContext 实例。
78
*
8-
* @param responseJsCode 脚本原始的响应代码是什么
9-
* @param jsonpCallbackArguments 如果是jsonp类型的请求,捕捉到的响应是什么
9+
* @param {string} responseJsCode - 脚本原始的响应代码。
10+
* @param {Array|null} [jsonpCallbackArguments=null] - 如果是 JSONP 类型的请求,捕捉到的响应参数。
1011
*/
11-
constructor(responseJsCode, jsonpCallbackArguments) {
12+
constructor(responseJsCode, jsonpCallbackArguments = null) {
1213
this.responseJsCode = responseJsCode;
1314
this.jsonpCallbackArguments = jsonpCallbackArguments;
1415
}
1516

1617
/**
18+
* 从 HTMLScriptElement 解析响应上下文。
1719
*
18-
* @param script {HTMLScriptElement}
20+
* @param {HTMLScriptElement} script - 包含响应代码的 script 元素。
21+
* @return {ResponseContext} - 返回解析好的响应上下文。
1922
*/
2023
static parseResponseContext(script) {
2124
const responseJsCode = script.text;
2225
return new ResponseContext(responseJsCode);
2326
}
2427

28+
/**
29+
* 判断此响应是否是 JSONP 类型的响应。
30+
*
31+
* @return {boolean} - 如果是 JSONP 响应则返回 true,否则返回 false。
32+
*/
2533
isJsonpResponse() {
2634
return !!this.jsonpCallbackArguments;
2735
}
2836

37+
/**
38+
* 将响应上下文转换为方便人类阅读的格式。
39+
*
40+
* @return {string} - 返回格式化后的字符串。
41+
*/
2942
toHumanReadable() {
3043
const msgs = [];
3144
if (this.isJsonpResponse()) {
@@ -42,4 +55,4 @@ class ResponseContext {
4255

4356
module.exports = {
4457
ResponseContext
45-
}
58+
};
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
/**
2-
* 一次Script请求的上下文封装
2+
* 封装一次 Script 请求的上下文,包含请求和响应的相关信息。
33
*/
4-
const {RequestContext} = require("../request/request-context");
5-
const {ResponseContext} = require("../response/response-context");
64
const {randomId} = require("../../utils/id-util");
75

86
class ScriptContext {
97

108
/**
11-
* script级别的上下文
9+
* 构造函数,创建一个 ScriptContext 实例。
1210
*
13-
* @param url {String}
14-
* @param requestContext {RequestContext}
15-
* @param responseContext {ResponseContext}
11+
* @param {string} url - 请求的 URL。
12+
* @param {RequestContext} requestContext - 请求上下文,包含请求的详细信息。
13+
* @param {ResponseContext} responseContext - 响应上下文,包含响应的详细信息。
1614
*/
1715
constructor(url, requestContext, responseContext) {
16+
// 生成唯一的请求 ID
1817
this.requestId = "js-script-hook-" + randomId();
1918
this.url = url;
2019
this.requestContext = requestContext;
2120
this.responseContext = responseContext;
2221
}
2322

2423
/**
25-
* 判断这个请求是否是jsonp请求
24+
* 判断此请求是否是 JSONP 请求。
2625
*
27-
* @returns {boolean}
26+
* @returns {boolean} - 如果是 JSONP 请求则返回 true,否则返回 false。
2827
*/
2928
isJsonp() {
3029
if (this.requestContext && this.requestContext.isJsonpRequest()) {
@@ -37,20 +36,20 @@ class ScriptContext {
3736
}
3837

3938
/**
40-
* 判断是否是请求的js文件
39+
* 判断此请求是否是 .js 文件请求。
4140
*
42-
* @return {""|boolean}
41+
* @return {boolean} - 如果请求的路径以 .js 结尾则返回 true,否则返回 false。
4342
*/
4443
isJsSuffixRequest() {
4544
return this.requestContext && this.requestContext.isJsSuffixRequest();
4645
}
4746

4847
/**
48+
* 将 Script 上下文转换为方便人类阅读的格式。
4949
*
50-
* @return {string}
50+
* @return {string} - 返回格式化后的字符串。
5151
*/
5252
toHumanReadable() {
53-
5453
const msgs = [];
5554

5655
if (this.requestContext) {
@@ -61,8 +60,8 @@ class ScriptContext {
6160
msgs.push("\n\n");
6261

6362
if (this.responseContext) {
64-
msgs.push("Response Information: ")
65-
msgs.push(this.responseContext.toHumanReadable(4));
63+
msgs.push("Response Information: ");
64+
msgs.push(this.responseContext.toHumanReadable());
6665
}
6766

6867
return msgs.join("\n\n");
@@ -72,4 +71,4 @@ class ScriptContext {
7271

7372
module.exports = {
7473
ScriptContext
75-
}
74+
};

0 commit comments

Comments
 (0)