@cspell/url 8.11.0 → 8.13.0

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,6 +1,5 @@
1
1
  import Path from 'node:path';
2
2
  export declare const isWindows: boolean;
3
- export declare const isWindowsPathRegEx: RegExp;
4
3
  export declare const percentRegEx: RegExp;
5
4
  export declare const backslashRegEx: RegExp;
6
5
  export declare const newlineRegEx: RegExp;
@@ -1,10 +1,11 @@
1
1
  import assert from 'node:assert';
2
2
  import Path from 'node:path';
3
- import { toFilePathOrHref } from './fileUrl.mjs';
4
- import { addTrailingSlash, isUrlLike, urlParent, urlToUrlRelative } from './url.mjs';
3
+ import { pathToFileURL } from 'node:url';
4
+ import { pathWindowsDriveLetterToUpper, regExpWindowsPathDriveLetter, toFilePathOrHref } from './fileUrl.mjs';
5
+ import { addTrailingSlash, isUrlLike, normalizeWindowsUrl, regExpWindowsPath, urlParent, urlToUrlRelative, } from './url.mjs';
5
6
  export const isWindows = process.platform === 'win32';
6
- export const isWindowsPathRegEx = /^[a-z]:[\\/]/i;
7
- const isWindowsPathname = /^[\\/]([a-z]:[\\/])/i;
7
+ const isWindowsPathRegEx = regExpWindowsPathDriveLetter;
8
+ const isWindowsPathname = regExpWindowsPath;
8
9
  export const percentRegEx = /%/g;
9
10
  export const backslashRegEx = /\\/g;
10
11
  export const newlineRegEx = /\n/g;
@@ -13,7 +14,6 @@ export const tabRegEx = /\t/g;
13
14
  export const questionRegex = /\?/g;
14
15
  export const hashRegex = /#/g;
15
16
  const ProtocolFile = 'file:';
16
- const RootFileURL = 'file:///';
17
17
  export class FileUrlBuilder {
18
18
  windows;
19
19
  path;
@@ -81,6 +81,16 @@ export class FileUrlBuilder {
81
81
  * @returns a URL
82
82
  */
83
83
  toFileURL(filenameOrUrl, relativeTo) {
84
+ return normalizeWindowsUrl(this.#toFileURL(filenameOrUrl, relativeTo));
85
+ }
86
+ /**
87
+ * Try to make a file URL.
88
+ * - if filenameOrUrl is already a URL, it is returned as is.
89
+ * @param filenameOrUrl
90
+ * @param relativeTo - optional URL, if given, filenameOrUrl will be parsed as relative.
91
+ * @returns a URL
92
+ */
93
+ #toFileURL(filenameOrUrl, relativeTo) {
84
94
  if (typeof filenameOrUrl !== 'string')
85
95
  return filenameOrUrl;
86
96
  if (isUrlLike(filenameOrUrl))
@@ -116,7 +126,7 @@ export class FileUrlBuilder {
116
126
  const p = this.path === Path
117
127
  ? toFilePathOrHref(url)
118
128
  : decodeURIComponent(url.pathname.split('/').join(this.path.sep));
119
- return p.replace(isWindowsPathRegEx, (drive) => drive.toUpperCase()).replace(isWindowsPathname, '$1');
129
+ return pathWindowsDriveLetterToUpper(p.replace(isWindowsPathname, '$1'));
120
130
  }
121
131
  /**
122
132
  * Calculate the relative path to go from `urlFrom` to `urlTo`.
@@ -158,7 +168,13 @@ export class FileUrlBuilder {
158
168
  rootFileURL(filePath) {
159
169
  const path = this.path;
160
170
  const p = path.parse(path.normalize(path.resolve(filePath ?? '.')));
161
- return new URL(this.normalizeFilePathForUrl(p.root), RootFileURL);
171
+ return new URL(this.normalizeFilePathForUrl(p.root), this.#getFsRootURL());
172
+ }
173
+ #getFsRootURL() {
174
+ if (this.path === Path)
175
+ return pathToFileURL('/');
176
+ const p = this.path.resolve('/');
177
+ return new URL(this.normalizeFilePathForUrl(p), 'file:///');
162
178
  }
163
179
  /**
164
180
  * Determine if a filePath is absolute.
@@ -9,4 +9,6 @@ export declare function isFileURL(url: URL | string): boolean;
9
9
  * @returns path or href
10
10
  */
11
11
  export declare function toFilePathOrHref(url: URL | string): string;
12
+ export declare const regExpWindowsPathDriveLetter: RegExp;
13
+ export declare function pathWindowsDriveLetterToUpper(absoluteFilePath: string): string;
12
14
  //# sourceMappingURL=fileUrl.d.mts.map
package/dist/fileUrl.mjs CHANGED
@@ -16,9 +16,10 @@ export function toFilePathOrHref(url) {
16
16
  return isFileURL(url) ? toFilePath(url) : url.toString();
17
17
  }
18
18
  function toFilePath(url) {
19
- return windowsDriveLetterToUpper(fileURLToPath(url));
19
+ return pathWindowsDriveLetterToUpper(fileURLToPath(url));
20
20
  }
21
- function windowsDriveLetterToUpper(absoluteFilePath) {
22
- return absoluteFilePath.replace(/^([a-z]):\\/, (s) => s.toUpperCase());
21
+ export const regExpWindowsPathDriveLetter = /^([a-zA-Z]):[\\/]/;
22
+ export function pathWindowsDriveLetterToUpper(absoluteFilePath) {
23
+ return absoluteFilePath.replace(regExpWindowsPathDriveLetter, (s) => s.toUpperCase());
23
24
  }
24
25
  //# sourceMappingURL=fileUrl.mjs.map
package/dist/url.d.mts CHANGED
@@ -96,4 +96,13 @@ export declare function urlRelative(urlFrom: string | URL, urlTo: string | URL):
96
96
  * @returns the relative path
97
97
  */
98
98
  export declare function urlToUrlRelative(urlFrom: URL, urlTo: URL): string;
99
+ export declare const regExpWindowsPath: RegExp;
100
+ export declare const regExpEncodedColon: RegExp;
101
+ /**
102
+ * Ensure that a windows file url is correctly formatted with a capitol letter for the drive.
103
+ *
104
+ * @param url - URL to check.
105
+ * @returns a new URL if modified or converted from a string.
106
+ */
107
+ export declare function normalizeWindowsUrl(url: URL | string): URL;
99
108
  //# sourceMappingURL=url.d.mts.map
package/dist/url.mjs CHANGED
@@ -6,7 +6,7 @@ const isURLRegEx = /^(\w[\w-]{1,63}:\/|data:|stdin:)/i;
6
6
  * @returns a URL
7
7
  */
8
8
  export function toURL(url, relativeTo) {
9
- return url instanceof URL ? url : new URL(url, relativeTo);
9
+ return normalizeWindowsUrl(url instanceof URL ? url : new URL(url, relativeTo));
10
10
  }
11
11
  /**
12
12
  * Try to determine the parent directory URL of the uri.
@@ -148,4 +148,26 @@ export function urlToUrlRelative(urlFrom, urlTo) {
148
148
  const rel = '../'.repeat(p0Parts.length - i) + p1Parts.slice(i).join('/');
149
149
  return decodeURIComponent(rel.length < p1.length ? rel : p1);
150
150
  }
151
+ export const regExpWindowsPath = /^[\\/]([a-zA-Z]:[\\/])/;
152
+ export const regExpEncodedColon = /%3[aA]/g;
153
+ /**
154
+ * Ensure that a windows file url is correctly formatted with a capitol letter for the drive.
155
+ *
156
+ * @param url - URL to check.
157
+ * @returns a new URL if modified or converted from a string.
158
+ */
159
+ export function normalizeWindowsUrl(url) {
160
+ url = typeof url === 'string' ? new URL(url) : url;
161
+ if (url.protocol === 'file:') {
162
+ const pathname = url.pathname
163
+ .replaceAll(regExpEncodedColon, ':')
164
+ .replace(regExpWindowsPath, (d) => d.toUpperCase());
165
+ if (pathname !== url.pathname) {
166
+ url = new URL(url);
167
+ url.pathname = pathname;
168
+ return url;
169
+ }
170
+ }
171
+ return url;
172
+ }
151
173
  //# sourceMappingURL=url.mjs.map
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "8.11.0",
6
+ "version": "8.13.0",
7
7
  "description": "URL parsing and manipulation utilities for cspell",
8
8
  "keywords": [
9
9
  "cspell",
@@ -56,5 +56,5 @@
56
56
  "engines": {
57
57
  "node": ">=18.0"
58
58
  },
59
- "gitHead": "2b85b2b458b1117870a4f0aee18fb45ce991848d"
59
+ "gitHead": "2fd3fb430cc96a8a50543f57d96b288219a11923"
60
60
  }