diff --git a/app/core/service/ProxyCacheService.ts b/app/core/service/ProxyCacheService.ts index f77fb4da..46c2e511 100644 --- a/app/core/service/ProxyCacheService.ts +++ b/app/core/service/ProxyCacheService.ts @@ -67,8 +67,9 @@ export class ProxyCacheService extends AbstractService { return nfsPkgManifgest; } - const manifest = await this.rewriteManifestAndStore(fullname, fileType); + const manifest = await this.getRewrittenManifest(fullname, fileType); this.backgroundTaskHelper.run(async () => { + await this.storeRewrittenManifest(manifest, fullname, fileType); const cachedFiles = ProxyCache.create({ fullname, fileType }); await this.proxyCacheRepository.saveProxyCache(cachedFiles); }); @@ -91,8 +92,9 @@ export class ProxyCacheService extends AbstractService { const nfsString = Buffer.from(nfsBytes!).toString(); return JSON.parse(nfsString) as PackageJSONType | AbbreviatedPackageJSONType; } - const manifest = await this.rewriteManifestAndStore(fullname, fileType, versionOrTag); + const manifest = await this.getRewrittenManifest(fullname, fileType, versionOrTag); this.backgroundTaskHelper.run(async () => { + await this.storeRewrittenManifest(manifest, fullname, fileType); const cachedFiles = ProxyCache.create({ fullname, fileType, version }); await this.proxyCacheRepository.saveProxyCache(cachedFiles); }); @@ -124,7 +126,8 @@ export class ProxyCacheService extends AbstractService { try { const cachedFiles = await this.proxyCacheRepository.findProxyCache(fullname, fileType); if (!cachedFiles) throw new Error('task params error, can not found record in repo.'); - cachedManifest = await this.rewriteManifestAndStore(fullname, fileType); + cachedManifest = await this.getRewrittenManifest(fullname, fileType); + await this.storeRewrittenManifest(cachedManifest, fullname, fileType); ProxyCache.update(cachedFiles); await this.proxyCacheRepository.saveProxyCache(cachedFiles); } catch (error) { @@ -148,7 +151,7 @@ export class ProxyCacheService extends AbstractService { await this.taskService.finishTask(task, TaskState.Success, logs.join('\n')); } - async rewriteManifestAndStore(fullname:string, fileType: T, versionOrTag?:string): Promise> { + async getRewrittenManifest(fullname:string, fileType: T, versionOrTag?:string): Promise> { let responseResult; switch (fileType) { case DIST_NAMES.FULL_MANIFESTS: @@ -186,6 +189,10 @@ export class ProxyCacheService extends AbstractService { distItem.tarball = distItem.tarball.replace(sourceRegistry, registry); } } + return manifest; + } + + private async storeRewrittenManifest(manifest, fullname: string, fileType: DIST_NAMES) { let storeKey: string; if (isPkgManifest(fileType)) { storeKey = `/${PROXY_CACHE_DIR_NAME}/${fullname}/${fileType}`; @@ -195,7 +202,6 @@ export class ProxyCacheService extends AbstractService { } const nfsBytes = Buffer.from(JSON.stringify(manifest)); await this.nfsAdapter.uploadBytes(storeKey, nfsBytes); - return manifest; } private async getProxyResponse(ctx: Partial, options?: HttpClientRequestOptions): Promise { diff --git a/test/core/service/ProxyCacheService.test.ts b/test/core/service/ProxyCacheService.test.ts index f5440b9b..721cd8d8 100644 --- a/test/core/service/ProxyCacheService.test.ts +++ b/test/core/service/ProxyCacheService.test.ts @@ -30,8 +30,8 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { }); describe('getPackageManifest()', () => { - it('should invoke rewriteManifestAndStore first.', async () => { - mock(proxyCacheService, 'rewriteManifestAndStore', async () => { + it('should invoke getRewrittenManifest first.', async () => { + mock(proxyCacheService, 'getRewrittenManifest', async () => { return { name: 'mock info' }; }); const manifest = await proxyCacheService.getPackageManifest( @@ -43,7 +43,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { it('should read data from nfs when cached.', async () => { const nfsAdapter = await app.getEggObject(NFSAdapter); - mock(proxyCacheService, 'rewriteManifestAndStore', async () => { + mock(proxyCacheService, 'getRewrittenManifest', async () => { return { name: 'foo remote mock info' }; }); await proxyCacheRepository.saveProxyCache( @@ -64,8 +64,8 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { }); describe('getPackageVersionManifest()', () => { - it('should invoke rewriteManifestAndStore first.', async () => { - mock(proxyCacheService, 'rewriteManifestAndStore', async () => { + it('should invoke getRewrittenManifest first.', async () => { + mock(proxyCacheService, 'getRewrittenManifest', async () => { return { name: 'mock package version info' }; }); const manifest = await proxyCacheService.getPackageVersionManifest( @@ -78,7 +78,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { it('should read data from nfs when cached.', async () => { const nfsAdapter = await app.getEggObject(NFSAdapter); - mock(proxyCacheService, 'rewriteManifestAndStore', async () => { + mock(proxyCacheService, 'getRewrittenManifest', async () => { return { name: 'foo remote mock info' }; }); await proxyCacheRepository.saveProxyCache( @@ -127,7 +127,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { }); }); - describe('rewriteManifestAndStore()', () => { + describe('getRewrittenManifest()', () => { it('should get full package manifest', async () => { const data = await TestUtil.readJSONFile( TestUtil.getFixtures('registry.npmjs.org/foobar.json'), @@ -138,7 +138,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { data, }; }); - const manifest = await proxyCacheService.rewriteManifestAndStore( + const manifest = await proxyCacheService.getRewrittenManifest( 'foobar', DIST_NAMES.FULL_MANIFESTS, ); @@ -158,7 +158,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { data, }; }); - const manifest = await proxyCacheService.rewriteManifestAndStore( + const manifest = await proxyCacheService.getRewrittenManifest( 'foobar', DIST_NAMES.ABBREVIATED_MANIFESTS, ); @@ -178,7 +178,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { data, }; }); - const manifest = await proxyCacheService.rewriteManifestAndStore( + const manifest = await proxyCacheService.getRewrittenManifest( 'foobar', DIST_NAMES.MANIFEST, '1.0.0', @@ -203,7 +203,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { }; }, ); - const manifest = await proxyCacheService.rewriteManifestAndStore( + const manifest = await proxyCacheService.getRewrittenManifest( 'foobar', DIST_NAMES.ABBREVIATED, '1.0.0',