summaryrefslogtreecommitdiff
path: root/cli/npm/cache.rs
blob: f3436f7c090300b7cb8ea6d8bed1122375562ee7 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

use std::borrow::Cow;
use std::fs;
use std::path::PathBuf;

use deno_ast::ModuleSpecifier;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::custom_error;
use deno_core::error::AnyError;
use deno_core::url::Url;
use deno_runtime::colors;
use deno_runtime::deno_fetch::reqwest;

use crate::deno_dir::DenoDir;
use crate::file_fetcher::CacheSetting;
use crate::fs_util;

use super::tarball::verify_and_extract_tarball;
use super::NpmPackageId;
use super::NpmPackageVersionDistInfo;

pub const NPM_PACKAGE_SYNC_LOCK_FILENAME: &str = ".deno_sync_lock";

#[derive(Clone, Debug)]
pub struct ReadonlyNpmCache {
  root_dir: PathBuf,
  // cached url representation of the root directory
  root_dir_url: Url,
}

// todo(dsherret): implementing Default for this is error prone because someone
// might accidentally use the default implementation instead of getting the
// correct location of the deno dir, which might be provided via a CLI argument.
// That said, the rest of the LSP code does this at the moment and so this code
// copies that.
impl Default for ReadonlyNpmCache {
  fn default() -> Self {
    // This only gets used when creating the tsc runtime and for testing, and so
    // it shouldn't ever actually access the DenoDir, so it doesn't support a
    // custom root.
    Self::from_deno_dir(&crate::deno_dir::DenoDir::new(None).unwrap()).unwrap()
  }
}

impl ReadonlyNpmCache {
  pub fn new(root_dir: PathBuf) -> Result<Self, AnyError> {
    std::fs::create_dir_all(&root_dir)
      .with_context(|| format!("Error creating {}", root_dir.display()))?;
    let root_dir = crate::fs_util::canonicalize_path(&root_dir)?;
    let root_dir_url = Url::from_directory_path(&root_dir).unwrap();
    Ok(Self {
      root_dir,
      root_dir_url,
    })
  }

  pub fn from_deno_dir(dir: &DenoDir) -> Result<Self, AnyError> {
    Self::new(dir.root.join("npm"))
  }

  pub fn package_folder(
    &self,
    id: &NpmPackageId,
    registry_url: &Url,
  ) -> PathBuf {
    self
      .package_name_folder(&id.name, registry_url)
      .join(id.version.to_string())
  }

  pub fn package_name_folder(&self, name: &str, registry_url: &Url) -> PathBuf {
    let mut dir = self.registry_folder(registry_url);
    let mut parts = name.split('/').map(Cow::Borrowed).collect::<Vec<_>>();
    // package names were not always enforced to be lowercase and so we need
    // to ensure package names, which are therefore case sensitive, are stored
    // on a case insensitive file system to not have conflicts. We do this by
    // first putting it in a "_" folder then hashing the package name.
    if name.to_lowercase() != name {
      let last_part = parts.last_mut().unwrap();
      *last_part = Cow::Owned(crate::checksum::gen(&[last_part.as_bytes()]));
      // We can't just use the hash as part of the directory because it may
      // have a collision with an actual package name in case someone wanted
      // to name an actual package that. To get around this, put all these
      // in a folder called "_" since npm packages can't start with an underscore
      // and there is no package currently called just "_".
      dir = dir.join("_");
    }
    // ensure backslashes are used on windows
    for part in parts {
      dir = dir.join(&*part);
    }
    dir
  }

  pub fn registry_folder(&self, registry_url: &Url) -> PathBuf {
    self
      .root_dir
      .join(fs_util::root_url_to_safe_local_dirname(registry_url))
  }

  pub fn resolve_package_id_from_specifier(
    &self,
    specifier: &ModuleSpecifier,
    registry_url: &Url,
  ) -> Result<NpmPackageId, AnyError> {
    match self.maybe_resolve_package_id_from_specifier(specifier, registry_url)
    {
      Some(id) => Ok(id),
      None => bail!("could not find npm package for '{}'", specifier),
    }
  }

  fn maybe_resolve_package_id_from_specifier(
    &self,
    specifier: &ModuleSpecifier,
    registry_url: &Url,
  ) -> Option<NpmPackageId> {
    let registry_root_dir = self
      .root_dir_url
      .join(&format!(
        "{}/",
        fs_util::root_url_to_safe_local_dirname(registry_url)
          .to_string_lossy()
          .replace('\\', "/")
      ))
      // this not succeeding indicates a fatal issue, so unwrap
      .unwrap();
    let relative_url = registry_root_dir.make_relative(specifier)?;
    if relative_url.starts_with("../") {
      return None;
    }

    // examples:
    // * chalk/5.0.1/
    // * @types/chalk/5.0.1/
    let is_scoped_package = relative_url.starts_with('@');
    let mut parts = relative_url
      .split('/')
      .enumerate()
      .take(if is_scoped_package { 3 } else { 2 })
      .map(|(_, part)| part)
      .collect::<Vec<_>>();
    let version = parts.pop().unwrap();
    let name = parts.join("/");

    Some(NpmPackageId {
      name,
      version: semver::Version::parse(version).unwrap(),
    })
  }
}

/// Stores a single copy of npm packages in a cache.
#[derive(Clone, Debug)]
pub struct NpmCache {
  readonly: ReadonlyNpmCache,
  cache_setting: CacheSetting,
}

impl NpmCache {
  pub fn from_deno_dir(
    dir: &DenoDir,
    cache_setting: CacheSetting,
  ) -> Result<Self, AnyError> {
    Ok(Self {
      readonly: ReadonlyNpmCache::from_deno_dir(dir)?,
      cache_setting,
    })
  }

  pub fn as_readonly(&self) -> ReadonlyNpmCache {
    self.readonly.clone()
  }

  pub async fn ensure_package(
    &self,
    id: &NpmPackageId,
    dist: &NpmPackageVersionDistInfo,
    registry_url: &Url,
  ) -> Result<(), AnyError> {
    let package_folder = self.readonly.package_folder(id, registry_url);
    if package_folder.exists()
      // if this file exists, then the package didn't successfully extract
      // the first time, or another process is currently extracting the zip file
      && !package_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME).exists()
    {
      return Ok(());
    } else if self.cache_setting == CacheSetting::Only {
      return Err(custom_error(
        "NotCached",
        format!(
          "An npm specifier not found in cache: \"{}\", --cached-only is specified.",
          id.name
        )
      )
      );
    }

    log::log!(
      log::Level::Info,
      "{} {}",
      colors::green("Download"),
      dist.tarball,
    );

    let response = reqwest::get(&dist.tarball).await?;

    if response.status() == 404 {
      bail!("Could not find npm package tarball at: {}", dist.tarball);
    } else if !response.status().is_success() {
      let status = response.status();
      let maybe_response_text = response.text().await.ok();
      bail!(
        "Bad response: {:?}{}",
        status,
        match maybe_response_text {
          Some(text) => format!("\n\n{}", text),
          None => String::new(),
        }
      );
    } else {
      let bytes = response.bytes().await?;

      match verify_and_extract_tarball(id, &bytes, dist, &package_folder) {
        Ok(()) => Ok(()),
        Err(err) => {
          if let Err(remove_err) = fs::remove_dir_all(&package_folder) {
            if remove_err.kind() != std::io::ErrorKind::NotFound {
              bail!(
                concat!(
                  "Failed verifying and extracting npm tarball for {}, then ",
                  "failed cleaning up package cache folder.\n\nOriginal ",
                  "error:\n\n{}\n\nRemove error:\n\n{}\n\nPlease manually ",
                  "delete this folder or you will run into issues using this ",
                  "package in the future:\n\n{}"
                ),
                id,
                err,
                remove_err,
                package_folder.display(),
              );
            }
          }
          Err(err)
        }
      }
    }
  }

  pub fn package_folder(
    &self,
    id: &NpmPackageId,
    registry_url: &Url,
  ) -> PathBuf {
    self.readonly.package_folder(id, registry_url)
  }

  pub fn package_name_folder(&self, name: &str, registry_url: &Url) -> PathBuf {
    self.readonly.package_name_folder(name, registry_url)
  }

  pub fn registry_folder(&self, registry_url: &Url) -> PathBuf {
    self.readonly.registry_folder(registry_url)
  }

  pub fn resolve_package_id_from_specifier(
    &self,
    specifier: &ModuleSpecifier,
    registry_url: &Url,
  ) -> Result<NpmPackageId, AnyError> {
    self
      .readonly
      .resolve_package_id_from_specifier(specifier, registry_url)
  }
}

#[cfg(test)]
mod test {
  use deno_core::url::Url;

  use super::ReadonlyNpmCache;
  use crate::npm::NpmPackageId;

  #[test]
  fn should_get_lowercase_package_folder() {
    let root_dir = crate::deno_dir::DenoDir::new(None).unwrap().root;
    let cache = ReadonlyNpmCache::new(root_dir.clone()).unwrap();
    let registry_url = Url::parse("https://registry.npmjs.org/").unwrap();

    // all lowercase should be as-is
    assert_eq!(
      cache.package_folder(
        &NpmPackageId {
          name: "json".to_string(),
          version: semver::Version::parse("1.2.5").unwrap(),
        },
        &registry_url,
      ),
      root_dir
        .join("registry.npmjs.org")
        .join("json")
        .join("1.2.5"),
    );
  }

  #[test]
  fn should_handle_non_all_lowercase_package_names() {
    // it was possible at one point for npm packages to not just be lowercase
    let root_dir = crate::deno_dir::DenoDir::new(None).unwrap().root;
    let cache = ReadonlyNpmCache::new(root_dir.clone()).unwrap();
    let registry_url = Url::parse("https://registry.npmjs.org/").unwrap();
    let json_uppercase_hash =
      "db1a21a0bc2ef8fbe13ac4cf044e8c9116d29137d5ed8b916ab63dcb2d4290df";
    assert_eq!(
      cache.package_folder(
        &NpmPackageId {
          name: "JSON".to_string(),
          version: semver::Version::parse("1.2.5").unwrap(),
        },
        &registry_url,
      ),
      root_dir
        .join("registry.npmjs.org")
        .join("_")
        .join(json_uppercase_hash)
        .join("1.2.5"),
    );
    assert_eq!(
      cache.package_folder(
        &NpmPackageId {
          name: "@types/JSON".to_string(),
          version: semver::Version::parse("1.2.5").unwrap(),
        },
        &registry_url,
      ),
      root_dir
        .join("registry.npmjs.org")
        .join("_")
        .join("@types")
        .join(json_uppercase_hash)
        .join("1.2.5"),
    );
  }
}