Skip to content

Commit 742886c

Browse files
committed
added uint8array as input and output option
1 parent 0f58f95 commit 742886c

4 files changed

Lines changed: 64 additions & 3 deletions

File tree

lib/input.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
2828
if (is.string(input)) {
2929
// filesystem
3030
inputDescriptor.file = input;
31-
} else if (is.buffer(input)) {
32-
// Buffer
33-
inputDescriptor.buffer = input;
31+
} else if (is.uint8Array(input)) {
32+
// Buffer or Uint8Array
33+
inputDescriptor.buffer = Buffer.from(input);
3434
} else if (is.plainObject(input) && !is.defined(inputOptions)) {
3535
// Plain Object descriptor, e.g. create
3636
inputOptions = input;

lib/is.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ const buffer = function (val) {
4848
return val instanceof Buffer;
4949
};
5050

51+
/**
52+
* Is this value a Uint8Array?
53+
* @private
54+
*/
55+
const uint8Array = function (val) {
56+
return val instanceof Uint8Array;
57+
};
58+
5159
/**
5260
* Is this value a non-empty string?
5361
* @private
@@ -110,6 +118,7 @@ module.exports = {
110118
fn: fn,
111119
bool: bool,
112120
buffer: buffer,
121+
uint8Array: uint8Array,
113122
string: string,
114123
number: number,
115124
integer: integer,

lib/output.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,38 @@ function toBuffer (options, callback) {
116116
return this._pipeline(is.fn(options) ? options : callback);
117117
}
118118

119+
/**
120+
* Same as toBuffer, but writes output to a Uint8Array.
121+
*
122+
* @example
123+
* sharp(input)
124+
* .toBuffer((err, data, info) => { ... });
125+
*
126+
* @example
127+
* sharp(input)
128+
* .toUint8Array()
129+
* .then(data => { ... })
130+
* .catch(err => { ... });
131+
*
132+
* @example
133+
* sharp(input)
134+
* .toUint8Array({ resolveWithObject: true })
135+
* .then(({ data, info }) => { ... })
136+
* .catch(err => { ... });
137+
*
138+
* @param {Object} [options]
139+
* @param {boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
140+
* @param {Function} [callback]
141+
* @returns {Promise<Buffer>} - when no callback is provided
142+
*/
143+
function toUint8Array (options, callback) {
144+
return new Promise((resolve, reject) => {
145+
this.toBuffer(options, callback).then((buff) => {
146+
resolve(Uint8Array.from(buff));
147+
})
148+
});
149+
}
150+
119151
/**
120152
* Include all metadata (EXIF, XMP, IPTC) from the input image in the output image.
121153
* This will also convert to and add a web-friendly sRGB ICC profile.
@@ -797,6 +829,7 @@ module.exports = function (Sharp) {
797829
// Public
798830
toFile,
799831
toBuffer,
832+
toUint8Array,
800833
withMetadata,
801834
toFormat,
802835
jpeg,

test/unit/toUint8Array.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
const sharp = require('../../lib');
6+
const fixtures = require('../fixtures');
7+
8+
describe('toUint8Array', () => {
9+
it('reusing same sharp object does not reset previously passed parameters to toUint8Array', (done) => {
10+
const image = sharp(fixtures.inputJpg);
11+
image.toUint8Array({ resolveWithObject: true }).then((obj) => {
12+
image.toUint8Array().then((buff) => {
13+
assert.strictEqual(buff.constructor === Uint8Array, true);
14+
assert.strictEqual(typeof obj, 'object');
15+
done();
16+
});
17+
});
18+
});
19+
});

0 commit comments

Comments
 (0)