|
| 1 | +import { XMLParser } from 'fast-xml-parser' |
| 2 | + |
1 | 3 | import { canonicalizeRepoUrl } from '../utils/canonicalizeRepoUrl' |
2 | 4 |
|
3 | 5 | import { |
@@ -28,6 +30,18 @@ function parsePublishedDate(published: string | undefined): Date | null { |
28 | 30 | return !isNaN(date.getTime()) && date.getUTCFullYear() > 1900 ? date : null |
29 | 31 | } |
30 | 32 |
|
| 33 | +const nuspecParser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_' }) |
| 34 | + |
| 35 | +export function parseNuspecRepositoryUrl(nuspecXml: string): string | null { |
| 36 | + try { |
| 37 | + const doc = nuspecParser.parse(nuspecXml) |
| 38 | + const url = doc?.package?.metadata?.repository?.['@_url'] |
| 39 | + return typeof url === 'string' && url.trim() !== '' ? url.trim() : null |
| 40 | + } catch { |
| 41 | + return null |
| 42 | + } |
| 43 | +} |
| 44 | + |
31 | 45 | const SCM_HOSTS = ['github.com', 'gitlab.com', 'bitbucket.org'] |
32 | 46 |
|
33 | 47 | function isScmUrl(url: string | undefined): boolean { |
@@ -56,6 +70,7 @@ export function normalizeNuGetPackage( |
56 | 70 | packageId: string, |
57 | 71 | searchResult: NuGetSearchItem | null, |
58 | 72 | registration: NuGetRegistrationIndex, |
| 73 | + nuspecXml?: string | null, |
59 | 74 | ): NormalizedNuGetPackage { |
60 | 75 | const allLeaves = registration.items.flatMap((page) => page.items ?? []) |
61 | 76 | const allEntries: NuGetCatalogEntry[] = allLeaves.map((leaf) => leaf.catalogEntry) |
@@ -88,7 +103,9 @@ export function normalizeNuGetPackage( |
88 | 103 | ...(latestEntry ? [latestEntry] : []), |
89 | 104 | ] |
90 | 105 | : [...allEntries].reverse() |
91 | | - const nuspecRepoUrl = entriesForRepo.find((e) => e.repository?.url)?.repository?.url |
| 106 | + const catalogRepoUrl = entriesForRepo.find((e) => e.repository?.url)?.repository?.url |
| 107 | + const fetchedNuspecRepoUrl = nuspecXml ? parseNuspecRepositoryUrl(nuspecXml) : null |
| 108 | + const nuspecRepoUrl = fetchedNuspecRepoUrl ?? catalogRepoUrl |
92 | 109 | const declaredRepositoryUrl = nuspecRepoUrl ?? null |
93 | 110 | const repo = |
94 | 111 | (nuspecRepoUrl ? canonicalizeRepoUrl(nuspecRepoUrl) : null) ?? |
|
0 commit comments