From ca5b91c8a19e9ff44ceffeae1f223295ee84dd59 Mon Sep 17 00:00:00 2001 From: hezhengxu Date: Wed, 19 Jun 2024 23:13:54 +0800 Subject: [PATCH] refactor: rename function --- app/core/service/ProxyCacheService.ts | 8 ++++---- package.json | 1 + test/core/service/ProxyCacheService.test.ts | 22 ++++++++++----------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/core/service/ProxyCacheService.ts b/app/core/service/ProxyCacheService.ts index f12a82c1..304a1c8b 100644 --- a/app/core/service/ProxyCacheService.ts +++ b/app/core/service/ProxyCacheService.ts @@ -67,7 +67,7 @@ export class ProxyCacheService extends AbstractService { return nfsPkgManifgest; } - const manifest = await this.getSourceManifestAndCache(fullname, fileType); + const manifest = await this.rewriteManifestAndStore(fullname, fileType); this.backgroundTaskHelper.run(async () => { const cachedFiles = ProxyCache.create({ fullname, fileType }); await this.proxyCacheRepository.saveProxyCache(cachedFiles); @@ -91,7 +91,7 @@ export class ProxyCacheService extends AbstractService { const nfsString = Buffer.from(nfsBytes!).toString(); return JSON.parse(nfsString) as PackageJSONType | AbbreviatedPackageJSONType; } - const manifest = await this.getSourceManifestAndCache(fullname, fileType, versionOrTag); + const manifest = await this.rewriteManifestAndStore(fullname, fileType, versionOrTag); this.backgroundTaskHelper.run(async () => { const cachedFiles = ProxyCache.create({ fullname, fileType, version }); await this.proxyCacheRepository.saveProxyCache(cachedFiles); @@ -125,7 +125,7 @@ export class ProxyCacheService extends AbstractService { if (isPkgManifest(fileType)) { 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.getSourceManifestAndCache(fullname, fileType); + cachedManifest = await this.rewriteManifestAndStore(fullname, fileType); ProxyCache.update(cachedFiles); await this.proxyCacheRepository.saveProxyCache(cachedFiles); } else { @@ -158,7 +158,7 @@ export class ProxyCacheService extends AbstractService { await this.taskService.finishTask(task, TaskState.Success, logs.join('\n')); } - async getSourceManifestAndCache(fullname:string, fileType: T, versionOrTag?:string): Promise> { + async rewriteManifestAndStore(fullname:string, fileType: T, versionOrTag?:string): Promise> { let responseResult; switch (fileType) { case DIST_NAMES.FULL_MANIFESTS: diff --git a/package.json b/package.json index ffe7e5ab..d8c637d1 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "npm-package-arg": "^10.1.0", "oss-cnpm": "^5.0.1", "p-map": "^4.0.0", + "s3-cnpmcore": "^1.1.2", "semver": "^7.3.5", "ssri": "^8.0.1", "type-fest": "^2.5.3", diff --git a/test/core/service/ProxyCacheService.test.ts b/test/core/service/ProxyCacheService.test.ts index 43c5b5ab..f0f72ae4 100644 --- a/test/core/service/ProxyCacheService.test.ts +++ b/test/core/service/ProxyCacheService.test.ts @@ -18,8 +18,8 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { }); describe('getPackageManifest()', () => { - it('should invoke getSourceManifestAndCache first.', async () => { - mock(proxyCacheService, 'getSourceManifestAndCache', async () => { + it('should invoke rewriteManifestAndStore first.', async () => { + mock(proxyCacheService, 'rewriteManifestAndStore', async () => { return { name: 'mock info' }; }); const manifest = await proxyCacheService.getPackageManifest( @@ -31,7 +31,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, 'getSourceManifestAndCache', async () => { + mock(proxyCacheService, 'rewriteManifestAndStore', async () => { return { name: 'foo remote mock info' }; }); await proxyCacheRepository.saveProxyCache( @@ -52,8 +52,8 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { }); describe('getPackageVersionManifest()', () => { - it('should invoke getSourceManifestAndCache first.', async () => { - mock(proxyCacheService, 'getSourceManifestAndCache', async () => { + it('should invoke rewriteManifestAndStore first.', async () => { + mock(proxyCacheService, 'rewriteManifestAndStore', async () => { return { name: 'mock package version info' }; }); const manifest = await proxyCacheService.getPackageVersionManifest( @@ -66,7 +66,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, 'getSourceManifestAndCache', async () => { + mock(proxyCacheService, 'rewriteManifestAndStore', async () => { return { name: 'foo remote mock info' }; }); await proxyCacheRepository.saveProxyCache( @@ -114,7 +114,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { }); }); - describe('getSourceManifestAndCache()', () => { + describe('rewriteManifestAndStore()', () => { it('should get full package manifest', async () => { const data = await TestUtil.readJSONFile( TestUtil.getFixtures('registry.npmjs.org/foobar.json'), @@ -125,7 +125,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { data, }; }); - const manifest = await proxyCacheService.getSourceManifestAndCache( + const manifest = await proxyCacheService.rewriteManifestAndStore( 'foobar', DIST_NAMES.FULL_MANIFESTS, ); @@ -145,7 +145,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { data, }; }); - const manifest = await proxyCacheService.getSourceManifestAndCache( + const manifest = await proxyCacheService.rewriteManifestAndStore( 'foobar', DIST_NAMES.ABBREVIATED_MANIFESTS, ); @@ -165,7 +165,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { data, }; }); - const manifest = await proxyCacheService.getSourceManifestAndCache( + const manifest = await proxyCacheService.rewriteManifestAndStore( 'foobar', DIST_NAMES.MANIFEST, '1.0.0', @@ -186,7 +186,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => { data, }; }); - const manifest = await proxyCacheService.getSourceManifestAndCache( + const manifest = await proxyCacheService.rewriteManifestAndStore( 'foobar', DIST_NAMES.ABBREVIATED, '1.0.0',