@abtnode/router-provider 1.16.15-beta-f60064f3 → 1.16.15-beta-103b95c0
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 +4 -2
- package/lib/default/daemon.js +0 -4
- package/lib/nginx/index.js +46 -22
- package/lib/util.js +6 -2
- package/package.json +6 -6
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
|
-
|
|
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
|
-
|
|
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
|
|
package/lib/default/daemon.js
CHANGED
|
@@ -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
|
}
|
package/lib/nginx/index.js
CHANGED
|
@@ -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);
|
|
@@ -467,7 +468,6 @@ class NginxProvider extends BaseProvider {
|
|
|
467
468
|
*/
|
|
468
469
|
_addBlockletTypeLocation({
|
|
469
470
|
server,
|
|
470
|
-
serverName,
|
|
471
471
|
port,
|
|
472
472
|
root,
|
|
473
473
|
prefix,
|
|
@@ -489,7 +489,7 @@ class NginxProvider extends BaseProvider {
|
|
|
489
489
|
const location = this._getLastLocation(server);
|
|
490
490
|
|
|
491
491
|
// Note: 下面这段代码比较 tricky,不要在这段代码之前添加任何 add_header, proxy_set_header, proxy_hide_header 的语句,否则 nginx 配置可能无法按预期工作
|
|
492
|
-
this._addCors({ location, corsAllowedOrigins
|
|
492
|
+
this._addCors({ location, corsAllowedOrigins });
|
|
493
493
|
|
|
494
494
|
this._addCommonResHeaders(location, commonHeaders);
|
|
495
495
|
if (!cacheGroup && !suffix) {
|
|
@@ -512,9 +512,9 @@ class NginxProvider extends BaseProvider {
|
|
|
512
512
|
// kill cache
|
|
513
513
|
if (this.cacheEnabled === false) {
|
|
514
514
|
location._add('expires', '-1');
|
|
515
|
-
} else if (
|
|
515
|
+
} else if (cacheGroup) {
|
|
516
516
|
location._add('proxy_cache', cacheGroup);
|
|
517
|
-
location._add('proxy_cache_valid', `200 ${ROUTER_CACHE_GROUPS
|
|
517
|
+
location._add('proxy_cache_valid', `200 ${ROUTER_CACHE_GROUPS.blockletProxy.period}`);
|
|
518
518
|
location._add('include', 'includes/cache');
|
|
519
519
|
}
|
|
520
520
|
|
|
@@ -533,7 +533,6 @@ class NginxProvider extends BaseProvider {
|
|
|
533
533
|
location._add('proxy_set_header', `X-Group-Path-Prefix "${groupPrefix}"`);
|
|
534
534
|
}
|
|
535
535
|
|
|
536
|
-
location._add('proxy_set_header', `X-Blocklet-Url "http://127.0.0.1:${port}"`);
|
|
537
536
|
if (ruleId) {
|
|
538
537
|
location._add('proxy_set_header', `X-Routing-Rule-Id "${ruleId}"`);
|
|
539
538
|
}
|
|
@@ -558,12 +557,12 @@ class NginxProvider extends BaseProvider {
|
|
|
558
557
|
location._add('proxy_pass', `http://${getUpstreamName(port)}`);
|
|
559
558
|
}
|
|
560
559
|
|
|
561
|
-
_addRedirectTypeLocation({ server, url, redirectCode, prefix, suffix, corsAllowedOrigins
|
|
560
|
+
_addRedirectTypeLocation({ server, url, redirectCode, prefix, suffix, corsAllowedOrigins }) {
|
|
562
561
|
const cleanUrl = trimEndSlash(url);
|
|
563
562
|
server._add('location', `${concatPath(prefix, suffix)}`);
|
|
564
563
|
const location = this._getLastLocation(server);
|
|
565
564
|
|
|
566
|
-
this._addCors({ location, corsAllowedOrigins
|
|
565
|
+
this._addCors({ location, corsAllowedOrigins });
|
|
567
566
|
|
|
568
567
|
location._add('set $abt_query_string', '""');
|
|
569
568
|
location._addVerbatimBlock('if ($query_string)', 'set $abt_query_string "?$query_string";');
|
|
@@ -581,10 +580,10 @@ class NginxProvider extends BaseProvider {
|
|
|
581
580
|
}
|
|
582
581
|
}
|
|
583
582
|
|
|
584
|
-
_addRewriteTypeLocation({ server, url, prefix, suffix, corsAllowedOrigins
|
|
583
|
+
_addRewriteTypeLocation({ server, url, prefix, suffix, corsAllowedOrigins }) {
|
|
585
584
|
server._add('location', concatPath(prefix, suffix));
|
|
586
585
|
const location = this._getLastLocation(server);
|
|
587
|
-
this._addCors({ location, corsAllowedOrigins
|
|
586
|
+
this._addCors({ location, corsAllowedOrigins });
|
|
588
587
|
location._add('rewrite', `^${prefix}(.*) ${url}$1 last`);
|
|
589
588
|
}
|
|
590
589
|
|
|
@@ -884,6 +883,17 @@ class NginxProvider extends BaseProvider {
|
|
|
884
883
|
}
|
|
885
884
|
}
|
|
886
885
|
|
|
886
|
+
_addCacheGroups(conf, cacheGroups) {
|
|
887
|
+
const cacheDir = this.getRelativeConfigDir(formatBackSlash(this.cacheDir));
|
|
888
|
+
cacheGroups.forEach((group) => {
|
|
889
|
+
const config = ROUTER_CACHE_GROUPS.blockletProxy;
|
|
890
|
+
conf.nginx.http._add(
|
|
891
|
+
'proxy_cache_path',
|
|
892
|
+
`${cacheDir}/${group} levels=1:2 keys_zone=${group}:${config.minSize} inactive=${config.period} max_size=${config.maxSize}`
|
|
893
|
+
);
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
|
|
887
897
|
_addCorsMap(conf, siteCorsConfigs) {
|
|
888
898
|
siteCorsConfigs.forEach((corsConfig) => {
|
|
889
899
|
if (Array.isArray(corsConfig.corsAllowedOrigins) && corsConfig.corsAllowedOrigins.length > 0) {
|
|
@@ -905,14 +915,14 @@ class NginxProvider extends BaseProvider {
|
|
|
905
915
|
});
|
|
906
916
|
}
|
|
907
917
|
|
|
908
|
-
_addCors({ location, corsAllowedOrigins
|
|
918
|
+
_addCors({ location, corsAllowedOrigins }) {
|
|
909
919
|
if (!isEmpty(corsAllowedOrigins)) {
|
|
910
920
|
if (corsAllowedOrigins.includes(DOMAIN_FOR_DEFAULT_SITE)) {
|
|
911
921
|
location._add('include', 'includes/cors-loose');
|
|
912
922
|
location._add('include', 'includes/security');
|
|
913
923
|
} else {
|
|
914
924
|
// TODO: @zhanghan 此处是否需要变更默认的值,如果在逻辑代码中有指定的 Access-Control-Allow-Origin,则应该优先遵守逻辑代码中的配置
|
|
915
|
-
location._add('add_header', `Access-Control-Allow-Origin $allow_origin_${md5(serverName)} always`);
|
|
925
|
+
// location._add('add_header', `Access-Control-Allow-Origin $allow_origin_${md5(serverName)} always`); // TODO: zhenqiang 下面去掉了 OPTIONS 请求逻辑,这里也不应该处理 Access-Control-Allow-Origin
|
|
916
926
|
location._add('include', 'includes/cors-strict');
|
|
917
927
|
location._add('include', 'includes/security');
|
|
918
928
|
}
|
|
@@ -942,8 +952,19 @@ class NginxProvider extends BaseProvider {
|
|
|
942
952
|
return this.logDir;
|
|
943
953
|
}
|
|
944
954
|
|
|
945
|
-
|
|
946
|
-
|
|
955
|
+
/**
|
|
956
|
+
* Search cached files by group and pattern
|
|
957
|
+
* @param {string} pattern
|
|
958
|
+
* @param {string} group
|
|
959
|
+
* @returns {string[]} list of files cached match the pattern
|
|
960
|
+
*/
|
|
961
|
+
searchCache(pattern, group = '') {
|
|
962
|
+
if (!pattern) {
|
|
963
|
+
return [];
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
const cacheDir = group ? path.join(this.cacheDir, group) : this.cacheDir;
|
|
967
|
+
const result = shelljs.exec(`grep -rl '^KEY:.*${pattern}' ${cacheDir}`, { silent: true });
|
|
947
968
|
logger.info('search nginx cache', { pattern, result });
|
|
948
969
|
if (result.stderr) {
|
|
949
970
|
console.error('Failed to search nginx cache', result.stderr);
|
|
@@ -953,15 +974,18 @@ class NginxProvider extends BaseProvider {
|
|
|
953
974
|
return result.stdout.split(os.EOL).filter(Boolean);
|
|
954
975
|
}
|
|
955
976
|
|
|
956
|
-
|
|
977
|
+
/**
|
|
978
|
+
* Clear cache by group, if group is empty, clear all cache
|
|
979
|
+
* @param {string} group
|
|
980
|
+
* @returns {string[]} cleared cache files
|
|
981
|
+
*/
|
|
982
|
+
clearCache(group = '') {
|
|
957
983
|
try {
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
return cacheDir;
|
|
964
|
-
});
|
|
984
|
+
const cacheDir = group ? path.join(this.cacheDir, group) : this.cacheDir;
|
|
985
|
+
fs.rmSync(cacheDir, { recursive: true, force: true });
|
|
986
|
+
fs.ensureDirSync(cacheDir);
|
|
987
|
+
logger.info('nginx cache cleared', { group });
|
|
988
|
+
return [cacheDir];
|
|
965
989
|
} catch (err) {
|
|
966
990
|
console.error('Failed to clear nginx cache', err);
|
|
967
991
|
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-
|
|
3
|
+
"version": "1.16.15-beta-103b95c0",
|
|
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-
|
|
36
|
-
"@abtnode/logger": "1.16.15-beta-
|
|
37
|
-
"@abtnode/router-templates": "1.16.15-beta-
|
|
38
|
-
"@abtnode/util": "1.16.15-beta-
|
|
35
|
+
"@abtnode/constant": "1.16.15-beta-103b95c0",
|
|
36
|
+
"@abtnode/logger": "1.16.15-beta-103b95c0",
|
|
37
|
+
"@abtnode/router-templates": "1.16.15-beta-103b95c0",
|
|
38
|
+
"@abtnode/util": "1.16.15-beta-103b95c0",
|
|
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": "
|
|
62
|
+
"gitHead": "ba3bbf1566e9c3d62dbdf93d2ede8a1d4249c407"
|
|
63
63
|
}
|