mirror of
https://github.com/lovell/sharp.git
synced 2026-02-04 05:36:18 +01:00
Add toUint8Array for output backed by transferable ArrayBuffer #4355
This commit is contained in:
@@ -228,6 +228,19 @@ async.series({
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add('sharp-buffer-uint8array', {
|
||||
defer: true,
|
||||
fn: (deferred) => {
|
||||
sharp(inputJpgBuffer)
|
||||
.resize(width, height)
|
||||
.toUint8Array()
|
||||
.then(() => {
|
||||
deferred.resolve();
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
}).add('sharp-file-file', {
|
||||
defer: true,
|
||||
fn: (deferred) => {
|
||||
@@ -266,6 +279,19 @@ async.series({
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add('sharp-file-uint8array', {
|
||||
defer: true,
|
||||
fn: (deferred) => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(width, height)
|
||||
.toUint8Array()
|
||||
.then(() => {
|
||||
deferred.resolve();
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
}).add('sharp-promise', {
|
||||
defer: true,
|
||||
fn: (deferred) => {
|
||||
|
||||
@@ -86,6 +86,9 @@ let transformer = sharp()
|
||||
});
|
||||
readableStream.pipe(transformer).pipe(writableStream);
|
||||
|
||||
sharp().toUint8Array();
|
||||
sharp().toUint8Array().then(({ data }) => data.byteLength);
|
||||
|
||||
console.log(sharp.format);
|
||||
console.log(sharp.versions);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { afterEach, beforeEach, describe, it } = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const { isMarkedAsUntransferable } = require('node:worker_threads');
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
@@ -1092,4 +1093,39 @@ describe('Input/output', () => {
|
||||
assert.strictEqual(channels, 3);
|
||||
assert.strictEqual(format, 'jpeg');
|
||||
});
|
||||
|
||||
it('toBuffer resolves with an untransferable Buffer', async () => {
|
||||
const data = await sharp(fixtures.inputJpg)
|
||||
.resize({ width: 8, height: 8 })
|
||||
.toBuffer();
|
||||
|
||||
if (isMarkedAsUntransferable) {
|
||||
assert.strictEqual(isMarkedAsUntransferable(data.buffer), true);
|
||||
}
|
||||
assert.strictEqual(ArrayBuffer.isView(data), true);
|
||||
assert.strictEqual(ArrayBuffer.isView(data.buffer), false);
|
||||
});
|
||||
|
||||
it('toUint8Array resolves with a transferable Uint8Array', async () => {
|
||||
const { data, info } = await sharp(fixtures.inputJpg)
|
||||
.resize({ width: 8, height: 8 })
|
||||
.toUint8Array();
|
||||
|
||||
assert.strictEqual(data instanceof Uint8Array, true);
|
||||
if (isMarkedAsUntransferable) {
|
||||
assert.strictEqual(isMarkedAsUntransferable(data.buffer), false);
|
||||
}
|
||||
assert.strictEqual(ArrayBuffer.isView(data), true);
|
||||
assert.strictEqual(info.format, 'jpeg');
|
||||
assert.strictEqual(info.width, 8);
|
||||
assert.strictEqual(info.height, 8);
|
||||
assert.strictEqual(data.byteLength, info.size);
|
||||
assert.strictEqual(data[0], 0xFF);
|
||||
assert.strictEqual(data[1], 0xD8);
|
||||
|
||||
const metadata = await sharp(data).metadata();
|
||||
assert.strictEqual(metadata.format, 'jpeg');
|
||||
assert.strictEqual(metadata.width, 8);
|
||||
assert.strictEqual(metadata.height, 8);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user