Mercurial > sat_docs
comparison scripts/minifier/otr/sm-webworker.js @ 12:1596660ddf72
Add minifier script for otr.js and its dependencies
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 03 Sep 2014 19:38:05 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:4920c8da790b | 12:1596660ddf72 |
---|---|
1 ;(function (root) { | |
2 "use strict"; | |
3 | |
4 root.OTR = {} | |
5 root.crypto = { | |
6 randomBytes: function () { | |
7 throw new Error("Haven't seeded yet.") | |
8 } | |
9 } | |
10 | |
11 // default imports | |
12 var imports = [ | |
13 'vendor/salsa20.js' | |
14 , 'vendor/bigint.js' | |
15 , 'vendor/crypto.js' | |
16 , 'vendor/eventemitter.js' | |
17 , 'lib/const.js' | |
18 , 'lib/helpers.js' | |
19 , 'lib/sm.js' | |
20 ] | |
21 | |
22 function wrapPostMessage(method) { | |
23 return function () { | |
24 postMessage({ | |
25 method: method | |
26 , args: Array.prototype.slice.call(arguments, 0) | |
27 }) | |
28 } | |
29 } | |
30 | |
31 var sm | |
32 onmessage = function (e) { | |
33 var data = e.data | |
34 switch (data.type) { | |
35 case 'seed': | |
36 if (data.imports) imports = data.imports | |
37 importScripts.apply(root, imports) | |
38 | |
39 // use salsa20 since there's no prng in webworkers | |
40 var state = new root.Salsa20( | |
41 data.seed.slice(0, 32), | |
42 data.seed.slice(32) | |
43 ) | |
44 root.crypto.randomBytes = function (n) { | |
45 return state.getBytes(n) | |
46 } | |
47 break | |
48 case 'init': | |
49 sm = new root.OTR.SM(data.reqs) | |
50 ;['trust','question', 'send', 'abort'].forEach(function (m) { | |
51 sm.on(m, wrapPostMessage(m)); | |
52 }) | |
53 break | |
54 case 'method': | |
55 sm[data.method].apply(sm, data.args) | |
56 break | |
57 } | |
58 } | |
59 | |
60 }(this)) |