Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active August 26, 2019 06:03
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 ORESoftware/0ad178f4512bbf956e54dd08f2412883 to your computer and use it in GitHub Desktop.
Save ORESoftware/0ad178f4512bbf956e54dd08f2412883 to your computer and use it in GitHub Desktop.
Stdout/stderr for writing between procs can be faulty if extra stdio is included

Here is some node.js code:

#!/usr/bin/env node

const fs = require('fs');

const header = '#!/usr/bin/env bash\n\n';
try {
  const bytes = fs.writeSync(3, header, 0);
} catch (err) {
  console.error('Please open file descriptor 3 at the command line.');
  console.error('For example:');
  console.error('bunion --bash-completion 3> completion-location.sh');
  process.exit(1);
}

const w = fs.createWriteStream(null, {fd: 3, encoding: 'utf8', start: header.length});
w.write('foo');
w.end('\n');

you can invoke it like so:

 node test/fd-3.js 3> temp.sh

and then in temp.sh you will have:

#!/usr/bin/env bash

foo

this way processes can communicate via stdio on fd3, and you can write to stdout/stderr without worring so damn much about altering the important output. Or use JSON to communicate. In other words, stdout becomes for meta-data (instead of all these unimaginative assh*les using stderr for metadata), and fd3 is used for the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment