-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(start-server): Stop reusing http.ServerResponse for Upgrade
requests. It seems to cause the socket to hang
- Loading branch information
1 parent
1a02eb5
commit 9ac6a17
Showing
4 changed files
with
87 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {Buffer} from 'node:buffer' | ||
import {STATUS_CODES} from 'node:http' | ||
import {parseHttp1Body} from '../parse-body.js' | ||
|
||
export default function (computeResponse) { | ||
return async (nodeRequest, socket, head) => { | ||
const request = { | ||
version: nodeRequest.httpVersion, | ||
method: nodeRequest.method, | ||
url: nodeRequest.url, | ||
headers: nodeRequest.headers, | ||
body: await parseHttp1Body(nodeRequest), | ||
} | ||
|
||
const response = await computeResponse(request) | ||
|
||
socket.write( | ||
[ | ||
`HTTP/1.1 ${response.status} ${STATUS_CODES[response.status]}`, | ||
...Object.keys(response.headers ?? {}).map( | ||
(name) => `${name}: ${response.headers[name]}`, | ||
), | ||
'\r\n', | ||
].join('\r\n'), | ||
) | ||
|
||
if ( | ||
(typeof response.body === 'string' && response.body) || | ||
response.body instanceof Buffer | ||
) { | ||
socket.write(response.body) | ||
} else if (response.pipe) { | ||
response.body.pipe(socket, {end: false}) | ||
await new Promise((resolve) => { | ||
response.body.once('end', resolve) | ||
}) | ||
} | ||
|
||
if (response.status === 101) { | ||
response.upgrade(socket, head) | ||
} else { | ||
socket.end() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters