deathcap / nodeachrome

run Node.js scripts in Google Chrome (experimental)
MIT License
2 stars 0 forks source link

Unix command-line tool feedback, pipe stdout userland->kernel->host->cli tool #30

Closed deathcap closed 8 years ago

deathcap commented 8 years ago

When invoked from the cli interface, pipe the process.stdout stream from the browser process, to the kernel, to the native host, back to the Unix command-line tool:

host $ ./cli.js npm view ucfirst
Started pid=20, argv=["/bin/node","npm","view","ucfirst"], env={"TERM":"xterm-256color","SHELL":"/bin/sh","USER":"user","LOGNAME":"user","PATH":"~/.bin/:/usr/bin/:/bin:/usr/sbin:/sbin:/usr/local/bin","PWD":"/","HOME":"/home"}

{ name: 'ucfirst',
  description: 'uppercase first character of a string',
  'dist-tags': { latest: '1.0.0' },
  versions: [ '0.0.1', '1.0.0' ],
  maintainers: [ 'deathcap <deathcap@gmx.co.uk>' ],
  time: 
   { modified: '2015-07-09T02:59:54.506Z',
     created: '2014-03-10T05:26:24.181Z',
     '0.0.1': '2014-03-10T05:26:25.588Z',
     '1.0.0': '2015-07-09T02:58:48.068Z' },
  readmeFilename: 'README.md',
  keywords: [ 'ucfirst', 'uppercase', 'characters', 'text' ],
  repository: { type: 'git', url: 'git@github.com:deathcap/ucfirst.git' },
  bugs: { url: 'https://github.com/deathcap/ucfirst/issues' },
  license: 'MIT',
  homepage: 'https://github.com/deathcap/ucfirst',
  version: '1.0.0',
  main: 'ucfirst.js',
  files: [ 'ucfirst.js' ],
  scripts: { test: 'node test.js' },
  devDependencies: { tape: '^4.0.0' },
  gitHead: 'b827bc1e0a08a663c75c7258480deece761216d6',
  dist: 
   { shasum: '4e105b6448d05e264ecec435e0b919363c5f2f2f',
     tarball: 'https://registry.npmjs.org/ucfirst/-/ucfirst-1.0.0.tgz' },
  directories: {} }

relevant: https://github.com/deathcap/nodeachrome/issues/21 child_process API: stdio

deathcap commented 8 years ago

This sort of works, but with ./cli.js ls, losing some messages:

host $ ./cli.js ls
Host received { cmd: 'ack',
  msg: { fromUnix: true, unixID: 5624, args: [ 'ls' ] } }

Started pid=22, argv=["/bin/node","ls"], env={"TERM":"xterm-256color","SHELL":"/bin/sh","USER":"user","LOGNAME":"user","PATH":"~/.bin/:/usr/bin/:/bin:/usr/sbin:/sbin:/usr/local/bin","PWD":"/","HOME":"/home"}
a
b2.js
node_modules
package.json
Unknown message: { counter: 0 }
Unknown message: { counter: 1 }
Unknown message: { counter: 2 }
Unknown message: { counter: 4 }
Unknown message: { counter: 5 }
Unknown message: { counter: 6 }
Unknown message: { counter: 7 }
Unknown message: { counter: 8 }
Unknown message: { counter: 9 }
^C

not sure about the client.on('readable', ...) usage. Would like to be able to simply pipe to writable:

const Writable = require('stream').Writable;

rs
.pipe(new nativeMessage.Output())
.pipe(client)

const ws = new Writable({objectMode: true});
ws._write = (msg, encoding, cb) => {
  //console.log('msg',msg);

  if (msg.cmd === 'stdout') {
    process.stdout.write(msg.output);
  } else if (msg.cmd === 'ack') {
    console.log('Host received',msg);
  } else {
    console.log('Unknown message:',msg);
  }
};

client
.pipe(new nativeMessage.Input())
.pipe(ws);

but if I do this, I only get one message. Listening for the readable event on the client allows it to read more than one message, but then I would need to buffer the length like https://github.com/jdiamond/chrome-native-messaging/blob/master/index.js#L38