2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-02-18 10:42:15 -05:00
|
|
|
use crate::compiler::ModuleMetaData;
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::errors;
|
|
|
|
use crate::errors::DenoError;
|
|
|
|
use crate::errors::DenoResult;
|
|
|
|
use crate::errors::ErrorKind;
|
|
|
|
use crate::fs as deno_fs;
|
|
|
|
use crate::http_util;
|
|
|
|
use crate::js_errors::SourceMapGetter;
|
|
|
|
use crate::msg;
|
2019-03-19 16:45:39 -04:00
|
|
|
use crate::tokio_util;
|
2019-02-12 15:20:54 -05:00
|
|
|
use crate::version;
|
2019-01-14 01:30:38 -05:00
|
|
|
use dirs;
|
2019-04-01 21:46:40 -04:00
|
|
|
use futures::future::{loop_fn, Either, Loop};
|
2019-03-19 16:45:39 -04:00
|
|
|
use futures::Future;
|
2019-04-01 21:46:40 -04:00
|
|
|
use http;
|
2018-08-28 13:49:19 -04:00
|
|
|
use ring;
|
2019-04-01 21:46:40 -04:00
|
|
|
use serde_json;
|
2018-07-26 17:54:22 -04:00
|
|
|
use std;
|
2019-01-03 22:11:01 -05:00
|
|
|
use std::fmt::Write;
|
2018-08-26 05:51:25 -04:00
|
|
|
use std::fs;
|
2018-07-26 17:54:22 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::result::Result;
|
2019-02-18 10:42:15 -05:00
|
|
|
use std::str;
|
2018-08-28 13:49:19 -04:00
|
|
|
use url;
|
|
|
|
use url::Url;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
#[derive(Clone)]
|
2018-07-26 17:54:22 -04:00
|
|
|
pub struct DenoDir {
|
|
|
|
// Example: /Users/rld/.deno/
|
|
|
|
pub root: PathBuf,
|
|
|
|
// In the Go code this was called SrcDir.
|
|
|
|
// This is where we cache http resources. Example:
|
|
|
|
// /Users/rld/.deno/deps/github.com/ry/blah.js
|
|
|
|
pub gen: PathBuf,
|
|
|
|
// In the Go code this was called CacheDir.
|
|
|
|
// This is where we cache compilation outputs. Example:
|
|
|
|
// /Users/rld/.deno/gen/f39a473452321cacd7c346a870efb0e3e1264b43.js
|
|
|
|
pub deps: PathBuf,
|
2018-10-26 09:55:05 -04:00
|
|
|
// This splits to http and https deps
|
|
|
|
pub deps_http: PathBuf,
|
|
|
|
pub deps_https: PathBuf,
|
2019-04-29 10:58:31 -04:00
|
|
|
/// The active configuration file contents (or empty array) which applies to
|
|
|
|
/// source code cached by `DenoDir`.
|
|
|
|
pub config: Vec<u8>,
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DenoDir {
|
|
|
|
// Must be called before using any function from this module.
|
2018-08-02 19:13:02 -04:00
|
|
|
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L99-L111
|
2019-04-29 10:58:31 -04:00
|
|
|
pub fn new(
|
|
|
|
custom_root: Option<PathBuf>,
|
|
|
|
state_config: &Option<Vec<u8>>,
|
|
|
|
) -> std::io::Result<Self> {
|
2018-07-26 17:54:22 -04:00
|
|
|
// Only setup once.
|
2018-09-14 02:04:02 -04:00
|
|
|
let home_dir = dirs::home_dir().expect("Could not get home directory.");
|
2019-02-13 08:57:00 -05:00
|
|
|
let fallback = home_dir.join(".deno");
|
|
|
|
// We use the OS cache dir because all files deno writes are cache files
|
|
|
|
// Once that changes we need to start using different roots if DENO_DIR
|
|
|
|
// is not set, and keep a single one if it is.
|
|
|
|
let default = dirs::cache_dir()
|
|
|
|
.map(|d| d.join("deno"))
|
|
|
|
.unwrap_or(fallback);
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
let root: PathBuf = custom_root.unwrap_or(default);
|
2018-07-26 17:54:22 -04:00
|
|
|
let gen = root.as_path().join("gen");
|
|
|
|
let deps = root.as_path().join("deps");
|
2018-10-26 09:55:05 -04:00
|
|
|
let deps_http = deps.join("http");
|
|
|
|
let deps_https = deps.join("https");
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
// Internally within DenoDir, we use the config as part of the hash to
|
|
|
|
// determine if a file has been transpiled with the same configuration, but
|
|
|
|
// we have borrowed the `State` configuration, which we want to either clone
|
|
|
|
// or create an empty `Vec` which we will use in our hash function.
|
|
|
|
let config = match state_config {
|
|
|
|
Some(config) => config.clone(),
|
|
|
|
_ => b"".to_vec(),
|
|
|
|
};
|
|
|
|
|
2018-11-05 01:21:21 -05:00
|
|
|
let deno_dir = Self {
|
2018-08-14 16:50:53 -04:00
|
|
|
root,
|
|
|
|
gen,
|
|
|
|
deps,
|
2018-10-26 09:55:05 -04:00
|
|
|
deps_http,
|
|
|
|
deps_https,
|
2019-04-29 10:58:31 -04:00
|
|
|
config,
|
2018-08-14 16:50:53 -04:00
|
|
|
};
|
2019-01-17 23:39:06 -05:00
|
|
|
|
|
|
|
// TODO Lazily create these directories.
|
|
|
|
deno_fs::mkdir(deno_dir.gen.as_ref(), 0o755, true)?;
|
|
|
|
deno_fs::mkdir(deno_dir.deps.as_ref(), 0o755, true)?;
|
|
|
|
deno_fs::mkdir(deno_dir.deps_http.as_ref(), 0o755, true)?;
|
|
|
|
deno_fs::mkdir(deno_dir.deps_https.as_ref(), 0o755, true)?;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
|
|
|
debug!("root {}", deno_dir.root.display());
|
|
|
|
debug!("gen {}", deno_dir.gen.display());
|
|
|
|
debug!("deps {}", deno_dir.deps.display());
|
2018-10-26 09:55:05 -04:00
|
|
|
debug!("deps_http {}", deno_dir.deps_http.display());
|
|
|
|
debug!("deps_https {}", deno_dir.deps_https.display());
|
2018-07-26 17:54:22 -04:00
|
|
|
|
|
|
|
Ok(deno_dir)
|
|
|
|
}
|
|
|
|
|
2018-08-02 19:13:02 -04:00
|
|
|
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L32-L35
|
2018-07-26 17:54:22 -04:00
|
|
|
pub fn cache_path(
|
2018-11-05 01:21:21 -05:00
|
|
|
self: &Self,
|
2018-07-26 17:54:22 -04:00
|
|
|
filename: &str,
|
2019-02-18 10:42:15 -05:00
|
|
|
source_code: &[u8],
|
2018-10-28 19:41:10 -04:00
|
|
|
) -> (PathBuf, PathBuf) {
|
2019-04-29 10:58:31 -04:00
|
|
|
let cache_key =
|
|
|
|
source_code_hash(filename, source_code, version::DENO, &self.config);
|
2018-10-28 19:41:10 -04:00
|
|
|
(
|
|
|
|
self.gen.join(cache_key.to_string() + ".js"),
|
|
|
|
self.gen.join(cache_key.to_string() + ".js.map"),
|
|
|
|
)
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn code_cache(
|
2018-11-05 01:21:21 -05:00
|
|
|
self: &Self,
|
2019-02-18 10:42:15 -05:00
|
|
|
module_meta_data: &ModuleMetaData,
|
2018-07-26 17:54:22 -04:00
|
|
|
) -> std::io::Result<()> {
|
2019-02-18 10:42:15 -05:00
|
|
|
let (cache_path, source_map_path) = self
|
|
|
|
.cache_path(&module_meta_data.filename, &module_meta_data.source_code);
|
2018-07-26 17:54:22 -04:00
|
|
|
// TODO(ry) This is a race condition w.r.t to exists() -- probably should
|
|
|
|
// create the file in exclusive mode. A worry is what might happen is there
|
|
|
|
// are two processes and one reads the cache file while the other is in the
|
|
|
|
// midst of writing it.
|
2018-10-28 19:41:10 -04:00
|
|
|
if cache_path.exists() && source_map_path.exists() {
|
2018-07-26 17:54:22 -04:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2019-02-18 10:42:15 -05:00
|
|
|
match &module_meta_data.maybe_output_code {
|
|
|
|
Some(output_code) => fs::write(cache_path, output_code),
|
|
|
|
_ => Ok(()),
|
|
|
|
}?;
|
|
|
|
match &module_meta_data.maybe_source_map {
|
|
|
|
Some(source_map) => fs::write(source_map_path, source_map),
|
|
|
|
_ => Ok(()),
|
|
|
|
}?;
|
2018-10-28 19:41:10 -04:00
|
|
|
Ok(())
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
pub fn fetch_module_meta_data_async(
|
2018-11-05 01:21:21 -05:00
|
|
|
self: &Self,
|
2018-12-11 11:23:59 -05:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-03-28 16:05:41 -04:00
|
|
|
use_cache: bool,
|
2019-05-03 11:09:51 -04:00
|
|
|
no_fetch: bool,
|
2019-03-20 11:38:43 -04:00
|
|
|
) -> impl Future<Item = ModuleMetaData, Error = errors::DenoError> {
|
2019-02-18 10:42:15 -05:00
|
|
|
debug!(
|
|
|
|
"fetch_module_meta_data. specifier {} referrer {}",
|
|
|
|
specifier, referrer
|
|
|
|
);
|
2018-09-05 11:19:39 -04:00
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
let specifier = specifier.to_string();
|
|
|
|
let referrer = referrer.to_string();
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
let result = self.resolve_module(&specifier, &referrer);
|
|
|
|
if let Err(err) = result {
|
|
|
|
return Either::A(futures::future::err(DenoError::from(err)));
|
2019-02-18 10:42:15 -05:00
|
|
|
}
|
2019-03-20 11:38:43 -04:00
|
|
|
let (module_name, filename) = result.unwrap();
|
|
|
|
|
|
|
|
let gen = self.gen.clone();
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
// If we don't clone the config, we then end up creating an implied lifetime
|
|
|
|
// which gets returned in the future, so we clone here so as to not leak the
|
|
|
|
// move below when the future is resolving.
|
|
|
|
let config = self.config.clone();
|
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
Either::B(
|
2019-04-01 21:46:40 -04:00
|
|
|
get_source_code_async(
|
|
|
|
self,
|
|
|
|
module_name.as_str(),
|
|
|
|
filename.as_str(),
|
|
|
|
use_cache,
|
2019-05-03 11:09:51 -04:00
|
|
|
no_fetch,
|
2019-04-01 21:46:40 -04:00
|
|
|
).then(move |result| {
|
|
|
|
let mut out = match result {
|
|
|
|
Ok(out) => out,
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() == ErrorKind::NotFound {
|
|
|
|
// For NotFound, change the message to something better.
|
|
|
|
return Err(errors::new(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
format!(
|
|
|
|
"Cannot resolve module \"{}\" from \"{}\"",
|
|
|
|
specifier, referrer
|
|
|
|
),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return Err(err);
|
2019-03-20 11:38:43 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-01 21:46:40 -04:00
|
|
|
};
|
2018-11-15 05:43:19 -05:00
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
if out.source_code.starts_with(b"#!") {
|
|
|
|
out.source_code = filter_shebang(out.source_code);
|
|
|
|
}
|
2019-02-06 23:42:34 -05:00
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
// If TypeScript we have to also load corresponding compile js and
|
|
|
|
// source maps (called output_code and output_source_map)
|
|
|
|
if out.media_type != msg::MediaType::TypeScript || !use_cache {
|
|
|
|
return Ok(out);
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
let cache_key = source_code_hash(
|
|
|
|
&out.filename,
|
|
|
|
&out.source_code,
|
|
|
|
version::DENO,
|
|
|
|
&config,
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let (output_code_filename, output_source_map_filename) = (
|
|
|
|
gen.join(cache_key.to_string() + ".js"),
|
|
|
|
gen.join(cache_key.to_string() + ".js.map"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let result =
|
|
|
|
load_cache2(&output_code_filename, &output_source_map_filename);
|
|
|
|
match result {
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
// If there's no compiled JS or source map, that's ok, just
|
|
|
|
// return what we have.
|
2019-03-28 16:05:41 -04:00
|
|
|
Ok(out)
|
2019-04-01 21:46:40 -04:00
|
|
|
} else {
|
|
|
|
Err(err.into())
|
2019-03-28 16:05:41 -04:00
|
|
|
}
|
2019-03-20 11:38:43 -04:00
|
|
|
}
|
2019-04-01 21:46:40 -04:00
|
|
|
Ok((output_code, source_map)) => {
|
|
|
|
out.maybe_output_code = Some(output_code);
|
|
|
|
out.maybe_source_map = Some(source_map);
|
|
|
|
out.maybe_output_code_filename =
|
|
|
|
Some(output_code_filename.to_str().unwrap().to_string());
|
|
|
|
out.maybe_source_map_filename =
|
|
|
|
Some(output_source_map_filename.to_str().unwrap().to_string());
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
2019-03-20 11:38:43 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Synchronous version of fetch_module_meta_data_async
|
|
|
|
/// This function is deprecated.
|
|
|
|
pub fn fetch_module_meta_data(
|
|
|
|
self: &Self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-03-28 16:05:41 -04:00
|
|
|
use_cache: bool,
|
2019-05-03 11:09:51 -04:00
|
|
|
no_fetch: bool,
|
2019-03-20 11:38:43 -04:00
|
|
|
) -> Result<ModuleMetaData, errors::DenoError> {
|
2019-03-28 16:05:41 -04:00
|
|
|
tokio_util::block_on(
|
2019-05-03 11:09:51 -04:00
|
|
|
self
|
|
|
|
.fetch_module_meta_data_async(specifier, referrer, use_cache, no_fetch),
|
2019-03-28 16:05:41 -04:00
|
|
|
)
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:13:02 -04:00
|
|
|
// Prototype: https://github.com/denoland/deno/blob/golang/os.go#L56-L68
|
2018-11-05 01:21:21 -05:00
|
|
|
fn src_file_to_url(self: &Self, filename: &str) -> String {
|
2018-09-05 11:19:39 -04:00
|
|
|
let filename_path = Path::new(filename);
|
|
|
|
if filename_path.starts_with(&self.deps) {
|
2018-10-26 09:55:05 -04:00
|
|
|
let (rest, prefix) = if filename_path.starts_with(&self.deps_https) {
|
|
|
|
let rest = filename_path.strip_prefix(&self.deps_https).unwrap();
|
|
|
|
let prefix = "https://".to_string();
|
|
|
|
(rest, prefix)
|
|
|
|
} else if filename_path.starts_with(&self.deps_http) {
|
|
|
|
let rest = filename_path.strip_prefix(&self.deps_http).unwrap();
|
|
|
|
let prefix = "http://".to_string();
|
|
|
|
(rest, prefix)
|
|
|
|
} else {
|
|
|
|
// TODO(kevinkassimo): change this to support other protocols than http
|
|
|
|
unimplemented!()
|
|
|
|
};
|
2018-09-05 11:19:39 -04:00
|
|
|
// Windows doesn't support ":" in filenames, so we represent port using a
|
|
|
|
// special string.
|
|
|
|
// TODO(ry) This current implementation will break on a URL that has
|
|
|
|
// the default port but contains "_PORT" in the path.
|
|
|
|
let rest = rest.to_str().unwrap().replacen("_PORT", ":", 1);
|
2018-10-26 09:55:05 -04:00
|
|
|
prefix + &rest
|
2018-07-26 17:54:22 -04:00
|
|
|
} else {
|
2018-09-05 11:19:39 -04:00
|
|
|
String::from(filename)
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 17:21:31 -05:00
|
|
|
/// Returns (module name, local filename)
|
2019-02-12 21:14:02 -05:00
|
|
|
pub fn resolve_module_url(
|
2018-11-05 01:21:21 -05:00
|
|
|
self: &Self,
|
2018-12-11 11:23:59 -05:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-02-12 21:14:02 -05:00
|
|
|
) -> Result<Url, url::ParseError> {
|
2018-12-11 11:23:59 -05:00
|
|
|
let specifier = self.src_file_to_url(specifier);
|
2019-01-03 12:51:56 -05:00
|
|
|
let mut referrer = self.src_file_to_url(referrer);
|
2018-09-05 11:19:39 -04:00
|
|
|
|
2018-07-26 17:54:22 -04:00
|
|
|
debug!(
|
2018-12-11 11:23:59 -05:00
|
|
|
"resolve_module specifier {} referrer {}",
|
|
|
|
specifier, referrer
|
2018-07-26 17:54:22 -04:00
|
|
|
);
|
|
|
|
|
2019-01-15 07:06:25 -05:00
|
|
|
if referrer.starts_with('.') {
|
2019-01-03 12:51:56 -05:00
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
let referrer_path = cwd.join(referrer);
|
|
|
|
referrer = referrer_path.to_str().unwrap().to_string() + "/";
|
|
|
|
}
|
|
|
|
|
2019-04-13 13:27:27 -04:00
|
|
|
let j = if is_remote(&specifier)
|
|
|
|
|| (Path::new(&specifier).is_absolute() && !is_remote(&referrer))
|
|
|
|
{
|
2018-12-11 11:23:59 -05:00
|
|
|
parse_local_or_remote(&specifier)?
|
|
|
|
} else if referrer.ends_with('/') {
|
|
|
|
let r = Url::from_directory_path(&referrer);
|
2018-09-05 11:19:39 -04:00
|
|
|
// TODO(ry) Properly handle error.
|
|
|
|
if r.is_err() {
|
2018-12-11 11:23:59 -05:00
|
|
|
error!("Url::from_directory_path error {}", referrer);
|
2018-09-05 11:19:39 -04:00
|
|
|
}
|
|
|
|
let base = r.unwrap();
|
2018-12-11 11:23:59 -05:00
|
|
|
base.join(specifier.as_ref())?
|
2018-09-05 11:19:39 -04:00
|
|
|
} else {
|
2018-12-11 11:23:59 -05:00
|
|
|
let base = parse_local_or_remote(&referrer)?;
|
|
|
|
base.join(specifier.as_ref())?
|
2018-09-05 11:19:39 -04:00
|
|
|
};
|
2019-02-12 21:14:02 -05:00
|
|
|
Ok(j)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns (module name, local filename)
|
|
|
|
pub fn resolve_module(
|
|
|
|
self: &Self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
) -> Result<(String, String), url::ParseError> {
|
|
|
|
let j = self.resolve_module_url(specifier, referrer)?;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2019-02-12 21:14:02 -05:00
|
|
|
let module_name = j.to_string();
|
|
|
|
let filename;
|
2018-08-14 16:50:53 -04:00
|
|
|
match j.scheme() {
|
|
|
|
"file" => {
|
2019-02-12 21:14:02 -05:00
|
|
|
filename = deno_fs::normalize_path(j.to_file_path().unwrap().as_ref());
|
2018-08-14 16:50:53 -04:00
|
|
|
}
|
2018-10-26 09:55:05 -04:00
|
|
|
"https" => {
|
|
|
|
filename = deno_fs::normalize_path(
|
2018-11-04 09:04:24 -05:00
|
|
|
get_cache_filename(self.deps_https.as_path(), &j).as_ref(),
|
2018-10-26 09:55:05 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
"http" => {
|
2018-08-26 05:51:25 -04:00
|
|
|
filename = deno_fs::normalize_path(
|
2018-11-04 09:04:24 -05:00
|
|
|
get_cache_filename(self.deps_http.as_path(), &j).as_ref(),
|
2018-08-23 03:58:59 -04:00
|
|
|
)
|
2018-08-14 16:50:53 -04:00
|
|
|
}
|
2019-04-01 21:46:40 -04:00
|
|
|
// TODO(kevinkassimo): change this to support other protocols than http.
|
2018-10-26 09:55:05 -04:00
|
|
|
_ => unimplemented!(),
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
|
2018-08-14 16:50:53 -04:00
|
|
|
debug!("module_name: {}, filename: {}", module_name, filename);
|
2018-07-26 17:54:22 -04:00
|
|
|
Ok((module_name, filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
impl SourceMapGetter for DenoDir {
|
2019-02-18 10:42:15 -05:00
|
|
|
fn get_source_map(&self, script_name: &str) -> Option<Vec<u8>> {
|
2019-05-03 11:09:51 -04:00
|
|
|
match self.fetch_module_meta_data(script_name, ".", true, true) {
|
2019-01-15 07:06:25 -05:00
|
|
|
Err(_e) => None,
|
2018-12-06 23:05:36 -05:00
|
|
|
Ok(out) => match out.maybe_source_map {
|
2019-01-15 07:06:25 -05:00
|
|
|
None => None,
|
|
|
|
Some(source_map) => Some(source_map),
|
2018-12-06 23:05:36 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:05:41 -04:00
|
|
|
/// This fetches source code, locally or remotely.
|
|
|
|
/// module_name is the URL specifying the module.
|
|
|
|
/// filename is the local path to the module (if remote, it is in the cache
|
|
|
|
/// folder, and potentially does not exist yet)
|
|
|
|
///
|
|
|
|
/// It *does not* fill the compiled JS nor source map portions of
|
|
|
|
/// ModuleMetaData. This is the only difference between this function and
|
|
|
|
/// fetch_module_meta_data_async(). TODO(ry) change return type to reflect this
|
|
|
|
/// fact.
|
|
|
|
///
|
|
|
|
/// If this is a remote module, and it has not yet been cached, the resulting
|
|
|
|
/// download will be written to "filename". This happens no matter the value of
|
|
|
|
/// use_cache.
|
|
|
|
fn get_source_code_async(
|
2019-04-01 21:46:40 -04:00
|
|
|
deno_dir: &DenoDir,
|
2019-03-28 16:05:41 -04:00
|
|
|
module_name: &str,
|
|
|
|
filename: &str,
|
|
|
|
use_cache: bool,
|
2019-05-03 11:09:51 -04:00
|
|
|
no_fetch: bool,
|
2019-03-28 16:05:41 -04:00
|
|
|
) -> impl Future<Item = ModuleMetaData, Error = DenoError> {
|
|
|
|
let filename = filename.to_string();
|
|
|
|
let module_name = module_name.to_string();
|
|
|
|
let is_module_remote = is_remote(&module_name);
|
2019-05-03 11:09:51 -04:00
|
|
|
// We try fetch local. Three cases:
|
|
|
|
// 1. Remote downloads are not allowed, we're only allowed to use cache.
|
|
|
|
// 2. This is a remote module and we're allowed to use cached downloads.
|
|
|
|
// 3. This is a local module.
|
|
|
|
if !is_module_remote || use_cache || no_fetch {
|
2019-03-28 16:05:41 -04:00
|
|
|
debug!(
|
|
|
|
"fetch local or reload {} is_module_remote {}",
|
|
|
|
module_name, is_module_remote
|
|
|
|
);
|
|
|
|
// Note that local fetch is done synchronously.
|
2019-04-01 21:46:40 -04:00
|
|
|
match fetch_local_source(deno_dir, &module_name, &filename, None) {
|
2019-03-28 16:05:41 -04:00
|
|
|
Ok(Some(output)) => {
|
|
|
|
debug!("found local source ");
|
|
|
|
return Either::A(futures::future::ok(output));
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
debug!("fetch_local_source returned None");
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
return Either::A(futures::future::err(err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 11:09:51 -04:00
|
|
|
// If not remote file stop here!
|
2019-03-28 16:05:41 -04:00
|
|
|
if !is_module_remote {
|
|
|
|
debug!("not remote file stop here");
|
|
|
|
return Either::A(futures::future::err(DenoError::from(
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::NotFound,
|
|
|
|
format!("cannot find local file '{}'", &filename),
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2019-05-03 11:09:51 -04:00
|
|
|
// If remote downloads are not allowed stop here!
|
|
|
|
if no_fetch {
|
|
|
|
debug!("remote file with no_fetch stop here");
|
|
|
|
return Either::A(futures::future::err(DenoError::from(
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::NotFound,
|
|
|
|
format!("cannot find remote file '{}' in cache", &filename),
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:05:41 -04:00
|
|
|
debug!("is remote but didn't find module");
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
// not cached/local, try remote.
|
|
|
|
Either::B(
|
|
|
|
fetch_remote_source_async(deno_dir, &module_name, &filename).and_then(
|
|
|
|
move |maybe_remote_source| match maybe_remote_source {
|
|
|
|
Some(output) => Ok(output),
|
|
|
|
None => Err(DenoError::from(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::NotFound,
|
|
|
|
format!("cannot find remote file '{}'", &filename),
|
|
|
|
))),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
2019-03-28 16:05:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
/// Synchronous version of get_source_code_async
|
|
|
|
/// This function is deprecated.
|
|
|
|
fn get_source_code(
|
2019-04-01 21:46:40 -04:00
|
|
|
deno_dir: &DenoDir,
|
2019-03-28 16:05:41 -04:00
|
|
|
module_name: &str,
|
|
|
|
filename: &str,
|
|
|
|
use_cache: bool,
|
2019-05-03 11:09:51 -04:00
|
|
|
no_fetch: bool,
|
2019-03-28 16:05:41 -04:00
|
|
|
) -> DenoResult<ModuleMetaData> {
|
2019-04-01 21:46:40 -04:00
|
|
|
tokio_util::block_on(get_source_code_async(
|
|
|
|
deno_dir,
|
|
|
|
module_name,
|
|
|
|
filename,
|
|
|
|
use_cache,
|
2019-05-03 11:09:51 -04:00
|
|
|
no_fetch,
|
2019-04-01 21:46:40 -04:00
|
|
|
))
|
2019-03-28 16:05:41 -04:00
|
|
|
}
|
|
|
|
|
2018-11-04 09:04:24 -05:00
|
|
|
fn get_cache_filename(basedir: &Path, url: &Url) -> PathBuf {
|
2018-09-05 11:19:39 -04:00
|
|
|
let host = url.host_str().unwrap();
|
|
|
|
let host_port = match url.port() {
|
|
|
|
// Windows doesn't support ":" in filenames, so we represent port using a
|
|
|
|
// special string.
|
|
|
|
Some(port) => format!("{}_PORT{}", host, port),
|
|
|
|
None => host.to_string(),
|
|
|
|
};
|
|
|
|
|
2018-08-14 16:50:53 -04:00
|
|
|
let mut out = basedir.to_path_buf();
|
2018-09-05 11:19:39 -04:00
|
|
|
out.push(host_port);
|
2018-08-14 16:50:53 -04:00
|
|
|
for path_seg in url.path_segments().unwrap() {
|
|
|
|
out.push(path_seg);
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
fn load_cache2(
|
|
|
|
js_filename: &PathBuf,
|
|
|
|
map_filename: &PathBuf,
|
|
|
|
) -> Result<(Vec<u8>, Vec<u8>), std::io::Error> {
|
|
|
|
debug!(
|
|
|
|
"load_cache code: {} map: {}",
|
|
|
|
js_filename.display(),
|
|
|
|
map_filename.display()
|
|
|
|
);
|
|
|
|
let read_output_code = fs::read(&js_filename)?;
|
|
|
|
let read_source_map = fs::read(&map_filename)?;
|
|
|
|
Ok((read_output_code, read_source_map))
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
/// Generate an SHA1 hash for source code, to be used to determine if a cached
|
|
|
|
/// version of the code is valid or invalid.
|
2019-02-12 15:20:54 -05:00
|
|
|
fn source_code_hash(
|
|
|
|
filename: &str,
|
2019-02-18 10:42:15 -05:00
|
|
|
source_code: &[u8],
|
2019-02-12 15:20:54 -05:00
|
|
|
version: &str,
|
2019-04-29 10:58:31 -04:00
|
|
|
config: &[u8],
|
2019-02-12 15:20:54 -05:00
|
|
|
) -> String {
|
2018-08-28 13:49:19 -04:00
|
|
|
let mut ctx = ring::digest::Context::new(&ring::digest::SHA1);
|
2019-02-12 15:20:54 -05:00
|
|
|
ctx.update(version.as_bytes());
|
2018-08-28 13:49:19 -04:00
|
|
|
ctx.update(filename.as_bytes());
|
2019-02-18 10:42:15 -05:00
|
|
|
ctx.update(source_code);
|
2019-04-29 10:58:31 -04:00
|
|
|
ctx.update(config);
|
2018-08-28 13:49:19 -04:00
|
|
|
let digest = ctx.finish();
|
2019-01-03 22:11:01 -05:00
|
|
|
let mut out = String::new();
|
|
|
|
// TODO There must be a better way to do this...
|
2018-08-28 13:49:19 -04:00
|
|
|
for byte in digest.as_ref() {
|
2019-01-03 22:11:01 -05:00
|
|
|
write!(&mut out, "{:02x}", byte).unwrap();
|
2018-08-28 13:49:19 -04:00
|
|
|
}
|
|
|
|
out
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
|
2018-08-14 16:50:53 -04:00
|
|
|
fn is_remote(module_name: &str) -> bool {
|
2018-11-08 13:38:54 -05:00
|
|
|
module_name.starts_with("http://") || module_name.starts_with("https://")
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
2018-09-05 11:19:39 -04:00
|
|
|
|
|
|
|
fn parse_local_or_remote(p: &str) -> Result<url::Url, url::ParseError> {
|
2019-02-12 21:14:02 -05:00
|
|
|
if is_remote(p) || p.starts_with("file:") {
|
2018-09-05 11:19:39 -04:00
|
|
|
Url::parse(p)
|
|
|
|
} else {
|
|
|
|
Url::from_file_path(p).map_err(|_err| url::ParseError::IdnaError)
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 22:14:27 -04:00
|
|
|
|
|
|
|
fn map_file_extension(path: &Path) -> msg::MediaType {
|
|
|
|
match path.extension() {
|
|
|
|
None => msg::MediaType::Unknown,
|
|
|
|
Some(os_str) => match os_str.to_str() {
|
|
|
|
Some("ts") => msg::MediaType::TypeScript,
|
|
|
|
Some("js") => msg::MediaType::JavaScript,
|
2019-05-03 15:03:10 -04:00
|
|
|
Some("mjs") => msg::MediaType::JavaScript,
|
2018-10-21 22:14:27 -04:00
|
|
|
Some("json") => msg::MediaType::Json,
|
|
|
|
_ => msg::MediaType::Unknown,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert a ContentType string into a enumerated MediaType
|
|
|
|
fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType {
|
|
|
|
match content_type {
|
|
|
|
Some(content_type) => {
|
|
|
|
// sometimes there is additional data after the media type in
|
|
|
|
// Content-Type so we have to do a bit of manipulation so we are only
|
|
|
|
// dealing with the actual media type
|
2018-11-04 09:04:24 -05:00
|
|
|
let ct_vector: Vec<&str> = content_type.split(';').collect();
|
2018-10-21 22:14:27 -04:00
|
|
|
let ct: &str = ct_vector.first().unwrap();
|
|
|
|
match ct.to_lowercase().as_ref() {
|
|
|
|
"application/typescript"
|
|
|
|
| "text/typescript"
|
|
|
|
| "video/vnd.dlna.mpeg-tts"
|
2018-10-27 20:26:42 -04:00
|
|
|
| "video/mp2t"
|
|
|
|
| "application/x-typescript" => msg::MediaType::TypeScript,
|
2018-10-21 22:14:27 -04:00
|
|
|
"application/javascript"
|
|
|
|
| "text/javascript"
|
|
|
|
| "application/ecmascript"
|
|
|
|
| "text/ecmascript"
|
|
|
|
| "application/x-javascript" => msg::MediaType::JavaScript,
|
|
|
|
"application/json" | "text/json" => msg::MediaType::Json,
|
|
|
|
"text/plain" => map_file_extension(path),
|
|
|
|
_ => {
|
|
|
|
debug!("unknown content type: {}", content_type);
|
|
|
|
msg::MediaType::Unknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => map_file_extension(path),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 10:42:15 -05:00
|
|
|
fn filter_shebang(bytes: Vec<u8>) -> Vec<u8> {
|
|
|
|
let string = str::from_utf8(&bytes).unwrap();
|
|
|
|
if let Some(i) = string.find('\n') {
|
|
|
|
let (_, rest) = string.split_at(i);
|
|
|
|
rest.as_bytes().to_owned()
|
2018-11-30 03:30:49 -05:00
|
|
|
} else {
|
2019-02-18 10:42:15 -05:00
|
|
|
Vec::new()
|
2018-11-15 05:43:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:45:39 -04:00
|
|
|
/// Asynchronously fetch remote source file specified by the URL `module_name`
|
|
|
|
/// and write it to disk at `filename`.
|
|
|
|
fn fetch_remote_source_async(
|
2019-04-01 21:46:40 -04:00
|
|
|
deno_dir: &DenoDir,
|
2019-03-19 14:31:56 -04:00
|
|
|
module_name: &str,
|
|
|
|
filename: &str,
|
2019-03-19 16:45:39 -04:00
|
|
|
) -> impl Future<Item = Option<ModuleMetaData>, Error = DenoError> {
|
2019-04-01 21:46:40 -04:00
|
|
|
use crate::http_util::FetchOnceResult;
|
|
|
|
{
|
|
|
|
eprintln!("Downloading {}", module_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let filename = filename.to_owned();
|
|
|
|
let module_name = module_name.to_owned();
|
|
|
|
|
|
|
|
// We write a special ".headers.json" file into the `.deno/deps` directory along side the
|
|
|
|
// cached file, containing just the media type and possible redirect target (both are http headers).
|
|
|
|
// If redirect target is present, the file itself if not cached.
|
|
|
|
// In future resolutions, we would instead follow this redirect target ("redirect_to").
|
|
|
|
loop_fn(
|
|
|
|
(
|
|
|
|
deno_dir.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
module_name.clone(),
|
|
|
|
filename.clone(),
|
|
|
|
),
|
|
|
|
|(
|
|
|
|
dir,
|
2019-04-09 01:15:49 -04:00
|
|
|
mut maybe_initial_module_name,
|
|
|
|
mut maybe_initial_filename,
|
2019-04-01 21:46:40 -04:00
|
|
|
module_name,
|
|
|
|
filename,
|
|
|
|
)| {
|
|
|
|
let url = module_name.parse::<http::uri::Uri>().unwrap();
|
|
|
|
// Single pass fetch, either yields code or yields redirect.
|
|
|
|
http_util::fetch_string_once(url).and_then(move |fetch_once_result| {
|
|
|
|
match fetch_once_result {
|
|
|
|
FetchOnceResult::Redirect(url) => {
|
|
|
|
// If redirects, update module_name and filename for next looped call.
|
|
|
|
let resolve_result = dir
|
|
|
|
.resolve_module(&(url.to_string()), ".")
|
|
|
|
.map_err(DenoError::from);
|
|
|
|
match resolve_result {
|
|
|
|
Ok((new_module_name, new_filename)) => {
|
|
|
|
if maybe_initial_module_name.is_none() {
|
|
|
|
maybe_initial_module_name = Some(module_name.clone());
|
|
|
|
maybe_initial_filename = Some(filename.clone());
|
|
|
|
}
|
|
|
|
// Not yet completed. Follow the redirect and loop.
|
|
|
|
Ok(Loop::Continue((
|
|
|
|
dir,
|
|
|
|
maybe_initial_module_name,
|
|
|
|
maybe_initial_filename,
|
|
|
|
new_module_name,
|
|
|
|
new_filename,
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FetchOnceResult::Code(source, maybe_content_type) => {
|
|
|
|
// We land on the code.
|
|
|
|
let p = PathBuf::from(filename.clone());
|
|
|
|
match p.parent() {
|
|
|
|
Some(ref parent) => fs::create_dir_all(parent),
|
|
|
|
None => Ok(()),
|
|
|
|
}?;
|
|
|
|
// Write file and create .headers.json for the file.
|
|
|
|
deno_fs::write_file(&p, &source, 0o666)?;
|
|
|
|
{
|
2019-04-09 01:15:49 -04:00
|
|
|
save_source_code_headers(
|
|
|
|
&filename,
|
|
|
|
maybe_content_type.clone(),
|
|
|
|
None,
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
}
|
|
|
|
// Check if this file is downloaded due to some old redirect request.
|
|
|
|
if maybe_initial_filename.is_some() {
|
|
|
|
// If yes, record down the headers for redirect.
|
|
|
|
// Also create its containing folder.
|
|
|
|
let pp = PathBuf::from(filename.clone());
|
|
|
|
match pp.parent() {
|
|
|
|
Some(ref parent) => fs::create_dir_all(parent),
|
|
|
|
None => Ok(()),
|
|
|
|
}?;
|
|
|
|
{
|
|
|
|
save_source_code_headers(
|
|
|
|
&maybe_initial_filename.clone().unwrap(),
|
|
|
|
maybe_content_type.clone(),
|
|
|
|
Some(module_name.clone()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Loop::Break(Some(ModuleMetaData {
|
|
|
|
module_name: module_name.to_string(),
|
|
|
|
module_redirect_source_name: maybe_initial_module_name,
|
|
|
|
filename: filename.to_string(),
|
|
|
|
media_type: map_content_type(
|
|
|
|
&p,
|
2019-04-17 09:25:51 -04:00
|
|
|
maybe_content_type.as_ref().map(String::as_str),
|
2019-04-01 21:46:40 -04:00
|
|
|
),
|
|
|
|
source_code: source.as_bytes().to_owned(),
|
|
|
|
maybe_output_code_filename: None,
|
|
|
|
maybe_output_code: None,
|
|
|
|
maybe_source_map_filename: None,
|
|
|
|
maybe_source_map: None,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-03-19 16:45:39 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch remote source code.
|
2019-03-20 11:38:43 -04:00
|
|
|
#[cfg(test)]
|
2019-03-19 16:45:39 -04:00
|
|
|
fn fetch_remote_source(
|
2019-04-01 21:46:40 -04:00
|
|
|
deno_dir: &DenoDir,
|
2019-03-19 16:45:39 -04:00
|
|
|
module_name: &str,
|
|
|
|
filename: &str,
|
|
|
|
) -> DenoResult<Option<ModuleMetaData>> {
|
2019-04-01 21:46:40 -04:00
|
|
|
tokio_util::block_on(fetch_remote_source_async(
|
|
|
|
deno_dir,
|
|
|
|
module_name,
|
|
|
|
filename,
|
|
|
|
))
|
2019-03-19 14:31:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch local or cached source code.
|
2019-04-01 21:46:40 -04:00
|
|
|
/// This is a recursive operation if source file has redirection.
|
|
|
|
/// It will keep reading filename.headers.json for information about redirection.
|
|
|
|
/// module_initial_source_name would be None on first call,
|
|
|
|
/// and becomes the name of the very first module that initiates the call
|
|
|
|
/// in subsequent recursions.
|
|
|
|
/// AKA if redirection occurs, module_initial_source_name is the source path
|
|
|
|
/// that user provides, and the final module_name is the resolved path
|
|
|
|
/// after following all redirections.
|
2019-03-19 14:31:56 -04:00
|
|
|
fn fetch_local_source(
|
2019-04-01 21:46:40 -04:00
|
|
|
deno_dir: &DenoDir,
|
2019-03-19 14:31:56 -04:00
|
|
|
module_name: &str,
|
|
|
|
filename: &str,
|
2019-04-01 21:46:40 -04:00
|
|
|
module_initial_source_name: Option<String>,
|
2019-03-19 14:31:56 -04:00
|
|
|
) -> DenoResult<Option<ModuleMetaData>> {
|
|
|
|
let p = Path::new(&filename);
|
2019-04-01 21:46:40 -04:00
|
|
|
let source_code_headers = get_source_code_headers(&filename);
|
|
|
|
// If source code headers says that it would redirect elsewhere,
|
|
|
|
// (meaning that the source file might not exist; only .headers.json is present)
|
|
|
|
// Abort reading attempts to the cached source file and and follow the redirect.
|
|
|
|
if let Some(redirect_to) = source_code_headers.redirect_to {
|
|
|
|
// E.g.
|
|
|
|
// module_name https://import-meta.now.sh/redirect.js
|
|
|
|
// filename /Users/kun/Library/Caches/deno/deps/https/import-meta.now.sh/redirect.js
|
|
|
|
// redirect_to https://import-meta.now.sh/sub/final1.js
|
|
|
|
// real_filename /Users/kun/Library/Caches/deno/deps/https/import-meta.now.sh/sub/final1.js
|
|
|
|
// real_module_name = https://import-meta.now.sh/sub/final1.js
|
|
|
|
let (real_module_name, real_filename) =
|
|
|
|
deno_dir.resolve_module(&redirect_to, ".")?;
|
|
|
|
let mut module_initial_source_name = module_initial_source_name;
|
|
|
|
// If this is the first redirect attempt,
|
|
|
|
// then module_initial_source_name should be None.
|
|
|
|
// In that case, use current module name as module_initial_source_name.
|
|
|
|
if module_initial_source_name.is_none() {
|
|
|
|
module_initial_source_name = Some(module_name.to_owned());
|
|
|
|
}
|
|
|
|
// Recurse.
|
|
|
|
return fetch_local_source(
|
|
|
|
deno_dir,
|
|
|
|
&real_module_name,
|
|
|
|
&real_filename,
|
|
|
|
module_initial_source_name,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// No redirect needed or end of redirects.
|
|
|
|
// We can try read the file
|
2019-03-19 14:31:56 -04:00
|
|
|
let source_code = match fs::read(p) {
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
return Ok(None);
|
|
|
|
} else {
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(c) => c,
|
|
|
|
};
|
|
|
|
Ok(Some(ModuleMetaData {
|
|
|
|
module_name: module_name.to_string(),
|
2019-04-01 21:46:40 -04:00
|
|
|
module_redirect_source_name: module_initial_source_name,
|
2019-03-19 14:31:56 -04:00
|
|
|
filename: filename.to_string(),
|
2019-04-01 21:46:40 -04:00
|
|
|
media_type: map_content_type(
|
|
|
|
&p,
|
|
|
|
source_code_headers.mime_type.as_ref().map(String::as_str),
|
|
|
|
),
|
2019-03-19 14:31:56 -04:00
|
|
|
source_code,
|
|
|
|
maybe_output_code_filename: None,
|
|
|
|
maybe_output_code: None,
|
|
|
|
maybe_source_map_filename: None,
|
|
|
|
maybe_source_map: None,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
/// Header metadata associated with a particular "symbolic" source code file.
|
|
|
|
/// (the associated source code file might not be cached, while remaining
|
|
|
|
/// a user accessible entity through imports (due to redirects)).
|
|
|
|
pub struct SourceCodeHeaders {
|
|
|
|
/// MIME type of the source code.
|
|
|
|
pub mime_type: Option<String>,
|
|
|
|
/// Where should we actually look for source code.
|
|
|
|
/// This should be an absolute path!
|
|
|
|
pub redirect_to: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
static MIME_TYPE: &'static str = "mime_type";
|
|
|
|
static REDIRECT_TO: &'static str = "redirect_to";
|
|
|
|
|
|
|
|
fn source_code_headers_filename(filename: &str) -> String {
|
|
|
|
[&filename, ".headers.json"].concat()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get header metadata associated with a single source code file.
|
|
|
|
/// NOTICE: chances are that the source code itself is not downloaded due to redirects.
|
|
|
|
/// In this case, the headers file provides info about where we should go and get
|
|
|
|
/// the source code that redirect eventually points to (which should be cached).
|
|
|
|
fn get_source_code_headers(filename: &str) -> SourceCodeHeaders {
|
|
|
|
let headers_filename = source_code_headers_filename(filename);
|
|
|
|
let hd = Path::new(&headers_filename);
|
|
|
|
// .headers.json file might not exists.
|
|
|
|
// This is okay for local source.
|
|
|
|
let maybe_headers_string = fs::read_to_string(&hd).ok();
|
|
|
|
if let Some(headers_string) = maybe_headers_string {
|
|
|
|
// TODO(kevinkassimo): consider introduce serde::Deserialize to make things simpler.
|
|
|
|
let maybe_headers: serde_json::Result<serde_json::Value> =
|
|
|
|
serde_json::from_str(&headers_string);
|
|
|
|
if let Ok(headers) = maybe_headers {
|
|
|
|
return SourceCodeHeaders {
|
|
|
|
mime_type: headers[MIME_TYPE].as_str().map(String::from),
|
|
|
|
redirect_to: headers[REDIRECT_TO].as_str().map(String::from),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SourceCodeHeaders {
|
|
|
|
mime_type: None,
|
|
|
|
redirect_to: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Save headers related to source filename to {filename}.headers.json file,
|
|
|
|
/// only when there is actually something necessary to save.
|
|
|
|
/// For example, if the extension ".js" already mean JS file and we have
|
|
|
|
/// content type of "text/javascript", then we would not save the mime type.
|
|
|
|
/// If nothing needs to be saved, the headers file is not created.
|
|
|
|
fn save_source_code_headers(
|
|
|
|
filename: &str,
|
|
|
|
mime_type: Option<String>,
|
|
|
|
redirect_to: Option<String>,
|
|
|
|
) {
|
|
|
|
let headers_filename = source_code_headers_filename(filename);
|
|
|
|
// Remove possibly existing stale .headers.json file.
|
|
|
|
// May not exist. DON'T unwrap.
|
|
|
|
let _ = std::fs::remove_file(&headers_filename);
|
|
|
|
let p = PathBuf::from(filename);
|
|
|
|
// TODO(kevinkassimo): consider introduce serde::Deserialize to make things simpler.
|
|
|
|
// This is super ugly at this moment...
|
|
|
|
// Had trouble to make serde_derive work: I'm unable to build proc-macro2.
|
|
|
|
let mut value_map = serde_json::map::Map::new();
|
|
|
|
if mime_type.is_some() {
|
|
|
|
let mime_type_string = mime_type.clone().unwrap();
|
|
|
|
let resolved_mime_type =
|
|
|
|
{ map_content_type(Path::new(""), Some(mime_type_string.as_str())) };
|
2019-05-03 15:03:10 -04:00
|
|
|
let ext_based_mime_type = map_file_extension(&p);
|
2019-04-01 21:46:40 -04:00
|
|
|
// Add mime to headers only when content type is different from extension.
|
|
|
|
if ext_based_mime_type == msg::MediaType::Unknown
|
|
|
|
|| resolved_mime_type != ext_based_mime_type
|
|
|
|
{
|
|
|
|
value_map.insert(MIME_TYPE.to_string(), json!(mime_type_string));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if redirect_to.is_some() {
|
|
|
|
value_map.insert(REDIRECT_TO.to_string(), json!(redirect_to.unwrap()));
|
|
|
|
}
|
|
|
|
// Only save to file when there is actually data.
|
2019-04-09 01:15:49 -04:00
|
|
|
if !value_map.is_empty() {
|
2019-04-01 21:46:40 -04:00
|
|
|
let _ = serde_json::to_string(&value_map).map(|s| {
|
|
|
|
// It is possible that we need to create file
|
|
|
|
// with parent folders not yet created.
|
|
|
|
// (Due to .headers.json feature for redirection)
|
|
|
|
let hd = PathBuf::from(&headers_filename);
|
|
|
|
let _ = match hd.parent() {
|
|
|
|
Some(ref parent) => fs::create_dir_all(parent),
|
|
|
|
None => Ok(()),
|
|
|
|
};
|
|
|
|
let _ = deno_fs::write_file(&(hd.as_path()), s, 0o666);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 11:31:43 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2019-03-28 16:05:41 -04:00
|
|
|
fn test_setup() -> (TempDir, DenoDir) {
|
2018-12-11 11:31:43 -05:00
|
|
|
let temp_dir = TempDir::new().expect("tempdir fail");
|
2019-04-29 10:58:31 -04:00
|
|
|
let config = Some(b"{}".to_vec());
|
|
|
|
let deno_dir = DenoDir::new(Some(temp_dir.path().to_path_buf()), &config)
|
|
|
|
.expect("setup fail");
|
2018-12-11 11:31:43 -05:00
|
|
|
(temp_dir, deno_dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The `add_root` macro prepends "C:" to a string if on windows; on posix
|
|
|
|
// systems it returns the input string untouched. This is necessary because
|
|
|
|
// `Url::from_file_path()` fails if the input path isn't an absolute path.
|
|
|
|
macro_rules! add_root {
|
|
|
|
($path:expr) => {
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
concat!("C:", $path)
|
|
|
|
} else {
|
|
|
|
$path
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-12 21:14:02 -05:00
|
|
|
macro_rules! file_url {
|
|
|
|
($path:expr) => {
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
concat!("file:///C:", $path)
|
|
|
|
} else {
|
|
|
|
concat!("file://", $path)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-11 11:31:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_get_cache_filename() {
|
|
|
|
let url = Url::parse("http://example.com:1234/path/to/file.ts").unwrap();
|
|
|
|
let basedir = Path::new("/cache/dir/");
|
|
|
|
let cache_file = get_cache_filename(&basedir, &url);
|
|
|
|
assert_eq!(
|
|
|
|
cache_file,
|
|
|
|
Path::new("/cache/dir/example.com_PORT1234/path/to/file.ts")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cache_path() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (temp_dir, deno_dir) = test_setup();
|
2019-02-12 15:20:54 -05:00
|
|
|
let filename = "hello.js";
|
2019-03-28 08:09:19 -04:00
|
|
|
let source_code = b"1+2";
|
2019-04-29 10:58:31 -04:00
|
|
|
let config = b"{}";
|
|
|
|
let hash = source_code_hash(filename, source_code, version::DENO, config);
|
2018-12-11 11:31:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
(
|
2019-02-12 15:20:54 -05:00
|
|
|
temp_dir.path().join(format!("gen/{}.js", hash)),
|
|
|
|
temp_dir.path().join(format!("gen/{}.js.map", hash))
|
2018-12-11 11:31:43 -05:00
|
|
|
),
|
2019-02-12 15:20:54 -05:00
|
|
|
deno_dir.cache_path(filename, source_code)
|
2018-12-11 11:31:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:58:31 -04:00
|
|
|
#[test]
|
|
|
|
fn test_cache_path_config() {
|
|
|
|
// We are changing the compiler config from the "mock" and so we expect the
|
|
|
|
// resolved files coming back to not match the calculated hash.
|
|
|
|
let (temp_dir, deno_dir) = test_setup();
|
|
|
|
let filename = "hello.js";
|
|
|
|
let source_code = b"1+2";
|
|
|
|
let config = b"{\"compilerOptions\":{}}";
|
|
|
|
let hash = source_code_hash(filename, source_code, version::DENO, config);
|
|
|
|
assert_ne!(
|
|
|
|
(
|
|
|
|
temp_dir.path().join(format!("gen/{}.js", hash)),
|
|
|
|
temp_dir.path().join(format!("gen/{}.js.map", hash))
|
|
|
|
),
|
|
|
|
deno_dir.cache_path(filename, source_code)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-11 11:31:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_code_cache() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let filename = "hello.js";
|
2019-03-28 08:09:19 -04:00
|
|
|
let source_code = b"1+2";
|
|
|
|
let output_code = b"1+2 // output code";
|
|
|
|
let source_map = b"{}";
|
2019-04-29 10:58:31 -04:00
|
|
|
let config = b"{}";
|
|
|
|
let hash = source_code_hash(filename, source_code, version::DENO, config);
|
2018-12-11 11:31:43 -05:00
|
|
|
let (cache_path, source_map_path) =
|
|
|
|
deno_dir.cache_path(filename, source_code);
|
2019-02-12 15:20:54 -05:00
|
|
|
assert!(cache_path.ends_with(format!("gen/{}.js", hash)));
|
|
|
|
assert!(source_map_path.ends_with(format!("gen/{}.js.map", hash)));
|
2018-12-11 11:31:43 -05:00
|
|
|
|
2019-02-18 10:42:15 -05:00
|
|
|
let out = ModuleMetaData {
|
|
|
|
filename: filename.to_owned(),
|
2019-03-28 08:09:19 -04:00
|
|
|
source_code: source_code[..].to_owned(),
|
2019-02-18 10:42:15 -05:00
|
|
|
module_name: "hello.js".to_owned(),
|
2019-04-01 21:46:40 -04:00
|
|
|
module_redirect_source_name: None,
|
2019-02-18 10:42:15 -05:00
|
|
|
media_type: msg::MediaType::TypeScript,
|
2019-03-28 08:09:19 -04:00
|
|
|
maybe_output_code: Some(output_code[..].to_owned()),
|
2019-02-18 10:42:15 -05:00
|
|
|
maybe_output_code_filename: None,
|
2019-03-28 08:09:19 -04:00
|
|
|
maybe_source_map: Some(source_map[..].to_owned()),
|
2019-02-18 10:42:15 -05:00
|
|
|
maybe_source_map_filename: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let r = deno_dir.code_cache(&out);
|
2018-12-11 11:31:43 -05:00
|
|
|
r.expect("code_cache error");
|
|
|
|
assert!(cache_path.exists());
|
2019-03-28 08:09:19 -04:00
|
|
|
assert_eq!(output_code[..].to_owned(), fs::read(&cache_path).unwrap());
|
2018-12-11 11:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_source_code_hash() {
|
|
|
|
assert_eq!(
|
2019-04-29 10:58:31 -04:00
|
|
|
"830c8b63ba3194cf2108a3054c176b2bf53aee45",
|
|
|
|
source_code_hash("hello.ts", b"1+2", "0.2.11", b"{}")
|
2018-12-11 11:31:43 -05:00
|
|
|
);
|
|
|
|
// Different source_code should result in different hash.
|
|
|
|
assert_eq!(
|
2019-04-29 10:58:31 -04:00
|
|
|
"fb06127e9b2e169bea9c697fa73386ae7c901e8b",
|
|
|
|
source_code_hash("hello.ts", b"1", "0.2.11", b"{}")
|
2018-12-11 11:31:43 -05:00
|
|
|
);
|
|
|
|
// Different filename should result in different hash.
|
|
|
|
assert_eq!(
|
2019-04-29 10:58:31 -04:00
|
|
|
"3a17b6a493ff744b6a455071935f4bdcd2b72ec7",
|
|
|
|
source_code_hash("hi.ts", b"1+2", "0.2.11", b"{}")
|
2019-02-12 15:20:54 -05:00
|
|
|
);
|
|
|
|
// Different version should result in different hash.
|
|
|
|
assert_eq!(
|
2019-04-29 10:58:31 -04:00
|
|
|
"d6b2cfdc39dae9bd3ad5b493ee1544eb22e7475f",
|
|
|
|
source_code_hash("hi.ts", b"1+2", "0.2.0", b"{}")
|
2018-12-11 11:31:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
#[test]
|
|
|
|
fn test_source_code_headers_get_and_save() {
|
|
|
|
let (temp_dir, _deno_dir) = test_setup();
|
|
|
|
let filename =
|
|
|
|
deno_fs::normalize_path(temp_dir.into_path().join("f.js").as_ref());
|
|
|
|
let headers_file_name = source_code_headers_filename(&filename);
|
|
|
|
assert_eq!(headers_file_name, [&filename, ".headers.json"].concat());
|
|
|
|
let _ = deno_fs::write_file(&PathBuf::from(&headers_file_name),
|
|
|
|
"{\"mime_type\":\"text/javascript\",\"redirect_to\":\"http://example.com/a.js\"}", 0o666);
|
|
|
|
let headers = get_source_code_headers(&filename);
|
|
|
|
assert_eq!(headers.mime_type.clone().unwrap(), "text/javascript");
|
|
|
|
assert_eq!(
|
|
|
|
headers.redirect_to.clone().unwrap(),
|
|
|
|
"http://example.com/a.js"
|
|
|
|
);
|
|
|
|
|
|
|
|
save_source_code_headers(
|
|
|
|
&filename,
|
|
|
|
Some("text/typescript".to_owned()),
|
|
|
|
Some("http://deno.land/a.js".to_owned()),
|
|
|
|
);
|
|
|
|
let headers2 = get_source_code_headers(&filename);
|
|
|
|
assert_eq!(headers2.mime_type.clone().unwrap(), "text/typescript");
|
|
|
|
assert_eq!(
|
|
|
|
headers2.redirect_to.clone().unwrap(),
|
|
|
|
"http://deno.land/a.js"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-11 11:31:43 -05:00
|
|
|
#[test]
|
2018-12-27 15:40:06 -05:00
|
|
|
fn test_get_source_code_1() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-12 02:34:12 -05:00
|
|
|
// http_util::fetch_sync_string requires tokio
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let module_name = "http://localhost:4545/tests/subdir/mod2.ts";
|
|
|
|
let filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/mod2.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let headers_file_name = source_code_headers_filename(&filename);
|
2018-12-12 02:34:12 -05:00
|
|
|
|
2019-05-03 11:09:51 -04:00
|
|
|
let result =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, false);
|
2018-12-12 02:34:12 -05:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let r = result.unwrap();
|
2019-01-03 22:11:01 -05:00
|
|
|
assert_eq!(
|
2019-02-18 10:42:15 -05:00
|
|
|
r.source_code,
|
|
|
|
"export { printHello } from \"./print_hello.ts\";\n".as_bytes()
|
2019-01-03 22:11:01 -05:00
|
|
|
);
|
2018-12-12 02:34:12 -05:00
|
|
|
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
|
2019-04-01 21:46:40 -04:00
|
|
|
// Should not create .headers.json file due to matching ext
|
|
|
|
assert!(fs::read_to_string(&headers_file_name).is_err());
|
2018-12-12 02:34:12 -05:00
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
// Modify .headers.json, write using fs write and read using save_source_code_headers
|
|
|
|
let _ =
|
|
|
|
fs::write(&headers_file_name, "{ \"mime_type\": \"text/javascript\" }");
|
2019-05-03 11:09:51 -04:00
|
|
|
let result2 =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, false);
|
2018-12-12 02:34:12 -05:00
|
|
|
assert!(result2.is_ok());
|
|
|
|
let r2 = result2.unwrap();
|
2019-01-03 22:11:01 -05:00
|
|
|
assert_eq!(
|
2019-02-18 10:42:15 -05:00
|
|
|
r2.source_code,
|
|
|
|
"export { printHello } from \"./print_hello.ts\";\n".as_bytes()
|
2019-01-03 22:11:01 -05:00
|
|
|
);
|
2018-12-12 02:34:12 -05:00
|
|
|
// If get_source_code does not call remote, this should be JavaScript
|
2019-04-01 21:46:40 -04:00
|
|
|
// as we modified before! (we do not overwrite .headers.json due to no http fetch)
|
2018-12-12 02:34:12 -05:00
|
|
|
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
|
|
|
|
assert_eq!(
|
2019-04-01 21:46:40 -04:00
|
|
|
get_source_code_headers(&filename).mime_type.unwrap(),
|
2018-12-12 02:34:12 -05:00
|
|
|
"text/javascript"
|
|
|
|
);
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
// Modify .headers.json again, but the other way around
|
|
|
|
save_source_code_headers(
|
|
|
|
&filename,
|
|
|
|
Some("application/json".to_owned()),
|
|
|
|
None,
|
|
|
|
);
|
2019-05-03 11:09:51 -04:00
|
|
|
let result3 =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, false);
|
2018-12-12 02:34:12 -05:00
|
|
|
assert!(result3.is_ok());
|
|
|
|
let r3 = result3.unwrap();
|
2019-04-01 21:46:40 -04:00
|
|
|
assert_eq!(
|
|
|
|
r3.source_code,
|
|
|
|
"export { printHello } from \"./print_hello.ts\";\n".as_bytes()
|
|
|
|
);
|
|
|
|
// If get_source_code does not call remote, this should be JavaScript
|
|
|
|
// as we modified before! (we do not overwrite .headers.json due to no http fetch)
|
|
|
|
assert_eq!(&(r3.media_type), &msg::MediaType::Json);
|
|
|
|
assert!(
|
|
|
|
fs::read_to_string(&headers_file_name)
|
|
|
|
.unwrap()
|
|
|
|
.contains("application/json")
|
|
|
|
);
|
|
|
|
|
|
|
|
// Don't use_cache
|
2019-05-03 11:09:51 -04:00
|
|
|
let result4 =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, false, false);
|
2019-04-01 21:46:40 -04:00
|
|
|
assert!(result4.is_ok());
|
|
|
|
let r4 = result4.unwrap();
|
|
|
|
let expected4 =
|
2019-02-18 10:42:15 -05:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n".as_bytes();
|
2019-04-01 21:46:40 -04:00
|
|
|
assert_eq!(r4.source_code, expected4);
|
|
|
|
// Now the old .headers.json file should have gone! Resolved back to TypeScript
|
|
|
|
assert_eq!(&(r4.media_type), &msg::MediaType::TypeScript);
|
|
|
|
assert!(fs::read_to_string(&headers_file_name).is_err());
|
2018-12-27 15:40:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_source_code_2() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-27 15:40:06 -05:00
|
|
|
// http_util::fetch_sync_string requires tokio
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let module_name = "http://localhost:4545/tests/subdir/mismatch_ext.ts";
|
|
|
|
let filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/mismatch_ext.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let headers_file_name = source_code_headers_filename(&filename);
|
2018-12-27 15:40:06 -05:00
|
|
|
|
2019-05-03 11:09:51 -04:00
|
|
|
let result =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, false);
|
2018-12-27 15:40:06 -05:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let r = result.unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
let expected = "export const loaded = true;\n".as_bytes();
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(r.source_code, expected);
|
2019-04-01 21:46:40 -04:00
|
|
|
// Mismatch ext with content type, create .headers.json
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(&(r.media_type), &msg::MediaType::JavaScript);
|
2018-12-12 02:34:12 -05:00
|
|
|
assert_eq!(
|
2019-04-01 21:46:40 -04:00
|
|
|
get_source_code_headers(&filename).mime_type.unwrap(),
|
2018-12-27 15:40:06 -05:00
|
|
|
"text/javascript"
|
|
|
|
);
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
// Modify .headers.json
|
|
|
|
save_source_code_headers(
|
|
|
|
&filename,
|
|
|
|
Some("text/typescript".to_owned()),
|
|
|
|
None,
|
|
|
|
);
|
2019-05-03 11:09:51 -04:00
|
|
|
let result2 =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, false);
|
2018-12-27 15:40:06 -05:00
|
|
|
assert!(result2.is_ok());
|
|
|
|
let r2 = result2.unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
let expected2 = "export const loaded = true;\n".as_bytes();
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(r2.source_code, expected2);
|
|
|
|
// If get_source_code does not call remote, this should be TypeScript
|
2019-04-01 21:46:40 -04:00
|
|
|
// as we modified before! (we do not overwrite .headers.json due to no http fetch)
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(&(r2.media_type), &msg::MediaType::TypeScript);
|
2019-04-01 21:46:40 -04:00
|
|
|
assert!(fs::read_to_string(&headers_file_name).is_err());
|
2018-12-27 15:40:06 -05:00
|
|
|
|
2019-03-28 16:05:41 -04:00
|
|
|
// Don't use_cache
|
2019-05-03 11:09:51 -04:00
|
|
|
let result3 =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, false, false);
|
2018-12-27 15:40:06 -05:00
|
|
|
assert!(result3.is_ok());
|
|
|
|
let r3 = result3.unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
let expected3 = "export const loaded = true;\n".as_bytes();
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(r3.source_code, expected3);
|
2019-04-01 21:46:40 -04:00
|
|
|
// Now the old .headers.json file should be overwritten back to JavaScript!
|
2018-12-27 15:40:06 -05:00
|
|
|
// (due to http fetch)
|
|
|
|
assert_eq!(&(r3.media_type), &msg::MediaType::JavaScript);
|
|
|
|
assert_eq!(
|
2019-04-01 21:46:40 -04:00
|
|
|
get_source_code_headers(&filename).mime_type.unwrap(),
|
2018-12-27 15:40:06 -05:00
|
|
|
"text/javascript"
|
2018-12-12 02:34:12 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
#[test]
|
|
|
|
fn test_get_source_code_3() {
|
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
|
|
|
// Test basic follow and headers recording
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let redirect_module_name =
|
|
|
|
"http://localhost:4546/tests/subdir/redirects/redirect1.js";
|
|
|
|
let redirect_source_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4546/tests/subdir/redirects/redirect1.js")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
let target_module_name =
|
|
|
|
"http://localhost:4545/tests/subdir/redirects/redirect1.js";
|
|
|
|
let redirect_target_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/redirects/redirect1.js")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
let mod_meta = get_source_code(
|
|
|
|
&deno_dir,
|
|
|
|
redirect_module_name,
|
|
|
|
&redirect_source_filename,
|
|
|
|
true,
|
2019-05-03 11:09:51 -04:00
|
|
|
false,
|
2019-04-01 21:46:40 -04:00
|
|
|
).unwrap();
|
|
|
|
// File that requires redirection is not downloaded.
|
|
|
|
assert!(fs::read_to_string(&redirect_source_filename).is_err());
|
|
|
|
// ... but its .headers.json is created.
|
|
|
|
let redirect_source_headers =
|
|
|
|
get_source_code_headers(&redirect_source_filename);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_source_headers.redirect_to.unwrap(),
|
|
|
|
"http://localhost:4545/tests/subdir/redirects/redirect1.js"
|
|
|
|
);
|
|
|
|
// The target of redirection is downloaded instead.
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(&redirect_target_filename).unwrap(),
|
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
|
|
|
let redirect_target_headers =
|
|
|
|
get_source_code_headers(&redirect_target_filename);
|
|
|
|
assert!(redirect_target_headers.redirect_to.is_none());
|
|
|
|
|
|
|
|
// Examine the meta result.
|
|
|
|
assert_eq!(&mod_meta.module_name, target_module_name);
|
|
|
|
assert_eq!(
|
|
|
|
&mod_meta.module_redirect_source_name.clone().unwrap(),
|
|
|
|
redirect_module_name
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_source_code_4() {
|
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
|
|
|
// Test double redirects and headers recording
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let redirect_module_name =
|
|
|
|
"http://localhost:4548/tests/subdir/redirects/redirect1.js";
|
|
|
|
let redirect_source_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4548/tests/subdir/redirects/redirect1.js")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
let redirect_source_filename_intermediate = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4546/tests/subdir/redirects/redirect1.js")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
let target_module_name =
|
|
|
|
"http://localhost:4545/tests/subdir/redirects/redirect1.js";
|
|
|
|
let redirect_target_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/redirects/redirect1.js")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
let mod_meta = get_source_code(
|
|
|
|
&deno_dir,
|
|
|
|
redirect_module_name,
|
|
|
|
&redirect_source_filename,
|
|
|
|
true,
|
2019-05-03 11:09:51 -04:00
|
|
|
false,
|
2019-04-01 21:46:40 -04:00
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
// File that requires redirection is not downloaded.
|
|
|
|
assert!(fs::read_to_string(&redirect_source_filename).is_err());
|
|
|
|
// ... but its .headers.json is created.
|
|
|
|
let redirect_source_headers =
|
|
|
|
get_source_code_headers(&redirect_source_filename);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_source_headers.redirect_to.unwrap(),
|
|
|
|
target_module_name
|
|
|
|
);
|
|
|
|
|
|
|
|
// In the intermediate redirection step, file is also not downloaded.
|
|
|
|
assert!(
|
|
|
|
fs::read_to_string(&redirect_source_filename_intermediate).is_err()
|
|
|
|
);
|
|
|
|
|
|
|
|
// The target of redirection is downloaded instead.
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(&redirect_target_filename).unwrap(),
|
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
|
|
|
let redirect_target_headers =
|
|
|
|
get_source_code_headers(&redirect_target_filename);
|
|
|
|
assert!(redirect_target_headers.redirect_to.is_none());
|
|
|
|
|
|
|
|
// Examine the meta result.
|
|
|
|
assert_eq!(&mod_meta.module_name, target_module_name);
|
|
|
|
assert_eq!(
|
|
|
|
&mod_meta.module_redirect_source_name.clone().unwrap(),
|
|
|
|
redirect_module_name
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-03 11:09:51 -04:00
|
|
|
#[test]
|
|
|
|
fn test_get_source_code_no_fetch() {
|
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let module_name = "http://localhost:4545/tests/002_hello.ts";
|
|
|
|
let filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/002_hello.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// file hasn't been cached before and remote downloads are not allowed
|
|
|
|
let result =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, true);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::NotFound);
|
|
|
|
|
|
|
|
// download and cache file
|
|
|
|
let result =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, false);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
// module is already cached, should be ok even with `no_fetch`
|
|
|
|
let result =
|
|
|
|
get_source_code(&deno_dir, module_name, &filename, true, true);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:45:39 -04:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_source_async_1() {
|
|
|
|
use crate::tokio_util;
|
|
|
|
// http_util::fetch_sync_string requires tokio
|
|
|
|
tokio_util::init(|| {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2019-03-19 16:45:39 -04:00
|
|
|
let module_name =
|
|
|
|
"http://127.0.0.1:4545/tests/subdir/mt_video_mp2t.t3.ts".to_string();
|
|
|
|
let filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("127.0.0.1_PORT4545/tests/subdir/mt_video_mp2t.t3.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let headers_file_name = source_code_headers_filename(&filename);
|
2019-03-19 16:45:39 -04:00
|
|
|
|
|
|
|
let result = tokio_util::block_on(fetch_remote_source_async(
|
2019-04-01 21:46:40 -04:00
|
|
|
&deno_dir,
|
2019-03-19 16:45:39 -04:00
|
|
|
&module_name,
|
|
|
|
&filename,
|
|
|
|
));
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let r = result.unwrap().unwrap();
|
|
|
|
assert_eq!(r.source_code, b"export const loaded = true;\n");
|
|
|
|
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
|
2019-04-01 21:46:40 -04:00
|
|
|
// matching ext, no .headers.json file created
|
|
|
|
assert!(fs::read_to_string(&headers_file_name).is_err());
|
2019-03-19 16:45:39 -04:00
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
// Modify .headers.json, make sure read from local
|
|
|
|
save_source_code_headers(
|
|
|
|
&filename,
|
|
|
|
Some("text/javascript".to_owned()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let result2 =
|
|
|
|
fetch_local_source(&deno_dir, &module_name, &filename, None);
|
2019-03-19 16:45:39 -04:00
|
|
|
assert!(result2.is_ok());
|
|
|
|
let r2 = result2.unwrap().unwrap();
|
|
|
|
assert_eq!(r2.source_code, b"export const loaded = true;\n");
|
2019-04-01 21:46:40 -04:00
|
|
|
// Not MediaType::TypeScript due to .headers.json modification
|
2019-03-19 16:45:39 -04:00
|
|
|
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-12 02:34:12 -05:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_source_1() {
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::tokio_util;
|
2018-12-11 11:31:43 -05:00
|
|
|
// http_util::fetch_sync_string requires tokio
|
|
|
|
tokio_util::init(|| {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
let module_name =
|
|
|
|
"http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts";
|
|
|
|
let filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/mt_video_mp2t.t3.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let headers_file_name = source_code_headers_filename(&filename);
|
2018-12-11 11:31:43 -05:00
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
let result = fetch_remote_source(&deno_dir, module_name, &filename);
|
2018-12-11 11:31:43 -05:00
|
|
|
assert!(result.is_ok());
|
2018-12-12 02:34:12 -05:00
|
|
|
let r = result.unwrap().unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
|
2018-12-12 02:34:12 -05:00
|
|
|
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
|
2019-04-01 21:46:40 -04:00
|
|
|
// matching ext, no .headers.json file created
|
|
|
|
assert!(fs::read_to_string(&headers_file_name).is_err());
|
2018-12-11 11:31:43 -05:00
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
// Modify .headers.json, make sure read from local
|
|
|
|
save_source_code_headers(
|
|
|
|
&filename,
|
|
|
|
Some("text/javascript".to_owned()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let result2 = fetch_local_source(&deno_dir, module_name, &filename, None);
|
2018-12-11 11:31:43 -05:00
|
|
|
assert!(result2.is_ok());
|
2018-12-12 02:34:12 -05:00
|
|
|
let r2 = result2.unwrap().unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes());
|
2019-04-01 21:46:40 -04:00
|
|
|
// Not MediaType::TypeScript due to .headers.json modification
|
2018-12-12 02:34:12 -05:00
|
|
|
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
|
2018-12-11 11:31:43 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-12-12 02:34:12 -05:00
|
|
|
fn test_fetch_source_2() {
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::tokio_util;
|
2018-12-27 15:40:06 -05:00
|
|
|
// http_util::fetch_sync_string requires tokio
|
|
|
|
tokio_util::init(|| {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-27 15:40:06 -05:00
|
|
|
let module_name = "http://localhost:4545/tests/subdir/no_ext";
|
|
|
|
let filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/no_ext")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let result = fetch_remote_source(&deno_dir, module_name, &filename);
|
2018-12-27 15:40:06 -05:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let r = result.unwrap().unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
|
2019-04-01 21:46:40 -04:00
|
|
|
// no ext, should create .headers.json file
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(
|
2019-04-01 21:46:40 -04:00
|
|
|
get_source_code_headers(&filename).mime_type.unwrap(),
|
2018-12-27 15:40:06 -05:00
|
|
|
"text/typescript"
|
|
|
|
);
|
|
|
|
|
|
|
|
let module_name_2 = "http://localhost:4545/tests/subdir/mismatch_ext.ts";
|
|
|
|
let filename_2 = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/mismatch_ext.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let result_2 = fetch_remote_source(&deno_dir, module_name_2, &filename_2);
|
2018-12-27 15:40:06 -05:00
|
|
|
assert!(result_2.is_ok());
|
|
|
|
let r2 = result_2.unwrap().unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes());
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
|
2019-04-01 21:46:40 -04:00
|
|
|
// mismatch ext, should create .headers.json file
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(
|
2019-04-01 21:46:40 -04:00
|
|
|
get_source_code_headers(&filename_2).mime_type.unwrap(),
|
2018-12-27 15:40:06 -05:00
|
|
|
"text/javascript"
|
|
|
|
);
|
|
|
|
|
|
|
|
// test unknown extension
|
|
|
|
let module_name_3 = "http://localhost:4545/tests/subdir/unknown_ext.deno";
|
|
|
|
let filename_3 = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/unknown_ext.deno")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
2019-04-01 21:46:40 -04:00
|
|
|
let result_3 = fetch_remote_source(&deno_dir, module_name_3, &filename_3);
|
2018-12-27 15:40:06 -05:00
|
|
|
assert!(result_3.is_ok());
|
|
|
|
let r3 = result_3.unwrap().unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
assert_eq!(r3.source_code, "export const loaded = true;\n".as_bytes());
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(&(r3.media_type), &msg::MediaType::TypeScript);
|
2019-04-01 21:46:40 -04:00
|
|
|
// unknown ext, should create .headers.json file
|
2018-12-27 15:40:06 -05:00
|
|
|
assert_eq!(
|
2019-04-01 21:46:40 -04:00
|
|
|
get_source_code_headers(&filename_3).mime_type.unwrap(),
|
2018-12-27 15:40:06 -05:00
|
|
|
"text/typescript"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_source_3() {
|
2018-12-11 11:31:43 -05:00
|
|
|
// only local, no http_util::fetch_sync_string called
|
2019-04-01 21:46:40 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
let cwd_string = cwd.to_str().unwrap();
|
|
|
|
let module_name = "http://example.com/mt_text_typescript.t1.ts"; // not used
|
|
|
|
let filename =
|
|
|
|
format!("{}/tests/subdir/mt_text_typescript.t1.ts", &cwd_string);
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
let result = fetch_local_source(&deno_dir, module_name, &filename, None);
|
2018-12-11 11:31:43 -05:00
|
|
|
assert!(result.is_ok());
|
2018-12-12 02:34:12 -05:00
|
|
|
let r = result.unwrap().unwrap();
|
2019-02-18 10:42:15 -05:00
|
|
|
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
|
2018-12-12 02:34:12 -05:00
|
|
|
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
|
2018-12-11 11:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-02-18 10:42:15 -05:00
|
|
|
fn test_fetch_module_meta_data() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
let cwd_string = String::from(cwd.to_str().unwrap()) + "/";
|
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
tokio_util::init(|| {
|
|
|
|
// Test failure case.
|
|
|
|
let specifier = "hello.ts";
|
|
|
|
let referrer = add_root!("/baddir/badfile.ts");
|
2019-05-03 11:09:51 -04:00
|
|
|
let r = deno_dir.fetch_module_meta_data(specifier, referrer, true, false);
|
2019-03-20 11:38:43 -04:00
|
|
|
assert!(r.is_err());
|
|
|
|
|
|
|
|
// Assuming cwd is the deno repo root.
|
|
|
|
let specifier = "./js/main.ts";
|
|
|
|
let referrer = cwd_string.as_str();
|
2019-05-03 11:09:51 -04:00
|
|
|
let r = deno_dir.fetch_module_meta_data(specifier, referrer, true, false);
|
2019-03-20 11:38:43 -04:00
|
|
|
assert!(r.is_ok());
|
|
|
|
})
|
2018-12-11 11:31:43 -05:00
|
|
|
}
|
|
|
|
|
2019-02-06 23:42:34 -05:00
|
|
|
#[test]
|
2019-02-18 10:42:15 -05:00
|
|
|
fn test_fetch_module_meta_data_1() {
|
2019-02-06 23:42:34 -05:00
|
|
|
/*recompile ts file*/
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2019-02-06 23:42:34 -05:00
|
|
|
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
let cwd_string = String::from(cwd.to_str().unwrap()) + "/";
|
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
tokio_util::init(|| {
|
|
|
|
// Test failure case.
|
|
|
|
let specifier = "hello.ts";
|
|
|
|
let referrer = add_root!("/baddir/badfile.ts");
|
2019-05-03 11:09:51 -04:00
|
|
|
let r =
|
|
|
|
deno_dir.fetch_module_meta_data(specifier, referrer, false, false);
|
2019-03-20 11:38:43 -04:00
|
|
|
assert!(r.is_err());
|
|
|
|
|
|
|
|
// Assuming cwd is the deno repo root.
|
|
|
|
let specifier = "./js/main.ts";
|
|
|
|
let referrer = cwd_string.as_str();
|
2019-05-03 11:09:51 -04:00
|
|
|
let r =
|
|
|
|
deno_dir.fetch_module_meta_data(specifier, referrer, false, false);
|
2019-03-20 11:38:43 -04:00
|
|
|
assert!(r.is_ok());
|
|
|
|
})
|
2019-02-06 23:42:34 -05:00
|
|
|
}
|
|
|
|
|
2018-12-11 11:31:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_src_file_to_url_1() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
assert_eq!("hello", deno_dir.src_file_to_url("hello"));
|
|
|
|
assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
|
|
|
|
let x = deno_dir.deps_http.join("hello/world.txt");
|
|
|
|
assert_eq!(
|
|
|
|
"http://hello/world.txt",
|
|
|
|
deno_dir.src_file_to_url(x.to_str().unwrap())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_src_file_to_url_2() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
assert_eq!("hello", deno_dir.src_file_to_url("hello"));
|
|
|
|
assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
|
|
|
|
let x = deno_dir.deps_https.join("hello/world.txt");
|
|
|
|
assert_eq!(
|
|
|
|
"https://hello/world.txt",
|
|
|
|
deno_dir.src_file_to_url(x.to_str().unwrap())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_src_file_to_url_3() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
let x = deno_dir.deps_http.join("localhost_PORT4545/world.txt");
|
|
|
|
assert_eq!(
|
|
|
|
"http://localhost:4545/world.txt",
|
|
|
|
deno_dir.src_file_to_url(x.to_str().unwrap())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_src_file_to_url_4() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
let x = deno_dir.deps_https.join("localhost_PORT4545/world.txt");
|
|
|
|
assert_eq!(
|
|
|
|
"https://localhost:4545/world.txt",
|
|
|
|
deno_dir.src_file_to_url(x.to_str().unwrap())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/denoland/deno/blob/golang/os_test.go#L16-L87
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_1() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
(
|
|
|
|
"./subdir/print_hello.ts",
|
2019-01-15 07:06:25 -05:00
|
|
|
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"),
|
2019-02-12 21:14:02 -05:00
|
|
|
file_url!("/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"),
|
2019-01-15 07:06:25 -05:00
|
|
|
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"),
|
2018-12-11 11:31:43 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"testdata/001_hello.js",
|
|
|
|
add_root!("/Users/rld/go/src/github.com/denoland/deno/"),
|
2019-02-12 21:14:02 -05:00
|
|
|
file_url!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
|
2018-12-11 11:31:43 -05:00
|
|
|
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
add_root!("/Users/rld/src/deno/hello.js"),
|
|
|
|
".",
|
2019-02-12 21:14:02 -05:00
|
|
|
file_url!("/Users/rld/src/deno/hello.js"),
|
2018-12-11 11:31:43 -05:00
|
|
|
add_root!("/Users/rld/src/deno/hello.js"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
add_root!("/this/module/got/imported.js"),
|
|
|
|
add_root!("/that/module/did/it.js"),
|
2019-02-12 21:14:02 -05:00
|
|
|
file_url!("/this/module/got/imported.js"),
|
2018-12-11 11:31:43 -05:00
|
|
|
add_root!("/this/module/got/imported.js"),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
for &test in test_cases.iter() {
|
|
|
|
let specifier = String::from(test.0);
|
|
|
|
let referrer = String::from(test.1);
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(&specifier, &referrer).unwrap();
|
|
|
|
assert_eq!(module_name, test.2);
|
|
|
|
assert_eq!(filename, test.3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_2() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let specifier = "http://localhost:4545/testdata/subdir/print_hello.ts";
|
|
|
|
let referrer = add_root!("/deno/testdata/006_url_imports.ts");
|
|
|
|
|
|
|
|
let expected_module_name =
|
|
|
|
"http://localhost:4545/testdata/subdir/print_hello.ts";
|
|
|
|
let expected_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/testdata/subdir/print_hello.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, referrer).unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_3() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let specifier_ =
|
|
|
|
deno_dir.deps_http.join("unpkg.com/liltest@0.0.5/index.ts");
|
|
|
|
let specifier = specifier_.to_str().unwrap();
|
|
|
|
let referrer = ".";
|
|
|
|
|
|
|
|
let expected_module_name = "http://unpkg.com/liltest@0.0.5/index.ts";
|
|
|
|
let expected_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("unpkg.com/liltest@0.0.5/index.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, referrer).unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_4() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let specifier = "./util";
|
|
|
|
let referrer_ = deno_dir.deps_http.join("unpkg.com/liltest@0.0.5/index.ts");
|
|
|
|
let referrer = referrer_.to_str().unwrap();
|
|
|
|
|
|
|
|
// http containing files -> load relative import with http
|
|
|
|
let expected_module_name = "http://unpkg.com/liltest@0.0.5/util";
|
|
|
|
let expected_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("unpkg.com/liltest@0.0.5/util")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, referrer).unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_5() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let specifier = "./util";
|
|
|
|
let referrer_ =
|
|
|
|
deno_dir.deps_https.join("unpkg.com/liltest@0.0.5/index.ts");
|
|
|
|
let referrer = referrer_.to_str().unwrap();
|
|
|
|
|
|
|
|
// https containing files -> load relative import with https
|
|
|
|
let expected_module_name = "https://unpkg.com/liltest@0.0.5/util";
|
|
|
|
let expected_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_https
|
|
|
|
.join("unpkg.com/liltest@0.0.5/util")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, referrer).unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_6() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let specifier = "http://localhost:4545/tests/subdir/mod2.ts";
|
|
|
|
let referrer = add_root!("/deno/tests/006_url_imports.ts");
|
|
|
|
let expected_module_name = "http://localhost:4545/tests/subdir/mod2.ts";
|
|
|
|
let expected_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir
|
|
|
|
.deps_http
|
|
|
|
.join("localhost_PORT4545/tests/subdir/mod2.ts")
|
|
|
|
.as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, referrer).unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_7() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2018-12-11 11:31:43 -05:00
|
|
|
|
|
|
|
let specifier = "http_test.ts";
|
|
|
|
let referrer = add_root!("/Users/rld/src/deno_net/");
|
2019-02-13 13:26:30 -05:00
|
|
|
let expected_module_name =
|
|
|
|
file_url!("/Users/rld/src/deno_net/http_test.ts");
|
2018-12-11 11:31:43 -05:00
|
|
|
let expected_filename = add_root!("/Users/rld/src/deno_net/http_test.ts");
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, referrer).unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
2019-04-13 13:27:27 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_8() {
|
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
|
|
|
|
|
|
|
let specifier = "/util";
|
|
|
|
let referrer_ =
|
|
|
|
deno_dir.deps_https.join("unpkg.com/liltest@0.0.5/index.ts");
|
|
|
|
let referrer = referrer_.to_str().unwrap();
|
|
|
|
|
|
|
|
let expected_module_name = "https://unpkg.com/util";
|
|
|
|
let expected_filename = deno_fs::normalize_path(
|
|
|
|
deno_dir.deps_https.join("unpkg.com/util").as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, referrer).unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
2019-01-03 12:51:56 -05:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_referrer_dot() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2019-01-03 12:51:56 -05:00
|
|
|
|
|
|
|
let specifier = "tests/001_hello.js";
|
|
|
|
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
let expected_path = cwd.join(specifier);
|
2019-02-12 21:14:02 -05:00
|
|
|
let expected_module_name =
|
|
|
|
Url::from_file_path(&expected_path).unwrap().to_string();
|
|
|
|
let expected_filename = deno_fs::normalize_path(&expected_path);
|
2019-01-03 12:51:56 -05:00
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, ".").unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, "./").unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_module_referrer_dotdot() {
|
2019-03-28 16:05:41 -04:00
|
|
|
let (_temp_dir, deno_dir) = test_setup();
|
2019-01-03 12:51:56 -05:00
|
|
|
|
|
|
|
let specifier = "tests/001_hello.js";
|
|
|
|
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
let expected_path = cwd.join("..").join(specifier);
|
2019-02-12 21:14:02 -05:00
|
|
|
let expected_module_name =
|
|
|
|
Url::from_file_path(&expected_path).unwrap().to_string();
|
|
|
|
let expected_filename = deno_fs::normalize_path(&expected_path);
|
2019-01-03 12:51:56 -05:00
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, "..").unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
|
|
|
|
let (module_name, filename) =
|
|
|
|
deno_dir.resolve_module(specifier, "../").unwrap();
|
|
|
|
assert_eq!(module_name, expected_module_name);
|
|
|
|
assert_eq!(filename, expected_filename);
|
|
|
|
}
|
|
|
|
|
2018-12-11 11:31:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_map_file_extension() {
|
|
|
|
assert_eq!(
|
|
|
|
map_file_extension(Path::new("foo/bar.ts")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_file_extension(Path::new("foo/bar.d.ts")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_file_extension(Path::new("foo/bar.js")),
|
|
|
|
msg::MediaType::JavaScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_file_extension(Path::new("foo/bar.json")),
|
|
|
|
msg::MediaType::Json
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_file_extension(Path::new("foo/bar.txt")),
|
|
|
|
msg::MediaType::Unknown
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_file_extension(Path::new("foo/bar")),
|
|
|
|
msg::MediaType::Unknown
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_content_type() {
|
|
|
|
// Extension only
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar.ts"), None),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar.d.ts"), None),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar.js"), None),
|
|
|
|
msg::MediaType::JavaScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar.json"), None),
|
|
|
|
msg::MediaType::Json
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar.txt"), None),
|
|
|
|
msg::MediaType::Unknown
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), None),
|
|
|
|
msg::MediaType::Unknown
|
|
|
|
);
|
|
|
|
|
|
|
|
// Media Type
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("application/typescript")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("text/typescript")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("video/vnd.dlna.mpeg-tts")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("video/mp2t")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("application/x-typescript")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("application/javascript")),
|
|
|
|
msg::MediaType::JavaScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("text/javascript")),
|
|
|
|
msg::MediaType::JavaScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("application/ecmascript")),
|
|
|
|
msg::MediaType::JavaScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("text/ecmascript")),
|
|
|
|
msg::MediaType::JavaScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("application/x-javascript")),
|
|
|
|
msg::MediaType::JavaScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("application/json")),
|
|
|
|
msg::MediaType::Json
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar"), Some("text/json")),
|
|
|
|
msg::MediaType::Json
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar.ts"), Some("text/plain")),
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map_content_type(Path::new("foo/bar.ts"), Some("foo/bar")),
|
|
|
|
msg::MediaType::Unknown
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filter_shebang() {
|
2019-03-28 08:09:19 -04:00
|
|
|
assert_eq!(filter_shebang(b"#!"[..].to_owned()), b"");
|
2019-02-18 10:42:15 -05:00
|
|
|
assert_eq!(
|
|
|
|
filter_shebang("#!\n\n".as_bytes().to_owned()),
|
|
|
|
"\n\n".as_bytes()
|
|
|
|
);
|
|
|
|
let code = "#!/usr/bin/env deno\nconsole.log('hello');\n"
|
|
|
|
.as_bytes()
|
|
|
|
.to_owned();
|
|
|
|
assert_eq!(filter_shebang(code), "\nconsole.log('hello');\n".as_bytes());
|
2018-12-11 11:31:43 -05:00
|
|
|
}
|
2018-11-15 05:43:19 -05:00
|
|
|
}
|