kareem 2.3.4 → 2.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +33 -34
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -19,7 +19,7 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
|
19
19
|
var $args = args;
|
|
20
20
|
|
|
21
21
|
if (!numPres) {
|
|
22
|
-
return
|
|
22
|
+
return nextTick(function() {
|
|
23
23
|
callback(null);
|
|
24
24
|
});
|
|
25
25
|
}
|
|
@@ -57,24 +57,24 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
|
57
57
|
|
|
58
58
|
callMiddlewareFunction(pre.fn, context, args, args[0]);
|
|
59
59
|
} else {
|
|
60
|
-
let
|
|
60
|
+
let maybePromiseLike = null;
|
|
61
61
|
try {
|
|
62
|
-
|
|
62
|
+
maybePromiseLike = pre.fn.call(context);
|
|
63
63
|
} catch (err) {
|
|
64
64
|
if (err != null) {
|
|
65
65
|
return callback(err);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
if (
|
|
70
|
-
|
|
69
|
+
if (isPromiseLike(maybePromiseLike)) {
|
|
70
|
+
maybePromiseLike.then(() => _next(), err => _next(err));
|
|
71
71
|
} else {
|
|
72
72
|
if (++currentPre >= numPres) {
|
|
73
73
|
if (asyncPresLeft > 0) {
|
|
74
74
|
// Leave parallel hooks to run
|
|
75
75
|
return;
|
|
76
76
|
} else {
|
|
77
|
-
return
|
|
77
|
+
return nextTick(function() {
|
|
78
78
|
callback(null);
|
|
79
79
|
});
|
|
80
80
|
}
|
|
@@ -132,7 +132,7 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
if (!numPosts) {
|
|
135
|
-
return
|
|
135
|
+
return nextTick(function() {
|
|
136
136
|
callback.apply(null, [firstError].concat(args));
|
|
137
137
|
});
|
|
138
138
|
}
|
|
@@ -194,16 +194,16 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
194
194
|
callMiddlewareFunction(post, context, newArgs.concat([_cb]), _cb);
|
|
195
195
|
} else {
|
|
196
196
|
let error;
|
|
197
|
-
let
|
|
197
|
+
let maybePromiseLike;
|
|
198
198
|
try {
|
|
199
|
-
|
|
199
|
+
maybePromiseLike = post.apply(context, newArgs);
|
|
200
200
|
} catch (err) {
|
|
201
201
|
error = err;
|
|
202
202
|
firstError = err;
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
if (
|
|
206
|
-
return
|
|
205
|
+
if (isPromiseLike(maybePromiseLike)) {
|
|
206
|
+
return maybePromiseLike.then(() => _cb(), err => _cb(err));
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
if (++currentPost >= numPosts) {
|
|
@@ -252,9 +252,8 @@ function _handleWrapError(instance, error, name, context, args, options, callbac
|
|
|
252
252
|
|
|
253
253
|
Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
254
254
|
const lastArg = (args.length > 0 ? args[args.length - 1] : null);
|
|
255
|
-
const argsWithoutCb =
|
|
256
|
-
|
|
257
|
-
args;
|
|
255
|
+
const argsWithoutCb = Array.from(args);
|
|
256
|
+
typeof lastArg === 'function' && argsWithoutCb.pop();
|
|
258
257
|
const _this = this;
|
|
259
258
|
|
|
260
259
|
options = options || {};
|
|
@@ -271,17 +270,16 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
271
270
|
options, lastArg);
|
|
272
271
|
}
|
|
273
272
|
|
|
274
|
-
const end = (typeof lastArg === 'function' ? args.length - 1 : args.length);
|
|
275
273
|
const numParameters = fn.length;
|
|
276
274
|
let ret;
|
|
277
275
|
try {
|
|
278
|
-
ret = fn.apply(context,
|
|
276
|
+
ret = fn.apply(context, argsWithoutCb.concat(_cb));
|
|
279
277
|
} catch (err) {
|
|
280
278
|
return _cb(err);
|
|
281
279
|
}
|
|
282
280
|
|
|
283
281
|
if (checkForPromise) {
|
|
284
|
-
if (ret
|
|
282
|
+
if (isPromiseLike(ret)) {
|
|
285
283
|
// Thenable, use it
|
|
286
284
|
return ret.then(
|
|
287
285
|
res => _cb(null, res),
|
|
@@ -291,7 +289,7 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
291
289
|
|
|
292
290
|
// If `fn()` doesn't have a callback argument and doesn't return a
|
|
293
291
|
// promise, assume it is sync
|
|
294
|
-
if (numParameters <
|
|
292
|
+
if (numParameters < argsWithoutCb.length + 1) {
|
|
295
293
|
return _cb(null, ret);
|
|
296
294
|
}
|
|
297
295
|
}
|
|
@@ -308,15 +306,12 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
308
306
|
argsWithoutError, options, lastArg);
|
|
309
307
|
} else {
|
|
310
308
|
_this.execPost(name, context, argsWithoutError, function() {
|
|
311
|
-
if (
|
|
312
|
-
return
|
|
313
|
-
lastArg(arguments[0]) :
|
|
314
|
-
undefined;
|
|
309
|
+
if (lastArg === null) {
|
|
310
|
+
return;
|
|
315
311
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
lastArg.apply(context, arguments)
|
|
319
|
-
undefined;
|
|
312
|
+
arguments[0]
|
|
313
|
+
? lastArg(arguments[0])
|
|
314
|
+
: lastArg.apply(context, arguments)
|
|
320
315
|
});
|
|
321
316
|
}
|
|
322
317
|
}
|
|
@@ -369,7 +364,7 @@ Kareem.prototype.createWrapper = function(name, fn, context, options) {
|
|
|
369
364
|
// Fast path: if there's no hooks for this function, just return the
|
|
370
365
|
// function wrapped in a nextTick()
|
|
371
366
|
return function() {
|
|
372
|
-
|
|
367
|
+
nextTick(() => fn.apply(this, arguments));
|
|
373
368
|
};
|
|
374
369
|
}
|
|
375
370
|
return function() {
|
|
@@ -471,20 +466,20 @@ Kareem.prototype.merge = function(other, clone) {
|
|
|
471
466
|
};
|
|
472
467
|
|
|
473
468
|
function callMiddlewareFunction(fn, context, args, next) {
|
|
474
|
-
let
|
|
469
|
+
let maybePromiseLike;
|
|
475
470
|
try {
|
|
476
|
-
|
|
471
|
+
maybePromiseLike = fn.apply(context, args);
|
|
477
472
|
} catch (error) {
|
|
478
473
|
return next(error);
|
|
479
474
|
}
|
|
480
475
|
|
|
481
|
-
if (
|
|
482
|
-
|
|
476
|
+
if (isPromiseLike(maybePromiseLike)) {
|
|
477
|
+
maybePromiseLike.then(() => next(), err => next(err));
|
|
483
478
|
}
|
|
484
479
|
}
|
|
485
480
|
|
|
486
|
-
function
|
|
487
|
-
return v
|
|
481
|
+
function isPromiseLike(v) {
|
|
482
|
+
return (typeof v === 'object' && v !== null && typeof v.then === 'function');
|
|
488
483
|
}
|
|
489
484
|
|
|
490
485
|
function decorateNextFn(fn) {
|
|
@@ -498,8 +493,12 @@ function decorateNextFn(fn) {
|
|
|
498
493
|
called = true;
|
|
499
494
|
// Make sure to clear the stack so try/catch doesn't catch errors
|
|
500
495
|
// in subsequent middleware
|
|
501
|
-
return
|
|
496
|
+
return nextTick(() => fn.apply(_this, arguments));
|
|
502
497
|
};
|
|
503
498
|
}
|
|
504
499
|
|
|
500
|
+
const nextTick = typeof process === 'object' && process !== null && process.nextTick || function nextTick(cb) {
|
|
501
|
+
setTimeout(cb, 0);
|
|
502
|
+
}
|
|
503
|
+
|
|
505
504
|
module.exports = Kareem;
|