mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
Move fetch_remote_source, fetch_local_source
They do not depend on DenoDir, so should not be methods of it.
This commit is contained in:
parent
2c5030000b
commit
5e4312de4d
1 changed files with 96 additions and 99 deletions
195
cli/deno_dir.rs
195
cli/deno_dir.rs
|
@ -157,95 +157,6 @@ impl DenoDir {
|
|||
}
|
||||
}
|
||||
|
||||
// Prototype https://github.com/denoland/deno/blob/golang/deno_dir.go#L37-L73
|
||||
/// Fetch remote source code.
|
||||
fn fetch_remote_source(
|
||||
self: &Self,
|
||||
module_name: &str,
|
||||
filename: &str,
|
||||
) -> DenoResult<Option<ModuleMetaData>> {
|
||||
let p = Path::new(&filename);
|
||||
// We write a special ".mime" file into the `.deno/deps` directory along side the
|
||||
// cached file, containing just the media type.
|
||||
let media_type_filename = [&filename, ".mime"].concat();
|
||||
let mt = Path::new(&media_type_filename);
|
||||
eprint!("Downloading {}...", &module_name); // no newline
|
||||
let maybe_source = http_util::fetch_sync_string(&module_name);
|
||||
if let Ok((source, content_type)) = maybe_source {
|
||||
eprintln!(""); // next line
|
||||
match p.parent() {
|
||||
Some(ref parent) => fs::create_dir_all(parent),
|
||||
None => Ok(()),
|
||||
}?;
|
||||
deno_fs::write_file(&p, &source, 0o666)?;
|
||||
// Remove possibly existing stale .mime file
|
||||
// may not exist. DON'T unwrap
|
||||
let _ = std::fs::remove_file(&media_type_filename);
|
||||
// Create .mime file only when content type different from extension
|
||||
let resolved_content_type = map_content_type(&p, Some(&content_type));
|
||||
let ext = p
|
||||
.extension()
|
||||
.map(|x| x.to_str().unwrap_or(""))
|
||||
.unwrap_or("");
|
||||
let media_type = extmap(&ext);
|
||||
if media_type == msg::MediaType::Unknown
|
||||
|| media_type != resolved_content_type
|
||||
{
|
||||
deno_fs::write_file(&mt, content_type.as_bytes(), 0o666)?
|
||||
}
|
||||
return Ok(Some(ModuleMetaData {
|
||||
module_name: module_name.to_string(),
|
||||
filename: filename.to_string(),
|
||||
media_type: map_content_type(&p, Some(&content_type)),
|
||||
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,
|
||||
}));
|
||||
} else {
|
||||
eprintln!(" NOT FOUND");
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Fetch local or cached source code.
|
||||
fn fetch_local_source(
|
||||
self: &Self,
|
||||
module_name: &str,
|
||||
filename: &str,
|
||||
) -> DenoResult<Option<ModuleMetaData>> {
|
||||
let p = Path::new(&filename);
|
||||
let media_type_filename = [&filename, ".mime"].concat();
|
||||
let mt = Path::new(&media_type_filename);
|
||||
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,
|
||||
};
|
||||
// .mime file might not exists
|
||||
// this is okay for local source: maybe_content_type_str will be None
|
||||
let maybe_content_type_string = fs::read_to_string(&mt).ok();
|
||||
// Option<String> -> Option<&str>
|
||||
let maybe_content_type_str =
|
||||
maybe_content_type_string.as_ref().map(String::as_str);
|
||||
Ok(Some(ModuleMetaData {
|
||||
module_name: module_name.to_string(),
|
||||
filename: filename.to_string(),
|
||||
media_type: map_content_type(&p, maybe_content_type_str),
|
||||
source_code,
|
||||
maybe_output_code_filename: None,
|
||||
maybe_output_code: None,
|
||||
maybe_source_map_filename: None,
|
||||
maybe_source_map: None,
|
||||
}))
|
||||
}
|
||||
|
||||
// Prototype: https://github.com/denoland/deno/blob/golang/os.go#L122-L138
|
||||
fn get_source_code(
|
||||
self: &Self,
|
||||
|
@ -261,7 +172,7 @@ impl DenoDir {
|
|||
"fetch local or reload {} is_module_remote {}",
|
||||
module_name, is_module_remote
|
||||
);
|
||||
match self.fetch_local_source(&module_name, &filename)? {
|
||||
match fetch_local_source(&module_name, &filename)? {
|
||||
Some(output) => {
|
||||
debug!("found local source ");
|
||||
return Ok(output);
|
||||
|
@ -284,8 +195,7 @@ impl DenoDir {
|
|||
debug!("is remote but didn't find module");
|
||||
|
||||
// not cached/local, try remote
|
||||
let maybe_remote_source =
|
||||
self.fetch_remote_source(&module_name, &filename)?;
|
||||
let maybe_remote_source = fetch_remote_source(&module_name, &filename)?;
|
||||
if let Some(output) = maybe_remote_source {
|
||||
return Ok(output);
|
||||
}
|
||||
|
@ -582,6 +492,93 @@ fn filter_shebang(bytes: Vec<u8>) -> Vec<u8> {
|
|||
}
|
||||
}
|
||||
|
||||
// Prototype https://github.com/denoland/deno/blob/golang/deno_dir.go#L37-L73
|
||||
/// Fetch remote source code.
|
||||
fn fetch_remote_source(
|
||||
module_name: &str,
|
||||
filename: &str,
|
||||
) -> DenoResult<Option<ModuleMetaData>> {
|
||||
let p = Path::new(&filename);
|
||||
// We write a special ".mime" file into the `.deno/deps` directory along side the
|
||||
// cached file, containing just the media type.
|
||||
let media_type_filename = [&filename, ".mime"].concat();
|
||||
let mt = Path::new(&media_type_filename);
|
||||
eprint!("Downloading {}...", &module_name); // no newline
|
||||
let maybe_source = http_util::fetch_sync_string(&module_name);
|
||||
if let Ok((source, content_type)) = maybe_source {
|
||||
eprintln!(""); // next line
|
||||
match p.parent() {
|
||||
Some(ref parent) => fs::create_dir_all(parent),
|
||||
None => Ok(()),
|
||||
}?;
|
||||
deno_fs::write_file(&p, &source, 0o666)?;
|
||||
// Remove possibly existing stale .mime file
|
||||
// may not exist. DON'T unwrap
|
||||
let _ = std::fs::remove_file(&media_type_filename);
|
||||
// Create .mime file only when content type different from extension
|
||||
let resolved_content_type = map_content_type(&p, Some(&content_type));
|
||||
let ext = p
|
||||
.extension()
|
||||
.map(|x| x.to_str().unwrap_or(""))
|
||||
.unwrap_or("");
|
||||
let media_type = extmap(&ext);
|
||||
if media_type == msg::MediaType::Unknown
|
||||
|| media_type != resolved_content_type
|
||||
{
|
||||
deno_fs::write_file(&mt, content_type.as_bytes(), 0o666)?
|
||||
}
|
||||
return Ok(Some(ModuleMetaData {
|
||||
module_name: module_name.to_string(),
|
||||
filename: filename.to_string(),
|
||||
media_type: map_content_type(&p, Some(&content_type)),
|
||||
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,
|
||||
}));
|
||||
} else {
|
||||
eprintln!(" NOT FOUND");
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Fetch local or cached source code.
|
||||
fn fetch_local_source(
|
||||
module_name: &str,
|
||||
filename: &str,
|
||||
) -> DenoResult<Option<ModuleMetaData>> {
|
||||
let p = Path::new(&filename);
|
||||
let media_type_filename = [&filename, ".mime"].concat();
|
||||
let mt = Path::new(&media_type_filename);
|
||||
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,
|
||||
};
|
||||
// .mime file might not exists
|
||||
// this is okay for local source: maybe_content_type_str will be None
|
||||
let maybe_content_type_string = fs::read_to_string(&mt).ok();
|
||||
// Option<String> -> Option<&str>
|
||||
let maybe_content_type_str =
|
||||
maybe_content_type_string.as_ref().map(String::as_str);
|
||||
Ok(Some(ModuleMetaData {
|
||||
module_name: module_name.to_string(),
|
||||
filename: filename.to_string(),
|
||||
media_type: map_content_type(&p, maybe_content_type_str),
|
||||
source_code,
|
||||
maybe_output_code_filename: None,
|
||||
maybe_output_code: None,
|
||||
maybe_source_map_filename: None,
|
||||
maybe_source_map: None,
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -835,7 +832,7 @@ mod tests {
|
|||
);
|
||||
let mime_file_name = format!("{}.mime", &filename);
|
||||
|
||||
let result = deno_dir.fetch_remote_source(module_name, &filename);
|
||||
let result = fetch_remote_source(module_name, &filename);
|
||||
assert!(result.is_ok());
|
||||
let r = result.unwrap().unwrap();
|
||||
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
|
||||
|
@ -845,7 +842,7 @@ mod tests {
|
|||
|
||||
// Modify .mime, make sure read from local
|
||||
let _ = fs::write(&mime_file_name, "text/javascript");
|
||||
let result2 = deno_dir.fetch_local_source(module_name, &filename);
|
||||
let result2 = fetch_local_source(module_name, &filename);
|
||||
assert!(result2.is_ok());
|
||||
let r2 = result2.unwrap().unwrap();
|
||||
assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes());
|
||||
|
@ -868,7 +865,7 @@ mod tests {
|
|||
.as_ref(),
|
||||
);
|
||||
let mime_file_name = format!("{}.mime", &filename);
|
||||
let result = deno_dir.fetch_remote_source(module_name, &filename);
|
||||
let result = fetch_remote_source(module_name, &filename);
|
||||
assert!(result.is_ok());
|
||||
let r = result.unwrap().unwrap();
|
||||
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
|
||||
|
@ -887,7 +884,7 @@ mod tests {
|
|||
.as_ref(),
|
||||
);
|
||||
let mime_file_name_2 = format!("{}.mime", &filename_2);
|
||||
let result_2 = deno_dir.fetch_remote_source(module_name_2, &filename_2);
|
||||
let result_2 = fetch_remote_source(module_name_2, &filename_2);
|
||||
assert!(result_2.is_ok());
|
||||
let r2 = result_2.unwrap().unwrap();
|
||||
assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes());
|
||||
|
@ -907,7 +904,7 @@ mod tests {
|
|||
.as_ref(),
|
||||
);
|
||||
let mime_file_name_3 = format!("{}.mime", &filename_3);
|
||||
let result_3 = deno_dir.fetch_remote_source(module_name_3, &filename_3);
|
||||
let result_3 = fetch_remote_source(module_name_3, &filename_3);
|
||||
assert!(result_3.is_ok());
|
||||
let r3 = result_3.unwrap().unwrap();
|
||||
assert_eq!(r3.source_code, "export const loaded = true;\n".as_bytes());
|
||||
|
@ -923,14 +920,14 @@ mod tests {
|
|||
#[test]
|
||||
fn test_fetch_source_3() {
|
||||
// only local, no http_util::fetch_sync_string called
|
||||
let (_temp_dir, deno_dir) = test_setup(false, false);
|
||||
let (_temp_dir, _deno_dir) = test_setup(false, false);
|
||||
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);
|
||||
|
||||
let result = deno_dir.fetch_local_source(module_name, &filename);
|
||||
let result = fetch_local_source(module_name, &filename);
|
||||
assert!(result.is_ok());
|
||||
let r = result.unwrap().unwrap();
|
||||
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
|
||||
|
|
Loading…
Reference in a new issue