Skip to content

Commit

Permalink
feat: create task when proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jun 23, 2023
1 parent 9a71d3d commit d501dda
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
38 changes: 36 additions & 2 deletions app/port/controller/package/DownloadPackageVersionTar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ 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()
export class DownloadPackageVersionTarController extends AbstractController {
@Inject()
private packageManagerService: PackageManagerService;
@Inject()
private proxyModeService: ProxyModeService;
@Inject()
private packageSyncerService: PackageSyncerService;
@Inject()
private nfsAdapter: NFSAdapter;

@HTTPMethod({
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}
20 changes: 17 additions & 3 deletions app/port/controller/package/ShowPackageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ 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 {
@Inject()
private packageManagerService: PackageManagerService;
@Inject()
private cacheService: CacheService;
@Inject()
private proxyModeService: ProxyModeService;

@HTTPMethod({
// GET /:fullname
Expand Down Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions app/port/controller/package/ShowPackageVersionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit d501dda

Please sign in to comment.