From 4b044611e222efeed1c52af32301ccb07a85992d Mon Sep 17 00:00:00 2001 From: Eric Seppanen Date: Tue, 23 Jan 2024 13:40:06 -0800 Subject: [PATCH] add a way of getting the Crypto object web_sys understands how to acquire Crypto from a Window or from a WorkerGlobalScope, but neither of these work in a CF worker. It turns out to be possible, but it's not obvious how to do it, so it's convenient to have the worker crate provide this access. --- worker/Cargo.toml | 1 + worker/src/crypto.rs | 6 ++++++ worker/src/lib.rs | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 worker/src/crypto.rs diff --git a/worker/Cargo.toml b/worker/Cargo.toml index 8e4aee563..644f71a93 100644 --- a/worker/Cargo.toml +++ b/worker/Cargo.toml @@ -39,6 +39,7 @@ worker-sys = { path = "../worker-sys", version = "0.0.12" } [dependencies.web-sys] version = "0.3.63" features = [ + "Crypto", "File", "WorkerGlobalScope", "ReadableStreamDefaultReader", diff --git a/worker/src/crypto.rs b/worker/src/crypto.rs new file mode 100644 index 000000000..d4a71c1ba --- /dev/null +++ b/worker/src/crypto.rs @@ -0,0 +1,6 @@ +use wasm_bindgen::JsCast; + +pub fn crypto() -> web_sys::Crypto { + let global: web_sys::WorkerGlobalScope = js_sys::global().unchecked_into(); + global.crypto().expect("failed to acquire Crypto") +} diff --git a/worker/src/lib.rs b/worker/src/lib.rs index 9159a5c03..bf39946d8 100644 --- a/worker/src/lib.rs +++ b/worker/src/lib.rs @@ -25,6 +25,7 @@ pub use crate::abort::*; pub use crate::cache::{Cache, CacheDeletionOutcome, CacheKey}; pub use crate::context::Context; pub use crate::cors::Cors; +pub use crate::crypto::*; #[cfg(feature = "d1")] pub use crate::d1::*; pub use crate::date::{Date, DateInit}; @@ -55,6 +56,7 @@ mod cache; mod cf; mod context; mod cors; +mod crypto; // Require pub module for macro export #[cfg(feature = "d1")] pub mod d1;