summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2020-11-14 02:31:57 +0530
committerGitHub <noreply@github.com>2020-11-13 22:01:57 +0100
commitd5661f677e9f5675fc488c4629e85a58764ec3ff (patch)
tree6878ec81e2da2d41d93e3f429a189edcd4202539
parent2c8439bc1e8118225c8ba4d64658c1c6b2182937 (diff)
refactor: deno_crypto op crate (#7956)
This commit factors out "deno_crypto" op crate. "rand" crate dependency was consequently moved to "deno_crypto" crate and reexported.
-rw-r--r--.github/workflows/ci.yml3
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml1
-rw-r--r--cli/Cargo.toml3
-rw-r--r--cli/build.rs1
-rw-r--r--cli/ops/crypto.rs14
-rw-r--r--cli/ops/fs.rs4
-rw-r--r--cli/ops/mod.rs2
-rw-r--r--cli/worker.rs4
-rw-r--r--op_crates/crypto/01_crypto.js (renamed from cli/rt/11_crypto.js)13
-rw-r--r--op_crates/crypto/Cargo.toml19
-rw-r--r--op_crates/crypto/README.md3
-rw-r--r--op_crates/crypto/lib.rs (renamed from cli/ops/random.rs)23
13 files changed, 79 insertions, 21 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 68721a4d2..c7829529b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -223,6 +223,9 @@ jobs:
cd ../fetch
cargo publish
sleep 30
+ cd ../crypto
+ cargo publish
+ sleep 30
cd ../../cli
sleep 30
cargo publish
diff --git a/Cargo.lock b/Cargo.lock
index 882962755..ccae762f8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -417,6 +417,7 @@ dependencies = [
"chrono",
"clap",
"deno_core",
+ "deno_crypto",
"deno_doc",
"deno_fetch",
"deno_lint",
@@ -437,7 +438,6 @@ dependencies = [
"nix",
"notify",
"os_pipe",
- "rand 0.7.3",
"regex",
"ring",
"rustyline",
@@ -484,6 +484,14 @@ dependencies = [
]
[[package]]
+name = "deno_crypto"
+version = "0.1.1"
+dependencies = [
+ "deno_core",
+ "rand 0.7.3",
+]
+
+[[package]]
name = "deno_doc"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index eccd733d5..3281be1af 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ members = [
"test_util",
"op_crates/fetch",
"op_crates/web",
+ "op_crates/crypto"
]
exclude = [
"std/hash/_wasm"
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 8459139bf..8e389c23a 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -20,6 +20,7 @@ harness = false
path = "./bench/main.rs"
[build-dependencies]
+deno_crypto = { path = "../op_crates/crypto", version = "0.1.1" }
deno_core = { path = "../core", version = "0.67.0" }
deno_web = { path = "../op_crates/web", version = "0.18.0" }
deno_fetch = { path = "../op_crates/fetch", version = "0.10.0" }
@@ -31,6 +32,7 @@ winres = "0.1.11"
winapi = "0.3.9"
[dependencies]
+deno_crypto = { path = "../op_crates/crypto", version = "0.1.1" }
deno_core = { path = "../core", version = "0.67.0" }
deno_doc = "0.1.15"
deno_lint = "0.2.9"
@@ -55,7 +57,6 @@ libc = "0.2.77"
log = "0.4.11"
env_logger = "0.7.1"
notify = "5.0.0-pre.3"
-rand = "0.7.3"
regex = "1.3.9"
ring = "0.16.15"
rustyline = { version = "6.3.0", default-features = false }
diff --git a/cli/build.rs b/cli/build.rs
index 657041132..822ccd820 100644
--- a/cli/build.rs
+++ b/cli/build.rs
@@ -20,6 +20,7 @@ fn create_snapshot(
) {
deno_web::init(&mut js_runtime);
deno_fetch::init(&mut js_runtime);
+ deno_crypto::init(&mut js_runtime);
// TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
// workspace root.
let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
diff --git a/cli/ops/crypto.rs b/cli/ops/crypto.rs
new file mode 100644
index 000000000..a73843a33
--- /dev/null
+++ b/cli/ops/crypto.rs
@@ -0,0 +1,14 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use deno_crypto::op_get_random_values;
+use deno_crypto::rand::rngs::StdRng;
+use deno_crypto::rand::SeedableRng;
+
+pub fn init(rt: &mut deno_core::JsRuntime, maybe_seed: Option<u64>) {
+ if let Some(seed) = maybe_seed {
+ let rng = StdRng::seed_from_u64(seed);
+ let op_state = rt.op_state();
+ let mut state = op_state.borrow_mut();
+ state.put::<StdRng>(rng);
+ }
+ super::reg_json_sync(rt, "op_get_random_values", op_get_random_values);
+}
diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs
index 720a4db52..37558ec6b 100644
--- a/cli/ops/fs.rs
+++ b/cli/ops/fs.rs
@@ -13,8 +13,8 @@ use deno_core::serde_json::Value;
use deno_core::BufVec;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
-use rand::thread_rng;
-use rand::Rng;
+use deno_crypto::rand::thread_rng;
+use deno_crypto::rand::Rng;
use serde::Deserialize;
use std::cell::RefCell;
use std::convert::From;
diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs
index f5f42a2d7..b450f8989 100644
--- a/cli/ops/mod.rs
+++ b/cli/ops/mod.rs
@@ -3,6 +3,7 @@
mod dispatch_minimal;
pub use dispatch_minimal::MinimalOp;
+pub mod crypto;
pub mod errors;
pub mod fetch;
pub mod fs;
@@ -15,7 +16,6 @@ pub mod os;
pub mod permissions;
pub mod plugin;
pub mod process;
-pub mod random;
pub mod runtime;
pub mod runtime_compiler;
pub mod signal;
diff --git a/cli/worker.rs b/cli/worker.rs
index a09e96ce4..e27d6f73e 100644
--- a/cli/worker.rs
+++ b/cli/worker.rs
@@ -278,7 +278,7 @@ impl MainWorker {
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
ops::timers::init(js_runtime);
ops::worker_host::init(js_runtime, None);
- ops::random::init(js_runtime, program_state.flags.seed);
+ ops::crypto::init(js_runtime, program_state.flags.seed);
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
ops::reg_json_sync(
@@ -469,7 +469,7 @@ impl WebWorker {
ops::permissions::init(js_runtime);
ops::plugin::init(js_runtime);
ops::process::init(js_runtime);
- ops::random::init(js_runtime, program_state.flags.seed);
+ ops::crypto::init(js_runtime, program_state.flags.seed);
ops::runtime_compiler::init(js_runtime);
ops::signal::init(js_runtime);
ops::tls::init(js_runtime);
diff --git a/cli/rt/11_crypto.js b/op_crates/crypto/01_crypto.js
index 32b026a78..2f9424492 100644
--- a/cli/rt/11_crypto.js
+++ b/op_crates/crypto/01_crypto.js
@@ -2,11 +2,11 @@
((window) => {
const core = window.Deno.core;
- const { assert } = window.__bootstrap.util;
-
function getRandomValues(typedArray) {
- assert(typedArray !== null, "Input must not be null");
- assert(typedArray.length <= 65536, "Input must not be longer than 65536");
+ if (typedArray == null) throw new Error("Input must not be null");
+ if (typedArray.length > 65536) {
+ throw new Error("Input must not be longer than 65536");
+ }
const ui8 = new Uint8Array(
typedArray.buffer,
typedArray.byteOffset,
@@ -15,7 +15,10 @@
core.jsonOpSync("op_get_random_values", {}, ui8);
return typedArray;
}
-
+ window.crypto = {
+ getRandomValues,
+ };
+ window.__bootstrap = window.__bootstrap || {};
window.__bootstrap.crypto = {
getRandomValues,
};
diff --git a/op_crates/crypto/Cargo.toml b/op_crates/crypto/Cargo.toml
new file mode 100644
index 000000000..3bcaf7490
--- /dev/null
+++ b/op_crates/crypto/Cargo.toml
@@ -0,0 +1,19 @@
+# Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+[package]
+name = "deno_crypto"
+version = "0.1.1"
+edition = "2018"
+description = "Collection of WebCrypto APIs"
+authors = ["the Deno authors"]
+license = "MIT"
+readme = "README.md"
+repository = "https://github.com/denoland/deno"
+
+[lib]
+path = "lib.rs"
+
+[dependencies]
+deno_core = { version = "0.67.0", path = "../../core" }
+rand = "0.7.3"
+
diff --git a/op_crates/crypto/README.md b/op_crates/crypto/README.md
new file mode 100644
index 000000000..0e1c248e6
--- /dev/null
+++ b/op_crates/crypto/README.md
@@ -0,0 +1,3 @@
+# deno crypto
+
+Op crate that implements crypto functions.
diff --git a/cli/ops/random.rs b/op_crates/crypto/lib.rs
index 20296c667..a211a29b3 100644
--- a/cli/ops/random.rs
+++ b/op_crates/crypto/lib.rs
@@ -1,26 +1,31 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+#![deny(warnings)]
+
use deno_core::error::AnyError;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
+use deno_core::JsRuntime;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use rand::rngs::StdRng;
use rand::thread_rng;
use rand::Rng;
-use rand::SeedableRng;
-pub fn init(rt: &mut deno_core::JsRuntime, maybe_seed: Option<u64>) {
- if let Some(seed) = maybe_seed {
- let rng = StdRng::seed_from_u64(seed);
- let op_state = rt.op_state();
- let mut state = op_state.borrow_mut();
- state.put::<StdRng>(rng);
+pub use rand; // Re-export rand
+
+/// Execute this crates' JS source files.
+pub fn init(isolate: &mut JsRuntime) {
+ let files = vec![(
+ "deno:op_crates/crypto/01_crypto.js",
+ include_str!("01_crypto.js"),
+ )];
+ for (url, source_code) in files {
+ isolate.execute(url, source_code).unwrap();
}
- super::reg_json_sync(rt, "op_get_random_values", op_get_random_values);
}
-fn op_get_random_values(
+pub fn op_get_random_values(
state: &mut OpState,
_args: Value,
zero_copy: &mut [ZeroCopyBuf],