2020-03-03 01:43:02 +07:00
|
|
|
import * as http from "http"
|
|
|
|
import { HttpProvider, HttpResponse, Route } from "../http"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static file HTTP provider. Static requests do not require authentication and
|
|
|
|
* they only allow access to resources within the application.
|
|
|
|
*/
|
|
|
|
export class StaticHttpProvider extends HttpProvider {
|
|
|
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
|
|
|
this.ensureMethod(request)
|
|
|
|
const response = await this.getReplacedResource(route)
|
|
|
|
if (!this.isDev) {
|
|
|
|
response.cache = true
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a resource with variables replaced where necessary.
|
|
|
|
*/
|
|
|
|
protected async getReplacedResource(route: Route): Promise<HttpResponse> {
|
|
|
|
// The first part is always the commit (for caching purposes).
|
|
|
|
const split = route.requestPath.split("/").slice(1)
|
|
|
|
|
|
|
|
switch (split[split.length - 1]) {
|
2020-03-03 06:44:16 +07:00
|
|
|
case "manifest.json": {
|
2020-03-03 01:43:02 +07:00
|
|
|
const response = await this.getUtf8Resource(this.rootPath, ...split)
|
|
|
|
return this.replaceTemplates(route, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.getResource(this.rootPath, ...split)
|
|
|
|
}
|
|
|
|
}
|