@speclynx/apidom-reference 1.12.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.
- package/CHANGELOG.md +86 -0
- package/LICENSE +202 -0
- package/LICENSES/AFL-3.0.txt +182 -0
- package/LICENSES/Apache-2.0.txt +202 -0
- package/LICENSES/BSD-3-Clause.txt +26 -0
- package/LICENSES/MIT.txt +9 -0
- package/NOTICE +65 -0
- package/README.md +2107 -0
- package/dist/167.apidom-reference.browser.js +10 -0
- package/dist/167.apidom-reference.browser.min.js +1 -0
- package/dist/451.apidom-reference.browser.js +10 -0
- package/dist/451.apidom-reference.browser.min.js +1 -0
- package/dist/9786785aaddf11f37840fad896531940.wasm +0 -0
- package/dist/apidom-reference.browser.js +85376 -0
- package/dist/apidom-reference.browser.min.js +1 -0
- package/package.json +304 -0
- package/src/File.cjs +50 -0
- package/src/File.mjs +44 -0
- package/src/Reference.cjs +31 -0
- package/src/Reference.mjs +27 -0
- package/src/ReferenceSet.cjs +60 -0
- package/src/ReferenceSet.mjs +57 -0
- package/src/bundle/index.cjs +61 -0
- package/src/bundle/index.mjs +55 -0
- package/src/bundle/strategies/BundleStrategy.cjs +20 -0
- package/src/bundle/strategies/BundleStrategy.mjs +16 -0
- package/src/bundle/strategies/openapi-3-1/index.cjs +35 -0
- package/src/bundle/strategies/openapi-3-1/index.mjs +29 -0
- package/src/configuration/empty.cjs +9 -0
- package/src/configuration/empty.mjs +1 -0
- package/src/configuration/saturated.cjs +95 -0
- package/src/configuration/saturated.mjs +87 -0
- package/src/dereference/index.cjs +86 -0
- package/src/dereference/index.mjs +79 -0
- package/src/dereference/strategies/DereferenceStrategy.cjs +20 -0
- package/src/dereference/strategies/DereferenceStrategy.mjs +16 -0
- package/src/dereference/strategies/apidom/index.cjs +89 -0
- package/src/dereference/strategies/apidom/index.mjs +84 -0
- package/src/dereference/strategies/apidom/selectors/element-id.cjs +36 -0
- package/src/dereference/strategies/apidom/selectors/element-id.mjs +30 -0
- package/src/dereference/strategies/apidom/visitor.cjs +165 -0
- package/src/dereference/strategies/apidom/visitor.mjs +159 -0
- package/src/dereference/strategies/asyncapi-2/index.cjs +100 -0
- package/src/dereference/strategies/asyncapi-2/index.mjs +94 -0
- package/src/dereference/strategies/asyncapi-2/visitor.cjs +412 -0
- package/src/dereference/strategies/asyncapi-2/visitor.mjs +406 -0
- package/src/dereference/strategies/openapi-2/index.cjs +102 -0
- package/src/dereference/strategies/openapi-2/index.mjs +96 -0
- package/src/dereference/strategies/openapi-2/visitor.cjs +530 -0
- package/src/dereference/strategies/openapi-2/visitor.mjs +524 -0
- package/src/dereference/strategies/openapi-3-0/index.cjs +102 -0
- package/src/dereference/strategies/openapi-3-0/index.mjs +96 -0
- package/src/dereference/strategies/openapi-3-0/visitor.cjs +519 -0
- package/src/dereference/strategies/openapi-3-0/visitor.mjs +513 -0
- package/src/dereference/strategies/openapi-3-1/index.cjs +105 -0
- package/src/dereference/strategies/openapi-3-1/index.mjs +96 -0
- package/src/dereference/strategies/openapi-3-1/selectors/$anchor.cjs +66 -0
- package/src/dereference/strategies/openapi-3-1/selectors/$anchor.mjs +55 -0
- package/src/dereference/strategies/openapi-3-1/selectors/uri.cjs +50 -0
- package/src/dereference/strategies/openapi-3-1/selectors/uri.mjs +42 -0
- package/src/dereference/strategies/openapi-3-1/util.cjs +67 -0
- package/src/dereference/strategies/openapi-3-1/util.mjs +58 -0
- package/src/dereference/strategies/openapi-3-1/visitor.cjs +776 -0
- package/src/dereference/strategies/openapi-3-1/visitor.mjs +770 -0
- package/src/dereference/util.cjs +31 -0
- package/src/dereference/util.mjs +27 -0
- package/src/errors/BundleError.cjs +10 -0
- package/src/errors/BundleError.mjs +7 -0
- package/src/errors/DereferenceError.cjs +10 -0
- package/src/errors/DereferenceError.mjs +7 -0
- package/src/errors/EvaluationElementIdError.cjs +10 -0
- package/src/errors/EvaluationElementIdError.mjs +7 -0
- package/src/errors/EvaluationJsonSchema$anchorError.cjs +11 -0
- package/src/errors/EvaluationJsonSchema$anchorError.mjs +6 -0
- package/src/errors/EvaluationJsonSchemaUriError.cjs +11 -0
- package/src/errors/EvaluationJsonSchemaUriError.mjs +6 -0
- package/src/errors/InvalidJsonSchema$anchorError.cjs +15 -0
- package/src/errors/InvalidJsonSchema$anchorError.mjs +10 -0
- package/src/errors/JsonSchema$anchorError.cjs +10 -0
- package/src/errors/JsonSchema$anchorError.mjs +7 -0
- package/src/errors/JsonSchemaUriError.cjs +10 -0
- package/src/errors/JsonSchemaUriError.mjs +7 -0
- package/src/errors/MaximumBundleDepthError.cjs +11 -0
- package/src/errors/MaximumBundleDepthError.mjs +6 -0
- package/src/errors/MaximumDereferenceDepthError.cjs +11 -0
- package/src/errors/MaximumDereferenceDepthError.mjs +6 -0
- package/src/errors/MaximumResolveDepthError.cjs +11 -0
- package/src/errors/MaximumResolveDepthError.mjs +6 -0
- package/src/errors/ParseError.cjs +10 -0
- package/src/errors/ParseError.mjs +7 -0
- package/src/errors/ParserError.cjs +11 -0
- package/src/errors/ParserError.mjs +6 -0
- package/src/errors/PluginError.cjs +18 -0
- package/src/errors/PluginError.mjs +15 -0
- package/src/errors/ResolveError.cjs +10 -0
- package/src/errors/ResolveError.mjs +7 -0
- package/src/errors/ResolverError.cjs +11 -0
- package/src/errors/ResolverError.mjs +6 -0
- package/src/errors/UnmatchedBundleStrategyError.cjs +11 -0
- package/src/errors/UnmatchedBundleStrategyError.mjs +6 -0
- package/src/errors/UnmatchedDereferenceStrategyError.cjs +11 -0
- package/src/errors/UnmatchedDereferenceStrategyError.mjs +6 -0
- package/src/errors/UnmatchedResolveStrategyError.cjs +11 -0
- package/src/errors/UnmatchedResolveStrategyError.mjs +6 -0
- package/src/errors/UnmatchedResolverError.cjs +11 -0
- package/src/errors/UnmatchedResolverError.mjs +6 -0
- package/src/index.cjs +142 -0
- package/src/index.mjs +101 -0
- package/src/options/index.cjs +185 -0
- package/src/options/index.mjs +182 -0
- package/src/options/util.cjs +24 -0
- package/src/options/util.mjs +19 -0
- package/src/parse/index.cjs +69 -0
- package/src/parse/index.mjs +63 -0
- package/src/parse/parsers/Parser.cjs +48 -0
- package/src/parse/parsers/Parser.mjs +44 -0
- package/src/parse/parsers/api-design-systems-json/index.cjs +55 -0
- package/src/parse/parsers/api-design-systems-json/index.mjs +49 -0
- package/src/parse/parsers/api-design-systems-yaml/index.cjs +54 -0
- package/src/parse/parsers/api-design-systems-yaml/index.mjs +48 -0
- package/src/parse/parsers/apidom-json/index.cjs +70 -0
- package/src/parse/parsers/apidom-json/index.mjs +64 -0
- package/src/parse/parsers/arazzo-json-1/index.cjs +55 -0
- package/src/parse/parsers/arazzo-json-1/index.mjs +49 -0
- package/src/parse/parsers/arazzo-yaml-1/index.cjs +54 -0
- package/src/parse/parsers/arazzo-yaml-1/index.mjs +48 -0
- package/src/parse/parsers/asyncapi-json-2/index.cjs +55 -0
- package/src/parse/parsers/asyncapi-json-2/index.mjs +49 -0
- package/src/parse/parsers/asyncapi-yaml-2/index.cjs +54 -0
- package/src/parse/parsers/asyncapi-yaml-2/index.mjs +48 -0
- package/src/parse/parsers/binary/index-browser.cjs +56 -0
- package/src/parse/parsers/binary/index-browser.mjs +50 -0
- package/src/parse/parsers/binary/index-node.cjs +51 -0
- package/src/parse/parsers/binary/index-node.mjs +45 -0
- package/src/parse/parsers/json/index.cjs +54 -0
- package/src/parse/parsers/json/index.mjs +48 -0
- package/src/parse/parsers/openapi-json-2/index.cjs +55 -0
- package/src/parse/parsers/openapi-json-2/index.mjs +49 -0
- package/src/parse/parsers/openapi-json-3-0/index.cjs +55 -0
- package/src/parse/parsers/openapi-json-3-0/index.mjs +49 -0
- package/src/parse/parsers/openapi-json-3-1/index.cjs +55 -0
- package/src/parse/parsers/openapi-json-3-1/index.mjs +49 -0
- package/src/parse/parsers/openapi-yaml-2/index.cjs +54 -0
- package/src/parse/parsers/openapi-yaml-2/index.mjs +48 -0
- package/src/parse/parsers/openapi-yaml-3-0/index.cjs +54 -0
- package/src/parse/parsers/openapi-yaml-3-0/index.mjs +48 -0
- package/src/parse/parsers/openapi-yaml-3-1/index.cjs +54 -0
- package/src/parse/parsers/openapi-yaml-3-1/index.mjs +48 -0
- package/src/parse/parsers/yaml-1-2/index.cjs +54 -0
- package/src/parse/parsers/yaml-1-2/index.mjs +48 -0
- package/src/resolve/index.cjs +67 -0
- package/src/resolve/index.mjs +60 -0
- package/src/resolve/resolvers/HTTPResolver.cjs +38 -0
- package/src/resolve/resolvers/HTTPResolver.mjs +31 -0
- package/src/resolve/resolvers/Resolver.cjs +20 -0
- package/src/resolve/resolvers/Resolver.mjs +16 -0
- package/src/resolve/resolvers/file/index-browser.cjs +24 -0
- package/src/resolve/resolvers/file/index-browser.mjs +19 -0
- package/src/resolve/resolvers/file/index-node.cjs +49 -0
- package/src/resolve/resolvers/file/index-node.mjs +42 -0
- package/src/resolve/resolvers/http-axios/index.cjs +80 -0
- package/src/resolve/resolvers/http-axios/index.mjs +73 -0
- package/src/resolve/strategies/ResolveStrategy.cjs +20 -0
- package/src/resolve/strategies/ResolveStrategy.mjs +16 -0
- package/src/resolve/strategies/apidom/index.cjs +49 -0
- package/src/resolve/strategies/apidom/index.mjs +43 -0
- package/src/resolve/strategies/asyncapi-2/index.cjs +49 -0
- package/src/resolve/strategies/asyncapi-2/index.mjs +43 -0
- package/src/resolve/strategies/openapi-2/index.cjs +49 -0
- package/src/resolve/strategies/openapi-2/index.mjs +43 -0
- package/src/resolve/strategies/openapi-3-0/index.cjs +49 -0
- package/src/resolve/strategies/openapi-3-0/index.mjs +43 -0
- package/src/resolve/strategies/openapi-3-1/index.cjs +49 -0
- package/src/resolve/strategies/openapi-3-1/index.mjs +43 -0
- package/src/resolve/util.cjs +37 -0
- package/src/resolve/util.mjs +30 -0
- package/src/util/plugins.cjs +44 -0
- package/src/util/plugins.mjs +37 -0
- package/src/util/url.cjs +288 -0
- package/src/util/url.mjs +274 -0
- package/types/File.d.ts +24 -0
- package/types/Reference.d.ts +23 -0
- package/types/ReferenceSet.d.ts +25 -0
- package/types/apidom-reference.d.ts +584 -0
- package/types/bundle/index.d.ts +7 -0
- package/types/bundle/strategies/BundleStrategy.d.ts +19 -0
- package/types/bundle/strategies/openapi-3-1/index.d.ts +26 -0
- package/types/configuration/empty.d.ts +1 -0
- package/types/configuration/saturated.d.ts +1 -0
- package/types/dereference/index.d.ts +11 -0
- package/types/dereference/strategies/DereferenceStrategy.d.ts +19 -0
- package/types/dereference/strategies/apidom/index.d.ts +30 -0
- package/types/dereference/strategies/apidom/selectors/element-id.d.ts +11 -0
- package/types/dereference/strategies/apidom/visitor.d.ts +32 -0
- package/types/dereference/strategies/asyncapi-2/index.d.ts +31 -0
- package/types/dereference/strategies/asyncapi-2/visitor.d.ts +43 -0
- package/types/dereference/strategies/openapi-2/index.d.ts +32 -0
- package/types/dereference/strategies/openapi-2/visitor.d.ts +47 -0
- package/types/dereference/strategies/openapi-3-0/index.d.ts +31 -0
- package/types/dereference/strategies/openapi-3-0/visitor.d.ts +49 -0
- package/types/dereference/strategies/openapi-3-1/index.d.ts +32 -0
- package/types/dereference/strategies/openapi-3-1/selectors/$anchor.d.ts +22 -0
- package/types/dereference/strategies/openapi-3-1/selectors/uri.d.ts +12 -0
- package/types/dereference/strategies/openapi-3-1/util.d.ts +21 -0
- package/types/dereference/strategies/openapi-3-1/visitor.d.ts +52 -0
- package/types/dereference/util.d.ts +9 -0
- package/types/errors/BundleError.d.ts +7 -0
- package/types/errors/DereferenceError.d.ts +7 -0
- package/types/errors/EvaluationElementIdError.d.ts +7 -0
- package/types/errors/EvaluationJsonSchema$anchorError.d.ts +7 -0
- package/types/errors/EvaluationJsonSchemaUriError.d.ts +7 -0
- package/types/errors/InvalidJsonSchema$anchorError.d.ts +8 -0
- package/types/errors/JsonSchema$anchorError.d.ts +7 -0
- package/types/errors/JsonSchemaUriError.d.ts +7 -0
- package/types/errors/MaximumBundleDepthError.d.ts +7 -0
- package/types/errors/MaximumDereferenceDepthError.d.ts +7 -0
- package/types/errors/MaximumResolveDepthError.d.ts +7 -0
- package/types/errors/ParseError.d.ts +7 -0
- package/types/errors/ParserError.d.ts +7 -0
- package/types/errors/PluginError.d.ts +12 -0
- package/types/errors/ResolveError.d.ts +7 -0
- package/types/errors/ResolverError.d.ts +7 -0
- package/types/errors/UnmatchedBundleStrategyError.d.ts +7 -0
- package/types/errors/UnmatchedDereferenceStrategyError.d.ts +7 -0
- package/types/errors/UnmatchedResolveStrategyError.d.ts +7 -0
- package/types/errors/UnmatchedResolverError.d.ts +7 -0
- package/types/index.d.ts +75 -0
- package/types/options/index.d.ts +62 -0
- package/types/options/util.d.ts +5 -0
- package/types/parse/index.d.ts +7 -0
- package/types/parse/parsers/Parser.d.ts +38 -0
- package/types/parse/parsers/api-design-systems-json/index.d.ts +21 -0
- package/types/parse/parsers/api-design-systems-yaml/index.d.ts +20 -0
- package/types/parse/parsers/apidom-json/index.d.ts +24 -0
- package/types/parse/parsers/arazzo-json-1/index.d.ts +21 -0
- package/types/parse/parsers/arazzo-yaml-1/index.d.ts +20 -0
- package/types/parse/parsers/asyncapi-json-2/index.d.ts +21 -0
- package/types/parse/parsers/asyncapi-yaml-2/index.d.ts +20 -0
- package/types/parse/parsers/binary/index-browser.d.ts +21 -0
- package/types/parse/parsers/binary/index-node.d.ts +21 -0
- package/types/parse/parsers/json/index.d.ts +20 -0
- package/types/parse/parsers/openapi-json-2/index.d.ts +21 -0
- package/types/parse/parsers/openapi-json-3-0/index.d.ts +21 -0
- package/types/parse/parsers/openapi-json-3-1/index.d.ts +21 -0
- package/types/parse/parsers/openapi-yaml-2/index.d.ts +20 -0
- package/types/parse/parsers/openapi-yaml-3-0/index.d.ts +20 -0
- package/types/parse/parsers/openapi-yaml-3-1/index.d.ts +20 -0
- package/types/parse/parsers/yaml-1-2/index.d.ts +20 -0
- package/types/resolve/index.d.ts +12 -0
- package/types/resolve/resolvers/HTTPResolver.d.ts +22 -0
- package/types/resolve/resolvers/Resolver.d.ts +17 -0
- package/types/resolve/resolvers/file/index-browser.d.ts +12 -0
- package/types/resolve/resolvers/file/index-node.d.ts +20 -0
- package/types/resolve/resolvers/http-axios/index.d.ts +30 -0
- package/types/resolve/strategies/ResolveStrategy.d.ts +19 -0
- package/types/resolve/strategies/apidom/index.d.ts +27 -0
- package/types/resolve/strategies/asyncapi-2/index.d.ts +27 -0
- package/types/resolve/strategies/openapi-2/index.d.ts +27 -0
- package/types/resolve/strategies/openapi-3-0/index.d.ts +27 -0
- package/types/resolve/strategies/openapi-3-1/index.d.ts +27 -0
- package/types/resolve/util.d.ts +6 -0
- package/types/util/plugins.d.ts +14 -0
- package/types/util/url.d.ts +106 -0
package/src/util/url.mjs
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
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
|
+
};
|
package/types/File.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ParseResultElement } from '@speclynx/apidom-core';
|
|
2
|
+
/**
|
|
3
|
+
* This class represents a File object with url and data.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export interface FileOptions {
|
|
7
|
+
readonly uri: string;
|
|
8
|
+
readonly mediaType?: string;
|
|
9
|
+
readonly data?: Buffer | DataView | ArrayBuffer | Uint8Array | string;
|
|
10
|
+
readonly parseResult?: ParseResultElement;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
declare class File {
|
|
16
|
+
uri: string;
|
|
17
|
+
mediaType: string;
|
|
18
|
+
data?: Buffer | DataView | ArrayBuffer | Uint8Array | string;
|
|
19
|
+
parseResult?: ParseResultElement;
|
|
20
|
+
constructor({ uri, mediaType, data, parseResult }: FileOptions);
|
|
21
|
+
get extension(): string;
|
|
22
|
+
toString(): string;
|
|
23
|
+
}
|
|
24
|
+
export default File;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Element } from '@speclynx/apidom-core';
|
|
2
|
+
import ReferenceSet from './ReferenceSet.ts';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export interface ReferenceOptions<T = Element> {
|
|
7
|
+
readonly uri: string;
|
|
8
|
+
readonly depth?: number;
|
|
9
|
+
readonly refSet?: ReferenceSet;
|
|
10
|
+
readonly value: T;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
declare class Reference<T = Element> {
|
|
16
|
+
readonly uri: string;
|
|
17
|
+
readonly depth: number;
|
|
18
|
+
readonly value: T;
|
|
19
|
+
refSet?: ReferenceSet;
|
|
20
|
+
readonly errors: Array<Error>;
|
|
21
|
+
constructor({ uri, depth, refSet, value }: ReferenceOptions<T>);
|
|
22
|
+
}
|
|
23
|
+
export default Reference;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type Reference from './Reference.ts';
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export interface ReferenceSetOptions {
|
|
6
|
+
readonly refs?: Reference[];
|
|
7
|
+
readonly circular?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
declare class ReferenceSet {
|
|
13
|
+
rootRef?: Reference;
|
|
14
|
+
readonly refs: Reference[];
|
|
15
|
+
circular: boolean;
|
|
16
|
+
constructor({ refs, circular }?: ReferenceSetOptions);
|
|
17
|
+
get size(): number;
|
|
18
|
+
add(reference: Reference): this;
|
|
19
|
+
merge(anotherRefSet: this): this;
|
|
20
|
+
has(thing: string | Reference): boolean;
|
|
21
|
+
find(predicate: (value: Reference, index: number, obj: Reference[]) => boolean): Reference | undefined;
|
|
22
|
+
values(): Generator<Reference<import("minim").Element>, void, unknown>;
|
|
23
|
+
clean(): void;
|
|
24
|
+
}
|
|
25
|
+
export default ReferenceSet;
|