Skip to content

Commit 2bf48be

Browse files
committed
chore: simplify node-http-handler
1 parent 461a01c commit 2bf48be

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

packages/node-http-handler/src/node-http-handler.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,9 @@ export class NodeHttpHandler implements HttpHandler<Readable, NodeHttpOptions> {
3131

3232
handle(
3333
request: HttpRequest<Readable>,
34-
options: HttpHandlerOptions
34+
{ abortSignal }: HttpHandlerOptions
3535
): Promise<HttpResponse<Readable>> {
36-
// determine which http(s) client to use
37-
const isSSL = request.protocol === "https:";
38-
const httpClient = isSSL ? https : http;
39-
40-
let path = request.path;
41-
if (request.query) {
42-
const queryString = buildQueryString(request.query);
43-
if (queryString) {
44-
path += `?${queryString}`;
45-
}
46-
}
47-
48-
const nodeHttpsOptions: https.RequestOptions = {
49-
headers: request.headers,
50-
host: request.hostname,
51-
method: request.method,
52-
path: path,
53-
port: request.port,
54-
agent: isSSL ? this.httpsAgent : this.httpAgent
55-
};
56-
5736
return new Promise((resolve, reject) => {
58-
const abortSignal = options && options.abortSignal;
59-
const { connectionTimeout, socketTimeout } = this.httpOptions;
60-
6137
// if the request was already aborted, prevent doing extra work
6238
if (abortSignal && abortSignal.aborted) {
6339
const abortError = new Error("Request aborted");
@@ -66,8 +42,20 @@ export class NodeHttpHandler implements HttpHandler<Readable, NodeHttpOptions> {
6642
return;
6743
}
6844

45+
// determine which http(s) client to use
46+
const isSSL = request.protocol === "https:";
47+
const queryString = buildQueryString(request.query || {});
48+
const nodeHttpsOptions: https.RequestOptions = {
49+
headers: request.headers,
50+
host: request.hostname,
51+
method: request.method,
52+
path: queryString ? `${request.path}?${queryString}` : request.path,
53+
port: request.port,
54+
agent: isSSL ? this.httpsAgent : this.httpAgent
55+
};
56+
6957
// create the http request
70-
const req = (httpClient as typeof http).request(nodeHttpsOptions, res => {
58+
const req = (isSSL ? https : http).request(nodeHttpsOptions, res => {
7159
const httpResponse: HttpResponse<Readable> = {
7260
statusCode: res.statusCode || -1,
7361
headers: getTransformedHeaders(res.headers),
@@ -79,8 +67,8 @@ export class NodeHttpHandler implements HttpHandler<Readable, NodeHttpOptions> {
7967
req.on("error", reject);
8068

8169
// wire-up any timeout logic
82-
setConnectionTimeout(req, reject, connectionTimeout);
83-
setSocketTimeout(req, reject, socketTimeout);
70+
setConnectionTimeout(req, reject, this.httpOptions.connectionTimeout);
71+
setSocketTimeout(req, reject, this.httpOptions.socketTimeout);
8472

8573
// wire-up abort logic
8674
if (abortSignal) {

0 commit comments

Comments
 (0)