@hey-api/json-schema-ref-parser 0.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.
- package/LICENSE +21 -0
- package/README.md +138 -0
- package/dist/lib/bundle.d.ts +26 -0
- package/dist/lib/bundle.js +293 -0
- package/dist/lib/dereference.d.ts +11 -0
- package/dist/lib/dereference.js +224 -0
- package/dist/lib/index.d.ts +74 -0
- package/dist/lib/index.js +208 -0
- package/dist/lib/options.d.ts +61 -0
- package/dist/lib/options.js +45 -0
- package/dist/lib/parse.d.ts +13 -0
- package/dist/lib/parse.js +87 -0
- package/dist/lib/parsers/binary.d.ts +2 -0
- package/dist/lib/parsers/binary.js +12 -0
- package/dist/lib/parsers/json.d.ts +2 -0
- package/dist/lib/parsers/json.js +38 -0
- package/dist/lib/parsers/text.d.ts +2 -0
- package/dist/lib/parsers/text.js +18 -0
- package/dist/lib/parsers/yaml.d.ts +2 -0
- package/dist/lib/parsers/yaml.js +28 -0
- package/dist/lib/pointer.d.ts +88 -0
- package/dist/lib/pointer.js +293 -0
- package/dist/lib/ref.d.ts +180 -0
- package/dist/lib/ref.js +226 -0
- package/dist/lib/refs.d.ts +127 -0
- package/dist/lib/refs.js +232 -0
- package/dist/lib/resolve-external.d.ts +13 -0
- package/dist/lib/resolve-external.js +147 -0
- package/dist/lib/resolvers/file.d.ts +4 -0
- package/dist/lib/resolvers/file.js +61 -0
- package/dist/lib/resolvers/url.d.ts +11 -0
- package/dist/lib/resolvers/url.js +57 -0
- package/dist/lib/types/index.d.ts +43 -0
- package/dist/lib/types/index.js +2 -0
- 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/errors.d.ts +56 -0
- package/dist/lib/util/errors.js +112 -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.d.ts +16 -0
- package/dist/lib/util/plugins.js +45 -0
- package/dist/lib/util/url.d.ts +79 -0
- package/dist/lib/util/url.js +285 -0
- package/dist/vite.config.d.ts +2 -0
- package/dist/vite.config.js +18 -0
- package/lib/bundle.ts +299 -0
- package/lib/dereference.ts +286 -0
- package/lib/index.ts +209 -0
- package/lib/options.ts +108 -0
- package/lib/parse.ts +56 -0
- package/lib/parsers/binary.ts +13 -0
- package/lib/parsers/json.ts +39 -0
- package/lib/parsers/text.ts +21 -0
- package/lib/parsers/yaml.ts +26 -0
- package/lib/pointer.ts +327 -0
- package/lib/ref.ts +279 -0
- package/lib/refs.ts +239 -0
- package/lib/resolve-external.ts +141 -0
- package/lib/resolvers/file.ts +24 -0
- package/lib/resolvers/url.ts +78 -0
- package/lib/types/index.ts +51 -0
- package/lib/util/convert-path-to-posix.ts +11 -0
- package/lib/util/errors.ts +153 -0
- package/lib/util/is-windows.ts +2 -0
- package/lib/util/plugins.ts +56 -0
- package/lib/util/url.ts +266 -0
- package/package.json +96 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InvalidPointerError = exports.TimeoutError = exports.MissingPointerError = exports.UnmatchedResolverError = exports.ResolverError = exports.UnmatchedParserError = exports.ParserError = exports.JSONParserErrorGroup = exports.JSONParserError = void 0;
|
|
4
|
+
exports.isHandledError = isHandledError;
|
|
5
|
+
exports.normalizeError = normalizeError;
|
|
6
|
+
const ono_1 = require("@jsdevtools/ono");
|
|
7
|
+
const url_js_1 = require("./url.js");
|
|
8
|
+
class JSONParserError extends Error {
|
|
9
|
+
constructor(message, source) {
|
|
10
|
+
super();
|
|
11
|
+
this.code = "EUNKNOWN";
|
|
12
|
+
this.name = "JSONParserError";
|
|
13
|
+
this.message = message;
|
|
14
|
+
this.source = source;
|
|
15
|
+
this.path = null;
|
|
16
|
+
ono_1.Ono.extend(this);
|
|
17
|
+
}
|
|
18
|
+
get footprint() {
|
|
19
|
+
return `${this.path}+${this.source}+${this.code}+${this.message}`;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.JSONParserError = JSONParserError;
|
|
23
|
+
class JSONParserErrorGroup extends Error {
|
|
24
|
+
constructor(parser) {
|
|
25
|
+
super();
|
|
26
|
+
this.files = parser;
|
|
27
|
+
this.name = "JSONParserErrorGroup";
|
|
28
|
+
this.message = `${this.errors.length} error${this.errors.length > 1 ? "s" : ""} occurred while reading '${(0, url_js_1.toFileSystemPath)(parser.$refs._root$Ref.path)}'`;
|
|
29
|
+
ono_1.Ono.extend(this);
|
|
30
|
+
}
|
|
31
|
+
static getParserErrors(parser) {
|
|
32
|
+
const errors = [];
|
|
33
|
+
for (const $ref of Object.values(parser.$refs._$refs)) {
|
|
34
|
+
if ($ref.errors) {
|
|
35
|
+
errors.push(...$ref.errors);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return errors;
|
|
39
|
+
}
|
|
40
|
+
get errors() {
|
|
41
|
+
return JSONParserErrorGroup.getParserErrors(this.files);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.JSONParserErrorGroup = JSONParserErrorGroup;
|
|
45
|
+
class ParserError extends JSONParserError {
|
|
46
|
+
constructor(message, source) {
|
|
47
|
+
super(`Error parsing ${source}: ${message}`, source);
|
|
48
|
+
this.code = "EPARSER";
|
|
49
|
+
this.name = "ParserError";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.ParserError = ParserError;
|
|
53
|
+
class UnmatchedParserError extends JSONParserError {
|
|
54
|
+
constructor(source) {
|
|
55
|
+
super(`Could not find parser for "${source}"`, source);
|
|
56
|
+
this.code = "EUNMATCHEDPARSER";
|
|
57
|
+
this.name = "UnmatchedParserError";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.UnmatchedParserError = UnmatchedParserError;
|
|
61
|
+
class ResolverError extends JSONParserError {
|
|
62
|
+
constructor(ex, source) {
|
|
63
|
+
super(ex.message || `Error reading file "${source}"`, source);
|
|
64
|
+
this.code = "ERESOLVER";
|
|
65
|
+
this.name = "ResolverError";
|
|
66
|
+
if ("code" in ex) {
|
|
67
|
+
this.ioErrorCode = String(ex.code);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.ResolverError = ResolverError;
|
|
72
|
+
class UnmatchedResolverError extends JSONParserError {
|
|
73
|
+
constructor(source) {
|
|
74
|
+
super(`Could not find resolver for "${source}"`, source);
|
|
75
|
+
this.code = "EUNMATCHEDRESOLVER";
|
|
76
|
+
this.name = "UnmatchedResolverError";
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.UnmatchedResolverError = UnmatchedResolverError;
|
|
80
|
+
class MissingPointerError extends JSONParserError {
|
|
81
|
+
constructor(token, path) {
|
|
82
|
+
super(`Missing $ref pointer "${(0, url_js_1.getHash)(path)}". Token "${token}" does not exist.`, (0, url_js_1.stripHash)(path));
|
|
83
|
+
this.code = "EMISSINGPOINTER";
|
|
84
|
+
this.name = "MissingPointerError";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.MissingPointerError = MissingPointerError;
|
|
88
|
+
class TimeoutError extends JSONParserError {
|
|
89
|
+
constructor(timeout) {
|
|
90
|
+
super(`Dereferencing timeout reached: ${timeout}ms`);
|
|
91
|
+
this.code = "ETIMEOUT";
|
|
92
|
+
this.name = "TimeoutError";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.TimeoutError = TimeoutError;
|
|
96
|
+
class InvalidPointerError extends JSONParserError {
|
|
97
|
+
constructor(pointer, path) {
|
|
98
|
+
super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, (0, url_js_1.stripHash)(path));
|
|
99
|
+
this.code = "EUNMATCHEDRESOLVER";
|
|
100
|
+
this.name = "InvalidPointerError";
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.InvalidPointerError = InvalidPointerError;
|
|
104
|
+
function isHandledError(err) {
|
|
105
|
+
return err instanceof JSONParserError || err instanceof JSONParserErrorGroup;
|
|
106
|
+
}
|
|
107
|
+
function normalizeError(err) {
|
|
108
|
+
if (err.path === null) {
|
|
109
|
+
err.path = [];
|
|
110
|
+
}
|
|
111
|
+
return err;
|
|
112
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isWindows: () => boolean;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isWindows = void 0;
|
|
4
|
+
const isWindowsConst = /^win/.test(globalThis.process ? globalThis.process.platform : "");
|
|
5
|
+
const isWindows = () => isWindowsConst;
|
|
6
|
+
exports.isWindows = isWindows;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FileInfo, JSONSchema } from "../types/index.js";
|
|
2
|
+
import type { Plugin } from "../types/index.js";
|
|
3
|
+
export interface PluginResult {
|
|
4
|
+
error?: any;
|
|
5
|
+
plugin: Pick<Plugin, 'handler'>;
|
|
6
|
+
result?: string | Buffer | JSONSchema;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Runs the specified method of the given plugins, in order, until one of them returns a successful result.
|
|
10
|
+
* Each method can return a synchronous value, a Promise, or call an error-first callback.
|
|
11
|
+
* If the promise resolves successfully, or the callback is called without an error, then the result
|
|
12
|
+
* is immediately returned and no further plugins are called.
|
|
13
|
+
* If the promise rejects, or the callback is called with an error, then the next plugin is called.
|
|
14
|
+
* If ALL plugins fail, then the last error is thrown.
|
|
15
|
+
*/
|
|
16
|
+
export declare function run(plugins: Pick<Plugin, 'handler'>[], file: FileInfo): Promise<PluginResult>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.run = run;
|
|
4
|
+
/**
|
|
5
|
+
* Runs the specified method of the given plugins, in order, until one of them returns a successful result.
|
|
6
|
+
* Each method can return a synchronous value, a Promise, or call an error-first callback.
|
|
7
|
+
* If the promise resolves successfully, or the callback is called without an error, then the result
|
|
8
|
+
* is immediately returned and no further plugins are called.
|
|
9
|
+
* If the promise rejects, or the callback is called with an error, then the next plugin is called.
|
|
10
|
+
* If ALL plugins fail, then the last error is thrown.
|
|
11
|
+
*/
|
|
12
|
+
async function run(plugins, file) {
|
|
13
|
+
let index = 0;
|
|
14
|
+
let lastError;
|
|
15
|
+
let plugin;
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const runNextPlugin = async () => {
|
|
18
|
+
plugin = plugins[index++];
|
|
19
|
+
if (!plugin) {
|
|
20
|
+
// there are no more functions, re-throw the last error
|
|
21
|
+
return reject(lastError);
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const result = await plugin.handler(file);
|
|
25
|
+
if (result !== undefined) {
|
|
26
|
+
return resolve({
|
|
27
|
+
plugin,
|
|
28
|
+
result,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (index === plugins.length) {
|
|
32
|
+
throw new Error("No promise has been returned.");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
lastError = {
|
|
37
|
+
plugin,
|
|
38
|
+
error: e,
|
|
39
|
+
};
|
|
40
|
+
runNextPlugin();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
runNextPlugin();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF.
|
|
3
|
+
*
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolve(from: string, to: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the current working directory (in Node) or the current page URL (in browsers).
|
|
9
|
+
*
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare function cwd(): string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the protocol of the given URL, or `undefined` if it has no protocol.
|
|
15
|
+
*
|
|
16
|
+
* @param path
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export declare function getProtocol(path: string | undefined): string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the lowercased file extension of the given URL,
|
|
22
|
+
* or an empty string if it has no extension.
|
|
23
|
+
*
|
|
24
|
+
* @param path
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
export declare function getExtension(path: any): any;
|
|
28
|
+
/**
|
|
29
|
+
* Removes the query, if any, from the given path.
|
|
30
|
+
*
|
|
31
|
+
* @param path
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
export declare function stripQuery(path: any): any;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the hash (URL fragment), of the given path.
|
|
37
|
+
* If there is no hash, then the root hash ("#") is returned.
|
|
38
|
+
*
|
|
39
|
+
* @param path
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
export declare function getHash(path: undefined | string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Removes the hash (URL fragment), if any, from the given path.
|
|
45
|
+
*
|
|
46
|
+
* @param path
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
export declare function stripHash(path?: string | undefined): string;
|
|
50
|
+
/**
|
|
51
|
+
* Determines whether the given path is a filesystem path.
|
|
52
|
+
* This includes "file://" URLs.
|
|
53
|
+
*
|
|
54
|
+
* @param path
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
export declare function isFileSystemPath(path: string | undefined): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Converts a filesystem path to a properly-encoded URL.
|
|
60
|
+
*
|
|
61
|
+
* This is intended to handle situations where JSON Schema $Ref Parser is called
|
|
62
|
+
* with a filesystem path that contains characters which are not allowed in URLs.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* The following filesystem paths would be converted to the following URLs:
|
|
66
|
+
*
|
|
67
|
+
* <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json
|
|
68
|
+
* C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json
|
|
69
|
+
* file://Project #42/file.json ==> file://Project%20%2342/file.json
|
|
70
|
+
*
|
|
71
|
+
* @param path
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
export declare function fromFileSystemPath(path: string): string;
|
|
75
|
+
/**
|
|
76
|
+
* Converts a URL to a local filesystem path.
|
|
77
|
+
*/
|
|
78
|
+
export declare function toFileSystemPath(path: string | undefined, keepFileProtocol?: boolean): string;
|
|
79
|
+
export declare function relative(from: string, to: string): string;
|
|
@@ -0,0 +1,285 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.resolve = resolve;
|
|
40
|
+
exports.cwd = cwd;
|
|
41
|
+
exports.getProtocol = getProtocol;
|
|
42
|
+
exports.getExtension = getExtension;
|
|
43
|
+
exports.stripQuery = stripQuery;
|
|
44
|
+
exports.getHash = getHash;
|
|
45
|
+
exports.stripHash = stripHash;
|
|
46
|
+
exports.isFileSystemPath = isFileSystemPath;
|
|
47
|
+
exports.fromFileSystemPath = fromFileSystemPath;
|
|
48
|
+
exports.toFileSystemPath = toFileSystemPath;
|
|
49
|
+
exports.relative = relative;
|
|
50
|
+
const convert_path_to_posix_1 = __importDefault(require("./convert-path-to-posix"));
|
|
51
|
+
const path_1 = __importStar(require("path"));
|
|
52
|
+
const forwardSlashPattern = /\//g;
|
|
53
|
+
const protocolPattern = /^(\w{2,}):\/\//i;
|
|
54
|
+
const path_2 = require("path");
|
|
55
|
+
const is_windows_1 = require("./is-windows");
|
|
56
|
+
// RegExp patterns to URL-encode special characters in local filesystem paths
|
|
57
|
+
const urlEncodePatterns = [
|
|
58
|
+
[/\?/g, "%3F"],
|
|
59
|
+
[/#/g, "%23"],
|
|
60
|
+
];
|
|
61
|
+
// RegExp patterns to URL-decode special characters for local filesystem paths
|
|
62
|
+
const urlDecodePatterns = [/%23/g, "#", /%24/g, "$", /%26/g, "&", /%2C/g, ",", /%40/g, "@"];
|
|
63
|
+
/**
|
|
64
|
+
* Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF.
|
|
65
|
+
*
|
|
66
|
+
* @returns
|
|
67
|
+
*/
|
|
68
|
+
function resolve(from, to) {
|
|
69
|
+
const fromUrl = new URL((0, convert_path_to_posix_1.default)(from), "resolve://");
|
|
70
|
+
const resolvedUrl = new URL((0, convert_path_to_posix_1.default)(to), fromUrl);
|
|
71
|
+
const endSpaces = to.match(/(\s*)$/)?.[1] || "";
|
|
72
|
+
if (resolvedUrl.protocol === "resolve:") {
|
|
73
|
+
// `from` is a relative URL.
|
|
74
|
+
const { pathname, search, hash } = resolvedUrl;
|
|
75
|
+
return pathname + search + hash + endSpaces;
|
|
76
|
+
}
|
|
77
|
+
return resolvedUrl.toString() + endSpaces;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns the current working directory (in Node) or the current page URL (in browsers).
|
|
81
|
+
*
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
function cwd() {
|
|
85
|
+
if (typeof window !== "undefined") {
|
|
86
|
+
return location.href;
|
|
87
|
+
}
|
|
88
|
+
const path = process.cwd();
|
|
89
|
+
const lastChar = path.slice(-1);
|
|
90
|
+
if (lastChar === "/" || lastChar === "\\") {
|
|
91
|
+
return path;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return path + "/";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Returns the protocol of the given URL, or `undefined` if it has no protocol.
|
|
99
|
+
*
|
|
100
|
+
* @param path
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
function getProtocol(path) {
|
|
104
|
+
const match = protocolPattern.exec(path || "");
|
|
105
|
+
if (match) {
|
|
106
|
+
return match[1].toLowerCase();
|
|
107
|
+
}
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Returns the lowercased file extension of the given URL,
|
|
112
|
+
* or an empty string if it has no extension.
|
|
113
|
+
*
|
|
114
|
+
* @param path
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
function getExtension(path) {
|
|
118
|
+
const lastDot = path.lastIndexOf(".");
|
|
119
|
+
if (lastDot > -1) {
|
|
120
|
+
return stripQuery(path.substr(lastDot).toLowerCase());
|
|
121
|
+
}
|
|
122
|
+
return "";
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Removes the query, if any, from the given path.
|
|
126
|
+
*
|
|
127
|
+
* @param path
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
function stripQuery(path) {
|
|
131
|
+
const queryIndex = path.indexOf("?");
|
|
132
|
+
if (queryIndex > -1) {
|
|
133
|
+
path = path.substr(0, queryIndex);
|
|
134
|
+
}
|
|
135
|
+
return path;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Returns the hash (URL fragment), of the given path.
|
|
139
|
+
* If there is no hash, then the root hash ("#") is returned.
|
|
140
|
+
*
|
|
141
|
+
* @param path
|
|
142
|
+
* @returns
|
|
143
|
+
*/
|
|
144
|
+
function getHash(path) {
|
|
145
|
+
if (!path) {
|
|
146
|
+
return "#";
|
|
147
|
+
}
|
|
148
|
+
const hashIndex = path.indexOf("#");
|
|
149
|
+
if (hashIndex > -1) {
|
|
150
|
+
return path.substring(hashIndex);
|
|
151
|
+
}
|
|
152
|
+
return "#";
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Removes the hash (URL fragment), if any, from the given path.
|
|
156
|
+
*
|
|
157
|
+
* @param path
|
|
158
|
+
* @returns
|
|
159
|
+
*/
|
|
160
|
+
function stripHash(path) {
|
|
161
|
+
if (!path) {
|
|
162
|
+
return "";
|
|
163
|
+
}
|
|
164
|
+
const hashIndex = path.indexOf("#");
|
|
165
|
+
if (hashIndex > -1) {
|
|
166
|
+
path = path.substring(0, hashIndex);
|
|
167
|
+
}
|
|
168
|
+
return path;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Determines whether the given path is a filesystem path.
|
|
172
|
+
* This includes "file://" URLs.
|
|
173
|
+
*
|
|
174
|
+
* @param path
|
|
175
|
+
* @returns
|
|
176
|
+
*/
|
|
177
|
+
function isFileSystemPath(path) {
|
|
178
|
+
// @ts-ignore
|
|
179
|
+
if (typeof window !== "undefined" || (typeof process !== "undefined" && process.browser)) {
|
|
180
|
+
// We're running in a browser, so assume that all paths are URLs.
|
|
181
|
+
// This way, even relative paths will be treated as URLs rather than as filesystem paths
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
const protocol = getProtocol(path);
|
|
185
|
+
return protocol === undefined || protocol === "file";
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Converts a filesystem path to a properly-encoded URL.
|
|
189
|
+
*
|
|
190
|
+
* This is intended to handle situations where JSON Schema $Ref Parser is called
|
|
191
|
+
* with a filesystem path that contains characters which are not allowed in URLs.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* The following filesystem paths would be converted to the following URLs:
|
|
195
|
+
*
|
|
196
|
+
* <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json
|
|
197
|
+
* C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json
|
|
198
|
+
* file://Project #42/file.json ==> file://Project%20%2342/file.json
|
|
199
|
+
*
|
|
200
|
+
* @param path
|
|
201
|
+
* @returns
|
|
202
|
+
*/
|
|
203
|
+
function fromFileSystemPath(path) {
|
|
204
|
+
// Step 1: On Windows, replace backslashes with forward slashes,
|
|
205
|
+
// rather than encoding them as "%5C"
|
|
206
|
+
if ((0, is_windows_1.isWindows)()) {
|
|
207
|
+
const projectDir = cwd();
|
|
208
|
+
const upperPath = path.toUpperCase();
|
|
209
|
+
const projectDirPosixPath = (0, convert_path_to_posix_1.default)(projectDir);
|
|
210
|
+
const posixUpper = projectDirPosixPath.toUpperCase();
|
|
211
|
+
const hasProjectDir = upperPath.includes(posixUpper);
|
|
212
|
+
const hasProjectUri = upperPath.includes(posixUpper);
|
|
213
|
+
const isAbsolutePath = path_1.win32?.isAbsolute(path) ||
|
|
214
|
+
path.startsWith("http://") ||
|
|
215
|
+
path.startsWith("https://") ||
|
|
216
|
+
path.startsWith("file://");
|
|
217
|
+
if (!(hasProjectDir || hasProjectUri || isAbsolutePath) && !projectDir.startsWith("http")) {
|
|
218
|
+
path = (0, path_2.join)(projectDir, path);
|
|
219
|
+
}
|
|
220
|
+
path = (0, convert_path_to_posix_1.default)(path);
|
|
221
|
+
}
|
|
222
|
+
// Step 2: `encodeURI` will take care of MOST characters
|
|
223
|
+
path = encodeURI(path);
|
|
224
|
+
// Step 3: Manually encode characters that are not encoded by `encodeURI`.
|
|
225
|
+
// This includes characters such as "#" and "?", which have special meaning in URLs,
|
|
226
|
+
// but are just normal characters in a filesystem path.
|
|
227
|
+
for (const pattern of urlEncodePatterns) {
|
|
228
|
+
path = path.replace(pattern[0], pattern[1]);
|
|
229
|
+
}
|
|
230
|
+
return path;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Converts a URL to a local filesystem path.
|
|
234
|
+
*/
|
|
235
|
+
function toFileSystemPath(path, keepFileProtocol) {
|
|
236
|
+
// Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc.
|
|
237
|
+
path = decodeURI(path);
|
|
238
|
+
// Step 2: Manually decode characters that are not decoded by `decodeURI`.
|
|
239
|
+
// This includes characters such as "#" and "?", which have special meaning in URLs,
|
|
240
|
+
// but are just normal characters in a filesystem path.
|
|
241
|
+
for (let i = 0; i < urlDecodePatterns.length; i += 2) {
|
|
242
|
+
path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]);
|
|
243
|
+
}
|
|
244
|
+
// Step 3: If it's a "file://" URL, then format it consistently
|
|
245
|
+
// or convert it to a local filesystem path
|
|
246
|
+
let isFileUrl = path.substr(0, 7).toLowerCase() === "file://";
|
|
247
|
+
if (isFileUrl) {
|
|
248
|
+
// Strip-off the protocol, and the initial "/", if there is one
|
|
249
|
+
path = path[7] === "/" ? path.substr(8) : path.substr(7);
|
|
250
|
+
// insert a colon (":") after the drive letter on Windows
|
|
251
|
+
if ((0, is_windows_1.isWindows)() && path[1] === "/") {
|
|
252
|
+
path = path[0] + ":" + path.substr(1);
|
|
253
|
+
}
|
|
254
|
+
if (keepFileProtocol) {
|
|
255
|
+
// Return the consistently-formatted "file://" URL
|
|
256
|
+
path = "file:///" + path;
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
// Convert the "file://" URL to a local filesystem path.
|
|
260
|
+
// On Windows, it will start with something like "C:/".
|
|
261
|
+
// On Posix, it will start with "/"
|
|
262
|
+
isFileUrl = false;
|
|
263
|
+
path = (0, is_windows_1.isWindows)() ? path : "/" + path;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// Step 4: Normalize Windows paths (unless it's a "file://" URL)
|
|
267
|
+
if ((0, is_windows_1.isWindows)() && !isFileUrl) {
|
|
268
|
+
// Replace forward slashes with backslashes
|
|
269
|
+
path = path.replace(forwardSlashPattern, "\\");
|
|
270
|
+
// Capitalize the drive letter
|
|
271
|
+
if (path.substr(1, 2) === ":\\") {
|
|
272
|
+
path = path[0].toUpperCase() + path.substr(1);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return path;
|
|
276
|
+
}
|
|
277
|
+
function relative(from, to) {
|
|
278
|
+
if (!isFileSystemPath(from) || !isFileSystemPath(to)) {
|
|
279
|
+
return resolve(from, to);
|
|
280
|
+
}
|
|
281
|
+
const fromDir = path_1.default.dirname(stripHash(from));
|
|
282
|
+
const toPath = stripHash(to);
|
|
283
|
+
const result = path_1.default.relative(fromDir, toPath);
|
|
284
|
+
return result + getHash(to);
|
|
285
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const config_1 = require("vitest/config");
|
|
4
|
+
const isBrowser = process.env.BROWSER === "true";
|
|
5
|
+
exports.default = (0, config_1.defineConfig)({
|
|
6
|
+
test: {
|
|
7
|
+
environment: isBrowser ? "jsdom" : "node",
|
|
8
|
+
dir: "test",
|
|
9
|
+
exclude: ["**/__IGNORED__/**"],
|
|
10
|
+
watch: false,
|
|
11
|
+
globalSetup: isBrowser ? ["./test/fixtures/server.ts"] : undefined,
|
|
12
|
+
testTimeout: 5000,
|
|
13
|
+
globals: true,
|
|
14
|
+
passWithNoTests: true,
|
|
15
|
+
reporters: ["verbose"],
|
|
16
|
+
coverage: { reporter: ["lcov", "html", "text"] },
|
|
17
|
+
},
|
|
18
|
+
});
|