diff options
Diffstat (limited to 'op_crates/fetch')
-rw-r--r-- | op_crates/fetch/Cargo.toml | 2 | ||||
-rw-r--r-- | op_crates/fetch/lib.rs | 60 |
2 files changed, 39 insertions, 23 deletions
diff --git a/op_crates/fetch/Cargo.toml b/op_crates/fetch/Cargo.toml index feece79d5..9bb9a4a76 100644 --- a/op_crates/fetch/Cargo.toml +++ b/op_crates/fetch/Cargo.toml @@ -2,7 +2,7 @@ [package] name = "deno_fetch" -version = "0.2.0" +version = "0.3.0" edition = "2018" description = "provides fetch Web API to deno_core" authors = ["the Deno authors"] diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs index 5a314f752..49d951d8f 100644 --- a/op_crates/fetch/lib.rs +++ b/op_crates/fetch/lib.rs @@ -27,40 +27,56 @@ use std::cell::RefCell; use std::convert::From; use std::fs::File; use std::io::Read; -use std::path::Path; use std::path::PathBuf; use std::rc::Rc; pub use reqwest; // Re-export reqwest +/// Execute this crates' JS source files. pub fn init(isolate: &mut JsRuntime) { - let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); let files = vec![ - manifest_dir.join("01_fetch_util.js"), - manifest_dir.join("03_dom_iterable.js"), - manifest_dir.join("11_streams.js"), - manifest_dir.join("20_headers.js"), - manifest_dir.join("26_fetch.js"), + ( + "deno:op_crates/fetch/01_fetch_util.js", + include_str!("01_fetch_util.js"), + ), + ( + "deno:op_crates/fetch/03_dom_iterable.js", + include_str!("03_dom_iterable.js"), + ), + ( + "deno:op_crates/fetch/11_streams.js", + include_str!("11_streams.js"), + ), + ( + "deno:op_crates/fetch/20_headers.js", + include_str!("20_headers.js"), + ), + ( + "deno:op_crates/fetch/26_fetch.js", + include_str!("26_fetch.js"), + ), ]; - // TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the - // workspace root. - let display_root = manifest_dir.parent().unwrap().parent().unwrap(); - for file in files { - println!("cargo:rerun-if-changed={}", file.display()); - let display_path = file.strip_prefix(display_root).unwrap(); - let display_path_str = display_path.display().to_string(); - isolate - .execute( - &("deno:".to_string() + &display_path_str.replace('\\', "/")), - &std::fs::read_to_string(&file).unwrap(), - ) - .unwrap(); + for (url, source_code) in files { + isolate.execute(url, source_code).unwrap(); } } pub trait FetchPermissions { - fn check_net_url(&self, url: &Url) -> Result<(), AnyError>; - fn check_read(&self, p: &PathBuf) -> Result<(), AnyError>; + fn check_net_url(&self, _url: &Url) -> Result<(), AnyError>; + fn check_read(&self, _p: &PathBuf) -> Result<(), AnyError>; +} + +/// For use with `op_fetch` when the user does not want permissions. +pub struct NoFetchPermissions; + +impl FetchPermissions for NoFetchPermissions { + fn check_net_url(&self, _url: &Url) -> Result<(), AnyError> { + Ok(()) + } + + fn check_read(&self, _p: &PathBuf) -> Result<(), AnyError> { + Ok(()) + } } pub fn get_declaration() -> PathBuf { |