Replace 'window' with 'global'
In order to use Web Crypto in application workers, among other things.
This commit is contained in:
parent
81d6b45ba8
commit
6e13604a64
|
@ -15,7 +15,7 @@ function LocalStorage() {}
|
||||||
* if config is null the default config will be used
|
* if config is null the default config will be used
|
||||||
*/
|
*/
|
||||||
LocalStorage.prototype.read = function () {
|
LocalStorage.prototype.read = function () {
|
||||||
const raw = window.localStorage.getItem("config");
|
const raw = global.localStorage.getItem("config");
|
||||||
const cf = (raw === null ? null : JSON.parse(raw));
|
const cf = (raw === null ? null : JSON.parse(raw));
|
||||||
if (cf === null) {
|
if (cf === null) {
|
||||||
this.config = this.default_config;
|
this.config = this.default_config;
|
||||||
|
@ -29,7 +29,7 @@ LocalStorage.prototype.read = function () {
|
||||||
* Writes the config to HTML5 local storage
|
* Writes the config to HTML5 local storage
|
||||||
*/
|
*/
|
||||||
LocalStorage.prototype.write = function () {
|
LocalStorage.prototype.write = function () {
|
||||||
window.localStorage.setItem("config", JSON.stringify(this.config));
|
global.localStorage.setItem("config", JSON.stringify(this.config));
|
||||||
};
|
};
|
||||||
|
|
||||||
export default LocalStorage;
|
export default LocalStorage;
|
||||||
|
|
|
@ -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<elliptic>}
|
* @returns {Promise<elliptic>}
|
||||||
*/
|
*/
|
||||||
async function loadEllipticPromise() {
|
async function loadEllipticPromise() {
|
||||||
|
@ -51,10 +51,10 @@ async function loadEllipticPromise() {
|
||||||
const mainUrl = URL.createObjectURL(new Blob([ellipticContents], { type: 'text/javascript' }));
|
const mainUrl = URL.createObjectURL(new Blob([ellipticContents], { type: 'text/javascript' }));
|
||||||
await loadScript(mainUrl);
|
await loadScript(mainUrl);
|
||||||
URL.revokeObjectURL(mainUrl);
|
URL.revokeObjectURL(mainUrl);
|
||||||
if (!window.openpgp.elliptic) {
|
if (!global.openpgp.elliptic) {
|
||||||
throw new Error('Elliptic library failed to load correctly');
|
throw new Error('Elliptic library failed to load correctly');
|
||||||
}
|
}
|
||||||
return window.openpgp.elliptic;
|
return global.openpgp.elliptic;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ellipticPromise;
|
let ellipticPromise;
|
||||||
|
|
|
@ -186,7 +186,7 @@ export default {
|
||||||
if (util.getWebCrypto()) {
|
if (util.getWebCrypto()) {
|
||||||
let keyPair;
|
let keyPair;
|
||||||
let keyGenOpt;
|
let keyGenOpt;
|
||||||
if ((window.crypto && window.crypto.subtle) || window.msCrypto) {
|
if ((global.crypto && global.crypto.subtle) || global.msCrypto) {
|
||||||
// current standard spec
|
// current standard spec
|
||||||
keyGenOpt = {
|
keyGenOpt = {
|
||||||
name: 'RSASSA-PKCS1-v1_5',
|
name: 'RSASSA-PKCS1-v1_5',
|
||||||
|
@ -198,7 +198,7 @@ export default {
|
||||||
};
|
};
|
||||||
keyPair = webCrypto.generateKey(keyGenOpt, true, ['sign', 'verify']);
|
keyPair = webCrypto.generateKey(keyGenOpt, true, ['sign', 'verify']);
|
||||||
keyPair = await promisifyIE11Op(keyPair, 'Error generating RSA key pair.');
|
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
|
// outdated spec implemented by old Webkit
|
||||||
keyGenOpt = {
|
keyGenOpt = {
|
||||||
name: 'RSA-OAEP',
|
name: 'RSA-OAEP',
|
||||||
|
|
|
@ -41,8 +41,8 @@ export default {
|
||||||
const buf = new Uint8Array(length);
|
const buf = new Uint8Array(length);
|
||||||
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
||||||
crypto.getRandomValues(buf);
|
crypto.getRandomValues(buf);
|
||||||
} else if (typeof window !== 'undefined' && typeof window.msCrypto === 'object' && typeof window.msCrypto.getRandomValues === 'function') {
|
} else if (typeof global !== 'undefined' && typeof global.msCrypto === 'object' && typeof global.msCrypto.getRandomValues === 'function') {
|
||||||
window.msCrypto.getRandomValues(buf);
|
global.msCrypto.getRandomValues(buf);
|
||||||
} else if (nodeCrypto) {
|
} else if (nodeCrypto) {
|
||||||
const bytes = nodeCrypto.randomBytes(buf.length);
|
const bytes = nodeCrypto.randomBytes(buf.length);
|
||||||
buf.set(bytes);
|
buf.set(bytes);
|
||||||
|
|
|
@ -32,7 +32,7 @@ import config from './config';
|
||||||
*/
|
*/
|
||||||
function HKP(keyServerBaseUrl) {
|
function HKP(keyServerBaseUrl) {
|
||||||
this._baseUrl = keyServerBaseUrl || config.keyserver;
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -39,8 +39,8 @@ function LocalStore(prefix) {
|
||||||
prefix = prefix || 'openpgp-';
|
prefix = prefix || 'openpgp-';
|
||||||
this.publicKeysItem = prefix + this.publicKeysItem;
|
this.publicKeysItem = prefix + this.publicKeysItem;
|
||||||
this.privateKeysItem = prefix + this.privateKeysItem;
|
this.privateKeysItem = prefix + this.privateKeysItem;
|
||||||
if (typeof window !== 'undefined' && window.localStorage) {
|
if (typeof global !== 'undefined' && global.localStorage) {
|
||||||
this.storage = window.localStorage;
|
this.storage = global.localStorage;
|
||||||
} else {
|
} else {
|
||||||
this.storage = new (require('node-localstorage').LocalStorage)(config.node_store);
|
this.storage = new (require('node-localstorage').LocalStorage)(config.node_store);
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ let asyncProxy; // instance of the asyncproxy
|
||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
export async function initWorker({ path = 'openpgp.worker.js', n = 1, workers = [] } = {}) {
|
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 proxy = new AsyncProxy({ path, n, workers, config });
|
||||||
const loaded = await proxy.loaded();
|
const loaded = await proxy.loaded();
|
||||||
if (loaded) {
|
if (loaded) {
|
||||||
|
|
|
@ -8,14 +8,14 @@
|
||||||
|
|
||||||
import util from './util';
|
import util from './util';
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof global !== 'undefined') {
|
||||||
/********************************************************************
|
/********************************************************************
|
||||||
* NOTE: This list is duplicated in Gruntfile.js, *
|
* NOTE: This list is duplicated in Gruntfile.js, *
|
||||||
* so that these polyfills are only included in the compat bundle. *
|
* so that these polyfills are only included in the compat bundle. *
|
||||||
********************************************************************/
|
********************************************************************/
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (typeof window.fetch === 'undefined') {
|
if (typeof global.fetch === 'undefined') {
|
||||||
require('whatwg-fetch');
|
require('whatwg-fetch');
|
||||||
}
|
}
|
||||||
if (typeof Array.prototype.fill === 'undefined') {
|
if (typeof Array.prototype.fill === 'undefined') {
|
||||||
|
|
12
src/util.js
12
src/util.js
|
@ -546,7 +546,7 @@ export default {
|
||||||
return;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof global !== 'undefined') {
|
||||||
if (window.crypto) {
|
if (global.crypto) {
|
||||||
return window.crypto.subtle || window.crypto.webkitSubtle;
|
return global.crypto.subtle || global.crypto.webkitSubtle;
|
||||||
}
|
}
|
||||||
if (window.msCrypto) {
|
if (global.msCrypto) {
|
||||||
return window.msCrypto.subtle;
|
return global.msCrypto.subtle;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -31,7 +31,7 @@ import * as keyMod from './key';
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function WKD() {
|
function WKD() {
|
||||||
this._fetch = typeof window !== 'undefined' ? window.fetch : require('node-fetch');
|
this._fetch = typeof global !== 'undefined' ? global.fetch : require('node-fetch');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -28,10 +28,8 @@
|
||||||
* @module worker/worker
|
* @module worker/worker
|
||||||
*/
|
*/
|
||||||
|
|
||||||
self.window = self; // to make UMD bundles work
|
|
||||||
|
|
||||||
importScripts('openpgp.js');
|
importScripts('openpgp.js');
|
||||||
var openpgp = window.openpgp;
|
var openpgp = global.openpgp;
|
||||||
|
|
||||||
var randomQueue = [];
|
var randomQueue = [];
|
||||||
var MAX_SIZE_RANDOM_BUFFER = 60000;
|
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.
|
* as this api is only avalible in the main window.
|
||||||
* @param {ArrayBuffer} buffer Some random bytes
|
* @param {ArrayBuffer} buffer Some random bytes
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue
Block a user