-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(esm): dont add namespace query to bare specifier (#21)
- Loading branch information
1 parent
b273848
commit efb3509
Showing
5 changed files
with
64 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import path from 'node:path'; | ||
|
||
/** | ||
* Prior to calling this function, it's expected that Windows paths have been filtered out | ||
* via path.isAbsolute() | ||
* | ||
* Windows paths cannot be correctly parsed (e.g. new URL('C:\Users\Example\file.txt') | ||
*/ | ||
const getScheme = (url: string) => { | ||
const schemeIndex = url.indexOf(':'); | ||
if (schemeIndex === -1) { return; } | ||
return url.slice(0, schemeIndex); | ||
}; | ||
|
||
export const isRelativePath = (request: string) => ( | ||
request[0] === '.' | ||
&& ( | ||
request[1] === '/' | ||
|| (request[1] === '.' || request[2] === '/') | ||
) | ||
); | ||
|
||
const isUnixPath = (request: string) => ( | ||
isRelativePath(request) | ||
|| path.isAbsolute(request) | ||
); | ||
|
||
// In Node, bare specifiers (packages and core modules) do not accept queries | ||
export const requestAcceptsQuery = (request: string) => { | ||
// ./foo.js?query | ||
// /foo.js?query in UNIX | ||
if (isUnixPath(request)) { | ||
return true; | ||
} | ||
|
||
const scheme = getScheme(request); | ||
return ( | ||
// Expected to be file, https, etc... | ||
scheme | ||
|
||
// node:url maps to a bare-specifier, which does not accept queries | ||
// But URLs like file:// or https:// do | ||
&& scheme !== 'node' | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters