First typescript iteration
[oweals/peertube.git] / server / middlewares / pods.ts
1 'use strict'
2
3 const constants = require('../initializers/constants')
4
5 function setBodyHostsPort (req, res, next) {
6   if (!req.body.hosts) return next()
7
8   for (let i = 0; i < req.body.hosts.length; i++) {
9     const hostWithPort = getHostWithPort(req.body.hosts[i])
10
11     // Problem with the url parsing?
12     if (hostWithPort === null) {
13       return res.sendStatus(500)
14     }
15
16     req.body.hosts[i] = hostWithPort
17   }
18
19   return next()
20 }
21
22 function setBodyHostPort (req, res, next) {
23   if (!req.body.host) return next()
24
25   const hostWithPort = getHostWithPort(req.body.host)
26
27   // Problem with the url parsing?
28   if (hostWithPort === null) {
29     return res.sendStatus(500)
30   }
31
32   req.body.host = hostWithPort
33
34   return next()
35 }
36
37 // ---------------------------------------------------------------------------
38
39 export {
40   setBodyHostsPort,
41   setBodyHostPort
42 }
43
44 // ---------------------------------------------------------------------------
45
46 function getHostWithPort (host) {
47   const splitted = host.split(':')
48
49   // The port was not specified
50   if (splitted.length === 1) {
51     if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
52
53     return host + ':80'
54   }
55
56   return host
57 }