UNPKG

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