Skip to content

Commit

Permalink
fix: check path is string in pathToFsPath
Browse files Browse the repository at this point in the history
There's a chance this function can be called with a path that is not a string.

To catch that, we check if path is of a different type and throw an error if it
is.

This also adds a couple tests for this function.
  • Loading branch information
jsjoeio committed Jul 8, 2021
1 parent 343cec5 commit d989df0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ export function pathToFsPath(path: string, keepDriveLetterCasing = false): strin
const isWindows = process.platform === "win32"
const uri = { authority: undefined, path, scheme: "file" }
let value: string

if (typeof uri.path !== "string") {
throw new Error(
`Could not computer 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
12 changes: 12 additions & 0 deletions test/unit/node/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,15 @@ describe("escapeHtml", () => {
)
})
})

describe("pathToFsPath", () => {
it("should convert a path to a file system path", () => {
expect(util.pathToFsPath("/foo/bar/baz")).toBe("/foo/bar/baz")
})
it("should throw an error if an array is passed in for path", () => {
// @ts-expect-error We need to make sure it throws when an array is passed in.
expect(() => util.pathToFsPath([])).toThrow(
`Could not computer fsPath from given uri. Expected path to be of type string, but was of type object.`,
)
})
})

0 comments on commit d989df0

Please sign in to comment.