Skip to content

Commit 8500b78

Browse files
kevinkassimory
authored andcommitted
Add separate http/https cache dirs to DENO_DIR (#971)
Also change remote relative import logic.
1 parent dfe21af commit 8500b78

File tree

1 file changed

+94
-12
lines changed

1 file changed

+94
-12
lines changed

src/deno_dir.rs

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub struct DenoDir {
2929
// This is where we cache compilation outputs. Example:
3030
// /Users/rld/.deno/gen/f39a473452321cacd7c346a870efb0e3e1264b43.js
3131
pub deps: PathBuf,
32+
// This splits to http and https deps
33+
pub deps_http: PathBuf,
34+
pub deps_https: PathBuf,
3235
// If remote resources should be reloaded.
3336
reload: bool,
3437
}
@@ -50,19 +53,27 @@ impl DenoDir {
5053
};
5154
let gen = root.as_path().join("gen");
5255
let deps = root.as_path().join("deps");
56+
let deps_http = deps.join("http");
57+
let deps_https = deps.join("https");
5358

5459
let deno_dir = DenoDir {
5560
root,
5661
gen,
5762
deps,
63+
deps_http,
64+
deps_https,
5865
reload,
5966
};
6067
deno_fs::mkdir(deno_dir.gen.as_ref(), 0o755)?;
6168
deno_fs::mkdir(deno_dir.deps.as_ref(), 0o755)?;
69+
deno_fs::mkdir(deno_dir.deps_http.as_ref(), 0o755)?;
70+
deno_fs::mkdir(deno_dir.deps_https.as_ref(), 0o755)?;
6271

6372
debug!("root {}", deno_dir.root.display());
6473
debug!("gen {}", deno_dir.gen.display());
6574
debug!("deps {}", deno_dir.deps.display());
75+
debug!("deps_http {}", deno_dir.deps_http.display());
76+
debug!("deps_https {}", deno_dir.deps_https.display());
6677

6778
Ok(deno_dir)
6879
}
@@ -236,13 +247,24 @@ impl DenoDir {
236247
fn src_file_to_url(self: &DenoDir, filename: &str) -> String {
237248
let filename_path = Path::new(filename);
238249
if filename_path.starts_with(&self.deps) {
239-
let rest = filename_path.strip_prefix(&self.deps).unwrap();
250+
let (rest, prefix) = if filename_path.starts_with(&self.deps_https) {
251+
let rest = filename_path.strip_prefix(&self.deps_https).unwrap();
252+
let prefix = "https://".to_string();
253+
(rest, prefix)
254+
} else if filename_path.starts_with(&self.deps_http) {
255+
let rest = filename_path.strip_prefix(&self.deps_http).unwrap();
256+
let prefix = "http://".to_string();
257+
(rest, prefix)
258+
} else {
259+
// TODO(kevinkassimo): change this to support other protocols than http
260+
unimplemented!()
261+
};
240262
// Windows doesn't support ":" in filenames, so we represent port using a
241263
// special string.
242264
// TODO(ry) This current implementation will break on a URL that has
243265
// the default port but contains "_PORT" in the path.
244266
let rest = rest.to_str().unwrap().replacen("_PORT", ":", 1);
245-
"http://".to_string() + &rest
267+
prefix + &rest
246268
} else {
247269
String::from(filename)
248270
}
@@ -290,12 +312,20 @@ impl DenoDir {
290312
module_name = p.clone();
291313
filename = p;
292314
}
293-
_ => {
315+
"https" => {
316+
module_name = j.to_string();
317+
filename = deno_fs::normalize_path(
318+
get_cache_filename(self.deps_https.as_path(), j).as_ref(),
319+
)
320+
}
321+
"http" => {
294322
module_name = j.to_string();
295323
filename = deno_fs::normalize_path(
296-
get_cache_filename(self.deps.as_path(), j).as_ref(),
324+
get_cache_filename(self.deps_http.as_path(), j).as_ref(),
297325
)
298326
}
327+
// TODO(kevinkassimo): change this to support other protocols than http
328+
_ => unimplemented!(),
299329
}
300330

301331
debug!("module_name: {}, filename: {}", module_name, filename);
@@ -492,7 +522,7 @@ fn test_src_file_to_url_1() {
492522
let (_temp_dir, deno_dir) = test_setup();
493523
assert_eq!("hello", deno_dir.src_file_to_url("hello"));
494524
assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
495-
let x = deno_dir.deps.join("hello/world.txt");
525+
let x = deno_dir.deps_http.join("hello/world.txt");
496526
assert_eq!(
497527
"http://hello/world.txt",
498528
deno_dir.src_file_to_url(x.to_str().unwrap())
@@ -502,13 +532,35 @@ fn test_src_file_to_url_1() {
502532
#[test]
503533
fn test_src_file_to_url_2() {
504534
let (_temp_dir, deno_dir) = test_setup();
505-
let x = deno_dir.deps.join("localhost_PORT4545/world.txt");
535+
assert_eq!("hello", deno_dir.src_file_to_url("hello"));
536+
assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
537+
let x = deno_dir.deps_https.join("hello/world.txt");
538+
assert_eq!(
539+
"https://hello/world.txt",
540+
deno_dir.src_file_to_url(x.to_str().unwrap())
541+
);
542+
}
543+
544+
#[test]
545+
fn test_src_file_to_url_3() {
546+
let (_temp_dir, deno_dir) = test_setup();
547+
let x = deno_dir.deps_http.join("localhost_PORT4545/world.txt");
506548
assert_eq!(
507549
"http://localhost:4545/world.txt",
508550
deno_dir.src_file_to_url(x.to_str().unwrap())
509551
);
510552
}
511553

554+
#[test]
555+
fn test_src_file_to_url_4() {
556+
let (_temp_dir, deno_dir) = test_setup();
557+
let x = deno_dir.deps_https.join("localhost_PORT4545/world.txt");
558+
assert_eq!(
559+
"https://localhost:4545/world.txt",
560+
deno_dir.src_file_to_url(x.to_str().unwrap())
561+
);
562+
}
563+
512564
// https://github.com/denoland/deno/blob/golang/os_test.go#L16-L87
513565
#[test]
514566
fn test_resolve_module_1() {
@@ -568,7 +620,7 @@ fn test_resolve_module_2() {
568620
"http://localhost:4545/testdata/subdir/print_hello.ts";
569621
let expected_filename = deno_fs::normalize_path(
570622
deno_dir
571-
.deps
623+
.deps_http
572624
.join("localhost_PORT4545/testdata/subdir/print_hello.ts")
573625
.as_ref(),
574626
);
@@ -585,14 +637,14 @@ fn test_resolve_module_3() {
585637
let (_temp_dir, deno_dir) = test_setup();
586638

587639
let module_specifier_ =
588-
deno_dir.deps.join("unpkg.com/[email protected]/index.ts");
640+
deno_dir.deps_http.join("unpkg.com/[email protected]/index.ts");
589641
let module_specifier = module_specifier_.to_str().unwrap();
590642
let containing_file = ".";
591643

592644
let expected_module_name = "http://unpkg.com/[email protected]/index.ts";
593645
let expected_filename = deno_fs::normalize_path(
594646
deno_dir
595-
.deps
647+
.deps_http
596648
.join("unpkg.com/[email protected]/index.ts")
597649
.as_ref(),
598650
);
@@ -609,12 +661,17 @@ fn test_resolve_module_4() {
609661
let (_temp_dir, deno_dir) = test_setup();
610662

611663
let module_specifier = "./util";
612-
let containing_file_ = deno_dir.deps.join("unpkg.com/[email protected]/index.ts");
664+
let containing_file_ =
665+
deno_dir.deps_http.join("unpkg.com/[email protected]/index.ts");
613666
let containing_file = containing_file_.to_str().unwrap();
614667

668+
// http containing files -> load relative import with http
615669
let expected_module_name = "http://unpkg.com/[email protected]/util";
616670
let expected_filename = deno_fs::normalize_path(
617-
deno_dir.deps.join("unpkg.com/[email protected]/util").as_ref(),
671+
deno_dir
672+
.deps_http
673+
.join("unpkg.com/[email protected]/util")
674+
.as_ref(),
618675
);
619676

620677
let (module_name, filename) = deno_dir
@@ -628,12 +685,37 @@ fn test_resolve_module_4() {
628685
fn test_resolve_module_5() {
629686
let (_temp_dir, deno_dir) = test_setup();
630687

688+
let module_specifier = "./util";
689+
let containing_file_ =
690+
deno_dir.deps_https.join("unpkg.com/[email protected]/index.ts");
691+
let containing_file = containing_file_.to_str().unwrap();
692+
693+
// https containing files -> load relative import with https
694+
let expected_module_name = "https://unpkg.com/[email protected]/util";
695+
let expected_filename = deno_fs::normalize_path(
696+
deno_dir
697+
.deps_https
698+
.join("unpkg.com/[email protected]/util")
699+
.as_ref(),
700+
);
701+
702+
let (module_name, filename) = deno_dir
703+
.resolve_module(module_specifier, containing_file)
704+
.unwrap();
705+
assert_eq!(module_name, expected_module_name);
706+
assert_eq!(filename, expected_filename);
707+
}
708+
709+
#[test]
710+
fn test_resolve_module_6() {
711+
let (_temp_dir, deno_dir) = test_setup();
712+
631713
let module_specifier = "http://localhost:4545/tests/subdir/mod2.ts";
632714
let containing_file = add_root!("/deno/tests/006_url_imports.ts");
633715
let expected_module_name = "http://localhost:4545/tests/subdir/mod2.ts";
634716
let expected_filename = deno_fs::normalize_path(
635717
deno_dir
636-
.deps
718+
.deps_http
637719
.join("localhost_PORT4545/tests/subdir/mod2.ts")
638720
.as_ref(),
639721
);

0 commit comments

Comments
 (0)