1- import ky from 'ky-universal' ;
1+ import * as got from 'got' ;
2+ import * as tunnel from 'tunnel' ;
23import { IConfigFetcher } from "configcat-common" ;
34import { ProjectConfig } from "configcat-common/lib/ConfigServiceBase" ;
45import { OptionsBase } from "configcat-common/lib/ConfigCatClientOptions" ;
@@ -7,32 +8,47 @@ export class HttpConfigFetcher implements IConfigFetcher {
78
89 fetchLogic ( options : OptionsBase , lastProjectConfig : ProjectConfig , callback : ( newProjectConfig : ProjectConfig ) => void ) : void {
910
10- ky . get ( options . getUrl ( ) , {
11+ let agent ;
12+ if ( options . proxy ) {
13+ try {
14+ const proxy = new URL ( options . proxy ) ;
15+ let agentFactory = tunnel . httpsOverHttp ;
16+ if ( proxy . protocol === 'https:' ) {
17+ agentFactory = tunnel . httpsOverHttps ;
18+ }
19+ agent = agentFactory ( {
20+ proxy : {
21+ host : proxy . hostname ,
22+ port : proxy . port ,
23+ }
24+ } ) ;
25+ } catch {
26+ options . logger . log ( "Failed to parse options.proxy: " + options . proxy ) ;
27+ }
28+ }
29+
30+ got . get ( options . getUrl ( ) , {
31+ agent,
1132 headers : {
1233 "User-Agent" : "ConfigCat-JS/" + options . clientVersion ,
1334 "X-ConfigCat-UserAgent" : "ConfigCat-JS/" + options . clientVersion ,
1435 "If-None-Match" : lastProjectConfig ? lastProjectConfig . HttpETag : null
1536 }
1637 } ) . then ( ( response ) => {
17- if ( response && response . status === 304 ) {
18- callback ( new ProjectConfig ( new Date ( ) . getTime ( ) , JSON . stringify ( lastProjectConfig . ConfigJSON ) , response . headers . get ( 'etag' ) ) ) ;
19- } else if ( response && response . status === 200 ) {
20- response . json ( ) . then ( ( body ) => {
21- callback ( new ProjectConfig ( new Date ( ) . getTime ( ) , JSON . stringify ( body ) , response . headers . get ( 'etag' ) ) ) ;
22- } ) . catch ( ( reason ) => {
23- options . logger . log ( "Error while parsing response JSON. Reason: " + reason ) ;
24- callback ( lastProjectConfig ) ;
25- } ) ;
38+ if ( response && response . statusCode === 304 ) {
39+ callback ( new ProjectConfig ( new Date ( ) . getTime ( ) , JSON . stringify ( lastProjectConfig . ConfigJSON ) , response . headers . etag as string ) ) ;
40+ } else if ( response && response . statusCode === 200 ) {
41+ callback ( new ProjectConfig ( new Date ( ) . getTime ( ) , response . body , response . headers . etag as string ) ) ;
2642 } else {
27- options . logger . log ( "Failed to download config from ConfigCat. Status:" + ( response && response . status ) + " - " + response . statusText ) ;
43+ options . logger . log ( "Failed to download config from ConfigCat. Status:" + ( response && response . statusCode ) + " - " + response . statusMessage ) ;
2844 callback ( lastProjectConfig ) ;
2945 }
3046 } ) . catch ( ( reason ) => {
3147 const response = reason . response ;
3248 if ( response && response . status === 304 ) {
3349 callback ( new ProjectConfig ( new Date ( ) . getTime ( ) , JSON . stringify ( lastProjectConfig . ConfigJSON ) , response . headers . get ( 'etag' ) ) ) ;
3450 } else {
35- options . logger . log ( "Failed to download config from ConfigCat. Status:" + ( response && response . status ) + " - " + response . statusText ) ;
51+ options . logger . log ( "Failed to download config from ConfigCat. Status:" + ( response && response . statusCode ) + " - " + response . statusMessage ) ;
3652 callback ( lastProjectConfig ) ;
3753 }
3854 } ) ;
0 commit comments