PeerTube/server/helpers/requests.js

107 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict'
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const async = require('async')
const config = require('config')
const request = require('request')
const replay = require('request-replay')
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const constants = require('../initializers/constants')
const logger = require('./logger')
const peertubeCrypto = require('./peertubeCrypto')
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const http = config.get('webserver.https') ? 'https' : 'http'
const host = config.get('webserver.host')
const port = config.get('webserver.port')
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const requests = {
makeMultipleRetryRequest: makeMultipleRetryRequest
}
2016-02-05 11:03:20 -06:00
function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
if (!callback) {
callback = callbackEach
callbackEach = null
}
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const url = http + '://' + host + ':' + port
let signature
2016-02-05 11:03:20 -06:00
// Add signature if it is specified in the params
if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
signature = peertubeCrypto.sign(url)
}
2016-02-05 11:03:20 -06:00
// Make a request for each pod
async.each(pods, function (pod, callback_each_async) {
function callbackEachRetryRequest (err, response, body, url, pod) {
if (callbackEach !== null) {
callbackEach(err, response, body, url, pod, function () {
2016-02-05 11:03:20 -06:00
callback_each_async()
})
} else {
callback_each_async()
2016-02-05 11:03:20 -06:00
}
}
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const params = {
url: pod.url + all_data.path,
method: all_data.method
}
2016-02-05 11:03:20 -06:00
// Add data with POST requst ?
if (all_data.method === 'POST' && all_data.data) {
// Encrypt data ?
if (all_data.encrypt === true) {
2016-03-16 16:29:27 -05:00
peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
if (err) return callback(err)
2016-03-16 16:29:27 -05:00
params.json = {
data: encrypted.data,
key: encrypted.key
}
2016-03-16 16:29:27 -05:00
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
})
2016-02-05 11:03:20 -06:00
} else {
params.json = { data: all_data.data }
2016-02-05 11:03:20 -06:00
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
}
} else {
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
}
}, callback)
}
2016-02-05 11:03:20 -06:00
// ---------------------------------------------------------------------------
2016-02-05 11:03:20 -06:00
module.exports = requests
2016-02-05 11:03:20 -06:00
// ---------------------------------------------------------------------------
2016-02-05 11:03:20 -06:00
function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
// Append the signature
if (signature) {
params.json.signature = {
url: from_url,
signature: signature
2016-02-05 11:03:20 -06:00
}
}
logger.debug('Make retry requests to %s.', to_pod.url)
replay(
request.post(params, function (err, response, body) {
callbackEach(err, response, body, params.url, to_pod)
}),
{
retries: constants.REQUEST_RETRIES,
factor: 3,
maxTimeout: Infinity,
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
}
).on('replay', function (replay) {
logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
})
}