From e5a7a975a30586cdccc82613f76b866a8889029a Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 5 Jul 2022 11:58:15 +0200 Subject: [PATCH 1/2] feat(bindings/crypto-nodejs): Transform `timeout` into milliseconds. --- .../matrix-sdk-crypto-nodejs/src/requests.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/bindings/matrix-sdk-crypto-nodejs/src/requests.rs b/bindings/matrix-sdk-crypto-nodejs/src/requests.rs index e7b536ae9..222cd78f9 100644 --- a/bindings/matrix-sdk-crypto-nodejs/src/requests.rs +++ b/bindings/matrix-sdk-crypto-nodejs/src/requests.rs @@ -1,5 +1,7 @@ //! Types to handle requests. +use std::time::Duration; + use matrix_sdk_crypto::requests::{ KeysBackupRequest as RumaKeysBackupRequest, KeysQueryRequest as RumaKeysQueryRequest, RoomMessageRequest as RumaRoomMessageRequest, ToDeviceRequest as RumaToDeviceRequest, @@ -220,7 +222,7 @@ impl KeysBackupRequest { } macro_rules! request { - ($request:ident from $ruma_request:ident maps fields $( $field:ident ),+ $(,)? ) => { + ($request:ident from $ruma_request:ident maps fields $( $field:ident $( { $transformation:expr } )? ),+ $(,)? ) => { impl TryFrom<(String, &$ruma_request)> for $request { type Error = serde_json::Error; @@ -229,7 +231,15 @@ macro_rules! request { ) -> Result { let mut map = serde_json::Map::new(); $( - map.insert(stringify!($field).to_owned(), serde_json::to_value(&request.$field)?); + let field = &request.$field; + $( + let field = { + let $field = field; + + $transformation + }; + )? + map.insert(stringify!($field).to_owned(), serde_json::to_value(field)?); )+ let value = serde_json::Value::Object(map); @@ -243,8 +253,8 @@ macro_rules! request { } request!(KeysUploadRequest from RumaKeysUploadRequest maps fields device_keys, one_time_keys, fallback_keys); -request!(KeysQueryRequest from RumaKeysQueryRequest maps fields timeout, device_keys, token); -request!(KeysClaimRequest from RumaKeysClaimRequest maps fields timeout, one_time_keys); +request!(KeysQueryRequest from RumaKeysQueryRequest maps fields timeout { timeout.as_ref().map(Duration::as_millis) }, device_keys, token); +request!(KeysClaimRequest from RumaKeysClaimRequest maps fields timeout { timeout.as_ref().map(Duration::as_millis) }, one_time_keys); request!(ToDeviceRequest from RumaToDeviceRequest maps fields event_type, txn_id, messages); request!(SignatureUploadRequest from RumaSignatureUploadRequest maps fields signed_keys); request!(RoomMessageRequest from RumaRoomMessageRequest maps fields room_id, txn_id, content); From 29c10b842494141223d5b07647659929b277707e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 7 Jul 2022 11:12:12 +0200 Subject: [PATCH 2/2] feat(bindings/crypto-nodejs): Convert timeout from u128 to u64. First, u128 has a bug in `serde`, cf. https://github.com/serde-rs/json/issues/625. Second, we don't need to represent the timeout as a u128, it's clearly too large. This patch tries to convert it to u64. It should never fail, but we propagate the error anyway. --- bindings/matrix-sdk-crypto-nodejs/src/machine.rs | 3 +-- bindings/matrix-sdk-crypto-nodejs/src/requests.rs | 14 ++++++++------ .../tests/requests.test.js | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bindings/matrix-sdk-crypto-nodejs/src/machine.rs b/bindings/matrix-sdk-crypto-nodejs/src/machine.rs index 3899981df..9128f2f4f 100644 --- a/bindings/matrix-sdk-crypto-nodejs/src/machine.rs +++ b/bindings/matrix-sdk-crypto-nodejs/src/machine.rs @@ -215,8 +215,7 @@ impl OlmMachine { .into_iter() .map(requests::OutgoingRequest) .map(TryFrom::try_from) - .collect::, _>>() - .map_err(into_err) + .collect() } /// Mark the request with the given request ID as sent. diff --git a/bindings/matrix-sdk-crypto-nodejs/src/requests.rs b/bindings/matrix-sdk-crypto-nodejs/src/requests.rs index 222cd78f9..9d1e6376d 100644 --- a/bindings/matrix-sdk-crypto-nodejs/src/requests.rs +++ b/bindings/matrix-sdk-crypto-nodejs/src/requests.rs @@ -14,6 +14,8 @@ use ruma::api::client::keys::{ upload_signatures::v3::Request as RumaSignatureUploadRequest, }; +use crate::into_err; + /// Data for a request to the `/keys/upload` API endpoint /// ([specification]). /// @@ -224,7 +226,7 @@ impl KeysBackupRequest { macro_rules! request { ($request:ident from $ruma_request:ident maps fields $( $field:ident $( { $transformation:expr } )? ),+ $(,)? ) => { impl TryFrom<(String, &$ruma_request)> for $request { - type Error = serde_json::Error; + type Error = napi::Error; fn try_from( (request_id, request): (String, &$ruma_request), @@ -239,13 +241,13 @@ macro_rules! request { $transformation }; )? - map.insert(stringify!($field).to_owned(), serde_json::to_value(field)?); + map.insert(stringify!($field).to_owned(), serde_json::to_value(field).map_err(into_err)?); )+ let value = serde_json::Value::Object(map); Ok($request { id: request_id, - body: serde_json::to_string(&value)?.into(), + body: serde_json::to_string(&value).map_err(into_err)?.into(), }) } } @@ -253,8 +255,8 @@ macro_rules! request { } request!(KeysUploadRequest from RumaKeysUploadRequest maps fields device_keys, one_time_keys, fallback_keys); -request!(KeysQueryRequest from RumaKeysQueryRequest maps fields timeout { timeout.as_ref().map(Duration::as_millis) }, device_keys, token); -request!(KeysClaimRequest from RumaKeysClaimRequest maps fields timeout { timeout.as_ref().map(Duration::as_millis) }, one_time_keys); +request!(KeysQueryRequest from RumaKeysQueryRequest maps fields timeout { timeout.as_ref().map(Duration::as_millis).map(u64::try_from).transpose().map_err(into_err)? }, device_keys, token); +request!(KeysClaimRequest from RumaKeysClaimRequest maps fields timeout { timeout.as_ref().map(Duration::as_millis).map(u64::try_from).transpose().map_err(into_err)? }, one_time_keys); request!(ToDeviceRequest from RumaToDeviceRequest maps fields event_type, txn_id, messages); request!(SignatureUploadRequest from RumaSignatureUploadRequest maps fields signed_keys); request!(RoomMessageRequest from RumaRoomMessageRequest maps fields room_id, txn_id, content); @@ -273,7 +275,7 @@ pub type OutgoingRequests = Either7< pub(crate) struct OutgoingRequest(pub(crate) matrix_sdk_crypto::OutgoingRequest); impl TryFrom for OutgoingRequests { - type Error = serde_json::Error; + type Error = napi::Error; fn try_from(outgoing_request: OutgoingRequest) -> Result { let request_id = outgoing_request.0.request_id().to_string(); diff --git a/bindings/matrix-sdk-crypto-nodejs/tests/requests.test.js b/bindings/matrix-sdk-crypto-nodejs/tests/requests.test.js index 79b43e662..96cf946b3 100644 --- a/bindings/matrix-sdk-crypto-nodejs/tests/requests.test.js +++ b/bindings/matrix-sdk-crypto-nodejs/tests/requests.test.js @@ -26,5 +26,4 @@ for (const request of [ expect(() => { new (request)() }).toThrow(); }); }) - }