Skip to content

Commit 9da684f

Browse files
committed
Add support for proxy
1 parent 38dcecb commit 9da684f

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

src/config.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
export type Config = {
22
api_key: string | null;
33
timeout: number;
4+
http_proxy?: string;
5+
https_proxy?: string;
6+
no_proxy?: string;
47
};
8+
9+
function getEnvVar(name: string): string | undefined {
10+
if (typeof Deno !== "undefined") {
11+
return Deno.env.get(name);
12+
// @ts-ignore: Node.js process
13+
} else if (typeof process !== "undefined") {
14+
// @ts-ignore: Node.js process
15+
return process.env[name];
16+
}
17+
return undefined;
18+
}
19+
520
export const config: Config = {
621
api_key: null,
722
timeout: 60000,
23+
http_proxy: getEnvVar("HTTP_PROXY") || getEnvVar("http_proxy"),
24+
https_proxy: getEnvVar("HTTPS_PROXY") || getEnvVar("https_proxy"),
25+
no_proxy: getEnvVar("NO_PROXY") || getEnvVar("no_proxy"),
826
};

src/utils.ts

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { version } from "../version.ts";
22
import https from "node:https";
3+
import http from "node:http";
34
import qs from "node:querystring";
5+
import { HttpsProxyAgent } from "npm:https-proxy-agent";
46
import { RequestTimeoutError } from "./errors.ts";
7+
import { config } from "./config.ts";
8+
import { Buffer } from "node:buffer";
59

610
/**
711
* This `_internals` object is needed to support stubbing/spying of
@@ -60,18 +64,30 @@ export function execute(
6064
...parameters,
6165
source: getSource(),
6266
});
67+
68+
// Check if we should use a proxy
69+
const urlObj = new URL(url);
70+
const shouldUseProxy = !config.no_proxy?.split(",").some((domain) =>
71+
urlObj.hostname.endsWith(domain.trim())
72+
);
73+
74+
const proxyUrl = shouldUseProxy
75+
? (urlObj.protocol === "https:" ? config.https_proxy : config.http_proxy)
76+
: undefined;
77+
6378
return new Promise((resolve, reject) => {
6479
let timer: number;
65-
const req = https.get(url, (resp) => {
80+
81+
const handleResponse = (resp: http.IncomingMessage) => {
6682
resp.setEncoding("utf8");
6783
let data = "";
6884

69-
// A chunk of data has been recieved.
70-
resp.on("data", (chunk) => {
85+
// A chunk of data has been received
86+
resp.on("data", (chunk: Buffer) => {
7187
data += chunk;
7288
});
7389

74-
// The whole response has been received. Print out the result.
90+
// The whole response has been received
7591
resp.on("end", () => {
7692
try {
7793
if (resp.statusCode == 200) {
@@ -85,10 +101,25 @@ export function execute(
85101
if (timer) clearTimeout(timer);
86102
}
87103
});
88-
}).on("error", (err) => {
104+
};
105+
106+
const handleError = (err: Error) => {
89107
reject(err);
90108
if (timer) clearTimeout(timer);
91-
});
109+
};
110+
111+
const options: https.RequestOptions = {
112+
timeout: timeout > 0 ? timeout : undefined,
113+
};
114+
115+
if (proxyUrl) {
116+
options.agent = new HttpsProxyAgent(proxyUrl);
117+
}
118+
119+
const req = https.get(url, options, handleResponse).on(
120+
"error",
121+
handleError,
122+
);
92123

93124
if (timeout > 0) {
94125
timer = setTimeout(() => {

0 commit comments

Comments
 (0)