Skip to content

Commit

Permalink
feat: source registry manifest (#493)
Browse files Browse the repository at this point in the history
> Add the `_source_registry_manifest` in pkgFullManifest.
* 🧶 Set registryInfo during reading because of the excessive existing
data.
* ♻️ The change takes effect when redis cache expired.
----------

> 在 pkgFullManifest 中添加 _source_registry_manifest 相关字段
1. 🧶 存量数据过多,在读取时统一设置,展示当前对应 registryId
2. ♻️ 在读取 db 时生效,需要等缓存过期
  • Loading branch information
elrrrrrrr authored Jun 4, 2023
1 parent 3a423a2 commit bbec9a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
26 changes: 20 additions & 6 deletions app/core/service/PackageManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,7 @@ export class PackageManagerService extends AbstractService {

// add _registry_name field to cmd.packageJson
if (!cmd.packageJson._source_registry_name) {
let registry: Registry | null;
if (cmd.registryId) {
registry = await this.registryManagerService.findByRegistryId(cmd.registryId);
} else {
registry = await this.registryManagerService.ensureDefaultRegistry();
}
const registry = await this.getSourceRegistry(pkg);
if (registry) {
cmd.packageJson._source_registry_name = registry.name;
}
Expand Down Expand Up @@ -669,6 +664,16 @@ export class PackageManagerService extends AbstractService {
return bugVersion;
}

async getSourceRegistry(pkg: Package): Promise<Registry | null> {
let registry: Registry | null;
if (pkg.registryId) {
registry = await this.registryManagerService.findByRegistryId(pkg.registryId);
} else {
registry = await this.registryManagerService.ensureDefaultRegistry();
}
return registry;
}

private async _listPackageDistTags(pkg: Package) {
const tags = await this.packageRepository.listPackageTags(pkg.packageId);
const distTags: { [key: string]: string } = {};
Expand Down Expand Up @@ -800,6 +805,7 @@ export class PackageManagerService extends AbstractService {
let blockReason = '';
const pkg = await this.packageRepository.findPackage(scope, name);
if (!pkg) return { etag, data: null, blockReason };
const registry = await this.getSourceRegistry(pkg);

const block = await this.packageVersionBlockRepository.findPackageBlock(pkg.packageId);
if (block) {
Expand All @@ -821,6 +827,12 @@ export class PackageManagerService extends AbstractService {
if (bugVersion) {
await this.bugVersionService.fixPackageBugVersions(bugVersion, fullname, data.versions);
}
// set _source_registry_name in full manifestDist
if (!data._source_registry_name && isFullManifests) {
if (registry) {
data._source_registry_name = registry?.name;
}
}
const distBytes = Buffer.from(JSON.stringify(data));
const distIntegrity = await calculateIntegrity(distBytes);
etag = `"${distIntegrity.shasum}"`;
Expand Down Expand Up @@ -861,6 +873,7 @@ export class PackageManagerService extends AbstractService {

const distTags = await this._listPackageDistTags(pkg);
const maintainers = await this._listPackageMaintainers(pkg);
const registry = await this.getSourceRegistry(pkg);
// https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md#full-metadata-format
const data:PackageManifestType = {
_id: `${pkg.fullname}`,
Expand Down Expand Up @@ -895,6 +908,7 @@ export class PackageManagerService extends AbstractService {
// as given in package.json, for the latest version
repository: undefined,
// users: an object whose keys are the npm user names of people who have starred this package
_source_registry_name: registry?.name,
};

let latestTagVersion = '';
Expand Down
1 change: 1 addition & 0 deletions test/port/controller/package/ShowPackageController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ describe('test/port/controller/package/ShowPackageController.test.ts', () => {
.expect('content-type', 'application/json; charset=utf-8');

const data = res.body as PackageManifestType;
assert(data._source_registry_name === 'self');
assert(Object.values(data.versions).every(v => v!._source_registry_name === 'self'));
});

Expand Down

0 comments on commit bbec9a3

Please sign in to comment.