@abtnode/router-provider 1.16.15-beta-e143b1cf → 1.16.15-beta-12f50442

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/lib/base.js CHANGED
@@ -37,11 +37,13 @@ class BaseProvider {
37
37
  throw new Error('rotateLogs method is not implemented');
38
38
  }
39
39
 
40
- searchCache() {
40
+ // eslint-disable-next-line no-unused-vars
41
+ searchCache(pattern, group) {
41
42
  throw new Error('searchCache method is not implemented');
42
43
  }
43
44
 
44
- clearCache() {
45
+ // eslint-disable-next-line no-unused-vars
46
+ clearCache(group) {
45
47
  throw new Error('clearCache method is not implemented');
46
48
  }
47
49
 
@@ -85,10 +85,6 @@ const createRequestHandler = (id) => (req, res, target) => {
85
85
  req.headers['x-path-prefix'] = rule.prefix.replace(WELLKNOWN_SERVICE_PATH_PREFIX, '') || '/';
86
86
  req.headers['x-group-path-prefix'] = rule.groupPrefix || '/';
87
87
 
88
- if (rule.type === ROUTING_RULE_TYPES.BLOCKLET) {
89
- req.headers['x-blocklet-url'] = `http://127.0.0.1:${rule.port}`;
90
- }
91
-
92
88
  if (rule.ruleId) {
93
89
  req.headers['x-routing-rule-id'] = rule.ruleId;
94
90
  }
@@ -186,9 +186,10 @@ class NginxProvider extends BaseProvider {
186
186
  conf.on('flushed', () => resolve());
187
187
  conf.live(this.configPath);
188
188
 
189
- const { sites, configs: siteCorsConfigs } = formatRoutingTable(routingTable);
189
+ const { sites, cacheGroups, configs: siteCorsConfigs } = formatRoutingTable(routingTable);
190
190
 
191
191
  this._addCorsMap(conf, siteCorsConfigs);
192
+ this._addCacheGroups(conf, cacheGroups);
192
193
  conf.nginx.http._add('server_tokens', 'off');
193
194
  this._addCommonResHeaders(conf.nginx.http, commonHeaders);
194
195
  this._addExposeServices(conf, services);
@@ -512,9 +513,9 @@ class NginxProvider extends BaseProvider {
512
513
  // kill cache
513
514
  if (this.cacheEnabled === false) {
514
515
  location._add('expires', '-1');
515
- } else if (ROUTER_CACHE_GROUPS[cacheGroup]) {
516
+ } else if (cacheGroup) {
516
517
  location._add('proxy_cache', cacheGroup);
517
- location._add('proxy_cache_valid', `200 ${ROUTER_CACHE_GROUPS[cacheGroup].period}`);
518
+ location._add('proxy_cache_valid', `200 ${ROUTER_CACHE_GROUPS.blockletProxy.period}`);
518
519
  location._add('include', 'includes/cache');
519
520
  }
520
521
 
@@ -533,7 +534,6 @@ class NginxProvider extends BaseProvider {
533
534
  location._add('proxy_set_header', `X-Group-Path-Prefix "${groupPrefix}"`);
534
535
  }
535
536
 
536
- location._add('proxy_set_header', `X-Blocklet-Url "http://127.0.0.1:${port}"`);
537
537
  if (ruleId) {
538
538
  location._add('proxy_set_header', `X-Routing-Rule-Id "${ruleId}"`);
539
539
  }
@@ -884,6 +884,17 @@ class NginxProvider extends BaseProvider {
884
884
  }
885
885
  }
886
886
 
887
+ _addCacheGroups(conf, cacheGroups) {
888
+ const cacheDir = this.getRelativeConfigDir(formatBackSlash(this.cacheDir));
889
+ cacheGroups.forEach((group) => {
890
+ const config = ROUTER_CACHE_GROUPS.blockletProxy;
891
+ conf.nginx.http._add(
892
+ 'proxy_cache_path',
893
+ `${cacheDir}/${group} levels=1:2 keys_zone=${group}:${config.minSize} inactive=${config.period} max_size=${config.maxSize}`
894
+ );
895
+ });
896
+ }
897
+
887
898
  _addCorsMap(conf, siteCorsConfigs) {
888
899
  siteCorsConfigs.forEach((corsConfig) => {
889
900
  if (Array.isArray(corsConfig.corsAllowedOrigins) && corsConfig.corsAllowedOrigins.length > 0) {
@@ -942,8 +953,19 @@ class NginxProvider extends BaseProvider {
942
953
  return this.logDir;
943
954
  }
944
955
 
945
- searchCache(pattern) {
946
- const result = shelljs.exec(`grep -rl '^KEY:.*${pattern}' ${this.cacheDir}`, { silent: true });
956
+ /**
957
+ * Search cached files by group and pattern
958
+ * @param {string} pattern
959
+ * @param {string} group
960
+ * @returns {string[]} list of files cached match the pattern
961
+ */
962
+ searchCache(pattern, group = '') {
963
+ if (!pattern) {
964
+ return [];
965
+ }
966
+
967
+ const cacheDir = group ? path.join(this.cacheDir, group) : this.cacheDir;
968
+ const result = shelljs.exec(`grep -rl '^KEY:.*${pattern}' ${cacheDir}`, { silent: true });
947
969
  logger.info('search nginx cache', { pattern, result });
948
970
  if (result.stderr) {
949
971
  console.error('Failed to search nginx cache', result.stderr);
@@ -953,15 +975,18 @@ class NginxProvider extends BaseProvider {
953
975
  return result.stdout.split(os.EOL).filter(Boolean);
954
976
  }
955
977
 
956
- clearCache() {
978
+ /**
979
+ * Clear cache by group, if group is empty, clear all cache
980
+ * @param {string} group
981
+ * @returns {string[]} cleared cache files
982
+ */
983
+ clearCache(group = '') {
957
984
  try {
958
- return Object.keys(ROUTER_CACHE_GROUPS).map((key) => {
959
- const cacheDir = path.join(this.cacheDir, key);
960
- fs.rmSync(cacheDir, { recursive: true, force: true });
961
- fs.ensureDirSync(cacheDir);
962
- logger.info('nginx cache cleared', { key });
963
- return cacheDir;
964
- });
985
+ const cacheDir = group ? path.join(this.cacheDir, group) : this.cacheDir;
986
+ fs.rmSync(cacheDir, { recursive: true, force: true });
987
+ fs.ensureDirSync(cacheDir);
988
+ logger.info('nginx cache cleared', { group });
989
+ return [cacheDir];
965
990
  } catch (err) {
966
991
  console.error('Failed to clear nginx cache', err);
967
992
  return [];
package/lib/util.js CHANGED
@@ -3,6 +3,7 @@ const fs = require('fs-extra');
3
3
  const path = require('path');
4
4
  const getPort = require('get-port');
5
5
  const portUsed = require('port-used');
6
+ const uniq = require('lodash/uniq');
6
7
  const sortBy = require('lodash/sortBy');
7
8
  const isValidDomain = require('is-valid-domain');
8
9
  const checkDomainMatch = require('@abtnode/util/lib/check-domain-match');
@@ -51,6 +52,7 @@ const concatPath = (prefix = '', suffix = '', root = false) => {
51
52
 
52
53
  const formatRoutingTable = (routingTable) => {
53
54
  const sites = {};
55
+ const cacheGroups = [];
54
56
  const configs = [];
55
57
 
56
58
  routingTable.forEach((site) => {
@@ -66,6 +68,7 @@ const formatRoutingTable = (routingTable) => {
66
68
  configs.push({ domain, corsAllowedOrigins });
67
69
 
68
70
  if (!sites[domain]) {
71
+ cacheGroups.push(site.blockletDid);
69
72
  sites[domain] = {
70
73
  domain,
71
74
  blockletDid: site.blockletDid,
@@ -102,7 +105,7 @@ const formatRoutingTable = (routingTable) => {
102
105
  } else {
103
106
  rule.port = +x.to.port;
104
107
  rule.did = x.to.did;
105
- rule.cacheGroup = x.to.cacheGroup;
108
+ rule.cacheGroup = x.to.cacheGroup ? site.blockletDid || x.to.did : '';
106
109
  rule.componentId = x.to.componentId;
107
110
  rule.target = trimEndSlash(normalizePathPrefix(x.to.target || '/'));
108
111
  rule.targetPrefix = x.to.targetPrefix || '';
@@ -128,8 +131,9 @@ const formatRoutingTable = (routingTable) => {
128
131
  sites[domain].rules = rulesWithoutSuffix.concat(rulesWithSuffix);
129
132
  });
130
133
 
131
- return { sites: Object.values(sites), configs };
134
+ return { sites: Object.values(sites), cacheGroups: uniq(cacheGroups).filter(Boolean), configs };
132
135
  };
136
+
133
137
  /**
134
138
  *
135
139
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.16.15-beta-e143b1cf",
3
+ "version": "1.16.15-beta-12f50442",
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.16.15-beta-e143b1cf",
36
- "@abtnode/logger": "1.16.15-beta-e143b1cf",
37
- "@abtnode/router-templates": "1.16.15-beta-e143b1cf",
38
- "@abtnode/util": "1.16.15-beta-e143b1cf",
35
+ "@abtnode/constant": "1.16.15-beta-12f50442",
36
+ "@abtnode/logger": "1.16.15-beta-12f50442",
37
+ "@abtnode/router-templates": "1.16.15-beta-12f50442",
38
+ "@abtnode/util": "1.16.15-beta-12f50442",
39
39
  "@arcblock/http-proxy": "^1.19.1",
40
40
  "axios": "^0.27.2",
41
41
  "debug": "^4.3.4",
@@ -59,5 +59,5 @@
59
59
  "bluebird": "^3.7.2",
60
60
  "fs-extra": "^10.1.0"
61
61
  },
62
- "gitHead": "86dcc908f3cb7259d1ca92b9ae75160a6e43509a"
62
+ "gitHead": "48bda4b29eb31a70e0eb9cab0bce6e544960c7fd"
63
63
  }