summaryrefslogtreecommitdiff
path: root/op_crates/crypto/lib.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2021-04-28 18:41:50 +0200
committerGitHub <noreply@github.com>2021-04-28 18:41:50 +0200
commit0260b488fbba9a43c64641428d3603b8761067a4 (patch)
tree66ce487f9241a3b91942dd048c7e43cb192bf9e8 /op_crates/crypto/lib.rs
parentb28f9445aae85dbf86033300cfcb55e404529a23 (diff)
core: introduce extensions (#9800)
Extensions allow declarative extensions to "JsRuntime" (ops, state, JS or middleware). This allows for: - `op_crates` to be plug-and-play & self-contained, reducing complexity leaked to consumers - op middleware (like metrics_op) to be opt-in and for new middleware (unstable, tracing,...) - `MainWorker` and `WebWorker` to be composable, allowing users to extend workers with their ops whilst benefiting from the other infrastructure (inspector, etc...) In short extensions improve deno's modularity, reducing complexity and leaky abstractions for embedders and the internal codebase.
Diffstat (limited to 'op_crates/crypto/lib.rs')
-rw-r--r--op_crates/crypto/lib.rs31
1 files changed, 21 insertions, 10 deletions
diff --git a/op_crates/crypto/lib.rs b/op_crates/crypto/lib.rs
index 8b4d34a7c..d5b0a30bc 100644
--- a/op_crates/crypto/lib.rs
+++ b/op_crates/crypto/lib.rs
@@ -2,25 +2,36 @@
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
-use deno_core::JsRuntime;
+use deno_core::include_js_files;
+use deno_core::op_sync;
+use deno_core::Extension;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use rand::rngs::StdRng;
use rand::thread_rng;
use rand::Rng;
+use rand::SeedableRng;
use std::path::PathBuf;
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();
- }
+pub fn init(maybe_seed: Option<u64>) -> Extension {
+ Extension::with_ops(
+ include_js_files!(
+ prefix "deno:op_crates/crypto",
+ "01_crypto.js",
+ ),
+ vec![(
+ "op_crypto_get_random_values",
+ op_sync(op_crypto_get_random_values),
+ )],
+ Some(Box::new(move |state| {
+ if let Some(seed) = maybe_seed {
+ state.put(StdRng::seed_from_u64(seed));
+ }
+ Ok(())
+ })),
+ )
}
pub fn op_crypto_get_random_values(