Skip to content

Commit

Permalink
feature: move http responses into separate files (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Nov 8, 2024
1 parent f1ca4c9 commit 18dc2c4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 70 deletions.
4 changes: 4 additions & 0 deletions src/parts/DefaultHeaders/DefaultHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const defaultHeaders = {
'Cross-Origin-Resource-Policy': 'cross-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as DefaultHeaders from '../DefaultHeaders/DefaultHeaders.ts'
import * as HttpStatusCode from '../HttpStatusCode/HttpStatusCode.ts'

export const create = () => {
return {
body: `500 - Internal Server Error`,
init: {
status: HttpStatusCode.ServerError,
headers: DefaultHeaders.defaultHeaders,
},
}
}
12 changes: 12 additions & 0 deletions src/parts/NotAllowedResponse/NotAllowedResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as DefaultHeaders from '../DefaultHeaders/DefaultHeaders.ts'
import * as HttpStatusCode from '../HttpStatusCode/HttpStatusCode.ts'

export const create = () => {
return {
body: '405 - Method not allowed',
init: {
status: HttpStatusCode.MethodNotAllowed,
headers: DefaultHeaders.defaultHeaders,
},
}
}
12 changes: 12 additions & 0 deletions src/parts/NotFoundResponse/NotFoundResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as DefaultHeaders from '../DefaultHeaders/DefaultHeaders.ts'
import * as HttpStatusCode from '../HttpStatusCode/HttpStatusCode.ts'

export const create = () => {
return {
body: '404 - Not Found',
init: {
status: HttpStatusCode.NotFound,
headers: DefaultHeaders.defaultHeaders,
},
}
}
11 changes: 11 additions & 0 deletions src/parts/SuccessResponse/SuccessResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as HttpStatusCode from '../HttpStatusCode/HttpStatusCode.ts'

export const create = (body: any, headers: any) => {
return {
body,
init: {
status: HttpStatusCode.Ok,
headers,
},
}
}
84 changes: 14 additions & 70 deletions src/parts/WebViewProtocol/WebViewProtocol.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,53 @@
import { readFile } from 'node:fs/promises'
import * as ErrorCodes from '../ErrorCodes/ErrorCodes.ts'
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 ErrorCodes from '../ErrorCodes/ErrorCodes.ts'
import * as InjectPreviewScript from '../InjectPreviewScript/InjectPreviewScript.ts'
import * as InternalServerErrorResponse from '../InternalServerErrorResponse/InternalServerErrorResponse.ts'
import * as NotAllowedResponse from '../NotAllowedResponse/NotAllowedResponse.ts'
import * as NotFoundResponse from '../NotFoundResponse/NotFoundResponse.ts'
import * as PreviewInjectedCode from '../PreviewInjectedCode/PreviewInjectedCode.ts'

const defaultHeaders = {
'Cross-Origin-Resource-Policy': 'cross-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
}
import * as SuccessResponse from '../SuccessResponse/SuccessResponse.ts'

export const getResponse = async (method: string, url: string) => {
// TODO allow head requests
if (method !== HttpMethod.Get) {
return {
body: '405 - Method not allowed',
init: {
status: HttpStatusCode.MethodNotAllowed,
headers: defaultHeaders,
},
}
return NotAllowedResponse.create()
}
const absolutePath = GetElectronFileResponseAbsolutePath.getElectronFileResponseAbsolutePath(url)
if (!absolutePath) {
return {
body: '404 - Not Found',
init: {
status: HttpStatusCode.NotFound,
headers: defaultHeaders,
},
}
return NotFoundResponse.create()
}
if (absolutePath.endsWith('/index.html')) {
let content
try {
content = await readFile(absolutePath, 'utf8')
} catch (error) {
if (error && error.code === ErrorCodes.ENOENT) {
return {
body: '404 - Not Found',
init: {
status: HttpStatusCode.NotFound,
headers: defaultHeaders,
},
}
}
return {
body: `500 - Internal Server Error`,
init: {
status: HttpStatusCode.ServerError,
headers: defaultHeaders,
},
return NotFoundResponse.create()
}
return InternalServerErrorResponse.create()
}
const newContent = InjectPreviewScript.injectPreviewScript(content)
const headers = GetHeaders.getHeaders(absolutePath)
return {
body: newContent,
init: {
status: HttpStatusCode.Ok,
headers,
},
}
return SuccessResponse.create(newContent, 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,
},
}
return SuccessResponse.create(injectedCode, headers)
}
let content
try {
content = await FileSystem.readFile(absolutePath)
} catch (error) {
if (error && error.code === ErrorCodes.ENOENT) {
return {
body: '404 - Not Found',
init: {
status: HttpStatusCode.NotFound,
headers: defaultHeaders,
},
}
}
return {
body: `500 - Internal Server Error`,
init: {
status: HttpStatusCode.ServerError,
headers: defaultHeaders,
},
return NotFoundResponse.create()
}
return InternalServerErrorResponse.create()
}
const headers = GetHeaders.getHeaders(absolutePath)
return {
body: content,
init: {
status: HttpStatusCode.Ok,
headers,
},
}
return SuccessResponse.create(content, headers)
}

0 comments on commit 18dc2c4

Please sign in to comment.