@apidevtools/json-schema-ref-parser 10.1.0 → 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.
package/dist/lib/refs.js CHANGED
@@ -29,8 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const ono_1 = require("@jsdevtools/ono");
30
30
  const ref_js_1 = __importDefault(require("./ref.js"));
31
31
  const url = __importStar(require("./util/url.js"));
32
- const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : "");
33
- const getPathFromOs = (filePath) => (isWindows ? filePath.replace(/\\/g, "/") : filePath);
32
+ const convert_path_to_posix_1 = __importDefault(require("./util/convert-path-to-posix"));
34
33
  /**
35
34
  * When you call the resolve method, the value that gets passed to the callback function (or Promise) is a $Refs object. This same object is accessible via the parser.$refs property of $RefParser objects.
36
35
  *
@@ -49,7 +48,7 @@ class $Refs {
49
48
  paths(...types) {
50
49
  const paths = getPaths(this._$refs, types);
51
50
  return paths.map((path) => {
52
- return getPathFromOs(path.decoded);
51
+ return (0, convert_path_to_posix_1.default)(path.decoded);
53
52
  });
54
53
  }
55
54
  /**
@@ -63,7 +62,7 @@ class $Refs {
63
62
  const $refs = this._$refs;
64
63
  const paths = getPaths($refs, types);
65
64
  return paths.reduce((obj, path) => {
66
- obj[getPathFromOs(path.decoded)] = $refs[path.encoded].value;
65
+ obj[(0, convert_path_to_posix_1.default)(path.decoded)] = $refs[path.encoded].value;
67
66
  return obj;
68
67
  }, {});
69
68
  }
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
35
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
27
  };
@@ -70,6 +61,7 @@ function resolveExternal(parser, options) {
70
61
  *
71
62
  * @param obj - The value to crawl. If it's not an object or array, it will be ignored.
72
63
  * @param path - The full path of `obj`, possibly with a JSON Pointer in the hash
64
+ * @param {boolean} external - Whether `obj` was found in an external document.
73
65
  * @param $refs
74
66
  * @param options
75
67
  * @param seen - Internal.
@@ -80,7 +72,7 @@ function resolveExternal(parser, options) {
80
72
  * If any of the JSON references point to files that contain additional JSON references,
81
73
  * then the corresponding promise will internally reference an array of promises.
82
74
  */
83
- function crawl(obj, path, $refs, options, seen) {
75
+ function crawl(obj, path, $refs, options, seen, external) {
84
76
  seen || (seen = new Set());
85
77
  let promises = [];
86
78
  if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
@@ -88,17 +80,11 @@ function crawl(obj, path, $refs, options, seen) {
88
80
  if (ref_js_1.default.isExternal$Ref(obj)) {
89
81
  promises.push(resolve$Ref(obj, path, $refs, options));
90
82
  }
91
- else {
92
- for (const key of Object.keys(obj)) {
93
- const keyPath = pointer_js_1.default.join(path, key);
94
- const value = obj[key];
95
- if (ref_js_1.default.isExternal$Ref(value)) {
96
- promises.push(resolve$Ref(value, keyPath, $refs, options));
97
- }
98
- else {
99
- promises = promises.concat(crawl(value, keyPath, $refs, options, seen));
100
- }
101
- }
83
+ const keys = Object.keys(obj);
84
+ for (const key of keys) {
85
+ const keyPath = pointer_js_1.default.join(path, key);
86
+ const value = obj[key];
87
+ promises = promises.concat(crawl(value, keyPath, $refs, options, seen, external));
102
88
  }
103
89
  }
104
90
  return promises;
@@ -115,34 +101,33 @@ function crawl(obj, path, $refs, options, seen) {
115
101
  * The promise resolves once all JSON references in the object have been resolved,
116
102
  * including nested references that are contained in externally-referenced files.
117
103
  */
118
- function resolve$Ref($ref, path, $refs, options) {
119
- return __awaiter(this, void 0, void 0, function* () {
120
- // console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path);
121
- const resolvedPath = url.resolve(path, $ref.$ref);
122
- const withoutHash = url.stripHash(resolvedPath);
123
- // Do we already have this $ref?
124
- $ref = $refs._$refs[withoutHash];
125
- if ($ref) {
126
- // We've already parsed this $ref, so use the existing value
127
- return Promise.resolve($ref.value);
128
- }
129
- // Parse the $referenced file/url
130
- try {
131
- const result = yield (0, parse_js_1.default)(resolvedPath, $refs, options);
132
- // Crawl the parsed value
133
- // console.log('Resolving $ref pointers in %s', withoutHash);
134
- const promises = crawl(result, withoutHash + "#", $refs, options);
135
- return Promise.all(promises);
104
+ async function resolve$Ref($ref, path, $refs, options) {
105
+ // console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path);
106
+ const resolvedPath = url.resolve(path, $ref.$ref);
107
+ const withoutHash = url.stripHash(resolvedPath);
108
+ // $ref.$ref = url.relative($refs._root$Ref.path, resolvedPath);
109
+ // Do we already have this $ref?
110
+ $ref = $refs._$refs[withoutHash];
111
+ if ($ref) {
112
+ // We've already parsed this $ref, so use the existing value
113
+ return Promise.resolve($ref.value);
114
+ }
115
+ // Parse the $referenced file/url
116
+ try {
117
+ const result = await (0, parse_js_1.default)(resolvedPath, $refs, options);
118
+ // Crawl the parsed value
119
+ // console.log('Resolving $ref pointers in %s', withoutHash);
120
+ const promises = crawl(result, withoutHash + "#", $refs, options, new Set(), true);
121
+ return Promise.all(promises);
122
+ }
123
+ catch (err) {
124
+ if (!options?.continueOnError || !(0, errors_js_1.isHandledError)(err)) {
125
+ throw err;
136
126
  }
137
- catch (err) {
138
- if (!(options === null || options === void 0 ? void 0 : options.continueOnError) || !(0, errors_js_1.isHandledError)(err)) {
139
- throw err;
140
- }
141
- if ($refs._$refs[withoutHash]) {
142
- err.source = decodeURI(url.stripHash(path));
143
- err.path = url.safePointerToPath(url.getHash(path));
144
- }
145
- return [];
127
+ if ($refs._$refs[withoutHash]) {
128
+ err.source = decodeURI(url.stripHash(path));
129
+ err.path = url.safePointerToPath(url.getHash(path));
146
130
  }
147
- });
131
+ return [];
132
+ }
148
133
  }
@@ -22,20 +22,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
25
  Object.defineProperty(exports, "__esModule", { value: true });
38
- const promises_1 = __importDefault(require("fs/promises"));
26
+ const fs_1 = require("fs");
39
27
  const ono_1 = require("@jsdevtools/ono");
40
28
  const url = __importStar(require("../util/url.js"));
41
29
  const errors_js_1 = require("../util/errors.js");
@@ -55,22 +43,20 @@ exports.default = {
55
43
  /**
56
44
  * Reads the given file and returns its raw contents as a Buffer.
57
45
  */
58
- read(file) {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- let path;
61
- try {
62
- path = url.toFileSystemPath(file.url);
63
- }
64
- catch (err) {
65
- throw new errors_js_1.ResolverError(ono_1.ono.uri(err, `Malformed URI: ${file.url}`), file.url);
66
- }
67
- try {
68
- const data = yield promises_1.default.readFile(path);
69
- return data;
70
- }
71
- catch (err) {
72
- throw new errors_js_1.ResolverError((0, ono_1.ono)(err, `Error opening file "${path}"`), path);
73
- }
74
- });
46
+ async read(file) {
47
+ let path;
48
+ try {
49
+ path = url.toFileSystemPath(file.url);
50
+ }
51
+ catch (err) {
52
+ throw new errors_js_1.ResolverError(ono_1.ono.uri(err, `Malformed URI: ${file.url}`), file.url);
53
+ }
54
+ try {
55
+ const data = await fs_1.promises.readFile(path);
56
+ return data;
57
+ }
58
+ catch (err) {
59
+ throw new errors_js_1.ResolverError((0, ono_1.ono)(err, `Error opening file "${path}"`), path);
60
+ }
75
61
  },
76
62
  };
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
25
  Object.defineProperty(exports, "__esModule", { value: true });
35
26
  const ono_1 = require("@jsdevtools/ono");
36
27
  const url = __importStar(require("../util/url.js"));
@@ -89,71 +80,67 @@ exports.default = {
89
80
  * @returns
90
81
  * The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
91
82
  */
92
- function download(u, httpOptions, _redirects) {
93
- return __awaiter(this, void 0, void 0, function* () {
94
- u = url.parse(u);
95
- const redirects = _redirects || [];
96
- redirects.push(u.href);
97
- try {
98
- const res = yield get(u, httpOptions);
99
- if (res.status >= 400) {
100
- throw (0, ono_1.ono)({ status: res.status }, `HTTP ERROR ${res.status}`);
83
+ async function download(u, httpOptions, _redirects) {
84
+ u = url.parse(u);
85
+ const redirects = _redirects || [];
86
+ redirects.push(u.href);
87
+ try {
88
+ const res = await get(u, httpOptions);
89
+ if (res.status >= 400) {
90
+ throw (0, ono_1.ono)({ status: res.status }, `HTTP ERROR ${res.status}`);
91
+ }
92
+ else if (res.status >= 300) {
93
+ if (!Number.isNaN(httpOptions.redirects) && redirects.length > httpOptions.redirects) {
94
+ throw new errors_js_1.ResolverError((0, ono_1.ono)({ status: res.status }, `Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`));
101
95
  }
102
- else if (res.status >= 300) {
103
- if (!Number.isNaN(httpOptions.redirects) && redirects.length > httpOptions.redirects) {
104
- throw new errors_js_1.ResolverError((0, ono_1.ono)({ status: res.status }, `Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`));
105
- }
106
- else if (!("location" in res.headers) || !res.headers.location) {
107
- throw (0, ono_1.ono)({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
108
- }
109
- else {
110
- const redirectTo = url.resolve(u, res.headers.location);
111
- return download(redirectTo, httpOptions, redirects);
112
- }
96
+ else if (!("location" in res.headers) || !res.headers.location) {
97
+ throw (0, ono_1.ono)({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
113
98
  }
114
99
  else {
115
- if (res.body) {
116
- const buf = yield res.arrayBuffer();
117
- return Buffer.from(buf);
118
- }
119
- return Buffer.alloc(0);
100
+ const redirectTo = url.resolve(u, res.headers.location);
101
+ return download(redirectTo, httpOptions, redirects);
120
102
  }
121
103
  }
122
- catch (err) {
123
- throw new errors_js_1.ResolverError((0, ono_1.ono)(err, `Error downloading ${u.href}`), u.href);
104
+ else {
105
+ if (res.body) {
106
+ const buf = await res.arrayBuffer();
107
+ return Buffer.from(buf);
108
+ }
109
+ return Buffer.alloc(0);
124
110
  }
125
- });
111
+ }
112
+ catch (err) {
113
+ throw new errors_js_1.ResolverError((0, ono_1.ono)(err, `Error downloading ${u.href}`), u.href);
114
+ }
126
115
  }
127
116
  /**
128
117
  * Sends an HTTP GET request.
129
118
  * The promise resolves with the HTTP Response object.
130
119
  */
131
- function get(u, httpOptions) {
132
- return __awaiter(this, void 0, void 0, function* () {
133
- let controller;
134
- let timeoutId;
135
- if (httpOptions.timeout) {
136
- controller = new AbortController();
137
- timeoutId = setTimeout(() => controller.abort(), httpOptions.timeout);
138
- }
139
- if (!global.fetch) {
140
- const { default: fetch, Request, Headers } = yield Promise.resolve().then(() => __importStar(require("node-fetch")));
141
- // @ts-ignore
142
- global.fetch = fetch;
143
- // @ts-ignore
144
- global.Request = Request;
145
- // @ts-ignore
146
- global.Headers = Headers;
147
- }
148
- const response = yield fetch(u, {
149
- method: "GET",
150
- headers: httpOptions.headers || {},
151
- credentials: httpOptions.withCredentials ? "include" : "same-origin",
152
- signal: controller ? controller.signal : null,
153
- });
154
- if (timeoutId) {
155
- clearTimeout(timeoutId);
156
- }
157
- return response;
120
+ async function get(u, httpOptions) {
121
+ let controller;
122
+ let timeoutId;
123
+ if (httpOptions.timeout) {
124
+ controller = new AbortController();
125
+ timeoutId = setTimeout(() => controller.abort(), httpOptions.timeout);
126
+ }
127
+ if (!global.fetch) {
128
+ const { default: fetch, Request, Headers } = await Promise.resolve().then(() => __importStar(require("node-fetch")));
129
+ // @ts-ignore
130
+ global.fetch = fetch;
131
+ // @ts-ignore
132
+ global.Request = Request;
133
+ // @ts-ignore
134
+ global.Headers = Headers;
135
+ }
136
+ const response = await fetch(u, {
137
+ method: "GET",
138
+ headers: httpOptions.headers || {},
139
+ credentials: httpOptions.withCredentials ? "include" : "same-origin",
140
+ signal: controller ? controller.signal : null,
158
141
  });
142
+ if (timeoutId) {
143
+ clearTimeout(timeoutId);
144
+ }
145
+ return response;
159
146
  }
@@ -0,0 +1 @@
1
+ export default function convertPathToPosix(filePath: string): string;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ function convertPathToPosix(filePath) {
8
+ const isExtendedLengthPath = filePath.startsWith("\\\\?\\");
9
+ if (isExtendedLengthPath) {
10
+ return filePath;
11
+ }
12
+ return filePath.split(path_1.default.win32.sep).join(path_1.default.posix.sep);
13
+ }
14
+ exports.default = convertPathToPosix;
@@ -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;
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.run = exports.sort = exports.filter = exports.all = void 0;
13
4
  /**
@@ -56,62 +47,60 @@ exports.sort = sort;
56
47
  * If the promise rejects, or the callback is called with an error, then the next plugin is called.
57
48
  * If ALL plugins fail, then the last error is thrown.
58
49
  */
59
- function run(plugins, method, file, $refs) {
60
- return __awaiter(this, void 0, void 0, function* () {
61
- let plugin;
62
- let lastError;
63
- let index = 0;
64
- return new Promise((resolve, reject) => {
65
- runNextPlugin();
66
- function runNextPlugin() {
67
- plugin = plugins[index++];
68
- if (!plugin) {
69
- // There are no more functions, so re-throw the last error
70
- return reject(lastError);
71
- }
72
- try {
73
- // console.log(' %s', plugin.name);
74
- const result = getResult(plugin, method, file, callback, $refs);
75
- if (result && typeof result.then === "function") {
76
- // A promise was returned
77
- result.then(onSuccess, onError);
78
- }
79
- else if (result !== undefined) {
80
- // A synchronous result was returned
81
- onSuccess(result);
82
- }
83
- else if (index === plugins.length) {
84
- throw new Error("No promise has been returned or callback has been called.");
85
- }
86
- }
87
- catch (e) {
88
- onError(e);
89
- }
50
+ async function run(plugins, method, file, $refs) {
51
+ let plugin;
52
+ let lastError;
53
+ let index = 0;
54
+ return new Promise((resolve, reject) => {
55
+ runNextPlugin();
56
+ function runNextPlugin() {
57
+ plugin = plugins[index++];
58
+ if (!plugin) {
59
+ // There are no more functions, so re-throw the last error
60
+ return reject(lastError);
90
61
  }
91
- function callback(err, result) {
92
- if (err) {
93
- onError(err);
62
+ try {
63
+ // console.log(' %s', plugin.name);
64
+ const result = getResult(plugin, method, file, callback, $refs);
65
+ if (result && typeof result.then === "function") {
66
+ // A promise was returned
67
+ result.then(onSuccess, onError);
94
68
  }
95
- else {
69
+ else if (result !== undefined) {
70
+ // A synchronous result was returned
96
71
  onSuccess(result);
97
72
  }
73
+ else if (index === plugins.length) {
74
+ throw new Error("No promise has been returned or callback has been called.");
75
+ }
98
76
  }
99
- function onSuccess(result) {
100
- // console.log(' success');
101
- resolve({
102
- plugin,
103
- result,
104
- });
77
+ catch (e) {
78
+ onError(e);
105
79
  }
106
- function onError(error) {
107
- // console.log(' %s', err.message || err);
108
- lastError = {
109
- plugin,
110
- error,
111
- };
112
- runNextPlugin();
80
+ }
81
+ function callback(err, result) {
82
+ if (err) {
83
+ onError(err);
84
+ }
85
+ else {
86
+ onSuccess(result);
113
87
  }
114
- });
88
+ }
89
+ function onSuccess(result) {
90
+ // console.log(' success');
91
+ resolve({
92
+ plugin,
93
+ result,
94
+ });
95
+ }
96
+ function onError(error) {
97
+ // console.log(' %s', err.message || err);
98
+ lastError = {
99
+ plugin,
100
+ error,
101
+ };
102
+ runNextPlugin();
103
+ }
115
104
  });
116
105
  }
117
106
  exports.run = run;
@@ -17,7 +17,7 @@ export declare function cwd(): string;
17
17
  * @param path
18
18
  * @returns
19
19
  */
20
- export declare function getProtocol(path: any): string | undefined;
20
+ export declare function getProtocol(path: string | undefined): string | undefined;
21
21
  /**
22
22
  * Returns the lowercased file extension of the given URL,
23
23
  * or an empty string if it has no extension.
@@ -62,7 +62,7 @@ export declare function isHttp(path: any): boolean;
62
62
  * @param path
63
63
  * @returns
64
64
  */
65
- export declare function isFileSystemPath(path: any): boolean;
65
+ export declare function isFileSystemPath(path: string | undefined): boolean;
66
66
  /**
67
67
  * Converts a filesystem path to a properly-encoded URL.
68
68
  *
@@ -91,3 +91,4 @@ export declare function toFileSystemPath(path: string | undefined, keepFileProto
91
91
  * @returns
92
92
  */
93
93
  export declare function safePointerToPath(pointer: any): any;
94
+ export declare function relative(from: string | undefined, to: string | undefined): string;
@@ -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 isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : ""), forwardSlashPattern = /\//g, protocolPattern = /^(\w{2,}):\/\//i, jsonPointerSlash = /~1/g, jsonPointerTilde = /~0/g;
5
- const path_1 = require("path");
6
- const projectDir = (0, path_1.join)(__dirname, "..", "..");
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
@@ -51,7 +83,7 @@ exports.cwd = cwd;
51
83
  * @returns
52
84
  */
53
85
  function getProtocol(path) {
54
- const match = protocolPattern.exec(path);
86
+ const match = protocolPattern.exec(path || "");
55
87
  if (match) {
56
88
  return match[1].toLowerCase();
57
89
  }
@@ -173,15 +205,17 @@ exports.isFileSystemPath = isFileSystemPath;
173
205
  function fromFileSystemPath(path) {
174
206
  // Step 1: On Windows, replace backslashes with forward slashes,
175
207
  // rather than encoding them as "%5C"
176
- if (isWindows) {
177
- const hasProjectDir = path.toUpperCase().includes(projectDir.replace(/\\/g, "\\").toUpperCase());
178
- const hasProjectUri = path.toUpperCase().includes(projectDir.replace(/\\/g, "/").toUpperCase());
179
- if (hasProjectDir || hasProjectUri) {
180
- path = path.replace(/\\/g, "/");
181
- }
182
- else {
183
- path = `${projectDir}/${path}`.replace(/\\/g, "/");
208
+ if ((0, is_windows_1.isWindows)()) {
209
+ const upperPath = path.toUpperCase();
210
+ const projectDirPosixPath = (0, convert_path_to_posix_1.default)(projectDir);
211
+ const posixUpper = projectDirPosixPath.toUpperCase();
212
+ const hasProjectDir = upperPath.includes(posixUpper);
213
+ const hasProjectUri = upperPath.includes(posixUpper);
214
+ const isAbsolutePath = path_1.win32.isAbsolute(path);
215
+ if (!(hasProjectDir || hasProjectUri || isAbsolutePath)) {
216
+ path = (0, path_2.join)(projectDir, path);
184
217
  }
218
+ path = (0, convert_path_to_posix_1.default)(path);
185
219
  }
186
220
  // Step 2: `encodeURI` will take care of MOST characters
187
221
  path = encodeURI(path);
@@ -213,7 +247,7 @@ function toFileSystemPath(path, keepFileProtocol) {
213
247
  // Strip-off the protocol, and the initial "/", if there is one
214
248
  path = path[7] === "/" ? path.substr(8) : path.substr(7);
215
249
  // insert a colon (":") after the drive letter on Windows
216
- if (isWindows && path[1] === "/") {
250
+ if ((0, is_windows_1.isWindows)() && path[1] === "/") {
217
251
  path = path[0] + ":" + path.substr(1);
218
252
  }
219
253
  if (keepFileProtocol) {
@@ -225,11 +259,11 @@ function toFileSystemPath(path, keepFileProtocol) {
225
259
  // On Windows, it will start with something like "C:/".
226
260
  // On Posix, it will start with "/"
227
261
  isFileUrl = false;
228
- path = isWindows ? path : "/" + path;
262
+ path = (0, is_windows_1.isWindows)() ? path : "/" + path;
229
263
  }
230
264
  }
231
265
  // Step 4: Normalize Windows paths (unless it's a "file://" URL)
232
- if (isWindows && !isFileUrl) {
266
+ if ((0, is_windows_1.isWindows)() && !isFileUrl) {
233
267
  // Replace forward slashes with backslashes
234
268
  path = path.replace(forwardSlashPattern, "\\");
235
269
  // Capitalize the drive letter
@@ -258,3 +292,13 @@ function safePointerToPath(pointer) {
258
292
  });
259
293
  }
260
294
  exports.safePointerToPath = safePointerToPath;
295
+ function relative(from, to) {
296
+ if (!isFileSystemPath(from) || !isFileSystemPath(to)) {
297
+ return resolve(from, to);
298
+ }
299
+ const fromDir = path_1.default.dirname(stripHash(from));
300
+ const toPath = stripHash(to);
301
+ const result = path_1.default.relative(fromDir, toPath);
302
+ return result + getHash(to);
303
+ }
304
+ exports.relative = relative;
@@ -1,2 +1,2 @@
1
- declare const _default: import("vitest/dist/config").UserConfigExport;
1
+ declare const _default: import("vite").UserConfig;
2
2
  export default _default;