alemonjs 2.1.83 → 2.1.85

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 (235) hide show
  1. package/lib/{app → application}/define-children.js +2 -1
  2. package/lib/{app → application/format}/message-api.d.ts +2 -2
  3. package/lib/{app → application/format}/message-api.js +12 -4
  4. package/lib/{app → application/format}/message-format-old.d.ts +1 -1
  5. package/lib/{app → application/format}/message-format.d.ts +1 -2
  6. package/lib/{app → application/format}/message-format.js +0 -2
  7. package/lib/{app/hook-use → application/hooks}/announce.js +4 -3
  8. package/lib/{app/hook-use → application/hooks}/channel.js +4 -3
  9. package/lib/{app/hook-use → application/hooks}/client.d.ts +1 -1
  10. package/lib/{app/hook-use → application/hooks}/client.js +4 -11
  11. package/lib/{app/hook-use → application/hooks}/common.d.ts +7 -7
  12. package/lib/{app/hook-use → application/hooks}/common.js +9 -8
  13. package/lib/{app/hook-use → application/hooks}/event.js +1 -1
  14. package/lib/{app/hook-use → application/hooks}/guild.js +4 -3
  15. package/lib/{app/hook-use → application/hooks}/history.js +4 -3
  16. package/lib/{app/hook-use → application/hooks}/me.js +12 -7
  17. package/lib/{app/hook-use → application/hooks}/media.d.ts +3 -3
  18. package/lib/{app/hook-use → application/hooks}/media.js +4 -3
  19. package/lib/{app/hook-use → application/hooks}/member.js +4 -3
  20. package/lib/{app/hook-use → application/hooks}/mention.js +4 -3
  21. package/lib/{app/hook-use → application/hooks}/message.js +6 -5
  22. package/lib/{app/hook-use → application/hooks}/permission.js +4 -3
  23. package/lib/{app/hook-use → application/hooks}/reaction.js +4 -3
  24. package/lib/{app/hook-use → application/hooks}/request.js +12 -7
  25. package/lib/{app/hook-use → application/hooks}/role.js +4 -3
  26. package/lib/{app/hook-use → application/hooks}/subscribe.js +4 -4
  27. package/lib/{app/hook-use → application/hooks}/user.js +12 -7
  28. package/lib/application/index.d.ts +29 -0
  29. package/lib/application/index.js +58 -0
  30. package/lib/{app → application}/router/dsl.d.ts +1 -1
  31. package/lib/{app → application}/router/dsl.js +14 -10
  32. package/lib/{cbp → application/runtime/cbp}/connects/client.d.ts +1 -1
  33. package/lib/{cbp → application/runtime/cbp}/connects/client.js +43 -50
  34. package/lib/application/runtime/cbp/index.d.ts +3 -0
  35. package/lib/application/runtime/cbp/index.js +3 -0
  36. package/lib/application/runtime/cbp/processor/actions.d.ts +3 -0
  37. package/lib/application/runtime/cbp/processor/actions.js +58 -0
  38. package/lib/application/runtime/cbp/processor/api.d.ts +3 -0
  39. package/lib/application/runtime/cbp/processor/api.js +58 -0
  40. package/lib/application/runtime/cbp/processor/request-registry.d.ts +8 -0
  41. package/lib/application/runtime/cbp/processor/request-registry.js +6 -0
  42. package/lib/application/runtime/client-runtime.d.ts +1 -0
  43. package/lib/application/runtime/client-runtime.js +95 -0
  44. package/lib/{app → application/runtime}/event-error.d.ts +1 -1
  45. package/lib/{app → application/runtime}/event-error.js +1 -1
  46. package/lib/{app → application/runtime}/event-group.d.ts +1 -1
  47. package/lib/{app → application/runtime}/event-middleware.d.ts +1 -1
  48. package/lib/{app → application/runtime}/event-middleware.js +1 -1
  49. package/lib/{app → application/runtime}/event-processor-callHandler.d.ts +1 -1
  50. package/lib/{app → application/runtime}/event-processor-callHandler.js +8 -1
  51. package/lib/{app → application/runtime}/event-processor-cycle.d.ts +1 -1
  52. package/lib/{app → application/runtime}/event-processor-cycleFiles.d.ts +1 -1
  53. package/lib/{app → application/runtime}/event-processor-cycleFiles.js +2 -2
  54. package/lib/{app → application/runtime}/event-processor-cycleRoute.d.ts +1 -1
  55. package/lib/{app → application/runtime}/event-processor-cycleRoute.js +9 -2
  56. package/lib/{app → application/runtime}/event-processor-event.d.ts +1 -1
  57. package/lib/{app → application/runtime}/event-processor-middleware.d.ts +1 -1
  58. package/lib/{app → application/runtime}/event-processor-subscribe.d.ts +1 -1
  59. package/lib/{app → application/runtime}/event-processor-subscribe.js +9 -2
  60. package/lib/{app → application/runtime}/event-processor.d.ts +1 -1
  61. package/lib/{app → application/runtime}/event-processor.js +4 -3
  62. package/lib/{app → application/runtime}/event-response.d.ts +1 -1
  63. package/lib/{app → application/runtime}/event-utils.d.ts +1 -1
  64. package/lib/{app → application/runtime}/event-utils.js +2 -2
  65. package/lib/{app → application/runtime}/hook-event-context.d.ts +2 -2
  66. package/lib/{app → application/runtime}/hook-event-context.js +2 -2
  67. package/lib/application/runtime/http/index.d.ts +4 -0
  68. package/lib/application/runtime/http/index.js +4 -0
  69. package/lib/application/runtime/http/routers/hello.html.d.ts +4 -0
  70. package/lib/application/runtime/http/routers/hello.html.js +363 -0
  71. package/lib/{server → application/runtime/http}/routers/router.js +265 -179
  72. package/lib/{server → application/runtime/http}/routers/utils.d.ts +1 -1
  73. package/lib/{server → application/runtime/http}/routers/utils.js +22 -8
  74. package/lib/{server/main.js → application/runtime/http-server.js} +2 -6
  75. package/lib/{app → application/runtime}/lifecycle-callbacks.d.ts +1 -1
  76. package/lib/{app → application/runtime}/lifecycle-callbacks.js +2 -7
  77. package/lib/{app/load_modules → application/runtime/load-modules}/load.js +2 -2
  78. package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.js +14 -5
  79. package/lib/{app → application/runtime}/schedule-store.d.ts +1 -1
  80. package/lib/{app → application/runtime}/schedule-store.js +1 -1
  81. package/lib/{app → application/runtime}/store.d.ts +14 -19
  82. package/lib/{app → application/runtime}/store.js +8 -103
  83. package/lib/application/schedule.d.ts +17 -0
  84. package/lib/{app/api → application}/schedule.js +1 -1
  85. package/lib/client.d.ts +1 -1
  86. package/lib/client.js +1 -117
  87. package/lib/common/cbp/constants.d.ts +11 -0
  88. package/lib/common/cbp/constants.js +13 -0
  89. package/lib/{cbp/connects/connect.js → common/cbp/heartbeat.js} +3 -6
  90. package/lib/common/cbp/normalize.d.ts +16 -0
  91. package/lib/common/cbp/normalize.js +324 -0
  92. package/lib/common/cbp/runtime.d.ts +2 -0
  93. package/lib/common/cbp/runtime.js +10 -0
  94. package/lib/common/cbp/typings.d.ts +159 -0
  95. package/lib/{cbp/connects/base.js → common/cbp/ws-connector.js} +7 -9
  96. package/lib/{core → common}/config.d.ts +2 -2
  97. package/lib/{core → common}/config.js +5 -4
  98. package/lib/common/identity.d.ts +11 -0
  99. package/lib/common/identity.js +34 -0
  100. package/lib/common/index.d.ts +15 -0
  101. package/lib/common/index.js +14 -0
  102. package/lib/common/logger.d.ts +6 -0
  103. package/lib/common/logger.js +99 -0
  104. package/lib/{core → common}/react.js +22 -3
  105. package/lib/common/result.d.ts +7 -0
  106. package/lib/common/result.js +19 -0
  107. package/lib/{core → common}/utils.d.ts +3 -18
  108. package/lib/{core → common}/utils.js +6 -49
  109. package/lib/{core → common}/variable.js +1 -1
  110. package/lib/core/cbp/index.d.ts +1 -0
  111. package/lib/core/cbp/index.js +1 -0
  112. package/lib/core/cbp/processor/config.d.ts +8 -0
  113. package/lib/core/cbp/processor/config.js +29 -0
  114. package/lib/{cbp → core/cbp}/routers/hello.html.js +1 -1
  115. package/lib/{cbp → core/cbp}/server/main.js +57 -35
  116. package/lib/{cbp → core/cbp}/server/testone.d.ts +1 -1
  117. package/lib/{cbp → core/cbp}/server/testone.js +8 -2
  118. package/lib/core/index.d.ts +1 -3
  119. package/lib/core/index.js +1 -3
  120. package/lib/core/process/index.d.ts +3 -0
  121. package/lib/{process → core/process}/index.js +0 -1
  122. package/lib/{process → core/process}/ipc-bridge.js +16 -2
  123. package/lib/{process → core/process}/module.js +13 -4
  124. package/lib/{process → core/process}/platform.js +12 -3
  125. package/lib/core/start.d.ts +2 -0
  126. package/lib/core/start.js +76 -0
  127. package/lib/global.d.ts +4 -3
  128. package/lib/index.d.ts +10 -2
  129. package/lib/index.js +74 -54
  130. package/lib/main.d.ts +1 -2
  131. package/lib/main.js +1 -76
  132. package/lib/{cbp/connects/platform.d.ts → platform/cbp-platform.d.ts} +2 -2
  133. package/lib/{cbp/connects/platform.js → platform/cbp-platform.js} +52 -75
  134. package/lib/{app → platform}/define-platform.js +2 -0
  135. package/lib/platform/event-value.d.ts +7 -0
  136. package/lib/platform/event-value.js +5 -0
  137. package/lib/platform/index.d.ts +5 -0
  138. package/lib/platform/index.js +5 -0
  139. package/lib/types/actions.d.ts +20 -1
  140. package/lib/types/apis.d.ts +2 -1
  141. package/lib/types/client/index.d.ts +1 -1
  142. package/lib/types/cycle/index.d.ts +1 -1
  143. package/lib/types/event/index.d.ts +1 -1
  144. package/lib/types/subscribe/index.d.ts +1 -1
  145. package/package.json +22 -1
  146. package/lib/app/api/schedule.d.ts +0 -17
  147. package/lib/app/index.d.ts +0 -25
  148. package/lib/app/index.js +0 -47
  149. package/lib/cbp/index.d.ts +0 -3
  150. package/lib/cbp/index.js +0 -3
  151. package/lib/cbp/processor/actions.d.ts +0 -3
  152. package/lib/cbp/processor/actions.js +0 -48
  153. package/lib/cbp/processor/api.d.ts +0 -3
  154. package/lib/cbp/processor/api.js +0 -48
  155. package/lib/cbp/processor/config.d.ts +0 -29
  156. package/lib/cbp/processor/config.js +0 -52
  157. package/lib/cbp/typings.d.ts +0 -20
  158. package/lib/process/index.d.ts +0 -4
  159. package/lib/server/routers/hello.html.d.ts +0 -2
  160. package/lib/server/routers/hello.html.js +0 -31
  161. /package/lib/{app → application}/define-children.d.ts +0 -0
  162. /package/lib/{app → application}/define-middleware.d.ts +0 -0
  163. /package/lib/{app → application}/define-middleware.js +0 -0
  164. /package/lib/{app → application}/define-response.d.ts +0 -0
  165. /package/lib/{app → application}/define-response.js +0 -0
  166. /package/lib/{app → application}/define-router.d.ts +0 -0
  167. /package/lib/{app → application}/define-router.js +0 -0
  168. /package/lib/{app → application}/expose.d.ts +0 -0
  169. /package/lib/{app → application}/expose.js +0 -0
  170. /package/lib/{app → application/format}/message-format-old.js +0 -0
  171. /package/lib/{app/hook-use → application/hooks}/announce.d.ts +0 -0
  172. /package/lib/{app/hook-use → application/hooks}/channel.d.ts +0 -0
  173. /package/lib/{app/hook-use → application/hooks}/event.d.ts +0 -0
  174. /package/lib/{app/hook-use → application/hooks}/guild.d.ts +0 -0
  175. /package/lib/{app/hook-use → application/hooks}/history.d.ts +0 -0
  176. /package/lib/{app/hook-use → application/hooks}/index.d.ts +0 -0
  177. /package/lib/{app/hook-use → application/hooks}/index.js +0 -0
  178. /package/lib/{app/hook-use → application/hooks}/me.d.ts +0 -0
  179. /package/lib/{app/hook-use → application/hooks}/member.d.ts +0 -0
  180. /package/lib/{app/hook-use → application/hooks}/mention.d.ts +0 -0
  181. /package/lib/{app/hook-use → application/hooks}/message.d.ts +0 -0
  182. /package/lib/{app/hook-use → application/hooks}/permission.d.ts +0 -0
  183. /package/lib/{app/hook-use → application/hooks}/reaction.d.ts +0 -0
  184. /package/lib/{app/hook-use → application/hooks}/request.d.ts +0 -0
  185. /package/lib/{app/hook-use → application/hooks}/role.d.ts +0 -0
  186. /package/lib/{app/hook-use → application/hooks}/route.d.ts +0 -0
  187. /package/lib/{app/hook-use → application/hooks}/route.js +0 -0
  188. /package/lib/{app/hook-use → application/hooks}/subscribe.d.ts +0 -0
  189. /package/lib/{app/hook-use → application/hooks}/user.d.ts +0 -0
  190. /package/lib/{app → application}/router/fallback.d.ts +0 -0
  191. /package/lib/{app → application}/router/fallback.js +0 -0
  192. /package/lib/{app → application}/router/main.d.ts +0 -0
  193. /package/lib/{app → application}/router/main.js +0 -0
  194. /package/lib/{app → application}/router/parser.d.ts +0 -0
  195. /package/lib/{app → application}/router/parser.js +0 -0
  196. /package/lib/{app → application}/router/types.d.ts +0 -0
  197. /package/lib/{app → application}/router/types.js +0 -0
  198. /package/lib/{app → application}/router/validator.d.ts +0 -0
  199. /package/lib/{app → application}/router/validator.js +0 -0
  200. /package/lib/{cbp → application/runtime/cbp}/processor/transport.d.ts +0 -0
  201. /package/lib/{cbp → application/runtime/cbp}/processor/transport.js +0 -0
  202. /package/lib/{app → application/runtime}/event-group.js +0 -0
  203. /package/lib/{app → application/runtime}/event-processor-cycle.js +0 -0
  204. /package/lib/{app → application/runtime}/event-processor-event.js +0 -0
  205. /package/lib/{app → application/runtime}/event-processor-middleware.js +0 -0
  206. /package/lib/{app → application/runtime}/event-response.js +0 -0
  207. /package/lib/{server → application/runtime/http}/routers/middleware.d.ts +0 -0
  208. /package/lib/{server → application/runtime/http}/routers/middleware.js +0 -0
  209. /package/lib/{cbp → application/runtime/http}/routers/router.d.ts +0 -0
  210. /package/lib/{server/main.d.ts → application/runtime/http-server.d.ts} +0 -0
  211. /package/lib/{app/load_modules → application/runtime/load-modules}/index.d.ts +0 -0
  212. /package/lib/{app/load_modules → application/runtime/load-modules}/index.js +0 -0
  213. /package/lib/{app/load_modules → application/runtime/load-modules}/load.d.ts +0 -0
  214. /package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.d.ts +0 -0
  215. /package/lib/{app/config.d.ts → application/runtime/subscribe-status.d.ts} +0 -0
  216. /package/lib/{app/config.js → application/runtime/subscribe-status.js} +0 -0
  217. /package/lib/{app → common}/SinglyLinkedList.d.ts +0 -0
  218. /package/lib/{app → common}/SinglyLinkedList.js +0 -0
  219. /package/lib/{cbp/connects/connect.d.ts → common/cbp/heartbeat.d.ts} +0 -0
  220. /package/lib/{cbp → common/cbp}/typings.js +0 -0
  221. /package/lib/{cbp/connects/base.d.ts → common/cbp/ws-connector.d.ts} +0 -0
  222. /package/lib/{process → common}/direct-channel.d.ts +0 -0
  223. /package/lib/{process → common}/direct-channel.js +0 -0
  224. /package/lib/{core → common}/react.d.ts +0 -0
  225. /package/lib/{core → common}/variable.d.ts +0 -0
  226. /package/lib/{cbp → core/cbp}/routers/hello.html.d.ts +0 -0
  227. /package/lib/{server → core/cbp}/routers/router.d.ts +0 -0
  228. /package/lib/{cbp → core/cbp}/routers/router.js +0 -0
  229. /package/lib/{cbp → core/cbp}/server/main.d.ts +0 -0
  230. /package/lib/{process → core/process}/ipc-bridge.d.ts +0 -0
  231. /package/lib/{process → core/process}/module.d.ts +0 -0
  232. /package/lib/{process → core/process}/platform.d.ts +0 -0
  233. /package/lib/{app → platform}/define-platform.d.ts +0 -0
  234. /package/lib/{app → platform}/event-format.d.ts +0 -0
  235. /package/lib/{app → platform}/event-format.js +0 -0
@@ -2,15 +2,22 @@ import KoaRouter from 'koa-router';
2
2
  import fs__default, { existsSync } from 'fs';
3
3
  import path__default, { join, dirname } from 'path';
4
4
  import mime from 'mime-types';
5
- import hello from './hello.html.js';
5
+ import { renderHelloHtml } from './hello.html.js';
6
6
  import { safePath, getModuelFile, formatPath, isValidPackageName } from './utils.js';
7
7
  import { collectMiddlewares, runMiddlewares } from './middleware.js';
8
8
  import module$1 from 'module';
9
- import { ResultCode } from '../../core/variable.js';
10
9
  import 'yaml';
11
- import '../../core/utils.js';
12
- import { listRuntimeApps, getRuntimeApp, toRuntimeAppSnapshot, listRuntimeAppKoaRouters, hasRuntimeAppCapability, getRuntimeAppKoaRouters } from '../../app/store.js';
13
- import { dispatchHttpError } from '../../app/lifecycle-callbacks.js';
10
+ import '../../../../common/logger.js';
11
+ import { ResultCode } from '../../../../common/variable.js';
12
+ import '../../../../common/utils.js';
13
+ import 'net';
14
+ import 'v8';
15
+ import 'os';
16
+ import 'flatted';
17
+ import '../../../../common/cbp/runtime.js';
18
+ import 'ws';
19
+ import { listRuntimeApps, getRuntimeApp, toRuntimeAppSnapshot, listRuntimeAppKoaRouters, hasRuntimeAppCapability, getChildrenApp, getRuntimeAppKoaRouters } from '../../store.js';
20
+ import { dispatchHttpError } from '../../lifecycle-callbacks.js';
14
21
 
15
22
  const initRequire = () => { };
16
23
  initRequire.resolve = () => '';
@@ -31,14 +38,61 @@ const resolvePackageRoot = (startDir) => {
31
38
  const readWebRootConfig = (packageRoot) => {
32
39
  const packageJsonPath = path__default.join(packageRoot, 'package.json');
33
40
  if (!existsSync(packageJsonPath)) {
41
+ if (existsSync(path__default.join(packageRoot, 'dist', 'index.html'))) {
42
+ return 'dist';
43
+ }
34
44
  return '';
35
45
  }
36
46
  const pkg = require$1(packageJsonPath) ?? {};
37
- return pkg?.alemonjs?.web?.root ?? '';
47
+ const configuredRoot = pkg?.alemonjs?.web?.root ?? '';
48
+ if (typeof configuredRoot === 'string' && configuredRoot.trim()) {
49
+ return configuredRoot;
50
+ }
51
+ if (existsSync(path__default.join(packageRoot, 'dist', 'index.html'))) {
52
+ return 'dist';
53
+ }
54
+ return '';
55
+ };
56
+ const matchBasePath = (requestPath, basePath) => {
57
+ if (!basePath) {
58
+ return requestPath;
59
+ }
60
+ if (requestPath === basePath) {
61
+ return '/';
62
+ }
63
+ if (requestPath.startsWith(`${basePath}/`)) {
64
+ return requestPath.slice(basePath.length) || '/';
65
+ }
66
+ return '';
67
+ };
68
+ const matchApiBasePath = (requestPath, basePath) => {
69
+ return requestPath === basePath || requestPath.startsWith(`${basePath}/`);
70
+ };
71
+ const needsTrailingSlashRedirect = (requestUrl, basePath) => {
72
+ return requestUrl === basePath || requestUrl.startsWith(`${basePath}?`);
73
+ };
74
+ const getOriginalPathname = (ctx) => {
75
+ const rawUrl = typeof ctx.originalUrl === 'string' && ctx.originalUrl ? ctx.originalUrl : ctx.url;
76
+ const [pathname] = String(rawUrl).split('?');
77
+ return pathname || '/';
78
+ };
79
+ const rewriteCtxPath = async (ctx, nextPath, handler) => {
80
+ const search = ctx.querystring ? `?${ctx.querystring}` : '';
81
+ const originalUrl = ctx.url;
82
+ const originalReqUrl = ctx.req.url;
83
+ ctx.url = `${nextPath}${search}`;
84
+ ctx.req.url = `${nextPath}${search}`;
85
+ try {
86
+ await handler();
87
+ }
88
+ finally {
89
+ ctx.url = originalUrl;
90
+ ctx.req.url = originalReqUrl;
91
+ }
38
92
  };
39
93
  const denyRuntimeAppAccess = (ctx, appName, capability) => {
40
94
  const runtimeApp = getRuntimeApp(appName);
41
- if (!runtimeApp || !runtimeApp.enabled) {
95
+ if (!runtimeApp?.enabled) {
42
96
  ctx.status = 404;
43
97
  ctx.body = {
44
98
  code: 404,
@@ -96,30 +150,59 @@ const denyRuntimeAppAccess = (ctx, appName, capability) => {
96
150
  }
97
151
  return runtimeApp;
98
152
  };
99
- const dispatchRegisteredKoaRouters = async (ctx) => {
100
- const registeredRouters = listRuntimeAppKoaRouters();
101
- for (const item of registeredRouters) {
102
- const runtimeApp = getRuntimeApp(item.name);
103
- if (!runtimeApp || !runtimeApp.enabled || runtimeApp.status !== 'ready' || !hasRuntimeAppCapability(item.name, 'httpApi')) {
104
- continue;
105
- }
106
- const routers = getRuntimeAppKoaRouters(item.name);
107
- for (const koaRouter of routers) {
153
+ const getRuntimeAppRouters = (appName) => {
154
+ const registerRouters = getChildrenApp(appName)?.register?.koaRouter;
155
+ const storedRouters = getRuntimeAppKoaRouters(appName);
156
+ return (storedRouters.length ? storedRouters : Array.isArray(registerRouters) ? registerRouters : registerRouters ? [registerRouters] : []).filter(Boolean);
157
+ };
158
+ const dispatchAppKoaRouters = async (ctx, appName) => {
159
+ const runtimeApp = getRuntimeApp(appName);
160
+ if (!runtimeApp || !runtimeApp.enabled || runtimeApp.status !== 'ready' || !hasRuntimeAppCapability(appName, 'httpApi')) {
161
+ return false;
162
+ }
163
+ const routers = getRuntimeAppRouters(appName);
164
+ const aliasBases = appName === 'main' ? ['/app'] : [`/apps/${appName}`];
165
+ for (const koaRouter of routers) {
166
+ for (const basePath of aliasBases) {
167
+ const rewrittenPath = matchBasePath(ctx.path, basePath);
168
+ if (!rewrittenPath) {
169
+ continue;
170
+ }
108
171
  try {
109
- const beforeMatched = Array.isArray(ctx.matched) ? ctx.matched.length : 0;
110
- await koaRouter.routes()(ctx, async () => { });
111
- const afterMatched = Array.isArray(ctx.matched) ? ctx.matched.length : 0;
112
- if (afterMatched <= beforeMatched) {
172
+ const matchedContext = ctx;
173
+ const beforeMatched = Array.isArray(matchedContext.matched) ? matchedContext.matched.length : 0;
174
+ const beforeStatus = ctx.status;
175
+ const beforeBody = ctx.body;
176
+ const beforeMatchedRoute = ctx._matchedRoute;
177
+ const beforeRouterPath = ctx.routerPath;
178
+ let fallthrough = false;
179
+ await rewriteCtxPath(ctx, rewrittenPath, async () => {
180
+ await koaRouter.routes()(ctx, async () => {
181
+ fallthrough = true;
182
+ });
183
+ });
184
+ const afterMatched = Array.isArray(matchedContext.matched) ? matchedContext.matched.length : 0;
185
+ const afterMatchedRoute = ctx._matchedRoute;
186
+ const afterRouterPath = ctx.routerPath;
187
+ const handled = afterMatched > beforeMatched
188
+ || afterMatchedRoute !== beforeMatchedRoute
189
+ || afterRouterPath !== beforeRouterPath
190
+ || ctx.status !== beforeStatus
191
+ || ctx.body !== beforeBody
192
+ || !fallthrough;
193
+ if (!handled) {
113
194
  continue;
114
195
  }
115
- await koaRouter.allowedMethods()(ctx, async () => { });
196
+ await rewriteCtxPath(ctx, rewrittenPath, async () => {
197
+ await koaRouter.allowedMethods()(ctx, async () => { });
198
+ });
116
199
  return true;
117
200
  }
118
201
  catch (error) {
119
202
  const handled = await dispatchHttpError({
120
203
  ctx,
121
204
  error,
122
- appName: item.name,
205
+ appName,
123
206
  path: ctx.path,
124
207
  method: ctx.method,
125
208
  kind: 'koa-router'
@@ -144,10 +227,112 @@ const dispatchRegisteredKoaRouters = async (ctx) => {
144
227
  }
145
228
  return false;
146
229
  };
230
+ const isNamespacedHtmlRequest = (ctx) => {
231
+ if (ctx.method !== 'GET') {
232
+ return false;
233
+ }
234
+ if (ctx.path === '/app' || ctx.path.startsWith('/app/')) {
235
+ return !matchApiBasePath(ctx.path, '/app/api');
236
+ }
237
+ if (!ctx.path.startsWith('/apps/')) {
238
+ return false;
239
+ }
240
+ const [, , appName, segment = ''] = ctx.path.split('/');
241
+ if (!appName) {
242
+ return false;
243
+ }
244
+ return !matchApiBasePath(ctx.path, `/apps/${appName}/api`);
245
+ };
246
+ const dispatchRegisteredKoaRouters = async (ctx) => {
247
+ const registeredRouters = listRuntimeAppKoaRouters();
248
+ const candidates = new Set(registeredRouters.map(item => item.name));
249
+ const runtimeApps = listRuntimeApps();
250
+ runtimeApps.forEach(item => {
251
+ if (item.capabilities?.httpApi) {
252
+ candidates.add(item.name);
253
+ }
254
+ });
255
+ for (const appName of candidates) {
256
+ const handled = await dispatchAppKoaRouters(ctx, appName);
257
+ if (handled) {
258
+ return true;
259
+ }
260
+ }
261
+ return false;
262
+ };
263
+ const serveStaticResource = async (ctx, appName, rootDir, resourcePath) => {
264
+ const packageRoot = resolvePackageRoot(rootDir);
265
+ let root = '';
266
+ try {
267
+ root = readWebRootConfig(packageRoot);
268
+ }
269
+ catch (err) {
270
+ const handled = await dispatchHttpError({
271
+ ctx,
272
+ error: err,
273
+ appName,
274
+ path: ctx.path,
275
+ method: ctx.method,
276
+ kind: 'web'
277
+ });
278
+ if (handled) {
279
+ return 'handled';
280
+ }
281
+ ctx.status = 500;
282
+ ctx.body = {
283
+ code: 500,
284
+ message: '加载 package.json 时发生错误。',
285
+ error: err instanceof Error ? err.message : String(err)
286
+ };
287
+ return 'handled';
288
+ }
289
+ const webRoot = root ? path__default.join(packageRoot, root) : packageRoot;
290
+ const fullPath = safePath(webRoot, resourcePath);
291
+ if (!fullPath) {
292
+ ctx.status = 403;
293
+ ctx.body = {
294
+ code: 403,
295
+ message: '非法路径',
296
+ data: null
297
+ };
298
+ return 'handled';
299
+ }
300
+ if (!existsSync(fullPath)) {
301
+ return 'miss';
302
+ }
303
+ try {
304
+ const file = await fs__default.promises.readFile(fullPath);
305
+ const mimeType = mime.lookup(fullPath) || 'application/octet-stream';
306
+ ctx.set('Content-Type', mimeType);
307
+ ctx.body = file;
308
+ ctx.status = 200;
309
+ return 'handled';
310
+ }
311
+ catch (err) {
312
+ const handled = await dispatchHttpError({
313
+ ctx,
314
+ error: err,
315
+ appName,
316
+ path: ctx.path,
317
+ method: ctx.method,
318
+ kind: 'web'
319
+ });
320
+ if (handled) {
321
+ return 'handled';
322
+ }
323
+ ctx.status = 500;
324
+ ctx.body = {
325
+ code: 500,
326
+ message: appName === 'main' ? '加载资源时发生服务器错误。' : `加载子应用 '${appName}' 资源时发生服务器错误。`,
327
+ error: err instanceof Error ? err.message : String(err)
328
+ };
329
+ return 'handled';
330
+ }
331
+ };
147
332
  router.get('/', ctx => {
148
333
  ctx.status = 200;
149
334
  ctx.set('Content-Type', 'text/html; charset=utf-8');
150
- ctx.body = hello;
335
+ ctx.body = renderHelloHtml(listRuntimeApps());
151
336
  });
152
337
  router.get('api/online', ctx => {
153
338
  ctx.status = 200;
@@ -192,13 +377,21 @@ router.get('api/runtime/apps/:app', ctx => {
192
377
  };
193
378
  });
194
379
  router.use(async (ctx, next) => {
380
+ if (isNamespacedHtmlRequest(ctx)) {
381
+ await next();
382
+ return;
383
+ }
195
384
  const handled = await dispatchRegisteredKoaRouters(ctx);
196
385
  if (handled) {
197
386
  return;
198
387
  }
199
388
  await next();
200
389
  });
201
- router.all('app/{*path}', async (ctx) => {
390
+ const handleMainAppRequest = async (ctx) => {
391
+ if (needsTrailingSlashRedirect(getOriginalPathname(ctx), '/app')) {
392
+ ctx.redirect('/app/');
393
+ return;
394
+ }
202
395
  if (!process.env.input) {
203
396
  ctx.status = 400;
204
397
  ctx.body = {
@@ -210,7 +403,7 @@ router.all('app/{*path}', async (ctx) => {
210
403
  }
211
404
  const rootPath = process.cwd();
212
405
  const apiPath = '/app/api';
213
- if (ctx.path.startsWith(apiPath)) {
406
+ if (matchApiBasePath(ctx.path, apiPath)) {
214
407
  const runtimeApp = denyRuntimeAppAccess(ctx, 'main', 'httpApi');
215
408
  if (!runtimeApp) {
216
409
  return;
@@ -291,88 +484,36 @@ router.all('app/{*path}', async (ctx) => {
291
484
  ctx.status = 405;
292
485
  return;
293
486
  }
294
- let root = '';
487
+ const runtimeApp = getRuntimeApp('main');
295
488
  const resourcePath = formatPath(ctx.params?.path);
296
- const runtimeApp = denyRuntimeAppAccess(ctx, 'main', 'web');
297
- if (!runtimeApp) {
489
+ if (!runtimeApp?.enabled || runtimeApp.status !== 'ready') {
490
+ denyRuntimeAppAccess(ctx, 'main', 'web');
298
491
  return;
299
492
  }
300
- const packageRoot = resolvePackageRoot(runtimeApp.rootDir);
301
- try {
302
- root = readWebRootConfig(packageRoot);
303
- }
304
- catch (err) {
305
- const handled = await dispatchHttpError({
306
- ctx,
307
- error: err,
308
- appName: 'main',
309
- path: ctx.path,
310
- method: ctx.method,
311
- kind: 'web'
312
- });
313
- if (handled) {
493
+ if (hasRuntimeAppCapability('main', 'web')) {
494
+ const staticResult = await serveStaticResource(ctx, 'main', runtimeApp.rootDir, resourcePath);
495
+ if (staticResult === 'handled') {
314
496
  return;
315
497
  }
316
- ctx.status = 500;
317
- ctx.body = {
318
- code: 500,
319
- message: '加载 package.json 时发生错误。',
320
- error: err instanceof Error ? err.message : String(err)
321
- };
322
- return;
323
498
  }
324
- const webRoot = root ? path__default.join(packageRoot, root) : packageRoot;
325
- const fullPath = safePath(webRoot, resourcePath);
326
- if (!fullPath) {
327
- ctx.status = 403;
328
- ctx.body = {
329
- code: 403,
330
- message: '非法路径',
331
- data: null
332
- };
499
+ if (await dispatchAppKoaRouters(ctx, 'main')) {
333
500
  return;
334
501
  }
335
- try {
336
- const file = await fs__default.promises.readFile(fullPath);
337
- const mimeType = mime.lookup(fullPath) || 'application/octet-stream';
338
- ctx.set('Content-Type', mimeType);
339
- ctx.body = file;
340
- ctx.status = 200;
341
- }
342
- catch (err) {
343
- const handled = await dispatchHttpError({
344
- ctx,
345
- error: err,
346
- appName: 'main',
347
- path: ctx.path,
348
- method: ctx.method,
349
- kind: 'web'
350
- });
351
- if (handled) {
352
- return;
353
- }
354
- if (err?.status === 404) {
355
- ctx.status = 404;
356
- ctx.body = {
357
- code: 404,
358
- message: '资源中未找到。',
359
- data: null
360
- };
361
- }
362
- else {
363
- ctx.status = 500;
364
- ctx.body = {
365
- code: 500,
366
- message: '加载资源时发生服务器错误。',
367
- error: err instanceof Error ? err.message : String(err)
368
- };
369
- }
502
+ if (!hasRuntimeAppCapability('main', 'web')) {
503
+ denyRuntimeAppAccess(ctx, 'main', 'web');
504
+ return;
370
505
  }
371
- });
372
- router.all('app', ctx => {
373
- ctx.redirect('/app/');
374
- });
375
- router.all('apps/:app/{*path}', async (ctx) => {
506
+ ctx.status = 404;
507
+ ctx.body = {
508
+ code: 404,
509
+ message: '资源中未找到。',
510
+ data: null
511
+ };
512
+ };
513
+ router.all('app', handleMainAppRequest);
514
+ router.all('app/', handleMainAppRequest);
515
+ router.all('app/{*path}', handleMainAppRequest);
516
+ const handlePluginAppRequest = async (ctx) => {
376
517
  const appName = ctx.params.app;
377
518
  if (!isValidPackageName(appName)) {
378
519
  ctx.status = 400;
@@ -383,8 +524,12 @@ router.all('apps/:app/{*path}', async (ctx) => {
383
524
  };
384
525
  return;
385
526
  }
527
+ if (needsTrailingSlashRedirect(getOriginalPathname(ctx), `/apps/${appName}`)) {
528
+ ctx.redirect(`/apps/${appName}/`);
529
+ return;
530
+ }
386
531
  const apiPath = `/apps/${appName}/api`;
387
- if (ctx.path.startsWith(apiPath)) {
532
+ if (matchApiBasePath(ctx.path, apiPath)) {
388
533
  const runtimeApp = denyRuntimeAppAccess(ctx, appName, 'httpApi');
389
534
  if (!runtimeApp) {
390
535
  return;
@@ -459,93 +604,34 @@ router.all('apps/:app/{*path}', async (ctx) => {
459
604
  ctx.status = 405;
460
605
  return;
461
606
  }
462
- const runtimeApp = denyRuntimeAppAccess(ctx, appName, 'web');
463
- if (!runtimeApp) {
464
- return;
465
- }
466
- const packageRoot = resolvePackageRoot(runtimeApp.rootDir);
607
+ const runtimeApp = getRuntimeApp(appName);
467
608
  const resourcePath = formatPath(ctx.params?.path);
468
- let root = '';
469
- try {
470
- root = readWebRootConfig(packageRoot);
609
+ if (!runtimeApp?.enabled || runtimeApp.status !== 'ready') {
610
+ denyRuntimeAppAccess(ctx, appName, 'web');
611
+ return;
471
612
  }
472
- catch (err) {
473
- const handled = await dispatchHttpError({
474
- ctx,
475
- error: err,
476
- appName,
477
- path: ctx.path,
478
- method: ctx.method,
479
- kind: 'web'
480
- });
481
- if (handled) {
613
+ if (hasRuntimeAppCapability(appName, 'web')) {
614
+ const staticResult = await serveStaticResource(ctx, appName, runtimeApp.rootDir, resourcePath);
615
+ if (staticResult === 'handled') {
482
616
  return;
483
617
  }
484
- ctx.status = 500;
485
- ctx.body = {
486
- code: 500,
487
- message: '加载 package.json 时发生错误。',
488
- error: err instanceof Error ? err.message : String(err)
489
- };
490
- return;
491
618
  }
492
- const webRoot = root ? path__default.join(packageRoot, root) : packageRoot;
493
- const fullPath = safePath(webRoot, resourcePath);
494
- if (!fullPath) {
495
- ctx.status = 403;
496
- ctx.body = {
497
- code: 403,
498
- message: '非法路径',
499
- data: null
500
- };
619
+ if (await dispatchAppKoaRouters(ctx, appName)) {
501
620
  return;
502
621
  }
503
- try {
504
- const file = await fs__default.promises.readFile(fullPath);
505
- const mimeType = mime.lookup(fullPath) || 'application/octet-stream';
506
- ctx.set('Content-Type', mimeType);
507
- ctx.body = file;
508
- ctx.status = 200;
509
- }
510
- catch (err) {
511
- const handled = await dispatchHttpError({
512
- ctx,
513
- error: err,
514
- appName,
515
- path: ctx.path,
516
- method: ctx.method,
517
- kind: 'web'
518
- });
519
- if (handled) {
520
- return;
521
- }
522
- if (err?.status === 404) {
523
- ctx.status = 404;
524
- ctx.body = {
525
- code: 404,
526
- message: `资源 '${resourcePath}' 在子应用 '${appName}' 中未找到。`,
527
- data: null
528
- };
529
- }
530
- else {
531
- logger.warn({
532
- code: ResultCode.Fail,
533
- message: `Error request ${ctx.path}:`,
534
- data: err instanceof Error ? err.message : String(err)
535
- });
536
- ctx.status = 500;
537
- ctx.body = {
538
- code: 500,
539
- message: `加载子应用 '${appName}' 资源时发生服务器错误。`,
540
- error: err instanceof Error ? err.message : String(err)
541
- };
542
- }
543
- }
544
- });
545
- router.all('apps/:name', ctx => {
546
- if (ctx.path === `/apps/${ctx.params.name}`) {
547
- ctx.redirect(`/apps/${ctx.params.name}/`);
622
+ if (!hasRuntimeAppCapability(appName, 'web')) {
623
+ denyRuntimeAppAccess(ctx, appName, 'web');
624
+ return;
548
625
  }
549
- });
626
+ ctx.status = 404;
627
+ ctx.body = {
628
+ code: 404,
629
+ message: `资源 '${resourcePath}' 在子应用 '${appName}' 中未找到。`,
630
+ data: null
631
+ };
632
+ };
633
+ router.all('apps/:app', handlePluginAppRequest);
634
+ router.all('apps/:app/', handlePluginAppRequest);
635
+ router.all('apps/:app/{*path}', handlePluginAppRequest);
550
636
 
551
637
  export { router as default };
@@ -1,4 +1,4 @@
1
1
  export declare const safePath: (root: string, untrusted: string) => string | null;
2
2
  export declare const isValidPackageName: (name: string) => boolean;
3
3
  export declare const getModuelFile: (dir: string) => string;
4
- export declare const formatPath: (path: string) => string;
4
+ export declare const formatPath: (pathValue: string) => string;
@@ -34,17 +34,31 @@ const getModuelFile = (dir) => {
34
34
  }
35
35
  return '';
36
36
  };
37
- const formatPath = (path) => {
38
- if (!path || path === '/') {
39
- return '/index.html';
37
+ const formatPath = (pathValue) => {
38
+ const normalizedPath = String(pathValue || '')
39
+ .replace(/\\/g, '/')
40
+ .replace(/^\/+/, '')
41
+ .replace(/\/{2,}/g, '/')
42
+ .trim();
43
+ if (!normalizedPath) {
44
+ return 'index.html';
45
+ }
46
+ if (normalizedPath === 'index' || normalizedPath === 'index.html') {
47
+ return 'index.html';
48
+ }
49
+ if (normalizedPath.endsWith('/')) {
50
+ return `${normalizedPath}index.html`;
51
+ }
52
+ const parts = normalizedPath.split('/');
53
+ const lastPath = parts[parts.length - 1];
54
+ if (lastPath === 'index' || lastPath === 'index.html') {
55
+ parts[parts.length - 1] = 'index.html';
56
+ return parts.join('/');
40
57
  }
41
- const pates = path.split('/');
42
- const lastPath = pates[pates.length - 1];
43
58
  if (lastPath.includes('.')) {
44
- return path;
59
+ return normalizedPath;
45
60
  }
46
- path += '.html';
47
- return path;
61
+ return `${normalizedPath}.html`;
48
62
  };
49
63
 
50
64
  export { formatPath, getModuelFile, isValidPackageName, safePath };
@@ -1,11 +1,7 @@
1
1
  import Koa from 'koa';
2
2
  import koaCors from '@koa/cors';
3
- import router from './routers/router.js';
4
- import { ResultCode } from '../core/variable.js';
5
- import 'fs';
6
- import 'path';
7
- import 'yaml';
8
- import '../core/utils.js';
3
+ import router from './http/routers/router.js';
4
+ import { ResultCode } from '../../common/variable.js';
9
5
 
10
6
  const createServer = (port, listeningListener) => {
11
7
  try {
@@ -1,4 +1,4 @@
1
- import type { EventFinishedContext, EventStartContext, HttpErrorContext, RuntimeStatusChangeContext } from '../types';
1
+ import type { EventFinishedContext, EventStartContext, HttpErrorContext, RuntimeStatusChangeContext } from '../../types/index.js';
2
2
  export declare const dispatchEventStart: (context: EventStartContext) => Promise<void>;
3
3
  export declare const dispatchEventFinished: (context: EventFinishedContext) => Promise<void>;
4
4
  export declare const dispatchHttpError: (context: HttpErrorContext) => Promise<boolean>;
@@ -1,4 +1,4 @@
1
- import { showErrorModule } from '../core/utils.js';
1
+ import { showErrorModule } from '../../common/utils.js';
2
2
 
3
3
  const swallowLifecycleError = (error) => {
4
4
  showErrorModule(error instanceof Error ? error : new Error(typeof error === 'string' ? error : 'Unknown lifecycle error'));
@@ -65,12 +65,7 @@ const dispatchAppReady = async (appName, store) => {
65
65
  if (!handler) {
66
66
  return;
67
67
  }
68
- try {
69
- await handler(store);
70
- }
71
- catch (error) {
72
- throw error;
73
- }
68
+ await handler(store);
74
69
  };
75
70
  const dispatchAppDispose = async (appName, error) => {
76
71
  const app = getChildrenApp(appName);
@@ -1,8 +1,8 @@
1
- import { getConfig } from '../../core/config.js';
1
+ import { getConfig } from '../../../common/config.js';
2
2
  import { loadChildren, loadChildrenFile } from './loadChild.js';
3
3
  import { join, dirname } from 'path';
4
4
  import { existsSync } from 'fs';
5
- import { ResultCode } from '../../core/variable.js';
5
+ import { ResultCode } from '../../../common/variable.js';
6
6
  import { registerRuntimeApp } from '../store.js';
7
7
 
8
8
  const loadApps = () => {