Setup for TypeScript type gen for WASM using Tsify

- Includes breaking changes to the WASM API surface, since it now accepts objects instead of stringified JSON.
This commit is contained in:
Brandon Vandegrift 2023-08-16 10:25:09 -04:00
parent 65826b219b
commit 1b20037053
22 changed files with 323 additions and 109 deletions

78
Cargo.lock generated
View File

@ -2136,6 +2136,19 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "gloo-utils"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037fcb07216cb3a30f7292bd0176b050b7b9a052ba830ef7d5d65f6dc64ba58e"
dependencies = [
"js-sys",
"serde",
"serde_json",
"wasm-bindgen",
"web-sys",
]
[[package]] [[package]]
name = "gloo-utils" name = "gloo-utils"
version = "0.2.0" version = "0.2.0"
@ -4250,7 +4263,7 @@ checksum = "ec0f696e21e10fa546b7ffb1c9672c6de8fbc7a81acf59524386d8639bf12737"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"serde_derive_internals", "serde_derive_internals 0.26.0",
"syn 1.0.109", "syn 1.0.109",
] ]
@ -4358,6 +4371,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde-wasm-bindgen"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3b143e2833c57ab9ad3ea280d21fd34e285a42837aeb0ee301f4f41890fa00e"
dependencies = [
"js-sys",
"serde",
"wasm-bindgen",
]
[[package]] [[package]]
name = "serde_cbor" name = "serde_cbor"
version = "0.11.2" version = "0.11.2"
@ -4390,6 +4414,17 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "serde_derive_internals"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e578a843d40b4189a4d66bba51d7684f57da5bd7c304c64e14bd63efbef49509"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.105" version = "1.0.105"
@ -5347,6 +5382,41 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed"
[[package]]
name = "tsify"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6b26cf145f2f3b9ff84e182c448eaf05468e247f148cf3d2a7d67d78ff023a0"
dependencies = [
"gloo-utils 0.1.7",
"serde",
"serde-wasm-bindgen",
"serde_json",
"tsify-macros",
"wasm-bindgen",
]
[[package]]
name = "tsify-async"
version = "0.1.0"
dependencies = [
"quote",
"serde-wasm-bindgen",
"syn 1.0.109",
]
[[package]]
name = "tsify-macros"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a94b0f0954b3e59bfc2c246b4c8574390d94a4ad4ad246aaf2fb07d7dfd3b47"
dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals 0.28.0",
"syn 2.0.31",
]
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.11.1" version = "0.11.1"
@ -5614,6 +5684,7 @@ dependencies = [
"send_wrapper 0.6.0", "send_wrapper 0.6.0",
"serde", "serde",
"serde-big-array", "serde-big-array",
"serde-wasm-bindgen",
"serde_json", "serde_json",
"serial_test 2.0.0", "serial_test 2.0.0",
"shell-words", "shell-words",
@ -5631,6 +5702,7 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
"tracing-wasm", "tracing-wasm",
"trust-dns-resolver", "trust-dns-resolver",
"tsify",
"veilid-bugsalot", "veilid-bugsalot",
"veilid-hashlink", "veilid-hashlink",
"veilid-igd", "veilid-igd",
@ -5813,15 +5885,17 @@ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"data-encoding", "data-encoding",
"futures-util", "futures-util",
"gloo-utils", "gloo-utils 0.2.0",
"js-sys", "js-sys",
"lazy_static", "lazy_static",
"send_wrapper 0.6.0", "send_wrapper 0.6.0",
"serde", "serde",
"serde-wasm-bindgen",
"serde_json", "serde_json",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"tracing-wasm", "tracing-wasm",
"tsify",
"veilid-core", "veilid-core",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",

View File

@ -6,6 +6,7 @@ members = [
"veilid-cli", "veilid-cli",
"veilid-flutter/rust", "veilid-flutter/rust",
"veilid-wasm", "veilid-wasm",
"tsify-async"
] ]
exclude = ["./external"] exclude = ["./external"]

14
tsify-async/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "tsify-async"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
proc-macro = true
[dependencies]
syn = "1.0"
quote = "1.0"
serde-wasm-bindgen = "0.5.0"

30
tsify-async/src/lib.rs Normal file
View File

@ -0,0 +1,30 @@
// Copied from https://github.com/madonoharu/tsify/issues/24#issuecomment-1670228789
// TODO: I think this had more to do with:
// the trait `From<veilid_core::VeilidUpdate>` is not implemented for `wasm_bindgen::JsValue`
// than it does with tsify itself... Maybe rename to something else?
use proc_macro::TokenStream;
use quote::quote;
use syn;
#[proc_macro_derive(TsifyAsync)]
pub fn tsify_async_macro_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
// Build the trait implementation
impl_tsify_async_macro(&ast)
}
fn impl_tsify_async_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl From<#name> for JsValue {
fn from(value: #name) -> Self {
serde_wasm_bindgen::to_value(&value).unwrap()
}
}
};
gen.into()
}

View File

@ -140,6 +140,9 @@ lz4_flex = { version = "0.11.1", default-features = false, features = [
"safe-encode", "safe-encode",
"safe-decode", "safe-decode",
] } ] }
tsify = { version = "0.4.5", features = ["js"] }
wasm-bindgen = "^0"
serde-wasm-bindgen = "0.5.0"
# Dependencies for native builds only # Dependencies for native builds only
# Linux, Windows, Mac, iOS, Android # Linux, Windows, Mac, iOS, Android

View File

@ -231,7 +231,7 @@ impl VeilidCoreContext {
update_callback: UpdateCallback, update_callback: UpdateCallback,
config_json: String, config_json: String,
) -> VeilidAPIResult<VeilidCoreContext> { ) -> VeilidAPIResult<VeilidCoreContext> {
// Set up config from callback // Set up config from json
trace!("setup config with json"); trace!("setup config with json");
let mut config = VeilidConfig::new(); let mut config = VeilidConfig::new();
config.setup_from_json(config_json, update_callback.clone())?; config.setup_from_json(config_json, update_callback.clone())?;

View File

@ -293,11 +293,17 @@ macro_rules! byte_array_type {
byte_array_type!(CryptoKey, CRYPTO_KEY_LENGTH, CRYPTO_KEY_LENGTH_ENCODED); byte_array_type!(CryptoKey, CRYPTO_KEY_LENGTH, CRYPTO_KEY_LENGTH_ENCODED);
#[declare]
pub type PublicKey = CryptoKey; pub type PublicKey = CryptoKey;
#[declare]
pub type SecretKey = CryptoKey; pub type SecretKey = CryptoKey;
#[declare]
pub type HashDigest = CryptoKey; pub type HashDigest = CryptoKey;
#[declare]
pub type SharedSecret = CryptoKey; pub type SharedSecret = CryptoKey;
#[declare]
pub type RouteId = CryptoKey; pub type RouteId = CryptoKey;
#[declare]
pub type CryptoKeyDistance = CryptoKey; pub type CryptoKeyDistance = CryptoKey;
byte_array_type!(Signature, SIGNATURE_LENGTH, SIGNATURE_LENGTH_ENCODED); byte_array_type!(Signature, SIGNATURE_LENGTH, SIGNATURE_LENGTH_ENCODED);

View File

@ -1,6 +1,6 @@
use super::*; use super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Tsify)]
pub struct CryptoTyped<K> pub struct CryptoTyped<K>
where where
K: Clone K: Clone

View File

@ -1,6 +1,8 @@
use super::*; use super::*;
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq, Hash, Default)] #[derive(
Clone, Debug, Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq, Hash, Default, Tsify,
)]
#[serde(from = "Vec<CryptoTyped<K>>", into = "Vec<CryptoTyped<K>>")] #[serde(from = "Vec<CryptoTyped<K>>", into = "Vec<CryptoTyped<K>>")]
pub struct CryptoTypedGroup<K = PublicKey> pub struct CryptoTypedGroup<K = PublicKey>
where where

View File

@ -6,6 +6,7 @@ use core::fmt;
use core::hash::Hash; use core::hash::Hash;
/// Cryptography version fourcc code /// Cryptography version fourcc code
#[declare]
pub type CryptoKind = FourCC; pub type CryptoKind = FourCC;
/// Sort best crypto kinds first /// Sort best crypto kinds first
@ -51,14 +52,24 @@ pub use crypto_typed::*;
pub use crypto_typed_group::*; pub use crypto_typed_group::*;
pub use keypair::*; pub use keypair::*;
#[declare]
pub type TypedKey = CryptoTyped<PublicKey>; pub type TypedKey = CryptoTyped<PublicKey>;
#[declare]
pub type TypedSecret = CryptoTyped<SecretKey>; pub type TypedSecret = CryptoTyped<SecretKey>;
#[declare]
pub type TypedKeyPair = CryptoTyped<KeyPair>; pub type TypedKeyPair = CryptoTyped<KeyPair>;
#[declare]
pub type TypedSignature = CryptoTyped<Signature>; pub type TypedSignature = CryptoTyped<Signature>;
#[declare]
pub type TypedSharedSecret = CryptoTyped<SharedSecret>; pub type TypedSharedSecret = CryptoTyped<SharedSecret>;
#[declare]
pub type TypedKeyGroup = CryptoTypedGroup<PublicKey>; pub type TypedKeyGroup = CryptoTypedGroup<PublicKey>;
#[declare]
pub type TypedSecretGroup = CryptoTypedGroup<SecretKey>; pub type TypedSecretGroup = CryptoTypedGroup<SecretKey>;
#[declare]
pub type TypedKeyPairGroup = CryptoTypedGroup<KeyPair>; pub type TypedKeyPairGroup = CryptoTypedGroup<KeyPair>;
#[declare]
pub type TypedSignatureGroup = CryptoTypedGroup<Signature>; pub type TypedSignatureGroup = CryptoTypedGroup<Signature>;
#[declare]
pub type TypedSharedSecretGroup = CryptoTypedGroup<SharedSecret>; pub type TypedSharedSecretGroup = CryptoTypedGroup<SharedSecret>;

View File

@ -126,4 +126,7 @@ use serde::*;
use stop_token::*; use stop_token::*;
use thiserror::Error as ThisError; use thiserror::Error as ThisError;
use tracing::*; use tracing::*;
use tsify::*;
use tsify_async::*;
use veilid_tools::*; use veilid_tools::*;
use wasm_bindgen::prelude::*;

View File

@ -105,9 +105,21 @@ macro_rules! apibail_already_initialized {
} }
#[derive( #[derive(
ThisError, Clone, Debug, PartialOrd, PartialEq, Eq, Ord, Serialize, Deserialize, JsonSchema, ThisError,
Clone,
Debug,
PartialOrd,
PartialEq,
Eq,
Ord,
Serialize,
Deserialize,
JsonSchema,
Tsify,
TsifyAsync,
)] )]
#[serde(tag = "kind")] #[serde(tag = "kind")]
#[tsify(into_wasm_abi)]
pub enum VeilidAPIError { pub enum VeilidAPIError {
#[error("Not initialized")] #[error("Not initialized")]
NotInitialized, NotInitialized,
@ -213,6 +225,7 @@ impl VeilidAPIError {
} }
} }
#[declare]
pub type VeilidAPIResult<T> = Result<T, VeilidAPIError>; pub type VeilidAPIResult<T> = Result<T, VeilidAPIError>;
impl From<std::io::Error> for VeilidAPIError { impl From<std::io::Error> for VeilidAPIError {

View File

@ -6,7 +6,18 @@ use super::*;
/// Supports serializing to string for JSON as well, since JSON can't handle 64-bit numbers to Javascript /// Supports serializing to string for JSON as well, since JSON can't handle 64-bit numbers to Javascript
#[derive( #[derive(
Clone, Default, PartialEq, Eq, PartialOrd, Ord, Copy, Hash, Serialize, Deserialize, JsonSchema, Clone,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Copy,
Hash,
Serialize,
Deserialize,
JsonSchema,
Tsify,
)] )]
#[repr(C, align(8))] #[repr(C, align(8))]
#[serde(transparent)] #[serde(transparent)]
@ -117,13 +128,17 @@ impl AlignedU64 {
///////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////
/// Microseconds since epoch /// Microseconds since epoch
#[declare]
pub type Timestamp = AlignedU64; pub type Timestamp = AlignedU64;
pub fn get_aligned_timestamp() -> Timestamp { pub fn get_aligned_timestamp() -> Timestamp {
get_timestamp().into() get_timestamp().into()
} }
/// Microseconds duration /// Microseconds duration
#[declare]
pub type TimestampDuration = AlignedU64; pub type TimestampDuration = AlignedU64;
/// Request/Response matching id /// Request/Response matching id
#[declare]
pub type OperationId = AlignedU64; pub type OperationId = AlignedU64;
/// Number of bytes /// Number of bytes
#[declare]
pub type ByteCount = AlignedU64; pub type ByteCount = AlignedU64;

View File

@ -1,10 +1,11 @@
use super::*; use super::*;
/// Direct statement blob passed to hosting application for processing /// Direct statement blob passed to hosting application for processing
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidAppMessage { pub struct VeilidAppMessage {
#[serde(with = "as_human_opt_string")] #[serde(with = "as_human_opt_string")]
#[schemars(with = "Option<String>")] #[schemars(with = "Option<String>")]
#[tsify(optional)]
sender: Option<TypedKey>, sender: Option<TypedKey>,
#[serde(with = "as_human_base64")] #[serde(with = "as_human_base64")]
@ -29,10 +30,11 @@ impl VeilidAppMessage {
} }
/// Direct question blob passed to hosting application for processing to send an eventual AppReply /// Direct question blob passed to hosting application for processing to send an eventual AppReply
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidAppCall { pub struct VeilidAppCall {
#[serde(with = "as_human_opt_string")] #[serde(with = "as_human_opt_string")]
#[schemars(with = "Option<String>")] #[schemars(with = "Option<String>")]
#[tsify(optional)]
sender: Option<TypedKey>, sender: Option<TypedKey>,
#[serde(with = "as_human_base64")] #[serde(with = "as_human_base64")]

View File

@ -11,6 +11,8 @@ pub use value_data::*;
pub use value_subkey_range_set::*; pub use value_subkey_range_set::*;
/// Value subkey /// Value subkey
#[declare]
pub type ValueSubkey = u32; pub type ValueSubkey = u32;
/// Value sequence number /// Value sequence number
#[declare]
pub type ValueSeqNum = u32; pub type ValueSeqNum = u32;

View File

@ -1,7 +1,9 @@
use super::*; use super::*;
use veilid_api::VeilidAPIResult; use veilid_api::VeilidAPIResult;
#[derive(Clone, Default, PartialOrd, PartialEq, Eq, Ord, Serialize, Deserialize, JsonSchema)] #[derive(
Clone, Default, PartialOrd, PartialEq, Eq, Ord, Serialize, Deserialize, JsonSchema, Tsify,
)]
pub struct ValueData { pub struct ValueData {
/// An increasing sequence number to time-order the DHT record changes /// An increasing sequence number to time-order the DHT record changes
seq: ValueSeqNum, seq: ValueSeqNum,

View File

@ -2,7 +2,18 @@ use super::*;
/// FOURCC code /// FOURCC code
#[derive( #[derive(
Copy, Default, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Copy,
Default,
Clone,
Hash,
PartialOrd,
Ord,
PartialEq,
Eq,
Serialize,
Deserialize,
JsonSchema,
Tsify,
)] )]
#[serde(try_from = "String")] #[serde(try_from = "String")]
#[serde(into = "String")] #[serde(into = "String")]

View File

@ -2,8 +2,9 @@ use super::*;
/// Log level for VeilidCore /// Log level for VeilidCore
#[derive( #[derive(
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy, Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy, Serialize, Deserialize, JsonSchema, Tsify,
)] )]
#[tsify(namespace)]
pub enum VeilidLogLevel { pub enum VeilidLogLevel {
Error = 1, Error = 1,
Warn = 2, Warn = 2,
@ -79,9 +80,10 @@ impl fmt::Display for VeilidLogLevel {
} }
} }
/// A VeilidCore log message with optional backtrace /// A VeilidCore log message with optional backtrace
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidLog { pub struct VeilidLog {
pub log_level: VeilidLogLevel, pub log_level: VeilidLogLevel,
pub message: String, pub message: String,
#[tsify(optional)]
pub backtrace: Option<String>, pub backtrace: Option<String>,
} }

View File

@ -1,7 +1,8 @@
use super::*; use super::*;
/// Attachment abstraction for network 'signal strength' /// Attachment abstraction for network 'signal strength'
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, JsonSchema)] #[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, JsonSchema, Tsify)]
#[tsify(namespace, from_wasm_abi, into_wasm_abi)]
pub enum AttachmentState { pub enum AttachmentState {
Detached = 0, Detached = 0,
Attaching = 1, Attaching = 1,
@ -47,14 +48,14 @@ impl TryFrom<String> for AttachmentState {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidStateAttachment { pub struct VeilidStateAttachment {
pub state: AttachmentState, pub state: AttachmentState,
pub public_internet_ready: bool, pub public_internet_ready: bool,
pub local_network_ready: bool, pub local_network_ready: bool,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct PeerTableData { pub struct PeerTableData {
#[schemars(with = "Vec<String>")] #[schemars(with = "Vec<String>")]
pub node_ids: Vec<TypedKey>, pub node_ids: Vec<TypedKey>,
@ -62,7 +63,7 @@ pub struct PeerTableData {
pub peer_stats: PeerStats, pub peer_stats: PeerStats,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidStateNetwork { pub struct VeilidStateNetwork {
pub started: bool, pub started: bool,
pub bps_down: ByteCount, pub bps_down: ByteCount,
@ -70,7 +71,7 @@ pub struct VeilidStateNetwork {
pub peers: Vec<PeerTableData>, pub peers: Vec<PeerTableData>,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidRouteChange { pub struct VeilidRouteChange {
#[schemars(with = "Vec<String>")] #[schemars(with = "Vec<String>")]
pub dead_routes: Vec<RouteId>, pub dead_routes: Vec<RouteId>,
@ -78,12 +79,12 @@ pub struct VeilidRouteChange {
pub dead_remote_routes: Vec<RouteId>, pub dead_remote_routes: Vec<RouteId>,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidStateConfig { pub struct VeilidStateConfig {
pub config: VeilidConfigInner, pub config: VeilidConfigInner,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidValueChange { pub struct VeilidValueChange {
#[schemars(with = "String")] #[schemars(with = "String")]
pub key: TypedKey, pub key: TypedKey,
@ -92,8 +93,9 @@ pub struct VeilidValueChange {
pub value: ValueData, pub value: ValueData,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify, TsifyAsync)]
#[serde(tag = "kind")] #[serde(tag = "kind")]
#[tsify(into_wasm_abi)]
pub enum VeilidUpdate { pub enum VeilidUpdate {
Log(VeilidLog), Log(VeilidLog),
AppMessage(VeilidAppMessage), AppMessage(VeilidAppMessage),
@ -106,7 +108,8 @@ pub enum VeilidUpdate {
Shutdown, Shutdown,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify, TsifyAsync)]
#[tsify(into_wasm_abi)]
pub struct VeilidState { pub struct VeilidState {
pub attachment: VeilidStateAttachment, pub attachment: VeilidStateAttachment,
pub network: VeilidStateNetwork, pub network: VeilidStateNetwork,

View File

@ -1,7 +1,9 @@
use crate::*; use crate::*;
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
#[declare]
pub type ConfigCallbackReturn = VeilidAPIResult<Box<dyn core::any::Any + Send>>; pub type ConfigCallbackReturn = VeilidAPIResult<Box<dyn core::any::Any + Send>>;
#[declare]
pub type ConfigCallback = Arc<dyn Fn(String) -> ConfigCallbackReturn + Send + Sync>; pub type ConfigCallback = Arc<dyn Fn(String) -> ConfigCallbackReturn + Send + Sync>;
/// Enable and configure HTTPS access to the Veilid node /// Enable and configure HTTPS access to the Veilid node
@ -14,11 +16,12 @@ pub type ConfigCallback = Arc<dyn Fn(String) -> ConfigCallbackReturn + Send + Sy
/// url: 'https://localhost:5150' /// url: 'https://localhost:5150'
/// ``` /// ```
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigHTTPS { pub struct VeilidConfigHTTPS {
pub enabled: bool, pub enabled: bool,
pub listen_address: String, pub listen_address: String,
pub path: String, pub path: String,
#[tsify(optional)]
pub url: Option<String>, // Fixed URL is not optional for TLS-based protocols and is dynamically validated pub url: Option<String>, // Fixed URL is not optional for TLS-based protocols and is dynamically validated
} }
@ -32,11 +35,12 @@ pub struct VeilidConfigHTTPS {
/// url: 'https://localhost:5150' /// url: 'https://localhost:5150'
/// ``` /// ```
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigHTTP { pub struct VeilidConfigHTTP {
pub enabled: bool, pub enabled: bool,
pub listen_address: String, pub listen_address: String,
pub path: String, pub path: String,
#[tsify(optional)]
pub url: Option<String>, pub url: Option<String>,
} }
@ -46,7 +50,7 @@ pub struct VeilidConfigHTTP {
/// ///
/// To be implemented... /// To be implemented...
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigApplication { pub struct VeilidConfigApplication {
pub https: VeilidConfigHTTPS, pub https: VeilidConfigHTTPS,
pub http: VeilidConfigHTTP, pub http: VeilidConfigHTTP,
@ -62,11 +66,12 @@ pub struct VeilidConfigApplication {
/// public_address: '' /// public_address: ''
/// ``` /// ```
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigUDP { pub struct VeilidConfigUDP {
pub enabled: bool, pub enabled: bool,
pub socket_pool_size: u32, pub socket_pool_size: u32,
pub listen_address: String, pub listen_address: String,
#[tsify(optional)]
pub public_address: Option<String>, pub public_address: Option<String>,
} }
@ -80,12 +85,13 @@ pub struct VeilidConfigUDP {
/// listen_address: ':5150' /// listen_address: ':5150'
/// public_address: '' /// public_address: ''
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigTCP { pub struct VeilidConfigTCP {
pub connect: bool, pub connect: bool,
pub listen: bool, pub listen: bool,
pub max_connections: u32, pub max_connections: u32,
pub listen_address: String, pub listen_address: String,
#[tsify(optional)]
pub public_address: Option<String>, pub public_address: Option<String>,
} }
@ -100,7 +106,7 @@ pub struct VeilidConfigTCP {
/// path: 'ws' /// path: 'ws'
/// url: 'ws://localhost:5150/ws' /// url: 'ws://localhost:5150/ws'
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigWS { pub struct VeilidConfigWS {
pub connect: bool, pub connect: bool,
@ -108,6 +114,7 @@ pub struct VeilidConfigWS {
pub max_connections: u32, pub max_connections: u32,
pub listen_address: String, pub listen_address: String,
pub path: String, pub path: String,
#[tsify(optional)]
pub url: Option<String>, pub url: Option<String>,
} }
@ -122,7 +129,7 @@ pub struct VeilidConfigWS {
/// path: 'ws' /// path: 'ws'
/// url: '' /// url: ''
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigWSS { pub struct VeilidConfigWSS {
pub connect: bool, pub connect: bool,
@ -130,6 +137,7 @@ pub struct VeilidConfigWSS {
pub max_connections: u32, pub max_connections: u32,
pub listen_address: String, pub listen_address: String,
pub path: String, pub path: String,
#[tsify(optional)]
pub url: Option<String>, // Fixed URL is not optional for TLS-based protocols and is dynamically validated pub url: Option<String>, // Fixed URL is not optional for TLS-based protocols and is dynamically validated
} }
@ -140,7 +148,7 @@ pub struct VeilidConfigWSS {
/// All protocols are available by default, and the Veilid node will /// All protocols are available by default, and the Veilid node will
/// sort out which protocol is used for each peer connection. /// sort out which protocol is used for each peer connection.
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigProtocol { pub struct VeilidConfigProtocol {
pub udp: VeilidConfigUDP, pub udp: VeilidConfigUDP,
@ -157,7 +165,7 @@ pub struct VeilidConfigProtocol {
/// private_key_path: /path/to/private/key /// private_key_path: /path/to/private/key
/// connection_initial_timeout_ms: 2000 /// connection_initial_timeout_ms: 2000
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigTLS { pub struct VeilidConfigTLS {
pub certificate_path: String, pub certificate_path: String,
pub private_key_path: String, pub private_key_path: String,
@ -166,7 +174,7 @@ pub struct VeilidConfigTLS {
/// Configure the Distributed Hash Table (DHT) /// Configure the Distributed Hash Table (DHT)
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigDHT { pub struct VeilidConfigDHT {
pub max_find_node_count: u32, pub max_find_node_count: u32,
pub resolve_node_timeout_ms: u32, pub resolve_node_timeout_ms: u32,
@ -191,11 +199,13 @@ pub struct VeilidConfigDHT {
/// Configure RPC /// Configure RPC
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigRPC { pub struct VeilidConfigRPC {
pub concurrency: u32, pub concurrency: u32,
pub queue_size: u32, pub queue_size: u32,
#[tsify(optional)]
pub max_timestamp_behind_ms: Option<u32>, pub max_timestamp_behind_ms: Option<u32>,
#[tsify(optional)]
pub max_timestamp_ahead_ms: Option<u32>, pub max_timestamp_ahead_ms: Option<u32>,
pub timeout_ms: u32, pub timeout_ms: u32,
pub max_route_hop_count: u8, pub max_route_hop_count: u8,
@ -204,7 +214,7 @@ pub struct VeilidConfigRPC {
/// Configure the network routing table /// Configure the network routing table
/// ///
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigRoutingTable { pub struct VeilidConfigRoutingTable {
#[schemars(with = "Vec<String>")] #[schemars(with = "Vec<String>")]
pub node_id: TypedKeyGroup, pub node_id: TypedKeyGroup,
@ -220,7 +230,7 @@ pub struct VeilidConfigRoutingTable {
// xxx pub enable_local_network: bool, // xxx pub enable_local_network: bool,
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigNetwork { pub struct VeilidConfigNetwork {
pub connection_initial_timeout_ms: u32, pub connection_initial_timeout_ms: u32,
pub connection_inactivity_timeout_ms: u32, pub connection_inactivity_timeout_ms: u32,
@ -231,6 +241,7 @@ pub struct VeilidConfigNetwork {
pub client_whitelist_timeout_ms: u32, pub client_whitelist_timeout_ms: u32,
pub reverse_connection_receipt_time_ms: u32, pub reverse_connection_receipt_time_ms: u32,
pub hole_punch_receipt_time_ms: u32, pub hole_punch_receipt_time_ms: u32,
#[tsify(optional)]
pub network_key_password: Option<String>, pub network_key_password: Option<String>,
pub routing_table: VeilidConfigRoutingTable, pub routing_table: VeilidConfigRoutingTable,
pub rpc: VeilidConfigRPC, pub rpc: VeilidConfigRPC,
@ -243,34 +254,36 @@ pub struct VeilidConfigNetwork {
pub protocol: VeilidConfigProtocol, pub protocol: VeilidConfigProtocol,
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigTableStore { pub struct VeilidConfigTableStore {
pub directory: String, pub directory: String,
pub delete: bool, pub delete: bool,
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigBlockStore { pub struct VeilidConfigBlockStore {
pub directory: String, pub directory: String,
pub delete: bool, pub delete: bool,
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigProtectedStore { pub struct VeilidConfigProtectedStore {
pub allow_insecure_fallback: bool, pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool, pub always_use_insecure_storage: bool,
pub directory: String, pub directory: String,
pub delete: bool, pub delete: bool,
pub device_encryption_key_password: String, pub device_encryption_key_password: String,
#[tsify(optional)]
pub new_device_encryption_key_password: Option<String>, pub new_device_encryption_key_password: Option<String>,
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigCapabilities { pub struct VeilidConfigCapabilities {
pub disable: Vec<FourCC>, pub disable: Vec<FourCC>,
} }
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize, JsonSchema, Tsify)]
#[tsify(namespace, from_wasm_abi)]
pub enum VeilidConfigLogLevel { pub enum VeilidConfigLogLevel {
Off, Off,
Error, Error,
@ -357,7 +370,7 @@ impl fmt::Display for VeilidConfigLogLevel {
} }
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
pub struct VeilidConfigInner { pub struct VeilidConfigInner {
pub program_name: String, pub program_name: String,
pub namespace: String, pub namespace: String,

View File

@ -34,6 +34,8 @@ send_wrapper = "^0"
futures-util = { version = "^0" } futures-util = { version = "^0" }
data-encoding = { version = "^2" } data-encoding = { version = "^2" }
gloo-utils = { version = "^0", features = ["serde"] } gloo-utils = { version = "^0", features = ["serde"] }
tsify = { version = "0.4.5", features = ["js"] }
serde-wasm-bindgen = "0.5.0"
[dev-dependencies] [dev-dependencies]
wasm-bindgen-test = "^0" wasm-bindgen-test = "^0"

View File

@ -19,6 +19,7 @@ use serde::*;
use tracing_subscriber::prelude::*; use tracing_subscriber::prelude::*;
use tracing_subscriber::*; use tracing_subscriber::*;
use tracing_wasm::{WASMLayerConfigBuilder, *}; use tracing_wasm::{WASMLayerConfigBuilder, *};
use tsify::*;
use veilid_core::tools::*; use veilid_core::tools::*;
use veilid_core::*; use veilid_core::*;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -85,6 +86,7 @@ type APIResult<T> = Result<T, veilid_core::VeilidAPIError>;
const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(()); const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(());
pub fn wrap_api_future_json<F, T>(future: F) -> Promise pub fn wrap_api_future_json<F, T>(future: F) -> Promise
// Result<String, VeilidAPIError>
where where
F: Future<Output = APIResult<T>> + 'static, F: Future<Output = APIResult<T>> + 'static,
T: Serialize + Debug + 'static, T: Serialize + Debug + 'static,
@ -93,6 +95,7 @@ where
} }
pub fn wrap_api_future_plain<F, T>(future: F) -> Promise pub fn wrap_api_future_plain<F, T>(future: F) -> Promise
// Result<T, VeilidAPIError>
where where
F: Future<Output = APIResult<T>> + 'static, F: Future<Output = APIResult<T>> + 'static,
JsValue: From<T>, JsValue: From<T>,
@ -102,6 +105,7 @@ where
} }
pub fn wrap_api_future_void<F>(future: F) -> Promise pub fn wrap_api_future_void<F>(future: F) -> Promise
// Result<(), VeilidAPIError>
where where
F: Future<Output = APIResult<()>> + 'static, F: Future<Output = APIResult<()>> + 'static,
{ {
@ -111,7 +115,7 @@ where
///////////////////////////////////////// /////////////////////////////////////////
// WASM-specific // WASM-specific
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Tsify)]
pub struct VeilidWASMConfigLoggingPerformance { pub struct VeilidWASMConfigLoggingPerformance {
pub enabled: bool, pub enabled: bool,
pub level: veilid_core::VeilidConfigLogLevel, pub level: veilid_core::VeilidConfigLogLevel,
@ -119,24 +123,25 @@ pub struct VeilidWASMConfigLoggingPerformance {
pub logs_in_console: bool, pub logs_in_console: bool,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Tsify)]
pub struct VeilidWASMConfigLoggingAPI { pub struct VeilidWASMConfigLoggingAPI {
pub enabled: bool, pub enabled: bool,
pub level: veilid_core::VeilidConfigLogLevel, pub level: veilid_core::VeilidConfigLogLevel,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Tsify)]
pub struct VeilidWASMConfigLogging { pub struct VeilidWASMConfigLogging {
pub performance: VeilidWASMConfigLoggingPerformance, pub performance: VeilidWASMConfigLoggingPerformance,
pub api: VeilidWASMConfigLoggingAPI, pub api: VeilidWASMConfigLoggingAPI,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Tsify)]
#[tsify(from_wasm_abi)]
pub struct VeilidWASMConfig { pub struct VeilidWASMConfig {
pub logging: VeilidWASMConfigLogging, pub logging: VeilidWASMConfigLogging,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Tsify)]
pub struct VeilidRouteBlob { pub struct VeilidRouteBlob {
pub route_id: veilid_core::RouteId, pub route_id: veilid_core::RouteId,
#[serde(with = "veilid_core::as_human_base64")] #[serde(with = "veilid_core::as_human_base64")]
@ -152,13 +157,10 @@ pub fn initialize_veilid_wasm() {
static INITIALIZED: AtomicBool = AtomicBool::new(false); static INITIALIZED: AtomicBool = AtomicBool::new(false);
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn initialize_veilid_core(platform_config: String) { pub fn initialize_veilid_core(platform_config: VeilidWASMConfig) {
if INITIALIZED.swap(true, Ordering::Relaxed) { if INITIALIZED.swap(true, Ordering::Relaxed) {
return; return;
} }
let platform_config: VeilidWASMConfig = veilid_core::deserialize_json(&platform_config)
.expect("failed to deserialize platform config json");
// Set up subscriber and layers // Set up subscriber and layers
let subscriber = Registry::default(); let subscriber = Registry::default();
let mut layers = Vec::new(); let mut layers = Vec::new();
@ -199,9 +201,8 @@ pub fn initialize_veilid_core(platform_config: String) {
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn change_log_level(layer: String, log_level: String) { pub fn change_log_level(layer: String, log_level: VeilidConfigLogLevel) {
let layer = if layer == "all" { "".to_owned() } else { layer }; let layer = if layer == "all" { "".to_owned() } else { layer };
let log_level: veilid_core::VeilidConfigLogLevel = deserialize_json(&log_level).unwrap();
let filters = (*FILTERS).borrow(); let filters = (*FILTERS).borrow();
if layer.is_empty() { if layer.is_empty() {
// Change all layers // Change all layers
@ -215,65 +216,73 @@ pub fn change_log_level(layer: String, log_level: String) {
} }
} }
#[wasm_bindgen()] #[wasm_bindgen(typescript_custom_section)]
pub fn startup_veilid_core(update_callback_js: Function, json_config: String) -> Promise { const IUPDATE_VEILID_FUNCTION: &'static str = r#"
type UpdateVeilidFunction = (event: VeilidUpdate) => void;
"#;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Function, typescript_type = "UpdateVeilidFunction")]
pub type UpdateVeilidFunction;
}
#[wasm_bindgen]
pub async fn startup_veilid_core(
update_callback_js: UpdateVeilidFunction,
json_config: String,
) -> Result<(), VeilidAPIError> {
let update_callback_js = SendWrapper::new(update_callback_js); let update_callback_js = SendWrapper::new(update_callback_js);
wrap_api_future_void(async move { let update_callback = Arc::new(move |update: VeilidUpdate| {
let update_callback = Arc::new(move |update: VeilidUpdate| { let _ret = match Function::call1(
let _ret = &update_callback_js,
match Function::call1(&update_callback_js, &JsValue::UNDEFINED, &to_json(update)) { &JsValue::UNDEFINED,
Ok(v) => v, &to_jsvalue(update),
Err(e) => { ) {
console_log(&format!("calling update callback failed: {:?}", e)); Ok(v) => v,
return; Err(e) => {
} console_log(&format!("calling update callback failed: {:?}", e));
}; return;
}); }
};
});
if VEILID_API.borrow().is_some() { if VEILID_API.borrow().is_some() {
return Err(veilid_core::VeilidAPIError::AlreadyInitialized); return Err(veilid_core::VeilidAPIError::AlreadyInitialized);
} }
let veilid_api = veilid_core::api_startup_json(update_callback, json_config).await?; let veilid_api = veilid_core::api_startup_json(update_callback, json_config).await?;
VEILID_API.replace(Some(veilid_api)); // veilid_core::api_startup(update_callback, config_callback)
APIRESULT_UNDEFINED VEILID_API.replace(Some(veilid_api));
}) Ok(())
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn get_veilid_state() -> Promise { pub async fn get_veilid_state() -> Result<VeilidState, VeilidAPIError> {
wrap_api_future_json(async move { let veilid_api = get_veilid_api()?;
let veilid_api = get_veilid_api()?; let core_state = veilid_api.get_state().await?;
let core_state = veilid_api.get_state().await?; Ok(core_state)
APIResult::Ok(core_state)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn attach() -> Promise { pub async fn attach() -> Result<(), VeilidAPIError> {
wrap_api_future_void(async move { let veilid_api = get_veilid_api()?;
let veilid_api = get_veilid_api()?; veilid_api.attach().await?;
veilid_api.attach().await?; Ok(())
APIRESULT_UNDEFINED
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn detach() -> Promise { pub async fn detach() -> Result<(), VeilidAPIError> {
wrap_api_future_void(async move { let veilid_api = get_veilid_api()?;
let veilid_api = get_veilid_api()?; veilid_api.detach().await?;
veilid_api.detach().await?; Ok(())
APIRESULT_UNDEFINED
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn shutdown_veilid_core() -> Promise { pub async fn shutdown_veilid_core() -> Result<(), VeilidAPIError> {
wrap_api_future_void(async move { let veilid_api = take_veilid_api()?;
let veilid_api = take_veilid_api()?; veilid_api.shutdown().await;
veilid_api.shutdown().await; Ok(())
APIRESULT_UNDEFINED
})
} }
fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 { fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 {
@ -287,13 +296,11 @@ fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 {
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn routing_context() -> Promise { pub async fn routing_context() -> Result<u32, VeilidAPIError> {
wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?;
let veilid_api = get_veilid_api()?; let routing_context = veilid_api.routing_context();
let routing_context = veilid_api.routing_context(); let new_id = add_routing_context(routing_context);
let new_id = add_routing_context(routing_context); Ok(new_id)
APIResult::Ok(new_id)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]
@ -1440,12 +1447,10 @@ pub fn now() -> u64 {
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn debug(command: String) -> Promise { pub async fn debug(command: String) -> Result<String, VeilidAPIError> {
wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?;
let veilid_api = get_veilid_api()?; let out = veilid_api.debug(command).await?;
let out = veilid_api.debug(command).await?; APIResult::Ok(out)
APIResult::Ok(out)
})
} }
#[wasm_bindgen()] #[wasm_bindgen()]