The Node.js implementation accepts IncomingMessage from the node:http module:
import type { IncomingMessage } from 'node:http';import { meros } from 'meros/node';interface Result { name: string;}declare const responseNode: IncomingMessage;const result = await meros<Result>(responseNode);if (!(result instanceof IncomingMessage)) { for await (let item of result) { if (item.json) { // item.body is typed as Result console.log(item.body.name); } else { // item.body is a Buffer console.log(item.body.toString()); } }}
Unlike the browser implementation which returns strings, the Node.js version returns Buffer objects:
for await (const part of parts) { if (part.json) { // body is a parsed JavaScript object console.log(part.body); } else { // body is a Buffer - convert as needed const text = part.body.toString('utf8'); console.log(text); }}
The Node.js implementation uses efficient Buffer operations:
for await (let chunk of stream) { idx_boundary = buffer.byteLength; buffer = Buffer.concat([buffer, chunk]); let idx_chunk = (chunk as Buffer).indexOf(boundary); // Process boundaries...}
The implementation uses Buffer.subarray() instead of slice() for zero-copy operations, improving performance.
Provide a generic type parameter for type-safe parsing:
interface Message { id: string; content: string;}const parts = await meros<Message>(response);for await (const part of parts) { if (part.json) { // part.body is typed as Message console.log(part.body.id, part.body.content); } else { // part.body is typed as Buffer const text = part.body.toString(); }}
const result = await meros(response);if (!(result instanceof IncomingMessage)) { // Safe to iterate - this is multipart for await (const part of result) { console.log(part.body); }} else { // Not multipart - handle as regular response let data = ''; result.on('data', chunk => data += chunk); result.on('end', () => console.log(data));}