From 2362310aba09df62c1ae2539f71e5774307852a2 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Thu, 22 Nov 2018 13:39:39 +0100 Subject: [PATCH] feat: stop hit proxies on search web UI (#1126) Since we look for metadata on sidebar does not make sense to hit the proxies on search, this is the first step to help with other tickets to search in the whole proxy data packages, now is limited to private packages. Unit test are not need it here since is an "assumed" behaviour we don't want anymore. This will clearly help to improve performance on UI. --- src/api/endpoint/api/dist-tags.js | 1 + src/api/endpoint/api/package.js | 1 + src/api/web/endpoint/package.js | 2 ++ src/api/web/endpoint/search.js | 1 + src/lib/storage.js | 15 ++++++++++----- types/index.js | 13 +++++++++++++ 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/api/endpoint/api/dist-tags.js b/src/api/endpoint/api/dist-tags.js index 1a5baa027..436776e8d 100644 --- a/src/api/endpoint/api/dist-tags.js +++ b/src/api/endpoint/api/dist-tags.js @@ -52,6 +52,7 @@ export default function(route: Router, auth: IAuth, storage: IStorageHandler) { route.get('/-/package/:package/dist-tags', can('access'), function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) { storage.getPackage({ name: req.params.package, + uplinksLook: true, req, callback: function(err, info) { if (err) { diff --git a/src/api/endpoint/api/package.js b/src/api/endpoint/api/package.js index c5432b42d..c51bfd504 100644 --- a/src/api/endpoint/api/package.js +++ b/src/api/endpoint/api/package.js @@ -45,6 +45,7 @@ export default function(route: Router, auth: IAuth, storage: IStorageHandler, co storage.getPackage({ name: req.params.package, + uplinksLook: true, req, callback: getPackageMetaCallback, }); diff --git a/src/api/web/endpoint/package.js b/src/api/web/endpoint/package.js index a9d721829..dd0ff7b5e 100644 --- a/src/api/web/endpoint/package.js +++ b/src/api/web/endpoint/package.js @@ -60,6 +60,7 @@ function addPackageWebApi(route: Router, storage: IStorageHandler, auth: IAuth) storage.getPackage({ name: packageName, + uplinksLook: true, req, callback: function(err, info) { if (err) { @@ -77,6 +78,7 @@ function addPackageWebApi(route: Router, storage: IStorageHandler, auth: IAuth) storage.getPackage({ name: packageName, + uplinksLook: true, keepUpLinkData: true, req, callback: function(err: Error, info: $SidebarPackage) { diff --git a/src/api/web/endpoint/search.js b/src/api/web/endpoint/search.js index eca300172..b40780b56 100644 --- a/src/api/web/endpoint/search.js +++ b/src/api/web/endpoint/search.js @@ -17,6 +17,7 @@ function addSearchWebApi(route: Router, storage: IStorageHandler, auth: IAuth) { const getPackageInfo = function(i) { storage.getPackage({ name: results[i].ref, + uplinksLook: false, callback: (err, entry) => { if (!err && entry) { auth.allow_access(entry.name, req.remote_user, function(err, allowed) { diff --git a/src/lib/storage.js b/src/lib/storage.js index 5c631685b..96c41fa2a 100644 --- a/src/lib/storage.js +++ b/src/lib/storage.js @@ -16,7 +16,7 @@ import { checkPackageLocal, publishPackage, checkPackageRemote, cleanUpLinksRef, import { setupUpLinks, updateVersionsHiddenUpLink } from './uplink-util'; import { mergeVersions } from './metadata-utils'; import { ErrorCode, normalizeDistTags, validateMetadata, isObject } from './utils'; -import type { IStorage, IProxy, IStorageHandler, ProxyList, StringValue } from '../../types'; +import type { IStorage, IProxy, IStorageHandler, ProxyList, StringValue, IGetPackageOptions, ISyncUplinks } from '../../types'; import type { Versions, Package, Config, MergeTags, Version, DistFile, Callback, Logger } from '@verdaccio/types'; import type { IReadTarball, IUploadTarball } from '@verdaccio/streams'; import { hasProxyTo } from './config-utils'; @@ -263,14 +263,18 @@ class Storage implements IStorageHandler { * @property {boolean} options.keepUpLinkData keep up link info in package meta, last update, etc. * @property {function} options.callback Callback for receive data */ - getPackage(options: any) { + getPackage(options: IGetPackageOptions) { this.localStorage.getPackageMetadata(options.name, (err, data) => { if (err && (!err.status || err.status >= HTTP_STATUS.INTERNAL_ERROR)) { // report internal errors right away return options.callback(err); } - this._syncUplinksMetadata(options.name, data, { req: options.req }, function getPackageSynUpLinksCallback(err, result: Package, uplinkErrors) { + this._syncUplinksMetadata(options.name, data, { req: options.req, uplinksLook: options.uplinksLook }, function getPackageSynUpLinksCallback( + err, + result: Package, + uplinkErrors + ) { if (err) { return options.callback(err); } @@ -401,10 +405,11 @@ class Storage implements IStorageHandler { if package is available locally, it MUST be provided in pkginfo returns callback(err, result, uplink_errors) */ - _syncUplinksMetadata(name: string, packageInfo: Package, options: any, callback: Callback): void { + _syncUplinksMetadata(name: string, packageInfo: Package, options: ISyncUplinks, callback: Callback): void { let exists = true; const self = this; const upLinks = []; + const hasToLookIntoUplinks = _.isNil(options.uplinksLook) || options.uplinksLook; if (!packageInfo || packageInfo === null) { exists = false; @@ -412,7 +417,7 @@ class Storage implements IStorageHandler { } for (let uplink in this.uplinks) { - if (hasProxyTo(name, uplink, this.config.packages)) { + if (hasProxyTo(name, uplink, this.config.packages) && hasToLookIntoUplinks) { upLinks.push(this.uplinks[uplink]); } } diff --git a/types/index.js b/types/index.js index 796212d31..67ab5b013 100644 --- a/types/index.js +++ b/types/index.js @@ -135,6 +135,19 @@ export interface IStorage extends IBasicStorage { logger: Logger; } +export type IGetPackageOptions = { + callback: Callback; + name: string; + keepUpLinkData: boolean; + uplinksLook: boolean; + req: any; +} + +export type ISyncUplinks = { + uplinksLook?: boolean; + etag?: string; +} + export interface IStorageHandler extends IStorageManager { localStorage: IStorage; uplinks: ProxyList;