|
3 | 3 | SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
| 6 | +const { once } = require('node:events'); |
6 | 7 | const { createReadStream, createWriteStream } = require('node:fs'); |
7 | 8 | const fs = require('node:fs/promises'); |
8 | 9 | const path = require('node:path'); |
@@ -368,6 +369,81 @@ suite('Input/output', () => { |
368 | 369 | await fs.rm(outputJpg); |
369 | 370 | }); |
370 | 371 |
|
| 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 | + |
371 | 447 | test('Non-Stream input generates error when provided Stream-like data', async (t) => { |
372 | 448 | t.plan(2); |
373 | 449 | t.assert.throws( |
|
0 commit comments