refactor: write route.query via settings.write
I added a shallow parameter, because the query should not be extends, but should be replaced directly.
This commit is contained in:
parent
5ba650bb6f
commit
b6e791f7d0
@ -134,7 +134,6 @@ export class VscodeHttpProvider extends HttpProvider {
|
|||||||
return { redirect: "/login", query: { to: this.options.base } }
|
return { redirect: "/login", query: { to: this.options.base } }
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
this.persistRouteQuery(request, route)
|
|
||||||
return await this.getRoot(request, route)
|
return await this.getRoot(request, route)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = `<div>VS Code failed to load.</div> ${
|
const message = `<div>VS Code failed to load.</div> ${
|
||||||
@ -166,13 +165,6 @@ export class VscodeHttpProvider extends HttpProvider {
|
|||||||
throw new HttpError("Not found", HttpCode.NotFound)
|
throw new HttpError("Not found", HttpCode.NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
private persistRouteQuery(request: http.IncomingMessage, route: Route): void {
|
|
||||||
const content = Object.keys(route.query).reduce((content, next) => {
|
|
||||||
return (content += `${next}=${route.query[next]}\n`)
|
|
||||||
}, "")
|
|
||||||
fs.writeFile(path.resolve(paths.data, "query"), content)
|
|
||||||
}
|
|
||||||
|
|
||||||
private async getRoot(request: http.IncomingMessage, route: Route): Promise<HttpResponse> {
|
private async getRoot(request: http.IncomingMessage, route: Route): Promise<HttpResponse> {
|
||||||
const remoteAuthority = request.headers.host as string
|
const remoteAuthority = request.headers.host as string
|
||||||
const { lastVisited } = await settings.read()
|
const { lastVisited } = await settings.read()
|
||||||
@ -191,11 +183,15 @@ export class VscodeHttpProvider extends HttpProvider {
|
|||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
let promise = Promise.resolve()
|
||||||
if (startPath) {
|
if (startPath) {
|
||||||
settings.write({
|
promise = settings.write({ lastVisited: startPath })
|
||||||
lastVisited: startPath,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
// `settings.write` depends on `settings.read` internally. To avoid race conditions, a promise is added here to synchronize.
|
||||||
|
promise.then(() => {
|
||||||
|
// the query should not be extends, but should be replaced directly.
|
||||||
|
settings.write({ query: route.query }, true)
|
||||||
|
})
|
||||||
|
|
||||||
if (!this.isDev) {
|
if (!this.isDev) {
|
||||||
response.content = response.content.replace(/<!-- PROD_ONLY/g, "").replace(/END_PROD_ONLY -->/g, "")
|
response.content = response.content.replace(/<!-- PROD_ONLY/g, "").replace(/END_PROD_ONLY -->/g, "")
|
||||||
|
@ -2,6 +2,7 @@ import * as fs from "fs-extra"
|
|||||||
import * as path from "path"
|
import * as path from "path"
|
||||||
import { extend, paths } from "./util"
|
import { extend, paths } from "./util"
|
||||||
import { logger } from "@coder/logger"
|
import { logger } from "@coder/logger"
|
||||||
|
import { Route } from "./http"
|
||||||
|
|
||||||
export type Settings = { [key: string]: Settings | string | boolean | number }
|
export type Settings = { [key: string]: Settings | string | boolean | number }
|
||||||
|
|
||||||
@ -31,9 +32,11 @@ export class SettingsProvider<T> {
|
|||||||
* Write settings combined with current settings. On failure log a warning.
|
* Write settings combined with current settings. On failure log a warning.
|
||||||
* Objects will be merged and everything else will be replaced.
|
* Objects will be merged and everything else will be replaced.
|
||||||
*/
|
*/
|
||||||
public async write(settings: Partial<T>): Promise<void> {
|
public async write(settings: Partial<T>, shallow?: boolean): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await fs.writeFile(this.settingsPath, JSON.stringify(extend(await this.read(), settings), null, 2))
|
const oldSettings = await this.read()
|
||||||
|
const nextSettings = shallow ? Object.assign({}, oldSettings, settings) : extend(oldSettings, settings)
|
||||||
|
await fs.writeFile(this.settingsPath, JSON.stringify(nextSettings, null, 2))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn(error.message)
|
logger.warn(error.message)
|
||||||
}
|
}
|
||||||
@ -55,6 +58,7 @@ export interface CoderSettings extends UpdateSettings {
|
|||||||
url: string
|
url: string
|
||||||
workspace: boolean
|
workspace: boolean
|
||||||
}
|
}
|
||||||
|
query: Route["query"]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user