diff --git a/crates/matrix-sdk-crypto-js/Cargo.toml b/crates/matrix-sdk-crypto-js/Cargo.toml index 7efccb75b..6080b04ca 100644 --- a/crates/matrix-sdk-crypto-js/Cargo.toml +++ b/crates/matrix-sdk-crypto-js/Cargo.toml @@ -22,21 +22,20 @@ wasm-opt = ['-Oz'] crate-type = ["cdylib"] [features] -default = ["js"] +default = [] qrcode = ["matrix-sdk-crypto/qrcode"] -backups_v1 = ["matrix-sdk-crypto/backups_v1"] docsrs = [] js = [] -nodejs = [] +nodejs = ["napi", "napi-derive"] [dependencies] matrix-sdk-crypto = { version = "0.5.0", path = "../matrix-sdk-crypto" } -http = "0.2.6" -serde_json = "1.0.79" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] ruma = { version = "0.6.2", features = ["client-api-c", "rand", "unstable-msc2676", "unstable-msc2677"] } vodozemac = "0.2.0" +napi = { version = "2.4.3", default-features = false, features = ["napi4"], optional = true } +napi-derive = { version = "2.4.1", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] ruma = { version = "0.6.2", features = ["client-api-c", "js", "rand", "unstable-msc2676", "unstable-msc2677"] } @@ -44,4 +43,9 @@ vodozemac = { version = "0.2.0", features = ["js"] } wasm-bindgen = "0.2.80" wasm-bindgen-futures = "0.4.30" js-sys = "0.3.49" -anyhow = "1.0" \ No newline at end of file +serde_json = "1.0.79" +http = "0.2.6" +anyhow = "1.0" + +[target.'cfg(not(target_arch = "wasm32"))'.build-dependencies] +napi-build = "2.0.0" \ No newline at end of file diff --git a/crates/matrix-sdk-crypto-js/build.rs b/crates/matrix-sdk-crypto-js/build.rs new file mode 100644 index 000000000..46bfd15de --- /dev/null +++ b/crates/matrix-sdk-crypto-js/build.rs @@ -0,0 +1,7 @@ +#[cfg(feature = "nodejs")] +fn main() { + napi_build::setup(); +} + +#[cfg(not(feature = "nodejs"))] +fn main() {} diff --git a/crates/matrix-sdk-crypto-js/nodejs/Makefile b/crates/matrix-sdk-crypto-js/nodejs/Makefile new file mode 100644 index 000000000..2bdfdad2c --- /dev/null +++ b/crates/matrix-sdk-crypto-js/nodejs/Makefile @@ -0,0 +1,5 @@ +build: + cd .. && napi build --platform --release --features nodejs + +test: + echo 'nop' diff --git a/crates/matrix-sdk-crypto-js/package.json b/crates/matrix-sdk-crypto-js/package.json new file mode 100644 index 000000000..be38b651c --- /dev/null +++ b/crates/matrix-sdk-crypto-js/package.json @@ -0,0 +1,27 @@ +{ + "name": "matrix-sdk-crypto", + "version": "0.5.0", + "main": "index.js", + "types": "index.d.ts", + "napi": { + "name": "matrix-sdk-crypto", + "triples": { + "additional": [ + "aarch64-apple-darwin" + ] + } + }, + "license": "MIT", + "devDependencies": { + "@napi-rs/cli": "^2.9.0", + "ava": "^4.2.0" + }, + "engines": { + "node": ">= 10" + }, + "scripts": { + "artifacts": "napi artifacts", + "build": "napi build --platform --release", + "test": "ava" + } +} diff --git a/crates/matrix-sdk-crypto-js/src/errors.rs b/crates/matrix-sdk-crypto-js/src/errors.rs new file mode 100644 index 000000000..cec0d369a --- /dev/null +++ b/crates/matrix-sdk-crypto-js/src/errors.rs @@ -0,0 +1,16 @@ +#[cfg(feature = "js")] +pub type Error = wasm_bindgen::JsError; + +#[cfg(feature = "nodejs")] +#[derive(Debug)] +pub struct Error(napi::Error); + +#[cfg(feature = "nodejs")] +impl From for Error +where + E: std::error::Error, +{ + fn from(error: E) -> Self { + Self(napi::Error::from_reason(error.to_string())) + } +} diff --git a/crates/matrix-sdk-crypto-js/src/lib.rs b/crates/matrix-sdk-crypto-js/src/lib.rs index acc9d95bc..765a32557 100644 --- a/crates/matrix-sdk-crypto-js/src/lib.rs +++ b/crates/matrix-sdk-crypto-js/src/lib.rs @@ -15,11 +15,19 @@ #![doc = include_str!("../README.md")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![warn(missing_docs, missing_debug_implementations)] + +#[cfg(all(not(feature = "js"), not(feature = "nodejs")))] +compile_error!("One of the following features must be enabled: `js` or `nodejs`"); + +#[cfg(all(feature = "js", feature = "nodejs"))] +compile_error!("The `js` and `nodejs` features are mutually exclusive."); + #[cfg(all(feature = "js", not(target_arch = "wasm32")))] compile_error!( "The `js` feature must be enabled only for the `wasm32` target (either `wasm32-unknown-unknown` or `wasm32-wasi`)." ); +//mod errors; pub mod events; mod future; pub mod identifiers; @@ -36,6 +44,7 @@ use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*}; /// https://github.com/rustwasm/wasm-bindgen/issues/2231#issuecomment-656293288. /// /// The returned value is a likely to be `wasm_bindgen::__ref::Ref`. +#[cfg(feature = "js")] fn downcast(value: &JsValue, classname: &str) -> Result where T: RefFromWasmAbi,