PeerTube/server/helpers/requests.js

72 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict'
2016-02-05 18:03:20 +01:00
2016-03-16 22:29:27 +01:00
const replay = require('request-replay')
const request = require('request')
2016-02-05 18:03:20 +01:00
2016-03-16 22:29:27 +01:00
const constants = require('../initializers/constants')
2016-07-01 16:22:36 +02:00
const peertubeCrypto = require('./peertube-crypto')
2016-02-05 18:03:20 +01:00
2016-03-16 22:29:27 +01:00
const requests = {
makeRetryRequest: makeRetryRequest,
makeSecureRequest: makeSecureRequest
}
2016-02-05 18:03:20 +01: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 18:03:20 +01:00
function makeSecureRequest (params, callback) {
const requestParams = {
url: params.toPod.url + params.path
}
2016-02-05 18:03:20 +01:00
// Add data with POST requst ?
if (params.method === 'POST') {
requestParams.json = {}
2016-02-05 18:03:20 +01:00
// Add signature if it is specified in the params
if (params.sign === true) {
requestParams.json.signature = {
2016-08-19 21:34:51 +02:00
url: constants.CONFIG.WEBSERVER.URL,
signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL)
}
}
2016-02-05 18:03:20 +01:00
// If there are data informations
if (params.data) {
// Encrypt data
if (params.encrypt === true) {
peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) {
2016-03-16 22:29:27 +01:00
if (err) return callback(err)
requestParams.json.data = encrypted.data
requestParams.json.key = encrypted.key
request.post(requestParams, callback)
2016-03-16 22:29:27 +01:00
})
2016-02-05 18:03:20 +01:00
} else {
// No encryption
requestParams.json.data = params.data
request.post(requestParams, callback)
2016-02-05 18:03:20 +01:00
}
} else {
// No data
request.post(requestParams, callback)
}
} else {
request.get(requestParams, callback)
}
}
2016-02-05 18:03:20 +01:00
// ---------------------------------------------------------------------------
2016-02-05 18:03:20 +01:00
module.exports = requests