gm().stream() now returns a stream #155

master
Jonathan Ong 11 years ago
parent bc7a81d4d2
commit e8aac382e8
  1. 21
      lib/command.js
  2. 3
      package.json
  3. 25
      test/streamOut.js
  4. 27
      test/streamOutFormat.js

@ -8,6 +8,7 @@ var spawn = require('child_process').spawn;
var utils = require('./utils'); var utils = require('./utils');
var debug = require('debug')('gm'); var debug = require('debug')('gm');
var series = require('array-series'); var series = require('array-series');
var through = require('through');
/** /**
* Error messaging. * Error messaging.
@ -89,17 +90,27 @@ module.exports = function (proto) {
} }
/** /**
* Execute the command and return stdin and stderr ReadableStreams providing the image data. * Execute the command and return stdin and stderr
* ReadableStreams providing the image data.
* If no callback is passed, a "through" stream will be returned,
* and stdout will be piped through, otherwise the error will be passed.
* *
* @param {Function} callback * @param {String} format (optional)
* @return {Object} gm * @param {Function} callback (optional)
* @return {Stream}
*/ */
proto.stream = function stream (format, callback) { proto.stream = function stream (format, callback) {
if (!callback) callback = format, format = null; if (!callback) callback = format, format = null;
var throughStream;
if ("function" !== typeof callback) { if ("function" !== typeof callback) {
throw new TypeError("gm().stream() expects a callback function") throughStream = through()
callback = function (err, stdout, stderr) {
if (err) throughStream.emit('error', err);
else stdout.pipe(throughStream);
}
} }
if (format) { if (format) {
@ -112,6 +123,8 @@ module.exports = function (proto) {
if (err) return callback(err); if (err) return callback(err);
return self._spawn(self.args(), false, callback); return self._spawn(self.args(), false, callback);
}); });
return throughStream
} }
/** /**

@ -20,6 +20,7 @@
, "dependencies": { , "dependencies": {
"debug": "0.7.0", "debug": "0.7.0",
"array-series": "~0.1.0", "array-series": "~0.1.0",
"array-parallel": "~0.1.0" "array-parallel": "~0.1.0",
"through": "~2.3.1"
} }
} }

@ -6,11 +6,26 @@ module.exports = function (gm, dir, finish, GM) {
if (!GM.integration) if (!GM.integration)
return finish(); return finish();
gm withCallback(function (err) {
.stream(function streamOut (err, stdout, stderr) {
if (err) return finish(err); if (err) return finish(err);
stdout.pipe(fs.createWriteStream(dir + '/streamOut.jpg'));
stdout.on('error', finish); withoutCallback(finish);
stdout.on('close', finish);
}); });
function withCallback(done) {
gm
.stream(function streamOut (err, stdout, stderr) {
if (err) return done(err);
stdout.pipe(fs.createWriteStream(dir + '/streamOut.png'));
stdout.on('error', done);
stdout.on('close', done);
});
}
function withoutCallback(done) {
var stream = gm.stream()
stream.on('error', done)
stream.pipe(fs.createWriteStream(dir + '/streamOut2.png'))
stream.on('end', done)
}
} }

@ -3,6 +3,7 @@ var assert = require('assert')
var fs = require('fs'); var fs = require('fs');
module.exports = function (gm, dir, finish, GM) { module.exports = function (gm, dir, finish, GM) {
/*
assert.throws(function () { assert.throws(function () {
gm.stream() gm.stream()
}, /expects a callback/); }, /expects a callback/);
@ -10,15 +11,31 @@ module.exports = function (gm, dir, finish, GM) {
assert.throws(function () { assert.throws(function () {
gm.stream('PNG') gm.stream('PNG')
}, /expects a callback/); }, /expects a callback/);
*/
if (!GM.integration) if (!GM.integration)
return finish(); return finish();
gm withCallback(function (err) {
.stream('PNG', function streamOut (err, stdout, stderr) {
if (err) return finish(err); if (err) return finish(err);
stdout.pipe(fs.createWriteStream(dir + '/streamOut.png'));
stdout.on('error', finish); withoutCallback(finish);
stdout.on('close', finish);
}); });
function withCallback(done) {
gm
.stream('PNG', function streamOut (err, stdout, stderr) {
if (err) return done(err);
stdout.pipe(fs.createWriteStream(dir + '/streamOut.png'));
stdout.on('error', done);
stdout.on('close', done);
});
}
function withoutCallback(done) {
var stream = gm.stream('PNG')
stream.on('error', done)
stream.pipe(fs.createWriteStream(dir + '/streamOut2.png'))
stream.on('end', done)
}
} }

Loading…
Cancel
Save