diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-09-30 16:51:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-30 10:51:01 -0400 |
commit | 290da280a8a8ab8f886462cd61484f5a6a4d57a4 (patch) | |
tree | 473d3f8f3cef55b6167ef983de33a8f0d97f01ba /op_crates/fetch/lib.rs | |
parent | dcd0595058c9a736529c11e64cf13911774be2b5 (diff) |
refactor: improve op crate interfaces for other consumers (#7745)
Diffstat (limited to 'op_crates/fetch/lib.rs')
-rw-r--r-- | op_crates/fetch/lib.rs | 60 |
1 files changed, 38 insertions, 22 deletions
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 { |