1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const _ = require('underscore');
|
8 | const express = require('express');
|
9 | const domain = require('domain');
|
10 | const url = require('url');
|
11 | const yieldable = require('abacus-yieldable');
|
12 | const transform = require('abacus-transform');
|
13 |
|
14 | const toArray = _.toArray;
|
15 | const map = _.map;
|
16 | const extend = _.extend;
|
17 | const omit = _.omit;
|
18 | const pick = _.pick;
|
19 | const without = _.without;
|
20 | const allKeys = _.allKeys;
|
21 |
|
22 |
|
23 | const debug = require('abacus-debug')('abacus-router');
|
24 | const edebug = require('abacus-debug')('e-abacus-router');
|
25 |
|
26 |
|
27 |
|
28 | const callbackify = (m, trusted) => {
|
29 |
|
30 |
|
31 |
|
32 | const mfunc = yieldable.functioncb(m);
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | return function() {
|
38 | const next = arguments[arguments.length - 1];
|
39 | const res = arguments[1];
|
40 | const params = toArray(arguments).slice(0, arguments.length - 1);
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const error = (err, type) => {
|
46 | edebug('Route error - %s - %o', type, err);
|
47 | debug('Route error - %s - %o', type, err);
|
48 | if(!trusted && !err.status && !err.statusCode)
|
49 | err.bailout = true;
|
50 | next(err);
|
51 | };
|
52 |
|
53 |
|
54 | try {
|
55 | mfunc.apply(undefined, params.concat([(err, value) => {
|
56 | if(err) error(err, 'generator error');
|
57 | else if(value)
|
58 |
|
59 |
|
60 |
|
61 | res.value = value;
|
62 | next();
|
63 | }]));
|
64 | }
|
65 | catch (exc) {
|
66 | error(exc, 'exception');
|
67 | }
|
68 | };
|
69 | };
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | const use = (original, trusted) => {
|
75 | return function(path, m) {
|
76 | return typeof path === 'function' ?
|
77 | original.call(this, callbackify(path, trusted)) :
|
78 | original.call(this, path, callbackify(m, trusted));
|
79 | };
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 | const route = (original, trusted) => {
|
85 | return function() {
|
86 |
|
87 | const r = original.apply(this, arguments);
|
88 |
|
89 |
|
90 | map(['get', 'head', 'post', 'put', 'patch', 'delete', 'options', 'all',
|
91 | 'use'
|
92 | ], (method) => {
|
93 | const f = r[method];
|
94 | r[method] = function() {
|
95 | const middleware = map(toArray(arguments), function(m) {
|
96 | return callbackify(m, trusted);
|
97 | });
|
98 | return f.apply(this, middleware);
|
99 | };
|
100 | });
|
101 | return r;
|
102 | };
|
103 | };
|
104 |
|
105 |
|
106 |
|
107 | const catchall = (trusted) => {
|
108 | return (req, res, next) => {
|
109 | const d = domain.create();
|
110 | d.on('error', (err) => {
|
111 | debug('Route domain error %o', err);
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | if(!trusted && !err.status && !err.statusCode)
|
118 | err.bailout = true;
|
119 | next(err);
|
120 | });
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | d.add(req);
|
126 | d.add(res);
|
127 |
|
128 |
|
129 | d.run(next);
|
130 | };
|
131 | };
|
132 |
|
133 |
|
134 | const router = (trusted) => {
|
135 | const r = express.Router();
|
136 |
|
137 |
|
138 | r.use(catchall(trusted));
|
139 |
|
140 |
|
141 |
|
142 | r.use = use(r.use, trusted);
|
143 | r.route = route(r.route, trusted);
|
144 |
|
145 | return r;
|
146 | };
|
147 |
|
148 |
|
149 |
|
150 | const batch = (routes) => {
|
151 | return (req, res, next) => {
|
152 | if(req.method !== 'POST' || req.url !== '/batch')
|
153 | return next();
|
154 | debug('Handling batch request %o', req.body);
|
155 |
|
156 |
|
157 | transform.map(req.body, (r, i, reqs, rcb) => {
|
158 |
|
159 | const path = url.resolve(req.url, r.uri);
|
160 | const rreq = extend({}, pick(req, without(allKeys(req), 'host')), {
|
161 | method: r.method,
|
162 | url: path,
|
163 | path: path,
|
164 | body: r.body
|
165 | });
|
166 |
|
167 |
|
168 | const rres = extend({}, res, {
|
169 | status: (s) => {
|
170 | rres.statusCode = s;
|
171 | return rres;
|
172 | },
|
173 | header: (k, v) => {
|
174 | if (!rres.header) rres.header = {};
|
175 | rres.header[k] = v;
|
176 | return rres;
|
177 | },
|
178 | send: (b) => {
|
179 | rres.body = b;
|
180 | return rres.end();
|
181 | },
|
182 | json: (b) => {
|
183 | rres.body = b;
|
184 | return rres.end();
|
185 | },
|
186 | end: () => {
|
187 | rcb(undefined, {
|
188 | statusCode: rres.statusCode,
|
189 | header: omit(rres.header, 'setHeader'),
|
190 | body: rres.body
|
191 | });
|
192 | return rres;
|
193 | }
|
194 | });
|
195 |
|
196 |
|
197 | routes(rreq, rres, (err) => {
|
198 | if (err) return rcb(err);
|
199 |
|
200 | if(rres.value) return rcb(undefined, rres.value);
|
201 | next();
|
202 | });
|
203 |
|
204 | }, (err, bres) => {
|
205 | if(err) return res.status(500).end();
|
206 |
|
207 |
|
208 | res.send(bres);
|
209 | });
|
210 | };
|
211 | };
|
212 |
|
213 |
|
214 | module.exports = router;
|
215 | module.exports.batch = batch;
|
216 |
|