mirror of
https://github.com/lovell/sharp.git
synced 2025-07-13 12:20:13 +02:00
Simplify 'this' in IO pipeline using arrow functions
This commit is contained in:
parent
233b015d77
commit
6cf0b3240d
50
lib/input.js
50
lib/input.js
@ -115,9 +115,8 @@ function _write (chunk, encoding, callback) {
|
|||||||
/* istanbul ignore else */
|
/* istanbul ignore else */
|
||||||
if (is.buffer(chunk)) {
|
if (is.buffer(chunk)) {
|
||||||
if (this.options.input.buffer.length === 0) {
|
if (this.options.input.buffer.length === 0) {
|
||||||
const that = this;
|
this.on('finish', () => {
|
||||||
this.on('finish', function () {
|
this.streamInFinished = true;
|
||||||
that.streamInFinished = true;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.options.input.buffer.push(chunk);
|
this.options.input.buffer.push(chunk);
|
||||||
@ -165,16 +164,15 @@ function _isStreamInput () {
|
|||||||
* @returns {Sharp}
|
* @returns {Sharp}
|
||||||
*/
|
*/
|
||||||
function clone () {
|
function clone () {
|
||||||
const that = this;
|
|
||||||
// Clone existing options
|
// Clone existing options
|
||||||
const clone = this.constructor.call();
|
const clone = this.constructor.call();
|
||||||
clone.options = Object.assign({}, this.options);
|
clone.options = Object.assign({}, this.options);
|
||||||
// Pass 'finish' event to clone for Stream-based input
|
// Pass 'finish' event to clone for Stream-based input
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
this.on('finish', function () {
|
this.on('finish', () => {
|
||||||
// Clone inherits input data
|
// Clone inherits input data
|
||||||
that._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
clone.options.bufferIn = that.options.bufferIn;
|
clone.options.bufferIn = this.options.bufferIn;
|
||||||
clone.emit('finish');
|
clone.emit('finish');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -224,12 +222,11 @@ function clone () {
|
|||||||
* @returns {Promise<Object>|Sharp}
|
* @returns {Promise<Object>|Sharp}
|
||||||
*/
|
*/
|
||||||
function metadata (callback) {
|
function metadata (callback) {
|
||||||
const that = this;
|
|
||||||
if (is.fn(callback)) {
|
if (is.fn(callback)) {
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
this.on('finish', function () {
|
this.on('finish', () => {
|
||||||
that._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
sharp.metadata(that.options, callback);
|
sharp.metadata(this.options, callback);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
sharp.metadata(this.options, callback);
|
sharp.metadata(this.options, callback);
|
||||||
@ -237,10 +234,10 @@ function metadata (callback) {
|
|||||||
return this;
|
return this;
|
||||||
} else {
|
} else {
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
that.on('finish', function () {
|
this.on('finish', () => {
|
||||||
that._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
sharp.metadata(that.options, function (err, metadata) {
|
sharp.metadata(this.options, (err, metadata) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
@ -250,8 +247,8 @@ function metadata (callback) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
sharp.metadata(that.options, function (err, metadata) {
|
sharp.metadata(this.options, (err, metadata) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
@ -293,12 +290,11 @@ function metadata (callback) {
|
|||||||
* @returns {Promise<Object>}
|
* @returns {Promise<Object>}
|
||||||
*/
|
*/
|
||||||
function stats (callback) {
|
function stats (callback) {
|
||||||
const that = this;
|
|
||||||
if (is.fn(callback)) {
|
if (is.fn(callback)) {
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
this.on('finish', function () {
|
this.on('finish', () => {
|
||||||
that._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
sharp.stats(that.options, callback);
|
sharp.stats(this.options, callback);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
sharp.stats(this.options, callback);
|
sharp.stats(this.options, callback);
|
||||||
@ -306,10 +302,10 @@ function stats (callback) {
|
|||||||
return this;
|
return this;
|
||||||
} else {
|
} else {
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
that.on('finish', function () {
|
this.on('finish', function () {
|
||||||
that._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
sharp.stats(that.options, function (err, stats) {
|
sharp.stats(this.options, (err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
@ -319,8 +315,8 @@ function stats (callback) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
sharp.stats(that.options, function (err, stats) {
|
sharp.stats(this.options, (err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
@ -650,6 +650,7 @@ function _setBooleanOption (key, val) {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function _read () {
|
function _read () {
|
||||||
|
/* istanbul ignore else */
|
||||||
if (!this.options.streamOut) {
|
if (!this.options.streamOut) {
|
||||||
this.options.streamOut = true;
|
this.options.streamOut = true;
|
||||||
this._pipeline();
|
this._pipeline();
|
||||||
@ -662,14 +663,13 @@ function _read () {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function _pipeline (callback) {
|
function _pipeline (callback) {
|
||||||
const that = this;
|
|
||||||
if (typeof callback === 'function') {
|
if (typeof callback === 'function') {
|
||||||
// output=file/buffer
|
// output=file/buffer
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
// output=file/buffer, input=stream
|
// output=file/buffer, input=stream
|
||||||
this.on('finish', function () {
|
this.on('finish', () => {
|
||||||
that._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
sharp.pipeline(that.options, callback);
|
sharp.pipeline(this.options, callback);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// output=file/buffer, input=file/buffer
|
// output=file/buffer, input=file/buffer
|
||||||
@ -680,41 +680,31 @@ function _pipeline (callback) {
|
|||||||
// output=stream
|
// output=stream
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
// output=stream, input=stream
|
// output=stream, input=stream
|
||||||
if (this.streamInFinished) {
|
this.once('finish', () => {
|
||||||
this._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
sharp.pipeline(this.options, function (err, data, info) {
|
sharp.pipeline(this.options, (err, data, info) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
that.emit('error', err);
|
this.emit('error', err);
|
||||||
} else {
|
} else {
|
||||||
that.emit('info', info);
|
this.emit('info', info);
|
||||||
that.push(data);
|
this.push(data);
|
||||||
}
|
}
|
||||||
that.push(null);
|
this.push(null);
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.on('finish', function () {
|
|
||||||
that._flattenBufferIn();
|
|
||||||
sharp.pipeline(that.options, function (err, data, info) {
|
|
||||||
if (err) {
|
|
||||||
that.emit('error', err);
|
|
||||||
} else {
|
|
||||||
that.emit('info', info);
|
|
||||||
that.push(data);
|
|
||||||
}
|
|
||||||
that.push(null);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
if (this.streamInFinished) {
|
||||||
|
this.emit('finish');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// output=stream, input=file/buffer
|
// output=stream, input=file/buffer
|
||||||
sharp.pipeline(this.options, function (err, data, info) {
|
sharp.pipeline(this.options, (err, data, info) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
that.emit('error', err);
|
this.emit('error', err);
|
||||||
} else {
|
} else {
|
||||||
that.emit('info', info);
|
this.emit('info', info);
|
||||||
that.push(data);
|
this.push(data);
|
||||||
}
|
}
|
||||||
that.push(null);
|
this.push(null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -722,15 +712,15 @@ function _pipeline (callback) {
|
|||||||
// output=promise
|
// output=promise
|
||||||
if (this._isStreamInput()) {
|
if (this._isStreamInput()) {
|
||||||
// output=promise, input=stream
|
// output=promise, input=stream
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
that.on('finish', function () {
|
this.once('finish', () => {
|
||||||
that._flattenBufferIn();
|
this._flattenBufferIn();
|
||||||
sharp.pipeline(that.options, function (err, data, info) {
|
sharp.pipeline(this.options, (err, data, info) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
if (that.options.resolveWithObject) {
|
if (this.options.resolveWithObject) {
|
||||||
resolve({ data: data, info: info });
|
resolve({ data, info });
|
||||||
} else {
|
} else {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
}
|
}
|
||||||
@ -740,12 +730,12 @@ function _pipeline (callback) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// output=promise, input=file/buffer
|
// output=promise, input=file/buffer
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
sharp.pipeline(that.options, function (err, data, info) {
|
sharp.pipeline(this.options, (err, data, info) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
if (that.options.resolveWithObject) {
|
if (this.options.resolveWithObject) {
|
||||||
resolve({ data: data, info: info });
|
resolve({ data: data, info: info });
|
||||||
} else {
|
} else {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
|
@ -99,7 +99,7 @@
|
|||||||
"nan": "^2.14.0",
|
"nan": "^2.14.0",
|
||||||
"npmlog": "^4.1.2",
|
"npmlog": "^4.1.2",
|
||||||
"prebuild-install": "^5.3.0",
|
"prebuild-install": "^5.3.0",
|
||||||
"semver": "^6.2.0",
|
"semver": "^6.3.0",
|
||||||
"simple-get": "^3.0.3",
|
"simple-get": "^3.0.3",
|
||||||
"tar": "^4.4.10",
|
"tar": "^4.4.10",
|
||||||
"tunnel-agent": "^0.6.0"
|
"tunnel-agent": "^0.6.0"
|
||||||
|
@ -392,6 +392,22 @@ describe('Input/output', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Stream input with corrupt header fails gracefully', function (done) {
|
||||||
|
const transformer = sharp();
|
||||||
|
transformer
|
||||||
|
.toBuffer()
|
||||||
|
.then(function () {
|
||||||
|
done(new Error('Unexpectedly resolved Promise'));
|
||||||
|
})
|
||||||
|
.catch(function (err) {
|
||||||
|
assert.strictEqual(true, !!err);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
fs
|
||||||
|
.createReadStream(fixtures.inputJpgWithCorruptHeader)
|
||||||
|
.pipe(transformer);
|
||||||
|
});
|
||||||
|
|
||||||
describe('Output filename with unknown extension', function () {
|
describe('Output filename with unknown extension', function () {
|
||||||
it('Match JPEG input', function (done) {
|
it('Match JPEG input', function (done) {
|
||||||
sharp(fixtures.inputJpg)
|
sharp(fixtures.inputJpg)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user