]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/requests.js
Update to Angular RC 1
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
index e19afa5ca305b5789daa3b23980cc9440459e2bf..1e1bb4111de742473aeab66e205c1b54b7b146a3 100644 (file)
@@ -1,72 +1,69 @@
 'use strict'
 
-var async = require('async')
-var config = require('config')
-var request = require('request')
-var replay = require('request-replay')
+const async = require('async')
+const config = require('config')
+const request = require('request')
+const replay = require('request-replay')
 
-var constants = require('../initializers/constants')
-var logger = require('./logger')
-var peertubeCrypto = require('./peertubeCrypto')
+const constants = require('../initializers/constants')
+const logger = require('./logger')
+const peertubeCrypto = require('./peertubeCrypto')
 
-var http = config.get('webserver.https') ? 'https' : 'http'
-var host = config.get('webserver.host')
-var port = config.get('webserver.port')
+const http = config.get('webserver.https') ? 'https' : 'http'
+const host = config.get('webserver.host')
+const port = config.get('webserver.port')
 
-var requests = {
+const requests = {
   makeMultipleRetryRequest: makeMultipleRetryRequest
 }
 
-function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
+function makeMultipleRetryRequest (allData, pods, callbackEach, callback) {
   if (!callback) {
     callback = callbackEach
     callbackEach = null
   }
 
-  var url = http + '://' + host + ':' + port
-  var signature
+  const url = http + '://' + host + ':' + port
+  let signature
 
   // Add signature if it is specified in the params
-  if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
+  if (allData.method === 'POST' && allData.data && allData.sign === true) {
     signature = peertubeCrypto.sign(url)
   }
 
   // Make a request for each pod
-  async.each(pods, function (pod, callback_each_async) {
+  async.each(pods, function (pod, callbackEachAsync) {
     function callbackEachRetryRequest (err, response, body, url, pod) {
       if (callbackEach !== null) {
         callbackEach(err, response, body, url, pod, function () {
-          callback_each_async()
+          callbackEachAsync()
         })
       } else {
-        callback_each_async()
+        callbackEachAsync()
       }
     }
 
-    var params = {
-      url: pod.url + all_data.path,
-      method: all_data.method
+    const params = {
+      url: pod.url + allData.path,
+      method: allData.method
     }
 
     // Add data with POST requst ?
-    if (all_data.method === 'POST' && all_data.data) {
+    if (allData.method === 'POST' && allData.data) {
       // Encrypt data ?
-      if (all_data.encrypt === true) {
-        // TODO: ES6 with let
-        ;(function (copy_params, copy_url, copy_pod, copy_signature) {
-          peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
-            if (err) return callback(err)
-
-            copy_params.json = {
-              data: encrypted.data,
-              key: encrypted.key
-            }
-
-            makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
-          })
-        })(params, url, pod, signature)
+      if (allData.encrypt === true) {
+        peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) {
+          if (err) return callback(err)
+
+          params.json = {
+            data: encrypted.data,
+            key: encrypted.key
+          }
+
+          makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
+        })
       } else {
-        params.json = { data: all_data.data }
+        params.json = { data: allData.data }
         makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
       }
     } else {
@@ -81,20 +78,20 @@ module.exports = requests
 
 // ---------------------------------------------------------------------------
 
-function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
+function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) {
   // Append the signature
   if (signature) {
     params.json.signature = {
-      url: from_url,
+      url: fromUrl,
       signature: signature
     }
   }
 
-  logger.debug('Make retry requests to %s.', to_pod.url)
+  logger.debug('Make retry requests to %s.', toPod.url)
 
   replay(
     request.post(params, function (err, response, body) {
-      callbackEach(err, response, body, params.url, to_pod)
+      callbackEach(err, response, body, params.url, toPod)
     }),
     {
       retries: constants.REQUEST_RETRIES,