Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created April 14, 2019 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoehrmann/bae3432a01fb4dc627c27dbc3a33d833 to your computer and use it in GitHub Desktop.
Save hoehrmann/bae3432a01fb4dc627c27dbc3a33d833 to your computer and use it in GitHub Desktop.
node <> Perl communication over additional fds
import { spawn } from 'child_process';
import { Writable, Readable } from 'stream';
const child = spawn(
'perl',
['-e', `
use IO::Handle;
my $in = IO::Handle->new_from_fd($ENV{JIPE_RHS_STDIN_FD}, 'r');
my $out = IO::Handle->new_from_fd($ENV{JIPE_RHS_STDOUT_FD}, 'w');
$out->autoflush(1);
while (<$in>) {
print $out $_;
}
`],
{
env: {
...process.env,
JIPE_RHS_STDOUT_FD: `3`,
JIPE_RHS_STDIN_FD: `4`,
},
shell: false,
stdio: ['pipe', 'pipe', 'inherit', 'pipe', 'pipe']
}
);
const childStdout: Readable = child.stdio[3] as Readable;
const childStdin: Writable = child.stdio[4] as Writable;
childStdout.on('data', data => {
console.error('child said', data.toString());
})
childStdin.write(`Hello\n`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment