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

feat: proxy模式增强 #724

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fangzhengjin
Copy link

@fangzhengjin fangzhengjin commented Nov 1, 2024

  1. 刷新代理缓存时,如果source仓库找不到(404)时,尝试在private仓库中查找,以private仓库查询结果返回
  2. 刷新代理缓存时,如果在同时在source与private中找到,则将private结果中的versions与time数组合并至source查询的结果中(只合并source中不存在的key)
  3. 当在private中推送私有包时,立即从private库中刷新该包的缓存信息,可即时被查询或下载,无需等待凌晨3点刷新缓存

Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

Warning

Rate limit exceeded

@fangzhengjin has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 0 minutes and 4 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 760c862 and 3aea4c8.

Walkthrough

The ProxyCacheService class in app/core/service/ProxyCacheService.ts has been updated to enhance its functionality and error handling. A new dependency, PackageManagerService, has been introduced to assist with managing package manifests, particularly when handling 404 responses from upstream requests. The getRewrittenManifest method has been enhanced to retrieve manifests from private sources if necessary, while error handling in the getPackageManifest method has been strengthened to ensure resilience during cache retrieval. Additionally, the UpdatePackageController class has been modified to incorporate proxy cache updates.

Changes

File Path Change Summary
app/core/service/ProxyCacheService.ts - Injected PackageManagerService into ProxyCacheService.
- Updated getRewrittenManifest to handle 404 responses and retrieve manifests from private sources.
- Improved error handling in getPackageManifest with try-catch and logging.
- Destructured response for concise access and adjusted tarball URL replacement logic.
app/port/controller/package/UpdatePackageController.ts - Added dependencies for SyncMode, ProxyCacheRepository, and ProxyCacheService.
- Introduced properties proxyCacheRepository and proxyCacheService.
- Updated update method to handle proxy cache updates based on synchronization mode.

Possibly related PRs

  • feat: proxy mode [sql changed] #571: The changes in ProxyCacheService regarding the handling of package manifests and integration with private sources are directly related to the modifications made in the same service within this PR, which also focuses on caching mechanisms and managing package versions.

Suggested labels

enhancement, sql

Suggested reviewers

  • elrrrrrrr
  • fengmk2

Poem

🐰 In the garden where the packages grow,
A service now smarter, with a new flow.
When 404s come knocking, it won't miss,
Private sources to fetch, a manifest bliss.
With errors now caught, like leaves in the breeze,
ProxyCache dances, with grace and with ease! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

sweep-ai bot commented Nov 1, 2024

Hey @fangzhengjin, here is an example of how you can ask me to improve this pull request:

@Sweep Add unit tests for the updated `getPackageManifest` method to verify error handling behavior when cache retrieval fails

📖 For more information on how to use Sweep, please read our documentation.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (1)
app/core/service/ProxyCacheService.ts (1)

184-194: Avoid variable shadowing of 'manifest'

Within the if (status === 404) block, the variable manifest is redefined when destructuring from data, which can lead to confusion due to variable shadowing. Consider renaming the inner manifest variable to avoid this issue.

Apply this diff to rename the variable:

- const { etag, data: manifest, blockReason } = ...
+ const { etag, data: privateManifest, blockReason } = ...

And update the return statement:

- return manifest as any;
+ return privateManifest as any;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4facf90 and e2c4c36.

📒 Files selected for processing (1)
  • app/core/service/ProxyCacheService.ts (6 hunks)
🧰 Additional context used
📓 Learnings (1)
app/core/service/ProxyCacheService.ts (3)
Learnt from: hezhengxu2018
PR: cnpm/cnpmcore#571
File: app/core/service/ProxyCacheService.ts:161-209
Timestamp: 2024-10-09T03:45:47.108Z
Learning: In the `ProxyCacheService` class, requests to the upstream repository already throw an error if they fail, making additional checks for the response status unnecessary.
Learnt from: hezhengxu2018
PR: cnpm/cnpmcore#571
File: app/core/service/ProxyCacheService.ts:161-209
Timestamp: 2024-10-09T03:45:48.990Z
Learning: In the `ProxyCacheService` class, requests to the upstream repository already throw an error if they fail, making additional checks for the response status unnecessary.
Learnt from: hezhengxu2018
PR: cnpm/cnpmcore#571
File: app/core/service/ProxyCacheService.ts:161-209
Timestamp: 2024-07-19T06:26:05.533Z
Learning: In the `ProxyCacheService` class, requests to the upstream repository already throw an error if they fail, making additional checks for the response status unnecessary.

app/core/service/ProxyCacheService.ts Outdated Show resolved Hide resolved
app/core/service/ProxyCacheService.ts Outdated Show resolved Hide resolved
@fangzhengjin fangzhengjin changed the title [WIP] feat: 在proxy模式下,可以返回私有包 feat: 在proxy模式下,可以返回私有包 Nov 1, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
app/core/service/ProxyCacheService.ts (1)

218-225: Add timestamp validation for time merging

The time merging logic should validate timestamp formats to prevent invalid dates.

Consider adding timestamp validation:

 if (manifest.time) {
   const privateTimeMap = data?.time || {};
   for (const key in privateTimeMap) {
+    const timestamp = privateTimeMap[key];
+    if (!timestamp || isNaN(Date.parse(timestamp))) continue;
     if (!manifest.time[key]) {
       manifest.time[key] = privateTimeMap[key];
     }
   }
 }
app/port/controller/package/UpdatePackageController.ts (2)

74-74: Maintain consistency in comment language

The comment on line 74 is in Chinese: // 代理模式下,更新代理缓存. To maintain consistency across the codebase, consider translating comments to English.


79-79: Maintain consistency in comment language

The comment on line 79 is in Chinese: // 仅manifests需要更新,指定版本的package.json文件发布后不会改变. To maintain consistency across the codebase, consider translating comments to English.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e2c4c36 and b430e6d.

📒 Files selected for processing (2)
  • app/core/service/ProxyCacheService.ts (6 hunks)
  • app/port/controller/package/UpdatePackageController.ts (3 hunks)
🧰 Additional context used
📓 Learnings (1)
app/core/service/ProxyCacheService.ts (3)
Learnt from: hezhengxu2018
PR: cnpm/cnpmcore#571
File: app/core/service/ProxyCacheService.ts:161-209
Timestamp: 2024-10-09T03:45:47.108Z
Learning: In the `ProxyCacheService` class, requests to the upstream repository already throw an error if they fail, making additional checks for the response status unnecessary.
Learnt from: hezhengxu2018
PR: cnpm/cnpmcore#571
File: app/core/service/ProxyCacheService.ts:161-209
Timestamp: 2024-10-09T03:45:48.990Z
Learning: In the `ProxyCacheService` class, requests to the upstream repository already throw an error if they fail, making additional checks for the response status unnecessary.
Learnt from: hezhengxu2018
PR: cnpm/cnpmcore#571
File: app/core/service/ProxyCacheService.ts:161-209
Timestamp: 2024-07-19T06:26:05.533Z
Learning: In the `ProxyCacheService` class, requests to the upstream repository already throw an error if they fail, making additional checks for the response status unnecessary.
🔇 Additional comments (6)
app/core/service/ProxyCacheService.ts (4)

20-20: LGTM: PackageManagerService integration

The PackageManagerService dependency is correctly imported and injected following the established pattern.

Also applies to: 55-56


66-76: LGTM: Robust error handling for cache retrieval

The error handling implementation is well-structured:

  • Gracefully handles cache misses
  • Properly logs errors without breaking execution
  • Uses correct context in log messages

Line range hint 197-226: Optimize version map merging logic

The current implementation of merging private and public versions could be more efficient and safer.

Consider refactoring to use a more functional approach:

-      const privateVersionMap = data?.versions || {};
-      for (const key in privateVersionMap) {
-        if (!versionMap[key]) {
-          versionMap[key] = privateVersionMap[key];
-        }
-      }
+      manifest.versions = {
+        ...privateVersionMap,
+        ...versionMap,
+      };

Also, consider adding a lock or synchronization mechanism to prevent potential race conditions during version map updates.


184-194: Consider adding manifest validation

The code handles 404 status and private package retrieval, but there's no validation of the manifest structure before processing.

Consider adding validation before processing the manifest:

 const { status, data: { manifest } } = responseResult;
+if (!manifest || typeof manifest !== 'object') {
+  throw new Error('Invalid manifest structure received');
+}
 // sourceRegistry not found, check private package
app/port/controller/package/UpdatePackageController.ts (2)

20-22: Imports are properly added

The modules SyncMode, ProxyCacheRepository, isPkgManifest, and ProxyCacheService are correctly imported and used in the code.


36-39: Dependency injection setup is correct

The dependencies proxyCacheRepository and proxyCacheService are properly injected as private readonly class properties with the @Inject() decorator.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e2c4c36 and 760c862.

📒 Files selected for processing (2)
  • app/core/service/ProxyCacheService.ts (5 hunks)
  • app/port/controller/package/UpdatePackageController.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/core/service/ProxyCacheService.ts

@fangzhengjin fangzhengjin force-pushed the feature/proxySupportPrivatePackage branch from c54a9e4 to 3aea4c8 Compare November 1, 2024 15:35
@fangzhengjin fangzhengjin changed the title feat: 在proxy模式下,可以返回私有包 feat: proxy模式增强 Nov 1, 2024
@fangzhengjin
Copy link
Author

@fengmk2 空了麻烦看下有啥要调整的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant