-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
httpExecutor.ts
297 lines (251 loc) · 10 KB
/
httpExecutor.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { createHash } from "crypto"
import { Transform } from "stream"
import { createWriteStream } from "fs-extra-p"
import { RequestOptions } from "http"
import { parse as parseUrl } from "url"
import _debug from "debug"
import { ProgressCallbackTransform } from "./ProgressCallbackTransform"
import { safeLoad } from "js-yaml"
import { EventEmitter } from "events"
import { Socket } from "net"
export interface RequestHeaders {
[key: string]: any
}
export interface Response extends EventEmitter {
statusCode?: number
statusMessage?: string
headers: any
setEncoding(encoding: string): void
}
export interface DownloadOptions {
headers?: RequestHeaders | null
skipDirCreation?: boolean
sha2?: string
onProgress?(progress: any): void
}
export class HttpExecutorHolder {
private _httpExecutor: HttpExecutor<any, any>
get httpExecutor(): HttpExecutor<any, any> {
if (this._httpExecutor == null) {
this._httpExecutor = new (require("electron-builder/out/util/nodeHttpExecutor").NodeHttpExecutor)()
}
return this._httpExecutor
}
set httpExecutor(value: HttpExecutor<any, any>) {
this._httpExecutor = value
}
}
export const executorHolder = new HttpExecutorHolder()
export function download(url: string, destination: string, options?: DownloadOptions | null): Promise<string> {
return executorHolder.httpExecutor.download(url, destination, options)
}
export class HttpError extends Error {
constructor(public readonly response: {statusMessage?: string | undefined, statusCode?: number | undefined, headers?: { [key: string]: string[]; } | undefined}, public description: any | null = null) {
super(response.statusCode + " " + response.statusMessage + (description == null ? "" : ("\n" + JSON.stringify(description, null, " "))) + "\nHeaders: " + JSON.stringify(response.headers, null, " "))
this.name = "HttpError"
}
}
export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
protected readonly maxRedirects = 10
protected readonly debug = _debug("electron-builder")
request<T>(options: RequestOptions, data?: { [name: string]: any; } | null): Promise<T> {
configureRequestOptions(options)
const encodedData = data == null ? undefined : new Buffer(JSON.stringify(data))
if (encodedData != null) {
options.method = "post"
options.headers!["Content-Type"] = "application/json"
options.headers!["Content-Length"] = encodedData.length
}
return this.doApiRequest<T>(<REQUEST_OPTS>options, it => (<any>it).end(encodedData), 0)
}
protected abstract doApiRequest<T>(options: REQUEST_OPTS, requestProcessor: (request: REQUEST, reject: (error: Error) => void) => void, redirectCount: number): Promise<T>
abstract download(url: string, destination: string, options?: DownloadOptions | null): Promise<string>
protected handleResponse(response: Response, options: RequestOptions, resolve: (data?: any) => void, reject: (error: Error) => void, redirectCount: number, requestProcessor: (request: REQUEST, reject: (error: Error) => void) => void) {
if (this.debug.enabled) {
this.debug(`Response status: ${response.statusCode} ${response.statusMessage}, request options: ${dumpRequestOptions(options)}`)
}
// we handle any other >= 400 error on request end (read detailed message in the response body)
if (response.statusCode === 404) {
// error is clear, we don't need to read detailed error description
reject(new HttpError(response, `method: ${options.method} url: https://${options.hostname}${options.path}
Please double check that your authentication token is correct. Due to security reasons actual status maybe not reported, but 404.
`))
return
}
else if (response.statusCode === 204) {
// on DELETE request
resolve()
return
}
const redirectUrl = safeGetHeader(response, "location")
if (redirectUrl != null) {
if (redirectCount > 10) {
reject(new Error("Too many redirects (> 10)"))
return
}
this.doApiRequest(<REQUEST_OPTS>Object.assign({}, options, parseUrl(redirectUrl)), requestProcessor, redirectCount)
.then(resolve)
.catch(reject)
return
}
let data = ""
response.setEncoding("utf8")
response.on("data", (chunk: string) => {
data += chunk
})
response.on("end", () => {
try {
const contentType = response.headers["content-type"]
const isJson = contentType != null && (Array.isArray(contentType) ? contentType.find(it => it.includes("json")) != null : contentType.includes("json"))
if (response.statusCode >= 400) {
reject(new HttpError(response, isJson ? JSON.parse(data) : data))
}
else {
const pathname = (<any>options).pathname || options.path
if (data.length === 0) {
resolve()
}
else if (pathname != null && pathname.endsWith(".yml")) {
resolve(safeLoad(data))
}
else {
resolve(isJson || (pathname != null && pathname.endsWith(".json")) ? JSON.parse(data) : data)
}
}
}
catch (e) {
reject(e)
}
})
}
protected abstract doRequest(options: any, callback: (response: any) => void): any
protected doDownload(requestOptions: any, destination: string, redirectCount: number, options: DownloadOptions, callback: (error: Error | null) => void) {
const request = this.doRequest(requestOptions, (response: Electron.IncomingMessage) => {
if (response.statusCode >= 400) {
callback(new Error(`Cannot download "${requestOptions.protocol || "https"}://${requestOptions.hostname}/${requestOptions.path}", status ${response.statusCode}: ${response.statusMessage}`))
return
}
const redirectUrl = safeGetHeader(response, "location")
if (redirectUrl != null) {
if (redirectCount < this.maxRedirects) {
const parsedUrl = parseUrl(redirectUrl)
this.doDownload(Object.assign({}, requestOptions, {
hostname: parsedUrl.hostname,
path: parsedUrl.path,
port: parsedUrl.port == null ? undefined : parsedUrl.port
}), destination, redirectCount++, options, callback)
}
else {
callback(new Error(`Too many redirects (> ${this.maxRedirects})`))
}
return
}
configurePipes(options, response, destination, callback)
})
this.addTimeOutHandler(request, callback)
request.on("error", callback)
request.end()
}
protected addTimeOutHandler(request: any, callback: (error: Error) => void) {
request.on("socket", function (socket: Socket) {
socket.setTimeout(60 * 1000, () => {
callback(new Error("Request timed out"))
request.abort()
})
})
}
}
class DigestTransform extends Transform {
private readonly digester = createHash("sha256")
constructor(private expected: string) {
super()
}
_transform(chunk: any, encoding: string, callback: Function) {
this.digester.update(chunk)
callback(null, chunk)
}
_flush(callback: Function): void {
const hash = this.digester.digest("hex")
callback(hash === this.expected ? null : new Error(`SHA2 checksum mismatch, expected ${this.expected}, got ${hash}`))
}
}
export function request<T>(options: RequestOptions, data?: {[name: string]: any; } | null): Promise<T> {
return executorHolder.httpExecutor.request(options, data)
}
function checkSha2(sha2Header: string | null | undefined, sha2: string | null | undefined, callback: (error: Error | null) => void): boolean {
if (sha2Header != null && sha2 != null) {
// todo why bintray doesn't send this header always
if (sha2Header == null) {
callback(new Error("checksum is required, but server response doesn't contain X-Checksum-Sha2 header"))
return false
}
else if (sha2Header !== sha2) {
callback(new Error(`checksum mismatch: expected ${sha2} but got ${sha2Header} (X-Checksum-Sha2 header)`))
return false
}
}
return true
}
function safeGetHeader(response: any, headerKey: string) {
const value = response.headers[headerKey]
if (value == null) {
return null
}
else if (Array.isArray(value)) {
// electron API
return value.length === 0 ? null : value[value.length - 1]
}
else {
return value
}
}
function configurePipes(options: DownloadOptions, response: any, destination: string, callback: (error: Error | null) => void) {
if (!checkSha2(safeGetHeader(response, "X-Checksum-Sha2"), options.sha2, callback)) {
return
}
const streams: Array<any> = []
if (options.onProgress != null) {
const contentLength = safeGetHeader(response, "content-length")
if (contentLength != null) {
streams.push(new ProgressCallbackTransform(parseInt(contentLength, 10), options.onProgress))
}
}
if (options.sha2 != null) {
streams.push(new DigestTransform(options.sha2))
}
const fileOut = createWriteStream(destination)
streams.push(fileOut)
let lastStream = response
for (const stream of streams) {
stream.on("error", callback)
lastStream = lastStream.pipe(stream)
}
fileOut.on("finish", () => (<any>fileOut.close)(callback))
}
export function configureRequestOptions(options: RequestOptions, token?: string | null, method?: "GET" | "DELETE" | "PUT"): RequestOptions {
if (method != null) {
options.method = method
}
let headers = options.headers
if (headers == null) {
headers = {}
options.headers = headers
}
if (token != null) {
(<any>headers).authorization = token.startsWith("Basic") ? token : `token ${token}`
}
if (headers["User-Agent"] == null) {
headers["User-Agent"] = "electron-builder"
}
if ((method == null || method === "GET") || headers["Cache-Control"] == null) {
headers["Cache-Control"] = "no-cache"
}
return options
}
export function dumpRequestOptions(options: RequestOptions): string {
const safe: any = Object.assign({}, options)
if (safe.headers != null && safe.headers.authorization != null) {
safe.headers.authorization = "<skipped>"
}
return JSON.stringify(safe, null, 2)
}