Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions eng/common-tests/Verify-Links-ProcessNpmLink.Tests.ps1
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"
}
}
Comment thread
weshaggard marked this conversation as resolved.
18 changes: 17 additions & 1 deletion eng/common/scripts/Verify-Links.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
weshaggard marked this conversation as resolved.
Outdated
$urlString = $linkUri.ToString()
if ($urlString -match '^https?://(?:www\.)?npmjs\.com/package/([^/]+(?:/[^/]+)?)/v/(.+)$') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if ($urlString -match '^https?://(?:www\.)?npmjs\.com/package/([^/]+(?:/[^/]+)?)/v/(.+)$') {
if ($urlString -match '^https?://(?:www\.)?npmjs\.com/package/(.+)/v/(.+)$') {

Copilot you can simplify this regex with the suggestion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Simplified the regex pattern as suggested. Commit a36dad8 changes ([^/]+(?:/[^/]+)?) to (.+) in both the main code and test helper function. All tests pass with the simplified pattern.

# 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)
}
Expand Down