1 |
|
2 |
|
3 |
|
4 |
|
5 | const _ = require('lodash');
|
6 | const Ajv = require('ajv');
|
7 | const blueprintSchema = require('../schema.json');
|
8 | const uuid = require('uuid').v4;
|
9 |
|
10 |
|
11 | const ajv = new Ajv({ allErrors: true });
|
12 | const validate = ajv.compile(blueprintSchema);
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | module.exports = class BlueprintManager {
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | static parse(text ) {
|
34 | const input = JSON.parse(text);
|
35 | this.validateInput(input);
|
36 | const parsed = input.map(this.fillDefaults);
|
37 |
|
38 | return this.isValidTree(parsed)
|
39 |
|
40 | ? this.buildExecutionSequence(parsed)
|
41 | : [];
|
42 | }
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | static fillDefaults(rawItem ) {
|
54 | rawItem.requestId = rawItem.requestId || uuid();
|
55 | if (typeof rawItem.body !== 'undefined') {
|
56 | rawItem.body = JSON.parse(rawItem.body);
|
57 | }
|
58 | const headersObject = rawItem.headers || {};
|
59 | rawItem.headers = Object.keys(headersObject).reduce((carry, key) => {
|
60 | carry.set(key, headersObject[key]);
|
61 | return carry;
|
62 | }, new Map());
|
63 | rawItem.waitFor = rawItem.waitFor || ['<ROOT>'];
|
64 | rawItem._resolved = false;
|
65 |
|
66 | if (
|
67 | rawItem.uri &&
|
68 | rawItem.uri.indexOf('%7B%7B') !== -1 &&
|
69 | rawItem.uri.indexOf('%7D%7D') !== -1
|
70 | ) {
|
71 | rawItem.uri = decodeURIComponent(rawItem.uri);
|
72 | }
|
73 |
|
74 | return rawItem;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | static buildExecutionSequence(parsed ) {
|
91 | const sequence = [
|
92 | parsed.filter(({ waitFor }) => _.difference(waitFor, ['<ROOT>']).length === 0),
|
93 | ];
|
94 | let subreqsWithUnresolvedDeps = parsed.filter(({ waitFor }) =>
|
95 | _.difference(waitFor, ['<ROOT>']).length !== 0);
|
96 |
|
97 |
|
98 |
|
99 | const dependencyIsResolved = ({ waitFor }, seq) =>
|
100 | _.difference(waitFor, this._allSubrequestIds(seq)).length === 0;
|
101 | while (subreqsWithUnresolvedDeps && subreqsWithUnresolvedDeps.length) {
|
102 | const noDeps = subreqsWithUnresolvedDeps.filter(sub =>
|
103 | dependencyIsResolved(sub, sequence));
|
104 | if (noDeps.length === 0) {
|
105 | throw new Error('Waiting for unresolvable request. Abort.');
|
106 | }
|
107 | sequence.push(noDeps);
|
108 | subreqsWithUnresolvedDeps = _.difference(subreqsWithUnresolvedDeps, noDeps);
|
109 | }
|
110 | return sequence;
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | static _allSubrequestIds(sequence ) {
|
125 | const output = new Set(['<ROOT>']);
|
126 | sequence.forEach(subrequests => subrequests
|
127 | .forEach(subrequest => output.add(subrequest.requestId)));
|
128 | return Array.from(output);
|
129 | }
|
130 |
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | static validateInput(parsed ) {
|
143 | const valid = validate(parsed);
|
144 | if (!valid) {
|
145 | const errors = JSON.stringify(validate.errors, null, 2);
|
146 | throw new Error(`The provided blueprint is not valid: ${errors}.`);
|
147 | }
|
148 | }
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | static isValidTree(parsed ) {
|
160 |
|
161 |
|
162 | const isValidItem = (item ) => ([
|
163 | 'requestId',
|
164 | 'waitFor',
|
165 | 'uri',
|
166 | 'action',
|
167 | 'headers',
|
168 | ].reduce((all, key) => all
|
169 | && (typeof item[key] !== 'undefined')
|
170 | && typeof item.requestId === 'string'
|
171 | && Array.isArray(item.waitFor)
|
172 | && typeof item.uri === 'string'
|
173 | && typeof item.headers === 'object'
|
174 | && (typeof item.body === 'undefined' || typeof item.body === 'object')
|
175 | && ['view', 'create', 'update', 'replace', 'delete', 'exists', 'discover']
|
176 | .indexOf(item.action) !== -1, true));
|
177 | return parsed.reduce((valid, rawItem) => valid && isValidItem(rawItem), true);
|
178 | }
|
179 | };
|