From aae2e94cc811e345e206bcef33db9c9212d9ffc3 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Sun, 4 Apr 2021 11:12:18 +0800 Subject: [PATCH] move bogus "unsafe" to utils * These are needed to make rust-analyzer stop complaining --- src/client.rs | 8 ++++---- src/util.rs | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 2b2195f..e746792 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,7 +6,7 @@ use domain_core::bits::record::Record; use domain_core::bits::{ParsedDname, SectionBuilder}; use domain_core::iana::Opcode; use domain_core::rdata::AllRecordData; -use js_sys::{ArrayBuffer, Math, Uint8Array}; +use js_sys::{ArrayBuffer, Uint8Array}; use wasm_bindgen_futures::JsFuture; use web_sys::{Headers, Request, RequestInit, Response}; @@ -51,7 +51,7 @@ impl Client { // Select an upstream randomly fn select_upstream(&self) -> String { - let idx = unsafe { Math::random() } * self.options.upstream_urls.len() as f64; + let idx = crate::util::random() * self.options.upstream_urls.len() as f64; self.options.upstream_urls[idx as usize].clone() } @@ -62,7 +62,7 @@ impl Client { let mut builder = MessageBuilder::new_udp(); // Set up the header let header = builder.header_mut(); - header.set_id((unsafe { Math::random() } * u16::MAX as f64) as u16); + header.set_id((crate::util::random() * u16::MAX as f64) as u16); header.set_qr(false); // For queries, QR = false header.set_opcode(Opcode::Query); header.set_rd(true); // Ask for recursive queries @@ -93,7 +93,7 @@ impl Client { let request = Request::new_with_str_and_init(upstream, &request_init) .map_err(|_| "Failed to create Request object".to_string())?; - let resp: Response = JsFuture::from(unsafe { crate::util::fetch(&request) }) + let resp: Response = crate::util::fetch_rs(&request) .await .map_err(|_| "Upstream request error".to_string())? .into(); diff --git a/src/util.rs b/src/util.rs index 44e0d77..87b000d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,9 @@ use bytes::Bytes; use domain_core::bits::message::Message; -use js_sys::Promise; +use js_sys::{Math, Promise}; +use wasm_bindgen::JsValue; use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::JsFuture; use web_sys::Request; #[wasm_bindgen] @@ -10,10 +12,23 @@ extern "C" { // In cloudflare workers, there's no Window object // and unfortunately the bionding in web_sys depends // on Window being present. - pub fn fetch(req: &Request) -> Promise; + fn fetch(req: &Request) -> Promise; } pub fn parse_dns_wireformat(msg: &[u8]) -> Result { let bytes = Bytes::from(msg); Message::from_bytes(bytes).map_err(|_| "Failed to parse DNS wireformat message".to_string()) } + +// Rust wrapper around JS functions +// For convenience, and also to work around bugs in rust-analyzer +// which thinks all JS functions are "unsafe" +#[allow(unused_unsafe)] +pub async fn fetch_rs(req: &Request) -> Result { + JsFuture::from(unsafe { fetch(req) }).await +} + +#[allow(unused_unsafe)] +pub fn random() -> f64 { + unsafe { Math::random() } +} \ No newline at end of file