-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
208 lines (174 loc) · 4.42 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
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
'use strict';
var dollars = require('dollars')
, display = require('fn.name');
/**
* Representation of a single middleware layer.
*
* @constructor
* @param {String} name Identification of the middleware.
* @param {Function} fn Middleware function.
* @param {Mixed} context Execution context of the function.
* @api private
*/
function Layer(name, fn) {
this.length = fn.length;
this.name = name;
this.fn = fn;
}
/**
* A minimal middleware layer system.
*
* @constructor
* @param {EventEmitter} provider EventEmitter instance.
* @param {Object} options Optional options.
* @api public
*/
function Supply(provider, options) {
if (!this) return new Supply(provider, options);
options = options || {};
this.provider = provider || this;
this.layers = [];
this.length = 0;
if ('function' === typeof this.initialize) {
this.initialize(options);
}
}
Supply.extend = require('extendible');
Supply.Layer = Layer;
/**
* Removes the middleware from the stack.
*
* @param {String} name Name of the layer we wish to remove.
* @returns {Boolean} Indication of successful removal.
* @api public
*/
Supply.prototype.remove = function remove(name) {
var i = this.indexOf(name)
, layer;
if (i === -1) return false;
layer = this.layers.splice(i, 1)[0];
this.length--;
if (this.provider.emit) {
this.provider.emit('remove', layer);
}
return true;
};
/**
* Add a new middleware layer at the beginning of the stack.
*
* @param {String} name Middleware name
* @param {Function} fn Function to execute
* @param {Object} opts Additional middleware configuration.
* @returns {Provider|Supply}
* @api public
*/
Supply.prototype.before = function before(name, fn, opts) {
if ('function' === typeof name) {
fn = name;
name = display(name);
}
return this.use(name, fn, dollars.object.concat(opts || {}, {
at: 0
}));
};
/**
* Add a new middleware layer to the stack.
*
* @param {String} name Middleware name
* @param {Function} fn Function to execute
* @param {Object} opts Additional middleware configuration.
* @returns {Provider|Supply}
* @api public
*/
Supply.prototype.use = function use(name, fn, opts) {
if ('function' === typeof name) {
fn = name;
name = display(name);
}
opts = dollars.object.concat({
at: this.layers.length
}, opts || {});
if ('string' === typeof opts.at) opts.at = this.indexOf(opts.at);
if (opts.at > this.layers.length) opts.at = this.layers.length;
if (opts.at < 0) opts.at = 0;
var layer = new Layer(name, fn, opts);
this.layers.splice(opts.at, 0, layer);
this.length++;
if (this.provider.emit) {
this.provider.emit('use', layer);
}
return this.provider;
};
/**
* Find the index an object which has the given name.
*
* @param {Array} array Array to search for index.
* @param {String} name Name we should match.
* @returns {Number} Index.
* @api private
*/
Supply.prototype.indexOf = function index(name) {
for (var i = 0, l = this.layers.length; i < l; i++) {
if (name === this.layers[i].name) return i;
}
return -1;
};
/**
* Iterate over all existing middleware layers.
*
* @returns {Supply}
* @api public
*/
Supply.prototype.each = function each() {
var length = arguments.length
, args = new Array(length)
, fn = dollars.nope
, supply = this
, i = 0;
//
// Create a copy of the arguments to prevent it from leaking.
//
for (; i < length; i++) {
args[i] = arguments[i];
}
if ('function' === typeof args[args.length - 1]) fn = args.pop();
/**
* Simple middleware layer iterator.
*
* @param {Error} err A failed middleware iteration.
* @param {Boolean} done Stop iterating the layers as we are done.
* @api private
*/
function next(err, done) {
var layer = supply.layers[i++];
if (err || done || !layer) {
return fn(err, !!done);
}
if (layer.length > length) {
return layer.fn.apply(supply.provider, args.concat(next));
} else {
dollars.catch(function catching() {
return layer.fn.apply(supply.provider, args);
}, next);
}
}
length = args.length;
i = 0;
next();
return this.provider;
};
/**
* Completely destroy the instance and nuke all references.
*
* @returns {Boolean}
* @api public
*/
Supply.prototype.destroy = function supply() {
if (!this.layers) return false;
this.layers = this.provider = null;
return true;
};
//
// Expose the middleware layer.
//
module.exports = Supply;