@abtnode/router-provider 1.16.25-next-bc2f4c63 → 1.16.26-beta-e0056285
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/default/proxy.js +27 -16
- package/package.json +6 -6
package/lib/default/proxy.js
CHANGED
|
@@ -6,7 +6,18 @@ const https = require('https');
|
|
|
6
6
|
const httpProxy = require('@arcblock/http-proxy');
|
|
7
7
|
const validUrl = require('valid-url');
|
|
8
8
|
const path = require('path');
|
|
9
|
-
const
|
|
9
|
+
const clone = require('lodash/clone');
|
|
10
|
+
const remove = require('lodash/remove');
|
|
11
|
+
const uniq = require('lodash/uniq');
|
|
12
|
+
const find = require('lodash/find');
|
|
13
|
+
const flatten = require('lodash/flatten');
|
|
14
|
+
const map = require('lodash/map');
|
|
15
|
+
const sortBy = require('lodash/sortBy');
|
|
16
|
+
const defaults = require('lodash/defaults');
|
|
17
|
+
const isArray = require('lodash/isArray');
|
|
18
|
+
const isString = require('lodash/isString');
|
|
19
|
+
const isObject = require('lodash/isObject');
|
|
20
|
+
const isFunction = require('lodash/isFunction');
|
|
10
21
|
const hash = require('object-hash');
|
|
11
22
|
const LRUCache = require('lru-cache');
|
|
12
23
|
const tls = require('tls');
|
|
@@ -16,8 +27,8 @@ const logger = require('@abtnode/logger')('router:default:proxy', { filename: 'e
|
|
|
16
27
|
|
|
17
28
|
const ensureHttp = (str) => (!str.startsWith('http://') && !str.startsWith('https://') ? `http://${str}` : str);
|
|
18
29
|
const prepareUrl = (url) => {
|
|
19
|
-
url =
|
|
20
|
-
if (
|
|
30
|
+
url = clone(url);
|
|
31
|
+
if (isString(url)) {
|
|
21
32
|
url = ensureHttp(url);
|
|
22
33
|
if (!validUrl.isHttpUri(url) && !validUrl.isHttpsUri(url)) {
|
|
23
34
|
throw new Error(`uri is not a valid http uri ${url}`);
|
|
@@ -182,7 +193,7 @@ module.exports = class ReverseProxy {
|
|
|
182
193
|
}
|
|
183
194
|
|
|
184
195
|
if (sslOpts.opts) {
|
|
185
|
-
ssl =
|
|
196
|
+
ssl = defaults(ssl, sslOpts.opts);
|
|
186
197
|
}
|
|
187
198
|
|
|
188
199
|
this.httpsServer = https.createServer(ssl, (req, res) => {
|
|
@@ -217,7 +228,7 @@ module.exports = class ReverseProxy {
|
|
|
217
228
|
addResolver(_resolver) {
|
|
218
229
|
const resolver = Array.isArray(_resolver) ? _resolver : [_resolver];
|
|
219
230
|
resolver.forEach((resolveObj) => {
|
|
220
|
-
if (!
|
|
231
|
+
if (!isFunction(resolveObj)) {
|
|
221
232
|
throw new Error('Resolver must be an invokable function.');
|
|
222
233
|
}
|
|
223
234
|
|
|
@@ -228,7 +239,7 @@ module.exports = class ReverseProxy {
|
|
|
228
239
|
this.resolvers.push(resolveObj);
|
|
229
240
|
});
|
|
230
241
|
|
|
231
|
-
this.resolvers =
|
|
242
|
+
this.resolvers = sortBy(uniq(this.resolvers), ['priority']).reverse();
|
|
232
243
|
}
|
|
233
244
|
|
|
234
245
|
removeResolver(resolver) {
|
|
@@ -292,14 +303,14 @@ module.exports = class ReverseProxy {
|
|
|
292
303
|
|
|
293
304
|
const host = this.routing[src.hostname];
|
|
294
305
|
const pathname = src.pathname || '/';
|
|
295
|
-
let route =
|
|
306
|
+
let route = find(host, { path: pathname });
|
|
296
307
|
|
|
297
308
|
if (!route) {
|
|
298
309
|
route = { path: pathname, rr: 0, urls: [], opts: Object.assign({}, opts) };
|
|
299
310
|
host.push(route);
|
|
300
311
|
|
|
301
312
|
// Sort routes
|
|
302
|
-
this.routing[src.hostname] =
|
|
313
|
+
this.routing[src.hostname] = sortBy(host, (x) => -x.path.length);
|
|
303
314
|
}
|
|
304
315
|
|
|
305
316
|
route.urls.push(target);
|
|
@@ -329,7 +340,7 @@ module.exports = class ReverseProxy {
|
|
|
329
340
|
|
|
330
341
|
if (target) {
|
|
331
342
|
target = prepareUrl(target);
|
|
332
|
-
|
|
343
|
+
remove(route.urls, (x) => x.href === target.href);
|
|
333
344
|
} else {
|
|
334
345
|
route.urls = [];
|
|
335
346
|
}
|
|
@@ -403,24 +414,24 @@ module.exports = class ReverseProxy {
|
|
|
403
414
|
}
|
|
404
415
|
|
|
405
416
|
static buildRoute(route) {
|
|
406
|
-
if (!
|
|
417
|
+
if (!isString(route) && !isObject(route)) {
|
|
407
418
|
return null;
|
|
408
419
|
}
|
|
409
420
|
|
|
410
421
|
// default route type matched.
|
|
411
|
-
if (
|
|
422
|
+
if (isObject(route) && route.urls && route.path) {
|
|
412
423
|
return route;
|
|
413
424
|
}
|
|
414
425
|
|
|
415
426
|
// to bust cache for route, you can attach an id to the route object in business layer
|
|
416
|
-
const cacheKey =
|
|
427
|
+
const cacheKey = isString(route) ? route : hash(route);
|
|
417
428
|
const entry = routeCache.get(cacheKey);
|
|
418
429
|
if (entry) {
|
|
419
430
|
return entry;
|
|
420
431
|
}
|
|
421
432
|
|
|
422
433
|
const routeObject = { rr: 0, isResolved: true };
|
|
423
|
-
if (
|
|
434
|
+
if (isString(route)) {
|
|
424
435
|
routeObject.urls = [ReverseProxy.buildTarget(route)];
|
|
425
436
|
routeObject.path = '/';
|
|
426
437
|
} else {
|
|
@@ -428,7 +439,7 @@ module.exports = class ReverseProxy {
|
|
|
428
439
|
return null;
|
|
429
440
|
}
|
|
430
441
|
|
|
431
|
-
routeObject.urls = (
|
|
442
|
+
routeObject.urls = (isArray(route.url) ? route.url : [route.url]).map((url) =>
|
|
432
443
|
ReverseProxy.buildTarget(url, route.opts || {})
|
|
433
444
|
);
|
|
434
445
|
|
|
@@ -531,9 +542,9 @@ module.exports = class ReverseProxy {
|
|
|
531
542
|
_getCertData(source, unbundle) {
|
|
532
543
|
let data;
|
|
533
544
|
if (source) {
|
|
534
|
-
if (
|
|
545
|
+
if (isArray(source)) {
|
|
535
546
|
const sources = source;
|
|
536
|
-
return
|
|
547
|
+
return flatten(map(sources, (_source) => this._getCertData(_source, unbundle)));
|
|
537
548
|
}
|
|
538
549
|
if (Buffer.isBuffer(source)) {
|
|
539
550
|
data = source.toString('utf8');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abtnode/router-provider",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.26-beta-e0056285",
|
|
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.
|
|
36
|
-
"@abtnode/logger": "1.16.
|
|
37
|
-
"@abtnode/router-templates": "1.16.
|
|
38
|
-
"@abtnode/util": "1.16.
|
|
35
|
+
"@abtnode/constant": "1.16.26-beta-e0056285",
|
|
36
|
+
"@abtnode/logger": "1.16.26-beta-e0056285",
|
|
37
|
+
"@abtnode/router-templates": "1.16.26-beta-e0056285",
|
|
38
|
+
"@abtnode/util": "1.16.26-beta-e0056285",
|
|
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": "^11.2.0"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "a79b8637ad50d136e7c5d91bfb41dc7a7c337485"
|
|
63
63
|
}
|