Skip to content

Commit

Permalink
don't buffer streams by default, opt-in for identify() and getters
Browse files Browse the repository at this point in the history
  • Loading branch information
kainosnoema authored and aheckmann committed Dec 14, 2011
1 parent 26af0c9 commit ccc128c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ gm(readStream, 'img.jpg')
var writeStream = fs.createWriteStream('/path/to/my/resized.jpg');
stdout.pipe(writeStream);
});

// when working with input streams and any 'identify'
// operation (size, format, etc), you must pass "{bufferStream: true}" if
// you also need to convert (write() or stream()) the image afterwards
// NOTE: this temporarily buffers the image stream in Node memory
var readStream = fs.createReadStream('/path/to/my/img.jpg');
gm(readStream, 'img.jpg')
.size({bufferStream: true}, function(err, size) {
this.resize(size.width / 2, size.height / 2)
this.write('/path/to/resized.jpg', function (err) {
if (!err) console.log('done');
});
}

````

## Getting started
Expand Down
25 changes: 14 additions & 11 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ module.exports = function (proto) {
this.outname = format + ":-";
}

var self = this;

return this._spawn("gm", this.args(), false, callback);
}

Expand Down Expand Up @@ -105,30 +103,35 @@ module.exports = function (proto) {
* @return {Object} gm
*/

proto._spawn = function _spawn (bin, args, shouldBuffer, callback) {
proto._spawn = function _spawn (bin, args, bufferOutput, callback) {
var proc = spawn(bin, args)
, cmd = bin + " " + args.map(utils.escape).join(' ')
, self = this;

var cmd = bin + " " + args.join(' ');

// pipe in the sourceStream if present
if(self.sourceStream) {

if(!self.sourceStream.readable && !self.bufferStream) {
throw new Error("gm().stream() or gm().write() with a non-readable " +
"stream. Pass \"{bufferStream: true}\" to identify() " +
"or getter (size, format, etc...)");
}

self.sourceStream.pipe(proc.stdin);

// resume any buffered events from a previous step
if(self.streamBuffer) {
self.streamBuffer.resume();
if(self.buffer) {
self.buffer.resume();

// if this isn't the last step, we need to buffer the
// events on the input stream so we can use them again
} else if(shouldBuffer) {
self.streamBuffer = utils.buffer(self.sourceStream);
} else if(self.bufferStream) {
self.buffer = utils.buffer(self.sourceStream);
}
}

// sometimes we need to buffer the output stream too
if(shouldBuffer) {
if(bufferOutput) {
var stdout = ''
, stderr = '';

Expand Down Expand Up @@ -164,7 +167,7 @@ module.exports = function (proto) {
, this.sourceStream ? "-" : this.source
, this._out
, this.outname || "-"
);
).filter(function(arg) { return !!arg });
}

/**
Expand Down
13 changes: 9 additions & 4 deletions lib/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ module.exports = function (proto) {

;['size', 'format', 'depth', 'color', 'res', 'filesize'].forEach(function (getter) {

proto[getter] = function (callback) {
var self = this;
proto[getter] = function (opts, callback) {
if(!callback) callback = opts, opts = {};

var self = this;
if (self.data[getter]) {
callback.call(self, null, self.data[getter]);
return self;
}

self.identify(function (err, stdout, stderr, cmd) {
self.identify(opts, function (err, stdout, stderr, cmd) {
if (err) {
return callback.call(self, err, stdout, stderr, cmd);
}
Expand All @@ -29,11 +30,15 @@ module.exports = function (proto) {
}
});

proto.identify = function identify (callback) {
proto.identify = function identify (opts, callback) {
if(!callback) callback = opts, opts = {};

var self = this;

if (!callback) return self;

self.bufferStream = opts.bufferStream == true;

if (self._identifying) {
self._iq.push(callback);
return self;
Expand Down
14 changes: 14 additions & 0 deletions test/streamInGetter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

// gm - Copyright Aaron Heckmann <[email protected]> (MIT Licensed)

var fs = require('fs');

module.exports = function (_, dir, finish, gm) {

gm(fs.createReadStream(dir + '/original.jpg'), "original.jpg")
.size({bufferStream: true}, function(err, size) {
this.write(dir + '/streamInGetter.png', function streamInGetter (err){
finish(err);
});
});
}

0 comments on commit ccc128c

Please sign in to comment.