diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-02-07 11:23:23 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-02-07 11:23:23 +0100 |
commit | 9f10b2928df655c3672d9607e864e667d4bc903a (patch) | |
tree | 7743911b974b3a7fb0d4c7cec2a723942466b7f1 /helpers/requests.js | |
parent | d7c01e7793d813d804a3b5716d8288f9dcf71a16 (diff) | |
download | PeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.tar.gz PeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.tar.zst PeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.zip |
Remove useless anonymous functions of files
Diffstat (limited to 'helpers/requests.js')
-rw-r--r-- | helpers/requests.js | 178 |
1 files changed, 88 insertions, 90 deletions
diff --git a/helpers/requests.js b/helpers/requests.js index 0e301da79..e19afa5ca 100644 --- a/helpers/requests.js +++ b/helpers/requests.js | |||
@@ -1,111 +1,109 @@ | |||
1 | ;(function () { | 1 | 'use strict' |
2 | 'use strict' | ||
3 | 2 | ||
4 | var async = require('async') | 3 | var async = require('async') |
5 | var config = require('config') | 4 | var config = require('config') |
6 | var request = require('request') | 5 | var request = require('request') |
7 | var replay = require('request-replay') | 6 | var replay = require('request-replay') |
8 | 7 | ||
9 | var constants = require('../initializers/constants') | 8 | var constants = require('../initializers/constants') |
10 | var logger = require('./logger') | 9 | var logger = require('./logger') |
11 | var peertubeCrypto = require('./peertubeCrypto') | 10 | var peertubeCrypto = require('./peertubeCrypto') |
12 | 11 | ||
13 | var http = config.get('webserver.https') ? 'https' : 'http' | 12 | var http = config.get('webserver.https') ? 'https' : 'http' |
14 | var host = config.get('webserver.host') | 13 | var host = config.get('webserver.host') |
15 | var port = config.get('webserver.port') | 14 | var port = config.get('webserver.port') |
16 | 15 | ||
17 | var requests = { | 16 | var requests = { |
18 | makeMultipleRetryRequest: makeMultipleRetryRequest | 17 | makeMultipleRetryRequest: makeMultipleRetryRequest |
19 | } | 18 | } |
20 | 19 | ||
21 | function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { | 20 | function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { |
22 | if (!callback) { | 21 | if (!callback) { |
23 | callback = callbackEach | 22 | callback = callbackEach |
24 | callbackEach = null | 23 | callbackEach = null |
25 | } | 24 | } |
26 | 25 | ||
27 | var url = http + '://' + host + ':' + port | 26 | var url = http + '://' + host + ':' + port |
28 | var signature | 27 | var signature |
29 | 28 | ||
30 | // Add signature if it is specified in the params | 29 | // Add signature if it is specified in the params |
31 | if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { | 30 | if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { |
32 | signature = peertubeCrypto.sign(url) | 31 | signature = peertubeCrypto.sign(url) |
33 | } | 32 | } |
34 | 33 | ||
35 | // Make a request for each pod | 34 | // Make a request for each pod |
36 | async.each(pods, function (pod, callback_each_async) { | 35 | async.each(pods, function (pod, callback_each_async) { |
37 | function callbackEachRetryRequest (err, response, body, url, pod) { | 36 | function callbackEachRetryRequest (err, response, body, url, pod) { |
38 | if (callbackEach !== null) { | 37 | if (callbackEach !== null) { |
39 | callbackEach(err, response, body, url, pod, function () { | 38 | callbackEach(err, response, body, url, pod, function () { |
40 | callback_each_async() | ||
41 | }) | ||
42 | } else { | ||
43 | callback_each_async() | 39 | callback_each_async() |
44 | } | 40 | }) |
41 | } else { | ||
42 | callback_each_async() | ||
45 | } | 43 | } |
44 | } | ||
46 | 45 | ||
47 | var params = { | 46 | var params = { |
48 | url: pod.url + all_data.path, | 47 | url: pod.url + all_data.path, |
49 | method: all_data.method | 48 | method: all_data.method |
50 | } | 49 | } |
51 | 50 | ||
52 | // Add data with POST requst ? | 51 | // Add data with POST requst ? |
53 | if (all_data.method === 'POST' && all_data.data) { | 52 | if (all_data.method === 'POST' && all_data.data) { |
54 | // Encrypt data ? | 53 | // Encrypt data ? |
55 | if (all_data.encrypt === true) { | 54 | if (all_data.encrypt === true) { |
56 | // TODO: ES6 with let | 55 | // TODO: ES6 with let |
57 | ;(function (copy_params, copy_url, copy_pod, copy_signature) { | 56 | ;(function (copy_params, copy_url, copy_pod, copy_signature) { |
58 | peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) { | 57 | peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) { |
59 | if (err) return callback(err) | 58 | if (err) return callback(err) |
60 | 59 | ||
61 | copy_params.json = { | 60 | copy_params.json = { |
62 | data: encrypted.data, | 61 | data: encrypted.data, |
63 | key: encrypted.key | 62 | key: encrypted.key |
64 | } | 63 | } |
65 | 64 | ||
66 | makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest) | 65 | makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest) |
67 | }) | 66 | }) |
68 | })(params, url, pod, signature) | 67 | })(params, url, pod, signature) |
69 | } else { | ||
70 | params.json = { data: all_data.data } | ||
71 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) | ||
72 | } | ||
73 | } else { | 68 | } else { |
69 | params.json = { data: all_data.data } | ||
74 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) | 70 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) |
75 | } | 71 | } |
76 | }, callback) | 72 | } else { |
77 | } | 73 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) |
74 | } | ||
75 | }, callback) | ||
76 | } | ||
78 | 77 | ||
79 | // --------------------------------------------------------------------------- | 78 | // --------------------------------------------------------------------------- |
80 | 79 | ||
81 | module.exports = requests | 80 | module.exports = requests |
82 | 81 | ||
83 | // --------------------------------------------------------------------------- | 82 | // --------------------------------------------------------------------------- |
84 | 83 | ||
85 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { | 84 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { |
86 | // Append the signature | 85 | // Append the signature |
87 | if (signature) { | 86 | if (signature) { |
88 | params.json.signature = { | 87 | params.json.signature = { |
89 | url: from_url, | 88 | url: from_url, |
90 | signature: signature | 89 | signature: signature |
91 | } | ||
92 | } | 90 | } |
93 | |||
94 | logger.debug('Make retry requests to %s.', to_pod.url) | ||
95 | |||
96 | replay( | ||
97 | request.post(params, function (err, response, body) { | ||
98 | callbackEach(err, response, body, params.url, to_pod) | ||
99 | }), | ||
100 | { | ||
101 | retries: constants.REQUEST_RETRIES, | ||
102 | factor: 3, | ||
103 | maxTimeout: Infinity, | ||
104 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | ||
105 | } | ||
106 | ).on('replay', function (replay) { | ||
107 | logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.', | ||
108 | params.url, replay.error.code, replay.error.message, replay.number, replay.delay) | ||
109 | }) | ||
110 | } | 91 | } |
111 | })() | 92 | |
93 | logger.debug('Make retry requests to %s.', to_pod.url) | ||
94 | |||
95 | replay( | ||
96 | request.post(params, function (err, response, body) { | ||
97 | callbackEach(err, response, body, params.url, to_pod) | ||
98 | }), | ||
99 | { | ||
100 | retries: constants.REQUEST_RETRIES, | ||
101 | factor: 3, | ||
102 | maxTimeout: Infinity, | ||
103 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | ||
104 | } | ||
105 | ).on('replay', function (replay) { | ||
106 | logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.', | ||
107 | params.url, replay.error.code, replay.error.message, replay.number, replay.delay) | ||
108 | }) | ||
109 | } | ||