Skip to content

Commit

Permalink
feat: long description (#349)
Browse files Browse the repository at this point in the history
目前 db 限制 pkg.description 长度为 10k,cnpmjs.org 为 longtext,可能导致不兼容。

* 长 description 场景,截断字符保存,防止创建 pkg 失败
  • Loading branch information
elrrrrrrr authored Nov 4, 2022
1 parent 3e7a434 commit 43d77ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/core/service/PackageManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface PublishPackageCmd {

const TOTAL = '@@TOTAL@@';
const SCOPE_TOTAL_PREFIX = '@@SCOPE@@:';
const DESCRIPTION_LIMIT = 1024 * 10;

@ContextProto({
accessLevel: AccessLevel.PUBLIC,
Expand Down Expand Up @@ -109,6 +110,11 @@ export class PackageManagerService extends AbstractService {
pkg.registryId = cmd.registryId;
}
}

// 防止 description 长度超过 db 限制
if (pkg.description?.length > DESCRIPTION_LIMIT) {
pkg.description = pkg.description.substring(0, DESCRIPTION_LIMIT);
}
await this.packageRepository.savePackage(pkg);
// create maintainer
await this.packageRepository.savePackageMaintainer(pkg.packageId, publisher.userId);
Expand Down
22 changes: 22 additions & 0 deletions test/core/service/PackageManagerService/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ describe('test/core/service/PackageManagerService/publish.test.ts', () => {
app.expectLog(/\[\d+\.\d+\] \[NFSAdapter:uploadBytes|T\]/);
});

it('should work slice long description', async () => {
app.mockLog();
const { packageId } = await packageManagerService.publish({
dist: {
content: Buffer.alloc(0),
},
tag: '',
scope: '',
name: 'foo',
description: '~'.repeat(1100 * 100),
packageJson: {},
readme: '',
version: '1.0.0',
isPrivate: true,
}, publisher);
const pkgVersion = await packageRepository.findPackageVersion(packageId, '1.0.0');
assert(pkgVersion);
assert.equal(pkgVersion.version, '1.0.0');
const pkg = await packageRepository.findPackage('', 'foo');
assert(pkg?.description === '~'.repeat(1024 * 10));
});

it('should work with dist.localFile', async () => {
const { packageId } = await packageManagerService.publish({
dist: {
Expand Down

0 comments on commit 43d77ee

Please sign in to comment.