From 74815362b095d58bcfff0cc725ffd59f39d3729d Mon Sep 17 00:00:00 2001 From: hezhengxu Date: Fri, 23 Jun 2023 23:26:20 +0800 Subject: [PATCH] feat: create task when proxy. --- .../package/DownloadPackageVersionTar.ts | 38 ++++++++++++++++++- .../package/ShowPackageController.ts | 20 ++++++++-- .../package/ShowPackageVersionController.ts | 20 ++++++++-- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/app/port/controller/package/DownloadPackageVersionTar.ts b/app/port/controller/package/DownloadPackageVersionTar.ts index d53812da..75df3c4e 100644 --- a/app/port/controller/package/DownloadPackageVersionTar.ts +++ b/app/port/controller/package/DownloadPackageVersionTar.ts @@ -14,6 +14,8 @@ 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 { ProxyModeService } from '../../../core/service/ProxyModeService'; +import { PackageSyncerService } from '../../../core/service/PackageSyncerService'; import { SyncMode } from '../../../common/constants'; @HTTPController() @@ -21,6 +23,10 @@ export class DownloadPackageVersionTarController extends AbstractController { @Inject() private packageManagerService: PackageManagerService; @Inject() + private proxyModeService: ProxyModeService; + @Inject() + private packageSyncerService: PackageSyncerService; + @Inject() private nfsAdapter: NFSAdapter; @HTTPMethod({ @@ -42,8 +48,21 @@ export class DownloadPackageVersionTarController extends AbstractController { // check package version in database const allowSync = this.getAllowSync(ctx); - const pkg = await this.getPackageEntityByFullname(fullname, allowSync); - const packageVersion = await this.getPackageVersionEntity(pkg, version, allowSync); + let pkg; + let packageVersion; + try { + pkg = await this.getPackageEntityByFullname(fullname, allowSync); + packageVersion = await this.getPackageVersionEntity(pkg, version, allowSync); + } catch (error) { + if (this.config.cnpmcore.syncMode === SyncMode.proxy) { + // proxy mode package version not found. + const tgzBuffer = await this.#getTgzBuffer(ctx, fullname, version); + this.packageManagerService.plusPackageVersionCounter(fullname, version); + ctx.attachment(`${filenameWithVersion}.tgz`); + return tgzBuffer; + } + throw error; + } // read by nfs url if (downloadUrl) { @@ -72,10 +91,25 @@ export class DownloadPackageVersionTarController extends AbstractController { path: `/:fullname(${FULLNAME_REG_STRING})/download/:fullnameWithVersion+.tgz`, method: HTTPMethodEnum.GET, }) + async deprecatedDownload(@Context() ctx: EggContext, @HTTPParam() fullname: string, @HTTPParam() fullnameWithVersion: string) { // /@emotion/utils/download/@emotion/utils-0.11.3.tgz // => /@emotion/utils/-/utils-0.11.3.tgz const filenameWithVersion = getScopeAndName(fullnameWithVersion)[1]; return await this.download(ctx, fullname, filenameWithVersion); } + + async #getTgzBuffer(ctx: EggContext, fullname: string, version: string) { + const { tgzBuffer } = await this.proxyModeService.getPackageVersionTarAndTempFilePath(fullname, ctx.url); + const task = await this.packageSyncerService.createTask(fullname, { + authorIp: ctx.ip, + authorId: `pid_${process.pid}`, + tips: `Sync specific version in proxy mode cause by "${ctx.href}"`, + skipDependencies: true, + specificVersions: [ version ], + }); + ctx.logger.info('[DownloadPackageVersionTarController.createSyncTask:success] taskId: %s, fullname: %s', + task.taskId, fullname); + return tgzBuffer; + } } diff --git a/app/port/controller/package/ShowPackageController.ts b/app/port/controller/package/ShowPackageController.ts index 60fda994..46267c8a 100644 --- a/app/port/controller/package/ShowPackageController.ts +++ b/app/port/controller/package/ShowPackageController.ts @@ -12,6 +12,8 @@ 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 { ProxyModeService } from '../../../core/service/ProxyModeService'; @HTTPController() export class ShowPackageController extends AbstractController { @@ -19,6 +21,8 @@ export class ShowPackageController extends AbstractController { private packageManagerService: PackageManagerService; @Inject() private cacheService: CacheService; + @Inject() + private proxyModeService: ProxyModeService; @HTTPMethod({ // GET /:fullname @@ -64,10 +68,20 @@ export class ShowPackageController extends AbstractController { // handle cache miss let result: { etag: string; data: any, blockReason: string }; - if (isFullManifests) { - result = await this.packageManagerService.listPackageFullManifests(scope, name, isSync); + if (this.config.cnpmcore.syncMode === SyncMode.proxy) { + // proxy mode + if (isFullManifests) { + result = await this.proxyModeService.getPackageFullManifests(fullname); + } else { + result = await this.proxyModeService.getPackageAbbreviatedManifests(fullname); + } } else { - result = await this.packageManagerService.listPackageAbbreviatedManifests(scope, name, isSync); + // sync mode + if (isFullManifests) { + result = await this.packageManagerService.listPackageFullManifests(scope, name, isSync); + } else { + result = await this.packageManagerService.listPackageAbbreviatedManifests(scope, name, isSync); + } } const { etag, data, blockReason } = result; // 404, no data diff --git a/app/port/controller/package/ShowPackageVersionController.ts b/app/port/controller/package/ShowPackageVersionController.ts index b80b3102..06f13b65 100644 --- a/app/port/controller/package/ShowPackageVersionController.ts +++ b/app/port/controller/package/ShowPackageVersionController.ts @@ -12,12 +12,16 @@ import { AbstractController } from '../AbstractController'; import { getScopeAndName, FULLNAME_REG_STRING } from '../../../common/PackageUtil'; import { isSyncWorkerRequest } from '../../../common/SyncUtil'; import { PackageManagerService } from '../../../core/service/PackageManagerService'; +import { ProxyModeService } from '../../../core/service/ProxyModeService'; import { Spec } from '../../../port/typebox'; +import { SyncMode } from '../../../common/constants'; @HTTPController() export class ShowPackageVersionController extends AbstractController { @Inject() private packageManagerService: PackageManagerService; + @Inject() + private proxyModeService: ProxyModeService; @HTTPMethod({ // GET /:fullname/:versionSpec @@ -32,17 +36,25 @@ export class ShowPackageVersionController extends AbstractController { const abbreviatedMetaType = 'application/vnd.npm.install-v1+json'; const isFullManifests = ctx.accepts([ 'json', abbreviatedMetaType ]) !== abbreviatedMetaType; - const { blockReason, manifest, pkg } = await this.packageManagerService.showPackageVersionManifest(scope, name, versionSpec, isSync, isFullManifests); + let { blockReason, manifest, pkg } = await this.packageManagerService.showPackageVersionManifest(scope, name, versionSpec, isSync, isFullManifests); if (!pkg) { - const allowSync = this.getAllowSync(ctx); - throw this.createPackageNotFoundErrorWithRedirect(fullname, undefined, allowSync); + if (this.config.cnpmcore.syncMode === SyncMode.proxy) { + manifest = await this.proxyModeService.getPackageVersionOrTagManifest(fullname, versionSpec); + } else { + const allowSync = this.getAllowSync(ctx); + throw this.createPackageNotFoundErrorWithRedirect(fullname, undefined, allowSync); + } } if (blockReason) { this.setCDNHeaders(ctx); throw this.createPackageBlockError(blockReason, fullname, versionSpec); } if (!manifest) { - throw new NotFoundError(`${fullname}@${versionSpec} not found`); + if (this.config.cnpmcore.syncMode === SyncMode.proxy) { + manifest = await this.proxyModeService.getPackageVersionOrTagManifest(fullname, versionSpec); + } else { + throw new NotFoundError(`${fullname}@${versionSpec} not found`); + } } this.setCDNHeaders(ctx); return manifest;