1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
17 |
|
18 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
19 |
|
20 | const NodeCache = require('node-cache');
|
21 |
|
22 | const Template = require('./template');
|
23 |
|
24 | const Logger = require('@accordproject/ergo-compiler').Logger;
|
25 |
|
26 | const rp = require('request-promise-native');
|
27 |
|
28 | const crypto = require('crypto');
|
29 |
|
30 | const stringify = require('json-stable-stringify');
|
31 |
|
32 | const semver = require('semver');
|
33 |
|
34 | const globalTemplateCache = new NodeCache({
|
35 | stdTTL: 600,
|
36 | useClones: false
|
37 | });
|
38 | const globalTemplateIndexCache = new NodeCache({
|
39 | stdTTL: 600,
|
40 | useClones: false
|
41 | });
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | class TemplateLibrary {
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | constructor() {
|
61 | let url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
62 | this.url = url || 'https://templates.accordproject.org';
|
63 | Logger.info('Creating TemplateLibrary for ' + this.url);
|
64 | }
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 | clearCache() {
|
71 | return _asyncToGenerator(function* () {
|
72 | globalTemplateCache.flushAll();
|
73 | globalTemplateIndexCache.flushAll();
|
74 | })();
|
75 | }
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | static filterTemplateIndexLatestVersion(templateIndex) {
|
86 | const result = {};
|
87 | const nameToVersion = {};
|
88 |
|
89 | for (let template of Object.keys(templateIndex)) {
|
90 | const atIndex = template.indexOf('@');
|
91 | const name = template.substring(0, atIndex);
|
92 | const version = template.substring(atIndex + 1);
|
93 | const existingVersion = nameToVersion[name];
|
94 |
|
95 | if (!existingVersion || semver.lt(existingVersion, version)) {
|
96 | nameToVersion[name] = version;
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 | for (let name in nameToVersion) {
|
102 | const id = "".concat(name, "@").concat(nameToVersion[name]);
|
103 | result[id] = templateIndex[id];
|
104 | }
|
105 |
|
106 | return result;
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | static filterTemplateIndexCiceroVersion(templateIndex, ciceroVersion) {
|
120 | const result = {};
|
121 |
|
122 | for (let key of Object.keys(templateIndex)) {
|
123 | const template = templateIndex[key];
|
124 |
|
125 | if (semver.satisfies(ciceroVersion, template.ciceroVersion, {
|
126 | includePrerelease: true
|
127 | })) {
|
128 | result[key] = template;
|
129 | }
|
130 | }
|
131 |
|
132 | return result;
|
133 | }
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | getTemplateIndex(options) {
|
145 | var _this = this;
|
146 |
|
147 | return _asyncToGenerator(function* () {
|
148 | const cacheKey = _this.getTemplateIndexCacheKey(options);
|
149 |
|
150 | const result = globalTemplateIndexCache.get(cacheKey);
|
151 |
|
152 | if (result) {
|
153 | Logger.info('Returning template index from cache');
|
154 | return Promise.resolve(result);
|
155 | }
|
156 |
|
157 | const httpOptions = {
|
158 | uri: "".concat(_this.url, "/template-library.json"),
|
159 | headers: {
|
160 | 'User-Agent': 'clause'
|
161 | },
|
162 | json: true
|
163 |
|
164 | };
|
165 | Logger.info('Loading template library from', httpOptions.uri);
|
166 | return rp(httpOptions).then(templateIndex => {
|
167 | if (options && options.latestVersion) {
|
168 | templateIndex = TemplateLibrary.filterTemplateIndexLatestVersion(templateIndex);
|
169 | }
|
170 |
|
171 | if (options && options.ciceroVersion) {
|
172 | templateIndex = TemplateLibrary.filterTemplateIndexCiceroVersion(templateIndex, options.ciceroVersion);
|
173 | }
|
174 |
|
175 | globalTemplateIndexCache.set(cacheKey, templateIndex);
|
176 | return templateIndex;
|
177 | }).catch(err => {
|
178 | Logger.error('Failed to load template index', err);
|
179 | throw err;
|
180 | });
|
181 | })();
|
182 | }
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | static acceptsURI(templateUri) {
|
191 | return templateUri.startsWith('ap://');
|
192 | }
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | static parseURI(templateUri) {
|
203 | if (!templateUri.startsWith('ap://')) {
|
204 | throw new Error("Unsupported protocol: ".concat(templateUri));
|
205 | }
|
206 |
|
207 | const atIndex = templateUri.indexOf('@');
|
208 | const hashIndex = templateUri.indexOf('#');
|
209 |
|
210 | if (atIndex < 0 || hashIndex < 0) {
|
211 | throw new Error("Invalid template specifier. Must contain @ and #: ".concat(templateUri));
|
212 | }
|
213 |
|
214 | return {
|
215 | protocol: 'ap',
|
216 | templateName: templateUri.substring(5, atIndex),
|
217 | templateVersion: templateUri.substring(atIndex + 1, hashIndex),
|
218 | templateHash: templateUri.substring(hashIndex + 1)
|
219 | };
|
220 | }
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | getTemplate(templateUri) {
|
231 | var _this2 = this;
|
232 |
|
233 | return _asyncToGenerator(function* () {
|
234 | const cacheKey = _this2.getTemplateCacheKey(templateUri);
|
235 |
|
236 | const result = globalTemplateCache.get(cacheKey);
|
237 |
|
238 | if (result) {
|
239 | Logger.info('Returning template from cache', templateUri);
|
240 | return result;
|
241 | }
|
242 |
|
243 | const templateUriInfo = TemplateLibrary.parseURI(templateUri);
|
244 | const templateIndex = yield _this2.getTemplateIndex();
|
245 | const templateMetadata = templateIndex["".concat(templateUriInfo.templateName, "@").concat(templateUriInfo.templateVersion)];
|
246 |
|
247 | if (!templateMetadata) {
|
248 | throw new Error("Failed to find template ".concat(templateUri));
|
249 | }
|
250 |
|
251 |
|
252 | const template = yield Template.fromUrl(templateMetadata.url);
|
253 |
|
254 | const templateHash = template.getHash();
|
255 |
|
256 | if (templateHash !== templateUriInfo.templateHash) {
|
257 | Logger.warn("Requested template ".concat(templateUri, " but the hash of the template is ").concat(templateHash));
|
258 | }
|
259 |
|
260 | globalTemplateCache.set(cacheKey, template);
|
261 | return template;
|
262 | })();
|
263 | }
|
264 | |
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | getTemplateIndexCacheKey(options) {
|
272 | let prefix = '';
|
273 |
|
274 | if (options) {
|
275 | const hasher = crypto.createHash('sha256');
|
276 | hasher.update(stringify(options));
|
277 | prefix = "".concat(hasher.digest('hex'), "-");
|
278 | }
|
279 |
|
280 | return "".concat(this.url, "/").concat(prefix, "template-library.json");
|
281 | }
|
282 | |
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | getTemplateCacheKey(templateUri) {
|
290 | return "".concat(this.url, "/").concat(templateUri);
|
291 | }
|
292 |
|
293 | }
|
294 |
|
295 | module.exports = TemplateLibrary; |
\ | No newline at end of file |