Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 254 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
callback to handle completion of pipe

#1
I am using the following node.js code to download documents from some url and save it in the disk.
I want to be informed about when the document is downloaded. i have not seen any callback with pipe.Or, Is there any 'end' event that can be captured on completion of download ?

request(some_url_doc).pipe(fs.createWriteStream('xyz.doc'));

Reply

#2
Based nodejs document,

[To see links please register here]

,
it should handle writableStream's `finish` event.

var writable = getWriteable();
var readable = getReadable();
readable.pipe(writable);
writable.on('finish', function(){ ... });
Reply

#3
Code snippet for piping content from web via http(s) to filesystem. As @starbeamrainbowlabs noticed event ```finish``` does job

var tmpFile = "/tmp/somefilename.doc";

var ws = fs.createWriteStream(tmpFile);
ws.on('finish', function() {
// pipe done here, do something with file
});

var client = url.slice(0, 5) === 'https' ? https : http;
client.get(url, function(response) {
return response.pipe(ws);
});
Reply

#4
Streams are `EventEmitter`s so you can listen to certain events. As you said there is a `finish` event for request (previously `end`).

var stream = request(...).pipe(...);
stream.on('finish', function () { ... });

For more information about which events are available you can check the [stream documentation page](

[To see links please register here]

).
Reply

#5
Here's a solution that handles errors in requests and calls a callback after the file is written:


request(opts)
.on('error', function(err){ return callback(err)})
.pipe(fs.createWriteStream(filename))
.on('finish', function (err) {
return callback(err);
});

Reply

#6
I found an a bit different solution of my problem regarding this context. Thought worth sharing.

Most of the example create `readStreams` from file. But in my case `readStream` has to be created from `JSON` string coming from a message pool.

var jsonStream = through2.obj(function(chunk, encoding, callback) {
this.push(JSON.stringify(chunk, null, 4) + '\n');
callback();
});
// message.value --> value/text to write in write.txt
jsonStream.write(JSON.parse(message.value));
var writeStream = sftp.createWriteStream("/path/to/write/write.txt");

//"close" event didn't work for me!
writeStream.on( 'close', function () {
console.log( "- done!" );
sftp.end();
}
);

//"finish" event didn't work for me either!
writeStream.on( 'close', function () {
console.log( "- done!"
sftp.end();
}
);

// finally this worked for me!
jsonStream.on('data', function(data) {
var toString = Object.prototype.toString.call(data);
console.log('type of data:', toString);
console.log( "- file transferred" );
});

jsonStream.pipe( writeStream );
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through