From 424660278dfbb1f6cfb146aec1d1039714a05b1c Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Thu, 3 Feb 2022 22:01:46 +0000 Subject: [PATCH] Allow use of toBuffer+toFile w/ same instance #3044 --- docs/changelog.md | 3 +++ lib/output.js | 1 + test/unit/io.js | 14 ++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index c90427e0..bafb472e 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,9 @@ Requires libvips v8.12.2 ### v0.30.1 - TBD +* Allow use of `toBuffer` and `toFile` on the same instance. + [#3044](https://github.com/lovell/sharp/issues/3044) + * Skip shrink-on-load for known libjpeg rounding errors. [#3066](https://github.com/lovell/sharp/issues/3066) diff --git a/lib/output.js b/lib/output.js index 28d6b5a8..c4a23429 100644 --- a/lib/output.js +++ b/lib/output.js @@ -139,6 +139,7 @@ function toBuffer (options, callback) { } else if (this.options.resolveWithObject) { this.options.resolveWithObject = false; } + this.options.fileOut = ''; return this._pipeline(is.fn(options) ? options : callback); } diff --git a/test/unit/io.js b/test/unit/io.js index 258b42b4..67c0f246 100644 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -328,6 +328,20 @@ describe('Input/output', function () { }); }); + it('Allow use of toBuffer and toFile with same instance', async () => { + const instance = sharp({ + create: { + width: 8, + height: 8, + channels: 3, + background: 'red' + } + }); + await instance.toFile(fixtures.path('output.jpg')); + const data = await instance.toBuffer(); + assert.strictEqual(Buffer.isBuffer(data), true); + }); + it('Fail when output File is input File', function (done) { sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, function (err) { assert(err instanceof Error);