Skip to content

Commit 4b5b7f4

Browse files
authored
[FSSDK-9369] fix: fix makeRequest method of NodeRequestHandler class (#826)
1 parent c1f0463 commit 4b5b7f4

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

packages/optimizely-sdk/lib/utils/http_request_handler/node_request_handler.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 Optimizely
2+
* Copyright 2022-2023 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
import http from 'http';
1817
import https from 'https';
1918
import url from 'url';
@@ -62,17 +61,14 @@ export class NodeRequestHandler implements RequestHandler {
6261
},
6362
timeout: this.timeout,
6463
});
65-
const responsePromise = this.getResponseFromRequest(request);
64+
const abortableRequest = this.getAbortableRequestFromRequest(request);
6665

6766
if (data) {
6867
request.write(data);
6968
}
7069
request.end();
7170

72-
return {
73-
abort: () => request.destroy(),
74-
responsePromise,
75-
};
71+
return abortableRequest;
7672
}
7773

7874
/**
@@ -119,11 +115,19 @@ export class NodeRequestHandler implements RequestHandler {
119115
* Sends a built request handling response, errors, and events around the transmission
120116
* @param request Request to send
121117
* @private
122-
* @returns Response Promise-wrapped, simplified response object
118+
* @returns AbortableRequest with simplified response promise
123119
*/
124-
private getResponseFromRequest(request: http.ClientRequest): Promise<Response> {
125-
return new Promise((resolve, reject) => {
120+
private getAbortableRequestFromRequest(request: http.ClientRequest): AbortableRequest {
121+
let aborted = false;
122+
123+
const abort = () => {
124+
aborted = true;
125+
request.destroy();
126+
};
127+
128+
const responsePromise: Promise<Response> = new Promise((resolve, reject) => {
126129
request.on('timeout', () => {
130+
aborted = true;
127131
request.destroy();
128132
reject(new Error('Request timed out'));
129133
});
@@ -140,7 +144,7 @@ export class NodeRequestHandler implements RequestHandler {
140144
});
141145

142146
request.once('response', (incomingMessage: http.IncomingMessage) => {
143-
if (request.destroyed) {
147+
if (aborted) {
144148
return;
145149
}
146150

@@ -150,13 +154,13 @@ export class NodeRequestHandler implements RequestHandler {
150154

151155
let responseData = '';
152156
response.on('data', (chunk: string) => {
153-
if (!request.destroyed) {
157+
if (!aborted) {
154158
responseData += chunk;
155159
}
156160
});
157161

158162
response.on('end', () => {
159-
if (request.destroyed) {
163+
if (aborted) {
160164
return;
161165
}
162166

@@ -168,5 +172,7 @@ export class NodeRequestHandler implements RequestHandler {
168172
});
169173
});
170174
});
175+
176+
return { abort, responsePromise };
171177
}
172178
}

0 commit comments

Comments
 (0)