Skip to content

Commit 411c61f

Browse files
committed
Create helper for determining if route is the root
1 parent 74a0bac commit 411c61f

File tree

7 files changed

+15
-14
lines changed

7 files changed

+15
-14
lines changed

src/node/app/api.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export class ApiHttpProvider extends HttpProvider {
4343

4444
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
4545
this.ensureAuthenticated(request)
46-
// Only serve root pages.
47-
if (route.requestPath && route.requestPath !== "/index.html") {
46+
if (!this.isRoot(route)) {
4847
throw new HttpError("Not found", HttpCode.NotFound)
4948
}
5049

src/node/app/dashboard.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export class DashboardHttpProvider extends HttpProvider {
2020
}
2121

2222
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
23-
// Only serve root pages.
24-
if (route.requestPath && route.requestPath !== "/index.html") {
23+
if (!this.isRoot(route)) {
2524
throw new HttpError("Not found", HttpCode.NotFound)
2625
}
2726

src/node/app/login.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ interface LoginPayload {
1818
*/
1919
export class LoginHttpProvider extends HttpProvider {
2020
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
21-
// Only serve root pages and only if password authentication is enabled.
22-
if (this.options.auth !== AuthType.Password || (route.requestPath && route.requestPath !== "/index.html")) {
21+
if (this.options.auth !== AuthType.Password || !this.isRoot(route)) {
2322
throw new HttpError("Not found", HttpCode.NotFound)
2423
}
2524
switch (route.base) {

src/node/app/proxy.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
4141
request: http.IncomingMessage,
4242
response: http.ServerResponse,
4343
): Promise<HttpResponse> {
44-
const isRoot = !route.requestPath || route.requestPath === "/index.html"
4544
if (!this.authenticated(request)) {
46-
// Only redirect from the root. Other requests get an unauthorized error.
47-
if (isRoot) {
45+
if (this.isRoot(route)) {
4846
return { redirect: "/login", query: { to: route.fullPath } }
4947
}
5048
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
@@ -53,7 +51,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
5351
// Ensure there is a trailing slash so relative paths work correctly.
5452
const port = route.base.replace(/^\//, "")
5553
const base = `${this.options.base}/${port}`
56-
if (isRoot && !route.fullPath.endsWith("/")) {
54+
if (this.isRoot(route) && !route.fullPath.endsWith("/")) {
5755
return {
5856
redirect: `${base}/`,
5957
}

src/node/app/update.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ export class UpdateHttpProvider extends HttpProvider {
6161
this.ensureAuthenticated(request)
6262
this.ensureMethod(request)
6363

64-
// Only serve root pages.
65-
if (route.requestPath && route.requestPath !== "/index.html") {
64+
if (!this.isRoot(route)) {
6665
throw new HttpError("Not found", HttpCode.NotFound)
6766
}
6867

src/node/app/vscode.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ export class VscodeHttpProvider extends HttpProvider {
128128

129129
switch (route.base) {
130130
case "/":
131-
// Only serve this at the root.
132-
if (route.requestPath && route.requestPath !== "/index.html") {
131+
if (!this.isRoot(route)) {
133132
throw new HttpError("Not found", HttpCode.NotFound)
134133
} else if (!this.authenticated(request)) {
135134
return { redirect: "/login", query: { to: this.options.base } }

src/node/http.ts

+8
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ export abstract class HttpProvider {
359359
}
360360
return cookies as T
361361
}
362+
363+
/**
364+
* Return true if the route is for the root page. For example /base, /base/,
365+
* or /base/index.html but not /base/path or /base/file.js.
366+
*/
367+
protected isRoot(route: Route): boolean {
368+
return !route.requestPath || route.requestPath === "/index.html"
369+
}
362370
}
363371

364372
/**

0 commit comments

Comments
 (0)