2020-09-07 00:47:53 +00:00
|
|
|
import http from 'http'
|
|
|
|
import https from 'https'
|
|
|
|
import fs from 'fs'
|
2020-09-12 20:31:36 +00:00
|
|
|
import url from 'url'
|
2020-09-07 00:47:53 +00:00
|
|
|
|
2020-09-13 04:36:33 +00:00
|
|
|
export function request(config, path, filePath = null, redirects, returnText = false) {
|
|
|
|
if (!config || typeof(config) === 'string') {
|
|
|
|
return Promise.reject(new Error('Request must be called with config in first parameter'))
|
|
|
|
}
|
2020-09-12 20:31:36 +00:00
|
|
|
let newRedirects = redirects + 1
|
|
|
|
if (!path || !path.startsWith('http')) {
|
|
|
|
return Promise.reject(new Error('URL was empty or missing http in front'))
|
|
|
|
}
|
|
|
|
let parsed = new url.URL(path)
|
2020-09-07 00:47:53 +00:00
|
|
|
|
|
|
|
let h
|
|
|
|
if (parsed.protocol === 'https:') {
|
|
|
|
h = https
|
|
|
|
} else {
|
|
|
|
h = http
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
if (!path) {
|
|
|
|
return reject(new Error('Request path was empty'))
|
|
|
|
}
|
2020-09-13 04:36:33 +00:00
|
|
|
let headers = {
|
|
|
|
'User-Agent': 'TheThing/service-core',
|
|
|
|
Accept: 'application/vnd.github.v3+json'
|
|
|
|
}
|
|
|
|
if (config.githubAuthToken && path.indexOf('api.github.com') >= 0) {
|
|
|
|
headers['Authorization'] = `token ${config.githubAuthToken}`
|
|
|
|
}
|
2020-09-07 00:47:53 +00:00
|
|
|
let req = h.request({
|
|
|
|
path: parsed.pathname + parsed.search,
|
|
|
|
port: parsed.port,
|
|
|
|
method: 'GET',
|
2020-09-13 04:36:33 +00:00
|
|
|
headers: headers,
|
|
|
|
timeout: returnText ? 5000 : 10000,
|
2020-09-07 00:47:53 +00:00
|
|
|
hostname: parsed.hostname
|
|
|
|
}, function(res) {
|
|
|
|
let output = ''
|
|
|
|
if (filePath) {
|
|
|
|
let file = fs.createWriteStream(filePath)
|
|
|
|
res.pipe(file)
|
|
|
|
} else {
|
|
|
|
res.on('data', function(chunk) {
|
|
|
|
output += chunk
|
|
|
|
})
|
|
|
|
}
|
|
|
|
res.on('end', function() {
|
|
|
|
if (res.statusCode >= 300 && res.statusCode < 400) {
|
2020-09-12 20:31:36 +00:00
|
|
|
if (newRedirects > 5) {
|
2020-09-07 00:47:53 +00:00
|
|
|
return reject(new Error(`Too many redirects (last one was ${res.headers.location})`))
|
|
|
|
}
|
2020-09-12 20:31:36 +00:00
|
|
|
if (!res.headers.location) {
|
|
|
|
return reject(new Error('Redirect returned no path in location header'))
|
|
|
|
}
|
|
|
|
if (res.headers.location.startsWith('http')) {
|
2020-09-13 04:36:33 +00:00
|
|
|
return resolve(request(config, res.headers.location, filePath, newRedirects, returnText))
|
2020-09-12 20:31:36 +00:00
|
|
|
} else {
|
2020-09-13 04:36:33 +00:00
|
|
|
return resolve(request(config, url.resolve(path, res.headers.location), filePath, newRedirects, returnText))
|
2020-09-12 20:31:36 +00:00
|
|
|
}
|
2020-09-07 00:47:53 +00:00
|
|
|
} else if (res.statusCode >= 400) {
|
2020-09-12 20:31:36 +00:00
|
|
|
return reject(new Error(`HTTP Error ${res.statusCode}: ${output}`))
|
2020-09-07 00:47:53 +00:00
|
|
|
}
|
|
|
|
resolve({
|
|
|
|
statusCode: res.statusCode,
|
|
|
|
status: res.statusCode,
|
|
|
|
statusMessage: res.statusMessage,
|
|
|
|
headers: res.headers,
|
|
|
|
body: output
|
|
|
|
})
|
|
|
|
})
|
|
|
|
req.on('error', reject)
|
2020-09-12 20:31:36 +00:00
|
|
|
req.on('timeout', function(err) {
|
|
|
|
reject(err)
|
|
|
|
})
|
2020-09-07 00:47:53 +00:00
|
|
|
})
|
|
|
|
req.end()
|
|
|
|
}).then(function(res) {
|
2020-09-12 20:31:36 +00:00
|
|
|
if (!filePath && !returnText) {
|
2020-09-07 00:47:53 +00:00
|
|
|
try {
|
|
|
|
res.body = JSON.parse(res.body)
|
|
|
|
} catch(e) {
|
|
|
|
throw new Error(res.body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
}
|