@hey-api/json-schema-ref-parser 1.2.3 → 1.3.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 +9 -84
- package/dist/index.d.mts +629 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1887 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +42 -78
- package/src/__tests__/bundle.test.ts +59 -0
- package/src/__tests__/index.test.ts +43 -0
- package/src/__tests__/pointer.test.ts +34 -0
- package/src/__tests__/utils.ts +3 -0
- package/{lib → src}/bundle.ts +143 -229
- package/{lib → src}/dereference.ts +20 -43
- package/{lib → src}/index.ts +103 -125
- package/{lib → src}/options.ts +13 -9
- package/{lib → src}/parse.ts +19 -15
- package/src/parsers/binary.ts +13 -0
- package/{lib → src}/parsers/json.ts +5 -6
- package/src/parsers/text.ts +21 -0
- package/{lib → src}/parsers/yaml.ts +9 -9
- package/{lib → src}/pointer.ts +42 -23
- package/{lib → src}/ref.ts +25 -21
- package/{lib → src}/refs.ts +23 -26
- package/{lib → src}/resolve-external.ts +91 -60
- package/{lib → src}/resolvers/file.ts +7 -10
- package/{lib → src}/resolvers/url.ts +12 -8
- package/{lib → src}/types/index.ts +9 -2
- package/src/util/convert-path-to-posix.ts +8 -0
- package/{lib → src}/util/errors.ts +38 -36
- package/{lib → src}/util/is-windows.ts +1 -1
- package/{lib → src}/util/plugins.ts +7 -8
- package/{lib → src}/util/url.ts +41 -42
- package/dist/lib/__tests__/bundle.test.d.ts +0 -1
- package/dist/lib/__tests__/bundle.test.js +0 -50
- package/dist/lib/__tests__/index.test.d.ts +0 -1
- package/dist/lib/__tests__/index.test.js +0 -43
- package/dist/lib/__tests__/pointer.test.d.ts +0 -1
- package/dist/lib/__tests__/pointer.test.js +0 -27
- package/dist/lib/bundle.d.ts +0 -26
- package/dist/lib/bundle.js +0 -600
- package/dist/lib/dereference.d.ts +0 -11
- package/dist/lib/dereference.js +0 -226
- package/dist/lib/index.d.ts +0 -92
- package/dist/lib/index.js +0 -525
- package/dist/lib/options.d.ts +0 -61
- package/dist/lib/options.js +0 -45
- package/dist/lib/parse.d.ts +0 -13
- package/dist/lib/parse.js +0 -87
- package/dist/lib/parsers/binary.d.ts +0 -2
- package/dist/lib/parsers/binary.js +0 -12
- package/dist/lib/parsers/json.d.ts +0 -2
- package/dist/lib/parsers/json.js +0 -38
- package/dist/lib/parsers/text.d.ts +0 -2
- package/dist/lib/parsers/text.js +0 -18
- package/dist/lib/parsers/yaml.d.ts +0 -2
- package/dist/lib/parsers/yaml.js +0 -28
- package/dist/lib/pointer.d.ts +0 -88
- package/dist/lib/pointer.js +0 -297
- package/dist/lib/ref.d.ts +0 -180
- package/dist/lib/ref.js +0 -226
- package/dist/lib/refs.d.ts +0 -127
- package/dist/lib/refs.js +0 -232
- package/dist/lib/resolve-external.d.ts +0 -13
- package/dist/lib/resolve-external.js +0 -151
- package/dist/lib/resolvers/file.d.ts +0 -6
- package/dist/lib/resolvers/file.js +0 -61
- package/dist/lib/resolvers/url.d.ts +0 -17
- package/dist/lib/resolvers/url.js +0 -62
- package/dist/lib/types/index.d.ts +0 -43
- package/dist/lib/types/index.js +0 -2
- package/dist/lib/util/convert-path-to-posix.d.ts +0 -1
- package/dist/lib/util/convert-path-to-posix.js +0 -14
- package/dist/lib/util/errors.d.ts +0 -56
- package/dist/lib/util/errors.js +0 -112
- package/dist/lib/util/is-windows.d.ts +0 -1
- package/dist/lib/util/is-windows.js +0 -6
- package/dist/lib/util/plugins.d.ts +0 -16
- package/dist/lib/util/plugins.js +0 -45
- package/dist/lib/util/url.d.ts +0 -79
- package/dist/lib/util/url.js +0 -285
- package/dist/vite.config.d.ts +0 -2
- package/dist/vite.config.js +0 -19
- package/lib/__tests__/bundle.test.ts +0 -52
- package/lib/__tests__/index.test.ts +0 -45
- package/lib/__tests__/pointer.test.ts +0 -26
- package/lib/__tests__/spec/circular-ref-with-description.json +0 -11
- package/lib/__tests__/spec/multiple-refs.json +0 -34
- package/lib/__tests__/spec/openapi-paths-ref.json +0 -46
- package/lib/__tests__/spec/path-parameter.json +0 -16
- package/lib/parsers/binary.ts +0 -13
- package/lib/parsers/text.ts +0 -21
- package/lib/util/convert-path-to-posix.ts +0 -11
- /package/{LICENSE → LICENSE.md} +0 -0
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import $
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import type
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import
|
|
1
|
+
import type { $RefParser } from '.';
|
|
2
|
+
import { getResolvedInput } from '.';
|
|
3
|
+
import type { $RefParserOptions } from './options';
|
|
4
|
+
import { newFile, parseFile } from './parse';
|
|
5
|
+
import Pointer from './pointer';
|
|
6
|
+
import $Ref from './ref';
|
|
7
|
+
import type $Refs from './refs';
|
|
8
|
+
import { fileResolver } from './resolvers/file';
|
|
9
|
+
import { urlResolver } from './resolvers/url';
|
|
10
|
+
import type { JSONSchema } from './types';
|
|
11
|
+
import { isHandledError } from './util/errors';
|
|
12
|
+
import * as url from './util/url';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Crawls the JSON schema, finds all external JSON references, and resolves their values.
|
|
@@ -21,54 +21,68 @@ import { urlResolver } from "./resolvers/url.js";
|
|
|
21
21
|
* The promise resolves once all JSON references in the schema have been resolved,
|
|
22
22
|
* including nested references that are contained in externally-referenced files.
|
|
23
23
|
*/
|
|
24
|
-
export function resolveExternal(parser: $RefParser, options: $RefParserOptions) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
}
|
|
24
|
+
export async function resolveExternal(parser: $RefParser, options: $RefParserOptions) {
|
|
25
|
+
const promises = crawl(parser.schema, {
|
|
26
|
+
$refs: parser.$refs,
|
|
27
|
+
options: options.parse,
|
|
28
|
+
path: `${parser.$refs._root$Ref.path}#`,
|
|
29
|
+
});
|
|
30
|
+
await Promise.all(promises);
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
34
|
* Recursively crawls the given value, and resolves any external JSON references.
|
|
36
35
|
*
|
|
37
36
|
* @param obj - The value to crawl. If it's not an object or array, it will be ignored.
|
|
38
|
-
* @
|
|
39
|
-
* @param {boolean} external - Whether `obj` was found in an external document.
|
|
40
|
-
* @param $refs
|
|
41
|
-
* @param options
|
|
42
|
-
* @param seen - Internal.
|
|
43
|
-
*
|
|
44
|
-
* @returns
|
|
45
|
-
* Returns an array of promises. There will be one promise for each JSON reference in `obj`.
|
|
37
|
+
* @returns An array of promises. There will be one promise for each JSON reference in `obj`.
|
|
46
38
|
* If `obj` does not contain any JSON references, then the array will be empty.
|
|
47
39
|
* If any of the JSON references point to files that contain additional JSON references,
|
|
48
40
|
* then the corresponding promise will internally reference an array of promises.
|
|
49
41
|
*/
|
|
50
42
|
function crawl<S extends object = JSONSchema>(
|
|
51
43
|
obj: string | Buffer | S | undefined | null,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
|
|
44
|
+
{
|
|
45
|
+
$refs,
|
|
46
|
+
external = false,
|
|
47
|
+
options,
|
|
48
|
+
path,
|
|
49
|
+
seen = new Set(),
|
|
50
|
+
}: {
|
|
51
|
+
$refs: $Refs<S>;
|
|
52
|
+
/** Whether `obj` was found in an external document. */
|
|
53
|
+
external?: boolean;
|
|
54
|
+
options: $RefParserOptions['parse'];
|
|
55
|
+
/** The full path of `obj`, possibly with a JSON Pointer in the hash. */
|
|
56
|
+
path: string;
|
|
57
|
+
seen?: Set<unknown>;
|
|
58
|
+
},
|
|
59
|
+
): ReadonlyArray<Promise<unknown>> {
|
|
60
|
+
let promises: Array<Promise<unknown>> = [];
|
|
61
|
+
|
|
62
|
+
if (obj && typeof obj === 'object' && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
|
|
63
|
+
seen.add(obj);
|
|
60
64
|
|
|
61
|
-
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
|
|
62
|
-
seen.add(obj); // Track previously seen objects to avoid infinite recursion
|
|
63
65
|
if ($Ref.isExternal$Ref(obj)) {
|
|
64
|
-
promises.push(
|
|
66
|
+
promises.push(
|
|
67
|
+
resolve$Ref<S>(obj, {
|
|
68
|
+
$refs,
|
|
69
|
+
options,
|
|
70
|
+
path,
|
|
71
|
+
seen,
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
65
74
|
}
|
|
66
75
|
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
77
|
+
promises = promises.concat(
|
|
78
|
+
crawl(value, {
|
|
79
|
+
$refs,
|
|
80
|
+
external,
|
|
81
|
+
options,
|
|
82
|
+
path: Pointer.join(path, key),
|
|
83
|
+
seen,
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
72
86
|
}
|
|
73
87
|
}
|
|
74
88
|
|
|
@@ -89,15 +103,21 @@ function crawl<S extends object = JSONSchema>(
|
|
|
89
103
|
*/
|
|
90
104
|
async function resolve$Ref<S extends object = JSONSchema>(
|
|
91
105
|
$ref: S,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
106
|
+
{
|
|
107
|
+
$refs,
|
|
108
|
+
options,
|
|
109
|
+
path,
|
|
110
|
+
seen,
|
|
111
|
+
}: {
|
|
112
|
+
$refs: $Refs<S>;
|
|
113
|
+
options: $RefParserOptions['parse'];
|
|
114
|
+
path: string;
|
|
115
|
+
seen: Set<unknown>;
|
|
116
|
+
},
|
|
117
|
+
): Promise<unknown> {
|
|
96
118
|
const resolvedPath = url.resolve(path, ($ref as JSONSchema).$ref!);
|
|
97
119
|
const withoutHash = url.stripHash(resolvedPath);
|
|
98
120
|
|
|
99
|
-
// $ref.$ref = url.relative($refs._root$Ref.path, resolvedPath);
|
|
100
|
-
|
|
101
121
|
// If this ref points back to an input source we've already merged, avoid re-importing
|
|
102
122
|
// by checking if the path (without hash) matches a known source in parser and we can serve it internally later.
|
|
103
123
|
// We keep normal flow but ensure cache hit if already added.
|
|
@@ -105,7 +125,13 @@ async function resolve$Ref<S extends object = JSONSchema>(
|
|
|
105
125
|
const ref = $refs._$refs[withoutHash];
|
|
106
126
|
if (ref) {
|
|
107
127
|
// We've already parsed this $ref, so crawl it to resolve its own externals
|
|
108
|
-
const promises = crawl(ref.value as S,
|
|
128
|
+
const promises = crawl(ref.value as S, {
|
|
129
|
+
$refs,
|
|
130
|
+
external: true,
|
|
131
|
+
options,
|
|
132
|
+
path: `${withoutHash}#`,
|
|
133
|
+
seen,
|
|
134
|
+
});
|
|
109
135
|
return Promise.all(promises);
|
|
110
136
|
}
|
|
111
137
|
|
|
@@ -121,22 +147,27 @@ async function resolve$Ref<S extends object = JSONSchema>(
|
|
|
121
147
|
|
|
122
148
|
$refAdded.pathType = resolvedInput.type;
|
|
123
149
|
|
|
124
|
-
let promises:
|
|
150
|
+
let promises: ReadonlyArray<Promise<unknown>> = [];
|
|
125
151
|
|
|
126
|
-
if (resolvedInput.type !==
|
|
127
|
-
const resolver = resolvedInput.type ===
|
|
152
|
+
if (resolvedInput.type !== 'json') {
|
|
153
|
+
const resolver = resolvedInput.type === 'file' ? fileResolver : urlResolver;
|
|
128
154
|
await resolver.handler({ file });
|
|
129
155
|
const parseResult = await parseFile(file, options);
|
|
130
156
|
$refAdded.value = parseResult.result;
|
|
131
|
-
promises = crawl(parseResult.result,
|
|
157
|
+
promises = crawl(parseResult.result, {
|
|
158
|
+
$refs,
|
|
159
|
+
external: true,
|
|
160
|
+
options,
|
|
161
|
+
path: `${withoutHash}#`,
|
|
162
|
+
seen,
|
|
163
|
+
});
|
|
132
164
|
}
|
|
133
165
|
|
|
134
166
|
return Promise.all(promises);
|
|
135
|
-
} catch (
|
|
136
|
-
if (isHandledError(
|
|
137
|
-
$refAdded.value =
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (isHandledError(error)) {
|
|
169
|
+
$refAdded.value = error;
|
|
138
170
|
}
|
|
139
|
-
|
|
140
|
-
throw err;
|
|
171
|
+
throw error;
|
|
141
172
|
}
|
|
142
173
|
}
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
1
|
+
import { ono } from '@jsdevtools/ono';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
|
|
4
|
+
import type { FileInfo } from '../types';
|
|
5
|
+
import { ResolverError } from '../util/errors';
|
|
6
|
+
import * as url from '../util/url';
|
|
6
7
|
|
|
7
8
|
export const fileResolver = {
|
|
8
|
-
handler: async ({
|
|
9
|
-
file,
|
|
10
|
-
}: {
|
|
11
|
-
file: FileInfo;
|
|
12
|
-
}): Promise<void> => {
|
|
9
|
+
handler: async ({ file }: { file: FileInfo }): Promise<void> => {
|
|
13
10
|
let path: string | undefined;
|
|
14
11
|
|
|
15
12
|
try {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { ono } from
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import { ono } from '@jsdevtools/ono';
|
|
2
|
+
|
|
3
|
+
import type { FileInfo } from '../types';
|
|
4
|
+
import { ResolverError } from '../util/errors';
|
|
5
|
+
import { resolve } from '../util/url';
|
|
5
6
|
|
|
6
7
|
export const sendRequest = async ({
|
|
7
8
|
fetchOptions,
|
|
@@ -35,13 +36,16 @@ export const sendRequest = async ({
|
|
|
35
36
|
throw new ResolverError(
|
|
36
37
|
ono(
|
|
37
38
|
{ status: response.status },
|
|
38
|
-
`Error requesting ${redirects[0]}. \nToo many redirects: \n ${redirects.join(
|
|
39
|
+
`Error requesting ${redirects[0]}. \nToo many redirects: \n ${redirects.join(' \n ')}`,
|
|
39
40
|
),
|
|
40
41
|
);
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
if (!(
|
|
44
|
-
throw ono(
|
|
44
|
+
if (!('location' in response.headers) || !response.headers.location) {
|
|
45
|
+
throw ono(
|
|
46
|
+
{ status: response.status },
|
|
47
|
+
`HTTP ${response.status} redirect with no location header`,
|
|
48
|
+
);
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
return sendRequest({
|
|
@@ -53,7 +57,7 @@ export const sendRequest = async ({
|
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
return { fetchOptions, response };
|
|
56
|
-
}
|
|
60
|
+
};
|
|
57
61
|
|
|
58
62
|
export const urlResolver = {
|
|
59
63
|
handler: async ({
|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
JSONSchema6Object,
|
|
6
6
|
JSONSchema7,
|
|
7
7
|
JSONSchema7Object,
|
|
8
|
-
} from
|
|
8
|
+
} from 'json-schema';
|
|
9
9
|
|
|
10
10
|
export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
|
|
11
11
|
export type JSONSchemaObject = JSONSchema4Object | JSONSchema6Object | JSONSchema7Object;
|
|
@@ -20,7 +20,14 @@ export interface Plugin {
|
|
|
20
20
|
*
|
|
21
21
|
* Unlike the `canHandle` function, the `parse` method can also be asynchronous. This might be important if your parser needs to retrieve data from a database or if it relies on an external HTTP service to return the parsed value. You can return your asynchronous value via a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Here are examples of both approaches:
|
|
22
22
|
*/
|
|
23
|
-
handler: (
|
|
23
|
+
handler: (
|
|
24
|
+
file: FileInfo,
|
|
25
|
+
) =>
|
|
26
|
+
| string
|
|
27
|
+
| Buffer
|
|
28
|
+
| JSONSchema
|
|
29
|
+
| Promise<{ data: Buffer }>
|
|
30
|
+
| Promise<string | Buffer | JSONSchema>;
|
|
24
31
|
name: 'binary' | 'file' | 'http' | 'json' | 'text' | 'yaml';
|
|
25
32
|
}
|
|
26
33
|
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { Ono } from
|
|
2
|
-
|
|
3
|
-
import type { $RefParser } from
|
|
4
|
-
import type
|
|
5
|
-
import type
|
|
1
|
+
import { Ono } from '@jsdevtools/ono';
|
|
2
|
+
|
|
3
|
+
import type { $RefParser } from '..';
|
|
4
|
+
import type $Ref from '../ref';
|
|
5
|
+
import type { JSONSchema } from '../types';
|
|
6
|
+
import { getHash, stripHash, toFileSystemPath } from './url';
|
|
6
7
|
|
|
7
8
|
export type JSONParserErrorType =
|
|
8
|
-
|
|
|
9
|
-
|
|
|
10
|
-
|
|
|
11
|
-
|
|
|
12
|
-
|
|
|
13
|
-
|
|
|
14
|
-
|
|
|
15
|
-
|
|
|
9
|
+
| 'EUNKNOWN'
|
|
10
|
+
| 'EPARSER'
|
|
11
|
+
| 'EUNMATCHEDPARSER'
|
|
12
|
+
| 'ETIMEOUT'
|
|
13
|
+
| 'ERESOLVER'
|
|
14
|
+
| 'EUNMATCHEDRESOLVER'
|
|
15
|
+
| 'EMISSINGPOINTER'
|
|
16
|
+
| 'EINVALIDPOINTER';
|
|
16
17
|
|
|
17
18
|
export class JSONParserError extends Error {
|
|
18
19
|
public readonly name: string;
|
|
@@ -23,8 +24,8 @@ export class JSONParserError extends Error {
|
|
|
23
24
|
public constructor(message: string, source?: string) {
|
|
24
25
|
super();
|
|
25
26
|
|
|
26
|
-
this.code =
|
|
27
|
-
this.name =
|
|
27
|
+
this.code = 'EUNKNOWN';
|
|
28
|
+
this.name = 'JSONParserError';
|
|
28
29
|
this.message = message;
|
|
29
30
|
this.source = source;
|
|
30
31
|
this.path = null;
|
|
@@ -44,17 +45,15 @@ export class JSONParserErrorGroup<S extends object = JSONSchema> extends Error {
|
|
|
44
45
|
super();
|
|
45
46
|
|
|
46
47
|
this.files = parser;
|
|
47
|
-
this.name =
|
|
48
|
+
this.name = 'JSONParserErrorGroup';
|
|
48
49
|
this.message = `${this.errors.length} error${
|
|
49
|
-
this.errors.length > 1 ?
|
|
50
|
+
this.errors.length > 1 ? 's' : ''
|
|
50
51
|
} occurred while reading '${toFileSystemPath(parser.$refs._root$Ref!.path)}'`;
|
|
51
52
|
|
|
52
53
|
Ono.extend(this);
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
static getParserErrors<S extends object = JSONSchema>(
|
|
56
|
-
parser: $RefParser,
|
|
57
|
-
) {
|
|
56
|
+
static getParserErrors<S extends object = JSONSchema>(parser: $RefParser) {
|
|
58
57
|
const errors = [];
|
|
59
58
|
|
|
60
59
|
for (const $ref of Object.values(parser.$refs._$refs) as $Ref<S>[]) {
|
|
@@ -80,16 +79,16 @@ export class JSONParserErrorGroup<S extends object = JSONSchema> extends Error {
|
|
|
80
79
|
}
|
|
81
80
|
|
|
82
81
|
export class ParserError extends JSONParserError {
|
|
83
|
-
code =
|
|
84
|
-
name =
|
|
82
|
+
code = 'EPARSER' as JSONParserErrorType;
|
|
83
|
+
name = 'ParserError';
|
|
85
84
|
constructor(message: any, source: any) {
|
|
86
85
|
super(`Error parsing ${source}: ${message}`, source);
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
88
|
|
|
90
89
|
export class UnmatchedParserError extends JSONParserError {
|
|
91
|
-
code =
|
|
92
|
-
name =
|
|
90
|
+
code = 'EUNMATCHEDPARSER' as JSONParserErrorType;
|
|
91
|
+
name = 'UnmatchedParserError';
|
|
93
92
|
|
|
94
93
|
constructor(source: string) {
|
|
95
94
|
super(`Could not find parser for "${source}"`, source);
|
|
@@ -97,44 +96,47 @@ export class UnmatchedParserError extends JSONParserError {
|
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
export class ResolverError extends JSONParserError {
|
|
100
|
-
code =
|
|
101
|
-
name =
|
|
99
|
+
code = 'ERESOLVER' as JSONParserErrorType;
|
|
100
|
+
name = 'ResolverError';
|
|
102
101
|
ioErrorCode?: string;
|
|
103
102
|
constructor(ex: Error | any, source?: string) {
|
|
104
103
|
super(ex.message || `Error reading file "${source}"`, source);
|
|
105
|
-
if (
|
|
104
|
+
if ('code' in ex) {
|
|
106
105
|
this.ioErrorCode = String(ex.code);
|
|
107
106
|
}
|
|
108
107
|
}
|
|
109
108
|
}
|
|
110
109
|
|
|
111
110
|
export class UnmatchedResolverError extends JSONParserError {
|
|
112
|
-
code =
|
|
113
|
-
name =
|
|
111
|
+
code = 'EUNMATCHEDRESOLVER' as JSONParserErrorType;
|
|
112
|
+
name = 'UnmatchedResolverError';
|
|
114
113
|
constructor(source: any) {
|
|
115
114
|
super(`Could not find resolver for "${source}"`, source);
|
|
116
115
|
}
|
|
117
116
|
}
|
|
118
117
|
|
|
119
118
|
export class MissingPointerError extends JSONParserError {
|
|
120
|
-
code =
|
|
121
|
-
name =
|
|
119
|
+
code = 'EMISSINGPOINTER' as JSONParserErrorType;
|
|
120
|
+
name = 'MissingPointerError';
|
|
122
121
|
constructor(token: string, path: string) {
|
|
123
|
-
super(
|
|
122
|
+
super(
|
|
123
|
+
`Missing $ref pointer "${getHash(path)}". Token "${token}" does not exist.`,
|
|
124
|
+
stripHash(path),
|
|
125
|
+
);
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
export class TimeoutError extends JSONParserError {
|
|
128
|
-
code =
|
|
129
|
-
name =
|
|
130
|
+
code = 'ETIMEOUT' as JSONParserErrorType;
|
|
131
|
+
name = 'TimeoutError';
|
|
130
132
|
constructor(timeout: number) {
|
|
131
133
|
super(`Dereferencing timeout reached: ${timeout}ms`);
|
|
132
134
|
}
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
export class InvalidPointerError extends JSONParserError {
|
|
136
|
-
code =
|
|
137
|
-
name =
|
|
138
|
+
code = 'EUNMATCHEDRESOLVER' as JSONParserErrorType;
|
|
139
|
+
name = 'InvalidPointerError';
|
|
138
140
|
constructor(pointer: string, path: string) {
|
|
139
141
|
super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path));
|
|
140
142
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const isWindowsConst = /^win/.test(globalThis.process ? globalThis.process.platform :
|
|
1
|
+
const isWindowsConst = /^win/.test(globalThis.process ? globalThis.process.platform : '');
|
|
2
2
|
export const isWindows = () => isWindowsConst;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { FileInfo, JSONSchema } from
|
|
2
|
-
import type { Plugin } from "../types/index.js";
|
|
1
|
+
import type { FileInfo, JSONSchema, Plugin } from '../types';
|
|
3
2
|
|
|
4
3
|
export interface PluginResult {
|
|
5
4
|
error?: any;
|
|
@@ -22,7 +21,7 @@ export async function run(plugins: Pick<Plugin, 'handler'>[], file: FileInfo) {
|
|
|
22
21
|
|
|
23
22
|
return new Promise<PluginResult>((resolve, reject) => {
|
|
24
23
|
const runNextPlugin = async () => {
|
|
25
|
-
plugin = plugins[index++]
|
|
24
|
+
plugin = plugins[index++]!;
|
|
26
25
|
|
|
27
26
|
if (!plugin) {
|
|
28
27
|
// there are no more functions, re-throw the last error
|
|
@@ -30,7 +29,7 @@ export async function run(plugins: Pick<Plugin, 'handler'>[], file: FileInfo) {
|
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
try {
|
|
33
|
-
const result = await plugin.handler(file)
|
|
32
|
+
const result = await plugin.handler(file);
|
|
34
33
|
|
|
35
34
|
if (result !== undefined) {
|
|
36
35
|
return resolve({
|
|
@@ -40,16 +39,16 @@ export async function run(plugins: Pick<Plugin, 'handler'>[], file: FileInfo) {
|
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
if (index === plugins.length) {
|
|
43
|
-
throw new Error(
|
|
42
|
+
throw new Error('No promise has been returned.');
|
|
44
43
|
}
|
|
45
|
-
} catch (
|
|
44
|
+
} catch (error) {
|
|
46
45
|
lastError = {
|
|
46
|
+
error,
|
|
47
47
|
plugin,
|
|
48
|
-
error: e,
|
|
49
48
|
};
|
|
50
49
|
runNextPlugin();
|
|
51
50
|
}
|
|
52
|
-
}
|
|
51
|
+
};
|
|
53
52
|
|
|
54
53
|
runNextPlugin();
|
|
55
54
|
});
|