egg 4.0.10 → 4.1.0-beta.11

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 +12 -0
  23. package/dist/config/config.default.js +260 -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} +41 -49
  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 +22 -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} +73 -94
  79. package/dist/lib/types.js +36 -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 +81 -99
  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/plugin.d.ts +0 -122
  113. package/dist/commonjs/config/plugin.js +0 -124
  114. package/dist/commonjs/index.d.ts +0 -69
  115. package/dist/commonjs/index.js +0 -83
  116. package/dist/commonjs/lib/agent.d.ts +0 -19
  117. package/dist/commonjs/lib/agent.js +0 -58
  118. package/dist/commonjs/lib/application.d.ts +0 -67
  119. package/dist/commonjs/lib/application.js +0 -266
  120. package/dist/commonjs/lib/core/base_context_class.d.ts +0 -17
  121. package/dist/commonjs/lib/core/base_context_class.js +0 -21
  122. package/dist/commonjs/lib/core/base_context_logger.d.ts +0 -36
  123. package/dist/commonjs/lib/core/base_context_logger.js +0 -64
  124. package/dist/commonjs/lib/core/base_hook_class.d.ts +0 -11
  125. package/dist/commonjs/lib/core/base_hook_class.js +0 -29
  126. package/dist/commonjs/lib/core/context_httpclient.d.ts +0 -16
  127. package/dist/commonjs/lib/core/context_httpclient.js +0 -30
  128. package/dist/commonjs/lib/core/httpclient.d.ts +0 -14
  129. package/dist/commonjs/lib/core/httpclient.js +0 -42
  130. package/dist/commonjs/lib/core/logger.d.ts +0 -3
  131. package/dist/commonjs/lib/core/logger.js +0 -40
  132. package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +0 -50
  133. package/dist/commonjs/lib/core/messenger/IMessenger.js +0 -3
  134. package/dist/commonjs/lib/core/messenger/base.d.ts +0 -8
  135. package/dist/commonjs/lib/core/messenger/base.js +0 -31
  136. package/dist/commonjs/lib/core/messenger/index.d.ts +0 -7
  137. package/dist/commonjs/lib/core/messenger/index.js +0 -15
  138. package/dist/commonjs/lib/core/messenger/ipc.d.ts +0 -58
  139. package/dist/commonjs/lib/core/messenger/ipc.js +0 -135
  140. package/dist/commonjs/lib/core/messenger/local.d.ts +0 -60
  141. package/dist/commonjs/lib/core/messenger/local.js +0 -132
  142. package/dist/commonjs/lib/core/utils.d.ts +0 -2
  143. package/dist/commonjs/lib/core/utils.js +0 -77
  144. package/dist/commonjs/lib/egg.d.ts +0 -305
  145. package/dist/commonjs/lib/egg.js +0 -628
  146. package/dist/commonjs/lib/error/CookieLimitExceedError.d.ts +0 -5
  147. package/dist/commonjs/lib/error/CookieLimitExceedError.js +0 -16
  148. package/dist/commonjs/lib/error/MessageUnhandledRejectionError.d.ts +0 -5
  149. package/dist/commonjs/lib/error/MessageUnhandledRejectionError.js +0 -16
  150. package/dist/commonjs/lib/error/index.d.ts +0 -2
  151. package/dist/commonjs/lib/error/index.js +0 -19
  152. package/dist/commonjs/lib/loader/AgentWorkerLoader.d.ts +0 -12
  153. package/dist/commonjs/lib/loader/AgentWorkerLoader.js +0 -24
  154. package/dist/commonjs/lib/loader/AppWorkerLoader.d.ts +0 -17
  155. package/dist/commonjs/lib/loader/AppWorkerLoader.js +0 -43
  156. package/dist/commonjs/lib/loader/EggApplicationLoader.d.ts +0 -4
  157. package/dist/commonjs/lib/loader/EggApplicationLoader.js +0 -8
  158. package/dist/commonjs/lib/loader/index.d.ts +0 -3
  159. package/dist/commonjs/lib/loader/index.js +0 -22
  160. package/dist/commonjs/lib/start.d.ts +0 -24
  161. package/dist/commonjs/lib/start.js +0 -49
  162. package/dist/commonjs/lib/types.d.ts +0 -257
  163. package/dist/commonjs/lib/types.js +0 -16
  164. package/dist/commonjs/lib/utils.d.ts +0 -2
  165. package/dist/commonjs/lib/utils.js +0 -21
  166. package/dist/commonjs/package.json +0 -3
  167. package/dist/commonjs/urllib.d.ts +0 -1
  168. package/dist/commonjs/urllib.js +0 -18
  169. package/dist/esm/agent.d.ts +0 -4
  170. package/dist/esm/agent.js +0 -7
  171. package/dist/esm/app/extend/context.d.ts +0 -204
  172. package/dist/esm/app/extend/context.js +0 -259
  173. package/dist/esm/app/extend/helper.d.ts +0 -37
  174. package/dist/esm/app/extend/helper.js +0 -43
  175. package/dist/esm/app/extend/request.d.ts +0 -141
  176. package/dist/esm/app/extend/request.js +0 -264
  177. package/dist/esm/app/extend/response.d.ts +0 -31
  178. package/dist/esm/app/extend/response.js +0 -34
  179. package/dist/esm/app/middleware/body_parser.d.ts +0 -2
  180. package/dist/esm/app/middleware/body_parser.js +0 -3
  181. package/dist/esm/app/middleware/meta.d.ts +0 -10
  182. package/dist/esm/app/middleware/meta.js +0 -20
  183. package/dist/esm/app/middleware/notfound.d.ts +0 -7
  184. package/dist/esm/app/middleware/notfound.js +0 -29
  185. package/dist/esm/app/middleware/override_method.d.ts +0 -2
  186. package/dist/esm/app/middleware/override_method.js +0 -3
  187. package/dist/esm/app/middleware/site_file.d.ts +0 -9
  188. package/dist/esm/app/middleware/site_file.js +0 -53
  189. package/dist/esm/config/config.default.d.ts +0 -9
  190. package/dist/esm/config/config.default.js +0 -374
  191. package/dist/esm/config/config.local.d.ts +0 -8
  192. package/dist/esm/config/config.local.js +0 -10
  193. package/dist/esm/config/config.unittest.d.ts +0 -7
  194. package/dist/esm/config/config.unittest.js +0 -9
  195. package/dist/esm/config/favicon.png +0 -0
  196. package/dist/esm/config/plugin.d.ts +0 -122
  197. package/dist/esm/config/plugin.js +0 -122
  198. package/dist/esm/index.d.ts +0 -69
  199. package/dist/esm/index.js +0 -73
  200. package/dist/esm/lib/agent.d.ts +0 -19
  201. package/dist/esm/lib/agent.js +0 -54
  202. package/dist/esm/lib/application.d.ts +0 -67
  203. package/dist/esm/lib/application.js +0 -259
  204. package/dist/esm/lib/core/base_context_class.d.ts +0 -17
  205. package/dist/esm/lib/core/base_context_class.js +0 -17
  206. package/dist/esm/lib/core/base_context_logger.d.ts +0 -36
  207. package/dist/esm/lib/core/base_context_logger.js +0 -60
  208. package/dist/esm/lib/core/base_hook_class.d.ts +0 -11
  209. package/dist/esm/lib/core/base_hook_class.js +0 -22
  210. package/dist/esm/lib/core/context_httpclient.d.ts +0 -16
  211. package/dist/esm/lib/core/context_httpclient.js +0 -26
  212. package/dist/esm/lib/core/httpclient.d.ts +0 -14
  213. package/dist/esm/lib/core/httpclient.js +0 -38
  214. package/dist/esm/lib/core/logger.d.ts +0 -3
  215. package/dist/esm/lib/core/logger.js +0 -37
  216. package/dist/esm/lib/core/messenger/IMessenger.d.ts +0 -50
  217. package/dist/esm/lib/core/messenger/IMessenger.js +0 -2
  218. package/dist/esm/lib/core/messenger/base.d.ts +0 -8
  219. package/dist/esm/lib/core/messenger/base.js +0 -27
  220. package/dist/esm/lib/core/messenger/index.d.ts +0 -7
  221. package/dist/esm/lib/core/messenger/index.js +0 -12
  222. package/dist/esm/lib/core/messenger/ipc.d.ts +0 -58
  223. package/dist/esm/lib/core/messenger/ipc.js +0 -128
  224. package/dist/esm/lib/core/messenger/local.d.ts +0 -60
  225. package/dist/esm/lib/core/messenger/local.js +0 -128
  226. package/dist/esm/lib/core/utils.d.ts +0 -2
  227. package/dist/esm/lib/core/utils.js +0 -70
  228. package/dist/esm/lib/egg.d.ts +0 -305
  229. package/dist/esm/lib/egg.js +0 -586
  230. package/dist/esm/lib/error/CookieLimitExceedError.d.ts +0 -5
  231. package/dist/esm/lib/error/CookieLimitExceedError.js +0 -12
  232. package/dist/esm/lib/error/MessageUnhandledRejectionError.d.ts +0 -5
  233. package/dist/esm/lib/error/MessageUnhandledRejectionError.js +0 -12
  234. package/dist/esm/lib/error/index.d.ts +0 -2
  235. package/dist/esm/lib/error/index.js +0 -3
  236. package/dist/esm/lib/loader/AgentWorkerLoader.d.ts +0 -12
  237. package/dist/esm/lib/loader/AgentWorkerLoader.js +0 -20
  238. package/dist/esm/lib/loader/AppWorkerLoader.d.ts +0 -17
  239. package/dist/esm/lib/loader/AppWorkerLoader.js +0 -39
  240. package/dist/esm/lib/loader/EggApplicationLoader.d.ts +0 -4
  241. package/dist/esm/lib/loader/EggApplicationLoader.js +0 -4
  242. package/dist/esm/lib/loader/index.d.ts +0 -3
  243. package/dist/esm/lib/loader/index.js +0 -4
  244. package/dist/esm/lib/start.d.ts +0 -24
  245. package/dist/esm/lib/start.js +0 -43
  246. package/dist/esm/lib/types.d.ts +0 -257
  247. package/dist/esm/lib/types.js +0 -14
  248. package/dist/esm/lib/utils.d.ts +0 -2
  249. package/dist/esm/lib/utils.js +0 -14
  250. package/dist/esm/package.json +0 -3
  251. package/dist/esm/urllib.d.ts +0 -1
  252. package/dist/esm/urllib.js +0 -2
  253. package/dist/package.json +0 -4
  254. package/src/agent.ts +0 -7
  255. package/src/app/extend/context.ts +0 -332
  256. package/src/app/extend/request.ts +0 -300
  257. package/src/app/middleware/body_parser.ts +0 -3
  258. package/src/app/middleware/meta.ts +0 -27
  259. package/src/app/middleware/notfound.ts +0 -41
  260. package/src/app/middleware/override_method.ts +0 -3
  261. package/src/app/middleware/site_file.ts +0 -68
  262. package/src/config/config.default.ts +0 -404
  263. package/src/config/config.local.ts +0 -11
  264. package/src/config/config.unittest.ts +0 -10
  265. package/src/config/favicon.png +0 -0
  266. package/src/index.ts +0 -120
  267. package/src/lib/agent.ts +0 -66
  268. package/src/lib/application.ts +0 -308
  269. package/src/lib/core/base_context_class.ts +0 -24
  270. package/src/lib/core/base_context_logger.ts +0 -67
  271. package/src/lib/core/base_hook_class.ts +0 -30
  272. package/src/lib/core/context_httpclient.ts +0 -33
  273. package/src/lib/core/httpclient.ts +0 -64
  274. package/src/lib/core/logger.ts +0 -42
  275. package/src/lib/core/messenger/base.ts +0 -30
  276. package/src/lib/core/messenger/index.ts +0 -16
  277. package/src/lib/core/messenger/ipc.ts +0 -146
  278. package/src/lib/core/messenger/local.ts +0 -147
  279. package/src/lib/core/utils.ts +0 -77
  280. package/src/lib/egg.ts +0 -705
  281. package/src/lib/error/CookieLimitExceedError.ts +0 -12
  282. package/src/lib/error/MessageUnhandledRejectionError.ts +0 -12
  283. package/src/lib/error/index.ts +0 -2
  284. package/src/lib/loader/AgentWorkerLoader.ts +0 -21
  285. package/src/lib/loader/AppWorkerLoader.ts +0 -42
  286. package/src/lib/loader/EggApplicationLoader.ts +0 -5
  287. package/src/lib/loader/index.ts +0 -3
  288. package/src/lib/start.ts +0 -66
  289. package/src/lib/utils.ts +0 -15
  290. package/src/urllib.ts +0 -1
  291. /package/dist/{commonjs/config → config}/favicon.png +0 -0
package/src/lib/egg.ts DELETED
@@ -1,705 +0,0 @@
1
- import { performance } from 'node:perf_hooks';
2
- import path from 'node:path';
3
- import fs from 'node:fs';
4
- import http, { type IncomingMessage, type ServerResponse } from 'node:http';
5
- import inspector from 'node:inspector';
6
- import { AsyncLocalStorage } from 'node:async_hooks';
7
- import {
8
- EggCore,
9
- Request as EggCoreRequest,
10
- Response as EggCoreResponse,
11
- Router,
12
- } from '@eggjs/core';
13
- import type {
14
- EggCoreOptions,
15
- Next, MiddlewareFunc as EggCoreMiddlewareFunc,
16
- ILifecycleBoot,
17
- } from '@eggjs/core';
18
- import { utils as eggUtils } from '@eggjs/core';
19
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
20
- // @ts-ignore
21
- import createClusterClient, { close as closeClusterClient } from 'cluster-client';
22
- import { extend } from 'extend2';
23
- import { EggContextLogger as ContextLogger, EggLoggers, EggLogger } from 'egg-logger';
24
- import { Cookies as ContextCookies } from '@eggjs/cookies';
25
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
26
- // @ts-ignore
27
- import CircularJSON from 'circular-json-for-egg';
28
- import type { Agent } from './agent.js';
29
- import type { Application } from './application.js';
30
- import Context from '../app/extend/context.js';
31
- import type { EggAppConfig } from './types.js';
32
- import { create as createMessenger, IMessenger } from './core/messenger/index.js';
33
- import { ContextHttpClient } from './core/context_httpclient.js';
34
- import {
35
- HttpClient,
36
- type HttpClientRequestOptions, type HttpClientRequestURL, type HttpClientResponse,
37
- type HttpClientOptions,
38
- } from './core/httpclient.js';
39
- import { createLoggers } from './core/logger.js';
40
- import { convertObject } from './core/utils.js';
41
- import { BaseContextClass } from './core/base_context_class.js';
42
- import { BaseHookClass } from './core/base_hook_class.js';
43
- import type { EggApplicationLoader } from './loader/index.js';
44
- import { getSourceDirname } from './utils.js';
45
-
46
- const EGG_PATH = Symbol.for('egg#eggPath');
47
-
48
- export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir'> {
49
- mode?: 'cluster' | 'single';
50
- clusterPort?: number;
51
- baseDir?: string;
52
- }
53
-
54
- export class Request extends EggCoreRequest {
55
- declare app: EggCore;
56
- declare response: Response;
57
- declare ctx: Context;
58
- }
59
-
60
- export class Response extends EggCoreResponse {
61
- declare app: EggCore;
62
- declare request: Request;
63
- declare ctx: Context;
64
- }
65
-
66
- // export egg types
67
- export type {
68
- ILifecycleBoot,
69
- // keep compatible with egg version 3.x
70
- ILifecycleBoot as IBoot,
71
- Next,
72
- };
73
- // keep compatible with egg version 3.x
74
- export type EggContext = Context;
75
- export type MiddlewareFunc<T extends Context = Context> = EggCoreMiddlewareFunc<T>;
76
-
77
- // export egg classes
78
- export {
79
- Context,
80
- Router,
81
- };
82
-
83
- /**
84
- * Based on koa's Application
85
- * @see https://github.com/eggjs/egg-core
86
- * @see https://github.com/eggjs/koa/blob/master/src/application.ts
87
- * @augments EggCore
88
- */
89
- export class EggApplicationCore extends EggCore {
90
- declare ctxStorage: AsyncLocalStorage<Context>;
91
- // export context base classes, let framework can impl sub class and over context extend easily.
92
- ContextCookies = ContextCookies;
93
- ContextLogger = ContextLogger;
94
- ContextHttpClient = ContextHttpClient;
95
- HttpClient = HttpClient;
96
- // keep compatible with egg version 3.x
97
- HttpClientNext = HttpClient;
98
- /**
99
- * Retrieve base context class
100
- * @member {BaseContextClass} BaseContextClass
101
- * @since 1.0.0
102
- */
103
- BaseContextClass = BaseContextClass;
104
-
105
- /**
106
- * Retrieve base controller
107
- * @member {Controller} Controller
108
- * @since 1.0.0
109
- */
110
- Controller = BaseContextClass;
111
-
112
- /**
113
- * Retrieve base service
114
- * @member {Service} Service
115
- * @since 1.0.0
116
- */
117
- Service = BaseContextClass;
118
-
119
- /**
120
- * Retrieve base subscription
121
- * @member {Subscription} Subscription
122
- * @since 2.12.0
123
- */
124
- Subscription = BaseContextClass;
125
-
126
- /**
127
- * Retrieve base context class
128
- * @member {BaseHookClass} BaseHookClass
129
- */
130
- BaseHookClass = BaseHookClass;
131
-
132
- /**
133
- * Retrieve base boot
134
- * @member {Boot}
135
- * @alias BaseHookClass
136
- */
137
- Boot = BaseHookClass;
138
-
139
- declare options: Required<EggApplicationCoreOptions>;
140
-
141
- #httpClient?: HttpClient;
142
- #loggers?: EggLoggers;
143
- #clusterClients: any[] = [];
144
-
145
- readonly messenger: IMessenger;
146
- agent?: Agent;
147
- application?: Application;
148
- declare loader: EggApplicationLoader;
149
-
150
- /**
151
- * @class
152
- * @param {Object} options
153
- * - {Object} [type] - type of instance, Agent and Application both extend koa, type can determine what it is.
154
- * - {String} [baseDir] - app root dir, default is `process.cwd()`
155
- * - {Object} [plugins] - custom plugin config, use it in unittest
156
- * - {String} [mode] - process mode, can be cluster / single, default is `cluster`
157
- */
158
- constructor(options?: EggApplicationCoreOptions) {
159
- options = {
160
- mode: 'cluster',
161
- type: 'application',
162
- baseDir: process.cwd(),
163
- ...options,
164
- };
165
- super(options);
166
- /**
167
- * messenger instance
168
- * @member {Messenger}
169
- * @since 1.0.0
170
- */
171
- this.messenger = createMessenger(this);
172
-
173
- // trigger `serverDidReady` hook when all the app workers
174
- // and agent worker are ready
175
- this.messenger.once('egg-ready', () => {
176
- this.lifecycle.triggerServerDidReady();
177
- });
178
- this.lifecycle.registerBeforeStart(async () => {
179
- await this.load();
180
- }, 'load files');
181
- }
182
-
183
- /**
184
- * @deprecated please use `options` property instead
185
- */
186
- get _options() {
187
- return this.options;
188
- }
189
-
190
- protected async loadConfig() {
191
- await this.loader.loadConfig();
192
- }
193
-
194
- protected async load() {
195
- await this.loadConfig();
196
- // dump config after ready, ensure all the modifications during start will be recorded
197
- // make sure dumpConfig is the last ready callback
198
- this.ready(() => process.nextTick(() => {
199
- const dumpStartTime = Date.now();
200
- this.dumpConfig();
201
- this.dumpTiming();
202
- this.coreLogger.info('[egg] dump config after ready, %sms', Date.now() - dumpStartTime);
203
- }));
204
- this.#setupTimeoutTimer();
205
-
206
- this.console.info('[egg] App root: %s', this.baseDir);
207
- this.console.info('[egg] All *.log files save on %j', this.config.logger.dir);
208
- this.console.info('[egg] Loaded enabled plugin %j', this.loader.orderPlugins);
209
-
210
- // Listen the error that promise had not catch, then log it in common-error
211
- this._unhandledRejectionHandler = this._unhandledRejectionHandler.bind(this);
212
- process.on('unhandledRejection', this._unhandledRejectionHandler);
213
-
214
- // register close function
215
- this.lifecycle.registerBeforeClose(async () => {
216
- // close all cluster clients
217
- for (const clusterClient of this.#clusterClients) {
218
- await closeClusterClient(clusterClient);
219
- }
220
- this.#clusterClients = [];
221
-
222
- // single process mode will close agent before app close
223
- if (this.type === 'application' && this.options.mode === 'single') {
224
- await this.agent!.close();
225
- }
226
-
227
- for (const logger of this.loggers.values()) {
228
- logger.close();
229
- }
230
- this.messenger.close();
231
- process.removeListener('unhandledRejection', this._unhandledRejectionHandler);
232
- });
233
-
234
- await this.loader.load();
235
- }
236
-
237
- /**
238
- * Usage: new ApiClient({ cluster: app.cluster })
239
- */
240
- get cluster() {
241
- return this.clusterWrapper.bind(this);
242
- }
243
-
244
- /**
245
- * Wrap the Client with Leader/Follower Pattern
246
- *
247
- * @description almost the same as Agent.cluster API, the only different is that this method create Follower.
248
- *
249
- * @see https://github.com/node-modules/cluster-client
250
- * @param {Function} clientClass - client class function
251
- * @param {Object} [options]
252
- * - {Boolean} [autoGenerate] - whether generate delegate rule automatically, default is true
253
- * - {Function} [formatKey] - a method to transform the subscription info into a string,default is JSON.stringify
254
- * - {Object} [transcode|JSON.stringify/parse]
255
- * - {Function} encode - custom serialize method
256
- * - {Function} decode - custom deserialize method
257
- * - {Boolean} [isBroadcast] - whether broadcast subscription result to all followers or just one, default is true
258
- * - {Number} [responseTimeout] - response timeout, default is 3 seconds
259
- * - {Number} [maxWaitTime|30000] - leader startup max time, default is 30 seconds
260
- * @return {ClientWrapper} wrapper
261
- */
262
- clusterWrapper(clientClass: unknown, options?: object) {
263
- const clientClassOptions = {
264
- ...this.config.clusterClient,
265
- ...options,
266
- singleMode: this.options.mode === 'single',
267
- // cluster need a port that can't conflict on the environment
268
- port: this.options.clusterPort,
269
- // agent worker is leader, app workers are follower
270
- isLeader: this.type === 'agent',
271
- logger: this.coreLogger,
272
- // debug mode does not check heartbeat
273
- isCheckHeartbeat: this.config.env === 'prod' ? true : inspector.url() === undefined,
274
- };
275
- const client = createClusterClient(clientClass, clientClassOptions);
276
- this.#patchClusterClient(client);
277
- return client;
278
- }
279
-
280
- /**
281
- * print the information when console.log(app)
282
- * @return {Object} inspected app.
283
- * @since 1.0.0
284
- * @example
285
- * ```js
286
- * console.log(app);
287
- * =>
288
- * {
289
- * name: 'mock-app',
290
- * env: 'test',
291
- * subdomainOffset: 2,
292
- * config: '<egg config>',
293
- * controller: '<egg controller>',
294
- * service: '<egg service>',
295
- * middlewares: '<egg middlewares>',
296
- * urllib: '<egg urllib>',
297
- * loggers: '<egg loggers>'
298
- * }
299
- * ```
300
- */
301
- inspect(): any {
302
- const res = {
303
- env: this.config.env,
304
- };
305
-
306
- function delegate(res: any, app: any, keys: string[]) {
307
- for (const key of keys) {
308
- if (app[key]) {
309
- res[key] = app[key];
310
- }
311
- }
312
- }
313
-
314
- function abbr(res: any, app: any, keys: string[]) {
315
- for (const key of keys) {
316
- if (app[key]) {
317
- res[key] = `<egg ${key}>`;
318
- }
319
- }
320
- }
321
-
322
- delegate(res, this, [
323
- 'name',
324
- 'baseDir',
325
- 'subdomainOffset',
326
- ]);
327
-
328
- abbr(res, this, [
329
- 'config',
330
- 'controller',
331
- 'httpclient',
332
- 'loggers',
333
- 'middlewares',
334
- 'router',
335
- 'serviceClasses',
336
- ]);
337
-
338
- return res;
339
- }
340
-
341
- toJSON() {
342
- return this.inspect();
343
- }
344
-
345
- /**
346
- * http request helper base on {@link httpclient}, it will auto save httpclient log.
347
- * Keep the same api with `httpclient.request(url, args)`.
348
- *
349
- * See https://github.com/node-modules/urllib#api-doc for more details.
350
- *
351
- * @param {String} url request url address.
352
- * @param {Object} options
353
- * - method {String} - Request method, defaults to GET. Could be GET, POST, DELETE or PUT. Alias 'type'.
354
- * - data {Object} - Data to be sent. Will be stringify automatically.
355
- * - dataType {String} - String - Type of response data. Could be `text` or `json`.
356
- * If it's `text`, the callback data would be a String.
357
- * If it's `json`, the data of callback would be a parsed JSON Object.
358
- * Default callback data would be a Buffer.
359
- * - headers {Object} - Request headers.
360
- * - timeout {Number} - Request timeout in milliseconds. Defaults to exports.TIMEOUT.
361
- * Include remote server connecting timeout and response timeout.
362
- * When timeout happen, will return ConnectionTimeout or ResponseTimeout.
363
- * - auth {String} - `username:password` used in HTTP Basic Authorization.
364
- * - followRedirect {Boolean} - follow HTTP 3xx responses as redirects. defaults to false.
365
- * - gzip {Boolean} - let you get the res object when request connected, default false. alias customResponse
366
- * - nestedQuerystring {Boolean} - urllib default use querystring to stringify form data which don't
367
- * support nested object, will use qs instead of querystring to support nested object by set this option to true.
368
- * - more options see https://github.com/node-modules/urllib
369
- * @return {Object}
370
- * - status {Number} - HTTP response status
371
- * - headers {Object} - HTTP response headers
372
- * - res {Object} - HTTP response meta
373
- * - data {Object} - HTTP response body
374
- *
375
- * @example
376
- * ```js
377
- * const result = await app.curl('http://example.com/foo.json', {
378
- * method: 'GET',
379
- * dataType: 'json',
380
- * });
381
- * console.log(result.status, result.headers, result.data);
382
- * ```
383
- */
384
- async curl<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions): Promise<HttpClientResponse<T>> {
385
- return await this.httpClient.request<T>(url, options);
386
- }
387
-
388
- /**
389
- * Create a new HttpClient instance with custom options
390
- * @param {Object} [options] HttpClient init options
391
- */
392
- createHttpClient(options?: HttpClientOptions): HttpClient {
393
- return new this.HttpClient(this, options);
394
- }
395
-
396
- /**
397
- * HttpClient instance
398
- * @see https://github.com/node-modules/urllib
399
- * @member {HttpClient}
400
- */
401
- get httpClient(): HttpClient {
402
- if (!this.#httpClient) {
403
- this.#httpClient = this.createHttpClient();
404
- }
405
- return this.#httpClient;
406
- }
407
-
408
- /**
409
- * @deprecated please use httpClient instead
410
- * @alias httpClient
411
- * @member {HttpClient}
412
- */
413
- get httpclient() {
414
- return this.httpClient;
415
- }
416
-
417
- /**
418
- * All loggers contain logger, coreLogger and customLogger
419
- * @member {Object}
420
- * @since 1.0.0
421
- */
422
- get loggers() {
423
- if (!this.#loggers) {
424
- this.#loggers = createLoggers(this);
425
- }
426
- return this.#loggers;
427
- }
428
-
429
- /**
430
- * Get logger by name, it's equal to app.loggers['name'],
431
- * but you can extend it with your own logical.
432
- * @param {String} name - logger name
433
- * @return {Logger} logger
434
- */
435
- getLogger(name: string): EggLogger {
436
- return this.loggers[name] || null;
437
- }
438
-
439
- /**
440
- * application logger, log file is `$HOME/logs/{appname}/{appname}-web`
441
- * @member {Logger}
442
- * @since 1.0.0
443
- */
444
- get logger() {
445
- return this.getLogger('logger');
446
- }
447
-
448
- /**
449
- * core logger for framework and plugins, log file is `$HOME/logs/{appname}/egg-web`
450
- * @member {Logger}
451
- * @since 1.0.0
452
- */
453
- get coreLogger() {
454
- return this.getLogger('coreLogger');
455
- }
456
-
457
- _unhandledRejectionHandler(err: any) {
458
- this.coreLogger.error('[egg:unhandledRejection] %s', err && err.message || err);
459
- if (!(err instanceof Error)) {
460
- const newError = new Error(String(err));
461
- // err maybe an object, try to copy the name, message and stack to the new error instance
462
- if (err) {
463
- if (err.name) newError.name = err.name;
464
- if (err.message) newError.message = err.message;
465
- if (err.stack) newError.stack = err.stack;
466
- }
467
- err = newError;
468
- }
469
- if (err.name === 'Error') {
470
- err.name = 'unhandledRejectionError';
471
- }
472
- this.coreLogger.error(err);
473
- }
474
-
475
- /**
476
- * dump out the config and meta object
477
- * @private
478
- */
479
- dumpConfigToObject() {
480
- let ignoreList: (string | RegExp)[];
481
- try {
482
- // support array and set
483
- ignoreList = Array.from(this.config.dump.ignore);
484
- } catch (_) {
485
- ignoreList = [];
486
- }
487
- const config = extend(true, {}, {
488
- config: this.config,
489
- plugins: this.loader.allPlugins,
490
- appInfo: this.loader.appInfo,
491
- });
492
- convertObject(config, ignoreList);
493
- return {
494
- config,
495
- meta: this.loader.configMeta,
496
- };
497
- }
498
-
499
- /**
500
- * save app.config to `run/${type}_config.json`
501
- * @private
502
- */
503
- dumpConfig() {
504
- const rundir = this.config.rundir;
505
- try {
506
- if (!fs.existsSync(rundir)) {
507
- fs.mkdirSync(rundir);
508
- }
509
-
510
- // get dumped object
511
- const { config, meta } = this.dumpConfigToObject();
512
-
513
- // dump config
514
- const dumpFile = path.join(rundir, `${this.type}_config.json`);
515
- fs.writeFileSync(dumpFile, CircularJSON.stringify(config, null, 2));
516
-
517
- // dump config meta
518
- const dumpMetaFile = path.join(rundir, `${this.type}_config_meta.json`);
519
- fs.writeFileSync(dumpMetaFile, CircularJSON.stringify(meta, null, 2));
520
- } catch (err: any) {
521
- this.coreLogger.warn(`[egg] dumpConfig error: ${err.message}`);
522
- }
523
- }
524
-
525
- dumpTiming() {
526
- try {
527
- const items = this.timing.toJSON();
528
- const rundir = this.config.rundir;
529
- const dumpFile = path.join(rundir, `${this.type}_timing_${process.pid}.json`);
530
- fs.writeFileSync(dumpFile, CircularJSON.stringify(items, null, 2));
531
- this.coreLogger.info(this.timing.toString());
532
- // only disable, not clear bootstrap timing data.
533
- this.timing.disable();
534
- // show duration >= ${slowBootActionMinDuration}ms action to warning log
535
- for (const item of items) {
536
- // ignore #0 name: Process Start
537
- if (item.index > 0 && item.duration && item.duration >= this.config.dump.timing.slowBootActionMinDuration) {
538
- this.coreLogger.warn('[egg][dumpTiming][slow-boot-action] #%d %dms, name: %s',
539
- item.index, item.duration, item.name);
540
- }
541
- }
542
- } catch (err: any) {
543
- this.coreLogger.warn(`[egg] dumpTiming error: ${err.message}`);
544
- }
545
- }
546
-
547
- get [EGG_PATH]() {
548
- return getSourceDirname();
549
- }
550
-
551
- #setupTimeoutTimer() {
552
- const startTimeoutTimer = setTimeout(() => {
553
- this.coreLogger.error(this.timing.toString());
554
- this.coreLogger.error(`${this.type} still doesn't ready after ${this.config.workerStartTimeout} ms.`);
555
- // log unfinished
556
- const items = this.timing.toJSON();
557
- for (const item of items) {
558
- if (item.end) continue;
559
- this.coreLogger.error(`unfinished timing item: ${CircularJSON.stringify(item)}`);
560
- }
561
- this.coreLogger.error('[egg][setupTimeoutTimer] check run/%s_timing_%s.json for more details.',
562
- this.type, process.pid);
563
- this.emit('startTimeout');
564
- this.dumpConfig();
565
- this.dumpTiming();
566
- }, this.config.workerStartTimeout);
567
- this.ready(() => clearTimeout(startTimeoutTimer));
568
- }
569
-
570
- get config() {
571
- return super.config as EggAppConfig;
572
- }
573
-
574
- /**
575
- * app.env delegate app.config.env
576
- * @deprecated
577
- */
578
- get env() {
579
- this.deprecate('please use app.config.env instead');
580
- return this.config.env;
581
- }
582
- /* eslint no-empty-function: off */
583
- set env(_) {}
584
-
585
- /**
586
- * app.proxy delegate app.config.proxy
587
- * @deprecated
588
- */
589
- get proxy() {
590
- // this.deprecate('please use app.config.proxy instead');
591
- return this.config.proxy;
592
- }
593
- /* eslint no-empty-function: off */
594
- set proxy(_) {}
595
-
596
- #patchClusterClient(client: any) {
597
- const rawCreate = client.create;
598
- client.create = (...args: any) => {
599
- const realClient = rawCreate.apply(client, args);
600
- this.#clusterClients.push(realClient);
601
- return realClient;
602
- };
603
- }
604
-
605
- /**
606
- * Create an anonymous context, the context isn't request level, so the request is mocked.
607
- * then you can use context level API like `ctx.service`
608
- * @member {String} EggApplication#createAnonymousContext
609
- * @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
610
- * @return {Context} context
611
- */
612
- createAnonymousContext(req?: any): EggContext {
613
- const request: any = {
614
- headers: {
615
- host: '127.0.0.1',
616
- 'x-forwarded-for': '127.0.0.1',
617
- },
618
- query: {},
619
- querystring: '',
620
- host: '127.0.0.1',
621
- hostname: '127.0.0.1',
622
- protocol: 'http',
623
- secure: 'false',
624
- method: 'GET',
625
- url: '/',
626
- path: '/',
627
- socket: {
628
- remoteAddress: '127.0.0.1',
629
- remotePort: 7001,
630
- },
631
- };
632
- if (req) {
633
- for (const key in req) {
634
- if (key === 'headers' || key === 'query' || key === 'socket') {
635
- Object.assign(request[key], req[key]);
636
- } else {
637
- request[key] = req[key];
638
- }
639
- }
640
- }
641
- const response = new http.ServerResponse(request);
642
- return this.createContext(request, response);
643
- }
644
-
645
- /**
646
- * Run async function in the anonymous context scope
647
- * @see Context#runInAnonymousContextScope
648
- * @param {Function} scope - the first args is an anonymous ctx, scope should be async function
649
- * @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
650
- */
651
- async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
652
- const ctx = this.createAnonymousContext(req);
653
- if (!scope.name) {
654
- Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
655
- }
656
- return await this.ctxStorage.run(ctx, async () => {
657
- return await scope(ctx);
658
- });
659
- }
660
-
661
- /**
662
- * Create egg context
663
- * @function EggApplication#createContext
664
- * @param {Req} req - node native Request object
665
- * @param {Res} res - node native Response object
666
- * @return {Context} context object
667
- */
668
- createContext(req: IncomingMessage, res: ServerResponse): Context {
669
- const context = Object.create(this.context) as Context;
670
- const request = context.request = Object.create(this.request);
671
- const response = context.response = Object.create(this.response);
672
- context.app = request.app = response.app = this as any;
673
- context.req = request.req = response.req = req;
674
- context.res = request.res = response.res = res;
675
- request.ctx = response.ctx = context;
676
- request.response = response;
677
- response.request = request;
678
- context.onerror = context.onerror.bind(context);
679
- context.originalUrl = request.originalUrl = req.url as string;
680
- context.starttime = Date.now();
681
- context.performanceStarttime = performance.now();
682
- return context;
683
- }
684
- }
685
-
686
- declare module '@eggjs/core' {
687
- // add EggApplicationCore overrides types
688
- interface EggCore {
689
- inspect(): any;
690
- get currentContext(): EggContext | undefined;
691
- ctxStorage: AsyncLocalStorage<EggContext>;
692
- getLogger(name: string): EggLogger;
693
- get loggers(): EggLoggers;
694
- createHttpClient(options?: HttpClientOptions): HttpClient;
695
- HttpClient: typeof HttpClient;
696
- get httpClient(): HttpClient;
697
- curl<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions): Promise<HttpClientResponse<T>>;
698
- createAnonymousContext(req?: any): EggContext;
699
- runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown): Promise<void>;
700
- readonly messenger: IMessenger;
701
- Subscription: typeof BaseContextClass;
702
- BaseHookClass: typeof BaseHookClass;
703
- Boot: typeof BaseHookClass;
704
- }
705
- }
@@ -1,12 +0,0 @@
1
- export class CookieLimitExceedError extends Error {
2
- key: string;
3
- cookie: string;
4
-
5
- constructor(key: string, cookie: string) {
6
- super(`cookie ${key}'s length(${cookie.length}) exceed the limit(4093)`);
7
- this.name = this.constructor.name;
8
- this.key = key;
9
- this.cookie = cookie;
10
- Error.captureStackTrace(this, this.constructor);
11
- }
12
- }