@abtnode/router-provider 1.8.65-beta-5405baf2 → 1.8.65-beta-bfcc12ce

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.
@@ -0,0 +1,6 @@
1
+ proxy_cache_key $scheme$proxy_host$request_uri;
2
+ proxy_cache_bypass $cookie_nocache $arg_nocache;
3
+ proxy_cache_lock on;
4
+ proxy_cache_revalidate on;
5
+
6
+ add_header X-Cache-Status $upstream_cache_status;
@@ -18,7 +18,9 @@ const {
18
18
  DEFAULT_ADMIN_PATH,
19
19
  SLOT_FOR_IP_DNS_SITE,
20
20
  WELLKNOWN_SERVICE_PATH_PREFIX,
21
+ USER_AVATAR_PATH_PREFIX,
21
22
  LOG_RETAIN_IN_DAYS,
23
+ ROUTER_CACHE_GROUPS,
22
24
  } = require('@abtnode/constant');
23
25
  const md5 = require('@abtnode/util/lib/md5');
24
26
 
@@ -101,6 +103,7 @@ class NginxProvider extends BaseProvider {
101
103
  this.errorLog = path.join(this.logDir, 'error.log');
102
104
  this.tmpDir = path.join(this.configDir, 'tmp');
103
105
  this.certDir = path.join(this.configDir, 'certs');
106
+ this.cacheDir = path.join(this.configDir, 'cache');
104
107
  this.includesDir = path.join(this.configDir, 'includes');
105
108
  this.wwwDir = path.join(this.configDir, 'www');
106
109
 
@@ -120,7 +123,7 @@ class NginxProvider extends BaseProvider {
120
123
  });
121
124
 
122
125
  // ensure directories
123
- [this.configDir, this.logDir, this.tmpDir, this.certDir].forEach((dir) => {
126
+ [this.configDir, this.logDir, this.cacheDir, this.tmpDir, this.certDir].forEach((dir) => {
124
127
  if (!fs.existsSync(dir)) {
125
128
  fs.mkdirSync(dir);
126
129
  }
@@ -143,6 +146,7 @@ class NginxProvider extends BaseProvider {
143
146
  return getMainTemplate({
144
147
  logDir: this.getRelativeConfigDir(formatBackSlash(this.logDir)),
145
148
  tmpDir: this.getRelativeConfigDir(formatBackSlash(this.tmpDir)),
149
+ cacheDir: this.getRelativeConfigDir(formatBackSlash(this.cacheDir)),
146
150
  workerProcess: this.getWorkerProcess(),
147
151
  nginxLoadModules: getNginxLoadModuleDirectives(REQUIRED_MODULES, this.readNginxConfigParams()).join(os.EOL),
148
152
  });
@@ -449,6 +453,7 @@ class NginxProvider extends BaseProvider {
449
453
  ruleId,
450
454
  type,
451
455
  commonHeaders,
456
+ cacheGroup,
452
457
  }) {
453
458
  server._add('location', concatPath(prefix, suffix));
454
459
 
@@ -459,7 +464,9 @@ class NginxProvider extends BaseProvider {
459
464
  this._addCors({ location, corsAllowedOrigins, serverName, isDidAuthPath });
460
465
 
461
466
  this._addCommonResHeaders(location, commonHeaders);
462
- this._addTailSlashRedirection(location, prefix); // Note: 末尾 "/" 的重定向要放在 CORS(OPTIONS) 响应之后, 这样不会影响 OPTIONS 的响应
467
+ if (!cacheGroup && !suffix) {
468
+ this._addTailSlashRedirection(location, prefix); // Note: 末尾 "/" 的重定向要放在 CORS(OPTIONS) 响应之后, 这样不会影响 OPTIONS 的响应
469
+ }
463
470
 
464
471
  if (did) {
465
472
  location._add('set', `$did "${did}"`);
@@ -467,18 +474,22 @@ class NginxProvider extends BaseProvider {
467
474
  location._add('proxy_set_header', `X-Blocklet-Component-Id "${componentId}"`);
468
475
  }
469
476
 
477
+ location._add('include', 'includes/proxy');
478
+
470
479
  // kill cache
471
480
  // TODO: 这个放在管理端的 Gateway 配置中
472
481
  if (this.cacheDisabled) {
473
482
  location._add('expires', '-1');
483
+ } else if (ROUTER_CACHE_GROUPS[cacheGroup]) {
484
+ location._add('proxy_cache', cacheGroup);
485
+ location._add('proxy_cache_valid', `200 ${ROUTER_CACHE_GROUPS[cacheGroup].period}`);
486
+ location._add('include', 'includes/cache');
474
487
  }
475
488
 
476
- location._add('include', 'includes/proxy');
477
-
478
489
  // Redirect blocklet traffic
479
490
  if (type === ROUTING_RULE_TYPES.BLOCKLET) {
480
- // FIXME logic related to server gateway should not in provider
481
- const rewritePathPrefix = prefix.replace(WELLKNOWN_SERVICE_PATH_PREFIX, '') || '/';
491
+ // FIXME: logic related to server gateway should not in provider
492
+ const rewritePathPrefix = prefix.replace(WELLKNOWN_SERVICE_PATH_PREFIX, '').replace(USER_AVATAR_PATH_PREFIX, '');
482
493
 
483
494
  // Add header
484
495
 
package/lib/nginx/util.js CHANGED
@@ -8,7 +8,7 @@ const { NginxConfFile } = require('nginx-conf');
8
8
  const shelljs = require('shelljs');
9
9
  const findProcess = require('find-process');
10
10
  const moment = require('moment');
11
- const { MAX_UPLOAD_FILE_SIZE, MAX_NGINX_WORKER_CONNECTIONS } = require('@abtnode/constant');
11
+ const { MAX_UPLOAD_FILE_SIZE, MAX_NGINX_WORKER_CONNECTIONS, ROUTER_CACHE_GROUPS } = require('@abtnode/constant');
12
12
  const formatBackSlash = require('@abtnode/util/lib/format-back-slash');
13
13
  const { deleteOldLogfiles } = require('@abtnode/logger');
14
14
 
@@ -150,6 +150,7 @@ const getWorkerConnectionCount = (maxWorkerConnections, workerProcess) => {
150
150
  const getMainTemplate = ({
151
151
  logDir,
152
152
  tmpDir,
153
+ cacheDir,
153
154
  nginxLoadModules = '',
154
155
  workerProcess,
155
156
  maxBodySize = CLIENT_MAX_BODY_SIZE,
@@ -189,8 +190,22 @@ http {
189
190
  include includes/params;
190
191
  include includes/mime.types;
191
192
  include includes/compression;
193
+
194
+ ${Object.keys(ROUTER_CACHE_GROUPS)
195
+ .map((x) => getCachePathDirective(cacheDir, x))
196
+ .join('\n')}
192
197
  }
193
198
  `;
199
+
200
+ const getCachePathDirective = (cacheDir, group) => {
201
+ if (!ROUTER_CACHE_GROUPS[group]) {
202
+ return '';
203
+ }
204
+
205
+ const config = ROUTER_CACHE_GROUPS[group];
206
+ return `proxy_cache_path ${cacheDir}/${group} levels=1:2 keys_zone=${group}:${config.minSize} inactive=${config.period} max_size=${config.maxSize};`;
207
+ };
208
+
194
209
  const getNginxStatus = async (configPath) => {
195
210
  const list = await findProcess('name', /nginx:/);
196
211
  const result = {
@@ -299,6 +314,7 @@ module.exports = {
299
314
  rotateNginxLogFile,
300
315
  getUserGroup,
301
316
  getWorkerConnectionCount,
317
+ getCachePathDirective,
302
318
  getUpstreamName,
303
319
  joinNginxPath,
304
320
  };
package/lib/util.js CHANGED
@@ -86,6 +86,7 @@ const formatRoutingTable = (routingTable, onRule) => {
86
86
  } else {
87
87
  rule.port = +x.to.port;
88
88
  rule.did = x.to.did;
89
+ rule.cacheGroup = x.to.cacheGroup;
89
90
  rule.componentId = x.to.componentId;
90
91
  rule.target = trimEndSlash(normalizePathPrefix(x.to.target || '/'));
91
92
  rule.services = x.services || [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.8.65-beta-5405baf2",
3
+ "version": "1.8.65-beta-bfcc12ce",
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.8.65-beta-5405baf2",
36
- "@abtnode/logger": "1.8.65-beta-5405baf2",
37
- "@abtnode/router-templates": "1.8.65-beta-5405baf2",
38
- "@abtnode/util": "1.8.65-beta-5405baf2",
35
+ "@abtnode/constant": "1.8.65-beta-bfcc12ce",
36
+ "@abtnode/logger": "1.8.65-beta-bfcc12ce",
37
+ "@abtnode/router-templates": "1.8.65-beta-bfcc12ce",
38
+ "@abtnode/util": "1.8.65-beta-bfcc12ce",
39
39
  "axios": "^0.27.2",
40
40
  "debug": "^4.3.4",
41
41
  "find-process": "^1.4.7",
@@ -62,5 +62,5 @@
62
62
  "fs-extra": "^10.1.0",
63
63
  "needle": "^3.1.0"
64
64
  },
65
- "gitHead": "e5dc838baded7031096118462a604fd70d78ff1c"
65
+ "gitHead": "f6a56512275a55feecf2f3953542ea98777a08f2"
66
66
  }