1
- import { logger } from "@coder/logger"
2
1
import * as http from "http"
3
2
import * as querystring from "querystring"
4
3
import { Application } from "../../common/api"
5
4
import { HttpCode , HttpError } from "../../common/http"
6
- import { Options } from "../../common/util"
7
5
import { HttpProvider , HttpProviderOptions , HttpResponse , Route } from "../http"
8
6
import { ApiHttpProvider } from "./api"
9
7
import { UpdateHttpProvider } from "./update"
@@ -61,15 +59,7 @@ export class MainHttpProvider extends HttpProvider {
61
59
}
62
60
63
61
if ( sessionId ) {
64
- return this . getAppRoot (
65
- route ,
66
- {
67
- sessionId,
68
- base : this . base ( route ) ,
69
- logLevel : logger . level ,
70
- } ,
71
- ( app && app . name ) || "" ,
72
- )
62
+ return this . getAppRoot ( route , ( app && app . name ) || "" , sessionId )
73
63
}
74
64
75
65
return this . getErrorRoot ( route , "404" , "404" , "Application not found" )
@@ -79,12 +69,12 @@ export class MainHttpProvider extends HttpProvider {
79
69
* Return a resource with variables replaced where necessary.
80
70
*/
81
71
protected async getReplacedResource ( route : Route ) : Promise < HttpResponse > {
82
- if ( route . requestPath . endsWith ( "/manifest.json" ) ) {
83
- const response = await this . getUtf8Resource ( this . rootPath , route . requestPath )
84
- response . content = response . content
85
- . replace ( / { { BASE } } / g , this . base ( route ) )
86
- . replace ( / { { COMMIT } } / g , this . options . commit )
87
- return response
72
+ const split = route . requestPath . split ( "/" )
73
+ switch ( split [ split . length - 1 ] ) {
74
+ case "manifest.json" : {
75
+ const response = await this . getUtf8Resource ( this . rootPath , route . requestPath )
76
+ return this . replaceTemplates ( route , response )
77
+ }
88
78
}
89
79
return this . getResource ( this . rootPath , route . requestPath )
90
80
}
@@ -94,8 +84,6 @@ export class MainHttpProvider extends HttpProvider {
94
84
const apps = await this . api . installedApplications ( )
95
85
const response = await this . getUtf8Resource ( this . rootPath , "src/browser/pages/home.html" )
96
86
response . content = response . content
97
- . replace ( / { { COMMIT} } / g, this . options . commit )
98
- . replace ( / { { BASE} } / g, this . base ( route ) )
99
87
. replace ( / { { UPDATE:N A M E } } / , await this . getUpdate ( ) )
100
88
. replace ( / { { APP_ L I S T : R U N N I N G } } / , this . getAppRows ( running . applications ) )
101
89
. replace (
@@ -106,17 +94,13 @@ export class MainHttpProvider extends HttpProvider {
106
94
/ { { APP_ L I S T : O T H E R } } / ,
107
95
this . getAppRows ( apps . filter ( ( app ) => ! app . categories || ! app . categories . includes ( "Editor" ) ) ) ,
108
96
)
109
- return response
97
+ return this . replaceTemplates ( route , response )
110
98
}
111
99
112
- public async getAppRoot ( route : Route , options : Options , name : string ) : Promise < HttpResponse > {
100
+ public async getAppRoot ( route : Route , name : string , sessionId : string ) : Promise < HttpResponse > {
113
101
const response = await this . getUtf8Resource ( this . rootPath , "src/browser/pages/app.html" )
114
- response . content = response . content
115
- . replace ( / { { COMMIT} } / g, this . options . commit )
116
- . replace ( / { { BASE} } / g, this . base ( route ) )
117
- . replace ( / { { APP_ N A M E } } / , name )
118
- . replace ( / " { { O P T I O N S } } " / , `'${ JSON . stringify ( options ) } '` )
119
- return response
102
+ response . content = response . content . replace ( / { { APP_ N A M E } } / , name )
103
+ return this . replaceTemplates ( route , response , sessionId )
120
104
}
121
105
122
106
public async handleWebSocket ( ) : Promise < undefined > {
0 commit comments