Skip to content

Commit e86c066

Browse files
committed
Add helper functions to make some code clearer
1 parent c581bca commit e86c066

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/common/util.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ export const normalize = (url: string, keepTrailing = false): string => {
3333
return url.replace(/\/\/+/g, "/").replace(/\/+$/, keepTrailing ? "/" : "")
3434
}
3535

36+
/**
37+
* Remove leading and trailing slashes.
38+
*/
39+
export const trimSlashes = (url: string): string => {
40+
return url.replace(/^\/+|\/+$/g, "")
41+
}
42+
3643
/**
3744
* Get options embedded in the HTML or query params.
3845
*/
@@ -75,3 +82,17 @@ export const getOptions = <T extends Options>(): T => {
7582

7683
return options
7784
}
85+
86+
/**
87+
* Wrap the value in an array if it's not already an array. If the value is
88+
* undefined return an empty array.
89+
*/
90+
export const arrayify = <T>(value?: T | T[]): T[] => {
91+
if (Array.isArray(value)) {
92+
return value
93+
}
94+
if (typeof value === "undefined") {
95+
return []
96+
}
97+
return [value]
98+
}

src/node/app/vscode.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
WorkbenchOptions,
1515
} from "../../../lib/vscode/src/vs/server/ipc"
1616
import { HttpCode, HttpError } from "../../common/http"
17-
import { generateUuid } from "../../common/util"
17+
import { arrayify, generateUuid } from "../../common/util"
1818
import { Args } from "../cli"
1919
import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
2020
import { settings } from "../settings"
@@ -224,8 +224,7 @@ export class VscodeHttpProvider extends HttpProvider {
224224
}
225225
for (let i = 0; i < startPaths.length; ++i) {
226226
const startPath = startPaths[i]
227-
const url =
228-
startPath && (typeof startPath.url === "string" ? [startPath.url] : startPath.url || []).find((p) => !!p)
227+
const url = arrayify(startPath && startPath.url).find((p) => !!p)
229228
if (startPath && url) {
230229
return {
231230
url,

src/node/http.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Readable } from "stream"
1212
import * as tls from "tls"
1313
import * as url from "url"
1414
import { HttpCode, HttpError } from "../common/http"
15-
import { normalize, Options, plural, split } from "../common/util"
15+
import { arrayify, normalize, Options, plural, split, trimSlashes } from "../common/util"
1616
import { SocketProxyProvider } from "./socket"
1717
import { getMediaMime, paths } from "./util"
1818

@@ -287,7 +287,7 @@ export abstract class HttpProvider {
287287
* Helper to error on invalid methods (default GET).
288288
*/
289289
protected ensureMethod(request: http.IncomingMessage, method?: string | string[]): void {
290-
const check = Array.isArray(method) ? method : [method || "GET"]
290+
const check = arrayify(method || "GET")
291291
if (!request.method || !check.includes(request.method)) {
292292
throw new HttpError(`Unsupported method ${request.method}`, HttpCode.BadRequest)
293293
}
@@ -559,7 +559,7 @@ export class HttpServer {
559559
},
560560
...args,
561561
)
562-
const endpoints = (typeof endpoint === "string" ? [endpoint] : endpoint).map((e) => e.replace(/^\/+|\/+$/g, ""))
562+
const endpoints = arrayify(endpoint).map(trimSlashes)
563563
endpoints.forEach((endpoint) => {
564564
if (/\//.test(endpoint)) {
565565
throw new Error(`Only top-level endpoints are supported (got ${endpoint})`)

0 commit comments

Comments
 (0)