UNPKG

rcon-node

Version:

A TypeScript RCON client library for modern game servers.

33 lines 1.24 kB
/** * Wait until the socket receives data matching the provided pattern. * @param socket The TCP socket. * @param matcher String or regular expression to match. * @returns Resolves with the received data once the matcher is satisfied. */ export function readUntil(socket, matcher) { return new Promise((resolve) => { let buffer = ""; const isMatch = (data) => typeof matcher === "string" ? data.includes(matcher) : matcher.test(data); const onData = (chunk) => { buffer += chunk.toString(); if (isMatch(buffer)) { socket.off("data", onData); resolve(buffer); } }; socket.on("data", onData); }); } /** * Send a command to the socket and read until the pattern is seen again. * @param socket The TCP socket. * @param command Command text to send. * @param matcher Pattern signalling the end of the response. * @returns Resolves with the captured response text. */ export async function sendAndRead(socket, command, matcher) { socket.write(command.endsWith("\n") ? command : `${command}\n`); const data = await readUntil(socket, matcher); return data; } //# sourceMappingURL=seven-days-to-die.utils.js.map