Skip to content

Commit

Permalink
fix: legacy pkg publish (#533)
Browse files Browse the repository at this point in the history
> pkgs sync from cnpmjs.org may contain uppercase characters.
1. 🧶 Update the validation rules, allow to publish existing packages.
---------
> 部分包从 cnpmjs.org 进行同步,可能含有大写字符
1. 🧶 更新校验规则,允许存量包修改
  • Loading branch information
elrrrrrrr authored Jun 27, 2023
1 parent c0415c0 commit 20ffba8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/port/controller/package/SavePackageVersionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,18 @@ export class SavePackageVersionController extends AbstractController {
if (fullname !== pkg.name) {
throw new UnprocessableEntityError(`fullname(${fullname}) not match package.name(${pkg.name})`);
}

// Using https://github.com/npm/validate-npm-package-name to validate package name
const validateResult = validateNpmPackageName(pkg.name);
if (!validateResult.validForNewPackages) {
const errors = (validateResult.errors || validateResult.warnings).join(', ');
throw new UnprocessableEntityError(`package.name invalid, errors: ${errors}`);
// if pkg already exists, still allow to publish
const [ scope, name ] = getScopeAndName(fullname);
const pkg = await this.packageRepository.findPackage(scope, name);
console.log('pkg', pkg);
if (!pkg) {
const errors = (validateResult.errors || validateResult.warnings).join(', ');
throw new UnprocessableEntityError(`package.name invalid, errors: ${errors}`);
}
}
const versions = Object.values(pkg.versions);
if (versions.length === 0) {
Expand Down
32 changes: 32 additions & 0 deletions test/port/controller/package/SavePackageVersionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Token, TokenType } from '../../../../app/core/entity/Token';
import { Token as TokenModel } from '../../../../app/repository/model/Token';
import { User } from '../../../../app/core/entity/User';
import dayjs from 'dayjs';
import { PackageManagerService } from '../../../../app/core/service/PackageManagerService';

describe('test/port/controller/package/SavePackageVersionController.test.ts', () => {
let userRepository: UserRepository;
Expand Down Expand Up @@ -711,6 +712,37 @@ describe('test/port/controller/package/SavePackageVersionController.test.ts', ()
assert(res.body.error === '[UNPROCESSABLE_ENTITY] package.name invalid, errors: name can no longer contain more than 214 characters, name can no longer contain capital letters');
});

it('should allow to publish exists pkg', async () => {
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
const packageManagerService = await app.getEggObject(PackageManagerService);
const pkg = await TestUtil.getFullPackage({
name: '@cnpm/LegacyName',
});
const user = await userRepository.findUserByName(publisher.name);
await packageManagerService.publish({
scope: '@cnpm',
name: 'LegacyName',
version: '1.0.0',
description: '-',
packageJson: pkg,
readme: '',
dist: {
content: Buffer.from('', 'base64'),
},
tag: 'latest',
isPrivate: true,
}, user!);


const res = await app.httpRequest()
.put(`/${pkg.name}`)
.set('authorization', publisher.authorization)
.set('user-agent', publisher.ua)
.send(pkg);
assert(res.status === 201);

});

it('should 422 when attachment data format invalid', async () => {
let pkg = await TestUtil.getFullPackage({
attachment: {
Expand Down

0 comments on commit 20ffba8

Please sign in to comment.