underpost 2.85.0 → 2.85.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.
@@ -11,9 +11,12 @@ import dotenv from 'dotenv';
11
11
 
12
12
  import { createProxyMiddleware } from 'http-proxy-middleware';
13
13
  import { loggerFactory, loggerMiddleware } from './logger.js';
14
- import { TLS } from './tls.js';
15
- import { buildPortProxyRouter, buildProxyRouter } from './conf.js';
14
+ import { buildPortProxyRouter, buildProxyRouter, getTlsHosts, isDevProxyContext, isTlsDevProxy } from './conf.js';
16
15
  import UnderpostStartUp from './start.js';
16
+ import UnderpostDeploy from '../cli/deploy.js';
17
+ import { SSL_BASE, TLS } from './tls.js';
18
+ import { shellExec } from './process.js';
19
+ import fs from 'fs-extra';
17
20
 
18
21
  dotenv.config();
19
22
 
@@ -22,28 +25,27 @@ const logger = loggerFactory(import.meta);
22
25
  /**
23
26
  * Main class for building and running the proxy server.
24
27
  * All utility methods are implemented as static to serve as a namespace container.
25
- * @class Proxy
26
- * @augments Proxy
28
+ * @class ProxyService
27
29
  * @memberof ProxyService
28
30
  */
29
- class Proxy {
31
+ class ProxyService {
30
32
  /**
31
- * Initializes and starts the reverse proxy server for all configured ports and hosts.
32
- * @async
33
+ * Builds and starts the proxy server with appropriate routing and SSL configuration.
33
34
  * @static
34
- * @memberof ProxyService
35
- * @returns {Promise<void>}
36
- * @memberof ProxyService
35
+ * @returns {Promise<void>} Resolves when the server is successfully started.
37
36
  */
38
- static async buildProxy() {
37
+ static async build() {
38
+ if (process.env.NODE_ENV === 'production') process.env.DEV_PROXY_PORT_OFFSET = 0;
39
+
39
40
  // Start a default Express listener on process.env.PORT (potentially unused, but ensures Express is initialized)
41
+ process.env.PORT = parseInt(process.env.PORT) + parseInt(process.env.DEV_PROXY_PORT_OFFSET);
40
42
  express().listen(process.env.PORT);
41
43
 
42
44
  const proxyRouter = buildProxyRouter();
43
45
 
44
46
  for (let port of Object.keys(proxyRouter)) {
45
- port = parseInt(port);
46
47
  const hosts = proxyRouter[port];
48
+ port = parseInt(port) + parseInt(process.env.DEV_PROXY_PORT_OFFSET);
47
49
  const proxyPath = '/';
48
50
  const proxyHost = 'localhost';
49
51
  const runningData = { host: proxyHost, path: proxyPath, client: null, runtime: 'nodejs', meta: import.meta };
@@ -56,19 +58,22 @@ class Proxy {
56
58
  /** @type {import('http-proxy-middleware/dist/types').Options} */
57
59
  const options = {
58
60
  ws: true, // Enable websocket proxying
59
- target: `http://localhost:${process.env.PORT}`, // Default target (should be overridden by router)
61
+ target: `http://localhost:${parseInt(process.env.PORT - 1)}`, // Default target (should be overridden by router)
60
62
  router: {},
63
+ // changeOrigin: true,
64
+ logLevel: 'debug',
61
65
  xfwd: true, // Adds x-forward headers (Host, Proto, etc.)
62
- onProxyReq: (proxyReq, req, res, options) => {
63
- // Use the static method from the TLS class for redirection logic
64
- TLS.sslRedirectMiddleware(req, res, port, proxyRouter);
65
- },
66
- pathRewrite: {
67
- // Add path rewrite rules here if necessary
68
- },
66
+ onProxyReq: (proxyReq, req, res, options) => {},
67
+ pathRewrite: {},
69
68
  };
70
69
 
71
- options.router = buildPortProxyRouter(port, proxyRouter, { orderByPathLength: true });
70
+ options.router = buildPortProxyRouter({
71
+ port,
72
+ proxyRouter,
73
+ hosts,
74
+ orderByPathLength: true,
75
+ devProxyContext: process.env.NODE_ENV !== 'production',
76
+ });
72
77
 
73
78
  const filter = proxyPath; // Use '/' as the general filter
74
79
  app.use(proxyPath, createProxyMiddleware(filter, options));
@@ -91,11 +96,33 @@ class Proxy {
91
96
  break;
92
97
 
93
98
  default:
94
- // In non-production, always use standard HTTP listener
95
- await UnderpostStartUp.API.listenPortController(app, port, runningData);
96
- break;
99
+ switch (port) {
100
+ case 443: {
101
+ let tlsHosts = hosts;
102
+ if (isDevProxyContext() && isTlsDevProxy()) {
103
+ tlsHosts = {};
104
+ for (const tlsHost of getTlsHosts(hosts)) {
105
+ if (fs.existsSync(SSL_BASE(tlsHost))) fs.removeSync(SSL_BASE(tlsHost));
106
+ if (!TLS.validateSecureContext(tlsHost)) shellExec(`node bin/deploy tls "${tlsHost}"`);
107
+ tlsHosts[tlsHost] = {};
108
+ }
109
+ }
110
+ const { ServerSSL } = await TLS.createSslServer(app, tlsHosts);
111
+ await UnderpostStartUp.API.listenPortController(ServerSSL, port, runningData);
112
+ break;
113
+ }
114
+ default: // In non-production, always use standard HTTP listener
115
+ await UnderpostStartUp.API.listenPortController(app, port, runningData);
116
+ break;
117
+ }
97
118
  }
98
119
  logger.info('Proxy running', { port, options });
120
+ if (process.env.NODE_ENV === 'development')
121
+ logger.info(
122
+ UnderpostDeploy.API.etcHostFactory(Object.keys(options.router), {
123
+ append: true,
124
+ }).renderHosts,
125
+ );
99
126
  }
100
127
  }
101
128
  }
@@ -105,6 +132,6 @@ class Proxy {
105
132
  * @type {function(): Promise<void>}
106
133
  * @memberof ProxyService
107
134
  */
108
- const buildProxy = Proxy.buildProxy;
135
+ const buildProxy = ProxyService.build;
109
136
 
110
- export { Proxy, buildProxy };
137
+ export { ProxyService, buildProxy };
@@ -53,7 +53,7 @@ class UnderpostStartUp {
53
53
  throw new Error(message);
54
54
  }
55
55
  }, msDelta);
56
- return logic ? await logic(...args) : undefined, args[1]();
56
+ return (logic ? await logic(...args) : undefined, args[1]());
57
57
  },
58
58
  };
59
59
  },
@@ -95,8 +95,8 @@ class UnderpostStartUp {
95
95
  port === 80
96
96
  ? `http://${host}${path}`
97
97
  : port === 443
98
- ? `https://${host}${path}`
99
- : `http://${host}:${port}${path}`,
98
+ ? `https://${host}${path}`
99
+ : `http://${host}:${port}${path}`,
100
100
  local: `http://localhost:${port}${path}`,
101
101
  apis: metadata.apis,
102
102
  };
@@ -158,4 +158,14 @@ class UnderpostStartUp {
158
158
  };
159
159
  }
160
160
 
161
+ /**
162
+ * Creates a keep-alive process to maintain server activity.
163
+ * @memberof UnderpostStartUp
164
+ * @returns
165
+ */
166
+ const createKeepAliveProcess = async () =>
167
+ await UnderpostStartUp.API.listenPortController(UnderpostStartUp.API.listenServerFactory(), ':');
168
+
161
169
  export default UnderpostStartUp;
170
+
171
+ export { createKeepAliveProcess, UnderpostStartUp };
package/src/server/tls.js CHANGED
@@ -248,4 +248,4 @@ const validateSecureContext = TLS.validateSecureContext;
248
248
  const createSslServer = TLS.createSslServer;
249
249
  const sslRedirectMiddleware = TLS.sslRedirectMiddleware;
250
250
 
251
- export { TLS, buildSSL, buildSecureContext, validateSecureContext, createSslServer, sslRedirectMiddleware };
251
+ export { TLS, SSL_BASE, buildSSL, buildSecureContext, validateSecureContext, createSslServer, sslRedirectMiddleware };