diff --git a/src/node/util.ts b/src/node/util.ts index 37eefc707..140431cc8 100644 --- a/src/node/util.ts +++ b/src/node/util.ts @@ -404,6 +404,37 @@ export const isWsl = async ( } } +interface OpenOptions { + args: string[] + command: string + urlSearch: string +} + +/** + * A helper function to construct options for `open` function. + * + * Extract to make it easier to test. + * + * @param platform - platform on machine + * @param urlSearch - url.search + * @returns an object with args, command, options and urlSearch + */ +export function constructOpenOptions(platform: NodeJS.Platform | "wsl", urlSearch: string): OpenOptions { + const args: string[] = [] + let command = platform === "darwin" ? "open" : "xdg-open" + if (platform === "win32" || platform === "wsl") { + command = platform === "wsl" ? "cmd.exe" : "cmd" + args.push("/c", "start", '""', "/b") + urlSearch = urlSearch.replace(/&/g, "^&") + } + + return { + args, + command, + urlSearch, + } +} + /** * Try opening an address using whatever the system has set for opening URLs. */ @@ -416,16 +447,10 @@ export const open = async (address: URL | string): Promise => { if (url.hostname === "0.0.0.0") { url.hostname = "localhost" } - const args = [] as string[] - const options = {} as cp.SpawnOptions const platform = (await isWsl(process.platform, os.release(), "/proc/version")) ? "wsl" : process.platform - let command = platform === "darwin" ? "open" : "xdg-open" - if (platform === "win32" || platform === "wsl") { - command = platform === "wsl" ? "cmd.exe" : "cmd" - args.push("/c", "start", '""', "/b") - url.search = url.search.replace(/&/g, "^&") - } - const proc = cp.spawn(command, [...args, url.toString()], options) + const { command, args, urlSearch } = constructOpenOptions(platform, url.search) + url.search = urlSearch + const proc = cp.spawn(command, [...args, url.toString()], {}) await new Promise((resolve, reject) => { proc.on("error", reject) proc.on("close", (code) => { diff --git a/test/unit/node/util.test.ts b/test/unit/node/util.test.ts index 805455b8e..aea682baa 100644 --- a/test/unit/node/util.test.ts +++ b/test/unit/node/util.test.ts @@ -544,3 +544,37 @@ describe("open", () => { await expect(util.open(address)).rejects.toThrow("Cannot open socket paths") }) }) +describe("constructOpenOptions", () => { + it("should return options for darwin", () => { + const platform: NodeJS.Platform | "wsl" = "darwin" + const url = new URL("localhost:8080") + const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search) + expect(args).toStrictEqual([]) + expect(command).toBe("open") + expect(urlSearch).toBe("") + }) + it("should return options for linux", () => { + const platform: NodeJS.Platform | "wsl" = "linux" + const url = new URL("localhost:8080") + const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search) + expect(args).toStrictEqual([]) + expect(command).toBe("xdg-open") + expect(urlSearch).toBe("") + }) + it("should return options for win32", () => { + const platform: NodeJS.Platform | "wsl" = "win32" + const url = new URL("localhost:8080?q=&test") + const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search) + expect(args).toStrictEqual(["/c", "start", '""', "/b"]) + expect(command).toBe("cmd") + expect(urlSearch).toBe("?q=^&test") + }) + it("should return options for wsl", () => { + const platform: NodeJS.Platform | "wsl" = "wsl" + const url = new URL("localhost:8080?q=&test") + const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search) + expect(args).toStrictEqual(["/c", "start", '""', "/b"]) + expect(command).toBe("cmd.exe") + expect(urlSearch).toBe("?q=^&test") + }) +})