]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
dac0a531 | 2 | |
f0f5567b | 3 | const replay = require('request-replay') |
528a9efa | 4 | const request = require('request') |
dac0a531 | 5 | |
f0f5567b | 6 | const constants = require('../initializers/constants') |
5f698b82 | 7 | const peertubeCrypto = require('./peertube-crypto') |
dac0a531 | 8 | |
f0f5567b | 9 | const requests = { |
c4403b29 C |
10 | makeRetryRequest, |
11 | makeSecureRequest | |
9f10b292 | 12 | } |
dac0a531 | 13 | |
528a9efa C |
14 | function makeRetryRequest (params, callback) { |
15 | replay( | |
16 | request(params, callback), | |
17 | { | |
18 | retries: constants.RETRY_REQUESTS, | |
19 | factor: 3, | |
20 | maxTimeout: Infinity, | |
21 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | |
22 | } | |
23 | ) | |
24 | } | |
dac0a531 | 25 | |
528a9efa | 26 | function makeSecureRequest (params, callback) { |
528a9efa C |
27 | const requestParams = { |
28 | url: params.toPod.url + params.path | |
9f10b292 | 29 | } |
dac0a531 | 30 | |
528a9efa C |
31 | // Add data with POST requst ? |
32 | if (params.method === 'POST') { | |
33 | requestParams.json = {} | |
dac0a531 | 34 | |
528a9efa C |
35 | // Add signature if it is specified in the params |
36 | if (params.sign === true) { | |
37 | requestParams.json.signature = { | |
e861452f C |
38 | url: constants.CONFIG.WEBSERVER.URL, |
39 | signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL) | |
528a9efa | 40 | } |
9f10b292 | 41 | } |
dac0a531 | 42 | |
528a9efa C |
43 | // If there are data informations |
44 | if (params.data) { | |
45 | // Encrypt data | |
46 | if (params.encrypt === true) { | |
47 | peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) { | |
f0f5567b | 48 | if (err) return callback(err) |
9f10b292 | 49 | |
528a9efa C |
50 | requestParams.json.data = encrypted.data |
51 | requestParams.json.key = encrypted.key | |
9f10b292 | 52 | |
528a9efa | 53 | request.post(requestParams, callback) |
f0f5567b | 54 | }) |
dac0a531 | 55 | } else { |
528a9efa C |
56 | // No encryption |
57 | requestParams.json.data = params.data | |
58 | request.post(requestParams, callback) | |
dac0a531 | 59 | } |
9f10b292 | 60 | } else { |
528a9efa C |
61 | // No data |
62 | request.post(requestParams, callback) | |
9f10b292 | 63 | } |
528a9efa C |
64 | } else { |
65 | request.get(requestParams, callback) | |
66 | } | |
9f10b292 | 67 | } |
dac0a531 | 68 | |
9f10b292 | 69 | // --------------------------------------------------------------------------- |
dac0a531 | 70 | |
9f10b292 | 71 | module.exports = requests |