summaryrefslogtreecommitdiff
path: root/cli/npm/managed/cache/registry_info.rs
blob: ea6b4796983ba5e2af10c9681e410b8c6ab313a7 (plain)
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
279
280
281
282
283
284
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::collections::HashMap;
use std::sync::Arc;

use deno_core::anyhow::anyhow;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::custom_error;
use deno_core::error::AnyError;
use deno_core::futures::future::BoxFuture;
use deno_core::futures::future::Shared;
use deno_core::futures::FutureExt;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_core::url::Url;
use deno_npm::npm_rc::RegistryConfig;
use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::registry::NpmPackageInfo;

use crate::args::CacheSetting;
use crate::http_util::HttpClient;
use crate::npm::common::maybe_auth_header_for_npm_registry;
use crate::util::progress_bar::ProgressBar;

use super::NpmCache;

// todo(dsherret): create seams and unit test this

#[derive(Debug, Clone)]
enum MemoryCacheItem {
  /// The cache item hasn't loaded yet.
  PendingFuture(Shared<PendingRegistryLoadFuture>),
  /// The item has loaded in the past and was stored in the file system cache.
  /// There is no reason to request this package from the npm registry again
  /// for the duration of execution.
  FsCached,
  /// An item is memory cached when it fails saving to the file system cache
  /// or the package does not exist.
  MemoryCached(Result<Option<Arc<NpmPackageInfo>>, Arc<AnyError>>),
}

#[derive(Debug, Clone)]
enum FutureResult {
  PackageNotExists,
  SavedFsCache(Arc<NpmPackageInfo>),
  ErroredFsCache(Arc<NpmPackageInfo>),
}

type PendingRegistryLoadFuture =
  BoxFuture<'static, Result<FutureResult, Arc<AnyError>>>;

/// Downloads packuments from the npm registry.
///
/// This is shared amongst all the workers.
#[derive(Debug)]
pub struct RegistryInfoDownloader {
  cache: Arc<NpmCache>,
  npmrc: Arc<ResolvedNpmRc>,
  progress_bar: ProgressBar,
  memory_cache: Mutex<HashMap<String, MemoryCacheItem>>,
}

impl RegistryInfoDownloader {
  pub fn new(
    cache: Arc<NpmCache>,
    npmrc: Arc<ResolvedNpmRc>,
    progress_bar: ProgressBar,
  ) -> Self {
    Self {
      cache,
      npmrc,
      progress_bar,
      memory_cache: Default::default(),
    }
  }

  pub async fn load_package_info(
    &self,
    name: &str,
    current_runtime_http_client: &Arc<HttpClient>,
  ) -> Result<Option<Arc<NpmPackageInfo>>, AnyError> {
    let registry_url = self.npmrc.get_registry_url(name);
    let registry_config = self.npmrc.get_registry_config(name);

    self
      .load_package_info_inner(
        name,
        registry_url,
        registry_config,
        current_runtime_http_client,
      )
      .await
      .with_context(|| {
        format!(
          "Error getting response at {} for package \"{}\"",
          self.get_package_url(name, registry_url),
          name
        )
      })
  }

  async fn load_package_info_inner(
    &self,
    name: &str,
    registry_url: &Url,
    registry_config: &RegistryConfig,
    current_runtime_http_client: &Arc<HttpClient>,
  ) -> Result<Option<Arc<NpmPackageInfo>>, AnyError> {
    if *self.cache.cache_setting() == CacheSetting::Only {
      return Err(custom_error(
        "NotCached",
        format!(
          "An npm specifier not found in cache: \"{name}\", --cached-only is specified."
        )
      ));
    }

    let (created, cache_item) = {
      let mut mem_cache = self.memory_cache.lock();
      if let Some(cache_item) = mem_cache.get(name) {
        (false, cache_item.clone())
      } else {
        let future = self.create_load_future(
          name,
          registry_url,
          registry_config,
          current_runtime_http_client,
        );
        let cache_item = MemoryCacheItem::PendingFuture(future);
        mem_cache.insert(name.to_string(), cache_item.clone());
        (true, cache_item)
      }
    };
    match cache_item {
      MemoryCacheItem::FsCached => {
        // this struct previously loaded from the registry, so we can load it from the file system cache
        self
          .load_file_cached_package_info(name)
          .await
          .map(|info| Some(Arc::new(info)))
      }
      MemoryCacheItem::MemoryCached(maybe_info) => {
        maybe_info.clone().map_err(|e| anyhow!("{}", e))
      }
      MemoryCacheItem::PendingFuture(future) => {
        if created {
          match future.await {
            Ok(FutureResult::SavedFsCache(info)) => {
              // return back the future and mark this package as having
              // been saved in the cache for next time it's requested
              *self.memory_cache.lock().get_mut(name).unwrap() =
                MemoryCacheItem::FsCached;
              Ok(Some(info))
            }
            Ok(FutureResult::ErroredFsCache(info)) => {
              // since saving to the fs cache failed, keep the package information in memory
              *self.memory_cache.lock().get_mut(name).unwrap() =
                MemoryCacheItem::MemoryCached(Ok(Some(info.clone())));
              Ok(Some(info))
            }
            Ok(FutureResult::PackageNotExists) => {
              *self.memory_cache.lock().get_mut(name).unwrap() =
                MemoryCacheItem::MemoryCached(Ok(None));
              Ok(None)
            }
            Err(err) => {
              let return_err = anyhow!("{}", err);
              *self.memory_cache.lock().get_mut(name).unwrap() =
                MemoryCacheItem::MemoryCached(Err(err));
              Err(return_err)
            }
          }
        } else {
          match future.await {
            Ok(FutureResult::SavedFsCache(info)) => Ok(Some(info)),
            Ok(FutureResult::ErroredFsCache(info)) => Ok(Some(info)),
            Ok(FutureResult::PackageNotExists) => Ok(None),
            Err(err) => Err(anyhow!("{}", err)),
          }
        }
      }
    }
  }

  async fn load_file_cached_package_info(
    &self,
    name: &str,
  ) -> Result<NpmPackageInfo, AnyError> {
    // this scenario failing should be exceptionally rare so let's
    // deal with improving it only when anyone runs into an issue
    let maybe_package_info = deno_core::unsync::spawn_blocking({
      let cache = self.cache.clone();
      let name = name.to_string();
      move || cache.load_package_info(&name)
    })
    .await
    .unwrap()
    .with_context(|| {
      format!(
        "Previously saved '{}' from the npm cache, but now it fails to load.",
        name
      )
    })?;
    match maybe_package_info {
      Some(package_info) => Ok(package_info),
      None => {
        bail!("The package '{}' previously saved its registry information to the file system cache, but that file no longer exists.", name)
      }
    }
  }

  fn create_load_future(
    &self,
    name: &str,
    registry_url: &Url,
    registry_config: &RegistryConfig,
    current_runtime_http_client: &Arc<HttpClient>,
  ) -> Shared<PendingRegistryLoadFuture> {
    let package_url = self.get_package_url(name, registry_url);
    let maybe_auth_header = maybe_auth_header_for_npm_registry(registry_config);
    let guard = self.progress_bar.update(package_url.as_str());
    let cache = self.cache.clone();
    let http_client = current_runtime_http_client.clone();
    let name = name.to_string();
    // force this future to be polled on the current runtime because it's not
    // safe to share `HttpClient`s across runtimes and because a restart of
    // npm resolution might cause this package not to be resolved again
    // causing the future to never be polled
    deno_core::unsync::spawn(async move {
      let maybe_bytes = http_client
        .download_with_progress(package_url, maybe_auth_header, &guard)
        .await?;
      match maybe_bytes {
        Some(bytes) => {
          let future_result = deno_core::unsync::spawn_blocking(
            move || -> Result<FutureResult, AnyError> {
              let package_info = serde_json::from_slice(&bytes)?;
              match cache.save_package_info(&name, &package_info) {
                Ok(()) => {
                  Ok(FutureResult::SavedFsCache(Arc::new(package_info)))
                }
                Err(err) => {
                  log::debug!(
                    "Error saving package {} to cache: {:#}",
                    name,
                    err
                  );
                  Ok(FutureResult::ErroredFsCache(Arc::new(package_info)))
                }
              }
            },
          )
          .await??;
          Ok(future_result)
        }
        None => Ok(FutureResult::PackageNotExists),
      }
    })
    .map(|result| result.unwrap().map_err(Arc::new))
    .boxed()
    .shared()
  }

  fn get_package_url(&self, name: &str, registry_url: &Url) -> Url {
    // list of all characters used in npm packages:
    //  !, ', (, ), *, -, ., /, [0-9], @, [A-Za-z], _, ~
    const ASCII_SET: percent_encoding::AsciiSet =
      percent_encoding::NON_ALPHANUMERIC
        .remove(b'!')
        .remove(b'\'')
        .remove(b'(')
        .remove(b')')
        .remove(b'*')
        .remove(b'-')
        .remove(b'.')
        .remove(b'/')
        .remove(b'@')
        .remove(b'_')
        .remove(b'~');
    let name = percent_encoding::utf8_percent_encode(name, &ASCII_SET);
    registry_url.join(&name.to_string()).unwrap()
  }
}