@ms-cloudpack/path-string-parsing 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * Normalize a path:
3
- * - If it's empty, undefined, or `'.'`, return `'.'`.
3
+ * - If it's empty, undefined, or `.`, return `.`.
4
4
  * - If it's relative, normalize slashes and ensure it starts with `./`.
5
+ * - If it starts with `/`, add a leading `.` (this is an absolute path but is sometimes treated
6
+ * as package-relative)
7
+ * - If it's an absolute Windows path (starts with a drive letter), throw an error.
5
8
  * - If it's absolute, throw an error (since this likely indicates a bug in the calling code).
6
9
  *
7
10
  * Note that this will NOT remove a trailing slash (if present).
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeRelativePath.d.ts","sourceRoot":"","sources":["../src/normalizeRelativePath.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAgB9E"}
1
+ {"version":3,"file":"normalizeRelativePath.d.ts","sourceRoot":"","sources":["../src/normalizeRelativePath.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CA6B9E"}
@@ -1,8 +1,11 @@
1
1
  import { slash } from './slash.js';
2
2
  /**
3
3
  * Normalize a path:
4
- * - If it's empty, undefined, or `'.'`, return `'.'`.
4
+ * - If it's empty, undefined, or `.`, return `.`.
5
5
  * - If it's relative, normalize slashes and ensure it starts with `./`.
6
+ * - If it starts with `/`, add a leading `.` (this is an absolute path but is sometimes treated
7
+ * as package-relative)
8
+ * - If it's an absolute Windows path (starts with a drive letter), throw an error.
6
9
  * - If it's absolute, throw an error (since this likely indicates a bug in the calling code).
7
10
  *
8
11
  * Note that this will NOT remove a trailing slash (if present).
@@ -11,14 +14,24 @@ export function normalizeRelativePath(originalPath) {
11
14
  if (!originalPath || originalPath === '.') {
12
15
  return '.';
13
16
  }
14
- // Throw if the path is absolute.
15
- // NOTE: Don't use the `path` module because this function is used by `parseImportString`, which
16
- // is used in the browser by the overlay.
17
- if (originalPath[0] === '/' || /^[a-zA-Z]:[\\/]/.test(originalPath)) {
18
- throw new Error(`Expected a relative path, but received an absolute path: ${originalPath}`);
17
+ // Throw on an absolute Windows path: this is definitely absolute regardless of the context it
18
+ // came from, and if the calling code attempts to join it to another path as if it was relative,
19
+ // that will have bad results. It's more likely on Windows that we could unintentionally end up
20
+ // with an absolute path since there's no way to do relative paths across drive letters
21
+ // (path.relative() will return an absolute path), so we throw here to make it clear that
22
+ // Cloudpack needs to have specific handling for any cases where that might be possible.
23
+ if (/^[a-zA-Z]:[\\/]/.test(originalPath)) {
24
+ throw new Error(`Received an absolute path where a relative path was expected: ${originalPath}.\n` +
25
+ 'This indicates a bug in Cloudpack, likely due to calling path.relative() with two Windows paths ' +
26
+ 'on different drives (which returns an absolute path) and then assuming the result is relative.');
19
27
  }
20
28
  // Ensure we have the right slashes.
21
- originalPath = slash(originalPath);
22
- return originalPath.startsWith('./') ? originalPath : `./${originalPath}`;
29
+ const normalizedPath = slash(originalPath);
30
+ if (normalizedPath[0] === '/') {
31
+ // Technically a path starting with / is absolute, but it's possible that package.json
32
+ // entry points like "main" could also use a leading slash to indicate the package root.
33
+ return '.' + normalizedPath;
34
+ }
35
+ return normalizedPath.startsWith('./') ? normalizedPath : `./${normalizedPath}`;
23
36
  }
24
37
  //# sourceMappingURL=normalizeRelativePath.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeRelativePath.js","sourceRoot":"","sources":["../src/normalizeRelativePath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,YAAgC;IACpE,IAAI,CAAC,YAAY,IAAI,YAAY,KAAK,GAAG,EAAE,CAAC;QAC1C,OAAO,GAAG,CAAC;IACb,CAAC;IAED,iCAAiC;IACjC,gGAAgG;IAChG,yCAAyC;IACzC,IAAI,YAAY,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;QACpE,MAAM,IAAI,KAAK,CAAC,4DAA4D,YAAY,EAAE,CAAC,CAAC;IAC9F,CAAC;IAED,oCAAoC;IACpC,YAAY,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;IAEnC,OAAO,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,YAAY,EAAE,CAAC;AAC5E,CAAC","sourcesContent":["import { slash } from './slash.js';\n\n/**\n * Normalize a path:\n * - If it's empty, undefined, or `'.'`, return `'.'`.\n * - If it's relative, normalize slashes and ensure it starts with `./`.\n * - If it's absolute, throw an error (since this likely indicates a bug in the calling code).\n *\n * Note that this will NOT remove a trailing slash (if present).\n */\nexport function normalizeRelativePath(originalPath: string | undefined): string {\n if (!originalPath || originalPath === '.') {\n return '.';\n }\n\n // Throw if the path is absolute.\n // NOTE: Don't use the `path` module because this function is used by `parseImportString`, which\n // is used in the browser by the overlay.\n if (originalPath[0] === '/' || /^[a-zA-Z]:[\\\\/]/.test(originalPath)) {\n throw new Error(`Expected a relative path, but received an absolute path: ${originalPath}`);\n }\n\n // Ensure we have the right slashes.\n originalPath = slash(originalPath);\n\n return originalPath.startsWith('./') ? originalPath : `./${originalPath}`;\n}\n"]}
1
+ {"version":3,"file":"normalizeRelativePath.js","sourceRoot":"","sources":["../src/normalizeRelativePath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,qBAAqB,CAAC,YAAgC;IACpE,IAAI,CAAC,YAAY,IAAI,YAAY,KAAK,GAAG,EAAE,CAAC;QAC1C,OAAO,GAAG,CAAC;IACb,CAAC;IAED,8FAA8F;IAC9F,gGAAgG;IAChG,+FAA+F;IAC/F,uFAAuF;IACvF,yFAAyF;IACzF,wFAAwF;IACxF,IAAI,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,iEAAiE,YAAY,KAAK;YAChF,kGAAkG;YAClG,gGAAgG,CACnG,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,cAAc,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;IAE3C,IAAI,cAAc,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;QAC9B,sFAAsF;QACtF,wFAAwF;QACxF,OAAO,GAAG,GAAG,cAAc,CAAC;IAC9B,CAAC;IAED,OAAO,cAAc,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,cAAc,EAAE,CAAC;AAClF,CAAC","sourcesContent":["import { slash } from './slash.js';\n\n/**\n * Normalize a path:\n * - If it's empty, undefined, or `.`, return `.`.\n * - If it's relative, normalize slashes and ensure it starts with `./`.\n * - If it starts with `/`, add a leading `.` (this is an absolute path but is sometimes treated\n * as package-relative)\n * - If it's an absolute Windows path (starts with a drive letter), throw an error.\n * - If it's absolute, throw an error (since this likely indicates a bug in the calling code).\n *\n * Note that this will NOT remove a trailing slash (if present).\n */\nexport function normalizeRelativePath(originalPath: string | undefined): string {\n if (!originalPath || originalPath === '.') {\n return '.';\n }\n\n // Throw on an absolute Windows path: this is definitely absolute regardless of the context it\n // came from, and if the calling code attempts to join it to another path as if it was relative,\n // that will have bad results. It's more likely on Windows that we could unintentionally end up\n // with an absolute path since there's no way to do relative paths across drive letters\n // (path.relative() will return an absolute path), so we throw here to make it clear that\n // Cloudpack needs to have specific handling for any cases where that might be possible.\n if (/^[a-zA-Z]:[\\\\/]/.test(originalPath)) {\n throw new Error(\n `Received an absolute path where a relative path was expected: ${originalPath}.\\n` +\n 'This indicates a bug in Cloudpack, likely due to calling path.relative() with two Windows paths ' +\n 'on different drives (which returns an absolute path) and then assuming the result is relative.',\n );\n }\n\n // Ensure we have the right slashes.\n const normalizedPath = slash(originalPath);\n\n if (normalizedPath[0] === '/') {\n // Technically a path starting with / is absolute, but it's possible that package.json\n // entry points like \"main\" could also use a leading slash to indicate the package root.\n return '.' + normalizedPath;\n }\n\n return normalizedPath.startsWith('./') ? normalizedPath : `./${normalizedPath}`;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/path-string-parsing",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Common path-related string parsing utilities for Cloudpack",
5
5
  "license": "MIT",
6
6
  "type": "module",