Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unpkg redirect #479

Merged
merged 1 commit into from
May 29, 2023
Merged

fix: unpkg redirect #479

merged 1 commit into from
May 29, 2023

Conversation

elrrrrrrr
Copy link
Member

@types packages generally don't configure the main field, will cause infinite loops exp

  1. 🧶 Change the default logic when processing main field
  2. 📦 Keep operational logic consistent with unpkg ref

@types 包一般没有配置 main 字段,会导致无限循环 exp

  1. 🧶 修改 main 字段默认逻辑判断口径
  2. 📦 操作逻辑和 unpkg 保持一致 ref

@elrrrrrrr elrrrrrrr added the bug Something isn't working label May 29, 2023
@elrrrrrrr elrrrrrrr requested a review from fengmk2 May 29, 2023 09:01
@codecov
Copy link

codecov bot commented May 29, 2023

Codecov Report

Merging #479 (2f6b8a3) into master (cc01398) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #479   +/-   ##
=======================================
  Coverage   97.39%   97.39%           
=======================================
  Files         168      168           
  Lines       15683    15684    +1     
  Branches     2012     2013    +1     
=======================================
+ Hits        15274    15275    +1     
  Misses        409      409           
Impacted Files Coverage Δ
...pp/port/controller/PackageVersionFileController.ts 99.03% <100.00%> (+<0.01%) ⬆️

@@ -109,7 +109,8 @@ export class PackageVersionFileController extends AbstractController {
}
const { manifest } = await this.packageManagerService.showPackageVersionManifest(scope, name, versionOrTag);
// GET /foo/1.0.0/files => /foo/1.0.0/files/{main}
const indexFile = manifest?.main ?? 'index.js';
// ignore empty entry exp: @types/node@20.2.5/
const indexFile = manifest?.main || 'index.js';
ctx.redirect(join(ctx.path, indexFile));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码本身看起来很简单,主要实现了一个包文件版本管理的控制器类。该代码修改点只有一行,保证了不会因为 manifest 中的 main 字段为空而出现程序异常错误,并且在重定向时正确处理了 index 文件名。

然而代码中似乎存在一些数值硬编码(如 filesindex.js),这可能会增加代码维护困难度。建议可以将这些常量定义为属性或者常量变量。另外,由于代码短小精悍并没有完整的语境和上下文,所以评估函数运行效率、接口安全性等方面还需要进一步了解。

.expect('content-type', 'application/json; charset=utf-8');
assert.equal(res.body.error, '[NOT_FOUND] File foo@1.0.0/index.js not found');
});

it('should list one package version files', async () => {
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
const pkg = await TestUtil.getFullPackage({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码补丁中所做的改动主要是增加了一个单元测试用例。

该用例的功能为创建一个具有空main字段和自定义描述(通过getFullPackage()函数生成)的npm包,并对其进行PUT请求。测试接着发起了一次GET请求,期望该请求能正确返回404错误和一个JSON响应,表明该文件未被找到。

在代码实现方面,建议对代码缺少的类型注解进行补充,并检查mock的使用是否合理。

@elrrrrrrr elrrrrrrr requested a review from gemwuu May 29, 2023 09:51
@gemwuu gemwuu merged commit c395c79 into master May 29, 2023
@gemwuu gemwuu deleted the types-files-redirect branch May 29, 2023 10:02
fengmk2 pushed a commit that referenced this pull request May 29, 2023
[skip ci]

## [3.22.3](v3.22.2...v3.22.3) (2023-05-29)

### Bug Fixes

* unpkg redirect ([#479](#479)) ([c395c79](c395c79))
@github-actions
Copy link

🎉 This PR is included in version 3.22.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

name: 'foo',
version: '1.0.0',
versionObject: {
main: '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

啃爹。。。

@fengmk2 fengmk2 mentioned this pull request May 31, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants