Skip to content

Commit

Permalink
feat: show all mismatch keys
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jul 9, 2023
1 parent 0728b94 commit f56ec9d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/common/PackageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export async function extractPackageJSON(tarballBytes: Buffer): Promise<PackageJ
.pipe(tar.t({
filter: name => name === 'package/package.json',
onentry: async entry => {
let chunks: Buffer[] = [];
for await (let chunk of entry) {
const chunks: Buffer[] = [];
for await (const chunk of entry) {
chunks.push(chunk);
}
try {
Expand Down
6 changes: 3 additions & 3 deletions app/port/controller/package/SavePackageVersionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ export class SavePackageVersionController extends AbstractController {
if (this.config.cnpmcore.strictValidateTarballPkg) {
const tarballPkg = await extractPackageJSON(tarballBytes);
const versionManifest = pkg.versions[tarballPkg.version];
const diffKey = STRICT_CHECK_TARBALL_FIELDS.find(key => {
const diffKeys = STRICT_CHECK_TARBALL_FIELDS.filter(key => {
return !isEqual(tarballPkg[key], versionManifest[key]);
});
if (diffKey) {
throw new UnprocessableEntityError(`${diffKey} mismatch between tarball and manifest`);
if (diffKeys.length > 0) {
throw new UnprocessableEntityError(`${diffKeys} mismatch between tarball and manifest`);
}
}

Expand Down
22 changes: 20 additions & 2 deletions test/port/controller/package/SavePackageVersionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,27 @@ describe('test/port/controller/package/SavePackageVersionController.test.ts', ()
assert.equal(pkgEntity.registryId, selfRegistry.registryId);
});
it('should verify tgz and manifest', async () => {
const { pkg, user } = await TestUtil.createPackage({ name: '@cnpm/banana', version: '1.0.0' });
const pkg2 = await TestUtil.getFullPackage({ name: pkg.name, version: '0.0.1' });

pkg2.versions['0.0.1'].name = '@cnpm/orange';

mock(app.config.cnpmcore, 'strictValidateTarballPkg', true);
const res = await app.httpRequest()
.put(`/${pkg2.name}`)
.set('authorization', user.authorization)
.set('user-agent', user.ua)
.send(pkg2)
.expect(422);

assert.equal(res.body.error, '[UNPROCESSABLE_ENTITY] name mismatch between tarball and manifest');
});
it('should verify tgz and manifest with multiple fields', async () => {
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
const { pkg, user } = await TestUtil.createPackage({ name: 'non_scope_pkg', version: '1.0.0' });
const pkg2 = await TestUtil.getFullPackage({ name: pkg.name, version: '2.0.0' });
const pkg2 = await TestUtil.getFullPackage({ name: pkg.name, version: '0.0.1' });

pkg2.versions['0.0.1'].dependencies = { lodash: 'latest' };

mock(app.config.cnpmcore, 'strictValidateTarballPkg', true);
const res = await app.httpRequest()
Expand All @@ -99,7 +117,7 @@ describe('test/port/controller/package/SavePackageVersionController.test.ts', ()
.send(pkg2)
.expect(422);

assert.equal(res.body.error, '[UNPROCESSABLE_ENTITY] name mismatch between tarball and manifest');
assert.equal(res.body.error, '[UNPROCESSABLE_ENTITY] name,dependencies mismatch between tarball and manifest');
});

it('should add new version success on scoped package', async () => {
Expand Down

0 comments on commit f56ec9d

Please sign in to comment.