]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/requests.js
Better tests for a better world
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
... / ...
CommitLineData
1'use strict'
2
3const async = require('async')
4const config = require('config')
5const request = require('request')
6const replay = require('request-replay')
7
8const constants = require('../initializers/constants')
9const logger = require('./logger')
10const peertubeCrypto = require('./peertubeCrypto')
11
12const http = config.get('webserver.https') ? 'https' : 'http'
13const host = config.get('webserver.host')
14const port = config.get('webserver.port')
15
16const requests = {
17 makeMultipleRetryRequest: makeMultipleRetryRequest
18}
19
20function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
21 if (!callback) {
22 callback = callbackEach
23 callbackEach = null
24 }
25
26 const url = http + '://' + host + ':' + port
27 let signature
28
29 // Add signature if it is specified in the params
30 if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
31 signature = peertubeCrypto.sign(url)
32 }
33
34 // Make a request for each pod
35 async.each(pods, function (pod, callback_each_async) {
36 function callbackEachRetryRequest (err, response, body, url, pod) {
37 if (callbackEach !== null) {
38 callbackEach(err, response, body, url, pod, function () {
39 callback_each_async()
40 })
41 } else {
42 callback_each_async()
43 }
44 }
45
46 const params = {
47 url: pod.url + all_data.path,
48 method: all_data.method
49 }
50
51 // Add data with POST requst ?
52 if (all_data.method === 'POST' && all_data.data) {
53 // Encrypt data ?
54 if (all_data.encrypt === true) {
55 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
56 if (err) return callback(err)
57
58 params.json = {
59 data: encrypted.data,
60 key: encrypted.key
61 }
62
63 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
64 })
65 } else {
66 params.json = { data: all_data.data }
67 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
68 }
69 } else {
70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
71 }
72 }, callback)
73}
74
75// ---------------------------------------------------------------------------
76
77module.exports = requests
78
79// ---------------------------------------------------------------------------
80
81function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
82 // Append the signature
83 if (signature) {
84 params.json.signature = {
85 url: from_url,
86 signature: signature
87 }
88 }
89
90 logger.debug('Make retry requests to %s.', to_pod.url)
91
92 replay(
93 request.post(params, function (err, response, body) {
94 callbackEach(err, response, body, params.url, to_pod)
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}