egg 4.0.10 → 4.1.0-beta.2

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