Refactor typescript types, upgrade tsify

This commit is contained in:
Brandon Vandegrift 2025-05-02 20:18:30 -04:00
parent 9e8b8f8ea3
commit eb514f1221
20 changed files with 76 additions and 127 deletions

View file

@ -27,6 +27,7 @@
- Expose the is_shutdown API: https://gitlab.com/veilid/veilid/-/merge_requests/392
- veilid-wasm:
- **Breaking** Properly generate TypeScript types for `ValueSubkeyRangeSet`, which would previously resolve to `any`. This is breaking since it can cause type errors to correctly surface in existing applications. ([!397](https://gitlab.com/veilid/veilid/-/merge_requests/397))
- Expose the isShutdown API: https://gitlab.com/veilid/veilid/-/merge_requests/392
- CI:

57
Cargo.lock generated
View file

@ -2288,19 +2288,6 @@ dependencies = [
"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]]
name = "gloo-utils"
version = "0.2.0"
@ -4959,7 +4946,7 @@ checksum = "32e265784ad618884abaea0600a9adf15393368d840e0222d101a072f3f7534d"
dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals 0.29.1",
"serde_derive_internals",
"syn 2.0.101",
]
@ -5073,17 +5060,6 @@ dependencies = [
"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]]
name = "serde-wasm-bindgen"
version = "0.6.5"
@ -5125,17 +5101,6 @@ dependencies = [
"syn 2.0.101",
]
[[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.101",
]
[[package]]
name = "serde_derive_internals"
version = "0.29.1"
@ -6196,13 +6161,13 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "tsify"
version = "0.4.5"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6b26cf145f2f3b9ff84e182c448eaf05468e247f148cf3d2a7d67d78ff023a0"
checksum = "b2ec91b85e6c6592ed28636cb1dd1fac377ecbbeb170ff1d79f97aac5e38926d"
dependencies = [
"gloo-utils 0.1.7",
"gloo-utils",
"serde",
"serde-wasm-bindgen 0.5.0",
"serde-wasm-bindgen",
"serde_json",
"tsify-macros",
"wasm-bindgen",
@ -6210,13 +6175,13 @@ dependencies = [
[[package]]
name = "tsify-macros"
version = "0.4.5"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a94b0f0954b3e59bfc2c246b4c8574390d94a4ad4ad246aaf2fb07d7dfd3b47"
checksum = "9a324606929ad11628a19206d7853807481dcaecd6c08be70a235930b8241955"
dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals 0.28.0",
"serde_derive_internals",
"syn 2.0.101",
]
@ -6542,7 +6507,7 @@ dependencies = [
"send_wrapper 0.6.0",
"serde",
"serde-big-array",
"serde-wasm-bindgen 0.6.5",
"serde-wasm-bindgen",
"serde_bytes",
"serde_json",
"serde_with",
@ -6789,13 +6754,13 @@ dependencies = [
"console_error_panic_hook",
"data-encoding",
"futures-util",
"gloo-utils 0.2.0",
"gloo-utils",
"js-sys",
"lazy_static",
"parking_lot 0.12.3",
"send_wrapper 0.6.0",
"serde",
"serde-wasm-bindgen 0.6.5",
"serde-wasm-bindgen",
"serde_bytes",
"serde_json",
"tracing",

View file

@ -209,7 +209,7 @@ send_wrapper = { version = "0.6.0", features = ["futures"] }
serde_bytes = { version = "0.11", default-features = false, features = [
"alloc",
] }
tsify = { version = "0.4.5", features = ["js"] }
tsify = { version = "0.5.5", features = ["js"] }
serde-wasm-bindgen = "0.6.5"
# Network

View file

@ -78,24 +78,21 @@ where
macro_rules! byte_array_type {
($name:ident, $size:expr, $encoded_size:expr) => {
#[derive(Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq)]
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
#[cfg_attr(
all(target_arch = "wasm32", target_os = "unknown"),
derive(Tsify),
tsify(into_wasm_abi)
tsify(from_wasm_abi, into_wasm_abi)
)]
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), serde(transparent))]
#[must_use]
pub struct $name {
#[cfg_attr(
all(target_arch = "wasm32", target_os = "unknown"),
tsify(type = "string")
)]
pub bytes: [u8; $size],
}
impl Default for $name {
fn default() -> Self {
Self {
bytes: [0u8; $size],
}
}
}
impl serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -119,6 +116,14 @@ macro_rules! byte_array_type {
}
}
impl Default for $name {
fn default() -> Self {
Self {
bytes: [0u8; $size],
}
}
}
impl $name {
pub fn new(bytes: [u8; $size]) -> Self {
Self { bytes }

View file

@ -20,6 +20,15 @@ where
pub value: K,
}
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
#[wasm_bindgen(typescript_custom_section)]
const CRYPOTYPED_TYPE: &'static str = r#"
export type CryptoTyped<TCryptoKey extends string> = `${FourCC}:${TCryptoKey}`;
"#;
}
}
impl<K> CryptoTyped<K>
where
K: Clone

View file

@ -19,6 +19,15 @@ where
items: Vec<CryptoTyped<K>>,
}
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
#[wasm_bindgen(typescript_custom_section)]
const CRYPOTYPEDGROUP_TYPE: &'static str = r#"
export type CryptoTypedGroup<TCryptoKey extends string> = Array<CryptoTyped<TCryptoKey>>;
"#;
}
}
impl<K> CryptoTypedGroup<K>
where
K: Clone

View file

@ -1,17 +1,20 @@
use super::*;
#[derive(Clone, Copy, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
#[cfg_attr(
all(target_arch = "wasm32", target_os = "unknown"),
derive(Tsify),
tsify(from_wasm_abi, into_wasm_abi)
)]
#[must_use]
pub struct KeyPair {
pub key: PublicKey,
pub secret: SecretKey,
}
from_impl_to_jsvalue!(KeyPair);
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
#[wasm_bindgen(typescript_custom_section)]
const KEYPAIR_TYPE: &'static str = r#"
export type KeyPair = `${PublicKey}:${SecretKey}`;
"#;
}
}
impl KeyPair {
pub fn new(key: PublicKey, secret: SecretKey) -> Self {

View file

@ -55,7 +55,6 @@ mod storage_manager;
mod table_store;
mod veilid_api;
mod veilid_config;
mod wasm_helpers;
pub(crate) use self::component::*;
pub(crate) use self::core_context::RegisteredComponents;
@ -122,4 +121,10 @@ use stop_token::*;
use thiserror::Error as ThisError;
use tracing::*;
use veilid_tools::*;
use wasm_helpers::*;
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
pub use wasm_bindgen::prelude::*;
pub use tsify::*;
}
}

View file

@ -151,7 +151,6 @@ pub enum VeilidAPIError {
#[error("Generic: {message}")]
Generic { message: String },
}
from_impl_to_jsvalue!(VeilidAPIError);
impl VeilidAPIError {
pub fn not_initialized() -> Self {

View file

@ -23,7 +23,6 @@ pub struct DHTRecordDescriptor {
/// The schema in use associated with the key
schema: DHTSchema,
}
from_impl_to_jsvalue!(DHTRecordDescriptor);
impl DHTRecordDescriptor {
pub(crate) fn new(

View file

@ -20,7 +20,6 @@ pub struct DHTRecordReport {
/// The sequence numbers of each subkey requested from the DHT over the network
network_seqs: Vec<Option<ValueSeqNum>>,
}
from_impl_to_jsvalue!(DHTRecordReport);
impl DHTRecordReport {
pub(crate) fn new(

View file

@ -29,7 +29,6 @@ pub struct ValueData {
#[schemars(with = "String")]
writer: PublicKey,
}
from_impl_to_jsvalue!(ValueData);
impl ValueData {
pub const MAX_LEN: usize = 32768;

View file

@ -15,6 +15,10 @@ use range_set_blaze::*;
pub struct ValueSubkeyRangeSet {
#[serde(with = "serialize_range_set_blaze")]
#[schemars(with = "Vec<(u32,u32)>")]
#[cfg_attr(
all(target_arch = "wasm32", target_os = "unknown"),
tsify(type = "Array<[ValueSubkey, ValueSubkey]>")
)]
data: RangeSetBlaze<ValueSubkey>,
}

View file

@ -9,6 +9,15 @@ use super::*;
#[must_use]
pub struct FourCC(pub [u8; 4]);
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
#[wasm_bindgen(typescript_custom_section)]
const FOURCC_TYPE: &'static str = r#"
export type FourCC = string;
"#;
}
}
impl From<[u8; 4]> for FourCC {
fn from(b: [u8; 4]) -> Self {
Self(b)

View file

@ -197,7 +197,6 @@ pub enum VeilidUpdate {
ValueChange(Box<VeilidValueChange>),
Shutdown,
}
from_impl_to_jsvalue!(VeilidUpdate);
/// A queriable state of the internals of veilid-core.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
@ -212,4 +211,3 @@ pub struct VeilidState {
pub network: Box<VeilidStateNetwork>,
pub config: Box<VeilidStateConfig>,
}
from_impl_to_jsvalue!(VeilidState);

View file

@ -1,21 +0,0 @@
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
pub use tsify::*;
pub use wasm_bindgen::prelude::*;
macro_rules! from_impl_to_jsvalue {
($name: ident) => {
impl From<$name> for JsValue {
fn from(value: $name) -> Self {
serde_wasm_bindgen::to_value(&value).unwrap()
}
}
}
}
} else {
macro_rules! from_impl_to_jsvalue {
($name: ident) => {}
}
}
}
pub(crate) use from_impl_to_jsvalue;

View file

@ -41,7 +41,7 @@ send_wrapper = "^0"
futures-util = { version = "^0" }
data-encoding = { version = "^2" }
gloo-utils = { version = "^0", features = ["serde"] }
tsify = { version = "0.4.5", features = ["js"] }
tsify = { version = "0.5.5", features = ["js"] }
serde-wasm-bindgen = "0.6.5"
[dev-dependencies]

View file

@ -24,7 +24,6 @@ use tsify::*;
use veilid_core::*;
use veilid_core::{tools::*, VeilidAPIError};
use veilid_tracing_wasm::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::*;
pub mod veilid_client_js;
@ -184,7 +183,6 @@ pub struct VeilidRouteBlob {
)]
pub blob: Vec<u8>,
}
from_impl_to_jsvalue!(VeilidRouteBlob);
// WASM Bindings

View file

@ -4,17 +4,6 @@ use super::*;
#[wasm_bindgen(typescript_custom_section)]
const IUPDATE_VEILID_FUNCTION: &'static str = r#"
export type UpdateVeilidFunction = (event: VeilidUpdate) => void;
// Type overrides for structs that always get serialized by serde.
export type CryptoKey = string;
export type Nonce = string;
export type Signature = string;
export type KeyPair = `${PublicKey}:${SecretKey}`;
export type FourCC = "NONE" | "VLD0" | string;
export type CryptoTyped<TCryptoKey extends string> = `${FourCC}:${TCryptoKey}`;
export type CryptoTypedGroup<TCryptoKey extends string> = Array<CryptoTyped<TCryptoKey>>;
export
"#;
#[wasm_bindgen]

View file

@ -1,26 +1,5 @@
use super::*;
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
pub use wasm_bindgen::prelude::*;
macro_rules! from_impl_to_jsvalue {
($name: ident) => {
impl From<$name> for JsValue {
fn from(value: $name) -> Self {
serde_wasm_bindgen::to_value(&value).unwrap()
}
}
}
}
} else {
macro_rules! from_impl_to_jsvalue {
($name: ident) => {}
}
}
}
pub(crate) use from_impl_to_jsvalue;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "string[]")]