diff --git a/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts b/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts index d0451991..2779ae8b 100644 --- a/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts +++ b/src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.ts @@ -4,19 +4,12 @@ import * as GetPathName from '../GetPathName/GetPathName.ts' import * as GetResponse from '../GetResponse/GetResponse.ts' export const createHandler = (frameAncestors: string, webViewRoot: string) => { - const handleRequest = async ( - request: IncomingMessage, - response: ServerResponse, - ): Promise => { + const handleRequest = async (request: IncomingMessage, response: ServerResponse): Promise => { let pathName = GetPathName.getPathName(request) if (pathName === '/') { pathName += 'index.html' } - const result = await GetResponse.getResponse( - pathName, - frameAncestors, - webViewRoot, - ) + const result = await GetResponse.getResponse(pathName, frameAncestors, webViewRoot) if (!result?.body) { response.end('not found') diff --git a/src/parts/GetResponse/GetResponse.ts b/src/parts/GetResponse/GetResponse.ts index 476f6e26..4cfc9d1c 100644 --- a/src/parts/GetResponse/GetResponse.ts +++ b/src/parts/GetResponse/GetResponse.ts @@ -3,11 +3,7 @@ import * as HandleIndexHtml from '../HandleIndexHtml/HandleIndexHtml.ts' import * as HandleOther from '../HandleOther/HandleOther.ts' import * as HandlePreviewInjected from '../HandlePreviewInjected/HandlePreviewInjected.ts' -export const getResponse = async ( - pathName: string, - frameAncestors: string, - webViewRoot: string, -): Promise => { +export const getResponse = async (pathName: string, frameAncestors: string, webViewRoot: string): Promise => { const filePath = fileURLToPath(`file://${webViewRoot}${pathName}`) const isHtml = filePath.endsWith('index.html') if (isHtml) { diff --git a/src/parts/HandleIndexHtml/HandleIndexHtml.ts b/src/parts/HandleIndexHtml/HandleIndexHtml.ts index 543fb8c9..11098a21 100644 --- a/src/parts/HandleIndexHtml/HandleIndexHtml.ts +++ b/src/parts/HandleIndexHtml/HandleIndexHtml.ts @@ -1,23 +1,9 @@ import { readFile } from 'node:fs/promises' import * as GetContentSecurityPolicy from '../GetContentSecurityPolicy/GetContentSecurityPolicy.ts' import * as GetContentType from '../GetContentType/GetContentType.ts' +import * as InjectPreviewScript from '../InjectPreviewScript/InjectPreviewScript.ts' -const injectPreviewScript = (html: string) => { - const injectedCode = - '\n' - const titleEndIndex = html.indexOf('') - const newHtml = - html.slice(0, titleEndIndex + ''.length) + - '\n' + - injectedCode + - html.slice(titleEndIndex) - return newHtml -} - -export const handleIndexHtml = async ( - filePath: string, - frameAncestors: string, -): Promise => { +export const handleIndexHtml = async (filePath: string, frameAncestors: string): Promise => { try { const csp = GetContentSecurityPolicy.getContentSecurityPolicy([ "default-src 'none'", @@ -26,7 +12,7 @@ export const handleIndexHtml = async ( ]) const contentType = GetContentType.getContentType(filePath) const content = await readFile(filePath, 'utf8') - const newContent = injectPreviewScript(content) + const newContent = InjectPreviewScript.injectPreviewScript(content) return new Response(newContent, { headers: { 'Cross-Origin-Resource-Policy': 'cross-origin', diff --git a/src/parts/InjectPreviewScript/InjectPreviewScript.ts b/src/parts/InjectPreviewScript/InjectPreviewScript.ts new file mode 100644 index 00000000..2843a2f3 --- /dev/null +++ b/src/parts/InjectPreviewScript/InjectPreviewScript.ts @@ -0,0 +1,6 @@ +export const injectPreviewScript = (html: string) => { + const injectedCode = '\n' + const titleEndIndex = html.indexOf('') + const newHtml = html.slice(0, titleEndIndex + ''.length) + '\n' + injectedCode + html.slice(titleEndIndex) + return newHtml +} diff --git a/src/parts/WebViewProtocol/WebViewProtocol.ts b/src/parts/WebViewProtocol/WebViewProtocol.ts index 690e99a5..e65810a5 100644 --- a/src/parts/WebViewProtocol/WebViewProtocol.ts +++ b/src/parts/WebViewProtocol/WebViewProtocol.ts @@ -1,8 +1,11 @@ +import { readFile } from 'node:fs/promises' import * as FileSystem from '../FileSystem/FileSystem.ts' import * as GetElectronFileResponseAbsolutePath from '../GetElectronFileResponseAbsolutePath/GetElectronFileResponseAbsolutePath.ts' import * as GetHeaders from '../GetHeaders/GetHeaders.ts' import * as HttpMethod from '../HttpMethod/HttpMethod.ts' import * as HttpStatusCode from '../HttpStatusCode/HttpStatusCode.ts' +import * as InjectPreviewScript from '../InjectPreviewScript/InjectPreviewScript.ts' +import * as PreviewInjectedCode from '../PreviewInjectedCode/PreviewInjectedCode.ts' const defaultHeaders = { 'Cross-Origin-Resource-Policy': 'cross-origin', @@ -30,6 +33,29 @@ export const getResponse = async (method: string, url: string) => { }, } } + if (absolutePath.endsWith('/index.html')) { + const content = await readFile(absolutePath, 'utf8') + const newContent = InjectPreviewScript.injectPreviewScript(content) + const headers = GetHeaders.getHeaders(absolutePath) + return { + body: newContent, + init: { + status: HttpStatusCode.Ok, + headers, + }, + } + } + if (url.endsWith('preview-injected.js')) { + const { injectedCode } = PreviewInjectedCode + const headers = GetHeaders.getHeaders('/test/file.js') + return { + body: injectedCode, + init: { + status: HttpStatusCode.Ok, + headers, + }, + } + } const content = await FileSystem.readFile(absolutePath) const headers = GetHeaders.getHeaders(absolutePath) return { diff --git a/test/WebViewProtocol.test.ts b/test/WebViewProtocol.test.ts index 38dd03fd..926eb335 100644 --- a/test/WebViewProtocol.test.ts +++ b/test/WebViewProtocol.test.ts @@ -30,16 +30,16 @@ test('method not allowed - post', async () => { }) }) -test('get', async () => { +test('get - css file', async () => { const method = HttpMethod.Get - const url = 'lvce-webview://-/test/media/' + const url = 'lvce-webview://-/test/media/index.css' jest.spyOn(FileSystem, 'readFile').mockResolvedValue(Buffer.from('a')) expect(await WebViewProtocol.getResponse(method, url)).toEqual({ body: Buffer.from('a'), init: { status: HttpStatusCode.Ok, headers: { - 'Content-Type': 'text/html', + 'Content-Type': 'text/css', 'Cross-Origin-Resource-Policy': 'cross-origin', 'Cross-Origin-Embedder-Policy': 'require-corp', },