Comparing version 0.0.3 to 0.0.4
var childProcess = require('child_process'), | ||
Stream = require('stream').Stream, | ||
util = require('util'), | ||
// Fall back to the system's jpegtran if jpegtran-bin doesn't have a binary: | ||
jpegtranBinPath = require('jpegtran-bin').path || 'jpegtran'; | ||
which = require('which'), | ||
memoizeAsync = require('memoizeasync'); | ||
@@ -12,26 +12,46 @@ function JpegTran(jpegTranArgs) { | ||
this.jpegTranProcess = childProcess.spawn(jpegtranBinPath, jpegTranArgs); | ||
JpegTran.getBinaryPath(function (err, jpegTranBinaryPath) { | ||
if (err) { | ||
return this.emit('error', err); | ||
} | ||
this.jpegTranProcess = childProcess.spawn(jpegTranBinaryPath, jpegTranArgs); | ||
this.hasEnded = false; | ||
this.seenDataOnStdout = false; | ||
this.hasEnded = false; | ||
this.seenDataOnStdout = false; | ||
this.jpegTranProcess.on('exit', function (exitCode) { | ||
if (exitCode > 0 && !this.hasEnded) { | ||
return this.emit('error', new Error('The jpegtran process exited with a non-zero exit code: ' + exitCode)); | ||
this.hasEnded = true; | ||
} | ||
}.bind(this)); | ||
this.jpegTranProcess.on('exit', function (exitCode) { | ||
if (exitCode > 0 && !this.hasEnded) { | ||
return this.emit('error', new Error('The jpegtran process exited with a non-zero exit code: ' + exitCode)); | ||
this.hasEnded = true; | ||
} | ||
}.bind(this)); | ||
this.jpegTranProcess.stdout.on('data', function (chunk) { | ||
this.seenDataOnStdout = true; | ||
this.emit('data', chunk); | ||
}.bind(this)).on('end', function () { | ||
if (!this.hasEnded) { | ||
if (this.seenDataOnStdout) { | ||
this.emit('end'); | ||
} else { | ||
this.emit('error', new Error('JpegTran: The stdout stream ended without emitting any data')); | ||
this.jpegTranProcess.stdout.on('data', function (chunk) { | ||
this.seenDataOnStdout = true; | ||
this.emit('data', chunk); | ||
}.bind(this)).on('end', function () { | ||
if (!this.hasEnded) { | ||
if (this.seenDataOnStdout) { | ||
this.emit('end'); | ||
} else { | ||
this.emit('error', new Error('JpegTran: The stdout stream ended without emitting any data')); | ||
} | ||
this.hasEnded = true; | ||
} | ||
this.hasEnded = true; | ||
}.bind(this)); | ||
if (this.isPaused) { | ||
this.jpegTranProcess.stdout.pause(); | ||
} | ||
if (this.bufferedChunks) { | ||
this.bufferedChunks.forEach(function (chunk) { | ||
if (chunk === null) { | ||
this.jpegTranProcess.stdin.end(); | ||
} else { | ||
this.jpegTranProcess.stdin.write(chunk); | ||
} | ||
}, this); | ||
this.bufferedChunks = null; | ||
} | ||
}.bind(this)); | ||
@@ -42,4 +62,21 @@ } | ||
JpegTran.getBinaryPath = memoizeAsync(function (cb) { | ||
which('jpegtran', function (err, jpegTranBinaryPath) { | ||
if (err) { | ||
jpegTranBinaryPath = require('jpegtran-bin').path; | ||
} | ||
if (jpegTranBinaryPath) { | ||
cb(null, jpegTranBinaryPath); | ||
} else { | ||
cb(new Error('No jpegtran binary in PATH and jpegtran-bin does not provide a pre-built binary for your architecture')); | ||
} | ||
}); | ||
}); | ||
JpegTran.prototype.write = function (chunk) { | ||
this.jpegTranProcess.stdin.write(chunk); | ||
if (this.jpegTranProcess) { | ||
this.jpegTranProcess.stdin.write(chunk); | ||
} else { | ||
(this.bufferedChunks = this.bufferedChunks || []).push(chunk); | ||
} | ||
}; | ||
@@ -51,13 +88,23 @@ | ||
} | ||
this.jpegTranProcess.stdin.end(); | ||
if (this.jpegTranProcess) { | ||
this.jpegTranProcess.stdin.end(); | ||
} else { | ||
this.bufferedChunks.push(null); | ||
} | ||
}; | ||
JpegTran.prototype.pause = function () { | ||
this.jpegTranProcess.stdout.pause(); | ||
if (this.jpegTranProcess) { | ||
this.jpegTranProcess.stdout.pause(); | ||
} | ||
this.isPaused = true; | ||
}; | ||
JpegTran.prototype.resume = function () { | ||
this.jpegTranProcess.stdout.resume(); | ||
if (this.jpegTranProcess) { | ||
this.jpegTranProcess.stdout.resume(); | ||
} | ||
this.isPaused = false; | ||
}; | ||
module.exports = JpegTran; |
{ | ||
"name": "jpegtran", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "The jpegtran utility as a readable/writable stream", | ||
@@ -10,3 +10,5 @@ "main": "lib/JpegTran.js", | ||
"dependencies": { | ||
"jpegtran-bin": "=0.1.0" | ||
"memoizeasync": "=0.0.1", | ||
"jpegtran-bin": "=0.1.0", | ||
"which": "=1.0.5" | ||
}, | ||
@@ -13,0 +15,0 @@ "devDependencies": { |
@@ -6,2 +6,7 @@ node-jpegtran | ||
If you don't have a `jpegtran` binary in your PATH, `node-jpegtran` | ||
will try to use one of the binaries provided by <a | ||
href="https://github.com/yeoman/node-jpegtran-bin">the node-jpegtran-bin | ||
package</a>. | ||
The constructor optionally takes an array of command line options for | ||
@@ -38,3 +43,3 @@ the `jpegtran` binary: | ||
Make sure you have node.js and npm installed, and that the `jpegtran` binary is in your PATH, then run: | ||
Make sure you have node.js and npm installed, then run: | ||
@@ -41,0 +46,0 @@ npm install jpegtran |
115074
149
50
3
+ Addedmemoizeasync@=0.0.1
+ Addedwhich@=1.0.5