diff --git a/app/common/adapter/NPMRegistry.ts b/app/common/adapter/NPMRegistry.ts index 20c7ca634..f0301c390 100644 --- a/app/common/adapter/NPMRegistry.ts +++ b/app/common/adapter/NPMRegistry.ts @@ -11,6 +11,7 @@ import { HttpClientRequestOptions, HttpClientResponse, } from 'egg'; +import { ABBREVIATED_META_TYPE } from '../constants'; type HttpMethod = HttpClientRequestOptions['method']; @@ -118,7 +119,7 @@ export class NPMRegistry { // large package: https://r.cnpmjs.org/%40procore%2Fcore-icons // https://r.cnpmjs.org/intraactive-sdk-ui 44s const authorization = this.genAuthorizationHeader(optionalConfig?.remoteAuthToken); - const accept = optionalConfig?.isAbbreviated ? 'application/vnd.npm.install-v1+json' : ''; + const accept = optionalConfig?.isAbbreviated ? ABBREVIATED_META_TYPE : ''; return await this.request('GET', url, undefined, { timeout: 120000, headers: { authorization, accept } }); } catch (err: any) { if (err.name === 'ResponseTimeoutError') throw err; diff --git a/app/common/constants.ts b/app/common/constants.ts index 1cc615966..3127a5b76 100644 --- a/app/common/constants.ts +++ b/app/common/constants.ts @@ -2,6 +2,7 @@ export const BUG_VERSIONS = 'bug-versions'; export const LATEST_TAG = 'latest'; export const GLOBAL_WORKER = 'GLOBAL_WORKER'; export const PROXY_CACHE_DIR_NAME = 'proxy-cache-packages'; +export const ABBREVIATED_META_TYPE = 'application/vnd.npm.install-v1+json'; export const NOT_IMPLEMENTED_PATH = [ '/-/npm/v1/security/audits/quick', '/-/npm/v1/security/advisories/bulk' ]; export enum SyncMode { diff --git a/app/port/controller/package/ShowPackageController.ts b/app/port/controller/package/ShowPackageController.ts index f35a0f329..fe63d186e 100644 --- a/app/port/controller/package/ShowPackageController.ts +++ b/app/port/controller/package/ShowPackageController.ts @@ -12,7 +12,7 @@ import { getScopeAndName, FULLNAME_REG_STRING } from '../../../common/PackageUti import { isSyncWorkerRequest } from '../../../common/SyncUtil'; import { PackageManagerService } from '../../../core/service/PackageManagerService'; import { CacheService } from '../../../core/service/CacheService'; -import { SyncMode } from '../../../common/constants'; +import { ABBREVIATED_META_TYPE, SyncMode } from '../../../common/constants'; import { ProxyCacheService } from '../../../core/service/ProxyCacheService'; import { calculateIntegrity } from '../../../common/PackageUtil'; import { DIST_NAMES } from '../../../core/entity/Package'; @@ -35,8 +35,7 @@ export class ShowPackageController extends AbstractController { async show(@Context() ctx: EggContext, @HTTPParam() fullname: string) { const [ scope, name ] = getScopeAndName(fullname); const isSync = isSyncWorkerRequest(ctx); - const abbreviatedMetaType = 'application/vnd.npm.install-v1+json'; - const isFullManifests = ctx.accepts([ 'json', abbreviatedMetaType ]) !== abbreviatedMetaType; + const isFullManifests = ctx.accepts([ 'json', ABBREVIATED_META_TYPE ]) !== ABBREVIATED_META_TYPE; // handle cache // fallback to db when cache error diff --git a/app/port/controller/package/ShowPackageVersionController.ts b/app/port/controller/package/ShowPackageVersionController.ts index ce20624b0..b7a2a0590 100644 --- a/app/port/controller/package/ShowPackageVersionController.ts +++ b/app/port/controller/package/ShowPackageVersionController.ts @@ -7,14 +7,13 @@ import { Context, EggContext, } from '@eggjs/tegg'; -import { NotFoundError } from 'egg-errors'; import { AbstractController } from '../AbstractController'; import { getScopeAndName, FULLNAME_REG_STRING } from '../../../common/PackageUtil'; import { isSyncWorkerRequest } from '../../../common/SyncUtil'; import { PackageManagerService } from '../../../core/service/PackageManagerService'; import { ProxyCacheService } from '../../../core/service/ProxyCacheService'; import { Spec } from '../../../port/typebox'; -import { SyncMode } from '../../../common/constants'; +import { ABBREVIATED_META_TYPE, SyncMode } from '../../../common/constants'; import { DIST_NAMES } from '../../../core/entity/Package'; @HTTPController() @@ -34,35 +33,25 @@ export class ShowPackageVersionController extends AbstractController { ctx.tValidate(Spec, `${fullname}@${versionSpec}`); const [ scope, name ] = getScopeAndName(fullname); const isSync = isSyncWorkerRequest(ctx); - const abbreviatedMetaType = 'application/vnd.npm.install-v1+json'; - const isFullManifests = ctx.accepts([ 'json', abbreviatedMetaType ]) !== abbreviatedMetaType; + const isFullManifests = ctx.accepts([ 'json', ABBREVIATED_META_TYPE ]) !== ABBREVIATED_META_TYPE; - let { blockReason, manifest, pkg } = await this.packageManagerService.showPackageVersionManifest(scope, name, versionSpec, isSync, isFullManifests); - const fileType = isFullManifests ? DIST_NAMES.MANIFEST : DIST_NAMES.ABBREVIATED; - if (!pkg) { - if (this.config.cnpmcore.syncMode === SyncMode.proxy) { - try { - manifest = await this.proxyCacheService.getPackageVersionManifest(fullname, fileType, versionSpec); - } catch (error) { - // 缓存manifest错误,创建刷新缓存任务 - await this.proxyCacheService.createTask(`${fullname}/${fileType}`, { fullname, fileType }); - throw error; - } - } else { - const allowSync = this.getAllowSync(ctx); - throw this.createPackageNotFoundErrorWithRedirect(fullname, undefined, allowSync); - } + const { blockReason, manifest, pkg } = await this.packageManagerService.showPackageVersionManifest(scope, name, versionSpec, isSync, isFullManifests); + const allowSync = this.getAllowSync(ctx); + + if (this.config.cnpmcore.syncMode === SyncMode.proxy) { + const fileType = isFullManifests ? DIST_NAMES.MANIFEST : DIST_NAMES.ABBREVIATED; + return await this.proxyCacheService.getPackageVersionManifest(fullname, fileType, versionSpec); } + if (blockReason) { this.setCDNHeaders(ctx); throw this.createPackageBlockError(blockReason, fullname, versionSpec); } + if (!pkg) { + throw this.createPackageNotFoundErrorWithRedirect(fullname, undefined, allowSync); + } if (!manifest) { - if (this.config.cnpmcore.syncMode === SyncMode.proxy) { - manifest = await this.proxyCacheService.getPackageVersionManifest(fullname, fileType, versionSpec); - } else { - throw new NotFoundError(`${fullname}@${versionSpec} not found`); - } + throw this.createPackageNotFoundErrorWithRedirect(fullname, versionSpec, allowSync); } this.setCDNHeaders(ctx); return manifest;