From 3a84442b5f3a623f4394feae16e31153dc06056e Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Mon, 17 Feb 2020 18:02:18 +0100 Subject: [PATCH] Don't use native streams in old Edge --- src/openpgp.js | 7 ++++++- test/general/openpgp.js | 13 ++++++++----- test/general/streaming.js | 5 +++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/openpgp.js b/src/openpgp.js index 21452b15..a3938c49 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -50,7 +50,12 @@ import './polyfills'; import util from './util'; import AsyncProxy from './worker/async_proxy'; -const toNativeReadable = global.ReadableStream && createReadableStreamWrapper(global.ReadableStream); +let toNativeReadable; +if (global.ReadableStream) { + try { + toNativeReadable = createReadableStreamWrapper(global.ReadableStream); + } catch (e) {} +} ////////////////////////// // // diff --git a/test/general/openpgp.js b/test/general/openpgp.js index b3429d8f..77dc2933 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1998,7 +1998,9 @@ describe('OpenPGP.js public api tests', function() { it('Streaming encrypt and decrypt small message roundtrip', async function() { let plaintext = []; let i = 0; - const data = new (global.ReadableStream || openpgp.stream.ReadableStream)({ + const useNativeStream = (() => { try { new global.ReadableStream(); return true; } catch (e) { return false; } })(); + const ReadableStream = useNativeStream ? global.ReadableStream : openpgp.stream.ReadableStream; + const data = new ReadableStream({ async pull(controller) { if (i++ < 4) { let randomBytes = await openpgp.crypto.random.getRandomBytes(10); @@ -2013,7 +2015,7 @@ describe('OpenPGP.js public api tests', function() { message: openpgp.message.fromBinary(data), passwords: ['test'] })); - expect(openpgp.util.isStream(encrypted.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); + expect(openpgp.util.isStream(encrypted.data)).to.equal(useNativeStream ? 'web' : 'ponyfill'); const msgAsciiArmored = encrypted.data; const message = await openpgp.message.readArmored(msgAsciiArmored); @@ -2022,7 +2024,7 @@ describe('OpenPGP.js public api tests', function() { message, format: 'binary' }); - expect(openpgp.util.isStream(decrypted.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); + expect(openpgp.util.isStream(decrypted.data)).to.equal(useNativeStream ? 'web' : 'ponyfill'); expect(await openpgp.stream.readToEnd(decrypted.data)).to.deep.equal(openpgp.util.concatUint8Array(plaintext)); }); }); @@ -2328,8 +2330,9 @@ describe('OpenPGP.js public api tests', function() { streaming: 'web', format: 'binary' }; + const useNativeStream = (() => { try { new global.ReadableStream(); return true; } catch (e) { return false; } })(); return openpgp.sign(signOpt).then(async function (signed) { - expect(openpgp.util.isStream(signed.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); + expect(openpgp.util.isStream(signed.data)).to.equal(useNativeStream ? 'web' : 'ponyfill'); const message = await openpgp.message.read(signed.data); message.packets.concat(await openpgp.stream.readToEnd(message.packets.stream, _ => _)); const packets = new openpgp.packet.List(); @@ -2338,7 +2341,7 @@ describe('OpenPGP.js public api tests', function() { verifyOpt.message = new openpgp.message.Message(packets); return openpgp.verify(verifyOpt); }).then(async function (verified) { - expect(openpgp.stream.isStream(verified.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); + expect(openpgp.stream.isStream(verified.data)).to.equal(useNativeStream ? 'web' : 'ponyfill'); expect([].slice.call(await openpgp.stream.readToEnd(verified.data))).to.deep.equal([].slice.call(data)); expect(await verified.signatures[0].verified).to.be.true; expect(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid)) diff --git a/test/general/streaming.js b/test/general/streaming.js index 731051ce..7b811cd1 100644 --- a/test/general/streaming.js +++ b/test/general/streaming.js @@ -9,7 +9,8 @@ const { expect } = chai; const { stream, util } = openpgp; -const ReadableStream = global.ReadableStream || openpgp.stream.ReadableStream; +const useNativeStream = (() => { try { new global.ReadableStream(); return true; } catch (e) { return false; } })(); +const ReadableStream = useNativeStream ? global.ReadableStream : openpgp.stream.ReadableStream; const pub_key = ['-----BEGIN PGP PUBLIC KEY BLOCK-----', @@ -936,7 +937,7 @@ describe('Streaming', function() { tryTests('WhatWG Streams', tests, { if: true, beforeEach: function() { - expectedType = global.ReadableStream ? 'web' : 'ponyfill'; + expectedType = useNativeStream ? 'web' : 'ponyfill'; } });