* Asynchronously converts a stream of lines into messages. * @param {AsyncIterable } linesAsync - Stream of lines to convert into messages. * @returns {AsyncIterable } - An asynchronous iterable that yields parsed messages from the input lines.
(linesAsync: any)
| 25 | * @returns {AsyncIterable<any>} - An asynchronous iterable that yields parsed messages from the input lines. |
| 26 | */ |
| 27 | async function* linesToMessages(linesAsync: any) { |
| 28 | for await (const line of linesAsync) { |
| 29 | const message = line.substring("data :".length); |
| 30 | try { |
| 31 | const parsedMessage = JSON.parse(message); |
| 32 | yield parsedMessage; |
| 33 | } catch (error) { |
| 34 | console.error("Could not JSON parse stream message", message, error); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Asynchronously completes the stream by converting chunks to lines and then to messages. |