| 86 | const events = new SafeEventEmitter<StreamEvents>(); |
| 87 | |
| 88 | function processResponse(res: JRPCResponse<unknown>) { |
| 89 | const context = idMap[res.id as unknown as string]; |
| 90 | if (!context) { |
| 91 | throw new Error(`StreamMiddleware - Unknown response id "${res.id}"`); |
| 92 | } |
| 93 | |
| 94 | delete idMap[res.id as unknown as string]; |
| 95 | // copy whole res onto original res |
| 96 | Object.assign(context.res, res); |
| 97 | // run callback on empty stack, |
| 98 | // prevent internal stream-handler from catching errors |
| 99 | setTimeout(context.end); |
| 100 | } |
| 101 | |
| 102 | function processNotification(res: JRPCNotification<JRPCParams>) { |
| 103 | events.emit("notification", res); |