@nocobase/server 2.0.0-beta.9 → 2.0.0

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 (41) hide show
  1. package/lib/aes-encryptor.d.ts +1 -0
  2. package/lib/aes-encryptor.js +8 -5
  3. package/lib/ai/create-docs-index.d.ts +13 -0
  4. package/lib/ai/create-docs-index.js +892 -0
  5. package/lib/app-supervisor/app-options-factory.d.ts +80 -0
  6. package/lib/app-supervisor/app-options-factory.js +91 -0
  7. package/lib/app-supervisor/condition-registry.d.ts +18 -0
  8. package/lib/app-supervisor/condition-registry.js +60 -0
  9. package/lib/app-supervisor/db-creator.d.ts +16 -0
  10. package/lib/app-supervisor/db-creator.js +163 -0
  11. package/lib/app-supervisor/db-drivers.d.ts +11 -0
  12. package/lib/app-supervisor/db-drivers.js +52 -0
  13. package/lib/app-supervisor/index.d.ts +161 -0
  14. package/lib/app-supervisor/index.js +690 -0
  15. package/lib/app-supervisor/main-only-adapter.d.ts +37 -0
  16. package/lib/app-supervisor/main-only-adapter.js +156 -0
  17. package/lib/app-supervisor/types.d.ts +161 -0
  18. package/lib/app-supervisor/types.js +24 -0
  19. package/lib/application.d.ts +3 -1
  20. package/lib/application.js +10 -6
  21. package/lib/commands/ai.d.ts +11 -0
  22. package/lib/commands/ai.js +40 -0
  23. package/lib/commands/console.js +1 -1
  24. package/lib/commands/index.js +2 -0
  25. package/lib/commands/install.js +2 -0
  26. package/lib/commands/start.js +3 -0
  27. package/lib/commands/upgrade.js +2 -0
  28. package/lib/gateway/errors.js +1 -1
  29. package/lib/gateway/index.js +64 -15
  30. package/lib/gateway/ipc-socket-server.js +1 -1
  31. package/lib/gateway/ws-server.js +3 -2
  32. package/lib/helper.js +20 -3
  33. package/lib/plugin-manager/deps.js +1 -1
  34. package/lib/plugin-manager/plugin-manager.js +2 -0
  35. package/lib/plugin.d.ts +5 -0
  36. package/lib/plugin.js +25 -0
  37. package/lib/redis-connection-manager.d.ts +15 -5
  38. package/lib/redis-connection-manager.js +117 -24
  39. package/package.json +18 -17
  40. package/lib/app-supervisor.d.ts +0 -74
  41. package/lib/app-supervisor.js +0 -338
@@ -1,74 +0,0 @@
1
- /**
2
- * This file is part of the NocoBase (R) project.
3
- * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
- * Authors: NocoBase Team.
5
- *
6
- * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
- * For more information, please refer to: https://www.nocobase.com/agreement.
8
- */
9
- /// <reference types="node" />
10
- import { AsyncEmitter } from '@nocobase/utils';
11
- import { Mutex } from 'async-mutex';
12
- import { EventEmitter } from 'events';
13
- import Application, { ApplicationOptions } from './application';
14
- type BootOptions = {
15
- appName: string;
16
- options: any;
17
- appSupervisor: AppSupervisor;
18
- };
19
- type AppBootstrapper = (bootOptions: BootOptions) => Promise<void>;
20
- export type AppStatus = 'preparing' | 'initializing' | 'initialized' | 'running' | 'commanding' | 'stopped' | 'error' | 'not_found';
21
- export declare class AppSupervisor extends EventEmitter implements AsyncEmitter {
22
- private static instance;
23
- private bootstrapQueue;
24
- runningMode: 'single' | 'multiple';
25
- singleAppName: string | null;
26
- emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
27
- apps: {
28
- [appName: string]: Application;
29
- };
30
- lastSeenAt: Map<string, number>;
31
- appErrors: {
32
- [appName: string]: Error;
33
- };
34
- appStatus: {
35
- [appName: string]: AppStatus;
36
- };
37
- lastMaintainingMessage: {
38
- [appName: string]: string;
39
- };
40
- statusBeforeCommanding: {
41
- [appName: string]: AppStatus;
42
- };
43
- private appMutexes;
44
- private appBootstrapper;
45
- private constructor();
46
- static getInstance(): AppSupervisor;
47
- setAppError(appName: string, error: Error): void;
48
- hasAppError(appName: string): boolean;
49
- clearAppError(appName: string): void;
50
- reset(): Promise<void>;
51
- destroy(): Promise<void>;
52
- setAppStatus(appName: string, status: AppStatus, options?: {}): void;
53
- getMutexOfApp(appName: string): Mutex;
54
- private _bootStrapApp;
55
- bootStrapApp(appName: string, options?: {}): Promise<void>;
56
- getApp(appName: string, options?: {
57
- withOutBootStrap?: boolean;
58
- [key: string]: any;
59
- }): Promise<Application<import("./application").DefaultState, import("./application").DefaultContext>>;
60
- setAppBootstrapper(appBootstrapper: AppBootstrapper): void;
61
- getAppStatus(appName: string, defaultStatus?: AppStatus): AppStatus | null;
62
- bootMainApp(options: ApplicationOptions): Application<import("./application").DefaultState, import("./application").DefaultContext>;
63
- hasApp(appName: string): boolean;
64
- touchApp(appName: string): void;
65
- addApp(app: Application): Application<import("./application").DefaultState, import("./application").DefaultContext>;
66
- getAppsNames(): Promise<string[]>;
67
- startApp(appName: string): Promise<void>;
68
- stopApp(appName: string): Promise<void>;
69
- removeApp(appName: string): Promise<void>;
70
- subApps(): Application<import("./application").DefaultState, import("./application").DefaultContext>[];
71
- on(eventName: string | symbol, listener: (...args: any[]) => void): this;
72
- private bindAppEvents;
73
- }
74
- export {};
@@ -1,338 +0,0 @@
1
- /**
2
- * This file is part of the NocoBase (R) project.
3
- * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
- * Authors: NocoBase Team.
5
- *
6
- * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
- * For more information, please refer to: https://www.nocobase.com/agreement.
8
- */
9
-
10
- var __create = Object.create;
11
- var __defProp = Object.defineProperty;
12
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
- var __getOwnPropNames = Object.getOwnPropertyNames;
14
- var __getProtoOf = Object.getPrototypeOf;
15
- var __hasOwnProp = Object.prototype.hasOwnProperty;
16
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
17
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
18
- var __export = (target, all) => {
19
- for (var name in all)
20
- __defProp(target, name, { get: all[name], enumerable: true });
21
- };
22
- var __copyProps = (to, from, except, desc) => {
23
- if (from && typeof from === "object" || typeof from === "function") {
24
- for (let key of __getOwnPropNames(from))
25
- if (!__hasOwnProp.call(to, key) && key !== except)
26
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
27
- }
28
- return to;
29
- };
30
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
31
- // If the importer is in node compatibility mode or this is not an ESM
32
- // file that has been converted to a CommonJS file using a Babel-
33
- // compatible transform (i.e. "__esModule" has not been set), then set
34
- // "default" to the CommonJS "module.exports" for node compatibility.
35
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
36
- mod
37
- ));
38
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
39
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
40
- var app_supervisor_exports = {};
41
- __export(app_supervisor_exports, {
42
- AppSupervisor: () => AppSupervisor
43
- });
44
- module.exports = __toCommonJS(app_supervisor_exports);
45
- var import_utils = require("@nocobase/utils");
46
- var import_async_mutex = require("async-mutex");
47
- var import_events = require("events");
48
- var import_application = __toESM(require("./application"));
49
- var import_handler = require("./errors/handler");
50
- var import_p_queue = __toESM(require("p-queue"));
51
- const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
52
- bootstrapQueue;
53
- runningMode = "multiple";
54
- singleAppName = null;
55
- apps = {};
56
- lastSeenAt = /* @__PURE__ */ new Map();
57
- appErrors = {};
58
- appStatus = {};
59
- lastMaintainingMessage = {};
60
- statusBeforeCommanding = {};
61
- appMutexes = {};
62
- appBootstrapper = null;
63
- constructor() {
64
- super();
65
- if (process.env.STARTUP_SUBAPP) {
66
- this.runningMode = "single";
67
- this.singleAppName = process.env.STARTUP_SUBAPP;
68
- }
69
- this.bootstrapQueue = new import_p_queue.default({
70
- concurrency: process.env.SUBAPP_BOOTSTRAP_CONCURRENCY ? parseInt(process.env.SUBAPP_BOOTSTRAP_CONCURRENCY) : Infinity,
71
- intervalCap: process.env.SUBAPP_BOOTSTRAP_INTERVAL_CAP ? parseInt(process.env.SUBAPP_BOOTSTRAP_INTERVAL_CAP) : Infinity,
72
- interval: process.env.SUBAPP_BOOTSTRAP_INTERVAL ? parseInt(process.env.SUBAPP_BOOTSTRAP_INTERVAL) : 0,
73
- timeout: 1 * 60 * 1e3
74
- });
75
- }
76
- static getInstance() {
77
- if (!_AppSupervisor.instance) {
78
- _AppSupervisor.instance = new _AppSupervisor();
79
- }
80
- return _AppSupervisor.instance;
81
- }
82
- setAppError(appName, error) {
83
- this.appErrors[appName] = error;
84
- this.emit("appError", {
85
- appName,
86
- error
87
- });
88
- }
89
- hasAppError(appName) {
90
- return !!this.appErrors[appName];
91
- }
92
- clearAppError(appName) {
93
- delete this.appErrors[appName];
94
- }
95
- async reset() {
96
- const appNames = Object.keys(this.apps);
97
- for (const appName of appNames) {
98
- await this.removeApp(appName);
99
- }
100
- this.appBootstrapper = null;
101
- this.removeAllListeners();
102
- }
103
- async destroy() {
104
- await this.reset();
105
- _AppSupervisor.instance = null;
106
- }
107
- setAppStatus(appName, status, options = {}) {
108
- if (this.appStatus[appName] === status) {
109
- return;
110
- }
111
- this.appStatus[appName] = status;
112
- this.emit("appStatusChanged", {
113
- appName,
114
- status,
115
- options
116
- });
117
- }
118
- getMutexOfApp(appName) {
119
- if (!this.appMutexes[appName]) {
120
- this.appMutexes[appName] = new import_async_mutex.Mutex();
121
- }
122
- return this.appMutexes[appName];
123
- }
124
- async _bootStrapApp(appName, options = {}) {
125
- this.setAppStatus(appName, "initializing");
126
- if (this.appBootstrapper) {
127
- await this.appBootstrapper({
128
- appSupervisor: this,
129
- appName,
130
- options
131
- });
132
- }
133
- if (!this.hasApp(appName)) {
134
- this.setAppStatus(appName, "not_found");
135
- } else if (!this.getAppStatus(appName) || this.getAppStatus(appName) === "initializing") {
136
- this.setAppStatus(appName, "initialized");
137
- }
138
- }
139
- async bootStrapApp(appName, options = {}) {
140
- const mutex = this.getMutexOfApp(appName);
141
- try {
142
- await (0, import_async_mutex.tryAcquire)(mutex).runExclusive(async () => {
143
- if (this.hasApp(appName) && this.getAppStatus(appName) !== "preparing") {
144
- return;
145
- }
146
- if (appName === "main") {
147
- return this._bootStrapApp(appName, options);
148
- }
149
- this.setAppStatus(appName, "preparing");
150
- await this.bootstrapQueue.add(async () => {
151
- await this._bootStrapApp(appName, options);
152
- });
153
- });
154
- } catch (e) {
155
- console.log(e);
156
- if (e === import_async_mutex.E_ALREADY_LOCKED) {
157
- return;
158
- }
159
- }
160
- }
161
- async getApp(appName, options = {}) {
162
- if (!options.withOutBootStrap) {
163
- await this.bootStrapApp(appName, options);
164
- }
165
- return this.apps[appName];
166
- }
167
- setAppBootstrapper(appBootstrapper) {
168
- this.appBootstrapper = appBootstrapper;
169
- }
170
- getAppStatus(appName, defaultStatus) {
171
- const status = this.appStatus[appName];
172
- if (status === void 0 && defaultStatus !== void 0) {
173
- return defaultStatus;
174
- }
175
- return status;
176
- }
177
- bootMainApp(options) {
178
- return new import_application.default(options);
179
- }
180
- hasApp(appName) {
181
- return !!this.apps[appName];
182
- }
183
- touchApp(appName) {
184
- if (!this.hasApp(appName)) {
185
- return;
186
- }
187
- this.lastSeenAt.set(appName, Math.floor(Date.now() / 1e3));
188
- }
189
- // add app into supervisor
190
- addApp(app) {
191
- if (this.apps[app.name]) {
192
- throw new Error(`app ${app.name} already exists`);
193
- }
194
- app.logger.info(`add app ${app.name} into supervisor`, { submodule: "supervisor", method: "addApp" });
195
- this.bindAppEvents(app);
196
- this.apps[app.name] = app;
197
- this.emit("afterAppAdded", app);
198
- if (!this.getAppStatus(app.name) || this.getAppStatus(app.name) == "not_found") {
199
- this.setAppStatus(app.name, "preparing");
200
- }
201
- return app;
202
- }
203
- // get registered app names
204
- async getAppsNames() {
205
- const apps = Object.values(this.apps);
206
- return apps.map((app) => app.name);
207
- }
208
- async startApp(appName) {
209
- const appInstance = await _AppSupervisor.getInstance().getApp(appName);
210
- await appInstance.runCommand("start", "--quickstart");
211
- }
212
- async stopApp(appName) {
213
- if (!this.apps[appName]) {
214
- console.log(`app ${appName} not exists`);
215
- return;
216
- }
217
- await this.apps[appName].runCommand("stop");
218
- this.apps[appName] = null;
219
- }
220
- async removeApp(appName) {
221
- if (!this.apps[appName]) {
222
- console.log(`app ${appName} not exists`);
223
- return;
224
- }
225
- await this.apps[appName].runCommand("destroy");
226
- this.apps[appName] = null;
227
- }
228
- subApps() {
229
- return Object.values(this.apps).filter((app) => app.name !== "main");
230
- }
231
- on(eventName, listener) {
232
- const listeners = this.listeners(eventName);
233
- const listenerName = listener.name;
234
- if (listenerName !== "") {
235
- const exists = listeners.find((l) => l.name === listenerName);
236
- if (exists) {
237
- super.removeListener(eventName, exists);
238
- }
239
- }
240
- return super.on(eventName, listener);
241
- }
242
- bindAppEvents(app) {
243
- app.on("afterDestroy", () => {
244
- delete this.apps[app.name];
245
- delete this.appStatus[app.name];
246
- delete this.appErrors[app.name];
247
- delete this.lastMaintainingMessage[app.name];
248
- delete this.statusBeforeCommanding[app.name];
249
- this.lastSeenAt.delete(app.name);
250
- });
251
- app.on("maintainingMessageChanged", ({ message, maintainingStatus }) => {
252
- if (this.lastMaintainingMessage[app.name] === message) {
253
- return;
254
- }
255
- this.lastMaintainingMessage[app.name] = message;
256
- const appStatus = this.getAppStatus(app.name);
257
- if (!maintainingStatus && appStatus !== "running") {
258
- return;
259
- }
260
- this.emit("appMaintainingMessageChanged", {
261
- appName: app.name,
262
- message,
263
- status: appStatus,
264
- command: appStatus == "running" ? null : maintainingStatus.command
265
- });
266
- });
267
- app.on("__started", async (_app, options) => {
268
- const { maintainingStatus, options: startOptions } = options;
269
- if (maintainingStatus && [
270
- "install",
271
- "upgrade",
272
- "refresh",
273
- "restore",
274
- "pm.add",
275
- "pm.update",
276
- "pm.enable",
277
- "pm.disable",
278
- "pm.remove"
279
- ].includes(maintainingStatus.command.name) && !startOptions.recover) {
280
- this.setAppStatus(app.name, "running", {
281
- refresh: true
282
- });
283
- } else {
284
- this.setAppStatus(app.name, "running");
285
- }
286
- });
287
- app.on("__stopped", async () => {
288
- this.setAppStatus(app.name, "stopped");
289
- });
290
- app.on("maintaining", (maintainingStatus) => {
291
- const { status, command } = maintainingStatus;
292
- switch (status) {
293
- case "command_begin":
294
- {
295
- this.statusBeforeCommanding[app.name] = this.getAppStatus(app.name);
296
- this.setAppStatus(app.name, "commanding");
297
- }
298
- break;
299
- case "command_running":
300
- break;
301
- case "command_end":
302
- {
303
- const appStatus = this.getAppStatus(app.name);
304
- this.emit("appMaintainingStatusChanged", maintainingStatus);
305
- if (appStatus == "commanding") {
306
- this.setAppStatus(app.name, this.statusBeforeCommanding[app.name]);
307
- }
308
- }
309
- break;
310
- case "command_error":
311
- {
312
- const errorLevel = (0, import_handler.getErrorLevel)(maintainingStatus.error);
313
- if (errorLevel === "fatal") {
314
- this.setAppError(app.name, maintainingStatus.error);
315
- this.setAppStatus(app.name, "error");
316
- break;
317
- }
318
- if (errorLevel === "warn") {
319
- this.emit("appError", {
320
- appName: app.name,
321
- error: maintainingStatus.error
322
- });
323
- }
324
- this.setAppStatus(app.name, this.statusBeforeCommanding[app.name]);
325
- }
326
- break;
327
- }
328
- });
329
- }
330
- };
331
- __name(_AppSupervisor, "AppSupervisor");
332
- __publicField(_AppSupervisor, "instance");
333
- let AppSupervisor = _AppSupervisor;
334
- (0, import_utils.applyMixins)(AppSupervisor, [import_utils.AsyncEmitter]);
335
- // Annotate the CommonJS export names for ESM import in node:
336
- 0 && (module.exports = {
337
- AppSupervisor
338
- });