javascript - Node.js Piping the same readable stream into multiple (writable) targets -


i need run 2 commands in series need read data same stream. after piping stream buffer emptied can't read data stream again doesn't work:

var spawn = require('child_process').spawn; var fs = require('fs'); var request = require('request');  var inputstream = request('http://placehold.it/640x360'); var identify = spawn('identify',['-']);  inputstream.pipe(identify.stdin);  var chunks = []; identify.stdout.on('data',function(chunk) {   chunks.push(chunk); });  identify.stdout.on('end',function() {   var size = getsize(buffer.concat(chunks)); //width   var convert = spawn('convert',['-','-scale',size * 0.5,'png:-']);   inputstream.pipe(convert.stdin);   convert.stdout.pipe(fs.createwritestream('half.png')); });  function getsize(buffer){   return parseint(buffer.tostring().split(' ')[2].split('x')[0]); } 

request complains this

error: cannot pipe after data has been emitted response. 

and changing inputstream fs.createwritestream yields same issue of course. don't want write file reuse in way stream request produces (or other matter).

is there way reuse readable stream once finishes piping? best way accomplish above example?

you cannot reuse piped data, has been sent already. , cannot pipe stream after 'end'. cannot process same stream twice, , need 2 streams. have create duplicate of stream piping 2 streams. can create simple stream passthrough stream, passes input output.

spawn = require('child_process').spawn; pass = require('stream').passthrough;  = spawn('echo', ['hi user']); b = new pass; c = new pass;  a.stdout.pipe(b); a.stdout.pipe(c);  count = 0; b.on('data', function(chunk) { count += chunk.length; }); b.on('end', function() { console.log(count); c.pipe(process.stdout); }); 

output

8 hi user 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -