@nocobase/server 1.6.0-alpha.3 → 1.6.0-alpha.31

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.
@@ -0,0 +1,20 @@
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 Application from './application';
11
+ export declare class AesEncryptor {
12
+ private key;
13
+ constructor(key: Buffer);
14
+ encrypt(text: string): Promise<string>;
15
+ decrypt(encryptedText: string): Promise<string>;
16
+ static getOrGenerateKey(keyFilePath: string): Promise<Buffer>;
17
+ static getKeyPath(appName: string): Promise<string>;
18
+ static create(app: Application): Promise<AesEncryptor>;
19
+ }
20
+ export default AesEncryptor;
@@ -0,0 +1,126 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var aes_encryptor_exports = {};
39
+ __export(aes_encryptor_exports, {
40
+ AesEncryptor: () => AesEncryptor,
41
+ default: () => aes_encryptor_default
42
+ });
43
+ module.exports = __toCommonJS(aes_encryptor_exports);
44
+ var import_crypto = __toESM(require("crypto"));
45
+ var import_fs_extra = __toESM(require("fs-extra"));
46
+ var import_path = __toESM(require("path"));
47
+ const _AesEncryptor = class _AesEncryptor {
48
+ key;
49
+ constructor(key) {
50
+ if (key.length !== 32) {
51
+ throw new Error("Key must be 32 bytes for AES-256 encryption.");
52
+ }
53
+ this.key = key;
54
+ }
55
+ async encrypt(text) {
56
+ return new Promise((resolve, reject) => {
57
+ try {
58
+ const iv = import_crypto.default.randomBytes(16);
59
+ const cipher = import_crypto.default.createCipheriv("aes-256-cbc", this.key, iv);
60
+ const encrypted = Buffer.concat([cipher.update(Buffer.from(text, "utf8")), cipher.final()]);
61
+ resolve(iv.toString("hex") + encrypted.toString("hex"));
62
+ } catch (error) {
63
+ reject(error);
64
+ }
65
+ });
66
+ }
67
+ async decrypt(encryptedText) {
68
+ return new Promise((resolve, reject) => {
69
+ try {
70
+ const iv = Buffer.from(encryptedText.slice(0, 32), "hex");
71
+ const encrypted = Buffer.from(encryptedText.slice(32), "hex");
72
+ const decipher = import_crypto.default.createDecipheriv("aes-256-cbc", this.key, iv);
73
+ const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
74
+ resolve(decrypted.toString("utf8"));
75
+ } catch (error) {
76
+ reject(error);
77
+ }
78
+ });
79
+ }
80
+ static async getOrGenerateKey(keyFilePath) {
81
+ try {
82
+ const key = await import_fs_extra.default.readFile(keyFilePath);
83
+ if (key.length !== 32) {
84
+ throw new Error("Invalid key length in file.");
85
+ }
86
+ return key;
87
+ } catch (error) {
88
+ if (error.code === "ENOENT") {
89
+ const key = import_crypto.default.randomBytes(32);
90
+ await import_fs_extra.default.mkdir(import_path.default.dirname(keyFilePath), { recursive: true });
91
+ await import_fs_extra.default.writeFile(keyFilePath, key);
92
+ return key;
93
+ } else {
94
+ throw new Error(`Failed to load key: ${error.message}`);
95
+ }
96
+ }
97
+ }
98
+ static async getKeyPath(appName) {
99
+ const appKeyPath = import_path.default.resolve(process.cwd(), "storage", "apps", appName, "aes_key.dat");
100
+ const appKeyExists = await import_fs_extra.default.exists(appKeyPath);
101
+ if (appKeyExists) {
102
+ return appKeyPath;
103
+ }
104
+ const envKeyPath = import_path.default.resolve(process.cwd(), "storage", "environment-variables", appName, "aes_key.dat");
105
+ const envKeyExists = await import_fs_extra.default.exists(envKeyPath);
106
+ if (envKeyExists) {
107
+ return envKeyPath;
108
+ }
109
+ return appKeyPath;
110
+ }
111
+ static async create(app) {
112
+ let key = process.env.APP_AES_SECRET_KEY;
113
+ if (!key) {
114
+ const keyPath = await this.getKeyPath(app.name);
115
+ key = await _AesEncryptor.getOrGenerateKey(keyPath);
116
+ }
117
+ return new _AesEncryptor(key);
118
+ }
119
+ };
120
+ __name(_AesEncryptor, "AesEncryptor");
121
+ let AesEncryptor = _AesEncryptor;
122
+ var aes_encryptor_default = AesEncryptor;
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ AesEncryptor
126
+ });
@@ -15,8 +15,8 @@ import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/databas
15
15
  import { Logger, LoggerOptions, RequestLoggerOptions, SystemLogger, SystemLoggerOptions } from '@nocobase/logger';
16
16
  import { ResourceOptions, Resourcer } from '@nocobase/resourcer';
17
17
  import { Telemetry, TelemetryOptions } from '@nocobase/telemetry';
18
- import { AsyncEmitter, ToposortOptions } from '@nocobase/utils';
19
18
  import { LockManager, LockManagerOptions } from '@nocobase/lock-manager';
19
+ import { AsyncEmitter, ToposortOptions } from '@nocobase/utils';
20
20
  import { Command, CommandOptions, ParseOptions } from 'commander';
21
21
  import { IncomingMessage, ServerResponse } from 'http';
22
22
  import { i18n, InitOptions } from 'i18next';
@@ -31,7 +31,10 @@ import { Plugin } from './plugin';
31
31
  import { InstallOptions, PluginManager } from './plugin-manager';
32
32
  import { PubSubManager, PubSubManagerOptions } from './pub-sub-manager';
33
33
  import { SyncMessageManager } from './sync-message-manager';
34
+ import AesEncryptor from './aes-encryptor';
34
35
  import { AuditManager } from './audit-manager';
36
+ import { Environment } from './environment';
37
+ import { ServiceContainer } from './service-container';
35
38
  export type PluginType = string | typeof Plugin;
36
39
  export type PluginConfiguration = PluginType | [PluginType, any];
37
40
  export interface ResourceManagerOptions {
@@ -169,6 +172,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
169
172
  private _maintainingCommandStatus;
170
173
  private _maintainingStatusBeforeCommand;
171
174
  private _actionCommand;
175
+ container: ServiceContainer;
172
176
  lockManager: LockManager;
173
177
  constructor(options: ApplicationOptions);
174
178
  private static staticCommands;
@@ -193,6 +197,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
193
197
  * @internal
194
198
  */
195
199
  get maintainingMessage(): string;
200
+ private _env;
201
+ get environment(): Environment;
196
202
  protected _cronJobManager: CronJobManager;
197
203
  get cronJobManager(): CronJobManager;
198
204
  get mainDataSource(): SequelizeDataSource;
@@ -238,6 +244,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
238
244
  get name(): string;
239
245
  protected _dataSourceManager: DataSourceManager;
240
246
  get dataSourceManager(): DataSourceManager;
247
+ protected _aesEncryptor: AesEncryptor;
248
+ get aesEncryptor(): AesEncryptor;
241
249
  /**
242
250
  * @internal
243
251
  */
@@ -49,8 +49,8 @@ var import_data_source_manager = require("@nocobase/data-source-manager");
49
49
  var import_database = __toESM(require("@nocobase/database"));
50
50
  var import_logger = require("@nocobase/logger");
51
51
  var import_telemetry = require("@nocobase/telemetry");
52
- var import_utils = require("@nocobase/utils");
53
52
  var import_lock_manager = require("@nocobase/lock-manager");
53
+ var import_utils = require("@nocobase/utils");
54
54
  var import_crypto = require("crypto");
55
55
  var import_glob = __toESM(require("glob"));
56
56
  var import_koa = __toESM(require("koa"));
@@ -76,7 +76,11 @@ var import_plugin_manager = require("./plugin-manager");
76
76
  var import_pub_sub_manager = require("./pub-sub-manager");
77
77
  var import_sync_message_manager = require("./sync-message-manager");
78
78
  var import_package = __toESM(require("../package.json"));
79
+ var import_available_action = require("./acl/available-action");
80
+ var import_aes_encryptor = __toESM(require("./aes-encryptor"));
79
81
  var import_audit_manager = require("./audit-manager");
82
+ var import_environment = require("./environment");
83
+ var import_service_container = require("./service-container");
80
84
  const _Application = class _Application extends import_koa.default {
81
85
  constructor(options) {
82
86
  super();
@@ -125,6 +129,7 @@ const _Application = class _Application extends import_koa.default {
125
129
  _maintainingCommandStatus;
126
130
  _maintainingStatusBeforeCommand;
127
131
  _actionCommand;
132
+ container = new import_service_container.ServiceContainer();
128
133
  lockManager;
129
134
  static addCommand(callback) {
130
135
  this.staticCommands.push(callback);
@@ -161,6 +166,10 @@ const _Application = class _Application extends import_koa.default {
161
166
  get maintainingMessage() {
162
167
  return this._maintainingMessage;
163
168
  }
169
+ _env;
170
+ get environment() {
171
+ return this._env;
172
+ }
164
173
  _cronJobManager;
165
174
  get cronJobManager() {
166
175
  return this._cronJobManager;
@@ -250,6 +259,10 @@ const _Application = class _Application extends import_koa.default {
250
259
  get dataSourceManager() {
251
260
  return this._dataSourceManager;
252
261
  }
262
+ _aesEncryptor;
263
+ get aesEncryptor() {
264
+ return this._aesEncryptor;
265
+ }
253
266
  /**
254
267
  * @internal
255
268
  */
@@ -370,7 +383,10 @@ const _Application = class _Application extends import_koa.default {
370
383
  this._loaded = false;
371
384
  }
372
385
  async createCacheManager() {
373
- this._cacheManager = await (0, import_cache2.createCacheManager)(this, this.options.cacheManager);
386
+ this._cacheManager = await (0, import_cache2.createCacheManager)(this, {
387
+ prefix: this.name,
388
+ ...this.options.cacheManager
389
+ });
374
390
  return this._cacheManager;
375
391
  }
376
392
  async load(options) {
@@ -393,6 +409,7 @@ const _Application = class _Application extends import_koa.default {
393
409
  await oldDb.close();
394
410
  }
395
411
  }
412
+ this._aesEncryptor = await import_aes_encryptor.default.create(this);
396
413
  if (this.cacheManager) {
397
414
  await this.cacheManager.close();
398
415
  }
@@ -830,6 +847,7 @@ const _Application = class _Application extends import_koa.default {
830
847
  }
831
848
  this.createMainDataSource(options);
832
849
  this._cronJobManager = new import_cron_job_manager.CronJobManager(this);
850
+ this._env = new import_environment.Environment();
833
851
  this._cli = this.createCLI();
834
852
  this._i18n = (0, import_helper.createI18n)(options);
835
853
  this.pubSubManager = (0, import_pub_sub_manager.createPubSubManager)(this, options.pubSubManager);
@@ -863,6 +881,13 @@ const _Application = class _Application extends import_koa.default {
863
881
  name: "auth",
864
882
  actions: import_auth.actions
865
883
  });
884
+ this._dataSourceManager.afterAddDataSource((dataSource) => {
885
+ if (dataSource.collectionManager instanceof import_data_source_manager.SequelizeCollectionManager) {
886
+ for (const [actionName, actionParams] of Object.entries(import_available_action.availableActions)) {
887
+ dataSource.acl.setAvailableAction(actionName, actionParams);
888
+ }
889
+ }
890
+ });
866
891
  this._dataSourceManager.use(this._authManager.middleware(), { tag: "auth" });
867
892
  this._dataSourceManager.use(import_validate_filter_params.default, { tag: "validate-filter-params", before: ["auth"] });
868
893
  this._dataSourceManager.use(import_middlewares.parseVariables, {
@@ -922,6 +947,7 @@ const _Application = class _Application extends import_koa.default {
922
947
  },
923
948
  logger: this._logger.child({ module: "database" })
924
949
  });
950
+ db.setMaxListeners(100);
925
951
  return db;
926
952
  }
927
953
  };
@@ -260,6 +260,8 @@ const _AuditManager = class _AuditManager {
260
260
  const auditLog = this.formatAuditData(ctx);
261
261
  auditLog.uuid = reqId;
262
262
  auditLog.status = ctx.status;
263
+ const defaultMetaData = await this.getDefaultMetaData(ctx);
264
+ auditLog.metadata = { ...metadata, ...defaultMetaData };
263
265
  if (typeof action !== "string") {
264
266
  if (action.getUserInfo) {
265
267
  const userInfo = await action.getUserInfo(ctx);
@@ -274,10 +276,15 @@ const _AuditManager = class _AuditManager {
274
276
  }
275
277
  if (action.getMetaData) {
276
278
  const extra = await action.getMetaData(ctx);
277
- auditLog.metadata = { ...metadata, ...extra };
278
- } else {
279
- const defaultMetaData = await this.getDefaultMetaData(ctx);
280
- auditLog.metadata = { ...metadata, ...defaultMetaData };
279
+ if (extra) {
280
+ if (extra.request) {
281
+ auditLog.metadata.request = { ...auditLog.metadata.request, ...extra.request };
282
+ }
283
+ if (extra.response) {
284
+ auditLog.metadata.response = { ...auditLog.metadata.response, ...extra.response };
285
+ }
286
+ auditLog.metadata = { ...extra, ...auditLog.metadata };
287
+ }
281
288
  }
282
289
  if (action.getSourceAndTarget) {
283
290
  const sourceAndTarget = await action.getSourceAndTarget(ctx);
@@ -288,9 +295,6 @@ const _AuditManager = class _AuditManager {
288
295
  auditLog.targetRecordUK = sourceAndTarget.targetRecordUK;
289
296
  }
290
297
  }
291
- } else {
292
- const defaultMetaData = await this.getDefaultMetaData(ctx);
293
- auditLog.metadata = { ...metadata, ...defaultMetaData };
294
298
  }
295
299
  this.logger.log(auditLog);
296
300
  } catch (err) {
@@ -0,0 +1,18 @@
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
+ export declare class Environment {
10
+ private vars;
11
+ setVariable(key: string, value: string): void;
12
+ removeVariable(key: string): void;
13
+ getVariablesAndSecrets(): {};
14
+ getVariables(): {};
15
+ renderJsonTemplate(template: any, options?: {
16
+ omit?: string[];
17
+ }): any;
18
+ }
@@ -0,0 +1,80 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var environment_exports = {};
39
+ __export(environment_exports, {
40
+ Environment: () => Environment
41
+ });
42
+ module.exports = __toCommonJS(environment_exports);
43
+ var import_utils = require("@nocobase/utils");
44
+ var import_lodash = __toESM(require("lodash"));
45
+ const _Environment = class _Environment {
46
+ vars = {};
47
+ setVariable(key, value) {
48
+ this.vars[key] = value;
49
+ }
50
+ removeVariable(key) {
51
+ delete this.vars[key];
52
+ }
53
+ getVariablesAndSecrets() {
54
+ return this.vars;
55
+ }
56
+ getVariables() {
57
+ return this.vars;
58
+ }
59
+ renderJsonTemplate(template, options) {
60
+ if (options == null ? void 0 : options.omit) {
61
+ const omitTemplate = import_lodash.default.omit(template, options.omit);
62
+ const parsed = (0, import_utils.parse)(omitTemplate)({
63
+ $env: this.vars
64
+ });
65
+ for (const key of options.omit) {
66
+ import_lodash.default.set(parsed, key, import_lodash.default.get(template, key));
67
+ }
68
+ return parsed;
69
+ }
70
+ return (0, import_utils.parse)(template)({
71
+ $env: this.vars
72
+ });
73
+ }
74
+ };
75
+ __name(_Environment, "Environment");
76
+ let Environment = _Environment;
77
+ // Annotate the CommonJS export names for ESM import in node:
78
+ 0 && (module.exports = {
79
+ Environment
80
+ });
@@ -15,6 +15,7 @@ import http, { IncomingMessage, ServerResponse } from 'http';
15
15
  import { ApplicationOptions } from '../application';
16
16
  import { IPCSocketClient } from './ipc-socket-client';
17
17
  import { IPCSocketServer } from './ipc-socket-server';
18
+ import { WSServer } from './ws-server';
18
19
  export interface IncomingRequest {
19
20
  url: string;
20
21
  headers: any;
@@ -41,10 +42,10 @@ export declare class Gateway extends EventEmitter {
41
42
  selectorMiddlewares: Toposort<AppSelectorMiddleware>;
42
43
  server: http.Server | null;
43
44
  ipcSocketServer: IPCSocketServer | null;
45
+ wsServer: WSServer;
44
46
  loggers: Registry<SystemLogger>;
45
47
  private port;
46
48
  private host;
47
- private wsServer;
48
49
  private socketPath;
49
50
  private constructor();
50
51
  static getInstance(options?: any): Gateway;
@@ -63,7 +63,17 @@ var import_errors = require("./errors");
63
63
  var import_ipc_socket_client = require("./ipc-socket-client");
64
64
  var import_ipc_socket_server = require("./ipc-socket-server");
65
65
  var import_ws_server = require("./ws-server");
66
+ var import_node_worker_threads = require("node:worker_threads");
67
+ var import_node_process = __toESM(require("node:process"));
66
68
  const compress = (0, import_node_util.promisify)((0, import_compression.default)());
69
+ function getSocketPath() {
70
+ const { SOCKET_PATH } = import_node_process.default.env;
71
+ if ((0, import_path.isAbsolute)(SOCKET_PATH)) {
72
+ return SOCKET_PATH;
73
+ }
74
+ return (0, import_path.resolve)(import_node_process.default.cwd(), SOCKET_PATH);
75
+ }
76
+ __name(getSocketPath, "getSocketPath");
67
77
  const _Gateway = class _Gateway extends import_events.EventEmitter {
68
78
  /**
69
79
  * use main app as default app to handle request
@@ -71,17 +81,15 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
71
81
  selectorMiddlewares = new import_utils.Toposort();
72
82
  server = null;
73
83
  ipcSocketServer = null;
84
+ wsServer;
74
85
  loggers = new import_utils.Registry();
75
- port = process.env.APP_PORT ? parseInt(process.env.APP_PORT) : null;
86
+ port = import_node_process.default.env.APP_PORT ? parseInt(import_node_process.default.env.APP_PORT) : null;
76
87
  host = "0.0.0.0";
77
- wsServer;
78
- socketPath = (0, import_path.resolve)(process.cwd(), "storage", "gateway.sock");
88
+ socketPath = (0, import_path.resolve)(import_node_process.default.cwd(), "storage", "gateway.sock");
79
89
  constructor() {
80
90
  super();
81
91
  this.reset();
82
- if (process.env.SOCKET_PATH) {
83
- this.socketPath = (0, import_path.resolve)(process.cwd(), process.env.SOCKET_PATH);
84
- }
92
+ this.socketPath = getSocketPath();
85
93
  }
86
94
  static getInstance(options = {}) {
87
95
  if (!_Gateway.instance) {
@@ -90,7 +98,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
90
98
  return _Gateway.instance;
91
99
  }
92
100
  static async getIPCSocketClient() {
93
- const socketPath = (0, import_path.resolve)(process.cwd(), process.env.SOCKET_PATH || "storage/gateway.sock");
101
+ const socketPath = getSocketPath();
94
102
  try {
95
103
  return await import_ipc_socket_client.IPCSocketClient.getConnection(socketPath);
96
104
  } catch (error) {
@@ -166,7 +174,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
166
174
  }
167
175
  async requestHandler(req, res) {
168
176
  const { pathname } = (0, import_url.parse)(req.url);
169
- const { PLUGIN_STATICS_PATH, APP_PUBLIC_PATH } = process.env;
177
+ const { PLUGIN_STATICS_PATH, APP_PUBLIC_PATH } = import_node_process.default.env;
170
178
  if (pathname.endsWith("/__umi/api/bundle-status")) {
171
179
  res.statusCode = 200;
172
180
  res.end("ok");
@@ -176,7 +184,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
176
184
  req.url = req.url.substring(APP_PUBLIC_PATH.length - 1);
177
185
  await compress(req, res);
178
186
  return (0, import_serve_handler.default)(req, res, {
179
- public: (0, import_path.resolve)(process.cwd()),
187
+ public: (0, import_path.resolve)(import_node_process.default.cwd()),
180
188
  directoryListing: false
181
189
  });
182
190
  }
@@ -195,15 +203,22 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
195
203
  ]
196
204
  });
197
205
  }
198
- if (!pathname.startsWith(process.env.API_BASE_PATH)) {
206
+ if (!pathname.startsWith(import_node_process.default.env.API_BASE_PATH)) {
199
207
  req.url = req.url.substring(APP_PUBLIC_PATH.length - 1);
200
208
  await compress(req, res);
201
209
  return (0, import_serve_handler.default)(req, res, {
202
- public: `${process.env.APP_PACKAGE_ROOT}/dist/client`,
210
+ public: `${import_node_process.default.env.APP_PACKAGE_ROOT}/dist/client`,
203
211
  rewrites: [{ source: "/**", destination: "/index.html" }]
204
212
  });
205
213
  }
206
- const handleApp = await this.getRequestHandleAppName(req);
214
+ let handleApp = "main";
215
+ try {
216
+ handleApp = await this.getRequestHandleAppName(req);
217
+ } catch (error) {
218
+ console.log(error);
219
+ this.responseErrorWithCode("APP_INITIALIZING", res, { appName: handleApp });
220
+ return;
221
+ }
207
222
  const hasApp = import_app_supervisor.AppSupervisor.getInstance().hasApp(handleApp);
208
223
  if (!hasApp) {
209
224
  void import_app_supervisor.AppSupervisor.getInstance().bootStrapApp(handleApp);
@@ -257,10 +272,10 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
257
272
  }
258
273
  /* istanbul ignore next -- @preserve */
259
274
  async watch() {
260
- if (!process.env.IS_DEV_CMD) {
275
+ if (!import_node_process.default.env.IS_DEV_CMD) {
261
276
  return;
262
277
  }
263
- const file = process.env.WATCH_FILE;
278
+ const file = import_node_process.default.env.WATCH_FILE;
264
279
  if (!import_fs.default.existsSync(file)) {
265
280
  await import_fs.default.promises.writeFile(file, `export const watchId = '${(0, import_utils.uid)()}';`, "utf-8");
266
281
  }
@@ -273,8 +288,8 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
273
288
  if (isStart) {
274
289
  await this.watch();
275
290
  const startOptions = this.getStartOptions();
276
- const port = startOptions.port || process.env.APP_PORT || 13e3;
277
- const host = startOptions.host || process.env.APP_HOST || "0.0.0.0";
291
+ const port = startOptions.port || import_node_process.default.env.APP_PORT || 13e3;
292
+ const host = startOptions.host || import_node_process.default.env.APP_HOST || "0.0.0.0";
278
293
  this.start({
279
294
  port,
280
295
  host
@@ -282,7 +297,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
282
297
  } else if (!this.isHelp()) {
283
298
  ipcClient = await this.tryConnectToIPCServer();
284
299
  if (ipcClient) {
285
- const response = await ipcClient.write({ type: "passCliArgv", payload: { argv: process.argv } });
300
+ const response = await ipcClient.write({ type: "passCliArgv", payload: { argv: import_node_process.default.argv } });
286
301
  ipcClient.close();
287
302
  if (!["error", "not_found"].includes(response.type)) {
288
303
  return;
@@ -293,15 +308,20 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
293
308
  await (0, import_plugin_symlink.createStoragePluginsSymlink)();
294
309
  }
295
310
  const mainApp = import_app_supervisor.AppSupervisor.getInstance().bootMainApp(options.mainAppOptions);
296
- mainApp.runAsCLI(process.argv, {
297
- throwError: true,
298
- from: "node"
299
- }).then(async () => {
311
+ mainApp.setMaxListeners(50);
312
+ let runArgs = [import_node_process.default.argv, { throwError: true, from: "node" }];
313
+ if (!import_node_worker_threads.isMainThread) {
314
+ runArgs = [import_node_worker_threads.workerData.argv, { throwError: true, from: "user" }];
315
+ }
316
+ mainApp.runAsCLI(...runArgs).then(async () => {
300
317
  if (!isStart && !await mainApp.isStarted()) {
301
318
  await mainApp.stop({ logging: false });
302
319
  }
303
320
  }).catch(async (e) => {
304
321
  if (e.code !== "commander.helpDisplayed") {
322
+ if (!import_node_worker_threads.isMainThread) {
323
+ throw e;
324
+ }
305
325
  mainApp.log.error(e);
306
326
  }
307
327
  if (!isStart && !await mainApp.isStarted()) {
@@ -310,16 +330,16 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
310
330
  });
311
331
  }
312
332
  isStart() {
313
- const argv = process.argv;
333
+ const argv = import_node_process.default.argv;
314
334
  return argv[2] === "start";
315
335
  }
316
336
  isHelp() {
317
- const argv = process.argv;
337
+ const argv = import_node_process.default.argv;
318
338
  return argv[2] === "help";
319
339
  }
320
340
  getStartOptions() {
321
341
  const program = new import_commander.Command();
322
- program.allowUnknownOption().option("-s, --silent").option("-p, --port [post]").option("-h, --host [host]").option("--db-sync").parse(process.argv);
342
+ program.allowUnknownOption().option("-s, --silent").option("-p, --port [post]").option("-h, --host [host]").option("--db-sync").parse(import_node_process.default.argv);
323
343
  return program.opts();
324
344
  }
325
345
  start(options) {
@@ -344,7 +364,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
344
364
  this.wsServer = new import_ws_server.WSServer();
345
365
  this.server.on("upgrade", (request, socket, head) => {
346
366
  const { pathname } = (0, import_url.parse)(request.url);
347
- if (pathname === process.env.WS_PATH) {
367
+ if (pathname === import_node_process.default.env.WS_PATH) {
348
368
  this.wsServer.wss.handleUpgrade(request, socket, head, (ws) => {
349
369
  this.wsServer.wss.emit("connection", ws, request);
350
370
  });