From bdf1ce8d64a5b40025ed54644647ce4c94bb63d1 Mon Sep 17 00:00:00 2001 From: Bart Butler Date: Tue, 6 Feb 2018 10:56:29 -0800 Subject: [PATCH] add some more polyfills for old browsers --- npm-shrinkwrap.json | 8 ++------ package.json | 3 +-- src/openpgp.js | 10 ++++------ src/polyfills.js | 36 ++++++++++++++++++++++++++++++++++++ src/worker/worker.js | 5 ----- test/unittests.js | 5 ----- 6 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 src/polyfills.js diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index f92a1f62..bc5a6b08 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1920,7 +1920,8 @@ "core-js": { "version": "2.5.3", "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.3.tgz", - "integrity": "sha1-isw4NFgk8W2DZbfJtCWRaOjtYD4=" + "integrity": "sha1-isw4NFgk8W2DZbfJtCWRaOjtYD4=", + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2422,11 +2423,6 @@ "event-emitter": "0.3.5" } }, - "es6-promise": { - "version": "4.2.4", - "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.4.tgz", - "integrity": "sha512-/NdNZVJg+uZgtm9eS3O6lrOLYmQag2DjdEXuPaHlZ6RuVqgqaVZfgYCepEIKsLqwdQArOPtC3XzRLqGGfT8KQQ==" - }, "es6-set": { "version": "0.1.5", "resolved": "https://registry.npmjs.org/es6-set/-/es6-set-0.1.5.tgz", diff --git a/package.json b/package.json index 89ecf6a0..18f496b1 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "browserify-derequire": "^0.9.4", "chai": "^4.1.2", "chai-as-promised": "^7.1.1", + "core-js": "^2.5.3", "eslint": "^4.16.0", "eslint-config-airbnb": "^16.1.0", "eslint-config-airbnb-base": "^12.1.0", @@ -78,9 +79,7 @@ "babel-plugin-transform-remove-strict-mode": "0.0.2", "bn.js": "^4.11.8", "buffer": "^5.0.8", - "core-js": "^2.5.3", "elliptic": "git+https://github.com/openpgpjs/elliptic.git", - "es6-promise": "^4.2.4", "jwk-to-pem": "^1.2.6", "node-fetch": "^1.7.3", "node-localstorage": "~1.3.0", diff --git a/src/openpgp.js b/src/openpgp.js index be207d02..a7d56022 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -41,12 +41,10 @@ import config from './config/config'; import util from './util'; import AsyncProxy from './worker/async_proxy'; -// Polyfills -require('core-js/fn/array/fill'); -require('core-js/fn/array/find'); -require('core-js/fn/array/from'); -require('core-js/fn/typed/uint8-array'); -require('core-js/fn/symbol'); +// Old browser polyfills +if (typeof window !== 'undefined') { + require('./polyfills'); +} ////////////////////////// // // diff --git a/src/polyfills.js b/src/polyfills.js new file mode 100644 index 00000000..59fed34a --- /dev/null +++ b/src/polyfills.js @@ -0,0 +1,36 @@ +// Old browser polyfills +// All are listed as dev dependencies because Node does not need them +// and for browser babel will take care of it + +if (typeof window.fetch === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('whatwg-fetch'); +} +if (typeof Array.prototype.fill === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('core-js/fn/array/fill'); +} +if (typeof Array.prototype.find === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('core-js/fn/array/find'); +} +if (typeof Array.from === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('core-js/fn/array/from'); +} +if (typeof Promise === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('core-js/fn/promise'); +} +if (typeof Uint8Array.from === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('core-js/fn/typed/uint8-array'); +} +if (typeof String.prototype.repeat === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('core-js/fn/string/repeat'); +} +if (typeof Symbol === 'undefined') { + // eslint-disable-next-line import/no-extraneous-dependencies + require('core-js/fn/symbol'); +} diff --git a/src/worker/worker.js b/src/worker/worker.js index 3e52f781..2b25f319 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -19,11 +19,6 @@ self.window = {}; // to make UMD bundles work -// ES6 Promise polyfill -if (typeof Promise === 'undefined') { - require('es6-promise').polyfill(); -} - importScripts('openpgp.js'); var openpgp = window.openpgp; diff --git a/test/unittests.js b/test/unittests.js index 29b8100e..2e07de4b 100644 --- a/test/unittests.js +++ b/test/unittests.js @@ -1,8 +1,3 @@ -// ES6 Promise polyfill -if (typeof Promise === 'undefined') { - require('es6-promise').polyfill(); -} - (typeof window !== 'undefined' ? window : global).resolves = function(val) { return new Promise(function(res) { res(val); }); };