function readMessages
readMessages<T>(
socket: net.Socket,
onLine: (line: T) => void
): void

Reads NDJSON from socket, dispatching each parsed object via onLine. Tolerates packet splits across line boundaries; silently drops malformed lines. Used by both the daemon server (parsing client requests) and the client (parsing server responses).

import * as Socket from './socket.ts';

import net from 'node:net';
import { Buffer } from 'node:buffer';
const seen: Array<{ type: string }> = [];
const sock = new net.Socket();
Socket.readMessages<{ type: string }>(sock, (msg) => seen.push(msg));
sock.emit('data', Buffer.from('{"type":"ping"}\nnot-json\n'));
seen; // [{ type: 'ping' }] — the malformed line is dropped

Type Parameters

Parameters

socket: net.Socket
onLine: (line: T) => void

Return Type

void

Usage

import { readMessages } from "lib/commands/daemon/socket.ts";