Add support for ArrayBuffer input (#3548)

This commit is contained in:
Jérémy Lal
2023-02-05 10:45:17 +01:00
committed by GitHub
parent 4798d9da64
commit 9608f219bd
6 changed files with 37 additions and 2 deletions

View File

@@ -150,6 +150,23 @@ describe('Input/output', function () {
readable.pipe(pipeline).pipe(writable);
});
it('Read from ArrayBuffer and write to Buffer', async () => {
const uint8array = Uint8Array.from([255, 255, 255, 0, 0, 0]);
const arrayBuffer = new ArrayBuffer(uint8array.byteLength);
new Uint8Array(arrayBuffer).set(uint8array);
const { data, info } = await sharp(arrayBuffer, {
raw: {
width: 2,
height: 1,
channels: 3
}
}).toBuffer({ resolveWithObject: true });
assert.deepStrictEqual(uint8array, new Uint8Array(data));
assert.strictEqual(info.width, 2);
assert.strictEqual(info.height, 1);
});
it('Read from Uint8Array and write to Buffer', async () => {
const uint8array = Uint8Array.from([255, 255, 255, 0, 0, 0]);
const { data, info } = await sharp(uint8array, {

View File

@@ -11,6 +11,9 @@ describe('Raw pixel data', function () {
assert.throws(function () {
sharp(Buffer.from(''));
}, /empty/);
assert.throws(function () {
sharp(new ArrayBuffer(0));
}, /empty/);
assert.throws(function () {
sharp(new Uint8Array(0));
}, /empty/);