underpost 2.8.453 → 2.8.481

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/src/index.js CHANGED
@@ -5,6 +5,8 @@
5
5
  */
6
6
 
7
7
  import UnderpostCluster from './cli/cluster.js';
8
+ import UnderpostDB from './cli/db.js';
9
+ import UnderpostDeploy from './cli/deploy.js';
8
10
  import UnderpostRootEnv from './cli/env.js';
9
11
  import UnderpostImage from './cli/image.js';
10
12
  import UnderpostRepository from './cli/repository.js';
@@ -24,7 +26,7 @@ class Underpost {
24
26
  * @type {String}
25
27
  * @memberof Underpost
26
28
  */
27
- static version = 'v2.8.453';
29
+ static version = 'v2.8.481';
28
30
  /**
29
31
  * Repository cli API
30
32
  * @static
@@ -74,6 +76,20 @@ class Underpost {
74
76
  * @memberof Underpost
75
77
  */
76
78
  static script = UnderpostScript.API;
79
+ /**
80
+ * Database cli API
81
+ * @static
82
+ * @type {UnderpostDB.API}
83
+ * @memberof Underpost
84
+ */
85
+ static db = UnderpostDB.API;
86
+ /**
87
+ * Deployment cli API
88
+ * @static
89
+ * @type {UnderpostDeploy.API}
90
+ * @memberof Underpost
91
+ */
92
+ static deploy = UnderpostDeploy.API;
77
93
  }
78
94
 
79
95
  const up = Underpost;
@@ -40,21 +40,26 @@ const logger = loggerFactory(import.meta);
40
40
 
41
41
  const Config = {
42
42
  default: DefaultConf,
43
- build: async function (options = { folder: '' }) {
43
+ build: async function (options = { folder: '' }, deployContext, deployList, subConf) {
44
+ if (!deployContext) deployContext = process.argv[2];
44
45
  if (!fs.existsSync(`./tmp`)) fs.mkdirSync(`./tmp`, { recursive: true });
45
46
  fs.writeFileSync(`./tmp/await-deploy`, '', 'utf8');
46
- if (fs.existsSync(`./engine-private/conf/${process.argv[2]}`)) return loadConf(process.argv[2]);
47
- if (fs.existsSync(`./engine-private/replica/${process.argv[2]}`)) return loadConf(process.argv[2]);
47
+ if (fs.existsSync(`./engine-private/conf/${deployContext}`))
48
+ return loadConf(deployContext, process.env.NODE_ENV, subConf);
49
+ if (fs.existsSync(`./engine-private/replica/${deployContext}`))
50
+ return loadConf(deployContext, process.env.NODE_ENV, subConf);
48
51
 
49
- if (process.argv[2] === 'deploy') return;
52
+ if (deployContext === 'deploy') return;
50
53
 
51
- if (process.argv[2] === 'proxy') {
54
+ if (deployContext === 'proxy') {
55
+ if (!deployList) deployList = process.argv[3];
56
+ if (!subConf) subConf = process.argv[4];
52
57
  this.default.server = {};
53
- for (const deployId of process.argv[3].split(',')) {
58
+ for (const deployId of deployList.split(',')) {
54
59
  let confPath = `./engine-private/conf/${deployId}/conf.server.json`;
55
60
  const privateConfDevPath = fs.existsSync(`./engine-private/replica/${deployId}/conf.server.json`)
56
61
  ? `./engine-private/replica/${deployId}/conf.server.json`
57
- : `./engine-private/conf/${deployId}/conf.server.dev.${process.argv[4]}.json`;
62
+ : `./engine-private/conf/${deployId}/conf.server.dev.${subConf}.json`;
58
63
  const confDevPath = fs.existsSync(privateConfDevPath)
59
64
  ? privateConfDevPath
60
65
  : `./engine-private/conf/${deployId}/conf.server.dev.json`;
@@ -62,7 +67,7 @@ const Config = {
62
67
  if (process.env.NODE_ENV === 'development' && fs.existsSync(confDevPath)) confPath = confDevPath;
63
68
  const serverConf = JSON.parse(fs.readFileSync(confPath, 'utf8'));
64
69
 
65
- for (const host of Object.keys(loadReplicas(serverConf))) {
70
+ for (const host of Object.keys(loadReplicas(serverConf, deployContext, subConf))) {
66
71
  if (serverConf[host]['/'])
67
72
  this.default.server[host] = {
68
73
  ...this.default.server[host],
@@ -92,7 +97,7 @@ const Config = {
92
97
  },
93
98
  };
94
99
 
95
- const loadConf = (deployId, envInput) => {
100
+ const loadConf = (deployId, envInput, subConf) => {
96
101
  const folder = fs.existsSync(`./engine-private/replica/${deployId}`)
97
102
  ? `./engine-private/replica/${deployId}`
98
103
  : `./engine-private/conf/${deployId}`;
@@ -109,7 +114,8 @@ const loadConf = (deployId, envInput) => {
109
114
  ? fs.readFileSync(`${folder}/conf.${typeConf}.json`, 'utf8')
110
115
  : JSON.stringify(Config.default[typeConf]);
111
116
  if (process.env.NODE_ENV === 'development' && typeConf === 'server') {
112
- const devConfPath = `${folder}/conf.${typeConf}.dev${process.argv[3] ? `.${process.argv[3]}` : ''}.json`;
117
+ if (!subConf) subConf = process.argv[3];
118
+ const devConfPath = `${folder}/conf.${typeConf}.dev${subConf ? `.${subConf}` : ''}.json`;
113
119
  if (fs.existsSync(devConfPath)) srcConf = fs.readFileSync(devConfPath, 'utf8');
114
120
  }
115
121
  if (typeConf === 'server') srcConf = JSON.stringify(loadReplicas(JSON.parse(srcConf)), null, 4);
@@ -135,15 +141,17 @@ const loadConf = (deployId, envInput) => {
135
141
  return { folder, deployId };
136
142
  };
137
143
 
138
- const loadReplicas = (confServer) => {
144
+ const loadReplicas = (confServer, deployContext, subConf) => {
145
+ if (!deployContext) deployContext = process.argv[2];
146
+ if (!subConf) subConf = process.argv[3];
139
147
  for (const host of Object.keys(confServer)) {
140
148
  for (const path of Object.keys(confServer[host])) {
141
149
  const { replicas, singleReplica } = confServer[host][path];
142
150
  if (
143
151
  replicas &&
144
- (process.argv[2] === 'proxy' ||
152
+ (deployContext === 'proxy' ||
145
153
  !singleReplica ||
146
- (singleReplica && process.env.NODE_ENV === 'development' && !process.argv[3]))
154
+ (singleReplica && process.env.NODE_ENV === 'development' && !subConf))
147
155
  )
148
156
  for (const replicaPath of replicas) {
149
157
  confServer[host][replicaPath] = newInstance(confServer[host][path]);
@@ -513,14 +521,16 @@ const buildPortProxyRouter = (port, proxyRouter) => {
513
521
  // build router
514
522
  Object.keys(hosts).map((hostKey) => {
515
523
  let { host, path, target, proxy, peer } = hosts[hostKey];
516
- if (process.env.NODE_ENV === 'development') host = `localhost`;
524
+ if (process.argv.includes('localhost') && process.env.NODE_ENV === 'development') host = `localhost`;
517
525
 
518
526
  if (!proxy.includes(port)) return;
519
527
  const absoluteHost = [80, 443].includes(port)
520
528
  ? `${host}${path === '/' ? '' : path}`
521
529
  : `${host}:${port}${path === '/' ? '' : path}`;
522
530
 
523
- if (!(absoluteHost in router)) router[absoluteHost] = target;
531
+ if (process.argv.includes('localhost')) {
532
+ if (!(absoluteHost in router)) router[absoluteHost] = target;
533
+ } else router[absoluteHost] = target;
524
534
  }); // order router
525
535
 
526
536
  if (Object.keys(router).length === 0) return router;
@@ -85,6 +85,7 @@ const format = (meta) =>
85
85
  */
86
86
  const setUpInfo = async (logger = new winston.Logger()) => {
87
87
  logger.info('argv', process.argv);
88
+ logger.info('cwd', process.cwd());
88
89
  logger.info('platform', process.platform);
89
90
  logger.info('env', process.env.NODE_ENV);
90
91
  logger.info('--max-old-space-size', {
@@ -1,7 +1,7 @@
1
1
  import fs from 'fs-extra';
2
2
 
3
3
  import { publicIp, publicIpv4, publicIpv6 } from 'public-ip';
4
- import { loggerFactory } from './logger.js';
4
+ import { actionInitLog, loggerFactory } from './logger.js';
5
5
  import { DataBaseProvider } from '../db/DataBaseProvider.js';
6
6
  import { getDeployId } from './conf.js';
7
7
 
@@ -39,7 +39,7 @@ const saveRuntimeRouter = async () => {
39
39
  const host = process.env.DEFAULT_DEPLOY_HOST;
40
40
  const path = process.env.DEFAULT_DEPLOY_PATH;
41
41
  const confServerPath = `./engine-private/conf/${deployId}/conf.server.json`;
42
- if (!deployId || !host || !path) {
42
+ if (!deployId || !host || !path || !fs.existsSync(confServerPath)) {
43
43
  // logger.warn('default deploy instance not found');
44
44
  return;
45
45
  }
@@ -77,7 +77,7 @@ const saveRuntimeRouter = async () => {
77
77
 
78
78
  if (closeConn) await DataBaseProvider.instance[`${host}${path}`].mongoose.close();
79
79
  } catch (error) {
80
- logger.error(error);
80
+ logger.error(error, error.stack);
81
81
  }
82
82
  };
83
83
 
@@ -114,20 +114,30 @@ const saveRuntimeCron = async () => {
114
114
 
115
115
  if (closeConn) await DataBaseProvider.instance[`${host}${path}`].mongoose.close();
116
116
  } catch (error) {
117
- logger.error(error);
117
+ logger.error(error, error.stack);
118
118
  }
119
119
  };
120
120
 
121
121
  const listenServerFactory = (logic = async () => {}) => {
122
122
  return {
123
- listen: async (...args) => (logic ? await logic(...args) : undefined, args[1]()),
123
+ listen: async (...args) => (
124
+ setTimeout(() => {
125
+ const message = 'Listen server factory timeout';
126
+ logger.error(message);
127
+ throw new Error(message);
128
+ }, 80000000), // ~ 55 days
129
+ (logic ? await logic(...args) : undefined, args[1]())
130
+ ),
124
131
  };
125
132
  };
126
133
 
127
134
  const listenPortController = async (server, port, metadata) =>
128
135
  new Promise((resolve) => {
129
136
  try {
130
- if (!server) server = listenServerFactory();
137
+ if (port === ':') {
138
+ server.listen(port, actionInitLog);
139
+ return resolve(true);
140
+ }
131
141
 
132
142
  const { host, path, client, runtime, meta } = metadata;
133
143
  const error = [];