UNPKG

simple-tcp-proxy

Version:
68 lines (57 loc) 1.6 kB
"use strict"; var net = require('net'); /** @module helper */ /** * create a listener which runs the parameter function with the data from the listener * * @param {function(Error)} errorFunction function to run inside the return function * @return {function(Error)} errorListener runs errorFunction with own parameter */ function errorListener(errorFunction) { return ( /** @param {Error} errMsg Error Msg */ function (errMsg) { if (errorFunction) errorFunction(errMsg); } ); } /** * create a listener which writes the data to the socket * * @param {module:net.Socket} socket socket where the data is written to * @return {function((Uint8Array | string))} listener which writes data to the socket */ function dataListener(socket) { return function (data) { if (socket) socket.write(data); }; } /** * destroys the socket it the Listener is fired * * @param {module:net.Socket} socket to destroy * @return {function()} listener which will destroy the socket */ function closeListener(socket) { return function () { if (socket) socket.destroy(); }; } /** * create a socket to given parameter * * @param {Number} port port of the server * @param {String} host host of the server * @return {module:net.Socket} */ function getSocket(port, host) { var socket = new net.Socket(); socket.connect(port, host); return socket; } module.exports = { errorListener: errorListener, dataListener: dataListener, closeListener: closeListener, getSocket: getSocket };