@apidevtools/json-schema-ref-parser 10.1.0 → 11.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/README.md +22 -0
- package/dist/lib/bundle.js +1 -0
- package/dist/lib/dereference.js +3 -1
- package/dist/lib/index.js +92 -102
- package/dist/lib/options.d.ts +6 -0
- package/dist/lib/options.js +1 -0
- package/dist/lib/parse.js +86 -101
- package/dist/lib/parsers/json.js +18 -29
- package/dist/lib/parsers/yaml.js +17 -28
- package/dist/lib/ref.d.ts +1 -2
- package/dist/lib/ref.js +1 -2
- package/dist/lib/refs.js +3 -4
- package/dist/lib/resolve-external.js +34 -49
- package/dist/lib/resolvers/file.js +16 -30
- package/dist/lib/resolvers/http.js +51 -64
- package/dist/lib/util/convert-path-to-posix.d.ts +1 -0
- package/dist/lib/util/convert-path-to-posix.js +14 -0
- package/dist/lib/util/is-windows.d.ts +1 -0
- package/dist/lib/util/is-windows.js +6 -0
- package/dist/lib/util/plugins.js +47 -58
- package/dist/lib/util/url.d.ts +5 -4
- package/dist/lib/util/url.js +62 -17
- package/dist/vite.config.d.ts +1 -1
- package/dist/vite.config.js +0 -4
- package/lib/bundle.ts +1 -0
- package/lib/dereference.ts +3 -1
- package/lib/index.ts +7 -0
- package/lib/options.ts +9 -1
- package/lib/ref.ts +1 -2
- package/lib/refs.ts +6 -8
- package/lib/resolve-external.ts +13 -14
- package/lib/resolvers/file.ts +1 -1
- package/lib/resolvers/http.ts +1 -1
- package/lib/util/convert-path-to-posix.ts +11 -0
- package/lib/util/is-windows.ts +2 -0
- package/lib/util/url.ts +42 -21
- package/package.json +21 -22
package/dist/lib/util/url.js
CHANGED
|
@@ -1,9 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
2
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.safePointerToPath = exports.toFileSystemPath = exports.fromFileSystemPath = exports.isFileSystemPath = exports.isHttp = exports.stripHash = exports.getHash = exports.stripQuery = exports.getExtension = exports.getProtocol = exports.cwd = exports.resolve = exports.parse = void 0;
|
|
4
|
-
const
|
|
5
|
-
const path_1 = require("path");
|
|
6
|
-
const
|
|
29
|
+
exports.relative = exports.safePointerToPath = exports.toFileSystemPath = exports.fromFileSystemPath = exports.isFileSystemPath = exports.isHttp = exports.stripHash = exports.getHash = exports.stripQuery = exports.getExtension = exports.getProtocol = exports.cwd = exports.resolve = exports.parse = void 0;
|
|
30
|
+
const convert_path_to_posix_1 = __importDefault(require("./convert-path-to-posix"));
|
|
31
|
+
const path_1 = __importStar(require("path"));
|
|
32
|
+
const forwardSlashPattern = /\//g;
|
|
33
|
+
const protocolPattern = /^(\w{2,}):\/\//i;
|
|
34
|
+
const jsonPointerSlash = /~1/g;
|
|
35
|
+
const jsonPointerTilde = /~0/g;
|
|
36
|
+
const path_2 = require("path");
|
|
37
|
+
const is_windows_1 = require("./is-windows");
|
|
38
|
+
const projectDir = (0, path_2.join)(__dirname, "..", "..");
|
|
7
39
|
// RegExp patterns to URL-encode special characters in local filesystem paths
|
|
8
40
|
const urlEncodePatterns = [/\?/g, "%3F", /#/g, "%23"];
|
|
9
41
|
// RegExp patterns to URL-decode special characters for local filesystem paths
|
|
@@ -16,7 +48,8 @@ exports.parse = parse;
|
|
|
16
48
|
* @returns
|
|
17
49
|
*/
|
|
18
50
|
function resolve(from, to) {
|
|
19
|
-
const
|
|
51
|
+
const fromUrl = new URL((0, convert_path_to_posix_1.default)(from), "resolve://");
|
|
52
|
+
const resolvedUrl = new URL((0, convert_path_to_posix_1.default)(to), fromUrl);
|
|
20
53
|
if (resolvedUrl.protocol === "resolve:") {
|
|
21
54
|
// `from` is a relative URL.
|
|
22
55
|
const { pathname, search, hash } = resolvedUrl;
|
|
@@ -51,7 +84,7 @@ exports.cwd = cwd;
|
|
|
51
84
|
* @returns
|
|
52
85
|
*/
|
|
53
86
|
function getProtocol(path) {
|
|
54
|
-
const match = protocolPattern.exec(path);
|
|
87
|
+
const match = protocolPattern.exec(path || "");
|
|
55
88
|
if (match) {
|
|
56
89
|
return match[1].toLowerCase();
|
|
57
90
|
}
|
|
@@ -173,15 +206,17 @@ exports.isFileSystemPath = isFileSystemPath;
|
|
|
173
206
|
function fromFileSystemPath(path) {
|
|
174
207
|
// Step 1: On Windows, replace backslashes with forward slashes,
|
|
175
208
|
// rather than encoding them as "%5C"
|
|
176
|
-
if (isWindows) {
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
209
|
+
if ((0, is_windows_1.isWindows)()) {
|
|
210
|
+
const upperPath = path.toUpperCase();
|
|
211
|
+
const projectDirPosixPath = (0, convert_path_to_posix_1.default)(projectDir);
|
|
212
|
+
const posixUpper = projectDirPosixPath.toUpperCase();
|
|
213
|
+
const hasProjectDir = upperPath.includes(posixUpper);
|
|
214
|
+
const hasProjectUri = upperPath.includes(posixUpper);
|
|
215
|
+
const isAbsolutePath = path_1.win32.isAbsolute(path);
|
|
216
|
+
if (!(hasProjectDir || hasProjectUri || isAbsolutePath)) {
|
|
217
|
+
path = (0, path_2.join)(projectDir, path);
|
|
184
218
|
}
|
|
219
|
+
path = (0, convert_path_to_posix_1.default)(path);
|
|
185
220
|
}
|
|
186
221
|
// Step 2: `encodeURI` will take care of MOST characters
|
|
187
222
|
path = encodeURI(path);
|
|
@@ -213,7 +248,7 @@ function toFileSystemPath(path, keepFileProtocol) {
|
|
|
213
248
|
// Strip-off the protocol, and the initial "/", if there is one
|
|
214
249
|
path = path[7] === "/" ? path.substr(8) : path.substr(7);
|
|
215
250
|
// insert a colon (":") after the drive letter on Windows
|
|
216
|
-
if (isWindows && path[1] === "/") {
|
|
251
|
+
if ((0, is_windows_1.isWindows)() && path[1] === "/") {
|
|
217
252
|
path = path[0] + ":" + path.substr(1);
|
|
218
253
|
}
|
|
219
254
|
if (keepFileProtocol) {
|
|
@@ -225,11 +260,11 @@ function toFileSystemPath(path, keepFileProtocol) {
|
|
|
225
260
|
// On Windows, it will start with something like "C:/".
|
|
226
261
|
// On Posix, it will start with "/"
|
|
227
262
|
isFileUrl = false;
|
|
228
|
-
path = isWindows ? path : "/" + path;
|
|
263
|
+
path = (0, is_windows_1.isWindows)() ? path : "/" + path;
|
|
229
264
|
}
|
|
230
265
|
}
|
|
231
266
|
// Step 4: Normalize Windows paths (unless it's a "file://" URL)
|
|
232
|
-
if (isWindows && !isFileUrl) {
|
|
267
|
+
if ((0, is_windows_1.isWindows)() && !isFileUrl) {
|
|
233
268
|
// Replace forward slashes with backslashes
|
|
234
269
|
path = path.replace(forwardSlashPattern, "\\");
|
|
235
270
|
// Capitalize the drive letter
|
|
@@ -258,3 +293,13 @@ function safePointerToPath(pointer) {
|
|
|
258
293
|
});
|
|
259
294
|
}
|
|
260
295
|
exports.safePointerToPath = safePointerToPath;
|
|
296
|
+
function relative(from, to) {
|
|
297
|
+
if (!isFileSystemPath(from) || !isFileSystemPath(to)) {
|
|
298
|
+
return resolve(from, to);
|
|
299
|
+
}
|
|
300
|
+
const fromDir = path_1.default.dirname(stripHash(from));
|
|
301
|
+
const toPath = stripHash(to);
|
|
302
|
+
const result = path_1.default.relative(fromDir, toPath);
|
|
303
|
+
return result + getHash(to);
|
|
304
|
+
}
|
|
305
|
+
exports.relative = relative;
|
package/dist/vite.config.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("
|
|
1
|
+
declare const _default: import("vite").UserConfig;
|
|
2
2
|
export default _default;
|
package/dist/vite.config.js
CHANGED
package/lib/bundle.ts
CHANGED
|
@@ -94,6 +94,7 @@ function crawl(
|
|
|
94
94
|
* @param $refParent - The object that contains a JSON Reference as one of its keys
|
|
95
95
|
* @param $refKey - The key in `$refParent` that is a JSON Reference
|
|
96
96
|
* @param path - The full path of the JSON Reference at `$refKey`, possibly with a JSON Pointer in the hash
|
|
97
|
+
* @param indirections - unknown
|
|
97
98
|
* @param pathFromRoot - The path of the JSON Reference at `$refKey`, from the schema root
|
|
98
99
|
* @param inventory - An array of already-inventoried $ref pointers
|
|
99
100
|
* @param $refs
|
package/lib/dereference.ts
CHANGED
|
@@ -169,7 +169,9 @@ function dereference$Ref(
|
|
|
169
169
|
) {
|
|
170
170
|
// console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
|
|
171
171
|
|
|
172
|
-
const
|
|
172
|
+
const isExternalRef = $Ref.isExternal$Ref($ref);
|
|
173
|
+
const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root";
|
|
174
|
+
const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
|
|
173
175
|
|
|
174
176
|
const cache = dereferencedCache.get($refPath);
|
|
175
177
|
if (cache) {
|
package/lib/index.ts
CHANGED
|
@@ -100,6 +100,13 @@ export class $RefParser {
|
|
|
100
100
|
if (url.isFileSystemPath(args.path)) {
|
|
101
101
|
args.path = url.fromFileSystemPath(args.path);
|
|
102
102
|
pathType = "file";
|
|
103
|
+
} else if (!args.path && args.schema && args.schema.$id) {
|
|
104
|
+
// when schema id has defined an URL should use that hostname to request the references,
|
|
105
|
+
// instead of using the current page URL
|
|
106
|
+
const params = url.parse(args.schema.$id);
|
|
107
|
+
const port = params.protocol === "https:" ? 443 : 80;
|
|
108
|
+
|
|
109
|
+
args.path = `${params.protocol}//${params.hostname}:${port}`;
|
|
103
110
|
}
|
|
104
111
|
|
|
105
112
|
// Resolve the absolute path of the schema
|
package/lib/options.ts
CHANGED
|
@@ -83,6 +83,13 @@ interface $RefParserOptions {
|
|
|
83
83
|
* @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
|
|
84
84
|
*/
|
|
85
85
|
onDereference?(path: string, value: JSONSchemaObject): void;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether a reference should resolve relative to its directory/path, or from the cwd
|
|
89
|
+
*
|
|
90
|
+
* Default: `relative`
|
|
91
|
+
*/
|
|
92
|
+
externalReferenceResolution?: "relative" | "root";
|
|
86
93
|
};
|
|
87
94
|
}
|
|
88
95
|
|
|
@@ -149,8 +156,9 @@ const getDefaults = () => {
|
|
|
149
156
|
* @type {function}
|
|
150
157
|
*/
|
|
151
158
|
excludedPathMatcher: () => false,
|
|
159
|
+
referenceResolution: "relative",
|
|
152
160
|
},
|
|
153
|
-
};
|
|
161
|
+
} as $RefParserOptions;
|
|
154
162
|
return cloneDeep(defaults);
|
|
155
163
|
};
|
|
156
164
|
|
package/lib/ref.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { InvalidPointerError, isHandledError, normalizeError } from "./util/erro
|
|
|
4
4
|
import { safePointerToPath, stripHash, getHash } from "./util/url.js";
|
|
5
5
|
import type $Refs from "./refs.js";
|
|
6
6
|
import type $RefParserOptions from "./options.js";
|
|
7
|
-
import type { JSONSchema } from "./types";
|
|
8
7
|
|
|
9
8
|
type $RefError = JSONParserError | ResolverError | ParserError | MissingPointerError;
|
|
10
9
|
|
|
@@ -167,7 +166,7 @@ class $Ref {
|
|
|
167
166
|
* @param value - The value to inspect
|
|
168
167
|
* @returns
|
|
169
168
|
*/
|
|
170
|
-
static isExternal$Ref(value: any):
|
|
169
|
+
static isExternal$Ref(value: any): boolean {
|
|
171
170
|
return $Ref.is$Ref(value) && value.$ref![0] !== "#";
|
|
172
171
|
}
|
|
173
172
|
|
package/lib/refs.ts
CHANGED
|
@@ -4,9 +4,7 @@ import * as url from "./util/url.js";
|
|
|
4
4
|
import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
|
|
5
5
|
import type { JSONSchema } from "./types/index.js";
|
|
6
6
|
import type $RefParserOptions from "./options.js";
|
|
7
|
-
|
|
8
|
-
const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : "");
|
|
9
|
-
const getPathFromOs = (filePath: string): string => (isWindows ? filePath.replace(/\\/g, "/") : filePath);
|
|
7
|
+
import convertPathToPosix from "./util/convert-path-to-posix";
|
|
10
8
|
|
|
11
9
|
interface $RefsMap {
|
|
12
10
|
[url: string]: $Ref;
|
|
@@ -36,7 +34,7 @@ export default class $Refs {
|
|
|
36
34
|
paths(...types: string[]): string[] {
|
|
37
35
|
const paths = getPaths(this._$refs, types);
|
|
38
36
|
return paths.map((path) => {
|
|
39
|
-
return
|
|
37
|
+
return convertPathToPosix(path.decoded);
|
|
40
38
|
});
|
|
41
39
|
}
|
|
42
40
|
|
|
@@ -51,7 +49,7 @@ export default class $Refs {
|
|
|
51
49
|
const $refs = this._$refs;
|
|
52
50
|
const paths = getPaths($refs, types);
|
|
53
51
|
return paths.reduce<Record<string, any>>((obj, path) => {
|
|
54
|
-
obj[
|
|
52
|
+
obj[convertPathToPosix(path.decoded)] = $refs[path.encoded].value;
|
|
55
53
|
return obj;
|
|
56
54
|
}, {});
|
|
57
55
|
}
|
|
@@ -97,7 +95,7 @@ export default class $Refs {
|
|
|
97
95
|
* @param value The value to assign. Can be anything (object, string, number, etc.)
|
|
98
96
|
*/
|
|
99
97
|
set(path: any, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type) {
|
|
100
|
-
const absPath = url.resolve(this._root$Ref.path
|
|
98
|
+
const absPath = url.resolve(this._root$Ref.path!, path);
|
|
101
99
|
const withoutHash = url.stripHash(absPath);
|
|
102
100
|
const $ref = this._$refs[withoutHash];
|
|
103
101
|
|
|
@@ -115,7 +113,7 @@ export default class $Refs {
|
|
|
115
113
|
* @protected
|
|
116
114
|
*/
|
|
117
115
|
_get$Ref(path: any) {
|
|
118
|
-
path = url.resolve(this._root$Ref.path
|
|
116
|
+
path = url.resolve(this._root$Ref.path!, path);
|
|
119
117
|
const withoutHash = url.stripHash(path);
|
|
120
118
|
return this._$refs[withoutHash];
|
|
121
119
|
}
|
|
@@ -147,7 +145,7 @@ export default class $Refs {
|
|
|
147
145
|
* @protected
|
|
148
146
|
*/
|
|
149
147
|
_resolve(path: string, pathFromRoot: string, options?: any) {
|
|
150
|
-
const absPath = url.resolve(this._root$Ref.path
|
|
148
|
+
const absPath = url.resolve(this._root$Ref.path!, path);
|
|
151
149
|
const withoutHash = url.stripHash(absPath);
|
|
152
150
|
const $ref = this._$refs[withoutHash];
|
|
153
151
|
|
package/lib/resolve-external.ts
CHANGED
|
@@ -40,6 +40,7 @@ function resolveExternal(parser: $RefParser, options: Options) {
|
|
|
40
40
|
*
|
|
41
41
|
* @param obj - The value to crawl. If it's not an object or array, it will be ignored.
|
|
42
42
|
* @param path - The full path of `obj`, possibly with a JSON Pointer in the hash
|
|
43
|
+
* @param {boolean} external - Whether `obj` was found in an external document.
|
|
43
44
|
* @param $refs
|
|
44
45
|
* @param options
|
|
45
46
|
* @param seen - Internal.
|
|
@@ -56,6 +57,7 @@ function crawl(
|
|
|
56
57
|
$refs: $Refs,
|
|
57
58
|
options: Options,
|
|
58
59
|
seen?: Set<any>,
|
|
60
|
+
external?: boolean,
|
|
59
61
|
) {
|
|
60
62
|
seen ||= new Set();
|
|
61
63
|
let promises: any = [];
|
|
@@ -64,17 +66,13 @@ function crawl(
|
|
|
64
66
|
seen.add(obj); // Track previously seen objects to avoid infinite recursion
|
|
65
67
|
if ($Ref.isExternal$Ref(obj)) {
|
|
66
68
|
promises.push(resolve$Ref(obj, path, $refs, options));
|
|
67
|
-
}
|
|
68
|
-
for (const key of Object.keys(obj)) {
|
|
69
|
-
const keyPath = Pointer.join(path, key);
|
|
70
|
-
const value = obj[key] as string | JSONSchema | Buffer | undefined;
|
|
69
|
+
}
|
|
71
70
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
71
|
+
const keys = Object.keys(obj) as (keyof typeof obj)[];
|
|
72
|
+
for (const key of keys) {
|
|
73
|
+
const keyPath = Pointer.join(path, key);
|
|
74
|
+
const value = obj[key] as string | JSONSchema | Buffer | undefined;
|
|
75
|
+
promises = promises.concat(crawl(value, keyPath, $refs, options, seen, external));
|
|
78
76
|
}
|
|
79
77
|
}
|
|
80
78
|
|
|
@@ -94,11 +92,12 @@ function crawl(
|
|
|
94
92
|
* including nested references that are contained in externally-referenced files.
|
|
95
93
|
*/
|
|
96
94
|
async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options: Options) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const resolvedPath = url.resolve(path, $ref.$ref);
|
|
95
|
+
const shouldResolveOnCwd = options.dereference.externalReferenceResolution === "root";
|
|
96
|
+
const resolvedPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref!);
|
|
100
97
|
const withoutHash = url.stripHash(resolvedPath);
|
|
101
98
|
|
|
99
|
+
// $ref.$ref = url.relative($refs._root$Ref.path, resolvedPath);
|
|
100
|
+
|
|
102
101
|
// Do we already have this $ref?
|
|
103
102
|
$ref = $refs._$refs[withoutHash];
|
|
104
103
|
if ($ref) {
|
|
@@ -112,7 +111,7 @@ async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options
|
|
|
112
111
|
|
|
113
112
|
// Crawl the parsed value
|
|
114
113
|
// console.log('Resolving $ref pointers in %s', withoutHash);
|
|
115
|
-
const promises = crawl(result, withoutHash + "#", $refs, options);
|
|
114
|
+
const promises = crawl(result, withoutHash + "#", $refs, options, new Set(), true);
|
|
116
115
|
|
|
117
116
|
return Promise.all(promises);
|
|
118
117
|
} catch (err) {
|
package/lib/resolvers/file.ts
CHANGED
package/lib/resolvers/http.ts
CHANGED
|
@@ -86,7 +86,7 @@ async function download(u: URL | string, httpOptions: HTTPResolverOptions, _redi
|
|
|
86
86
|
} else if (!("location" in res.headers) || !res.headers.location) {
|
|
87
87
|
throw ono({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
|
|
88
88
|
} else {
|
|
89
|
-
const redirectTo = url.resolve(u, res.headers.location);
|
|
89
|
+
const redirectTo = url.resolve(u.href, res.headers.location as string);
|
|
90
90
|
return download(redirectTo, httpOptions, redirects);
|
|
91
91
|
}
|
|
92
92
|
} else {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
|
|
3
|
+
export default function convertPathToPosix(filePath: string) {
|
|
4
|
+
const isExtendedLengthPath = filePath.startsWith("\\\\?\\");
|
|
5
|
+
|
|
6
|
+
if (isExtendedLengthPath) {
|
|
7
|
+
return filePath;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return filePath.split(path.win32.sep).join(path.posix.sep);
|
|
11
|
+
}
|
package/lib/util/url.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import convertPathToPosix from "./convert-path-to-posix";
|
|
2
|
+
import path, { win32 } from "path";
|
|
3
|
+
|
|
4
|
+
const forwardSlashPattern = /\//g;
|
|
5
|
+
const protocolPattern = /^(\w{2,}):\/\//i;
|
|
6
|
+
const jsonPointerSlash = /~1/g;
|
|
7
|
+
const jsonPointerTilde = /~0/g;
|
|
8
|
+
|
|
6
9
|
import { join } from "path";
|
|
10
|
+
import { isWindows } from "./is-windows";
|
|
7
11
|
|
|
8
12
|
const projectDir = join(__dirname, "..", "..");
|
|
9
13
|
// RegExp patterns to URL-encode special characters in local filesystem paths
|
|
@@ -12,15 +16,16 @@ const urlEncodePatterns = [/\?/g, "%3F", /#/g, "%23"];
|
|
|
12
16
|
// RegExp patterns to URL-decode special characters for local filesystem paths
|
|
13
17
|
const urlDecodePatterns = [/%23/g, "#", /%24/g, "$", /%26/g, "&", /%2C/g, ",", /%40/g, "@"];
|
|
14
18
|
|
|
15
|
-
export const parse = (u:
|
|
19
|
+
export const parse = (u: string | URL) => new URL(u);
|
|
16
20
|
|
|
17
21
|
/**
|
|
18
22
|
* Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF.
|
|
19
23
|
*
|
|
20
24
|
* @returns
|
|
21
25
|
*/
|
|
22
|
-
export function resolve(from:
|
|
23
|
-
const
|
|
26
|
+
export function resolve(from: string, to: string) {
|
|
27
|
+
const fromUrl = new URL(convertPathToPosix(from), "resolve://");
|
|
28
|
+
const resolvedUrl = new URL(convertPathToPosix(to), fromUrl);
|
|
24
29
|
if (resolvedUrl.protocol === "resolve:") {
|
|
25
30
|
// `from` is a relative URL.
|
|
26
31
|
const { pathname, search, hash } = resolvedUrl;
|
|
@@ -55,8 +60,8 @@ export function cwd() {
|
|
|
55
60
|
* @param path
|
|
56
61
|
* @returns
|
|
57
62
|
*/
|
|
58
|
-
export function getProtocol(path:
|
|
59
|
-
const match = protocolPattern.exec(path);
|
|
63
|
+
export function getProtocol(path: string | undefined) {
|
|
64
|
+
const match = protocolPattern.exec(path || "");
|
|
60
65
|
if (match) {
|
|
61
66
|
return match[1].toLowerCase();
|
|
62
67
|
}
|
|
@@ -146,7 +151,7 @@ export function isHttp(path: any) {
|
|
|
146
151
|
* @param path
|
|
147
152
|
* @returns
|
|
148
153
|
*/
|
|
149
|
-
export function isFileSystemPath(path:
|
|
154
|
+
export function isFileSystemPath(path: string | undefined) {
|
|
150
155
|
// @ts-ignore
|
|
151
156
|
if (typeof window !== "undefined" || process.browser) {
|
|
152
157
|
// We're running in a browser, so assume that all paths are URLs.
|
|
@@ -177,14 +182,18 @@ export function isFileSystemPath(path: any) {
|
|
|
177
182
|
export function fromFileSystemPath(path: any) {
|
|
178
183
|
// Step 1: On Windows, replace backslashes with forward slashes,
|
|
179
184
|
// rather than encoding them as "%5C"
|
|
180
|
-
if (isWindows) {
|
|
181
|
-
const
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
if (isWindows()) {
|
|
186
|
+
const upperPath = path.toUpperCase();
|
|
187
|
+
const projectDirPosixPath = convertPathToPosix(projectDir);
|
|
188
|
+
const posixUpper = projectDirPosixPath.toUpperCase();
|
|
189
|
+
const hasProjectDir = upperPath.includes(posixUpper);
|
|
190
|
+
const hasProjectUri = upperPath.includes(posixUpper);
|
|
191
|
+
const isAbsolutePath = win32.isAbsolute(path);
|
|
192
|
+
|
|
193
|
+
if (!(hasProjectDir || hasProjectUri || isAbsolutePath)) {
|
|
194
|
+
path = join(projectDir, path);
|
|
187
195
|
}
|
|
196
|
+
path = convertPathToPosix(path);
|
|
188
197
|
}
|
|
189
198
|
|
|
190
199
|
// Step 2: `encodeURI` will take care of MOST characters
|
|
@@ -222,7 +231,7 @@ export function toFileSystemPath(path: string | undefined, keepFileProtocol?: bo
|
|
|
222
231
|
path = path[7] === "/" ? path.substr(8) : path.substr(7);
|
|
223
232
|
|
|
224
233
|
// insert a colon (":") after the drive letter on Windows
|
|
225
|
-
if (isWindows && path[1] === "/") {
|
|
234
|
+
if (isWindows() && path[1] === "/") {
|
|
226
235
|
path = path[0] + ":" + path.substr(1);
|
|
227
236
|
}
|
|
228
237
|
|
|
@@ -234,12 +243,12 @@ export function toFileSystemPath(path: string | undefined, keepFileProtocol?: bo
|
|
|
234
243
|
// On Windows, it will start with something like "C:/".
|
|
235
244
|
// On Posix, it will start with "/"
|
|
236
245
|
isFileUrl = false;
|
|
237
|
-
path = isWindows ? path : "/" + path;
|
|
246
|
+
path = isWindows() ? path : "/" + path;
|
|
238
247
|
}
|
|
239
248
|
}
|
|
240
249
|
|
|
241
250
|
// Step 4: Normalize Windows paths (unless it's a "file://" URL)
|
|
242
|
-
if (isWindows && !isFileUrl) {
|
|
251
|
+
if (isWindows() && !isFileUrl) {
|
|
243
252
|
// Replace forward slashes with backslashes
|
|
244
253
|
path = path.replace(forwardSlashPattern, "\\");
|
|
245
254
|
|
|
@@ -270,3 +279,15 @@ export function safePointerToPath(pointer: any) {
|
|
|
270
279
|
return decodeURIComponent(value).replace(jsonPointerSlash, "/").replace(jsonPointerTilde, "~");
|
|
271
280
|
});
|
|
272
281
|
}
|
|
282
|
+
|
|
283
|
+
export function relative(from: string, to: string) {
|
|
284
|
+
if (!isFileSystemPath(from) || !isFileSystemPath(to)) {
|
|
285
|
+
return resolve(from, to);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const fromDir = path.dirname(stripHash(from));
|
|
289
|
+
const toPath = stripHash(to);
|
|
290
|
+
|
|
291
|
+
const result = path.relative(fromDir, toPath);
|
|
292
|
+
return result + getHash(to);
|
|
293
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apidevtools/json-schema-ref-parser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.1.0",
|
|
4
4
|
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"json",
|
|
@@ -67,33 +67,32 @@
|
|
|
67
67
|
"test:watch": "vitest -w"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@types/eslint": "8.
|
|
71
|
-
"@types/js-yaml": "^4.0.
|
|
72
|
-
"@types/node": "^
|
|
73
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
74
|
-
"@typescript-eslint/eslint-plugin-tslint": "^
|
|
75
|
-
"@typescript-eslint/parser": "^
|
|
76
|
-
"@vitest/coverage-
|
|
70
|
+
"@types/eslint": "8.44.2",
|
|
71
|
+
"@types/js-yaml": "^4.0.6",
|
|
72
|
+
"@types/node": "^20.6.2",
|
|
73
|
+
"@typescript-eslint/eslint-plugin": "^6.7.2",
|
|
74
|
+
"@typescript-eslint/eslint-plugin-tslint": "^6.7.2",
|
|
75
|
+
"@typescript-eslint/parser": "^6.7.2",
|
|
76
|
+
"@vitest/coverage-v8": "^0.34.4",
|
|
77
77
|
"abortcontroller-polyfill": "^1.7.5",
|
|
78
|
-
"c8": "^7.12.0",
|
|
79
78
|
"cross-env": "^7.0.3",
|
|
80
|
-
"eslint": "^8.
|
|
81
|
-
"eslint-config-prettier": "^
|
|
82
|
-
"eslint-config-standard": "^17.
|
|
83
|
-
"eslint-plugin-import": "^2.
|
|
84
|
-
"eslint-plugin-prettier": "^
|
|
79
|
+
"eslint": "^8.49.0",
|
|
80
|
+
"eslint-config-prettier": "^9.0.0",
|
|
81
|
+
"eslint-config-standard": "^17.1.0",
|
|
82
|
+
"eslint-plugin-import": "^2.28.1",
|
|
83
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
85
84
|
"eslint-plugin-promise": "^6.1.1",
|
|
86
|
-
"eslint-plugin-unused-imports": "^
|
|
87
|
-
"jsdom": "^
|
|
88
|
-
"lint-staged": "^
|
|
89
|
-
"node-fetch": "^3.3.
|
|
90
|
-
"prettier": "^
|
|
91
|
-
"typescript": "^
|
|
92
|
-
"vitest": "^0.
|
|
85
|
+
"eslint-plugin-unused-imports": "^3.0.0",
|
|
86
|
+
"jsdom": "^22.1.0",
|
|
87
|
+
"lint-staged": "^14.0.1",
|
|
88
|
+
"node-fetch": "^3.3.2",
|
|
89
|
+
"prettier": "^3.0.3",
|
|
90
|
+
"typescript": "^5.2.2",
|
|
91
|
+
"vitest": "^0.34.4"
|
|
93
92
|
},
|
|
94
93
|
"dependencies": {
|
|
95
94
|
"@jsdevtools/ono": "^7.1.3",
|
|
96
|
-
"@types/json-schema": "^7.0.
|
|
95
|
+
"@types/json-schema": "^7.0.13",
|
|
97
96
|
"@types/lodash.clonedeep": "^4.5.7",
|
|
98
97
|
"js-yaml": "^4.1.0",
|
|
99
98
|
"lodash.clonedeep": "^4.5.0"
|