aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/requests.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-10 17:27:49 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit571389d43b8fc8aaf27e77c06f19b320b08dbbc9 (patch)
treee57173bcd0590d939c28952a29258fd02a281e35 /server/helpers/requests.ts
parent38fa2065831b5f55be0d7f30f19a62c967397208 (diff)
downloadPeerTube-571389d43b8fc8aaf27e77c06f19b320b08dbbc9.tar.gz
PeerTube-571389d43b8fc8aaf27e77c06f19b320b08dbbc9.tar.zst
PeerTube-571389d43b8fc8aaf27e77c06f19b320b08dbbc9.zip
Make it compile at least
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r--server/helpers/requests.ts83
1 files changed, 2 insertions, 81 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts
index 31cedd768..4b1deeadc 100644
--- a/server/helpers/requests.ts
+++ b/server/helpers/requests.ts
@@ -1,16 +1,6 @@
1import * as replay from 'request-replay'
2import * as request from 'request'
3import * as Promise from 'bluebird' 1import * as Promise from 'bluebird'
4
5import {
6 RETRY_REQUESTS,
7 REMOTE_SCHEME,
8 CONFIG
9} from '../initializers'
10import { PodInstance } from '../models'
11import { PodSignature } from '../../shared'
12import { signObject } from './peertube-crypto'
13import { createWriteStream } from 'fs' 2import { createWriteStream } from 'fs'
3import * as request from 'request'
14 4
15function doRequest (requestOptions: request.CoreOptions & request.UriOptions) { 5function doRequest (requestOptions: request.CoreOptions & request.UriOptions) {
16 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { 6 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
@@ -27,78 +17,9 @@ function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.U
27 }) 17 })
28} 18}
29 19
30type MakeRetryRequestParams = {
31 url: string,
32 method: 'GET' | 'POST',
33 json: Object
34}
35function makeRetryRequest (params: MakeRetryRequestParams) {
36 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
37 replay(
38 request(params, (err, response, body) => err ? rej(err) : res({ response, body })),
39 {
40 retries: RETRY_REQUESTS,
41 factor: 3,
42 maxTimeout: Infinity,
43 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
44 }
45 )
46 })
47}
48
49type MakeSecureRequestParams = {
50 toPod: PodInstance
51 path: string
52 data?: Object
53}
54function makeSecureRequest (params: MakeSecureRequestParams) {
55 const requestParams: {
56 method: 'POST',
57 uri: string,
58 json: {
59 signature: PodSignature,
60 data: any
61 }
62 } = {
63 method: 'POST',
64 uri: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
65 json: {
66 signature: null,
67 data: null
68 }
69 }
70
71 const host = CONFIG.WEBSERVER.HOST
72
73 let dataToSign
74 if (params.data) {
75 dataToSign = params.data
76 } else {
77 // We do not have data to sign so we just take our host
78 // It is not ideal but the connection should be in HTTPS
79 dataToSign = host
80 }
81
82 sign(dataToSign).then(signature => {
83 requestParams.json.signature = {
84 host, // Which host we pretend to be
85 signature
86 }
87
88 // If there are data information
89 if (params.data) {
90 requestParams.json.data = params.data
91 }
92
93 return doRequest(requestParams)
94 })
95}
96
97// --------------------------------------------------------------------------- 20// ---------------------------------------------------------------------------
98 21
99export { 22export {
100 doRequest, 23 doRequest,
101 doRequestAndSaveToFile, 24 doRequestAndSaveToFile
102 makeRetryRequest,
103 makeSecureRequest
104} 25}