Register a service worker
To make installing as a PWA possible. Fixes #1181.
This commit is contained in:
@ -1,9 +1,7 @@
|
||||
import { logger } from "@coder/logger"
|
||||
import * as http from "http"
|
||||
import * as querystring from "querystring"
|
||||
import { Application } from "../../common/api"
|
||||
import { HttpCode, HttpError } from "../../common/http"
|
||||
import { Options } from "../../common/util"
|
||||
import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
|
||||
import { ApiHttpProvider } from "./api"
|
||||
import { UpdateHttpProvider } from "./update"
|
||||
@ -61,15 +59,7 @@ export class MainHttpProvider extends HttpProvider {
|
||||
}
|
||||
|
||||
if (sessionId) {
|
||||
return this.getAppRoot(
|
||||
route,
|
||||
{
|
||||
sessionId,
|
||||
base: this.base(route),
|
||||
logLevel: logger.level,
|
||||
},
|
||||
(app && app.name) || "",
|
||||
)
|
||||
return this.getAppRoot(route, (app && app.name) || "", sessionId)
|
||||
}
|
||||
|
||||
return this.getErrorRoot(route, "404", "404", "Application not found")
|
||||
@ -79,12 +69,12 @@ export class MainHttpProvider extends HttpProvider {
|
||||
* Return a resource with variables replaced where necessary.
|
||||
*/
|
||||
protected async getReplacedResource(route: Route): Promise<HttpResponse> {
|
||||
if (route.requestPath.endsWith("/manifest.json")) {
|
||||
const response = await this.getUtf8Resource(this.rootPath, route.requestPath)
|
||||
response.content = response.content
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/{{COMMIT}}/g, this.options.commit)
|
||||
return response
|
||||
const split = route.requestPath.split("/")
|
||||
switch (split[split.length - 1]) {
|
||||
case "manifest.json": {
|
||||
const response = await this.getUtf8Resource(this.rootPath, route.requestPath)
|
||||
return this.replaceTemplates(route, response)
|
||||
}
|
||||
}
|
||||
return this.getResource(this.rootPath, route.requestPath)
|
||||
}
|
||||
@ -94,8 +84,6 @@ export class MainHttpProvider extends HttpProvider {
|
||||
const apps = await this.api.installedApplications()
|
||||
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/home.html")
|
||||
response.content = response.content
|
||||
.replace(/{{COMMIT}}/g, this.options.commit)
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/{{UPDATE:NAME}}/, await this.getUpdate())
|
||||
.replace(/{{APP_LIST:RUNNING}}/, this.getAppRows(running.applications))
|
||||
.replace(
|
||||
@ -106,17 +94,13 @@ export class MainHttpProvider extends HttpProvider {
|
||||
/{{APP_LIST:OTHER}}/,
|
||||
this.getAppRows(apps.filter((app) => !app.categories || !app.categories.includes("Editor"))),
|
||||
)
|
||||
return response
|
||||
return this.replaceTemplates(route, response)
|
||||
}
|
||||
|
||||
public async getAppRoot(route: Route, options: Options, name: string): Promise<HttpResponse> {
|
||||
public async getAppRoot(route: Route, name: string, sessionId: string): Promise<HttpResponse> {
|
||||
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/app.html")
|
||||
response.content = response.content
|
||||
.replace(/{{COMMIT}}/g, this.options.commit)
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/{{APP_NAME}}/, name)
|
||||
.replace(/"{{OPTIONS}}"/, `'${JSON.stringify(options)}'`)
|
||||
return response
|
||||
response.content = response.content.replace(/{{APP_NAME}}/, name)
|
||||
return this.replaceTemplates(route, response, sessionId)
|
||||
}
|
||||
|
||||
public async handleWebSocket(): Promise<undefined> {
|
||||
|
@ -48,11 +48,9 @@ export class LoginHttpProvider extends HttpProvider {
|
||||
public async getRoot(route: Route, value?: string, error?: Error): Promise<HttpResponse> {
|
||||
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/login.html")
|
||||
response.content = response.content
|
||||
.replace(/{{COMMIT}}/g, this.options.commit)
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/{{VALUE}}/, value || "")
|
||||
.replace(/{{ERROR}}/, error ? `<div class="error">${error.message}</div>` : "")
|
||||
return response
|
||||
return this.replaceTemplates(route, response)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,11 +86,9 @@ export class UpdateHttpProvider extends HttpProvider {
|
||||
public async getRoot(route: Route, error?: Error): Promise<HttpResponse> {
|
||||
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/update.html")
|
||||
response.content = response.content
|
||||
.replace(/{{COMMIT}}/g, this.options.commit)
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/{{UPDATE_STATUS}}/, await this.getUpdateHtml())
|
||||
.replace(/{{ERROR}}/, error ? `<div class="error">${error.message}</div>` : "")
|
||||
return response
|
||||
return this.replaceTemplates(route, response)
|
||||
}
|
||||
|
||||
public async handleWebSocket(): Promise<undefined> {
|
||||
|
@ -219,17 +219,13 @@ export class VscodeHttpProvider extends HttpProvider {
|
||||
response.content = response.content.replace(/<!-- PROD_ONLY/g, "").replace(/END_PROD_ONLY -->/g, "")
|
||||
}
|
||||
|
||||
return {
|
||||
...response,
|
||||
content: response.content
|
||||
.replace(/{{COMMIT}}/g, options.commit)
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/{{VS_BASE}}/g, this.base(route) + this.options.base)
|
||||
.replace(`"{{REMOTE_USER_DATA_URI}}"`, `'${JSON.stringify(options.remoteUserDataUri)}'`)
|
||||
.replace(`"{{PRODUCT_CONFIGURATION}}"`, `'${JSON.stringify(options.productConfiguration)}'`)
|
||||
.replace(`"{{WORKBENCH_WEB_CONFIGURATION}}"`, `'${JSON.stringify(options.workbenchWebConfiguration)}'`)
|
||||
.replace(`"{{NLS_CONFIGURATION}}"`, `'${JSON.stringify(options.nlsConfiguration)}'`),
|
||||
}
|
||||
response.content = response.content
|
||||
.replace(/{{VS_BASE}}/g, this.base(route) + this.options.base)
|
||||
.replace(`"{{REMOTE_USER_DATA_URI}}"`, `'${JSON.stringify(options.remoteUserDataUri)}'`)
|
||||
.replace(`"{{PRODUCT_CONFIGURATION}}"`, `'${JSON.stringify(options.productConfiguration)}'`)
|
||||
.replace(`"{{WORKBENCH_WEB_CONFIGURATION}}"`, `'${JSON.stringify(options.workbenchWebConfiguration)}'`)
|
||||
.replace(`"{{NLS_CONFIGURATION}}"`, `'${JSON.stringify(options.nlsConfiguration)}'`)
|
||||
return this.replaceTemplates(route, response)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,7 +12,7 @@ import * as tarFs from "tar-fs"
|
||||
import * as tls from "tls"
|
||||
import * as url from "url"
|
||||
import { HttpCode, HttpError } from "../common/http"
|
||||
import { normalize, plural, split } from "../common/util"
|
||||
import { normalize, Options, plural, split } from "../common/util"
|
||||
import { SocketProxyProvider } from "./socket"
|
||||
import { getMediaMime, xdgLocalDir } from "./util"
|
||||
|
||||
@ -165,14 +165,36 @@ export abstract class HttpProvider {
|
||||
return normalize("./" + (depth > 1 ? "../".repeat(depth - 1) : ""))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get error response.
|
||||
*/
|
||||
public async getErrorRoot(route: Route, title: string, header: string, body: string): Promise<HttpResponse> {
|
||||
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/error.html")
|
||||
response.content = response.content
|
||||
.replace(/{{COMMIT}}/g, this.options.commit)
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/{{ERROR_TITLE}}/g, title)
|
||||
.replace(/{{ERROR_HEADER}}/g, header)
|
||||
.replace(/{{ERROR_BODY}}/g, body)
|
||||
return this.replaceTemplates(route, response)
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace common templates strings.
|
||||
*/
|
||||
protected replaceTemplates(
|
||||
route: Route,
|
||||
response: HttpStringFileResponse,
|
||||
sessionId?: string,
|
||||
): HttpStringFileResponse {
|
||||
const options: Options = {
|
||||
base: this.base(route),
|
||||
commit: this.options.commit,
|
||||
logLevel: logger.level,
|
||||
sessionId,
|
||||
}
|
||||
response.content = response.content
|
||||
.replace(/{{COMMIT}}/g, this.options.commit)
|
||||
.replace(/{{BASE}}/g, this.base(route))
|
||||
.replace(/"{{OPTIONS}}"/, `'${JSON.stringify(options)}'`)
|
||||
return response
|
||||
}
|
||||
|
||||
@ -338,11 +360,15 @@ export class Heart {
|
||||
clearTimeout(this.heartbeatTimer)
|
||||
}
|
||||
this.heartbeatTimer = setTimeout(() => {
|
||||
this.isActive().then((active) => {
|
||||
if (active) {
|
||||
this.beat()
|
||||
}
|
||||
})
|
||||
this.isActive()
|
||||
.then((active) => {
|
||||
if (active) {
|
||||
this.beat()
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.warn(error.message)
|
||||
})
|
||||
}, this.heartbeatInterval)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user