1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | require("cross-fetch/polyfill");
|
5 | var debug_1 = tslib_1.__importDefault(require("debug"));
|
6 | var https_proxy_agent_1 = tslib_1.__importDefault(require("https-proxy-agent"));
|
7 | var utils_1 = require("../utils");
|
8 | var jsonSchema_1 = require("./jsonSchema");
|
9 | var schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
|
10 | var debug = debug_1.default('dtsgen');
|
11 | var ReferenceResolver = (function () {
|
12 | function ReferenceResolver() {
|
13 | this.schemaCache = new Map();
|
14 | this.referenceCache = new Map();
|
15 | }
|
16 | ReferenceResolver.prototype.dereference = function (refId) {
|
17 | var result = this.referenceCache.get(refId);
|
18 | if (result == null) {
|
19 | throw new Error('Target reference is not found: ' + refId);
|
20 | }
|
21 | return result;
|
22 | };
|
23 | ReferenceResolver.prototype.getAllRegisteredSchema = function () {
|
24 | return this.schemaCache.values();
|
25 | };
|
26 | ReferenceResolver.prototype.resolve = function () {
|
27 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
28 | var error, _a, _b, _c, key, schema, id, fileId, result, refSchema, e_1, rootSchema, targetSchema, e_2_1;
|
29 | var e_2, _d;
|
30 | return tslib_1.__generator(this, function (_e) {
|
31 | switch (_e.label) {
|
32 | case 0:
|
33 | debug("resolve reference: reference schema count=" + this.referenceCache.size + ".");
|
34 | error = [];
|
35 | _e.label = 1;
|
36 | case 1:
|
37 | _e.trys.push([1, 10, 11, 12]);
|
38 | _a = tslib_1.__values(this.referenceCache.entries()), _b = _a.next();
|
39 | _e.label = 2;
|
40 | case 2:
|
41 | if (!!_b.done) return [3, 9];
|
42 | _c = tslib_1.__read(_b.value, 2), key = _c[0], schema = _c[1];
|
43 | if (schema != null) {
|
44 | return [3, 8];
|
45 | }
|
46 | id = new schemaId_1.default(key);
|
47 | fileId = id.getFileId();
|
48 | result = this.schemaCache.get(id.getAbsoluteId());
|
49 | if (!(result == null)) return [3, 7];
|
50 | refSchema = this.schemaCache.get(fileId);
|
51 | debug("get from schema cache, fileId=" + fileId + ", exists=" + (refSchema != null) + ", " + id.getAbsoluteId());
|
52 | if (!(refSchema == null && id.isFetchable())) return [3, 6];
|
53 | _e.label = 3;
|
54 | case 3:
|
55 | _e.trys.push([3, 5, , 6]);
|
56 | debug("fetch remote schema: id=[" + fileId + "].");
|
57 | return [4, this.registerRemoteSchema(fileId)];
|
58 | case 4:
|
59 | _e.sent();
|
60 | return [3, 6];
|
61 | case 5:
|
62 | e_1 = _e.sent();
|
63 | error.push("Fail to fetch the $ref target: " + id.getAbsoluteId() + ", " + e_1);
|
64 | return [3, 8];
|
65 | case 6:
|
66 | result = this.schemaCache.get(id.getAbsoluteId());
|
67 | _e.label = 7;
|
68 | case 7:
|
69 | debug("resolve reference: ref=[" + id.getAbsoluteId() + "]");
|
70 | if (result != null) {
|
71 | this.referenceCache.set(id.getAbsoluteId(), result);
|
72 | }
|
73 | else {
|
74 | if (id.existsJsonPointerHash()) {
|
75 | rootSchema = this.searchParentSchema(id);
|
76 | if (rootSchema == null) {
|
77 | error.push("The $ref targets root is not found: " + id.getAbsoluteId());
|
78 | return [3, 8];
|
79 | }
|
80 | targetSchema = jsonSchema_1.getSubSchema(rootSchema, id.getJsonPointerHash(), id);
|
81 | this.addSchema(targetSchema);
|
82 | this.registerSchema(targetSchema);
|
83 | this.referenceCache.set(id.getAbsoluteId(), targetSchema);
|
84 | }
|
85 | else {
|
86 | error.push("The $ref target is not found: " + id.getAbsoluteId());
|
87 | return [3, 8];
|
88 | }
|
89 | }
|
90 | _e.label = 8;
|
91 | case 8:
|
92 | _b = _a.next();
|
93 | return [3, 2];
|
94 | case 9: return [3, 12];
|
95 | case 10:
|
96 | e_2_1 = _e.sent();
|
97 | e_2 = { error: e_2_1 };
|
98 | return [3, 12];
|
99 | case 11:
|
100 | try {
|
101 | if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
102 | }
|
103 | finally { if (e_2) throw e_2.error; }
|
104 | return [7];
|
105 | case 12:
|
106 | if (error.length > 0) {
|
107 | throw new Error(error.join('\n'));
|
108 | }
|
109 | return [2];
|
110 | }
|
111 | });
|
112 | });
|
113 | };
|
114 | ReferenceResolver.prototype.searchParentSchema = function (id) {
|
115 | var e_3, _a;
|
116 | var fileId = id.getFileId();
|
117 | var rootSchema = this.schemaCache.get(fileId);
|
118 | if (rootSchema != null) {
|
119 | return rootSchema;
|
120 | }
|
121 | var key = id.getAbsoluteId();
|
122 | try {
|
123 | for (var _b = tslib_1.__values(this.schemaCache.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
124 | var k = _c.value;
|
125 | if (key.startsWith(k)) {
|
126 | var s = this.schemaCache.get(k);
|
127 | if (s != null && s.rootSchema != null) {
|
128 | return s.rootSchema;
|
129 | }
|
130 | }
|
131 | }
|
132 | }
|
133 | catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
134 | finally {
|
135 | try {
|
136 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
137 | }
|
138 | finally { if (e_3) throw e_3.error; }
|
139 | }
|
140 | return;
|
141 | };
|
142 | ReferenceResolver.prototype.noProxy = function (url) {
|
143 | var e_4, _a;
|
144 | if (process.env.NO_PROXY) {
|
145 | try {
|
146 | for (var _b = tslib_1.__values(process.env.NO_PROXY.split(/[, ]+/)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
147 | var domain = _c.value;
|
148 | if (url.hostname.endsWith(domain)) {
|
149 | return true;
|
150 | }
|
151 | }
|
152 | }
|
153 | catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
154 | finally {
|
155 | try {
|
156 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
157 | }
|
158 | finally { if (e_4) throw e_4.error; }
|
159 | }
|
160 | }
|
161 | return false;
|
162 | };
|
163 | ReferenceResolver.prototype.registerRemoteSchema = function (url) {
|
164 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
165 | var fetchOptions, parsedUrl, proxyUrl, agentOptions, res, body, content, schema;
|
166 | return tslib_1.__generator(this, function (_a) {
|
167 | switch (_a.label) {
|
168 | case 0:
|
169 | fetchOptions = {};
|
170 | parsedUrl = new URL(url);
|
171 | if (!this.noProxy(parsedUrl)) {
|
172 | if (parsedUrl.protocol === 'http:' && process.env.HTTP_PROXY) {
|
173 | proxyUrl = new URL(process.env.HTTP_PROXY);
|
174 | }
|
175 | else if (parsedUrl.protocol === 'https:' && process.env.HTTPS_PROXY) {
|
176 | proxyUrl = new URL(process.env.HTTPS_PROXY);
|
177 | }
|
178 | }
|
179 | if (proxyUrl) {
|
180 | agentOptions = {};
|
181 | agentOptions.protocol = proxyUrl.protocol;
|
182 | agentOptions.host = proxyUrl.hostname;
|
183 | agentOptions.port = proxyUrl.port;
|
184 | if (proxyUrl.username) {
|
185 | agentOptions.auth = proxyUrl.username + ':' + proxyUrl.password;
|
186 | }
|
187 | fetchOptions.agent = https_proxy_agent_1.default(agentOptions);
|
188 | }
|
189 | return [4, fetch(url, fetchOptions)];
|
190 | case 1:
|
191 | res = _a.sent();
|
192 | return [4, res.text()];
|
193 | case 2:
|
194 | body = _a.sent();
|
195 | if (!res.ok) {
|
196 | throw new Error("Error on fetch from url(" + url + "): " + res.status + ", " + body);
|
197 | }
|
198 | content = utils_1.parseFileContent(body, url);
|
199 | schema = jsonSchema_1.parseSchema(content, url);
|
200 | this.registerSchema(schema);
|
201 | return [2];
|
202 | }
|
203 | });
|
204 | });
|
205 | };
|
206 | ReferenceResolver.prototype.registerSchema = function (schema) {
|
207 | var _this = this;
|
208 | debug("register schema: schemaId=[" + schema.id.getAbsoluteId() + "].");
|
209 | jsonSchema_1.searchAllSubSchema(schema, function (subSchema) {
|
210 | _this.addSchema(subSchema);
|
211 | }, function (refId) {
|
212 | _this.addReference(refId);
|
213 | });
|
214 | };
|
215 | ReferenceResolver.prototype.addSchema = function (schema) {
|
216 | var id = schema.id;
|
217 | var key = id.getAbsoluteId();
|
218 | if (!this.schemaCache.has(key)) {
|
219 | debug(" add schema: id=" + key);
|
220 | this.schemaCache.set(key, schema);
|
221 | if (schema.rootSchema == null) {
|
222 | var fileId = id.getFileId();
|
223 | if (!this.schemaCache.has(fileId)) {
|
224 | this.schemaCache.set(fileId, schema);
|
225 | }
|
226 | }
|
227 | }
|
228 | };
|
229 | ReferenceResolver.prototype.addReference = function (refId) {
|
230 | if (!this.referenceCache.has(refId.getAbsoluteId())) {
|
231 | debug(" add reference: id=" + refId.getAbsoluteId());
|
232 | this.referenceCache.set(refId.getAbsoluteId(), undefined);
|
233 | }
|
234 | };
|
235 | ReferenceResolver.prototype.clear = function () {
|
236 | debug('clear resolver cache.');
|
237 | this.schemaCache.clear();
|
238 | this.referenceCache.clear();
|
239 | };
|
240 | return ReferenceResolver;
|
241 | }());
|
242 | exports.default = ReferenceResolver;
|
243 |
|
\ | No newline at end of file |