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