Skip to content

Commit b6e791f

Browse files
committed
refactor: write route.query via settings.write
I added a shallow parameter, because the query should not be extends, but should be replaced directly.
1 parent 5ba650b commit b6e791f

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/node/app/vscode.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ export class VscodeHttpProvider extends HttpProvider {
134134
return { redirect: "/login", query: { to: this.options.base } }
135135
}
136136
try {
137-
this.persistRouteQuery(request, route)
138137
return await this.getRoot(request, route)
139138
} catch (error) {
140139
const message = `<div>VS Code failed to load.</div> ${
@@ -165,13 +164,6 @@ export class VscodeHttpProvider extends HttpProvider {
165164

166165
throw new HttpError("Not found", HttpCode.NotFound)
167166
}
168-
169-
private persistRouteQuery(request: http.IncomingMessage, route: Route): void {
170-
const content = Object.keys(route.query).reduce((content, next) => {
171-
return (content += `${next}=${route.query[next]}\n`)
172-
}, "")
173-
fs.writeFile(path.resolve(paths.data, "query"), content)
174-
}
175167

176168
private async getRoot(request: http.IncomingMessage, route: Route): Promise<HttpResponse> {
177169
const remoteAuthority = request.headers.host as string
@@ -191,11 +183,15 @@ export class VscodeHttpProvider extends HttpProvider {
191183
}),
192184
])
193185

186+
let promise = Promise.resolve()
194187
if (startPath) {
195-
settings.write({
196-
lastVisited: startPath,
197-
})
188+
promise = settings.write({ lastVisited: startPath })
198189
}
190+
// `settings.write` depends on `settings.read` internally. To avoid race conditions, a promise is added here to synchronize.
191+
promise.then(() => {
192+
// the query should not be extends, but should be replaced directly.
193+
settings.write({ query: route.query }, true)
194+
})
199195

200196
if (!this.isDev) {
201197
response.content = response.content.replace(/<!-- PROD_ONLY/g, "").replace(/END_PROD_ONLY -->/g, "")

src/node/settings.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs-extra"
22
import * as path from "path"
33
import { extend, paths } from "./util"
44
import { logger } from "@coder/logger"
5+
import { Route } from "./http"
56

67
export type Settings = { [key: string]: Settings | string | boolean | number }
78

@@ -31,9 +32,11 @@ export class SettingsProvider<T> {
3132
* Write settings combined with current settings. On failure log a warning.
3233
* Objects will be merged and everything else will be replaced.
3334
*/
34-
public async write(settings: Partial<T>): Promise<void> {
35+
public async write(settings: Partial<T>, shallow?: boolean): Promise<void> {
3536
try {
36-
await fs.writeFile(this.settingsPath, JSON.stringify(extend(await this.read(), settings), null, 2))
37+
const oldSettings = await this.read()
38+
const nextSettings = shallow ? Object.assign({}, oldSettings, settings) : extend(oldSettings, settings)
39+
await fs.writeFile(this.settingsPath, JSON.stringify(nextSettings, null, 2))
3740
} catch (error) {
3841
logger.warn(error.message)
3942
}
@@ -55,6 +58,7 @@ export interface CoderSettings extends UpdateSettings {
5558
url: string
5659
workspace: boolean
5760
}
61+
query: Route["query"]
5862
}
5963

6064
/**

0 commit comments

Comments
 (0)