Skip to content

Commit 3aea4c8

Browse files
committed
feat: 在proxy模式下,合并私有manifest缓存,可以返回私有包
1 parent 4facf90 commit 3aea4c8

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

app/core/service/ProxyCacheService.ts

+50-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { calculateIntegrity } from '../../common/PackageUtil';
1717
import { ABBREVIATED_META_TYPE, PROXY_CACHE_DIR_NAME } from '../../common/constants';
1818
import { DIST_NAMES } from '../entity/Package';
1919
import type { AbbreviatedPackageManifestType, AbbreviatedPackageJSONType, PackageManifestType, PackageJSONType } from '../../repository/PackageRepository';
20+
import { PackageManagerService } from './PackageManagerService';
21+
import { getScopeAndName } from '../../common/PackageUtil';
2022

2123
function isoNow() {
2224
return new Date().toISOString();
@@ -50,6 +52,8 @@ export class ProxyCacheService extends AbstractService {
5052
private readonly cacheService: CacheService;
5153
@Inject()
5254
private readonly backgroundTaskHelper:BackgroundTaskHelper;
55+
@Inject()
56+
private readonly packageManagerService: PackageManagerService;
5357

5458
async getPackageVersionTarResponse(fullname: string, ctx: EggContext): Promise<HttpClientResponse> {
5559
if (this.config.cnpmcore.syncPackageBlockList.includes(fullname)) {
@@ -59,12 +63,17 @@ export class ProxyCacheService extends AbstractService {
5963
}
6064

6165
async getPackageManifest(fullname: string, fileType: DIST_NAMES.FULL_MANIFESTS| DIST_NAMES.ABBREVIATED_MANIFESTS): Promise<AbbreviatedPackageManifestType|PackageManifestType> {
62-
const cachedStoreKey = (await this.proxyCacheRepository.findProxyCache(fullname, fileType))?.filePath;
63-
if (cachedStoreKey) {
64-
const nfsBytes = await this.nfsAdapter.getBytes(cachedStoreKey);
65-
const nfsString = Buffer.from(nfsBytes!).toString();
66-
const nfsPkgManifgest = JSON.parse(nfsString);
67-
return nfsPkgManifgest;
66+
try {
67+
const cachedStoreKey = (await this.proxyCacheRepository.findProxyCache(fullname, fileType))?.filePath;
68+
if (cachedStoreKey) {
69+
const nfsBytes = await this.nfsAdapter.getBytes(cachedStoreKey);
70+
const nfsString = Buffer.from(nfsBytes!).toString();
71+
const nfsPkgManifest = JSON.parse(nfsString);
72+
return nfsPkgManifest;
73+
}
74+
} catch (e) {
75+
this.logger.error(e);
76+
this.logger.error('[ProxyCacheService.getPackageManifest:error] get cache error, ignore');
6877
}
6978

7079
const manifest = await this.getRewrittenManifest<typeof fileType>(fullname, fileType);
@@ -152,6 +161,7 @@ export class ProxyCacheService extends AbstractService {
152161
}
153162

154163
async getRewrittenManifest<T extends DIST_NAMES>(fullname:string, fileType: T, versionOrTag?:string): Promise<GetSourceManifestAndCacheReturnType<T>> {
164+
const [ scope, name ] = getScopeAndName(fullname);
155165
let responseResult;
156166
switch (fileType) {
157167
case DIST_NAMES.FULL_MANIFESTS:
@@ -171,9 +181,24 @@ export class ProxyCacheService extends AbstractService {
171181
}
172182

173183
// replace tarball url
174-
const manifest = responseResult.data;
184+
const { status, data: manifest } = responseResult;
185+
// sourceRegistry not found, check private package
186+
if (status === 404) {
187+
const { etag, data: manifest, blockReason } = fileType === DIST_NAMES.FULL_MANIFESTS ?
188+
await this.packageManagerService.listPackageFullManifests(scope, name, false) :
189+
await this.packageManagerService.listPackageAbbreviatedManifests(scope, name, false);
190+
// found in private package
191+
if (etag && !blockReason) {
192+
return manifest as any;
193+
}
194+
}
175195
const { sourceRegistry, registry } = this.config.cnpmcore;
176196
if (isPkgManifest(fileType)) {
197+
const { etag, data, blockReason } = fileType === DIST_NAMES.FULL_MANIFESTS ?
198+
await this.packageManagerService.listPackageFullManifests(scope, name, false) :
199+
await this.packageManagerService.listPackageAbbreviatedManifests(scope, name, false);
200+
const hasPrivatePackage = etag && !blockReason;
201+
177202
// pkg manifest
178203
const versionMap = manifest.versions || {};
179204
for (const key in versionMap) {
@@ -182,9 +207,26 @@ export class ProxyCacheService extends AbstractService {
182207
versionItem.dist.tarball = versionItem.dist.tarball.replace(sourceRegistry, registry);
183208
}
184209
}
210+
// private manifest
211+
if (hasPrivatePackage) {
212+
const privateVersionMap = data?.versions || {};
213+
for (const key in privateVersionMap) {
214+
if (!versionMap[key]) {
215+
versionMap[key] = privateVersionMap[key];
216+
}
217+
}
218+
if (manifest.time) {
219+
const privateTimeMap = data?.time || {};
220+
for (const key in privateTimeMap) {
221+
if (!manifest.time[key]) {
222+
manifest.time[key] = privateTimeMap[key];
223+
}
224+
}
225+
}
226+
}
185227
} else {
186228
// pkg version manifest
187-
const distItem = manifest.dist || {};
229+
const distItem = manifest?.dist || {};
188230
if (distItem.tarball) {
189231
distItem.tarball = distItem.tarball.replace(sourceRegistry, registry);
190232
}

app/port/controller/package/UpdatePackageController.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import { AbstractController } from '../AbstractController';
1717
import { FULLNAME_REG_STRING } from '../../../common/PackageUtil';
1818
import { User as UserEntity } from '../../../core/entity/User';
1919
import { PackageManagerService } from '../../../core/service/PackageManagerService';
20+
import { SyncMode } from '../../../common/constants';
21+
import { ProxyCacheRepository } from '../../../repository/ProxyCacheRepository';
22+
import { isPkgManifest, ProxyCacheService } from '../../../core/service/ProxyCacheService';
2023

2124
const MaintainerDataRule = Type.Object({
2225
maintainers: Type.Array(Type.Object({
@@ -30,7 +33,10 @@ type Maintainer = Static<typeof MaintainerDataRule>;
3033
export class UpdatePackageController extends AbstractController {
3134
@Inject()
3235
private packageManagerService: PackageManagerService;
33-
36+
@Inject()
37+
private readonly proxyCacheRepository: ProxyCacheRepository;
38+
@Inject()
39+
private readonly proxyCacheService: ProxyCacheService;
3440
// https://github.com/npm/cli/blob/latest/lib/commands/owner.js#L191
3541
@HTTPMethod({
3642
// PUT /:fullname/-rev/:rev
@@ -65,6 +71,26 @@ export class UpdatePackageController extends AbstractController {
6571
}
6672

6773
await this.packageManagerService.replacePackageMaintainersAndDist(pkg, users);
74+
// 代理模式下,更新代理缓存
75+
if (this.config.cnpmcore.syncMode === SyncMode.proxy) {
76+
const refreshList = await this.proxyCacheRepository.findProxyCaches(fullname);
77+
if (refreshList.length !== 0) {
78+
const taskList = refreshList
79+
// 仅manifests需要更新,指定版本的package.json文件发布后不会改变
80+
.filter(i => isPkgManifest(i.fileType))
81+
.map(async item => {
82+
const task = await this.proxyCacheService.createTask(
83+
`${item.fullname}/${item.fileType}`,
84+
{
85+
fullname: item.fullname,
86+
fileType: item.fileType,
87+
},
88+
);
89+
return task;
90+
});
91+
await Promise.all(taskList);
92+
}
93+
}
6894
return { ok: true };
6995
}
7096

0 commit comments

Comments
 (0)