]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/requests.js
Update to Angular RC 1
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
CommitLineData
9f10b292 1'use strict'
dac0a531 2
f0f5567b
C
3const async = require('async')
4const config = require('config')
5const request = require('request')
6const replay = require('request-replay')
dac0a531 7
f0f5567b
C
8const constants = require('../initializers/constants')
9const logger = require('./logger')
10const peertubeCrypto = require('./peertubeCrypto')
dac0a531 11
f0f5567b
C
12const http = config.get('webserver.https') ? 'https' : 'http'
13const host = config.get('webserver.host')
14const port = config.get('webserver.port')
dac0a531 15
f0f5567b 16const requests = {
9f10b292
C
17 makeMultipleRetryRequest: makeMultipleRetryRequest
18}
dac0a531 19
bc503c2a 20function makeMultipleRetryRequest (allData, pods, callbackEach, callback) {
9f10b292
C
21 if (!callback) {
22 callback = callbackEach
23 callbackEach = null
24 }
dac0a531 25
f0f5567b
C
26 const url = http + '://' + host + ':' + port
27 let signature
dac0a531 28
9f10b292 29 // Add signature if it is specified in the params
bc503c2a 30 if (allData.method === 'POST' && allData.data && allData.sign === true) {
9f10b292
C
31 signature = peertubeCrypto.sign(url)
32 }
dac0a531 33
9f10b292 34 // Make a request for each pod
bc503c2a 35 async.each(pods, function (pod, callbackEachAsync) {
9f10b292
C
36 function callbackEachRetryRequest (err, response, body, url, pod) {
37 if (callbackEach !== null) {
38 callbackEach(err, response, body, url, pod, function () {
bc503c2a 39 callbackEachAsync()
9f10b292
C
40 })
41 } else {
bc503c2a 42 callbackEachAsync()
dac0a531 43 }
9f10b292 44 }
dac0a531 45
f0f5567b 46 const params = {
bc503c2a
C
47 url: pod.url + allData.path,
48 method: allData.method
9f10b292 49 }
dac0a531 50
9f10b292 51 // Add data with POST requst ?
bc503c2a 52 if (allData.method === 'POST' && allData.data) {
9f10b292 53 // Encrypt data ?
bc503c2a
C
54 if (allData.encrypt === true) {
55 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) {
f0f5567b 56 if (err) return callback(err)
9f10b292 57
f0f5567b
C
58 params.json = {
59 data: encrypted.data,
60 key: encrypted.key
61 }
9f10b292 62
f0f5567b
C
63 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
64 })
dac0a531 65 } else {
bc503c2a 66 params.json = { data: allData.data }
dac0a531
C
67 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
68 }
9f10b292
C
69 } else {
70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
71 }
72 }, callback)
73}
dac0a531 74
9f10b292 75// ---------------------------------------------------------------------------
dac0a531 76
9f10b292 77module.exports = requests
dac0a531 78
9f10b292 79// ---------------------------------------------------------------------------
dac0a531 80
bc503c2a 81function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) {
9f10b292
C
82 // Append the signature
83 if (signature) {
84 params.json.signature = {
bc503c2a 85 url: fromUrl,
9f10b292 86 signature: signature
dac0a531 87 }
dac0a531 88 }
9f10b292 89
bc503c2a 90 logger.debug('Make retry requests to %s.', toPod.url)
9f10b292
C
91
92 replay(
93 request.post(params, function (err, response, body) {
bc503c2a 94 callbackEach(err, response, body, params.url, toPod)
9f10b292
C
95 }),
96 {
97 retries: constants.REQUEST_RETRIES,
98 factor: 3,
99 maxTimeout: Infinity,
100 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
101 }
102 ).on('replay', function (replay) {
103 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
104 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
105 })
106}