Skip to content

Commit

Permalink
feature: move getPathName into separate file (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Aug 27, 2024
1 parent bec2993 commit c8531f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/parts/CreateWebViewServerHandler/CreateWebViewServerHandler.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { createReadStream } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { extname } from 'node:path'
import { pipeline } from 'node:stream/promises'
import { fileURLToPath } from 'node:url'
import * as GetContentSecurityPolicy from '../GetContentSecurityPolicy/GetContentSecurityPolicy.js'
import * as SetHeaders from '../SetHeaders/SetHeaders.js'
import * as GetPathName from '../GetPathName/GetPathName.js'
import * as PreviewInjectedCode from '../PreviewInjectedCode/PreviewInjectedCode.js'
import { readFile } from 'node:fs/promises'

const getPathName = (request) => {
const { pathname } = new URL(request.url || '', `https://${request.headers.host}`)
return pathname
}
import * as SetHeaders from '../SetHeaders/SetHeaders.js'

const textMimeType = {
'.html': 'text/html',
Expand Down Expand Up @@ -38,13 +34,21 @@ const getContentType = (filePath) => {
const injectPreviewScript = (html) => {
const injectedCode = `<script type="module" src="/preview-injected.js"></script>\n`
const titleEndIndex = html.indexOf('</title>')
const newHtml = html.slice(0, titleEndIndex + '</title>'.length) + '\n' + injectedCode + html.slice(titleEndIndex)
const newHtml =
html.slice(0, titleEndIndex + '</title>'.length) +
'\n' +
injectedCode +
html.slice(titleEndIndex)
return newHtml
}

const handleIndexHtml = async (response, filePath, frameAncestors) => {
try {
const csp = GetContentSecurityPolicy.getContentSecurityPolicy([`default-src 'none'`, `script-src 'self'`, `frame-ancestors ${frameAncestors}`])
const csp = GetContentSecurityPolicy.getContentSecurityPolicy([
`default-src 'none'`,
`script-src 'self'`,
`frame-ancestors ${frameAncestors}`,
])
const contentType = getContentType(filePath)
const content = await readFile(filePath, 'utf8')
SetHeaders.setHeaders(response, {
Expand Down Expand Up @@ -92,7 +96,7 @@ const handlePreviewInjected = (response) => {

export const createHandler = (frameAncestors, webViewRoot) => {
const handleRequest = async (request, response) => {
let pathName = getPathName(request)
let pathName = GetPathName.getPathName(request)
if (pathName === '/') {
pathName += 'index.html'
}
Expand Down
7 changes: 7 additions & 0 deletions src/parts/GetPathName/GetPathName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getPathName = (request) => {
const { pathname } = new URL(
request.url || '',
`https://${request.headers.host}`,
)
return pathname
}

0 comments on commit c8531f0

Please sign in to comment.