1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var Route = require('./route'),
|
9 | Context = require('./context'),
|
10 | events = require('events'),
|
11 | plugins = ['request', 'static', 'compression', 'shortcuts', 'access', 'core'];
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function Crixalis () {
|
18 | if (!(this instanceof Crixalis)) {
|
19 | return new Crixalis();
|
20 | }
|
21 |
|
22 | var self = this,
|
23 | routes = [],
|
24 | views = Object.create(null),
|
25 | features = Object.create(null);
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function define (name, value, descriptor) {
|
36 | var target = self;
|
37 |
|
38 | descriptor = descriptor || {};
|
39 | descriptor.value = value || {};
|
40 | descriptor.enumerable = typeof value !== 'function';
|
41 |
|
42 | if (/^(view|feature)::(\w+)$/.exec(name)) {
|
43 | name = RegExp.$2;
|
44 | target = { view: views, feature: features }[RegExp.$1];
|
45 | }
|
46 |
|
47 | Object.defineProperty(target, name, descriptor);
|
48 |
|
49 | return this;
|
50 | }
|
51 |
|
52 | define('define', define);
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | this.define('plugin', function (plugin, options) {
|
62 | var loader;
|
63 |
|
64 | if (~plugins.indexOf(plugin)) {
|
65 | loader = require('./plugins/' + plugin);
|
66 | } else {
|
67 | loader = require(plugin);
|
68 | }
|
69 |
|
70 | if (typeof loader !== 'function') {
|
71 | throw new Error('Plugin returned unexpected value');
|
72 | }
|
73 |
|
74 | loader.call(this, options);
|
75 |
|
76 | return this;
|
77 | });
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | this.define('route', function (source, options, callback) {
|
88 | if (arguments.length == 2) {
|
89 | callback = options,
|
90 | options = undefined;
|
91 | }
|
92 |
|
93 | if (typeof callback !== 'function') {
|
94 | throw new Error('Callback must be a function');
|
95 | }
|
96 |
|
97 | routes.push([transformRoute(new Route(source, options)), callback]);
|
98 |
|
99 | return this;
|
100 | });
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | this.define('select', function () {
|
114 | var i, l;
|
115 |
|
116 | for (i = 0, l = routes.length; i < l; i++) {
|
117 |
|
118 | if (routes[i][0].match(this)) {
|
119 |
|
120 | if (routes[i][1].call(this) === false) {
|
121 |
|
122 | continue;
|
123 | }
|
124 |
|
125 | return;
|
126 | }
|
127 | }
|
128 |
|
129 |
|
130 | this.error(404);
|
131 | });
|
132 |
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 | this.define('render', function (view) {
|
139 | var fn = views[view || 'auto'];
|
140 |
|
141 | if (typeof fn === 'function') {
|
142 | if (fn.call(this) === false) {
|
143 |
|
144 | return;
|
145 | }
|
146 | } else {
|
147 | throw new Error('Unsupported view ' + view);
|
148 | }
|
149 |
|
150 | if (this.body && this.has('compression')) {
|
151 | |
152 |
|
153 |
|
154 |
|
155 | this.emit('compression');
|
156 | } else {
|
157 | this.sendResponse();
|
158 | }
|
159 | });
|
160 |
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | this.define('createContext', function (request, response) {
|
169 | Context.prototype = this;
|
170 |
|
171 | return new Context(request, response);
|
172 | });
|
173 |
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 | this.define('has', function (feature) {
|
180 | return feature in features;
|
181 | });
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 | this.on('auto', function () {
|
188 | this.select()
|
189 | });
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 | this.on('error', function (error) {
|
197 | var types = this.types,
|
198 | i, l;
|
199 |
|
200 | this.logger.error(error);
|
201 |
|
202 | for (i = 0, l = types.length; i < l; i++) {
|
203 | switch (types[i]) {
|
204 | case 'application/json':
|
205 | this.stash.json = {
|
206 | error: {
|
207 | message: error.message
|
208 | }
|
209 | };
|
210 | break;
|
211 |
|
212 | case '*/*':
|
213 | case 'text/*':
|
214 | case 'text/html':
|
215 | this.body = error.message;
|
216 | break;
|
217 |
|
218 | default:
|
219 | continue;
|
220 | }
|
221 |
|
222 |
|
223 | break;
|
224 | }
|
225 |
|
226 | if (i === l) {
|
227 |
|
228 | this.body = error.message;
|
229 | }
|
230 |
|
231 | this.code = this.code || 500;
|
232 | this.render();
|
233 | });
|
234 |
|
235 |
|
236 | setupDefaultListeners(this, ['auto', 'error']);
|
237 |
|
238 |
|
239 | this.plugin('core');
|
240 |
|
241 | return this;
|
242 | }
|
243 |
|
244 | Crixalis.prototype = Object.create(events.EventEmitter.prototype, {
|
245 | constructor: { value: Crixalis },
|
246 |
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 | version: { value: require('../package').version, enumerable: true },
|
253 |
|
254 | |
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | signature: { value: 'Crixalis', enumerable: true, writable: true },
|
261 |
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | postSize: { value: 1048576, enumerable: true, writable: true },
|
269 |
|
270 | |
271 |
|
272 |
|
273 |
|
274 |
|
275 | logger: { value: console, enumerable: true, writable: true },
|
276 |
|
277 | |
278 |
|
279 |
|
280 |
|
281 |
|
282 | code: { value: 0, enumerable: true, writable: true },
|
283 |
|
284 | |
285 |
|
286 |
|
287 |
|
288 |
|
289 | body: { value: '', enumerable: true, writable: true }
|
290 | });
|
291 |
|
292 | function setupDefaultListeners (emitter, events) {
|
293 | events.forEach(function (defaultEvent) {
|
294 | function handleNewListener (event) {
|
295 | if (defaultEvent === event) {
|
296 | emitter.removeListener(event, (emitter.listeners(event))[0]);
|
297 | emitter.removeListener('newListener', handleNewListener);
|
298 | }
|
299 | }
|
300 |
|
301 | emitter.on('newListener', handleNewListener);
|
302 | });
|
303 | }
|
304 |
|
305 | function transformRoute (route, callback) {
|
306 | var i = 1;
|
307 |
|
308 | if (route.url) {
|
309 | route.mapping = Object.create(null);
|
310 |
|
311 | route.pattern = new RegExp('^' + route.url.split(/([:]\w+|[*])/).map(function (part) {
|
312 | if (part[0] === ':') {
|
313 | route.mapping['$' + i++] = part.slice(1);
|
314 |
|
315 | return '([^/.]+)';
|
316 | } else if (part === '*') {
|
317 | return '(?:.+)';
|
318 | }
|
319 |
|
320 | return part.replace(/[\/()\[\]{}?^$+*.,]/g, function (char) { return '\\' + char });
|
321 | }).join('') + '$', 'i');
|
322 |
|
323 | route.unset('url');
|
324 | }
|
325 |
|
326 | if (!(route.pattern instanceof RegExp)) {
|
327 | throw new Error('Nothing to route');
|
328 | }
|
329 |
|
330 | return route;
|
331 | }
|
332 |
|
333 | module.exports = Crixalis;
|