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
@@ -0,0 +1,12 @@
1
+ import { EggApplicationCore } from "../../egg.js";
2
+ import { EventEmitter, captureRejectionSymbol } from "node:events";
3
+
4
+ //#region src/lib/core/messenger/base.d.ts
5
+ declare class BaseMessenger extends EventEmitter {
6
+ protected readonly egg: EggApplicationCore;
7
+ constructor(egg: EggApplicationCore);
8
+ [captureRejectionSymbol](err: Error, event: string | symbol, ...args: any[]): void;
9
+ emit(eventName: string | symbol, ...args: any[]): boolean;
10
+ }
11
+ //#endregion
12
+ export { BaseMessenger };
@@ -0,0 +1,29 @@
1
+ import { MessageUnhandledRejectionError } from "../../error/MessageUnhandledRejectionError.js";
2
+ import "../../egg.js";
3
+ import { EventEmitter, captureRejectionSymbol } from "node:events";
4
+
5
+ //#region src/lib/core/messenger/base.ts
6
+ var BaseMessenger = class extends EventEmitter {
7
+ egg;
8
+ constructor(egg) {
9
+ super({ captureRejections: true });
10
+ this.egg = egg;
11
+ }
12
+ [captureRejectionSymbol](err, event, ...args) {
13
+ this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, event, args));
14
+ }
15
+ emit(eventName, ...args) {
16
+ const hasListeners = this.listenerCount(eventName) > 0;
17
+ try {
18
+ return super.emit(eventName, ...args);
19
+ } catch (e) {
20
+ let err = e;
21
+ if (!(err instanceof Error)) err = new Error(String(err));
22
+ this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, eventName, args));
23
+ return hasListeners;
24
+ }
25
+ }
26
+ };
27
+
28
+ //#endregion
29
+ export { BaseMessenger };
@@ -0,0 +1,11 @@
1
+ import { IMessenger } from "./IMessenger.js";
2
+ import { EggApplicationCore } from "../../egg.js";
3
+
4
+ //#region src/lib/core/messenger/index.d.ts
5
+
6
+ /**
7
+ * @class Messenger
8
+ */
9
+ declare function create(egg: EggApplicationCore): IMessenger;
10
+ //#endregion
11
+ export { type IMessenger, create };
@@ -0,0 +1,13 @@
1
+ import { Messenger } from "./local.js";
2
+ import { Messenger as Messenger$1 } from "./ipc.js";
3
+
4
+ //#region src/lib/core/messenger/index.ts
5
+ /**
6
+ * @class Messenger
7
+ */
8
+ function create(egg) {
9
+ return egg.options.mode === "single" ? new Messenger(egg) : new Messenger$1(egg);
10
+ }
11
+
12
+ //#endregion
13
+ export { create };
@@ -0,0 +1,63 @@
1
+ import { IMessenger } from "./IMessenger.js";
2
+ import { EggApplicationCore } from "../../egg.js";
3
+ import { BaseMessenger } from "./base.js";
4
+
5
+ //#region src/lib/core/messenger/ipc.d.ts
6
+
7
+ /**
8
+ * Communication between app worker and agent worker by IPC channel
9
+ */
10
+ declare class Messenger extends BaseMessenger implements IMessenger {
11
+ readonly pid: string;
12
+ opids: string[];
13
+ constructor(egg: EggApplicationCore);
14
+ /**
15
+ * Send message to all agent and app
16
+ * @param {String} action - message key
17
+ * @param {Object} data - message value
18
+ * @return {Messenger} this
19
+ */
20
+ broadcast(action: string, data?: unknown): Messenger;
21
+ /**
22
+ * send message to the specified process
23
+ * @param {String} workerId - the workerId of the receiver
24
+ * @param {String} action - message key
25
+ * @param {Object} data - message value
26
+ * @return {Messenger} this
27
+ */
28
+ sendTo(workerId: string, action: string, data?: unknown): Messenger;
29
+ /**
30
+ * send message to one app worker by random
31
+ * - if it's running in agent, it will send to one of app workers
32
+ * - if it's running in app, it will send to agent
33
+ * @param {String} action - message key
34
+ * @param {Object} data - message value
35
+ * @return {Messenger} this
36
+ */
37
+ sendRandom(action: string, data?: unknown): Messenger;
38
+ /**
39
+ * send message to app
40
+ * @param {String} action - message key
41
+ * @param {Object} data - message value
42
+ * @return {Messenger} this
43
+ */
44
+ sendToApp(action: string, data?: unknown): Messenger;
45
+ /**
46
+ * send message to agent
47
+ * @param {String} action - message key
48
+ * @param {Object} data - message value
49
+ * @return {Messenger} this
50
+ */
51
+ sendToAgent(action: string, data?: unknown): Messenger;
52
+ /**
53
+ * @param {String} action - message key
54
+ * @param {Object} data - message value
55
+ * @param {String} to - let master know how to send message
56
+ * @return {Messenger} this
57
+ */
58
+ send(action: string, data: unknown | undefined, to?: string): Messenger;
59
+ onMessage(message: any): void;
60
+ close(): void;
61
+ }
62
+ //#endregion
63
+ export { Messenger };
@@ -0,0 +1,122 @@
1
+ import { BaseMessenger } from "./base.js";
2
+ import { debuglog } from "node:util";
3
+ import workerThreads from "node:worker_threads";
4
+ import { sendmessage } from "sendmessage";
5
+
6
+ //#region src/lib/core/messenger/ipc.ts
7
+ const debug = debuglog("egg/lib/core/messenger/ipc");
8
+ /**
9
+ * Communication between app worker and agent worker by IPC channel
10
+ */
11
+ var Messenger = class extends BaseMessenger {
12
+ pid;
13
+ opids = [];
14
+ constructor(egg) {
15
+ super(egg);
16
+ this.pid = String(process.pid);
17
+ this.on("egg-pids", (workerIds) => {
18
+ debug("[%s:%s] got egg-pids %j", this.egg.type, this.pid, workerIds);
19
+ this.opids = workerIds.map((workerId) => String(workerId));
20
+ });
21
+ this.onMessage = this.onMessage.bind(this);
22
+ process.on("message", this.onMessage);
23
+ if (!workerThreads.isMainThread) workerThreads.parentPort.on("message", this.onMessage);
24
+ }
25
+ /**
26
+ * Send message to all agent and app
27
+ * @param {String} action - message key
28
+ * @param {Object} data - message value
29
+ * @return {Messenger} this
30
+ */
31
+ broadcast(action, data) {
32
+ debug("[%s:%s] broadcast %s with %j", this.egg.type, this.pid, action, data);
33
+ this.send(action, data, "app");
34
+ this.send(action, data, "agent");
35
+ return this;
36
+ }
37
+ /**
38
+ * send message to the specified process
39
+ * @param {String} workerId - the workerId of the receiver
40
+ * @param {String} action - message key
41
+ * @param {Object} data - message value
42
+ * @return {Messenger} this
43
+ */
44
+ sendTo(workerId, action, data) {
45
+ debug("[%s:%s] send %s with %j to workerId:%s", this.egg.type, this.pid, action, data, workerId);
46
+ sendmessage(process, {
47
+ action,
48
+ data,
49
+ receiverPid: String(workerId),
50
+ receiverWorkerId: String(workerId)
51
+ });
52
+ return this;
53
+ }
54
+ /**
55
+ * send message to one app worker by random
56
+ * - if it's running in agent, it will send to one of app workers
57
+ * - if it's running in app, it will send to agent
58
+ * @param {String} action - message key
59
+ * @param {Object} data - message value
60
+ * @return {Messenger} this
61
+ */
62
+ sendRandom(action, data) {
63
+ if (this.opids.length === 0) {
64
+ debug("[%s:%s] no pids, ignore sendRandom %s with %j", this.egg.type, this.pid, action, data);
65
+ return this;
66
+ }
67
+ const index = Math.floor(Math.random() * this.opids.length);
68
+ const workerId = this.opids[index];
69
+ this.sendTo(workerId, action, data);
70
+ return this;
71
+ }
72
+ /**
73
+ * send message to app
74
+ * @param {String} action - message key
75
+ * @param {Object} data - message value
76
+ * @return {Messenger} this
77
+ */
78
+ sendToApp(action, data) {
79
+ debug("[%s:%s] send %s with %j to all app", this.egg.type, this.pid, action, data);
80
+ this.send(action, data, "app");
81
+ return this;
82
+ }
83
+ /**
84
+ * send message to agent
85
+ * @param {String} action - message key
86
+ * @param {Object} data - message value
87
+ * @return {Messenger} this
88
+ */
89
+ sendToAgent(action, data) {
90
+ debug("[%s:%s] send %s with %j to all agent", this.egg.type, this.pid, action, data);
91
+ this.send(action, data, "agent");
92
+ return this;
93
+ }
94
+ /**
95
+ * @param {String} action - message key
96
+ * @param {Object} data - message value
97
+ * @param {String} to - let master know how to send message
98
+ * @return {Messenger} this
99
+ */
100
+ send(action, data, to) {
101
+ sendmessage(process, {
102
+ action,
103
+ data,
104
+ to
105
+ });
106
+ return this;
107
+ }
108
+ onMessage(message) {
109
+ if (typeof message?.action === "string") {
110
+ debug("[%s:%s] got message %s with %j, receiverWorkerId: %s", this.egg.type, this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
111
+ this.emit(message.action, message.data);
112
+ } else if (message?.type === "Buffer") debug("[%s:%s] got an invalid message: %s", this.egg.type, this.pid, Buffer.from(message.data));
113
+ else debug("[%s:%s] got an invalid message %j", this.egg.type, this.pid, message);
114
+ }
115
+ close() {
116
+ process.removeListener("message", this.onMessage);
117
+ this.removeAllListeners();
118
+ }
119
+ };
120
+
121
+ //#endregion
122
+ export { Messenger };
@@ -0,0 +1,65 @@
1
+ import { IMessenger } from "./IMessenger.js";
2
+ import { EggApplicationCore } from "../../egg.js";
3
+ import { BaseMessenger } from "./base.js";
4
+
5
+ //#region src/lib/core/messenger/local.d.ts
6
+
7
+ /**
8
+ * Communication between app worker and agent worker with EventEmitter
9
+ */
10
+ declare class Messenger extends BaseMessenger implements IMessenger {
11
+ readonly pid: string;
12
+ constructor(egg: EggApplicationCore);
13
+ /**
14
+ * Send message to all agent and app
15
+ * @param {String} action - message key
16
+ * @param {Object} data - message value
17
+ * @return {Messenger} this
18
+ */
19
+ broadcast(action: string, data?: unknown): Messenger;
20
+ /**
21
+ * send message to the specified process
22
+ * Notice: in single process mode, it only can send to self process,
23
+ * and it will send to both agent and app's messengers.
24
+ * @param {String} workerId - the workerId of the receiver
25
+ * @param {String} action - message key
26
+ * @param {Object} data - message value
27
+ * @return {Messenger} this
28
+ */
29
+ sendTo(workerId: string, action: string, data?: unknown): Messenger;
30
+ /**
31
+ * send message to one worker by random
32
+ * Notice: in single process mode, we only start one agent worker and one app worker
33
+ * - if it's running in agent, it will send to one of app workers
34
+ * - if it's running in app, it will send to agent
35
+ * @param {String} action - message key
36
+ * @param {Object} data - message value
37
+ * @return {Messenger} this
38
+ */
39
+ sendRandom(action: string, data?: unknown): Messenger;
40
+ /**
41
+ * send message to app
42
+ * @param {String} action - message key
43
+ * @param {Object} data - message value
44
+ * @return {Messenger} this
45
+ */
46
+ sendToApp(action: string, data?: unknown): Messenger;
47
+ /**
48
+ * send message to agent
49
+ * @param {String} action - message key
50
+ * @param {Object} data - message value
51
+ * @return {Messenger} this
52
+ */
53
+ sendToAgent(action: string, data?: unknown): Messenger;
54
+ /**
55
+ * @param {String} action - message key
56
+ * @param {Object} data - message value
57
+ * @param {String} to - let master know how to send message
58
+ * @return {Messenger} this
59
+ */
60
+ send(action: string, data: unknown, to?: string): Messenger;
61
+ onMessage(message: any): void;
62
+ close(): void;
63
+ }
64
+ //#endregion
65
+ export { Messenger };
@@ -0,0 +1,126 @@
1
+ import { BaseMessenger } from "./base.js";
2
+ import { debuglog } from "node:util";
3
+
4
+ //#region src/lib/core/messenger/local.ts
5
+ const debug = debuglog("egg/lib/core/messenger/local");
6
+ /**
7
+ * Communication between app worker and agent worker with EventEmitter
8
+ */
9
+ var Messenger = class extends BaseMessenger {
10
+ pid;
11
+ constructor(egg) {
12
+ super(egg);
13
+ this.pid = String(process.pid);
14
+ }
15
+ /**
16
+ * Send message to all agent and app
17
+ * @param {String} action - message key
18
+ * @param {Object} data - message value
19
+ * @return {Messenger} this
20
+ */
21
+ broadcast(action, data) {
22
+ debug("[%s:%s] broadcast %s with %j", this.egg.type, this.pid, action, data);
23
+ this.send(action, data, "both");
24
+ return this;
25
+ }
26
+ /**
27
+ * send message to the specified process
28
+ * Notice: in single process mode, it only can send to self process,
29
+ * and it will send to both agent and app's messengers.
30
+ * @param {String} workerId - the workerId of the receiver
31
+ * @param {String} action - message key
32
+ * @param {Object} data - message value
33
+ * @return {Messenger} this
34
+ */
35
+ sendTo(workerId, action, data) {
36
+ debug("[%s:%s] send %s with %j to %s", this.egg.type, this.pid, action, data, workerId);
37
+ if (String(workerId) !== this.pid) return this;
38
+ this.send(action, data, "both");
39
+ return this;
40
+ }
41
+ /**
42
+ * send message to one worker by random
43
+ * Notice: in single process mode, we only start one agent worker and one app worker
44
+ * - if it's running in agent, it will send to one of app workers
45
+ * - if it's running in app, it will send to agent
46
+ * @param {String} action - message key
47
+ * @param {Object} data - message value
48
+ * @return {Messenger} this
49
+ */
50
+ sendRandom(action, data) {
51
+ debug("[%s:%s] send %s with %j to opposite", this.egg.type, this.pid, action, data);
52
+ this.send(action, data, "opposite");
53
+ return this;
54
+ }
55
+ /**
56
+ * send message to app
57
+ * @param {String} action - message key
58
+ * @param {Object} data - message value
59
+ * @return {Messenger} this
60
+ */
61
+ sendToApp(action, data) {
62
+ debug("[%s:%s] send %s with %j to all app", this.egg.type, this.pid, action, data);
63
+ this.send(action, data, "application");
64
+ return this;
65
+ }
66
+ /**
67
+ * send message to agent
68
+ * @param {String} action - message key
69
+ * @param {Object} data - message value
70
+ * @return {Messenger} this
71
+ */
72
+ sendToAgent(action, data) {
73
+ debug("[%s:%s] send %s with %j to all agent", this.egg.type, this.pid, action, data);
74
+ this.send(action, data, "agent");
75
+ return this;
76
+ }
77
+ /**
78
+ * @param {String} action - message key
79
+ * @param {Object} data - message value
80
+ * @param {String} to - let master know how to send message
81
+ * @return {Messenger} this
82
+ */
83
+ send(action, data, to) {
84
+ process.nextTick(() => {
85
+ const { egg } = this;
86
+ let application;
87
+ let agent;
88
+ let opposite;
89
+ if (egg.type === "application") {
90
+ application = egg;
91
+ agent = egg.agent;
92
+ opposite = agent;
93
+ } else {
94
+ agent = egg;
95
+ application = egg.application;
96
+ opposite = application;
97
+ }
98
+ if (!to) to = egg.type === "application" ? "agent" : "application";
99
+ if (application && application.messenger && (to === "application" || to === "both")) application.messenger.onMessage({
100
+ action,
101
+ data
102
+ });
103
+ if (agent && agent.messenger && (to === "agent" || to === "both")) agent.messenger.onMessage({
104
+ action,
105
+ data
106
+ });
107
+ if (opposite && opposite.messenger && to === "opposite") opposite.messenger.onMessage({
108
+ action,
109
+ data
110
+ });
111
+ });
112
+ return this;
113
+ }
114
+ onMessage(message) {
115
+ if (typeof message?.action === "string") {
116
+ debug("[%s:%s] got message %s with %j", this.egg.type, this.pid, message.action, message.data);
117
+ this.emit(message.action, message.data);
118
+ } else debug("[%s:%s] got an invalid message %j", this.egg.type, this.pid, message);
119
+ }
120
+ close() {
121
+ this.removeAllListeners();
122
+ }
123
+ };
124
+
125
+ //#endregion
126
+ export { Messenger };
@@ -0,0 +1,5 @@
1
+ //#region src/lib/core/utils.d.ts
2
+ declare function convertObject(obj: any, ignore?: string | RegExp | (string | RegExp)[]): any;
3
+ declare function safeParseURL(url: string): URL | null;
4
+ //#endregion
5
+ export { convertObject, safeParseURL };
@@ -0,0 +1,48 @@
1
+ import util from "node:util";
2
+ import { isAsyncFunction, isClass, isFunction, isGeneratorFunction, isPrimitive, isRegExp, isSymbol } from "is-type-of";
3
+
4
+ //#region src/lib/core/utils.ts
5
+ function convertObject(obj, ignore = []) {
6
+ if (!Array.isArray(ignore)) ignore = [ignore];
7
+ for (const key of Object.keys(obj)) obj[key] = convertValue(key, obj[key], ignore);
8
+ return obj;
9
+ }
10
+ function convertValue(key, value, ignore) {
11
+ if (value === null || value === void 0) return value;
12
+ let hit = false;
13
+ for (const matchKey of ignore) if (typeof matchKey === "string" && matchKey === key) {
14
+ hit = true;
15
+ break;
16
+ } else if (isRegExp(matchKey) && matchKey.test(key)) {
17
+ hit = true;
18
+ break;
19
+ }
20
+ if (!hit) {
21
+ if (isSymbol(value) || isRegExp(value) || value instanceof URL) return value.toString();
22
+ if (isPrimitive(value) || Array.isArray(value)) return value;
23
+ }
24
+ if (Object.getPrototypeOf(value) === Object.prototype) return convertObject(value, ignore);
25
+ const name = value.name || "anonymous";
26
+ if (isClass(value)) return `<Class ${name}>`;
27
+ if (isFunction(value)) {
28
+ if (isGeneratorFunction(value)) return `<GeneratorFunction ${name}>`;
29
+ if (isAsyncFunction(value)) return `<AsyncFunction ${name}>`;
30
+ return `<Function ${name}>`;
31
+ }
32
+ const typeName = value.constructor.name;
33
+ if (typeName) {
34
+ if (Buffer.isBuffer(value) || typeof value === "string") return `<${typeName} len: ${value.length}>`;
35
+ return `<${typeName}>`;
36
+ }
37
+ return util.format(value);
38
+ }
39
+ function safeParseURL(url) {
40
+ try {
41
+ return new URL(url);
42
+ } catch {
43
+ return null;
44
+ }
45
+ }
46
+
47
+ //#endregion
48
+ export { convertObject, safeParseURL };