Skip to content

Commit

Permalink
Forcefully use map to address future problems
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Oct 1, 2018
1 parent 02fbf23 commit 9da5589
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
12 changes: 6 additions & 6 deletions src/deno_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
65 changes: 29 additions & 36 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,51 +29,43 @@ pub fn get_client() -> Client<Connector, hyper::Body> {
Client::builder().build(c)
}

struct FetchedBodyFuture {
body: Concat2<hyper::Body>,
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<Async<FetchedBody>, 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<hyper::Body>,
) -> impl Future<Item = String, Error = HyperOrIOError> {
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
// synchronous response, this utility method supports that.
pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> {
let url = module_name.parse::<Uri>().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.
Expand Down
1 change: 1 addition & 0 deletions tests/015_import_no_ext.ts.out
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9da5589

Please sign in to comment.