diff options
| author | Ryan Dahl <ry@tinyclouds.org> | 2018-08-14 16:50:53 -0400 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2018-08-23 09:41:08 -0400 |
| commit | e2f9b0e6fd01c4ad8d7fc966a531e48e5aaa334f (patch) | |
| tree | 8d32626de78163586ca5888d7ea534e7c7861214 /src/net.rs | |
| parent | 242e68e50c0ce6890c4b7812a751656b5d43d641 (diff) | |
First pass at HTTP imports
Implement --reload
Integrate hyper errors into DenoError
In collaboration with Tommy Savaria <tommy.savaria@protonmail.ch>
Diffstat (limited to 'src/net.rs')
| -rw-r--r-- | src/net.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/net.rs b/src/net.rs new file mode 100644 index 000000000..fc0df3fa2 --- /dev/null +++ b/src/net.rs @@ -0,0 +1,29 @@ +use errors::DenoResult; +use hyper::rt::{Future, Stream}; +use hyper::{Client, Uri}; +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<String> { + let url = module_name.parse::<Uri>().unwrap(); + let client = Client::new(); + + // 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); +} |
