diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/helpers/requests.ts | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r-- | server/helpers/requests.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts new file mode 100644 index 000000000..8ded52972 --- /dev/null +++ b/server/helpers/requests.ts | |||
@@ -0,0 +1,65 @@ | |||
1 | import replay = require('request-replay') | ||
2 | import request = require('request') | ||
3 | |||
4 | import { | ||
5 | RETRY_REQUESTS, | ||
6 | REMOTE_SCHEME, | ||
7 | CONFIG | ||
8 | } from '../initializers' | ||
9 | import { sign } from './peertube-crypto' | ||
10 | |||
11 | function makeRetryRequest (params, callback) { | ||
12 | replay( | ||
13 | request(params, callback), | ||
14 | { | ||
15 | retries: RETRY_REQUESTS, | ||
16 | factor: 3, | ||
17 | maxTimeout: Infinity, | ||
18 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | ||
19 | } | ||
20 | ) | ||
21 | } | ||
22 | |||
23 | function makeSecureRequest (params, callback) { | ||
24 | const requestParams = { | ||
25 | url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, | ||
26 | json: {} | ||
27 | } | ||
28 | |||
29 | if (params.method !== 'POST') { | ||
30 | return callback(new Error('Cannot make a secure request with a non POST method.')) | ||
31 | } | ||
32 | |||
33 | // Add signature if it is specified in the params | ||
34 | if (params.sign === true) { | ||
35 | const host = CONFIG.WEBSERVER.HOST | ||
36 | |||
37 | let dataToSign | ||
38 | if (params.data) { | ||
39 | dataToSign = params.data | ||
40 | } else { | ||
41 | // We do not have data to sign so we just take our host | ||
42 | // It is not ideal but the connection should be in HTTPS | ||
43 | dataToSign = host | ||
44 | } | ||
45 | |||
46 | requestParams.json['signature'] = { | ||
47 | host, // Which host we pretend to be | ||
48 | signature: sign(dataToSign) | ||
49 | } | ||
50 | } | ||
51 | |||
52 | // If there are data informations | ||
53 | if (params.data) { | ||
54 | requestParams.json['data'] = params.data | ||
55 | } | ||
56 | |||
57 | request.post(requestParams, callback) | ||
58 | } | ||
59 | |||
60 | // --------------------------------------------------------------------------- | ||
61 | |||
62 | export { | ||
63 | makeRetryRequest, | ||
64 | makeSecureRequest | ||
65 | } | ||