Skip to content

Commit

Permalink
use Url instead of String for ModuleSpecifier
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jun 12, 2019
1 parent 164f06d commit e5e540b
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use deno::JSError;
use deno::StartupData;
use futures::Async;
use futures::Future;
use std::fmt;
use std::sync::Arc;
use std::sync::Mutex;
use url::Url;
Expand Down Expand Up @@ -147,21 +148,40 @@ pub fn resolve_module_spec(
#[derive(Debug, PartialEq)]
#[allow(dead_code)]
/// Resolved module specifier
pub struct ModuleSpecifier(String);
pub struct ModuleSpecifier(Url);

impl ModuleSpecifier {
// TODO(bartlomieju): refactor
pub fn to_string(&self) -> String {
String::from(self.0.clone())
}

/// Resolves module using this algorithm:
/// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
pub fn resolve(
specifier: &str,
base: &str,
) -> Result<ModuleSpecifier, url::ParseError> {
Ok(ModuleSpecifier(resolve_module_spec(specifier, base)?))
// 1. Apply the URL parser to specifier. If the result is not failure, return
// the result.
// let specifier = parse_local_or_remote(specifier)?.to_string();
if let Ok(url) = Url::parse(specifier) {
return Ok(ModuleSpecifier(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.
if !specifier.starts_with('/')
&& !specifier.starts_with("./")
&& !specifier.starts_with("../")
{
// TODO This is (probably) not the correct error to return here.
// TODO: This error is very not-user-friendly
return Err(url::ParseError::RelativeUrlWithCannotBeABaseBase);
}

// 3. Return the result of applying the URL parser to specifier with base URL
// as the base URL.
let base_url = Url::parse(base)?;
let u = base_url.join(&specifier)?;
Ok(ModuleSpecifier(u))
}

/// Takes a string representing a path or URL to a module, but of the type
Expand All @@ -174,16 +194,21 @@ impl ModuleSpecifier {
root_specifier: &str,
) -> Result<ModuleSpecifier, url::ParseError> {
if let Ok(url) = Url::parse(root_specifier) {
Ok(ModuleSpecifier(url.to_string()))
Ok(ModuleSpecifier(url))
} else {
let cwd = std::env::current_dir().unwrap();
let base = Url::from_directory_path(cwd).unwrap();
let url = base.join(root_specifier)?;
Ok(ModuleSpecifier(url.to_string()))
Ok(ModuleSpecifier(url))
}
}
}

impl fmt::Display for ModuleSpecifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
/// Takes a string representing a path or URL to a module, but of the type
/// passed through the command-line interface for the main module. This is
/// slightly different than specifiers used in import statements: "foo.js" for
Expand Down

0 comments on commit e5e540b

Please sign in to comment.