diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2022-10-29 18:25:23 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-29 18:25:23 +0900 |
commit | 59ac110edd1f376bed7fa6bbdbe2ee09c266bf74 (patch) | |
tree | da654d1ecb6141b620141d634d99ca34c6d568db /ext/flash/01_http.js | |
parent | edaceecec771cf0395639175b5a21d20530f6080 (diff) |
fix(core): fix APIs not to be affected by `Promise.prototype.then` modification (#16326)
Diffstat (limited to 'ext/flash/01_http.js')
-rw-r--r-- | ext/flash/01_http.js | 85 |
1 files changed, 58 insertions, 27 deletions
diff --git a/ext/flash/01_http.js b/ext/flash/01_http.js index 5e6cb69aa..df013ce65 100644 --- a/ext/flash/01_http.js +++ b/ext/flash/01_http.js @@ -29,11 +29,13 @@ const { Function, ObjectPrototypeIsPrototypeOf, - PromiseAll, + Promise, + PromisePrototypeCatch, + PromisePrototypeThen, + SafePromiseAll, TypedArrayPrototypeSubarray, TypeError, Uint8Array, - Promise, Uint8ArrayPrototype, } = window.__bootstrap.primordials; @@ -342,24 +344,27 @@ } const reader = respBody.getReader(); // Aquire JS lock. try { - core.opAsync( - "op_flash_write_resource", - http1Response( - method, - innerResp.status ?? 200, - innerResp.headerList, - 0, // Content-Length will be set by the op. - null, - true, + PromisePrototypeThen( + core.opAsync( + "op_flash_write_resource", + http1Response( + method, + innerResp.status ?? 200, + innerResp.headerList, + 0, // Content-Length will be set by the op. + null, + true, + ), + serverId, + i, + resourceBacking.rid, + resourceBacking.autoClose, ), - serverId, - i, - resourceBacking.rid, - resourceBacking.autoClose, - ).then(() => { - // Release JS lock. - readableStreamClose(respBody); - }); + () => { + // Release JS lock. + readableStreamClose(respBody); + }, + ); } catch (error) { await reader.cancel(error); throw error; @@ -486,10 +491,16 @@ const serverId = core.ops.op_flash_serve(listenOpts); const serverPromise = core.opAsync("op_flash_drive_server", serverId); - core.opAsync("op_flash_wait_for_listening", serverId).then((port) => { - onListen({ hostname: listenOpts.hostname, port }); - }).catch(() => {}); - const finishedPromise = serverPromise.catch(() => {}); + PromisePrototypeCatch( + PromisePrototypeThen( + core.opAsync("op_flash_wait_for_listening", serverId), + (port) => { + onListen({ hostname: listenOpts.hostname, port }); + }, + ), + () => {}, + ); + const finishedPromise = PromisePrototypeCatch(serverPromise, () => {}); const server = { id: serverId, @@ -554,7 +565,27 @@ let resp; try { resp = handler(req); - if (resp instanceof Promise || typeof resp?.then === "function") { + if (resp instanceof Promise) { + PromisePrototypeCatch( + PromisePrototypeThen( + resp, + (resp) => + handleResponse( + req, + resp, + body, + hasBody, + method, + serverId, + i, + respondFast, + respondChunked, + ), + ), + onError, + ); + continue; + } else if (typeof resp?.then === "function") { resp.then((resp) => handleResponse( req, @@ -595,7 +626,7 @@ signal?.addEventListener("abort", () => { clearInterval(dateInterval); - server.close().then(() => {}, () => {}); + PromisePrototypeThen(server.close(), () => {}, () => {}); }, { once: true, }); @@ -638,8 +669,8 @@ }, 1000); } - await PromiseAll([ - server.serve().catch(console.error), + await SafePromiseAll([ + PromisePrototypeCatch(server.serve(), console.error), serverPromise, ]); } |