Skip to content

Commit 04222e3

Browse files
ziofatConfigCat
authored andcommitted
add proxy support with got and node-tunnel (#8)
1 parent 5b62002 commit 04222e3

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"license": "MIT",
2727
"dependencies": {
2828
"configcat-common": "^1.1.25",
29-
"ky": "^0.12.0",
30-
"ky-universal": "^0.3.0"
29+
"got": "^9.6.0",
30+
"tunnel": "0.0.6"
3131
},
3232
"devDependencies": {
3333
"@types/chai": "^4.2.0",

src/config-fetcher.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import ky from 'ky-universal';
1+
import * as got from 'got';
2+
import * as tunnel from 'tunnel';
23
import { IConfigFetcher } from "configcat-common";
34
import { ProjectConfig } from "configcat-common/lib/ConfigServiceBase";
45
import { 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

Comments
 (0)