Skip to content

Commit

Permalink
Using a file instead of stdin works on linux because of nodejs/node#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Apr 18, 2019
1 parent 4d1a95b commit 91fd4c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
20 changes: 12 additions & 8 deletions cb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const { ChildProcess } = require('child_process')
const { sep, resolve } = require('path')
const { createLockCb } = require('flexlock-cb')
const { readFile } = require('fs')
const { readFile, createWriteStream } = require('fs')
const once = require('once')
const assert = require('assert')

Expand Down Expand Up @@ -79,18 +79,18 @@ function track (proc, errPath, timeout, cb) {
})
}

function collectErrOut (proc, cb) {
function collectIOPath (proc, cb) {
let out = ''
proc.stdout.on('data', ondata)
proc.stderr.on('data', onerr)

function ondata (chunk) {
out += chunk.toString()
const line = out.indexOf('\n')
if (line >= 0) {
const lines = out.split('\n')
if (lines.length > 2) {
proc.stdout.removeListener('data', ondata)
proc.stderr.removeListener('data', onerr)
cb(null, out.substr(0, line))
cb(null, { errPath: lines[0], inPath: lines[1] })
}
}

Expand Down Expand Up @@ -127,7 +127,8 @@ class BashProcess extends ChildProcess {
this.spawn({
file: 'bash',
args: ['/bin/bash', '--noprofile', `${__dirname}${sep}index.sh`],
envPairs
envPairs,
stdio: ['ignore', 'pipe', 'pipe']
})
this._destruct = once((err) => {
this.destructed = err || new Error('Closed.')
Expand All @@ -136,14 +137,17 @@ class BashProcess extends ChildProcess {
this.on('close', this._destruct)
this.lock = createLockCb()
this._toggleTracker(false)
this.lock(unlock => collectErrOut(this, (err, errPath) => {
this.lock(unlock => collectIOPath(this, (err, paths) => {
if (err) {
this._destruct(Object.assign(new Error(`Couldn't receive error file`), {
code: err.code,
cause: err
}))
return unlock(err)
}
const { errPath, inPath } = paths
this._stdin = createWriteStream(inPath, { flags: 'a' })
this._stdin.on('error', this._destruct)
this.errPath = errPath
unlock()
}), () => {})
Expand All @@ -154,7 +158,7 @@ class BashProcess extends ChildProcess {
return unlock(this.destructed)
}
this._setCurrent(unlock, encoding, timeout)
this.stdin.write(`${cmd}\n`)
this._stdin.write(`${cmd}\n`)
}, cb)
}
close (cb) {
Expand Down
10 changes: 6 additions & 4 deletions index.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/bin/bash

errPath=$(mktemp)
errPath=$(mktemp)_err
inPath=$(mktemp)_in
mkfifo -m 600 "$inPath"
echo $errPath
echo $inPath

while IFS='$\n' read -r cmd
do
# cleanup eventual error output
rm $errPath
rm -f "$errPath"
eval "$cmd" 2>$errPath
# By printing the status code to stderr, we can establish that th
# code has finished running
exit=$?
# echo "${cmd} ... ${exit}" >&2
(printf "%x" $exit) >&2
done < /dev/stdin

done < "$inPath"

0 comments on commit 91fd4c1

Please sign in to comment.