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 @@ -38,4 +38,38 @@ return { registerUser }; } -export { getIdentityRustAPI }; +interface TunnelbrokerClientClass { + new( + onReceiveCallback: (err: Error | null, value: string) => any, + ): TunnelbrokerClientClass; + publish(toDeviceId: string, payload: string): Promise; +} + +async function getTunnelbrokerRustClient(): Promise { + let nativeBinding = null; + if (platform === 'darwin' && arch === 'x64') { + // $FlowFixMe + nativeBinding = await import('./naupi/rust-node-addon.darwin-x64.node'); + } else if (platform === 'darwin' && arch === 'arm64') { + // $FlowFixMe + nativeBinding = await import('./napi/rust-node-addon.darwin-arm64.node'); + } else if (platform === 'linux' && arch === 'x64') { + // $FlowFixMe + nativeBinding = await import('./napi/rust-node-addon.linux-x64-gnu.node'); + } else if (platform === 'linux' && arch === 'arm64') { + // $FlowFixMe + nativeBinding = await import('./napi/rust-node-addon.linux-arm64-gnu.node'); + } else { + throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`); + } + + if (!nativeBinding) { + throw new Error('Failed to load native binding'); + } + + const { TunnelbrokerClient } = nativeBinding.default; + // $FlowFixMe + return { TunnelbrokerClient }; +} + +export { getIdentityRustAPI, getTunnelbrokerRustClient };