@cspell/url 10.0.1 → 10.1.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.
- package/dist/index.d.ts +134 -134
- package/dist/index.js +18 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
1
|
//#region src/dataUrl.d.mts
|
|
2
2
|
/**
|
|
3
|
-
* Try to determine the base name of a URL.
|
|
4
|
-
* @param url
|
|
5
|
-
* @returns the base name of a URL, including the trailing `/` if present.
|
|
6
|
-
*/
|
|
3
|
+
* Try to determine the base name of a URL.
|
|
4
|
+
* @param url
|
|
5
|
+
* @returns the base name of a URL, including the trailing `/` if present.
|
|
6
|
+
*/
|
|
7
7
|
declare function urlBasename(url: string | URL): string;
|
|
8
8
|
declare function isDataURL(url: string | URL): boolean;
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/defaultFileUrlBuilder.d.mts
|
|
11
11
|
declare function encodePathChars(filepath: string): string;
|
|
12
12
|
/**
|
|
13
|
-
* Normalize a file path for use in a URL.
|
|
14
|
-
* ```js
|
|
15
|
-
* const url = new URL(normalizeFilePathForUrl('path\\to\\file.txt'), 'file:///Users/user/');
|
|
16
|
-
* // Result: file:///Users/user/path/to/file.txt
|
|
17
|
-
* ```
|
|
18
|
-
* @param filePath
|
|
19
|
-
* @returns a normalized file path for use as a relative path in a URL.
|
|
20
|
-
*/
|
|
13
|
+
* Normalize a file path for use in a URL.
|
|
14
|
+
* ```js
|
|
15
|
+
* const url = new URL(normalizeFilePathForUrl('path\\to\\file.txt'), 'file:///Users/user/');
|
|
16
|
+
* // Result: file:///Users/user/path/to/file.txt
|
|
17
|
+
* ```
|
|
18
|
+
* @param filePath
|
|
19
|
+
* @returns a normalized file path for use as a relative path in a URL.
|
|
20
|
+
*/
|
|
21
21
|
declare function normalizeFilePathForUrl(filePath: string): string;
|
|
22
22
|
/**
|
|
23
|
-
* Try to make a file URL.
|
|
24
|
-
* - if filenameOrUrl is already a URL, it is returned as is.
|
|
25
|
-
* -
|
|
26
|
-
* @param filenameOrUrl
|
|
27
|
-
* @param relativeTo - optional URL, if given, filenameOrUrl will be parsed as relative.
|
|
28
|
-
* @returns a URL
|
|
29
|
-
*/
|
|
23
|
+
* Try to make a file URL.
|
|
24
|
+
* - if filenameOrUrl is already a URL, it is returned as is.
|
|
25
|
+
* -
|
|
26
|
+
* @param filenameOrUrl
|
|
27
|
+
* @param relativeTo - optional URL, if given, filenameOrUrl will be parsed as relative.
|
|
28
|
+
* @returns a URL
|
|
29
|
+
*/
|
|
30
30
|
declare function toFileURL(filenameOrUrl: string | URL, relativeTo?: string | URL): URL;
|
|
31
31
|
/**
|
|
32
|
-
* Converts a file path to a URL and adds a trailing slash.
|
|
33
|
-
* @param dir - url to a directory
|
|
34
|
-
* @returns a URL
|
|
35
|
-
*/
|
|
32
|
+
* Converts a file path to a URL and adds a trailing slash.
|
|
33
|
+
* @param dir - url to a directory
|
|
34
|
+
* @returns a URL
|
|
35
|
+
*/
|
|
36
36
|
declare function toFileDirURL(dir: string | URL): URL;
|
|
37
37
|
//#endregion
|
|
38
38
|
//#region src/fileUrl.d.mts
|
|
39
39
|
/**
|
|
40
|
-
* @param url - URL or string to check if it is a file URL.
|
|
41
|
-
* @returns true if the URL is a file URL.
|
|
42
|
-
*/
|
|
40
|
+
* @param url - URL or string to check if it is a file URL.
|
|
41
|
+
* @returns true if the URL is a file URL.
|
|
42
|
+
*/
|
|
43
43
|
declare function isFileURL(url: URL | string): boolean;
|
|
44
44
|
/**
|
|
45
|
-
* Convert a URL into a string. If it is a file URL, convert it to a path.
|
|
46
|
-
* @param url - URL
|
|
47
|
-
* @returns path or href
|
|
48
|
-
*/
|
|
45
|
+
* Convert a URL into a string. If it is a file URL, convert it to a path.
|
|
46
|
+
* @param url - URL
|
|
47
|
+
* @returns path or href
|
|
48
|
+
*/
|
|
49
49
|
declare function toFilePathOrHref(url: URL | string): string;
|
|
50
50
|
//#endregion
|
|
51
51
|
//#region src/FileUrlBuilder.d.mts
|
|
52
52
|
interface ParsedPath {
|
|
53
53
|
/**
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
* The root of the path such as '/' or 'c:\'
|
|
55
|
+
*/
|
|
56
56
|
root: string;
|
|
57
57
|
/**
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
* The full directory path such as '/home/user/dir' or 'c:\path\dir'
|
|
59
|
+
*/
|
|
60
60
|
dir: string;
|
|
61
61
|
/**
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
* The file name including extension (if any) such as 'index.html'
|
|
63
|
+
*/
|
|
64
64
|
base: string;
|
|
65
65
|
/**
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
* The file extension (if any) such as '.html'
|
|
67
|
+
*/
|
|
68
68
|
ext: string;
|
|
69
69
|
/**
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
* The file name without extension (if any) such as 'index'
|
|
71
|
+
*/
|
|
72
72
|
name: string;
|
|
73
73
|
}
|
|
74
74
|
interface PathInterface {
|
|
@@ -91,149 +91,149 @@ declare class FileUrlBuilder {
|
|
|
91
91
|
readonly cwd: URL;
|
|
92
92
|
constructor(options?: BuilderOptions);
|
|
93
93
|
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
* Encode special characters in a file path to use in a URL.
|
|
95
|
+
* @param filepath
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
98
|
encodePathChars(filepath: string): string;
|
|
99
99
|
/**
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
100
|
+
* Normalize a file path for use in a URL.
|
|
101
|
+
* ```js
|
|
102
|
+
* const url = new URL(normalizeFilePathForUrl('path\\to\\file.txt'), 'file:///Users/user/');
|
|
103
|
+
* // Result: file:///Users/user/path/to/file.txt
|
|
104
|
+
* ```
|
|
105
|
+
* @param filePath
|
|
106
|
+
* @returns a normalized file path for use as a relative path in a URL.
|
|
107
|
+
*/
|
|
108
108
|
normalizeFilePathForUrl(filePath: string): string;
|
|
109
109
|
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
110
|
+
* Try to make a file URL.
|
|
111
|
+
* - if filenameOrUrl is already a URL, it is returned as is.
|
|
112
|
+
* @param filenameOrUrl
|
|
113
|
+
* @param relativeTo - optional URL, if given, filenameOrUrl will be parsed as relative.
|
|
114
|
+
* @returns a URL
|
|
115
|
+
*/
|
|
116
116
|
toFileURL(filenameOrUrl: string | URL, relativeTo?: string | URL): URL;
|
|
117
117
|
/**
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
118
|
+
* Try to make a URL for a directory.
|
|
119
|
+
* - if dirOrUrl is already a URL, a slash is appended to the pathname.
|
|
120
|
+
* @param dirOrUrl - directory path to convert to a file URL.
|
|
121
|
+
* @param relativeTo - optional URL, if given, filenameOrUrl will be parsed as relative.
|
|
122
|
+
* @returns a URL
|
|
123
|
+
*/
|
|
124
124
|
toFileDirURL(dirOrUrl: string | URL, relativeTo?: string | URL): URL;
|
|
125
125
|
urlToFilePathOrHref(url: URL | string): string;
|
|
126
126
|
/**
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
127
|
+
* Calculate the relative path to go from `urlFrom` to `urlTo`.
|
|
128
|
+
* The protocol is not evaluated. Only the `url.pathname` is used.
|
|
129
|
+
* The result: `new URL(relative(urlFrom, urlTo), urlFrom).pathname === urlTo.pathname`
|
|
130
|
+
* @param urlFrom
|
|
131
|
+
* @param urlTo
|
|
132
|
+
* @returns the relative path
|
|
133
|
+
*/
|
|
134
134
|
relative(urlFrom: URL, urlTo: URL): string;
|
|
135
135
|
/**
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
136
|
+
* Get the parent directory of a URL.
|
|
137
|
+
* @param url
|
|
138
|
+
*/
|
|
139
139
|
urlDirname(url: URL | string): URL;
|
|
140
140
|
pathToFileURL(pathname: string, relativeToURL?: URL | string): URL;
|
|
141
141
|
rootFileURL(filePath?: string): URL;
|
|
142
142
|
/**
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
143
|
+
* Determine if a filePath is absolute.
|
|
144
|
+
*
|
|
145
|
+
* @param filePath
|
|
146
|
+
* @returns true if `URL` or `path.isAbsolute(filePath)`
|
|
147
|
+
*/
|
|
148
148
|
isAbsolute(filePath: string): boolean;
|
|
149
149
|
isUrlLike(url: string | URL): boolean;
|
|
150
150
|
}
|
|
151
151
|
//#endregion
|
|
152
152
|
//#region src/url.d.mts
|
|
153
153
|
/**
|
|
154
|
-
* Try to make a URL.
|
|
155
|
-
* @param url
|
|
156
|
-
* @param relativeTo - optional URL, if given, url will be parsed as relative.
|
|
157
|
-
* @returns a URL
|
|
158
|
-
*/
|
|
154
|
+
* Try to make a URL.
|
|
155
|
+
* @param url
|
|
156
|
+
* @param relativeTo - optional URL, if given, url will be parsed as relative.
|
|
157
|
+
* @returns a URL
|
|
158
|
+
*/
|
|
159
159
|
declare function toURL(url: string | URL, relativeTo?: string | URL): URL;
|
|
160
160
|
/**
|
|
161
|
-
* Try to determine the parent directory URL of the uri.
|
|
162
|
-
* If it is not a hierarchical URL, then it will return the URL.
|
|
163
|
-
* @param url - url to extract the dirname from.
|
|
164
|
-
* @returns a URL
|
|
165
|
-
*/
|
|
161
|
+
* Try to determine the parent directory URL of the uri.
|
|
162
|
+
* If it is not a hierarchical URL, then it will return the URL.
|
|
163
|
+
* @param url - url to extract the dirname from.
|
|
164
|
+
* @returns a URL
|
|
165
|
+
*/
|
|
166
166
|
declare function urlParent(url: string | URL): URL;
|
|
167
167
|
/**
|
|
168
|
-
* Alias of {@link urlParent}
|
|
169
|
-
* Try to determine the parent directory URL of the uri.
|
|
170
|
-
* If it is not a hierarchical URL, then it will return the URL.
|
|
171
|
-
* @param url - url to extract the dirname from.
|
|
172
|
-
* @returns a URL
|
|
173
|
-
*/
|
|
168
|
+
* Alias of {@link urlParent}
|
|
169
|
+
* Try to determine the parent directory URL of the uri.
|
|
170
|
+
* If it is not a hierarchical URL, then it will return the URL.
|
|
171
|
+
* @param url - url to extract the dirname from.
|
|
172
|
+
* @returns a URL
|
|
173
|
+
*/
|
|
174
174
|
declare const urlDirname: typeof urlParent;
|
|
175
175
|
/**
|
|
176
|
-
* return the basename (last portion of the URL pathname) of a path. It does NOT remove the trailing slash.
|
|
177
|
-
* @param path - URL pathname to extract the basename from.
|
|
178
|
-
*/
|
|
176
|
+
* return the basename (last portion of the URL pathname) of a path. It does NOT remove the trailing slash.
|
|
177
|
+
* @param path - URL pathname to extract the basename from.
|
|
178
|
+
*/
|
|
179
179
|
declare function basenameOfUrlPathname(path: string): string;
|
|
180
180
|
/**
|
|
181
|
-
* @param filename - filename to check if it is a string containing a URL.
|
|
182
|
-
*/
|
|
181
|
+
* @param filename - filename to check if it is a string containing a URL.
|
|
182
|
+
*/
|
|
183
183
|
declare function isUrlLike(filename: string): boolean;
|
|
184
184
|
declare function isUrlLike(filename: URL): true;
|
|
185
185
|
/**
|
|
186
|
-
* @param filename - filename to check if it is a string containing a URL or a URL object.
|
|
187
|
-
*/
|
|
186
|
+
* @param filename - filename to check if it is a string containing a URL or a URL object.
|
|
187
|
+
*/
|
|
188
188
|
declare function isUrlLike(filename: string | URL): boolean;
|
|
189
189
|
/**
|
|
190
|
-
* @param filename - filename to check if it is a string containing a URL.
|
|
191
|
-
*/
|
|
190
|
+
* @param filename - filename to check if it is a string containing a URL.
|
|
191
|
+
*/
|
|
192
192
|
declare function isNotUrlLike(filename: string): boolean;
|
|
193
193
|
declare function isNotUrlLike(filename: URL): false;
|
|
194
194
|
/**
|
|
195
|
-
* @param filename - filename to check if it is a string containing a URL or a URL object.
|
|
196
|
-
*/
|
|
195
|
+
* @param filename - filename to check if it is a string containing a URL or a URL object.
|
|
196
|
+
*/
|
|
197
197
|
declare function isNotUrlLike(filename: string | URL): filename is string;
|
|
198
198
|
/**
|
|
199
|
-
* Check if `url` is a URL instance.
|
|
200
|
-
* @returns
|
|
201
|
-
*/
|
|
199
|
+
* Check if `url` is a URL instance.
|
|
200
|
+
* @returns
|
|
201
|
+
*/
|
|
202
202
|
declare function isURL(url: unknown): url is URL;
|
|
203
203
|
/**
|
|
204
|
-
*
|
|
205
|
-
* @param url - url to check
|
|
206
|
-
* @param protocol - protocol to check against - e.g. 'file:', 'http:', 'https:'
|
|
207
|
-
* @returns
|
|
208
|
-
*/
|
|
204
|
+
*
|
|
205
|
+
* @param url - url to check
|
|
206
|
+
* @param protocol - protocol to check against - e.g. 'file:', 'http:', 'https:'
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
209
|
declare function hasProtocol(url: string | URL, protocol: string): boolean;
|
|
210
210
|
/**
|
|
211
|
-
* Attempts to add a trailing slash to the URL pathname if it does not already have one.
|
|
212
|
-
* Some If the pathname doesn't start with a `/`, a trailing slash is not added.
|
|
213
|
-
* @param url - a URL
|
|
214
|
-
* @returns
|
|
215
|
-
*/
|
|
211
|
+
* Attempts to add a trailing slash to the URL pathname if it does not already have one.
|
|
212
|
+
* Some If the pathname doesn't start with a `/`, a trailing slash is not added.
|
|
213
|
+
* @param url - a URL
|
|
214
|
+
* @returns
|
|
215
|
+
*/
|
|
216
216
|
declare function addTrailingSlash(url: URL): URL;
|
|
217
217
|
/**
|
|
218
|
-
* Calculate the relative path to go from `urlFrom` to `urlTo`.
|
|
219
|
-
* The protocol is not evaluated. Only the `url.pathname` is used.
|
|
220
|
-
* @param urlFrom
|
|
221
|
-
* @param urlTo
|
|
222
|
-
* @returns the relative path
|
|
223
|
-
*/
|
|
218
|
+
* Calculate the relative path to go from `urlFrom` to `urlTo`.
|
|
219
|
+
* The protocol is not evaluated. Only the `url.pathname` is used.
|
|
220
|
+
* @param urlFrom
|
|
221
|
+
* @param urlTo
|
|
222
|
+
* @returns the relative path
|
|
223
|
+
*/
|
|
224
224
|
declare function urlRelative(urlFrom: string | URL, urlTo: string | URL): string;
|
|
225
225
|
/**
|
|
226
|
-
* Ensure that a windows file url is correctly formatted with a capitol letter for the drive.
|
|
227
|
-
*
|
|
228
|
-
* @param url - URL to check.
|
|
229
|
-
* @returns a new URL if modified or converted from a string.
|
|
230
|
-
*/
|
|
226
|
+
* Ensure that a windows file url is correctly formatted with a capitol letter for the drive.
|
|
227
|
+
*
|
|
228
|
+
* @param url - URL to check.
|
|
229
|
+
* @returns a new URL if modified or converted from a string.
|
|
230
|
+
*/
|
|
231
231
|
declare function normalizeWindowsUrl(url: URL | string): URL;
|
|
232
232
|
/**
|
|
233
|
-
* There is a bug is NodeJS that sometimes causes UNC paths converted to a URL to be prefixed with `file:////`.
|
|
234
|
-
* @param url - URL to check.
|
|
235
|
-
* @returns fixed URL if needed.
|
|
236
|
-
*/
|
|
233
|
+
* There is a bug is NodeJS that sometimes causes UNC paths converted to a URL to be prefixed with `file:////`.
|
|
234
|
+
* @param url - URL to check.
|
|
235
|
+
* @returns fixed URL if needed.
|
|
236
|
+
*/
|
|
237
237
|
declare function fixUncUrl(url: URL): URL;
|
|
238
238
|
//#endregion
|
|
239
239
|
export { type BuilderOptions, FileUrlBuilder, type PathInterface, addTrailingSlash, basenameOfUrlPathname, encodePathChars, fixUncUrl, hasProtocol, isDataURL, isFileURL, isNotUrlLike, isURL, isUrlLike, normalizeFilePathForUrl, normalizeWindowsUrl, toFileDirURL, toFilePathOrHref, toFileURL, toURL, urlBasename, urlDirname, urlParent, urlRelative };
|
package/dist/index.js
CHANGED
|
@@ -217,6 +217,23 @@ const regExpWindowsFileUrl = /^file:\/\/\/[a-zA-Z]:\//;
|
|
|
217
217
|
function isWindowsFileUrl(url) {
|
|
218
218
|
return regExpWindowsFileUrl.test(url.toString());
|
|
219
219
|
}
|
|
220
|
+
const uncLongPathPrefix = "\\\\?\\";
|
|
221
|
+
const uncLongPathPrefixAlt = "\\\\.\\";
|
|
222
|
+
function isUncPath(path) {
|
|
223
|
+
return path.startsWith("\\\\");
|
|
224
|
+
}
|
|
225
|
+
function hasLongPathPrefixAlt(path) {
|
|
226
|
+
return path.startsWith(uncLongPathPrefixAlt);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* If the path has the long path unc prefix (`\\.\`), replace it with the standard long path prefix (`\\?\`). This is needed to access paths longer than 260 characters on Windows.
|
|
230
|
+
* @param path - any path that may or may not have the long path prefix.
|
|
231
|
+
* @returns the path with the standard long path prefix if needed.
|
|
232
|
+
*/
|
|
233
|
+
function fixLongPathPrefix(path) {
|
|
234
|
+
if (!hasLongPathPrefixAlt(path)) return path;
|
|
235
|
+
return uncLongPathPrefix + path.slice(4);
|
|
236
|
+
}
|
|
220
237
|
//#endregion
|
|
221
238
|
//#region src/FileUrlBuilder.mts
|
|
222
239
|
const isWindowsPathRegEx = regExpWindowsPathDriveLetter;
|
|
@@ -300,6 +317,7 @@ var FileUrlBuilder = class {
|
|
|
300
317
|
if (typeof filenameOrUrl !== "string") return filenameOrUrl;
|
|
301
318
|
if (isUrlLike(filenameOrUrl)) return normalizeWindowsUrl(new URL(filenameOrUrl));
|
|
302
319
|
relativeTo ??= this.cwd;
|
|
320
|
+
if (isWindows && isUncPath(filenameOrUrl)) return pathToFileURL(fixLongPathPrefix(filenameOrUrl));
|
|
303
321
|
isWindows && (filenameOrUrl = filenameOrUrl.replaceAll("\\", "/"));
|
|
304
322
|
if (this.isAbsolute(filenameOrUrl) && isFileURL(relativeTo)) {
|
|
305
323
|
const pathname = this.normalizeFilePathForUrl(filenameOrUrl);
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public",
|
|
5
5
|
"provenance": true
|
|
6
6
|
},
|
|
7
|
-
"version": "10.0
|
|
7
|
+
"version": "10.1.0",
|
|
8
8
|
"description": "URL parsing and manipulation utilities for cspell",
|
|
9
9
|
"keywords": [
|
|
10
10
|
"cspell",
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"engines": {
|
|
58
58
|
"node": ">=22.18.0"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "401518c5e37c06fb406aedadb3fc7a1c27eca8e6"
|
|
61
61
|
}
|