From 4bdc5e92ab571ad55d056af866f13374b506980d Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Tue, 7 Aug 2018 16:27:43 +0200 Subject: [PATCH] Add --compat option Without it, the generated build is for recent versions of Chrome, Firefox, Safari and Edge. With it, the generated build is for IE11+. --- Gruntfile.js | 44 ++++++++++++++++++++++------ src/polyfills.js | 65 +++++++++++++++++++++++------------------- test/general/packet.js | 45 +++++++++++++++-------------- travis.sh | 10 +++---- 4 files changed, 99 insertions(+), 65 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 73c102c0..0bfd8c27 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -13,6 +13,8 @@ module.exports = function(grunt) { }; // Project configuration. + const dev = !!grunt.option('dev'); + const compat = !!grunt.option('compat'); grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), browserify: { @@ -22,24 +24,48 @@ module.exports = function(grunt) { }, options: { browserifyOptions: { - fullPaths: grunt.option('dev'), - debug: grunt.option('dev'), + fullPaths: dev, + debug: dev, standalone: 'openpgp' }, - cacheFile: 'browserify-cache.json', + cacheFile: 'browserify-cache' + (compat ? '-compat' : '') + '.json', // Don't bundle these packages with openpgp.js - external: ['crypto', 'zlib', 'node-localstorage', 'node-fetch', 'asn1.js', 'stream', 'buffer'], + external: ['crypto', 'zlib', 'node-localstorage', 'node-fetch', 'asn1.js', 'stream', 'buffer'].concat( + compat ? [] : [ + 'whatwg-fetch', + 'core-js/fn/array/fill', + 'core-js/fn/array/find', + 'core-js/fn/array/includes', + 'core-js/fn/array/from', + 'core-js/fn/promise', + 'core-js/fn/typed/uint8-array', + 'core-js/fn/string/repeat', + 'core-js/fn/symbol', + 'core-js/fn/object/assign', + ] + ), transform: [ ["babelify", { global: true, // Only babelify web-stream-tools, asmcrypto and address-rfc2822 in node_modules only: /^(?:.*\/node_modules\/web-stream-tools\/|.*\/node_modules\/asmcrypto\.js\/|.*\/node_modules\/address-rfc2822\/|(?!.*\/node_modules\/)).*$/, - plugins: ["transform-async-to-generator", - "syntax-async-functions", - "transform-regenerator", - "transform-runtime"], + plugins: compat ? [ + "transform-async-to-generator", + "syntax-async-functions", + "transform-regenerator", + "transform-runtime" + ] : [], ignore: ['*.min.js'], - presets: ["env"] + presets: [["env", { + targets: { + browsers: compat ? ['defaults'] : [ + 'Last 2 Chrome versions', + 'Last 2 Firefox versions', + 'Last 2 Safari versions', + 'Last 2 Edge versions' + ] + } + }]] }] ], plugin: ['browserify-derequire'] diff --git a/src/polyfills.js b/src/polyfills.js index aa869b1b..49aa9d86 100644 --- a/src/polyfills.js +++ b/src/polyfills.js @@ -11,37 +11,44 @@ import util from './util'; /* eslint-disable import/no-extraneous-dependencies */ if (typeof window !== 'undefined') { - if (typeof window.fetch === 'undefined') { - require('whatwg-fetch'); - } - if (typeof Array.prototype.fill === 'undefined') { - require('core-js/fn/array/fill'); - } - if (typeof Array.prototype.find === 'undefined') { - require('core-js/fn/array/find'); - } - if (typeof Array.prototype.includes === 'undefined') { - require('core-js/fn/array/includes'); - } - if (typeof Array.from === 'undefined') { - require('core-js/fn/array/from'); - } + /******************************************************************** + * NOTE: This list is duplicated in Gruntfile.js, * + * so that these polyfills are only included in the compat bundle. * + ********************************************************************/ - // No if-statement on Promise because of IE11. Otherwise Promise is undefined in the service worker. - require('core-js/fn/promise'); + try { + if (typeof window.fetch === 'undefined') { + require('whatwg-fetch'); + } + if (typeof Array.prototype.fill === 'undefined') { + require('core-js/fn/array/fill'); + } + if (typeof Array.prototype.find === 'undefined') { + require('core-js/fn/array/find'); + } + if (typeof Array.prototype.includes === 'undefined') { + require('core-js/fn/array/includes'); + } + if (typeof Array.from === 'undefined') { + require('core-js/fn/array/from'); + } - if (typeof Uint8Array.from === 'undefined') { - require('core-js/fn/typed/uint8-array'); - } - if (typeof String.prototype.repeat === 'undefined') { - require('core-js/fn/string/repeat'); - } - if (typeof Symbol === 'undefined') { - require('core-js/fn/symbol'); - } - if (typeof Object.assign === 'undefined') { - require('core-js/fn/object/assign'); - } + // No if-statement on Promise because of IE11. Otherwise Promise is undefined in the service worker. + require('core-js/fn/promise'); + + if (typeof Uint8Array.from === 'undefined') { + require('core-js/fn/typed/uint8-array'); + } + if (typeof String.prototype.repeat === 'undefined') { + require('core-js/fn/string/repeat'); + } + if (typeof Symbol === 'undefined') { + require('core-js/fn/symbol'); + } + if (typeof Object.assign === 'undefined') { + require('core-js/fn/object/assign'); + } + } catch(e) {} } if (typeof TransformStream === 'undefined') { diff --git a/test/general/packet.js b/test/general/packet.js index 71c8a930..dacc0b6e 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -150,7 +150,7 @@ describe("Packet", function() { }); }); - it('Sym. encrypted AEAD protected packet (draft04)', function() { + it('Sym. encrypted AEAD protected packet (draft04)', async function() { let aead_protectVal = openpgp.config.aead_protect; let aead_protect_versionVal = openpgp.config.aead_protect_version; openpgp.config.aead_protect = true; @@ -170,15 +170,15 @@ describe("Packet", function() { const msg2 = new openpgp.packet.List(); - return enc.encrypt(algo, key).then(async function() { + try { + await enc.encrypt(algo, key); await msg2.read(msg.write()); - return msg2[0].decrypt(algo, key); - }).then(async function() { + await msg2[0].decrypt(algo, key); expect(await openpgp.stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data); - }).finally(function() { + } finally { openpgp.config.aead_protect = aead_protectVal; openpgp.config.aead_protect_version = aead_protect_versionVal; - }); + } }); function cryptStub(webCrypto, method) { @@ -201,7 +201,7 @@ describe("Packet", function() { return cryptStub; } - it('Sym. encrypted AEAD protected packet is encrypted in parallel (GCM, draft04)', function() { + it('Sym. encrypted AEAD protected packet is encrypted in parallel (GCM, draft04)', async function() { const webCrypto = openpgp.util.getWebCrypto(); if (!webCrypto) return; const encryptStub = cryptStub(webCrypto, 'encrypt'); @@ -229,23 +229,23 @@ describe("Packet", function() { const msg2 = new openpgp.packet.List(); - return enc.encrypt(algo, key).then(async function() { + try { + await enc.encrypt(algo, key); await msg2.read(msg.write()); - return msg2[0].decrypt(algo, key); - }).then(async function() { + await msg2[0].decrypt(algo, key); expect(await openpgp.stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data); expect(encryptStub.callCount > 1).to.be.true; expect(decryptStub.callCount > 1).to.be.true; - }).finally(function() { + } finally { openpgp.config.aead_protect = aead_protectVal; openpgp.config.aead_protect_version = aead_protect_versionVal; openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteVal; encryptStub.restore(); decryptStub.restore(); - }); + } }); - it('Sym. encrypted AEAD protected packet test vector (draft04)', function() { + it('Sym. encrypted AEAD protected packet test vector (draft04)', async function() { // From https://gitlab.com/openpgp-wg/rfc4880bis/commit/00b20923e6233fb6ff1666ecd5acfefceb32907d let packetBytes = openpgp.util.hex_to_Uint8Array(` @@ -281,19 +281,19 @@ describe("Packet", function() { let randomBytesStub = stub(openpgp.crypto.random, 'getRandomBytes'); randomBytesStub.returns(resolves(iv)); - return enc.encrypt(algo, key).then(async function() { + try { + await enc.encrypt(algo, key); const data = msg.write(); expect(await openpgp.stream.readToEnd(openpgp.stream.clone(data))).to.deep.equal(packetBytes); await msg2.read(data); - return msg2[0].decrypt(algo, key); - }).then(async function() { + await msg2[0].decrypt(algo, key); expect(await openpgp.stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data); - }).finally(function() { + } finally { openpgp.config.aead_protect = aead_protectVal; openpgp.config.aead_protect_version = aead_protect_versionVal; openpgp.config.aead_chunk_size_byte = aead_chunk_size_byteVal; randomBytesStub.restore(); - }); + } }); it('Sym encrypted session key with a compressed packet', async function() { @@ -826,7 +826,7 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu }); }); - it('Writing and encryption of a secret key packet. (draft04)', function() { + it('Writing and encryption of a secret key packet. (draft04)', async function() { let aead_protectVal = openpgp.config.aead_protect; let aead_protect_versionVal = openpgp.config.aead_protect_version; openpgp.config.aead_protect = true; @@ -838,7 +838,8 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu const rsa = openpgp.crypto.publicKey.rsa; const keySize = openpgp.util.getWebCryptoAll() ? 2048 : 512; // webkit webcrypto accepts minimum 2048 bit keys - return rsa.generate(keySize, "10001").then(async function(mpiGen) { + try { + const mpiGen = await rsa.generate(keySize, "10001"); let mpi = [mpiGen.n, mpiGen.e, mpiGen.d, mpiGen.p, mpiGen.q, mpiGen.u]; mpi = mpi.map(function(k) { return new openpgp.MPI(k); @@ -855,10 +856,10 @@ kePFjAnu9cpynKXu3usf8+FuBw2zLsg1Id1n7ttxoAte416KjBN9lFBt8mcu await key2[0].decrypt('hello'); expect(key[0].params.toString()).to.equal(key2[0].params.toString()); - }).finally(function() { + } finally { openpgp.config.aead_protect = aead_protectVal; openpgp.config.aead_protect_version = aead_protect_versionVal; - }); + } }); it('Writing and verification of a signature packet.', function() { diff --git a/travis.sh b/travis.sh index 6c743961..5db421c2 100755 --- a/travis.sh +++ b/travis.sh @@ -15,13 +15,13 @@ elif [[ $OPENPGPJSTEST =~ ^end2end-.* ]]; then echo "Running OpenPGP.js browser unit tests on Saucelabs." declare -a capabilities=( - "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Firefox\", \"version\":\"34\", \"platform\":\"OS X 10.12\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" + "export COMPAT=1 SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Firefox\", \"version\":\"34\", \"platform\":\"OS X 10.12\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Firefox\", \"version\":\"54\", \"platform\":\"OS X 10.12\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" - "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Chrome\", \"version\":\"41\", \"platform\":\"OS X 10.12\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\", \"extendedDebugging\":true}'" + "export COMPAT=1 SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Chrome\", \"version\":\"41\", \"platform\":\"OS X 10.12\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\", \"extendedDebugging\":true}'" "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Chrome\", \"version\":\"59\", \"platform\":\"OS X 10.12\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\", \"extendedDebugging\":true}'" - "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Internet Explorer\", \"version\":\"11.103\", \"platform\":\"Windows 10\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" + "export COMPAT=1 SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Internet Explorer\", \"version\":\"11.103\", \"platform\":\"Windows 10\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"MicrosoftEdge\", \"version\":\"15.15063\", \"platform\":\"Windows 10\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" - "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Safari\", \"version\":\"8\", \"platform\":\"OS X 10.10\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" + "export COMPAT=1 SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Safari\", \"version\":\"8\", \"platform\":\"OS X 10.10\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Safari\", \"version\":\"10\", \"platform\":\"macOS 10.12\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Browser\", \"platformName\":\"Android\", \"platformVersion\": \"4.4\", \"deviceName\": \"Android Emulator\", \"deviceOrientation\": \"portrait\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" "export SELENIUM_BROWSER_CAPABILITIES='{\"browserName\":\"Chrome\", \"platformName\":\"Android\", \"platformVersion\": \"6.0\", \"deviceName\": \"Android Emulator\", \"deviceOrientation\": \"portrait\", \"maxDuration\":\"7200\", \"commandTimeout\":\"600\", \"idleTimeout\":\"270\"}'" @@ -36,7 +36,7 @@ elif [[ $OPENPGPJSTEST =~ ^end2end-.* ]]; then echo "Testing Configuration: ${testkey}" eval $capability - grunt saucelabs & + grunt saucelabs --compat=$COMPAT & background_process_pid=$! # https://github.com/travis-ci/travis-ci/issues/4190