(data: string)
| 103 | } |
| 104 | |
| 105 | private async handleData(data: string) { |
| 106 | this.logTraffic(`<== ${data}\n`); |
| 107 | const json = JSON.parse(data) as DtdMessage; |
| 108 | const id = json.id; |
| 109 | const method = json.method; |
| 110 | |
| 111 | if (method === "streamNotify") { |
| 112 | const notification = json as DtdNotification; |
| 113 | this.notificationsEmitters[notification.params?.streamId]?.fire(notification.params.eventKind, notification.params.eventData); |
| 114 | |
| 115 | } else if (id !== undefined && method) { |
| 116 | const request = json as DtdRequest; |
| 117 | // Handle service request. |
| 118 | const serviceHandler = this.serviceHandlers[method]; |
| 119 | if (serviceHandler) { |
| 120 | try { |
| 121 | const result = await serviceHandler(request.params); |
| 122 | this.send({ |
| 123 | id, |
| 124 | jsonrpc: "2.0", |
| 125 | result, |
| 126 | }); |
| 127 | } catch (e: unknown) { |
| 128 | const error = this.asDtdError(e); |
| 129 | this.logger.error(`Failed handling service request ${method}: ${error.message}`); |
| 130 | this.send({ |
| 131 | id, |
| 132 | jsonrpc: "2.0", |
| 133 | error, |
| 134 | }); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | } else if (id) { |
| 139 | // Handle response. |
| 140 | const completer: PromiseCompleter<DtdResult> = this.completers[id]; |
| 141 | const response = json as DtdResponse; |
| 142 | |
| 143 | if (completer) { |
| 144 | delete this.completers[id]; |
| 145 | |
| 146 | if ("error" in response) |
| 147 | completer.reject(response.error); |
| 148 | else |
| 149 | completer.resolve(response.result); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | public onNotification(stream: string, eventKind: string, listener: (e: any) => any, thisArgs?: any): IAmDisposable { |
| 155 | if (!this.notificationsEmitters[stream]) |
no test coverage detected