aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/requests.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-07-05 13:26:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-07-05 14:14:16 +0200
commit6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch)
tree3365a96d82bc7f00ae504a568725c8e914150cf8 /server/helpers/requests.ts
parent5fe7e898316e18369c3e1aba307b55077adc7bfb (diff)
downloadPeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r--server/helpers/requests.ts81
1 files changed, 43 insertions, 38 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts
index b40fc8e39..b31074373 100644
--- a/server/helpers/requests.ts
+++ b/server/helpers/requests.ts
@@ -1,5 +1,6 @@
1import * as replay from 'request-replay' 1import * as replay from 'request-replay'
2import * as request from 'request' 2import * as request from 'request'
3import * as Promise from 'bluebird'
3 4
4import { 5import {
5 RETRY_REQUESTS, 6 RETRY_REQUESTS,
@@ -14,16 +15,18 @@ type MakeRetryRequestParams = {
14 method: 'GET'|'POST', 15 method: 'GET'|'POST',
15 json: Object 16 json: Object
16} 17}
17function makeRetryRequest (params: MakeRetryRequestParams, callback: request.RequestCallback) { 18function makeRetryRequest (params: MakeRetryRequestParams) {
18 replay( 19 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
19 request(params, callback), 20 replay(
20 { 21 request(params, (err, response, body) => err ? rej(err) : res({ response, body })),
21 retries: RETRY_REQUESTS, 22 {
22 factor: 3, 23 retries: RETRY_REQUESTS,
23 maxTimeout: Infinity, 24 factor: 3,
24 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] 25 maxTimeout: Infinity,
25 } 26 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
26 ) 27 }
28 )
29 })
27} 30}
28 31
29type MakeSecureRequestParams = { 32type MakeSecureRequestParams = {
@@ -33,41 +36,43 @@ type MakeSecureRequestParams = {
33 sign: boolean 36 sign: boolean
34 data?: Object 37 data?: Object
35} 38}
36function makeSecureRequest (params: MakeSecureRequestParams, callback: request.RequestCallback) { 39function makeSecureRequest (params: MakeSecureRequestParams) {
37 const requestParams = { 40 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
38 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, 41 const requestParams = {
39 json: {} 42 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
40 } 43 json: {}
44 }
41 45
42 if (params.method !== 'POST') { 46 if (params.method !== 'POST') {
43 return callback(new Error('Cannot make a secure request with a non POST method.'), null, null) 47 return rej(new Error('Cannot make a secure request with a non POST method.'))
44 } 48 }
45 49
46 // Add signature if it is specified in the params 50 // Add signature if it is specified in the params
47 if (params.sign === true) { 51 if (params.sign === true) {
48 const host = CONFIG.WEBSERVER.HOST 52 const host = CONFIG.WEBSERVER.HOST
49 53
50 let dataToSign 54 let dataToSign
51 if (params.data) { 55 if (params.data) {
52 dataToSign = params.data 56 dataToSign = params.data
53 } else { 57 } else {
54 // We do not have data to sign so we just take our host 58 // 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 59 // It is not ideal but the connection should be in HTTPS
56 dataToSign = host 60 dataToSign = host
57 } 61 }
58 62
59 requestParams.json['signature'] = { 63 requestParams.json['signature'] = {
60 host, // Which host we pretend to be 64 host, // Which host we pretend to be
61 signature: sign(dataToSign) 65 signature: sign(dataToSign)
66 }
62 } 67 }
63 }
64 68
65 // If there are data informations 69 // If there are data informations
66 if (params.data) { 70 if (params.data) {
67 requestParams.json['data'] = params.data 71 requestParams.json['data'] = params.data
68 } 72 }
69 73
70 request.post(requestParams, callback) 74 request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body }))
75 })
71} 76}
72 77
73// --------------------------------------------------------------------------- 78// ---------------------------------------------------------------------------