]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/requests.js
Server: add createdAt from remote video in database
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
1 'use strict'
2
3 const replay = require('request-replay')
4 const request = require('request')
5
6 const constants = require('../initializers/constants')
7 const peertubeCrypto = require('./peertube-crypto')
8
9 const requests = {
10 makeRetryRequest,
11 makeSecureRequest
12 }
13
14 function 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 }
25
26 function makeSecureRequest (params, callback) {
27 const requestParams = {
28 url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
29 }
30
31 // Add data with POST requst ?
32 if (params.method === 'POST') {
33 requestParams.json = {}
34
35 // Add signature if it is specified in the params
36 if (params.sign === true) {
37 const host = constants.CONFIG.WEBSERVER.HOST
38
39 requestParams.json.signature = {
40 host,
41 signature: peertubeCrypto.sign(host)
42 }
43 }
44
45 // If there are data informations
46 if (params.data) {
47 requestParams.json.data = params.data
48 request.post(requestParams, callback)
49 } else {
50 // No data
51 request.post(requestParams, callback)
52 }
53 } else {
54 request.get(requestParams, callback)
55 }
56 }
57
58 // ---------------------------------------------------------------------------
59
60 module.exports = requests