@ms-cloudpack/path-string-parsing
Advanced tools
Comparing version
/** | ||
* Normalize a path: | ||
* - If it's empty, undefined, or `'.'`, return `'.'`. | ||
* - If it's empty, undefined, or `.`, return `.`. | ||
* - If it's relative, normalize slashes and ensure it starts with `./`. | ||
* - If it starts with `/`, add a leading `.` (this is an absolute path but is sometimes treated | ||
* as package-relative) | ||
* - If it's an absolute Windows path (starts with a drive letter), throw an error. | ||
* - If it's absolute, throw an error (since this likely indicates a bug in the calling code). | ||
@@ -6,0 +9,0 @@ * |
import { slash } from './slash.js'; | ||
/** | ||
* Normalize a path: | ||
* - If it's empty, undefined, or `'.'`, return `'.'`. | ||
* - If it's empty, undefined, or `.`, return `.`. | ||
* - If it's relative, normalize slashes and ensure it starts with `./`. | ||
* - If it starts with `/`, add a leading `.` (this is an absolute path but is sometimes treated | ||
* as package-relative) | ||
* - If it's an absolute Windows path (starts with a drive letter), throw an error. | ||
* - If it's absolute, throw an error (since this likely indicates a bug in the calling code). | ||
@@ -14,12 +17,22 @@ * | ||
} | ||
// Throw if the path is absolute. | ||
// NOTE: Don't use the `path` module because this function is used by `parseImportString`, which | ||
// is used in the browser by the overlay. | ||
if (originalPath[0] === '/' || /^[a-zA-Z]:[\\/]/.test(originalPath)) { | ||
throw new Error(`Expected a relative path, but received an absolute path: ${originalPath}`); | ||
// Throw on an absolute Windows path: this is definitely absolute regardless of the context it | ||
// came from, and if the calling code attempts to join it to another path as if it was relative, | ||
// that will have bad results. It's more likely on Windows that we could unintentionally end up | ||
// with an absolute path since there's no way to do relative paths across drive letters | ||
// (path.relative() will return an absolute path), so we throw here to make it clear that | ||
// Cloudpack needs to have specific handling for any cases where that might be possible. | ||
if (/^[a-zA-Z]:[\\/]/.test(originalPath)) { | ||
throw new Error(`Received an absolute path where a relative path was expected: ${originalPath}.\n` + | ||
'This indicates a bug in Cloudpack, likely due to calling path.relative() with two Windows paths ' + | ||
'on different drives (which returns an absolute path) and then assuming the result is relative.'); | ||
} | ||
// Ensure we have the right slashes. | ||
originalPath = slash(originalPath); | ||
return originalPath.startsWith('./') ? originalPath : `./${originalPath}`; | ||
const normalizedPath = slash(originalPath); | ||
if (normalizedPath[0] === '/') { | ||
// Technically a path starting with / is absolute, but it's possible that package.json | ||
// entry points like "main" could also use a leading slash to indicate the package root. | ||
return '.' + normalizedPath; | ||
} | ||
return normalizedPath.startsWith('./') ? normalizedPath : `./${normalizedPath}`; | ||
} | ||
//# sourceMappingURL=normalizeRelativePath.js.map |
{ | ||
"name": "@ms-cloudpack/path-string-parsing", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Common path-related string parsing utilities for Cloudpack", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
19240
15.22%170
10.39%0
-100%