@apidevtools/json-schema-ref-parser 10.0.1 → 11.0.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.
Files changed (111) hide show
  1. package/README.md +22 -0
  2. package/dist/lib/bundle.d.ts +10 -0
  3. package/dist/lib/bundle.js +266 -0
  4. package/dist/lib/dereference.d.ts +9 -0
  5. package/dist/lib/dereference.js +206 -0
  6. package/dist/lib/index.d.ts +206 -0
  7. package/dist/lib/index.js +206 -0
  8. package/dist/lib/normalize-args.d.ts +10 -0
  9. package/dist/lib/normalize-args.js +42 -0
  10. package/dist/lib/options.d.ts +77 -0
  11. package/dist/lib/options.js +119 -0
  12. package/dist/lib/parse.d.ts +8 -0
  13. package/dist/lib/parse.js +162 -0
  14. package/dist/lib/parsers/binary.d.ts +3 -0
  15. package/dist/lib/parsers/binary.js +35 -0
  16. package/dist/lib/parsers/json.d.ts +3 -0
  17. package/dist/lib/parsers/json.js +46 -0
  18. package/dist/lib/parsers/text.d.ts +3 -0
  19. package/dist/lib/parsers/text.js +42 -0
  20. package/dist/lib/parsers/yaml.d.ts +3 -0
  21. package/dist/lib/parsers/yaml.js +54 -0
  22. package/dist/lib/pointer.d.ts +87 -0
  23. package/dist/lib/pointer.js +256 -0
  24. package/dist/lib/ref.d.ts +180 -0
  25. package/dist/lib/ref.js +238 -0
  26. package/dist/lib/refs.d.ts +127 -0
  27. package/dist/lib/refs.js +222 -0
  28. package/dist/lib/resolve-external.d.ts +14 -0
  29. package/dist/lib/resolve-external.js +133 -0
  30. package/dist/lib/resolvers/file.d.ts +3 -0
  31. package/dist/lib/resolvers/file.js +62 -0
  32. package/dist/lib/resolvers/http.d.ts +3 -0
  33. package/dist/lib/resolvers/http.js +146 -0
  34. package/dist/lib/types/index.d.ts +104 -0
  35. package/dist/lib/types/index.js +2 -0
  36. package/dist/lib/util/convert-path-to-posix.d.ts +1 -0
  37. package/dist/lib/util/convert-path-to-posix.js +14 -0
  38. package/dist/lib/util/errors.d.ts +50 -0
  39. package/dist/lib/util/errors.js +106 -0
  40. package/dist/lib/util/is-windows.d.ts +1 -0
  41. package/dist/lib/util/is-windows.js +6 -0
  42. package/dist/lib/util/maybe.d.ts +3 -0
  43. package/dist/lib/util/maybe.js +24 -0
  44. package/dist/lib/util/next.d.ts +2 -0
  45. package/dist/lib/util/next.js +16 -0
  46. package/dist/lib/util/plugins.d.ts +36 -0
  47. package/dist/lib/util/plugins.js +133 -0
  48. package/dist/lib/util/url.d.ts +94 -0
  49. package/dist/lib/util/url.js +304 -0
  50. package/dist/vite.config.d.ts +2 -0
  51. package/dist/vite.config.js +19 -0
  52. package/lib/{bundle.js → bundle.ts} +106 -101
  53. package/lib/{dereference.js → dereference.ts} +113 -52
  54. package/lib/index.ts +413 -0
  55. package/lib/{normalize-args.js → normalize-args.ts} +7 -14
  56. package/lib/options.ts +202 -0
  57. package/lib/parse.ts +153 -0
  58. package/lib/parsers/binary.ts +39 -0
  59. package/lib/parsers/{json.js → json.ts} +9 -22
  60. package/lib/parsers/text.ts +46 -0
  61. package/lib/parsers/{yaml.js → yaml.ts} +15 -19
  62. package/lib/pointer.ts +296 -0
  63. package/lib/ref.ts +287 -0
  64. package/lib/refs.ts +236 -0
  65. package/lib/{resolve-external.js → resolve-external.ts} +44 -41
  66. package/lib/resolvers/file.ts +40 -0
  67. package/lib/resolvers/http.ts +136 -0
  68. package/lib/tsconfig.json +103 -0
  69. package/lib/types/index.ts +135 -0
  70. package/lib/util/convert-path-to-posix.ts +11 -0
  71. package/lib/util/errors.ts +141 -0
  72. package/lib/util/is-windows.ts +2 -0
  73. package/lib/util/maybe.ts +22 -0
  74. package/lib/util/next.ts +13 -0
  75. package/lib/util/{plugins.js → plugins.ts} +58 -57
  76. package/lib/util/{url.js → url.ts} +92 -91
  77. package/package.json +44 -46
  78. package/cjs/bundle.js +0 -304
  79. package/cjs/dereference.js +0 -258
  80. package/cjs/index.js +0 -603
  81. package/cjs/normalize-args.js +0 -64
  82. package/cjs/options.js +0 -125
  83. package/cjs/package.json +0 -3
  84. package/cjs/parse.js +0 -338
  85. package/cjs/parsers/binary.js +0 -54
  86. package/cjs/parsers/json.js +0 -199
  87. package/cjs/parsers/text.js +0 -61
  88. package/cjs/parsers/yaml.js +0 -239
  89. package/cjs/pointer.js +0 -290
  90. package/cjs/ref.js +0 -333
  91. package/cjs/refs.js +0 -214
  92. package/cjs/resolve-external.js +0 -333
  93. package/cjs/resolvers/file.js +0 -106
  94. package/cjs/resolvers/http.js +0 -184
  95. package/cjs/util/errors.js +0 -401
  96. package/cjs/util/plugins.js +0 -159
  97. package/cjs/util/projectDir.cjs +0 -6
  98. package/cjs/util/url.js +0 -228
  99. package/lib/index.d.ts +0 -496
  100. package/lib/index.js +0 -290
  101. package/lib/options.js +0 -128
  102. package/lib/parse.js +0 -162
  103. package/lib/parsers/binary.js +0 -53
  104. package/lib/parsers/text.js +0 -64
  105. package/lib/pointer.js +0 -293
  106. package/lib/ref.js +0 -292
  107. package/lib/refs.js +0 -196
  108. package/lib/resolvers/file.js +0 -63
  109. package/lib/resolvers/http.js +0 -155
  110. package/lib/util/errors.js +0 -134
  111. package/lib/util/projectDir.cjs +0 -6
@@ -1,48 +1,53 @@
1
+ import type { FileInfo } from "../types/index.js";
2
+ import type $RefParserOptions from "../options.js";
3
+ import type { ResolverOptions } from "../types/index.js";
4
+ import type $Refs from "../refs.js";
5
+ import type { Plugin } from "../types/index.js";
6
+ import type { JSONSchema } from "../types/index.js";
7
+
1
8
  /**
2
9
  * Returns the given plugins as an array, rather than an object map.
3
10
  * All other methods in this module expect an array of plugins rather than an object map.
4
11
  *
5
- * @param {object} plugins - A map of plugin objects
6
- * @return {object[]}
12
+ * @returns
7
13
  */
8
- export function all (plugins) {
14
+ export function all(plugins: $RefParserOptions["resolve"]): Plugin[] {
9
15
  return Object.keys(plugins)
10
16
  .filter((key) => {
11
17
  return typeof plugins[key] === "object";
12
18
  })
13
19
  .map((key) => {
14
- plugins[key].name = key;
15
- return plugins[key];
20
+ (plugins[key] as ResolverOptions)!.name = key;
21
+ return plugins[key] as Plugin;
16
22
  });
17
23
  }
18
24
 
19
25
  /**
20
26
  * Filters the given plugins, returning only the ones return `true` for the given method.
21
- *
22
- * @param {object[]} plugins - An array of plugin objects
23
- * @param {string} method - The name of the filter method to invoke for each plugin
24
- * @param {object} file - A file info object, which will be passed to each method
25
- * @return {object[]}
26
27
  */
27
- export function filter (plugins, method, file) {
28
- return plugins
29
- .filter((plugin) => {
30
- return !!getResult(plugin, method, file);
31
- });
28
+ export function filter(plugins: Plugin[], method: any, file: any) {
29
+ return plugins.filter((plugin: Plugin) => {
30
+ return !!getResult(plugin, method, file);
31
+ });
32
32
  }
33
33
 
34
34
  /**
35
35
  * Sorts the given plugins, in place, by their `order` property.
36
- *
37
- * @param {object[]} plugins - An array of plugin objects
38
- * @returns {object[]}
39
36
  */
40
- export function sort (plugins) {
41
- for (let plugin of plugins) {
37
+ export function sort(plugins: Plugin[]) {
38
+ for (const plugin of plugins) {
42
39
  plugin.order = plugin.order || Number.MAX_SAFE_INTEGER;
43
40
  }
44
41
 
45
- return plugins.sort((a, b) => { return a.order - b.order; });
42
+ return plugins.sort((a: any, b: any) => {
43
+ return a.order - b.order;
44
+ });
45
+ }
46
+
47
+ export interface PluginResult {
48
+ plugin: Plugin;
49
+ result?: string | Buffer | JSONSchema;
50
+ error?: any;
46
51
  }
47
52
 
48
53
  /**
@@ -52,19 +57,21 @@ export function sort (plugins) {
52
57
  * is immediately returned and no further plugins are called.
53
58
  * If the promise rejects, or the callback is called with an error, then the next plugin is called.
54
59
  * If ALL plugins fail, then the last error is thrown.
55
- *
56
- * @param {object[]} plugins - An array of plugin objects
57
- * @param {string} method - The name of the method to invoke for each plugin
58
- * @param {object} file - A file info object, which will be passed to each method
59
- * @returns {Promise}
60
60
  */
61
- export function run (plugins, method, file, $refs) {
62
- let plugin, lastError, index = 0;
61
+ export async function run(
62
+ plugins: Plugin[],
63
+ method: keyof Plugin | keyof ResolverOptions,
64
+ file: FileInfo,
65
+ $refs: $Refs,
66
+ ) {
67
+ let plugin: Plugin;
68
+ let lastError: PluginResult;
69
+ let index = 0;
63
70
 
64
- return new Promise(((resolve, reject) => {
71
+ return new Promise<PluginResult>((resolve, reject) => {
65
72
  runNextPlugin();
66
73
 
67
- function runNextPlugin () {
74
+ function runNextPlugin() {
68
75
  plugin = plugins[index++];
69
76
  if (!plugin) {
70
77
  // There are no more functions, so re-throw the last error
@@ -73,42 +80,38 @@ export function run (plugins, method, file, $refs) {
73
80
 
74
81
  try {
75
82
  // console.log(' %s', plugin.name);
76
- let result = getResult(plugin, method, file, callback, $refs);
83
+ const result = getResult(plugin, method, file, callback, $refs);
77
84
  if (result && typeof result.then === "function") {
78
85
  // A promise was returned
79
86
  result.then(onSuccess, onError);
80
- }
81
- else if (result !== undefined) {
87
+ } else if (result !== undefined) {
82
88
  // A synchronous result was returned
83
89
  onSuccess(result);
84
- }
85
- else if (index === plugins.length) {
90
+ } else if (index === plugins.length) {
86
91
  throw new Error("No promise has been returned or callback has been called.");
87
92
  }
88
- }
89
- catch (e) {
93
+ } catch (e) {
90
94
  onError(e);
91
95
  }
92
96
  }
93
97
 
94
- function callback (err, result) {
98
+ function callback(err: PluginResult["error"], result: PluginResult["result"]) {
95
99
  if (err) {
96
100
  onError(err);
97
- }
98
- else {
101
+ } else {
99
102
  onSuccess(result);
100
103
  }
101
104
  }
102
105
 
103
- function onSuccess (result) {
106
+ function onSuccess(result: PluginResult["result"]) {
104
107
  // console.log(' success');
105
108
  resolve({
106
109
  plugin,
107
- result
110
+ result,
108
111
  });
109
112
  }
110
113
 
111
- function onError (error) {
114
+ function onError(error: PluginResult["error"]) {
112
115
  // console.log(' %s', err.message || err);
113
116
  lastError = {
114
117
  plugin,
@@ -116,23 +119,23 @@ export function run (plugins, method, file, $refs) {
116
119
  };
117
120
  runNextPlugin();
118
121
  }
119
- }));
122
+ });
120
123
  }
121
124
 
122
125
  /**
123
126
  * Returns the value of the given property.
124
127
  * If the property is a function, then the result of the function is returned.
125
128
  * If the value is a RegExp, then it will be tested against the file URL.
126
- * If the value is an aray, then it will be compared against the file extension.
127
- *
128
- * @param {object} obj - The object whose property/method is called
129
- * @param {string} prop - The name of the property/method to invoke
130
- * @param {object} file - A file info object, which will be passed to the method
131
- * @param {function} [callback] - A callback function, which will be passed to the method
132
- * @returns {*}
129
+ * If the value is an array, then it will be compared against the file extension.
133
130
  */
134
- function getResult (obj, prop, file, callback, $refs) {
135
- let value = obj[prop];
131
+ function getResult(
132
+ obj: Plugin,
133
+ prop: keyof Plugin | keyof ResolverOptions,
134
+ file: FileInfo,
135
+ callback?: (err?: Error, result?: any) => void,
136
+ $refs?: any,
137
+ ) {
138
+ const value = obj[prop as keyof typeof obj] as unknown;
136
139
 
137
140
  if (typeof value === "function") {
138
141
  return value.apply(obj, [file, callback, $refs]);
@@ -144,11 +147,9 @@ function getResult (obj, prop, file, callback, $refs) {
144
147
  // files by RegExp or by file extension.
145
148
  if (value instanceof RegExp) {
146
149
  return value.test(file.url);
147
- }
148
- else if (typeof value === "string") {
150
+ } else if (typeof value === "string") {
149
151
  return value === file.extension;
150
- }
151
- else if (Array.isArray(value)) {
152
+ } else if (Array.isArray(value)) {
152
153
  return value.indexOf(file.extension) !== -1;
153
154
  }
154
155
  }
@@ -1,38 +1,33 @@
1
- import projectDir from "./projectDir.cjs";
1
+ import convertPathToPosix from "./convert-path-to-posix";
2
+ import path, { win32 } from "path";
2
3
 
3
- let isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined),
4
- forwardSlashPattern = /\//g,
5
- protocolPattern = /^(\w{2,}):\/\//i,
6
- jsonPointerSlash = /~1/g,
7
- jsonPointerTilde = /~0/g;
4
+ const forwardSlashPattern = /\//g;
5
+ const protocolPattern = /^(\w{2,}):\/\//i;
6
+ const jsonPointerSlash = /~1/g;
7
+ const jsonPointerTilde = /~0/g;
8
8
 
9
+ import { join } from "path";
10
+ import { isWindows } from "./is-windows";
11
+
12
+ const projectDir = join(__dirname, "..", "..");
9
13
  // RegExp patterns to URL-encode special characters in local filesystem paths
10
- let urlEncodePatterns = [
11
- /\?/g, "%3F",
12
- /\#/g, "%23",
13
- ];
14
+ const urlEncodePatterns = [/\?/g, "%3F", /#/g, "%23"];
14
15
 
15
16
  // RegExp patterns to URL-decode special characters for local filesystem paths
16
- let urlDecodePatterns = [
17
- /\%23/g, "#",
18
- /\%24/g, "$",
19
- /\%26/g, "&",
20
- /\%2C/g, ",",
21
- /\%40/g, "@"
22
- ];
17
+ const urlDecodePatterns = [/%23/g, "#", /%24/g, "$", /%26/g, "&", /%2C/g, ",", /%40/g, "@"];
23
18
 
24
- export const parse = (u) => new URL(u);
19
+ export const parse = (u: any) => new URL(u);
25
20
 
26
21
  /**
27
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.
28
23
  *
29
- * @return {string}
24
+ * @returns
30
25
  */
31
- export function resolve (from, to) {
32
- let resolvedUrl = new URL(to, new URL(from, "resolve://"));
26
+ export function resolve(from: any, to: any) {
27
+ const resolvedUrl = new URL(to, new URL(from, "resolve://"));
33
28
  if (resolvedUrl.protocol === "resolve:") {
34
29
  // `from` is a relative URL.
35
- let { pathname, search, hash } = resolvedUrl;
30
+ const { pathname, search, hash } = resolvedUrl;
36
31
  return pathname + search + hash;
37
32
  }
38
33
  return resolvedUrl.toString();
@@ -41,20 +36,19 @@ export function resolve (from, to) {
41
36
  /**
42
37
  * Returns the current working directory (in Node) or the current page URL (in browsers).
43
38
  *
44
- * @returns {string}
39
+ * @returns
45
40
  */
46
- export function cwd () {
41
+ export function cwd() {
47
42
  if (typeof window !== "undefined") {
48
43
  return location.href;
49
44
  }
50
45
 
51
- let path = process.cwd();
46
+ const path = process.cwd();
52
47
 
53
- let lastChar = path.slice(-1);
48
+ const lastChar = path.slice(-1);
54
49
  if (lastChar === "/" || lastChar === "\\") {
55
50
  return path;
56
- }
57
- else {
51
+ } else {
58
52
  return path + "/";
59
53
  }
60
54
  }
@@ -62,11 +56,11 @@ export function cwd () {
62
56
  /**
63
57
  * Returns the protocol of the given URL, or `undefined` if it has no protocol.
64
58
  *
65
- * @param {string} path
66
- * @returns {?string}
59
+ * @param path
60
+ * @returns
67
61
  */
68
- export function getProtocol (path) {
69
- let match = protocolPattern.exec(path);
62
+ export function getProtocol(path: string | undefined) {
63
+ const match = protocolPattern.exec(path || "");
70
64
  if (match) {
71
65
  return match[1].toLowerCase();
72
66
  }
@@ -76,11 +70,11 @@ export function getProtocol (path) {
76
70
  * Returns the lowercased file extension of the given URL,
77
71
  * or an empty string if it has no extension.
78
72
  *
79
- * @param {string} path
80
- * @returns {string}
73
+ * @param path
74
+ * @returns
81
75
  */
82
- export function getExtension (path) {
83
- let lastDot = path.lastIndexOf(".");
76
+ export function getExtension(path: any) {
77
+ const lastDot = path.lastIndexOf(".");
84
78
  if (lastDot >= 0) {
85
79
  return stripQuery(path.substr(lastDot).toLowerCase());
86
80
  }
@@ -90,11 +84,11 @@ export function getExtension (path) {
90
84
  /**
91
85
  * Removes the query, if any, from the given path.
92
86
  *
93
- * @param {string} path
94
- * @returns {string}
87
+ * @param path
88
+ * @returns
95
89
  */
96
- export function stripQuery (path) {
97
- let queryIndex = path.indexOf("?");
90
+ export function stripQuery(path: any) {
91
+ const queryIndex = path.indexOf("?");
98
92
  if (queryIndex >= 0) {
99
93
  path = path.substr(0, queryIndex);
100
94
  }
@@ -105,11 +99,11 @@ export function stripQuery (path) {
105
99
  * Returns the hash (URL fragment), of the given path.
106
100
  * If there is no hash, then the root hash ("#") is returned.
107
101
  *
108
- * @param {string} path
109
- * @returns {string}
102
+ * @param path
103
+ * @returns
110
104
  */
111
- export function getHash (path) {
112
- let hashIndex = path.indexOf("#");
105
+ export function getHash(path: any) {
106
+ const hashIndex = path.indexOf("#");
113
107
  if (hashIndex >= 0) {
114
108
  return path.substr(hashIndex);
115
109
  }
@@ -119,11 +113,11 @@ export function getHash (path) {
119
113
  /**
120
114
  * Removes the hash (URL fragment), if any, from the given path.
121
115
  *
122
- * @param {string} path
123
- * @returns {string}
116
+ * @param path
117
+ * @returns
124
118
  */
125
- export function stripHash (path) {
126
- let hashIndex = path.indexOf("#");
119
+ export function stripHash(path: any) {
120
+ const hashIndex = path.indexOf("#");
127
121
  if (hashIndex >= 0) {
128
122
  path = path.substr(0, hashIndex);
129
123
  }
@@ -133,19 +127,17 @@ export function stripHash (path) {
133
127
  /**
134
128
  * Determines whether the given path is an HTTP(S) URL.
135
129
  *
136
- * @param {string} path
137
- * @returns {boolean}
130
+ * @param path
131
+ * @returns
138
132
  */
139
- export function isHttp (path) {
140
- let protocol = getProtocol(path);
133
+ export function isHttp(path: any) {
134
+ const protocol = getProtocol(path);
141
135
  if (protocol === "http" || protocol === "https") {
142
136
  return true;
143
- }
144
- else if (protocol === undefined) {
137
+ } else if (protocol === undefined) {
145
138
  // There is no protocol. If we're running in a browser, then assume it's HTTP.
146
139
  return typeof window !== "undefined";
147
- }
148
- else {
140
+ } else {
149
141
  // It's some other protocol, such as "ftp://", "mongodb://", etc.
150
142
  return false;
151
143
  }
@@ -155,17 +147,18 @@ export function isHttp (path) {
155
147
  * Determines whether the given path is a filesystem path.
156
148
  * This includes "file://" URLs.
157
149
  *
158
- * @param {string} path
159
- * @returns {boolean}
150
+ * @param path
151
+ * @returns
160
152
  */
161
- export function isFileSystemPath (path) {
162
- if (process.browser) {
153
+ export function isFileSystemPath(path: string | undefined) {
154
+ // @ts-ignore
155
+ if (typeof window !== "undefined" || process.browser) {
163
156
  // We're running in a browser, so assume that all paths are URLs.
164
157
  // This way, even relative paths will be treated as URLs rather than as filesystem paths
165
158
  return false;
166
159
  }
167
160
 
168
- let protocol = getProtocol(path);
161
+ const protocol = getProtocol(path);
169
162
  return protocol === undefined || protocol === "file";
170
163
  }
171
164
 
@@ -182,21 +175,24 @@ export function isFileSystemPath (path) {
182
175
  * C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json
183
176
  * file://Project #42/file.json ==> file://Project%20%2342/file.json
184
177
  *
185
- * @param {string} path
186
- * @returns {string}
178
+ * @param path
179
+ * @returns
187
180
  */
188
- export function fromFileSystemPath (path) {
181
+ export function fromFileSystemPath(path: any) {
189
182
  // Step 1: On Windows, replace backslashes with forward slashes,
190
183
  // rather than encoding them as "%5C"
191
- if (isWindows) {
192
- const hasProjectDir = path.toUpperCase().includes(projectDir.replace(/\\/g, "\\").toUpperCase());
193
- const hasProjectUri = path.toUpperCase().includes(projectDir.replace(/\\/g, "/").toUpperCase());
194
- if (hasProjectDir || hasProjectUri) {
195
- path = path.replace(/\\/g, "/");
196
- }
197
- else {
198
- path = `${projectDir}/${path}`.replace(/\\/g, "/");
184
+ if (isWindows()) {
185
+ const upperPath = path.toUpperCase();
186
+ const projectDirPosixPath = convertPathToPosix(projectDir);
187
+ const posixUpper = projectDirPosixPath.toUpperCase();
188
+ const hasProjectDir = upperPath.includes(posixUpper);
189
+ const hasProjectUri = upperPath.includes(posixUpper);
190
+ const isAbsolutePath = win32.isAbsolute(path);
191
+
192
+ if (!(hasProjectDir || hasProjectUri || isAbsolutePath)) {
193
+ path = join(projectDir, path);
199
194
  }
195
+ path = convertPathToPosix(path);
200
196
  }
201
197
 
202
198
  // Step 2: `encodeURI` will take care of MOST characters
@@ -214,20 +210,16 @@ export function fromFileSystemPath (path) {
214
210
 
215
211
  /**
216
212
  * Converts a URL to a local filesystem path.
217
- *
218
- * @param {string} path
219
- * @param {boolean} [keepFileProtocol] - If true, then "file://" will NOT be stripped
220
- * @returns {string}
221
213
  */
222
- export function toFileSystemPath (path, keepFileProtocol) {
214
+ export function toFileSystemPath(path: string | undefined, keepFileProtocol?: boolean): string {
223
215
  // Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc.
224
- path = decodeURI(path);
216
+ path = decodeURI(path!);
225
217
 
226
218
  // Step 2: Manually decode characters that are not decoded by `decodeURI`.
227
219
  // This includes characters such as "#" and "?", which have special meaning in URLs,
228
220
  // but are just normal characters in a filesystem path.
229
221
  for (let i = 0; i < urlDecodePatterns.length; i += 2) {
230
- path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]);
222
+ path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1] as string);
231
223
  }
232
224
 
233
225
  // Step 3: If it's a "file://" URL, then format it consistently
@@ -238,25 +230,24 @@ export function toFileSystemPath (path, keepFileProtocol) {
238
230
  path = path[7] === "/" ? path.substr(8) : path.substr(7);
239
231
 
240
232
  // insert a colon (":") after the drive letter on Windows
241
- if (isWindows && path[1] === "/") {
233
+ if (isWindows() && path[1] === "/") {
242
234
  path = path[0] + ":" + path.substr(1);
243
235
  }
244
236
 
245
237
  if (keepFileProtocol) {
246
238
  // Return the consistently-formatted "file://" URL
247
239
  path = "file:///" + path;
248
- }
249
- else {
240
+ } else {
250
241
  // Convert the "file://" URL to a local filesystem path.
251
242
  // On Windows, it will start with something like "C:/".
252
243
  // On Posix, it will start with "/"
253
244
  isFileUrl = false;
254
- path = isWindows ? path : "/" + path;
245
+ path = isWindows() ? path : "/" + path;
255
246
  }
256
247
  }
257
248
 
258
249
  // Step 4: Normalize Windows paths (unless it's a "file://" URL)
259
- if (isWindows && !isFileUrl) {
250
+ if (isWindows() && !isFileUrl) {
260
251
  // Replace forward slashes with backslashes
261
252
  path = path.replace(forwardSlashPattern, "\\");
262
253
 
@@ -272,10 +263,10 @@ export function toFileSystemPath (path, keepFileProtocol) {
272
263
  /**
273
264
  * Converts a $ref pointer to a valid JSON Path.
274
265
  *
275
- * @param {string} pointer
276
- * @returns {Array<number | string>}
266
+ * @param pointer
267
+ * @returns
277
268
  */
278
- export function safePointerToPath (pointer) {
269
+ export function safePointerToPath(pointer: any) {
279
270
  if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") {
280
271
  return [];
281
272
  }
@@ -283,9 +274,19 @@ export function safePointerToPath (pointer) {
283
274
  return pointer
284
275
  .slice(2)
285
276
  .split("/")
286
- .map((value) => {
287
- return decodeURIComponent(value)
288
- .replace(jsonPointerSlash, "/")
289
- .replace(jsonPointerTilde, "~");
277
+ .map((value: any) => {
278
+ return decodeURIComponent(value).replace(jsonPointerSlash, "/").replace(jsonPointerTilde, "~");
290
279
  });
291
280
  }
281
+
282
+ export function relative(from: string | undefined, to: string | undefined) {
283
+ if (!isFileSystemPath(from) || !isFileSystemPath(to)) {
284
+ return resolve(from, to);
285
+ }
286
+
287
+ const fromDir = path.dirname(stripHash(from));
288
+ const toPath = stripHash(to);
289
+
290
+ const result = path.relative(fromDir, toPath);
291
+ return result + getHash(to);
292
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apidevtools/json-schema-ref-parser",
3
- "version": "10.0.1",
3
+ "version": "11.0.0",
4
4
  "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
5
5
  "keywords": [
6
6
  "json",
@@ -28,6 +28,10 @@
28
28
  {
29
29
  "name": "Jakub Rożek",
30
30
  "email": "jakub@stoplight.io"
31
+ },
32
+ {
33
+ "name": "JonLuca DeCaro",
34
+ "email": "apis@jonlu.ca"
31
35
  }
32
36
  ],
33
37
  "homepage": "https://apitools.dev/json-schema-ref-parser/",
@@ -37,67 +41,61 @@
37
41
  },
38
42
  "license": "MIT",
39
43
  "funding": "https://github.com/sponsors/philsturgeon",
40
- "main": "lib/index.js",
41
- "typings": "lib/index.d.ts",
42
- "exports": {
43
- "types": "./lib/index.d.ts",
44
- "require": "./cjs/index.js",
45
- "default": "./lib/index.js"
46
- },
44
+ "types": "dist/lib/index.d.ts",
45
+ "main": "dist/lib/index.js",
47
46
  "browser": {
48
47
  "fs": false
49
48
  },
50
49
  "engines": {
51
- "node": ">= 17"
50
+ "node": ">= 16"
52
51
  },
53
52
  "files": [
54
53
  "lib",
54
+ "dist",
55
55
  "cjs"
56
56
  ],
57
- "type": "module",
58
57
  "scripts": {
59
- "build": "swc ./lib/*.js -d ./cjs && swc ./lib/**/*.js -d ./cjs && copyfiles ./lib/**/*.cjs -u 1 ./cjs && node ./scripts/writeCJSPackageJSON.cjs",
60
- "clean": "shx rm -rf coverage cjs",
61
- "lint": "eslint lib test/fixtures test/specs",
62
- "test": "npm run test:node && npm run test:typescript && npm run test:browser && npm run lint",
63
- "test:node": "mocha",
64
- "test:browser": "cross-env NODE_OPTIONS=--openssl-legacy-provider karma start karma.conf.cjs --single-run",
65
- "test:typescript": "tsc --noEmit --strict --lib esnext,dom test/specs/typescript-definition.spec.ts",
66
- "coverage": "npm run coverage:node && npm run coverage:browser",
67
- "coverage:node": "c8 node_modules/mocha/bin/mocha",
68
- "coverage:browser": "npm run test:browser -- --coverage",
69
- "upgrade": "npm-check -u && npm audit fix"
58
+ "prepublishOnly": "yarn build",
59
+ "lint": "eslint lib",
60
+ "build": "rm -fr dist/* && tsc",
61
+ "typecheck": "tsc --noEmit",
62
+ "prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"",
63
+ "test": "vitest --coverage",
64
+ "test:node": "yarn test",
65
+ "test:browser": "cross-env BROWSER=\"true\" yarn test",
66
+ "test:update": "vitest -u",
67
+ "test:watch": "vitest -w"
70
68
  },
71
69
  "devDependencies": {
72
- "@babel/polyfill": "^7.12.1",
73
- "@chiragrupani/karma-chromium-edge-launcher": "^2.2.2",
74
- "@jsdevtools/eslint-config": "^1.0.7",
75
- "@jsdevtools/host-environment": "^2.1.2",
76
- "@jsdevtools/karma-config": "^3.1.7",
77
- "@swc/cli": "^0.1.59",
78
- "@swc/core": "^1.3.25",
79
- "@types/node": "^14.14.21",
80
- "c8": "^7.12.0",
81
- "chai": "^4.2.0",
82
- "chai-subset": "^1.6.0",
83
- "chokidar": "^3.5.3",
84
- "copyfiles": "^2.4.1",
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
+ "abortcontroller-polyfill": "^1.7.5",
85
78
  "cross-env": "^7.0.3",
86
- "eslint": "^7.18.0",
87
- "isomorphic-fetch": "^3.0.0",
88
- "karma": "^5.0.2",
89
- "karma-cli": "^2.0.0",
90
- "mocha": "^8.2.1",
91
- "node-abort-controller": "^3.0.1",
92
- "npm-check": "^5.9.0",
93
- "shx": "^0.3.2",
94
- "typescript": "^4.0.5"
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",
84
+ "eslint-plugin-promise": "^6.1.1",
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"
95
92
  },
96
93
  "dependencies": {
97
94
  "@jsdevtools/ono": "^7.1.3",
98
- "@types/json-schema": "^7.0.6",
99
- "call-me-maybe": "^1.0.1",
100
- "js-yaml": "^4.1.0"
95
+ "@types/json-schema": "^7.0.13",
96
+ "@types/lodash.clonedeep": "^4.5.7",
97
+ "js-yaml": "^4.1.0",
98
+ "lodash.clonedeep": "^4.5.0"
101
99
  },
102
100
  "release": {
103
101
  "branches": [