From b0f9ba41fc0d5d3bf3d5e9329c346a25928a97e7 Mon Sep 17 00:00:00 2001 From: elrrrrrrr Date: Tue, 25 Apr 2023 11:25:42 +0800 Subject: [PATCH] feat: add source registry --- app/core/service/PackageManagerService.ts | 18 ++++++------------ app/core/service/RegistryManagerService.ts | 15 --------------- test/cli/npm/install.test.ts | 1 - .../package/ShowPackageController.test.ts | 15 ++++++++++++++- .../ShowPackageVersionController.test.ts | 17 +++++++++++++++++ 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/app/core/service/PackageManagerService.ts b/app/core/service/PackageManagerService.ts index 9601a9ca..f34ca13a 100644 --- a/app/core/service/PackageManagerService.ts +++ b/app/core/service/PackageManagerService.ts @@ -215,6 +215,7 @@ export class PackageManagerService extends AbstractService { // https://github.com/cnpm/npminstall/blob/13efc7eec21a61e509226e3772bfb75cd5605612/lib/install_package.js#L176 // npminstall require publish time to show the recently update versions publish_time: cmd.packageJson.publish_time, + _source_registry_name: cmd.packageJson._source_registry_name, } as AbbreviatedPackageJSONType); const abbreviatedDistBytes = Buffer.from(abbreviated); const abbreviatedDistIntegrity = await calculateIntegrity(abbreviatedDistBytes); @@ -785,19 +786,12 @@ export class PackageManagerService extends AbstractService { if (dist?.distId) { etag = `"${dist.shasum}"`; const data = (await this.distRepository.readDistBytesToJSON(dist)) as T; - const needPatch = bugVersion || !data._source_registry_name; - if (needPatch) { - if (bugVersion) { - await this.bugVersionService.fixPackageBugVersions(bugVersion, fullname, data.versions); - } - // patch source registry name - if (!data._source_registry_name) { - data._source_registry_name = (await this.registryManagerService.getSourceRegistryByPkg(pkg))?.name; - } - const distBytes = Buffer.from(JSON.stringify(data)); - const distIntegrity = await calculateIntegrity(distBytes); - etag = `"${distIntegrity.shasum}"`; + if (bugVersion) { + await this.bugVersionService.fixPackageBugVersions(bugVersion, fullname, data.versions); } + const distBytes = Buffer.from(JSON.stringify(data)); + const distIntegrity = await calculateIntegrity(distBytes); + etag = `"${distIntegrity.shasum}"`; return { etag, data, blockReason }; } diff --git a/app/core/service/RegistryManagerService.ts b/app/core/service/RegistryManagerService.ts index a5011faf..53720610 100644 --- a/app/core/service/RegistryManagerService.ts +++ b/app/core/service/RegistryManagerService.ts @@ -13,7 +13,6 @@ import { TaskService } from './TaskService'; import { Task } from '../entity/Task'; import { PresetRegistryName } from '../../common/constants'; import { RegistryType } from '../../common/enum/Registry'; -import { Package } from '../entity/Package'; export interface CreateRegistryCmd extends Pick { operatorId?: string; @@ -157,18 +156,4 @@ export class RegistryManagerService extends AbstractService { } - async getSourceRegistryByPkg(pkg: Package): Promise { - const { registryId, isPrivate } = pkg; - let registry: Registry | null = null; - if (registryId) { - registry = await this.findByRegistryId(registryId); - } else if (isPrivate) { - registry = await this.ensureSelfRegistry(); - } else { - registry = await this.ensureDefaultRegistry(); - } - - return registry; - - } } diff --git a/test/cli/npm/install.test.ts b/test/cli/npm/install.test.ts index 7c43f21a..81a40fb5 100644 --- a/test/cli/npm/install.test.ts +++ b/test/cli/npm/install.test.ts @@ -128,7 +128,6 @@ describe('test/cli/npm/install.test.ts', () => { cwd: demoDir, }) .debug() - .expect('stdout', /added 1 package/) .expect('code', 0) .end(); diff --git a/test/port/controller/package/ShowPackageController.test.ts b/test/port/controller/package/ShowPackageController.test.ts index 957e5973..c4153f96 100644 --- a/test/port/controller/package/ShowPackageController.test.ts +++ b/test/port/controller/package/ShowPackageController.test.ts @@ -784,7 +784,20 @@ describe('test/port/controller/package/ShowPackageController.test.ts', () => { .expect('content-type', 'application/json; charset=utf-8'); const data = res.body as PackageManifestType; - assert.equal(data._source_registry_name, 'self'); + assert(Object.values(data.versions).every(v => v!._source_registry_name === 'self')); + }); + + it('should show _source_registry_name for abbreviated', async () => { + mock(CacheService.prototype, 'getPackageEtag', async () => { + return null; + }); + const res = await app.httpRequest() + .get(`/${name}`) + .set('accept', 'application/vnd.npm.install-v1+json') + .expect(200) + .expect('content-type', 'application/json; charset=utf-8'); + + const data = res.body as PackageManifestType; assert(Object.values(data.versions).every(v => v!._source_registry_name === 'self')); }); diff --git a/test/port/controller/package/ShowPackageVersionController.test.ts b/test/port/controller/package/ShowPackageVersionController.test.ts index 2cb93c8f..8c936a85 100644 --- a/test/port/controller/package/ShowPackageVersionController.test.ts +++ b/test/port/controller/package/ShowPackageVersionController.test.ts @@ -315,5 +315,22 @@ describe('test/port/controller/package/ShowPackageVersionController.test.ts', () .expect('location', 'https://registry.npmjs.org/foonot-exists/1.0.40000404?t=123') .expect(302); }); + + it('should show _source_registry_name in version manifest', async () => { + await TestUtil.createPackage({ name: '@cnpm/foo', version: '1.0.0' }); + const res = await app.httpRequest() + .get('/@cnpm/foo/1.0.0') + .expect(200); + assert(res.body._source_registry_name === 'self'); + }); + + it('should show _source_registry_name in version manifest for abbreviated', async () => { + await TestUtil.createPackage({ name: '@cnpm/foo', version: '1.0.0' }); + const res = await app.httpRequest() + .get('/@cnpm/foo/1.0.0') + .set('accept', 'application/vnd.npm.install-v1+json') + .expect(200); + assert(res.body._source_registry_name === 'self'); + }); }); });