kareem 2.3.4 → 2.4.1
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/.eslintrc.json +16 -0
- package/.github/workflows/test.yml +47 -0
- package/CHANGELOG.md +5 -0
- package/docs.js +2 -0
- package/index.js +107 -49
- package/package.json +4 -1
- package/test/examples.test.js +3 -0
- package/test/misc.test.js +1 -1
- package/test/post.test.js +66 -0
- package/test/pre.test.js +19 -0
- package/test/wrap.test.js +101 -0
- package/gulpfile.js +0 -18
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
on:
|
|
3
|
+
pull_request:
|
|
4
|
+
push:
|
|
5
|
+
permissions:
|
|
6
|
+
contents: read
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ${{ matrix.os }}
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
node: [12, 14, 16, 18]
|
|
15
|
+
os: [ubuntu-20.04]
|
|
16
|
+
name: Node ${{ matrix.node }}
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3
|
|
19
|
+
|
|
20
|
+
- name: Setup node
|
|
21
|
+
uses: actions/setup-node@5b52f097d36d4b0b2f94ed6de710023fbb8b2236 # v3.1.0
|
|
22
|
+
with:
|
|
23
|
+
node-version: ${{ matrix.node }}
|
|
24
|
+
|
|
25
|
+
- run: npm install
|
|
26
|
+
|
|
27
|
+
- run: npm test
|
|
28
|
+
|
|
29
|
+
lint:
|
|
30
|
+
runs-on: ${{ matrix.os }}
|
|
31
|
+
strategy:
|
|
32
|
+
fail-fast: false
|
|
33
|
+
matrix:
|
|
34
|
+
node: [18]
|
|
35
|
+
os: [ubuntu-20.04]
|
|
36
|
+
name: Lint
|
|
37
|
+
steps:
|
|
38
|
+
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3
|
|
39
|
+
|
|
40
|
+
- name: Setup node
|
|
41
|
+
uses: actions/setup-node@5b52f097d36d4b0b2f94ed6de710023fbb8b2236 # v3.1.0
|
|
42
|
+
with:
|
|
43
|
+
node-version: ${{ matrix.node }}
|
|
44
|
+
|
|
45
|
+
- run: npm install
|
|
46
|
+
|
|
47
|
+
- run: npm run lint
|
package/CHANGELOG.md
CHANGED
package/docs.js
CHANGED
package/index.js
CHANGED
|
@@ -5,6 +5,22 @@ function Kareem() {
|
|
|
5
5
|
this._posts = new Map();
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
Kareem.skipWrappedFunction = function skipWrappedFunction() {
|
|
9
|
+
if (!(this instanceof Kareem.skipWrappedFunction)) {
|
|
10
|
+
return new Kareem.skipWrappedFunction(...arguments);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
this.args = [...arguments];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
Kareem.overwriteResult = function overwriteResult() {
|
|
17
|
+
if (!(this instanceof Kareem.overwriteResult)) {
|
|
18
|
+
return new Kareem.overwriteResult(...arguments);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
this.args = [...arguments];
|
|
22
|
+
};
|
|
23
|
+
|
|
8
24
|
Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
9
25
|
if (arguments.length === 3) {
|
|
10
26
|
callback = args;
|
|
@@ -17,9 +33,10 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
|
17
33
|
var asyncPresLeft = numAsyncPres;
|
|
18
34
|
var done = false;
|
|
19
35
|
var $args = args;
|
|
36
|
+
var shouldSkipWrappedFunction = null;
|
|
20
37
|
|
|
21
38
|
if (!numPres) {
|
|
22
|
-
return
|
|
39
|
+
return nextTick(function() {
|
|
23
40
|
callback(null);
|
|
24
41
|
});
|
|
25
42
|
}
|
|
@@ -38,11 +55,15 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
|
38
55
|
if (done) {
|
|
39
56
|
return;
|
|
40
57
|
}
|
|
41
|
-
|
|
42
|
-
|
|
58
|
+
if (error instanceof Kareem.skipWrappedFunction) {
|
|
59
|
+
shouldSkipWrappedFunction = error;
|
|
60
|
+
} else {
|
|
61
|
+
done = true;
|
|
62
|
+
return callback(error);
|
|
63
|
+
}
|
|
43
64
|
}
|
|
44
65
|
if (--asyncPresLeft === 0 && currentPre >= numPres) {
|
|
45
|
-
return callback(
|
|
66
|
+
return callback(shouldSkipWrappedFunction);
|
|
46
67
|
}
|
|
47
68
|
})
|
|
48
69
|
];
|
|
@@ -57,25 +78,25 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
|
57
78
|
|
|
58
79
|
callMiddlewareFunction(pre.fn, context, args, args[0]);
|
|
59
80
|
} else {
|
|
60
|
-
let
|
|
81
|
+
let maybePromiseLike = null;
|
|
61
82
|
try {
|
|
62
|
-
|
|
83
|
+
maybePromiseLike = pre.fn.call(context);
|
|
63
84
|
} catch (err) {
|
|
64
85
|
if (err != null) {
|
|
65
86
|
return callback(err);
|
|
66
87
|
}
|
|
67
88
|
}
|
|
68
89
|
|
|
69
|
-
if (
|
|
70
|
-
|
|
90
|
+
if (isPromiseLike(maybePromiseLike)) {
|
|
91
|
+
maybePromiseLike.then(() => _next(), err => _next(err));
|
|
71
92
|
} else {
|
|
72
93
|
if (++currentPre >= numPres) {
|
|
73
94
|
if (asyncPresLeft > 0) {
|
|
74
95
|
// Leave parallel hooks to run
|
|
75
96
|
return;
|
|
76
97
|
} else {
|
|
77
|
-
return
|
|
78
|
-
callback(
|
|
98
|
+
return nextTick(function() {
|
|
99
|
+
callback(shouldSkipWrappedFunction);
|
|
79
100
|
});
|
|
80
101
|
}
|
|
81
102
|
}
|
|
@@ -91,8 +112,12 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
|
91
112
|
if (done) {
|
|
92
113
|
return;
|
|
93
114
|
}
|
|
94
|
-
|
|
95
|
-
|
|
115
|
+
if (error instanceof Kareem.skipWrappedFunction) {
|
|
116
|
+
shouldSkipWrappedFunction = error;
|
|
117
|
+
} else {
|
|
118
|
+
done = true;
|
|
119
|
+
return callback(error);
|
|
120
|
+
}
|
|
96
121
|
}
|
|
97
122
|
|
|
98
123
|
if (++currentPre >= numPres) {
|
|
@@ -100,7 +125,7 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
|
100
125
|
// Leave parallel hooks to run
|
|
101
126
|
return;
|
|
102
127
|
} else {
|
|
103
|
-
return callback(
|
|
128
|
+
return callback(shouldSkipWrappedFunction);
|
|
104
129
|
}
|
|
105
130
|
}
|
|
106
131
|
|
|
@@ -132,7 +157,7 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
132
157
|
}
|
|
133
158
|
|
|
134
159
|
if (!numPosts) {
|
|
135
|
-
return
|
|
160
|
+
return nextTick(function() {
|
|
136
161
|
callback.apply(null, [firstError].concat(args));
|
|
137
162
|
});
|
|
138
163
|
}
|
|
@@ -153,6 +178,13 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
153
178
|
if (post.length === numArgs + 2) {
|
|
154
179
|
const _cb = decorateNextFn(function(error) {
|
|
155
180
|
if (error) {
|
|
181
|
+
if (error instanceof Kareem.overwriteResult) {
|
|
182
|
+
args = error.args;
|
|
183
|
+
if (++currentPost >= numPosts) {
|
|
184
|
+
return callback.call(null, firstError);
|
|
185
|
+
}
|
|
186
|
+
return next();
|
|
187
|
+
}
|
|
156
188
|
firstError = error;
|
|
157
189
|
}
|
|
158
190
|
if (++currentPost >= numPosts) {
|
|
@@ -172,6 +204,13 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
172
204
|
} else {
|
|
173
205
|
const _cb = decorateNextFn(function(error) {
|
|
174
206
|
if (error) {
|
|
207
|
+
if (error instanceof Kareem.overwriteResult) {
|
|
208
|
+
args = error.args;
|
|
209
|
+
if (++currentPost >= numPosts) {
|
|
210
|
+
return callback.apply(null, [null].concat(args));
|
|
211
|
+
}
|
|
212
|
+
return next();
|
|
213
|
+
}
|
|
175
214
|
firstError = error;
|
|
176
215
|
return next();
|
|
177
216
|
}
|
|
@@ -194,16 +233,25 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
194
233
|
callMiddlewareFunction(post, context, newArgs.concat([_cb]), _cb);
|
|
195
234
|
} else {
|
|
196
235
|
let error;
|
|
197
|
-
let
|
|
236
|
+
let maybePromiseLike;
|
|
198
237
|
try {
|
|
199
|
-
|
|
238
|
+
maybePromiseLike = post.apply(context, newArgs);
|
|
200
239
|
} catch (err) {
|
|
201
240
|
error = err;
|
|
202
241
|
firstError = err;
|
|
203
242
|
}
|
|
204
243
|
|
|
205
|
-
if (
|
|
206
|
-
return
|
|
244
|
+
if (isPromiseLike(maybePromiseLike)) {
|
|
245
|
+
return maybePromiseLike.then(
|
|
246
|
+
(res) => {
|
|
247
|
+
_cb(res instanceof Kareem.overwriteResult ? res : null);
|
|
248
|
+
},
|
|
249
|
+
err => _cb(err)
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (maybePromiseLike instanceof Kareem.overwriteResult) {
|
|
254
|
+
args = maybePromiseLike.args;
|
|
207
255
|
}
|
|
208
256
|
|
|
209
257
|
if (++currentPost >= numPosts) {
|
|
@@ -223,8 +271,13 @@ Kareem.prototype.execPostSync = function(name, context, args) {
|
|
|
223
271
|
const numPosts = posts.length;
|
|
224
272
|
|
|
225
273
|
for (let i = 0; i < numPosts; ++i) {
|
|
226
|
-
posts[i].fn.apply(context, args || []);
|
|
274
|
+
const res = posts[i].fn.apply(context, args || []);
|
|
275
|
+
if (res instanceof Kareem.overwriteResult) {
|
|
276
|
+
args = res.args;
|
|
277
|
+
}
|
|
227
278
|
}
|
|
279
|
+
|
|
280
|
+
return args;
|
|
228
281
|
};
|
|
229
282
|
|
|
230
283
|
Kareem.prototype.createWrapperSync = function(name, fn) {
|
|
@@ -234,11 +287,11 @@ Kareem.prototype.createWrapperSync = function(name, fn) {
|
|
|
234
287
|
|
|
235
288
|
var toReturn = fn.apply(this, arguments);
|
|
236
289
|
|
|
237
|
-
kareem.execPostSync(name, this, [toReturn]);
|
|
290
|
+
const result = kareem.execPostSync(name, this, [toReturn]);
|
|
238
291
|
|
|
239
|
-
return
|
|
292
|
+
return result[0];
|
|
240
293
|
};
|
|
241
|
-
}
|
|
294
|
+
};
|
|
242
295
|
|
|
243
296
|
function _handleWrapError(instance, error, name, context, args, options, callback) {
|
|
244
297
|
if (options.useErrorHandlers) {
|
|
@@ -252,16 +305,15 @@ function _handleWrapError(instance, error, name, context, args, options, callbac
|
|
|
252
305
|
|
|
253
306
|
Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
254
307
|
const lastArg = (args.length > 0 ? args[args.length - 1] : null);
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
args;
|
|
308
|
+
let argsWithoutCb = Array.from(args);
|
|
309
|
+
typeof lastArg === 'function' && argsWithoutCb.pop();
|
|
258
310
|
const _this = this;
|
|
259
311
|
|
|
260
312
|
options = options || {};
|
|
261
313
|
const checkForPromise = options.checkForPromise;
|
|
262
314
|
|
|
263
315
|
this.execPre(name, context, args, function(error) {
|
|
264
|
-
if (error) {
|
|
316
|
+
if (error && !(error instanceof Kareem.skipWrappedFunction)) {
|
|
265
317
|
const numCallbackParams = options.numCallbackParams || 0;
|
|
266
318
|
const errorArgs = options.contextParameter ? [context] : [];
|
|
267
319
|
for (var i = errorArgs.length; i < numCallbackParams; ++i) {
|
|
@@ -271,17 +323,22 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
271
323
|
options, lastArg);
|
|
272
324
|
}
|
|
273
325
|
|
|
274
|
-
const end = (typeof lastArg === 'function' ? args.length - 1 : args.length);
|
|
275
326
|
const numParameters = fn.length;
|
|
276
327
|
let ret;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return _cb(
|
|
328
|
+
|
|
329
|
+
if (error instanceof Kareem.skipWrappedFunction) {
|
|
330
|
+
ret = error.args[0];
|
|
331
|
+
return _cb(null, ...error.args);
|
|
332
|
+
} else {
|
|
333
|
+
try {
|
|
334
|
+
ret = fn.apply(context, argsWithoutCb.concat(_cb));
|
|
335
|
+
} catch (err) {
|
|
336
|
+
return _cb(err);
|
|
337
|
+
}
|
|
281
338
|
}
|
|
282
339
|
|
|
283
340
|
if (checkForPromise) {
|
|
284
|
-
if (ret
|
|
341
|
+
if (isPromiseLike(ret)) {
|
|
285
342
|
// Thenable, use it
|
|
286
343
|
return ret.then(
|
|
287
344
|
res => _cb(null, res),
|
|
@@ -291,7 +348,7 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
291
348
|
|
|
292
349
|
// If `fn()` doesn't have a callback argument and doesn't return a
|
|
293
350
|
// promise, assume it is sync
|
|
294
|
-
if (numParameters <
|
|
351
|
+
if (numParameters < argsWithoutCb.length + 1) {
|
|
295
352
|
return _cb(null, ret);
|
|
296
353
|
}
|
|
297
354
|
}
|
|
@@ -308,15 +365,12 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
308
365
|
argsWithoutError, options, lastArg);
|
|
309
366
|
} else {
|
|
310
367
|
_this.execPost(name, context, argsWithoutError, function() {
|
|
311
|
-
if (
|
|
312
|
-
return
|
|
313
|
-
lastArg(arguments[0]) :
|
|
314
|
-
undefined;
|
|
368
|
+
if (lastArg === null) {
|
|
369
|
+
return;
|
|
315
370
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
lastArg.apply(context, arguments)
|
|
319
|
-
undefined;
|
|
371
|
+
arguments[0]
|
|
372
|
+
? lastArg(arguments[0])
|
|
373
|
+
: lastArg.apply(context, arguments);
|
|
320
374
|
});
|
|
321
375
|
}
|
|
322
376
|
}
|
|
@@ -369,7 +423,7 @@ Kareem.prototype.createWrapper = function(name, fn, context, options) {
|
|
|
369
423
|
// Fast path: if there's no hooks for this function, just return the
|
|
370
424
|
// function wrapped in a nextTick()
|
|
371
425
|
return function() {
|
|
372
|
-
|
|
426
|
+
nextTick(() => fn.apply(this, arguments));
|
|
373
427
|
};
|
|
374
428
|
}
|
|
375
429
|
return function() {
|
|
@@ -471,20 +525,20 @@ Kareem.prototype.merge = function(other, clone) {
|
|
|
471
525
|
};
|
|
472
526
|
|
|
473
527
|
function callMiddlewareFunction(fn, context, args, next) {
|
|
474
|
-
let
|
|
528
|
+
let maybePromiseLike;
|
|
475
529
|
try {
|
|
476
|
-
|
|
530
|
+
maybePromiseLike = fn.apply(context, args);
|
|
477
531
|
} catch (error) {
|
|
478
532
|
return next(error);
|
|
479
533
|
}
|
|
480
534
|
|
|
481
|
-
if (
|
|
482
|
-
|
|
535
|
+
if (isPromiseLike(maybePromiseLike)) {
|
|
536
|
+
maybePromiseLike.then(() => next(), err => next(err));
|
|
483
537
|
}
|
|
484
538
|
}
|
|
485
539
|
|
|
486
|
-
function
|
|
487
|
-
return v
|
|
540
|
+
function isPromiseLike(v) {
|
|
541
|
+
return (typeof v === 'object' && v !== null && typeof v.then === 'function');
|
|
488
542
|
}
|
|
489
543
|
|
|
490
544
|
function decorateNextFn(fn) {
|
|
@@ -498,8 +552,12 @@ function decorateNextFn(fn) {
|
|
|
498
552
|
called = true;
|
|
499
553
|
// Make sure to clear the stack so try/catch doesn't catch errors
|
|
500
554
|
// in subsequent middleware
|
|
501
|
-
return
|
|
555
|
+
return nextTick(() => fn.apply(_this, arguments));
|
|
502
556
|
};
|
|
503
557
|
}
|
|
504
558
|
|
|
559
|
+
const nextTick = typeof process === 'object' && process !== null && process.nextTick || function nextTick(cb) {
|
|
560
|
+
setTimeout(cb, 0);
|
|
561
|
+
};
|
|
562
|
+
|
|
505
563
|
module.exports = Kareem;
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kareem",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "Next-generation take on pre/post function hooks",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
+
"lint": "eslint .",
|
|
7
8
|
"test": "mocha ./test/*",
|
|
8
9
|
"test-travis": "nyc mocha ./test/*"
|
|
9
10
|
},
|
|
@@ -12,8 +13,10 @@
|
|
|
12
13
|
"url": "git://github.com/vkarpov15/kareem.git"
|
|
13
14
|
},
|
|
14
15
|
"devDependencies": {
|
|
16
|
+
"@masteringjs/eslint-config": "0.0.1",
|
|
15
17
|
"acquit": "1.x",
|
|
16
18
|
"acquit-ignore": "0.1.x",
|
|
19
|
+
"eslint": "8.15.0",
|
|
17
20
|
"mocha": "9.2.0",
|
|
18
21
|
"nyc": "15.1.0"
|
|
19
22
|
},
|
package/test/examples.test.js
CHANGED
package/test/misc.test.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const Kareem = require('../');
|
|
5
|
+
const { describe, it } = require('mocha');
|
|
5
6
|
|
|
6
7
|
describe('hasHooks', function() {
|
|
7
8
|
it('returns false for toString (Automattic/mongoose#6538)', function() {
|
|
@@ -14,7 +15,6 @@ describe('merge', function() {
|
|
|
14
15
|
it('handles async pres if source doesnt have them', function() {
|
|
15
16
|
const k1 = new Kareem();
|
|
16
17
|
k1.pre('cook', true, function(next, done) {
|
|
17
|
-
execed.first = true;
|
|
18
18
|
setTimeout(
|
|
19
19
|
function() {
|
|
20
20
|
done('error!');
|
package/test/post.test.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const Kareem = require('../');
|
|
5
|
+
const { beforeEach, describe, it } = require('mocha');
|
|
5
6
|
|
|
6
7
|
describe('execPost', function() {
|
|
7
8
|
var hooks;
|
|
@@ -165,6 +166,71 @@ describe('execPost', function() {
|
|
|
165
166
|
done();
|
|
166
167
|
});
|
|
167
168
|
});
|
|
169
|
+
|
|
170
|
+
it('supports overwriteResult', function(done) {
|
|
171
|
+
hooks.post('cook', function(eggs, callback) {
|
|
172
|
+
callback(Kareem.overwriteResult(5));
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
hooks.post('cook', function(eggs, callback) {
|
|
176
|
+
assert.equal(eggs, 5);
|
|
177
|
+
callback();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
var options = {};
|
|
181
|
+
hooks.execPost('cook', null, [4], options, function(error, eggs) {
|
|
182
|
+
assert.equal(eggs, 5);
|
|
183
|
+
done();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('supports sync returning overwriteResult', function(done) {
|
|
188
|
+
hooks.post('cook', function() {
|
|
189
|
+
return Kareem.overwriteResult(5);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
hooks.post('cook', function(eggs, callback) {
|
|
193
|
+
assert.equal(eggs, 5);
|
|
194
|
+
callback();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
var options = {};
|
|
198
|
+
hooks.execPost('cook', null, [4], options, function(error, eggs) {
|
|
199
|
+
assert.ifError(error);
|
|
200
|
+
assert.equal(eggs, 5);
|
|
201
|
+
done();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('supports sync overwriteResult', function() {
|
|
206
|
+
hooks.post('cook', function(eggs) {
|
|
207
|
+
return Kareem.overwriteResult(5);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
hooks.post('cook', function(eggs) {
|
|
211
|
+
assert.equal(eggs, 5);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
var options = {};
|
|
215
|
+
const res = hooks.execPostSync('cook', null, [4], options);
|
|
216
|
+
assert.deepEqual(res, [5]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('supports overwriteResult with promises', function(done) {
|
|
220
|
+
hooks.post('cook', function(eggs) {
|
|
221
|
+
return Promise.resolve(Kareem.overwriteResult(5));
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
hooks.post('cook', function(eggs) {
|
|
225
|
+
assert.equal(eggs, 5);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
var options = {};
|
|
229
|
+
hooks.execPost('cook', null, [4], options, function(error, eggs) {
|
|
230
|
+
assert.equal(eggs, 5);
|
|
231
|
+
done();
|
|
232
|
+
});
|
|
233
|
+
});
|
|
168
234
|
});
|
|
169
235
|
|
|
170
236
|
describe('execPostSync', function() {
|
package/test/pre.test.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const Kareem = require('../');
|
|
5
|
+
const { beforeEach, describe, it } = require('mocha');
|
|
5
6
|
|
|
6
7
|
describe('execPre', function() {
|
|
7
8
|
var hooks;
|
|
@@ -307,6 +308,24 @@ describe('execPre', function() {
|
|
|
307
308
|
done();
|
|
308
309
|
});
|
|
309
310
|
});
|
|
311
|
+
|
|
312
|
+
it('supports skipWrappedFunction', function(done) {
|
|
313
|
+
var execed = {};
|
|
314
|
+
|
|
315
|
+
hooks.pre('cook', function(callback) {
|
|
316
|
+
callback(Kareem.skipWrappedFunction(42));
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
hooks.pre('cook', function() {
|
|
320
|
+
execed.second = true;
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
hooks.execPre('cook', null, function(err) {
|
|
324
|
+
assert.ok(execed.second);
|
|
325
|
+
assert.ok(err instanceof Kareem.skipWrappedFunction);
|
|
326
|
+
done();
|
|
327
|
+
});
|
|
328
|
+
});
|
|
310
329
|
});
|
|
311
330
|
|
|
312
331
|
describe('execPreSync', function() {
|
package/test/wrap.test.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
1
3
|
var assert = require('assert');
|
|
2
4
|
var Kareem = require('../');
|
|
5
|
+
const { beforeEach, describe, it } = require('mocha');
|
|
3
6
|
|
|
4
7
|
describe('wrap()', function() {
|
|
5
8
|
var hooks;
|
|
@@ -287,6 +290,91 @@ describe('wrap()', function() {
|
|
|
287
290
|
25);
|
|
288
291
|
});
|
|
289
292
|
|
|
293
|
+
it('supports overwriteResult', function(done) {
|
|
294
|
+
hooks.post('cook', function(eggs, callback) {
|
|
295
|
+
callback(Kareem.overwriteResult(5));
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
const args = [(err, res) => {
|
|
299
|
+
assert.ifError(err);
|
|
300
|
+
assert.equal(res, 5);
|
|
301
|
+
done();
|
|
302
|
+
}];
|
|
303
|
+
|
|
304
|
+
hooks.wrap(
|
|
305
|
+
'cook',
|
|
306
|
+
function(callback) {
|
|
307
|
+
callback(null, 4);
|
|
308
|
+
},
|
|
309
|
+
null,
|
|
310
|
+
args);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('supports skipWrappedFunction', function(done) {
|
|
314
|
+
const execed = {};
|
|
315
|
+
hooks.pre('cook', function pre(callback) {
|
|
316
|
+
execed.pre = true;
|
|
317
|
+
callback(Kareem.skipWrappedFunction(3));
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
hooks.post('cook', function(res, callback) {
|
|
321
|
+
assert.equal(res, 3);
|
|
322
|
+
execed.post = true;
|
|
323
|
+
callback();
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
const args = [(err, res) => {
|
|
327
|
+
assert.ifError(err);
|
|
328
|
+
assert.equal(res, 3);
|
|
329
|
+
assert.ok(execed.pre);
|
|
330
|
+
assert.ok(execed.post);
|
|
331
|
+
assert.ok(!execed.wrapped);
|
|
332
|
+
done();
|
|
333
|
+
}];
|
|
334
|
+
|
|
335
|
+
hooks.wrap(
|
|
336
|
+
'cook',
|
|
337
|
+
function wrapped(callback) {
|
|
338
|
+
execed.wrapped = true;
|
|
339
|
+
callback();
|
|
340
|
+
},
|
|
341
|
+
null,
|
|
342
|
+
args);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('supports skipWrappedFunction with arguments', function(done) {
|
|
346
|
+
const execed = {};
|
|
347
|
+
hooks.pre('cook', function pre(callback, arg) {
|
|
348
|
+
execed.pre = true;
|
|
349
|
+
assert.strictEqual(arg, 4);
|
|
350
|
+
callback(Kareem.skipWrappedFunction(3));
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
hooks.post('cook', function(res, callback) {
|
|
354
|
+
assert.equal(res, 3);
|
|
355
|
+
execed.post = true;
|
|
356
|
+
callback();
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
const args = [4, (err, res) => {
|
|
360
|
+
assert.ifError(err);
|
|
361
|
+
assert.equal(res, 3);
|
|
362
|
+
assert.ok(execed.pre);
|
|
363
|
+
assert.ok(execed.post);
|
|
364
|
+
assert.ok(!execed.wrapped);
|
|
365
|
+
done();
|
|
366
|
+
}];
|
|
367
|
+
|
|
368
|
+
hooks.wrap(
|
|
369
|
+
'cook',
|
|
370
|
+
function wrapped(arg, callback) {
|
|
371
|
+
execed.wrapped = true;
|
|
372
|
+
callback();
|
|
373
|
+
},
|
|
374
|
+
null,
|
|
375
|
+
args);
|
|
376
|
+
});
|
|
377
|
+
|
|
290
378
|
it('handles post errors with no args', function(done) {
|
|
291
379
|
hooks.pre('cook', function(done) {
|
|
292
380
|
obj.waffles = false;
|
|
@@ -363,4 +451,17 @@ describe('wrap()', function() {
|
|
|
363
451
|
assert.equal(calledFn, 1);
|
|
364
452
|
assert.equal(calledPost, 1);
|
|
365
453
|
});
|
|
454
|
+
|
|
455
|
+
it('sync wrappers with overwriteResult', function() {
|
|
456
|
+
hooks.pre('cook', function() {
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
hooks.post('cook', function() {
|
|
460
|
+
return Kareem.overwriteResult(5);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const wrapper = hooks.createWrapperSync('cook', function() { return 4; });
|
|
464
|
+
|
|
465
|
+
assert.strictEqual(wrapper(), 5);
|
|
466
|
+
});
|
|
366
467
|
});
|
package/gulpfile.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
var gulp = require('gulp');
|
|
2
|
-
var mocha = require('gulp-mocha');
|
|
3
|
-
var config = require('./package.json');
|
|
4
|
-
var jscs = require('gulp-jscs');
|
|
5
|
-
|
|
6
|
-
gulp.task('mocha', function() {
|
|
7
|
-
return gulp.src('./test/*').
|
|
8
|
-
pipe(mocha({ reporter: 'dot' }));
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
gulp.task('jscs', function() {
|
|
12
|
-
return gulp.src('./index.js').
|
|
13
|
-
pipe(jscs(config.jscsConfig));
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
gulp.task('watch', function() {
|
|
17
|
-
gulp.watch('./index.js', ['jscs', 'mocha']);
|
|
18
|
-
});
|