@apidevtools/json-schema-ref-parser 9.1.1 → 9.1.2

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 CHANGED
@@ -1,7 +1,5 @@
1
- # JSON Schema $Ref Parser
2
-
3
- _**This package needs [a new maintainer](https://github.com/APIDevTools/json-schema-ref-parser/issues/285) or at least some contributors, or a decent number of sponsors so I can hire contractors to do the work. For more information [please read this article](https://phil.tech/2022/bundling-openapi-with-javascript/). I'll mark the repo as read-only and unmaintained on January 15th 2023 along with a bunch of other @apidevtools packages like swagger-parser, so I can focus on scaling up my [reforestation charity](https://protect.earth/) instead of burning myself out trying to maintain a whole load of OSS projects I don't use in my vanishingly small spare time.** - [Phil Sturgeon](https://github.com/philsturgeon)_
4
-
1
+ JSON Schema $Ref Parser
2
+ ============================
5
3
  #### Parse, Resolve, and Dereference JSON Schema $ref pointers
6
4
 
7
5
  [![Build Status](https://github.com/APIDevTools/json-schema-ref-parser/workflows/CI-CD/badge.svg?branch=master)](https://github.com/APIDevTools/json-schema-ref-parser/actions)
@@ -113,13 +111,6 @@ When using a transpiler such as [Babel](https://babeljs.io/) or [TypeScript](htt
113
111
  import $RefParser from "@apidevtools/json-schema-ref-parser";
114
112
  ```
115
113
 
116
- If you are using Node.js < 18, you'll need a polyfill for `fetch`, like [node-fetch](https://github.com/node-fetch/node-fetch):
117
- ```javascript
118
- import fetch from "node-fetch";
119
-
120
- globalThis.fetch = fetch;
121
- ```
122
-
123
114
 
124
115
 
125
116
  Browser support
package/lib/bundle.js CHANGED
@@ -135,7 +135,7 @@ function inventory$Ref ($refParent, $refKey, path, pathFromRoot, indirections, i
135
135
  });
136
136
 
137
137
  // Recursively crawl the resolved value
138
- if (!existingEntry || external) {
138
+ if (!existingEntry) {
139
139
  crawl(pointer.value, null, pointer.path, pathFromRoot, indirections + 1, inventory, $refs, options);
140
140
  }
141
141
  }
@@ -71,9 +71,6 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
71
71
  // Avoid pointless mutations; breaks frozen objects to no profit
72
72
  if (obj[key] !== dereferenced.value) {
73
73
  obj[key] = dereferenced.value;
74
- if (options.dereference.onDereference) {
75
- options.dereference.onDereference(value.$ref, obj[key]);
76
- }
77
74
  }
78
75
  }
79
76
  else {
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JSONSchema4, JSONSchema4Object, JSONSchema4Type, JSONSchema6, JSONSchema6Object, JSONSchema6Type, JSONSchema7, JSONSchema7Object, JSONSchema7Type } from "json-schema";
1
+ import { JSONSchema4, JSONSchema4Type, JSONSchema6, JSONSchema6Type, JSONSchema7, JSONSchema7Type } from "json-schema";
2
2
 
3
3
  export = $RefParser;
4
4
 
@@ -174,7 +174,6 @@ declare class $RefParser {
174
174
  declare namespace $RefParser {
175
175
 
176
176
  export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
177
- export type JSONSchemaObject = JSONSchema4Object | JSONSchema6Object | JSONSchema7Object;
178
177
  export type SchemaCallback = (err: Error | null, schema?: JSONSchema) => any;
179
178
  export type $RefsCallback = (err: Error | null, $refs?: $Refs) => any;
180
179
 
@@ -239,14 +238,6 @@ declare namespace $RefParser {
239
238
  * subpaths contain literal $ref keys that should not be dereferenced.
240
239
  */
241
240
  excludedPathMatcher?(path: string): boolean;
242
-
243
- /**
244
- * Callback invoked during dereferencing.
245
- *
246
- * @argument {string} path The path being dereferenced (ie. the `$ref` string).
247
- * @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
248
- */
249
- onDereference(path: string, value: JSONSchemaObject): void;
250
241
  };
251
242
  }
252
243
 
package/lib/refs.js CHANGED
@@ -4,9 +4,6 @@ const { ono } = require("@jsdevtools/ono");
4
4
  const $Ref = require("./ref");
5
5
  const url = require("./util/url");
6
6
 
7
- const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
8
- const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath;
9
-
10
7
  module.exports = $Refs;
11
8
 
12
9
  /**
@@ -47,7 +44,7 @@ function $Refs () {
47
44
  $Refs.prototype.paths = function (types) { // eslint-disable-line no-unused-vars
48
45
  let paths = getPaths(this._$refs, arguments);
49
46
  return paths.map((path) => {
50
- return getPathFromOs(path.decoded);
47
+ return path.decoded;
51
48
  });
52
49
  };
53
50
 
@@ -61,7 +58,7 @@ $Refs.prototype.values = function (types) { // eslint-disable-line no-unused-v
61
58
  let $refs = this._$refs;
62
59
  let paths = getPaths($refs, arguments);
63
60
  return paths.reduce((obj, path) => {
64
- obj[getPathFromOs(path.decoded)] = $refs[path.encoded].value;
61
+ obj[path.decoded] = $refs[path.encoded].value;
65
62
  return obj;
66
63
  }, {});
67
64
  };
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ const http = require("http");
4
+ const https = require("https");
3
5
  const { ono } = require("@jsdevtools/ono");
4
6
  const url = require("../util/url");
5
7
  const { ResolverError } = require("../util/errors");
@@ -73,7 +75,7 @@ module.exports = {
73
75
  read (file) {
74
76
  let u = url.parse(file.url);
75
77
 
76
- if (typeof window !== "undefined" && !u.protocol) {
78
+ if (process.browser && !u.protocol) {
77
79
  // Use the protocol of the current page
78
80
  u.protocol = url.parse(location.href).protocol;
79
81
  }
@@ -93,36 +95,38 @@ module.exports = {
93
95
  * The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
94
96
  */
95
97
  function download (u, httpOptions, redirects) {
96
- u = url.parse(u);
97
- redirects = redirects || [];
98
- redirects.push(u.href);
99
-
100
- return get(u, httpOptions)
101
- .then((res) => {
102
- if (res.status >= 400) {
103
- throw ono({ status: res.statusCode }, `HTTP ERROR ${res.status}`);
104
- }
105
- else if (res.status >= 300) {
106
- if (redirects.length > httpOptions.redirects) {
107
- throw new ResolverError(ono({ status: res.status },
108
- `Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`));
98
+ return new Promise(((resolve, reject) => {
99
+ u = url.parse(u);
100
+ redirects = redirects || [];
101
+ redirects.push(u.href);
102
+
103
+ get(u, httpOptions)
104
+ .then((res) => {
105
+ if (res.statusCode >= 400) {
106
+ throw ono({ status: res.statusCode }, `HTTP ERROR ${res.statusCode}`);
109
107
  }
110
- else if (!res.headers.location) {
111
- throw ono({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
108
+ else if (res.statusCode >= 300) {
109
+ if (redirects.length > httpOptions.redirects) {
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
+ }
112
121
  }
113
122
  else {
114
- // console.log('HTTP %d redirect %s -> %s', res.status, u.href, res.headers.location);
115
- let redirectTo = url.resolve(u, res.headers.location);
116
- return download(redirectTo, httpOptions, redirects);
123
+ resolve(res.body || Buffer.alloc(0));
117
124
  }
118
- }
119
- else {
120
- return res.body ? res.arrayBuffer().then(buf => Buffer.from(buf)) : Buffer.alloc(0);
121
- }
122
- })
123
- .catch((err) => {
124
- throw new ResolverError(ono(err, `Error downloading ${u.href}`), u.href);
125
- });
125
+ })
126
+ .catch((err) => {
127
+ reject(new ResolverError(ono(err, `Error downloading ${u.href}`), u.href));
128
+ });
129
+ }));
126
130
  }
127
131
 
128
132
  /**
@@ -135,23 +139,42 @@ function download (u, httpOptions, redirects) {
135
139
  * The promise resolves with the HTTP Response object.
136
140
  */
137
141
  function get (u, httpOptions) {
138
- let controller;
139
- let timeoutId;
140
- if (httpOptions.timeout) {
141
- controller = new AbortController();
142
- timeoutId = setTimeout(() => controller.abort(), httpOptions.timeout);
143
- }
142
+ return new Promise(((resolve, reject) => {
143
+ // console.log('GET', u.href);
144
144
 
145
- return fetch(u, {
146
- method: "GET",
147
- headers: httpOptions.headers || {},
148
- credentials: httpOptions.withCredentials ? "include" : "same-origin",
149
- signal: controller ? controller.signal : null,
150
- }).then(response => {
151
- if (timeoutId) {
152
- clearTimeout(timeoutId);
145
+ let protocol = u.protocol === "https:" ? https : http;
146
+ let req = protocol.get({
147
+ hostname: u.hostname,
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
+ });
155
+
156
+ if (typeof req.setTimeout === "function") {
157
+ req.setTimeout(httpOptions.timeout);
153
158
  }
154
159
 
155
- return response;
156
- });
160
+ req.on("timeout", () => {
161
+ req.abort();
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
+ }));
157
180
  }
package/lib/util/url.js CHANGED
@@ -1,9 +1,6 @@
1
1
  "use strict";
2
2
 
3
- const nodePath = require("path");
4
- const projectDir = nodePath.resolve(__dirname, "..", "..");
5
-
6
- let isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined),
3
+ let isWindows = /^win/.test(process.platform),
7
4
  forwardSlashPattern = /\//g,
8
5
  protocolPattern = /^(\w{2,}):\/\//i,
9
6
  url = module.exports,
@@ -25,22 +22,8 @@ let urlDecodePatterns = [
25
22
  /\%40/g, "@"
26
23
  ];
27
24
 
28
- exports.parse = (u) => new URL(u);
29
-
30
- /**
31
- * Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF.
32
- *
33
- * @return {string}
34
- */
35
- exports.resolve = function resolve (from, to) {
36
- let resolvedUrl = new URL(to, new URL(from, "resolve://"));
37
- if (resolvedUrl.protocol === "resolve:") {
38
- // `from` is a relative URL.
39
- let { pathname, search, hash } = resolvedUrl;
40
- return pathname + search + hash;
41
- }
42
- return resolvedUrl.toString();
43
- };
25
+ exports.parse = require("url").parse;
26
+ exports.resolve = require("url").resolve;
44
27
 
45
28
  /**
46
29
  * Returns the current working directory (in Node) or the current page URL (in browsers).
@@ -48,7 +31,7 @@ exports.resolve = function resolve (from, to) {
48
31
  * @returns {string}
49
32
  */
50
33
  exports.cwd = function cwd () {
51
- if (typeof window !== "undefined") {
34
+ if (process.browser) {
52
35
  return location.href;
53
36
  }
54
37
 
@@ -147,7 +130,7 @@ exports.isHttp = function isHttp (path) {
147
130
  }
148
131
  else if (protocol === undefined) {
149
132
  // There is no protocol. If we're running in a browser, then assume it's HTTP.
150
- return typeof window !== "undefined";
133
+ return process.browser;
151
134
  }
152
135
  else {
153
136
  // It's some other protocol, such as "ftp://", "mongodb://", etc.
@@ -193,14 +176,7 @@ exports.fromFileSystemPath = function fromFileSystemPath (path) {
193
176
  // Step 1: On Windows, replace backslashes with forward slashes,
194
177
  // rather than encoding them as "%5C"
195
178
  if (isWindows) {
196
- const hasProjectDir = path.toUpperCase().includes(projectDir.replace(/\\/g, "\\").toUpperCase());
197
- const hasProjectUri = path.toUpperCase().includes(projectDir.replace(/\\/g, "/").toUpperCase());
198
- if (hasProjectDir || hasProjectUri) {
199
- path = path.replace(/\\/g, "/");
200
- }
201
- else {
202
- path = `${projectDir}/${path}`.replace(/\\/g, "/");
203
- }
179
+ path = path.replace(/\\/g, "/");
204
180
  }
205
181
 
206
182
  // Step 2: `encodeURI` will take care of MOST characters
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apidevtools/json-schema-ref-parser",
3
- "version": "9.1.1",
3
+ "version": "9.1.2",
4
4
  "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
5
5
  "keywords": [
6
6
  "json",
@@ -21,10 +21,6 @@
21
21
  "name": "Boris Cherny",
22
22
  "email": "boris@performancejs.com"
23
23
  },
24
- {
25
- "name": "Phil Sturgeon",
26
- "email": "phil@apisyouwonthate.com"
27
- },
28
24
  {
29
25
  "name": "Jakub Rożek",
30
26
  "email": "jakub@stoplight.io"
@@ -36,15 +32,11 @@
36
32
  "url": "https://github.com/APIDevTools/json-schema-ref-parser.git"
37
33
  },
38
34
  "license": "MIT",
39
- "funding": "https://github.com/sponsors/philsturgeon",
40
35
  "main": "lib/index.js",
41
36
  "typings": "lib/index.d.ts",
42
37
  "browser": {
43
38
  "fs": false
44
39
  },
45
- "engines": {
46
- "node": ">= 17"
47
- },
48
40
  "files": [
49
41
  "lib"
50
42
  ],
@@ -54,7 +46,7 @@
54
46
  "lint": "eslint lib test/fixtures test/specs",
55
47
  "test": "npm run test:node && npm run test:typescript && npm run test:browser && npm run lint",
56
48
  "test:node": "mocha",
57
- "test:browser": "cross-env NODE_OPTIONS=--openssl-legacy-provider karma start --single-run",
49
+ "test:browser": "karma start --single-run",
58
50
  "test:typescript": "tsc --noEmit --strict --lib esnext,dom test/specs/typescript-definition.spec.ts",
59
51
  "coverage": "npm run coverage:node && npm run coverage:browser",
60
52
  "coverage:node": "nyc node_modules/mocha/bin/mocha",
@@ -63,21 +55,16 @@
63
55
  },
64
56
  "devDependencies": {
65
57
  "@babel/polyfill": "^7.12.1",
66
- "@chiragrupani/karma-chromium-edge-launcher": "^2.2.2",
67
58
  "@jsdevtools/eslint-config": "^1.0.7",
68
59
  "@jsdevtools/host-environment": "^2.1.2",
69
60
  "@jsdevtools/karma-config": "^3.1.7",
70
61
  "@types/node": "^14.14.21",
71
62
  "chai": "^4.2.0",
72
63
  "chai-subset": "^1.6.0",
73
- "chokidar": "^3.5.3",
74
- "cross-env": "^7.0.3",
75
64
  "eslint": "^7.18.0",
76
- "isomorphic-fetch": "^3.0.0",
77
65
  "karma": "^5.0.2",
78
66
  "karma-cli": "^2.0.0",
79
67
  "mocha": "^8.2.1",
80
- "node-abort-controller": "^3.0.1",
81
68
  "npm-check": "^5.9.0",
82
69
  "nyc": "^15.0.1",
83
70
  "semantic-release-plugin-update-version-in-files": "^1.1.0",
@@ -92,7 +79,12 @@
92
79
  },
93
80
  "release": {
94
81
  "branches": [
95
- "main"
82
+ "main",
83
+ "v9",
84
+ {
85
+ "name": "v9.1.x",
86
+ "range": "9.1.x"
87
+ }
96
88
  ],
97
89
  "plugins": [
98
90
  "@semantic-release/commit-analyzer",