diff options
author | 2022-10-19 00:18:00 -0700 | |
---|---|---|
committer | 2022-10-19 00:18:00 -0700 | |
commit | 87ca9948ec999c1f60129fa033639cfb151993a6 (patch) | |
tree | 988e1c392d437dec0780cad07c3ea6f1b29fb7cf /src | |
parent | 57e5c35277479c672f4da55319ce538c38321f59 (diff) | |
download | bun-87ca9948ec999c1f60129fa033639cfb151993a6.tar.gz bun-87ca9948ec999c1f60129fa033639cfb151993a6.tar.zst bun-87ca9948ec999c1f60129fa033639cfb151993a6.zip |
Allow returning a Response object when upgrading
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/api/server.zig | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 0adaf8b3b..ce0b388d7 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -658,6 +658,13 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp ctx.finalizeForAbort(); return; } + + // I don't think this case happens? + if (ctx.didUpgradeWebSocket()) { + ctx.finalize(); + return; + } + if (!ctx.resp.hasResponded()) { ctx.renderMissing(); return; @@ -1438,15 +1445,16 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp return; } + // if you return a Response object or a Promise<Response> + // but you upgraded the connection to a WebSocket + // just ignore the Response object. It doesn't do anything. + // it's better to do that than to throw an error if (ctx.didUpgradeWebSocket()) { ctx.finalize(); return; } if (response_value.isEmptyOrUndefinedOrNull()) { - if (ctx.didUpgradeWebSocket()) - return; - ctx.renderMissing(); return; } @@ -1489,10 +1497,17 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp .Pending => {}, .Fulfilled => { const fulfilled_value = promise.result(vm.global.vm()); - if (fulfilled_value.isEmptyOrUndefinedOrNull()) { - if (ctx.didUpgradeWebSocket()) - return; + // if you return a Response object or a Promise<Response> + // but you upgraded the connection to a WebSocket + // just ignore the Response object. It doesn't do anything. + // it's better to do that than to throw an error + if (ctx.didUpgradeWebSocket()) { + ctx.finalize(); + return; + } + + if (fulfilled_value.isEmptyOrUndefinedOrNull()) { ctx.renderMissing(); return; } |