]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/requests.js
Server: put config in constants
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
CommitLineData
9f10b292 1'use strict'
dac0a531 2
f0f5567b 3const replay = require('request-replay')
528a9efa 4const request = require('request')
dac0a531 5
f0f5567b 6const constants = require('../initializers/constants')
5f698b82 7const peertubeCrypto = require('./peertube-crypto')
dac0a531 8
f0f5567b 9const requests = {
528a9efa
C
10 makeRetryRequest: makeRetryRequest,
11 makeSecureRequest: makeSecureRequest
9f10b292 12}
dac0a531 13
528a9efa
C
14function makeRetryRequest (params, callback) {
15 replay(
16 request(params, callback),
17 {
18 retries: constants.RETRY_REQUESTS,
19 factor: 3,
20 maxTimeout: Infinity,
21 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
22 }
23 )
24}
dac0a531 25
528a9efa 26function makeSecureRequest (params, callback) {
528a9efa
C
27 const requestParams = {
28 url: params.toPod.url + params.path
9f10b292 29 }
dac0a531 30
528a9efa
C
31 // Add data with POST requst ?
32 if (params.method === 'POST') {
33 requestParams.json = {}
dac0a531 34
528a9efa
C
35 // Add signature if it is specified in the params
36 if (params.sign === true) {
37 requestParams.json.signature = {
e861452f
C
38 url: constants.CONFIG.WEBSERVER.URL,
39 signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL)
528a9efa 40 }
9f10b292 41 }
dac0a531 42
528a9efa
C
43 // If there are data informations
44 if (params.data) {
45 // Encrypt data
46 if (params.encrypt === true) {
47 peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) {
f0f5567b 48 if (err) return callback(err)
9f10b292 49
528a9efa
C
50 requestParams.json.data = encrypted.data
51 requestParams.json.key = encrypted.key
9f10b292 52
528a9efa 53 request.post(requestParams, callback)
f0f5567b 54 })
dac0a531 55 } else {
528a9efa
C
56 // No encryption
57 requestParams.json.data = params.data
58 request.post(requestParams, callback)
dac0a531 59 }
9f10b292 60 } else {
528a9efa
C
61 // No data
62 request.post(requestParams, callback)
9f10b292 63 }
528a9efa
C
64 } else {
65 request.get(requestParams, callback)
66 }
9f10b292 67}
dac0a531 68
9f10b292 69// ---------------------------------------------------------------------------
dac0a531 70
9f10b292 71module.exports = requests