File
Blob: src/rust/api/lib.rs
| 1 | // Copyright (c) 2026 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | use std::pin::Pin; |
| 6 | |
| 7 | use jsg::ToJS; |
| 8 | |
| 9 | use crate::dns::DnsUtil; |
| 10 | |
| 11 | pub mod dns; |
| 12 | |
| 13 | #[cxx::bridge(namespace = "workerd::rust::api")] |
| 14 | mod ffi { |
| 15 | #[namespace = "workerd::rust::jsg"] |
| 16 | unsafe extern "C++" { |
| 17 | include!("workerd/rust/jsg/ffi.h"); |
| 18 | |
| 19 | type ModuleRegistry = jsg::v8::ffi::ModuleRegistry; |
| 20 | } |
| 21 | extern "Rust" { |
| 22 | pub fn register_nodejs_modules(registry: Pin<&mut ModuleRegistry>); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn register_nodejs_modules(registry: Pin<&mut ffi::ModuleRegistry>) { |
| 27 | jsg::modules::add_builtin( |
| 28 | registry, |
| 29 | "node-internal:dns", |
| 30 | // SAFETY: isolate is valid and locked — called from C++ module registration. |
| 31 | |isolate| unsafe { |
| 32 | let mut lock = jsg::Lock::from_isolate_ptr(isolate); |
| 33 | let dns_util = DnsUtil::new(); |
| 34 | dns_util.to_js(&mut lock).into_ffi() |
| 35 | }, |
| 36 | jsg::modules::ModuleType::Internal, |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | #[cfg(test)] |
| 41 | mod tests { |
| 42 | use jsg_test::Harness; |
| 43 | |
| 44 | use super::*; |
| 45 | |
| 46 | #[test] |
| 47 | fn test_wrap_resource_equality() { |
| 48 | let harness = Harness::new(); |
| 49 | harness.run_in_context(|lock, _ctx| { |
| 50 | let dns_util = DnsUtil::new(); |
| 51 | |
| 52 | let lhs = dns_util.clone().to_js(lock); |
| 53 | let rhs = dns_util.to_js(lock); |
| 54 | |
| 55 | assert_eq!(lhs, rhs); |
| 56 | Ok(()) |
| 57 | }); |
| 58 | } |
| 59 | } |