-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
113 lines (111 loc) · 3.12 KB
/
index.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
var noop = require('101/noop');
var flow = module.exports = {};
flow.series = require('./lib/series');
flow.parallel = require('./lib/parallel');
flow.parallelWait = require('./lib/parallel-wait');
flow.or = require('./lib/or');
flow.each = require('./lib/each');
flow.next = function (req, res, next) {
next();
};
flow._execConditional = function (conditional) {
return function (req, res, next) {
try {
if (conditional.type === 'middleware') {
conditional.if(req, res, function (err) {
async(err, !err);
});
}
else if (conditional.type === 'async') {
conditional.if(req, res, async);
}
else if (conditional.type === 'sync') {
sync(conditional.if(req, res));
}
else { // if (conditional.type === 'value') {
sync(conditional.if);
}
}
catch (err) {
async(err, !err, true);
}
function sync (result) {
if (result) {
flow.series.apply(null, conditional.then)(req, res, next);
}
else {
flow.series.apply(null, conditional.else)(req, res, next);
}
}
function async (err, result, uncaught) {
if (err) {
if (uncaught || conditional.type !== 'middleware') {
next(err);
}
else { // if (conditional.type === 'middleware') {
if (conditional.else && conditional.else[0]) {
if (conditional.else[0].length === 4) {
var newElse = conditional.else.slice(); // shallow clone
newElse[0] = newElse[0].bind(null, err);
flow.series.apply(null, newElse)(req, res, next);
}
else {
flow.series.apply(null, conditional.else)(req, res, next);
}
}
else {
next();
}
}
}
else if (result) {
flow.series.apply(null, conditional.then)(req, res, next);
}
else if (conditional.else && conditional.else[0]) {
flow.series.apply(null, conditional.else)(req, res, next);
}
else {
next();
}
}
};
};
flow.conditional = function (type, test) {
var conditional = { // eslint-disable-line no-reserved-keys
type: type,
if: test
};
return thenAndElse(flow._execConditional(conditional), conditional);
};
flow.mwIf = function (mw) {
return flow.conditional('middleware', mw);
};
flow.syncIf = function (mw) {
return flow.conditional('sync', mw);
};
flow.asyncIf = function (mw) {
return flow.conditional('async', mw);
};
flow.if = function (val) {
return flow.conditional('value', val);
};
flow.and = flow.series;
flow.try = require('./lib/try-catch')(flow.mwIf);
flow.bg = flow.background = function (/* middlewares */) {
var mw = flow.series.apply(flow, arguments);
return function (req, res, next) {
mw(req, res, noop);
next();
};
};
function thenAndElse (exec, conditional) {
exec.then = function (/* middlewares */) {
conditional.then = Array.prototype.slice.call(arguments);
return exec;
};
exec.else = function (/* middlewares */) {
conditional.else = Array.prototype.slice.call(arguments);
return exec;
};
return exec;
}