Skip to content

Commit

Permalink
refactor: deno_crypto op crate (denoland#7956)
Browse files Browse the repository at this point in the history
This commit factors out "deno_crypto" op crate.

"rand" crate dependency was consequently moved to 
"deno_crypto" crate and reexported.
  • Loading branch information
littledivy committed Nov 13, 2020
1 parent 2c8439b commit d5661f6
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ jobs:
cd ../fetch
cargo publish
sleep 30
cd ../crypto
cargo publish
sleep 30
cd ../../cli
sleep 30
cargo publish
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"test_util",
"op_crates/fetch",
"op_crates/web",
"op_crates/crypto"
]
exclude = [
"std/hash/_wasm"
Expand Down
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"
Expand All @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions cli/ops/crypto.rs
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 2 additions & 2 deletions cli/ops/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod dispatch_minimal;
pub use dispatch_minimal::MinimalOp;

pub mod crypto;
pub mod errors;
pub mod fetch;
pub mod fs;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions cli/rt/11_crypto.js → op_crates/crypto/01_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,7 +15,10 @@
core.jsonOpSync("op_get_random_values", {}, ui8);
return typedArray;
}

window.crypto = {
getRandomValues,
};
window.__bootstrap = window.__bootstrap || {};
window.__bootstrap.crypto = {
getRandomValues,
};
Expand Down
19 changes: 19 additions & 0 deletions op_crates/crypto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"

3 changes: 3 additions & 0 deletions op_crates/crypto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# deno crypto

Op crate that implements crypto functions.
23 changes: 14 additions & 9 deletions cli/ops/random.rs → op_crates/crypto/lib.rs
Original file line number Diff line number Diff line change
@@ -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],
Expand Down

0 comments on commit d5661f6

Please sign in to comment.