From 49ac8ce65ed3fa5769edc8fb78c8c06dddcc3f13 Mon Sep 17 00:00:00 2001 From: Le Vivilet <72156503+levivilet@users.noreply.github.com> Date: Sat, 7 Sep 2024 21:31:57 +0200 Subject: [PATCH] fix: sending response status code (#34) --- .../CreateWebViewServerHandler.ts | 12 ++---------- src/parts/SendResponse/SendResponse.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 src/parts/SendResponse/SendResponse.ts diff --git a/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts b/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts index 2779ae8b..961ff8c9 100644 --- a/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts +++ b/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts @@ -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 => { @@ -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 diff --git a/src/parts/SendResponse/SendResponse.ts b/src/parts/SendResponse/SendResponse.ts new file mode 100644 index 00000000..5b2b2bf1 --- /dev/null +++ b/src/parts/SendResponse/SendResponse.ts @@ -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) +}