alemonjs 2.1.86 → 2.1.87
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.
- package/lib/application/define-router.js +1 -0
- package/lib/application/runtime/client-runtime.d.ts +9 -1
- package/lib/application/runtime/client-runtime.js +9 -0
- package/lib/application/runtime/http/routers/router.js +7 -7
- package/lib/application/runtime/load-modules/loadChild.js +67 -2
- package/lib/application/runtime/store.js +2 -2
- package/lib/global.d.ts +6 -1
- package/package.json +1 -1
|
@@ -1 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import '../define-children.js';
|
|
2
|
+
import '../define-response.js';
|
|
3
|
+
import '../define-middleware.js';
|
|
4
|
+
import '../define-router.js';
|
|
5
|
+
import '../format/message-api.js';
|
|
6
|
+
import './event-response.js';
|
|
7
|
+
import './event-middleware.js';
|
|
8
|
+
import './event-utils.js';
|
|
9
|
+
import './event-group.js';
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import '../define-children.js';
|
|
2
|
+
import '../define-response.js';
|
|
3
|
+
import '../define-middleware.js';
|
|
4
|
+
import '../define-router.js';
|
|
5
|
+
import '../format/message-api.js';
|
|
6
|
+
import './event-response.js';
|
|
7
|
+
import './event-middleware.js';
|
|
8
|
+
import './event-utils.js';
|
|
9
|
+
import './event-group.js';
|
|
1
10
|
import { cbpClient } from './cbp/connects/client.js';
|
|
2
11
|
import 'flatted';
|
|
3
12
|
import 'fs';
|
|
@@ -177,19 +177,19 @@ const dispatchAppKoaRouters = async (ctx, appName) => {
|
|
|
177
177
|
const beforeRouterPath = ctx.routerPath;
|
|
178
178
|
let fallthrough = false;
|
|
179
179
|
await rewriteCtxPath(ctx, rewrittenPath, async () => {
|
|
180
|
-
await koaRouter.routes()(ctx, () => {
|
|
180
|
+
await koaRouter.routes()(ctx, async () => {
|
|
181
181
|
fallthrough = true;
|
|
182
182
|
});
|
|
183
183
|
});
|
|
184
184
|
const afterMatched = Array.isArray(matchedContext.matched) ? matchedContext.matched.length : 0;
|
|
185
185
|
const afterMatchedRoute = ctx._matchedRoute;
|
|
186
186
|
const afterRouterPath = ctx.routerPath;
|
|
187
|
-
const handled = afterMatched > beforeMatched
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
187
|
+
const handled = afterMatched > beforeMatched ||
|
|
188
|
+
afterMatchedRoute !== beforeMatchedRoute ||
|
|
189
|
+
afterRouterPath !== beforeRouterPath ||
|
|
190
|
+
ctx.status !== beforeStatus ||
|
|
191
|
+
ctx.body !== beforeBody ||
|
|
192
|
+
!fallthrough;
|
|
193
193
|
if (!handled) {
|
|
194
194
|
continue;
|
|
195
195
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { dirname, join } from 'path';
|
|
2
|
-
import { existsSync } from 'fs';
|
|
2
|
+
import { existsSync, readFileSync } from 'fs';
|
|
3
3
|
import { showErrorModule, getRecursiveDirFiles, createEventName } from '../../../common/utils.js';
|
|
4
4
|
import { registerRuntimeApp, updateRuntimeAppStatus, ChildrenApp, clearRuntimeAppKoaRouters, setRuntimeAppKoaRouters, updateRuntimeAppCapabilities } from '../store.js';
|
|
5
5
|
import { registerExpose } from '../../expose.js';
|
|
@@ -11,6 +11,50 @@ import { dispatchRuntimeStatusChange, dispatchAppDispose, dispatchAppReady } fro
|
|
|
11
11
|
const initRequire = () => { };
|
|
12
12
|
initRequire.resolve = () => '';
|
|
13
13
|
const require$1 = module$1?.createRequire?.(import.meta.url) ?? initRequire;
|
|
14
|
+
const resolvePackageDir = (appName) => {
|
|
15
|
+
const resolveWithPaths = require$1.resolve;
|
|
16
|
+
const candidatePaths = resolveWithPaths?.paths?.(appName) ?? [];
|
|
17
|
+
for (const basePath of candidatePaths) {
|
|
18
|
+
const packageDir = join(basePath, appName);
|
|
19
|
+
if (existsSync(join(packageDir, 'package.json'))) {
|
|
20
|
+
return packageDir;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
const resolvePackageEntryFromPackageJson = (packageDir) => {
|
|
26
|
+
const packageJsonPath = join(packageDir, 'package.json');
|
|
27
|
+
if (!existsSync(packageJsonPath)) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf-8')) ?? {};
|
|
32
|
+
const exportsField = pkg?.exports;
|
|
33
|
+
let entry = null;
|
|
34
|
+
if (typeof exportsField === 'string') {
|
|
35
|
+
entry = exportsField;
|
|
36
|
+
}
|
|
37
|
+
else if (exportsField && typeof exportsField === 'object') {
|
|
38
|
+
const rootExport = exportsField['.'];
|
|
39
|
+
if (typeof rootExport === 'string') {
|
|
40
|
+
entry = rootExport;
|
|
41
|
+
}
|
|
42
|
+
else if (rootExport && typeof rootExport === 'object') {
|
|
43
|
+
entry = rootExport.import ?? rootExport.default ?? rootExport.require ?? null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (!entry) {
|
|
47
|
+
entry = pkg?.module ?? pkg?.main ?? 'index.js';
|
|
48
|
+
}
|
|
49
|
+
if (typeof entry !== 'string' || !entry.trim()) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
return join(packageDir, entry);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
14
58
|
const resolvePackageRoot = (startDir) => {
|
|
15
59
|
let currentDir = startDir;
|
|
16
60
|
while (currentDir && currentDir !== dirname(currentDir)) {
|
|
@@ -253,7 +297,14 @@ const loadChildrenFile = (appName) => {
|
|
|
253
297
|
return;
|
|
254
298
|
}
|
|
255
299
|
try {
|
|
256
|
-
|
|
300
|
+
let mainPath = require$1.resolve(appName);
|
|
301
|
+
if (!existsSync(mainPath)) {
|
|
302
|
+
const packageDir = resolvePackageDir(appName);
|
|
303
|
+
const fallbackMainPath = packageDir ? resolvePackageEntryFromPackageJson(packageDir) : null;
|
|
304
|
+
if (fallbackMainPath) {
|
|
305
|
+
mainPath = fallbackMainPath;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
257
308
|
if (!existsSync(mainPath)) {
|
|
258
309
|
updateRuntimeAppStatus(appName, 'failed', new Error('The main file does not exist,' + mainPath));
|
|
259
310
|
logger.error({
|
|
@@ -274,6 +325,20 @@ const loadChildrenFile = (appName) => {
|
|
|
274
325
|
void loadChildren(mainPath, appName);
|
|
275
326
|
}
|
|
276
327
|
catch (e) {
|
|
328
|
+
const packageDir = resolvePackageDir(appName);
|
|
329
|
+
const fallbackMainPath = packageDir ? resolvePackageEntryFromPackageJson(packageDir) : null;
|
|
330
|
+
if (fallbackMainPath && existsSync(fallbackMainPath)) {
|
|
331
|
+
registerRuntimeApp({
|
|
332
|
+
name: appName,
|
|
333
|
+
kind: 'plugin',
|
|
334
|
+
enabled: true,
|
|
335
|
+
status: 'discovered',
|
|
336
|
+
rootDir: dirname(fallbackMainPath),
|
|
337
|
+
mainPath: fallbackMainPath
|
|
338
|
+
});
|
|
339
|
+
void loadChildren(fallbackMainPath, appName);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
277
342
|
updateRuntimeAppStatus(appName, 'failed', e);
|
|
278
343
|
showErrorModule(e);
|
|
279
344
|
}
|
|
@@ -107,8 +107,8 @@ const registerRuntimeApp = (record) => {
|
|
|
107
107
|
createdAt: current?.createdAt ?? now,
|
|
108
108
|
updatedAt: now
|
|
109
109
|
};
|
|
110
|
-
if (current?.status !== record.status) {
|
|
111
|
-
logRuntimeAppStatus(
|
|
110
|
+
if (current?.status !== record.status && process.env.NODE_ENV === 'development') {
|
|
111
|
+
logRuntimeAppStatus('debug', runtimeApps[record.name]);
|
|
112
112
|
}
|
|
113
113
|
return runtimeApps[record.name];
|
|
114
114
|
};
|
package/lib/global.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DefineChildrenFunc, OnResponseReversalFunc, OnMiddlewareReversalFunc, OnSelectsFunc, OnDataFormatFunc, OnResponseReversalFuncBack, OnGroupFunc, OnMiddlewareReversalFuncBack, DefineResponseFunc, defineMiddlewareFunc, StoreChildrenApp, StateSubscribeMap, SubscribeKeysMap, LoggerUtils, ResponseState, StartOptions } from './types';
|
|
1
|
+
import type { DefineChildrenFunc, OnResponseReversalFunc, OnMiddlewareReversalFunc, OnSelectsFunc, OnDataFormatFunc, OnResponseReversalFuncBack, OnGroupFunc, OnMiddlewareReversalFuncBack, DefineResponseFunc, defineMiddlewareFunc, DefineRouterFunc, StoreChildrenApp, StateSubscribeMap, SubscribeKeysMap, LoggerUtils, ResponseState, StartOptions } from './types';
|
|
2
2
|
import { type Server } from 'ws';
|
|
3
3
|
import type WebSocket from 'ws';
|
|
4
4
|
import type { IncomingMessage } from 'http';
|
|
@@ -7,6 +7,9 @@ import type { RuntimeAppRecord } from './application/runtime/store.js';
|
|
|
7
7
|
declare global {
|
|
8
8
|
var __config: any;
|
|
9
9
|
var __options: StartOptions;
|
|
10
|
+
var __sandbox: boolean | undefined;
|
|
11
|
+
var __client_loaded: boolean | undefined;
|
|
12
|
+
var __publicIp: string | undefined;
|
|
10
13
|
var logger: LoggerUtils;
|
|
11
14
|
var alemonjsCore: {
|
|
12
15
|
storeState: ResponseState;
|
|
@@ -25,6 +28,7 @@ declare global {
|
|
|
25
28
|
var chatbotServer: Server<typeof WebSocket, typeof IncomingMessage>;
|
|
26
29
|
var chatbotPlatform: WebSocket;
|
|
27
30
|
var chatbotClient: WebSocket;
|
|
31
|
+
var testoneClient: WebSocket | undefined;
|
|
28
32
|
var onResponse: OnResponseReversalFunc;
|
|
29
33
|
var OnResponse: OnResponseReversalFuncBack;
|
|
30
34
|
var onMiddleware: OnMiddlewareReversalFunc;
|
|
@@ -32,6 +36,7 @@ declare global {
|
|
|
32
36
|
var defineChildren: DefineChildrenFunc;
|
|
33
37
|
var defineResponse: DefineResponseFunc;
|
|
34
38
|
var defineMiddleware: defineMiddlewareFunc;
|
|
39
|
+
var defineRouter: DefineRouterFunc;
|
|
35
40
|
var onSelects: OnSelectsFunc;
|
|
36
41
|
var format: OnDataFormatFunc;
|
|
37
42
|
var onGroup: OnGroupFunc;
|