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