@apidevtools/json-schema-ref-parser 9.1.0 → 10.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.
- package/README.md +11 -7
- package/cjs/bundle.js +304 -0
- package/cjs/dereference.js +258 -0
- package/cjs/index.js +603 -0
- package/cjs/normalize-args.js +64 -0
- package/cjs/options.js +125 -0
- package/cjs/package.json +3 -0
- package/cjs/parse.js +338 -0
- package/cjs/parsers/binary.js +54 -0
- package/cjs/parsers/json.js +199 -0
- package/cjs/parsers/text.js +61 -0
- package/cjs/parsers/yaml.js +239 -0
- package/cjs/pointer.js +290 -0
- package/cjs/ref.js +333 -0
- package/cjs/refs.js +214 -0
- package/cjs/resolve-external.js +333 -0
- package/cjs/resolvers/file.js +106 -0
- package/cjs/resolvers/http.js +184 -0
- package/cjs/util/errors.js +401 -0
- package/cjs/util/plugins.js +159 -0
- package/cjs/util/projectDir.cjs +6 -0
- package/cjs/util/url.js +228 -0
- package/lib/bundle.js +5 -7
- package/lib/dereference.js +8 -7
- package/lib/index.d.ts +10 -1
- package/lib/index.js +28 -21
- package/lib/normalize-args.js +2 -4
- package/lib/options.js +7 -9
- package/lib/parse.js +5 -7
- package/lib/parsers/binary.js +1 -3
- package/lib/parsers/json.js +2 -4
- package/lib/parsers/text.js +2 -4
- package/lib/parsers/yaml.js +4 -6
- package/lib/pointer.js +4 -6
- package/lib/ref.js +4 -6
- package/lib/refs.js +8 -9
- package/lib/resolve-external.js +6 -8
- package/lib/resolvers/file.js +5 -6
- package/lib/resolvers/http.js +47 -72
- package/lib/util/errors.js +14 -16
- package/lib/util/plugins.js +8 -10
- package/lib/util/projectDir.cjs +6 -0
- package/lib/util/url.js +53 -33
- package/package.json +34 -35
package/lib/refs.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import { ono } from "@jsdevtools/ono";
|
|
2
|
+
import $Ref from "./ref.js";
|
|
3
|
+
import * as url from "./util/url.js";
|
|
2
4
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const url = require("./util/url");
|
|
6
|
-
|
|
7
|
-
module.exports = $Refs;
|
|
5
|
+
const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
|
|
6
|
+
const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath;
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* This class is a map of JSON references and their resolved values.
|
|
11
10
|
*/
|
|
12
|
-
function $Refs () {
|
|
11
|
+
export default function $Refs () {
|
|
13
12
|
/**
|
|
14
13
|
* Indicates whether the schema contains any circular references.
|
|
15
14
|
*
|
|
@@ -44,7 +43,7 @@ function $Refs () {
|
|
|
44
43
|
$Refs.prototype.paths = function (types) { // eslint-disable-line no-unused-vars
|
|
45
44
|
let paths = getPaths(this._$refs, arguments);
|
|
46
45
|
return paths.map((path) => {
|
|
47
|
-
return path.decoded;
|
|
46
|
+
return getPathFromOs(path.decoded);
|
|
48
47
|
});
|
|
49
48
|
};
|
|
50
49
|
|
|
@@ -58,7 +57,7 @@ $Refs.prototype.values = function (types) { // eslint-disable-line no-unused-v
|
|
|
58
57
|
let $refs = this._$refs;
|
|
59
58
|
let paths = getPaths($refs, arguments);
|
|
60
59
|
return paths.reduce((obj, path) => {
|
|
61
|
-
obj[path.decoded] = $refs[path.encoded].value;
|
|
60
|
+
obj[getPathFromOs(path.decoded)] = $refs[path.encoded].value;
|
|
62
61
|
return obj;
|
|
63
62
|
}, {});
|
|
64
63
|
};
|
package/lib/resolve-external.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import $Ref from "./ref.js";
|
|
2
|
+
import Pointer from "./pointer.js";
|
|
3
|
+
import parse from "./parse.js";
|
|
4
|
+
import * as url from "./util/url.js";
|
|
5
|
+
import { isHandledError } from "./util/errors.js";
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
const Pointer = require("./pointer");
|
|
5
|
-
const parse = require("./parse");
|
|
6
|
-
const url = require("./util/url");
|
|
7
|
-
const { isHandledError } = require("./util/errors");
|
|
8
|
-
|
|
9
|
-
module.exports = resolveExternal;
|
|
7
|
+
export default resolveExternal;
|
|
10
8
|
|
|
11
9
|
/**
|
|
12
10
|
* Crawls the JSON schema, finds all external JSON references, and resolves their values.
|
package/lib/resolvers/file.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { ResolverError } = require("../util/errors");
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { ono } from "@jsdevtools/ono";
|
|
3
|
+
import * as url from "../util/url.js";
|
|
4
|
+
import { ResolverError } from "../util/errors.js";
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
export default {
|
|
8
7
|
/**
|
|
9
8
|
* The order that this resolver will run, in relation to other resolvers.
|
|
10
9
|
*
|
package/lib/resolvers/http.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { ono } from "@jsdevtools/ono";
|
|
2
|
+
import * as url from "../util/url.js";
|
|
3
|
+
import { ResolverError } from "../util/errors.js";
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
const https = require("https");
|
|
5
|
-
const { ono } = require("@jsdevtools/ono");
|
|
6
|
-
const url = require("../util/url");
|
|
7
|
-
const { ResolverError } = require("../util/errors");
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
5
|
+
export default {
|
|
10
6
|
/**
|
|
11
7
|
* The order that this resolver will run, in relation to other resolvers.
|
|
12
8
|
*
|
|
@@ -75,7 +71,7 @@ module.exports = {
|
|
|
75
71
|
read (file) {
|
|
76
72
|
let u = url.parse(file.url);
|
|
77
73
|
|
|
78
|
-
if (
|
|
74
|
+
if (typeof window !== "undefined" && !u.protocol) {
|
|
79
75
|
// Use the protocol of the current page
|
|
80
76
|
u.protocol = url.parse(location.href).protocol;
|
|
81
77
|
}
|
|
@@ -95,38 +91,36 @@ module.exports = {
|
|
|
95
91
|
* The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
|
|
96
92
|
*/
|
|
97
93
|
function download (u, httpOptions, redirects) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
94
|
+
u = url.parse(u);
|
|
95
|
+
redirects = redirects || [];
|
|
96
|
+
redirects.push(u.href);
|
|
97
|
+
|
|
98
|
+
return get(u, httpOptions)
|
|
99
|
+
.then((res) => {
|
|
100
|
+
if (res.status >= 400) {
|
|
101
|
+
throw ono({ status: res.statusCode }, `HTTP ERROR ${res.status}`);
|
|
102
|
+
}
|
|
103
|
+
else if (res.status >= 300) {
|
|
104
|
+
if (redirects.length > httpOptions.redirects) {
|
|
105
|
+
throw new ResolverError(ono({ status: res.status },
|
|
106
|
+
`Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`));
|
|
107
107
|
}
|
|
108
|
-
else if (res.
|
|
109
|
-
|
|
110
|
-
reject(new ResolverError(ono({ status: res.statusCode },
|
|
111
|
-
`Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`)));
|
|
112
|
-
}
|
|
113
|
-
else if (!res.headers.location) {
|
|
114
|
-
throw ono({ status: res.statusCode }, `HTTP ${res.statusCode} redirect with no location header`);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
// console.log('HTTP %d redirect %s -> %s', res.statusCode, u.href, res.headers.location);
|
|
118
|
-
let redirectTo = url.resolve(u, res.headers.location);
|
|
119
|
-
download(redirectTo, httpOptions, redirects).then(resolve, reject);
|
|
120
|
-
}
|
|
108
|
+
else if (!res.headers.location) {
|
|
109
|
+
throw ono({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
|
|
121
110
|
}
|
|
122
111
|
else {
|
|
123
|
-
|
|
112
|
+
// console.log('HTTP %d redirect %s -> %s', res.status, u.href, res.headers.location);
|
|
113
|
+
let redirectTo = url.resolve(u, res.headers.location);
|
|
114
|
+
return download(redirectTo, httpOptions, redirects);
|
|
124
115
|
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
return res.body ? res.arrayBuffer().then(buf => Buffer.from(buf)) : Buffer.alloc(0);
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
.catch((err) => {
|
|
122
|
+
throw new ResolverError(ono(err, `Error downloading ${u.href}`), u.href);
|
|
123
|
+
});
|
|
130
124
|
}
|
|
131
125
|
|
|
132
126
|
/**
|
|
@@ -139,42 +133,23 @@ function download (u, httpOptions, redirects) {
|
|
|
139
133
|
* The promise resolves with the HTTP Response object.
|
|
140
134
|
*/
|
|
141
135
|
function get (u, httpOptions) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
port: u.port,
|
|
149
|
-
path: u.path,
|
|
150
|
-
auth: u.auth,
|
|
151
|
-
protocol: u.protocol,
|
|
152
|
-
headers: httpOptions.headers || {},
|
|
153
|
-
withCredentials: httpOptions.withCredentials
|
|
154
|
-
});
|
|
136
|
+
let controller;
|
|
137
|
+
let timeoutId;
|
|
138
|
+
if (httpOptions.timeout) {
|
|
139
|
+
controller = new AbortController();
|
|
140
|
+
timeoutId = setTimeout(() => controller.abort(), httpOptions.timeout);
|
|
141
|
+
}
|
|
155
142
|
|
|
156
|
-
|
|
157
|
-
|
|
143
|
+
return fetch(u, {
|
|
144
|
+
method: "GET",
|
|
145
|
+
headers: httpOptions.headers || {},
|
|
146
|
+
credentials: httpOptions.withCredentials ? "include" : "same-origin",
|
|
147
|
+
signal: controller ? controller.signal : null,
|
|
148
|
+
}).then(response => {
|
|
149
|
+
if (timeoutId) {
|
|
150
|
+
clearTimeout(timeoutId);
|
|
158
151
|
}
|
|
159
152
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
req.on("error", reject);
|
|
165
|
-
|
|
166
|
-
req.once("response", (res) => {
|
|
167
|
-
res.body = Buffer.alloc(0);
|
|
168
|
-
|
|
169
|
-
res.on("data", (data) => {
|
|
170
|
-
res.body = Buffer.concat([res.body, Buffer.from(data)]);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
res.on("error", reject);
|
|
174
|
-
|
|
175
|
-
res.on("end", () => {
|
|
176
|
-
resolve(res);
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
}));
|
|
153
|
+
return response;
|
|
154
|
+
});
|
|
180
155
|
}
|
package/lib/util/errors.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { Ono } from "@jsdevtools/ono";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { stripHash, toFileSystemPath } from "./url.js";
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const JSONParserError = exports.JSONParserError = class JSONParserError extends Error {
|
|
5
|
+
export const JSONParserError = class JSONParserError extends Error {
|
|
8
6
|
constructor (message, source) {
|
|
9
7
|
super();
|
|
10
8
|
|
|
@@ -23,7 +21,7 @@ const JSONParserError = exports.JSONParserError = class JSONParserError extends
|
|
|
23
21
|
|
|
24
22
|
setErrorName(JSONParserError);
|
|
25
23
|
|
|
26
|
-
const JSONParserErrorGroup =
|
|
24
|
+
export const JSONParserErrorGroup = class JSONParserErrorGroup extends Error {
|
|
27
25
|
constructor (parser) {
|
|
28
26
|
super();
|
|
29
27
|
|
|
@@ -52,7 +50,7 @@ const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErro
|
|
|
52
50
|
|
|
53
51
|
setErrorName(JSONParserErrorGroup);
|
|
54
52
|
|
|
55
|
-
const ParserError =
|
|
53
|
+
export const ParserError = class ParserError extends JSONParserError {
|
|
56
54
|
constructor (message, source) {
|
|
57
55
|
super(`Error parsing ${source}: ${message}`, source);
|
|
58
56
|
|
|
@@ -62,7 +60,7 @@ const ParserError = exports.ParserError = class ParserError extends JSONParserEr
|
|
|
62
60
|
|
|
63
61
|
setErrorName(ParserError);
|
|
64
62
|
|
|
65
|
-
const UnmatchedParserError =
|
|
63
|
+
export const UnmatchedParserError = class UnmatchedParserError extends JSONParserError {
|
|
66
64
|
constructor (source) {
|
|
67
65
|
super(`Could not find parser for "${source}"`, source);
|
|
68
66
|
|
|
@@ -72,7 +70,7 @@ const UnmatchedParserError = exports.UnmatchedParserError = class UnmatchedParse
|
|
|
72
70
|
|
|
73
71
|
setErrorName(UnmatchedParserError);
|
|
74
72
|
|
|
75
|
-
const ResolverError =
|
|
73
|
+
export const ResolverError = class ResolverError extends JSONParserError {
|
|
76
74
|
constructor (ex, source) {
|
|
77
75
|
super(ex.message || `Error reading file "${source}"`, source);
|
|
78
76
|
|
|
@@ -86,7 +84,7 @@ const ResolverError = exports.ResolverError = class ResolverError extends JSONPa
|
|
|
86
84
|
|
|
87
85
|
setErrorName(ResolverError);
|
|
88
86
|
|
|
89
|
-
const UnmatchedResolverError =
|
|
87
|
+
export const UnmatchedResolverError = class UnmatchedResolverError extends JSONParserError {
|
|
90
88
|
constructor (source) {
|
|
91
89
|
super(`Could not find resolver for "${source}"`, source);
|
|
92
90
|
|
|
@@ -96,7 +94,7 @@ const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedR
|
|
|
96
94
|
|
|
97
95
|
setErrorName(UnmatchedResolverError);
|
|
98
96
|
|
|
99
|
-
const MissingPointerError =
|
|
97
|
+
export const MissingPointerError = class MissingPointerError extends JSONParserError {
|
|
100
98
|
constructor (token, path) {
|
|
101
99
|
super(`Token "${token}" does not exist.`, stripHash(path));
|
|
102
100
|
|
|
@@ -106,7 +104,7 @@ const MissingPointerError = exports.MissingPointerError = class MissingPointerEr
|
|
|
106
104
|
|
|
107
105
|
setErrorName(MissingPointerError);
|
|
108
106
|
|
|
109
|
-
const InvalidPointerError =
|
|
107
|
+
export const InvalidPointerError = class InvalidPointerError extends JSONParserError {
|
|
110
108
|
constructor (pointer, path) {
|
|
111
109
|
super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path));
|
|
112
110
|
|
|
@@ -123,14 +121,14 @@ function setErrorName (err) {
|
|
|
123
121
|
});
|
|
124
122
|
}
|
|
125
123
|
|
|
126
|
-
|
|
124
|
+
export function isHandledError (err) {
|
|
127
125
|
return err instanceof JSONParserError || err instanceof JSONParserErrorGroup;
|
|
128
|
-
}
|
|
126
|
+
}
|
|
129
127
|
|
|
130
|
-
|
|
128
|
+
export function normalizeError (err) {
|
|
131
129
|
if (err.path === null) {
|
|
132
130
|
err.path = [];
|
|
133
131
|
}
|
|
134
132
|
|
|
135
133
|
return err;
|
|
136
|
-
}
|
|
134
|
+
}
|
package/lib/util/plugins.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Returns the given plugins as an array, rather than an object map.
|
|
5
3
|
* All other methods in this module expect an array of plugins rather than an object map.
|
|
@@ -7,7 +5,7 @@
|
|
|
7
5
|
* @param {object} plugins - A map of plugin objects
|
|
8
6
|
* @return {object[]}
|
|
9
7
|
*/
|
|
10
|
-
|
|
8
|
+
export function all (plugins) {
|
|
11
9
|
return Object.keys(plugins)
|
|
12
10
|
.filter((key) => {
|
|
13
11
|
return typeof plugins[key] === "object";
|
|
@@ -16,7 +14,7 @@ exports.all = function (plugins) {
|
|
|
16
14
|
plugins[key].name = key;
|
|
17
15
|
return plugins[key];
|
|
18
16
|
});
|
|
19
|
-
}
|
|
17
|
+
}
|
|
20
18
|
|
|
21
19
|
/**
|
|
22
20
|
* Filters the given plugins, returning only the ones return `true` for the given method.
|
|
@@ -26,12 +24,12 @@ exports.all = function (plugins) {
|
|
|
26
24
|
* @param {object} file - A file info object, which will be passed to each method
|
|
27
25
|
* @return {object[]}
|
|
28
26
|
*/
|
|
29
|
-
|
|
27
|
+
export function filter (plugins, method, file) {
|
|
30
28
|
return plugins
|
|
31
29
|
.filter((plugin) => {
|
|
32
30
|
return !!getResult(plugin, method, file);
|
|
33
31
|
});
|
|
34
|
-
}
|
|
32
|
+
}
|
|
35
33
|
|
|
36
34
|
/**
|
|
37
35
|
* Sorts the given plugins, in place, by their `order` property.
|
|
@@ -39,13 +37,13 @@ exports.filter = function (plugins, method, file) {
|
|
|
39
37
|
* @param {object[]} plugins - An array of plugin objects
|
|
40
38
|
* @returns {object[]}
|
|
41
39
|
*/
|
|
42
|
-
|
|
40
|
+
export function sort (plugins) {
|
|
43
41
|
for (let plugin of plugins) {
|
|
44
42
|
plugin.order = plugin.order || Number.MAX_SAFE_INTEGER;
|
|
45
43
|
}
|
|
46
44
|
|
|
47
45
|
return plugins.sort((a, b) => { return a.order - b.order; });
|
|
48
|
-
}
|
|
46
|
+
}
|
|
49
47
|
|
|
50
48
|
/**
|
|
51
49
|
* Runs the specified method of the given plugins, in order, until one of them returns a successful result.
|
|
@@ -60,7 +58,7 @@ exports.sort = function (plugins) {
|
|
|
60
58
|
* @param {object} file - A file info object, which will be passed to each method
|
|
61
59
|
* @returns {Promise}
|
|
62
60
|
*/
|
|
63
|
-
|
|
61
|
+
export function run (plugins, method, file, $refs) {
|
|
64
62
|
let plugin, lastError, index = 0;
|
|
65
63
|
|
|
66
64
|
return new Promise(((resolve, reject) => {
|
|
@@ -119,7 +117,7 @@ exports.run = function (plugins, method, file, $refs) {
|
|
|
119
117
|
runNextPlugin();
|
|
120
118
|
}
|
|
121
119
|
}));
|
|
122
|
-
}
|
|
120
|
+
}
|
|
123
121
|
|
|
124
122
|
/**
|
|
125
123
|
* Returns the value of the given property.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
const nodePath = require("path");
|
|
2
|
+
|
|
3
|
+
// Webpack 4 (used by browser tests) can't transpile import.meta.url
|
|
4
|
+
// So export the project directory using __dirname from a .cjs module
|
|
5
|
+
const projectDir = nodePath.resolve(__dirname, "..", "..");
|
|
6
|
+
module.exports = projectDir
|
package/lib/util/url.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import projectDir from "./projectDir.cjs";
|
|
2
2
|
|
|
3
|
-
let isWindows = /^win/.test(process.platform),
|
|
3
|
+
let isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined),
|
|
4
4
|
forwardSlashPattern = /\//g,
|
|
5
5
|
protocolPattern = /^(\w{2,}):\/\//i,
|
|
6
|
-
url = module.exports,
|
|
7
6
|
jsonPointerSlash = /~1/g,
|
|
8
7
|
jsonPointerTilde = /~0/g;
|
|
9
8
|
|
|
@@ -22,16 +21,30 @@ let urlDecodePatterns = [
|
|
|
22
21
|
/\%40/g, "@"
|
|
23
22
|
];
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
export const parse = (u) => new URL(u);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 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
|
+
*
|
|
29
|
+
* @return {string}
|
|
30
|
+
*/
|
|
31
|
+
export function resolve (from, to) {
|
|
32
|
+
let resolvedUrl = new URL(to, new URL(from, "resolve://"));
|
|
33
|
+
if (resolvedUrl.protocol === "resolve:") {
|
|
34
|
+
// `from` is a relative URL.
|
|
35
|
+
let { pathname, search, hash } = resolvedUrl;
|
|
36
|
+
return pathname + search + hash;
|
|
37
|
+
}
|
|
38
|
+
return resolvedUrl.toString();
|
|
39
|
+
}
|
|
27
40
|
|
|
28
41
|
/**
|
|
29
42
|
* Returns the current working directory (in Node) or the current page URL (in browsers).
|
|
30
43
|
*
|
|
31
44
|
* @returns {string}
|
|
32
45
|
*/
|
|
33
|
-
|
|
34
|
-
if (
|
|
46
|
+
export function cwd () {
|
|
47
|
+
if (typeof window !== "undefined") {
|
|
35
48
|
return location.href;
|
|
36
49
|
}
|
|
37
50
|
|
|
@@ -44,7 +57,7 @@ exports.cwd = function cwd () {
|
|
|
44
57
|
else {
|
|
45
58
|
return path + "/";
|
|
46
59
|
}
|
|
47
|
-
}
|
|
60
|
+
}
|
|
48
61
|
|
|
49
62
|
/**
|
|
50
63
|
* Returns the protocol of the given URL, or `undefined` if it has no protocol.
|
|
@@ -52,12 +65,12 @@ exports.cwd = function cwd () {
|
|
|
52
65
|
* @param {string} path
|
|
53
66
|
* @returns {?string}
|
|
54
67
|
*/
|
|
55
|
-
|
|
68
|
+
export function getProtocol (path) {
|
|
56
69
|
let match = protocolPattern.exec(path);
|
|
57
70
|
if (match) {
|
|
58
71
|
return match[1].toLowerCase();
|
|
59
72
|
}
|
|
60
|
-
}
|
|
73
|
+
}
|
|
61
74
|
|
|
62
75
|
/**
|
|
63
76
|
* Returns the lowercased file extension of the given URL,
|
|
@@ -66,13 +79,13 @@ exports.getProtocol = function getProtocol (path) {
|
|
|
66
79
|
* @param {string} path
|
|
67
80
|
* @returns {string}
|
|
68
81
|
*/
|
|
69
|
-
|
|
82
|
+
export function getExtension (path) {
|
|
70
83
|
let lastDot = path.lastIndexOf(".");
|
|
71
84
|
if (lastDot >= 0) {
|
|
72
|
-
return
|
|
85
|
+
return stripQuery(path.substr(lastDot).toLowerCase());
|
|
73
86
|
}
|
|
74
87
|
return "";
|
|
75
|
-
}
|
|
88
|
+
}
|
|
76
89
|
|
|
77
90
|
/**
|
|
78
91
|
* Removes the query, if any, from the given path.
|
|
@@ -80,13 +93,13 @@ exports.getExtension = function getExtension (path) {
|
|
|
80
93
|
* @param {string} path
|
|
81
94
|
* @returns {string}
|
|
82
95
|
*/
|
|
83
|
-
|
|
96
|
+
export function stripQuery (path) {
|
|
84
97
|
let queryIndex = path.indexOf("?");
|
|
85
98
|
if (queryIndex >= 0) {
|
|
86
99
|
path = path.substr(0, queryIndex);
|
|
87
100
|
}
|
|
88
101
|
return path;
|
|
89
|
-
}
|
|
102
|
+
}
|
|
90
103
|
|
|
91
104
|
/**
|
|
92
105
|
* Returns the hash (URL fragment), of the given path.
|
|
@@ -95,13 +108,13 @@ exports.stripQuery = function stripQuery (path) {
|
|
|
95
108
|
* @param {string} path
|
|
96
109
|
* @returns {string}
|
|
97
110
|
*/
|
|
98
|
-
|
|
111
|
+
export function getHash (path) {
|
|
99
112
|
let hashIndex = path.indexOf("#");
|
|
100
113
|
if (hashIndex >= 0) {
|
|
101
114
|
return path.substr(hashIndex);
|
|
102
115
|
}
|
|
103
116
|
return "#";
|
|
104
|
-
}
|
|
117
|
+
}
|
|
105
118
|
|
|
106
119
|
/**
|
|
107
120
|
* Removes the hash (URL fragment), if any, from the given path.
|
|
@@ -109,13 +122,13 @@ exports.getHash = function getHash (path) {
|
|
|
109
122
|
* @param {string} path
|
|
110
123
|
* @returns {string}
|
|
111
124
|
*/
|
|
112
|
-
|
|
125
|
+
export function stripHash (path) {
|
|
113
126
|
let hashIndex = path.indexOf("#");
|
|
114
127
|
if (hashIndex >= 0) {
|
|
115
128
|
path = path.substr(0, hashIndex);
|
|
116
129
|
}
|
|
117
130
|
return path;
|
|
118
|
-
}
|
|
131
|
+
}
|
|
119
132
|
|
|
120
133
|
/**
|
|
121
134
|
* Determines whether the given path is an HTTP(S) URL.
|
|
@@ -123,20 +136,20 @@ exports.stripHash = function stripHash (path) {
|
|
|
123
136
|
* @param {string} path
|
|
124
137
|
* @returns {boolean}
|
|
125
138
|
*/
|
|
126
|
-
|
|
127
|
-
let protocol =
|
|
139
|
+
export function isHttp (path) {
|
|
140
|
+
let protocol = getProtocol(path);
|
|
128
141
|
if (protocol === "http" || protocol === "https") {
|
|
129
142
|
return true;
|
|
130
143
|
}
|
|
131
144
|
else if (protocol === undefined) {
|
|
132
145
|
// There is no protocol. If we're running in a browser, then assume it's HTTP.
|
|
133
|
-
return
|
|
146
|
+
return typeof window !== "undefined";
|
|
134
147
|
}
|
|
135
148
|
else {
|
|
136
149
|
// It's some other protocol, such as "ftp://", "mongodb://", etc.
|
|
137
150
|
return false;
|
|
138
151
|
}
|
|
139
|
-
}
|
|
152
|
+
}
|
|
140
153
|
|
|
141
154
|
/**
|
|
142
155
|
* Determines whether the given path is a filesystem path.
|
|
@@ -145,16 +158,16 @@ exports.isHttp = function isHttp (path) {
|
|
|
145
158
|
* @param {string} path
|
|
146
159
|
* @returns {boolean}
|
|
147
160
|
*/
|
|
148
|
-
|
|
161
|
+
export function isFileSystemPath (path) {
|
|
149
162
|
if (process.browser) {
|
|
150
163
|
// We're running in a browser, so assume that all paths are URLs.
|
|
151
164
|
// This way, even relative paths will be treated as URLs rather than as filesystem paths
|
|
152
165
|
return false;
|
|
153
166
|
}
|
|
154
167
|
|
|
155
|
-
let protocol =
|
|
168
|
+
let protocol = getProtocol(path);
|
|
156
169
|
return protocol === undefined || protocol === "file";
|
|
157
|
-
}
|
|
170
|
+
}
|
|
158
171
|
|
|
159
172
|
/**
|
|
160
173
|
* Converts a filesystem path to a properly-encoded URL.
|
|
@@ -172,11 +185,18 @@ exports.isFileSystemPath = function isFileSystemPath (path) {
|
|
|
172
185
|
* @param {string} path
|
|
173
186
|
* @returns {string}
|
|
174
187
|
*/
|
|
175
|
-
|
|
188
|
+
export function fromFileSystemPath (path) {
|
|
176
189
|
// Step 1: On Windows, replace backslashes with forward slashes,
|
|
177
190
|
// rather than encoding them as "%5C"
|
|
178
191
|
if (isWindows) {
|
|
179
|
-
|
|
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, "/");
|
|
199
|
+
}
|
|
180
200
|
}
|
|
181
201
|
|
|
182
202
|
// Step 2: `encodeURI` will take care of MOST characters
|
|
@@ -190,7 +210,7 @@ exports.fromFileSystemPath = function fromFileSystemPath (path) {
|
|
|
190
210
|
}
|
|
191
211
|
|
|
192
212
|
return path;
|
|
193
|
-
}
|
|
213
|
+
}
|
|
194
214
|
|
|
195
215
|
/**
|
|
196
216
|
* Converts a URL to a local filesystem path.
|
|
@@ -199,7 +219,7 @@ exports.fromFileSystemPath = function fromFileSystemPath (path) {
|
|
|
199
219
|
* @param {boolean} [keepFileProtocol] - If true, then "file://" will NOT be stripped
|
|
200
220
|
* @returns {string}
|
|
201
221
|
*/
|
|
202
|
-
|
|
222
|
+
export function toFileSystemPath (path, keepFileProtocol) {
|
|
203
223
|
// Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc.
|
|
204
224
|
path = decodeURI(path);
|
|
205
225
|
|
|
@@ -247,7 +267,7 @@ exports.toFileSystemPath = function toFileSystemPath (path, keepFileProtocol) {
|
|
|
247
267
|
}
|
|
248
268
|
|
|
249
269
|
return path;
|
|
250
|
-
}
|
|
270
|
+
}
|
|
251
271
|
|
|
252
272
|
/**
|
|
253
273
|
* Converts a $ref pointer to a valid JSON Path.
|
|
@@ -255,7 +275,7 @@ exports.toFileSystemPath = function toFileSystemPath (path, keepFileProtocol) {
|
|
|
255
275
|
* @param {string} pointer
|
|
256
276
|
* @returns {Array<number | string>}
|
|
257
277
|
*/
|
|
258
|
-
|
|
278
|
+
export function safePointerToPath (pointer) {
|
|
259
279
|
if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") {
|
|
260
280
|
return [];
|
|
261
281
|
}
|
|
@@ -268,4 +288,4 @@ exports.safePointerToPath = function safePointerToPath (pointer) {
|
|
|
268
288
|
.replace(jsonPointerSlash, "/")
|
|
269
289
|
.replace(jsonPointerTilde, "~");
|
|
270
290
|
});
|
|
271
|
-
}
|
|
291
|
+
}
|