@nocobase/server 2.4.0-alpha.10 → 2.4.0-alpha.12

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.
@@ -50,7 +50,10 @@ const _MainOnlyAdapter = class _MainOnlyAdapter {
50
50
  return this.apps[appName];
51
51
  }
52
52
  async bootstrapApp(appName) {
53
- if (appName !== "main" || !this.apps[appName]) {
53
+ if (appName !== "main") {
54
+ return;
55
+ }
56
+ if (!this.apps[appName]) {
54
57
  this.setAppStatus(appName, "not_found");
55
58
  return;
56
59
  }
@@ -138,13 +141,16 @@ const _MainOnlyAdapter = class _MainOnlyAdapter {
138
141
  return this.removeApp("main");
139
142
  }
140
143
  setAppStatus(appName, status, options = {}) {
141
- if (this.status === status) {
144
+ if (appName !== "main" || this.status === status) {
142
145
  return;
143
146
  }
144
147
  this.status = status;
145
148
  this.supervisor.emit("appStatusChanged", { appName, status, options });
146
149
  }
147
150
  getAppStatus(appName, defaultStatus) {
151
+ if (appName !== "main") {
152
+ return "not_found";
153
+ }
148
154
  return this.status ?? defaultStatus ?? null;
149
155
  }
150
156
  hasAppError(appName) {
@@ -383,6 +383,7 @@ const _Application = class _Application extends import_koa.default {
383
383
  return this.cli._findCommand(name);
384
384
  }
385
385
  async disposeServices() {
386
+ await this.workerIdAllocator.release();
386
387
  if (this.redisConnectionManager) {
387
388
  await this.redisConnectionManager.close();
388
389
  }
@@ -395,7 +396,6 @@ const _Application = class _Application extends import_koa.default {
395
396
  if (this.telemetry.started) {
396
397
  await this.telemetry.shutdown();
397
398
  }
398
- await this.workerIdAllocator.release();
399
399
  }
400
400
  /**
401
401
  * @internal
@@ -452,7 +452,7 @@ const _Application = class _Application extends import_koa.default {
452
452
  if ((options == null ? void 0 : options.hooks) !== false) {
453
453
  await this.emitAsync("beforeLoad", this, options);
454
454
  }
455
- if (!this._instanceId) {
455
+ if (this._instanceId == null) {
456
456
  this._instanceId = await this.workerIdAllocator.getWorkerId();
457
457
  this.log.info(`allocate worker id: ${this._instanceId}`, { method: "load" });
458
458
  }
@@ -881,6 +881,7 @@ const _Application = class _Application extends import_koa.default {
881
881
  init() {
882
882
  const options = this.options;
883
883
  this._instanceId = options.instanceId;
884
+ this.snowflakeIdGenerator = void 0;
884
885
  this.initLogger(options.logger);
885
886
  this.reInitEvents();
886
887
  this.middleware = new import_utils.Toposort();
@@ -6,6 +6,11 @@
6
6
  * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
+ /** Raised when a request resolves to an app name that could never name a real application. */
10
+ export declare class InvalidAppNameError extends Error {
11
+ readonly appName: unknown;
12
+ constructor(appName: unknown);
13
+ }
9
14
  interface AppError {
10
15
  status: number;
11
16
  message: any;
@@ -37,6 +37,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
37
37
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
38
  var errors_exports = {};
39
39
  __export(errors_exports, {
40
+ InvalidAppNameError: () => InvalidAppNameError,
40
41
  applyErrorWithArgs: () => applyErrorWithArgs,
41
42
  errors: () => errors,
42
43
  getErrorWithCode: () => getErrorWithCode
@@ -44,6 +45,15 @@ __export(errors_exports, {
44
45
  module.exports = __toCommonJS(errors_exports);
45
46
  var import_app_supervisor = require("../app-supervisor");
46
47
  var import_lodash = __toESM(require("lodash"));
48
+ const _InvalidAppNameError = class _InvalidAppNameError extends Error {
49
+ constructor(appName) {
50
+ super("invalid application name");
51
+ this.appName = appName;
52
+ this.name = "InvalidAppNameError";
53
+ }
54
+ };
55
+ __name(_InvalidAppNameError, "InvalidAppNameError");
56
+ let InvalidAppNameError = _InvalidAppNameError;
47
57
  function getMaintaining(app) {
48
58
  var _a;
49
59
  return (_a = app == null ? void 0 : app.getMaintaining) == null ? void 0 : _a.call(app);
@@ -54,6 +64,11 @@ function getAppName(app) {
54
64
  }
55
65
  __name(getAppName, "getAppName");
56
66
  const errors = {
67
+ INVALID_APP_NAME: {
68
+ status: 400,
69
+ message: "invalid application name",
70
+ maintaining: false
71
+ },
57
72
  APP_ENVIRONMENT_UNAVAILABLE: {
58
73
  status: 503,
59
74
  message: /* @__PURE__ */ __name(({ appName }) => `deployment environment for application ${appName} is unavailable`, "message"),
@@ -191,6 +206,7 @@ function applyErrorWithArgs(error, options) {
191
206
  __name(applyErrorWithArgs, "applyErrorWithArgs");
192
207
  // Annotate the CommonJS export names for ESM import in node:
193
208
  0 && (module.exports = {
209
+ InvalidAppNameError,
194
210
  applyErrorWithArgs,
195
211
  errors,
196
212
  getErrorWithCode
@@ -108,6 +108,7 @@ export declare class Gateway extends EventEmitter {
108
108
  requestHandler(req: IncomingMessage, res: ServerResponse): Promise<void>;
109
109
  getAppSelectorMiddlewares(): Toposort<AppSelectorMiddleware>;
110
110
  getRequestHandleAppName(req: IncomingMessage | IncomingRequest): Promise<string>;
111
+ private handleUpgrade;
111
112
  getCallback(): any;
112
113
  watch(): Promise<void>;
113
114
  run(options: RunOptions): Promise<void>;
@@ -72,6 +72,15 @@ var import_node_worker_threads = require("node:worker_threads");
72
72
  var import_node_process = __toESM(require("node:process"));
73
73
  var import_utils4 = require("./utils");
74
74
  const compress = (0, import_node_util.promisify)((0, import_compression.default)());
75
+ const APP_NAME_PATTERN = /^[A-Za-z0-9_][A-Za-z0-9_-]*$/;
76
+ const MAX_APP_NAME_LENGTH = 255;
77
+ function isValidAppName(name) {
78
+ if (typeof name !== "string" || name.length > MAX_APP_NAME_LENGTH || !APP_NAME_PATTERN.test(name)) {
79
+ return false;
80
+ }
81
+ return !(name in Object.prototype);
82
+ }
83
+ __name(isValidAppName, "isValidAppName");
75
84
  function normalizeBasePath(path = "") {
76
85
  const normalized = path.replace(/\/+/g, "/").replace(/\/$/, "");
77
86
  return normalized || "/";
@@ -272,6 +281,9 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
272
281
  this.emit("appSelectorChanged");
273
282
  }
274
283
  getLogger(appName, res) {
284
+ if (!isValidAppName(appName)) {
285
+ throw new Error("Invalid app name");
286
+ }
275
287
  const reqId = (0, import_crypto.randomUUID)();
276
288
  res.setHeader("X-Request-Id", reqId);
277
289
  let logger = this.loggers.get(appName);
@@ -295,7 +307,9 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
295
307
  res.end(JSON.stringify({ error }));
296
308
  }
297
309
  responseErrorWithCode(code, res, options) {
298
- const log = this.getLogger(options.appName, res);
310
+ const appName = options == null ? void 0 : options.appName;
311
+ const loggerName = isValidAppName(appName) && import_app_supervisor.AppSupervisor.getInstance().hasApp(appName) ? appName : "main";
312
+ const log = this.getLogger(loggerName, res);
299
313
  const error = (0, import_errors.applyErrorWithArgs)((0, import_errors.getErrorWithCode)(code), options);
300
314
  log.error(error.message, {
301
315
  method: "responseErrorWithCode",
@@ -613,6 +627,10 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
613
627
  try {
614
628
  handleApp = await this.getRequestHandleAppName(req);
615
629
  } catch (error) {
630
+ if (error instanceof import_errors.InvalidAppNameError) {
631
+ this.responseErrorWithCode("INVALID_APP_NAME", res, {});
632
+ return;
633
+ }
616
634
  this.getLogger("main", res).error("Failed to get handle app name", { error });
617
635
  this.responseErrorWithCode("APP_INITIALIZING", res, { appName: handleApp });
618
636
  return;
@@ -828,8 +846,32 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
828
846
  if (!ctx.resolvedAppName) {
829
847
  ctx.resolvedAppName = "main";
830
848
  }
849
+ if (!isValidAppName(ctx.resolvedAppName)) {
850
+ throw new import_errors.InvalidAppNameError(ctx.resolvedAppName);
851
+ }
831
852
  return ctx.resolvedAppName;
832
853
  }
854
+ async handleUpgrade(request, socket, head) {
855
+ const isProxy = await import_app_supervisor.AppSupervisor.getInstance().proxyWs(request, socket, head);
856
+ if (isProxy) {
857
+ return;
858
+ }
859
+ const appInstance = await import_app_supervisor.AppSupervisor.getInstance().getApp("main");
860
+ for (const handle of _Gateway.wsServers) {
861
+ const result = await handle(request, socket, head, appInstance);
862
+ if (result !== false) {
863
+ return;
864
+ }
865
+ }
866
+ const { pathname } = (0, import_url.parse)(request.url);
867
+ if (pathname === import_node_process.default.env.WS_PATH) {
868
+ this.wsServer.wss.handleUpgrade(request, socket, head, (ws) => {
869
+ this.wsServer.wss.emit("connection", ws, request);
870
+ });
871
+ } else {
872
+ socket.destroy();
873
+ }
874
+ }
833
875
  getCallback() {
834
876
  return this.requestHandler.bind(this);
835
877
  }
@@ -946,26 +988,11 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
946
988
  this.getCallback()(req, res);
947
989
  });
948
990
  this.wsServer = new import_ws_server.WSServer();
949
- this.server.on("upgrade", async (request, socket, head) => {
950
- const isProxy = await import_app_supervisor.AppSupervisor.getInstance().proxyWs(request, socket, head);
951
- if (isProxy) {
952
- return;
953
- }
954
- const appInstance = await import_app_supervisor.AppSupervisor.getInstance().getApp("main");
955
- for (const handle of _Gateway.wsServers) {
956
- const result = await handle(request, socket, head, appInstance);
957
- if (result !== false) {
958
- return;
959
- }
960
- }
961
- const { pathname } = (0, import_url.parse)(request.url);
962
- if (pathname === import_node_process.default.env.WS_PATH) {
963
- this.wsServer.wss.handleUpgrade(request, socket, head, (ws) => {
964
- this.wsServer.wss.emit("connection", ws, request);
965
- });
966
- } else {
991
+ this.server.on("upgrade", (request, socket, head) => {
992
+ this.handleUpgrade(request, socket, head).catch((error) => {
993
+ import_app_supervisor.AppSupervisor.getInstance().logger.error("Failed to handle websocket upgrade", { error });
967
994
  socket.destroy();
968
- }
995
+ });
969
996
  });
970
997
  this.server.listen(this.port, this.host, () => {
971
998
  console.log(`Gateway HTTP Server running at http://${this.host}:${this.port}/`);
@@ -32,6 +32,11 @@ export declare class WSServer extends EventEmitter {
32
32
  addNewConnection(ws: WebSocketWithId, request: IncomingMessage): WebSocketClient;
33
33
  setClientTag(clientId: string, tagKey: string, tagValue: string): void;
34
34
  removeClientTag(clientId: string, tagKey: string): void;
35
+ /**
36
+ * Drop a connection whose url or headers resolve to an app name that can never exist. Closing beats tagging it with
37
+ * the raw value: the tag would end up as a map key and would bootstrap an app on every selector change.
38
+ */
39
+ private rejectInvalidAppName;
35
40
  setClientApp(client: WebSocketClient): Promise<void>;
36
41
  removeConnection(id: string): void;
37
42
  sendMessageToConnection(client: WebSocketClient, sendMessage: object): void;
@@ -80,10 +80,19 @@ const _WSServer = class _WSServer extends import_events.default {
80
80
  });
81
81
  import_gateway.Gateway.getInstance().on("appSelectorChanged", () => {
82
82
  this.loopThroughConnections(async (client) => {
83
- const handleAppName = await import_gateway.Gateway.getInstance().getRequestHandleAppName({
84
- url: client.url,
85
- headers: client.headers
86
- });
83
+ let handleAppName;
84
+ try {
85
+ handleAppName = await import_gateway.Gateway.getInstance().getRequestHandleAppName({
86
+ url: client.url,
87
+ headers: client.headers
88
+ });
89
+ } catch (error) {
90
+ if (!(error instanceof import_errors.InvalidAppNameError)) {
91
+ throw error;
92
+ }
93
+ this.rejectInvalidAppName(client);
94
+ return;
95
+ }
87
96
  for (const tag of client.tags) {
88
97
  if (tag.startsWith("app#")) {
89
98
  client.tags.delete(tag);
@@ -238,12 +247,29 @@ const _WSServer = class _WSServer extends import_events.default {
238
247
  }
239
248
  });
240
249
  }
250
+ /**
251
+ * Drop a connection whose url or headers resolve to an app name that can never exist. Closing beats tagging it with
252
+ * the raw value: the tag would end up as a map key and would bootstrap an app on every selector change.
253
+ */
254
+ rejectInvalidAppName(client) {
255
+ client.ws.close(1008, "INVALID_APP_NAME");
256
+ this.removeConnection(client.id);
257
+ }
241
258
  async setClientApp(client) {
242
259
  const req = {
243
260
  url: client.url,
244
261
  headers: client.headers
245
262
  };
246
- const handleAppName = await import_gateway.Gateway.getInstance().getRequestHandleAppName(req);
263
+ let handleAppName;
264
+ try {
265
+ handleAppName = await import_gateway.Gateway.getInstance().getRequestHandleAppName(req);
266
+ } catch (error) {
267
+ if (!(error instanceof import_errors.InvalidAppNameError)) {
268
+ throw error;
269
+ }
270
+ this.rejectInvalidAppName(client);
271
+ return;
272
+ }
247
273
  client.app = handleAppName;
248
274
  console.log(`client tags: app#${handleAppName}`);
249
275
  client.tags.add(`app#${handleAppName}`);
@@ -70,7 +70,7 @@ const deps = {
70
70
  "pg-hstore": "2.x",
71
71
  sqlite3: "5.x",
72
72
  supertest: "6.x",
73
- axios: "1.7.x",
73
+ axios: "1.20.x",
74
74
  "@emotion/css": "11.x",
75
75
  ahooks: "3.x",
76
76
  lodash: "4.x",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "2.4.0-alpha.10",
3
+ "version": "2.4.0-alpha.12",
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.10",
14
- "@nocobase/actions": "2.4.0-alpha.10",
15
- "@nocobase/ai": "2.4.0-alpha.10",
16
- "@nocobase/auth": "2.4.0-alpha.10",
17
- "@nocobase/cache": "2.4.0-alpha.10",
18
- "@nocobase/data-source-manager": "2.4.0-alpha.10",
19
- "@nocobase/database": "2.4.0-alpha.10",
20
- "@nocobase/evaluators": "2.4.0-alpha.10",
21
- "@nocobase/lock-manager": "2.4.0-alpha.10",
22
- "@nocobase/logger": "2.4.0-alpha.10",
23
- "@nocobase/resourcer": "2.4.0-alpha.10",
24
- "@nocobase/sdk": "2.4.0-alpha.10",
25
- "@nocobase/snowflake-id": "2.4.0-alpha.10",
26
- "@nocobase/telemetry": "2.4.0-alpha.10",
27
- "@nocobase/utils": "2.4.0-alpha.10",
13
+ "@nocobase/acl": "2.4.0-alpha.12",
14
+ "@nocobase/actions": "2.4.0-alpha.12",
15
+ "@nocobase/ai": "2.4.0-alpha.12",
16
+ "@nocobase/auth": "2.4.0-alpha.12",
17
+ "@nocobase/cache": "2.4.0-alpha.12",
18
+ "@nocobase/data-source-manager": "2.4.0-alpha.12",
19
+ "@nocobase/database": "2.4.0-alpha.12",
20
+ "@nocobase/evaluators": "2.4.0-alpha.12",
21
+ "@nocobase/lock-manager": "2.4.0-alpha.12",
22
+ "@nocobase/logger": "2.4.0-alpha.12",
23
+ "@nocobase/resourcer": "2.4.0-alpha.12",
24
+ "@nocobase/sdk": "2.4.0-alpha.12",
25
+ "@nocobase/snowflake-id": "2.4.0-alpha.12",
26
+ "@nocobase/telemetry": "2.4.0-alpha.12",
27
+ "@nocobase/utils": "2.4.0-alpha.12",
28
28
  "@types/ini": "^1.3.31",
29
29
  "@types/koa-send": "^4.1.3",
30
30
  "@types/multer": "^1.4.12",
@@ -60,5 +60,5 @@
60
60
  "@types/serve-handler": "^6.1.1",
61
61
  "@types/ws": "^8.5.5"
62
62
  },
63
- "gitHead": "c869865c0413fbe4ef85b26be1f2b8d7078c38df"
63
+ "gitHead": "de0ec0596d238752ed56bae95600a59395c920ea"
64
64
  }