@tego/core 1.3.44
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/LICENSE +201 -0
- package/lib/acl/available-action.d.ts +5 -0
- package/lib/acl/available-action.js +62 -0
- package/lib/acl/index.d.ts +2 -0
- package/lib/acl/index.js +49 -0
- package/lib/aes-encryptor.d.ts +11 -0
- package/lib/aes-encryptor.js +116 -0
- package/lib/app-command.d.ts +12 -0
- package/lib/app-command.js +76 -0
- package/lib/app-supervisor.d.ts +61 -0
- package/lib/app-supervisor.js +299 -0
- package/lib/application.d.ts +354 -0
- package/lib/application.js +878 -0
- package/lib/cache/index.d.ts +3 -0
- package/lib/cache/index.js +35 -0
- package/lib/commands/console.d.ts +3 -0
- package/lib/commands/console.js +48 -0
- package/lib/commands/create-migration.d.ts +3 -0
- package/lib/commands/create-migration.js +80 -0
- package/lib/commands/db-auth.d.ts +3 -0
- package/lib/commands/db-auth.js +28 -0
- package/lib/commands/db-clean.d.ts +3 -0
- package/lib/commands/db-clean.js +31 -0
- package/lib/commands/db-sync.d.ts +3 -0
- package/lib/commands/db-sync.js +40 -0
- package/lib/commands/destroy.d.ts +3 -0
- package/lib/commands/destroy.js +30 -0
- package/lib/commands/index.d.ts +2 -0
- package/lib/commands/index.js +71 -0
- package/lib/commands/install.d.ts +3 -0
- package/lib/commands/install.js +33 -0
- package/lib/commands/migrator.d.ts +3 -0
- package/lib/commands/migrator.js +31 -0
- package/lib/commands/pm.d.ts +3 -0
- package/lib/commands/pm.js +75 -0
- package/lib/commands/refresh.d.ts +3 -0
- package/lib/commands/refresh.js +31 -0
- package/lib/commands/restart.d.ts +3 -0
- package/lib/commands/restart.js +35 -0
- package/lib/commands/start.d.ts +3 -0
- package/lib/commands/start.js +74 -0
- package/lib/commands/stop.d.ts +3 -0
- package/lib/commands/stop.js +34 -0
- package/lib/commands/upgrade.d.ts +3 -0
- package/lib/commands/upgrade.js +29 -0
- package/lib/cron/cron-job-manager.d.ts +25 -0
- package/lib/cron/cron-job-manager.js +89 -0
- package/lib/environment.d.ts +10 -0
- package/lib/environment.js +73 -0
- package/lib/errors/application-not-install.d.ts +4 -0
- package/lib/errors/application-not-install.js +35 -0
- package/lib/errors/handler.d.ts +3 -0
- package/lib/errors/handler.js +41 -0
- package/lib/errors/plugin-command-error.d.ts +2 -0
- package/lib/errors/plugin-command-error.js +31 -0
- package/lib/errors/plugin-not-exist.d.ts +2 -0
- package/lib/errors/plugin-not-exist.js +31 -0
- package/lib/gateway/errors.d.ts +22 -0
- package/lib/gateway/errors.js +138 -0
- package/lib/gateway/gateway.d.ts +56 -0
- package/lib/gateway/gateway.js +408 -0
- package/lib/gateway/index.d.ts +2 -0
- package/lib/gateway/index.js +30 -0
- package/lib/gateway/ipc-socket-client.d.ts +17 -0
- package/lib/gateway/ipc-socket-client.js +99 -0
- package/lib/gateway/ipc-socket-server.d.ts +12 -0
- package/lib/gateway/ipc-socket-server.js +140 -0
- package/lib/gateway/types.d.ts +25 -0
- package/lib/gateway/types.js +15 -0
- package/lib/gateway/ws-server.d.ts +63 -0
- package/lib/gateway/ws-server.js +335 -0
- package/lib/helper.d.ts +11 -0
- package/lib/helper.js +193 -0
- package/lib/helpers/application-version.d.ts +10 -0
- package/lib/helpers/application-version.js +77 -0
- package/lib/helpers/multiple-instance-manager.d.ts +6 -0
- package/lib/helpers/multiple-instance-manager.js +40 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +57 -0
- package/lib/locale/index.d.ts +1 -0
- package/lib/locale/index.js +21 -0
- package/lib/locale/locale.d.ts +22 -0
- package/lib/locale/locale.js +166 -0
- package/lib/locale/resource.d.ts +1 -0
- package/lib/locale/resource.js +74 -0
- package/lib/main-data-source.d.ts +4 -0
- package/lib/main-data-source.js +45 -0
- package/lib/middlewares/data-template.d.ts +2 -0
- package/lib/middlewares/data-template.js +39 -0
- package/lib/middlewares/data-wrapping.d.ts +3 -0
- package/lib/middlewares/data-wrapping.js +90 -0
- package/lib/middlewares/db2resource.d.ts +6 -0
- package/lib/middlewares/db2resource.js +76 -0
- package/lib/middlewares/extract-client-ip.d.ts +2 -0
- package/lib/middlewares/extract-client-ip.js +37 -0
- package/lib/middlewares/i18n.d.ts +1 -0
- package/lib/middlewares/i18n.js +44 -0
- package/lib/middlewares/index.d.ts +3 -0
- package/lib/middlewares/index.js +32 -0
- package/lib/middlewares/parse-variables.d.ts +1 -0
- package/lib/middlewares/parse-variables.js +84 -0
- package/lib/migration.d.ts +12 -0
- package/lib/migration.js +47 -0
- package/lib/migrations/20230912193824-package-name-unique.d.ts +6 -0
- package/lib/migrations/20230912193824-package-name-unique.js +52 -0
- package/lib/migrations/20230912294620-update-pkg.d.ts +6 -0
- package/lib/migrations/20230912294620-update-pkg.js +58 -0
- package/lib/migrations/20240106082756-update-plugins.d.ts +6 -0
- package/lib/migrations/20240106082756-update-plugins.js +44 -0
- package/lib/migrations/20240705000001-remove-pkgs-approval.d.ts +6 -0
- package/lib/migrations/20240705000001-remove-pkgs-approval.js +47 -0
- package/lib/notice/index.d.ts +25 -0
- package/lib/notice/index.js +91 -0
- package/lib/plugin-manager/clientStaticUtils.d.ts +24 -0
- package/lib/plugin-manager/clientStaticUtils.js +156 -0
- package/lib/plugin-manager/constants.d.ts +7 -0
- package/lib/plugin-manager/constants.js +120 -0
- package/lib/plugin-manager/deps.d.ts +2 -0
- package/lib/plugin-manager/deps.js +68 -0
- package/lib/plugin-manager/index.d.ts +2 -0
- package/lib/plugin-manager/index.js +23 -0
- package/lib/plugin-manager/middleware.d.ts +2 -0
- package/lib/plugin-manager/middleware.js +36 -0
- package/lib/plugin-manager/options/collection.d.ts +2 -0
- package/lib/plugin-manager/options/collection.js +39 -0
- package/lib/plugin-manager/options/resource.d.ts +15 -0
- package/lib/plugin-manager/options/resource.js +185 -0
- package/lib/plugin-manager/plugin-manager-repository.d.ts +27 -0
- package/lib/plugin-manager/plugin-manager-repository.js +136 -0
- package/lib/plugin-manager/plugin-manager.d.ts +205 -0
- package/lib/plugin-manager/plugin-manager.js +1032 -0
- package/lib/plugin-manager/types.d.ts +15 -0
- package/lib/plugin-manager/types.js +15 -0
- package/lib/plugin-manager/utils.d.ts +101 -0
- package/lib/plugin-manager/utils.js +513 -0
- package/lib/plugin-manager/web-service.d.ts +6 -0
- package/lib/plugin-manager/web-service.js +116 -0
- package/lib/plugin.d.ts +125 -0
- package/lib/plugin.js +359 -0
- package/lib/pub-sub-manager/handler-manager.d.ts +28 -0
- package/lib/pub-sub-manager/handler-manager.js +149 -0
- package/lib/pub-sub-manager/index.d.ts +3 -0
- package/lib/pub-sub-manager/index.js +25 -0
- package/lib/pub-sub-manager/memory-pub-sub-adapter.d.ts +21 -0
- package/lib/pub-sub-manager/memory-pub-sub-adapter.js +82 -0
- package/lib/pub-sub-manager/pub-sub-manager.d.ts +19 -0
- package/lib/pub-sub-manager/pub-sub-manager.js +108 -0
- package/lib/pub-sub-manager/types.d.ts +20 -0
- package/lib/pub-sub-manager/types.js +15 -0
- package/lib/sync-message-manager.d.ts +16 -0
- package/lib/sync-message-manager.js +112 -0
- package/package.json +58 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var ipc_socket_server_exports = {};
|
|
30
|
+
__export(ipc_socket_server_exports, {
|
|
31
|
+
IPCSocketServer: () => IPCSocketServer
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(ipc_socket_server_exports);
|
|
34
|
+
var import_node_crypto = require("node:crypto");
|
|
35
|
+
var import_node_fs = __toESM(require("node:fs"));
|
|
36
|
+
var import_node_net = __toESM(require("node:net"));
|
|
37
|
+
var import_node_path = __toESM(require("node:path"));
|
|
38
|
+
var import_xpipe = __toESM(require("xpipe"));
|
|
39
|
+
var import_app_supervisor = require("../app-supervisor");
|
|
40
|
+
var import_ipc_socket_client = require("./ipc-socket-client");
|
|
41
|
+
const _IPCSocketServer = class _IPCSocketServer {
|
|
42
|
+
constructor(server) {
|
|
43
|
+
this.socketServer = server;
|
|
44
|
+
}
|
|
45
|
+
static buildServer(socketPath) {
|
|
46
|
+
if (import_node_fs.default.existsSync(socketPath)) {
|
|
47
|
+
import_node_fs.default.unlinkSync(socketPath);
|
|
48
|
+
}
|
|
49
|
+
const dir = import_node_path.default.dirname(socketPath);
|
|
50
|
+
if (!import_node_fs.default.existsSync(dir)) {
|
|
51
|
+
import_node_fs.default.mkdirSync(dir, { recursive: true });
|
|
52
|
+
}
|
|
53
|
+
const socketServer = import_node_net.default.createServer((c) => {
|
|
54
|
+
console.log("client connected");
|
|
55
|
+
c.on("end", () => {
|
|
56
|
+
console.log("client disconnected");
|
|
57
|
+
});
|
|
58
|
+
c.on("data", (data) => {
|
|
59
|
+
const dataAsString = data.toString();
|
|
60
|
+
const messages = dataAsString.split("\n");
|
|
61
|
+
for (const message of messages) {
|
|
62
|
+
if (message.length === 0) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const reqId = (0, import_node_crypto.randomUUID)();
|
|
66
|
+
const dataObj = JSON.parse(message);
|
|
67
|
+
_IPCSocketServer.handleClientMessage({ reqId, ...dataObj }).then((result) => {
|
|
68
|
+
(0, import_ipc_socket_client.writeJSON)(c, {
|
|
69
|
+
reqId,
|
|
70
|
+
type: result === false ? "not_found" : "success"
|
|
71
|
+
});
|
|
72
|
+
}).catch((err) => {
|
|
73
|
+
(0, import_ipc_socket_client.writeJSON)(c, {
|
|
74
|
+
reqId,
|
|
75
|
+
type: "error",
|
|
76
|
+
payload: {
|
|
77
|
+
message: err.message,
|
|
78
|
+
stack: err.stack
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
socketServer.listen(import_xpipe.default.eq(socketPath), () => {
|
|
86
|
+
console.log(`Gateway IPC Server running at ${socketPath}`);
|
|
87
|
+
});
|
|
88
|
+
return new _IPCSocketServer(socketServer);
|
|
89
|
+
}
|
|
90
|
+
static async handleClientMessage({ reqId, type, payload }) {
|
|
91
|
+
if (type === "appReady") {
|
|
92
|
+
const status = await new Promise((resolve, reject) => {
|
|
93
|
+
let status2;
|
|
94
|
+
const max = 300;
|
|
95
|
+
let count = 0;
|
|
96
|
+
const timer = setInterval(async () => {
|
|
97
|
+
status2 = import_app_supervisor.AppSupervisor.getInstance().getAppStatus("main");
|
|
98
|
+
if (status2 === "running") {
|
|
99
|
+
clearInterval(timer);
|
|
100
|
+
resolve(status2);
|
|
101
|
+
}
|
|
102
|
+
if (count++ > max) {
|
|
103
|
+
reject("error");
|
|
104
|
+
}
|
|
105
|
+
}, 500);
|
|
106
|
+
});
|
|
107
|
+
console.log("status", status);
|
|
108
|
+
return status;
|
|
109
|
+
}
|
|
110
|
+
if (type === "passCliArgv") {
|
|
111
|
+
const argv = payload.argv;
|
|
112
|
+
const mainApp = await import_app_supervisor.AppSupervisor.getInstance().getApp("main");
|
|
113
|
+
if (!mainApp.cli.hasCommand(argv[2])) {
|
|
114
|
+
await mainApp.pm.loadCommands();
|
|
115
|
+
}
|
|
116
|
+
const cli = mainApp.cli;
|
|
117
|
+
if (!cli.parseHandleByIPCServer(argv, {
|
|
118
|
+
from: "node"
|
|
119
|
+
})) {
|
|
120
|
+
mainApp.logger.debug("Not handle by ipc server");
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
return mainApp.runAsCLI(argv, {
|
|
124
|
+
reqId,
|
|
125
|
+
from: "node",
|
|
126
|
+
throwError: true
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
throw new Error(`Unknown message type ${type}`);
|
|
130
|
+
}
|
|
131
|
+
close() {
|
|
132
|
+
this.socketServer.close();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
__name(_IPCSocketServer, "IPCSocketServer");
|
|
136
|
+
let IPCSocketServer = _IPCSocketServer;
|
|
137
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
138
|
+
0 && (module.exports = {
|
|
139
|
+
IPCSocketServer
|
|
140
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import http, { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { ApplicationOptions } from '../application';
|
|
3
|
+
export interface Handler {
|
|
4
|
+
name: string;
|
|
5
|
+
prefix: string;
|
|
6
|
+
callback: (req: IncomingMessage, res: ServerResponse) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface AppSelectorMiddlewareContext {
|
|
9
|
+
req: IncomingRequest;
|
|
10
|
+
resolvedAppName: string | null;
|
|
11
|
+
}
|
|
12
|
+
export interface RunOptions {
|
|
13
|
+
mainAppOptions: ApplicationOptions;
|
|
14
|
+
}
|
|
15
|
+
export interface StartHttpServerOptions {
|
|
16
|
+
port: number;
|
|
17
|
+
host: string;
|
|
18
|
+
callback?: (server: http.Server) => void;
|
|
19
|
+
}
|
|
20
|
+
export type AppSelectorMiddleware = (ctx: AppSelectorMiddlewareContext, next: () => Promise<void>) => void;
|
|
21
|
+
export type AppSelector = (req: IncomingRequest) => string | Promise<string>;
|
|
22
|
+
export interface IncomingRequest {
|
|
23
|
+
url: string;
|
|
24
|
+
headers: any;
|
|
25
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { IncomingMessage } from 'node:http';
|
|
2
|
+
import { Logger } from '@tachybase/logger';
|
|
3
|
+
import WebSocket from 'ws';
|
|
4
|
+
declare class WebSocketWithId extends WebSocket {
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
interface WebSocketClient {
|
|
8
|
+
ws: WebSocketWithId;
|
|
9
|
+
tags: Set<string>;
|
|
10
|
+
url: string;
|
|
11
|
+
headers: any;
|
|
12
|
+
app?: string;
|
|
13
|
+
}
|
|
14
|
+
type WSEventType = 'close' | 'error' | 'message' | 'connection';
|
|
15
|
+
type WSEventHandler = (ws: WebSocketWithId, ...args: any[]) => Promise<void> | void;
|
|
16
|
+
export declare class WSServer {
|
|
17
|
+
wss: WebSocket.Server;
|
|
18
|
+
webSocketClients: Map<string, WebSocketClient>;
|
|
19
|
+
static KEY_CORE_MESSAGE: string;
|
|
20
|
+
private currentId;
|
|
21
|
+
logger: Logger;
|
|
22
|
+
private appEventHandlers;
|
|
23
|
+
constructor();
|
|
24
|
+
/**
|
|
25
|
+
* 为指定应用注册 WebSocket 事件处理函数
|
|
26
|
+
* @param appName 应用名称
|
|
27
|
+
* @param eventType 事件类型
|
|
28
|
+
* @param handler 事件处理函数
|
|
29
|
+
*/
|
|
30
|
+
registerAppEventHandler(appName: string, eventType: WSEventType, handler: WSEventHandler): this;
|
|
31
|
+
/**
|
|
32
|
+
* 移除指定应用的 WebSocket 事件处理函数
|
|
33
|
+
* @param appName 应用名称
|
|
34
|
+
* @param eventType 事件类型
|
|
35
|
+
* @param handler 事件处理函数
|
|
36
|
+
*/
|
|
37
|
+
removeAppEventHandler(appName: string, eventType: WSEventType, handler: WSEventHandler): this;
|
|
38
|
+
/**
|
|
39
|
+
* 触发指定应用的事件处理函数
|
|
40
|
+
* 只有当客户端与应用名称匹配时,才会触发相应的事件处理函数
|
|
41
|
+
* @param appName 应用名称
|
|
42
|
+
* @param eventType 事件类型
|
|
43
|
+
* @param ws WebSocket 实例
|
|
44
|
+
* @param args 事件参数
|
|
45
|
+
*/
|
|
46
|
+
private triggerAppEventHandlers;
|
|
47
|
+
bindAppWSEvents(app: any): void;
|
|
48
|
+
addNewConnection(ws: WebSocketWithId, request: IncomingMessage): WebSocketClient;
|
|
49
|
+
setClientTag(clientId: string, tagKey: string, tagValue: string): void;
|
|
50
|
+
removeClientTag(clientId: string, tagKey: string): void;
|
|
51
|
+
setClientApp(client: WebSocketClient): Promise<void>;
|
|
52
|
+
removeConnection(id: string): void;
|
|
53
|
+
sendMessageToConnection(client: WebSocketClient, sendMessage: object): void;
|
|
54
|
+
sendToConnectionsByTag(tagName: string, tagValue: string, sendMessage: object): void;
|
|
55
|
+
sendToConnectionsByTags(tags: Array<{
|
|
56
|
+
tagName: string;
|
|
57
|
+
tagValue: string;
|
|
58
|
+
}>, sendMessage: object): void;
|
|
59
|
+
sendToClient(clientId: string, sendMessage: object): void;
|
|
60
|
+
loopThroughConnections(callback: (client: WebSocketClient) => void): void;
|
|
61
|
+
close(): void;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var ws_server_exports = {};
|
|
30
|
+
__export(ws_server_exports, {
|
|
31
|
+
WSServer: () => WSServer
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(ws_server_exports);
|
|
34
|
+
var import_lodash = __toESM(require("lodash"));
|
|
35
|
+
var import_nanoid = require("nanoid");
|
|
36
|
+
var import_ws = require("ws");
|
|
37
|
+
var import__ = require(".");
|
|
38
|
+
var import_app_supervisor = require("../app-supervisor");
|
|
39
|
+
var import_errors = require("./errors");
|
|
40
|
+
function getPayloadByErrorCode(code, options) {
|
|
41
|
+
const error = (0, import_errors.getErrorWithCode)(code);
|
|
42
|
+
return import_lodash.default.omit((0, import_errors.applyErrorWithArgs)(error, options), ["status", "maintaining"]);
|
|
43
|
+
}
|
|
44
|
+
__name(getPayloadByErrorCode, "getPayloadByErrorCode");
|
|
45
|
+
const _WSServer = class _WSServer {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.webSocketClients = /* @__PURE__ */ new Map();
|
|
48
|
+
this.currentId = (0, import_nanoid.nanoid)();
|
|
49
|
+
// 存储每个应用注册的事件处理函数
|
|
50
|
+
this.appEventHandlers = {};
|
|
51
|
+
this.wss = new import_ws.WebSocketServer({ noServer: true });
|
|
52
|
+
this.wss.on("connection", (ws, request) => {
|
|
53
|
+
const client = this.addNewConnection(ws, request);
|
|
54
|
+
console.log(`new client connected ${ws.id}`);
|
|
55
|
+
ws.on("error", (error) => {
|
|
56
|
+
this.removeConnection(ws.id);
|
|
57
|
+
if (client && client.app) {
|
|
58
|
+
this.triggerAppEventHandlers(client.app, "error", ws, error);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
ws.on("close", (code, reason) => {
|
|
62
|
+
if (client && client.app) {
|
|
63
|
+
this.triggerAppEventHandlers(client.app, "close", ws, code, reason);
|
|
64
|
+
}
|
|
65
|
+
this.removeConnection(ws.id);
|
|
66
|
+
});
|
|
67
|
+
ws.on("message", (data) => {
|
|
68
|
+
if (client && client.app) {
|
|
69
|
+
this.triggerAppEventHandlers(client.app, "message", ws, data);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
if (client && client.app) {
|
|
73
|
+
this.triggerAppEventHandlers(client.app, "connection", ws, request);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
import__.Gateway.getInstance().on("appSelectorChanged", () => {
|
|
77
|
+
this.loopThroughConnections(async (client) => {
|
|
78
|
+
const handleAppName = await import__.Gateway.getInstance().getRequestHandleAppName({
|
|
79
|
+
url: client.url,
|
|
80
|
+
headers: client.headers
|
|
81
|
+
});
|
|
82
|
+
for (const tag of client.tags) {
|
|
83
|
+
if (tag.startsWith("app#")) {
|
|
84
|
+
client.tags.delete(tag);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
client.tags.add(`app#${handleAppName}`);
|
|
88
|
+
import_app_supervisor.AppSupervisor.getInstance().bootStrapApp(handleAppName);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
import_app_supervisor.AppSupervisor.getInstance().on("appError", async ({ appName, error }) => {
|
|
92
|
+
this.sendToConnectionsByTag("app", appName, {
|
|
93
|
+
type: "notification",
|
|
94
|
+
payload: {
|
|
95
|
+
message: error.message,
|
|
96
|
+
type: "error"
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
import_app_supervisor.AppSupervisor.getInstance().on("appMaintainingMessageChanged", async ({ appName, message, command, status }) => {
|
|
101
|
+
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(appName, {
|
|
102
|
+
withOutBootStrap: true
|
|
103
|
+
});
|
|
104
|
+
const payload = getPayloadByErrorCode(status, {
|
|
105
|
+
app,
|
|
106
|
+
message,
|
|
107
|
+
command
|
|
108
|
+
});
|
|
109
|
+
this.sendToConnectionsByTag("app", appName, {
|
|
110
|
+
type: "maintaining",
|
|
111
|
+
payload
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
import_app_supervisor.AppSupervisor.getInstance().on("appStatusChanged", async ({ appName, status, options }) => {
|
|
115
|
+
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(appName, {
|
|
116
|
+
withOutBootStrap: true
|
|
117
|
+
});
|
|
118
|
+
const payload = getPayloadByErrorCode(status, { app, appName });
|
|
119
|
+
this.sendToConnectionsByTag("app", appName, {
|
|
120
|
+
type: "maintaining",
|
|
121
|
+
payload: {
|
|
122
|
+
...payload,
|
|
123
|
+
...options
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
import_app_supervisor.AppSupervisor.getInstance().on("afterAppAdded", (app) => {
|
|
128
|
+
this.bindAppWSEvents(app);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 为指定应用注册 WebSocket 事件处理函数
|
|
133
|
+
* @param appName 应用名称
|
|
134
|
+
* @param eventType 事件类型
|
|
135
|
+
* @param handler 事件处理函数
|
|
136
|
+
*/
|
|
137
|
+
registerAppEventHandler(appName, eventType, handler) {
|
|
138
|
+
if (!this.appEventHandlers[appName]) {
|
|
139
|
+
this.appEventHandlers[appName] = {};
|
|
140
|
+
}
|
|
141
|
+
if (!this.appEventHandlers[appName][eventType]) {
|
|
142
|
+
this.appEventHandlers[appName][eventType] = /* @__PURE__ */ new Set();
|
|
143
|
+
}
|
|
144
|
+
this.appEventHandlers[appName][eventType].add(handler);
|
|
145
|
+
console.log(`Registered ${eventType} handler for app ${appName}`);
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 移除指定应用的 WebSocket 事件处理函数
|
|
150
|
+
* @param appName 应用名称
|
|
151
|
+
* @param eventType 事件类型
|
|
152
|
+
* @param handler 事件处理函数
|
|
153
|
+
*/
|
|
154
|
+
removeAppEventHandler(appName, eventType, handler) {
|
|
155
|
+
if (this.appEventHandlers[appName] && this.appEventHandlers[appName][eventType]) {
|
|
156
|
+
this.appEventHandlers[appName][eventType].delete(handler);
|
|
157
|
+
console.log(`Removed ${eventType} handler for app ${appName}`);
|
|
158
|
+
}
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 触发指定应用的事件处理函数
|
|
163
|
+
* 只有当客户端与应用名称匹配时,才会触发相应的事件处理函数
|
|
164
|
+
* @param appName 应用名称
|
|
165
|
+
* @param eventType 事件类型
|
|
166
|
+
* @param ws WebSocket 实例
|
|
167
|
+
* @param args 事件参数
|
|
168
|
+
*/
|
|
169
|
+
triggerAppEventHandlers(appName, eventType, ws, ...args) {
|
|
170
|
+
if (!appName || !this.appEventHandlers[appName] || !this.appEventHandlers[appName][eventType]) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const client = this.webSocketClients.get(ws.id);
|
|
174
|
+
if (!client) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (client.app !== appName) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
for (const handler of this.appEventHandlers[appName][eventType]) {
|
|
181
|
+
try {
|
|
182
|
+
handler(ws, ...args);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
console.error(`Error in ${appName} WebSocket ${eventType} handler:`, error);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
bindAppWSEvents(app) {
|
|
189
|
+
if (app.listenerCount("ws:setTag") > 0) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
app.on("ws:setTag", ({ clientId, tagKey, tagValue }) => {
|
|
193
|
+
this.setClientTag(clientId, tagKey, tagValue);
|
|
194
|
+
});
|
|
195
|
+
app.on("ws:removeTag", ({ clientId, tagKey }) => {
|
|
196
|
+
this.removeClientTag(clientId, tagKey);
|
|
197
|
+
});
|
|
198
|
+
app.on("ws:sendToTag", ({ tagKey, tagValue, message }) => {
|
|
199
|
+
this.sendToConnectionsByTags(
|
|
200
|
+
[
|
|
201
|
+
{ tagName: tagKey, tagValue },
|
|
202
|
+
{ tagName: "app", tagValue: app.name }
|
|
203
|
+
],
|
|
204
|
+
message
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
app.on("ws:sendToClient", ({ clientId, message }) => {
|
|
208
|
+
this.sendToClient(clientId, message);
|
|
209
|
+
});
|
|
210
|
+
app.on("ws:sendToCurrentApp", ({ message }) => {
|
|
211
|
+
this.sendToConnectionsByTag("app", app.name, message);
|
|
212
|
+
});
|
|
213
|
+
app.on("ws:sendToTags", ({ tags, message }) => {
|
|
214
|
+
this.sendToConnectionsByTags(tags, message);
|
|
215
|
+
});
|
|
216
|
+
app.on("ws:authorized", ({ clientId, userId }) => {
|
|
217
|
+
this.sendToClient(clientId, { type: "authorized" });
|
|
218
|
+
});
|
|
219
|
+
app.on("ws:registerEventHandler", ({ eventType, handler }) => {
|
|
220
|
+
this.registerAppEventHandler(app.name, eventType, handler);
|
|
221
|
+
});
|
|
222
|
+
app.on("ws:removeEventHandler", ({ eventType, handler }) => {
|
|
223
|
+
this.removeAppEventHandler(app.name, eventType, handler);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
addNewConnection(ws, request) {
|
|
227
|
+
const id = (0, import_nanoid.nanoid)();
|
|
228
|
+
ws.id = id;
|
|
229
|
+
this.webSocketClients.set(id, {
|
|
230
|
+
ws,
|
|
231
|
+
tags: /* @__PURE__ */ new Set(),
|
|
232
|
+
url: request.url,
|
|
233
|
+
headers: request.headers
|
|
234
|
+
});
|
|
235
|
+
this.setClientApp(this.webSocketClients.get(id));
|
|
236
|
+
return this.webSocketClients.get(id);
|
|
237
|
+
}
|
|
238
|
+
setClientTag(clientId, tagKey, tagValue) {
|
|
239
|
+
const client = this.webSocketClients.get(clientId);
|
|
240
|
+
if (!client) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
client.tags.add(`${tagKey}#${tagValue}`);
|
|
244
|
+
console.log(`client tags: ${Array.from(client.tags)}`);
|
|
245
|
+
}
|
|
246
|
+
removeClientTag(clientId, tagKey) {
|
|
247
|
+
const client = this.webSocketClients.get(clientId);
|
|
248
|
+
if (!client) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
client.tags.forEach((tag) => {
|
|
252
|
+
if (tag.startsWith(`${tagKey}#`)) {
|
|
253
|
+
client.tags.delete(tag);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
async setClientApp(client) {
|
|
258
|
+
const req = {
|
|
259
|
+
url: client.url,
|
|
260
|
+
headers: client.headers
|
|
261
|
+
};
|
|
262
|
+
const handleAppName = await import__.Gateway.getInstance().getRequestHandleAppName(req);
|
|
263
|
+
client.app = handleAppName;
|
|
264
|
+
console.log(`client tags: app#${handleAppName}`);
|
|
265
|
+
client.tags.add(`app#${handleAppName}`);
|
|
266
|
+
const hasApp = import_app_supervisor.AppSupervisor.getInstance().hasApp(handleAppName);
|
|
267
|
+
if (!hasApp) {
|
|
268
|
+
import_app_supervisor.AppSupervisor.getInstance().bootStrapApp(handleAppName);
|
|
269
|
+
}
|
|
270
|
+
const appStatus = import_app_supervisor.AppSupervisor.getInstance().getAppStatus(handleAppName, "initializing");
|
|
271
|
+
if (appStatus === "not_found") {
|
|
272
|
+
this.sendMessageToConnection(client, {
|
|
273
|
+
type: "maintaining",
|
|
274
|
+
payload: getPayloadByErrorCode("APP_NOT_FOUND", { appName: handleAppName })
|
|
275
|
+
});
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (appStatus === "initializing") {
|
|
279
|
+
this.sendMessageToConnection(client, {
|
|
280
|
+
type: "maintaining",
|
|
281
|
+
payload: getPayloadByErrorCode("APP_INITIALIZING", { appName: handleAppName })
|
|
282
|
+
});
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(handleAppName);
|
|
286
|
+
this.sendMessageToConnection(client, {
|
|
287
|
+
type: "maintaining",
|
|
288
|
+
payload: getPayloadByErrorCode(appStatus, { app })
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
removeConnection(id) {
|
|
292
|
+
console.log(`client disconnected ${id}`);
|
|
293
|
+
this.webSocketClients.delete(id);
|
|
294
|
+
}
|
|
295
|
+
sendMessageToConnection(client, sendMessage) {
|
|
296
|
+
client.ws.send(JSON.stringify(sendMessage));
|
|
297
|
+
}
|
|
298
|
+
sendToConnectionsByTag(tagName, tagValue, sendMessage) {
|
|
299
|
+
const tagString = `${tagName}#${tagValue}`;
|
|
300
|
+
this.webSocketClients.forEach((client) => {
|
|
301
|
+
if (client.tags.has(tagString)) {
|
|
302
|
+
this.sendMessageToConnection(client, sendMessage);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
sendToConnectionsByTags(tags, sendMessage) {
|
|
307
|
+
const tagStrings = tags.map(({ tagName, tagValue }) => `${tagName}#${tagValue}`);
|
|
308
|
+
this.webSocketClients.forEach((client) => {
|
|
309
|
+
if (tagStrings.every((tagString) => client.tags.has(tagString))) {
|
|
310
|
+
this.sendMessageToConnection(client, sendMessage);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
sendToClient(clientId, sendMessage) {
|
|
315
|
+
const client = this.webSocketClients.get(clientId);
|
|
316
|
+
if (client) {
|
|
317
|
+
this.sendMessageToConnection(client, sendMessage);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
loopThroughConnections(callback) {
|
|
321
|
+
this.webSocketClients.forEach((client) => {
|
|
322
|
+
callback(client);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
close() {
|
|
326
|
+
this.wss.close();
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
__name(_WSServer, "WSServer");
|
|
330
|
+
_WSServer.KEY_CORE_MESSAGE = "KEY_CORE_MESSAGE";
|
|
331
|
+
let WSServer = _WSServer;
|
|
332
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
333
|
+
0 && (module.exports = {
|
|
334
|
+
WSServer
|
|
335
|
+
});
|
package/lib/helper.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Resourcer } from '@tachybase/resourcer';
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import { type i18n as TypeI18n } from 'i18next';
|
|
4
|
+
import Application, { ApplicationOptions } from './application';
|
|
5
|
+
export declare function createI18n(options: ApplicationOptions): TypeI18n;
|
|
6
|
+
export declare function createResourcer(options: ApplicationOptions): Resourcer;
|
|
7
|
+
export declare function registerMiddlewares(app: Application, options: ApplicationOptions): void;
|
|
8
|
+
export declare const createAppProxy: (app: Application) => Application<import("./application").DefaultState, import("./application").DefaultContext>;
|
|
9
|
+
export declare const getCommandFullName: (command: Command) => string;
|
|
10
|
+
export declare const tsxRerunning: () => Promise<void>;
|
|
11
|
+
export declare const enablePerfHooks: (app: Application) => void;
|