Beispiel #1
0
        /// <inheritdoc />
        public override async Task <bool> PackageExistsAsync(PackageURL purl, bool useCache = true)
        {
            Logger.Trace("PackageExists {0}", purl?.ToString());
            if (string.IsNullOrEmpty(purl?.Name))
            {
                Logger.Trace("Provided PackageURL was null.");
                return(false);
            }
            string     packageName = purl.GetFullName();
            HttpClient httpClient  = CreateHttpClient();

            return(await CheckJsonCacheForPackage(httpClient, $"{ENV_NPM_API_ENDPOINT}/{packageName}", useCache));
        }
Beispiel #2
0
        /// <inheritdoc />
        public override async Task <IEnumerable <string> > EnumerateVersionsAsync(PackageURL purl, bool useCache = true, bool includePrerelease = true)
        {
            Logger.Trace("EnumerateVersions {0}", purl?.ToString());
            if (purl?.Name is null)
            {
                return(new List <string>());
            }

            try
            {
                string     packageName = purl.GetFullName();
                HttpClient httpClient  = CreateHttpClient();

                JsonDocument doc = await GetJsonCache(httpClient, $"{ENV_NPM_API_ENDPOINT}/{purl.GetFullName(encoded: true)}", useCache);

                List <string> versionList = new();

                foreach (JsonProperty versionKey in doc.RootElement.GetProperty("versions").EnumerateObject())
                {
                    Logger.Debug("Identified {0} version {1}.", packageName, versionKey.Name);
                    versionList.Add(versionKey.Name);
                }

                string?latestVersion = doc.RootElement.GetProperty("dist-tags").GetProperty("latest").GetString();

                // If there was no "latest" property for some reason.
                if (string.IsNullOrWhiteSpace(latestVersion))
                {
                    return(SortVersions(versionList.Distinct()));
                }

                Logger.Debug("Identified {0} latest version as {1}.", packageName, latestVersion);

                // Remove the latest version from the list of versions, so we can add it after sorting.
                versionList.Remove(latestVersion);

                // Sort the list of distinct versions.
                List <string> sortedList = SortVersions(versionList.Distinct()).ToList();

                // Insert the latest version at the beginning of the list.
                sortedList.Insert(0, latestVersion);

                return(sortedList);
            }
            catch (Exception ex)
            {
                Logger.Debug("Unable to enumerate versions: {0}", ex.Message);
                throw;
            }
        }