Skip to content

Commit

Permalink
refactor: rename function
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jul 27, 2024
1 parent f2c84ed commit 732be72
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
16 changes: 11 additions & 5 deletions app/core/service/ProxyCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export class ProxyCacheService extends AbstractService {
return nfsPkgManifgest;
}

const manifest = await this.rewriteManifestAndStore<typeof fileType>(fullname, fileType);
const manifest = await this.getRewrittenManifest<typeof fileType>(fullname, fileType);
this.backgroundTaskHelper.run(async () => {
await this.storeRewrittenManifest(manifest, fullname, fileType);
const cachedFiles = ProxyCache.create({ fullname, fileType });
await this.proxyCacheRepository.saveProxyCache(cachedFiles);
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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<typeof fileType>(fullname, fileType);
cachedManifest = await this.getRewrittenManifest<typeof fileType>(fullname, fileType);
await this.storeRewrittenManifest(cachedManifest, fullname, fileType);
ProxyCache.update(cachedFiles);
await this.proxyCacheRepository.saveProxyCache(cachedFiles);
} catch (error) {
Expand All @@ -148,7 +151,7 @@ export class ProxyCacheService extends AbstractService {
await this.taskService.finishTask(task, TaskState.Success, logs.join('\n'));
}

async rewriteManifestAndStore<T extends DIST_NAMES>(fullname:string, fileType: T, versionOrTag?:string): Promise<GetSourceManifestAndCacheReturnType<T>> {
async getRewrittenManifest<T extends DIST_NAMES>(fullname:string, fileType: T, versionOrTag?:string): Promise<GetSourceManifestAndCacheReturnType<T>> {
let responseResult;
switch (fileType) {
case DIST_NAMES.FULL_MANIFESTS:
Expand Down Expand Up @@ -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}`;
Expand All @@ -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<EggContext>, options?: HttpClientRequestOptions): Promise<HttpClientResponse> {
Expand Down
22 changes: 11 additions & 11 deletions test/core/service/ProxyCacheService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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'),
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 732be72

Please sign in to comment.