From 93255d17b8bf7ba07b6f5f53f4b0561e1d383ce8 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 11 Apr 2023 17:28:57 +0200 Subject: [PATCH] fix: properly type object returns --- Justfile | 17 ++++++++++++++++- client/src/login.rs | 10 ++++++++++ client/src/registration.rs | 9 +++++++++ server/src/login.rs | 8 ++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Justfile b/Justfile index b79bf3c..8c8d6e6 100644 --- a/Justfile +++ b/Justfile @@ -1,4 +1,4 @@ -build: +build: && _fix-typescript RUSTFLAGS='-C opt-level=s' wasm-pack build -s squirrelchat --release client RUSTFLAGS='-C opt-level=3' wasm-pack build -s squirrelchat --release --target nodejs server @@ -9,3 +9,18 @@ lint: cargo deny check cargo clippy rustfmt --check client/**/*.rs core/**/*.rs server/**/*.rs + +# Replace `object` with proper types, something wasm-bindgen doesn't support because it's a piece of [redacted] +# Oen could use skip_typescript and write all the types manually, but let's just do it this way for now eh. +_fix-typescript: + #!/usr/bin/env node + const { readFileSync, writeFileSync } = require('fs') + let clientTypes = readFileSync('client/pkg/opaque_wasm_client.d.ts', 'utf8') + let serverTypes = readFileSync('server/pkg/opaque_wasm_server.d.ts', 'utf8') + + clientTypes = clientTypes.replace('object', 'ClientLoginResult').replace('object', 'ClientLoginResult') + clientTypes = clientTypes.replace('object', 'ClientRegistrationResult').replace('object', 'ClientRegistrationResult') + serverTypes = serverTypes.replace('object', 'ServerRegistration').replace('object', 'ServerRegistration') + + writeFileSync('client/pkg/opaque_wasm_client.d.ts', clientTypes) + writeFileSync('server/pkg/opaque_wasm_server.d.ts', serverTypes) diff --git a/client/src/login.rs b/client/src/login.rs index 6564e22..e7cce17 100644 --- a/client/src/login.rs +++ b/client/src/login.rs @@ -36,6 +36,16 @@ use rand::rngs::OsRng; use wasm_bindgen::prelude::*; +#[wasm_bindgen(typescript_custom_section)] +const TS_APPEND_CONTENT: &'static str = r#" +export type ClientLoginResult = { + message: Uint8Array, + serverPublicKey: Uint8Array, + sessionKey: Uint8Array, + exportKey: Uint8Array, +} +"#; + #[wasm_bindgen] pub struct ClientLogin { password: Vec, diff --git a/client/src/registration.rs b/client/src/registration.rs index ae37c85..beef66d 100644 --- a/client/src/registration.rs +++ b/client/src/registration.rs @@ -36,6 +36,15 @@ use rand::rngs::OsRng; use wasm_bindgen::prelude::*; +#[wasm_bindgen(typescript_custom_section)] +const TS_APPEND_CONTENT: &'static str = r#" +export type ClientRegistrationResult = { + exportKey: Uint8Array, + serverPublicKey: Uint8Array, + record: Uint8Array, +} +"#; + #[wasm_bindgen] pub struct ClientRegistration { rng: OsRng, diff --git a/server/src/login.rs b/server/src/login.rs index cd6d87a..9272077 100644 --- a/server/src/login.rs +++ b/server/src/login.rs @@ -35,6 +35,14 @@ use opaque_wasm_core::OpaqueWasmCipherSuite; use wasm_bindgen::prelude::*; +#[wasm_bindgen(typescript_custom_section)] +const TS_APPEND_CONTENT: &'static str = r#" +export type ServerRegistration = { + response: Uint8Array, + state: Uint8Array, +} +"#; + #[wasm_bindgen] impl Server { #[wasm_bindgen(js_name = "startLogin")]