Skip to content

Commit

Permalink
fix: properly type object returns
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyynthia committed Apr 11, 2023
1 parent d9e8353 commit 93255d1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
10 changes: 10 additions & 0 deletions client/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
Expand Down
9 changes: 9 additions & 0 deletions client/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions server/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 93255d1

Please sign in to comment.