aboutsummaryrefslogtreecommitdiffhomepage
path: root/helpers/requests.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-05 18:03:20 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-05 18:03:20 +0100
commitdac0a5319ab1c52a0958647b1593f85339b77e29 (patch)
tree716bc9e7e0b9fd3a7066530e50e73991fb278d05 /helpers/requests.js
parenta5fa04b0cce851f680a45055f57ed2c3d22f1c82 (diff)
downloadPeerTube-dac0a5319ab1c52a0958647b1593f85339b77e29.tar.gz
PeerTube-dac0a5319ab1c52a0958647b1593f85339b77e29.tar.zst
PeerTube-dac0a5319ab1c52a0958647b1593f85339b77e29.zip
Split utils file
Diffstat (limited to 'helpers/requests.js')
-rw-r--r--helpers/requests.js111
1 files changed, 111 insertions, 0 deletions
diff --git a/helpers/requests.js b/helpers/requests.js
new file mode 100644
index 000000000..0e301da79
--- /dev/null
+++ b/helpers/requests.js
@@ -0,0 +1,111 @@
1;(function () {
2 'use strict'
3
4 var async = require('async')
5 var config = require('config')
6 var request = require('request')
7 var replay = require('request-replay')
8
9 var constants = require('../initializers/constants')
10 var logger = require('./logger')
11 var peertubeCrypto = require('./peertubeCrypto')
12
13 var http = config.get('webserver.https') ? 'https' : 'http'
14 var host = config.get('webserver.host')
15 var port = config.get('webserver.port')
16
17 var requests = {
18 makeMultipleRetryRequest: makeMultipleRetryRequest
19 }
20
21 function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
22 if (!callback) {
23 callback = callbackEach
24 callbackEach = null
25 }
26
27 var url = http + '://' + host + ':' + port
28 var signature
29
30 // Add signature if it is specified in the params
31 if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
32 signature = peertubeCrypto.sign(url)
33 }
34
35 // Make a request for each pod
36 async.each(pods, function (pod, callback_each_async) {
37 function callbackEachRetryRequest (err, response, body, url, pod) {
38 if (callbackEach !== null) {
39 callbackEach(err, response, body, url, pod, function () {
40 callback_each_async()
41 })
42 } else {
43 callback_each_async()
44 }
45 }
46
47 var params = {
48 url: pod.url + all_data.path,
49 method: all_data.method
50 }
51
52 // Add data with POST requst ?
53 if (all_data.method === 'POST' && all_data.data) {
54 // Encrypt data ?
55 if (all_data.encrypt === true) {
56 // TODO: ES6 with let
57 ;(function (copy_params, copy_url, copy_pod, copy_signature) {
58 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
59 if (err) return callback(err)
60
61 copy_params.json = {
62 data: encrypted.data,
63 key: encrypted.key
64 }
65
66 makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
67 })
68 })(params, url, pod, signature)
69 } else {
70 params.json = { data: all_data.data }
71 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
72 }
73 } else {
74 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
75 }
76 }, callback)
77 }
78
79 // ---------------------------------------------------------------------------
80
81 module.exports = requests
82
83 // ---------------------------------------------------------------------------
84
85 function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
86 // Append the signature
87 if (signature) {
88 params.json.signature = {
89 url: from_url,
90 signature: signature
91 }
92 }
93
94 logger.debug('Make retry requests to %s.', to_pod.url)
95
96 replay(
97 request.post(params, function (err, response, body) {
98 callbackEach(err, response, body, params.url, to_pod)
99 }),
100 {
101 retries: constants.REQUEST_RETRIES,
102 factor: 3,
103 maxTimeout: Infinity,
104 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
105 }
106 ).on('replay', function (replay) {
107 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
108 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
109 })
110 }
111})()