Skip to content

Commit

Permalink
Add support for ArrayBuffer input (#3548)
Browse files Browse the repository at this point in the history
  • Loading branch information
kapouer committed Feb 5, 2023
1 parent 4798d9d commit 9608f21
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ const debuglog = util.debuglog('sharp');
* }
* }).toFile('text_rgba.png');
*
* @param {(Buffer|Uint8Array|Uint8ClampedArray|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|string)} [input] - if present, can be
* a Buffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image data, or
* @param {(Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|string)} [input] - if present, can be
* a Buffer / ArrayBuffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image data, or
* a TypedArray containing raw pixel image data, or
* a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file.
* JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare function sharp(options?: sharp.SharpOptions): sharp.Sharp;
declare function sharp(
input?:
| Buffer
| ArrayBuffer
| Uint8Array
| Uint8ClampedArray
| Int8Array
Expand Down
5 changes: 5 additions & 0 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
throw Error('Input Buffer is empty');
}
inputDescriptor.buffer = input;
} else if (is.arrayBuffer(input)) {
if (input.byteLength === 0) {
throw Error('Input bit Array is empty');
}
inputDescriptor.buffer = Buffer.from(input, 0, input.byteLength);
} else if (is.typedArray(input)) {
if (input.length === 0) {
throw Error('Input Bit Array is empty');
Expand Down
9 changes: 9 additions & 0 deletions lib/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ const typedArray = function (val) {
return false;
};

/**
* Is this value an ArrayBuffer object?
* @private
*/
const arrayBuffer = function (val) {
return val instanceof ArrayBuffer;
};

/**
* Is this value a non-empty string?
* @private
Expand Down Expand Up @@ -134,6 +142,7 @@ module.exports = {
bool: bool,
buffer: buffer,
typedArray: typedArray,
arrayBuffer: arrayBuffer,
string: string,
number: number,
integer: integer,
Expand Down
17 changes: 17 additions & 0 deletions test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down

0 comments on commit 9608f21

Please sign in to comment.