Skip to content

Commit

Permalink
Use single block_on and add import failure test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Sep 30, 2018
1 parent c4f5adb commit 02fbf23
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.

use errors::DenoResult;
use errors::{DenoError, DenoResult};
use futures::stream::Concat2;
use futures::{Async, Future};
use tokio_util;

// use futures::Future;
use futures::Stream;
use hyper;
use hyper::client::Client;
Expand All @@ -27,21 +28,51 @@ pub fn get_client() -> Client<Connector, hyper::Body> {
Client::builder().build(c)
}

struct FetchedBodyFuture {
body: Concat2<hyper::Body>,
status: hyper::StatusCode,
}

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),
}
}
}

// 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 get_future = client.get(url);
let response = tokio_util::block_on(get_future)?;
if !response.status().is_success() {
Err(io::Error::new(
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 body = tokio_util::block_on(response.into_body().concat2())?;
Ok(String::from_utf8(body.to_vec()).unwrap())
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
File renamed without changes.
1 change: 1 addition & 0 deletions tests/error_006_import_ext_failure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./non-existent";
11 changes: 11 additions & 0 deletions tests/error_006_import_ext_failure.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NotFound: Cannot resolve module "./non-existent" from "[WILDCARD]deno/tests/error_006_import_ext_failure.ts"
at maybeError (deno/js/errors.ts:[WILDCARD])
at maybeThrowError (deno/js/errors.ts:[WILDCARD])
at sendSync (deno/js/dispatch.ts:[WILDCARD])
at Object.codeFetch (deno/js/os.ts:[WILDCARD])
at DenoCompiler.resolveModule (deno/js/compiler.ts:[WILDCARD])
at DenoCompiler._resolveModuleName (deno/js/compiler.ts:[WILDCARD])
at moduleNames.map.name (deno/js/compiler.ts:[WILDCARD])
at Array.map (<anonymous>)
at DenoCompiler.resolveModuleNames (deno/js/compiler.ts:[WILDCARD])
at Object.compilerHost.resolveModuleNames (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])

0 comments on commit 02fbf23

Please sign in to comment.