summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-09-30 16:51:01 +0200
committerGitHub <noreply@github.com>2020-09-30 10:51:01 -0400
commit290da280a8a8ab8f886462cd61484f5a6a4d57a4 (patch)
tree473d3f8f3cef55b6167ef983de33a8f0d97f01ba
parentdcd0595058c9a736529c11e64cf13911774be2b5 (diff)
refactor: improve op crate interfaces for other consumers (#7745)
-rw-r--r--Cargo.lock4
-rw-r--r--cli/Cargo.toml8
-rw-r--r--op_crates/fetch/Cargo.toml2
-rw-r--r--op_crates/fetch/lib.rs60
-rw-r--r--op_crates/web/Cargo.toml2
-rw-r--r--op_crates/web/lib.rs46
6 files changed, 71 insertions, 51 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 452f3f835..90082aa0d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -483,7 +483,7 @@ dependencies = [
[[package]]
name = "deno_fetch"
-version = "0.2.0"
+version = "0.3.0"
dependencies = [
"deno_core",
"reqwest",
@@ -508,7 +508,7 @@ dependencies = [
[[package]]
name = "deno_web"
-version = "0.10.0"
+version = "0.11.0"
dependencies = [
"deno_core",
"futures",
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 6150fa421..9cbe4c16a 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -21,8 +21,8 @@ path = "./bench/main.rs"
[build-dependencies]
deno_core = { path = "../core", version = "0.59.0" }
-deno_web = { path = "../op_crates/web", version = "0.10.0" }
-deno_fetch = { path = "../op_crates/fetch", version = "0.2.0" }
+deno_web = { path = "../op_crates/web", version = "0.11.0" }
+deno_fetch = { path = "../op_crates/fetch", version = "0.3.0" }
[target.'cfg(windows)'.build-dependencies]
winres = "0.1.11"
@@ -32,8 +32,8 @@ winapi = "0.3.9"
deno_core = { path = "../core", version = "0.59.0" }
deno_doc = "0.1.9"
deno_lint = { version = "0.2.0", features = ["json"] }
-deno_web = { path = "../op_crates/web", version = "0.10.0" }
-deno_fetch = { path = "../op_crates/fetch", version = "0.2.0" }
+deno_web = { path = "../op_crates/web", version = "0.11.0" }
+deno_fetch = { path = "../op_crates/fetch", version = "0.3.0" }
atty = "0.2.14"
base64 = "0.12.3"
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 {
diff --git a/op_crates/web/Cargo.toml b/op_crates/web/Cargo.toml
index 31cec21e6..f3f278ffa 100644
--- a/op_crates/web/Cargo.toml
+++ b/op_crates/web/Cargo.toml
@@ -2,7 +2,7 @@
[package]
name = "deno_web"
-version = "0.10.0"
+version = "0.11.0"
edition = "2018"
description = "Collection of Web APIs"
authors = ["the Deno authors"]
diff --git a/op_crates/web/lib.rs b/op_crates/web/lib.rs
index 1078a7917..26e36365b 100644
--- a/op_crates/web/lib.rs
+++ b/op_crates/web/lib.rs
@@ -10,7 +10,7 @@ use deno_core::ZeroCopyBuf;
use idna::domain_to_ascii;
use idna::domain_to_ascii_strict;
use serde::Deserialize;
-use std::path::{Path, PathBuf};
+use std::path::PathBuf;
pub fn op_domain_to_ascii(
_state: &mut deno_core::OpState,
@@ -37,29 +37,33 @@ pub fn op_domain_to_ascii(
.map(|domain| json!(domain))
}
+/// Load and execute the javascript code.
pub fn init(isolate: &mut JsRuntime) {
- let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let files = vec![
- manifest_dir.join("00_dom_exception.js"),
- manifest_dir.join("01_event.js"),
- manifest_dir.join("02_abort_signal.js"),
- manifest_dir.join("08_text_encoding.js"),
- manifest_dir.join("11_url.js"),
- manifest_dir.join("21_filereader.js"),
+ (
+ "deno:op_crates/web/00_dom_exception.js",
+ include_str!("00_dom_exception.js"),
+ ),
+ (
+ "deno:op_crates/web/01_event.js",
+ include_str!("01_event.js"),
+ ),
+ (
+ "deno:op_crates/web/02_abort_signal.js",
+ include_str!("02_abort_signal.js"),
+ ),
+ (
+ "deno:op_crates/web/08_text_encoding.js",
+ include_str!("08_text_encoding.js"),
+ ),
+ ("deno:op_crates/web/11_url.js", include_str!("11_url.js")),
+ (
+ "deno:op_crates/web/21_filereader.js",
+ include_str!("21_filereader.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();
}
}