Cleanup OpenPGP.js a bit.

openpgp.worker doesn't need webpack
This commit is contained in:
djmaze 2021-05-01 16:33:30 +02:00
parent 5e63adc904
commit 8c454ccd9d
7 changed files with 212 additions and 207 deletions

View file

@ -137,6 +137,11 @@ RainLoop 1.15 vs SnappyMail
|TOTAL |1.445.204 | 394.361 |425.477 |123.965 |353.301 |107.321 |
|TOTAL (no admin) |1.189.690 | 344.490 |351.061 |109.194 |292.627 | 94.108 |
|OpenPGP |RainLoop |Snappy |RL gzip |SM gzip |RL brotli |SM brotli |
|--------------- |--------: |--------: |------: |------: |--------: |--------: |
|openpgp.min.js | 330.742 | 330.300 |102.388 |102.388 | 84.241 | 84.121 |
|openpgp.worker | 1.499 | 1.125 | 824 | 567 | 695 | 467 |
For a user its around 66% smaller and faster than traditional RainLoop.
### CSS changes

View file

@ -37,8 +37,8 @@ const jsServiceWorker = () => {
// OpenPGP
const jsOpenPGP = () => {
return gulp
.src('vendors/openpgp-2.6.2/dist/openpgp.min.js')
.pipe(gulp.dest(config.paths.staticMinJS));
.src('vendors/openpgp-2.6.2/dist/openpgp.js')
.pipe(gulp.dest(config.paths.staticJS));
};
// OpenPGP Worker

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,3 @@
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH
//
@ -16,10 +15,6 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
/* globals self: true */
self.window = {}; // to make UMD bundles work
importScripts('openpgp.js');
var openpgp = window.openpgp;
@ -32,7 +27,7 @@ openpgp.crypto.random.randomBuffer.init(MAX_SIZE_RANDOM_BUFFER);
* Handle messages from the main window.
* @param {Object} event Contains event type and data
*/
self.onmessage = function(event) {
self.onmessage = event => {
var msg = event.data || {};
switch (msg.event) {
@ -101,4 +96,3 @@ function response(event) {
}
self.postMessage(event, openpgp.util.getTransferables.call(openpgp.util, event.data));
}
},{}]},{},[1]);

View file

@ -1,2 +1 @@
/*! OpenPGP.js v2.6.2 - 2018-01-21 - this is LGPL licensed code, see LICENSE/our website http://openpgpjs.org/ for more information. */
!function e(n,r,t){function o(i,f){if(!r[i]){if(!n[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(a)return a(i,!0);var s=new Error("Cannot find module '"+i+"'");throw s.code="MODULE_NOT_FOUND",s}var u=r[i]={exports:{}};n[i][0].call(u.exports,function(e){var r=n[i][1][e];return o(r||e)},u,u.exports,e,n,r,t)}return r[i].exports}for(var a="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({1:[function(e,n,r){function t(e){o.crypto.random.randomBuffer.size<a&&self.postMessage({event:"request-seed"}),self.postMessage(e,o.util.getTransferables.call(o.util,e.data))}self.window={},importScripts("openpgp.min.js");var o=window.openpgp,a=4e4;o.crypto.random.randomBuffer.init(6e4),self.onmessage=function(e){var n=e.data||{};switch(n.event){case"configure":!function(e){for(var n in e)o.config[n]=e[n]}(n.config);break;case"seed-random":!function(e){e instanceof Uint8Array||(e=new Uint8Array(e)),o.crypto.random.randomBuffer.set(e)}(n.buf);break;default:!function(e,n,r){"function"==typeof o[n]?(r=o.packet.clone.parseClonedPackets(r,n),o[n](r).then(function(n){t({id:e,event:"method-return",data:o.packet.clone.clonePackets(n)})}).catch(function(n){t({id:e,event:"method-return",err:n.message,stack:n.stack})})):t({id:e,event:"method-return",err:"Unknown Worker Event"})}(n.id,n.event,n.options||{})}}},{}]},{},[1]);
importScripts("openpgp.min.js");var openpgp=window.openpgp,MIN_SIZE_RANDOM_BUFFER=4e4,MAX_SIZE_RANDOM_BUFFER=6e4;function configure(e){for(var n in e)openpgp.config[n]=e[n]}function seedRandom(e){e instanceof Uint8Array||(e=new Uint8Array(e)),openpgp.crypto.random.randomBuffer.set(e)}function delegate(e,n,o){"function"==typeof openpgp[n]?(o=openpgp.packet.clone.parseClonedPackets(o,n),openpgp[n](o).then((function(n){response({id:e,event:"method-return",data:openpgp.packet.clone.clonePackets(n)})})).catch((function(n){response({id:e,event:"method-return",err:n.message,stack:n.stack})}))):response({id:e,event:"method-return",err:"Unknown Worker Event"})}function response(e){openpgp.crypto.random.randomBuffer.size<MIN_SIZE_RANDOM_BUFFER&&self.postMessage({event:"request-seed"}),self.postMessage(e,openpgp.util.getTransferables.call(openpgp.util,e.data))}openpgp.crypto.random.randomBuffer.init(MAX_SIZE_RANDOM_BUFFER),self.onmessage=e=>{var n=e.data||{};switch(n.event){case"configure":configure(n.config);break;case"seed-random":seedRandom(n.buf);break;default:delegate(n.id,n.event,n.options||{})}};

View file

@ -34,92 +34,72 @@ import LocalStore from './localstore.js';
* @constructor
* @param {class} [storeHandler] class implementing loadPublic(), loadPrivate(), storePublic(), and storePrivate() methods
*/
export default function Keyring(storeHandler) {
export default class Keyring
{
constructor(storeHandler) {
this.storeHandler = storeHandler || new LocalStore();
this.publicKeys = new KeyArray(this.storeHandler.loadPublic());
this.privateKeys = new KeyArray(this.storeHandler.loadPrivate());
}
}
/**
/**
* Calls the storeHandler to save the keys
*/
Keyring.prototype.store = function () {
store() {
this.storeHandler.storePublic(this.publicKeys.keys);
this.storeHandler.storePrivate(this.privateKeys.keys);
};
}
/**
/**
* Clear the keyring - erase all the keys
*/
Keyring.prototype.clear = function() {
clear() {
this.publicKeys.keys = [];
this.privateKeys.keys = [];
};
}
/**
/**
* Searches the keyring for keys having the specified key id
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @param {Boolean} deep if true search also in subkeys
* @return {Array<module:key~Key>|null} keys found or null
* @returns {Array<module:key.Key>|null} keys found or null
*/
Keyring.prototype.getKeysForId = function (keyId, deep) {
getKeysForId(keyId, deep) {
var result = [];
result = result.concat(this.publicKeys.getForId(keyId, deep) || []);
result = result.concat(this.privateKeys.getForId(keyId, deep) || []);
return result.length ? result : null;
};
}
/**
/**
* Removes keys having the specified key id from the keyring
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @return {Array<module:key~Key>|null} keys found or null
* @returns {Array<module:key.Key>|null} keys found or null
*/
Keyring.prototype.removeKeysForId = function (keyId) {
removeKeysForId(keyId) {
var result = [];
result = result.concat(this.publicKeys.removeForId(keyId) || []);
result = result.concat(this.privateKeys.removeForId(keyId) || []);
return result.length ? result : null;
};
}
/**
/**
* Get all public and private keys
* @return {Array<module:key~Key>} all keys
* @returns {Array<module:key.Key>} all keys
*/
Keyring.prototype.getAllKeys = function () {
getAllKeys() {
return this.publicKeys.keys.concat(this.privateKeys.keys);
};
/**
* Array of keys
* @param {Array<module:key~Key>} keys The keys to store in this array
*/
function KeyArray(keys) {
this.keys = keys;
}
}
/**
* Searches all keys in the KeyArray matching the address or address part of the user ids
* @param {String} email email address to search for
* @return {Array<module:key~Key>} The public keys associated with provided email address.
*/
KeyArray.prototype.getForAddress = function(email) {
var results = [];
for (var i = 0; i < this.keys.length; i++) {
if (emailCheck(email, this.keys[i])) {
results.push(this.keys[i]);
}
}
return results;
};
/**
* Checks a key to see if it matches the specified email address
* @private
* @param {String} email email address to search for
* @param {module:key~Key} key The key to be checked.
* @return {Boolean} True if the email address is defined in the specified key
* @param {module:key.Key} key The key to be checked.
* @returns {Boolean} True if the email address is defined in the specified key
*/
function emailCheck(email, key) {
email = email.toLowerCase();
@ -142,7 +122,7 @@ function emailCheck(email, key) {
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @param {module:packet/secret_key|public_key|public_subkey|secret_subkey} keypacket The keypacket to be checked
* @return {Boolean} True if keypacket has the specified keyid
* @returns {Boolean} True if keypacket has the specified keyid
*/
function keyIdCheck(keyId, keypacket) {
if (keyId.length === 16) {
@ -153,13 +133,38 @@ function keyIdCheck(keyId, keypacket) {
}
/**
* Array of keys
* @param {Array<module:key.Key>} keys The keys to store in this array
*/
class KeyArray
{
constructor(keys) {
this.keys = keys;
}
/**
* Searches all keys in the KeyArray matching the address or address part of the user ids
* @param {String} email email address to search for
* @returns {Array<module:key.Key>} The public keys associated with provided email address.
*/
getForAddress(email) {
var results = [];
for (var i = 0; i < this.keys.length; i++) {
if (emailCheck(email, this.keys[i])) {
results.push(this.keys[i]);
}
}
return results;
}
/**
* Searches the KeyArray for a key having the specified key id
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @param {Boolean} deep if true search also in subkeys
* @return {module:key~Key|null} key found or null
* @returns {module:key.Key|null} key found or null
*/
KeyArray.prototype.getForId = function (keyId, deep) {
getForId(keyId, deep) {
for (var i = 0; i < this.keys.length; i++) {
if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
return this.keys[i];
@ -173,14 +178,14 @@ KeyArray.prototype.getForId = function (keyId, deep) {
}
}
return null;
};
}
/**
/**
* Imports a key from an ascii armored message
* @param {String} armored message to read the keys/key from
* @return {Array<Error>|null} array of error objects or null
* @returns {Array<Error>|null} array of error objects or null
*/
KeyArray.prototype.importKey = function (armored) {
importKey(armored) {
var imported = keyModule.readArmored(armored);
var that = this;
imported.keys.forEach(function(key) {
@ -194,28 +199,29 @@ KeyArray.prototype.importKey = function (armored) {
}
});
return imported.err ? imported.err : null;
};
}
/**
/**
* Add key to KeyArray
* @param {module:key~Key} key The key that will be added to the keyring
* @return {Number} The new length of the KeyArray
* @param {module:key.Key} key The key that will be added to the keyring
* @returns {Number} The new length of the KeyArray
*/
KeyArray.prototype.push = function (key) {
push(key) {
return this.keys.push(key);
};
}
/**
/**
* Removes a key with the specified keyid from the keyring
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @return {module:key~Key|null} The key object which has been removed or null
* @returns {module:key.Key|null} The key object which has been removed or null
*/
KeyArray.prototype.removeForId = function (keyId) {
for (var i = 0; i < this.keys.length; i++) {
removeForId(keyId) {
for (let i = 0; i < this.keys.length; i++) {
if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
return this.keys.splice(i, 1)[0];
}
}
return null;
};
}
}

View file

@ -28,39 +28,6 @@ import config from '../config';
import * as keyModule from '../key.js';
import util from '../util.js';
export default 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;
} else {
this.storage = new (require('node-localstorage').LocalStorage)(config.node_store);
}
}
/*
* Declare the localstore itemnames
*/
LocalStore.prototype.publicKeysItem = 'public-keys';
LocalStore.prototype.privateKeysItem = 'private-keys';
/**
* Load the public keys from HTML5 local storage.
* @return {Array<module:key~Key>} array of keys retrieved from localstore
*/
LocalStore.prototype.loadPublic = function () {
return loadKeys(this.storage, this.publicKeysItem);
};
/**
* Load the private keys from HTML5 local storage.
* @return {Array<module:key~Key>} array of keys retrieved from localstore
*/
LocalStore.prototype.loadPrivate = function () {
return loadKeys(this.storage, this.privateKeysItem);
};
function loadKeys(storage, itemname) {
var armoredKeys = JSON.parse(storage.getItem(itemname));
var keys = [];
@ -78,24 +45,6 @@ function loadKeys(storage, itemname) {
return keys;
}
/**
* Saves the current state of the public keys to HTML5 local storage.
* The key array gets stringified using JSON
* @param {Array<module:key~Key>} keys array of keys to save in localstore
*/
LocalStore.prototype.storePublic = function (keys) {
storeKeys(this.storage, this.publicKeysItem, keys);
};
/**
* Saves the current state of the private keys to HTML5 local storage.
* The key array gets stringified using JSON
* @param {Array<module:key~Key>} keys array of keys to save in localstore
*/
LocalStore.prototype.storePrivate = function (keys) {
storeKeys(this.storage, this.privateKeysItem, keys);
};
function storeKeys(storage, itemname, keys) {
var armoredKeys = [];
if (keys.length) {
@ -107,3 +56,57 @@ function storeKeys(storage, itemname, keys) {
storage.removeItem(itemname);
}
}
export default class LocalStore
{
constructor(prefix) {
prefix = prefix || 'openpgp-';
this.publicKeysItem = prefix + this.publicKeysItem;
this.privateKeysItem = prefix + this.privateKeysItem;
if (typeof window !== 'undefined' && window.localStorage) {
this.storage = window.localStorage;
} else {
this.storage = new (require('node-localstorage').LocalStorage)(config.node_store);
}
}
/**
* Load the public keys from HTML5 local storage.
* @return {Array<module:key~Key>} array of keys retrieved from localstore
*/
loadPublic() {
return loadKeys(this.storage, this.publicKeysItem);
}
/**
* Load the private keys from HTML5 local storage.
* @return {Array<module:key~Key>} array of keys retrieved from localstore
*/
loadPrivate() {
return loadKeys(this.storage, this.privateKeysItem);
}
/**
* Saves the current state of the public keys to HTML5 local storage.
* The key array gets stringified using JSON
* @param {Array<module:key~Key>} keys array of keys to save in localstore
*/
storePublic(keys) {
storeKeys(this.storage, this.publicKeysItem, keys);
}
/**
* Saves the current state of the private keys to HTML5 local storage.
* The key array gets stringified using JSON
* @param {Array<module:key~Key>} keys array of keys to save in localstore
*/
storePrivate(keys) {
storeKeys(this.storage, this.privateKeysItem, keys);
}
}
/*
* Declare the localstore itemnames
*/
LocalStore.prototype.publicKeysItem = 'public-keys';
LocalStore.prototype.privateKeysItem = 'private-keys';