From 02fbf2394b7fbe4388694c3c8f5f317f10254d61 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Sun, 30 Sep 2018 15:57:33 -0700 Subject: [PATCH] Use single block_on and add import failure test --- src/http.rs | 49 +++++++++++++++---- ...rt_no_ext.out => 015_import_no_ext.ts.out} | 0 tests/error_006_import_ext_failure.ts | 1 + tests/error_006_import_ext_failure.ts.out | 11 +++++ 4 files changed, 52 insertions(+), 9 deletions(-) rename tests/{015_import_no_ext.out => 015_import_no_ext.ts.out} (100%) create mode 100644 tests/error_006_import_ext_failure.ts create mode 100644 tests/error_006_import_ext_failure.ts.out diff --git a/src/http.rs b/src/http.rs index cbb1ba9b114d59..182a9ff11c4bf9 100644 --- a/src/http.rs +++ b/src/http.rs @@ -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; @@ -27,21 +28,51 @@ pub fn get_client() -> Client { Client::builder().build(c) } +struct FetchedBodyFuture { + body: Concat2, + 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, 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 { let url = module_name.parse::().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. diff --git a/tests/015_import_no_ext.out b/tests/015_import_no_ext.ts.out similarity index 100% rename from tests/015_import_no_ext.out rename to tests/015_import_no_ext.ts.out diff --git a/tests/error_006_import_ext_failure.ts b/tests/error_006_import_ext_failure.ts new file mode 100644 index 00000000000000..3c32303a3456cf --- /dev/null +++ b/tests/error_006_import_ext_failure.ts @@ -0,0 +1 @@ +import "./non-existent"; diff --git a/tests/error_006_import_ext_failure.ts.out b/tests/error_006_import_ext_failure.ts.out new file mode 100644 index 00000000000000..30c9965da7d664 --- /dev/null +++ b/tests/error_006_import_ext_failure.ts.out @@ -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 () + at DenoCompiler.resolveModuleNames (deno/js/compiler.ts:[WILDCARD]) + at Object.compilerHost.resolveModuleNames (deno/third_party/node_modules/typescript/lib/typescript.js:[WILDCARD])