UNPKG

1.45 kBJavaScriptView Raw
1import window from 'global/window';
2import {Decrypter} from 'aes-decrypter';
3import { createTransferableMessage } from './bin-utils';
4
5/**
6 * Our web worker interface so that things can talk to aes-decrypter
7 * that will be running in a web worker. the scope is passed to this by
8 * webworkify.
9 *
10 * @param {Object} self
11 * the scope for the web worker
12 */
13const DecrypterWorker = function(self) {
14 self.onmessage = function(event) {
15 const data = event.data;
16 const encrypted = new Uint8Array(data.encrypted.bytes,
17 data.encrypted.byteOffset,
18 data.encrypted.byteLength);
19 const key = new Uint32Array(data.key.bytes,
20 data.key.byteOffset,
21 data.key.byteLength / 4);
22 const iv = new Uint32Array(data.iv.bytes,
23 data.iv.byteOffset,
24 data.iv.byteLength / 4);
25
26 /* eslint-disable no-new, handle-callback-err */
27 new Decrypter(encrypted,
28 key,
29 iv,
30 function(err, bytes) {
31 window.postMessage(createTransferableMessage({
32 source: data.source,
33 decrypted: bytes
34 }), [bytes.buffer]);
35 });
36 /* eslint-enable */
37 };
38};
39
40export default (self) => {
41 return new DecrypterWorker(self);
42};