/
middlewareEngine.js
212 lines (155 loc) · 5.18 KB
/
middlewareEngine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
/*
* MIDDLEWARE ENGINE
*/
const extender = require(`object-extender`);
module.exports = class MiddlewareEngine {
/*
* Instantiate a new middleware engine.
*/
constructor (_config) {
// These configuration options change the behaviour of the middleware engine.
this.config = extender.defaults({
chainMiddlewareResults: false,
throwOnMissingHandler: true,
throwOnMissingDependency: true,
}, _config);
this.requirements = [];
this.injected = {};
this.handlers = {};
this.middleware = [];
}
/*
* Returns an array of dependency names this module requires, or any empty array.
*/
requires () {
return this.requirements || [];
}
/*
* Allows dependencies to be injected after the engine has been initialised.
*/
inject (key, dependency) {
this.injected[key] = dependency;
}
/*
* Returns true if all the required dependencies have been injected.
*/
areDependenciesSatisfied () {
const requirements = this.requires();
const missingDependency = requirements.some(requirement => !this.hasDependency(requirement));
return !missingDependency;
}
/*
* Returns true if the given dependency has been injected.
*/
hasDependency (key) {
return Boolean(this.injected[key]);
}
/*
* Returns a pointer to the given injected dependency, if any.
*/
__dep (key, ignoreMissing = false) {
const dependency = this.injected[key];
if (!dependency && this.config.throwOnMissingDependency && !ignoreMissing) {
throw new Error(`MISSING_DEPENDENCY`);
}
return dependency;
}
/*
* Store handler middleware for later execution.
*/
configure (handlerId, ..._funcs) {
let functionList;
try {
functionList = this.__reduceToFunctionList(_funcs);
}
catch (err) {
throw new Error(`One of the parameters you provided for the handler "${handlerId}" is not a function or falsy.`);
}
if (!functionList || !functionList.length) {
throw new Error(`You must provide at least one function for the handler "${handlerId}".`);
}
this.handlers[handlerId] = functionList;
}
/*
* Returns true if the given handler ID has some middleware functions attached.
*/
isConfigured (handlerId) {
return Boolean(this.handlers[handlerId]);
}
/*
* Store general middleware for later execution.
*/
use (..._funcs) {
let functionList;
try {
functionList = this.__reduceToFunctionList(_funcs);
}
catch (err) {
throw new Error(`One of the parameters you provided for to the .use() method is not a function or falsy.`);
}
if (!functionList || !functionList.length) {
throw new Error(`You must provide at least one function to the .use() method.`);
}
this.middleware.push(...functionList);
}
/*
* Takes an array of parameters and returns just thopse that are functions.
*/
__reduceToFunctionList (array) {
const functionList = [];
array.forEach(func => {
if (typeof func === `function`) { functionList.push(func); }
else if (func) { throw new Error(`NOT_A_FUNCTION`); }
});
return functionList;
}
/*
* Executes all the middleware functions provided for the given handler ID.
*/
__executeHandler (handlerId, primaryValue, ...otherArgs) {
const handlerFunctions = this.handlers[handlerId];
if (!handlerFunctions && this.config.throwOnMissingHandler) {
throw new Error(`The handler "${handlerId}" has not been configured.`);
}
return this.__executeMultipleFunctions(handlerFunctions, primaryValue, ...otherArgs);
}
/*
* Executes all the general middleware functions that are not handlers.
*/
__executeMiddleware (primaryValue, ...otherArgs) {
return this.__executeMultipleFunctions(this.middleware, primaryValue, ...otherArgs);
}
/*
* Executes a multiple middleware functions in the order provided.
*/
__executeMultipleFunctions (executableFuncs, primaryValue, ...otherArgs) {
const startsWith = Promise.resolve();
const promiseChain = executableFuncs.reduce(
(chain, func) => chain.then(prevResult => this.__executeFunction(func, primaryValue, otherArgs, prevResult)),
startsWith
);
return promiseChain
.catch(err => {
if (typeof err !== `string` || err !== `MIDDLEWARE_ENGINE_BREAK`) { throw err; } // Allow chain breaking when the "stop()" method is called.
});
}
/*
* Executes a single middleware function.
*/
__executeFunction (executableFunc, primaryValue, _otherArgs, prevResult) {
const promise = new Promise((resolve, reject) => {
// Callbacks.
const next = (err, result) => (err ? reject(err) : resolve(result));
const stop = err => reject(err || `MIDDLEWARE_ENGINE_BREAK`);
// Do we pass the previous result as a parameter to the next middleware?
const otherArgs = (this.config.chainMiddlewareResults ? [..._otherArgs, prevResult] : [..._otherArgs]);
// Execute the next middleware function and convert its return value into a promise in case it's an async func.
const middlewareReturnValue = executableFunc.call(this, primaryValue, ...otherArgs, next, stop);
const middlewareReturnPromise = Promise.resolve(middlewareReturnValue);
// Return the actual (top-level) promise we are waiting on, or reject it if an error occured.
return middlewareReturnPromise.then(() => promise).catch(reject);
});
return promise;
}
};