| 40 | } |
| 41 | |
| 42 | class WshClient { |
| 43 | routeId: string; |
| 44 | openRpcs: Map<string, ClientRpcEntry> = new Map(); |
| 45 | |
| 46 | constructor(routeId: string) { |
| 47 | this.routeId = routeId; |
| 48 | } |
| 49 | |
| 50 | wshRpcCall(command: string, data: any, opts: RpcOpts): Promise<any> { |
| 51 | const msg: RpcMessage = { |
| 52 | command: command, |
| 53 | data: data, |
| 54 | source: this.routeId, |
| 55 | }; |
| 56 | if (!opts?.noresponse) { |
| 57 | msg.reqid = crypto.randomUUID(); |
| 58 | } |
| 59 | if (opts?.timeout) { |
| 60 | msg.timeout = opts.timeout; |
| 61 | } |
| 62 | if (opts?.route) { |
| 63 | msg.route = opts.route; |
| 64 | } |
| 65 | const rpcGen = sendRpcCommand(this.openRpcs, msg); |
| 66 | if (rpcGen == null) { |
| 67 | return null; |
| 68 | } |
| 69 | const respMsgPromise = rpcGen.next(true); // pass true to force termination of rpc after 1 response (not streaming) |
| 70 | return respMsgPromise.then((msg: IteratorResult<any, void>) => { |
| 71 | return msg.value; |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | wshRpcStream(command: string, data: any, opts: RpcOpts): AsyncGenerator<any, void, boolean> { |
| 76 | if (opts?.noresponse) { |
| 77 | throw new Error("noresponse not supported for responsestream calls"); |
| 78 | } |
| 79 | const msg: RpcMessage = { |
| 80 | command: command, |
| 81 | data: data, |
| 82 | reqid: crypto.randomUUID(), |
| 83 | source: this.routeId, |
| 84 | }; |
| 85 | if (opts?.timeout) { |
| 86 | msg.timeout = opts.timeout; |
| 87 | } |
| 88 | if (opts?.route) { |
| 89 | msg.route = opts.route; |
| 90 | } |
| 91 | const rpcGen = sendRpcCommand(this.openRpcs, msg); |
| 92 | return rpcGen; |
| 93 | } |
| 94 | |
| 95 | async handleIncomingCommand(msg: RpcMessage) { |
| 96 | // TODO implement a timeout (setTimeout + sendResponse) |
| 97 | const helper = new RpcResponseHelper(this, msg); |
| 98 | const handlerName = `handle_${msg.command}`; |
| 99 | try { |
nothing calls this directly
no outgoing calls
no test coverage detected