UNPKG

cesr

Version:

[![NPM Version](https://img.shields.io/npm/v/cesr.svg?style=flat)](https://www.npmjs.com/package/cesr) [![NPM License](https://img.shields.io/npm/l/cesr.svg?style=flat)](https://github.com/lenkan/cesr-js/blob/main/LICENSE)

26 lines (25 loc) 763 B
import { parse } from "./parser.js"; /** * Parses JSON messages with CESR attachments from an incoming stream of bytes. * * @param input Incoming stream of bytes * @returns An async iterable of messages with attachments */ export async function* parseMessages(input, options = {}) { let message = null; for await (const frame of parse(input, options)) { if (frame.type === "message") { if (message) { yield message; } message = { payload: JSON.parse(frame.text), attachments: [] }; } else { message = message ?? { payload: {}, attachments: [] }; message.attachments.push(frame.text); } } if (message) { yield message; } }