Skip to content

Commit

Permalink
perf: reduce amount of JSON.stringify calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 15, 2023
1 parent cc836d0 commit e27b885
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/middleware/node/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ export async function middleware(
response.writeHead(422, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: `Request URL could not be parsed: ${request.url}`,
}),
);
response.end(`{"error":"Request URL could not be parsed: ${request.url}"}`);
return true;
}

Expand All @@ -55,9 +51,7 @@ export async function middleware(
accept: "application/json",
});
response.end(
JSON.stringify({
error: `Unsupported "Content-Type" header value. Must be "application/json"`,
}),
'{"error":"Unsupported \\"Content-Type\\" header value. Must be \\"application/json\\""}',
);
return true;
}
Expand All @@ -68,11 +62,8 @@ export async function middleware(
response.writeHead(400, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: `Required headers missing: ${missingHeaders}`,
}),
);

response.end(`{"error":"Required headers missing: ${missingHeaders}"}`);

return true;
}
Expand Down Expand Up @@ -113,18 +104,19 @@ export async function middleware(
if (didTimeout) return true;

const err = Array.from(error as WebhookEventHandlerError)[0];
const errorMessage = err.message
? `${err.name}: ${err.message}`
: "Error: An Unspecified error occurred";
response.statusCode = typeof err.status !== "undefined" ? err.status : 500;

options.log.error(error);

response.end(
JSON.stringify({
error: errorMessage,
}),
);
if (err.message) {
response.end(
JSON.stringify({
error: `${err.name}: ${err.message}`,
}),
);
} else {
response.end('{"error": "Error: An Unspecified error occurred"}');
}

return true;
}
Expand Down

0 comments on commit e27b885

Please sign in to comment.