From 7c128df4a041f3b2c04725a0f5f3320db684d067 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 24 Sep 2018 19:51:37 -0400 Subject: Use lazy_static for HttpsConnector And rename net.rs to http.rs Share HTTP connection. --- BUILD.gn | 1 + src/deno_dir.rs | 4 ++-- src/handlers.rs | 2 +- src/http.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 +++- src/net.rs | 33 --------------------------------- 6 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 src/http.rs delete mode 100644 src/net.rs diff --git a/BUILD.gn b/BUILD.gn index dd995c00f..4a7293262 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -47,6 +47,7 @@ main_extern = [ "$rust_build:hyper", "$rust_build:hyper_rustls", "$rust_build:futures", + "$rust_build:lazy_static", "$rust_build:libc", "$rust_build:log", "$rust_build:ring", diff --git a/src/deno_dir.rs b/src/deno_dir.rs index cdebf05c3..cb769fb86 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -4,7 +4,7 @@ use errors::DenoError; use errors::DenoResult; use errors::ErrorKind; use fs as deno_fs; -use net; +use http; use ring; use std; use std::fmt::Write; @@ -114,7 +114,7 @@ impl DenoDir { let src = if self.reload || !p.exists() { println!("Downloading {}", module_name); - let source = net::fetch_sync_string(module_name)?; + let source = http::fetch_sync_string(module_name)?; match p.parent() { Some(ref parent) => fs::create_dir_all(parent), None => Ok(()), diff --git a/src/handlers.rs b/src/handlers.rs index 964baabf8..623c64110 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -147,7 +147,7 @@ fn permission_denied() -> DenoError { fn not_implemented() -> DenoError { DenoError::from(std::io::Error::new( std::io::ErrorKind::Other, - "Not implemented" + "Not implemented", )) } diff --git a/src/http.rs b/src/http.rs new file mode 100644 index 000000000..5907b35ed --- /dev/null +++ b/src/http.rs @@ -0,0 +1,49 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. + +use errors::DenoResult; + +use futures::Future; +use futures::Stream; +use hyper; +use hyper::client::HttpConnector; +use hyper::Uri; +use hyper_rustls; + +type Connector = hyper_rustls::HttpsConnector; + +lazy_static! { + static ref CONNECTOR: Connector = { + let num_dns_threads = 4; + Connector::new(num_dns_threads) + }; +} + +pub fn get_client() -> Client { + // TODO use Hyper's connection pool. + let c = CONNECTOR.clone(); + Client::builder().build(c) +} + +// 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(); + + // TODO Use Deno's RT + let mut rt = Runtime::new().unwrap(); + let body = rt.block_on( + client + .get(url) + .and_then(|response| response.into_body().concat2()), + )?; + Ok(String::from_utf8(body.to_vec()).unwrap()) +} + +#[test] +fn test_fetch_sync_string() { + // Relies on external http server. See tools/http_server.py + let p = fetch_sync_string("http://localhost:4545/package.json").unwrap(); + println!("package.json len {}", p.len()); + assert!(p.len() > 1); +} diff --git a/src/main.rs b/src/main.rs index 8cf29dd37..1f9ead019 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ extern crate tempfile; extern crate tokio; extern crate url; #[macro_use] +extern crate lazy_static; +#[macro_use] extern crate log; extern crate dirs; extern crate hyper_rustls; @@ -20,9 +22,9 @@ mod errors; mod flags; mod fs; pub mod handlers; +mod http; mod isolate; mod libdeno; -mod net; mod version; use isolate::Isolate; diff --git a/src/net.rs b/src/net.rs deleted file mode 100644 index 7e0700bb6..000000000 --- a/src/net.rs +++ /dev/null @@ -1,33 +0,0 @@ -use errors::DenoResult; -use hyper; -use hyper::rt::{Future, Stream}; -use hyper::{Client, Uri}; -use hyper_rustls; -use tokio::runtime::current_thread::Runtime; - -// 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 https = hyper_rustls::HttpsConnector::new(4); - let client: Client<_, hyper::Body> = Client::builder().build(https); - - // TODO Use Deno's RT - let mut rt = Runtime::new().unwrap(); - - let body = rt.block_on( - client - .get(url) - .and_then(|response| response.into_body().concat2()), - )?; - Ok(String::from_utf8(body.to_vec()).unwrap()) -} - -#[test] -fn test_fetch_sync_string() { - // Relies on external http server. See tools/http_server.py - let p = fetch_sync_string("http://localhost:4545/package.json").unwrap(); - println!("package.json len {}", p.len()); - assert!(p.len() > 1); -} -- cgit v1.2.3