PeerTube/server/helpers/requests.js

67 lines
1.6 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 replay = require('request-replay')
const request = require('request')
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const constants = require('../initializers/constants')
2016-07-01 09:22:36 -05:00
const peertubeCrypto = require('./peertube-crypto')
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const requests = {
makeRetryRequest,
makeSecureRequest
}
2016-02-05 11:03:20 -06:00
function makeRetryRequest (params, callback) {
replay(
request(params, callback),
{
retries: constants.RETRY_REQUESTS,
factor: 3,
maxTimeout: Infinity,
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
}
)
}
2016-02-05 11:03:20 -06:00
function makeSecureRequest (params, callback) {
const requestParams = {
url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
}
2016-02-05 11:03:20 -06:00
if (params.method !== 'POST') {
return callback(new Error('Cannot make a secure request with a non POST method.'))
}
requestParams.json = {}
2016-02-05 11:03:20 -06:00
// Add signature if it is specified in the params
if (params.sign === true) {
const host = constants.CONFIG.WEBSERVER.HOST
let dataToSign
if (params.data) {
dataToSign = dataToSign = params.data
} else {
// We do not have data to sign so we just take our host
// It is not ideal but the connection should be in HTTPS
dataToSign = host
}
requestParams.json.signature = {
host, // Which host we pretend to be
signature: peertubeCrypto.sign(dataToSign)
}
}
// If there are data informations
if (params.data) {
requestParams.json.data = params.data
}
request.post(requestParams, callback)
}
2016-02-05 11:03:20 -06:00
// ---------------------------------------------------------------------------
2016-02-05 11:03:20 -06:00
module.exports = requests