@nocobase/server 2.4.0-alpha.5 → 2.4.0-alpha.7

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.
@@ -53,6 +53,7 @@ export declare class AppSupervisor extends EventEmitter implements AsyncEmitter
53
53
  private appSsoIssuer?;
54
54
  appOptionsFactory: AppOptionsFactory;
55
55
  private environmentHeartbeatInterval;
56
+ private environmentHeartbeatTimeout;
56
57
  private environmentHeartbeatTimer;
57
58
  private constructor();
58
59
  private resolveDiscoveryAdapterName;
@@ -163,11 +164,13 @@ export declare class AppSupervisor extends EventEmitter implements AsyncEmitter
163
164
  proxyWeb(appName: string, req: IncomingMessage, res: ServerResponse): Promise<boolean>;
164
165
  proxyWs(req: IncomingMessage, socket: any, head: Buffer): Promise<boolean>;
165
166
  registerEnvironment(mainApp: Application): Promise<void>;
167
+ private getEnvironmentInfo;
166
168
  unregisterEnvironment(): Promise<void>;
167
169
  private normalizeEnvInfo;
168
170
  listEnvironments(): Promise<EnvironmentInfo[]>;
169
171
  getEnvironment(environmentName: string): Promise<EnvironmentInfo>;
170
- heartbeatEnvironment(): Promise<void>;
172
+ heartbeatEnvironment(mainApp: Application): Promise<void>;
173
+ private stopEnvironmentHeartbeat;
171
174
  dispatchCommand(command: ProcessCommand): Promise<void>;
172
175
  registerCommandHandler(mainApp: Application): void;
173
176
  sendSyncMessage(mainApp: Application, message: {
@@ -74,6 +74,7 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
74
74
  appSsoIssuer;
75
75
  appOptionsFactory = import_app_options_factory.appOptionsFactory;
76
76
  environmentHeartbeatInterval = 2 * 60 * 1e3;
77
+ environmentHeartbeatTimeout = 5 * 60 * 1e3;
77
78
  environmentHeartbeatTimer = null;
78
79
  constructor() {
79
80
  super();
@@ -282,13 +283,11 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
282
283
  }
283
284
  async reset() {
284
285
  var _a, _b, _c, _d;
286
+ this.stopEnvironmentHeartbeat();
285
287
  await this.processAdapter.removeAllApps();
286
288
  await ((_b = (_a = this.discoveryAdapter).dispose) == null ? void 0 : _b.call(_a));
287
289
  await ((_d = (_c = this.commandAdapter) == null ? void 0 : _c.dispose) == null ? void 0 : _d.call(_c));
288
290
  this.appManifests.clear();
289
- if (this.environmentHeartbeatTimer) {
290
- this.environmentHeartbeatTimer = null;
291
- }
292
291
  this.removeAllListeners();
293
292
  this.logger.close();
294
293
  }
@@ -604,24 +603,25 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
604
603
  if (!this.environmentName || typeof this.discoveryAdapter.registerEnvironment !== "function") {
605
604
  return;
606
605
  }
607
- const registered = await this.discoveryAdapter.registerEnvironment({
606
+ const registered = await this.discoveryAdapter.registerEnvironment(this.getEnvironmentInfo(mainApp));
607
+ if (registered) {
608
+ this.heartbeatEnvironment(mainApp);
609
+ }
610
+ }
611
+ getEnvironmentInfo(mainApp) {
612
+ return {
608
613
  name: this.environmentName,
609
614
  url: this.environmentUrl || "",
610
615
  proxyUrl: this.environmentProxyUrl || this.environmentUrl || "",
611
616
  appVersion: mainApp.getPackageVersion(),
612
617
  lastHeartbeatAt: Date.now()
613
- });
614
- if (registered) {
615
- this.heartbeatEnvironment();
616
- }
618
+ };
617
619
  }
618
620
  async unregisterEnvironment() {
621
+ this.stopEnvironmentHeartbeat();
619
622
  if (this.environmentName && typeof this.discoveryAdapter.unregisterEnvironment === "function") {
620
623
  await this.discoveryAdapter.unregisterEnvironment();
621
624
  }
622
- if (this.environmentHeartbeatTimer) {
623
- this.environmentHeartbeatTimer = null;
624
- }
625
625
  }
626
626
  normalizeEnvInfo(environment) {
627
627
  if (typeof environment.available === "boolean") {
@@ -634,7 +634,7 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
634
634
  available: false
635
635
  };
636
636
  }
637
- const available = Date.now() - lastHeartbeatAt <= this.environmentHeartbeatInterval;
637
+ const available = Date.now() - lastHeartbeatAt <= this.environmentHeartbeatTimeout;
638
638
  return {
639
639
  ...environment,
640
640
  available
@@ -657,17 +657,26 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
657
657
  }
658
658
  return this.normalizeEnvInfo(environment);
659
659
  }
660
- async heartbeatEnvironment() {
660
+ async heartbeatEnvironment(mainApp) {
661
661
  if (typeof this.discoveryAdapter.heartbeatEnvironment !== "function") {
662
662
  return;
663
663
  }
664
664
  if (this.environmentHeartbeatTimer) {
665
665
  return;
666
666
  }
667
- this.environmentHeartbeatTimer = setInterval(
668
- () => this.discoveryAdapter.heartbeatEnvironment(),
669
- this.environmentHeartbeatInterval
670
- );
667
+ this.environmentHeartbeatTimer = setInterval(async () => {
668
+ try {
669
+ await this.discoveryAdapter.heartbeatEnvironment(this.getEnvironmentInfo(mainApp));
670
+ } catch (error) {
671
+ this.logger.error(error instanceof Error ? error.message : String(error), { method: "heartbeatEnvironment" });
672
+ }
673
+ }, this.environmentHeartbeatInterval);
674
+ }
675
+ stopEnvironmentHeartbeat() {
676
+ if (this.environmentHeartbeatTimer) {
677
+ clearInterval(this.environmentHeartbeatTimer);
678
+ this.environmentHeartbeatTimer = null;
679
+ }
671
680
  }
672
681
  async dispatchCommand(command) {
673
682
  var _a;
@@ -132,7 +132,7 @@ export interface AppDiscoveryAdapter {
132
132
  unregisterEnvironment?(): Promise<void>;
133
133
  listEnvironments?(): Promise<EnvironmentInfo[]>;
134
134
  getEnvironment?(environmentName: string): Promise<EnvironmentInfo | null>;
135
- heartbeatEnvironment?(): Promise<void>;
135
+ heartbeatEnvironment?(environment: EnvironmentInfo): Promise<void>;
136
136
  getBootstrapLock?(appName: string): Promise<BootstrapLock | null> | BootstrapLock | null;
137
137
  proxyWeb?(appName: string, req: IncomingMessage, res: ServerResponse): Promise<boolean>;
138
138
  proxyWs?(req: IncomingMessage, socket: any, head: Buffer): Promise<boolean>;
@@ -8,10 +8,14 @@
8
8
  */
9
9
  export declare class Environment {
10
10
  private vars;
11
- setVariable(key: string, value: string): void;
11
+ private secretKeys;
12
+ setVariable(key: string, value: string, options?: {
13
+ isSecret?: boolean;
14
+ }): void;
12
15
  removeVariable(key: string): void;
13
16
  getVariablesAndSecrets(): {};
14
17
  getVariables(): {};
18
+ getNonSecretVariables(): {};
15
19
  renderJsonTemplate(template: any, options?: {
16
20
  omit?: string[];
17
21
  }): any;
@@ -44,11 +44,18 @@ var import_utils = require("@nocobase/utils");
44
44
  var import_lodash = __toESM(require("lodash"));
45
45
  const _Environment = class _Environment {
46
46
  vars = {};
47
- setVariable(key, value) {
47
+ secretKeys = /* @__PURE__ */ new Set();
48
+ setVariable(key, value, options) {
48
49
  this.vars[key] = value;
50
+ if ((options == null ? void 0 : options.isSecret) === true) {
51
+ this.secretKeys.add(key);
52
+ } else if ((options == null ? void 0 : options.isSecret) === false) {
53
+ this.secretKeys.delete(key);
54
+ }
49
55
  }
50
56
  removeVariable(key) {
51
57
  delete this.vars[key];
58
+ this.secretKeys.delete(key);
52
59
  }
53
60
  getVariablesAndSecrets() {
54
61
  return this.vars;
@@ -56,6 +63,15 @@ const _Environment = class _Environment {
56
63
  getVariables() {
57
64
  return this.vars;
58
65
  }
66
+ getNonSecretVariables() {
67
+ const result = {};
68
+ for (const key of Object.keys(this.vars)) {
69
+ if (!this.secretKeys.has(key)) {
70
+ result[key] = this.vars[key];
71
+ }
72
+ }
73
+ return result;
74
+ }
59
75
  renderJsonTemplate(template, options) {
60
76
  if (options == null ? void 0 : options.omit) {
61
77
  const omitTemplate = import_lodash.default.omit(template, options.omit);
@@ -54,6 +54,11 @@ function getAppName(app) {
54
54
  }
55
55
  __name(getAppName, "getAppName");
56
56
  const errors = {
57
+ APP_ENVIRONMENT_UNAVAILABLE: {
58
+ status: 503,
59
+ message: /* @__PURE__ */ __name(({ appName }) => `deployment environment for application ${appName} is unavailable`, "message"),
60
+ maintaining: true
61
+ },
57
62
  APP_NOT_FOUND: {
58
63
  status: 404,
59
64
  message: /* @__PURE__ */ __name(({ appName }) => `application ${appName} not found`, "message"),
@@ -122,7 +122,17 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
122
122
  const internalUrl = req.url;
123
123
  req.url = this.getOriginalRequestUrl(req);
124
124
  try {
125
- return await supervisor.proxyWeb(appName, req, res);
125
+ const proxied = await supervisor.proxyWeb(appName, req, res);
126
+ const adapter = supervisor.getDiscoveryAdapter();
127
+ if (!proxied && import_node_process.default.env.APP_MODE === "supervisor" && typeof adapter.proxyWeb === "function") {
128
+ let code = "APP_ENVIRONMENT_UNAVAILABLE";
129
+ if (typeof adapter.getAppModel === "function" && !await supervisor.getAppModel(appName)) {
130
+ code = "APP_NOT_FOUND";
131
+ }
132
+ this.responseErrorWithCode(code, res, { appName });
133
+ return true;
134
+ }
135
+ return proxied;
126
136
  } finally {
127
137
  req.url = internalUrl;
128
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "2.4.0-alpha.5",
3
+ "version": "2.4.0-alpha.7",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "Apache-2.0",
@@ -10,21 +10,21 @@
10
10
  "@koa/cors": "^5.0.0",
11
11
  "@koa/multer": "^3.1.0",
12
12
  "@koa/router": "^13.1.0",
13
- "@nocobase/acl": "2.4.0-alpha.5",
14
- "@nocobase/actions": "2.4.0-alpha.5",
15
- "@nocobase/ai": "2.4.0-alpha.5",
16
- "@nocobase/auth": "2.4.0-alpha.5",
17
- "@nocobase/cache": "2.4.0-alpha.5",
18
- "@nocobase/data-source-manager": "2.4.0-alpha.5",
19
- "@nocobase/database": "2.4.0-alpha.5",
20
- "@nocobase/evaluators": "2.4.0-alpha.5",
21
- "@nocobase/lock-manager": "2.4.0-alpha.5",
22
- "@nocobase/logger": "2.4.0-alpha.5",
23
- "@nocobase/resourcer": "2.4.0-alpha.5",
24
- "@nocobase/sdk": "2.4.0-alpha.5",
25
- "@nocobase/snowflake-id": "2.4.0-alpha.5",
26
- "@nocobase/telemetry": "2.4.0-alpha.5",
27
- "@nocobase/utils": "2.4.0-alpha.5",
13
+ "@nocobase/acl": "2.4.0-alpha.7",
14
+ "@nocobase/actions": "2.4.0-alpha.7",
15
+ "@nocobase/ai": "2.4.0-alpha.7",
16
+ "@nocobase/auth": "2.4.0-alpha.7",
17
+ "@nocobase/cache": "2.4.0-alpha.7",
18
+ "@nocobase/data-source-manager": "2.4.0-alpha.7",
19
+ "@nocobase/database": "2.4.0-alpha.7",
20
+ "@nocobase/evaluators": "2.4.0-alpha.7",
21
+ "@nocobase/lock-manager": "2.4.0-alpha.7",
22
+ "@nocobase/logger": "2.4.0-alpha.7",
23
+ "@nocobase/resourcer": "2.4.0-alpha.7",
24
+ "@nocobase/sdk": "2.4.0-alpha.7",
25
+ "@nocobase/snowflake-id": "2.4.0-alpha.7",
26
+ "@nocobase/telemetry": "2.4.0-alpha.7",
27
+ "@nocobase/utils": "2.4.0-alpha.7",
28
28
  "@types/decompress": "4.2.7",
29
29
  "@types/ini": "^1.3.31",
30
30
  "@types/koa-send": "^4.1.3",
@@ -61,5 +61,5 @@
61
61
  "@types/serve-handler": "^6.1.1",
62
62
  "@types/ws": "^8.5.5"
63
63
  },
64
- "gitHead": "ce4b0948641b13d2964c44905cb30bebe877a657"
64
+ "gitHead": "bb018bfc8bdcc0f399244d2f248c55e51ceb2d1e"
65
65
  }