1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 13:00:36 -05:00

perf(ext/http): optimize inferrable content-type responses

This commit is contained in:
Divy Srivastava 2024-10-18 19:07:48 +05:30
parent c77c9b2958
commit cf6f921df5
3 changed files with 31 additions and 1 deletions

View file

@ -122,6 +122,7 @@ function cloneInnerResponse(response) {
body,
headerList,
urlList,
canInferContentType: response.canInferContentType,
status: response.status,
statusMessage: response.statusMessage,
aborted: response.aborted,
@ -142,6 +143,7 @@ function newInnerResponse(status = 200, statusMessage = "") {
headerList: [],
urlList: [],
status,
canInferContentType: false,
statusMessage,
aborted: false,
url() {
@ -178,6 +180,8 @@ function abortedNetworkError() {
* @param {{ body: fetchBody.InnerBody, contentType: string | null } | null} bodyWithType
*/
function initializeAResponse(response, init, bodyWithType) {
let canInferContentType = false;
// 1.
if ((init.status < 200 || init.status > 599) && init.status != 101) {
throw new RangeError(
@ -228,10 +232,14 @@ function initializeAResponse(response, init, bodyWithType) {
}
}
if (!hasContentType) {
// keep case in sync with `extractBody`
canInferContentType = contentType === "text/plain;charset=UTF-8";
ArrayPrototypePush(list, ["Content-Type", contentType]);
}
}
}
return canInferContentType;
}
class Response {
@ -335,7 +343,11 @@ class Response {
if (body !== null) {
bodyWithType = extractBody(body);
}
initializeAResponse(this, init, bodyWithType);
this[_response].canInferContentType = initializeAResponse(
this,
init,
bodyWithType,
);
this[_brand] = _brand;
}

View file

@ -564,6 +564,12 @@ function mapToCallback(context, callback, onError) {
const headers = inner.headerList;
if (headers && headers.length > 0) {
if (headers.length == 1) {
if (
inner.canInferContentType &&
headers[0][0] === "Content-Type"
) {
continue;
}
op_http_set_response_header(req, headers[0][0], headers[0][1]);
} else {
op_http_set_response_headers(req, headers);

View file

@ -739,7 +739,19 @@ pub fn op_http_set_response_body_text(
let http =
// SAFETY: external is deleted before calling this op.
unsafe { take_external!(external, "op_http_set_response_body_text") };
if !text.is_empty() {
{
let mut response_parts = http.response_parts();
if !response_parts.headers.contains_key(CONTENT_TYPE) {
response_parts.headers.append(
CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
);
}
}
// set content-type to text/plain; charset=utf-8 if not set
set_response(http, Some(text.len()), status, false, |compression| {
ResponseBytesInner::from_vec(compression, text.into_bytes())
});