feat(crypto-js): Start adding support for NodeJS.

This commit is contained in:
Ivan Enderlin
2022-05-23 15:40:37 +02:00
parent 250b85dc79
commit 6c472f873f
6 changed files with 74 additions and 6 deletions
+10 -6
View File
@@ -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"
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"
+7
View File
@@ -0,0 +1,7 @@
#[cfg(feature = "nodejs")]
fn main() {
napi_build::setup();
}
#[cfg(not(feature = "nodejs"))]
fn main() {}
@@ -0,0 +1,5 @@
build:
cd .. && napi build --platform --release --features nodejs
test:
echo 'nop'
+27
View File
@@ -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"
}
}
+16
View File
@@ -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<E> From<E> for Error
where
E: std::error::Error,
{
fn from(error: E) -> Self {
Self(napi::Error::from_reason(error.to_string()))
}
}
+9
View File
@@ -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<T>`.
#[cfg(feature = "js")]
fn downcast<T>(value: &JsValue, classname: &str) -> Result<T::Anchor, JsError>
where
T: RefFromWasmAbi<Abi = u32>,