mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
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.
This commit is contained in:
parent
2c8439bc1e
commit
d5661f677e
13 changed files with 79 additions and 21 deletions
3
.github/workflows/ci.yml
vendored
3
.github/workflows/ci.yml
vendored
|
@ -223,6 +223,9 @@ jobs:
|
||||||
cd ../fetch
|
cd ../fetch
|
||||||
cargo publish
|
cargo publish
|
||||||
sleep 30
|
sleep 30
|
||||||
|
cd ../crypto
|
||||||
|
cargo publish
|
||||||
|
sleep 30
|
||||||
cd ../../cli
|
cd ../../cli
|
||||||
sleep 30
|
sleep 30
|
||||||
cargo publish
|
cargo publish
|
||||||
|
|
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -417,6 +417,7 @@ dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"deno_core",
|
"deno_core",
|
||||||
|
"deno_crypto",
|
||||||
"deno_doc",
|
"deno_doc",
|
||||||
"deno_fetch",
|
"deno_fetch",
|
||||||
"deno_lint",
|
"deno_lint",
|
||||||
|
@ -437,7 +438,6 @@ dependencies = [
|
||||||
"nix",
|
"nix",
|
||||||
"notify",
|
"notify",
|
||||||
"os_pipe",
|
"os_pipe",
|
||||||
"rand 0.7.3",
|
|
||||||
"regex",
|
"regex",
|
||||||
"ring",
|
"ring",
|
||||||
"rustyline",
|
"rustyline",
|
||||||
|
@ -483,6 +483,14 @@ dependencies = [
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "deno_crypto"
|
||||||
|
version = "0.1.1"
|
||||||
|
dependencies = [
|
||||||
|
"deno_core",
|
||||||
|
"rand 0.7.3",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "deno_doc"
|
name = "deno_doc"
|
||||||
version = "0.1.15"
|
version = "0.1.15"
|
||||||
|
|
|
@ -8,6 +8,7 @@ members = [
|
||||||
"test_util",
|
"test_util",
|
||||||
"op_crates/fetch",
|
"op_crates/fetch",
|
||||||
"op_crates/web",
|
"op_crates/web",
|
||||||
|
"op_crates/crypto"
|
||||||
]
|
]
|
||||||
exclude = [
|
exclude = [
|
||||||
"std/hash/_wasm"
|
"std/hash/_wasm"
|
||||||
|
|
|
@ -20,6 +20,7 @@ harness = false
|
||||||
path = "./bench/main.rs"
|
path = "./bench/main.rs"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
|
deno_crypto = { path = "../op_crates/crypto", version = "0.1.1" }
|
||||||
deno_core = { path = "../core", version = "0.67.0" }
|
deno_core = { path = "../core", version = "0.67.0" }
|
||||||
deno_web = { path = "../op_crates/web", version = "0.18.0" }
|
deno_web = { path = "../op_crates/web", version = "0.18.0" }
|
||||||
deno_fetch = { path = "../op_crates/fetch", version = "0.10.0" }
|
deno_fetch = { path = "../op_crates/fetch", version = "0.10.0" }
|
||||||
|
@ -31,6 +32,7 @@ winres = "0.1.11"
|
||||||
winapi = "0.3.9"
|
winapi = "0.3.9"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
deno_crypto = { path = "../op_crates/crypto", version = "0.1.1" }
|
||||||
deno_core = { path = "../core", version = "0.67.0" }
|
deno_core = { path = "../core", version = "0.67.0" }
|
||||||
deno_doc = "0.1.15"
|
deno_doc = "0.1.15"
|
||||||
deno_lint = "0.2.9"
|
deno_lint = "0.2.9"
|
||||||
|
@ -55,7 +57,6 @@ libc = "0.2.77"
|
||||||
log = "0.4.11"
|
log = "0.4.11"
|
||||||
env_logger = "0.7.1"
|
env_logger = "0.7.1"
|
||||||
notify = "5.0.0-pre.3"
|
notify = "5.0.0-pre.3"
|
||||||
rand = "0.7.3"
|
|
||||||
regex = "1.3.9"
|
regex = "1.3.9"
|
||||||
ring = "0.16.15"
|
ring = "0.16.15"
|
||||||
rustyline = { version = "6.3.0", default-features = false }
|
rustyline = { version = "6.3.0", default-features = false }
|
||||||
|
|
|
@ -20,6 +20,7 @@ fn create_snapshot(
|
||||||
) {
|
) {
|
||||||
deno_web::init(&mut js_runtime);
|
deno_web::init(&mut js_runtime);
|
||||||
deno_fetch::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
|
// TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
|
||||||
// workspace root.
|
// workspace root.
|
||||||
let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
|
let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
|
||||||
|
|
14
cli/ops/crypto.rs
Normal file
14
cli/ops/crypto.rs
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -13,8 +13,8 @@ use deno_core::serde_json::Value;
|
||||||
use deno_core::BufVec;
|
use deno_core::BufVec;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use rand::thread_rng;
|
use deno_crypto::rand::thread_rng;
|
||||||
use rand::Rng;
|
use deno_crypto::rand::Rng;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
mod dispatch_minimal;
|
mod dispatch_minimal;
|
||||||
pub use dispatch_minimal::MinimalOp;
|
pub use dispatch_minimal::MinimalOp;
|
||||||
|
|
||||||
|
pub mod crypto;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod fetch;
|
pub mod fetch;
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
|
@ -15,7 +16,6 @@ pub mod os;
|
||||||
pub mod permissions;
|
pub mod permissions;
|
||||||
pub mod plugin;
|
pub mod plugin;
|
||||||
pub mod process;
|
pub mod process;
|
||||||
pub mod random;
|
|
||||||
pub mod runtime;
|
pub mod runtime;
|
||||||
pub mod runtime_compiler;
|
pub mod runtime_compiler;
|
||||||
pub mod signal;
|
pub mod signal;
|
||||||
|
|
|
@ -278,7 +278,7 @@ impl MainWorker {
|
||||||
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
|
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
|
||||||
ops::timers::init(js_runtime);
|
ops::timers::init(js_runtime);
|
||||||
ops::worker_host::init(js_runtime, None);
|
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_close", deno_core::op_close);
|
||||||
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
|
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
|
||||||
ops::reg_json_sync(
|
ops::reg_json_sync(
|
||||||
|
@ -469,7 +469,7 @@ impl WebWorker {
|
||||||
ops::permissions::init(js_runtime);
|
ops::permissions::init(js_runtime);
|
||||||
ops::plugin::init(js_runtime);
|
ops::plugin::init(js_runtime);
|
||||||
ops::process::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::runtime_compiler::init(js_runtime);
|
||||||
ops::signal::init(js_runtime);
|
ops::signal::init(js_runtime);
|
||||||
ops::tls::init(js_runtime);
|
ops::tls::init(js_runtime);
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
((window) => {
|
((window) => {
|
||||||
const core = window.Deno.core;
|
const core = window.Deno.core;
|
||||||
const { assert } = window.__bootstrap.util;
|
|
||||||
|
|
||||||
function getRandomValues(typedArray) {
|
function getRandomValues(typedArray) {
|
||||||
assert(typedArray !== null, "Input must not be null");
|
if (typedArray == null) throw new Error("Input must not be null");
|
||||||
assert(typedArray.length <= 65536, "Input must not be longer than 65536");
|
if (typedArray.length > 65536) {
|
||||||
|
throw new Error("Input must not be longer than 65536");
|
||||||
|
}
|
||||||
const ui8 = new Uint8Array(
|
const ui8 = new Uint8Array(
|
||||||
typedArray.buffer,
|
typedArray.buffer,
|
||||||
typedArray.byteOffset,
|
typedArray.byteOffset,
|
||||||
|
@ -15,7 +15,10 @@
|
||||||
core.jsonOpSync("op_get_random_values", {}, ui8);
|
core.jsonOpSync("op_get_random_values", {}, ui8);
|
||||||
return typedArray;
|
return typedArray;
|
||||||
}
|
}
|
||||||
|
window.crypto = {
|
||||||
|
getRandomValues,
|
||||||
|
};
|
||||||
|
window.__bootstrap = window.__bootstrap || {};
|
||||||
window.__bootstrap.crypto = {
|
window.__bootstrap.crypto = {
|
||||||
getRandomValues,
|
getRandomValues,
|
||||||
};
|
};
|
19
op_crates/crypto/Cargo.toml
Normal file
19
op_crates/crypto/Cargo.toml
Normal file
|
@ -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
op_crates/crypto/README.md
Normal file
3
op_crates/crypto/README.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# deno crypto
|
||||||
|
|
||||||
|
Op crate that implements crypto functions.
|
|
@ -1,26 +1,31 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
#![deny(warnings)]
|
||||||
|
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::json;
|
||||||
use deno_core::serde_json::Value;
|
use deno_core::serde_json::Value;
|
||||||
|
use deno_core::JsRuntime;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use rand::rngs::StdRng;
|
use rand::rngs::StdRng;
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rand::SeedableRng;
|
|
||||||
|
|
||||||
pub fn init(rt: &mut deno_core::JsRuntime, maybe_seed: Option<u64>) {
|
pub use rand; // Re-export rand
|
||||||
if let Some(seed) = maybe_seed {
|
|
||||||
let rng = StdRng::seed_from_u64(seed);
|
/// Execute this crates' JS source files.
|
||||||
let op_state = rt.op_state();
|
pub fn init(isolate: &mut JsRuntime) {
|
||||||
let mut state = op_state.borrow_mut();
|
let files = vec![(
|
||||||
state.put::<StdRng>(rng);
|
"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,
|
state: &mut OpState,
|
||||||
_args: Value,
|
_args: Value,
|
||||||
zero_copy: &mut [ZeroCopyBuf],
|
zero_copy: &mut [ZeroCopyBuf],
|
Loading…
Add table
Reference in a new issue