diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2018-10-29 10:41:10 +1100 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-10-29 08:16:35 -0700 |
commit | 8b39d2c99ef41736bb1d5b74ccda2f3aa6223e84 (patch) | |
tree | b5598c6d1a8b3de147cf1b7b26e9f636731859d5 /src | |
parent | a68403d09006af3e7f75aacb9e818ce3bcf60b13 (diff) |
Separate source map from output code.
Diffstat (limited to 'src')
-rw-r--r-- | src/deno_dir.rs | 56 | ||||
-rw-r--r-- | src/msg.fbs | 4 | ||||
-rw-r--r-- | src/ops.rs | 17 |
3 files changed, 55 insertions, 22 deletions
diff --git a/src/deno_dir.rs b/src/deno_dir.rs index b0e9ba43c..1144bd0ab 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -83,19 +83,28 @@ impl DenoDir { self: &DenoDir, filename: &str, source_code: &str, - ) -> PathBuf { + ) -> (PathBuf, PathBuf) { let cache_key = source_code_hash(filename, source_code); - self.gen.join(cache_key + ".js") + ( + self.gen.join(cache_key.to_string() + ".js"), + self.gen.join(cache_key.to_string() + ".js.map"), + ) } fn load_cache( self: &DenoDir, filename: &str, source_code: &str, - ) -> std::io::Result<String> { - let path = self.cache_path(filename, source_code); - debug!("load_cache {}", path.display()); - fs::read_to_string(&path) + ) -> Result<(String, String), std::io::Error> { + let (output_code, source_map) = self.cache_path(filename, source_code); + debug!( + "load_cache code: {} map: {}", + output_code.display(), + source_map.display() + ); + let read_output_code = fs::read_to_string(&output_code)?; + let read_source_map = fs::read_to_string(&source_map)?; + Ok((read_output_code, read_source_map)) } pub fn code_cache( @@ -103,16 +112,19 @@ impl DenoDir { filename: &str, source_code: &str, output_code: &str, + source_map: &str, ) -> std::io::Result<()> { - let cache_path = self.cache_path(filename, source_code); + let (cache_path, source_map_path) = self.cache_path(filename, source_code); // 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. - if cache_path.exists() { + if cache_path.exists() && source_map_path.exists() { Ok(()) } else { - fs::write(cache_path, output_code.as_bytes()) + fs::write(cache_path, output_code.as_bytes())?; + fs::write(source_map_path, source_map.as_bytes())?; + Ok(()) } } @@ -176,6 +188,7 @@ impl DenoDir { media_type, source_code, maybe_output_code: None, + maybe_source_map: None, }); }; let default_attempt = use_extension(""); @@ -233,12 +246,13 @@ impl DenoDir { Err(err.into()) } } - Ok(output_code) => Ok(CodeFetchOutput { + Ok((output_code, source_map)) => Ok(CodeFetchOutput { module_name: out.module_name, filename: out.filename, media_type: out.media_type, source_code: out.source_code, maybe_output_code: Some(output_code), + maybe_source_map: Some(source_map), }), } } @@ -368,6 +382,7 @@ pub struct CodeFetchOutput { pub media_type: msg::MediaType, pub source_code: String, pub maybe_output_code: Option<String>, + pub maybe_source_map: Option<String>, } #[cfg(test)] @@ -382,9 +397,14 @@ pub fn test_setup() -> (TempDir, DenoDir) { fn test_cache_path() { let (temp_dir, deno_dir) = test_setup(); assert_eq!( - temp_dir - .path() - .join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js"), + ( + temp_dir + .path() + .join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js"), + temp_dir + .path() + .join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js.map") + ), deno_dir.cache_path("hello.ts", "1+2") ); } @@ -396,12 +416,18 @@ fn test_code_cache() { let filename = "hello.js"; let source_code = "1+2"; let output_code = "1+2 // output code"; - let cache_path = deno_dir.cache_path(filename, source_code); + let source_map = "{}"; + let (cache_path, source_map_path) = + deno_dir.cache_path(filename, source_code); assert!( cache_path.ends_with("gen/e8e3ee6bee4aef2ec63f6ec3db7fc5fdfae910ae.js") ); + assert!( + source_map_path + .ends_with("gen/e8e3ee6bee4aef2ec63f6ec3db7fc5fdfae910ae.js.map") + ); - let r = deno_dir.code_cache(filename, source_code, output_code); + let r = deno_dir.code_cache(filename, source_code, output_code, source_map); r.expect("code_cache error"); assert!(cache_path.exists()); assert_eq!(output_code, fs::read_to_string(&cache_path).unwrap()); diff --git a/src/msg.fbs b/src/msg.fbs index a3c9bcb84..e7ebe6684 100644 --- a/src/msg.fbs +++ b/src/msg.fbs @@ -146,14 +146,18 @@ table CodeFetchRes { module_name: string; filename: string; media_type: MediaType; + // TODO These should be [ubyte]. + // See: https://github.com/denoland/deno/issues/1113 source_code: string; output_code: string; // Non-empty only if cached. + source_map: string; // Non-empty only if cached. } table CodeCache { filename: string; source_code: string; output_code: string; + source_map: string; } table Chdir { diff --git a/src/ops.rs b/src/ops.rs index b7a20a46e..95522fb6b 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -257,12 +257,12 @@ fn op_code_fetch( source_code: Some(builder.create_string(&out.source_code)), ..Default::default() }; - match out.maybe_output_code { - Some(ref output_code) => { - msg_args.output_code = Some(builder.create_string(output_code)); - } - _ => (), - }; + if let Some(ref output_code) = out.maybe_output_code { + msg_args.output_code = Some(builder.create_string(output_code)); + } + if let Some(ref source_map) = out.maybe_source_map { + msg_args.source_map = Some(builder.create_string(source_map)); + } let inner = msg::CodeFetchRes::create(builder, &msg_args); Ok(serialize_response( cmd_id, @@ -287,8 +287,11 @@ fn op_code_cache( let filename = inner.filename().unwrap(); let source_code = inner.source_code().unwrap(); let output_code = inner.output_code().unwrap(); + let source_map = inner.source_map().unwrap(); Box::new(futures::future::result(|| -> OpResult { - state.dir.code_cache(filename, source_code, output_code)?; + state + .dir + .code_cache(filename, source_code, output_code, source_map)?; Ok(empty_buf()) }())) } |