diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-10-26 22:00:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-26 22:00:01 +0200 |
commit | c27ef0ac7b5fd7aba4de24292e80387c8243896e (patch) | |
tree | cb2ce5631491245d12777e62d0696580b080bef8 /ext/http | |
parent | c5a35aba82f5cb24f0ba478875e492dd9ae0524d (diff) |
perf(http): encode string bodies in op-layer (#12451)
Using serde_v8's StringOrBuffer
Diffstat (limited to 'ext/http')
-rw-r--r-- | ext/http/01_http.js | 16 | ||||
-rw-r--r-- | ext/http/lib.rs | 13 |
2 files changed, 17 insertions, 12 deletions
diff --git a/ext/http/01_http.js b/ext/http/01_http.js index 4e6b8fc00..9ce6997c6 100644 --- a/ext/http/01_http.js +++ b/ext/http/01_http.js @@ -199,11 +199,17 @@ SetPrototypeDelete(httpConn.managedResources, responseSenderRid); let responseBodyRid; try { - responseBodyRid = await core.opAsync("op_http_response", [ - responseSenderRid, - innerResp.status ?? 200, - innerResp.headerList, - ], respBody instanceof Uint8Array ? respBody : null); + responseBodyRid = await core.opAsync( + "op_http_response", + [ + responseSenderRid, + innerResp.status ?? 200, + innerResp.headerList, + ], + (respBody instanceof Uint8Array || typeof respBody === "string") + ? respBody + : null, + ); } catch (error) { const connError = httpConn[connErrorSymbol]; if (error instanceof BadResource && connError != null) { diff --git a/ext/http/lib.rs b/ext/http/lib.rs index f040ce104..ffca4fa2f 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -19,6 +19,7 @@ use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; use deno_core::ResourceId; +use deno_core::StringOrBuffer; use deno_core::ZeroCopyBuf; use hyper::body::HttpBody; use hyper::header::CONNECTION; @@ -400,7 +401,7 @@ struct RespondArgs( async fn op_http_response( state: Rc<RefCell<OpState>>, args: RespondArgs, - data: Option<ZeroCopyBuf>, + data: Option<StringOrBuffer>, ) -> Result<Option<ResourceId>, AnyError> { let RespondArgs(rid, status, headers) = args; @@ -426,15 +427,13 @@ async fn op_http_response( builder = builder.header(key.as_ref(), value.as_ref()); } - let res; - let maybe_response_body_rid = if let Some(d) = data { + let (maybe_response_body_rid, res) = if let Some(d) = data { // If a body is passed, we use it, and don't return a body for streaming. - res = builder.body(Vec::from(&*d).into())?; - None + (None, builder.body(d.into_bytes().into())?) } else { // If no body is passed, we return a writer for streaming the body. let (sender, body) = Body::channel(); - res = builder.body(body)?; + let res = builder.body(body)?; let response_body_rid = state.borrow_mut().resource_table.add(ResponseBodyResource { @@ -442,7 +441,7 @@ async fn op_http_response( conn_rid, }); - Some(response_body_rid) + (Some(response_body_rid), res) }; // oneshot::Sender::send(v) returns |v| on error, not an error object. |