Archived
1
0

Generalize initial app logic

This commit is contained in:
Asher
2020-02-05 18:47:00 -06:00
parent 205775ac97
commit 6cebfa469d
9 changed files with 78 additions and 57 deletions

View File

@ -3,6 +3,7 @@ import * as http from "http"
import * as net from "net"
import * as ws from "ws"
import {
Application,
ApplicationsResponse,
ClientMessage,
FilesResponse,
@ -15,6 +16,12 @@ import { normalize } from "../../common/util"
import { HttpProvider, HttpProviderOptions, HttpResponse, HttpServer, Route } from "../http"
import { hash } from "../util"
export const Vscode: Application = {
name: "VS Code",
path: "/",
embedPath: "./vscode-embed",
}
/**
* API HTTP provider.
*/
@ -104,6 +111,8 @@ export class ApiHttpProvider extends HttpProvider {
return {
content: {
success: true,
// TEMP: Auto-load VS Code.
app: Vscode,
},
cookie:
typeof password === "string"
@ -149,13 +158,7 @@ export class ApiHttpProvider extends HttpProvider {
private async applications(): Promise<HttpResponse<ApplicationsResponse>> {
return {
content: {
applications: [
{
name: "VS Code",
path: "/vscode",
embedPath: "/vscode-embed",
},
],
applications: [Vscode],
},
}
}

View File

@ -5,6 +5,7 @@ import * as ReactDOMServer from "react-dom/server"
import App from "../../browser/app"
import { HttpCode, HttpError } from "../../common/http"
import { Options } from "../../common/util"
import { Vscode } from "../api/server"
import { HttpProvider, HttpResponse, Route } from "../http"
/**
@ -21,39 +22,40 @@ export class MainHttpProvider extends HttpProvider {
return response
}
case "/vscode":
case "/": {
if (route.requestPath !== "/index.html") {
throw new HttpError("Not found", HttpCode.NotFound)
}
const options: Options = {
authed: !!this.authenticated(request),
basePath: this.base(route),
logLevel: logger.level,
}
if (options.authed) {
// TEMP: Auto-load VS Code for now. In future versions we'll need to check
// the URL for the appropriate application to load, if any.
options.app = {
name: "VS Code",
path: "/",
embedPath: "/vscode-embed",
}
// TODO: Load other apps based on the URL as well.
if (route.base === Vscode.path && options.authed) {
options.app = Vscode
}
const response = await this.getUtf8Resource(this.rootPath, "src/browser/index.html")
response.content = response.content
.replace(/{{COMMIT}}/g, this.options.commit)
.replace(/{{BASE}}/g, this.base(route))
.replace(/"{{OPTIONS}}"/g, `'${JSON.stringify(options)}'`)
.replace(/{{COMPONENT}}/g, ReactDOMServer.renderToString(<App options={options} />))
return response
return this.getRoot(route, options)
}
}
return undefined
}
public async getRoot(route: Route, options: Options): Promise<HttpResponse> {
const response = await this.getUtf8Resource(this.rootPath, "src/browser/index.html")
response.content = response.content
.replace(/{{COMMIT}}/g, this.options.commit)
.replace(/{{BASE}}/g, this.base(route))
.replace(/"{{OPTIONS}}"/g, `'${JSON.stringify(options)}'`)
.replace(/{{COMPONENT}}/g, ReactDOMServer.renderToString(<App options={options} />))
return response
}
public async handleWebSocket(): Promise<undefined> {
return undefined
}

View File

@ -116,7 +116,6 @@ interface ProviderRoute extends Route {
}
export interface HttpProviderOptions {
readonly base: string
readonly auth: AuthType
readonly password?: string
readonly commit: string
@ -154,7 +153,7 @@ export abstract class HttpProvider {
* Get the base relative to the provided route.
*/
public base(route: Route): string {
const depth = route.fullPath ? (route.fullPath.match(/\//g) || []).length : 1
const depth = ((route.fullPath + "/").match(/\//g) || []).length
return normalize("./" + (depth > 1 ? "../".repeat(depth - 1) : ""))
}
@ -404,7 +403,6 @@ export class HttpServer {
new provider(
{
auth: this.options.auth || AuthType.None,
base: endpoint,
commit: this.options.commit,
password: this.options.password,
},