brenden / node-webshot

Easy website screenshots in Node.js
2.12k stars 286 forks source link

Return stream instance synchronously #82

Open TheLudd opened 9 years ago

TheLudd commented 9 years ago

In the documentation, this is declared:

var webshot = require('webshot');
var fs      = require('fs');

webshot('google.com', function(err, renderStream) {
  var file = fs.createWriteStream('google.png', {encoding: 'binary'});

  renderStream.on('data', function(data) {
    file.write(data.toString('binary'), 'binary');
  });
});

The stream instance is returned asynchronously. Why is this? Should the code not look like this?

var webshot = require('webshot');
var fs      = require('fs');
var file = fs.createWriteStream('google.png', {encoding: 'binary'});

var renderStream = webshot('google.com');
renderStream.pipe(file);
Wizek commented 9 years ago

:+1:

lukepolo commented 9 years ago

Its because it spins up a instance of phantom

OleMchls commented 9 years ago

I stumbled upon this today as well, if i understand the code correctly returning the writable stream sync should not a problem https://github.com/brenden/node-webshot/blob/ce4b8630fc3ef0edf5078087e14b0a28a30ba0ab/lib/webshot.js#L245 should be

cb(null, s);
return s;