openpgp.worker.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. // GPG4Browsers - An OpenPGP implementation in javascript
  3. // Copyright (C) 2011 Recurity Labs GmbH
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 3.0 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. /* eslint-disable no-restricted-globals */
  19. /* eslint-disable no-var */
  20. /* eslint-disable vars-on-top */
  21. /**
  22. * @fileoverview Provides functions for communicating with workers
  23. * @see module:openpgp.initWorker
  24. * @see module:openpgp.getWorker
  25. * @see module:openpgp.destroyWorker
  26. * @see module:worker/async_proxy
  27. * @module worker/worker
  28. */
  29. self.window = self; // to make UMD bundles work
  30. importScripts('openpgp.js');
  31. var openpgp = window.openpgp;
  32. var randomQueue = [];
  33. var MAX_SIZE_RANDOM_BUFFER = 60000;
  34. /**
  35. * Handle random buffer exhaustion by requesting more random bytes from the main window
  36. * @returns {Promise<Object>} Empty promise whose resolution indicates that the buffer has been refilled
  37. */
  38. function randomCallback() {
  39. if (!randomQueue.length) {
  40. self.postMessage({ event: 'request-seed', amount: MAX_SIZE_RANDOM_BUFFER });
  41. }
  42. return new Promise(function(resolve) {
  43. randomQueue.push(resolve);
  44. });
  45. }
  46. openpgp.crypto.random.randomBuffer.init(MAX_SIZE_RANDOM_BUFFER, randomCallback);
  47. /**
  48. * Handle messages from the main window.
  49. * @param {Object} event Contains event type and data
  50. */
  51. self.onmessage = function(event) {
  52. var msg = event.data || {};
  53. switch (msg.event) {
  54. case 'configure':
  55. configure(msg.config);
  56. break;
  57. case 'seed-random':
  58. seedRandom(msg.buf);
  59. var queueCopy = randomQueue;
  60. randomQueue = [];
  61. for (var i = 0; i < queueCopy.length; i++) {
  62. queueCopy[i]();
  63. }
  64. break;
  65. default:
  66. delegate(msg.id, msg.event, msg.options || {});
  67. }
  68. };
  69. /**
  70. * Set config from main context to worker context.
  71. * @param {Object} config The openpgp configuration
  72. */
  73. function configure(config) {
  74. Object.keys(config).forEach(function(key) {
  75. openpgp.config[key] = config[key];
  76. });
  77. }
  78. /**
  79. * Seed the library with entropy gathered window.crypto.getRandomValues
  80. * as this api is only avalible in the main window.
  81. * @param {ArrayBuffer} buffer Some random bytes
  82. */
  83. function seedRandom(buffer) {
  84. if (!(buffer instanceof Uint8Array)) {
  85. buffer = new Uint8Array(buffer);
  86. }
  87. openpgp.crypto.random.randomBuffer.set(buffer);
  88. }
  89. /**
  90. * Generic proxy function that handles all commands from the public api.
  91. * @param {String} method The public api function to be delegated to the worker thread
  92. * @param {Object} options The api function's options
  93. */
  94. function delegate(id, method, options) {
  95. if (typeof openpgp[method] !== 'function') {
  96. response({ id:id, event:'method-return', err:'Unknown Worker Event' });
  97. return;
  98. }
  99. // construct ReadableStreams from MessagePorts
  100. openpgp.util.restoreStreams(options);
  101. // parse cloned packets
  102. options = openpgp.packet.clone.parseClonedPackets(options, method);
  103. openpgp[method](options).then(function(data) {
  104. // clone packets (for web worker structured cloning algorithm)
  105. response({ id:id, event:'method-return', data:openpgp.packet.clone.clonePackets(data) });
  106. }).catch(function(e) {
  107. openpgp.util.print_debug_error(e);
  108. response({
  109. id:id, event:'method-return', err:e.message, stack:e.stack
  110. });
  111. });
  112. }
  113. /**
  114. * Respond to the main window.
  115. * @param {Object} event Contains event type and data
  116. */
  117. function response(event) {
  118. self.postMessage(event, openpgp.util.getTransferables(event.data, true));
  119. }
  120. /**
  121. * Let the main window know the worker has loaded.
  122. */
  123. postMessage({ event: 'loaded' });
  124. },{}]},{},[1]);