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