@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,299 @@
|
|
|
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 app_supervisor_exports = {};
|
|
30
|
+
__export(app_supervisor_exports, {
|
|
31
|
+
AppSupervisor: () => AppSupervisor
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(app_supervisor_exports);
|
|
34
|
+
var import_node_events = require("node:events");
|
|
35
|
+
var import_utils = require("@tachybase/utils");
|
|
36
|
+
var import_async_mutex = require("async-mutex");
|
|
37
|
+
var import_application = __toESM(require("./application"));
|
|
38
|
+
var import_handler = require("./errors/handler");
|
|
39
|
+
const _AppSupervisor = class _AppSupervisor extends import_node_events.EventEmitter {
|
|
40
|
+
constructor() {
|
|
41
|
+
super();
|
|
42
|
+
this.runningMode = "multiple";
|
|
43
|
+
this.singleAppName = null;
|
|
44
|
+
this.apps = {};
|
|
45
|
+
this.lastSeenAt = /* @__PURE__ */ new Map();
|
|
46
|
+
this.appErrors = {};
|
|
47
|
+
this.appStatus = {};
|
|
48
|
+
this.lastMaintainingMessage = {};
|
|
49
|
+
this.statusBeforeCommanding = {};
|
|
50
|
+
this.appMutexes = {};
|
|
51
|
+
this.appBootstrapper = null;
|
|
52
|
+
this.blockApps = /* @__PURE__ */ new Set();
|
|
53
|
+
if (process.env.STARTUP_SUBAPP) {
|
|
54
|
+
this.runningMode = "single";
|
|
55
|
+
this.singleAppName = process.env.STARTUP_SUBAPP;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
static getInstance() {
|
|
59
|
+
if (!_AppSupervisor.instance) {
|
|
60
|
+
_AppSupervisor.instance = new _AppSupervisor();
|
|
61
|
+
}
|
|
62
|
+
return _AppSupervisor.instance;
|
|
63
|
+
}
|
|
64
|
+
setAppError(appName, error) {
|
|
65
|
+
this.appErrors[appName] = error;
|
|
66
|
+
this.emit("appError", {
|
|
67
|
+
appName,
|
|
68
|
+
error
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
hasAppError(appName) {
|
|
72
|
+
return !!this.appErrors[appName];
|
|
73
|
+
}
|
|
74
|
+
clearAppError(appName) {
|
|
75
|
+
delete this.appErrors[appName];
|
|
76
|
+
}
|
|
77
|
+
async reset() {
|
|
78
|
+
const appNames = Object.keys(this.apps);
|
|
79
|
+
for (const appName of appNames) {
|
|
80
|
+
await this.removeApp(appName);
|
|
81
|
+
}
|
|
82
|
+
this.appBootstrapper = null;
|
|
83
|
+
this.removeAllListeners();
|
|
84
|
+
}
|
|
85
|
+
async destroy() {
|
|
86
|
+
await this.reset();
|
|
87
|
+
_AppSupervisor.instance = null;
|
|
88
|
+
}
|
|
89
|
+
setAppStatus(appName, status, options = {}) {
|
|
90
|
+
if (this.appStatus[appName] === status) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.appStatus[appName] = status;
|
|
94
|
+
this.emit("appStatusChanged", {
|
|
95
|
+
appName,
|
|
96
|
+
status,
|
|
97
|
+
options
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
touchApp(appName) {
|
|
101
|
+
if (!this.hasApp(appName)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.lastSeenAt.set(appName, Math.floor(Date.now() / 1e3));
|
|
105
|
+
}
|
|
106
|
+
getMutexOfApp(appName) {
|
|
107
|
+
if (!this.appMutexes[appName]) {
|
|
108
|
+
this.appMutexes[appName] = new import_async_mutex.Mutex();
|
|
109
|
+
}
|
|
110
|
+
return this.appMutexes[appName];
|
|
111
|
+
}
|
|
112
|
+
async bootStrapApp(appName, options = {}) {
|
|
113
|
+
await this.getMutexOfApp(appName).runExclusive(async () => {
|
|
114
|
+
if (!this.hasApp(appName)) {
|
|
115
|
+
if (!this.getAppStatus(appName)) {
|
|
116
|
+
this.setAppStatus(appName, "initializing");
|
|
117
|
+
}
|
|
118
|
+
if (this.appBootstrapper) {
|
|
119
|
+
await this.appBootstrapper({
|
|
120
|
+
appSupervisor: this,
|
|
121
|
+
appName,
|
|
122
|
+
options
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
if (!this.hasApp(appName)) {
|
|
126
|
+
this.setAppStatus(appName, "not_found", {
|
|
127
|
+
error: new Error(`app ${appName} not found`)
|
|
128
|
+
});
|
|
129
|
+
} else if (!this.getAppStatus(appName) || this.getAppStatus(appName) === "initializing") {
|
|
130
|
+
this.setAppStatus(appName, "initialized");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async getApp(appName, options = {}) {
|
|
136
|
+
if (!options.withOutBootStrap) {
|
|
137
|
+
if (!this.blockApps.has(appName)) {
|
|
138
|
+
await this.bootStrapApp(appName, options);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return this.apps[appName];
|
|
142
|
+
}
|
|
143
|
+
setAppBootstrapper(appBootstrapper) {
|
|
144
|
+
this.appBootstrapper = appBootstrapper;
|
|
145
|
+
}
|
|
146
|
+
getAppStatus(appName, defaultStatus) {
|
|
147
|
+
const status = this.appStatus[appName];
|
|
148
|
+
if (status === void 0 && defaultStatus !== void 0) {
|
|
149
|
+
return defaultStatus;
|
|
150
|
+
}
|
|
151
|
+
return status;
|
|
152
|
+
}
|
|
153
|
+
bootMainApp(options) {
|
|
154
|
+
return new import_application.default(options);
|
|
155
|
+
}
|
|
156
|
+
hasApp(appName) {
|
|
157
|
+
return !!this.apps[appName];
|
|
158
|
+
}
|
|
159
|
+
// add app into supervisor
|
|
160
|
+
addApp(app) {
|
|
161
|
+
if (this.blockApps.has(app.name)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (this.apps[app.name]) {
|
|
165
|
+
throw new Error(`app ${app.name} already exists`);
|
|
166
|
+
}
|
|
167
|
+
app.logger.info(`add app ${app.name} into supervisor`, { submodule: "supervisor", method: "addApp" });
|
|
168
|
+
this.bindAppEvents(app);
|
|
169
|
+
this.apps[app.name] = app;
|
|
170
|
+
this.emit("afterAppAdded", app);
|
|
171
|
+
if (!this.getAppStatus(app.name) || this.getAppStatus(app.name) === "not_found") {
|
|
172
|
+
this.setAppStatus(app.name, "initialized");
|
|
173
|
+
}
|
|
174
|
+
return app;
|
|
175
|
+
}
|
|
176
|
+
// get registered app names
|
|
177
|
+
async getAppsNames() {
|
|
178
|
+
const apps = Object.values(this.apps);
|
|
179
|
+
return apps.map((app) => app.name);
|
|
180
|
+
}
|
|
181
|
+
async removeApp(appName) {
|
|
182
|
+
if (!this.apps[appName]) {
|
|
183
|
+
console.log(`app ${appName} not exists`);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
await this.apps[appName].runCommand("destroy");
|
|
187
|
+
}
|
|
188
|
+
subApps() {
|
|
189
|
+
return Object.values(this.apps).filter((app) => app.name !== "main");
|
|
190
|
+
}
|
|
191
|
+
on(eventName, listener) {
|
|
192
|
+
const listeners = this.listeners(eventName);
|
|
193
|
+
const listenerName = listener.name;
|
|
194
|
+
if (listenerName !== "") {
|
|
195
|
+
const exists = listeners.find((l) => l.name === listenerName);
|
|
196
|
+
if (exists) {
|
|
197
|
+
super.removeListener(eventName, exists);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return super.on(eventName, listener);
|
|
201
|
+
}
|
|
202
|
+
bindAppEvents(app) {
|
|
203
|
+
app.on("afterDestroy", () => {
|
|
204
|
+
delete this.apps[app.name];
|
|
205
|
+
delete this.appStatus[app.name];
|
|
206
|
+
delete this.appErrors[app.name];
|
|
207
|
+
delete this.lastMaintainingMessage[app.name];
|
|
208
|
+
delete this.statusBeforeCommanding[app.name];
|
|
209
|
+
this.lastSeenAt.delete(app.name);
|
|
210
|
+
});
|
|
211
|
+
app.on("maintainingMessageChanged", ({ message, maintainingStatus }) => {
|
|
212
|
+
if (this.lastMaintainingMessage[app.name] === message) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
this.lastMaintainingMessage[app.name] = message;
|
|
216
|
+
const appStatus = this.getAppStatus(app.name);
|
|
217
|
+
if (!maintainingStatus && appStatus !== "running") {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
this.emit("appMaintainingMessageChanged", {
|
|
221
|
+
appName: app.name,
|
|
222
|
+
message,
|
|
223
|
+
status: appStatus,
|
|
224
|
+
command: appStatus === "running" ? null : maintainingStatus.command
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
app.on("__started", async (_app, options) => {
|
|
228
|
+
const { maintainingStatus, options: startOptions } = options;
|
|
229
|
+
if (maintainingStatus && [
|
|
230
|
+
"install",
|
|
231
|
+
"upgrade",
|
|
232
|
+
"refresh",
|
|
233
|
+
"restore",
|
|
234
|
+
"pm.add",
|
|
235
|
+
"pm.update",
|
|
236
|
+
"pm.enable",
|
|
237
|
+
"pm.disable",
|
|
238
|
+
"pm.remove"
|
|
239
|
+
].includes(maintainingStatus.command.name) && !startOptions.recover) {
|
|
240
|
+
this.setAppStatus(app.name, "running", {
|
|
241
|
+
refresh: true
|
|
242
|
+
});
|
|
243
|
+
} else {
|
|
244
|
+
this.setAppStatus(app.name, "running");
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
app.on("afterStop", async () => {
|
|
248
|
+
this.setAppStatus(app.name, "stopped");
|
|
249
|
+
});
|
|
250
|
+
app.on("maintaining", (maintainingStatus) => {
|
|
251
|
+
const { status, command } = maintainingStatus;
|
|
252
|
+
switch (status) {
|
|
253
|
+
case "command_begin":
|
|
254
|
+
{
|
|
255
|
+
this.statusBeforeCommanding[app.name] = this.getAppStatus(app.name);
|
|
256
|
+
this.setAppStatus(app.name, "commanding");
|
|
257
|
+
}
|
|
258
|
+
break;
|
|
259
|
+
case "command_running":
|
|
260
|
+
break;
|
|
261
|
+
case "command_end":
|
|
262
|
+
{
|
|
263
|
+
const appStatus = this.getAppStatus(app.name);
|
|
264
|
+
this.emit("appMaintainingStatusChanged", maintainingStatus);
|
|
265
|
+
if (appStatus === "commanding") {
|
|
266
|
+
this.setAppStatus(app.name, this.statusBeforeCommanding[app.name]);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
break;
|
|
270
|
+
case "command_error":
|
|
271
|
+
{
|
|
272
|
+
const errorLevel = (0, import_handler.getErrorLevel)(maintainingStatus.error);
|
|
273
|
+
if (errorLevel === "fatal") {
|
|
274
|
+
this.setAppError(app.name, maintainingStatus.error);
|
|
275
|
+
this.setAppStatus(app.name, "error", {
|
|
276
|
+
error: maintainingStatus.error
|
|
277
|
+
});
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
if (errorLevel === "warn") {
|
|
281
|
+
this.emit("appError", {
|
|
282
|
+
appName: app.name,
|
|
283
|
+
error: maintainingStatus.error
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
this.setAppStatus(app.name, this.statusBeforeCommanding[app.name]);
|
|
287
|
+
}
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
__name(_AppSupervisor, "AppSupervisor");
|
|
294
|
+
let AppSupervisor = _AppSupervisor;
|
|
295
|
+
(0, import_utils.applyMixins)(AppSupervisor, [import_utils.AsyncEmitter]);
|
|
296
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
297
|
+
0 && (module.exports = {
|
|
298
|
+
AppSupervisor
|
|
299
|
+
});
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { RecordableHistogram } from 'node:perf_hooks';
|
|
3
|
+
import { AuthManager, AuthManagerOptions } from '@tachybase/auth';
|
|
4
|
+
import { Cache, CacheManager, CacheManagerOptions } from '@tachybase/cache';
|
|
5
|
+
import { DataSourceManager, SequelizeDataSource } from '@tachybase/data-source';
|
|
6
|
+
import Database, { CollectionOptions, IDatabaseOptions } from '@tachybase/database';
|
|
7
|
+
import { ContainerInstance } from '@tachybase/di';
|
|
8
|
+
import { LoggerOptions, RequestLoggerOptions, SystemLogger, SystemLoggerOptions } from '@tachybase/logger';
|
|
9
|
+
import { ResourceOptions, Resourcer } from '@tachybase/resourcer';
|
|
10
|
+
import { AsyncEmitter, Constructable, ToposortOptions } from '@tachybase/utils';
|
|
11
|
+
import { Command, CommandOptions, ParseOptions } from 'commander';
|
|
12
|
+
import { i18n, InitOptions } from 'i18next';
|
|
13
|
+
import Koa, { DefaultContext as KoaDefaultContext, DefaultState as KoaDefaultState } from 'koa';
|
|
14
|
+
import WebSocket from 'ws';
|
|
15
|
+
import AesEncryptor from './aes-encryptor';
|
|
16
|
+
import { AppCommand } from './app-command';
|
|
17
|
+
import { AppSupervisor } from './app-supervisor';
|
|
18
|
+
import { CronJobManager } from './cron/cron-job-manager';
|
|
19
|
+
import { Environment } from './environment';
|
|
20
|
+
import { ApplicationVersion } from './helpers/application-version';
|
|
21
|
+
import { Locale } from './locale';
|
|
22
|
+
import { NoticeManager } from './notice';
|
|
23
|
+
import { Plugin } from './plugin';
|
|
24
|
+
import { InstallOptions, PluginManager } from './plugin-manager';
|
|
25
|
+
import { PubSubManager, PubSubManagerOptions } from './pub-sub-manager';
|
|
26
|
+
import { SyncMessageManager } from './sync-message-manager';
|
|
27
|
+
type WSEventType = 'close' | 'error' | 'message' | 'connection';
|
|
28
|
+
type WSEventHandler = (ws: WebSocket & {
|
|
29
|
+
id: string;
|
|
30
|
+
}, ...args: any[]) => Promise<void> | void;
|
|
31
|
+
export { Logger } from 'winston';
|
|
32
|
+
export type PluginType = string | typeof Plugin;
|
|
33
|
+
export type PluginConfiguration = PluginType | [PluginType, any];
|
|
34
|
+
declare module '@tachybase/resourcer' {
|
|
35
|
+
interface ResourcerContext {
|
|
36
|
+
app?: Application;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export interface ResourcerOptions {
|
|
40
|
+
prefix?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface AppLoggerOptions {
|
|
43
|
+
request: RequestLoggerOptions;
|
|
44
|
+
system: SystemLoggerOptions;
|
|
45
|
+
}
|
|
46
|
+
export interface ApplicationOptions {
|
|
47
|
+
database?: IDatabaseOptions | Database;
|
|
48
|
+
cacheManager?: CacheManagerOptions;
|
|
49
|
+
resourcer?: ResourcerOptions;
|
|
50
|
+
pubSubManager?: PubSubManagerOptions;
|
|
51
|
+
syncMessageManager?: any;
|
|
52
|
+
bodyParser?: any;
|
|
53
|
+
cors?: any;
|
|
54
|
+
dataWrapping?: boolean;
|
|
55
|
+
registerActions?: boolean;
|
|
56
|
+
i18n?: i18n | InitOptions;
|
|
57
|
+
plugins?: PluginConfiguration[];
|
|
58
|
+
acl?: boolean;
|
|
59
|
+
logger?: AppLoggerOptions;
|
|
60
|
+
pmSock?: string;
|
|
61
|
+
name?: string;
|
|
62
|
+
authManager?: AuthManagerOptions;
|
|
63
|
+
perfHooks?: boolean;
|
|
64
|
+
tmpl?: any;
|
|
65
|
+
}
|
|
66
|
+
export interface DefaultState extends KoaDefaultState {
|
|
67
|
+
currentUser?: any;
|
|
68
|
+
[key: string]: any;
|
|
69
|
+
}
|
|
70
|
+
export interface DefaultContext extends KoaDefaultContext {
|
|
71
|
+
db: Database;
|
|
72
|
+
cache: Cache;
|
|
73
|
+
resourcer: Resourcer;
|
|
74
|
+
i18n: any;
|
|
75
|
+
[key: string]: any;
|
|
76
|
+
}
|
|
77
|
+
interface ActionsOptions {
|
|
78
|
+
resourceName?: string;
|
|
79
|
+
resourceNames?: string[];
|
|
80
|
+
}
|
|
81
|
+
interface StartOptions {
|
|
82
|
+
cliArgs?: any[];
|
|
83
|
+
dbSync?: boolean;
|
|
84
|
+
checkInstall?: boolean;
|
|
85
|
+
quickstart?: boolean;
|
|
86
|
+
reload?: boolean;
|
|
87
|
+
recover?: boolean;
|
|
88
|
+
}
|
|
89
|
+
type MaintainingStatus = 'command_begin' | 'command_end' | 'command_running' | 'command_error';
|
|
90
|
+
export type MaintainingCommandStatus = {
|
|
91
|
+
command: {
|
|
92
|
+
name: string;
|
|
93
|
+
};
|
|
94
|
+
status: MaintainingStatus;
|
|
95
|
+
error?: Error;
|
|
96
|
+
};
|
|
97
|
+
export declare class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
|
98
|
+
options: ApplicationOptions;
|
|
99
|
+
/**
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
middleware: any;
|
|
103
|
+
/**
|
|
104
|
+
* @internal
|
|
105
|
+
*/
|
|
106
|
+
stopped: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
ready: boolean;
|
|
111
|
+
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
112
|
+
/**
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
rawOptions: ApplicationOptions;
|
|
116
|
+
/**
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
activatedCommand: {
|
|
120
|
+
name: string;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* @internal
|
|
124
|
+
*/
|
|
125
|
+
running: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* @internal
|
|
128
|
+
*/
|
|
129
|
+
perfHistograms: Map<string, RecordableHistogram>;
|
|
130
|
+
/**
|
|
131
|
+
* @internal
|
|
132
|
+
*/
|
|
133
|
+
pubSubManager: PubSubManager;
|
|
134
|
+
syncMessageManager: SyncMessageManager;
|
|
135
|
+
protected plugins: Map<string, Plugin>;
|
|
136
|
+
protected _appSupervisor: AppSupervisor;
|
|
137
|
+
protected _started: boolean;
|
|
138
|
+
protected _logger: SystemLogger;
|
|
139
|
+
private _authenticated;
|
|
140
|
+
private _maintaining;
|
|
141
|
+
private _maintainingCommandStatus;
|
|
142
|
+
private _maintainingStatusBeforeCommand;
|
|
143
|
+
private _actionCommand;
|
|
144
|
+
private _noticeManager;
|
|
145
|
+
static KEY_CORE_APP_PREFIX: string;
|
|
146
|
+
private currentId;
|
|
147
|
+
container: ContainerInstance;
|
|
148
|
+
modules: Record<string, any>;
|
|
149
|
+
middlewareSourceMap: WeakMap<Function, string>;
|
|
150
|
+
private wsEventHandlers;
|
|
151
|
+
constructor(options: ApplicationOptions);
|
|
152
|
+
get noticeManager(): NoticeManager;
|
|
153
|
+
protected _loaded: boolean;
|
|
154
|
+
/**
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
get loaded(): boolean;
|
|
158
|
+
private _maintainingMessage;
|
|
159
|
+
/**
|
|
160
|
+
* @internal
|
|
161
|
+
*/
|
|
162
|
+
get maintainingMessage(): string;
|
|
163
|
+
private _env;
|
|
164
|
+
get environment(): Environment;
|
|
165
|
+
protected _aesEncryptor: AesEncryptor;
|
|
166
|
+
get aesEncryptor(): AesEncryptor;
|
|
167
|
+
protected _cronJobManager: CronJobManager;
|
|
168
|
+
get cronJobManager(): CronJobManager;
|
|
169
|
+
get mainDataSource(): SequelizeDataSource;
|
|
170
|
+
get db(): Database;
|
|
171
|
+
get logger(): SystemLogger;
|
|
172
|
+
get resourcer(): import("@tachybase/data-source").ResourceManager;
|
|
173
|
+
protected _cacheManager: CacheManager;
|
|
174
|
+
get cacheManager(): CacheManager;
|
|
175
|
+
protected _cache: Cache;
|
|
176
|
+
get cache(): Cache;
|
|
177
|
+
/**
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
180
|
+
set cache(cache: Cache);
|
|
181
|
+
protected _cli: AppCommand;
|
|
182
|
+
get cli(): AppCommand;
|
|
183
|
+
protected _i18n: i18n;
|
|
184
|
+
get i18n(): i18n;
|
|
185
|
+
protected _pm: PluginManager;
|
|
186
|
+
get pm(): PluginManager;
|
|
187
|
+
get acl(): import("packages/acl/lib").ACL;
|
|
188
|
+
protected _authManager: AuthManager;
|
|
189
|
+
get authManager(): AuthManager;
|
|
190
|
+
protected _locales: Locale;
|
|
191
|
+
/**
|
|
192
|
+
* This method is deprecated and should not be used.
|
|
193
|
+
* Use {@link #localeManager} instead.
|
|
194
|
+
* @deprecated
|
|
195
|
+
*/
|
|
196
|
+
get locales(): Locale;
|
|
197
|
+
get localeManager(): Locale;
|
|
198
|
+
protected _version: ApplicationVersion;
|
|
199
|
+
get version(): ApplicationVersion;
|
|
200
|
+
/**
|
|
201
|
+
* Use {@link logger}
|
|
202
|
+
* @deprecated
|
|
203
|
+
*/
|
|
204
|
+
get log(): SystemLogger;
|
|
205
|
+
get name(): string;
|
|
206
|
+
protected _dataSourceManager: DataSourceManager;
|
|
207
|
+
get dataSourceManager(): DataSourceManager;
|
|
208
|
+
/**
|
|
209
|
+
* @internal
|
|
210
|
+
*/
|
|
211
|
+
getMaintaining(): MaintainingCommandStatus;
|
|
212
|
+
/**
|
|
213
|
+
* @internal
|
|
214
|
+
*/
|
|
215
|
+
setMaintaining(_maintainingCommandStatus: MaintainingCommandStatus): void;
|
|
216
|
+
/**
|
|
217
|
+
* @internal
|
|
218
|
+
*/
|
|
219
|
+
setMaintainingMessage(message: string): void;
|
|
220
|
+
/**
|
|
221
|
+
* This method is deprecated and should not be used.
|
|
222
|
+
* Use {@link #this.version.get()} instead.
|
|
223
|
+
* @deprecated
|
|
224
|
+
*/
|
|
225
|
+
getVersion(): string;
|
|
226
|
+
use<NewStateT = {}, NewContextT = {}>(middleware: Koa.Middleware<StateT & NewStateT, ContextT & NewContextT>, options?: ToposortOptions): this;
|
|
227
|
+
/**
|
|
228
|
+
* @internal
|
|
229
|
+
*/
|
|
230
|
+
callback(): (req: IncomingMessage, res: ServerResponse) => any;
|
|
231
|
+
/**
|
|
232
|
+
* This method is deprecated and should not be used.
|
|
233
|
+
* Use {@link #this.db.collection()} instead.
|
|
234
|
+
* @deprecated
|
|
235
|
+
*/
|
|
236
|
+
collection(options: CollectionOptions): import("@tachybase/database").Collection<any, any>;
|
|
237
|
+
/**
|
|
238
|
+
* This method is deprecated and should not be used.
|
|
239
|
+
* Use {@link #this.resourcer.define()} instead.
|
|
240
|
+
* @deprecated
|
|
241
|
+
*/
|
|
242
|
+
resource(options: ResourceOptions): import("@tachybase/resourcer").Resource;
|
|
243
|
+
/**
|
|
244
|
+
* This method is deprecated and should not be used.
|
|
245
|
+
* Use {@link #this.resourcer.registerActions()} instead.
|
|
246
|
+
* @deprecated
|
|
247
|
+
*/
|
|
248
|
+
actions(handlers: any, options?: ActionsOptions): void;
|
|
249
|
+
command(name: string, desc?: string, opts?: CommandOptions): AppCommand;
|
|
250
|
+
findCommand(name: string): Command;
|
|
251
|
+
/**
|
|
252
|
+
* @internal
|
|
253
|
+
*/
|
|
254
|
+
reInit(): Promise<void>;
|
|
255
|
+
load(options?: any): Promise<void>;
|
|
256
|
+
reload(options?: any): Promise<void>;
|
|
257
|
+
/**
|
|
258
|
+
* This method is deprecated and should not be used.
|
|
259
|
+
* Use {@link this.pm.get()} instead.
|
|
260
|
+
* @deprecated
|
|
261
|
+
*/
|
|
262
|
+
getPlugin<P extends Plugin>(name: string | Constructable<P>): P;
|
|
263
|
+
/**
|
|
264
|
+
* This method is deprecated and should not be used.
|
|
265
|
+
* Use {@link this.runAsCLI()} instead.
|
|
266
|
+
* @deprecated
|
|
267
|
+
*/
|
|
268
|
+
parse(argv?: string[]): Promise<AppCommand>;
|
|
269
|
+
authenticate(): Promise<void>;
|
|
270
|
+
runCommand(command: string, ...args: any[]): Promise<AppCommand>;
|
|
271
|
+
runCommandThrowError(command: string, ...args: any[]): Promise<AppCommand>;
|
|
272
|
+
protected createCLI(): AppCommand;
|
|
273
|
+
/**
|
|
274
|
+
* @internal
|
|
275
|
+
*/
|
|
276
|
+
loadMigrations(options: any): Promise<{
|
|
277
|
+
beforeLoad: any[];
|
|
278
|
+
afterSync: any[];
|
|
279
|
+
afterLoad: any[];
|
|
280
|
+
}>;
|
|
281
|
+
/**
|
|
282
|
+
* @internal
|
|
283
|
+
*/
|
|
284
|
+
loadCoreMigrations(): Promise<{
|
|
285
|
+
beforeLoad: {
|
|
286
|
+
up: () => Promise<void>;
|
|
287
|
+
};
|
|
288
|
+
afterSync: {
|
|
289
|
+
up: () => Promise<void>;
|
|
290
|
+
};
|
|
291
|
+
afterLoad: {
|
|
292
|
+
up: () => Promise<void>;
|
|
293
|
+
};
|
|
294
|
+
}>;
|
|
295
|
+
/**
|
|
296
|
+
* @internal
|
|
297
|
+
*/
|
|
298
|
+
loadPluginCommands(): Promise<void>;
|
|
299
|
+
/**
|
|
300
|
+
* @internal
|
|
301
|
+
*/
|
|
302
|
+
runAsCLI(argv?: string[], options?: ParseOptions & {
|
|
303
|
+
throwError?: boolean;
|
|
304
|
+
reqId?: string;
|
|
305
|
+
}): Promise<AppCommand>;
|
|
306
|
+
start(options?: StartOptions): Promise<void>;
|
|
307
|
+
/**
|
|
308
|
+
* @internal
|
|
309
|
+
*/
|
|
310
|
+
emitStartedEvent(options?: StartOptions): Promise<void>;
|
|
311
|
+
isStarted(): Promise<boolean>;
|
|
312
|
+
/**
|
|
313
|
+
* @internal
|
|
314
|
+
*/
|
|
315
|
+
tryReloadOrRestart(options?: StartOptions): Promise<void>;
|
|
316
|
+
restart(options?: StartOptions): Promise<void>;
|
|
317
|
+
stop(options?: any): Promise<void>;
|
|
318
|
+
destroy(options?: any): Promise<void>;
|
|
319
|
+
isInstalled(): Promise<boolean>;
|
|
320
|
+
install(options?: InstallOptions): Promise<void>;
|
|
321
|
+
upgrade(options?: any): Promise<void>;
|
|
322
|
+
toJSON(): {
|
|
323
|
+
appName: string;
|
|
324
|
+
name: string;
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* @internal
|
|
328
|
+
*/
|
|
329
|
+
reInitEvents(): void;
|
|
330
|
+
createLogger(options: LoggerOptions): import("winston").Logger;
|
|
331
|
+
protected init(): void;
|
|
332
|
+
protected createMainDataSource(options: ApplicationOptions): void;
|
|
333
|
+
protected createDatabase(options: ApplicationOptions): Database;
|
|
334
|
+
/**
|
|
335
|
+
* 初始化 WebSocket 事件处理
|
|
336
|
+
* 注册应用级别的事件,用于与 WSServer 通信
|
|
337
|
+
*/
|
|
338
|
+
private initWSEventHandlers;
|
|
339
|
+
/**
|
|
340
|
+
* 为 WebSocket 事件注册处理函数
|
|
341
|
+
* 这是一个适配器方法,将事件处理函数注册到 Gateway 的 WSServer
|
|
342
|
+
* @param eventType 事件类型
|
|
343
|
+
* @param handler 事件处理函数
|
|
344
|
+
*/
|
|
345
|
+
registerWSEventHandler(eventType: WSEventType, handler: WSEventHandler): this;
|
|
346
|
+
/**
|
|
347
|
+
* 移除 WebSocket 事件处理函数
|
|
348
|
+
* @param eventType 事件类型
|
|
349
|
+
* @param handler 事件处理函数
|
|
350
|
+
*/
|
|
351
|
+
removeWSEventHandler(eventType: WSEventType, handler: WSEventHandler): this;
|
|
352
|
+
[key: string]: any;
|
|
353
|
+
}
|
|
354
|
+
export default Application;
|