-
Notifications
You must be signed in to change notification settings - Fork 702
/
Copy pathRegistrationUtility.cs
122 lines (106 loc) · 4.44 KB
/
RegistrationUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NuGet.Common;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
namespace NuGet.Protocol
{
public static class RegistrationUtility
{
public static VersionRange CreateVersionRange(string stringToParse)
{
if (string.IsNullOrEmpty(stringToParse))
{
return VersionRange.All;
}
var range = VersionRange.Parse(stringToParse);
return new VersionRange(range.MinVersion, range.IsMinInclusive, range.MaxVersion, range.IsMaxInclusive);
}
public async static Task<IEnumerable<JObject>> LoadRanges(
HttpSource httpSource,
Uri registrationUri,
string packageId,
VersionRange range,
SourceCacheContext cacheContext,
ILogger log,
CancellationToken token)
{
var packageIdLowerCase = packageId.ToLowerInvariant();
var httpSourceCacheContext = HttpSourceCacheContext.Create(cacheContext, 0);
var index = await httpSource.GetAsync(
new HttpSourceCachedRequest(
registrationUri.OriginalString,
$"list_{packageIdLowerCase}_index",
httpSourceCacheContext)
{
IgnoreNotFounds = true,
},
async httpSourceResult =>
{
return await httpSourceResult.Stream.AsJObjectAsync(token);
},
log,
token);
if (index == null)
{
// The server returned a 404, the package does not exist
return Enumerable.Empty<JObject>();
}
IList<Task<JObject>> rangeTasks = new List<Task<JObject>>();
foreach (JObject item in index["items"])
{
var lower = NuGetVersion.Parse(item["lower"].ToString());
var upper = NuGetVersion.Parse(item["upper"].ToString());
if (IsItemRangeRequired(range, lower, upper))
{
JToken items;
if (!item.TryGetValue("items", out items))
{
var rangeUri = item["@id"].ToString();
rangeTasks.Add(httpSource.GetAsync(
new HttpSourceCachedRequest(
rangeUri,
$"list_{packageIdLowerCase}_range_{lower.ToNormalizedString()}-{upper.ToNormalizedString()}",
httpSourceCacheContext)
{
IgnoreNotFounds = true,
},
async httpSourceResult =>
{
return await httpSourceResult.Stream.AsJObjectAsync(token);
},
log,
token));
}
else
{
rangeTasks.Add(Task.FromResult(item));
}
}
}
await Task.WhenAll(rangeTasks.ToArray());
return rangeTasks.Select((t) => t.Result);
}
private static bool IsItemRangeRequired(VersionRange dependencyRange, NuGetVersion catalogItemLower, NuGetVersion catalogItemUpper)
{
var catalogItemVersionRange = new VersionRange(minVersion: catalogItemLower, includeMinVersion: true,
maxVersion: catalogItemUpper, includeMaxVersion: true);
if (dependencyRange.HasLowerAndUpperBounds) // Mainly to cover the '!dependencyRange.IsMaxInclusive && !dependencyRange.IsMinInclusive' case
{
return catalogItemVersionRange.Satisfies(dependencyRange.MinVersion) || catalogItemVersionRange.Satisfies(dependencyRange.MaxVersion);
}
else
{
return dependencyRange.Satisfies(catalogItemLower) || dependencyRange.Satisfies(catalogItemUpper);
}
}
}
}