-
Notifications
You must be signed in to change notification settings - Fork 87
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
fix: unpkg redirect #479
Conversation
elrrrrrrr
commented
May 29, 2023
- 🧶 Change the default logic when processing main field
- 📦 Keep operational logic consistent with unpkg ref
- 🧶 修改 main 字段默认逻辑判断口径
- 📦 操作逻辑和 unpkg 保持一致 ref
Codecov Report
@@ 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
|
@@ -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)); | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码本身看起来很简单,主要实现了一个包文件版本管理的控制器类。该代码修改点只有一行,保证了不会因为 manifest 中的 main 字段为空而出现程序异常错误,并且在重定向时正确处理了 index 文件名。
然而代码中似乎存在一些数值硬编码(如 files
和 index.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({ |
There was a problem hiding this comment.
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的使用是否合理。
[skip ci] ## [3.22.3](v3.22.2...v3.22.3) (2023-05-29) ### Bug Fixes * unpkg redirect ([#479](#479)) ([c395c79](c395c79))
🎉 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: '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
啃爹。。。