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
// Copyright 2018-2024 the Deno authors. MIT license.

use std::error::Error;
use std::fmt;
use std::path::PathBuf;
use url::ParseError;

pub type ModuleSpecifier = deno_ast::ModuleSpecifier;

pub const EMPTY_SPECIFIER: &str = "deno://empty";

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpecifierError {
  InvalidUrl(ParseError),
  ImportPrefixMissing(String, Option<ModuleSpecifier>),
}

use SpecifierError::*;

impl Error for SpecifierError {
  fn source(&self) -> Option<&(dyn Error + 'static)> {
    match self {
      InvalidUrl(ref err) => Some(err),
      _ => None,
    }
  }
}

impl fmt::Display for SpecifierError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      InvalidUrl(ref err) => write!(f, "invalid URL: {err}"),
      ImportPrefixMissing(ref specifier, _) => write!(
        f,
        "Relative import path \"{specifier}\" not prefixed with / or ./ or ../",
      ),
    }
  }
}

#[cfg(not(target_arch = "wasm32"))]
fn specifier_from_path(path: PathBuf) -> ModuleSpecifier {
  ModuleSpecifier::from_file_path(path).unwrap()
}

#[cfg(target_arch = "wasm32")]
fn specifier_from_path(_path: PathBuf) -> ModuleSpecifier {
  ModuleSpecifier::parse(EMPTY_SPECIFIER).unwrap()
}

/// Given a specifier string and a referring module specifier, try to resolve
/// the target module specifier, erroring if it cannot be resolved.
pub fn resolve_import(
  specifier: &str,
  referrer: &ModuleSpecifier,
) -> Result<ModuleSpecifier, SpecifierError> {
  let url = match ModuleSpecifier::parse(specifier) {
    // 1. Apply the URL parser to specifier.
    //    If the result is not failure, return he result.
    Ok(url) => url,

    // 2. If specifier does not start with the character U+002F SOLIDUS (/),
    //    the two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./),
    //    or the three-character sequence U+002E FULL STOP, U+002E FULL STOP,
    //    U+002F SOLIDUS (../), return failure.
    Err(ParseError::RelativeUrlWithoutBase)
      if !(specifier.starts_with('/')
        || specifier.starts_with("./")
        || specifier.starts_with("../")) =>
    {
      return Err(ImportPrefixMissing(
        specifier.to_string(),
        Some(referrer.clone()),
      ));
    }

    // 3. Return the result of applying the URL parser to specifier with base
    //    URL as the base URL.
    Err(ParseError::RelativeUrlWithoutBase) => {
      let referrer = if referrer.as_str() == EMPTY_SPECIFIER {
        // Handle <unknown> case, happening under e.g. repl.
        // Use CWD for such case.

        // Forcefully join base to current dir.
        // Otherwise, later joining in Url would be interpreted in
        // the parent directory (appending trailing slash does not work)
        let path = std::env::current_dir().unwrap().join("empty");
        specifier_from_path(path)
      } else {
        referrer.clone()
      };
      referrer.join(specifier).map_err(InvalidUrl)?
    }

    // If parsing the specifier as a URL failed for a different reason than
    // it being relative, always return the original error. We don't want to
    // return `ImportPrefixMissing` or `InvalidBaseUrl` if the real
    // problem lies somewhere else.
    Err(err) => return Err(InvalidUrl(err)),
  };

  Ok(url)
}

pub fn is_fs_root_specifier(url: &ModuleSpecifier) -> bool {
  if url.scheme() != "file" {
    return false;
  }

  let path = url.path();
  let path = path.trim_start_matches('/').trim_end_matches('/');
  let mut parts = path.split('/');
  let Some(first_part) = parts.next() else {
    return true;
  };
  if parts.next().is_some() {
    return false;
  }
  let mut first_part_chars = first_part.chars();
  let Some(first_char) = first_part_chars.next() else {
    return true;
  };
  let Some(second_char) = first_part_chars.next() else {
    return false;
  };

  // Windows path: file:///C:/example
  first_part_chars.next().is_none()
    && first_char.is_ascii_alphabetic()
    && second_char == ':'
}

#[cfg(test)]
mod test {
  use crate::ModuleSpecifier;

  use super::*;

  #[test]
  fn test_is_fs_root_specifier() {
    let cases = [
      ("https://deno.land", false),
      ("file:///", true),
      ("file://", true),
      ("file:///C:/", true),
      ("file:///V:/", true),
      ("file:///V:/test/", false),
    ];
    for (specifier, expected) in cases {
      let url = ModuleSpecifier::parse(specifier).unwrap();
      assert_eq!(is_fs_root_specifier(&url), expected, "{:?}", specifier);
    }
  }
}