Skip to content

Commit

Permalink
fix file:/ deviation on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 24, 2023
1 parent 22887a8 commit 06049b7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export default function fileURLToPath(href: string, separator: string): string {
file = separator + separator + href.substring(7)
} else if (href.startsWith('file:/')) {
// is full path with unknown drive letter
file = separator + separator + 'localhost' + separator + href.substring(6)
// conform with Node.js fileURLToPath
const error = new Error('File URL path must be absolute') as any
error.code = 'ERR_INVALID_FILE_URL_PATH'
throw error
} else {
file = href
}
Expand Down
2 changes: 1 addition & 1 deletion source/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fileURLToPath from './index.js'

const fixtures = [
{ args: ['file:/path', '/'], result: '/path' },
{ args: ['file:/path', '\\'], result: '\\\\localhost\\path' },
{ args: ['file:/path', '\\'], errorCode: 'ERR_INVALID_FILE_URL_PATH' },

{ args: ['file://localhost/path', '\\'], result: '\\\\localhost\\path' },
{ args: ['file://hostname/path', '\\'], result: '\\\\hostname\\path' },
Expand Down

0 comments on commit 06049b7

Please sign in to comment.