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: compatible verdaccio path style #723

Merged

Conversation

fangzhengjin
Copy link
Contributor

@fangzhengjin fangzhengjin commented Nov 1, 2024

兼容Verdaccio下载地址风格,镜像库从Verdaccio切换至cnpmcore后无需大面积调整lock文件

Summary by CodeRabbit

  • New Features

    • Introduced a comprehensive test suite for the download functionality, ensuring robust behavior for various package retrieval scenarios.
    • Enhanced validation for CORS requests and CDN redirection during package downloads.
    • Added new test cases for downloading version tarballs with scoped package handling.
  • Bug Fixes

    • Improved error handling for non-existent packages and versions, providing clearer error messages.
    • Ensured proper handling of deprecated download paths and scoped/non-scoped package names.

Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

Walkthrough

The changes replace the implementation of the DownloadPackageVersionTarController class with a comprehensive test suite. The original methods for handling package downloads have been removed, and the new code introduces tests that validate the download functionality, including CORS handling, CDN redirection, and various package retrieval scenarios. The tests ensure correct HTTP status codes, headers, and error messages for different conditions, focusing on robustness and adherence to specified requirements.

Changes

File Path Change Summary
app/port/controller/package/DownloadPackageVersionTar.ts Removed DownloadPackageVersionTarController class and replaced it with a comprehensive test suite for download functionality.
test/port/controller/package/DownloadPackageVersionTarController.test.ts Updated variable scopedName to scope, added new test cases for scoped package handling, and ensured comprehensive coverage for various download scenarios.

Poem

In the burrow where packages dwell,
New tests are hopping, oh what a swell!
With CORS and CDN, they play all day,
Validating paths in a cheerful way.
A rabbit's cheer for the code so bright,
Ensuring downloads work just right! 🐇✨


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 new `downloadVerdaccioPathStyle` endpoint that verify:
1. The correct handling of scoped package downloads using the Verdaccio-style path format
2. That the endpoint correctly forwards requests to the existing `download` method
3. That the URL pattern matching works as expected with different package name formats

📖 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: 0

🧹 Outside diff range and nitpick comments (1)
app/port/controller/package/DownloadPackageVersionTar.ts (1)

137-155: Consider alternative approaches for path compatibility.

While the current implementation works, consider these architectural improvements:

  1. Instead of duplicating route handlers, consider implementing path normalization middleware that converts Verdaccio-style paths to your standard format before reaching the controller.
  2. This would reduce code duplication and make it easier to maintain path compatibility in the future.

Example middleware approach:

// middleware/normalizePackagePath.ts
export async function normalizePackagePath(ctx: EggContext, next: () => Promise<void>) {
  const match = ctx.path.match(/\/-\/@(.+?)\//);
  if (match) {
    ctx.path = ctx.path.replace(`/-/@${match[1]}/`, '/-/');
  }
  await next();
}

Would you like me to help create a detailed proposal for this alternative approach?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

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

📒 Files selected for processing (1)
  • app/port/controller/package/DownloadPackageVersionTar.ts (1 hunks)
🔇 Additional comments (3)
app/port/controller/package/DownloadPackageVersionTar.ts (3)

136-138: LGTM! Good section separation.

The added comment and spacing improve code organization and readability.


148-155: ⚠️ Potential issue

Add validation and documentation for Verdaccio path style.

Similar issues as the OPTIONS handler, plus missing documentation about the Verdaccio path style compatibility.

Apply similar changes to this method:

 @HTTPMethod({
-    // GET /:fullname/-/:scope/:filenameWithVersion.tgz
+    // GET /:fullname/-/@:scope/:filenameWithVersion.tgz
-    path: `/:fullname(${FULLNAME_REG_STRING})/-/:scope/:filenameWithVersion.tgz`,
+    path: `/:fullname(${FULLNAME_REG_STRING})/-/@:scope/:filenameWithVersion.tgz`,
     method: HTTPMethodEnum.GET,
   })
-  async downloadVerdaccioPathStyle(@Context() ctx: EggContext, @HTTPParam() fullname: string, @HTTPParam() filenameWithVersion: string) {
+  async downloadVerdaccioPathStyle(@Context() ctx: EggContext, @HTTPParam() fullname: string, @HTTPParam() scope: string, @HTTPParam() filenameWithVersion: string) {
+    const [ pkgScope ] = getScopeAndName(fullname);
+    if (pkgScope !== `@${scope}`) {
+      throw new NotFoundError(`Scope mismatch: ${pkgScope} !== @${scope}`);
+    }
     return this.download(ctx, fullname, filenameWithVersion);
   }

Add JSDoc comments to document the Verdaccio path style compatibility:

+  /**
+   * Handle package downloads using Verdaccio-style paths
+   * Example: /@scope/pkg/-/@scope/pkg-1.0.0.tgz
+   * This provides compatibility for projects migrating from Verdaccio
+   * without requiring changes to their lock files.
+   */
   @HTTPMethod({

Let's verify the path style compatibility with Verdaccio:

#!/bin/bash
# Search for examples of Verdaccio path patterns in the codebase
rg -l "verdaccio" . | while read -r file; do
  echo "=== $file ==="
  rg -A 5 "/-/@.*\.tgz" "$file" || true
done

139-146: ⚠️ Potential issue

Method naming and path handling need improvements.

Several issues to address:

  1. The method name downloadVerdaccioPathStyleorOptions has inconsistent casing. Consider renaming to downloadVerdaccioPathStyleOrOptions.
  2. The :scope parameter in the path pattern is captured but unused. Consider validating it matches the scope in fullname.
  3. The path pattern might conflict with the original download path.

Consider applying these changes:

 @HTTPMethod({
-    // GET /:fullname/-/:scope/:filenameWithVersion.tgz
+    // GET /:fullname/-/@:scope/:filenameWithVersion.tgz
-    path: `/:fullname(${FULLNAME_REG_STRING})/-/:scope/:filenameWithVersion.tgz`,
+    path: `/:fullname(${FULLNAME_REG_STRING})/-/@:scope/:filenameWithVersion.tgz`,
     method: HTTPMethodEnum.OPTIONS,
   })
-  async downloadVerdaccioPathStyleorOptions(@Context() ctx: EggContext) {
+  async downloadVerdaccioPathStyleOrOptions(@Context() ctx: EggContext, @HTTPParam() scope: string) {
+    const [ pkgScope ] = getScopeAndName(ctx.params.fullname);
+    if (pkgScope !== `@${scope}`) {
+      throw new NotFoundError(`Scope mismatch: ${pkgScope} !== @${scope}`);
+    }
     return this.downloadForOptions(ctx);
   }

Let's verify if there are any potential path conflicts:

@fengmk2 fengmk2 changed the title feat: Compatible Verdaccio path style feat: compatible Verdaccio path style 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: 3

🧹 Outside diff range and nitpick comments (2)
app/port/controller/package/DownloadPackageVersionTar.ts (2)

77-90: Consider documenting the OSS-specific behavior.

The test for non-async URL function is skipped for OSS storage type. Consider adding a comment explaining why this test is not applicable for OSS to improve maintainability.

    if (process.env.CNPMCORE_NFS_TYPE !== 'oss') {
+     // OSS SDK only supports async URL generation
      it('should download a version tar redirect to mock cdn success with url function is not async function', async () => {

374-377: Remove commented debug statements.

Remove the commented console.log statement to keep the code clean.

     mock(nfsClientAdapter, 'url', async (storeKey: string) => {
-       // console.log('call url: ', storeKey);
        return `https://cdn.mock.com${storeKey}`;
     });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 492a676 and f58615c.

📒 Files selected for processing (1)
  • app/port/controller/package/DownloadPackageVersionTar.ts (1 hunks)
🧰 Additional context used
🪛 Biome
app/port/controller/package/DownloadPackageVersionTar.ts

[error] 19-41: Disallow duplicate setup and teardown hooks.

Disallow beforeEach duplicacy inside the describe function.

(lint/suspicious/noDuplicateTestHooks)

🔇 Additional comments (1)
app/port/controller/package/DownloadPackageVersionTar.ts (1)

1-419: Test implementation looks good!

The test suite is comprehensive and covers:

  • Different download paths (main, deprecated, and scoped)
  • CDN redirects and streaming downloads
  • CORS handling
  • Error cases and edge scenarios
  • Sync behavior for different modes

This addresses the previous review comment requesting unit tests.

🧰 Tools
🪛 Biome

[error] 19-41: Disallow duplicate setup and teardown hooks.

Disallow beforeEach duplicacy inside the describe function.

(lint/suspicious/noDuplicateTestHooks)

app/port/controller/package/DownloadPackageVersionTar.ts Outdated Show resolved Hide resolved
app/port/controller/package/DownloadPackageVersionTar.ts Outdated Show resolved Hide resolved
app/port/controller/package/DownloadPackageVersionTar.ts Outdated Show resolved Hide resolved
@fangzhengjin fangzhengjin force-pushed the feature/CompatibleVerdaccioPathStyle branch from f58615c to 88b4f78 Compare November 1, 2024 12:32
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 (1)
test/port/controller/package/DownloadPackageVersionTarController.test.ts (1)

375-376: Remove commented console.log statement.

Commented debug statements should be removed to maintain clean test code.

-        // console.log('call url: ', storeKey);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f58615c and 88b4f78.

📒 Files selected for processing (1)
  • test/port/controller/package/DownloadPackageVersionTarController.test.ts (2 hunks)
🔇 Additional comments (3)
test/port/controller/package/DownloadPackageVersionTarController.test.ts (3)

16-18: LGTM! Clear and reusable variable structure.

The refactoring of package name variables improves code clarity and better supports the Verdaccio path style compatibility objective.


372-418: Verify test coverage for Verdaccio path style compatibility.

While the basic success and error cases are covered, we should verify if additional edge cases need testing:

  1. Invalid scope format
  2. Mismatched scope between URL and package name
  3. Non-scoped packages using this route

398-400: ⚠️ Potential issue

Fix incorrect response object usage in assertions.

The assertions are using the response object from the first request instead of the second request's response.

-      assert(res.status === 200);
-      assert(res.headers['content-type'] === 'application/octet-stream');
-      assert(res.headers['content-disposition'] === `attachment; filename="${name}-1.0.0.tgz"`);
+      const scopedRes = await app.httpRequest()
+        .get(`/${scopedName}/-/${scope}/${scopedName}-1.0.0.tgz`);
+      assert(scopedRes.status === 200);
+      assert(scopedRes.headers['content-type'] === 'application/octet-stream');
+      assert(scopedRes.headers['content-disposition'] === `attachment; filename="${name}-1.0.0.tgz"`);

Likely invalid or redundant comment.

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 (1)
test/port/controller/package/DownloadPackageVersionTarController.test.ts (1)

375-376: Remove debug comment.

The commented console.log statement should be removed as it's not providing value.

-        // console.log('call url: ', storeKey);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 88b4f78 and 8788874.

📒 Files selected for processing (1)
  • test/port/controller/package/DownloadPackageVersionTarController.test.ts (2 hunks)
🔇 Additional comments (2)
test/port/controller/package/DownloadPackageVersionTarController.test.ts (2)

16-18: LGTM! Good variable organization.

The separation of scope and name variables improves code clarity and maintainability while supporting the new Verdaccio path style tests.


379-384: ⚠️ Potential issue

Simplify URL pattern to match Verdaccio style.

The current URL pattern includes the scope twice (in scopedName and in the path), which doesn't match Verdaccio's path style.

Apply this fix:

-        .get(`/${name}/-/${scope}/${name}-1.0.0.tgz`);
+        .get(`/${name}/-/${name}-1.0.0.tgz`);
-        .get(`/${scopedName}/-/${scope}/${name}-1.0.0.tgz`);
+        .get(`/${scopedName}/-/${name}-1.0.0.tgz`);

@fangzhengjin
Copy link
Contributor Author

@fengmk2 已添加

@fengmk2 fengmk2 added the enhancement New feature or request label Nov 2, 2024
@fengmk2 fengmk2 self-assigned this Nov 2, 2024
@fengmk2 fengmk2 changed the title feat: compatible Verdaccio path style feat: compatible verdaccio path style Nov 3, 2024
@fengmk2 fengmk2 merged commit 7158e66 into cnpm:master Nov 3, 2024
11 checks passed
fengmk2 pushed a commit that referenced this pull request Nov 3, 2024
[skip ci]

## [3.66.0](v3.65.0...v3.66.0) (2024-11-03)

### Features

* compatible verdaccio path style ([#723](#723)) ([7158e66](7158e66))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants