1- const { Param} = require ( "./param" ) ;
21const { repeat} = require ( "../../utils/string-util" ) ;
32
43/**
5- * 用于封装请求的上下文
4+ * 用于封装请求的上下文,包含从 URL 中解析出的各种信息。
65 */
76class 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
159158module . exports = {
160159 RequestContext
161- }
162-
160+ } ;
0 commit comments