]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - helpers/requests.js
4 var async
= require('async')
5 var config
= require('config')
6 var request
= require('request')
7 var replay
= require('request-replay')
9 var constants
= require('../initializers/constants')
10 var logger
= require('./logger')
11 var peertubeCrypto
= require('./peertubeCrypto')
13 var http
= config
.get('webserver.https') ? 'https' : 'http'
14 var host
= config
.get('webserver.host')
15 var port
= config
.get('webserver.port')
18 makeMultipleRetryRequest: makeMultipleRetryRequest
21 function makeMultipleRetryRequest (all_data
, pods
, callbackEach
, callback
) {
23 callback
= callbackEach
27 var url
= http
+ '://' + host
+ ':' + port
30 // Add signature if it is specified in the params
31 if (all_data
.method
=== 'POST' && all_data
.data
&& all_data
.sign
=== true) {
32 signature
= peertubeCrypto
.sign(url
)
35 // Make a request for each pod
36 async
.each(pods
, function (pod
, callback_each_async
) {
37 function callbackEachRetryRequest (err
, response
, body
, url
, pod
) {
38 if (callbackEach
!== null) {
39 callbackEach(err
, response
, body
, url
, pod
, function () {
48 url: pod
.url
+ all_data
.path
,
49 method: all_data
.method
52 // Add data with POST requst ?
53 if (all_data
.method
=== 'POST' && all_data
.data
) {
55 if (all_data
.encrypt
=== true) {
57 ;(function (copy_params
, copy_url
, copy_pod
, copy_signature
) {
58 peertubeCrypto
.encrypt(pod
.publicKey
, JSON
.stringify(all_data
.data
), function (err
, encrypted
) {
59 if (err
) return callback(err
)
66 makeRetryRequest(copy_params
, copy_url
, copy_pod
, copy_signature
, callbackEachRetryRequest
)
68 })(params
, url
, pod
, signature
)
70 params
.json
= { data: all_data
.data
}
71 makeRetryRequest(params
, url
, pod
, signature
, callbackEachRetryRequest
)
74 makeRetryRequest(params
, url
, pod
, signature
, callbackEachRetryRequest
)
79 // ---------------------------------------------------------------------------
81 module
.exports
= requests
83 // ---------------------------------------------------------------------------
85 function makeRetryRequest (params
, from_url
, to_pod
, signature
, callbackEach
) {
86 // Append the signature
88 params
.json
.signature
= {
94 logger
.debug('Make retry requests to %s.', to_pod
.url
)
97 request
.post(params
, function (err
, response
, body
) {
98 callbackEach(err
, response
, body
, params
.url
, to_pod
)
101 retries: constants
.REQUEST_RETRIES
,
103 maxTimeout: Infinity
,
104 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
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
)