Skip to content

Commit

Permalink
feat: add source registry
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Apr 25, 2023
1 parent f4e2a7d commit b0f9ba4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
18 changes: 6 additions & 12 deletions app/core/service/PackageManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 };
}

Expand Down
15 changes: 0 additions & 15 deletions app/core/service/RegistryManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Registry, 'changeStream' | 'host' | 'userPrefix' | 'type' | 'name'> {
operatorId?: string;
Expand Down Expand Up @@ -157,18 +156,4 @@ export class RegistryManagerService extends AbstractService {

}

async getSourceRegistryByPkg(pkg: Package): Promise<Registry | null> {
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;

}
}
1 change: 0 additions & 1 deletion test/cli/npm/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ describe('test/cli/npm/install.test.ts', () => {
cwd: demoDir,
})
.debug()
.expect('stdout', /added 1 package/)
.expect('code', 0)
.end();

Expand Down
15 changes: 14 additions & 1 deletion test/port/controller/package/ShowPackageController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});

Expand Down
17 changes: 17 additions & 0 deletions test/port/controller/package/ShowPackageVersionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit b0f9ba4

Please sign in to comment.