diff options
Diffstat (limited to 'runtime/js/40_http.js')
-rw-r--r-- | runtime/js/40_http.js | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/runtime/js/40_http.js b/runtime/js/40_http.js index dd6c99a2d..d9dff7b52 100644 --- a/runtime/js/40_http.js +++ b/runtime/js/40_http.js @@ -9,15 +9,6 @@ const core = window.Deno.core; const { ReadableStream } = window.__bootstrap.streams; - function flatEntries(obj) { - const entries = []; - for (const key in obj) { - entries.push(key); - entries.push(obj[key]); - } - return entries; - } - function serveHttp(conn) { const rid = Deno.core.jsonOpSync("op_http_start", conn.rid); return new HttpConn(rid); @@ -104,12 +95,14 @@ ); } - function respond(responseSenderRid, resp, zeroCopyBuf) { - return Deno.core.jsonOpSync("op_http_response", [ - responseSenderRid, - resp.status ?? 200, - flatEntries(resp.headers ?? {}), - ], zeroCopyBuf); + /** IMPORTANT: Equivalent to `Array.from(headers).flat()` but more performant. + * Please preserve. */ + function flattenHeaders(headers) { + const array = []; + for (const pair of headers) { + array.push(pair[0], pair[1]); + } + return array; } function createRespondWith(responseSenderRid, connRid) { @@ -136,11 +129,11 @@ zeroCopyBuf = null; } - const responseBodyRid = respond( + const responseBodyRid = Deno.core.jsonOpSync("op_http_response", [ responseSenderRid, - resp, - zeroCopyBuf, - ); + resp.status ?? 200, + flattenHeaders(resp.headers), + ], zeroCopyBuf); // If `respond` returns a responseBodyRid, we should stream the body // to that resource. |