From 1bdb49a38df01e948fa5b1bbd12ff8a723735f28 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 20 Jun 2023 10:15:26 +0800 Subject: [PATCH] fix: avoid db query on sync mode all (#527) https://github.com/cnpm/cnpmcore/pull/522/files#r1234655574 --- app/port/controller/package/DownloadPackageVersionTar.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/port/controller/package/DownloadPackageVersionTar.ts b/app/port/controller/package/DownloadPackageVersionTar.ts index c14763ca..d53812da 100644 --- a/app/port/controller/package/DownloadPackageVersionTar.ts +++ b/app/port/controller/package/DownloadPackageVersionTar.ts @@ -14,6 +14,7 @@ import { AbstractController } from '../AbstractController'; import { FULLNAME_REG_STRING, getScopeAndName } from '../../../common/PackageUtil'; import { NFSAdapter } from '../../../common/adapter/NFSAdapter'; import { PackageManagerService } from '../../../core/service/PackageManagerService'; +import { SyncMode } from '../../../common/constants'; @HTTPController() export class DownloadPackageVersionTarController extends AbstractController { @@ -28,11 +29,17 @@ export class DownloadPackageVersionTarController extends AbstractController { method: HTTPMethodEnum.GET, }) async download(@Context() ctx: EggContext, @HTTPParam() fullname: string, @HTTPParam() filenameWithVersion: string) { - // can not try nfs url first, pnpm project with lock will try to get tgz file path directly. // tgz file storeKey: `/packages/${this.fullname}/${version}/${filename}` const version = this.getAndCheckVersionFromFilename(ctx, fullname, filenameWithVersion); const storeKey = `/packages/${fullname}/${version}/${filenameWithVersion}.tgz`; const downloadUrl = await this.nfsAdapter.getDownloadUrl(storeKey); + if (this.config.cnpmcore.syncMode === SyncMode.all && downloadUrl) { + // try nfs url first, avoid db query + this.packageManagerService.plusPackageVersionCounter(fullname, version); + ctx.redirect(downloadUrl); + return; + } + // check package version in database const allowSync = this.getAllowSync(ctx); const pkg = await this.getPackageEntityByFullname(fullname, allowSync);