kareem 2.4.1 → 2.5.0
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/LICENSE +1 -1
- package/index.js +154 -52
- package/package.json +12 -6
- package/.eslintrc.json +0 -16
- package/.github/workflows/test.yml +0 -47
- package/.travis.yml +0 -12
- package/CHANGELOG.md +0 -808
- package/Makefile +0 -5
- package/docs.js +0 -41
- package/test/examples.test.js +0 -429
- package/test/misc.test.js +0 -71
- package/test/post.test.js +0 -264
- package/test/pre.test.js +0 -359
- package/test/wrap.test.js +0 -467
package/test/wrap.test.js
DELETED
|
@@ -1,467 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var assert = require('assert');
|
|
4
|
-
var Kareem = require('../');
|
|
5
|
-
const { beforeEach, describe, it } = require('mocha');
|
|
6
|
-
|
|
7
|
-
describe('wrap()', function() {
|
|
8
|
-
var hooks;
|
|
9
|
-
|
|
10
|
-
beforeEach(function() {
|
|
11
|
-
hooks = new Kareem();
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('handles pre errors', function(done) {
|
|
15
|
-
hooks.pre('cook', function(done) {
|
|
16
|
-
done('error!');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
hooks.post('cook', function(obj) {
|
|
20
|
-
obj.tofu = 'no';
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
24
|
-
|
|
25
|
-
var args = [obj];
|
|
26
|
-
args.push(function(error, result) {
|
|
27
|
-
assert.equal('error!', error);
|
|
28
|
-
assert.ok(!result);
|
|
29
|
-
assert.equal(undefined, obj.tofu);
|
|
30
|
-
done();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
hooks.wrap(
|
|
34
|
-
'cook',
|
|
35
|
-
function(o, callback) {
|
|
36
|
-
// Should never get called
|
|
37
|
-
assert.ok(false);
|
|
38
|
-
callback(null, o);
|
|
39
|
-
},
|
|
40
|
-
obj,
|
|
41
|
-
args);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('handles pre errors when no callback defined', function(done) {
|
|
45
|
-
hooks.pre('cook', function(done) {
|
|
46
|
-
done('error!');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
hooks.post('cook', function(obj) {
|
|
50
|
-
obj.tofu = 'no';
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
54
|
-
|
|
55
|
-
var args = [obj];
|
|
56
|
-
|
|
57
|
-
hooks.wrap(
|
|
58
|
-
'cook',
|
|
59
|
-
function(o, callback) {
|
|
60
|
-
// Should never get called
|
|
61
|
-
assert.ok(false);
|
|
62
|
-
callback(null, o);
|
|
63
|
-
},
|
|
64
|
-
obj,
|
|
65
|
-
args);
|
|
66
|
-
|
|
67
|
-
setTimeout(
|
|
68
|
-
function() {
|
|
69
|
-
done();
|
|
70
|
-
},
|
|
71
|
-
25);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('handles errors in wrapped function', function(done) {
|
|
75
|
-
hooks.pre('cook', function(done) {
|
|
76
|
-
done();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
hooks.post('cook', function(obj) {
|
|
80
|
-
obj.tofu = 'no';
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
84
|
-
|
|
85
|
-
var args = [obj];
|
|
86
|
-
args.push(function(error, result) {
|
|
87
|
-
assert.equal('error!', error);
|
|
88
|
-
assert.ok(!result);
|
|
89
|
-
assert.equal(undefined, obj.tofu);
|
|
90
|
-
done();
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
hooks.wrap(
|
|
94
|
-
'cook',
|
|
95
|
-
function(o, callback) {
|
|
96
|
-
callback('error!');
|
|
97
|
-
},
|
|
98
|
-
obj,
|
|
99
|
-
args);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('handles errors in post', function(done) {
|
|
103
|
-
hooks.pre('cook', function(done) {
|
|
104
|
-
done();
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
hooks.post('cook', function(obj, callback) {
|
|
108
|
-
obj.tofu = 'no';
|
|
109
|
-
callback('error!');
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
113
|
-
|
|
114
|
-
var args = [obj];
|
|
115
|
-
args.push(function(error, result) {
|
|
116
|
-
assert.equal('error!', error);
|
|
117
|
-
assert.ok(!result);
|
|
118
|
-
assert.equal('no', obj.tofu);
|
|
119
|
-
done();
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
hooks.wrap(
|
|
123
|
-
'cook',
|
|
124
|
-
function(o, callback) {
|
|
125
|
-
callback(null, o);
|
|
126
|
-
},
|
|
127
|
-
obj,
|
|
128
|
-
args);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('defers errors to post hooks if enabled', function(done) {
|
|
132
|
-
hooks.pre('cook', function(done) {
|
|
133
|
-
done(new Error('fail'));
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
hooks.post('cook', function(error, res, callback) {
|
|
137
|
-
callback(new Error('another error occurred'));
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
var args = [];
|
|
141
|
-
args.push(function(error) {
|
|
142
|
-
assert.equal(error.message, 'another error occurred');
|
|
143
|
-
done();
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
hooks.wrap(
|
|
147
|
-
'cook',
|
|
148
|
-
function(callback) {
|
|
149
|
-
assert.ok(false);
|
|
150
|
-
callback();
|
|
151
|
-
},
|
|
152
|
-
null,
|
|
153
|
-
args,
|
|
154
|
-
{ useErrorHandlers: true, numCallbackParams: 1 });
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('error handlers with no callback', function(done) {
|
|
158
|
-
hooks.pre('cook', function(done) {
|
|
159
|
-
done(new Error('fail'));
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
hooks.post('cook', function(error, callback) {
|
|
163
|
-
assert.equal(error.message, 'fail');
|
|
164
|
-
done();
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
var args = [];
|
|
168
|
-
|
|
169
|
-
hooks.wrap(
|
|
170
|
-
'cook',
|
|
171
|
-
function(callback) {
|
|
172
|
-
assert.ok(false);
|
|
173
|
-
callback();
|
|
174
|
-
},
|
|
175
|
-
null,
|
|
176
|
-
args,
|
|
177
|
-
{ useErrorHandlers: true });
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('error handlers with no error', function(done) {
|
|
181
|
-
hooks.post('cook', function(error, callback) {
|
|
182
|
-
callback(new Error('another error occurred'));
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
var args = [];
|
|
186
|
-
args.push(function(error) {
|
|
187
|
-
assert.ifError(error);
|
|
188
|
-
done();
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
hooks.wrap(
|
|
192
|
-
'cook',
|
|
193
|
-
function(callback) {
|
|
194
|
-
callback();
|
|
195
|
-
},
|
|
196
|
-
null,
|
|
197
|
-
args,
|
|
198
|
-
{ useErrorHandlers: true });
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('works with no args', function(done) {
|
|
202
|
-
hooks.pre('cook', function(done) {
|
|
203
|
-
done();
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
hooks.post('cook', function(callback) {
|
|
207
|
-
obj.tofu = 'no';
|
|
208
|
-
callback();
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
212
|
-
|
|
213
|
-
var args = [];
|
|
214
|
-
|
|
215
|
-
hooks.wrap(
|
|
216
|
-
'cook',
|
|
217
|
-
function(callback) {
|
|
218
|
-
callback(null);
|
|
219
|
-
},
|
|
220
|
-
obj,
|
|
221
|
-
args);
|
|
222
|
-
|
|
223
|
-
setTimeout(
|
|
224
|
-
function() {
|
|
225
|
-
assert.equal('no', obj.tofu);
|
|
226
|
-
done();
|
|
227
|
-
},
|
|
228
|
-
25);
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it('handles pre errors with no args', function(done) {
|
|
232
|
-
hooks.pre('cook', function(done) {
|
|
233
|
-
done('error!');
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
hooks.post('cook', function(callback) {
|
|
237
|
-
obj.tofu = 'no';
|
|
238
|
-
callback();
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
242
|
-
|
|
243
|
-
var args = [];
|
|
244
|
-
|
|
245
|
-
hooks.wrap(
|
|
246
|
-
'cook',
|
|
247
|
-
function(callback) {
|
|
248
|
-
callback(null);
|
|
249
|
-
},
|
|
250
|
-
obj,
|
|
251
|
-
args);
|
|
252
|
-
|
|
253
|
-
setTimeout(
|
|
254
|
-
function() {
|
|
255
|
-
assert.equal(undefined, obj.tofu);
|
|
256
|
-
done();
|
|
257
|
-
},
|
|
258
|
-
25);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it('handles wrapped function errors with no args', function(done) {
|
|
262
|
-
hooks.pre('cook', function(done) {
|
|
263
|
-
obj.waffles = false;
|
|
264
|
-
done();
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
hooks.post('cook', function(callback) {
|
|
268
|
-
obj.tofu = 'no';
|
|
269
|
-
callback();
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
273
|
-
|
|
274
|
-
var args = [];
|
|
275
|
-
|
|
276
|
-
hooks.wrap(
|
|
277
|
-
'cook',
|
|
278
|
-
function(callback) {
|
|
279
|
-
callback('error!');
|
|
280
|
-
},
|
|
281
|
-
obj,
|
|
282
|
-
args);
|
|
283
|
-
|
|
284
|
-
setTimeout(
|
|
285
|
-
function() {
|
|
286
|
-
assert.equal(false, obj.waffles);
|
|
287
|
-
assert.equal(undefined, obj.tofu);
|
|
288
|
-
done();
|
|
289
|
-
},
|
|
290
|
-
25);
|
|
291
|
-
});
|
|
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
|
-
|
|
378
|
-
it('handles post errors with no args', function(done) {
|
|
379
|
-
hooks.pre('cook', function(done) {
|
|
380
|
-
obj.waffles = false;
|
|
381
|
-
done();
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
hooks.post('cook', function(callback) {
|
|
385
|
-
obj.tofu = 'no';
|
|
386
|
-
callback('error!');
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
var obj = { bacon: 0, eggs: 0 };
|
|
390
|
-
|
|
391
|
-
var args = [];
|
|
392
|
-
|
|
393
|
-
hooks.wrap(
|
|
394
|
-
'cook',
|
|
395
|
-
function(callback) {
|
|
396
|
-
callback();
|
|
397
|
-
},
|
|
398
|
-
obj,
|
|
399
|
-
args);
|
|
400
|
-
|
|
401
|
-
setTimeout(
|
|
402
|
-
function() {
|
|
403
|
-
assert.equal(false, obj.waffles);
|
|
404
|
-
assert.equal('no', obj.tofu);
|
|
405
|
-
done();
|
|
406
|
-
},
|
|
407
|
-
25);
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
it('catches sync errors', function(done) {
|
|
411
|
-
hooks.pre('cook', function(done) {
|
|
412
|
-
done();
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
hooks.post('cook', function(callback) {
|
|
416
|
-
callback();
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
var args = [];
|
|
420
|
-
args.push(function(error) {
|
|
421
|
-
assert.equal(error.message, 'oops!');
|
|
422
|
-
done();
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
hooks.wrap(
|
|
426
|
-
'cook',
|
|
427
|
-
function() {
|
|
428
|
-
throw new Error('oops!');
|
|
429
|
-
},
|
|
430
|
-
null,
|
|
431
|
-
args);
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
it('sync wrappers', function() {
|
|
435
|
-
var calledPre = 0;
|
|
436
|
-
var calledFn = 0;
|
|
437
|
-
var calledPost = 0;
|
|
438
|
-
hooks.pre('cook', function() {
|
|
439
|
-
++calledPre;
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
hooks.post('cook', function() {
|
|
443
|
-
++calledPost;
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
var wrapper = hooks.createWrapperSync('cook', function() { ++calledFn; });
|
|
447
|
-
|
|
448
|
-
wrapper();
|
|
449
|
-
|
|
450
|
-
assert.equal(calledPre, 1);
|
|
451
|
-
assert.equal(calledFn, 1);
|
|
452
|
-
assert.equal(calledPost, 1);
|
|
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
|
-
});
|
|
467
|
-
});
|