Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleaup stream and handle errors #1769

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: avoid unnecessary API
  • Loading branch information
alexander-akait committed Mar 19, 2024
commit 5fa75bcc3dfd5608c37d66ae7dd770b01f37b879
28 changes: 3 additions & 25 deletions src/utils/compatibleAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,6 @@
res.setHeader(name, value);
}

/**
* @template {ServerResponse} Response
* @param {Response} res
* @param {Record<string, number | string | string[]>} headers
*/
function setHeadersForResponse(res, headers) {
const keys = Object.keys(headers);

for (let i = 0; i < keys.length; i++) {
const key = keys[i];

setHeaderForResponse(res, key, headers[key]);
}
}

/**
* @template {ServerResponse} Response
* @param {Response} res
Expand Down Expand Up @@ -160,7 +145,7 @@
// @ts-ignore
if (typeof this.fd === "number") {
// actually close down the fd
this.close();

Check warning on line 148 in src/utils/compatibleAPI.js

View check run for this annotation

Codecov / codecov/patch

src/utils/compatibleAPI.js#L148

Added line #L148 was not covered by tests
}
},
);
Expand All @@ -182,10 +167,9 @@
* @template {ServerResponse} Response
* @param {Response} res response
* @param {number} status status
* @param {Error & { headers?: Record<string, number | string | string[]>}} err error
* @returns {void}
*/
function sendError(res, status, err) {
function sendError(res, status) {
const msg = statuses[status] || String(status);
const doc = `<!DOCTYPE html>
<html lang="en">
Expand All @@ -200,12 +184,6 @@

// Clear existing headers
clearHeadersForResponse(res);

// Add error headers
if (err && err.headers) {
setHeadersForResponse(res, err.headers);
}

// Send basic response
setStatusCode(res, status);
setHeaderForResponse(res, "Content-Type", "text/html; charset=UTF-8");
Expand Down Expand Up @@ -261,10 +239,10 @@
case "ENAMETOOLONG":
case "ENOENT":
case "ENOTDIR":
sendError(res, 404, error);
sendError(res, 404);
break;
default:
sendError(res, 500, error);
sendError(res, 500);
break;
}
});
Expand Down
Loading