Ejemplo n.º 1
0
        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 (range.DoesRangeSatisfy(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));
        }
Ejemplo n.º 2
0
        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);
        }