Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: only accept string in pathToFsPath #3812

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/node/routes/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ router.get("/", async (req, res) => {
* TODO: Might currently be unused.
*/
router.get("/resource(/*)?", ensureAuthenticated, async (req, res) => {
if (typeof req.query.path === "string") {
res.set("Content-Type", getMediaMime(req.query.path))
res.send(await fs.readFile(pathToFsPath(req.query.path)))
const path = getFirstString(req.query.path)
if (path) {
res.set("Content-Type", getMediaMime(path))
res.send(await fs.readFile(pathToFsPath(path)))
}
})

/**
* Used by VS Code to load files.
*/
router.get("/vscode-remote-resource(/*)?", ensureAuthenticated, async (req, res) => {
if (typeof req.query.path === "string") {
res.set("Content-Type", getMediaMime(req.query.path))
res.send(await fs.readFile(pathToFsPath(req.query.path)))
const path = getFirstString(req.query.path)
if (path) {
res.set("Content-Type", getMediaMime(path))
res.send(await fs.readFile(pathToFsPath(path)))
}
})

Expand Down
10 changes: 2 additions & 8 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,17 +458,11 @@ enum CharCode {
* Taken from vs/base/common/uri.ts. It's not imported to avoid also importing
* everything that file imports.
*/
export function pathToFsPath(path: string | string[], keepDriveLetterCasing = false): string {
export function pathToFsPath(path: string, keepDriveLetterCasing = false): string {
const isWindows = process.platform === "win32"
const uri = { authority: undefined, path: getFirstString(path), scheme: "file" }
const uri = { authority: undefined, path: getFirstString(path) || "", scheme: "file" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering - would this be more readable in expanded form, e.g.

const uri = {
  authority: undefined,
  path: getFirstString(path) || "",
  scheme: "file",
}

I'm not sure if prettier prefers this form though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely is for me! I think Prettier autoformats it though :(

let value: string

if (typeof uri.path !== "string") {
throw new Error(
`Could not compute fsPath from given uri. Expected path to be of type string, but was of type ${typeof uri.path}.`,
)
}

if (uri.authority && uri.path.length > 1 && uri.scheme === "file") {
// unc path: file://shares/c$/far/boo
value = `//${uri.authority}${uri.path}`
Expand Down
37 changes: 23 additions & 14 deletions test/unit/node/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as cp from "child_process"
import * as path from "path"
import { promises as fs } from "fs"
import { generateUuid } from "../../../src/common/util"
import * as util from "../../../src/node/util"
import { tmpdir } from "../../../src/node/constants"

describe("getEnvPaths", () => {
describe("on darwin", () => {
Expand Down Expand Up @@ -464,20 +467,6 @@ describe("pathToFsPath", () => {
it("should keep drive letter casing when set to true", () => {
expect(util.pathToFsPath("/C:/far/bo", true)).toBe("C:/far/bo")
})
it("should throw an error if a non-string is passed in for path", () => {
expect(() =>
util
// @ts-expect-error We need to check other types
.pathToFsPath({}),
).toThrow(`Could not compute fsPath from given uri. Expected path to be of type string, but was of type undefined.`)
})
it("should not throw an error for a string array", () => {
// @ts-expect-error We need to check other types
expect(() => util.pathToFsPath(["/hello/foo", "/hello/bar"]).not.toThrow())
})
it("should use the first string in a string array", () => {
expect(util.pathToFsPath(["/hello/foo", "/hello/bar"])).toBe("/hello/foo")
})
it("should replace / with \\ on Windows", () => {
let ORIGINAL_PLATFORM = process.platform

Expand All @@ -492,3 +481,23 @@ describe("pathToFsPath", () => {
})
})
})

describe("isFile", () => {
const testDir = path.join(tmpdir, "tests", "isFile")
let pathToFile = ""

beforeEach(async () => {
pathToFile = path.join(testDir, "foo.txt")
await fs.mkdir(testDir, { recursive: true })
await fs.writeFile(pathToFile, "hello")
})
afterEach(async () => {
await fs.rm(testDir, { recursive: true, force: true })
})
it("should return false if the path doesn't exist", async () => {
expect(await util.isFile(testDir)).toBe(false)
})
it("should return true if is file", async () => {
expect(await util.isFile(pathToFile)).toBe(true)
})
})