// node modules
var path = require('path');
var fs = require('fs');

// npm modules
var uuid = require('node-uuid');

function fetchScript(context, script, next) {
  var url = require('url');
  var res = url.parse(String(script));
  if (res.protocol === 'http:' ||
      res.protocol === 'https:') {
    context.emit('data', 'silly', 'fetch-script-start', String(script));
    var dest = path.resolve(context.path, uuid.v4());
    var request = require('request');

    var hmac, check;

    var req = request(res.href).
      on('error', function(err) {
        next(err);
      }).
      on('end', function() {
        if (hmac) {
          var res = hmac.digest('base64');
          context.emit('data', 'silly', 'fetch-script-hmac', 'Calculated HMAC: ' + res);
          if (res != check) {
            next(Error('Error: Invalid HMAC!'));
            return;
          }
        }
        context.emit('data', 'silly', 'fetch-script-end', dest);
        next(null,dest);
      });

    if (context.options.hmac) {
      req.on('response', function(response) {
        check = response.headers['content-hmac'];
        if (check) {
          var parts = check.split(' ',2);
          hmac = require('crypto').createHmac(parts[0], context.options.hmac);
          check = parts[1];
          context.emit('data', 'silly', 'fetch-script-hmac', 'Content-HMAC: ' + check);
          context.emit('data', 'silly', 'fetch-script-hmac', 'Algorithm: ' + parts[0]);
        } else {
          context.emit('data', 'warn', 'fetch-script-warn', 'No Content-HMAC Provided');
        }
      });
      req.on('data', function(chunk) {
        if (hmac) hmac.update(chunk);
      });
    }

    req.pipe(fs.createWriteStream(dest)); // todo add verification
  } else {
    next(null,path.resolve(process.cwd(),String(script)));
  }
}

module.exports = fetchScript;

@@ -60,16 +60,42 @@
 e('fs');
+%0Avar url = require('url');
 %0A%0A// npm
@@ -135,16 +135,50 @@
 -uuid');
+%0Avar request = require('request');
 %0A%0Afuncti
@@ -221,36 +221,8 @@
 ) %7B%0A
-  var url = require('url');%0A
   va
@@ -455,46 +455,8 @@
 ());
-%0A    var request = require('request');
 %0A%0A  
@@ -506,24 +506,31 @@
 es.href)
-.
+;
 %0A    
+%0A 
   
+ req.
 on('erro
@@ -551,26 +551,24 @@
 rr) %7B%0A      
-  
 next(err);%0A 
@@ -574,20 +574,21 @@
     
-  %7D).
+%7D);%0A
 %0A    
-  
+req.
 on('
@@ -604,34 +604,32 @@
 ction() %7B%0A      
-  
 if (hmac) %7B%0A    
@@ -628,26 +628,24 @@
 ) %7B%0A        
- 
- 
 var res = hm
@@ -661,26 +661,24 @@
 ('base64');%0A
-  
         cont
@@ -752,18 +752,16 @@
 + res);%0A
-  
         
@@ -786,26 +786,24 @@
 %7B%0A          
-  
 next(Error('
@@ -837,18 +837,16 @@
         
-  
 return;%0A
@@ -857,22 +857,18 @@
     
- 
- 
 %7D%0A
-  
       %7D%0A
     
@@ -859,26 +859,24 @@
   %7D%0A      %7D%0A
-  
       contex
@@ -928,26 +928,24 @@
 est);%0A      
-  
 next(null,de
@@ -942,25 +942,24 @@
 xt(null,
+ 
 dest);%0A
-  
     %7D);%0A
@@ -1745,16 +1745,17 @@
 xt(null,
+ 
 path.res
@@ -1773,16 +1773,17 @@
 s.cwd(),
+ 
 String(s

move requires out of function