// Alternative to crypto-browserify's randombytes shim
// https://github.com/crypto-browserify/randombytes
// Falls back to freedom.js core.crypto when crypto.getRandomValues is
// unavailable, which is currently the case in Firefox web workers.
// TODO: post FF48 (2016-08-02) remove fill and just use built-in crypto

const cryptoAvailable = typeof crypto !== 'undefined';
var getBufferedRandomBytes, refreshBuffer, offset;
if (!cryptoAvailable) {
  // Filling with freedom.js core.crypto, which requires a buffer due to async
  var rand;
  try {
    rand = freedom['core.crypto']();
  } catch (e) {
    console.error('could not instantiate core.crypto: ', e);
    return;
  }

  var buf;
  var refreshBuffer = function(size) {
    return rand.getRandomBytes(size).then(function (bytes) {
      buf = new Uint8Array(bytes);
      offset = 0;
    }, function (err) {
      console.log(err);
    });
  }.bind(this);
  refreshBuffer(10000);  // initial randomness
  offset = 0;

  getBufferedRandomBytes = function(buffer) {
    if ((size * 100) - offset < size * 10) {
      refreshBuffer(size*100);
    }
    var size = buffer.byteLength,
        view = new Uint8Array(buffer),
        i;
    if (offset + size > buf.length) {
      throw new Error('Insufficient Randomness Allocated.');
    }
    view = buf.slice(offset, offset + size);
    offset += size;
  };
}

module.exports = function(size, cb) {
  if (!cryptoAvailable && (size*100) - offset < size*10) {
    // using freedom.js core.crypto, may need to refresh buffer
    refreshBuffer(size*100);
  }
  if (cryptoAvailable) {
    const buffer = new Buffer(size);
    crypto.getRandomValues(buffer);
    if (cb) {
      cb(undefined, buffer);
    } else {
      return buffer;
    }
  } else {
    if (cb) {
      rand.getRandomBytes(size).then(function(ab) {
        cb(new Buffer(ab));
      });
    } else {
      const buffer = new Buffer(size);
      getBufferedRandomBytes(buffer);
      return buffer;
    }
  }
};

@@ -526,20 +526,8 @@
 ync%0A
-  var rand;%0A
   tr
@@ -530,24 +530,30 @@
   try %7B%0A    
+const 
 rand = freed
@@ -577,102 +577,11 @@
 ();%0A
+%0A
   
-%7D catch (e) %7B%0A    console.error('could not instantiate core.crypto: ', e);%0A    return;%0A  %7D%0A%0A
   va
@@ -587,16 +587,18 @@
 ar buf;%0A
+  
   var re
@@ -628,24 +628,26 @@
 size) %7B%0A    
+  
 return rand.
@@ -697,16 +697,18 @@
 %7B%0A      
+  
 buf = ne
@@ -730,24 +730,26 @@
 tes);%0A      
+  
 offset = 0;%0A
@@ -752,16 +752,18 @@
  0;%0A    
+ 
+ 
 %7D, funct
@@ -776,24 +776,26 @@
 rr) %7B%0A      
+  
 console.log(
@@ -804,20 +804,24 @@
 r);%0A    
+ 
+ 
 %7D);%0A
+  
   %7D.bind
@@ -830,16 +830,18 @@
 his);%0A  
+ 
+ 
 refreshB
@@ -877,16 +877,18 @@
 domness%0A
+  
   offset
@@ -896,16 +896,18 @@
 = 0;%0A%0A  
+ 
+ 
 getBuffe
@@ -938,24 +938,26 @@
 n(buffer) %7B%0A
+  
     if ((siz
@@ -991,24 +991,26 @@
 10) %7B%0A      
+  
 refreshBuffe
@@ -1022,16 +1022,18 @@
 e*100);%0A
+  
     %7D%0A  
@@ -1030,24 +1030,26 @@
       %7D%0A    
+  
 var size = b
@@ -1066,16 +1066,18 @@
 Length,%0A
+  
         
@@ -1119,11 +1119,15 @@
     
+  
 i;%0A
+  
     
@@ -1156,24 +1156,26 @@
 f.length) %7B%0A
+  
       throw 
@@ -1223,26 +1223,30 @@
 ted.');%0A    
+ 
+ 
 %7D%0A
+  
     view = b
@@ -1278,24 +1278,26 @@
  size);%0A    
+  
 offset += si
@@ -1302,18 +1302,101 @@
 size;%0A  
+  %7D;%0A  %7D catch (e) %7B%0A    console.error('could not instantiate core.crypto: ', e);%0A  
 %7D
-;
 %0A%7D%0A%0Amodu

remove weird return statement