コード例 #1
0
        public async Task <NpmPackageFile?> DownloadPackageAsync(NpmPackageId id, CancellationToken token)
        {
            // does not work: https://registry.npmjs.org/@types%2Fangular/1.6.55
            var url = new Uri(new Uri(Host), UrlEncoder.Default.Encode(id.Name));

            JObject index;

            using (var client = HttpClientFactory())
                using (var response = await client.GetAsync(url, token))
                {
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(null);
                    }

                    await response.AssertStatusCodeOk();

                    using (var stream = await response.Content.ReadAsStreamAsync())
                    {
                        index = stream.JsonDeserialize <JObject>();
                    }
                }

            var version = index.Value <JObject>("versions").Value <JObject>(id.Version);

            if (version == null)
            {
                return(null);
            }

            // https://registry.npmjs.org/@types/angular/-/angular-1.6.55.tgz
            var packageUrl = new Uri(version.Value <JObject>("dist").Value <string>("tarball"));

            var fileName = packageUrl.LocalPath.Substring(packageUrl.LocalPath.LastIndexOf('/') + 1);

            using (var client = HttpClientFactory())
                using (var stream = await client.GetStreamAsync(packageUrl))
                {
                    var content = await stream.ToArrayAsync(token);

                    return(new NpmPackageFile(fileName, content));
                }
        }
コード例 #2
0
        public async Task DownloadPackage()
        {
            var package = new NpmPackageId("@types/angular", "1.6.55");

            _mockHttp
            .When(HttpMethod.Get, NpmApi.Host + "/@types%2Fangular")
            .Respond(
                MediaTypeNames.Application.Json,
                TempFile.OpenResource(GetType(), "NpmApiTest.TypesAngular.get.json"));

            _mockHttp
            .When(HttpMethod.Get, "https://registry.npmjs.org/@types/angular/-/angular-1.6.55.tgz")
            .Respond(
                MediaTypeNames.Application.Octet,
                TempFile.OpenResource(GetType(), "NpmApiTest.TypesAngular.1.6.56.tgz"));

            var content = await _sut.DownloadPackageAsync(package, CancellationToken.None);

            content.ShouldNotBeNull();
            content.Value.Name.ShouldBe("angular-1.6.55.tgz");
            content.Value.Content.Length.ShouldBe(32132);
        }