From 6e13604a64d96dced12afa9443c6e265b9376689 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Wed, 15 Jan 2020 14:44:48 +0100 Subject: [PATCH] Replace 'window' with 'global' In order to use Web Crypto in application workers, among other things. --- src/config/localStorage.js | 4 ++-- src/crypto/public_key/elliptic/indutnyKey.js | 6 +++--- src/crypto/public_key/rsa.js | 4 ++-- src/crypto/random.js | 4 ++-- src/hkp.js | 2 +- src/keyring/localstore.js | 4 ++-- src/openpgp.js | 2 +- src/polyfills.js | 4 ++-- src/util.js | 12 ++++++------ src/wkd.js | 2 +- src/worker/worker.js | 6 ++---- 11 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/config/localStorage.js b/src/config/localStorage.js index e6671e0a..fb6e20f0 100644 --- a/src/config/localStorage.js +++ b/src/config/localStorage.js @@ -15,7 +15,7 @@ function LocalStorage() {} * if config is null the default config will be used */ LocalStorage.prototype.read = function () { - const raw = window.localStorage.getItem("config"); + const raw = global.localStorage.getItem("config"); const cf = (raw === null ? null : JSON.parse(raw)); if (cf === null) { this.config = this.default_config; @@ -29,7 +29,7 @@ LocalStorage.prototype.read = function () { * Writes the config to HTML5 local storage */ LocalStorage.prototype.write = function () { - window.localStorage.setItem("config", JSON.stringify(this.config)); + global.localStorage.setItem("config", JSON.stringify(this.config)); }; export default LocalStorage; diff --git a/src/crypto/public_key/elliptic/indutnyKey.js b/src/crypto/public_key/elliptic/indutnyKey.js index 50806d91..de947cb8 100644 --- a/src/crypto/public_key/elliptic/indutnyKey.js +++ b/src/crypto/public_key/elliptic/indutnyKey.js @@ -40,7 +40,7 @@ export function keyFromPublic(indutnyCurve, pub) { } /** - * Load elliptic on demand to the window.openpgp.elliptic + * Load elliptic on demand to global.openpgp.elliptic * @returns {Promise} */ async function loadEllipticPromise() { @@ -51,10 +51,10 @@ async function loadEllipticPromise() { const mainUrl = URL.createObjectURL(new Blob([ellipticContents], { type: 'text/javascript' })); await loadScript(mainUrl); URL.revokeObjectURL(mainUrl); - if (!window.openpgp.elliptic) { + if (!global.openpgp.elliptic) { throw new Error('Elliptic library failed to load correctly'); } - return window.openpgp.elliptic; + return global.openpgp.elliptic; } let ellipticPromise; diff --git a/src/crypto/public_key/rsa.js b/src/crypto/public_key/rsa.js index 26b4d8a6..95d0c1f9 100644 --- a/src/crypto/public_key/rsa.js +++ b/src/crypto/public_key/rsa.js @@ -186,7 +186,7 @@ export default { if (util.getWebCrypto()) { let keyPair; let keyGenOpt; - if ((window.crypto && window.crypto.subtle) || window.msCrypto) { + if ((global.crypto && global.crypto.subtle) || global.msCrypto) { // current standard spec keyGenOpt = { name: 'RSASSA-PKCS1-v1_5', @@ -198,7 +198,7 @@ export default { }; keyPair = webCrypto.generateKey(keyGenOpt, true, ['sign', 'verify']); keyPair = await promisifyIE11Op(keyPair, 'Error generating RSA key pair.'); - } else if (window.crypto && window.crypto.webkitSubtle) { + } else if (global.crypto && global.crypto.webkitSubtle) { // outdated spec implemented by old Webkit keyGenOpt = { name: 'RSA-OAEP', diff --git a/src/crypto/random.js b/src/crypto/random.js index 9f1af6b0..b96569fe 100644 --- a/src/crypto/random.js +++ b/src/crypto/random.js @@ -41,8 +41,8 @@ export default { const buf = new Uint8Array(length); if (typeof crypto !== 'undefined' && crypto.getRandomValues) { crypto.getRandomValues(buf); - } else if (typeof window !== 'undefined' && typeof window.msCrypto === 'object' && typeof window.msCrypto.getRandomValues === 'function') { - window.msCrypto.getRandomValues(buf); + } else if (typeof global !== 'undefined' && typeof global.msCrypto === 'object' && typeof global.msCrypto.getRandomValues === 'function') { + global.msCrypto.getRandomValues(buf); } else if (nodeCrypto) { const bytes = nodeCrypto.randomBytes(buf.length); buf.set(bytes); diff --git a/src/hkp.js b/src/hkp.js index a2b798a6..9d181223 100644 --- a/src/hkp.js +++ b/src/hkp.js @@ -32,7 +32,7 @@ import config from './config'; */ function HKP(keyServerBaseUrl) { this._baseUrl = keyServerBaseUrl || config.keyserver; - this._fetch = typeof window !== 'undefined' ? window.fetch : require('node-fetch'); + this._fetch = typeof global !== 'undefined' ? global.fetch : require('node-fetch'); } /** diff --git a/src/keyring/localstore.js b/src/keyring/localstore.js index 902b2fed..26ca4cd7 100644 --- a/src/keyring/localstore.js +++ b/src/keyring/localstore.js @@ -39,8 +39,8 @@ function LocalStore(prefix) { prefix = prefix || 'openpgp-'; this.publicKeysItem = prefix + this.publicKeysItem; this.privateKeysItem = prefix + this.privateKeysItem; - if (typeof window !== 'undefined' && window.localStorage) { - this.storage = window.localStorage; + if (typeof global !== 'undefined' && global.localStorage) { + this.storage = global.localStorage; } else { this.storage = new (require('node-localstorage').LocalStorage)(config.node_store); } diff --git a/src/openpgp.js b/src/openpgp.js index 495ea9e8..2a5d2cab 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -67,7 +67,7 @@ let asyncProxy; // instance of the asyncproxy * @async */ export async function initWorker({ path = 'openpgp.worker.js', n = 1, workers = [] } = {}) { - if (workers.length || (typeof window !== 'undefined' && window.Worker && window.MessageChannel)) { + if (workers.length || (typeof global !== 'undefined' && global.Worker && global.MessageChannel)) { const proxy = new AsyncProxy({ path, n, workers, config }); const loaded = await proxy.loaded(); if (loaded) { diff --git a/src/polyfills.js b/src/polyfills.js index 5cfc146d..27b88917 100644 --- a/src/polyfills.js +++ b/src/polyfills.js @@ -8,14 +8,14 @@ import util from './util'; -if (typeof window !== 'undefined') { +if (typeof global !== 'undefined') { /******************************************************************** * NOTE: This list is duplicated in Gruntfile.js, * * so that these polyfills are only included in the compat bundle. * ********************************************************************/ try { - if (typeof window.fetch === 'undefined') { + if (typeof global.fetch === 'undefined') { require('whatwg-fetch'); } if (typeof Array.prototype.fill === 'undefined') { diff --git a/src/util.js b/src/util.js index b5d11b71..9fe5403e 100644 --- a/src/util.js +++ b/src/util.js @@ -546,7 +546,7 @@ export default { return; } - return typeof window !== 'undefined' && window.crypto && window.crypto.subtle; + return typeof global !== 'undefined' && global.crypto && global.crypto.subtle; }, /** @@ -561,12 +561,12 @@ export default { return; } - if (typeof window !== 'undefined') { - if (window.crypto) { - return window.crypto.subtle || window.crypto.webkitSubtle; + if (typeof global !== 'undefined') { + if (global.crypto) { + return global.crypto.subtle || global.crypto.webkitSubtle; } - if (window.msCrypto) { - return window.msCrypto.subtle; + if (global.msCrypto) { + return global.msCrypto.subtle; } } }, diff --git a/src/wkd.js b/src/wkd.js index 709bd4ae..9b307662 100644 --- a/src/wkd.js +++ b/src/wkd.js @@ -31,7 +31,7 @@ import * as keyMod from './key'; * @constructor */ function WKD() { - this._fetch = typeof window !== 'undefined' ? window.fetch : require('node-fetch'); + this._fetch = typeof global !== 'undefined' ? global.fetch : require('node-fetch'); } /** diff --git a/src/worker/worker.js b/src/worker/worker.js index 05815504..42207421 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -28,10 +28,8 @@ * @module worker/worker */ -self.window = self; // to make UMD bundles work - importScripts('openpgp.js'); -var openpgp = window.openpgp; +var openpgp = global.openpgp; var randomQueue = []; var MAX_SIZE_RANDOM_BUFFER = 60000; @@ -92,7 +90,7 @@ function configure(config) { } /** - * Seed the library with entropy gathered window.crypto.getRandomValues + * Seed the library with entropy gathered global.crypto.getRandomValues * as this api is only avalible in the main window. * @param {ArrayBuffer} buffer Some random bytes */