mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Improve Stream error handling #88
This commit is contained in:
parent
34c96ff925
commit
515b4656e6
16
README.md
16
README.md
@ -419,23 +419,25 @@ var counters = sharp.counters(); // { queue: 2, process: 4 }
|
|||||||
|
|
||||||
### It worked on my machine
|
### It worked on my machine
|
||||||
|
|
||||||
npm test
|
```
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
Running the comparative performance tests requires _ImageMagick_ and _GraphicsMagick_.
|
Running the comparative performance tests requires _ImageMagick_ and _GraphicsMagick_.
|
||||||
|
|
||||||
```
|
```
|
||||||
brew install imagemagick
|
brew install imagemagick
|
||||||
brew install graphicsmagick
|
brew install graphicsmagick
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo apt-get install -qq imagemagick graphicsmagick
|
sudo apt-get install -qq imagemagick graphicsmagick
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo yum install ImageMagick
|
sudo yum install ImageMagick
|
||||||
sudo yum install -y http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
|
sudo yum install -y http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
|
||||||
sudo yum install -y --enablerepo=epel GraphicsMagick
|
sudo yum install -y --enablerepo=epel GraphicsMagick
|
||||||
```
|
```
|
||||||
|
|
||||||
## Performance
|
## Performance
|
||||||
|
14
index.js
14
index.js
@ -331,16 +331,22 @@ Sharp.prototype._sharp = function(callback) {
|
|||||||
// output=stream, input=stream
|
// output=stream, input=stream
|
||||||
this.on('finish', function() {
|
this.on('finish', function() {
|
||||||
sharp.resize(that.options, function(err, data) {
|
sharp.resize(that.options, function(err, data) {
|
||||||
if (err) throw err;
|
if (err) {
|
||||||
that.push(data);
|
that.emit('error', new Error(err));
|
||||||
|
} else {
|
||||||
|
that.push(data);
|
||||||
|
}
|
||||||
that.push(null);
|
that.push(null);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// output=stream, input=file/buffer
|
// output=stream, input=file/buffer
|
||||||
sharp.resize(this.options, function(err, data) {
|
sharp.resize(this.options, function(err, data) {
|
||||||
if (err) throw err;
|
if (err) {
|
||||||
that.push(data);
|
that.emit('error', new Error(err));
|
||||||
|
} else {
|
||||||
|
that.push(data);
|
||||||
|
}
|
||||||
that.push(null);
|
that.push(null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -417,6 +417,33 @@ async.series([
|
|||||||
var pipeline = sharp().resize(320, 240);
|
var pipeline = sharp().resize(320, 240);
|
||||||
readable.pipe(pipeline).pipe(writable);
|
readable.pipe(pipeline).pipe(writable);
|
||||||
},
|
},
|
||||||
|
// Stream-Stream error handling
|
||||||
|
function(done) {
|
||||||
|
var pipeline = sharp().resize(320, 240);
|
||||||
|
var anErrorWasEmitted = false;
|
||||||
|
pipeline.on('error', function(err) {
|
||||||
|
anErrorWasEmitted = !!err;
|
||||||
|
}).on('end', function() {
|
||||||
|
assert(anErrorWasEmitted);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
var readableButNotAnImage = fs.createReadStream(__filename);
|
||||||
|
var writable = fs.createWriteStream(outputJpg);
|
||||||
|
readableButNotAnImage.pipe(pipeline).pipe(writable);
|
||||||
|
},
|
||||||
|
// File-Stream error handling
|
||||||
|
function(done) {
|
||||||
|
var readableButNotAnImage = sharp(__filename).resize(320, 240);
|
||||||
|
var anErrorWasEmitted = false;
|
||||||
|
readableButNotAnImage.on('error', function(err) {
|
||||||
|
anErrorWasEmitted = !!err;
|
||||||
|
}).on('end', function() {
|
||||||
|
assert(anErrorWasEmitted);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
var writable = fs.createWriteStream(outputJpg);
|
||||||
|
readableButNotAnImage.pipe(writable);
|
||||||
|
},
|
||||||
// Crop, gravity=north
|
// Crop, gravity=north
|
||||||
function(done) {
|
function(done) {
|
||||||
sharp(inputJpg).resize(320, 80).crop(sharp.gravity.north).toFile(path.join(fixturesPath, 'output.gravity-north.jpg'), function(err, info) {
|
sharp(inputJpg).resize(320, 80).crop(sharp.gravity.north).toFile(path.join(fixturesPath, 'output.gravity-north.jpg'), function(err, info) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user