-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: move http responses into separate files (#55)
- Loading branch information
Showing
6 changed files
with
65 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} |
12 changes: 12 additions & 0 deletions
12
src/parts/InternalServerErrorResponse/InternalServerErrorResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |