From 5a3835184ed6c31bff97e716f172abaeae93f100 Mon Sep 17 00:00:00 2001 From: Florrie Date: Tue, 4 Feb 2020 22:03:56 -0400 Subject: remove mkfifo; use socat instead --- socat.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 socat.js (limited to 'socat.js') diff --git a/socat.js b/socat.js new file mode 100644 index 0000000..ca317ea --- /dev/null +++ b/socat.js @@ -0,0 +1,41 @@ +// Simple interface for making a socat process and interacting with it. +// Assumes access to the `socat` command as a child process. + +const EventEmitter = require('events') +const { spawn } = require('child_process') +const { killProcess } = require('./general-util') + +module.exports = class Socat extends EventEmitter { + constructor(path) { + super() + this.setPath(path) + } + + setPath(path) { + this.stop() + this.path = path + } + + start() { + this.stop() + this.subprocess = spawn('socat', ['-', this.path]) + this.subprocess.stdout.on('data', data => this.emit('data', data)) + this.subprocess.on('close', () => { + this.subprocess = null + }) + } + + stop() { + if (this.subprocess) { + killProcess(this.subprocess) + this.subprocess = null + } + } + + send(message) { + if (!this.subprocess) { + this.start() + } + this.subprocess.stdin.write(message + '\r\n') + } +} -- cgit 1.3.0-6-gf8a5