diff --git a/src/util.js b/src/util.js index ea3ee8f5..8b6e609f 100644 --- a/src/util.js +++ b/src/util.js @@ -64,7 +64,7 @@ export default { if (config.zeroCopy && Object.prototype.isPrototypeOf(obj)) { const transferables = []; this.collectBuffers(obj, transferables); - return transferables; + return transferables.length ? transferables : undefined; } }, diff --git a/test/general/packet.js b/test/general/packet.js index da6e31f1..0bc59f14 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -1,6 +1,6 @@ 'use strict'; -var openpgp = typeof window != 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); function stringify(array) { if(!Uint8Array.prototype.isPrototypeOf(array)) { diff --git a/test/general/util.js b/test/general/util.js index 48e791fd..bad06718 100644 --- a/test/general/util.js +++ b/test/general/util.js @@ -144,4 +144,40 @@ describe('Util unit tests', function() { }); }); + describe('getTransferables', function() { + var zeroCopyVal, + buf1 = new Uint8Array(1), + buf2 = new Uint8Array(1), + obj = { + data1: buf1, + data2: buf1, + data3: { + data4: buf2 + } + }; + + beforeEach(function() { + zeroCopyVal = openpgp.config.zeroCopy; + openpgp.config.zeroCopy = true; + }); + + afterEach(function() { + openpgp.config.zeroCopy = zeroCopyVal; + }); + + it('should return undefined when zeroCopy is false', function() { + openpgp.config.zeroCopy = false; + expect(openpgp.util.getTransferables(obj)).to.be.undefined; + }); + it('should return undefined for no input', function() { + expect(openpgp.util.getTransferables()).to.be.undefined; + }); + it('should return undefined for an empty oject', function() { + expect(openpgp.util.getTransferables({})).to.be.undefined; + }); + it('should return two buffers', function() { + expect(openpgp.util.getTransferables(obj)).to.deep.equal([buf1.buffer, buf2.buffer]); + }); + }); + });