egg 4.0.10 → 4.1.0-beta.3

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.
Files changed (291) hide show
  1. package/README.md +4 -22
  2. package/dist/agent.d.ts +8 -0
  3. package/dist/agent.js +11 -0
  4. package/dist/app/extend/context.d.ts +207 -0
  5. package/dist/app/extend/context.js +244 -0
  6. package/{src/app/extend/helper.ts → dist/app/extend/helper.d.ts} +8 -10
  7. package/dist/app/extend/helper.js +47 -0
  8. package/dist/app/extend/request.d.ts +145 -0
  9. package/dist/app/extend/request.js +226 -0
  10. package/{src/app/extend/response.ts → dist/app/extend/response.d.ts} +7 -16
  11. package/dist/app/extend/response.js +36 -0
  12. package/dist/app/middleware/body_parser.d.ts +2 -0
  13. package/dist/app/middleware/body_parser.js +7 -0
  14. package/dist/app/middleware/meta.d.ts +11 -0
  15. package/dist/app/middleware/meta.js +14 -0
  16. package/dist/app/middleware/notfound.d.ts +11 -0
  17. package/dist/app/middleware/notfound.js +26 -0
  18. package/dist/app/middleware/override_method.d.ts +2 -0
  19. package/dist/app/middleware/override_method.js +7 -0
  20. package/dist/app/middleware/site_file.d.ts +13 -0
  21. package/dist/app/middleware/site_file.js +38 -0
  22. package/dist/config/config.default.d.ts +13 -0
  23. package/dist/config/config.default.js +257 -0
  24. package/dist/config/config.local.d.ts +10 -0
  25. package/dist/config/config.local.js +7 -0
  26. package/dist/config/config.unittest.d.ts +9 -0
  27. package/dist/config/config.unittest.js +10 -0
  28. package/{src/config/plugin.ts → dist/config/plugin.d.ts} +40 -48
  29. package/dist/config/plugin.js +54 -0
  30. package/dist/index.d.ts +19 -0
  31. package/dist/index.js +21 -0
  32. package/dist/lib/agent.d.ts +22 -0
  33. package/dist/lib/agent.js +56 -0
  34. package/dist/lib/application.d.ts +71 -0
  35. package/dist/lib/application.js +209 -0
  36. package/dist/lib/core/base_context_class.d.ts +23 -0
  37. package/dist/lib/core/base_context_class.js +19 -0
  38. package/dist/lib/core/base_context_logger.d.ts +40 -0
  39. package/dist/lib/core/base_context_logger.js +58 -0
  40. package/dist/lib/core/base_hook_class.d.ts +18 -0
  41. package/dist/lib/core/base_hook_class.js +26 -0
  42. package/dist/lib/core/context_httpclient.d.ts +21 -0
  43. package/dist/lib/core/context_httpclient.js +29 -0
  44. package/dist/lib/core/httpclient.d.ts +17 -0
  45. package/dist/lib/core/httpclient.js +36 -0
  46. package/dist/lib/core/logger.d.ts +7 -0
  47. package/dist/lib/core/logger.js +31 -0
  48. package/{src/lib/core/messenger/IMessenger.ts → dist/lib/core/messenger/IMessenger.d.ts} +6 -10
  49. package/dist/lib/core/messenger/IMessenger.js +1 -0
  50. package/dist/lib/core/messenger/base.d.ts +12 -0
  51. package/dist/lib/core/messenger/base.js +29 -0
  52. package/dist/lib/core/messenger/index.d.ts +11 -0
  53. package/dist/lib/core/messenger/index.js +13 -0
  54. package/dist/lib/core/messenger/ipc.d.ts +63 -0
  55. package/dist/lib/core/messenger/ipc.js +122 -0
  56. package/dist/lib/core/messenger/local.d.ts +65 -0
  57. package/dist/lib/core/messenger/local.js +126 -0
  58. package/dist/lib/core/utils.d.ts +5 -0
  59. package/dist/lib/core/utils.js +48 -0
  60. package/dist/lib/egg.d.ts +306 -0
  61. package/dist/lib/egg.js +512 -0
  62. package/dist/lib/error/CookieLimitExceedError.d.ts +8 -0
  63. package/dist/lib/error/CookieLimitExceedError.js +15 -0
  64. package/dist/lib/error/MessageUnhandledRejectionError.d.ts +8 -0
  65. package/dist/lib/error/MessageUnhandledRejectionError.js +15 -0
  66. package/dist/lib/error/index.d.ts +3 -0
  67. package/dist/lib/error/index.js +4 -0
  68. package/dist/lib/loader/AgentWorkerLoader.d.ts +17 -0
  69. package/dist/lib/loader/AgentWorkerLoader.js +24 -0
  70. package/dist/lib/loader/AppWorkerLoader.d.ts +22 -0
  71. package/dist/lib/loader/AppWorkerLoader.js +37 -0
  72. package/dist/lib/loader/EggApplicationLoader.d.ts +8 -0
  73. package/dist/lib/loader/EggApplicationLoader.js +7 -0
  74. package/dist/lib/loader/index.d.ts +4 -0
  75. package/dist/lib/loader/index.js +5 -0
  76. package/dist/lib/start.d.ts +28 -0
  77. package/dist/lib/start.js +36 -0
  78. package/{src/lib/types.ts → dist/lib/types.d.ts} +45 -94
  79. package/dist/lib/types.js +14 -0
  80. package/dist/lib/utils.d.ts +5 -0
  81. package/dist/lib/utils.js +14 -0
  82. package/dist/urllib.d.ts +1 -0
  83. package/dist/urllib.js +3 -0
  84. package/package.json +76 -97
  85. package/README.zh-CN.md +0 -61
  86. package/dist/commonjs/agent.d.ts +0 -4
  87. package/dist/commonjs/agent.js +0 -10
  88. package/dist/commonjs/app/extend/context.d.ts +0 -204
  89. package/dist/commonjs/app/extend/context.js +0 -262
  90. package/dist/commonjs/app/extend/helper.d.ts +0 -37
  91. package/dist/commonjs/app/extend/helper.js +0 -49
  92. package/dist/commonjs/app/extend/request.d.ts +0 -141
  93. package/dist/commonjs/app/extend/request.js +0 -270
  94. package/dist/commonjs/app/extend/response.d.ts +0 -31
  95. package/dist/commonjs/app/extend/response.js +0 -37
  96. package/dist/commonjs/app/middleware/body_parser.d.ts +0 -2
  97. package/dist/commonjs/app/middleware/body_parser.js +0 -8
  98. package/dist/commonjs/app/middleware/meta.d.ts +0 -10
  99. package/dist/commonjs/app/middleware/meta.js +0 -22
  100. package/dist/commonjs/app/middleware/notfound.d.ts +0 -7
  101. package/dist/commonjs/app/middleware/notfound.js +0 -31
  102. package/dist/commonjs/app/middleware/override_method.d.ts +0 -2
  103. package/dist/commonjs/app/middleware/override_method.js +0 -8
  104. package/dist/commonjs/app/middleware/site_file.d.ts +0 -9
  105. package/dist/commonjs/app/middleware/site_file.js +0 -58
  106. package/dist/commonjs/config/config.default.d.ts +0 -9
  107. package/dist/commonjs/config/config.default.js +0 -379
  108. package/dist/commonjs/config/config.local.d.ts +0 -8
  109. package/dist/commonjs/config/config.local.js +0 -12
  110. package/dist/commonjs/config/config.unittest.d.ts +0 -7
  111. package/dist/commonjs/config/config.unittest.js +0 -11
  112. package/dist/commonjs/config/favicon.png +0 -0
  113. package/dist/commonjs/config/plugin.d.ts +0 -122
  114. package/dist/commonjs/config/plugin.js +0 -124
  115. package/dist/commonjs/index.d.ts +0 -69
  116. package/dist/commonjs/index.js +0 -83
  117. package/dist/commonjs/lib/agent.d.ts +0 -19
  118. package/dist/commonjs/lib/agent.js +0 -58
  119. package/dist/commonjs/lib/application.d.ts +0 -67
  120. package/dist/commonjs/lib/application.js +0 -266
  121. package/dist/commonjs/lib/core/base_context_class.d.ts +0 -17
  122. package/dist/commonjs/lib/core/base_context_class.js +0 -21
  123. package/dist/commonjs/lib/core/base_context_logger.d.ts +0 -36
  124. package/dist/commonjs/lib/core/base_context_logger.js +0 -64
  125. package/dist/commonjs/lib/core/base_hook_class.d.ts +0 -11
  126. package/dist/commonjs/lib/core/base_hook_class.js +0 -29
  127. package/dist/commonjs/lib/core/context_httpclient.d.ts +0 -16
  128. package/dist/commonjs/lib/core/context_httpclient.js +0 -30
  129. package/dist/commonjs/lib/core/httpclient.d.ts +0 -14
  130. package/dist/commonjs/lib/core/httpclient.js +0 -42
  131. package/dist/commonjs/lib/core/logger.d.ts +0 -3
  132. package/dist/commonjs/lib/core/logger.js +0 -40
  133. package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +0 -50
  134. package/dist/commonjs/lib/core/messenger/IMessenger.js +0 -3
  135. package/dist/commonjs/lib/core/messenger/base.d.ts +0 -8
  136. package/dist/commonjs/lib/core/messenger/base.js +0 -31
  137. package/dist/commonjs/lib/core/messenger/index.d.ts +0 -7
  138. package/dist/commonjs/lib/core/messenger/index.js +0 -15
  139. package/dist/commonjs/lib/core/messenger/ipc.d.ts +0 -58
  140. package/dist/commonjs/lib/core/messenger/ipc.js +0 -135
  141. package/dist/commonjs/lib/core/messenger/local.d.ts +0 -60
  142. package/dist/commonjs/lib/core/messenger/local.js +0 -132
  143. package/dist/commonjs/lib/core/utils.d.ts +0 -2
  144. package/dist/commonjs/lib/core/utils.js +0 -77
  145. package/dist/commonjs/lib/egg.d.ts +0 -305
  146. package/dist/commonjs/lib/egg.js +0 -628
  147. package/dist/commonjs/lib/error/CookieLimitExceedError.d.ts +0 -5
  148. package/dist/commonjs/lib/error/CookieLimitExceedError.js +0 -16
  149. package/dist/commonjs/lib/error/MessageUnhandledRejectionError.d.ts +0 -5
  150. package/dist/commonjs/lib/error/MessageUnhandledRejectionError.js +0 -16
  151. package/dist/commonjs/lib/error/index.d.ts +0 -2
  152. package/dist/commonjs/lib/error/index.js +0 -19
  153. package/dist/commonjs/lib/loader/AgentWorkerLoader.d.ts +0 -12
  154. package/dist/commonjs/lib/loader/AgentWorkerLoader.js +0 -24
  155. package/dist/commonjs/lib/loader/AppWorkerLoader.d.ts +0 -17
  156. package/dist/commonjs/lib/loader/AppWorkerLoader.js +0 -43
  157. package/dist/commonjs/lib/loader/EggApplicationLoader.d.ts +0 -4
  158. package/dist/commonjs/lib/loader/EggApplicationLoader.js +0 -8
  159. package/dist/commonjs/lib/loader/index.d.ts +0 -3
  160. package/dist/commonjs/lib/loader/index.js +0 -22
  161. package/dist/commonjs/lib/start.d.ts +0 -24
  162. package/dist/commonjs/lib/start.js +0 -49
  163. package/dist/commonjs/lib/types.d.ts +0 -257
  164. package/dist/commonjs/lib/types.js +0 -16
  165. package/dist/commonjs/lib/utils.d.ts +0 -2
  166. package/dist/commonjs/lib/utils.js +0 -21
  167. package/dist/commonjs/package.json +0 -3
  168. package/dist/commonjs/urllib.d.ts +0 -1
  169. package/dist/commonjs/urllib.js +0 -18
  170. package/dist/esm/agent.d.ts +0 -4
  171. package/dist/esm/agent.js +0 -7
  172. package/dist/esm/app/extend/context.d.ts +0 -204
  173. package/dist/esm/app/extend/context.js +0 -259
  174. package/dist/esm/app/extend/helper.d.ts +0 -37
  175. package/dist/esm/app/extend/helper.js +0 -43
  176. package/dist/esm/app/extend/request.d.ts +0 -141
  177. package/dist/esm/app/extend/request.js +0 -264
  178. package/dist/esm/app/extend/response.d.ts +0 -31
  179. package/dist/esm/app/extend/response.js +0 -34
  180. package/dist/esm/app/middleware/body_parser.d.ts +0 -2
  181. package/dist/esm/app/middleware/body_parser.js +0 -3
  182. package/dist/esm/app/middleware/meta.d.ts +0 -10
  183. package/dist/esm/app/middleware/meta.js +0 -20
  184. package/dist/esm/app/middleware/notfound.d.ts +0 -7
  185. package/dist/esm/app/middleware/notfound.js +0 -29
  186. package/dist/esm/app/middleware/override_method.d.ts +0 -2
  187. package/dist/esm/app/middleware/override_method.js +0 -3
  188. package/dist/esm/app/middleware/site_file.d.ts +0 -9
  189. package/dist/esm/app/middleware/site_file.js +0 -53
  190. package/dist/esm/config/config.default.d.ts +0 -9
  191. package/dist/esm/config/config.default.js +0 -374
  192. package/dist/esm/config/config.local.d.ts +0 -8
  193. package/dist/esm/config/config.local.js +0 -10
  194. package/dist/esm/config/config.unittest.d.ts +0 -7
  195. package/dist/esm/config/config.unittest.js +0 -9
  196. package/dist/esm/config/favicon.png +0 -0
  197. package/dist/esm/config/plugin.d.ts +0 -122
  198. package/dist/esm/config/plugin.js +0 -122
  199. package/dist/esm/index.d.ts +0 -69
  200. package/dist/esm/index.js +0 -73
  201. package/dist/esm/lib/agent.d.ts +0 -19
  202. package/dist/esm/lib/agent.js +0 -54
  203. package/dist/esm/lib/application.d.ts +0 -67
  204. package/dist/esm/lib/application.js +0 -259
  205. package/dist/esm/lib/core/base_context_class.d.ts +0 -17
  206. package/dist/esm/lib/core/base_context_class.js +0 -17
  207. package/dist/esm/lib/core/base_context_logger.d.ts +0 -36
  208. package/dist/esm/lib/core/base_context_logger.js +0 -60
  209. package/dist/esm/lib/core/base_hook_class.d.ts +0 -11
  210. package/dist/esm/lib/core/base_hook_class.js +0 -22
  211. package/dist/esm/lib/core/context_httpclient.d.ts +0 -16
  212. package/dist/esm/lib/core/context_httpclient.js +0 -26
  213. package/dist/esm/lib/core/httpclient.d.ts +0 -14
  214. package/dist/esm/lib/core/httpclient.js +0 -38
  215. package/dist/esm/lib/core/logger.d.ts +0 -3
  216. package/dist/esm/lib/core/logger.js +0 -37
  217. package/dist/esm/lib/core/messenger/IMessenger.d.ts +0 -50
  218. package/dist/esm/lib/core/messenger/IMessenger.js +0 -2
  219. package/dist/esm/lib/core/messenger/base.d.ts +0 -8
  220. package/dist/esm/lib/core/messenger/base.js +0 -27
  221. package/dist/esm/lib/core/messenger/index.d.ts +0 -7
  222. package/dist/esm/lib/core/messenger/index.js +0 -12
  223. package/dist/esm/lib/core/messenger/ipc.d.ts +0 -58
  224. package/dist/esm/lib/core/messenger/ipc.js +0 -128
  225. package/dist/esm/lib/core/messenger/local.d.ts +0 -60
  226. package/dist/esm/lib/core/messenger/local.js +0 -128
  227. package/dist/esm/lib/core/utils.d.ts +0 -2
  228. package/dist/esm/lib/core/utils.js +0 -70
  229. package/dist/esm/lib/egg.d.ts +0 -305
  230. package/dist/esm/lib/egg.js +0 -586
  231. package/dist/esm/lib/error/CookieLimitExceedError.d.ts +0 -5
  232. package/dist/esm/lib/error/CookieLimitExceedError.js +0 -12
  233. package/dist/esm/lib/error/MessageUnhandledRejectionError.d.ts +0 -5
  234. package/dist/esm/lib/error/MessageUnhandledRejectionError.js +0 -12
  235. package/dist/esm/lib/error/index.d.ts +0 -2
  236. package/dist/esm/lib/error/index.js +0 -3
  237. package/dist/esm/lib/loader/AgentWorkerLoader.d.ts +0 -12
  238. package/dist/esm/lib/loader/AgentWorkerLoader.js +0 -20
  239. package/dist/esm/lib/loader/AppWorkerLoader.d.ts +0 -17
  240. package/dist/esm/lib/loader/AppWorkerLoader.js +0 -39
  241. package/dist/esm/lib/loader/EggApplicationLoader.d.ts +0 -4
  242. package/dist/esm/lib/loader/EggApplicationLoader.js +0 -4
  243. package/dist/esm/lib/loader/index.d.ts +0 -3
  244. package/dist/esm/lib/loader/index.js +0 -4
  245. package/dist/esm/lib/start.d.ts +0 -24
  246. package/dist/esm/lib/start.js +0 -43
  247. package/dist/esm/lib/types.d.ts +0 -257
  248. package/dist/esm/lib/types.js +0 -14
  249. package/dist/esm/lib/utils.d.ts +0 -2
  250. package/dist/esm/lib/utils.js +0 -14
  251. package/dist/esm/package.json +0 -3
  252. package/dist/esm/urllib.d.ts +0 -1
  253. package/dist/esm/urllib.js +0 -2
  254. package/dist/package.json +0 -4
  255. package/src/agent.ts +0 -7
  256. package/src/app/extend/context.ts +0 -332
  257. package/src/app/extend/request.ts +0 -300
  258. package/src/app/middleware/body_parser.ts +0 -3
  259. package/src/app/middleware/meta.ts +0 -27
  260. package/src/app/middleware/notfound.ts +0 -41
  261. package/src/app/middleware/override_method.ts +0 -3
  262. package/src/app/middleware/site_file.ts +0 -68
  263. package/src/config/config.default.ts +0 -404
  264. package/src/config/config.local.ts +0 -11
  265. package/src/config/config.unittest.ts +0 -10
  266. package/src/config/favicon.png +0 -0
  267. package/src/index.ts +0 -120
  268. package/src/lib/agent.ts +0 -66
  269. package/src/lib/application.ts +0 -308
  270. package/src/lib/core/base_context_class.ts +0 -24
  271. package/src/lib/core/base_context_logger.ts +0 -67
  272. package/src/lib/core/base_hook_class.ts +0 -30
  273. package/src/lib/core/context_httpclient.ts +0 -33
  274. package/src/lib/core/httpclient.ts +0 -64
  275. package/src/lib/core/logger.ts +0 -42
  276. package/src/lib/core/messenger/base.ts +0 -30
  277. package/src/lib/core/messenger/index.ts +0 -16
  278. package/src/lib/core/messenger/ipc.ts +0 -146
  279. package/src/lib/core/messenger/local.ts +0 -147
  280. package/src/lib/core/utils.ts +0 -77
  281. package/src/lib/egg.ts +0 -705
  282. package/src/lib/error/CookieLimitExceedError.ts +0 -12
  283. package/src/lib/error/MessageUnhandledRejectionError.ts +0 -12
  284. package/src/lib/error/index.ts +0 -2
  285. package/src/lib/loader/AgentWorkerLoader.ts +0 -21
  286. package/src/lib/loader/AppWorkerLoader.ts +0 -42
  287. package/src/lib/loader/EggApplicationLoader.ts +0 -5
  288. package/src/lib/loader/index.ts +0 -3
  289. package/src/lib/start.ts +0 -66
  290. package/src/lib/utils.ts +0 -15
  291. package/src/urllib.ts +0 -1
@@ -1,332 +0,0 @@
1
- import { assign } from 'utility';
2
- import { now, diff } from 'performance-ms';
3
- import {
4
- utils, Context as EggCoreContext, Router,
5
- } from '@eggjs/core';
6
- import type { Cookies as ContextCookies } from '@eggjs/cookies';
7
- import type { EggLogger } from 'egg-logger';
8
- import type { Application } from '../../lib/application.js';
9
- import type {
10
- HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
11
- } from '../../lib/core/httpclient.js';
12
- import type { BaseContextClass } from '../../lib//core/base_context_class.js';
13
- import type Request from './request.js';
14
- import type Response from './response.js';
15
- import type Helper from './helper.js';
16
-
17
- const HELPER = Symbol('ctx helper');
18
- const LOCALS = Symbol('ctx locals');
19
- const LOCALS_LIST = Symbol('ctx localsList');
20
- const COOKIES = Symbol('ctx cookies');
21
- const CONTEXT_HTTPCLIENT = Symbol('ctx httpclient');
22
- const CONTEXT_ROUTER = Symbol('ctx router');
23
-
24
- interface Cookies extends ContextCookies {
25
- request: any;
26
- response: any;
27
- }
28
-
29
- export default class Context extends EggCoreContext {
30
- declare app: Application;
31
- declare request: Request;
32
- declare response: Response;
33
- declare service: BaseContextClass;
34
- declare proxy: any;
35
-
36
- /**
37
- * Request start time
38
- * @member {Number} Context#starttime
39
- */
40
- starttime: number;
41
- /**
42
- * Request start timer using `performance.now()`
43
- * @member {Number} Context#performanceStarttime
44
- */
45
- performanceStarttime: number;
46
-
47
- /**
48
- * Get the current visitor's cookies.
49
- */
50
- get cookies() {
51
- let cookies = this[COOKIES];
52
- if (!cookies) {
53
- this[COOKIES] = cookies = new this.app.ContextCookies(this, this.app.keys, this.app.config.cookies);
54
- }
55
- return cookies as Cookies;
56
- }
57
-
58
- /**
59
- * Get a wrapper httpclient instance contain ctx in the hold request process
60
- *
61
- * @return {HttpClient} the wrapper httpclient instance
62
- */
63
- get httpclient(): HttpClient {
64
- if (!this[CONTEXT_HTTPCLIENT]) {
65
- this[CONTEXT_HTTPCLIENT] = new this.app.ContextHttpClient(this as any);
66
- }
67
- return this[CONTEXT_HTTPCLIENT] as HttpClient;
68
- }
69
-
70
- /**
71
- * Alias to {@link Context#httpclient}
72
- */
73
- get httpClient(): HttpClient {
74
- return this.httpclient;
75
- }
76
-
77
- /**
78
- * Shortcut for httpclient.curl
79
- *
80
- * @function Context#curl
81
- * @param {String|Object} url - request url address.
82
- * @param {Object} [options] - options for request.
83
- * @return {Object} see {@link ContextHttpClient#curl}
84
- */
85
- async curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']> {
86
- return await this.httpclient.curl(url, options);
87
- }
88
-
89
- /**
90
- * Alias to {@link Application#router}
91
- *
92
- * @member {Router} Context#router
93
- * @since 1.0.0
94
- * @example
95
- * ```js
96
- * this.router.pathFor('post', { id: 12 });
97
- * ```
98
- */
99
- get router(): Router {
100
- if (this[CONTEXT_ROUTER]) {
101
- return this[CONTEXT_ROUTER] as Router;
102
- }
103
- return this.app.router;
104
- }
105
-
106
- /**
107
- * Set router to Context, only use on EggRouter
108
- * @param {Router} val router instance
109
- */
110
- set router(val: Router) {
111
- this[CONTEXT_ROUTER] = val;
112
- }
113
-
114
- /**
115
- * Get helper instance from {@link Application#Helper}
116
- *
117
- * @member {Helper} Context#helper
118
- * @since 1.0.0
119
- */
120
- get helper(): Helper {
121
- if (!this[HELPER]) {
122
- this[HELPER] = new this.app.Helper(this as any);
123
- }
124
- return this[HELPER] as Helper;
125
- }
126
-
127
- /**
128
- * Wrap app.loggers with context information,
129
- * if a custom logger is defined by naming aLogger, then you can `ctx.getLogger('aLogger')`
130
- *
131
- * @param {String} name - logger name
132
- */
133
- getLogger(name: string): EggLogger {
134
- return this.app.getLogger(name);
135
- }
136
-
137
- /**
138
- * Logger for Application
139
- *
140
- * @member {Logger} Context#logger
141
- * @since 1.0.0
142
- * @example
143
- * ```js
144
- * this.logger.info('some request data: %j', this.request.body);
145
- * this.logger.warn('WARNING!!!!');
146
- * ```
147
- */
148
- get logger(): EggLogger {
149
- return this.getLogger('logger');
150
- }
151
-
152
- /**
153
- * Logger for frameworks and plugins
154
- *
155
- * @member {Logger} Context#coreLogger
156
- * @since 1.0.0
157
- */
158
- get coreLogger(): EggLogger {
159
- return this.getLogger('coreLogger');
160
- }
161
-
162
- /**
163
- * locals is an object for view, you can use `app.locals` and `ctx.locals` to set variables,
164
- * which will be used as data when view is rendering.
165
- * The difference between `app.locals` and `ctx.locals` is the context level, `app.locals` is global level, and `ctx.locals` is request level. when you get `ctx.locals`, it will merge `app.locals`.
166
- *
167
- * when you set locals, only object is available
168
- *
169
- * ```js
170
- * this.locals = {
171
- * a: 1
172
- * };
173
- * this.locals = {
174
- * b: 1
175
- * };
176
- * this.locals.c = 1;
177
- * console.log(this.locals);
178
- * {
179
- * a: 1,
180
- * b: 1,
181
- * c: 1,
182
- * };
183
- * ```
184
- *
185
- * `ctx.locals` has cache, it only merges `app.locals` once in one request.
186
- *
187
- * @member {Object} Context#locals
188
- */
189
- get locals() {
190
- if (!this[LOCALS]) {
191
- this[LOCALS] = assign({}, this.app.locals);
192
- }
193
- if (Array.isArray(this[LOCALS_LIST]) && this[LOCALS_LIST].length > 0) {
194
- assign(this[LOCALS], this[LOCALS_LIST]);
195
- this[LOCALS_LIST] = null;
196
- }
197
- return this[LOCALS] as Record<string, any>;
198
- }
199
-
200
- set locals(val) {
201
- const localsList = this[LOCALS_LIST] as Record<string, any>[] ?? [];
202
- localsList.push(val);
203
- this[LOCALS_LIST] = localsList;
204
- }
205
-
206
- /**
207
- * alias to {@link Context#locals}, compatible with koa that use this variable
208
- * @member {Object} state
209
- * @see Context#locals
210
- */
211
- get state() {
212
- return this.locals;
213
- }
214
-
215
- set state(val) {
216
- this.locals = val;
217
- }
218
-
219
- /**
220
- * Run async function in the background
221
- * @param {Function} scope - the first args is ctx
222
- * ```js
223
- * this.body = 'hi';
224
- *
225
- * this.runInBackground(async ctx => {
226
- * await ctx.mysql.query(sql);
227
- * await ctx.curl(url);
228
- * });
229
- * ```
230
- */
231
- runInBackground(scope: (ctx: Context) => Promise<void>, taskName?: string): void {
232
- // try to use custom function name first
233
- if (!taskName) {
234
- taskName = Reflect.get(scope, '_name') || scope.name || utils.getCalleeFromStack(true);
235
- }
236
- // use setImmediate to ensure all sync logic will run async
237
- setImmediate(() => {
238
- this._runInBackground(scope, taskName!);
239
- });
240
- }
241
-
242
- // let plugins or frameworks to reuse _runInBackground in some cases.
243
- // e.g.: https://github.com/eggjs/egg-mock/pull/78
244
- async _runInBackground(scope: (ctx: Context) => Promise<void>, taskName: string) {
245
- const startTime = now();
246
- try {
247
- await scope(this as any);
248
- this.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, diff(startTime));
249
- } catch (err: any) {
250
- // background task process log
251
- this.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, diff(startTime));
252
-
253
- // emit error when promise catch, and set err.runInBackground flag
254
- err.runInBackground = true;
255
- this.app.emit('error', err, this);
256
- }
257
- }
258
-
259
- /**
260
- * @member {Boolean} Context#acceptJSON
261
- * @see Request#acceptJSON
262
- * @since 1.0.0
263
- */
264
- get acceptJSON(): boolean {
265
- return this.request.acceptJSON;
266
- }
267
-
268
- get query(): Record<string, string> {
269
- return this.request.query;
270
- }
271
-
272
- /**
273
- * @member {Array} Context#queries
274
- * @see Request#queries
275
- * @since 1.0.0
276
- */
277
- get queries(): Record<string, string[]> {
278
- return this.request.queries;
279
- }
280
-
281
- /**
282
- * @member {string} Context#ip
283
- * @see Request#ip
284
- * @since 1.0.0
285
- */
286
- get ip(): string {
287
- return this.request.ip;
288
- }
289
-
290
- set ip(val: string) {
291
- this.request.ip = val;
292
- }
293
-
294
- /**
295
- * @member {Number} Context#realStatus
296
- * @see Response#realStatus
297
- * @since 1.0.0
298
- */
299
- get realStatus(): number {
300
- return this.response.realStatus;
301
- }
302
-
303
- set realStatus(val: number) {
304
- this.response.realStatus = val;
305
- }
306
- }
307
-
308
- declare module '@eggjs/core' {
309
- // add Context overrides types
310
- interface Context {
311
- proxy: any;
312
- performanceStarttime: number;
313
- starttime: number;
314
- runInBackground(scope: (ctx: Context) => Promise<void>, taskName?: string): void;
315
- _runInBackground(scope: (ctx: Context) => Promise<void>, taskName: string): void;
316
- get acceptJSON(): boolean;
317
- get query(): Record<string, string>;
318
- get queries(): Record<string, string[]>;
319
- curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']>;
320
- get router(): Router;
321
- set router(val: Router);
322
- get helper(): Helper;
323
- get httpclient(): HttpClient;
324
- get httpClient(): HttpClient;
325
- getLogger(name: string): EggLogger;
326
- get logger(): EggLogger;
327
- get coreLogger(): EggLogger;
328
- get locals(): Record<string, any>;
329
- get realStatus(): number;
330
- set realStatus(val: number);
331
- }
332
- }
@@ -1,300 +0,0 @@
1
- import querystring from 'node:querystring';
2
- import { Request as EggCoreRequest } from '@eggjs/core';
3
- import type { Application } from '../../lib/application.js';
4
- import type Context from './context.js';
5
- import Response from './response.js';
6
-
7
- const QUERY_CACHE = Symbol('request query cache');
8
- const QUERIES_CACHE = Symbol('request queries cache');
9
- const PROTOCOL = Symbol('request protocol');
10
- const HOST = Symbol('request host');
11
- const IPS = Symbol('request ips');
12
- const RE_ARRAY_KEY = /[^\[\]]+\[\]$/;
13
-
14
- export default class Request extends EggCoreRequest {
15
- declare app: Application;
16
- declare ctx: Context;
17
- declare response: Response;
18
-
19
- /**
20
- * Request body, parsed from koa-bodyparser or @eggjs/multipart
21
- */
22
- declare body: any;
23
-
24
- /**
25
- * Parse the "Host" header field host
26
- * and support X-Forwarded-Host when a
27
- * proxy is enabled.
28
- * @member {String} Request#host
29
- * @example
30
- * ip + port
31
- * ```js
32
- * this.request.host
33
- * => '127.0.0.1:7001'
34
- * ```
35
- * or domain
36
- * ```js
37
- * this.request.host
38
- * => 'demo.eggjs.org'
39
- * ```
40
- */
41
- get host(): string {
42
- let host = this[HOST] as string | undefined;
43
- if (host) {
44
- return host;
45
- }
46
-
47
- if (this.app.config.proxy) {
48
- host = getFromHeaders(this, this.app.config.hostHeaders);
49
- }
50
- host = host || this.get('host') || '';
51
- this[HOST] = host = host.split(',')[0].trim();
52
- return host;
53
- }
54
-
55
- /**
56
- * @member {String} Request#protocol
57
- * @example
58
- * ```js
59
- * this.request.protocol
60
- * => 'https'
61
- * ```
62
- */
63
- get protocol(): string {
64
- let protocol = this[PROTOCOL] as string;
65
- if (protocol) {
66
- return protocol;
67
- }
68
- // detect encrypted socket
69
- if (this.socket?.encrypted) {
70
- this[PROTOCOL] = protocol = 'https';
71
- return protocol;
72
- }
73
- // get from headers specified in `app.config.protocolHeaders`
74
- if (this.app.config.proxy) {
75
- const proto = getFromHeaders(this, this.app.config.protocolHeaders);
76
- if (proto) {
77
- this[PROTOCOL] = protocol = proto.split(/\s*,\s*/)[0];
78
- return protocol;
79
- }
80
- }
81
- // use protocol specified in `app.config.protocol`
82
- this[PROTOCOL] = protocol = this.app.config.protocol || 'http';
83
- return protocol;
84
- }
85
-
86
- /**
87
- * Get all pass through ip addresses from the request.
88
- * Enable only on `app.config.proxy = true`
89
- *
90
- * @member {Array} Request#ips
91
- * @example
92
- * ```js
93
- * this.request.ips
94
- * => ['100.23.1.2', '201.10.10.2']
95
- * ```
96
- */
97
- get ips(): string[] {
98
- let ips = this[IPS] as string[] | undefined;
99
- if (ips) {
100
- return ips;
101
- }
102
-
103
- // return empty array when proxy=false
104
- if (!this.app.config.proxy) {
105
- this[IPS] = ips = [];
106
- return ips;
107
- }
108
-
109
- const val = getFromHeaders(this, this.app.config.ipHeaders);
110
- this[IPS] = ips = val ? val.split(/\s*,\s*/) : [];
111
-
112
- let maxIpsCount = this.app.config.maxIpsCount;
113
- // Compatible with maxProxyCount logic (previous logic is wrong, only for compatibility with legacy logic)
114
- if (!maxIpsCount && this.app.config.maxProxyCount) {
115
- maxIpsCount = this.app.config.maxProxyCount + 1;
116
- }
117
-
118
- if (maxIpsCount > 0) {
119
- // if maxIpsCount present, only keep `maxIpsCount` ips
120
- // [ illegalIp, clientRealIp, proxyIp1, proxyIp2 ...]
121
- this[IPS] = ips = ips.slice(-maxIpsCount);
122
- }
123
- return ips;
124
- }
125
-
126
- /**
127
- * Get the request remote IPv4 address
128
- * @member {String} Request#ip
129
- * @return {String} IPv4 address
130
- * @example
131
- * ```js
132
- * this.request.ip
133
- * => '127.0.0.1'
134
- * => '111.10.2.1'
135
- * ```
136
- */
137
- get ip(): string {
138
- if (this._ip) {
139
- return this._ip;
140
- }
141
- const ip = this.ips[0] ?? this.socket.remoteAddress;
142
- // will be '::ffff:x.x.x.x', should convert to standard IPv4 format
143
- // https://zh.wikipedia.org/wiki/IPv6
144
- this._ip = ip && ip.startsWith('::ffff:') ? ip.substring(7) : ip;
145
- return this._ip;
146
- }
147
-
148
- /**
149
- * Set the request remote IPv4 address
150
- * @member {String} Request#ip
151
- * @param {String} ip - IPv4 address
152
- * @example
153
- * ```js
154
- * this.request.ip
155
- * => '127.0.0.1'
156
- * => '111.10.2.1'
157
- * ```
158
- */
159
- set ip(ip: string) {
160
- this._ip = ip;
161
- }
162
-
163
- /**
164
- * detect if response should be json
165
- * 1. url path ends with `.json`
166
- * 2. response type is set to json
167
- * 3. detect by request accept header
168
- *
169
- * @member {Boolean} Request#acceptJSON
170
- * @since 1.0.0
171
- */
172
- get acceptJSON(): boolean {
173
- if (this.path.endsWith('.json')) return true;
174
- if (this.response.type && this.response.type.indexOf('json') >= 0) return true;
175
- if (this.accepts('html', 'text', 'json') === 'json') return true;
176
- return false;
177
- }
178
-
179
- // How to read query safely
180
- // https://github.com/koajs/qs/issues/5
181
- _customQuery(cacheName: symbol, filter: (value: string | string[]) => string | string[]) {
182
- const str = this.querystring || '';
183
- let c = this[cacheName] as Record<string, Record<string, string | string[]>>;
184
- if (!c) {
185
- c = this[cacheName] = {};
186
- }
187
- let cacheQuery = c[str];
188
- if (!cacheQuery) {
189
- cacheQuery = c[str] = {};
190
- const isQueries = cacheName === QUERIES_CACHE;
191
- // `querystring.parse` CANNOT parse something like `a[foo]=1&a[bar]=2`
192
- const query = str ? querystring.parse(str) : {};
193
- for (const key in query) {
194
- if (!key) {
195
- // key is '', like `a=b&`
196
- continue;
197
- }
198
- const value = filter(query[key]!);
199
- cacheQuery[key] = value;
200
- if (isQueries && RE_ARRAY_KEY.test(key)) {
201
- // `this.queries['key'] => this.queries['key[]']` is compatibly supported
202
- const subKey = key.substring(0, key.length - 2);
203
- if (!cacheQuery[subKey]) {
204
- cacheQuery[subKey] = value;
205
- }
206
- }
207
- }
208
- }
209
- return cacheQuery;
210
- }
211
-
212
- /**
213
- * get params pass by querystring, all values are of string type.
214
- * @member {Object} Request#query
215
- * @example
216
- * ```js
217
- * GET http://127.0.0.1:7001?name=Foo&age=20&age=21
218
- * this.query
219
- * => { 'name': 'Foo', 'age': '20' }
220
- *
221
- * GET http://127.0.0.1:7001?a=b&a=c&o[foo]=bar&b[]=1&b[]=2&e=val
222
- * this.query
223
- * =>
224
- * {
225
- * "a": "b",
226
- * "o[foo]": "bar",
227
- * "b[]": "1",
228
- * "e": "val"
229
- * }
230
- * ```
231
- */
232
- get query() {
233
- return this._customQuery(QUERY_CACHE, firstValue) as Record<string, string>;
234
- }
235
-
236
- /**
237
- * get params pass by querystring, all value are Array type. {@link Request#query}
238
- * @member {Array} Request#queries
239
- * @example
240
- * ```js
241
- * GET http://127.0.0.1:7001?a=b&a=c&o[foo]=bar&b[]=1&b[]=2&e=val
242
- * this.queries
243
- * =>
244
- * {
245
- * "a": ["b", "c"],
246
- * "o[foo]": ["bar"],
247
- * "b[]": ["1", "2"],
248
- * "e": ["val"]
249
- * }
250
- * ```
251
- */
252
- get queries() {
253
- return this._customQuery(QUERIES_CACHE, arrayValue) as Record<string, string[]>;
254
- }
255
-
256
- /**
257
- * Set query-string as an object.
258
- *
259
- * @function Request#query
260
- * @param {Object} obj set querystring and query object for request.
261
- */
262
- set query(obj: Record<string, string>) {
263
- this.querystring = querystring.stringify(obj);
264
- }
265
- }
266
-
267
- declare module '@eggjs/core' {
268
- // add Request overrides types
269
- interface Request {
270
- body: any;
271
- get acceptJSON(): boolean;
272
- get query(): Record<string, string>;
273
- set query(obj: Record<string, string>);
274
- get queries(): Record<string, string[]>;
275
- }
276
- }
277
-
278
- function firstValue(value: string | string[]) {
279
- if (Array.isArray(value)) {
280
- value = value[0];
281
- }
282
- return value;
283
- }
284
-
285
- function arrayValue(value: string | string[]) {
286
- if (!Array.isArray(value)) {
287
- value = [ value ];
288
- }
289
- return value;
290
- }
291
-
292
- function getFromHeaders(request: Request, names: string) {
293
- if (!names) return '';
294
- const fields = names.split(/\s*,\s*/);
295
- for (const name of fields) {
296
- const value = request.get<string>(name);
297
- if (value) return value;
298
- }
299
- return '';
300
- }
@@ -1,3 +0,0 @@
1
- import bodyparser from 'koa-bodyparser';
2
-
3
- export default bodyparser;
@@ -1,27 +0,0 @@
1
- /**
2
- * meta middleware, should be the first middleware
3
- */
4
-
5
- import { performance } from 'node:perf_hooks';
6
- import type { Context, Next } from '../../lib/egg.js';
7
-
8
- export interface MetaMiddlewareOptions {
9
- enable: boolean;
10
- logging: boolean;
11
- }
12
-
13
- export default (options: MetaMiddlewareOptions) => {
14
- return async function meta(ctx: Context, next: Next) {
15
- if (options.logging) {
16
- ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s',
17
- ctx.host, ctx.header['user-agent']);
18
- }
19
- await next();
20
- // total response time header
21
- if (ctx.performanceStarttime) {
22
- ctx.set('x-readtime', Math.floor((performance.now() - ctx.performanceStarttime) * 1000) / 1000);
23
- } else {
24
- ctx.set('x-readtime', Date.now() - ctx.starttime);
25
- }
26
- };
27
- };
@@ -1,41 +0,0 @@
1
- import type { Next, Context } from '../../lib/egg.js';
2
-
3
- export interface NotFoundMiddlewareOptions {
4
- enable: boolean;
5
- pageUrl: string;
6
- }
7
-
8
- export default (options: NotFoundMiddlewareOptions) => {
9
- return async function notfound(ctx: Context, next: Next) {
10
- await next();
11
-
12
- if (ctx.status !== 404 || ctx.body) {
13
- return;
14
- }
15
-
16
- // set status first, make sure set body not set status
17
- ctx.status = 404;
18
-
19
- if (ctx.acceptJSON) {
20
- ctx.body = {
21
- message: 'Not Found',
22
- };
23
- return;
24
- }
25
-
26
- const notFoundHtml = '<h1>404 Not Found</h1>';
27
-
28
- // notfound handler is unimplemented
29
- if (options.pageUrl && ctx.path === options.pageUrl) {
30
- ctx.body = `${notFoundHtml}<p><pre><code>config.notfound.pageUrl(${options.pageUrl})</code></pre> is unimplemented</p>`;
31
- return;
32
- }
33
-
34
- if (options.pageUrl) {
35
- ctx.realStatus = 404;
36
- ctx.redirect(options.pageUrl);
37
- return;
38
- }
39
- ctx.body = notFoundHtml;
40
- };
41
- };
@@ -1,3 +0,0 @@
1
- import override from 'koa-override';
2
-
3
- export default override;