1 import * as replay from 'request-replay'
2 import * as request from 'request'
3 import * as Promise from 'bluebird'
9 } from '../initializers'
10 import { PodInstance } from '../models'
11 import { PodSignature } from '../../shared'
12 import { signObject } from './peertube-crypto'
14 function doRequest (requestOptions: request.CoreOptions & request.UriOptions) {
15 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
16 request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
20 type MakeRetryRequestParams = {
22 method: 'GET' | 'POST',
25 function makeRetryRequest (params: MakeRetryRequestParams) {
26 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
28 request(params, (err, response, body) => err ? rej(err) : res({ response, body })),
30 retries: RETRY_REQUESTS,
33 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
39 type MakeSecureRequestParams = {
44 function makeSecureRequest (params: MakeSecureRequestParams) {
45 const requestParams: {
49 signature: PodSignature,
54 uri: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
61 const host = CONFIG.WEBSERVER.HOST
65 dataToSign = params.data
67 // We do not have data to sign so we just take our host
68 // It is not ideal but the connection should be in HTTPS
72 sign(dataToSign).then(signature => {
73 requestParams.json.signature = {
74 host, // Which host we pretend to be
78 // If there are data information
80 requestParams.json.data = params.data
83 return doRequest(requestParams)
87 // ---------------------------------------------------------------------------