diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2021-04-23 11:34:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-23 12:34:04 +0200 |
commit | 8a416a5ba22bb8b9e6d9eb48e78a167fdea6d64c (patch) | |
tree | 6e3f495097f036702b15f39f637f299ef83b3a6c /runtime/js | |
parent | 2d722832c4382c20c2f889456affa0e9b6a58d7d (diff) |
fix(runtime/js/http): cancel body on response failure (#10225)
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/40_http.js | 65 |
1 files changed, 38 insertions, 27 deletions
diff --git a/runtime/js/40_http.js b/runtime/js/40_http.js index 67faf19cb..afc5635ac 100644 --- a/runtime/js/40_http.js +++ b/runtime/js/40_http.js @@ -136,40 +136,51 @@ respBody = new Uint8Array(0); } - const responseBodyRid = await Deno.core.opAsync("op_http_response", [ - responseSenderRid, - innerResp.status ?? 200, - innerResp.headerList, - ], respBody instanceof Uint8Array ? respBody : null); + let responseBodyRid; + try { + responseBodyRid = await Deno.core.opAsync("op_http_response", [ + responseSenderRid, + innerResp.status ?? 200, + innerResp.headerList, + ], respBody instanceof Uint8Array ? respBody : null); + } catch (error) { + if (respBody !== null && respBody instanceof ReadableStream) { + await respBody.cancel(error); + } + throw error; + } // If `respond` returns a responseBodyRid, we should stream the body // to that resource. if (responseBodyRid !== null) { - if (respBody === null || !(respBody instanceof ReadableStream)) { - throw new TypeError("Unreachable"); - } - const reader = respBody.getReader(); - while (true) { - const { value, done } = await reader.read(); - if (done) break; - if (!(value instanceof Uint8Array)) { - await reader.cancel("value not a Uint8Array"); - break; + try { + if (respBody === null || !(respBody instanceof ReadableStream)) { + throw new TypeError("Unreachable"); } - try { - await Deno.core.opAsync( - "op_http_response_write", - responseBodyRid, - value, - ); - } catch (err) { - await reader.cancel(err); - break; + const reader = respBody.getReader(); + while (true) { + const { value, done } = await reader.read(); + if (done) break; + if (!(value instanceof Uint8Array)) { + await reader.cancel(new TypeError("Value not a Uint8Array")); + break; + } + try { + await Deno.core.opAsync( + "op_http_response_write", + responseBodyRid, + value, + ); + } catch (error) { + await reader.cancel(error); + throw error; + } } + } finally { + // Once all chunks are sent, and the request body is closed, we can + // close the response body. + await Deno.core.opAsync("op_http_response_close", responseBodyRid); } - // Once all chunks are sent, and the request body is closed, we can close - // the response body. - await Deno.core.opAsync("op_http_response_close", responseBodyRid); } }; } |