From e8aac382e8f516ef4fac255a86b8ef6440d48a77 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Sat, 20 Apr 2013 00:10:51 -0700 Subject: [PATCH] gm().stream() now returns a stream #155 --- lib/command.js | 21 +++++++++++++++++---- package.json | 3 ++- test/streamOut.js | 25 ++++++++++++++++++++----- test/streamOutFormat.js | 27 ++++++++++++++++++++++----- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/lib/command.js b/lib/command.js index 8e08116..6c71997 100644 --- a/lib/command.js +++ b/lib/command.js @@ -8,6 +8,7 @@ var spawn = require('child_process').spawn; var utils = require('./utils'); var debug = require('debug')('gm'); var series = require('array-series'); +var through = require('through'); /** * 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 - * @return {Object} gm + * @param {String} format (optional) + * @param {Function} callback (optional) + * @return {Stream} */ proto.stream = function stream (format, callback) { if (!callback) callback = format, format = null; + var throughStream; + 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) { @@ -112,6 +123,8 @@ module.exports = function (proto) { if (err) return callback(err); return self._spawn(self.args(), false, callback); }); + + return throughStream } /** diff --git a/package.json b/package.json index d5d7461..3dca6c7 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ , "dependencies": { "debug": "0.7.0", "array-series": "~0.1.0", - "array-parallel": "~0.1.0" + "array-parallel": "~0.1.0", + "through": "~2.3.1" } } diff --git a/test/streamOut.js b/test/streamOut.js index 105d82e..3b070db 100644 --- a/test/streamOut.js +++ b/test/streamOut.js @@ -6,11 +6,26 @@ module.exports = function (gm, dir, finish, GM) { if (!GM.integration) return finish(); - gm - .stream(function streamOut (err, stdout, stderr) { + withCallback(function (err) { if (err) return finish(err); - stdout.pipe(fs.createWriteStream(dir + '/streamOut.jpg')); - stdout.on('error', finish); - stdout.on('close', finish); + + withoutCallback(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) + } } diff --git a/test/streamOutFormat.js b/test/streamOutFormat.js index 9e44c1a..f2200f5 100644 --- a/test/streamOutFormat.js +++ b/test/streamOutFormat.js @@ -3,6 +3,7 @@ var assert = require('assert') var fs = require('fs'); module.exports = function (gm, dir, finish, GM) { + /* assert.throws(function () { gm.stream() }, /expects a callback/); @@ -10,15 +11,31 @@ module.exports = function (gm, dir, finish, GM) { assert.throws(function () { gm.stream('PNG') }, /expects a callback/); + */ if (!GM.integration) return finish(); - gm - .stream('PNG', function streamOut (err, stdout, stderr) { + withCallback(function (err) { if (err) return finish(err); - stdout.pipe(fs.createWriteStream(dir + '/streamOut.png')); - stdout.on('error', finish); - stdout.on('close', finish); + + withoutCallback(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) + } }