From e5128393415f6ff12d58ff0b33ec048733b520d8 Mon Sep 17 00:00:00 2001 From: Marc Bernard <59966492+mbtools@users.noreply.github.com> Date: Fri, 29 Nov 2024 12:12:15 -0500 Subject: [PATCH] update middleware, storage --- .changeset/breezy-geckos-search.md | 2 ++ packages/core/core/src/tarball-utils.ts | 11 ++++++++--- packages/core/core/test/tarball-utils.spec.ts | 7 ++++--- packages/middleware/src/middlewares/allow.ts | 5 ++--- packages/store/src/storage.ts | 7 +++---- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.changeset/breezy-geckos-search.md b/.changeset/breezy-geckos-search.md index 285a09977..9b77c61cd 100644 --- a/.changeset/breezy-geckos-search.md +++ b/.changeset/breezy-geckos-search.md @@ -2,6 +2,8 @@ '@verdaccio/tarball': patch '@verdaccio/core': patch '@verdaccio/utils': patch +'@verdaccio/middleware': patch +'@verdaccio/store': patch --- chore: move tarball utils to core diff --git a/packages/core/core/src/tarball-utils.ts b/packages/core/core/src/tarball-utils.ts index 2a6356e51..c13b1fb6e 100644 --- a/packages/core/core/src/tarball-utils.ts +++ b/packages/core/core/src/tarball-utils.ts @@ -2,11 +2,13 @@ import { URL } from 'url'; /** * Return package version from tarball name - * @param {String} name + * + * test-1.2.4.tgz -> 1.2.4 + * @param {String} fileName * @returns {String} */ -export function getVersionFromTarball(name: string): string | void { - const groups = name.match(/.+-(\d.+)\.tgz/); +export function getVersionFromTarball(fileName: string): string | void { + const groups = fileName.match(/.+-(\d+\.\d+\.\d+.+)\.tgz$/); return groups !== null ? groups[1] : undefined; } @@ -25,6 +27,9 @@ export function extractTarballFromUrl(url: string): string { /** * Build the tarball filename from paackage name and version + * + * test, 1.2.4 -> test-1.2.4.tgz + * @scope/name, 1.2.4 -> name-1.2.4.tgz * @param name package name * @param version package version * @returns tarball filename diff --git a/packages/core/core/test/tarball-utils.spec.ts b/packages/core/core/test/tarball-utils.spec.ts index 1e9d6a57b..e752a7f06 100644 --- a/packages/core/core/test/tarball-utils.spec.ts +++ b/packages/core/core/test/tarball-utils.spec.ts @@ -17,8 +17,9 @@ describe('Utilities', () => { expect(getVersionFromTarball(otherComplexName)).toEqual('3.5.0-6'); }); - test("should don'n fall at incorrect tarball name", () => { + test('should fail at incorrect tarball name', () => { expect(getVersionFromTarball('incorrectName')).toBeUndefined(); + expect(getVersionFromTarball('test-1.2.tgz')).toBeUndefined(); }); }); }); @@ -65,11 +66,11 @@ describe('extractTarballFromUrl', () => { ); }); - test('extractTarballName with no tarball should not fails', () => { + test('without tarball should not fails', () => { expect(extractTarballFromUrl('https://registry.npmjs.org/')).toBe(''); }); - test('extractTarballName fails', () => { + test('fails with incomplete URL', () => { expect(() => extractTarballFromUrl('xxxxregistry.npmjs.org/test/-/test-0.0.2.tgz')).toThrow(); }); }); diff --git a/packages/middleware/src/middlewares/allow.ts b/packages/middleware/src/middlewares/allow.ts index 94720afc9..23a4c553f 100644 --- a/packages/middleware/src/middlewares/allow.ts +++ b/packages/middleware/src/middlewares/allow.ts @@ -1,7 +1,6 @@ import buildDebug from 'debug'; -import { API_ERROR, errorUtils } from '@verdaccio/core'; -import { getVersionFromTarball } from '@verdaccio/utils'; +import { API_ERROR, errorUtils, tarballUtils } from '@verdaccio/core'; import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types'; @@ -24,7 +23,7 @@ export function allow( ? `@${req.params.scope}/${req.params.package}` : req.params.package; const packageVersion = req.params.filename - ? getVersionFromTarball(req.params.filename) + ? tarballUtils.getVersionFromTarball(req.params.filename) : req.params.version ? req.params.version : undefined; diff --git a/packages/store/src/storage.ts b/packages/store/src/storage.ts index 94aca68c5..e98614f99 100644 --- a/packages/store/src/storage.ts +++ b/packages/store/src/storage.ts @@ -19,9 +19,9 @@ import { SUPPORT_ERRORS, USERS, errorUtils, - pkgUtils, pluginUtils, searchUtils, + tarballUtils, validatioUtils, } from '@verdaccio/core'; import { asyncLoadPlugin } from '@verdaccio/loaders'; @@ -39,7 +39,6 @@ import { TarballDetails, convertDistRemoteToLocalTarballUrls, convertDistVersionToLocalTarballsUrl, - extractTarballFromUrl, getTarballDetails, } from '@verdaccio/tarball'; import { @@ -1424,7 +1423,7 @@ class Storage { // if uploaded tarball has a different shasum, it's very likely that we // have some kind of error if (validatioUtils.isObject(metadata.dist) && _.isString(metadata.dist.tarball)) { - const tarball = extractTarballFromUrl(metadata.dist.tarball); + const tarball = tarballUtils.extractTarballFromUrl(metadata.dist.tarball); if (validatioUtils.isObject(data._attachments[tarball])) { if ( _.isNil(data._attachments[tarball].shasum) === false && @@ -1979,7 +1978,7 @@ class Storage { cacheManifest.versions[versionId] = version; if (version?.dist?.tarball) { - const filename = pkgUtils.extractTarballName(version.dist.tarball); + const filename = tarballUtils.extractTarballFromUrl(version.dist.tarball); // store a fast access to the dist file by tarball name // it does NOT overwrite any existing records if (_.isNil(cacheManifest?._distfiles[filename])) {