-
Notifications
You must be signed in to change notification settings - Fork 256
Fix npm package URL transformation to handle versioned URLs and edge cases correctly #13307
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
Changes from 3 commits
b135735
d040a06
b22fb96
a36dad8
1585d74
9c852d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| Import-Module Pester | ||
|
|
||
| BeforeAll { | ||
| . $PSScriptRoot/../common/scripts/Verify-Links.ps1 | ||
|
|
||
| # Helper function to test URL transformation without making web requests | ||
| function Get-TransformedNpmUrl([System.Uri]$linkUri) { | ||
| $urlString = $linkUri.ToString() | ||
| if ($urlString -match '^https?://(?:www\.)?npmjs\.com/package/([^/]+(?:/[^/]+)?)/v/(.+)$') { | ||
| # Versioned URL: remove the /v/ segment but keep the version | ||
| return "https://registry.npmjs.org/$($matches[1])/$($matches[2])" | ||
| } | ||
| elseif ($urlString -match '^https?://(?:www\.)?npmjs\.com/package/(.+)$') { | ||
| # Non-versioned URL: just replace the domain | ||
| return "https://registry.npmjs.org/$($matches[1])" | ||
| } | ||
| else { | ||
| # Fallback: use the original URL if it doesn't match expected patterns | ||
| return $urlString | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Describe "ProcessNpmLink" { | ||
| It "Should handle versioned scoped package URL" { | ||
| $inputUrl = [System.Uri]"https://www.npmjs.com/package/@azure/ai-agents/v/1.1.0" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/@azure/ai-agents/1.1.0" | ||
| } | ||
|
|
||
| It "Should handle versioned unscoped package URL" { | ||
| $inputUrl = [System.Uri]"https://www.npmjs.com/package/express/v/4.18.2" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/express/4.18.2" | ||
| } | ||
|
|
||
| It "Should handle non-versioned scoped package URL" { | ||
| $inputUrl = [System.Uri]"https://www.npmjs.com/package/@azure/ai-agents" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/@azure/ai-agents" | ||
| } | ||
|
|
||
| It "Should handle non-versioned unscoped package URL" { | ||
| $inputUrl = [System.Uri]"https://www.npmjs.com/package/express" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/express" | ||
| } | ||
|
|
||
| It "Should handle URL without www prefix - versioned" { | ||
| $inputUrl = [System.Uri]"https://npmjs.com/package/@azure/ai-agents/v/1.1.0" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/@azure/ai-agents/1.1.0" | ||
| } | ||
|
|
||
| It "Should handle URL without www prefix - non-versioned" { | ||
| $inputUrl = [System.Uri]"https://npmjs.com/package/@azure/identity" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/@azure/identity" | ||
| } | ||
|
|
||
| It "Should handle package name with hyphens and numbers" { | ||
| $inputUrl = [System.Uri]"https://www.npmjs.com/package/lodash-es" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/lodash-es" | ||
| } | ||
|
|
||
| It "Should handle package with multiple version segments" { | ||
| $inputUrl = [System.Uri]"https://www.npmjs.com/package/@azure/ai-agents/v/1.1.0-beta.1" | ||
| $apiUrl = Get-TransformedNpmUrl $inputUrl | ||
| $apiUrl | Should -Be "https://registry.npmjs.org/@azure/ai-agents/1.1.0-beta.1" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -163,7 +163,23 @@ function ProcessCratesIoLink([System.Uri]$linkUri, $path) { | |||||
| function ProcessNpmLink([System.Uri]$linkUri) { | ||||||
| # npmjs.com started using Cloudflare which returns 403 and we need to instead check the registry api for existence checks | ||||||
| # https://github.com/orgs/community/discussions/174098#discussioncomment-14461226 | ||||||
| $apiUrl = $linkUri.ToString() -replace '^https?://(?:www\.)?npmjs\.com/package/(.*)/v', 'https://registry.npmjs.org/$1' | ||||||
|
|
||||||
| # Handle versioned URLs: https://www.npmjs.com/package/@azure/ai-agents/v/1.1.0 -> https://registry.npmjs.org/@azure/ai-agents/1.1.0 | ||||||
| # Handle non-versioned URLs: https://www.npmjs.com/package/@azure/ai-agents -> https://registry.npmjs.org/@azure/ai-agents | ||||||
| # The regex captures the package name (which may contain a slash for scoped packages) and optionally the version | ||||||
|
weshaggard marked this conversation as resolved.
Outdated
|
||||||
| $urlString = $linkUri.ToString() | ||||||
| if ($urlString -match '^https?://(?:www\.)?npmjs\.com/package/([^/]+(?:/[^/]+)?)/v/(.+)$') { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Copilot you can simplify this regex with the suggestion.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplified the regex pattern as suggested. Commit a36dad8 changes |
||||||
| # Versioned URL: remove the /v/ segment but keep the version | ||||||
| $apiUrl = "https://registry.npmjs.org/$($matches[1])/$($matches[2])" | ||||||
| } | ||||||
| elseif ($urlString -match '^https?://(?:www\.)?npmjs\.com/package/(.+)$') { | ||||||
| # Non-versioned URL: just replace the domain | ||||||
| $apiUrl = "https://registry.npmjs.org/$($matches[1])" | ||||||
| } | ||||||
| else { | ||||||
| # Fallback: use the original URL if it doesn't match expected patterns | ||||||
| $apiUrl = $urlString | ||||||
| } | ||||||
|
|
||||||
| return ProcessStandardLink ([System.Uri]$apiUrl) | ||||||
| } | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.