-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
330 lines (281 loc) · 8.95 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Router for Koa v2
*
* @author blaz <[email protected]>
* @link https://github.com/menems/koa-66
* @license MIT
*
*/
'use strict';
const debug = require('debug')('koa-66');
const pathToRegexp = require('path-to-regexp');
const compose = require('koa-compose');
const util = require('util');
const methods = [
'options',
'head',
'get',
'post',
'put',
'patch',
'delete'
];
/**
* Expose Koa66 class.
*/
class Koa66 {
/**
* Initialise a new Koa66
*
* @api public
*/
constructor() {
this.stacks = [];
this.methods = methods;
this.plugs = [];
}
/**
* Mount a Koa66 instance on a prefix path
*
* @todo: too memory expensive instance are clone
* maybe add a keep args to remove the router instance mounted!
*
* @param {string} prefix
* @param {Object} router Koa66 instance
* @return {Object} Koa66 instance
* @api public
*/
mount(prefix, router) {
if (typeof router != 'object')
throw new TypeError('require a Koa66 instance');
router.stacks.forEach(s => {
if (s.paramKey)
this.register(s.methods, prefix + s.path, s.paramKey, s.middleware);
else
this.register(s.methods, prefix + s.path, s.middleware);
});
return this;
}
/**
* Use given middleware before route callback
*
* @param {String|Function} path
* @param {Function} fn
* @return {Object} Koa66 instance
* @api public
*/
use() {
const args = Array.prototype.slice.call(arguments);
if (typeof args[0] != 'string')
args.unshift('(.*)');
args.unshift(false);
return this.register.apply(this, args);
}
/**
* param express like function
*
* @param {String} key
* @param {Function} fn
* @return {Object}
*/
param(key, fn) {
if (typeof key != 'string' || typeof fn != 'function')
throw new TypeError('usage: param(string, function)');
return this.register(false, '(.*)', key, fn);
}
/**
* plugin registration method
*
* @param {String} name
* @param {[Function]...}
* @return {Object}
*/
plugin(name) {
if (typeof name != 'string')
throw new TypeError('usage: plugin(string, function)');
const args = Array.prototype.slice.call(arguments, 1);
let middlewares = [];
args.forEach(m => {
if (util.isArray(m)) middlewares = middlewares.concat(m);
if (typeof m == 'function') middlewares.push(m);
});
this.plugs[name] = compose(middlewares);
return this;
}
/**
* Expose middleware for koa
*
* @return {Function}
* @api public
*/
routes(options) {
options = options || {};
return (ctx, next) => {
const middlewares = [];
const allowed = [];
const paramMiddlewares = [];
let matched;
this.stacks.forEach(route => {
// path test
if (!route.regexp.test(ctx.path)) return;
// Save the route so we can access ctx.route.path
ctx.route = route;
if (route.paramNames)
ctx.params = this.parseParams(ctx.params, route.paramNames, ctx.path.match(route.regexp).slice(1));
if (route.paramKey) {
paramMiddlewares[route.paramKey] = (ctx, next) =>
route.middleware(ctx, next, ctx.params[route.paramKey]);
return;
}
if ( typeof route.middleware === 'object') {
for (let i in route.middleware) {
if ( this.plugs[i]) {
middlewares.push((ctx, next) => {
ctx.state.plugins = ctx.state.plugins || {};
ctx.state.plugins[i] = route.middleware[i];
return this.plugs[i](ctx, next);
});
}
}
return;
}
if (!route.methods)
return middlewares.push(route.middleware);
if (route.methods.indexOf('GET') !== -1)
allowed.push('HEAD');
route.methods.forEach(m => allowed.push(m));
// method test
if ((route.methods.indexOf(ctx.method) !== -1) ||
(ctx.method === 'HEAD' && route.methods.indexOf('GET') !== -1)) {
matched = true;
for (let i in ctx.params) {
if (paramMiddlewares[i])
middlewares.push(paramMiddlewares[i]);
delete paramMiddlewares[i]
}
middlewares.push(route.middleware);
}
});
// only use middleware
if (!allowed.length) return next();
// 501
if (this.methods.indexOf(ctx.method.toLowerCase()) === -1) {
if (options.throw)
ctx.throw(501);
ctx.status = 501;
return next();
}
// 405
if (!matched) {
// automatic OPTIONS response
if (ctx.method === 'OPTIONS') {
ctx.status = 204;
return next();
}
const _allowed = allowed.filter((value, index, self) => self.indexOf(value) === index).join(', ');
if (options.throw)
ctx.throw(405, {headers: {allow: _allowed}});
ctx.status = 405;
ctx.set('Allow', _allowed);
return next();
}
return compose(middlewares)(ctx, next);
};
}
/**
* Register a route for all methods.
*
* @param {String} path
* @return {[Functions]...} middleware
*/
all() {
const args = Array.prototype.slice.call(arguments);
if (typeof args[0] != 'string') args.unshift('/');
args.unshift(methods);
return this.register.apply(this, args);
}
sanitizePath(path) {
if (!path) return '/';
return '/' + path
.replace(/^\/+/i, '')
.replace(/\/+$/, '')
.replace(/\/{2,}/, '/');
}
/**
* Register a new middleware, http route or use middeware
*
* @param {string} methods
* @param {string} path
* @param {Function} middleware
* @return {Object} Koa66 instance
* @api private
*/
register(methods, path) {
debug('Register %s %s', methods, path);
let middlewares;
let paramKey;
if (typeof arguments[2] === 'string') {
paramKey = arguments[2];
middlewares = Array.prototype.slice.call(arguments, 3);
} else
middlewares = Array.prototype.slice.call(arguments, 2);
if (!middlewares.length)
throw new Error('middleware is required');
middlewares.forEach(m => {
if (Array.isArray(m)) {
m.forEach(_m => this.register(methods, path, _m));
return this;
}
if ( typeof m !== 'function' && typeof m !== 'object')
throw new TypeError('middleware must be a function');
const keys = [];
path = (!path || path === '(.*)' || util.isRegExp(path)) ? path : this.sanitizePath(path);
const regexp = pathToRegexp(path, keys);
const route = {
path: path,
middleware: m,
regexp: regexp,
paramNames: keys
};
if (paramKey) route.paramKey = paramKey;
if (methods) route.methods = methods.map((m) => m.toUpperCase());
this.stacks.push(route);
});
return this;
}
/**
* parse params from route
*
* @param {[Object]} paramNames
* @param {[String]} captures
* @return {[Object]}
* @api private
*/
parseParams(params, paramNames, captures) {
const len = captures.length;
params = params || {};
for (let i = 0; i < len; i++) {
if (paramNames[i]) {
let c = captures[i];
params[paramNames[i].name] = c ? decodeURIComponent(c) : c;
}
}
return params;
}
}
/**
* Add http methods
*
* @param {String} path
* @return {[Functions]...} middleware
*/
methods.forEach(method => {
Koa66.prototype[method] = function() {
const args = Array.prototype.slice.call(arguments);
if (typeof args[0] !== 'string')
args.unshift('/');
args.unshift([method]);
return this.register.apply(this, args);
};
});
module.exports = Koa66;