diff --git a/keyserver/addons/rust-node-addon/index.js b/keyserver/addons/rust-node-addon/index.js --- a/keyserver/addons/rust-node-addon/index.js +++ b/keyserver/addons/rust-node-addon/index.js @@ -3,24 +3,15 @@ import invariant from 'invariant'; import { createRequire } from 'module'; +import type { RustNativeBindingAPI } from 'lib/types/rust-binding-types.js'; + const { platform, arch } = process; const importMetaURL = import.meta.url; invariant(importMetaURL, 'import.meta.url should be set'); const require = createRequire(importMetaURL); -type RustAPI = { - +registerUser: ( - userId: string, - deviceId: string, - username: string, - password: string, - userPublicKey: string, - ) => Promise, - +deleteUser: (userId: string) => Promise, -}; - -async function getRustAPI(): Promise { +async function getRustAPI(): Promise { let nativeBinding = null; if (platform === 'darwin' && arch === 'x64') { // $FlowFixMe @@ -39,11 +30,10 @@ } if (!nativeBinding) { - throw new Error('Failed to load native binding'); + throw new Error('Failed to load Rust native binding'); } - const { registerUser, deleteUser } = nativeBinding; - return { registerUser, deleteUser }; + return nativeBinding.default; } export { getRustAPI }; diff --git a/lib/types/rust-binding-types.js b/lib/types/rust-binding-types.js new file mode 100644 --- /dev/null +++ b/lib/types/rust-binding-types.js @@ -0,0 +1,31 @@ +// @flow + +type tunnelBrokerOnReceiveCallback = ( + err: Error | null, + payload: string, +) => any; + +interface TunnelbrokerClientClass { + new( + deviceId: string, + onReceiveCallback: tunnelBrokerOnReceiveCallback, + ): TunnelbrokerClientClass; + publish(toDeviceId: string, payload: string): Promise; +} + +type RustNativeBindingAPI = { + +registerUser: ( + userId: string, + deviceId: string, + username: string, + password: string, + userPublicKey: string, + ) => Promise, + +deleteUser: (userId: string) => Promise, + +TunnelbrokerClient: ( + deviceId: string, + onReceiveCallback: tunnelBrokerOnReceiveCallback, + ) => TunnelbrokerClientClass, +}; + +export type { RustNativeBindingAPI };