@abtnode/router-provider 1.7.8 → 1.7.11

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.
@@ -2,6 +2,7 @@
2
2
  /* eslint-disable consistent-return */
3
3
  const fs = require('fs');
4
4
  const http = require('http');
5
+ const https = require('https');
5
6
  const httpProxy = require('http-proxy');
6
7
  const validUrl = require('valid-url');
7
8
  const path = require('path');
@@ -119,8 +120,7 @@ module.exports = class ReverseProxy {
119
120
  }
120
121
 
121
122
  setupHttpProxy(proxy, websocketUpgrade, opts) {
122
- const httpServerModule = opts.serverModule || http;
123
- const server = httpServerModule.createServer((req, res) => {
123
+ const server = http.createServer((req, res) => {
124
124
  const src = this._getSource(req);
125
125
  if (this.opts.corsHandler(req, res) === false) {
126
126
  return;
@@ -163,8 +163,6 @@ module.exports = class ReverseProxy {
163
163
  }
164
164
 
165
165
  setupHttpsProxy(proxy, websocketUpgrade, sslOpts) {
166
- let https;
167
-
168
166
  let ssl = {
169
167
  SNICallback: (hostname, cb) => {
170
168
  if (cb) {
@@ -187,17 +185,6 @@ module.exports = class ReverseProxy {
187
185
  ssl = _.defaults(ssl, sslOpts.opts);
188
186
  }
189
187
 
190
- if (sslOpts.http2) {
191
- // eslint-disable-next-line
192
- https = sslOpts.serverModule || require('spdy');
193
- if (_.isObject(sslOpts.http2)) {
194
- sslOpts.spdy = sslOpts.http2;
195
- }
196
- } else {
197
- // eslint-disable-next-line global-require
198
- https = sslOpts.serverModule || require('https');
199
- }
200
-
201
188
  this.httpsServer = https.createServer(ssl, (req, res) => {
202
189
  const src = this._getSource(req);
203
190
  if (this.opts.corsHandler(req, res) === false) {
@@ -15,3 +15,5 @@ proxy_set_header Upgrade $http_upgrade;
15
15
  proxy_set_header Connection $connection_upgrade;
16
16
 
17
17
  proxy_read_timeout 3600;
18
+
19
+ proxy_pass_header server;
@@ -105,6 +105,7 @@ class NginxProvider extends BaseProvider {
105
105
  this.httpPort = decideHttpPort(httpPort);
106
106
  this.httpsPort = decideHttpsPort(httpsPort);
107
107
  this.cacheDisabled = !!cacheDisabled;
108
+ this.isHttp2Supported = this._isHttp2Supported();
108
109
 
109
110
  logger.info('nginx provider config', {
110
111
  configDir,
@@ -125,7 +126,7 @@ class NginxProvider extends BaseProvider {
125
126
  this.initialize();
126
127
  }
127
128
 
128
- async update({ routingTable = [], certificates = [], globalHeaders, services = [], nodeInfo = {} } = {}) {
129
+ async update({ routingTable = [], certificates = [], commonHeaders, services = [], nodeInfo = {} } = {}) {
129
130
  if (!Array.isArray(routingTable)) {
130
131
  throw new Error('routingTable must be an array');
131
132
  }
@@ -162,7 +163,7 @@ class NginxProvider extends BaseProvider {
162
163
 
163
164
  this._addCorsMap(conf, siteCorsConfigs);
164
165
  conf.nginx.http._add('server_tokens', 'off');
165
- this._addGlobalHeaders(conf, globalHeaders);
166
+ this._addCommonResHeaders(conf.nginx.http, commonHeaders);
166
167
  this._addExposeServices(conf, services);
167
168
 
168
169
  logger.info('routing sites:', sites);
@@ -190,6 +191,7 @@ class NginxProvider extends BaseProvider {
190
191
  serverName: parsedServerName,
191
192
  corsAllowedOrigins,
192
193
  daemonPort: nodeInfo.port,
194
+ commonHeaders,
193
195
  });
194
196
  } else {
195
197
  this._addHttpServer({
@@ -199,6 +201,7 @@ class NginxProvider extends BaseProvider {
199
201
  corsAllowedOrigins,
200
202
  port,
201
203
  daemonPort: nodeInfo.port,
204
+ commonHeaders,
202
205
  });
203
206
  }
204
207
  }
@@ -302,6 +305,11 @@ class NginxProvider extends BaseProvider {
302
305
  return result;
303
306
  }
304
307
 
308
+ _isHttp2Supported() {
309
+ const configArgs = this.readNginxConfigParams();
310
+ return typeof configArgs['with-http_v2_module'] !== 'undefined';
311
+ }
312
+
305
313
  readNginxConfigParams() {
306
314
  const result = shelljs.exec(`${this.binPath} -V`, { silent: true });
307
315
 
@@ -374,6 +382,7 @@ class NginxProvider extends BaseProvider {
374
382
  target,
375
383
  ruleId,
376
384
  type,
385
+ commonHeaders,
377
386
  }) {
378
387
  server._add('location', concatPath(prefix, suffix));
379
388
 
@@ -396,6 +405,7 @@ class NginxProvider extends BaseProvider {
396
405
  location._add('include', 'includes/security');
397
406
  }
398
407
 
408
+ this._addCommonResHeaders(location, commonHeaders);
399
409
  this._addTailSlashRedirection(location, prefix); // Note: 末尾 "/" 的重定向要放在 CORS(OPTIONS) 响应之后, 这样不会影响 OPTIONS 的响应
400
410
 
401
411
  if (did) {
@@ -612,13 +622,13 @@ class NginxProvider extends BaseProvider {
612
622
  }
613
623
  }
614
624
 
615
- _addGlobalHeaders(conf, headers) {
625
+ _addCommonResHeaders(block, headers) {
616
626
  if (!headers || Object.prototype.toString.call(headers) !== '[object Object]') {
617
627
  return;
618
628
  }
619
629
 
620
630
  Object.keys(headers).forEach((key) => {
621
- conf.nginx.http._add('add_header', `${key} ${headers[key]}`);
631
+ block._add('add_header', `${key} ${headers[key]}`);
622
632
  });
623
633
  }
624
634
 
@@ -650,14 +660,14 @@ class NginxProvider extends BaseProvider {
650
660
  : conf.nginx.stream.server;
651
661
  }
652
662
 
653
- _addHttpServer({ locations = [], serverName, conf, corsAllowedOrigins, port, daemonPort }) {
663
+ _addHttpServer({ locations = [], serverName, conf, corsAllowedOrigins, port, daemonPort, commonHeaders }) {
654
664
  const httpServerUnit = this._addHttpServerUnit({ conf, serverName, port });
655
665
  this._addDefaultLocations(httpServerUnit, daemonPort);
656
666
  // eslint-disable-next-line max-len
657
- locations.forEach((x) => this._addReverseProxy({ server: httpServerUnit, ...x, serverName, corsAllowedOrigins })); // prettier-ignore
667
+ locations.forEach((x) => this._addReverseProxy({ server: httpServerUnit, ...x, serverName, corsAllowedOrigins, commonHeaders })); // prettier-ignore
658
668
  }
659
669
 
660
- _addHttpsServer({ conf, locations, certificateFileName, serverName, corsAllowedOrigins, daemonPort }) {
670
+ _addHttpsServer({ conf, locations, certificateFileName, serverName, corsAllowedOrigins, daemonPort, commonHeaders }) {
661
671
  const httpsServerUnit = this._addHttpsServerUnit({ conf, serverName, certificateFileName });
662
672
 
663
673
  const httpServerUnit = this._addHttpServerUnit({ conf, serverName });
@@ -665,7 +675,7 @@ class NginxProvider extends BaseProvider {
665
675
 
666
676
  this._addDefaultLocations(httpsServerUnit, daemonPort);
667
677
  // eslint-disable-next-line max-len
668
- locations.forEach((x) => this._addReverseProxy({ server: httpsServerUnit, ...x, serverName, corsAllowedOrigins })); // prettier-ignore
678
+ locations.forEach((x) => this._addReverseProxy({ server: httpsServerUnit, ...x, serverName, corsAllowedOrigins,commonHeaders })); // prettier-ignore
669
679
  }
670
680
 
671
681
  _addHttpServerUnit({ conf, serverName, port }) {
@@ -690,7 +700,7 @@ class NginxProvider extends BaseProvider {
690
700
  const crtPath = `${path.join(this.certDir, certificateFileName)}.crt`;
691
701
  const keyPath = `${path.join(this.certDir, certificateFileName)}.key`;
692
702
 
693
- let listen = `${this.httpsPort} ssl`;
703
+ let listen = `${this.httpsPort} ssl ${this.isHttp2Supported ? 'http2' : ''}`.trim();
694
704
  if (serverName === '_') {
695
705
  listen = `${listen} default_server`;
696
706
  }
package/lib/nginx/util.js CHANGED
@@ -12,7 +12,7 @@ const { MAX_UPLOAD_FILE_SIZE } = require('@abtnode/constant');
12
12
 
13
13
  const logger = require('@abtnode/logger')('router:nginx:util');
14
14
 
15
- const WORKER_CONNECTIONS = 1000 * 10; // 10K
15
+ const MAX_WORKER_CONNECTIONS = 10240;
16
16
  const CLIENT_MAX_BODY_SIZE = process.env.MAX_UPLOAD_FILE_SIZE || MAX_UPLOAD_FILE_SIZE;
17
17
 
18
18
  const formatError = (errStr) => {
@@ -130,6 +130,17 @@ const getUserGroup = (username) => {
130
130
  return res.stdout.trim();
131
131
  };
132
132
 
133
+ const getWorkerConnectionCount = (maxWorkerConnections, workerProcess) => {
134
+ const { stdout, code } = shelljs.exec('ulimit -n', { silent: true });
135
+ if (code !== 0) {
136
+ return maxWorkerConnections;
137
+ }
138
+
139
+ const ulimit = Number(stdout.trim());
140
+
141
+ return Math.min(maxWorkerConnections, Math.floor(ulimit / workerProcess));
142
+ };
143
+
133
144
  const getMainTemplate = ({
134
145
  logDir,
135
146
  tmpDir,
@@ -145,7 +156,7 @@ user ${os.userInfo().username} ${getUserGroup(os.userInfo().username)};
145
156
  pid nginx.pid;
146
157
 
147
158
  events {
148
- worker_connections ${WORKER_CONNECTIONS};
159
+ worker_connections ${getWorkerConnectionCount(MAX_WORKER_CONNECTIONS, workerProcess)};
149
160
  }
150
161
 
151
162
  http {
@@ -270,4 +281,5 @@ module.exports = {
270
281
  getNginxStatus,
271
282
  rotateNginxLogFile,
272
283
  getUserGroup,
284
+ getWorkerConnectionCount,
273
285
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.7.8",
3
+ "version": "1.7.11",
4
4
  "description": "Routing engine implementations for abt node",
5
5
  "author": "polunzh <polunzh@gmail.com>",
6
6
  "homepage": "https://github.com/ArcBlock/blocklet-server#readme",
@@ -32,10 +32,10 @@
32
32
  "url": "https://github.com/ArcBlock/blocklet-server/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@abtnode/constant": "1.7.8",
36
- "@abtnode/logger": "1.7.8",
37
- "@abtnode/router-templates": "1.7.8",
38
- "@abtnode/util": "1.7.8",
35
+ "@abtnode/constant": "1.7.11",
36
+ "@abtnode/logger": "1.7.11",
37
+ "@abtnode/router-templates": "1.7.11",
38
+ "@abtnode/util": "1.7.11",
39
39
  "axios": "^0.26.1",
40
40
  "debug": "^4.3.3",
41
41
  "find-process": "^1.4.3",
@@ -62,5 +62,5 @@
62
62
  "fs-extra": "^10.0.1",
63
63
  "needle": "^3.0.0"
64
64
  },
65
- "gitHead": "ae75dd20b4750a31dc53c88b75ed1d95c8f15397"
65
+ "gitHead": "84e741e6b37e47d52bf834ce3ace8ed601d0896c"
66
66
  }