diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-04-27 00:58:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-27 00:58:18 +0200 |
commit | e2761df3fe2a457948948dcd38fb4f7e02cd350e (patch) | |
tree | 2924535980a53ff044b7ba9f6fc40522c1554679 /ext/http/00_serve.js | |
parent | a8b4e346b4477e340f36a59f83a0974afd541f4b (diff) |
fix(ext/http): internal upgradeHttpRaw works with "Deno.serve()" API (#18859)
Fix internal "upgradeHttpRaw" API restoring capability to upgrade HTTP
connection in polyfilles "node:http" API.
Diffstat (limited to 'ext/http/00_serve.js')
-rw-r--r-- | ext/http/00_serve.js | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index 8518e8d62..0b2c60538 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -1,6 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. const core = globalThis.Deno.core; const primordials = globalThis.__bootstrap.primordials; +const internals = globalThis.__bootstrap.internals; const { BadResourcePrototype } = core; import { InnerBody } from "ext:deno_fetch/22_body.js"; @@ -10,7 +11,7 @@ import { newInnerResponse, toInnerResponse, } from "ext:deno_fetch/23_response.js"; -import { fromInnerRequest } from "ext:deno_fetch/23_request.js"; +import { fromInnerRequest, toInnerRequest } from "ext:deno_fetch/23_request.js"; import { AbortController } from "ext:deno_web/03_abort_signal.js"; import { _eventLoop, @@ -32,6 +33,7 @@ import { readableStreamForRid, ReadableStreamPrototype, } from "ext:deno_web/06_streams.js"; +import { TcpConn } from "ext:deno_net/01_net.js"; const { ObjectPrototypeIsPrototypeOf, SafeSet, @@ -82,6 +84,14 @@ const UPGRADE_RESPONSE_SENTINEL = fromInnerResponse( "immutable", ); +function upgradeHttpRaw(req, conn) { + const inner = toInnerRequest(req); + if (inner._wantsUpgrade) { + return inner._wantsUpgrade("upgradeHttpRaw", conn); + } + throw new TypeError("upgradeHttpRaw may only be used with Deno.serve"); +} + class InnerRequest { #slabId; #context; @@ -122,10 +132,26 @@ class InnerRequest { throw "upgradeHttp is unavailable in Deno.serve at this time"; } - // upgradeHttpRaw is async - // TODO(mmastrac) + // upgradeHttpRaw is sync if (upgradeType == "upgradeHttpRaw") { - throw "upgradeHttp is unavailable in Deno.serve at this time"; + const slabId = this.#slabId; + const underlyingConn = originalArgs[0]; + + this.url(); + this.headerList; + this.close(); + + this.#upgraded = () => {}; + + const upgradeRid = core.ops.op_upgrade_raw(slabId); + + const conn = new TcpConn( + upgradeRid, + underlyingConn?.remoteAddr, + underlyingConn?.localAddr, + ); + + return { response: UPGRADE_RESPONSE_SENTINEL, conn }; } // upgradeWebSocket is sync @@ -623,4 +649,6 @@ async function serve(arg1, arg2) { } } -export { serve }; +internals.upgradeHttpRaw = upgradeHttpRaw; + +export { serve, upgradeHttpRaw }; |