-
Notifications
You must be signed in to change notification settings - Fork 12
/
https-request.js
129 lines (118 loc) · 4.46 KB
/
https-request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const gently = require('./gently')
module.exports = async (context, hostname, method, requestPath, body, headers) => {
headers = {
'User-Agent': 'GitForWindowsHelperApp/0.0',
Accept: 'application/json',
...headers || {}
}
let writeBody = req => {
req.end()
}
if (body) {
const fs = require('fs')
if (body instanceof fs.ReadStream) {
const path = body.path
const stat = fs.statSync(path)
headers['Content-Length'] = stat.size
if (!headers['Content-Type']) {
const [fileExtension] = path.match(/\.[^.]+$/)
headers['Content-Type'] = {
'.7z': 'application/zip',
'.bz2': 'application/x-bzip2',
'.deb': 'application/x-debian-package',
'.exe': 'application/executable',
'.gz': 'application/gzip',
'.js': 'application/javascript',
'.md': 'text/markdown',
'.png': 'image/png',
'.sh': 'text/x-shellscript',
'.svg': 'image/svg+xml',
'.txt': 'text/plain',
'.xz': 'application/x-xz',
'.zip': 'application/zip'
}[fileExtension] || 'application/octet-stream'
}
writeBody = req => {
body.pipe(req)
}
} else {
if (typeof body === 'object') body = JSON.stringify(body)
headers['Content-Type'] = 'application/json'
headers['Content-Length'] = body.length
writeBody = req => {
req.write(body)
req.end()
}
}
}
const options = {
port: 443,
hostname: hostname || 'api.github.com',
method: method || 'GET',
path: requestPath,
headers
}
return new Promise((resolve, reject) => {
try {
const https = require('https')
const handler = res => {
if (res.statusCode === 204) {
resolve({
statusCode: res.statusCode,
statusMessage: res.statusMessage,
headers: res.headers
})
return
}
res.on('error', e => reject(e))
const chunks = []
res.on('data', data => chunks.push(data))
res.on('end', () => {
const json = Buffer.concat(chunks).toString('utf-8')
if (res.statusCode > 299) {
context.log('FAILED GitHub REST API call!')
context.log(options)
reject({
statusCode: res.statusCode,
statusMessage: res.statusMessage,
body: json,
json: gently(() => JSON.parse(json))
})
return
}
try {
resolve(JSON.parse(json))
} catch (e) {
reject(`Invalid JSON: ${json}`)
}
})
}
const req = https.request(options, res => {
if (res.statusCode === 301 || res.statusCode === 302) {
const url = res.headers.location
const match = url.match(/^https:\/\/([^/]+)(.*)$/)
if (!match) throw new Error(`Unsupported redirect URL: ${url}`)
const options = {
port: 443,
hostname: match[1],
method: 'GET',
path: match[2],
headers
}
const portNumberMatch = options.hostname.match(/^(.*):(\d+)$/)
if (portNumberMatch) {
options.port = Number(portNumberMatch[2])
options.hostname = portNumberMatch[1]
}
const req = https.request(options, handler)
req.on('error', err => reject(err))
req.end()
} else handler(res)
})
req.on('error', err => reject(err))
writeBody(req)
} catch (e) {
reject(e)
}
})
}