2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-21 08:26:41 -04:00
|
|
|
|
2020-11-16 14:48:50 -05:00
|
|
|
use crate::fs_util;
|
2020-08-03 08:55:03 -04:00
|
|
|
use crate::http_cache::url_to_filename;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::{Host, Url};
|
2019-07-17 18:15:30 -04:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::fs;
|
2020-05-07 08:32:57 -04:00
|
|
|
use std::io;
|
2019-08-11 19:12:09 -04:00
|
|
|
use std::path::Component;
|
2019-07-17 18:15:30 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2019-08-11 19:12:09 -04:00
|
|
|
use std::path::Prefix;
|
|
|
|
use std::str;
|
2019-07-17 18:15:30 -04:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DiskCache {
|
|
|
|
pub location: PathBuf,
|
|
|
|
}
|
|
|
|
|
2020-01-24 17:16:31 -05:00
|
|
|
fn with_io_context<T: AsRef<str>>(
|
|
|
|
e: &std::io::Error,
|
|
|
|
context: T,
|
|
|
|
) -> std::io::Error {
|
|
|
|
std::io::Error::new(e.kind(), format!("{} (for '{}')", e, context.as_ref()))
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:15:30 -04:00
|
|
|
impl DiskCache {
|
2020-05-15 10:32:52 -04:00
|
|
|
/// `location` must be an absolute path.
|
2019-07-17 18:15:30 -04:00
|
|
|
pub fn new(location: &Path) -> Self {
|
2020-05-15 10:32:52 -04:00
|
|
|
assert!(location.is_absolute());
|
2019-07-17 18:15:30 -04:00
|
|
|
Self {
|
|
|
|
location: location.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:32:57 -04:00
|
|
|
/// Ensures the location of the cache.
|
2020-05-24 13:20:40 -04:00
|
|
|
pub fn ensure_dir_exists(&self, path: &Path) -> io::Result<()> {
|
|
|
|
if path.is_dir() {
|
2020-05-07 08:32:57 -04:00
|
|
|
return Ok(());
|
|
|
|
}
|
2020-05-24 13:20:40 -04:00
|
|
|
fs::create_dir_all(&path).map_err(|e| {
|
2020-05-07 08:32:57 -04:00
|
|
|
io::Error::new(e.kind(), format!(
|
|
|
|
"Could not create TypeScript compiler cache location: {:?}\nCheck the permission of the directory.",
|
2020-05-24 13:20:40 -04:00
|
|
|
path
|
2020-05-07 08:32:57 -04:00
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:52:55 -05:00
|
|
|
fn get_cache_filename(&self, url: &Url) -> Option<PathBuf> {
|
2019-07-17 18:15:30 -04:00
|
|
|
let mut out = PathBuf::new();
|
|
|
|
|
|
|
|
let scheme = url.scheme();
|
|
|
|
out.push(scheme);
|
2019-08-11 19:12:09 -04:00
|
|
|
|
2019-07-17 18:15:30 -04:00
|
|
|
match scheme {
|
2020-08-03 08:55:03 -04:00
|
|
|
"wasm" => {
|
2019-07-17 18:15:30 -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(),
|
|
|
|
};
|
|
|
|
out.push(host_port);
|
2019-08-11 19:12:09 -04:00
|
|
|
|
|
|
|
for path_seg in url.path_segments().unwrap() {
|
|
|
|
out.push(path_seg);
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 09:22:14 -04:00
|
|
|
"http" | "https" | "data" | "blob" => out = url_to_filename(url)?,
|
2019-08-11 19:12:09 -04:00
|
|
|
"file" => {
|
2021-01-04 04:33:20 -05:00
|
|
|
let path = match url.to_file_path() {
|
|
|
|
Ok(path) => path,
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
2019-08-11 19:12:09 -04:00
|
|
|
let mut path_components = path.components();
|
|
|
|
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
if let Some(Component::Prefix(prefix_component)) =
|
|
|
|
path_components.next()
|
|
|
|
{
|
|
|
|
// Windows doesn't support ":" in filenames, so we need to extract disk prefix
|
|
|
|
// Example: file:///C:/deno/js/unit_test_runner.ts
|
|
|
|
// it should produce: file\c\deno\js\unit_test_runner.ts
|
|
|
|
match prefix_component.kind() {
|
|
|
|
Prefix::Disk(disk_byte) | Prefix::VerbatimDisk(disk_byte) => {
|
|
|
|
let disk = (disk_byte as char).to_string();
|
|
|
|
out.push(disk);
|
|
|
|
}
|
2020-06-26 13:25:13 -04:00
|
|
|
Prefix::UNC(server, share)
|
|
|
|
| Prefix::VerbatimUNC(server, share) => {
|
|
|
|
out.push("UNC");
|
|
|
|
let host = Host::parse(server.to_str().unwrap()).unwrap();
|
|
|
|
let host = host.to_string().replace(":", "_");
|
|
|
|
out.push(host);
|
|
|
|
out.push(share);
|
|
|
|
}
|
2019-08-11 19:12:09 -04:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be relative, so strip forward slash
|
|
|
|
let mut remaining_components = path_components.as_path();
|
|
|
|
if let Ok(stripped) = remaining_components.strip_prefix("/") {
|
|
|
|
remaining_components = stripped;
|
|
|
|
};
|
|
|
|
|
|
|
|
out = out.join(remaining_components);
|
|
|
|
}
|
2020-12-15 00:52:55 -05:00
|
|
|
_ => return None,
|
2019-07-17 18:15:30 -04:00
|
|
|
};
|
|
|
|
|
2020-12-15 00:52:55 -05:00
|
|
|
Some(out)
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_cache_filename_with_extension(
|
2020-01-04 05:20:52 -05:00
|
|
|
&self,
|
2019-07-17 18:15:30 -04:00
|
|
|
url: &Url,
|
|
|
|
extension: &str,
|
2020-12-15 00:52:55 -05:00
|
|
|
) -> Option<PathBuf> {
|
|
|
|
let base = self.get_cache_filename(url)?;
|
2019-07-17 18:15:30 -04:00
|
|
|
|
|
|
|
match base.extension() {
|
2020-12-15 00:52:55 -05:00
|
|
|
None => Some(base.with_extension(extension)),
|
2019-07-17 18:15:30 -04:00
|
|
|
Some(ext) => {
|
|
|
|
let original_extension = OsStr::to_str(ext).unwrap();
|
|
|
|
let final_extension = format!("{}.{}", original_extension, extension);
|
2020-12-15 00:52:55 -05:00
|
|
|
Some(base.with_extension(final_extension))
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 05:20:52 -05:00
|
|
|
pub fn get(&self, filename: &Path) -> std::io::Result<Vec<u8>> {
|
2019-07-17 18:15:30 -04:00
|
|
|
let path = self.location.join(filename);
|
|
|
|
fs::read(&path)
|
|
|
|
}
|
|
|
|
|
2020-01-04 05:20:52 -05:00
|
|
|
pub fn set(&self, filename: &Path, data: &[u8]) -> std::io::Result<()> {
|
2019-07-17 18:15:30 -04:00
|
|
|
let path = self.location.join(filename);
|
|
|
|
match path.parent() {
|
2020-05-24 13:20:40 -04:00
|
|
|
Some(ref parent) => self.ensure_dir_exists(parent),
|
2019-07-17 18:15:30 -04:00
|
|
|
None => Ok(()),
|
|
|
|
}?;
|
2020-12-18 13:30:49 -05:00
|
|
|
fs_util::atomic_write_file(&path, data, crate::http_cache::CACHE_PERM)
|
2020-01-24 17:16:31 -05:00
|
|
|
.map_err(|e| with_io_context(&e, format!("{:#?}", &path)))
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-04-03 22:43:49 -04:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_cache_if_dir_exits() {
|
|
|
|
let cache_location = TempDir::new().unwrap();
|
|
|
|
let mut cache_path = cache_location.path().to_owned();
|
|
|
|
cache_path.push("foo");
|
2020-05-07 08:32:57 -04:00
|
|
|
let cache = DiskCache::new(&cache_path);
|
2020-05-24 13:20:40 -04:00
|
|
|
cache
|
|
|
|
.ensure_dir_exists(&cache.location)
|
|
|
|
.expect("Testing expect:");
|
2020-04-03 22:43:49 -04:00
|
|
|
assert!(cache_path.is_dir());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_cache_if_dir_not_exits() {
|
2020-04-06 10:08:53 -04:00
|
|
|
let temp_dir = TempDir::new().unwrap();
|
|
|
|
let mut cache_location = temp_dir.path().to_owned();
|
|
|
|
assert!(fs::remove_dir(&cache_location).is_ok());
|
|
|
|
cache_location.push("foo");
|
2021-06-17 15:56:30 -04:00
|
|
|
assert!(!cache_location.is_dir());
|
2020-05-07 08:32:57 -04:00
|
|
|
let cache = DiskCache::new(&cache_location);
|
2020-05-24 13:20:40 -04:00
|
|
|
cache
|
|
|
|
.ensure_dir_exists(&cache.location)
|
|
|
|
.expect("Testing expect:");
|
2021-06-17 15:56:30 -04:00
|
|
|
assert!(cache_location.is_dir());
|
2020-04-03 22:43:49 -04:00
|
|
|
}
|
2019-07-17 18:15:30 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_cache_filename() {
|
2019-08-11 19:12:09 -04:00
|
|
|
let cache_location = if cfg!(target_os = "windows") {
|
|
|
|
PathBuf::from(r"C:\deno_dir\")
|
|
|
|
} else {
|
|
|
|
PathBuf::from("/deno_dir/")
|
|
|
|
};
|
|
|
|
|
|
|
|
let cache = DiskCache::new(&cache_location);
|
2019-07-17 18:15:30 -04:00
|
|
|
|
2019-08-11 19:12:09 -04:00
|
|
|
let mut test_cases = vec![
|
2019-07-17 18:15:30 -04:00
|
|
|
(
|
|
|
|
"http://deno.land/std/http/file_server.ts",
|
2020-08-03 08:55:03 -04:00
|
|
|
"http/deno.land/d8300752800fe3f0beda9505dc1c3b5388beb1ee45afd1f1e2c9fc0866df15cf",
|
2019-07-17 18:15:30 -04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"http://localhost:8000/std/http/file_server.ts",
|
2020-08-03 08:55:03 -04:00
|
|
|
"http/localhost_PORT8000/d8300752800fe3f0beda9505dc1c3b5388beb1ee45afd1f1e2c9fc0866df15cf",
|
2019-07-17 18:15:30 -04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/std/http/file_server.ts",
|
2020-08-03 08:55:03 -04:00
|
|
|
"https/deno.land/d8300752800fe3f0beda9505dc1c3b5388beb1ee45afd1f1e2c9fc0866df15cf",
|
2019-07-17 18:15:30 -04:00
|
|
|
),
|
2020-06-11 11:03:27 -04:00
|
|
|
("wasm://wasm/d1c677ea", "wasm/wasm/d1c677ea"),
|
2019-08-11 19:12:09 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
test_cases.push(("file:///D:/a/1/s/format.ts", "file/D/a/1/s/format.ts"));
|
2020-06-26 13:25:13 -04:00
|
|
|
// IPv4 localhost
|
|
|
|
test_cases.push((
|
|
|
|
"file://127.0.0.1/d$/a/1/s/format.ts",
|
|
|
|
"file/UNC/127.0.0.1/d$/a/1/s/format.ts",
|
|
|
|
));
|
|
|
|
// IPv6 localhost
|
|
|
|
test_cases.push((
|
|
|
|
"file://[0:0:0:0:0:0:0:1]/d$/a/1/s/format.ts",
|
|
|
|
"file/UNC/[__1]/d$/a/1/s/format.ts",
|
|
|
|
));
|
|
|
|
// shared folder
|
|
|
|
test_cases.push((
|
|
|
|
"file://comp/t-share/a/1/s/format.ts",
|
|
|
|
"file/UNC/comp/t-share/a/1/s/format.ts",
|
|
|
|
));
|
2019-08-11 19:12:09 -04:00
|
|
|
} else {
|
|
|
|
test_cases.push((
|
2019-07-17 18:15:30 -04:00
|
|
|
"file:///std/http/file_server.ts",
|
|
|
|
"file/std/http/file_server.ts",
|
2019-08-11 19:12:09 -04:00
|
|
|
));
|
|
|
|
}
|
2019-07-17 18:15:30 -04:00
|
|
|
|
|
|
|
for test_case in &test_cases {
|
2019-08-11 19:12:09 -04:00
|
|
|
let cache_filename =
|
|
|
|
cache.get_cache_filename(&Url::parse(test_case.0).unwrap());
|
2020-12-15 00:52:55 -05:00
|
|
|
assert_eq!(cache_filename, Some(PathBuf::from(test_case.1)));
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_cache_filename_with_extension() {
|
2020-05-15 10:32:52 -04:00
|
|
|
let p = if cfg!(target_os = "windows") {
|
|
|
|
"C:\\foo"
|
|
|
|
} else {
|
|
|
|
"/foo"
|
|
|
|
};
|
|
|
|
let cache = DiskCache::new(&PathBuf::from(p));
|
2019-07-17 18:15:30 -04:00
|
|
|
|
2019-08-11 19:12:09 -04:00
|
|
|
let mut test_cases = vec![
|
2019-07-17 18:15:30 -04:00
|
|
|
(
|
|
|
|
"http://deno.land/std/http/file_server.ts",
|
|
|
|
"js",
|
2020-08-03 08:55:03 -04:00
|
|
|
"http/deno.land/d8300752800fe3f0beda9505dc1c3b5388beb1ee45afd1f1e2c9fc0866df15cf.js",
|
2019-07-17 18:15:30 -04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"http://deno.land/std/http/file_server.ts",
|
|
|
|
"js.map",
|
2020-08-03 08:55:03 -04:00
|
|
|
"http/deno.land/d8300752800fe3f0beda9505dc1c3b5388beb1ee45afd1f1e2c9fc0866df15cf.js.map",
|
2019-07-17 18:15:30 -04:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2019-08-11 19:12:09 -04:00
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
test_cases.push((
|
|
|
|
"file:///D:/std/http/file_server",
|
|
|
|
"js",
|
|
|
|
"file/D/std/http/file_server.js",
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
test_cases.push((
|
|
|
|
"file:///std/http/file_server",
|
|
|
|
"js",
|
|
|
|
"file/std/http/file_server.js",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:15:30 -04:00
|
|
|
for test_case in &test_cases {
|
|
|
|
assert_eq!(
|
|
|
|
cache.get_cache_filename_with_extension(
|
|
|
|
&Url::parse(test_case.0).unwrap(),
|
|
|
|
test_case.1
|
|
|
|
),
|
2020-12-15 00:52:55 -05:00
|
|
|
Some(PathBuf::from(test_case.2))
|
2019-07-17 18:15:30 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 04:33:20 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_cache_filename_invalid_urls() {
|
|
|
|
let cache_location = if cfg!(target_os = "windows") {
|
|
|
|
PathBuf::from(r"C:\deno_dir\")
|
|
|
|
} else {
|
|
|
|
PathBuf::from("/deno_dir/")
|
|
|
|
};
|
|
|
|
|
|
|
|
let cache = DiskCache::new(&cache_location);
|
|
|
|
|
|
|
|
let mut test_cases = vec!["unknown://localhost/test.ts"];
|
|
|
|
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
test_cases.push("file://");
|
|
|
|
test_cases.push("file:///");
|
|
|
|
}
|
|
|
|
|
|
|
|
for test_case in &test_cases {
|
|
|
|
let cache_filename =
|
|
|
|
cache.get_cache_filename(&Url::parse(test_case).unwrap());
|
|
|
|
assert_eq!(cache_filename, None);
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|