@@ -3,8 +3,7 @@ import { net } from "electron"
33import { createWriteStream , ensureDir } from "fs-extra-p"
44import BluebirdPromise from "bluebird-lst-c"
55import * as path from "path"
6- import { HttpExecutor , DownloadOptions , HttpError , DigestTransform , checkSha2 , calculateDownloadProgress } from "electron-builder-http/out/httpExecutor"
7- import { Url } from "url"
6+ import { HttpExecutor , DownloadOptions , HttpError , DigestTransform , checkSha2 , calculateDownloadProgress , maxRedirects } from "electron-builder-http/out/httpExecutor"
87import { safeLoad } from "js-yaml"
98import _debug from "debug"
109import Debugger = debug . Debugger
@@ -14,32 +13,9 @@ function safeGetHeader(response: Electron.IncomingMessage, headerKey: string) {
1413 return response . headers [ headerKey ] ? response . headers [ headerKey ] . pop ( ) : null
1514}
1615
17- export class ElectronHttpExecutor implements HttpExecutor {
16+ export class ElectronHttpExecutor extends HttpExecutor < Electron . RequestOptions , Electron . ClientRequest > {
1817 private readonly debug : Debugger = _debug ( "electron-builder" )
1918
20- private readonly maxRedirects = 10
21-
22- request < T > ( url : Url , token : string | null = null , data : { [ name : string ] : any ; } | null = null , method : string = "GET" ) : Promise < T > {
23- const options : any = Object . assign ( {
24- method : method ,
25- headers : {
26- "User-Agent" : "electron-builder"
27- }
28- } , url )
29-
30- if ( url . hostname ! ! . includes ( "github" ) && ! url . path ! . endsWith ( ".yml" ) ) {
31- options . headers . Accept = "application/vnd.github.v3+json"
32- }
33-
34- const encodedData = data == null ? undefined : new Buffer ( JSON . stringify ( data ) )
35- if ( encodedData != null ) {
36- options . method = "post"
37- options . headers [ "Content-Type" ] = "application/json"
38- options . headers [ "Content-Length" ] = encodedData . length
39- }
40- return this . doApiRequest < T > ( options , token , it => it . end ( encodedData ) )
41- }
42-
4319 download ( url : string , destination : string , options ?: DownloadOptions | null ) : Promise < string > {
4420 return new BluebirdPromise ( ( resolve , reject ) => {
4521 this . doDownload ( url , destination , 0 , options || { } , ( error : Error ) => {
@@ -86,11 +62,11 @@ export class ElectronHttpExecutor implements HttpExecutor {
8662
8763 const redirectUrl = safeGetHeader ( response , "location" )
8864 if ( redirectUrl != null ) {
89- if ( redirectCount < this . maxRedirects ) {
65+ if ( redirectCount < maxRedirects ) {
9066 this . doDownload ( redirectUrl , destination , redirectCount ++ , options , callback )
9167 }
9268 else {
93- callback ( new Error ( " Too many redirects (> " + this . maxRedirects + ")" ) )
69+ callback ( new Error ( ` Too many redirects (> ${ maxRedirects } )` ) )
9470 }
9571 return
9672 }
@@ -162,14 +138,10 @@ Please double check that your authentication token is correct. Due to security r
162138 return
163139 }
164140
165- if ( options . path ! . endsWith ( "/latest" ) ) {
166- resolve ( < any > { location : redirectUrl } )
167- }
168- else {
169- this . doApiRequest ( Object . assign ( { } , options , parseUrl ( redirectUrl ) ) , token , requestProcessor )
170- . then ( < any > resolve )
171- . catch ( reject )
172- }
141+ this . doApiRequest ( Object . assign ( { } , options , parseUrl ( redirectUrl ) ) , token , requestProcessor )
142+ . then ( < any > resolve )
143+ . catch ( reject )
144+
173145 return
174146 }
175147
@@ -182,7 +154,8 @@ Please double check that your authentication token is correct. Due to security r
182154 response . on ( "end" , ( ) => {
183155 try {
184156 const contentType = response . headers [ "content-type" ]
185- const isJson = contentType != null && contentType . includes ( "json" )
157+ const isJson = contentType != null && contentType . find ( ( i ) => i . indexOf ( "json" ) !== - 1 )
158+ const isYaml = options . path ! . includes ( ".yml" )
186159 if ( response . statusCode >= 400 ) {
187160 if ( isJson ) {
188161 reject ( new HttpError ( response , JSON . parse ( data ) ) )
@@ -192,7 +165,7 @@ Please double check that your authentication token is correct. Due to security r
192165 }
193166 }
194167 else {
195- resolve ( data . length === 0 ? null : ( isJson || ! options . path ! . includes ( ".yml" ) ) ? JSON . parse ( data ) : safeLoad ( data ) )
168+ resolve ( data . length === 0 ? null : ( isJson ? JSON . parse ( data ) : isYaml ? safeLoad ( data ) : data ) )
196169 }
197170 }
198171 catch ( e ) {
0 commit comments