Skip to content

Commit

Permalink
refactor: package version controller in proxy mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jun 19, 2024
1 parent 9c9a7cc commit 948dad5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
3 changes: 2 additions & 1 deletion app/common/adapter/NPMRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
HttpClientRequestOptions,
HttpClientResponse,
} from 'egg';
import { ABBREVIATED_META_TYPE } from '../constants';

type HttpMethod = HttpClientRequestOptions['method'];

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions app/port/controller/package/ShowPackageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down
37 changes: 13 additions & 24 deletions app/port/controller/package/ShowPackageVersionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down

0 comments on commit 948dad5

Please sign in to comment.