Change encoding/decoding

This commit is contained in:
Andras Schmelczer 2025-01-05 13:32:21 +00:00
parent b549bb96b6
commit 9db478bc23
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 7 additions and 5 deletions

View file

@ -10,7 +10,7 @@
//! - `errors`: Contains error types used in this crate. //! - `errors`: Contains error types used in this crate.
use core::str; use core::str;
use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _}; use base64::{engine::general_purpose::STANDARD, Engine as _};
use errors::SyncLibError; use errors::SyncLibError;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -19,12 +19,12 @@ pub mod errors;
/// Encode binary data for easy transport over HTTP. Inverse of /// Encode binary data for easy transport over HTTP. Inverse of
/// `base64_to_bytes`. /// `base64_to_bytes`.
#[wasm_bindgen(js_name = bytesToBase64)] #[wasm_bindgen(js_name = bytesToBase64)]
pub fn bytes_to_base64(input: &[u8]) -> String { STANDARD_NO_PAD.encode(input) } pub fn bytes_to_base64(input: &[u8]) -> String { STANDARD.encode(input) }
/// Inverse of `bytes_to_base64`. /// Inverse of `bytes_to_base64`.
#[wasm_bindgen(js_name = base64ToBytes)] #[wasm_bindgen(js_name = base64ToBytes)]
pub fn base64_to_bytes(input: &str) -> Result<Vec<u8>, SyncLibError> { pub fn base64_to_bytes(input: &str) -> Result<Vec<u8>, SyncLibError> {
STANDARD_NO_PAD.decode(input).map_err(SyncLibError::from) STANDARD.decode(input).map_err(SyncLibError::from)
} }
/// Merge two documents with a common parent. Relies on `reconcile::reconcile` /// Merge two documents with a common parent. Relies on `reconcile::reconcile`

View file

@ -4,16 +4,18 @@ use insta::assert_debug_snapshot;
use sync_lib::*; use sync_lib::*;
use wasm_bindgen_test::*; use wasm_bindgen_test::*;
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test(unsupported = test)] #[wasm_bindgen_test(unsupported = test)]
fn test_bytes_to_base64() { fn test_bytes_to_base64() {
let input = b"hello"; let input = b"hello";
let expected = "aGVsbG8"; let expected = "aGVsbG8=";
assert_eq!(bytes_to_base64(input), expected); assert_eq!(bytes_to_base64(input), expected);
} }
#[wasm_bindgen_test(unsupported = test)] #[wasm_bindgen_test(unsupported = test)]
fn test_base64_to_bytes() { fn test_base64_to_bytes() {
let input = "aGVsbG8"; let input = "aGVsbG8=";
let expected = b"hello".to_vec(); let expected = b"hello".to_vec();
assert_eq!(base64_to_bytes(input).unwrap(), expected); assert_eq!(base64_to_bytes(input).unwrap(), expected);
} }