@speclynx/apidom-reference 3.2.1 → 4.0.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.
Files changed (192) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/apidom-reference.browser.js +40 -36
  3. package/dist/apidom-reference.browser.min.js +1 -1
  4. package/package.json +25 -25
  5. package/types/dereference/strategies/apidom/visitor.d.ts +1 -1
  6. package/types/dereference/strategies/arazzo-1/visitor.d.ts +1 -1
  7. package/types/dereference/strategies/asyncapi-2/visitor.d.ts +1 -1
  8. package/types/dereference/strategies/openapi-2/visitor.d.ts +1 -1
  9. package/types/dereference/strategies/openapi-3-0/visitor.d.ts +1 -1
  10. package/types/dereference/strategies/openapi-3-1/visitor.d.ts +1 -1
  11. package/src/File.cjs +0 -50
  12. package/src/File.mjs +0 -44
  13. package/src/Reference.cjs +0 -31
  14. package/src/Reference.mjs +0 -27
  15. package/src/ReferenceSet.cjs +0 -60
  16. package/src/ReferenceSet.mjs +0 -57
  17. package/src/bundle/index.cjs +0 -61
  18. package/src/bundle/index.mjs +0 -55
  19. package/src/bundle/strategies/BundleStrategy.cjs +0 -20
  20. package/src/bundle/strategies/BundleStrategy.mjs +0 -16
  21. package/src/bundle/strategies/openapi-3-1/index.cjs +0 -35
  22. package/src/bundle/strategies/openapi-3-1/index.mjs +0 -29
  23. package/src/configuration/empty.cjs +0 -9
  24. package/src/configuration/empty.mjs +0 -1
  25. package/src/configuration/saturated.cjs +0 -88
  26. package/src/configuration/saturated.mjs +0 -80
  27. package/src/dereference/index.cjs +0 -90
  28. package/src/dereference/index.mjs +0 -83
  29. package/src/dereference/strategies/DereferenceStrategy.cjs +0 -20
  30. package/src/dereference/strategies/DereferenceStrategy.mjs +0 -16
  31. package/src/dereference/strategies/apidom/index.cjs +0 -89
  32. package/src/dereference/strategies/apidom/index.mjs +0 -83
  33. package/src/dereference/strategies/apidom/selectors/element-id.cjs +0 -47
  34. package/src/dereference/strategies/apidom/selectors/element-id.mjs +0 -41
  35. package/src/dereference/strategies/apidom/visitor.cjs +0 -266
  36. package/src/dereference/strategies/apidom/visitor.mjs +0 -259
  37. package/src/dereference/strategies/arazzo-1/index.cjs +0 -109
  38. package/src/dereference/strategies/arazzo-1/index.mjs +0 -100
  39. package/src/dereference/strategies/arazzo-1/selectors/$anchor.cjs +0 -12
  40. package/src/dereference/strategies/arazzo-1/selectors/$anchor.mjs +0 -1
  41. package/src/dereference/strategies/arazzo-1/selectors/uri.cjs +0 -8
  42. package/src/dereference/strategies/arazzo-1/selectors/uri.mjs +0 -1
  43. package/src/dereference/strategies/arazzo-1/source-descriptions.cjs +0 -248
  44. package/src/dereference/strategies/arazzo-1/source-descriptions.mjs +0 -243
  45. package/src/dereference/strategies/arazzo-1/util.cjs +0 -37
  46. package/src/dereference/strategies/arazzo-1/util.mjs +0 -29
  47. package/src/dereference/strategies/arazzo-1/visitor.cjs +0 -507
  48. package/src/dereference/strategies/arazzo-1/visitor.mjs +0 -500
  49. package/src/dereference/strategies/asyncapi-2/index.cjs +0 -94
  50. package/src/dereference/strategies/asyncapi-2/index.mjs +0 -88
  51. package/src/dereference/strategies/asyncapi-2/visitor.cjs +0 -501
  52. package/src/dereference/strategies/asyncapi-2/visitor.mjs +0 -494
  53. package/src/dereference/strategies/openapi-2/index.cjs +0 -96
  54. package/src/dereference/strategies/openapi-2/index.mjs +0 -90
  55. package/src/dereference/strategies/openapi-2/visitor.cjs +0 -629
  56. package/src/dereference/strategies/openapi-2/visitor.mjs +0 -622
  57. package/src/dereference/strategies/openapi-3-0/index.cjs +0 -96
  58. package/src/dereference/strategies/openapi-3-0/index.mjs +0 -90
  59. package/src/dereference/strategies/openapi-3-0/visitor.cjs +0 -621
  60. package/src/dereference/strategies/openapi-3-0/visitor.mjs +0 -614
  61. package/src/dereference/strategies/openapi-3-1/index.cjs +0 -99
  62. package/src/dereference/strategies/openapi-3-1/index.mjs +0 -90
  63. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.cjs +0 -67
  64. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.mjs +0 -56
  65. package/src/dereference/strategies/openapi-3-1/selectors/uri.cjs +0 -50
  66. package/src/dereference/strategies/openapi-3-1/selectors/uri.mjs +0 -42
  67. package/src/dereference/strategies/openapi-3-1/util.cjs +0 -68
  68. package/src/dereference/strategies/openapi-3-1/util.mjs +0 -59
  69. package/src/dereference/strategies/openapi-3-1/visitor.cjs +0 -873
  70. package/src/dereference/strategies/openapi-3-1/visitor.mjs +0 -866
  71. package/src/dereference/util.cjs +0 -31
  72. package/src/dereference/util.mjs +0 -27
  73. package/src/errors/BundleError.cjs +0 -10
  74. package/src/errors/BundleError.mjs +0 -7
  75. package/src/errors/DereferenceError.cjs +0 -10
  76. package/src/errors/DereferenceError.mjs +0 -7
  77. package/src/errors/EvaluationElementIdError.cjs +0 -10
  78. package/src/errors/EvaluationElementIdError.mjs +0 -7
  79. package/src/errors/EvaluationJsonSchema$anchorError.cjs +0 -11
  80. package/src/errors/EvaluationJsonSchema$anchorError.mjs +0 -6
  81. package/src/errors/EvaluationJsonSchemaUriError.cjs +0 -11
  82. package/src/errors/EvaluationJsonSchemaUriError.mjs +0 -6
  83. package/src/errors/InvalidJsonSchema$anchorError.cjs +0 -15
  84. package/src/errors/InvalidJsonSchema$anchorError.mjs +0 -10
  85. package/src/errors/JsonSchema$anchorError.cjs +0 -10
  86. package/src/errors/JsonSchema$anchorError.mjs +0 -7
  87. package/src/errors/JsonSchemaUriError.cjs +0 -10
  88. package/src/errors/JsonSchemaUriError.mjs +0 -7
  89. package/src/errors/MaximumBundleDepthError.cjs +0 -11
  90. package/src/errors/MaximumBundleDepthError.mjs +0 -6
  91. package/src/errors/MaximumDereferenceDepthError.cjs +0 -11
  92. package/src/errors/MaximumDereferenceDepthError.mjs +0 -6
  93. package/src/errors/MaximumResolveDepthError.cjs +0 -11
  94. package/src/errors/MaximumResolveDepthError.mjs +0 -6
  95. package/src/errors/ParseError.cjs +0 -10
  96. package/src/errors/ParseError.mjs +0 -7
  97. package/src/errors/ParserError.cjs +0 -11
  98. package/src/errors/ParserError.mjs +0 -6
  99. package/src/errors/PluginError.cjs +0 -18
  100. package/src/errors/PluginError.mjs +0 -15
  101. package/src/errors/ResolveError.cjs +0 -10
  102. package/src/errors/ResolveError.mjs +0 -7
  103. package/src/errors/ResolverError.cjs +0 -11
  104. package/src/errors/ResolverError.mjs +0 -6
  105. package/src/errors/UnmatchedBundleStrategyError.cjs +0 -11
  106. package/src/errors/UnmatchedBundleStrategyError.mjs +0 -6
  107. package/src/errors/UnmatchedDereferenceStrategyError.cjs +0 -11
  108. package/src/errors/UnmatchedDereferenceStrategyError.mjs +0 -6
  109. package/src/errors/UnmatchedParserError.cjs +0 -11
  110. package/src/errors/UnmatchedParserError.mjs +0 -6
  111. package/src/errors/UnmatchedResolveStrategyError.cjs +0 -11
  112. package/src/errors/UnmatchedResolveStrategyError.mjs +0 -6
  113. package/src/errors/UnmatchedResolverError.cjs +0 -11
  114. package/src/errors/UnmatchedResolverError.mjs +0 -6
  115. package/src/errors/UnresolvableReferenceError.cjs +0 -11
  116. package/src/errors/UnresolvableReferenceError.mjs +0 -6
  117. package/src/index.cjs +0 -146
  118. package/src/index.mjs +0 -103
  119. package/src/options/index.cjs +0 -194
  120. package/src/options/index.mjs +0 -191
  121. package/src/options/util.cjs +0 -24
  122. package/src/options/util.mjs +0 -19
  123. package/src/parse/index.cjs +0 -69
  124. package/src/parse/index.mjs +0 -63
  125. package/src/parse/parsers/Parser.cjs +0 -62
  126. package/src/parse/parsers/Parser.mjs +0 -58
  127. package/src/parse/parsers/apidom-json/index.cjs +0 -70
  128. package/src/parse/parsers/apidom-json/index.mjs +0 -64
  129. package/src/parse/parsers/arazzo-json-1/index.cjs +0 -62
  130. package/src/parse/parsers/arazzo-json-1/index.mjs +0 -56
  131. package/src/parse/parsers/arazzo-json-1/source-descriptions.cjs +0 -221
  132. package/src/parse/parsers/arazzo-json-1/source-descriptions.mjs +0 -214
  133. package/src/parse/parsers/arazzo-yaml-1/index.cjs +0 -62
  134. package/src/parse/parsers/arazzo-yaml-1/index.mjs +0 -56
  135. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.cjs +0 -12
  136. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.mjs +0 -7
  137. package/src/parse/parsers/asyncapi-json-2/index.cjs +0 -54
  138. package/src/parse/parsers/asyncapi-json-2/index.mjs +0 -48
  139. package/src/parse/parsers/asyncapi-yaml-2/index.cjs +0 -54
  140. package/src/parse/parsers/asyncapi-yaml-2/index.mjs +0 -48
  141. package/src/parse/parsers/binary/index-browser.cjs +0 -56
  142. package/src/parse/parsers/binary/index-browser.mjs +0 -50
  143. package/src/parse/parsers/binary/index-node.cjs +0 -51
  144. package/src/parse/parsers/binary/index-node.mjs +0 -45
  145. package/src/parse/parsers/json/index.cjs +0 -53
  146. package/src/parse/parsers/json/index.mjs +0 -47
  147. package/src/parse/parsers/openapi-json-2/index.cjs +0 -54
  148. package/src/parse/parsers/openapi-json-2/index.mjs +0 -48
  149. package/src/parse/parsers/openapi-json-3-0/index.cjs +0 -54
  150. package/src/parse/parsers/openapi-json-3-0/index.mjs +0 -48
  151. package/src/parse/parsers/openapi-json-3-1/index.cjs +0 -54
  152. package/src/parse/parsers/openapi-json-3-1/index.mjs +0 -48
  153. package/src/parse/parsers/openapi-yaml-2/index.cjs +0 -54
  154. package/src/parse/parsers/openapi-yaml-2/index.mjs +0 -48
  155. package/src/parse/parsers/openapi-yaml-3-0/index.cjs +0 -54
  156. package/src/parse/parsers/openapi-yaml-3-0/index.mjs +0 -48
  157. package/src/parse/parsers/openapi-yaml-3-1/index.cjs +0 -54
  158. package/src/parse/parsers/openapi-yaml-3-1/index.mjs +0 -48
  159. package/src/parse/parsers/yaml-1-2/index.cjs +0 -56
  160. package/src/parse/parsers/yaml-1-2/index.mjs +0 -50
  161. package/src/resolve/index.cjs +0 -67
  162. package/src/resolve/index.mjs +0 -60
  163. package/src/resolve/resolvers/HTTPResolver.cjs +0 -45
  164. package/src/resolve/resolvers/HTTPResolver.mjs +0 -37
  165. package/src/resolve/resolvers/Resolver.cjs +0 -20
  166. package/src/resolve/resolvers/Resolver.mjs +0 -16
  167. package/src/resolve/resolvers/file/index-browser.cjs +0 -24
  168. package/src/resolve/resolvers/file/index-browser.mjs +0 -19
  169. package/src/resolve/resolvers/file/index-node.cjs +0 -49
  170. package/src/resolve/resolvers/file/index-node.mjs +0 -42
  171. package/src/resolve/resolvers/http-axios/cache/MemoryCache.cjs +0 -41
  172. package/src/resolve/resolvers/http-axios/cache/MemoryCache.mjs +0 -37
  173. package/src/resolve/resolvers/http-axios/index.cjs +0 -113
  174. package/src/resolve/resolvers/http-axios/index.mjs +0 -105
  175. package/src/resolve/strategies/ResolveStrategy.cjs +0 -20
  176. package/src/resolve/strategies/ResolveStrategy.mjs +0 -16
  177. package/src/resolve/strategies/apidom/index.cjs +0 -49
  178. package/src/resolve/strategies/apidom/index.mjs +0 -43
  179. package/src/resolve/strategies/asyncapi-2/index.cjs +0 -49
  180. package/src/resolve/strategies/asyncapi-2/index.mjs +0 -43
  181. package/src/resolve/strategies/openapi-2/index.cjs +0 -49
  182. package/src/resolve/strategies/openapi-2/index.mjs +0 -43
  183. package/src/resolve/strategies/openapi-3-0/index.cjs +0 -49
  184. package/src/resolve/strategies/openapi-3-0/index.mjs +0 -43
  185. package/src/resolve/strategies/openapi-3-1/index.cjs +0 -49
  186. package/src/resolve/strategies/openapi-3-1/index.mjs +0 -43
  187. package/src/resolve/util.cjs +0 -37
  188. package/src/resolve/util.mjs +0 -30
  189. package/src/util/plugins.cjs +0 -39
  190. package/src/util/plugins.mjs +0 -34
  191. package/src/util/url.cjs +0 -288
  192. package/src/util/url.mjs +0 -274
package/src/util/url.cjs DELETED
@@ -1,288 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.unsanitize = exports.toFileSystemPath = exports.stripHash = exports.sanitize = exports.resolve = exports.isURI = exports.isHttpUrl = exports.isFileSystemPath = exports.hasProtocol = exports.getProtocol = exports.getHash = exports.getExtension = exports.fromFileSystemPath = exports.cwd = void 0;
6
- var _process = _interopRequireDefault(require("process"));
7
- var _ramda = require("ramda");
8
- var _ramdaAdjunct = require("ramda-adjunct");
9
- /**
10
- * SPDX-FileCopyrightText: Copyright (c) 2015 James Messinger
11
- *
12
- * SPDX-License-Identifier: MIT
13
- */
14
-
15
- /**
16
- * @public
17
- */
18
-
19
- const isWindows = () => (0, _ramda.pathSatisfies)((0, _ramda.test)(/^win/), ['platform'], _process.default);
20
-
21
- /**
22
- * Returns the protocol of the given URL, or `undefined` if it has no protocol.
23
- * @public
24
- */
25
- const getProtocol = url => {
26
- try {
27
- const parsedUrl = new URL(url);
28
- return (0, _ramdaAdjunct.trimCharsEnd)(':', parsedUrl.protocol);
29
- } catch {
30
- return undefined;
31
- }
32
- };
33
-
34
- /**
35
- * Returns true if given URL has protocol.
36
- * @public
37
- */
38
- exports.getProtocol = getProtocol;
39
- const hasProtocol = exports.hasProtocol = (0, _ramda.pipe)(getProtocol, _ramdaAdjunct.isNotUndefined);
40
-
41
- /**
42
- * Returns the lower-cased file extension of the given URL,
43
- * or an empty string if it has no extension.
44
- * @public
45
- */
46
- const getExtension = url => {
47
- const lastDotPosition = url.lastIndexOf('.');
48
- if (lastDotPosition >= 0) {
49
- return url.substring(lastDotPosition).toLowerCase();
50
- }
51
- return '';
52
- };
53
-
54
- /**
55
- * Determines whether the given path is a filesystem path.
56
- * This includes "file://" URLs.
57
- * @public
58
- */
59
- exports.getExtension = getExtension;
60
- const isFileSystemPath = uri => {
61
- const protocol = getProtocol(uri);
62
- return (0, _ramdaAdjunct.isUndefined)(protocol) || protocol === 'file' || /^[a-zA-Z]$/.test(protocol);
63
- };
64
-
65
- /**
66
- * Determines whether the given URI is an HTTP(S) URL.
67
- * @public
68
- */
69
- exports.isFileSystemPath = isFileSystemPath;
70
- const isHttpUrl = url => {
71
- const protocol = getProtocol(url);
72
- return protocol === 'http' || protocol === 'https';
73
- };
74
-
75
- /**
76
- * Determines whether the given URI
77
- * @public
78
- */
79
- exports.isHttpUrl = isHttpUrl;
80
- const isURI = uri => {
81
- try {
82
- new URL(uri);
83
- return true;
84
- } catch {
85
- return false;
86
- }
87
- };
88
-
89
- /**
90
- * @public
91
- */
92
- exports.isURI = isURI;
93
- /**
94
- * Converts a URL to a local filesystem path.
95
- * @public
96
- */
97
- const toFileSystemPath = (uri, options) => {
98
- // RegExp patterns to URL-decode special characters for local filesystem paths
99
- const urlDecodePatterns = [/%23/g, '#', /%24/g, '$', /%26/g, '&', /%2C/g, ',', /%40/g, '@'];
100
- const keepFileProtocol = (0, _ramda.propOr)(false, 'keepFileProtocol', options);
101
- const isWindowsPredicate = (0, _ramda.propOr)(isWindows, 'isWindows', options);
102
-
103
- // Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc.
104
- let path = decodeURI(uri);
105
-
106
- // Step 2: Manually decode characters that are not decoded by `decodeURI`.
107
- // This includes characters such as "#" and "?", which have special meaning in URLs,
108
- // but are just normal characters in a filesystem path.
109
- for (let i = 0; i < urlDecodePatterns.length; i += 2) {
110
- // @ts-ignore
111
- path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]);
112
- }
113
-
114
- // Step 3: If it's a "file://" URL, then format it consistently
115
- // or convert it to a local filesystem path
116
- let isFileUrl = path.substring(0, 7).toLowerCase() === 'file://';
117
- if (isFileUrl) {
118
- // Strip-off the protocol, and the initial "/", if there is one
119
- path = path[7] === '/' ? path.substring(8) : path.substring(7);
120
-
121
- // insert a colon (":") after the drive letter on Windows
122
- if (isWindowsPredicate() && path[1] === '/') {
123
- path = `${path[0]}:${path.substring(1)}`;
124
- }
125
- if (keepFileProtocol) {
126
- // Return the consistently-formatted "file://" URL
127
- path = `file:///${path}`;
128
- } else {
129
- // Convert the "file://" URL to a local filesystem path.
130
- // On Windows, it will start with something like "C:/".
131
- // On Posix, it will start with "/"
132
- isFileUrl = false;
133
- path = isWindowsPredicate() ? path : `/${path}`;
134
- }
135
- }
136
-
137
- // Step 4: Normalize Windows paths (unless it's a "file://" URL)
138
- if (isWindowsPredicate() && !isFileUrl) {
139
- // Replace forward slashes with backslashes
140
- path = (0, _ramdaAdjunct.replaceAll)('/', '\\', path);
141
-
142
- // Capitalize the drive letter
143
- if (path.substring(1, 3) === ':\\') {
144
- path = path[0].toUpperCase() + path.substring(1);
145
- }
146
- }
147
- return path;
148
- };
149
-
150
- /**
151
- * Converts a filesystem path to a properly-encoded URL.
152
- *
153
- * This is intended to handle situations where resolver is called
154
- * with a filesystem path that contains characters which are not allowed in URLs.
155
- *
156
- * @example
157
- * The following filesystem paths would be converted to the following URLs:
158
- *```
159
- * <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json
160
- * C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json
161
- * file://Project #42/file.json ==> file://Project%20%2342/file.json
162
- * ```
163
- * @public
164
- */
165
- exports.toFileSystemPath = toFileSystemPath;
166
- const fromFileSystemPath = uri => {
167
- const urlEncodePatterns = [/\?/g, '%3F', /#/g, '%23'];
168
- let path = uri;
169
-
170
- // Step 1: On Windows, replace backslashes with forward slashes,
171
- // rather than encoding them as "%5C"
172
- if (isWindows()) {
173
- path = path.replace(/\\/g, '/');
174
- }
175
-
176
- // Step 2: `encodeURI` will take care of MOST characters
177
- path = encodeURI(path);
178
-
179
- // Step 3: Manually encode characters that are not encoded by `encodeURI`.
180
- // This includes characters such as "#" and "?", which have special meaning in URLs,
181
- // but are just normal characters in a filesystem path.
182
- for (let i = 0; i < urlEncodePatterns.length; i += 2) {
183
- // @ts-ignore
184
- path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]);
185
- }
186
- return path;
187
- };
188
-
189
- /**
190
- * Returns the hash (URL fragment), of the given path.
191
- * If there is no hash, then the root hash ("#") is returned.
192
- * @public
193
- */
194
- exports.fromFileSystemPath = fromFileSystemPath;
195
- const getHash = uri => {
196
- const hashIndex = uri.indexOf('#');
197
- if (hashIndex !== -1) {
198
- return uri.substring(hashIndex);
199
- }
200
- return '#';
201
- };
202
-
203
- /**
204
- * Removes the hash (URL fragment), if any, from the given path.
205
- * @public
206
- */
207
- exports.getHash = getHash;
208
- const stripHash = uri => {
209
- const hashIndex = uri.indexOf('#');
210
- let hashStrippedUri = uri;
211
- if (hashIndex >= 0) {
212
- hashStrippedUri = uri.substring(0, hashIndex);
213
- }
214
- return hashStrippedUri;
215
- };
216
-
217
- /**
218
- * Returns the current working directory (in Node) or the current page URL (in browsers).
219
- * @public
220
- */
221
- exports.stripHash = stripHash;
222
- const cwd = () => {
223
- // @ts-ignore
224
- if (_process.default.browser) {
225
- return stripHash(globalThis.location.href);
226
- }
227
- const path = _process.default.cwd();
228
- const lastChar = (0, _ramda.last)(path);
229
- if (['/', '\\'].includes(lastChar)) {
230
- return path;
231
- }
232
- return path + (isWindows() ? '\\' : '/');
233
- };
234
-
235
- /**
236
- * Resolves a target URI relative to a base URI in a manner similar to that of a Web browser resolving an anchor tag HREF.
237
- * @public
238
- */
239
- exports.cwd = cwd;
240
- const resolve = (from, to) => {
241
- const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
242
- if (resolvedUrl.protocol === 'resolve:') {
243
- // `from` is a relative URL.
244
- const {
245
- pathname,
246
- search,
247
- hash
248
- } = resolvedUrl;
249
- return pathname + search + hash;
250
- }
251
- return resolvedUrl.toString();
252
- };
253
-
254
- /**
255
- * Sanitizes/Encodes URI to it's url encoded form.
256
- *
257
- * The functional will compensate with the usecase when
258
- * already sanitized URI is passed to it,
259
- * by first unsatizing it and then performing sanitization again.
260
- * @public
261
- */
262
- exports.resolve = resolve;
263
- const sanitize = uri => {
264
- if (isFileSystemPath(uri)) {
265
- return fromFileSystemPath(toFileSystemPath(uri));
266
- }
267
- try {
268
- return new URL(uri).toString();
269
- } catch {
270
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_ipv6
271
- return encodeURI(decodeURI(uri)).replace(/%5B/g, '[').replace(/%5D/g, ']');
272
- }
273
- };
274
-
275
- /**
276
- * Unsanitizes/Decodes URI to it's url encoded form.
277
- * This function already assumes that hash part of the URI
278
- * has been removed prior to transforming it to it's sanitized form.
279
- * @public
280
- */
281
- exports.sanitize = sanitize;
282
- const unsanitize = uri => {
283
- if (isFileSystemPath(uri)) {
284
- return toFileSystemPath(uri);
285
- }
286
- return decodeURI(uri);
287
- };
288
- exports.unsanitize = unsanitize;
package/src/util/url.mjs DELETED
@@ -1,274 +0,0 @@
1
- import process from 'process';
2
- import { pathSatisfies, propOr, pipe, test, last } from 'ramda';
3
- import { isUndefined, replaceAll, isNotUndefined, trimCharsEnd } from 'ramda-adjunct';
4
-
5
- /**
6
- * SPDX-FileCopyrightText: Copyright (c) 2015 James Messinger
7
- *
8
- * SPDX-License-Identifier: MIT
9
- */
10
-
11
- /**
12
- * @public
13
- */
14
-
15
- const isWindows = () => pathSatisfies(test(/^win/), ['platform'], process);
16
-
17
- /**
18
- * Returns the protocol of the given URL, or `undefined` if it has no protocol.
19
- * @public
20
- */
21
- export const getProtocol = url => {
22
- try {
23
- const parsedUrl = new URL(url);
24
- return trimCharsEnd(':', parsedUrl.protocol);
25
- } catch {
26
- return undefined;
27
- }
28
- };
29
-
30
- /**
31
- * Returns true if given URL has protocol.
32
- * @public
33
- */
34
- export const hasProtocol = pipe(getProtocol, isNotUndefined);
35
-
36
- /**
37
- * Returns the lower-cased file extension of the given URL,
38
- * or an empty string if it has no extension.
39
- * @public
40
- */
41
- export const getExtension = url => {
42
- const lastDotPosition = url.lastIndexOf('.');
43
- if (lastDotPosition >= 0) {
44
- return url.substring(lastDotPosition).toLowerCase();
45
- }
46
- return '';
47
- };
48
-
49
- /**
50
- * Determines whether the given path is a filesystem path.
51
- * This includes "file://" URLs.
52
- * @public
53
- */
54
- export const isFileSystemPath = uri => {
55
- const protocol = getProtocol(uri);
56
- return isUndefined(protocol) || protocol === 'file' || /^[a-zA-Z]$/.test(protocol);
57
- };
58
-
59
- /**
60
- * Determines whether the given URI is an HTTP(S) URL.
61
- * @public
62
- */
63
- export const isHttpUrl = url => {
64
- const protocol = getProtocol(url);
65
- return protocol === 'http' || protocol === 'https';
66
- };
67
-
68
- /**
69
- * Determines whether the given URI
70
- * @public
71
- */
72
- export const isURI = uri => {
73
- try {
74
- new URL(uri);
75
- return true;
76
- } catch {
77
- return false;
78
- }
79
- };
80
-
81
- /**
82
- * @public
83
- */
84
-
85
- /**
86
- * Converts a URL to a local filesystem path.
87
- * @public
88
- */
89
- export const toFileSystemPath = (uri, options) => {
90
- // RegExp patterns to URL-decode special characters for local filesystem paths
91
- const urlDecodePatterns = [/%23/g, '#', /%24/g, '$', /%26/g, '&', /%2C/g, ',', /%40/g, '@'];
92
- const keepFileProtocol = propOr(false, 'keepFileProtocol', options);
93
- const isWindowsPredicate = propOr(isWindows, 'isWindows', options);
94
-
95
- // Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc.
96
- let path = decodeURI(uri);
97
-
98
- // Step 2: Manually decode characters that are not decoded by `decodeURI`.
99
- // This includes characters such as "#" and "?", which have special meaning in URLs,
100
- // but are just normal characters in a filesystem path.
101
- for (let i = 0; i < urlDecodePatterns.length; i += 2) {
102
- // @ts-ignore
103
- path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]);
104
- }
105
-
106
- // Step 3: If it's a "file://" URL, then format it consistently
107
- // or convert it to a local filesystem path
108
- let isFileUrl = path.substring(0, 7).toLowerCase() === 'file://';
109
- if (isFileUrl) {
110
- // Strip-off the protocol, and the initial "/", if there is one
111
- path = path[7] === '/' ? path.substring(8) : path.substring(7);
112
-
113
- // insert a colon (":") after the drive letter on Windows
114
- if (isWindowsPredicate() && path[1] === '/') {
115
- path = `${path[0]}:${path.substring(1)}`;
116
- }
117
- if (keepFileProtocol) {
118
- // Return the consistently-formatted "file://" URL
119
- path = `file:///${path}`;
120
- } else {
121
- // Convert the "file://" URL to a local filesystem path.
122
- // On Windows, it will start with something like "C:/".
123
- // On Posix, it will start with "/"
124
- isFileUrl = false;
125
- path = isWindowsPredicate() ? path : `/${path}`;
126
- }
127
- }
128
-
129
- // Step 4: Normalize Windows paths (unless it's a "file://" URL)
130
- if (isWindowsPredicate() && !isFileUrl) {
131
- // Replace forward slashes with backslashes
132
- path = replaceAll('/', '\\', path);
133
-
134
- // Capitalize the drive letter
135
- if (path.substring(1, 3) === ':\\') {
136
- path = path[0].toUpperCase() + path.substring(1);
137
- }
138
- }
139
- return path;
140
- };
141
-
142
- /**
143
- * Converts a filesystem path to a properly-encoded URL.
144
- *
145
- * This is intended to handle situations where resolver is called
146
- * with a filesystem path that contains characters which are not allowed in URLs.
147
- *
148
- * @example
149
- * The following filesystem paths would be converted to the following URLs:
150
- *```
151
- * <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json
152
- * C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json
153
- * file://Project #42/file.json ==> file://Project%20%2342/file.json
154
- * ```
155
- * @public
156
- */
157
- export const fromFileSystemPath = uri => {
158
- const urlEncodePatterns = [/\?/g, '%3F', /#/g, '%23'];
159
- let path = uri;
160
-
161
- // Step 1: On Windows, replace backslashes with forward slashes,
162
- // rather than encoding them as "%5C"
163
- if (isWindows()) {
164
- path = path.replace(/\\/g, '/');
165
- }
166
-
167
- // Step 2: `encodeURI` will take care of MOST characters
168
- path = encodeURI(path);
169
-
170
- // Step 3: Manually encode characters that are not encoded by `encodeURI`.
171
- // This includes characters such as "#" and "?", which have special meaning in URLs,
172
- // but are just normal characters in a filesystem path.
173
- for (let i = 0; i < urlEncodePatterns.length; i += 2) {
174
- // @ts-ignore
175
- path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]);
176
- }
177
- return path;
178
- };
179
-
180
- /**
181
- * Returns the hash (URL fragment), of the given path.
182
- * If there is no hash, then the root hash ("#") is returned.
183
- * @public
184
- */
185
- export const getHash = uri => {
186
- const hashIndex = uri.indexOf('#');
187
- if (hashIndex !== -1) {
188
- return uri.substring(hashIndex);
189
- }
190
- return '#';
191
- };
192
-
193
- /**
194
- * Removes the hash (URL fragment), if any, from the given path.
195
- * @public
196
- */
197
- export const stripHash = uri => {
198
- const hashIndex = uri.indexOf('#');
199
- let hashStrippedUri = uri;
200
- if (hashIndex >= 0) {
201
- hashStrippedUri = uri.substring(0, hashIndex);
202
- }
203
- return hashStrippedUri;
204
- };
205
-
206
- /**
207
- * Returns the current working directory (in Node) or the current page URL (in browsers).
208
- * @public
209
- */
210
- export const cwd = () => {
211
- // @ts-ignore
212
- if (process.browser) {
213
- return stripHash(globalThis.location.href);
214
- }
215
- const path = process.cwd();
216
- const lastChar = last(path);
217
- if (['/', '\\'].includes(lastChar)) {
218
- return path;
219
- }
220
- return path + (isWindows() ? '\\' : '/');
221
- };
222
-
223
- /**
224
- * Resolves a target URI relative to a base URI in a manner similar to that of a Web browser resolving an anchor tag HREF.
225
- * @public
226
- */
227
- export const resolve = (from, to) => {
228
- const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
229
- if (resolvedUrl.protocol === 'resolve:') {
230
- // `from` is a relative URL.
231
- const {
232
- pathname,
233
- search,
234
- hash
235
- } = resolvedUrl;
236
- return pathname + search + hash;
237
- }
238
- return resolvedUrl.toString();
239
- };
240
-
241
- /**
242
- * Sanitizes/Encodes URI to it's url encoded form.
243
- *
244
- * The functional will compensate with the usecase when
245
- * already sanitized URI is passed to it,
246
- * by first unsatizing it and then performing sanitization again.
247
- * @public
248
- */
249
-
250
- export const sanitize = uri => {
251
- if (isFileSystemPath(uri)) {
252
- return fromFileSystemPath(toFileSystemPath(uri));
253
- }
254
- try {
255
- return new URL(uri).toString();
256
- } catch {
257
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_ipv6
258
- return encodeURI(decodeURI(uri)).replace(/%5B/g, '[').replace(/%5D/g, ']');
259
- }
260
- };
261
-
262
- /**
263
- * Unsanitizes/Decodes URI to it's url encoded form.
264
- * This function already assumes that hash part of the URI
265
- * has been removed prior to transforming it to it's sanitized form.
266
- * @public
267
- */
268
-
269
- export const unsanitize = uri => {
270
- if (isFileSystemPath(uri)) {
271
- return toFileSystemPath(uri);
272
- }
273
- return decodeURI(uri);
274
- };