use crate::derec_message;
use crate::interop::wasm::primitives::helpers::to_js;
use crate::interop::wasm::ts_bindings_utils::{js_error, js_error_from_lib};
use serde::Serialize;
use wasm_bindgen::prelude::*;
#[derive(Serialize)]
struct MessageId {
#[serde(with = "serde_bytes")]
id: Vec<u8>,
timestamp: u64,
}
#[wasm_bindgen(js_name = "envelope_apply_trace_id")]
pub fn apply_trace_id(envelope_bytes: &[u8], trace_id: u64) -> Result<Vec<u8>, JsValue> {
derec_message::apply_trace_id(envelope_bytes, trace_id)
.map_err(|e| js_error("envelope_decode_error", e.to_string()))
}
#[wasm_bindgen(js_name = "envelope_read_trace_id")]
pub fn read_trace_id(envelope_bytes: &[u8]) -> Result<u64, JsValue> {
derec_message::read_trace_id(envelope_bytes)
.map_err(|e| js_error("envelope_decode_error", e.to_string()))
}
#[wasm_bindgen(js_name = "envelope_message_id")]
pub fn message_id(envelope_bytes: &[u8]) -> Result<JsValue, JsValue> {
let identity = derec_message::message_id(envelope_bytes).map_err(js_error_from_lib)?;
to_js(&MessageId {
id: identity.id.to_vec(),
timestamp: identity.timestamp,
})
}