cesr
Version:
[](https://www.npmjs.com/package/cesr) [](https://github.com/lenkan/cesr-js/blob/main/LICENSE)
26 lines (25 loc) • 763 B
JavaScript
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;
}
}