@verdaccio/store 7.0.0-next-7.11 → 7.0.0-next-7.13

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/build/storage.js CHANGED
@@ -16,14 +16,12 @@ var _core = require("@verdaccio/core");
16
16
  var _loaders = require("@verdaccio/loaders");
17
17
  var _logger = require("@verdaccio/logger");
18
18
  var _proxy = require("@verdaccio/proxy");
19
+ var _search = _interopRequireDefault(require("@verdaccio/search"));
19
20
  var _tarball = require("@verdaccio/tarball");
20
21
  var _utils = require("@verdaccio/utils");
21
22
  var _2 = require(".");
22
- var _TransFormResults = require("./lib/TransFormResults");
23
- var _searchUtils = require("./lib/search-utils");
24
23
  var _starUtils = require("./lib/star-utils");
25
24
  var _storageUtils = require("./lib/storage-utils");
26
- var _uplinkUtil = require("./lib/uplink-util");
27
25
  var _versionsUtils = require("./lib/versions-utils");
28
26
  var _localStorage = require("./local-storage");
29
27
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
@@ -36,10 +34,11 @@ const PROTO_NAME = exports.PROTO_NAME = '__proto__';
36
34
  class Storage {
37
35
  constructor(config) {
38
36
  this.config = config;
39
- this.uplinks = (0, _uplinkUtil.setupUpLinks)(config);
40
37
  this.logger = _logger.logger.child({
41
38
  module: 'storage'
42
39
  });
40
+ this.uplinks = (0, _proxy.setupUpLinks)(config, this.logger);
41
+ this.searchService = new _search.default(config, this.logger);
43
42
  this.filters = null;
44
43
  // @ts-ignore
45
44
  this.localStorage = null;
@@ -164,74 +163,19 @@ class Storage {
164
163
  /**
165
164
  * Handle search on packages and proxies.
166
165
  * Iterate all proxies configured and search in all endpoints in v2 and pipe all responses
167
- * to a stream, once the proxies request has finished search in local storage for all packages
166
+ * once the proxies request has finished search in local storage for all packages
168
167
  * (privated and cached).
169
168
  */
170
169
  async search(options) {
171
- const transformResults = new _TransFormResults.TransFormResults({
172
- objectMode: true
173
- });
174
- const streamPassThrough = new _stream.PassThrough({
175
- objectMode: true
176
- });
177
- const upLinkList = this.getProxyList();
178
- debug('uplinks found %s', upLinkList.length);
179
- const searchUplinksStreams = upLinkList.map(uplinkId => {
180
- const uplink = this.uplinks[uplinkId];
181
- if (!uplink) {
182
- // this line should never happens
183
- this.logger.error({
184
- uplinkId
185
- }, 'uplink @upLinkId not found');
186
- }
187
- return this.consumeSearchStream(uplinkId, uplink, options, streamPassThrough);
188
- });
189
- try {
190
- debug('searching on %s uplinks...', searchUplinksStreams === null || searchUplinksStreams === void 0 ? void 0 : searchUplinksStreams.length);
191
- // only process those streams end successfully, if all request fails
192
- // just include local storage results (if local fails then return 500)
193
- await Promise.allSettled([...searchUplinksStreams]);
194
- debug('searching all uplinks done');
195
- } catch (err) {
196
- this.logger.error({
197
- err: err === null || err === void 0 ? void 0 : err.message
198
- }, ' error on uplinks search @{err}');
199
- streamPassThrough.emit('error', err);
200
- }
201
- debug('search local');
202
- try {
203
- await this.searchCachedPackages(streamPassThrough, options.query);
204
- } catch (err) {
205
- this.logger.error({
206
- err: err === null || err === void 0 ? void 0 : err.message
207
- }, ' error on local search @{err}');
208
- streamPassThrough.emit('error', err);
209
- }
210
- const data = [];
211
- const outPutStream = new _stream.PassThrough({
212
- objectMode: true
213
- });
214
- (0, _stream.pipeline)(streamPassThrough, transformResults, outPutStream, err => {
215
- if (err) {
216
- this.logger.error({
217
- err: err === null || err === void 0 ? void 0 : err.message
218
- }, ' error on search @{err}');
219
- throw _core.errorUtils.getInternalError(err ? err.message : 'unknown search error');
220
- } else {
221
- debug('pipeline succeeded');
222
- }
223
- });
224
- outPutStream.on('data', chunk => {
225
- data.push(chunk);
226
- });
227
- return new Promise(resolve => {
228
- outPutStream.on('finish', async () => {
229
- const searchFinalResults = (0, _searchUtils.removeDuplicates)(data);
230
- debug('search stream total results: %o', searchFinalResults.length);
231
- return resolve(searchFinalResults);
232
- });
233
- debug('search done');
234
- });
170
+ debug('search on cache packages');
171
+ const cachePackages = await this.getCachedPackages(options.query);
172
+ debug('search found on cache packages %o', cachePackages.length);
173
+ const remotePackages = await this.searchService.search(options);
174
+ debug('search found on remote packages %o', remotePackages.length);
175
+ const totalResults = [...cachePackages, ...remotePackages];
176
+ const uniqueResults = (0, _versionsUtils.removeLowerVersions)(totalResults);
177
+ debug('unique results %o', uniqueResults.length);
178
+ return uniqueResults;
235
179
  }
236
180
  async getTarballFromUpstream(name, filename, {
237
181
  signal
@@ -313,7 +257,7 @@ class Storage {
313
257
  // should not be the case
314
258
  const passThroughRemoteStream = new _stream.PassThrough();
315
259
  // ensure get the latest data
316
- const [updatedManifest] = await this.syncUplinksMetadataNext(name, cachedManifest, {
260
+ const [updatedManifest] = await this.syncUplinksMetadata(name, cachedManifest, {
317
261
  uplinksLook: true
318
262
  });
319
263
  const distFile = updatedManifest._distfiles[filename];
@@ -356,7 +300,7 @@ class Storage {
356
300
  * @param param2
357
301
  * @returns
358
302
  */
359
- async getTarballNext(name, filename, {
303
+ async getTarball(name, filename, {
360
304
  signal
361
305
  }) {
362
306
  debug('get tarball for package %o filename %o', name, filename);
@@ -568,32 +512,11 @@ class Storage {
568
512
  }, plugin => {
569
513
  return typeof plugin.filter_metadata !== 'undefined';
570
514
  }, (_this$config = this.config) === null || _this$config === void 0 ? void 0 : (_this$config$serverSe = _this$config.serverSettings) === null || _this$config$serverSe === void 0 ? void 0 : _this$config$serverSe.pluginPrefix);
571
- debug('filters available %o', this.filters);
515
+ debug('filters available %o', this.filters.length);
572
516
  }
573
517
  return;
574
518
  }
575
519
 
576
- /**
577
- * Consume the upstream and pipe it to a transformable stream.
578
- */
579
- consumeSearchStream(uplinkId, uplink, options, searchPassThrough) {
580
- return uplink.search({
581
- ...options
582
- }).then(bodyStream => {
583
- bodyStream.pipe(searchPassThrough, {
584
- end: false
585
- });
586
- bodyStream.on('error', err => {
587
- _logger.logger.error({
588
- uplinkId,
589
- err: err
590
- }, 'search error for uplink @{uplinkId}: @{err?.message}');
591
- searchPassThrough.end();
592
- });
593
- return new Promise(resolve => bodyStream.on('end', resolve));
594
- });
595
- }
596
-
597
520
  /**
598
521
  * Retrieve a wrapper that provide access to the package location.
599
522
  * @param {Object} pkgName package name.
@@ -623,9 +546,14 @@ class Storage {
623
546
  signal
624
547
  });
625
548
  }
626
- async searchCachedPackages(searchStream, query) {
627
- debug('search on each package');
628
- this.logger.info({
549
+ async getCachedPackages(query) {
550
+ debug('search on each package', query);
551
+ const results = [];
552
+ if (typeof query === 'undefined' || typeof (query === null || query === void 0 ? void 0 : query.text) === 'undefined') {
553
+ debug('search query for cached not found');
554
+ return results;
555
+ }
556
+ _logger.logger.info({
629
557
  t: query.text,
630
558
  q: query.quality,
631
559
  p: query.popularity,
@@ -633,16 +561,16 @@ class Storage {
633
561
  s: query.size
634
562
  }, 'search by text @{t}| maintenance @{m}| quality @{q}| popularity @{p}');
635
563
  if (typeof this.localStorage.getStoragePlugin().search === 'undefined') {
636
- this.logger.info('plugin search not implemented yet');
637
- searchStream.end();
564
+ _logger.logger.info('plugin search not implemented yet');
638
565
  } else {
639
- debug('search on each package by plugin');
566
+ debug('search on each package by plugin query');
640
567
  const items = await this.localStorage.getStoragePlugin().search(query);
641
568
  try {
642
569
  for (const searchItem of items) {
643
570
  const manifest = await this.getPackageLocalMetadata(searchItem.package.name);
644
571
  if (_lodash.default.isEmpty(manifest === null || manifest === void 0 ? void 0 : manifest.versions) === false) {
645
- const searchPackage = (0, _2.mapManifestToSearchPackageBody)(manifest, searchItem);
572
+ const searchPackage = (0, _storageUtils.mapManifestToSearchPackageBody)(manifest, searchItem);
573
+ debug('search local stream found %o', searchPackage.name);
646
574
  const searchPackageItem = {
647
575
  package: searchPackage,
648
576
  score: searchItem.score,
@@ -652,19 +580,21 @@ class Storage {
652
580
  // FUTURE: find a better way to calculate the score
653
581
  searchScore: 1
654
582
  };
655
- searchStream.write(searchPackageItem);
583
+ results.push(searchPackageItem);
584
+ } else {
585
+ debug('local item without versions detected %s', searchItem.package.name);
656
586
  }
657
587
  }
658
588
  debug('search local stream end');
659
- searchStream.end();
660
589
  } catch (err) {
661
590
  this.logger.error({
662
591
  err,
663
592
  query
664
593
  }, 'error on search by plugin @{err.message}');
665
- searchStream.emit('error', err);
594
+ throw err;
666
595
  }
667
596
  }
597
+ return results;
668
598
  }
669
599
  async removePackageByRevision(pkgName, revision) {
670
600
  const storage = this.getPrivatePackageStorage(pkgName);
@@ -1314,7 +1244,7 @@ class Storage {
1314
1244
  async checkPackageRemote(name, uplinksLook) {
1315
1245
  try {
1316
1246
  // we provide a null manifest, thus the manifest returned will be the remote one
1317
- const [remoteManifest, upLinksErrors] = await this.syncUplinksMetadataNext(name, null, {
1247
+ const [remoteManifest, upLinksErrors] = await this.syncUplinksMetadata(name, null, {
1318
1248
  uplinksLook
1319
1249
  });
1320
1250
 
@@ -1419,7 +1349,7 @@ class Storage {
1419
1349
  // if we can't get the local metadata, we try to get the remote metadata
1420
1350
  // if we do to have local metadata, we try to update it with the upstream registry
1421
1351
  debug('sync uplinks for %o', name);
1422
- const [remoteManifest, upLinksErrors] = await this.syncUplinksMetadataNext(name, data, {
1352
+ const [remoteManifest, upLinksErrors] = await this.syncUplinksMetadata(name, data, {
1423
1353
  uplinksLook: options.uplinksLook,
1424
1354
  retry: options.retry,
1425
1355
  remoteAddress: options.requestOptions.remoteAddress
@@ -1465,7 +1395,7 @@ class Storage {
1465
1395
  in that case the request returns empty body and we want ask next on the list if has fresh
1466
1396
  updates.
1467
1397
  */
1468
- async syncUplinksMetadataNext(name, localManifest, options = {}) {
1398
+ async syncUplinksMetadata(name, localManifest, options = {}) {
1469
1399
  let found = localManifest !== null;
1470
1400
  let syncManifest = null;
1471
1401
  const upLinks = [];
@@ -1581,7 +1511,7 @@ class Storage {
1581
1511
  // merge time field cache and remote
1582
1512
  _cacheManifest = (0, _storageUtils.mergeUplinkTimeIntoLocalNext)(_cacheManifest, remoteManifest);
1583
1513
  // update the _uplinks field in the cache
1584
- _cacheManifest = (0, _uplinkUtil.updateVersionsHiddenUpLinkNext)(_cacheManifest, uplink);
1514
+ _cacheManifest = (0, _proxy.updateVersionsHiddenUpLinkNext)(_cacheManifest, uplink);
1585
1515
  try {
1586
1516
  // merge versions from remote into the cache
1587
1517
  _cacheManifest = (0, _storageUtils.mergeVersions)(_cacheManifest, remoteManifest);