From 9da5589ae353e6b0560783fcddd4cc9c62f8067c Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Mon, 1 Oct 2018 12:06:09 -0700 Subject: [PATCH] Forcefully use map to address future problems --- src/deno_dir.rs | 12 +++---- src/http.rs | 65 +++++++++++++++------------------- tests/015_import_no_ext.ts.out | 1 + 3 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/deno_dir.rs b/src/deno_dir.rs index c75a7685f0b896..fea4bea38f19f7 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -138,7 +138,7 @@ impl DenoDir { panic!("Asset resolution should be done in JS, not Rust."); } let is_module_remote = is_remote(module_name); - let try_extension = |ext| { + let use_extension = |ext| { let module_name = format!("{}{}", module_name, ext); let filename = format!("{}{}", filename, ext); let source_code = if is_module_remote { @@ -157,17 +157,17 @@ impl DenoDir { maybe_output_code: None, }); }; - // Has extension, no guessing required - if module_name.ends_with(".ts") || module_name.ends_with(".js") { - return try_extension(""); + let default_attempt = use_extension(""); + if let Ok(_) = default_attempt { + return default_attempt; } debug!("Trying {}.ts...", module_name); - let ts_attempt = try_extension(".ts"); + let ts_attempt = use_extension(".ts"); if let Ok(_) = ts_attempt { return ts_attempt; } debug!("Trying {}.js...", module_name); - try_extension(".js") + use_extension(".js") } pub fn code_fetch( diff --git a/src/http.rs b/src/http.rs index 182a9ff11c4bf9..985e6dbda7143e 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,8 +1,9 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. use errors::{DenoError, DenoResult}; -use futures::stream::Concat2; -use futures::{Async, Future}; +use futures; +use futures::future::Either; +use futures::Future; use tokio_util; use futures::Stream; @@ -28,28 +29,27 @@ pub fn get_client() -> Client { Client::builder().build(c) } -struct FetchedBodyFuture { - body: Concat2, - status: hyper::StatusCode, +enum HyperOrIOError { + IO(io::Error), + Hyper(hyper::Error), } -struct FetchedBody { - body: hyper::Chunk, - status: hyper::StatusCode, -} - -impl Future for FetchedBodyFuture { - type Item = FetchedBody; - type Error = hyper::Error; - fn poll(&mut self) -> Result, hyper::Error> { - match self.body.poll()? { - Async::Ready(body) => Ok(Async::Ready(FetchedBody { - body, - status: self.status.clone(), - })), - Async::NotReady => Ok(Async::NotReady), - } +fn response_future( + response: hyper::Response, +) -> impl Future { + if !response.status().is_success() { + return Either::A(futures::future::err(HyperOrIOError::IO(io::Error::new( + io::ErrorKind::NotFound, + format!("module not found"), + )))); } + Either::B( + response + .into_body() + .concat2() + .map(|body| String::from_utf8(body.to_vec()).unwrap()) + .map_err(|err| HyperOrIOError::Hyper(err)), + ) } // The CodeFetch message is used to load HTTP javascript resources and expects a @@ -57,22 +57,15 @@ impl Future for FetchedBodyFuture { pub fn fetch_sync_string(module_name: &str) -> DenoResult { let url = module_name.parse::().unwrap(); let client = get_client(); - let fetch_future = client.get(url).and_then(|response| { - let status = response.status(); - FetchedBodyFuture { - body: response.into_body().concat2(), - status, - } - }); - - let fetch_result = tokio_util::block_on(fetch_future)?; - if !fetch_result.status.is_success() { - return Err(DenoError::from(io::Error::new( - io::ErrorKind::NotFound, - format!("cannot load from '{}'", module_name), - ))); + let fetch_future = client + .get(url) + .map_err(|err| HyperOrIOError::Hyper(err)) + .and_then(response_future); + match tokio_util::block_on(fetch_future) { + Ok(s) => Ok(s), + Err(HyperOrIOError::Hyper(err)) => Err(DenoError::from(err)), + Err(HyperOrIOError::IO(err)) => Err(DenoError::from(err)), } - Ok(String::from_utf8(fetch_result.body.to_vec()).unwrap()) } /* TODO(ry) Re-enabled this test. Disabling to work around bug in #782. diff --git a/tests/015_import_no_ext.ts.out b/tests/015_import_no_ext.ts.out index 018eb4338a09e2..92f21e2a7a881d 100644 --- a/tests/015_import_no_ext.ts.out +++ b/tests/015_import_no_ext.ts.out @@ -1,3 +1,4 @@ +Downloading http://localhost:4545/tests/subdir/mod2 Downloading http://localhost:4545/tests/subdir/mod2.ts Downloading http://localhost:4545/tests/subdir/print_hello.ts true