@abtnode/router-provider 1.7.19 → 1.7.22

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.
@@ -78,8 +78,8 @@ const createRequestHandler = (id) => (req, res, target) => {
78
78
  if (rule.did) {
79
79
  req.headers['x-blocklet-did'] = rule.did;
80
80
  }
81
- if (rule.realDid && rule.did !== rule.realDid) {
82
- req.headers['x-blocklet-real-did'] = rule.realDid;
81
+ if (rule.componentId) {
82
+ req.headers['x-blocklet-component-id'] = rule.componentId;
83
83
  }
84
84
 
85
85
  req.headers['x-path-prefix'] = rule.prefix.replace(WELLKNOWN_SERVICE_PATH_PREFIX, '') || '/';
@@ -34,6 +34,7 @@ const {
34
34
  rotateNginxLogFile,
35
35
  getMissingModules,
36
36
  getMainTemplate,
37
+ getUpstreamName,
37
38
  } = require('./util');
38
39
  const {
39
40
  decideHttpPort,
@@ -106,6 +107,7 @@ class NginxProvider extends BaseProvider {
106
107
  this.httpsPort = decideHttpsPort(httpsPort);
107
108
  this.cacheDisabled = !!cacheDisabled;
108
109
  this.isHttp2Supported = this._isHttp2Supported();
110
+ this.conf = null; // nginx `conf` object
109
111
 
110
112
  logger.info('nginx provider config', {
111
113
  configDir,
@@ -156,6 +158,8 @@ class NginxProvider extends BaseProvider {
156
158
  return;
157
159
  }
158
160
 
161
+ this.conf = conf;
162
+
159
163
  conf.on('flushed', () => resolve());
160
164
  conf.live(this.configPath);
161
165
 
@@ -177,6 +181,14 @@ class NginxProvider extends BaseProvider {
177
181
  }
178
182
 
179
183
  logger.info('routing sites:', sites);
184
+
185
+ const allRules = sites.reduce((acc, site) => {
186
+ acc.push(...(site.rules || []));
187
+ return acc;
188
+ }, []);
189
+
190
+ this.ensureUpstreamServers(allRules);
191
+
180
192
  // eslint-disable-next-line no-restricted-syntax
181
193
  for (const site of sites) {
182
194
  const { domain, port, rules, corsAllowedOrigins } = site;
@@ -363,6 +375,44 @@ class NginxProvider extends BaseProvider {
363
375
  this._addBlockletTypeLocation(args);
364
376
  }
365
377
 
378
+ addUpstreamServer(port) {
379
+ this.conf.nginx.http._add('upstream', getUpstreamName(port));
380
+ const upstream = this.conf.nginx.http.upstream.length
381
+ ? this.conf.nginx.http.upstream[this.conf.nginx.http.upstream.length - 1]
382
+ : this.conf.nginx.http.upstream;
383
+
384
+ upstream._add('server', `127.0.0.1:${port} max_fails=1 fail_timeout=2s`);
385
+ upstream._add('keepalive', '2');
386
+ }
387
+
388
+ ensureUpstreamServers(rules) {
389
+ const upstreamMap = new Map();
390
+
391
+ const servicePort = process.env.ABT_NODE_SERVICE_PORT;
392
+
393
+ if (!upstreamMap.has(servicePort)) {
394
+ this.addUpstreamServer(servicePort);
395
+ upstreamMap.set(servicePort, 1);
396
+ }
397
+
398
+ for (let i = 0; i < rules.length; i++) {
399
+ const rule = rules[i];
400
+
401
+ if (
402
+ [
403
+ ROUTING_RULE_TYPES.DAEMON,
404
+ ROUTING_RULE_TYPES.SERVICE,
405
+ ROUTING_RULE_TYPES.BLOCKLET,
406
+ ROUTING_RULE_TYPES.GENERAL_PROXY,
407
+ ].includes(rule.type) &&
408
+ !upstreamMap.has(String(rule.port))
409
+ ) {
410
+ this.addUpstreamServer(rule.port);
411
+ upstreamMap.set(String(rule.port), 1);
412
+ }
413
+ }
414
+ }
415
+
366
416
  /**
367
417
  * Returns:
368
418
  * server /flash/ {
@@ -376,8 +426,8 @@ class NginxProvider extends BaseProvider {
376
426
  * @param {number} port
377
427
  * @param {string} pathPrefix
378
428
  * @param {string} pathSuffix
379
- * @param {string} did root blocklet did
380
- * @param {string} realDid child blocklet did
429
+ * @param {string} did app did
430
+ * @param {string} componentId component id
381
431
  */
382
432
  _addBlockletTypeLocation({
383
433
  server,
@@ -387,7 +437,7 @@ class NginxProvider extends BaseProvider {
387
437
  groupPrefix,
388
438
  suffix,
389
439
  did,
390
- realDid,
440
+ componentId,
391
441
  corsAllowedOrigins,
392
442
  target,
393
443
  ruleId,
@@ -421,12 +471,11 @@ class NginxProvider extends BaseProvider {
421
471
  if (did) {
422
472
  location._add('set', `$did "${did}"`);
423
473
  location._add('proxy_set_header', `X-Blocklet-Did "${did}"`);
424
- if (realDid) {
425
- location._add('proxy_set_header', `X-Blocklet-Real-Did "${realDid}"`);
426
- }
474
+ location._add('proxy_set_header', `X-Blocklet-Component-Id "${componentId}"`);
427
475
  }
428
476
 
429
477
  // kill cache
478
+ // TODO: 这个放在管理端的 Gateway 配置中
430
479
  if (this.cacheDisabled) {
431
480
  location._add('expires', '-1');
432
481
  }
@@ -456,7 +505,7 @@ class NginxProvider extends BaseProvider {
456
505
 
457
506
  // Rewrite path
458
507
  location._add('rewrite', `^${rewritePathPrefix}/?(.*) /$1 break`);
459
- location._add('proxy_pass', `http://127.0.0.1:${process.env.ABT_NODE_SERVICE_PORT}`);
508
+ location._add('proxy_pass', `http://${getUpstreamName(process.env.ABT_NODE_SERVICE_PORT)}/`);
460
509
 
461
510
  return;
462
511
  }
@@ -467,8 +516,8 @@ class NginxProvider extends BaseProvider {
467
516
  if (!suffix && prefix !== target) {
468
517
  location._add('rewrite', `^${rewritePathPrefix}/?(.*) ${`${target}/`.replace(/\/\//g, '/')}$1 break`);
469
518
  }
470
- // location._add('rewrite', `^${rewritePathPrefix}/?(.*) /$1 break`);
471
- location._add('proxy_pass', `http://127.0.0.1:${port}`);
519
+
520
+ location._add('proxy_pass', `http://${getUpstreamName(port)}`);
472
521
  }
473
522
 
474
523
  _addRedirectTypeLocation({ server, url, redirectCode, prefix, suffix }) {
@@ -505,7 +554,7 @@ class NginxProvider extends BaseProvider {
505
554
  const location = this._getLastLocation(server);
506
555
  this._addCommonHeader(location);
507
556
 
508
- location._add('proxy_pass', `http://127.0.0.1:${port}`);
557
+ location._add('proxy_pass', `http://${getUpstreamName(port)}`);
509
558
  }
510
559
 
511
560
  _addDirectResponseLocation({ server, response, prefix, suffix }) {
package/lib/nginx/util.js CHANGED
@@ -162,7 +162,7 @@ events {
162
162
  http {
163
163
  map $http_upgrade $connection_upgrade {
164
164
  default upgrade;
165
- '' close;
165
+ '' "";
166
166
  }
167
167
  client_body_temp_path ${path.join(tmpDir, 'client_body')};
168
168
  proxy_temp_path ${path.join(tmpDir, 'proxy')};
@@ -271,6 +271,8 @@ const getMissingModules = (configParams) => {
271
271
  return missingModules;
272
272
  };
273
273
 
274
+ const getUpstreamName = (port) => `server_${port}`;
275
+
274
276
  module.exports = {
275
277
  addTestServer,
276
278
  getMainTemplate,
@@ -282,4 +284,5 @@ module.exports = {
282
284
  rotateNginxLogFile,
283
285
  getUserGroup,
284
286
  getWorkerConnectionCount,
287
+ getUpstreamName,
285
288
  };
package/lib/util.js CHANGED
@@ -85,7 +85,7 @@ const formatRoutingTable = (routingTable, onRule) => {
85
85
  } else {
86
86
  rule.port = +x.to.port;
87
87
  rule.did = x.to.did;
88
- rule.realDid = x.to.realDid;
88
+ rule.componentId = x.to.componentId;
89
89
  rule.target = trimEndSlash(normalizePathPrefix(x.to.target || '/'));
90
90
  rule.services = x.services || [];
91
91
  }
@@ -109,17 +109,18 @@ const formatRoutingTable = (routingTable, onRule) => {
109
109
 
110
110
  return { sites: Object.values(sites), configs };
111
111
  };
112
-
112
+ /**
113
+ *
114
+ *
115
+ * @param {string} domain
116
+ * @return {boolean}
117
+ */
113
118
  const isSpecificDomain = (domain) => {
114
119
  if (isValidDomain(domain) === false) {
115
120
  return false;
116
121
  }
117
122
 
118
- if (domain.endsWith(DEFAULT_IP_DOMAIN_SUFFIX)) {
119
- return false;
120
- }
121
-
122
- return true;
123
+ return !domain.endsWith(DEFAULT_IP_DOMAIN_SUFFIX);
123
124
  };
124
125
 
125
126
  const toSlotDomain = (domain) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.7.19",
3
+ "version": "1.7.22",
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.19",
36
- "@abtnode/logger": "1.7.19",
37
- "@abtnode/router-templates": "1.7.19",
38
- "@abtnode/util": "1.7.19",
35
+ "@abtnode/constant": "1.7.22",
36
+ "@abtnode/logger": "1.7.22",
37
+ "@abtnode/router-templates": "1.7.22",
38
+ "@abtnode/util": "1.7.22",
39
39
  "axios": "^0.27.2",
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": "73c02fe34d02270e59c5edfeedd7e2a878b772c4"
65
+ "gitHead": "6190972e62b5e6b1039aa71a291b3e2c521df367"
66
66
  }