Skip to content

Commit

Permalink
fix: create sync task by 'GET /:fullname/-/:filenameWithVersion.tgz' (#…
Browse files Browse the repository at this point in the history
…526)

used by pnpm project with lock

closes #525
  • Loading branch information
hezhengxu2018 committed Jun 21, 2023
1 parent 110fdae commit 5ceaa6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
12 changes: 6 additions & 6 deletions app/port/controller/AbstractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,25 @@ export abstract class AbstractController extends MiddlewareController {
return new UnavailableForLegalReasonsError(`${message}, reason: ${reason}`);
}

protected async getPackageEntityByFullname(fullname: string): Promise<PackageEntity> {
protected async getPackageEntityByFullname(fullname: string, allowSync?: boolean): Promise<PackageEntity> {
const [ scope, name ] = getScopeAndName(fullname);
return await this.getPackageEntity(scope, name);
return await this.getPackageEntity(scope, name, allowSync);
}

// try to get package entity, throw NotFoundError when package not exists
protected async getPackageEntity(scope: string, name: string): Promise<PackageEntity> {
protected async getPackageEntity(scope: string, name: string, allowSync?:boolean): Promise<PackageEntity> {
const packageEntity = await this.packageRepository.findPackage(scope, name);
if (!packageEntity) {
const fullname = getFullname(scope, name);
throw this.createPackageNotFoundErrorWithRedirect(fullname);
throw this.createPackageNotFoundErrorWithRedirect(fullname, undefined, allowSync);
}
return packageEntity;
}

protected async getPackageVersionEntity(pkg: PackageEntity, version: string): Promise<PackageVersionEntity> {
protected async getPackageVersionEntity(pkg: PackageEntity, version: string, allowSync?: boolean): Promise<PackageVersionEntity> {
const packageVersion = await this.packageRepository.findPackageVersion(pkg.packageId, version);
if (!packageVersion) {
throw this.createPackageNotFoundErrorWithRedirect(pkg.fullname, version);
throw this.createPackageNotFoundErrorWithRedirect(pkg.fullname, version, allowSync);
}
return packageVersion;
}
Expand Down
5 changes: 3 additions & 2 deletions app/port/controller/package/DownloadPackageVersionTar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ export class DownloadPackageVersionTarController extends AbstractController {
}

// check package version in database
const pkg = await this.getPackageEntityByFullname(fullname);
const packageVersion = await this.getPackageVersionEntity(pkg, version);
const allowSync = this.getAllowSync(ctx);
const pkg = await this.getPackageEntityByFullname(fullname, allowSync);
const packageVersion = await this.getPackageVersionEntity(pkg, version, allowSync);

// read by nfs url
if (downloadUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,31 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test.
error: '[NOT_FOUND] @cnpm/[email protected] not found',
});
});

it('should not create sync task when package version tgz not exists and syncNotFound=false', async () => {
mock(app.config.cnpmcore, 'syncMode', 'exist');
mock(app.config.cnpmcore, 'syncNotFound', false);
mock(app.config.cnpmcore, 'redirectNotFound', false);
const res = await app.httpRequest()
.get('/lodash/-/lodash-1.404.404.tgz')
.set('user-agent', publisher.ua + ' node/16.0.0')
.set('Accept', 'application/vnd.npm.install-v1+json');
assert(res.status === 404);
app.notExpectLog('[middleware:ErrorHandler][syncPackage] create sync package');
});

it('should create sync task when package version tgz not exists and syncNotFound=true', async () => {
mock(app.config.cnpmcore, 'syncMode', 'exist');
mock(app.config.cnpmcore, 'syncNotFound', true);
mock(app.config.cnpmcore, 'redirectNotFound', false);
const res = await app.httpRequest()
.get('/lodash/-/lodash-1.404.404.tgz')
.set('user-agent', publisher.ua + ' node/16.0.0')
.set('Accept', 'application/vnd.npm.install-v1+json');
assert(res.status === 404);
app.expectLog('[middleware:ErrorHandler][syncPackage] create sync package');
});

});

describe('[GET /:fullname/download/:fullname-:version.tgz] deprecatedDownload()', () => {
Expand Down

0 comments on commit 5ceaa6b

Please sign in to comment.