1 | const http = require('http');
|
2 | const Router = require('trouter');
|
3 | const parseurl = require('parseurl');
|
4 | const { parse } = require('querystring');
|
5 |
|
6 | function lead(x) {
|
7 | return x.charCodeAt(0) === 47 ? x : ('/' + x);
|
8 | }
|
9 |
|
10 | function value(x) {
|
11 | let y = x.indexOf('/', 1);
|
12 | return y > 1 ? x.substring(0, y) : x;
|
13 | }
|
14 |
|
15 | function mutate(str, req) {
|
16 | req.url = req.url.substring(str.length) || '/';
|
17 | req.pathname = req.pathname.substring(str.length) || '/';
|
18 | }
|
19 |
|
20 | function isRootWild(obj) {
|
21 | return Object.keys(obj).length === 1 && obj['*'];
|
22 | }
|
23 |
|
24 | function onError(err, req, res, next) {
|
25 | let code = (res.statusCode = err.code || err.status || 500);
|
26 | res.end(err.length && err || err.message || http.STATUS_CODES[code]);
|
27 | }
|
28 |
|
29 | class Polka extends Router {
|
30 | constructor(opts={}) {
|
31 | super(opts);
|
32 | this.apps = {};
|
33 | this.wares = [];
|
34 | this.bwares = {};
|
35 | this.parse = parseurl;
|
36 | this.handler = this.handler.bind(this);
|
37 | this.server = http.createServer(this.handler);
|
38 | this.onError = opts.onError || onError;
|
39 | this.onNoMatch = opts.onNoMatch || this.onError.bind(null, { code:404 });
|
40 | }
|
41 |
|
42 | use(base, ...fns) {
|
43 | if (typeof base === 'function') {
|
44 | this.wares = this.wares.concat(base, fns);
|
45 | } else {
|
46 | base = lead(base);
|
47 | fns.forEach(fn => {
|
48 | if (fn instanceof Polka) {
|
49 | this.apps[base] = fn;
|
50 | } else {
|
51 | let arr = this.bwares[base] || [];
|
52 | (arr.length === 0) && arr.push((r, _, nxt) => (mutate(base, r),nxt()));
|
53 | this.bwares[base] = arr.concat(fn);
|
54 | }
|
55 | });
|
56 | }
|
57 | return this;
|
58 | }
|
59 |
|
60 | listen(port, hostname) {
|
61 | return new Promise((res, rej) => {
|
62 | this.server.listen(port, hostname, err => err ? rej(err) : res());
|
63 | });
|
64 | }
|
65 |
|
66 | handler(req, res, info) {
|
67 | info = info || this.parse(req);
|
68 | let fn, arr=this.wares, obj=this.find(req.method, info.pathname);
|
69 | req.originalUrl = req.originalUrl || req.url;
|
70 | req.pathname = info.pathname;
|
71 | if (obj) {
|
72 | fn = obj.handler;
|
73 | req.params = obj.params;
|
74 | }
|
75 | if (!obj || isRootWild(obj.params)) {
|
76 |
|
77 | let base = value(req.pathname);
|
78 | if (this.apps[base] !== void 0) {
|
79 | mutate(base, req); info.pathname=req.pathname;
|
80 | fn = this.apps[base].handler.bind(null, req, res, info);
|
81 | } else {
|
82 | fn = fn || this.onNoMatch;
|
83 | if (this.bwares[base] !== void 0) {
|
84 | arr = arr.concat(this.bwares[base]);
|
85 | }
|
86 | }
|
87 | }
|
88 |
|
89 | req.search = info.search;
|
90 | req.query = parse(info.query);
|
91 |
|
92 | let i=0, len=arr.length;
|
93 | if (len === i) return fn(req, res);
|
94 |
|
95 | let next = err => err ? this.onError(err, req, res, next) : loop();
|
96 | let loop = _ => res.finished || (i < len) ? arr[i++](req, res, next) : fn(req, res);
|
97 | loop();
|
98 | }
|
99 | }
|
100 |
|
101 | module.exports = opts => new Polka(opts);
|