Skip to content

Commit

Permalink
fix: sending response status code (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Sep 7, 2024
1 parent 238e8cc commit 49ac8ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IncomingMessage, ServerResponse } from 'node:http'
import { pipeline } from 'node:stream/promises'
import * as GetPathName from '../GetPathName/GetPathName.ts'
import * as GetResponse from '../GetResponse/GetResponse.ts'
import * as SendResponse from '../SendResponse/SendResponse.ts'

export const createHandler = (frameAncestors: string, webViewRoot: string) => {
const handleRequest = async (request: IncomingMessage, response: ServerResponse): Promise<void> => {
Expand All @@ -10,15 +10,7 @@ export const createHandler = (frameAncestors: string, webViewRoot: string) => {
pathName += 'index.html'
}
const result = await GetResponse.getResponse(pathName, frameAncestors, webViewRoot)

if (!result?.body) {
response.end('not found')
return
}
result.headers.forEach((value, key) => {
response.setHeader(key, value)
})
await pipeline(result.body, response)
await SendResponse.sendResponse(response, result)
}

return handleRequest
Expand Down
14 changes: 14 additions & 0 deletions src/parts/SendResponse/SendResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { ServerResponse } from 'node:http'
import { pipeline } from 'node:stream/promises'

export const sendResponse = async (response: ServerResponse, result: Response) => {
if (!result?.body) {
response.end('not found')
return
}
response.statusCode = result.status
result.headers.forEach((value, key) => {
response.setHeader(key, value)
})
await pipeline(result.body, response)
}

0 comments on commit 49ac8ce

Please sign in to comment.