-
Notifications
You must be signed in to change notification settings - Fork 703
/
Copy pathPackageMetadataResourceV3.cs
278 lines (250 loc) · 12.1 KB
/
PackageMetadataResourceV3.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// 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.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NuGet.Common;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Extensions;
using NuGet.Protocol.Model;
using NuGet.Versioning;
namespace NuGet.Protocol
{
public class PackageMetadataResourceV3 : PackageMetadataResource
{
private readonly RegistrationResourceV3 _regResource;
private readonly ReportAbuseResourceV3 _reportAbuseResource;
private readonly PackageDetailsUriResourceV3 _packageDetailsUriResource;
private readonly HttpSource _client;
public PackageMetadataResourceV3(
HttpSource client,
RegistrationResourceV3 regResource,
ReportAbuseResourceV3 reportAbuseResource,
PackageDetailsUriResourceV3 packageDetailsUriResource)
{
_regResource = regResource;
_client = client;
_reportAbuseResource = reportAbuseResource;
_packageDetailsUriResource = packageDetailsUriResource;
}
/// <param name="packageId">PackageId for package we're looking.</param>
/// <param name="includePrerelease">Whether to include PreRelease versions into result.</param>
/// <param name="includeUnlisted">Whether to include Unlisted versions into result.</param>
/// <param name="sourceCacheContext">SourceCacheContext for cache.</param>
/// <param name="log">Logger Instance.</param>
/// <param name="token">Cancellation token.</param>
/// <returns>List of package metadata.</returns>
public override async Task<IEnumerable<IPackageSearchMetadata>> GetMetadataAsync(
string packageId,
bool includePrerelease,
bool includeUnlisted,
SourceCacheContext sourceCacheContext,
Common.ILogger log,
CancellationToken token)
{
return await GetMetadataAsync(packageId, includePrerelease, includeUnlisted, range: VersionRange.All, sourceCacheContext, log, token);
}
/// <summary>
/// Returns the registration metadata for the id and version
/// </summary>
/// <param name="package"></param>
/// <param name="sourceCacheContext"></param>
/// <param name="log"></param>
/// <param name="token"></param>
/// <returns>Package meta data.</returns>
/// <remarks>The inlined entries are potentially going away soon</remarks>
public override async Task<IPackageSearchMetadata> GetMetadataAsync(
PackageIdentity package,
SourceCacheContext sourceCacheContext,
Common.ILogger log,
CancellationToken token)
{
var range = new VersionRange(package.Version, includeMinVersion: true, package.Version, includeMaxVersion: true);
var packageMetaDatas = await GetMetadataAsync(package.Id, includePrerelease: true, includeUnlisted: true, range, sourceCacheContext, log, token);
return packageMetaDatas.SingleOrDefault();
}
private async Task<IEnumerable<IPackageSearchMetadata>> GetMetadataAsync(
string packageId,
bool includePrerelease,
bool includeUnlisted,
VersionRange range,
SourceCacheContext sourceCacheContext,
ILogger log,
CancellationToken token)
{
var metadataCache = new MetadataReferenceCache();
var registrationUri = _regResource.GetUri(packageId);
var (registrationIndex, httpSourceCacheContext) = await LoadRegistrationIndexAsync(
_client,
registrationUri,
packageId,
sourceCacheContext,
httpSourceResult => DeserializeStreamDataAsync<RegistrationIndex>(httpSourceResult.Stream, token),
log,
token);
if (registrationIndex == null)
{
// The server returned a 404, the package does not exist
return Enumerable.Empty<PackageSearchMetadataRegistration>();
}
var results = new List<PackageSearchMetadataRegistration>();
foreach (var registrationPage in registrationIndex.Items)
{
if (registrationPage == null)
{
throw new InvalidDataException(registrationUri.AbsoluteUri);
}
var lower = NuGetVersion.Parse(registrationPage.Lower);
var upper = NuGetVersion.Parse(registrationPage.Upper);
if (range.DoesRangeSatisfy(lower, upper))
{
if (registrationPage.Items == null)
{
var rangeUri = registrationPage.Url;
var leafRegistrationPage = await GetRegistratioIndexPageAsync(_client, rangeUri, packageId, lower, upper, httpSourceCacheContext, log, token);
if (registrationPage == null)
{
throw new InvalidDataException(registrationUri.AbsoluteUri);
}
ProcessRegistrationPage(leafRegistrationPage, results, range, includePrerelease, includeUnlisted, metadataCache);
}
else
{
ProcessRegistrationPage(registrationPage, results, range, includePrerelease, includeUnlisted, metadataCache);
}
}
}
return results;
}
/// <summary>
/// Deserialize stream from RegistrationIndex/RegistrationPage and return list of RegistrationPages or RegistrationPage.
/// </summary>
/// <typeparam name="T">Generic type</typeparam>
/// <param name="stream">Stream data to read.</param>
/// <param name="token">Cancellation token.</param>
/// <returns></returns>
private async Task<T> DeserializeStreamDataAsync<T>(Stream stream, CancellationToken token)
{
token.ThrowIfCancellationRequested();
if (stream == null)
{
return default(T);
}
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var registrationIndex = JsonExtensions.JsonObjectSerializer
.Deserialize<T>(jsonReader);
return await Task.FromResult(registrationIndex);
}
}
/// <summary>
/// Query RegistrationIndex from nuget server for Package Manager UI. This implementation optimized for performance so instead of keeping giant JObject in memory we use strong types.
/// </summary>
/// <param name="httpSource">Httpsource instance</param>
/// <param name="registrationUri">Package registration url</param>
/// <param name="packageId">PackageId for package we're looking.</param>
/// <param name="cacheContext">CacheContext for cache.</param>
/// <param name="processAsync">Func expression used for HttpSource.cs</param>
/// <param name="log">Logger Instance.</param>
/// <param name="token">Cancellation token.</param>
/// <returns></returns>
private async Task<ValueTuple<RegistrationIndex, HttpSourceCacheContext>> LoadRegistrationIndexAsync(
HttpSource httpSource,
Uri registrationUri,
string packageId,
SourceCacheContext cacheContext,
Func<HttpSourceResult, Task<RegistrationIndex>> processAsync,
ILogger log,
CancellationToken token)
{
var packageIdLowerCase = packageId.ToLowerInvariant();
var retryCount = 0;
var httpSourceCacheContext = HttpSourceCacheContext.Create(cacheContext, retryCount);
var index = await httpSource.GetAsync(
new HttpSourceCachedRequest(
registrationUri.OriginalString,
$"list_{packageIdLowerCase}_index",
httpSourceCacheContext)
{
IgnoreNotFounds = true,
},
async httpSourceResult => await processAsync(httpSourceResult),
log,
token);
return new ValueTuple<RegistrationIndex, HttpSourceCacheContext>(index, httpSourceCacheContext);
}
/// <summary>
/// Process RegistrationIndex
/// </summary>
/// <param name="httpSource">Httpsource instance.</param>
/// <param name="rangeUri">Paged registration index url address.</param>
/// <param name="packageId">PackageId for package we're checking.</param>
/// <param name="lower">Lower bound of nuget package.</param>
/// <param name="upper">Upper bound of nuget package.</param>
/// <param name="httpSourceCacheContext">SourceCacheContext for cache.</param>
/// <param name="log">Logger Instance.</param>
/// <param name="token">Cancellation token.</param>
/// <returns></returns>
private Task<RegistrationPage> GetRegistratioIndexPageAsync(
HttpSource httpSource,
string rangeUri,
string packageId,
NuGetVersion lower,
NuGetVersion upper,
HttpSourceCacheContext httpSourceCacheContext,
ILogger log,
CancellationToken token)
{
var packageIdLowerCase = packageId.ToLowerInvariant();
var registrationPage = httpSource.GetAsync(
new HttpSourceCachedRequest(
rangeUri,
$"list_{packageIdLowerCase}_range_{lower.ToNormalizedString()}-{upper.ToNormalizedString()}",
httpSourceCacheContext)
{
IgnoreNotFounds = true,
},
httpSourceResult => DeserializeStreamDataAsync<RegistrationPage>(httpSourceResult.Stream, token),
log,
token);
return registrationPage;
}
/// <summary>
/// Process RegistrationPage
/// </summary>
/// <param name="registrationPage">Nuget registration page.</param>
/// <param name="results">Used to return nuget result.</param>
/// <param name="range">Nuget version range.</param>
/// <param name="includePrerelease">Whether to include PreRelease versions into result.</param>
/// <param name="includeUnlisted">Whether to include Unlisted versions into result.</param>
private void ProcessRegistrationPage(
RegistrationPage registrationPage,
List<PackageSearchMetadataRegistration> results,
VersionRange range, bool includePrerelease,
bool includeUnlisted,
MetadataReferenceCache metadataCache)
{
foreach (RegistrationLeafItem registrationLeaf in registrationPage.Items)
{
PackageSearchMetadataRegistration catalogEntry = registrationLeaf.CatalogEntry;
NuGetVersion version = catalogEntry.Version;
bool listed = catalogEntry.IsListed;
if (range.Satisfies(catalogEntry.Version)
&& (includePrerelease || !version.IsPrerelease)
&& (includeUnlisted || listed))
{
catalogEntry.ReportAbuseUrl = _reportAbuseResource?.GetReportAbuseUrl(catalogEntry.PackageId, catalogEntry.Version);
catalogEntry.PackageDetailsUrl = _packageDetailsUriResource?.GetUri(catalogEntry.PackageId, catalogEntry.Version);
catalogEntry = metadataCache.GetObject(catalogEntry);
results.Add(catalogEntry);
}
}
}
}
}