Skip to content

Commit

Permalink
feat: Fix FD leak in request (#456)
Browse files Browse the repository at this point in the history
* feat: Fix FD leak in request

* feat: Add response.read() alternative

* feat: I forget semicolons a lot
  • Loading branch information
astuyve authored Dec 20, 2023
1 parent f116ecb commit 6baae54
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ export function get(url: URL, options?: Partial<RequestOptions>): Promise<Reques
return sendRequest(url, requestOptions);
}

// This utility function returns NO data, as per the types indicate
// if a response body is needed, we can implement a listener
// response.on('data', cb)
// or call
// response.read()
// which can append data to a buffer and return it
function sendRequest(url: URL, options: RequestOptions, buffer?: Buffer): Promise<RequestResult> {
return new Promise((resolve) => {
const requestMethod = url.protocol === "https:" ? https.request : http.request;

const request = requestMethod(options, (response) => {
const statusCode = response.statusCode;

// https://nodejs.org/api/http.html#class-httpclientrequest
// "Until the data is consumed, the 'end' event will not fire. Also, until the data is read it will consume memory that can eventually lead to a 'process out of memory' error"
response.resume();

if (statusCode === undefined || statusCode < 200 || statusCode > 299) {
return resolve({
success: false,
Expand All @@ -61,7 +71,7 @@ function sendRequest(url: URL, options: RequestOptions, buffer?: Buffer): Promis
});
});

request.on("error", (error) => {
request.once("error", (error) => {
resolve({
success: false,
errorMessage: error.message,
Expand Down

0 comments on commit 6baae54

Please sign in to comment.