140 lines
3.9 KiB
TypeScript
140 lines
3.9 KiB
TypeScript
import { createReadStream } from 'fs'
|
|
import { stat } from 'fs/promises'
|
|
import type { IncomingMessage, RequestListener, ServerResponse } from 'http'
|
|
import { extname, isAbsolute, posix, relative, resolve } from 'path'
|
|
|
|
const STATIC_WEB_ALLOWED_PATHS = new Set(['/web-index.html'])
|
|
const STATIC_WEB_ALLOWED_PREFIXES = ['/assets/']
|
|
const STATIC_WEB_CONTENT_TYPES = new Map([
|
|
['.css', 'text/css; charset=utf-8'],
|
|
['.html', 'text/html; charset=utf-8'],
|
|
['.js', 'text/javascript; charset=utf-8'],
|
|
['.json', 'application/json; charset=utf-8'],
|
|
['.png', 'image/png'],
|
|
['.svg', 'image/svg+xml; charset=utf-8'],
|
|
['.wasm', 'application/wasm'],
|
|
['.webp', 'image/webp'],
|
|
['.woff2', 'font/woff2']
|
|
])
|
|
|
|
export function createStaticWebClientHandler(staticRoot: string): RequestListener {
|
|
const resolvedRoot = resolve(staticRoot)
|
|
return (request, response) => {
|
|
void handleStaticRequest(resolvedRoot, request, response)
|
|
}
|
|
}
|
|
|
|
async function handleStaticRequest(
|
|
staticRoot: string,
|
|
request: IncomingMessage,
|
|
response: ServerResponse
|
|
): Promise<void> {
|
|
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
response.setHeader('Allow', 'GET, HEAD')
|
|
writeHttpStatus(response, 405)
|
|
return
|
|
}
|
|
|
|
const pathname = parseStaticPathname(request.url)
|
|
if (!pathname) {
|
|
writeHttpStatus(response, 400)
|
|
return
|
|
}
|
|
if (!isAllowedStaticWebPath(pathname)) {
|
|
writeHttpStatus(response, 404)
|
|
return
|
|
}
|
|
|
|
const absolutePath = resolve(staticRoot, pathname.slice(1))
|
|
const relativePath = relative(staticRoot, absolutePath)
|
|
if (relativePath === '' || relativePath.startsWith('..') || isAbsolute(relativePath)) {
|
|
writeHttpStatus(response, 404)
|
|
return
|
|
}
|
|
|
|
let fileStat
|
|
try {
|
|
fileStat = await stat(absolutePath)
|
|
} catch {
|
|
writeHttpStatus(response, 404)
|
|
return
|
|
}
|
|
if (!fileStat.isFile()) {
|
|
writeHttpStatus(response, 404)
|
|
return
|
|
}
|
|
|
|
response.statusCode = 200
|
|
response.setHeader(
|
|
'Content-Type',
|
|
STATIC_WEB_CONTENT_TYPES.get(extname(absolutePath)) ?? 'application/octet-stream'
|
|
)
|
|
response.setHeader('Content-Length', fileStat.size)
|
|
response.setHeader(
|
|
'Cache-Control',
|
|
pathname.startsWith('/assets/') ? 'public, max-age=31536000, immutable' : 'no-cache'
|
|
)
|
|
if (request.method === 'HEAD') {
|
|
response.end()
|
|
return
|
|
}
|
|
|
|
const stream = createReadStream(absolutePath)
|
|
stream.on('error', () => {
|
|
if (!response.headersSent) {
|
|
writeHttpStatus(response, 500)
|
|
return
|
|
}
|
|
response.destroy()
|
|
})
|
|
stream.pipe(response)
|
|
}
|
|
|
|
function parseStaticPathname(rawUrl: string | undefined): string | null {
|
|
if (!rawUrl) {
|
|
return '/web-index.html'
|
|
}
|
|
let pathname: string
|
|
try {
|
|
pathname = decodeURIComponent(new URL(rawUrl, 'http://127.0.0.1').pathname)
|
|
} catch {
|
|
return null
|
|
}
|
|
if (pathname === '/' || pathname === '/index.html') {
|
|
return '/web-index.html'
|
|
}
|
|
if (pathname.includes('\0') || pathname.includes('\\') || pathname.split('/').includes('..')) {
|
|
return null
|
|
}
|
|
if (posix.normalize(pathname) !== pathname) {
|
|
return null
|
|
}
|
|
return mapProxyPrefixedStaticPathname(pathname)
|
|
}
|
|
|
|
function mapProxyPrefixedStaticPathname(pathname: string): string {
|
|
if (pathname === '/web-index.html' || pathname.endsWith('/web-index.html')) {
|
|
return '/web-index.html'
|
|
}
|
|
const assetMarker = '/assets/'
|
|
const assetIndex = pathname.indexOf(assetMarker)
|
|
if (assetIndex !== -1) {
|
|
// Why: reverse proxies may forward the external path prefix through to
|
|
// Orca. Only the bundled /assets subtree is served after the prefix.
|
|
return pathname.slice(assetIndex)
|
|
}
|
|
return pathname
|
|
}
|
|
|
|
function isAllowedStaticWebPath(pathname: string): boolean {
|
|
return (
|
|
STATIC_WEB_ALLOWED_PATHS.has(pathname) ||
|
|
STATIC_WEB_ALLOWED_PREFIXES.some((prefix) => pathname.startsWith(prefix))
|
|
)
|
|
}
|
|
|
|
function writeHttpStatus(response: ServerResponse, statusCode: number): void {
|
|
response.statusCode = statusCode
|
|
response.end()
|
|
}
|