egg 3.29.0 → 4.0.0-beta.10

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 (232) hide show
  1. package/README.md +2 -1
  2. package/README.zh-CN.md +7 -5
  3. package/dist/commonjs/agent.d.ts +4 -0
  4. package/dist/commonjs/agent.js +10 -0
  5. package/dist/commonjs/app/extend/context.d.ts +154 -0
  6. package/dist/commonjs/app/extend/context.js +261 -0
  7. package/dist/commonjs/app/extend/context.types.d.ts +16 -0
  8. package/dist/commonjs/app/extend/context.types.js +3 -0
  9. package/dist/commonjs/app/extend/helper.d.ts +37 -0
  10. package/dist/commonjs/app/extend/helper.js +49 -0
  11. package/dist/commonjs/app/extend/request.d.ts +128 -0
  12. package/dist/commonjs/app/extend/request.js +270 -0
  13. package/dist/commonjs/app/extend/response.d.ts +25 -0
  14. package/dist/commonjs/app/extend/response.js +37 -0
  15. package/dist/commonjs/app/middleware/body_parser.d.ts +2 -0
  16. package/dist/commonjs/app/middleware/body_parser.js +8 -0
  17. package/dist/commonjs/app/middleware/meta.d.ts +10 -0
  18. package/dist/commonjs/app/middleware/meta.js +22 -0
  19. package/dist/commonjs/app/middleware/notfound.d.ts +7 -0
  20. package/dist/commonjs/app/middleware/notfound.js +31 -0
  21. package/dist/commonjs/app/middleware/override_method.d.ts +2 -0
  22. package/dist/commonjs/app/middleware/override_method.js +8 -0
  23. package/dist/commonjs/app/middleware/site_file.d.ts +9 -0
  24. package/dist/commonjs/app/middleware/site_file.js +58 -0
  25. package/dist/commonjs/config/config.default.d.ts +9 -0
  26. package/dist/commonjs/config/config.default.js +379 -0
  27. package/dist/commonjs/config/config.local.d.ts +8 -0
  28. package/dist/commonjs/config/config.local.js +12 -0
  29. package/dist/commonjs/config/config.unittest.d.ts +7 -0
  30. package/dist/commonjs/config/config.unittest.js +11 -0
  31. package/dist/commonjs/config/plugin.d.ts +122 -0
  32. package/dist/commonjs/config/plugin.js +125 -0
  33. package/dist/commonjs/index.d.ts +63 -0
  34. package/dist/commonjs/index.js +91 -0
  35. package/dist/commonjs/lib/agent.d.ts +19 -0
  36. package/dist/commonjs/lib/agent.js +58 -0
  37. package/dist/commonjs/lib/application.d.ts +59 -0
  38. package/dist/commonjs/lib/application.js +270 -0
  39. package/dist/commonjs/lib/core/base_context_class.d.ts +14 -0
  40. package/dist/commonjs/lib/core/base_context_class.js +22 -0
  41. package/dist/commonjs/lib/core/base_context_logger.d.ts +36 -0
  42. package/dist/commonjs/lib/core/base_context_logger.js +64 -0
  43. package/dist/commonjs/lib/core/base_hook_class.d.ts +11 -0
  44. package/dist/commonjs/lib/core/base_hook_class.js +30 -0
  45. package/dist/commonjs/lib/core/context_httpclient.d.ts +16 -0
  46. package/dist/commonjs/lib/core/context_httpclient.js +30 -0
  47. package/dist/commonjs/lib/core/httpclient.d.ts +13 -0
  48. package/dist/commonjs/lib/core/httpclient.js +37 -0
  49. package/dist/commonjs/lib/core/logger.d.ts +3 -0
  50. package/dist/commonjs/lib/core/logger.js +40 -0
  51. package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +50 -0
  52. package/dist/commonjs/lib/core/messenger/IMessenger.js +3 -0
  53. package/dist/commonjs/lib/core/messenger/index.d.ts +7 -0
  54. package/dist/commonjs/lib/core/messenger/index.js +14 -0
  55. package/dist/commonjs/lib/core/messenger/ipc.d.ts +59 -0
  56. package/dist/commonjs/lib/core/messenger/ipc.js +137 -0
  57. package/dist/commonjs/lib/core/messenger/local.d.ts +61 -0
  58. package/dist/commonjs/lib/core/messenger/local.js +137 -0
  59. package/dist/commonjs/lib/core/singleton.d.ts +23 -0
  60. package/dist/commonjs/lib/core/singleton.js +120 -0
  61. package/dist/commonjs/lib/core/utils.d.ts +2 -0
  62. package/dist/commonjs/lib/core/utils.js +77 -0
  63. package/dist/commonjs/lib/egg.d.ts +276 -0
  64. package/dist/commonjs/lib/egg.js +617 -0
  65. package/dist/commonjs/lib/egg.types.d.ts +6 -0
  66. package/dist/commonjs/lib/egg.types.js +3 -0
  67. package/dist/commonjs/lib/loader/AgentWorkerLoader.d.ts +12 -0
  68. package/dist/commonjs/lib/loader/AgentWorkerLoader.js +24 -0
  69. package/dist/commonjs/lib/loader/AppWorkerLoader.d.ts +17 -0
  70. package/dist/commonjs/lib/loader/AppWorkerLoader.js +43 -0
  71. package/dist/commonjs/lib/loader/EggApplicationLoader.d.ts +4 -0
  72. package/dist/commonjs/lib/loader/EggApplicationLoader.js +8 -0
  73. package/dist/commonjs/lib/loader/index.d.ts +3 -0
  74. package/dist/commonjs/lib/loader/index.js +22 -0
  75. package/dist/commonjs/lib/start.d.ts +15 -0
  76. package/dist/commonjs/lib/start.js +49 -0
  77. package/dist/commonjs/lib/type.d.ts +293 -0
  78. package/dist/commonjs/lib/type.js +3 -0
  79. package/dist/commonjs/lib/utils.d.ts +2 -0
  80. package/dist/commonjs/lib/utils.js +21 -0
  81. package/dist/commonjs/package.json +3 -0
  82. package/dist/esm/agent.d.ts +4 -0
  83. package/dist/esm/agent.js +7 -0
  84. package/dist/esm/app/extend/context.d.ts +154 -0
  85. package/dist/esm/app/extend/context.js +255 -0
  86. package/dist/esm/app/extend/context.types.d.ts +16 -0
  87. package/dist/esm/app/extend/context.types.js +2 -0
  88. package/dist/esm/app/extend/helper.d.ts +37 -0
  89. package/dist/esm/app/extend/helper.js +43 -0
  90. package/dist/esm/app/extend/request.d.ts +128 -0
  91. package/dist/esm/app/extend/request.js +264 -0
  92. package/dist/esm/app/extend/response.d.ts +25 -0
  93. package/dist/esm/app/extend/response.js +34 -0
  94. package/dist/esm/app/middleware/body_parser.d.ts +2 -0
  95. package/dist/esm/app/middleware/body_parser.js +3 -0
  96. package/dist/esm/app/middleware/meta.d.ts +10 -0
  97. package/dist/esm/app/middleware/meta.js +20 -0
  98. package/dist/esm/app/middleware/notfound.d.ts +7 -0
  99. package/dist/esm/app/middleware/notfound.js +29 -0
  100. package/dist/esm/app/middleware/override_method.d.ts +2 -0
  101. package/dist/esm/app/middleware/override_method.js +3 -0
  102. package/dist/esm/app/middleware/site_file.d.ts +9 -0
  103. package/dist/esm/app/middleware/site_file.js +53 -0
  104. package/dist/esm/config/config.default.d.ts +9 -0
  105. package/dist/esm/config/config.default.js +374 -0
  106. package/dist/esm/config/config.local.d.ts +8 -0
  107. package/dist/esm/config/config.local.js +10 -0
  108. package/dist/esm/config/config.unittest.d.ts +7 -0
  109. package/dist/esm/config/config.unittest.js +9 -0
  110. package/dist/esm/config/favicon.png +0 -0
  111. package/dist/esm/config/plugin.d.ts +122 -0
  112. package/dist/esm/config/plugin.js +123 -0
  113. package/dist/esm/index.d.ts +63 -0
  114. package/dist/esm/index.js +66 -0
  115. package/dist/esm/lib/agent.d.ts +19 -0
  116. package/dist/esm/lib/agent.js +54 -0
  117. package/dist/esm/lib/application.d.ts +59 -0
  118. package/dist/esm/lib/application.js +263 -0
  119. package/dist/esm/lib/core/base_context_class.d.ts +14 -0
  120. package/dist/esm/lib/core/base_context_class.js +18 -0
  121. package/dist/esm/lib/core/base_context_logger.d.ts +36 -0
  122. package/dist/esm/lib/core/base_context_logger.js +60 -0
  123. package/dist/esm/lib/core/base_hook_class.d.ts +11 -0
  124. package/dist/esm/lib/core/base_hook_class.js +23 -0
  125. package/dist/esm/lib/core/context_httpclient.d.ts +16 -0
  126. package/dist/esm/lib/core/context_httpclient.js +26 -0
  127. package/dist/esm/lib/core/httpclient.d.ts +13 -0
  128. package/dist/esm/lib/core/httpclient.js +33 -0
  129. package/dist/esm/lib/core/logger.d.ts +3 -0
  130. package/dist/esm/lib/core/logger.js +37 -0
  131. package/dist/esm/lib/core/messenger/IMessenger.d.ts +50 -0
  132. package/dist/esm/lib/core/messenger/IMessenger.js +2 -0
  133. package/dist/esm/lib/core/messenger/index.d.ts +7 -0
  134. package/dist/esm/lib/core/messenger/index.js +11 -0
  135. package/dist/esm/lib/core/messenger/ipc.d.ts +59 -0
  136. package/dist/esm/lib/core/messenger/ipc.js +130 -0
  137. package/dist/esm/lib/core/messenger/local.d.ts +61 -0
  138. package/dist/esm/lib/core/messenger/local.js +130 -0
  139. package/dist/esm/lib/core/singleton.d.ts +23 -0
  140. package/dist/esm/lib/core/singleton.js +113 -0
  141. package/dist/esm/lib/core/utils.d.ts +2 -0
  142. package/dist/esm/lib/core/utils.js +70 -0
  143. package/dist/esm/lib/egg.d.ts +276 -0
  144. package/dist/esm/lib/egg.js +574 -0
  145. package/dist/esm/lib/egg.types.d.ts +6 -0
  146. package/dist/esm/lib/egg.types.js +2 -0
  147. package/dist/esm/lib/loader/AgentWorkerLoader.d.ts +12 -0
  148. package/dist/esm/lib/loader/AgentWorkerLoader.js +20 -0
  149. package/dist/esm/lib/loader/AppWorkerLoader.d.ts +17 -0
  150. package/dist/esm/lib/loader/AppWorkerLoader.js +39 -0
  151. package/dist/esm/lib/loader/EggApplicationLoader.d.ts +4 -0
  152. package/dist/esm/lib/loader/EggApplicationLoader.js +4 -0
  153. package/dist/esm/lib/loader/index.d.ts +3 -0
  154. package/dist/esm/lib/loader/index.js +4 -0
  155. package/dist/esm/lib/start.d.ts +15 -0
  156. package/dist/esm/lib/start.js +43 -0
  157. package/dist/esm/lib/type.d.ts +293 -0
  158. package/dist/esm/lib/type.js +2 -0
  159. package/dist/esm/lib/utils.d.ts +2 -0
  160. package/dist/esm/lib/utils.js +14 -0
  161. package/dist/esm/package.json +3 -0
  162. package/dist/package.json +4 -0
  163. package/package.json +97 -79
  164. package/src/agent.ts +7 -0
  165. package/src/app/extend/context.ts +303 -0
  166. package/src/app/extend/context.types.ts +24 -0
  167. package/{app/extend/helper.js → src/app/extend/helper.ts} +14 -13
  168. package/{app/extend/request.js → src/app/extend/request.ts} +81 -79
  169. package/src/app/extend/response.ts +36 -0
  170. package/src/app/middleware/body_parser.ts +3 -0
  171. package/{app/middleware/meta.js → src/app/middleware/meta.ts} +11 -4
  172. package/{app/middleware/notfound.js → src/app/middleware/notfound.ts} +8 -3
  173. package/src/app/middleware/override_method.ts +3 -0
  174. package/src/app/middleware/site_file.ts +68 -0
  175. package/{config/config.default.js → src/config/config.default.ts} +25 -45
  176. package/src/config/config.local.ts +11 -0
  177. package/src/config/config.unittest.ts +10 -0
  178. package/src/config/favicon.png +0 -0
  179. package/{config/plugin.js → src/config/plugin.ts} +3 -5
  180. package/src/index.ts +80 -0
  181. package/src/lib/agent.ts +66 -0
  182. package/{lib/application.js → src/lib/application.ts} +76 -124
  183. package/src/lib/core/base_context_class.ts +21 -0
  184. package/src/lib/core/base_context_logger.ts +67 -0
  185. package/src/lib/core/base_hook_class.ts +30 -0
  186. package/src/lib/core/context_httpclient.ts +33 -0
  187. package/src/lib/core/httpclient.ts +51 -0
  188. package/src/lib/core/logger.ts +42 -0
  189. package/src/lib/core/messenger/IMessenger.ts +58 -0
  190. package/src/lib/core/messenger/index.ts +15 -0
  191. package/src/lib/core/messenger/ipc.ts +148 -0
  192. package/{lib/core/messenger/local.js → src/lib/core/messenger/local.ts} +36 -28
  193. package/{lib/core/singleton.js → src/lib/core/singleton.ts} +56 -33
  194. package/src/lib/core/utils.ts +77 -0
  195. package/{lib/egg.js → src/lib/egg.ts} +287 -221
  196. package/src/lib/egg.types.ts +6 -0
  197. package/src/lib/loader/AgentWorkerLoader.ts +21 -0
  198. package/src/lib/loader/AppWorkerLoader.ts +42 -0
  199. package/src/lib/loader/EggApplicationLoader.ts +5 -0
  200. package/src/lib/loader/index.ts +3 -0
  201. package/src/lib/start.ts +56 -0
  202. package/src/lib/type.ts +329 -0
  203. package/src/lib/utils.ts +16 -0
  204. package/CHANGELOG.md +0 -2395
  205. package/History.md +0 -52
  206. package/agent.js +0 -11
  207. package/app/extend/context.js +0 -285
  208. package/app/extend/response.js +0 -101
  209. package/app/middleware/body_parser.js +0 -3
  210. package/app/middleware/override_method.js +0 -3
  211. package/app/middleware/site_file.js +0 -31
  212. package/config/config.local.js +0 -7
  213. package/config/config.unittest.js +0 -8
  214. package/index.d.ts +0 -1288
  215. package/index.js +0 -68
  216. package/lib/agent.js +0 -95
  217. package/lib/core/base_context_class.js +0 -20
  218. package/lib/core/base_context_logger.js +0 -64
  219. package/lib/core/base_hook_class.js +0 -31
  220. package/lib/core/context_httpclient.js +0 -26
  221. package/lib/core/dnscache_httpclient.js +0 -93
  222. package/lib/core/httpclient.js +0 -119
  223. package/lib/core/httpclient_next.js +0 -80
  224. package/lib/core/logger.js +0 -35
  225. package/lib/core/messenger/index.js +0 -14
  226. package/lib/core/messenger/ipc.js +0 -141
  227. package/lib/core/utils.js +0 -73
  228. package/lib/loader/agent_worker_loader.js +0 -27
  229. package/lib/loader/app_worker_loader.js +0 -48
  230. package/lib/loader/index.js +0 -5
  231. package/lib/start.js +0 -39
  232. /package/{config → dist/commonjs/config}/favicon.png +0 -0
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "egg",
3
- "version": "3.29.0",
3
+ "version": "4.0.0-beta.10",
4
+ "engines": {
5
+ "node": ">= 18.19.0"
6
+ },
4
7
  "publishConfig": {
5
- "tag": "release-3.x",
6
- "access": "public"
8
+ "tag": "beta"
7
9
  },
8
- "description": "A web framework's framework for Node.js",
10
+ "description": "A web application framework for Node.js",
9
11
  "keywords": [
10
12
  "web",
11
13
  "app",
@@ -17,119 +19,135 @@
17
19
  "egg"
18
20
  ],
19
21
  "dependencies": {
20
- "@types/accepts": "^1.3.5",
21
- "@types/koa": "^2.13.5",
22
- "@types/koa-router": "^7.4.4",
23
- "accepts": "^1.3.8",
24
- "agentkeepalive": "^4.2.1",
25
- "cache-content-type": "^1.0.1",
22
+ "@eggjs/cluster": "^3.0.0",
23
+ "@eggjs/cookies": "^3.0.0",
24
+ "@eggjs/core": "^6.2.5",
25
+ "@eggjs/schedule": "^5.0.2",
26
+ "@eggjs/utils": "^4.1.5",
27
+ "@eggjs/watcher": "^4.0.1",
26
28
  "circular-json-for-egg": "^1.0.0",
27
- "cluster-client": "^3.3.0",
29
+ "cluster-client": "^3.7.0",
28
30
  "delegates": "^1.0.0",
29
- "egg-cluster": "^2.0.0",
30
- "egg-cookies": "^2.6.1",
31
- "egg-core": "^5.4.0",
32
31
  "egg-development": "^3.0.0",
33
32
  "egg-errors": "^2.3.1",
34
33
  "egg-i18n": "^2.1.1",
35
34
  "egg-jsonp": "^2.0.0",
36
- "egg-logger": "^3.0.1",
35
+ "egg-logger": "^3.6.0",
37
36
  "egg-logrotator": "^3.1.0",
38
37
  "egg-multipart": "^3.1.0",
39
38
  "egg-onerror": "^2.1.1",
40
- "egg-schedule": "^4.0.0",
41
39
  "egg-security": "^3.0.0",
42
40
  "egg-session": "^3.3.0",
43
41
  "egg-static": "^2.2.0",
44
42
  "egg-view": "^2.1.3",
45
- "egg-watcher": "^3.1.1",
46
- "extend2": "^1.0.1",
47
- "graceful": "^1.1.0",
48
- "humanize-ms": "^1.2.1",
43
+ "extend2": "^4.0.0",
44
+ "graceful": "^2.0.0",
45
+ "humanize-ms": "^2.0.0",
49
46
  "is-type-of": "^2.1.0",
50
47
  "koa-bodyparser": "^4.4.1",
51
- "koa-is-json": "^1.0.0",
52
- "koa-override": "^3.0.0",
53
- "ms": "^2.1.3",
54
- "on-finished": "^2.4.1",
48
+ "koa-override": "^4.0.0",
55
49
  "onelogger": "^1.0.0",
56
- "sendmessage": "^2.0.0",
57
- "urllib": "^2.33.0",
58
- "urllib-next": "npm:urllib@^3.27.1",
59
- "urllib4": "npm:urllib@^4.5.0",
50
+ "performance-ms": "^1.1.0",
51
+ "sendmessage": "^3.0.1",
52
+ "urllib": "^4.0.0",
60
53
  "utility": "^2.1.0",
61
54
  "ylru": "^1.3.2"
62
55
  },
63
56
  "devDependencies": {
64
- "@eggjs/tsconfig": "^1.1.0",
65
- "@types/node": "^20.1.2",
66
- "@umijs/preset-react": "^2.1.6",
67
- "address": "^1.2.1",
68
- "antd": "^4.23.2",
69
- "assert-file": "^1.0.0",
70
- "coffee": "^5.4.0",
71
- "cross-env": "^7.0.3",
72
- "dumi": "^1.1.47",
73
- "dumi-theme-egg": "^1.2.2",
74
- "egg-bin": "^6.4.1",
75
- "egg-mock": "^5.10.7",
57
+ "@arethetypeswrong/cli": "^0.17.1",
58
+ "@eggjs/bin": "^7.0.0",
59
+ "@eggjs/koa": "^2.19.1",
60
+ "@eggjs/mock": "beta",
61
+ "@eggjs/tsconfig": "1",
62
+ "@types/delegates": "^1.0.3",
63
+ "@types/koa-bodyparser": "^4.3.12",
64
+ "@types/mocha": "^10.0.7",
65
+ "@types/ms": "^0.7.34",
66
+ "@types/node": "22",
67
+ "@types/supertest": "^6.0.2",
68
+ "address": "2",
69
+ "assert-file": "1",
70
+ "coffee": "5",
71
+ "cross-env": "7",
76
72
  "egg-plugin-puml": "^2.4.0",
77
- "egg-tracer": "^2.0.0",
73
+ "egg-tracer": "^2.1.0",
78
74
  "egg-view-nunjucks": "^2.3.0",
79
- "eslint": "^8.23.1",
80
- "eslint-config-egg": "^12.0.0",
81
- "formstream": "^1.1.1",
82
- "https-pem": "^3.0.0",
83
- "jsdoc": "^3.6.11",
84
- "koa": "^2.13.4",
75
+ "eslint": "8",
76
+ "eslint-config-egg": "14",
77
+ "formstream": "^1.5.1",
85
78
  "koa-static": "^5.0.0",
86
- "node-libs-browser": "^2.2.1",
79
+ "mm": "^3.4.0",
87
80
  "pedding": "^1.1.0",
88
81
  "prettier": "^2.7.1",
89
- "react": "^16.14.0",
90
- "react-dom": "^16.14.0",
91
- "react-router": "^5.3.4",
82
+ "rimraf": "6",
83
+ "runscript": "^2.0.1",
92
84
  "sdk-base": "^4.2.1",
93
85
  "spy": "^1.0.0",
94
- "supertest": "^6.2.4",
95
- "ts-node": "^10.9.1",
96
- "tsd": "^0.28.1",
97
- "typescript": "^5.0.4",
98
- "umi": "^3.5.36"
86
+ "supertest": "^7.0.0",
87
+ "tshy": "^3.0.2",
88
+ "tshy-after": "1",
89
+ "typescript": "5"
99
90
  },
100
- "main": "index.js",
101
- "types": "index.d.ts",
102
- "files": [
103
- "index.js",
104
- "lib",
105
- "app",
106
- "config",
107
- "agent.js",
108
- "index.d.ts"
109
- ],
110
91
  "scripts": {
111
- "lint": "eslint app config lib test *.js",
112
- "tsd": "tsd",
113
- "test": "npm run lint -- --fix && npm run tsd && npm run test-local",
114
- "test-local": "egg-bin test --ts false",
115
- "test-local-changed": "egg-bin test --changed --ts false",
116
- "cov": "egg-bin cov --timeout 100000 --ts false",
117
- "ci": "npm run lint && npm run tsd && npm run cov",
92
+ "clean": "rimraf dist",
93
+ "lint": "eslint src test --ext .ts",
94
+ "pretest": "npm run clean && npm run lint -- --fix",
95
+ "test": "egg-bin test",
96
+ "test-local": "egg-bin test",
97
+ "test:changed": "egg-bin test --changed",
98
+ "preci": "npm run clean && npm run lint",
99
+ "ci": "egg-bin cov",
100
+ "postci": "npm run prepublishOnly && npm run clean",
101
+ "prepublishOnly": "tshy && tshy-after && attw --pack",
118
102
  "site:dev": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi dev",
119
103
  "site:devWithNode14-16": "cross-env APP_ROOT=./site dumi dev",
120
104
  "site:build": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi build",
121
105
  "site:buildWithNode14-16": "cross-env APP_ROOT=./site dumi build",
122
106
  "site:prettier": "prettier --config site/.prettierrc --ignore-path site/.prettierignore --write \"site/**/*.{js,jsx,tsx,ts,less,md,json}\"",
123
- "puml": "puml . --dest ./site",
124
- "commits": "./scripts/commits.sh"
107
+ "puml": "puml . --dest ./site"
125
108
  },
126
109
  "homepage": "https://github.com/eggjs/egg",
127
110
  "repository": {
128
111
  "type": "git",
129
- "url": "https://github.com/eggjs/egg.git"
112
+ "url": "git://github.com/eggjs/egg.git"
130
113
  },
131
- "engines": {
132
- "node": ">= 14.20.0"
114
+ "license": "MIT",
115
+ "tnpm": {
116
+ "mode": "npm"
117
+ },
118
+ "egg": {
119
+ "framework": true,
120
+ "exports": {
121
+ "import": "./dist/esm",
122
+ "require": "./dist/commonjs",
123
+ "typescript": "./src"
124
+ }
125
+ },
126
+ "files": [
127
+ "dist",
128
+ "src"
129
+ ],
130
+ "type": "module",
131
+ "tshy": {
132
+ "exports": {
133
+ ".": "./src/index.ts",
134
+ "./package.json": "./package.json"
135
+ }
136
+ },
137
+ "exports": {
138
+ ".": {
139
+ "import": {
140
+ "types": "./dist/esm/index.d.ts",
141
+ "default": "./dist/esm/index.js"
142
+ },
143
+ "require": {
144
+ "types": "./dist/commonjs/index.d.ts",
145
+ "default": "./dist/commonjs/index.js"
146
+ }
147
+ },
148
+ "./package.json": "./package.json"
133
149
  },
134
- "license": "MIT"
150
+ "types": "./dist/commonjs/index.d.ts",
151
+ "main": "./dist/commonjs/index.js",
152
+ "module": "./dist/esm/index.js"
135
153
  }
package/src/agent.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { BaseHookClass } from './lib/core/base_hook_class.js';
2
+
3
+ export default class EggAgentHook extends BaseHookClass {
4
+ configDidLoad() {
5
+ this.agent._wrapMessenger();
6
+ }
7
+ }
@@ -0,0 +1,303 @@
1
+ import delegate from 'delegates';
2
+ import { assign } from 'utility';
3
+ import { now, diff } from 'performance-ms';
4
+ import {
5
+ utils, Context as EggCoreContext, Router,
6
+ type ContextDelegation as EggCoreContextDelegation,
7
+ } from '@eggjs/core';
8
+ import type { Cookies as ContextCookies } from '@eggjs/cookies';
9
+ import { EggLogger } from 'egg-logger';
10
+ import type { Application } from '../../lib/application.js';
11
+ import type {
12
+ HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
13
+ } from '../../lib/core/httpclient.js';
14
+ import type { ContextHttpClient } from '../../lib/core/context_httpclient.js';
15
+ import type { BaseContextClass } from '../../lib//core/base_context_class.js';
16
+ import Request from './request.js';
17
+ import Response from './response.js';
18
+ import type Helper from './helper.js';
19
+
20
+ import './context.types.js';
21
+
22
+ const HELPER = Symbol('ctx helper');
23
+ const LOCALS = Symbol('ctx locals');
24
+ const LOCALS_LIST = Symbol('ctx localsList');
25
+ const COOKIES = Symbol('ctx cookies');
26
+ const CONTEXT_HTTPCLIENT = Symbol('ctx httpclient');
27
+ const CONTEXT_ROUTER = Symbol('ctx router');
28
+
29
+ interface Cookies extends ContextCookies {
30
+ request: any;
31
+ response: any;
32
+ }
33
+
34
+ export default class Context extends EggCoreContext {
35
+ declare app: Application;
36
+ declare request: Request;
37
+ declare service: BaseContextClass;
38
+
39
+ /**
40
+ * Request start time
41
+ * @member {Number} Context#starttime
42
+ */
43
+ starttime: number;
44
+ /**
45
+ * Request start timer using `performance.now()`
46
+ * @member {Number} Context#performanceStarttime
47
+ */
48
+ performanceStarttime: number;
49
+
50
+ /**
51
+ * Get the current visitor's cookies.
52
+ */
53
+ get cookies() {
54
+ let cookies = this[COOKIES];
55
+ if (!cookies) {
56
+ this[COOKIES] = cookies = new this.app.ContextCookies(this, this.app.keys, this.app.config.cookies);
57
+ }
58
+ return cookies as Cookies;
59
+ }
60
+
61
+ /**
62
+ * Get a wrapper httpclient instance contain ctx in the hold request process
63
+ *
64
+ * @return {ContextHttpClient} the wrapper httpclient instance
65
+ */
66
+ get httpclient(): ContextHttpClient {
67
+ if (!this[CONTEXT_HTTPCLIENT]) {
68
+ this[CONTEXT_HTTPCLIENT] = new this.app.ContextHttpClient(this as any);
69
+ }
70
+ return this[CONTEXT_HTTPCLIENT] as ContextHttpClient;
71
+ }
72
+
73
+ /**
74
+ * Alias to {@link Context#httpclient}
75
+ */
76
+ get httpClient(): ContextHttpClient {
77
+ return this.httpclient;
78
+ }
79
+
80
+ /**
81
+ * Shortcut for httpclient.curl
82
+ *
83
+ * @function Context#curl
84
+ * @param {String|Object} url - request url address.
85
+ * @param {Object} [options] - options for request.
86
+ * @return {Object} see {@link ContextHttpClient#curl}
87
+ */
88
+ async curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']> {
89
+ return await this.httpclient.curl(url, options);
90
+ }
91
+
92
+ /**
93
+ * Alias to {@link Application#router}
94
+ *
95
+ * @member {Router} Context#router
96
+ * @since 1.0.0
97
+ * @example
98
+ * ```js
99
+ * this.router.pathFor('post', { id: 12 });
100
+ * ```
101
+ */
102
+ get router(): Router {
103
+ if (this[CONTEXT_ROUTER]) {
104
+ return this[CONTEXT_ROUTER] as Router;
105
+ }
106
+ return this.app.router;
107
+ }
108
+
109
+ /**
110
+ * Set router to Context, only use on EggRouter
111
+ * @param {Router} val router instance
112
+ */
113
+ set router(val: Router) {
114
+ this[CONTEXT_ROUTER] = val;
115
+ }
116
+
117
+ /**
118
+ * Get helper instance from {@link Application#Helper}
119
+ *
120
+ * @member {Helper} Context#helper
121
+ * @since 1.0.0
122
+ */
123
+ get helper(): Helper {
124
+ if (!this[HELPER]) {
125
+ this[HELPER] = new this.app.Helper(this as any);
126
+ }
127
+ return this[HELPER] as Helper;
128
+ }
129
+
130
+ /**
131
+ * Wrap app.loggers with context information,
132
+ * if a custom logger is defined by naming aLogger, then you can `ctx.getLogger('aLogger')`
133
+ *
134
+ * @param {String} name - logger name
135
+ */
136
+ getLogger(name: string): EggLogger {
137
+ return this.app.getLogger(name);
138
+ }
139
+
140
+ /**
141
+ * Logger for Application
142
+ *
143
+ * @member {Logger} Context#logger
144
+ * @since 1.0.0
145
+ * @example
146
+ * ```js
147
+ * this.logger.info('some request data: %j', this.request.body);
148
+ * this.logger.warn('WARNING!!!!');
149
+ * ```
150
+ */
151
+ get logger(): EggLogger {
152
+ return this.getLogger('logger');
153
+ }
154
+
155
+ /**
156
+ * Logger for frameworks and plugins
157
+ *
158
+ * @member {Logger} Context#coreLogger
159
+ * @since 1.0.0
160
+ */
161
+ get coreLogger(): EggLogger {
162
+ return this.getLogger('coreLogger');
163
+ }
164
+
165
+ /**
166
+ * locals is an object for view, you can use `app.locals` and `ctx.locals` to set variables,
167
+ * which will be used as data when view is rendering.
168
+ * The difference between `app.locals` and `ctx.locals` is the context level, `app.locals` is global level, and `ctx.locals` is request level. when you get `ctx.locals`, it will merge `app.locals`.
169
+ *
170
+ * when you set locals, only object is available
171
+ *
172
+ * ```js
173
+ * this.locals = {
174
+ * a: 1
175
+ * };
176
+ * this.locals = {
177
+ * b: 1
178
+ * };
179
+ * this.locals.c = 1;
180
+ * console.log(this.locals);
181
+ * {
182
+ * a: 1,
183
+ * b: 1,
184
+ * c: 1,
185
+ * };
186
+ * ```
187
+ *
188
+ * `ctx.locals` has cache, it only merges `app.locals` once in one request.
189
+ *
190
+ * @member {Object} Context#locals
191
+ */
192
+ get locals() {
193
+ if (!this[LOCALS]) {
194
+ this[LOCALS] = assign({}, this.app.locals);
195
+ }
196
+ if (Array.isArray(this[LOCALS_LIST]) && this[LOCALS_LIST].length > 0) {
197
+ assign(this[LOCALS], this[LOCALS_LIST]);
198
+ this[LOCALS_LIST] = null;
199
+ }
200
+ return this[LOCALS] as Record<string, any>;
201
+ }
202
+
203
+ set locals(val) {
204
+ const localsList = this[LOCALS_LIST] as Record<string, any>[] ?? [];
205
+ localsList.push(val);
206
+ this[LOCALS_LIST] = localsList;
207
+ }
208
+
209
+ /**
210
+ * alias to {@link Context#locals}, compatible with koa that use this variable
211
+ * @member {Object} state
212
+ * @see Context#locals
213
+ */
214
+ get state() {
215
+ return this.locals;
216
+ }
217
+
218
+ set state(val) {
219
+ this.locals = val;
220
+ }
221
+
222
+ /**
223
+ * Run async function in the background
224
+ * @param {Function} scope - the first args is ctx
225
+ * ```js
226
+ * this.body = 'hi';
227
+ *
228
+ * this.runInBackground(async ctx => {
229
+ * await ctx.mysql.query(sql);
230
+ * await ctx.curl(url);
231
+ * });
232
+ * ```
233
+ */
234
+ runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, taskName?: string): void {
235
+ // try to use custom function name first
236
+ if (!taskName) {
237
+ taskName = Reflect.get(scope, '_name') || scope.name || utils.getCalleeFromStack(true);
238
+ }
239
+ // use setImmediate to ensure all sync logic will run async
240
+ setImmediate(() => {
241
+ this._runInBackground(scope, taskName!);
242
+ });
243
+ }
244
+
245
+ // let plugins or frameworks to reuse _runInBackground in some cases.
246
+ // e.g.: https://github.com/eggjs/egg-mock/pull/78
247
+ async _runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, taskName: string) {
248
+ const startTime = now();
249
+ try {
250
+ await scope(this as any);
251
+ this.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, diff(startTime));
252
+ } catch (err: any) {
253
+ // background task process log
254
+ this.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, diff(startTime));
255
+
256
+ // emit error when promise catch, and set err.runInBackground flag
257
+ err.runInBackground = true;
258
+ this.app.emit('error', err, this);
259
+ }
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Context delegation.
265
+ */
266
+
267
+ delegate(Context.prototype, 'request')
268
+ /**
269
+ * @member {Boolean} Context#acceptJSON
270
+ * @see Request#acceptJSON
271
+ * @since 1.0.0
272
+ */
273
+ .getter('acceptJSON')
274
+ /**
275
+ * @member {Array} Context#queries
276
+ * @see Request#queries
277
+ * @since 1.0.0
278
+ */
279
+ .getter('queries')
280
+ /**
281
+ * @member {Boolean} Context#accept
282
+ * @see Request#accept
283
+ * @since 1.0.0
284
+ */
285
+ .getter('accept')
286
+ /**
287
+ * @member {string} Context#ip
288
+ * @see Request#ip
289
+ * @since 1.0.0
290
+ */
291
+ .access('ip');
292
+
293
+ delegate(Context.prototype, 'response')
294
+ /**
295
+ * @member {Number} Context#realStatus
296
+ * @see Response#realStatus
297
+ * @since 1.0.0
298
+ */
299
+ .access('realStatus');
300
+
301
+ export type ContextDelegation = EggCoreContextDelegation & Context
302
+ & Pick<Request, 'acceptJSON' | 'queries' | 'accept' | 'ip'>
303
+ & Pick<Response, 'realStatus'>;
@@ -0,0 +1,24 @@
1
+ import type {
2
+ Router,
3
+ } from '@eggjs/core';
4
+ import type {
5
+ HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
6
+ } from '../../lib/core/httpclient.js';
7
+ import type {
8
+ ContextHttpClient,
9
+ } from '../../lib/core/context_httpclient.js';
10
+ import type Helper from './helper.js';
11
+ import type { EggLogger } from 'egg-logger';
12
+
13
+ declare module '@eggjs/core' {
14
+ // add Context overrides types
15
+ interface Context {
16
+ curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']>;
17
+ get router(): Router;
18
+ set router(val: Router);
19
+ get helper(): Helper;
20
+ get httpclient(): ContextHttpClient;
21
+ get httpClient(): ContextHttpClient;
22
+ getLogger(name: string): EggLogger;
23
+ }
24
+ }
@@ -1,10 +1,12 @@
1
- 'use strict';
2
-
3
- const url = require('url');
4
-
5
-
6
- module.exports = {
1
+ import url from 'node:url';
2
+ import { BaseContextClass } from '../../lib/core/base_context_class.js';
7
3
 
4
+ /**
5
+ * The Helper class which can be used as utility function.
6
+ * We support developers to extend Helper through ${baseDir}/app/extend/helper.js ,
7
+ * then you can use all method on `ctx.helper` that is a instance of Helper.
8
+ */
9
+ export default class Helper extends BaseContextClass {
8
10
  /**
9
11
  * Generate URL path(without host) for route. Takes the route name and a map of named params.
10
12
  * @function Helper#pathFor
@@ -19,9 +21,9 @@ module.exports = {
19
21
  * ```
20
22
  * @return {String} url path(without host)
21
23
  */
22
- pathFor(name, params) {
24
+ pathFor(name: string, params: Record<string, any>): string {
23
25
  return this.app.router.url(name, params);
24
- },
26
+ }
25
27
 
26
28
  /**
27
29
  * Generate full URL(with host) for route. Takes the route name and a map of named params.
@@ -36,8 +38,7 @@ module.exports = {
36
38
  * ```
37
39
  * @return {String} full url(with host)
38
40
  */
39
- urlFor(name, params) {
40
- return this.ctx.protocol + '://' + this.ctx.host + url.resolve('/', this.app.router.url(name, params));
41
- },
42
-
43
- };
41
+ urlFor(name: string, params: Record<string, any>): string {
42
+ return this.ctx.protocol + '://' + this.ctx.host + url.resolve('/', this.pathFor(name, params));
43
+ }
44
+ }