Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: more readable #132

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ const responseViaResponseObject = async (
res: Response | Promise<Response>,
outgoing: ServerResponse | Http2ServerResponse
) => {
if (res instanceof Promise) {
res = await res.catch(handleFetchError)
}
res = res instanceof Promise ? await res.catch(handleFetchError) : res

try {
if (cacheKey in res) {
const isCached = cacheKey in res
if (isCached) {
return responseViaCache(res as Response, outgoing)
}
} catch (e: unknown) {
Expand All @@ -77,20 +76,30 @@ const responseViaResponseObject = async (
* Else if content-type is not application/json nor text/* but can be text/event-stream,
* we assume that the response should be streamed.
*/

const {
'transfer-encoding': transferEncoding,
'content-encoding': contentEncoding,
'content-length': contentLength,
'x-accel-buffering': accelBuffering,
'content-type': contentType,
} = resHeaderRecord

if (
resHeaderRecord['transfer-encoding'] ||
resHeaderRecord['content-encoding'] ||
resHeaderRecord['content-length'] ||
transferEncoding ||
contentEncoding ||
contentLength ||
// nginx buffering variant
(resHeaderRecord['x-accel-buffering'] &&
regBuffer.test(resHeaderRecord['x-accel-buffering'] as string)) ||
!regContentType.test(resHeaderRecord['content-type'] as string)
(accelBuffering && regBuffer.test(accelBuffering as string)) ||
!regContentType.test(contentType as string)
) {
outgoing.writeHead(res.status, resHeaderRecord)

await writeFromReadableStream(res.body, outgoing)
} else {
const buffer = await res.arrayBuffer()
resHeaderRecord['content-length'] = buffer.byteLength
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I don't need to use the contentLength variable here
Because the content-length in resHeaderRecord should have changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed.


outgoing.writeHead(res.status, resHeaderRecord)
outgoing.end(new Uint8Array(buffer))
}
Expand All @@ -115,7 +124,9 @@ export const getRequestListener = (fetchCallback: FetchCallback) => {
const req = newRequest(incoming)

try {
res = fetchCallback(req, { incoming, outgoing } as HttpBindings) as Response | Promise<Response>
res = fetchCallback(req, { incoming, outgoing } as HttpBindings) as
| Response
| Promise<Response>
if (cacheKey in res) {
// synchronous, cacheable response
return responseViaCache(res as Response, outgoing)
Expand Down
Loading