Skip to content

Commit ea5bef2

Browse files
authored
Improve support for input Streams finishing before output is requested (#4584)
1 parent 09f0070 commit ea5bef2

4 files changed

Lines changed: 100 additions & 22 deletions

File tree

‎lib/constructor.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ function clone () {
492492
clone.options.queueListener = queueListener;
493493
// Pass 'finish' event to clone for Stream-based input
494494
if (this._isStreamInput()) {
495-
this.on('finish', () => {
495+
this._whenStreamInFinished(() => {
496496
// Clone inherits input data
497497
this._flattenBufferIn();
498498
clone.options.input.buffer = this.options.input.buffer;

‎lib/input.mjs‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,6 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
531531
function _write (chunk, _encoding, callback) {
532532
if (Array.isArray(this.options.input.buffer)) {
533533
if (is.buffer(chunk)) {
534-
if (this.options.input.buffer.length === 0) {
535-
this.on('finish', () => {
536-
this.streamInFinished = true;
537-
});
538-
}
539534
this.options.input.buffer.push(chunk);
540535
callback();
541536
} else {
@@ -565,6 +560,20 @@ function _isStreamInput () {
565560
return Array.isArray(this.options.input.buffer);
566561
}
567562

563+
/**
564+
* Call fn when the Writable side of Stream-based input has finished,
565+
* or immediately when it already has, as 'finish' is emitted only once.
566+
* @private
567+
* @param {Function} fn
568+
*/
569+
function _whenStreamInFinished (fn) {
570+
if (this.writableFinished) {
571+
fn();
572+
} else {
573+
this.once('finish', fn);
574+
}
575+
}
576+
568577
/**
569578
* Fast access to (uncached) image metadata without decoding any compressed pixel data.
570579
*
@@ -642,7 +651,7 @@ function metadata (callback) {
642651
const stack = Error();
643652
if (is.fn(callback)) {
644653
if (this._isStreamInput()) {
645-
this.on('finish', () => {
654+
this._whenStreamInFinished(() => {
646655
this._flattenBufferIn();
647656
sharp.metadata(this.options, (err, metadata) => {
648657
if (err) {
@@ -665,7 +674,7 @@ function metadata (callback) {
665674
} else {
666675
if (this._isStreamInput()) {
667676
return new Promise((resolve, reject) => {
668-
const finished = () => {
677+
this._whenStreamInFinished(() => {
669678
this._flattenBufferIn();
670679
sharp.metadata(this.options, (err, metadata) => {
671680
if (err) {
@@ -674,12 +683,7 @@ function metadata (callback) {
674683
resolve(metadata);
675684
}
676685
});
677-
};
678-
if (this.writableFinished) {
679-
finished();
680-
} else {
681-
this.once('finish', finished);
682-
}
686+
});
683687
});
684688
} else {
685689
return new Promise((resolve, reject) => {
@@ -744,7 +748,7 @@ function stats (callback) {
744748
const stack = Error();
745749
if (is.fn(callback)) {
746750
if (this._isStreamInput()) {
747-
this.on('finish', () => {
751+
this._whenStreamInFinished(() => {
748752
this._flattenBufferIn();
749753
sharp.stats(this.options, (err, stats) => {
750754
if (err) {
@@ -767,7 +771,7 @@ function stats (callback) {
767771
} else {
768772
if (this._isStreamInput()) {
769773
return new Promise((resolve, reject) => {
770-
this.on('finish', function () {
774+
this._whenStreamInFinished(() => {
771775
this._flattenBufferIn();
772776
sharp.stats(this.options, (err, stats) => {
773777
if (err) {
@@ -805,6 +809,7 @@ export default (Sharp) => {
805809
_write,
806810
_flattenBufferIn,
807811
_isStreamInput,
812+
_whenStreamInFinished,
808813
// Public
809814
metadata,
810815
stats

‎lib/output.mjs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ function _pipeline (callback, stack) {
16491649
// output=file/buffer
16501650
if (this._isStreamInput()) {
16511651
// output=file/buffer, input=stream
1652-
this.on('finish', () => {
1652+
this._whenStreamInFinished(() => {
16531653
this._flattenBufferIn();
16541654
sharp.pipeline(this.options, (err, data, info) => {
16551655
if (err) {
@@ -1674,7 +1674,7 @@ function _pipeline (callback, stack) {
16741674
// output=stream
16751675
if (this._isStreamInput()) {
16761676
// output=stream, input=stream
1677-
this.once('finish', () => {
1677+
this._whenStreamInFinished(() => {
16781678
this._flattenBufferIn();
16791679
sharp.pipeline(this.options, (err, data, info) => {
16801680
if (err) {
@@ -1687,9 +1687,6 @@ function _pipeline (callback, stack) {
16871687
this.on('end', () => this.emit('close'));
16881688
});
16891689
});
1690-
if (this.streamInFinished) {
1691-
this.emit('finish');
1692-
}
16931690
} else {
16941691
// output=stream, input=file/buffer
16951692
sharp.pipeline(this.options, (err, data, info) => {
@@ -1709,7 +1706,7 @@ function _pipeline (callback, stack) {
17091706
if (this._isStreamInput()) {
17101707
// output=promise, input=stream
17111708
return new Promise((resolve, reject) => {
1712-
this.once('finish', () => {
1709+
this._whenStreamInFinished(() => {
17131710
this._flattenBufferIn();
17141711
sharp.pipeline(this.options, (err, data, info) => {
17151712
if (err) {

‎test/unit/io.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
const { once } = require('node:events');
67
const { createReadStream, createWriteStream } = require('node:fs');
78
const fs = require('node:fs/promises');
89
const path = require('node:path');
@@ -368,6 +369,81 @@ suite('Input/output', () => {
368369
await fs.rm(outputJpg);
369370
});
370371

372+
suite('Output requested after Writable side of Stream-based input has finished', () => {
373+
const finishedPipeline = async (pipeline) => {
374+
createReadStream(fixtures.inputJpg).pipe(pipeline);
375+
await once(pipeline, 'finish');
376+
return pipeline;
377+
};
378+
379+
test('Write to Buffer via Promise', async (t) => {
380+
t.plan(2);
381+
const pipeline = await finishedPipeline(sharp().resize(320, 240));
382+
const { info } = await pipeline.toBuffer({ resolveWithObject: true });
383+
t.assert.strictEqual(320, info.width);
384+
t.assert.strictEqual(240, info.height);
385+
});
386+
387+
test('Write to Buffer via callback', async (t) => {
388+
t.plan(2);
389+
const pipeline = await finishedPipeline(sharp().resize(320, 240));
390+
const info = await new Promise((resolve, reject) => {
391+
pipeline.toBuffer((err, _data, info) => err ? reject(err) : resolve(info));
392+
});
393+
t.assert.strictEqual(320, info.width);
394+
t.assert.strictEqual(240, info.height);
395+
});
396+
397+
test('Write to File via Promise', async (t) => {
398+
t.plan(2);
399+
const pipeline = await finishedPipeline(sharp().resize(320, 240));
400+
const info = await pipeline.toFile(outputJpg);
401+
t.assert.strictEqual(320, info.width);
402+
t.assert.strictEqual(240, info.height);
403+
await fs.rm(outputJpg);
404+
});
405+
406+
test('Read metadata via Promise', async (t) => {
407+
t.plan(1);
408+
const pipeline = await finishedPipeline(sharp());
409+
const { width } = await pipeline.metadata();
410+
t.assert.strictEqual(2725, width);
411+
});
412+
413+
test('Read metadata via callback', async (t) => {
414+
t.plan(1);
415+
const pipeline = await finishedPipeline(sharp());
416+
const { width } = await new Promise((resolve, reject) => {
417+
pipeline.metadata((err, metadata) => err ? reject(err) : resolve(metadata));
418+
});
419+
t.assert.strictEqual(2725, width);
420+
});
421+
422+
test('Read stats via Promise', async (t) => {
423+
t.plan(1);
424+
const pipeline = await finishedPipeline(sharp());
425+
const { isOpaque } = await pipeline.stats();
426+
t.assert.strictEqual(true, isOpaque);
427+
});
428+
429+
test('Read stats via callback', async (t) => {
430+
t.plan(1);
431+
const pipeline = await finishedPipeline(sharp());
432+
const { isOpaque } = await new Promise((resolve, reject) => {
433+
pipeline.stats((err, stats) => err ? reject(err) : resolve(stats));
434+
});
435+
t.assert.strictEqual(true, isOpaque);
436+
});
437+
438+
test('Clone inherits input', async (t) => {
439+
t.plan(2);
440+
const pipeline = await finishedPipeline(sharp());
441+
const { info } = await pipeline.clone().resize(320, 240).toBuffer({ resolveWithObject: true });
442+
t.assert.strictEqual(320, info.width);
443+
t.assert.strictEqual(240, info.height);
444+
});
445+
});
446+
371447
test('Non-Stream input generates error when provided Stream-like data', async (t) => {
372448
t.plan(2);
373449
t.assert.throws(

0 commit comments

Comments
 (0)