]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/requests.ts
Type models
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.ts
CommitLineData
65fcc311
C
1import replay = require('request-replay')
2import request = require('request')
dac0a531 3
65fcc311
C
4import {
5 RETRY_REQUESTS,
6 REMOTE_SCHEME,
7 CONFIG
8} from '../initializers'
9import { sign } from './peertube-crypto'
dac0a531 10
528a9efa
C
11function makeRetryRequest (params, callback) {
12 replay(
13 request(params, callback),
14 {
65fcc311 15 retries: RETRY_REQUESTS,
528a9efa
C
16 factor: 3,
17 maxTimeout: Infinity,
18 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
19 }
20 )
21}
dac0a531 22
528a9efa 23function makeSecureRequest (params, callback) {
528a9efa 24 const requestParams = {
65fcc311
C
25 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
26 json: {}
9f10b292 27 }
dac0a531 28
bdfbd4f1
C
29 if (params.method !== 'POST') {
30 return callback(new Error('Cannot make a secure request with a non POST method.'))
31 }
32
bdfbd4f1
C
33 // Add signature if it is specified in the params
34 if (params.sign === true) {
65fcc311 35 const host = CONFIG.WEBSERVER.HOST
bdfbd4f1
C
36
37 let dataToSign
528a9efa 38 if (params.data) {
5a976a8c 39 dataToSign = params.data
9f10b292 40 } else {
bdfbd4f1
C
41 // We do not have data to sign so we just take our host
42 // It is not ideal but the connection should be in HTTPS
43 dataToSign = host
9f10b292 44 }
bdfbd4f1 45
65fcc311 46 requestParams.json['signature'] = {
bdfbd4f1 47 host, // Which host we pretend to be
65fcc311 48 signature: sign(dataToSign)
bdfbd4f1
C
49 }
50 }
51
52 // If there are data informations
53 if (params.data) {
65fcc311 54 requestParams.json['data'] = params.data
528a9efa 55 }
bdfbd4f1
C
56
57 request.post(requestParams, callback)
9f10b292 58}
dac0a531 59
9f10b292 60// ---------------------------------------------------------------------------
dac0a531 61
65fcc311
C
62export {
63 makeRetryRequest,
64 makeSecureRequest
65}