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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | 1× 1× 33× 33× 33× 33× 34× 34× 34× 34× 34× 34× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 2× 22× 22× 22× 22× 1× 22× 44× 44× 44× 2× 2× 44× 44× 44× 44× 23× 23× 23× 21× 22× 1× 23× 23× 23× 23× 23× 41× 41× 41× 41× 41× 28× 28× 28× 5× 5× 5× 5× 13× 13× 6× 13× 23× 23× 1× 23× 13× 10× 1× 41× 41× 41× 11× 11× 11× 11× 11× 11× 4× 3× 1× 41× 34× 34× 34× 34× 34× 1× 2× 33× 33× 33× 33× 44× 44× 2× 4× 42× 42× 42× 42× 75× 34× 1× 44× 1× | 'use strict'; const is = require('is'); const arrify = require('arrify'); class HooksPromise { static wrap(scope) { scope.pre = this.pre; scope.post = this.post; scope.hook = this.hook; scope.__setupHooks = this.__setupHooks; } static hook(name, originalMethod) { const proto = this; const presHooks = proto.__pres = proto.__pres || {}; const postsHooks = proto.__posts = proto.__posts || {}; presHooks[name] = presHooks[name] || []; postsHooks[name] = postsHooks[name] || []; // We wrapp the original method with he hooked logig proto[name] = function wrapper() { const passedArgs = Array.prototype.slice.apply(arguments); const self = this; /** * Array or pre hooks */ const pres = this.__pres[name]; /** * Array of post hooks */ const posts = this.__posts[name]; /** * Total pre hooks */ const totalPres = pres.length; /** * Total posts hooks */ const totalPost = posts.length; /** * Scope for the hooks */ let scope; /** * Current hook being processed */ let currentPre = -1; /** * arguments eventually passed to the hook - are mutable */ let hookArgs; let resolveFn; let rejectFn; if (self.preHooksEnabled === false) { return done.apply(self, passedArgs); } return new Promise((resolve, reject) => { resolveFn = resolve; rejectFn = reject; /** * Error Handler */ const handleError = function handleError(err) { reject(err); }; /** * Middleware (hook) wrapper */ const nextPreHook = function nextPreHook() { let args = Array.prototype.slice.apply(arguments); /** * If the current hook is overwriting the arguments * of the original method */ let override = false; if (is.object(args[0]) && {}.hasOwnProperty.call(args[0], '__override')) { args = arrify(args[0].__override); override = true; } // If there is a __scopeHook function on the object // we call it to get the scope wanted for the hook scope = getScope(self, name, args); /** * Reference to current pre hook */ let currentHook; hookArgs = override ? args : passedArgs; if (currentPre + 1 < totalPres) { currentPre += 1; currentHook = pres[currentPre]; return currentHook.apply(scope, hookArgs).then(nextPreHook, handleError); } return done.apply(scope, hookArgs); }; return nextPreHook.apply(this, passedArgs); }); function done() { const targetMethodArgs = Array.prototype.slice.apply(arguments); resolveFn = resolveFn || function resolve(data) { return Promise.resolve(data); }; let targetMethodResponse; let currentPost = -1; const nextPostHook = function nextPostHook(res) { // we save the target method response targetMethodResponse = targetMethodResponse || res; const { arg, override } = parsePostArgs(res, targetMethodResponse); const hookArg = override ? arg : targetMethodResponse; targetMethodResponse = hookArg; if (currentPost + 1 < totalPost && self.__hooksEnabled !== false) { /** * Recursively call all the post hooks */ currentPost += 1; // Reference to current post hook const currPost = posts[currentPost]; // Call nextPostHook return currPost.call(scope, hookArg).then(nextPostHook, (err) => { // ---------------------------- // Error handling // ---------------------------- const resOverride = { __override: { result: arg, }, }; // create errors Array resOverride.__override.errorsPostHook = resOverride.errorsPostHook || []; resOverride.__override.errorsPostHook.push(err); return nextPostHook(resOverride); }); } // Resolve... we're done! :) let resolveResponse = hookArg; if (is.object(hookArg) && {}.hasOwnProperty.call(hookArg, '__override')) { resolveResponse = hookArg.__override; } return resolveFn(resolveResponse); }; // We execute the actual (target) method let responsePromised = originalMethod.apply(self, targetMethodArgs); if (responsePromised.constructor.name !== 'Promise') { // If the response from the target method is not a promise // we convert it to a Promise responsePromised = Promise.resolve(responsePromised); } // If there are post hooks, we chain the response with the hook if (totalPost > 0) { return responsePromised.then(nextPostHook, rejectFn); } // no "post" hook, we're done! return responsePromised.then(resolveFn, rejectFn); } function parsePostArgs(_arg, originalResponse) { let arg = _arg; let override = false; if (is.object(arg) && {}.hasOwnProperty.call(arg, '__override')) { arg = Object.assign({}, _arg); override = true; const isObject = is.object(originalResponse); const hasOverride = isObject && {}.hasOwnProperty.call(originalResponse, '__override'); const hasErrorsPostHooks = hasOverride && {}.hasOwnProperty.call(originalResponse.__override, 'errorsPostHook'); if (isObject && hasOverride && hasErrorsPostHooks) { // The response has been overriden in previous hook if (is.object(arg.__override)) { arg.__override.errorsPostHook = originalResponse.__override.errorsPostHook; } else { arg.__override = { result: arg.__override, errorsPostHook: originalResponse.__override.errorsPostHook, }; } } } return { arg, override }; } }; proto[name].__numAsyncPres = 0; proto[name].__hooked = true; return this; } static pre(name, fn) { const proto = this; if (is.array(fn)) { return fn.forEach((middleware) => { proto.pre.call(proto, name, middleware); }); } const pres = proto.__pres = proto.__pres || {}; proto.__setupHooks(proto, name); pres[name].push(fn); return proto; } static post(name, fn) { const proto = this; if (is.array(fn)) { return fn.forEach((middleware) => { proto.post.call(proto, name, middleware); }); } const posts = proto.__posts = proto.__posts || {}; proto.__setupHooks(proto, name); posts[name].push(fn); return proto; } static __setupHooks(proto, methodName) { if (proto[methodName] && proto[methodName].__hooked !== true) { this.hook(methodName, proto[methodName]); } } } // Helpers function getScope(self, hookName, args) { return self.__scopeHook && typeof self.__scopeHook === 'function' && typeof self.__scopeHook(hookName, args) !== 'undefined' ? self.__scopeHook(hookName, args) : self; } module.exports = HooksPromise; |